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