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