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"
35 * The actual command line formatter.
38 struct pgp_command_context {
39 short need_passphrase; /* %p */
40 const char *fname; /* %f */
41 const char *sig_fname; /* %s */
42 const char *signas; /* %a */
43 const char *ids; /* %r */
47 const char *_mutt_fmt_pgp_command (char *dest,
53 const char *elsestring,
54 unsigned long data, format_flag flags)
57 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
58 int optional = (flags & M_FORMAT_OPTIONAL);
64 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
65 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
75 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
76 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
78 else if (!cctx->signas)
86 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
87 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
89 else if (!cctx->sig_fname)
97 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
98 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
100 else if (!cctx->fname)
108 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
109 snprintf (dest, destlen, fmt,
110 cctx->need_passphrase ? "PGPPASSFD=0" : "");
112 else if (!cctx->need_passphrase || pgp_use_gpg_agent ())
124 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data,
126 else if (flags & M_FORMAT_OPTIONAL)
127 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data,
133 void mutt_pgp_command (char *d, size_t dlen, struct pgp_command_context *cctx,
136 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command,
137 (unsigned long) cctx, 0);
138 dprint (2, (debugfile, "mutt_pgp_command: %s\n", d));
146 static pid_t pgp_invoke (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
147 int pgpinfd, int pgpoutfd, int pgperrfd,
148 short need_passphrase,
150 const char *sig_fname,
152 const char *ids, const char *format)
154 struct pgp_command_context cctx;
155 char cmd[HUGE_STRING];
157 memset (&cctx, 0, sizeof (cctx));
159 if (!format || !*format)
162 cctx.need_passphrase = need_passphrase;
164 cctx.sig_fname = sig_fname;
165 cctx.signas = signas;
168 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
170 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
171 pgpinfd, pgpoutfd, pgperrfd);
176 * The exported interface.
178 * This is historic and may be removed at some point.
183 pid_t pgp_invoke_decode (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
184 int pgpinfd, int pgpoutfd, int pgperrfd,
185 const char *fname, short need_passphrase)
187 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
188 need_passphrase, fname, NULL, NULL, NULL,
192 pid_t pgp_invoke_verify (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
193 int pgpinfd, int pgpoutfd, int pgperrfd,
194 const char *fname, const char *sig_fname)
196 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
197 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
200 pid_t pgp_invoke_decrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
201 int pgpinfd, int pgpoutfd, int pgperrfd,
204 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
205 1, fname, NULL, NULL, NULL, PgpDecryptCommand);
208 pid_t pgp_invoke_sign (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
209 int pgpinfd, int pgpoutfd, int pgperrfd,
212 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
213 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
217 pid_t pgp_invoke_encrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
218 int pgpinfd, int pgpoutfd, int pgperrfd,
219 const char *fname, const char *uids, int sign)
222 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
223 1, fname, NULL, PgpSignAs, uids,
224 PgpEncryptSignCommand);
226 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
227 0, fname, NULL, NULL, uids, PgpEncryptOnlyCommand);
230 pid_t pgp_invoke_traditional (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
231 int pgpinfd, int pgpoutfd, int pgperrfd,
232 const char *fname, const char *uids, int flags)
235 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
236 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
237 flags & SIGN ? PgpEncryptSignCommand :
238 PgpEncryptOnlyCommand);
240 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
241 1, fname, NULL, PgpSignAs, NULL, PgpClearSignCommand);
245 void pgp_invoke_import (const char *fname)
247 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
248 char cmd[HUGE_STRING];
249 struct pgp_command_context cctx;
251 memset (&cctx, 0, sizeof (cctx));
253 mutt_quote_filename (_fname, sizeof (_fname), fname);
256 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
260 void pgp_invoke_getkeys (ADDRESS * addr)
262 char buff[LONG_STRING];
263 char tmp[LONG_STRING];
264 char cmd[HUGE_STRING];
269 struct pgp_command_context cctx;
271 if (!PgpGetkeysCommand)
274 memset (&cctx, 0, sizeof (cctx));
276 personal = addr->personal;
277 addr->personal = NULL;
280 mutt_addrlist_to_local (addr);
281 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
282 mutt_quote_filename (buff, sizeof (buff), tmp);
284 addr->personal = personal;
288 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
290 devnull = open ("/dev/null", O_RDWR);
293 mutt_message _("Fetching PGP key...");
303 pid_t pgp_invoke_export (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
304 int pgpinfd, int pgpoutfd, int pgperrfd,
307 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
308 0, NULL, NULL, NULL, uids, PgpExportCommand);
311 pid_t pgp_invoke_verify_key (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
312 int pgpinfd, int pgpoutfd, int pgperrfd,
315 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
316 0, NULL, NULL, NULL, uids, PgpVerifyKeyCommand);
319 pid_t pgp_invoke_list_keys (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
320 int pgpinfd, int pgpoutfd, int pgperrfd,
321 pgp_ring_t keyring, LIST * hints)
323 char uids[HUGE_STRING];
324 char tmpuids[HUGE_STRING];
325 char quoted[HUGE_STRING];
329 for (; hints; hints = hints->next) {
330 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
331 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
332 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
335 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
336 0, NULL, NULL, NULL, uids,
337 keyring == PGP_SECRING ? PgpListSecringCommand :
338 PgpListPubringCommand);