2 * Copyright notice from original mutt:
3 * Copyright (C) 1997-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* This file contains the new pgp invocation code. Note that this
11 * is almost entirely format based.
18 #include <sys/types.h>
28 #include <lib-lib/mem.h>
29 #include <lib-lib/str.h>
30 #include <lib-lib/macros.h>
31 #include <lib-lib/file.h>
34 #include "mutt_curses.h"
35 #include "mutt_idna.h"
39 #include "lib/debug.h"
42 * The actual command line formatter.
45 struct pgp_command_context {
46 short need_passphrase; /* %p */
47 const char *fname; /* %f */
48 const char *sig_fname; /* %s */
49 const char *signas; /* %a */
50 const char *ids; /* %r */
54 const char *_mutt_fmt_pgp_command (char *dest,
60 const char *elsestring,
61 unsigned long data, format_flag flags)
64 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
65 int optional = (flags & M_FORMAT_OPTIONAL);
71 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
72 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
82 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
83 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
85 else if (!cctx->signas)
93 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
94 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
96 else if (!cctx->sig_fname)
104 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
105 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
107 else if (!cctx->fname)
115 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
116 snprintf (dest, destlen, fmt,
117 cctx->need_passphrase ? "PGPPASSFD=0" : "");
119 else if (!cctx->need_passphrase || pgp_use_gpg_agent ())
131 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data,
133 else if (flags & M_FORMAT_OPTIONAL)
134 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data,
140 void mutt_pgp_command (char *d, size_t dlen, struct pgp_command_context *cctx,
143 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command,
144 (unsigned long) cctx, 0);
145 debug_print (2, ("%s\n", d));
153 static pid_t pgp_invoke (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
154 int pgpinfd, int pgpoutfd, int pgperrfd,
155 short need_passphrase,
157 const char *sig_fname,
159 const char *ids, const char *format)
161 struct pgp_command_context cctx;
162 char cmd[HUGE_STRING];
166 if (!format || !*format)
169 cctx.need_passphrase = need_passphrase;
171 cctx.sig_fname = sig_fname;
172 cctx.signas = signas;
175 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
177 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
178 pgpinfd, pgpoutfd, pgperrfd);
183 * The exported interface.
185 * This is historic and may be removed at some point.
190 pid_t pgp_invoke_decode (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
191 int pgpinfd, int pgpoutfd, int pgperrfd,
192 const char *fname, short need_passphrase)
194 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
195 need_passphrase, fname, NULL, NULL, NULL,
199 pid_t pgp_invoke_verify (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
200 int pgpinfd, int pgpoutfd, int pgperrfd,
201 const char *fname, const char *sig_fname)
203 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
204 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
207 pid_t pgp_invoke_decrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
208 int pgpinfd, int pgpoutfd, int pgperrfd,
211 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
212 1, fname, NULL, NULL, NULL, PgpDecryptCommand);
215 pid_t pgp_invoke_sign (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
216 int pgpinfd, int pgpoutfd, int pgperrfd,
219 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
220 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
224 pid_t pgp_invoke_encrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
225 int pgpinfd, int pgpoutfd, int pgperrfd,
226 const char *fname, const char *uids, int sign)
229 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
230 1, fname, NULL, PgpSignAs, uids,
231 PgpEncryptSignCommand);
233 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
234 0, fname, NULL, NULL, uids, PgpEncryptOnlyCommand);
237 pid_t pgp_invoke_traditional (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
238 int pgpinfd, int pgpoutfd, int pgperrfd,
239 const char *fname, const char *uids, int flags)
242 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
243 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
244 flags & SIGN ? PgpEncryptSignCommand :
245 PgpEncryptOnlyCommand);
247 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
248 1, fname, NULL, PgpSignAs, NULL, PgpClearSignCommand);
252 void pgp_invoke_import (const char *fname)
254 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
255 char cmd[HUGE_STRING];
256 struct pgp_command_context cctx;
260 mutt_quote_filename (_fname, sizeof (_fname), fname);
263 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
267 void pgp_invoke_getkeys (ADDRESS * addr)
269 char buff[LONG_STRING];
270 char tmp[LONG_STRING];
271 char cmd[HUGE_STRING];
276 struct pgp_command_context cctx;
278 if (!PgpGetkeysCommand)
283 personal = addr->personal;
284 addr->personal = NULL;
287 mutt_addrlist_to_local (addr);
288 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
289 mutt_quote_filename (buff, sizeof (buff), tmp);
291 addr->personal = personal;
295 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
297 devnull = open ("/dev/null", O_RDWR);
300 mutt_message _("Fetching PGP key...");
310 pid_t pgp_invoke_export (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
311 int pgpinfd, int pgpoutfd, int pgperrfd,
314 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
315 0, NULL, NULL, NULL, uids, PgpExportCommand);
318 pid_t pgp_invoke_verify_key (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
319 int pgpinfd, int pgpoutfd, int pgperrfd,
322 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
323 0, NULL, NULL, NULL, uids, PgpVerifyKeyCommand);
326 pid_t pgp_invoke_list_keys (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
327 int pgpinfd, int pgpoutfd, int pgperrfd,
328 pgp_ring_t keyring, LIST * hints)
330 char uids[HUGE_STRING];
331 char tmpuids[HUGE_STRING];
332 char quoted[HUGE_STRING];
336 for (; hints; hints = hints->next) {
337 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
338 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
339 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
342 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
343 0, NULL, NULL, NULL, uids,
344 keyring == PGP_SECRING ? PgpListSecringCommand :
345 PgpListPubringCommand);