Rocco Rutte:
[apps/madmutt.git] / pgp.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5  * Copyright (C) 2004 g10 Code GmbH
6  *
7  * This file is part of mutt-ng, see http://www.muttng.org/.
8  * It's licensed under the GNU General Public License,
9  * please see the file GPL in the top level source directory.
10  */
11
12 /*
13  * This file contains all of the PGP routines necessary to sign, encrypt,
14  * verify and decrypt PGP messages in either the new PGP/MIME format, or
15  * in the older Application/Pgp format.  It also contains some code to
16  * cache the user's passphrase for repeat use when decrypting or signing
17  * a message.
18  */
19
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "mutt.h"
25 #include "enter.h"
26 #include "ascii.h"
27 #include "handler.h"
28 #include "mutt_curses.h"
29 #include "pgp.h"
30 #include "mime.h"
31 #include "copy.h"
32 #include "attach.h"
33
34 #include "lib/mem.h"
35 #include "lib/intl.h"
36 #include "lib/str.h"
37 #include "lib/debug.h"
38
39 #include <sys/wait.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <ctype.h>
46
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
50
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54
55 #ifdef HAVE_SYS_RESOURCE_H
56 # include <sys/resource.h>
57 #endif
58
59 #ifdef CRYPT_BACKEND_CLASSIC_PGP
60
61 #include "mutt_crypt.h"
62 #include "mutt_menu.h"
63
64
65 char PgpPass[LONG_STRING];
66 time_t PgpExptime = 0;          /* when does the cached passphrase expire? */
67
68 void pgp_void_passphrase (void)
69 {
70   memset (PgpPass, 0, sizeof (PgpPass));
71   PgpExptime = 0;
72 }
73
74 int pgp_valid_passphrase (void)
75 {
76   time_t now = time (NULL);
77
78   if (pgp_use_gpg_agent ()) {
79     *PgpPass = 0;
80     return 1;                   /* handled by gpg-agent */
81   }
82
83   if (now < PgpExptime)
84     /* Use cached copy.  */
85     return 1;
86
87   pgp_void_passphrase ();
88
89   if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
90                                  sizeof (PgpPass), M_PASS) == 0) {
91     PgpExptime = time (NULL) + PgpTimeout;
92     return (1);
93   }
94   else
95     PgpExptime = 0;
96
97   return 0;
98 }
99
100 void pgp_forget_passphrase (void)
101 {
102   pgp_void_passphrase ();
103   mutt_message _("PGP passphrase forgotten.");
104 }
105
106 int pgp_use_gpg_agent (void) {
107   char *tty;
108
109   if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
110     return 0;
111
112   if ((tty = ttyname(0)))
113     setenv("GPG_TTY", tty, 0);
114
115   return 1;
116 }
117
118 char *pgp_keyid (pgp_key_t k)
119 {
120   if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
121     k = k->parent;
122
123   return _pgp_keyid (k);
124 }
125
126 char *_pgp_keyid (pgp_key_t k)
127 {
128   if (option (OPTPGPLONGIDS))
129     return k->keyid;
130   else
131     return (k->keyid + 8);
132 }
133
134 /* ----------------------------------------------------------------------------
135  * Routines for handing PGP input.
136  */
137
138
139
140 /* Copy PGP output messages and look for signs of a good signature */
141
142 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
143 {
144   int rv = -1;
145
146   if (PgpGoodSign.pattern) {
147     char *line = NULL;
148     int lineno = 0;
149     size_t linelen;
150
151     while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
152       if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
153         debug_print (2, ("\"%s\" matches regexp.\n", line));
154         rv = 0;
155       }
156       else
157         debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
158
159       if (strncmp (line, "[GNUPG:] ", 9) == 0)
160         continue;
161       fputs (line, fpout);
162       fputc ('\n', fpout);
163     }
164     mem_free (&line);
165   }
166   else {
167     debug_print (2, ("No pattern.\n"));
168     mutt_copy_stream (fpin, fpout);
169     rv = 1;
170   }
171
172   return rv;
173 }
174
175 /* 
176  * Copy a clearsigned message, and strip the signature and PGP's
177  * dash-escaping.
178  * 
179  * XXX - charset handling: We assume that it is safe to do
180  * character set decoding first, dash decoding second here, while
181  * we do it the other way around in the main handler.
182  * 
183  * (Note that we aren't worse than Outlook &c in this, and also
184  * note that we can successfully handle anything produced by any
185  * existing versions of mutt.) 
186  */
187
188 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
189 {
190   char buf[HUGE_STRING];
191   short complete, armor_header;
192
193   FGETCONV *fc;
194
195   rewind (fpin);
196
197   fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
198
199   for (complete = 1, armor_header = 1;
200        fgetconvs (buf, sizeof (buf), fc) != NULL;
201        complete = strchr (buf, '\n') != NULL) {
202     if (!complete) {
203       if (!armor_header)
204         state_puts (buf, s);
205       continue;
206     }
207
208     if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
209       break;
210
211     if (armor_header) {
212       char *p = str_skip_initws (buf);
213
214       if (*p == '\0')
215         armor_header = 0;
216       continue;
217     }
218
219     if (s->prefix)
220       state_puts (s->prefix, s);
221
222     if (buf[0] == '-' && buf[1] == ' ')
223       state_puts (buf + 2, s);
224     else
225       state_puts (buf, s);
226   }
227
228   fgetconv_close (&fc);
229 }
230
231
232 /* Support for the Application/PGP Content Type. */
233
234 int pgp_application_pgp_handler (BODY * m, STATE * s)
235 {
236   int needpass = -1, pgp_keyblock = 0;
237   int c = 1;
238   int clearsign = 0, rv, rc;
239   long start_pos = 0;
240   long bytes, last_pos, offset;
241   char buf[HUGE_STRING];
242   char outfile[_POSIX_PATH_MAX];
243   char tmpfname[_POSIX_PATH_MAX];
244   FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
245   FILE *tmpfp = NULL;
246   pid_t thepid;
247
248   short maybe_goodsig = 1;
249   short have_any_sigs = 0;
250
251   char body_charset[STRING];
252
253   mutt_get_body_charset (body_charset, sizeof (body_charset), m);
254
255   rc = 0;                       /* silence false compiler warning if (s->flags & M_DISPLAY) */
256
257   fseek (s->fpin, m->offset, 0);
258   last_pos = m->offset;
259
260   for (bytes = m->length; bytes > 0;) {
261     if (fgets (buf, sizeof (buf), s->fpin) == NULL)
262       break;
263
264     offset = ftell (s->fpin);
265     bytes -= (offset - last_pos);       /* don't rely on str_len(buf) */
266     last_pos = offset;
267
268     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
269       clearsign = 0;
270       start_pos = last_pos;
271
272       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
273         needpass = 1;
274       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
275         clearsign = 1;
276         needpass = 0;
277       }
278       else if (!option (OPTDONTHANDLEPGPKEYS) &&
279                str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
280         needpass = 0;
281         pgp_keyblock = 1;
282       }
283       else {
284         /* XXX - we may wish to recode here */
285         if (s->prefix)
286           state_puts (s->prefix, s);
287         state_puts (buf, s);
288         continue;
289       }
290
291       have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
292
293       /* Copy PGP material to temporary file */
294       mutt_mktemp (tmpfname);
295       if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
296         mutt_perror (tmpfname);
297         return (-1);
298       }
299
300       fputs (buf, tmpfp);
301       while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
302         offset = ftell (s->fpin);
303         bytes -= (offset - last_pos);   /* don't rely on str_len(buf) */
304         last_pos = offset;
305
306         fputs (buf, tmpfp);
307
308         if ((needpass
309              && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
310             || (!needpass
311                 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
312                     || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
313                                     buf) == 0)))
314           break;
315       }
316
317       /* leave tmpfp open in case we still need it - but flush it! */
318       fflush (tmpfp);
319
320
321       /* Invoke PGP if needed */
322       if (!clearsign || (s->flags & M_VERIFY)) {
323         mutt_mktemp (outfile);
324         if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
325           mutt_perror (tmpfname);
326           return (-1);
327         }
328
329         if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
330                                          fileno (pgpout), -1, tmpfname,
331                                          needpass)) == -1) {
332           safe_fclose (&pgpout);
333           maybe_goodsig = 0;
334           pgpin = NULL;
335           pgperr = NULL;
336           state_attach_puts (_
337                              ("[-- Error: unable to create PGP subprocess! --]\n"),
338                              s);
339         }
340         else {                  /* PGP started successfully */
341
342           if (needpass) {
343             if (!pgp_valid_passphrase ())
344               pgp_void_passphrase ();
345             if (pgp_use_gpg_agent ())
346               *PgpPass = 0;
347             fprintf (pgpin, "%s\n", PgpPass);
348           }
349
350           safe_fclose (&pgpin);
351
352           if (s->flags & M_DISPLAY) {
353             crypt_current_time (s, "PGP");
354             rc = pgp_copy_checksig (pgperr, s->fpout);
355           }
356
357           safe_fclose (&pgperr);
358           rv = mutt_wait_filter (thepid);
359
360           if (s->flags & M_DISPLAY) {
361             if (rc == 0)
362               have_any_sigs = 1;
363
364             /*
365              * Sig is bad if
366              * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
367              * Sig _is_ correct if
368              * gpg_good_sign="" && pgp_decode_command returned 0
369              */
370             if (rc == -1 || rv)
371               maybe_goodsig = 0;
372
373             state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
374           }
375         }
376
377         /* treat empty result as sign of failure */
378         if (pgpout) {
379           rewind (pgpout);
380           c = fgetc (pgpout);
381           ungetc (c, pgpout);
382         }
383         if (!clearsign && (!pgpout || c == EOF)) {
384             mutt_error _("Could not decrypt PGP message");
385             mutt_sleep (1);
386             pgp_void_passphrase ();
387             rc = -1;
388             goto out;
389         }
390       }
391
392       /*
393        * Now, copy cleartext to the screen.  NOTE - we expect that PGP
394        * outputs utf-8 cleartext.  This may not always be true, but it 
395        * seems to be a reasonable guess.
396        */
397
398       if (s->flags & M_DISPLAY) {
399         if (needpass)
400           state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
401         else if (pgp_keyblock)
402           state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
403         else
404           state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
405       }
406
407       if (clearsign) {
408         rewind (tmpfp);
409         if (tmpfp)
410           pgp_copy_clearsigned (tmpfp, s, body_charset);
411       }
412       else if (pgpout) {
413         FGETCONV *fc;
414         int c;
415
416         rewind (pgpout);
417         state_set_prefix (s);
418         fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
419         while ((c = fgetconv (fc)) != EOF)
420           state_prefix_putc (c, s);
421         fgetconv_close (&fc);
422       }
423
424       if (s->flags & M_DISPLAY) {
425         state_putc ('\n', s);
426         if (needpass) {
427           state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
428           mutt_message _("PGP message successfully decrypted.");
429         }
430         else if (pgp_keyblock)
431           state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
432         else
433           state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
434       }
435
436     }
437     else {
438       /* XXX - we may wish to recode here */
439       if (s->prefix)
440         state_puts (s->prefix, s);
441       state_puts (buf, s);
442     }
443   }
444
445   rc = 0;
446
447 out:
448   m->goodsig = (maybe_goodsig && have_any_sigs);
449
450   if (tmpfp) {
451     safe_fclose (&tmpfp);
452     mutt_unlink (tmpfname);
453   }
454   if (pgpout) {
455     safe_fclose (&pgpout);
456     mutt_unlink (outfile);
457   }
458
459   if (needpass == -1) {
460     state_attach_puts (_
461                        ("[-- Error: could not find beginning of PGP message! --]\n\n"),
462                        s);
463     return (-1);
464   }
465
466   return (rc);
467 }
468
469 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
470                                            int tagged_only)
471 {
472   char tempfile[_POSIX_PATH_MAX];
473   char buf[HUGE_STRING];
474   FILE *tfp;
475
476   short sgn = 0;
477   short enc = 0;
478   short key = 0;
479
480   if (b->type != TYPETEXT)
481     return 0;
482
483   if (tagged_only && !b->tagged)
484     return 0;
485
486   mutt_mktemp (tempfile);
487   if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
488     unlink (tempfile);
489     return 0;
490   }
491
492   if ((tfp = fopen (tempfile, "r")) == NULL) {
493     unlink (tempfile);
494     return 0;
495   }
496
497   while (fgets (buf, sizeof (buf), tfp)) {
498     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
499       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
500         enc = 1;
501       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
502         sgn = 1;
503       else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
504         key = 1;
505     }
506   }
507   safe_fclose (&tfp);
508   unlink (tempfile);
509
510   if (!enc && !sgn && !key)
511     return 0;
512
513   /* fix the content type */
514
515   mutt_set_parameter ("format", "fixed", &b->parameter);
516   if (enc)
517     mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
518   else if (sgn)
519     mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
520   else if (key)
521     mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
522
523   return 1;
524 }
525
526 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
527 {
528   int rv = 0;
529   int r;
530
531   for (; b; b = b->next) {
532     if (is_multipart (b))
533       rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
534     else if (b->type == TYPETEXT) {
535       if ((r = mutt_is_application_pgp (b)))
536         rv = rv || r;
537       else
538         rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
539     }
540   }
541
542   return rv;
543 }
544
545
546
547
548
549 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
550 {
551   char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
552   FILE *fp, *pgpout, *pgperr;
553   pid_t thepid;
554   int badsig = -1;
555   int rv;
556
557   snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
558
559   if (!(fp = safe_fopen (sigfile, "w"))) {
560     mutt_perror (sigfile);
561     return -1;
562   }
563
564   fseek (s->fpin, sigbdy->offset, 0);
565   mutt_copy_bytes (s->fpin, fp, sigbdy->length);
566   fclose (fp);
567
568   mutt_mktemp (pgperrfile);
569   if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
570     mutt_perror (pgperrfile);
571     unlink (sigfile);
572     return -1;
573   }
574
575   crypt_current_time (s, "PGP");
576
577   if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
578                                    -1, -1, fileno (pgperr),
579                                    tempfile, sigfile)) != -1) {
580     if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
581       badsig = 0;
582
583
584     safe_fclose (&pgpout);
585     fflush (pgperr);
586     rewind (pgperr);
587
588     if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
589       badsig = 0;
590
591     if ((rv = mutt_wait_filter (thepid)))
592       badsig = -1;
593
594     debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
595   }
596
597   safe_fclose (&pgperr);
598
599   state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
600
601   mutt_unlink (sigfile);
602   mutt_unlink (pgperrfile);
603
604   debug_print (1, ("returning %d.\n", badsig));
605
606   return badsig;
607 }
608
609
610 /* Extract pgp public keys from messages or attachments */
611
612 void pgp_extract_keys_from_messages (HEADER * h)
613 {
614   int i;
615   char tempfname[_POSIX_PATH_MAX];
616   FILE *fpout;
617
618   if (h) {
619     mutt_parse_mime_message (Context, h);
620     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
621       return;
622   }
623
624   mutt_mktemp (tempfname);
625   if (!(fpout = safe_fopen (tempfname, "w"))) {
626     mutt_perror (tempfname);
627     return;
628   }
629
630   set_option (OPTDONTHANDLEPGPKEYS);
631
632   if (!h) {
633     for (i = 0; i < Context->vcount; i++) {
634       if (Context->hdrs[Context->v2r[i]]->tagged) {
635         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
636         if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
637             && !pgp_valid_passphrase ()) {
638           fclose (fpout);
639           goto bailout;
640         }
641         mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
642                            M_CM_DECODE | M_CM_CHARCONV, 0);
643       }
644     }
645   }
646   else {
647     mutt_parse_mime_message (Context, h);
648     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
649       fclose (fpout);
650       goto bailout;
651     }
652     mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
653   }
654
655   fclose (fpout);
656   mutt_endwin (NULL);
657   pgp_invoke_import (tempfname);
658   mutt_any_key_to_continue (NULL);
659
660 bailout:
661
662   mutt_unlink (tempfname);
663   unset_option (OPTDONTHANDLEPGPKEYS);
664
665 }
666
667 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
668 {
669   STATE s;
670   FILE *tempfp;
671   char tempfname[_POSIX_PATH_MAX];
672
673   mutt_mktemp (tempfname);
674   if (!(tempfp = safe_fopen (tempfname, "w"))) {
675     mutt_perror (tempfname);
676     return;
677   }
678
679   memset (&s, 0, sizeof (STATE));
680
681   s.fpin = fp;
682   s.fpout = tempfp;
683
684   mutt_body_handler (top, &s);
685
686   fclose (tempfp);
687
688   pgp_invoke_import (tempfname);
689   mutt_any_key_to_continue (NULL);
690
691   mutt_unlink (tempfname);
692 }
693
694 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
695 {
696   if (!fp) {
697     mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
698
699     return;
700   }
701
702   mutt_endwin (NULL);
703   set_option (OPTDONTHANDLEPGPKEYS);
704
705   for (; top; top = top->next) {
706     if (!tag || top->tagged)
707       pgp_extract_keys_from_attachment (fp, top);
708
709     if (!tag)
710       break;
711   }
712
713   unset_option (OPTDONTHANDLEPGPKEYS);
714 }
715
716 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
717 {
718   char buf[LONG_STRING];
719   FILE *pgpin, *pgpout, *pgperr, *pgptmp;
720   struct stat info;
721   BODY *tattach;
722   int len;
723   char pgperrfile[_POSIX_PATH_MAX];
724   char pgptmpfile[_POSIX_PATH_MAX];
725   pid_t thepid;
726   int rv;
727
728   mutt_mktemp (pgperrfile);
729   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
730     mutt_perror (pgperrfile);
731     return NULL;
732   }
733   unlink (pgperrfile);
734
735   mutt_mktemp (pgptmpfile);
736   if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
737     mutt_perror (pgptmpfile);
738     fclose (pgperr);
739     return NULL;
740   }
741
742   /* Position the stream at the beginning of the body, and send the data to
743    * the temporary file.
744    */
745
746   fseek (s->fpin, a->offset, 0);
747   mutt_copy_bytes (s->fpin, pgptmp, a->length);
748   fclose (pgptmp);
749
750   if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
751                                     fileno (pgperr), pgptmpfile)) == -1) {
752     fclose (pgperr);
753     unlink (pgptmpfile);
754     if (s->flags & M_DISPLAY)
755       state_attach_puts (_
756                          ("[-- Error: could not create a PGP subprocess! --]\n\n"),
757                          s);
758     return (NULL);
759   }
760
761   /* send the PGP passphrase to the subprocess.  Never do this if the
762      agent is active, because this might lead to a passphrase send as
763      the message. */
764   if (!pgp_use_gpg_agent ())
765     fputs (PgpPass, pgpin);
766   fputc ('\n', pgpin);
767   fclose (pgpin);
768
769   /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
770    * read_mime_header has a hard time parsing the message.
771    */
772   while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
773     len = str_len (buf);
774     if (len > 1 && buf[len - 2] == '\r')
775       strcpy (buf + len - 2, "\n");     /* __STRCPY_CHECKED__ */
776     fputs (buf, fpout);
777   }
778
779   fclose (pgpout);
780   rv = mutt_wait_filter (thepid);
781   mutt_unlink (pgptmpfile);
782
783   if (s->flags & M_DISPLAY) {
784     fflush (pgperr);
785     rewind (pgperr);
786     if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
787       p->goodsig = 1;
788     else
789       p->goodsig = 0;
790     state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
791   }
792   fclose (pgperr);
793
794   fflush (fpout);
795   rewind (fpout);
796
797   if (fgetc (fpout) == EOF) {
798     mutt_error (_("Decryption failed."));
799     pgp_void_passphrase ();
800     return NULL;
801   }
802
803   rewind (fpout);
804
805   if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
806     /*
807      * Need to set the length of this body part.
808      */
809     fstat (fileno (fpout), &info);
810     tattach->length = info.st_size - tattach->offset;
811
812     /* See if we need to recurse on this MIME part.  */
813
814     mutt_parse_part (fpout, tattach);
815   }
816
817   return (tattach);
818 }
819
820 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
821 {
822   char tempfile[_POSIX_PATH_MAX];
823   STATE s;
824   BODY *p = b;
825
826   if (!mutt_is_multipart_encrypted (b))
827     return -1;
828
829   if (!b->parts || !b->parts->next)
830     return -1;
831
832   b = b->parts->next;
833
834   memset (&s, 0, sizeof (s));
835   s.fpin = fpin;
836   mutt_mktemp (tempfile);
837   if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
838     mutt_perror (tempfile);
839     return (-1);
840   }
841   unlink (tempfile);
842
843   *cur = pgp_decrypt_part (b, &s, *fpout, p);
844
845   rewind (*fpout);
846
847   if (!*cur)
848     return -1;
849
850   return (0);
851 }
852
853 int pgp_encrypted_handler (BODY * a, STATE * s)
854 {
855   char tempfile[_POSIX_PATH_MAX];
856   FILE *fpout, *fpin;
857   BODY *tattach;
858   BODY *p = a;
859   int rc = 0;
860
861   a = a->parts;
862   if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
863       ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
864       !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
865       ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
866     if (s->flags & M_DISPLAY)
867       state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
868                          s);
869     return (-1);
870   }
871
872   /*
873    * Move forward to the application/pgp-encrypted body.
874    */
875   a = a->next;
876
877   mutt_mktemp (tempfile);
878   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
879     if (s->flags & M_DISPLAY)
880       state_attach_puts (_
881                          ("[-- Error: could not create temporary file! --]\n"),
882                          s);
883     return (-1);
884   }
885
886   if (s->flags & M_DISPLAY)
887     crypt_current_time (s, "PGP");
888
889   if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
890     if (s->flags & M_DISPLAY)
891       state_attach_puts (_
892                          ("[-- The following data is PGP/MIME encrypted --]\n\n"),
893                          s);
894
895     fpin = s->fpin;
896     s->fpin = fpout;
897     rc = mutt_body_handler (tattach, s);
898     s->fpin = fpin;
899
900     /* 
901      * if a multipart/signed is the _only_ sub-part of a
902      * multipart/encrypted, cache signature verification
903      * status.
904      *
905      */
906
907     if (mutt_is_multipart_signed (tattach) && !tattach->next)
908       p->goodsig |= tattach->goodsig;
909
910     if (s->flags & M_DISPLAY) {
911       state_puts ("\n", s);
912       state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
913     }
914
915     mutt_free_body (&tattach);
916     /* clear 'Invoking...' message, since there's no error */
917     mutt_message _("PGP message successfully decrypted.");
918   } else {
919     mutt_error _("Could not decrypt PGP message");
920     pgp_void_passphrase ();
921     rc = -1;
922   }
923
924   fclose (fpout);
925   mutt_unlink (tempfile);
926
927   return (rc);
928 }
929
930 /* ----------------------------------------------------------------------------
931  * Routines for sending PGP/MIME messages.
932  */
933
934
935 BODY *pgp_sign_message (BODY * a)
936 {
937   BODY *t;
938   char buffer[LONG_STRING];
939   char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
940   FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
941   int err = 0;
942   int empty = 1;
943   pid_t thepid;
944
945   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
946
947   mutt_mktemp (sigfile);
948   if ((fp = safe_fopen (sigfile, "w")) == NULL) {
949     return (NULL);
950   }
951
952   mutt_mktemp (signedfile);
953   if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
954     mutt_perror (signedfile);
955     fclose (fp);
956     unlink (sigfile);
957     return NULL;
958   }
959
960   mutt_write_mime_header (a, sfp);
961   fputc ('\n', sfp);
962   mutt_write_mime_body (a, sfp);
963   fclose (sfp);
964
965   if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
966                                  -1, -1, -1, signedfile)) == -1) {
967     mutt_perror (_("Can't open PGP subprocess!"));
968
969     fclose (fp);
970     unlink (sigfile);
971     unlink (signedfile);
972     return NULL;
973   }
974
975   if (!pgp_use_gpg_agent ())
976     fputs (PgpPass, pgpin);
977   fputc ('\n', pgpin);
978   fclose (pgpin);
979
980   /*
981    * Read back the PGP signature.  Also, change MESSAGE=>SIGNATURE as
982    * recommended for future releases of PGP.
983    */
984   while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
985     if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
986       fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
987     else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
988       fputs ("-----END PGP SIGNATURE-----\n", fp);
989     else
990       fputs (buffer, fp);
991     empty = 0;                  /* got some output, so we're ok */
992   }
993
994   /* check for errors from PGP */
995   err = 0;
996   while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
997     err = 1;
998     fputs (buffer, stdout);
999   }
1000
1001   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1002     empty = 1;
1003
1004   fclose (pgperr);
1005   fclose (pgpout);
1006   unlink (signedfile);
1007
1008   if (fclose (fp) != 0) {
1009     mutt_perror ("fclose");
1010     unlink (sigfile);
1011     return (NULL);
1012   }
1013
1014   if (err) {
1015     pgp_void_passphrase ();
1016     mutt_any_key_to_continue (NULL);
1017   }
1018
1019   if (empty) {
1020     unlink (sigfile);
1021     return (NULL);              /* fatal error while signing */
1022   }
1023
1024   t = mutt_new_body ();
1025   t->type = TYPEMULTIPART;
1026   t->subtype = str_dup ("signed");
1027   t->encoding = ENC7BIT;
1028   t->use_disp = 0;
1029   t->disposition = DISPINLINE;
1030
1031   mutt_generate_boundary (&t->parameter);
1032   mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1033   mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1034
1035   t->parts = a;
1036   a = t;
1037
1038   t->parts->next = mutt_new_body ();
1039   t = t->parts->next;
1040   t->type = TYPEAPPLICATION;
1041   t->subtype = str_dup ("pgp-signature");
1042   t->filename = str_dup (sigfile);
1043   t->use_disp = 0;
1044   t->disposition = DISPINLINE;
1045   t->encoding = ENC7BIT;
1046   t->unlink = 1;                /* ok to remove this file after sending. */
1047
1048   return (a);
1049 }
1050
1051 static short is_numerical_keyid (const char *s)
1052 {
1053   /* or should we require the "0x"? */
1054   if (strncmp (s, "0x", 2) == 0)
1055     s += 2;
1056   if (str_len (s) % 8)
1057     return 0;
1058   while (*s)
1059     if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1060       return 0;
1061
1062   return 1;
1063 }
1064
1065 /* This routine attempts to find the keyids of the recipients of a message.
1066  * It returns NULL if any of the keys can not be found.
1067  */
1068 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1069 {
1070   char *keyID, *keylist = NULL, *t;
1071   size_t keylist_size = 0;
1072   size_t keylist_used = 0;
1073   ADDRESS *tmp = NULL, *addr = NULL;
1074   ADDRESS **last = &tmp;
1075   ADDRESS *p, *q;
1076   int i;
1077   pgp_key_t k_info = NULL, key = NULL;
1078
1079   const char *fqdn = mutt_fqdn (1);
1080
1081   for (i = 0; i < 3; i++) {
1082     switch (i) {
1083     case 0:
1084       p = to;
1085       break;
1086     case 1:
1087       p = cc;
1088       break;
1089     case 2:
1090       p = bcc;
1091       break;
1092     default:
1093       abort ();
1094     }
1095
1096     *last = rfc822_cpy_adr (p);
1097     while (*last)
1098       last = &((*last)->next);
1099   }
1100
1101   if (fqdn)
1102     rfc822_qualify (tmp, fqdn);
1103
1104   tmp = mutt_remove_duplicates (tmp);
1105
1106   for (p = tmp; p; p = p->next) {
1107     char buf[LONG_STRING];
1108
1109     q = p;
1110     k_info = NULL;
1111
1112     if ((keyID = mutt_crypt_hook (p)) != NULL) {
1113       int r;
1114
1115       snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1116                 p->mailbox);
1117       if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1118         if (is_numerical_keyid (keyID)) {
1119           if (strncmp (keyID, "0x", 2) == 0)
1120             keyID += 2;
1121           goto bypass_selection;        /* you don't see this. */
1122         }
1123
1124         /* check for e-mail address */
1125         if ((t = strchr (keyID, '@')) &&
1126             (addr = rfc822_parse_adrlist (NULL, keyID))) {
1127           if (fqdn)
1128             rfc822_qualify (addr, fqdn);
1129           q = addr;
1130         }
1131         else
1132           k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1133       }
1134       else if (r == -1) {
1135         mem_free (&keylist);
1136         rfc822_free_address (&tmp);
1137         rfc822_free_address (&addr);
1138         return NULL;
1139       }
1140     }
1141
1142     if (k_info == NULL)
1143       pgp_invoke_getkeys (q);
1144
1145     if (k_info == NULL
1146         && (k_info =
1147             pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1148       snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1149
1150       if ((key = pgp_ask_for_key (buf, q->mailbox,
1151                                   KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1152         mem_free (&keylist);
1153         rfc822_free_address (&tmp);
1154         rfc822_free_address (&addr);
1155         return NULL;
1156       }
1157     }
1158     else
1159       key = k_info;
1160
1161     keyID = pgp_keyid (key);
1162
1163   bypass_selection:
1164     keylist_size += str_len (keyID) + 4;
1165     mem_realloc (&keylist, keylist_size);
1166     sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1167              keyID);
1168     keylist_used = str_len (keylist);
1169
1170     pgp_free_key (&key);
1171     rfc822_free_address (&addr);
1172
1173   }
1174   rfc822_free_address (&tmp);
1175   return (keylist);
1176 }
1177
1178 /* Warning: "a" is no longer freed in this routine, you need
1179  * to free it later.  This is necessary for $fcc_attach. */
1180
1181 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1182 {
1183   char buf[LONG_STRING];
1184   char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1185   char pgpinfile[_POSIX_PATH_MAX];
1186   FILE *pgpin, *pgperr, *fpout, *fptmp;
1187   BODY *t;
1188   int err = 0;
1189   int empty = 0;
1190   pid_t thepid;
1191
1192   mutt_mktemp (tempfile);
1193   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1194     mutt_perror (tempfile);
1195     return (NULL);
1196   }
1197
1198   mutt_mktemp (pgperrfile);
1199   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1200     mutt_perror (pgperrfile);
1201     unlink (tempfile);
1202     fclose (fpout);
1203     return NULL;
1204   }
1205   unlink (pgperrfile);
1206
1207   mutt_mktemp (pgpinfile);
1208   if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1209     mutt_perror (pgpinfile);
1210     unlink (tempfile);
1211     fclose (fpout);
1212     fclose (pgperr);
1213     return NULL;
1214   }
1215
1216   if (sign)
1217     convert_to_7bit (a);
1218
1219   mutt_write_mime_header (a, fptmp);
1220   fputc ('\n', fptmp);
1221   mutt_write_mime_body (a, fptmp);
1222   fclose (fptmp);
1223
1224   if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1225                                     fileno (fpout), fileno (pgperr),
1226                                     pgpinfile, keylist, sign)) == -1) {
1227     fclose (pgperr);
1228     unlink (pgpinfile);
1229     return (NULL);
1230   }
1231
1232   if (sign) {
1233     if (!pgp_use_gpg_agent ())
1234       fputs (PgpPass, pgpin);
1235     fputc ('\n', pgpin);
1236   }
1237   fclose (pgpin);
1238
1239   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1240     empty = 1;
1241
1242   unlink (pgpinfile);
1243
1244   fflush (fpout);
1245   rewind (fpout);
1246   if (!empty)
1247     empty = (fgetc (fpout) == EOF);
1248   fclose (fpout);
1249
1250   fflush (pgperr);
1251   rewind (pgperr);
1252   while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1253     err = 1;
1254     fputs (buf, stdout);
1255   }
1256   fclose (pgperr);
1257
1258   /* pause if there is any error output from PGP */
1259   if (err)
1260     mutt_any_key_to_continue (NULL);
1261
1262   if (empty) {
1263     /* fatal error while trying to encrypt message */
1264     if (sign)
1265       pgp_void_passphrase (); /* just in case */
1266     unlink (tempfile);
1267     return (NULL);
1268   }
1269
1270   t = mutt_new_body ();
1271   t->type = TYPEMULTIPART;
1272   t->subtype = str_dup ("encrypted");
1273   t->encoding = ENC7BIT;
1274   t->use_disp = 0;
1275   t->disposition = DISPINLINE;
1276
1277   mutt_generate_boundary (&t->parameter);
1278   mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1279
1280   t->parts = mutt_new_body ();
1281   t->parts->type = TYPEAPPLICATION;
1282   t->parts->subtype = str_dup ("pgp-encrypted");
1283   t->parts->encoding = ENC7BIT;
1284
1285   t->parts->next = mutt_new_body ();
1286   t->parts->next->type = TYPEAPPLICATION;
1287   t->parts->next->subtype = str_dup ("octet-stream");
1288   t->parts->next->encoding = ENC7BIT;
1289   t->parts->next->filename = str_dup (tempfile);
1290   t->parts->next->use_disp = 1;
1291   t->parts->next->disposition = DISPINLINE;
1292   t->parts->next->unlink = 1;   /* delete after sending the message */
1293   t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1294
1295   return (t);
1296 }
1297
1298 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1299 {
1300   BODY *b;
1301
1302   char pgpoutfile[_POSIX_PATH_MAX];
1303   char pgperrfile[_POSIX_PATH_MAX];
1304   char pgpinfile[_POSIX_PATH_MAX];
1305
1306   char body_charset[STRING];
1307   char *from_charset;
1308   const char *send_charset;
1309
1310   FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1311   FILE *fp;
1312
1313   int empty = 0;
1314   int err;
1315
1316   char buff[STRING];
1317
1318   pid_t thepid;
1319
1320   if (a->type != TYPETEXT)
1321     return NULL;
1322   if (ascii_strcasecmp (a->subtype, "plain"))
1323     return NULL;
1324
1325   if ((fp = fopen (a->filename, "r")) == NULL) {
1326     mutt_perror (a->filename);
1327     return NULL;
1328   }
1329
1330   mutt_mktemp (pgpinfile);
1331   if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1332     mutt_perror (pgpinfile);
1333     fclose (fp);
1334     return NULL;
1335   }
1336
1337   /* The following code is really correct:  If noconv is set,
1338    * a's charset parameter contains the on-disk character set, and
1339    * we have to convert from that to utf-8.  If noconv is not set,
1340    * we have to convert from $charset to utf-8.
1341    */
1342
1343   mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1344   if (a->noconv)
1345     from_charset = body_charset;
1346   else
1347     from_charset = Charset;
1348
1349   if (!mutt_is_us_ascii (body_charset)) {
1350     int c;
1351     FGETCONV *fc;
1352
1353     if (flags & ENCRYPT)
1354       send_charset = "us-ascii";
1355     else
1356       send_charset = "utf-8";
1357
1358     fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1359     while ((c = fgetconv (fc)) != EOF)
1360       fputc (c, pgpin);
1361
1362     fgetconv_close (&fc);
1363   }
1364   else {
1365     send_charset = "us-ascii";
1366     mutt_copy_stream (fp, pgpin);
1367   }
1368   safe_fclose (&fp);
1369   fclose (pgpin);
1370
1371   mutt_mktemp (pgpoutfile);
1372   mutt_mktemp (pgperrfile);
1373   if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1374       (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1375     mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1376     unlink (pgpinfile);
1377     if (pgpout) {
1378       fclose (pgpout);
1379       unlink (pgpoutfile);
1380     }
1381     return NULL;
1382   }
1383
1384   unlink (pgperrfile);
1385
1386   if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1387                                         -1, fileno (pgpout), fileno (pgperr),
1388                                         pgpinfile, keylist, flags)) == -1) {
1389     mutt_perror (_("Can't invoke PGP"));
1390
1391     fclose (pgpout);
1392     fclose (pgperr);
1393     mutt_unlink (pgpinfile);
1394     unlink (pgpoutfile);
1395     return NULL;
1396   }
1397
1398   if (pgp_use_gpg_agent ())
1399     *PgpPass = 0;
1400   if (flags & SIGN)
1401     fprintf (pgpin, "%s\n", PgpPass);
1402   fclose (pgpin);
1403
1404   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1405     empty = 1;
1406
1407   mutt_unlink (pgpinfile);
1408
1409   fflush (pgpout);
1410   fflush (pgperr);
1411
1412   rewind (pgpout);
1413   rewind (pgperr);
1414
1415   if (!empty)
1416     empty = (fgetc (pgpout) == EOF);
1417   fclose (pgpout);
1418
1419   err = 0;
1420
1421   while (fgets (buff, sizeof (buff), pgperr)) {
1422     err = 1;
1423     fputs (buff, stdout);
1424   }
1425
1426   fclose (pgperr);
1427
1428   if (err)
1429     mutt_any_key_to_continue (NULL);
1430
1431   if (empty) {
1432     if (flags & SIGN)
1433       pgp_void_passphrase (); /* just in case */
1434     unlink (pgpoutfile);
1435     return NULL;
1436   }
1437
1438   b = mutt_new_body ();
1439
1440   b->encoding = ENC7BIT;
1441
1442   b->type = TYPETEXT;
1443   b->subtype = str_dup ("plain");
1444
1445   mutt_set_parameter ("x-action",
1446                       flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1447                       &b->parameter);
1448   mutt_set_parameter ("charset", send_charset, &b->parameter);
1449
1450   b->filename = str_dup (pgpoutfile);
1451
1452 #if 0
1453   /* The following is intended to give a clue to some completely brain-dead 
1454    * "mail environments" which are typically used by large corporations.
1455    */
1456
1457   b->d_filename = str_dup ("msg.pgp");
1458   b->use_disp = 1;
1459
1460 #endif
1461
1462   b->disposition = DISPINLINE;
1463   b->unlink = 1;
1464
1465   b->noconv = 1;
1466   b->use_disp = 0;
1467
1468   if (!(flags & ENCRYPT))
1469     b->encoding = a->encoding;
1470
1471   return b;
1472 }
1473
1474 int pgp_send_menu (HEADER * msg, int *redraw)
1475 {
1476   pgp_key_t p;
1477   char input_signas[SHORT_STRING];
1478
1479   char prompt[LONG_STRING];
1480
1481   if (!(WithCrypto & APPLICATION_PGP))
1482     return msg->security;
1483
1484   /* If autoinline and no crypto options set, then set inline. */
1485   if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1486                                      && (msg->security & (SIGN | ENCRYPT))))
1487     msg->security |= INLINE;
1488
1489   snprintf (prompt, sizeof (prompt),
1490             _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1491             (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1492
1493   switch (mutt_multi_choice (prompt, _("esabifc"))) {
1494   case 1:                      /* (e)ncrypt */
1495     msg->security |= ENCRYPT;
1496     msg->security &= ~SIGN;
1497     break;
1498
1499   case 2:                      /* (s)ign */
1500     msg->security |= SIGN;
1501     msg->security &= ~ENCRYPT;
1502     break;
1503
1504   case 3:                      /* sign (a)s */
1505     unset_option (OPTPGPCHECKTRUST);
1506
1507     if ((p =
1508          pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1509                           PGP_PUBRING))) {
1510       snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1511       str_replace (&PgpSignAs, input_signas);
1512       pgp_free_key (&p);
1513
1514       msg->security |= SIGN;
1515
1516       crypt_pgp_void_passphrase ();     /* probably need a different passphrase */
1517     }
1518 #if 0
1519     else {
1520       msg->security &= ~SIGN;
1521     }
1522 #endif
1523
1524     *redraw = REDRAW_FULL;
1525     break;
1526
1527   case 4:                      /* (b)oth */
1528     msg->security |= (ENCRYPT | SIGN);
1529     break;
1530
1531   case 5:                      /* (i)nline */
1532     if ((msg->security & (ENCRYPT | SIGN)))
1533       msg->security ^= INLINE;
1534     else
1535       msg->security &= ~INLINE;
1536     break;
1537
1538   case 6:                      /* (f)orget it */
1539   case 7:                      /* (c)lear     */
1540     msg->security = 0;
1541     break;
1542   }
1543
1544   if (msg->security) {
1545     if (!(msg->security & (ENCRYPT | SIGN)))
1546       msg->security = 0;
1547     else
1548       msg->security |= APPLICATION_PGP;
1549   }
1550
1551   return (msg->security);
1552 }
1553
1554
1555 #endif /* CRYPT_BACKEND_CLASSIC_PGP */