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