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.
14 #include <lib-lib/lib-lib.h>
16 #include <lib-sys/unix.h>
17 #include <lib-mime/mime.h>
18 #include <lib-ui/curses.h>
20 #include "mutt_idna.h"
25 * The actual command line formatter.
28 struct pgp_command_context {
29 short need_passphrase; /* %p */
30 const char *fname; /* %f */
31 const char *sig_fname; /* %s */
32 const char *signas; /* %a */
33 const char *ids; /* %r */
38 _mutt_fmt_pgp_command(char *dest, ssize_t destlen,
39 char op, const char *src, const char *prefix,
40 const char *ifstring, const char *elsestring,
41 unsigned long data, format_flag flags)
44 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
45 int optional = (flags & M_FORMAT_OPTIONAL);
51 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
52 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
62 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
63 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
65 else if (!cctx->signas)
73 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
74 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
76 else if (!cctx->sig_fname)
84 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
85 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
87 else if (!cctx->fname)
95 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
96 snprintf (dest, destlen, fmt,
97 cctx->need_passphrase ? "PGPPASSFD=0" : "");
99 else if (!cctx->need_passphrase || pgp_use_gpg_agent ())
111 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data,
113 else if (flags & M_FORMAT_OPTIONAL)
114 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data,
121 mutt_pgp_command(char *d, ssize_t dlen, struct pgp_command_context *cctx,
124 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command,
125 (unsigned long) cctx, 0);
133 static pid_t pgp_invoke (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
134 int pgpinfd, int pgpoutfd, int pgperrfd,
135 short need_passphrase,
137 const char *sig_fname,
139 const char *ids, const char *format)
141 struct pgp_command_context cctx;
142 char cmd[HUGE_STRING];
146 if (!format || !*format)
149 cctx.need_passphrase = need_passphrase;
151 cctx.sig_fname = sig_fname;
152 cctx.signas = signas;
155 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
157 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
158 pgpinfd, pgpoutfd, pgperrfd);
163 * The exported interface.
165 * This is historic and may be removed at some point.
170 pid_t pgp_invoke_decode (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
171 int pgpinfd, int pgpoutfd, int pgperrfd,
172 const char *fname, short need_passphrase)
174 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
175 need_passphrase, fname, NULL, NULL, NULL,
179 pid_t pgp_invoke_verify (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
180 int pgpinfd, int pgpoutfd, int pgperrfd,
181 const char *fname, const char *sig_fname)
183 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
184 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
187 pid_t pgp_invoke_decrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
188 int pgpinfd, int pgpoutfd, int pgperrfd,
191 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
192 1, fname, NULL, NULL, NULL, PgpDecryptCommand);
195 pid_t pgp_invoke_sign (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
196 int pgpinfd, int pgpoutfd, int pgperrfd,
199 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
200 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
204 pid_t pgp_invoke_encrypt (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
205 int pgpinfd, int pgpoutfd, int pgperrfd,
206 const char *fname, const char *uids, int sign)
209 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
210 1, fname, NULL, PgpSignAs, uids,
211 PgpEncryptSignCommand);
213 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
214 0, fname, NULL, NULL, uids, PgpEncryptOnlyCommand);
217 pid_t pgp_invoke_traditional (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
218 int pgpinfd, int pgpoutfd, int pgperrfd,
219 const char *fname, const char *uids, int flags)
222 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
223 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
224 flags & SIGN ? PgpEncryptSignCommand :
225 PgpEncryptOnlyCommand);
227 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
228 1, fname, NULL, PgpSignAs, NULL, PgpClearSignCommand);
232 void pgp_invoke_import (const char *fname)
234 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
235 char cmd[HUGE_STRING];
236 struct pgp_command_context cctx;
240 mutt_quote_filename (_fname, sizeof (_fname), fname);
243 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
247 void pgp_invoke_getkeys (address_t * addr)
249 char buff[LONG_STRING];
250 char tmp[LONG_STRING];
251 char cmd[HUGE_STRING];
256 struct pgp_command_context cctx;
258 if (!PgpGetkeysCommand)
263 personal = addr->personal;
264 addr->personal = NULL;
267 mutt_addrlist_to_local (addr);
268 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
269 mutt_quote_filename (buff, sizeof (buff), tmp);
271 addr->personal = personal;
275 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
277 devnull = open ("/dev/null", O_RDWR);
280 mutt_message _("Fetching PGP key...");
290 pid_t pgp_invoke_export (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
291 int pgpinfd, int pgpoutfd, int pgperrfd,
294 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
295 0, NULL, NULL, NULL, uids, PgpExportCommand);
298 pid_t pgp_invoke_verify_key (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
299 int pgpinfd, int pgpoutfd, int pgperrfd,
302 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
303 0, NULL, NULL, NULL, uids, PgpVerifyKeyCommand);
306 pid_t pgp_invoke_list_keys (FILE ** pgpin, FILE ** pgpout, FILE ** pgperr,
307 int pgpinfd, int pgpoutfd, int pgperrfd,
308 pgp_ring_t keyring, string_list_t * hints)
310 char uids[HUGE_STRING];
311 char tmpuids[HUGE_STRING];
312 char quoted[HUGE_STRING];
316 for (; hints; hints = hints->next) {
317 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
318 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
319 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
322 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
323 0, NULL, NULL, NULL, uids,
324 keyring == PGP_SECRING ? PgpListSecringCommand :
325 PgpListPubringCommand);