3ed7d877bc5abf1de10fe5380ffbc531536104ca
[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         /* TODO: maybe on failure mutt should include the original undecoded text. */
379         if (pgpout) {
380           rewind (pgpout);
381           c = fgetc (pgpout);
382           ungetc (c, pgpout);
383         }
384         if (!clearsign && (!pgpout || c == EOF)) {
385             mutt_error _("Could not decrypt PGP message");
386             mutt_sleep (1);
387             pgp_void_passphrase ();
388             if (!(s->flags & M_DISPLAY)) {
389               rc = -1;
390               goto out;
391             }
392         }
393       }
394
395       /*
396        * Now, copy cleartext to the screen.  NOTE - we expect that PGP
397        * outputs utf-8 cleartext.  This may not always be true, but it 
398        * seems to be a reasonable guess.
399        */
400
401       if (s->flags & M_DISPLAY) {
402         if (needpass)
403           state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
404         else if (pgp_keyblock)
405           state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
406         else
407           state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
408       }
409
410       if (clearsign) {
411         rewind (tmpfp);
412         if (tmpfp)
413           pgp_copy_clearsigned (tmpfp, s, body_charset);
414       }
415       else if (pgpout) {
416         FGETCONV *fc;
417         int c;
418
419         rewind (pgpout);
420         state_set_prefix (s);
421         fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
422         while ((c = fgetconv (fc)) != EOF)
423           state_prefix_putc (c, s);
424         fgetconv_close (&fc);
425       }
426
427       if (s->flags & M_DISPLAY) {
428         state_putc ('\n', s);
429         if (needpass) {
430           state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
431           mutt_message _("PGP message successfully decrypted.");
432         }
433         else if (pgp_keyblock)
434           state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
435         else
436           state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
437       }
438
439     }
440     else {
441       /* XXX - we may wish to recode here */
442       if (s->prefix)
443         state_puts (s->prefix, s);
444       state_puts (buf, s);
445     }
446   }
447
448   rc = 0;
449
450 out:
451   m->goodsig = (maybe_goodsig && have_any_sigs);
452
453   if (tmpfp) {
454     safe_fclose (&tmpfp);
455     mutt_unlink (tmpfname);
456   }
457   if (pgpout) {
458     safe_fclose (&pgpout);
459     mutt_unlink (outfile);
460   }
461
462   if (needpass == -1) {
463     state_attach_puts (_
464                        ("[-- Error: could not find beginning of PGP message! --]\n\n"),
465                        s);
466     return (-1);
467   }
468
469   return (rc);
470 }
471
472 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
473                                            int tagged_only)
474 {
475   char tempfile[_POSIX_PATH_MAX];
476   char buf[HUGE_STRING];
477   FILE *tfp;
478
479   short sgn = 0;
480   short enc = 0;
481   short key = 0;
482
483   if (b->type != TYPETEXT)
484     return 0;
485
486   if (tagged_only && !b->tagged)
487     return 0;
488
489   mutt_mktemp (tempfile);
490   if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
491     unlink (tempfile);
492     return 0;
493   }
494
495   if ((tfp = fopen (tempfile, "r")) == NULL) {
496     unlink (tempfile);
497     return 0;
498   }
499
500   while (fgets (buf, sizeof (buf), tfp)) {
501     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
502       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
503         enc = 1;
504       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
505         sgn = 1;
506       else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
507         key = 1;
508     }
509   }
510   safe_fclose (&tfp);
511   unlink (tempfile);
512
513   if (!enc && !sgn && !key)
514     return 0;
515
516   /* fix the content type */
517
518   mutt_set_parameter ("format", "fixed", &b->parameter);
519   if (enc)
520     mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
521   else if (sgn)
522     mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
523   else if (key)
524     mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
525
526   return 1;
527 }
528
529 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
530 {
531   int rv = 0;
532   int r;
533
534   for (; b; b = b->next) {
535     if (is_multipart (b))
536       rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
537     else if (b->type == TYPETEXT) {
538       if ((r = mutt_is_application_pgp (b)))
539         rv = rv || r;
540       else
541         rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
542     }
543   }
544
545   return rv;
546 }
547
548
549
550
551
552 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
553 {
554   char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
555   FILE *fp, *pgpout, *pgperr;
556   pid_t thepid;
557   int badsig = -1;
558   int rv;
559
560   snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
561
562   if (!(fp = safe_fopen (sigfile, "w"))) {
563     mutt_perror (sigfile);
564     return -1;
565   }
566
567   fseek (s->fpin, sigbdy->offset, 0);
568   mutt_copy_bytes (s->fpin, fp, sigbdy->length);
569   fclose (fp);
570
571   mutt_mktemp (pgperrfile);
572   if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
573     mutt_perror (pgperrfile);
574     unlink (sigfile);
575     return -1;
576   }
577
578   crypt_current_time (s, "PGP");
579
580   if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
581                                    -1, -1, fileno (pgperr),
582                                    tempfile, sigfile)) != -1) {
583     if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
584       badsig = 0;
585
586
587     safe_fclose (&pgpout);
588     fflush (pgperr);
589     rewind (pgperr);
590
591     if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
592       badsig = 0;
593
594     if ((rv = mutt_wait_filter (thepid)))
595       badsig = -1;
596
597     debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
598   }
599
600   safe_fclose (&pgperr);
601
602   state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
603
604   mutt_unlink (sigfile);
605   mutt_unlink (pgperrfile);
606
607   debug_print (1, ("returning %d.\n", badsig));
608
609   return badsig;
610 }
611
612
613 /* Extract pgp public keys from messages or attachments */
614
615 void pgp_extract_keys_from_messages (HEADER * h)
616 {
617   int i;
618   char tempfname[_POSIX_PATH_MAX];
619   FILE *fpout;
620
621   if (h) {
622     mutt_parse_mime_message (Context, h);
623     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
624       return;
625   }
626
627   mutt_mktemp (tempfname);
628   if (!(fpout = safe_fopen (tempfname, "w"))) {
629     mutt_perror (tempfname);
630     return;
631   }
632
633   set_option (OPTDONTHANDLEPGPKEYS);
634
635   if (!h) {
636     for (i = 0; i < Context->vcount; i++) {
637       if (Context->hdrs[Context->v2r[i]]->tagged) {
638         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
639         if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
640             && !pgp_valid_passphrase ()) {
641           fclose (fpout);
642           goto bailout;
643         }
644         mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
645                            M_CM_DECODE | M_CM_CHARCONV, 0);
646       }
647     }
648   }
649   else {
650     mutt_parse_mime_message (Context, h);
651     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
652       fclose (fpout);
653       goto bailout;
654     }
655     mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
656   }
657
658   fclose (fpout);
659   mutt_endwin (NULL);
660   pgp_invoke_import (tempfname);
661   mutt_any_key_to_continue (NULL);
662
663 bailout:
664
665   mutt_unlink (tempfname);
666   unset_option (OPTDONTHANDLEPGPKEYS);
667
668 }
669
670 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
671 {
672   STATE s;
673   FILE *tempfp;
674   char tempfname[_POSIX_PATH_MAX];
675
676   mutt_mktemp (tempfname);
677   if (!(tempfp = safe_fopen (tempfname, "w"))) {
678     mutt_perror (tempfname);
679     return;
680   }
681
682   memset (&s, 0, sizeof (STATE));
683
684   s.fpin = fp;
685   s.fpout = tempfp;
686
687   mutt_body_handler (top, &s);
688
689   fclose (tempfp);
690
691   pgp_invoke_import (tempfname);
692   mutt_any_key_to_continue (NULL);
693
694   mutt_unlink (tempfname);
695 }
696
697 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
698 {
699   if (!fp) {
700     mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
701
702     return;
703   }
704
705   mutt_endwin (NULL);
706   set_option (OPTDONTHANDLEPGPKEYS);
707
708   for (; top; top = top->next) {
709     if (!tag || top->tagged)
710       pgp_extract_keys_from_attachment (fp, top);
711
712     if (!tag)
713       break;
714   }
715
716   unset_option (OPTDONTHANDLEPGPKEYS);
717 }
718
719 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
720 {
721   char buf[LONG_STRING];
722   FILE *pgpin, *pgpout, *pgperr, *pgptmp;
723   struct stat info;
724   BODY *tattach;
725   int len;
726   char pgperrfile[_POSIX_PATH_MAX];
727   char pgptmpfile[_POSIX_PATH_MAX];
728   pid_t thepid;
729   int rv;
730
731   mutt_mktemp (pgperrfile);
732   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
733     mutt_perror (pgperrfile);
734     return NULL;
735   }
736   unlink (pgperrfile);
737
738   mutt_mktemp (pgptmpfile);
739   if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
740     mutt_perror (pgptmpfile);
741     fclose (pgperr);
742     return NULL;
743   }
744
745   /* Position the stream at the beginning of the body, and send the data to
746    * the temporary file.
747    */
748
749   fseek (s->fpin, a->offset, 0);
750   mutt_copy_bytes (s->fpin, pgptmp, a->length);
751   fclose (pgptmp);
752
753   if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
754                                     fileno (pgperr), pgptmpfile)) == -1) {
755     fclose (pgperr);
756     unlink (pgptmpfile);
757     if (s->flags & M_DISPLAY)
758       state_attach_puts (_
759                          ("[-- Error: could not create a PGP subprocess! --]\n\n"),
760                          s);
761     return (NULL);
762   }
763
764   /* send the PGP passphrase to the subprocess.  Never do this if the
765      agent is active, because this might lead to a passphrase send as
766      the message. */
767   if (!pgp_use_gpg_agent ())
768     fputs (PgpPass, pgpin);
769   fputc ('\n', pgpin);
770   fclose (pgpin);
771
772   /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
773    * read_mime_header has a hard time parsing the message.
774    */
775   while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
776     len = str_len (buf);
777     if (len > 1 && buf[len - 2] == '\r')
778       strcpy (buf + len - 2, "\n");     /* __STRCPY_CHECKED__ */
779     fputs (buf, fpout);
780   }
781
782   fclose (pgpout);
783   rv = mutt_wait_filter (thepid);
784   mutt_unlink (pgptmpfile);
785
786   if (s->flags & M_DISPLAY) {
787     fflush (pgperr);
788     rewind (pgperr);
789     if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
790       p->goodsig = 1;
791     else
792       p->goodsig = 0;
793     state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
794   }
795   fclose (pgperr);
796
797   fflush (fpout);
798   rewind (fpout);
799
800   if (fgetc (fpout) == EOF) {
801     mutt_error (_("Decryption failed."));
802     pgp_void_passphrase ();
803     return NULL;
804   }
805
806   rewind (fpout);
807
808   if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
809     /*
810      * Need to set the length of this body part.
811      */
812     fstat (fileno (fpout), &info);
813     tattach->length = info.st_size - tattach->offset;
814
815     /* See if we need to recurse on this MIME part.  */
816
817     mutt_parse_part (fpout, tattach);
818   }
819
820   return (tattach);
821 }
822
823 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
824 {
825   char tempfile[_POSIX_PATH_MAX];
826   STATE s;
827   BODY *p = b;
828
829   if (!mutt_is_multipart_encrypted (b))
830     return -1;
831
832   if (!b->parts || !b->parts->next)
833     return -1;
834
835   b = b->parts->next;
836
837   memset (&s, 0, sizeof (s));
838   s.fpin = fpin;
839   mutt_mktemp (tempfile);
840   if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
841     mutt_perror (tempfile);
842     return (-1);
843   }
844   unlink (tempfile);
845
846   *cur = pgp_decrypt_part (b, &s, *fpout, p);
847
848   rewind (*fpout);
849
850   if (!*cur)
851     return -1;
852
853   return (0);
854 }
855
856 int pgp_encrypted_handler (BODY * a, STATE * s)
857 {
858   char tempfile[_POSIX_PATH_MAX];
859   FILE *fpout, *fpin;
860   BODY *tattach;
861   BODY *p = a;
862   int rc = 0;
863
864   a = a->parts;
865   if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
866       ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
867       !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
868       ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
869     if (s->flags & M_DISPLAY)
870       state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
871                          s);
872     return (-1);
873   }
874
875   /*
876    * Move forward to the application/pgp-encrypted body.
877    */
878   a = a->next;
879
880   mutt_mktemp (tempfile);
881   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
882     if (s->flags & M_DISPLAY)
883       state_attach_puts (_
884                          ("[-- Error: could not create temporary file! --]\n"),
885                          s);
886     return (-1);
887   }
888
889   if (s->flags & M_DISPLAY)
890     crypt_current_time (s, "PGP");
891
892   if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
893     if (s->flags & M_DISPLAY)
894       state_attach_puts (_
895                          ("[-- The following data is PGP/MIME encrypted --]\n\n"),
896                          s);
897
898     fpin = s->fpin;
899     s->fpin = fpout;
900     rc = mutt_body_handler (tattach, s);
901     s->fpin = fpin;
902
903     /* 
904      * if a multipart/signed is the _only_ sub-part of a
905      * multipart/encrypted, cache signature verification
906      * status.
907      *
908      */
909
910     if (mutt_is_multipart_signed (tattach) && !tattach->next)
911       p->goodsig |= tattach->goodsig;
912
913     if (s->flags & M_DISPLAY) {
914       state_puts ("\n", s);
915       state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
916     }
917
918     mutt_free_body (&tattach);
919     /* clear 'Invoking...' message, since there's no error */
920     mutt_message _("PGP message successfully decrypted.");
921   } else {
922     mutt_error _("Could not decrypt PGP message");
923     pgp_void_passphrase ();
924     rc = -1;
925   }
926
927   fclose (fpout);
928   mutt_unlink (tempfile);
929
930   return (rc);
931 }
932
933 /* ----------------------------------------------------------------------------
934  * Routines for sending PGP/MIME messages.
935  */
936
937
938 BODY *pgp_sign_message (BODY * a)
939 {
940   BODY *t;
941   char buffer[LONG_STRING];
942   char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
943   FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
944   int err = 0;
945   int empty = 1;
946   pid_t thepid;
947
948   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
949
950   mutt_mktemp (sigfile);
951   if ((fp = safe_fopen (sigfile, "w")) == NULL) {
952     return (NULL);
953   }
954
955   mutt_mktemp (signedfile);
956   if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
957     mutt_perror (signedfile);
958     fclose (fp);
959     unlink (sigfile);
960     return NULL;
961   }
962
963   mutt_write_mime_header (a, sfp);
964   fputc ('\n', sfp);
965   mutt_write_mime_body (a, sfp);
966   fclose (sfp);
967
968   if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
969                                  -1, -1, -1, signedfile)) == -1) {
970     mutt_perror (_("Can't open PGP subprocess!"));
971
972     fclose (fp);
973     unlink (sigfile);
974     unlink (signedfile);
975     return NULL;
976   }
977
978   if (!pgp_use_gpg_agent ())
979     fputs (PgpPass, pgpin);
980   fputc ('\n', pgpin);
981   fclose (pgpin);
982
983   /*
984    * Read back the PGP signature.  Also, change MESSAGE=>SIGNATURE as
985    * recommended for future releases of PGP.
986    */
987   while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
988     if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
989       fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
990     else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
991       fputs ("-----END PGP SIGNATURE-----\n", fp);
992     else
993       fputs (buffer, fp);
994     empty = 0;                  /* got some output, so we're ok */
995   }
996
997   /* check for errors from PGP */
998   err = 0;
999   while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1000     err = 1;
1001     fputs (buffer, stdout);
1002   }
1003
1004   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1005     empty = 1;
1006
1007   fclose (pgperr);
1008   fclose (pgpout);
1009   unlink (signedfile);
1010
1011   if (fclose (fp) != 0) {
1012     mutt_perror ("fclose");
1013     unlink (sigfile);
1014     return (NULL);
1015   }
1016
1017   if (err) {
1018     pgp_void_passphrase ();
1019     mutt_any_key_to_continue (NULL);
1020   }
1021
1022   if (empty) {
1023     unlink (sigfile);
1024     return (NULL);              /* fatal error while signing */
1025   }
1026
1027   t = mutt_new_body ();
1028   t->type = TYPEMULTIPART;
1029   t->subtype = str_dup ("signed");
1030   t->encoding = ENC7BIT;
1031   t->use_disp = 0;
1032   t->disposition = DISPINLINE;
1033
1034   mutt_generate_boundary (&t->parameter);
1035   mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1036   mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1037
1038   t->parts = a;
1039   a = t;
1040
1041   t->parts->next = mutt_new_body ();
1042   t = t->parts->next;
1043   t->type = TYPEAPPLICATION;
1044   t->subtype = str_dup ("pgp-signature");
1045   t->filename = str_dup (sigfile);
1046   t->use_disp = 0;
1047   t->disposition = DISPINLINE;
1048   t->encoding = ENC7BIT;
1049   t->unlink = 1;                /* ok to remove this file after sending. */
1050
1051   return (a);
1052 }
1053
1054 static short is_numerical_keyid (const char *s)
1055 {
1056   /* or should we require the "0x"? */
1057   if (strncmp (s, "0x", 2) == 0)
1058     s += 2;
1059   if (str_len (s) % 8)
1060     return 0;
1061   while (*s)
1062     if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1063       return 0;
1064
1065   return 1;
1066 }
1067
1068 /* This routine attempts to find the keyids of the recipients of a message.
1069  * It returns NULL if any of the keys can not be found.
1070  */
1071 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1072 {
1073   char *keyID, *keylist = NULL, *t;
1074   size_t keylist_size = 0;
1075   size_t keylist_used = 0;
1076   ADDRESS *tmp = NULL, *addr = NULL;
1077   ADDRESS **last = &tmp;
1078   ADDRESS *p, *q;
1079   int i;
1080   pgp_key_t k_info = NULL, key = NULL;
1081
1082   const char *fqdn = mutt_fqdn (1);
1083
1084   for (i = 0; i < 3; i++) {
1085     switch (i) {
1086     case 0:
1087       p = to;
1088       break;
1089     case 1:
1090       p = cc;
1091       break;
1092     case 2:
1093       p = bcc;
1094       break;
1095     default:
1096       abort ();
1097     }
1098
1099     *last = rfc822_cpy_adr (p);
1100     while (*last)
1101       last = &((*last)->next);
1102   }
1103
1104   if (fqdn)
1105     rfc822_qualify (tmp, fqdn);
1106
1107   tmp = mutt_remove_duplicates (tmp);
1108
1109   for (p = tmp; p; p = p->next) {
1110     char buf[LONG_STRING];
1111
1112     q = p;
1113     k_info = NULL;
1114
1115     if ((keyID = mutt_crypt_hook (p)) != NULL) {
1116       int r;
1117
1118       snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1119                 p->mailbox);
1120       if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1121         if (is_numerical_keyid (keyID)) {
1122           if (strncmp (keyID, "0x", 2) == 0)
1123             keyID += 2;
1124           goto bypass_selection;        /* you don't see this. */
1125         }
1126
1127         /* check for e-mail address */
1128         if ((t = strchr (keyID, '@')) &&
1129             (addr = rfc822_parse_adrlist (NULL, keyID))) {
1130           if (fqdn)
1131             rfc822_qualify (addr, fqdn);
1132           q = addr;
1133         }
1134         else
1135           k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1136       }
1137       else if (r == -1) {
1138         mem_free (&keylist);
1139         rfc822_free_address (&tmp);
1140         rfc822_free_address (&addr);
1141         return NULL;
1142       }
1143     }
1144
1145     if (k_info == NULL)
1146       pgp_invoke_getkeys (q);
1147
1148     if (k_info == NULL
1149         && (k_info =
1150             pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1151       snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1152
1153       if ((key = pgp_ask_for_key (buf, q->mailbox,
1154                                   KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1155         mem_free (&keylist);
1156         rfc822_free_address (&tmp);
1157         rfc822_free_address (&addr);
1158         return NULL;
1159       }
1160     }
1161     else
1162       key = k_info;
1163
1164     keyID = pgp_keyid (key);
1165
1166   bypass_selection:
1167     keylist_size += str_len (keyID) + 4;
1168     mem_realloc (&keylist, keylist_size);
1169     sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1170              keyID);
1171     keylist_used = str_len (keylist);
1172
1173     pgp_free_key (&key);
1174     rfc822_free_address (&addr);
1175
1176   }
1177   rfc822_free_address (&tmp);
1178   return (keylist);
1179 }
1180
1181 /* Warning: "a" is no longer freed in this routine, you need
1182  * to free it later.  This is necessary for $fcc_attach. */
1183
1184 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1185 {
1186   char buf[LONG_STRING];
1187   char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1188   char pgpinfile[_POSIX_PATH_MAX];
1189   FILE *pgpin, *pgperr, *fpout, *fptmp;
1190   BODY *t;
1191   int err = 0;
1192   int empty = 0;
1193   pid_t thepid;
1194
1195   mutt_mktemp (tempfile);
1196   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1197     mutt_perror (tempfile);
1198     return (NULL);
1199   }
1200
1201   mutt_mktemp (pgperrfile);
1202   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1203     mutt_perror (pgperrfile);
1204     unlink (tempfile);
1205     fclose (fpout);
1206     return NULL;
1207   }
1208   unlink (pgperrfile);
1209
1210   mutt_mktemp (pgpinfile);
1211   if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1212     mutt_perror (pgpinfile);
1213     unlink (tempfile);
1214     fclose (fpout);
1215     fclose (pgperr);
1216     return NULL;
1217   }
1218
1219   if (sign)
1220     convert_to_7bit (a);
1221
1222   mutt_write_mime_header (a, fptmp);
1223   fputc ('\n', fptmp);
1224   mutt_write_mime_body (a, fptmp);
1225   fclose (fptmp);
1226
1227   if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1228                                     fileno (fpout), fileno (pgperr),
1229                                     pgpinfile, keylist, sign)) == -1) {
1230     fclose (pgperr);
1231     unlink (pgpinfile);
1232     return (NULL);
1233   }
1234
1235   if (sign) {
1236     if (!pgp_use_gpg_agent ())
1237       fputs (PgpPass, pgpin);
1238     fputc ('\n', pgpin);
1239   }
1240   fclose (pgpin);
1241
1242   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1243     empty = 1;
1244
1245   unlink (pgpinfile);
1246
1247   fflush (fpout);
1248   rewind (fpout);
1249   if (!empty)
1250     empty = (fgetc (fpout) == EOF);
1251   fclose (fpout);
1252
1253   fflush (pgperr);
1254   rewind (pgperr);
1255   while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1256     err = 1;
1257     fputs (buf, stdout);
1258   }
1259   fclose (pgperr);
1260
1261   /* pause if there is any error output from PGP */
1262   if (err)
1263     mutt_any_key_to_continue (NULL);
1264
1265   if (empty) {
1266     /* fatal error while trying to encrypt message */
1267     if (sign)
1268       pgp_void_passphrase (); /* just in case */
1269     unlink (tempfile);
1270     return (NULL);
1271   }
1272
1273   t = mutt_new_body ();
1274   t->type = TYPEMULTIPART;
1275   t->subtype = str_dup ("encrypted");
1276   t->encoding = ENC7BIT;
1277   t->use_disp = 0;
1278   t->disposition = DISPINLINE;
1279
1280   mutt_generate_boundary (&t->parameter);
1281   mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1282
1283   t->parts = mutt_new_body ();
1284   t->parts->type = TYPEAPPLICATION;
1285   t->parts->subtype = str_dup ("pgp-encrypted");
1286   t->parts->encoding = ENC7BIT;
1287
1288   t->parts->next = mutt_new_body ();
1289   t->parts->next->type = TYPEAPPLICATION;
1290   t->parts->next->subtype = str_dup ("octet-stream");
1291   t->parts->next->encoding = ENC7BIT;
1292   t->parts->next->filename = str_dup (tempfile);
1293   t->parts->next->use_disp = 1;
1294   t->parts->next->disposition = DISPINLINE;
1295   t->parts->next->unlink = 1;   /* delete after sending the message */
1296   t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1297
1298   return (t);
1299 }
1300
1301 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1302 {
1303   BODY *b;
1304
1305   char pgpoutfile[_POSIX_PATH_MAX];
1306   char pgperrfile[_POSIX_PATH_MAX];
1307   char pgpinfile[_POSIX_PATH_MAX];
1308
1309   char body_charset[STRING];
1310   char *from_charset;
1311   const char *send_charset;
1312
1313   FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1314   FILE *fp;
1315
1316   int empty = 0;
1317   int err;
1318
1319   char buff[STRING];
1320
1321   pid_t thepid;
1322
1323   if (a->type != TYPETEXT)
1324     return NULL;
1325   if (ascii_strcasecmp (a->subtype, "plain"))
1326     return NULL;
1327
1328   if ((fp = fopen (a->filename, "r")) == NULL) {
1329     mutt_perror (a->filename);
1330     return NULL;
1331   }
1332
1333   mutt_mktemp (pgpinfile);
1334   if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1335     mutt_perror (pgpinfile);
1336     fclose (fp);
1337     return NULL;
1338   }
1339
1340   /* The following code is really correct:  If noconv is set,
1341    * a's charset parameter contains the on-disk character set, and
1342    * we have to convert from that to utf-8.  If noconv is not set,
1343    * we have to convert from $charset to utf-8.
1344    */
1345
1346   mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1347   if (a->noconv)
1348     from_charset = body_charset;
1349   else
1350     from_charset = Charset;
1351
1352   if (!mutt_is_us_ascii (body_charset)) {
1353     int c;
1354     FGETCONV *fc;
1355
1356     if (flags & ENCRYPT)
1357       send_charset = "us-ascii";
1358     else
1359       send_charset = "utf-8";
1360
1361     fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1362     while ((c = fgetconv (fc)) != EOF)
1363       fputc (c, pgpin);
1364
1365     fgetconv_close (&fc);
1366   }
1367   else {
1368     send_charset = "us-ascii";
1369     mutt_copy_stream (fp, pgpin);
1370   }
1371   safe_fclose (&fp);
1372   fclose (pgpin);
1373
1374   mutt_mktemp (pgpoutfile);
1375   mutt_mktemp (pgperrfile);
1376   if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1377       (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1378     mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1379     unlink (pgpinfile);
1380     if (pgpout) {
1381       fclose (pgpout);
1382       unlink (pgpoutfile);
1383     }
1384     return NULL;
1385   }
1386
1387   unlink (pgperrfile);
1388
1389   if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1390                                         -1, fileno (pgpout), fileno (pgperr),
1391                                         pgpinfile, keylist, flags)) == -1) {
1392     mutt_perror (_("Can't invoke PGP"));
1393
1394     fclose (pgpout);
1395     fclose (pgperr);
1396     mutt_unlink (pgpinfile);
1397     unlink (pgpoutfile);
1398     return NULL;
1399   }
1400
1401   if (pgp_use_gpg_agent ())
1402     *PgpPass = 0;
1403   if (flags & SIGN)
1404     fprintf (pgpin, "%s\n", PgpPass);
1405   fclose (pgpin);
1406
1407   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1408     empty = 1;
1409
1410   mutt_unlink (pgpinfile);
1411
1412   fflush (pgpout);
1413   fflush (pgperr);
1414
1415   rewind (pgpout);
1416   rewind (pgperr);
1417
1418   if (!empty)
1419     empty = (fgetc (pgpout) == EOF);
1420   fclose (pgpout);
1421
1422   err = 0;
1423
1424   while (fgets (buff, sizeof (buff), pgperr)) {
1425     err = 1;
1426     fputs (buff, stdout);
1427   }
1428
1429   fclose (pgperr);
1430
1431   if (err)
1432     mutt_any_key_to_continue (NULL);
1433
1434   if (empty) {
1435     if (flags & SIGN)
1436       pgp_void_passphrase (); /* just in case */
1437     unlink (pgpoutfile);
1438     return NULL;
1439   }
1440
1441   b = mutt_new_body ();
1442
1443   b->encoding = ENC7BIT;
1444
1445   b->type = TYPETEXT;
1446   b->subtype = str_dup ("plain");
1447
1448   mutt_set_parameter ("x-action",
1449                       flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1450                       &b->parameter);
1451   mutt_set_parameter ("charset", send_charset, &b->parameter);
1452
1453   b->filename = str_dup (pgpoutfile);
1454
1455 #if 0
1456   /* The following is intended to give a clue to some completely brain-dead 
1457    * "mail environments" which are typically used by large corporations.
1458    */
1459
1460   b->d_filename = str_dup ("msg.pgp");
1461   b->use_disp = 1;
1462
1463 #endif
1464
1465   b->disposition = DISPINLINE;
1466   b->unlink = 1;
1467
1468   b->noconv = 1;
1469   b->use_disp = 0;
1470
1471   if (!(flags & ENCRYPT))
1472     b->encoding = a->encoding;
1473
1474   return b;
1475 }
1476
1477 int pgp_send_menu (HEADER * msg, int *redraw)
1478 {
1479   pgp_key_t p;
1480   char input_signas[SHORT_STRING];
1481
1482   char prompt[LONG_STRING];
1483
1484   if (!(WithCrypto & APPLICATION_PGP))
1485     return msg->security;
1486
1487   /* If autoinline and no crypto options set, then set inline. */
1488   if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1489                                      && (msg->security & (SIGN | ENCRYPT))))
1490     msg->security |= INLINE;
1491
1492   snprintf (prompt, sizeof (prompt),
1493             _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1494             (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1495
1496   switch (mutt_multi_choice (prompt, _("esabifc"))) {
1497   case 1:                      /* (e)ncrypt */
1498     msg->security |= ENCRYPT;
1499     msg->security &= ~SIGN;
1500     break;
1501
1502   case 2:                      /* (s)ign */
1503     msg->security |= SIGN;
1504     msg->security &= ~ENCRYPT;
1505     break;
1506
1507   case 3:                      /* sign (a)s */
1508     unset_option (OPTPGPCHECKTRUST);
1509
1510     if ((p =
1511          pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1512                           PGP_PUBRING))) {
1513       snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1514       str_replace (&PgpSignAs, input_signas);
1515       pgp_free_key (&p);
1516
1517       msg->security |= SIGN;
1518
1519       crypt_pgp_void_passphrase ();     /* probably need a different passphrase */
1520     }
1521 #if 0
1522     else {
1523       msg->security &= ~SIGN;
1524     }
1525 #endif
1526
1527     *redraw = REDRAW_FULL;
1528     break;
1529
1530   case 4:                      /* (b)oth */
1531     msg->security |= (ENCRYPT | SIGN);
1532     break;
1533
1534   case 5:                      /* (i)nline */
1535     if ((msg->security & (ENCRYPT | SIGN)))
1536       msg->security ^= INLINE;
1537     else
1538       msg->security &= ~INLINE;
1539     break;
1540
1541   case 6:                      /* (f)orget it */
1542   case 7:                      /* (c)lear     */
1543     msg->security = 0;
1544     break;
1545   }
1546
1547   if (msg->security) {
1548     if (!(msg->security & (ENCRYPT | SIGN)))
1549       msg->security = 0;
1550     else
1551       msg->security |= APPLICATION_PGP;
1552   }
1553
1554   return (msg->security);
1555 }
1556
1557
1558 #endif /* CRYPT_BACKEND_CLASSIC_PGP */