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>
29 #include "mutt_curses.h"
30 #include "mutt_idna.h"
39 * The actual command line formatter.
42 struct pgp_command_context {
43 short need_passphrase; /* %p */
44 const char *fname; /* %f */
45 const char *sig_fname; /* %s */
46 const char *signas; /* %a */
47 const char *ids; /* %r */
51 const char *_mutt_fmt_pgp_command (char *dest,
57 const char *elsestring,
58 unsigned long data, format_flag flags)
61 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
62 int optional = (flags & M_FORMAT_OPTIONAL);
68 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
69 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
79 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
80 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
82 else if (!cctx->signas)
90 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
91 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
93 else if (!cctx->sig_fname)
101 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
102 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
104 else if (!cctx->fname)
112 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
113 snprintf (dest, destlen, fmt,
114 cctx->need_passphrase ? "PGPPASSFD=0" : "");
116 else if (!cctx->need_passphrase || pgp_use_gpg_agent ())
128 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data,
130 else if (flags & M_FORMAT_OPTIONAL)
131 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data,
137 void mutt_pgp_command (char *d, size_t dlen, struct pgp_command_context *cctx,
140 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command,
141 (unsigned long) cctx, 0);
142 dprint (2, (debugfile, "mutt_pgp_command: %s\n", d));
150 static pid_t pgp_invoke (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
151 int pgpinfd, int pgpoutfd, int pgperrfd,
152 short need_passphrase,
154 const char *sig_fname,
156 const char *ids, const char *format)
158 struct pgp_command_context cctx;
159 char cmd[HUGE_STRING];
161 memset (&cctx, 0, sizeof (cctx));
163 if (!format || !*format)
166 cctx.need_passphrase = need_passphrase;
168 cctx.sig_fname = sig_fname;
169 cctx.signas = signas;
172 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
174 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
175 pgpinfd, pgpoutfd, pgperrfd);
180 * The exported interface.
182 * This is historic and may be removed at some point.
187 pid_t pgp_invoke_decode (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
188 int pgpinfd, int pgpoutfd, int pgperrfd,
189 const char *fname, short need_passphrase)
191 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
192 need_passphrase, fname, NULL, NULL, NULL,
196 pid_t pgp_invoke_verify (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
197 int pgpinfd, int pgpoutfd, int pgperrfd,
198 const char *fname, const char *sig_fname)
200 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
201 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
204 pid_t pgp_invoke_decrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
205 int pgpinfd, int pgpoutfd, int pgperrfd,
208 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
209 1, fname, NULL, NULL, NULL, PgpDecryptCommand);
212 pid_t pgp_invoke_sign (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
213 int pgpinfd, int pgpoutfd, int pgperrfd,
216 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
217 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
221 pid_t pgp_invoke_encrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
222 int pgpinfd, int pgpoutfd, int pgperrfd,
223 const char *fname, const char *uids, int sign)
226 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
227 1, fname, NULL, PgpSignAs, uids,
228 PgpEncryptSignCommand);
230 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
231 0, fname, NULL, NULL, uids, PgpEncryptOnlyCommand);
234 pid_t pgp_invoke_traditional (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
235 int pgpinfd, int pgpoutfd, int pgperrfd,
236 const char *fname, const char *uids, int flags)
239 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
240 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
241 flags & SIGN ? PgpEncryptSignCommand :
242 PgpEncryptOnlyCommand);
244 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
245 1, fname, NULL, PgpSignAs, NULL, PgpClearSignCommand);
249 void pgp_invoke_import (const char *fname)
251 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
252 char cmd[HUGE_STRING];
253 struct pgp_command_context cctx;
255 memset (&cctx, 0, sizeof (cctx));
257 mutt_quote_filename (_fname, sizeof (_fname), fname);
260 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
264 void pgp_invoke_getkeys (ADDRESS * addr)
266 char buff[LONG_STRING];
267 char tmp[LONG_STRING];
268 char cmd[HUGE_STRING];
273 struct pgp_command_context cctx;
275 if (!PgpGetkeysCommand)
278 memset (&cctx, 0, sizeof (cctx));
280 personal = addr->personal;
281 addr->personal = NULL;
284 mutt_addrlist_to_local (addr);
285 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
286 mutt_quote_filename (buff, sizeof (buff), tmp);
288 addr->personal = personal;
292 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
294 devnull = open ("/dev/null", O_RDWR);
297 mutt_message _("Fetching PGP key...");
307 pid_t pgp_invoke_export (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
308 int pgpinfd, int pgpoutfd, int pgperrfd,
311 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
312 0, NULL, NULL, NULL, uids, PgpExportCommand);
315 pid_t pgp_invoke_verify_key (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
316 int pgpinfd, int pgpoutfd, int pgperrfd,
319 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
320 0, NULL, NULL, NULL, uids, PgpVerifyKeyCommand);
323 pid_t pgp_invoke_list_keys (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
324 int pgpinfd, int pgpoutfd, int pgperrfd,
325 pgp_ring_t keyring, LIST * hints)
327 char uids[HUGE_STRING];
328 char tmpuids[HUGE_STRING];
329 char quoted[HUGE_STRING];
333 for (; hints; hints = hints->next) {
334 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
335 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
336 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
339 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
340 0, NULL, NULL, NULL, uids,
341 keyring == PGP_SECRING ? PgpListSecringCommand :
342 PgpListPubringCommand);