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