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