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