2 * Copyright (C) 1997-2000 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111, USA.
22 /* This file contains the new pgp invocation code. Note that this
23 * is almost entirely format based.
26 #include <sys/types.h>
37 #include "mutt_curses.h"
38 #include "mutt_idna.h"
43 * The actual command line formatter.
46 struct pgp_command_context {
47 short need_passphrase; /* %p */
48 const char *fname; /* %f */
49 const char *sig_fname; /* %s */
50 const char *signas; /* %a */
51 const char *ids; /* %r */
55 const char *_mutt_fmt_pgp_command (char *dest,
61 const char *elsestring,
66 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
67 int optional = (flags & M_FORMAT_OPTIONAL);
75 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
76 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
87 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
88 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
90 else if (!cctx->signas)
99 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
100 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
102 else if (!cctx->sig_fname)
111 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
112 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
114 else if (!cctx->fname)
123 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
124 snprintf (dest, destlen, fmt, cctx->need_passphrase ? "PGPPASSFD=0" : "");
126 else if (!cctx->need_passphrase || pgp_use_gpg_agent())
138 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data, 0);
139 else if (flags & M_FORMAT_OPTIONAL)
140 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data, 0);
145 void mutt_pgp_command (char *d, size_t dlen, struct pgp_command_context *cctx, const char *fmt)
147 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command, (unsigned long) cctx, 0);
148 dprint (2, (debugfile, "mutt_pgp_command: %s\n", d));
156 static pid_t pgp_invoke (FILE **pgpin, FILE **pgpout, FILE **pgperr,
157 int pgpinfd, int pgpoutfd, int pgperrfd,
158 short need_passphrase,
160 const char *sig_fname,
165 struct pgp_command_context cctx;
166 char cmd[HUGE_STRING];
168 memset (&cctx, 0, sizeof (cctx));
170 if (!format || !*format)
173 cctx.need_passphrase = need_passphrase;
175 cctx.sig_fname = sig_fname;
176 cctx.signas = signas;
179 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
181 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
182 pgpinfd, pgpoutfd, pgperrfd);
187 * The exported interface.
189 * This is historic and may be removed at some point.
194 pid_t pgp_invoke_decode (FILE **pgpin, FILE **pgpout, FILE **pgperr,
195 int pgpinfd, int pgpoutfd, int pgperrfd,
196 const char *fname, short need_passphrase)
198 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
199 need_passphrase, fname, NULL, NULL, NULL,
203 pid_t pgp_invoke_verify (FILE **pgpin, FILE **pgpout, FILE **pgperr,
204 int pgpinfd, int pgpoutfd, int pgperrfd,
205 const char *fname, const char *sig_fname)
207 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
208 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
211 pid_t pgp_invoke_decrypt (FILE **pgpin, FILE **pgpout, FILE **pgperr,
212 int pgpinfd, int pgpoutfd, int pgperrfd,
215 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
216 1, fname, NULL, NULL, NULL, PgpDecryptCommand);
219 pid_t pgp_invoke_sign (FILE **pgpin, FILE **pgpout, FILE **pgperr,
220 int pgpinfd, int pgpoutfd, int pgperrfd,
223 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
224 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
228 pid_t pgp_invoke_encrypt (FILE **pgpin, FILE **pgpout, FILE **pgperr,
229 int pgpinfd, int pgpoutfd, int pgperrfd,
230 const char *fname, const char *uids, int sign)
233 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
234 1, fname, NULL, PgpSignAs, uids,
235 PgpEncryptSignCommand);
237 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
238 0, fname, NULL, NULL, uids,
239 PgpEncryptOnlyCommand);
242 pid_t pgp_invoke_traditional (FILE **pgpin, FILE **pgpout, FILE **pgperr,
243 int pgpinfd, int pgpoutfd, int pgperrfd,
244 const char *fname, const char *uids, int flags)
247 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
248 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
249 flags & SIGN ? PgpEncryptSignCommand : PgpEncryptOnlyCommand);
251 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
252 1, fname, NULL, PgpSignAs, NULL,
253 PgpClearSignCommand);
257 void pgp_invoke_import (const char *fname)
259 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
260 char cmd[HUGE_STRING];
261 struct pgp_command_context cctx;
263 memset (&cctx, 0, sizeof (cctx));
265 mutt_quote_filename (_fname, sizeof (_fname), fname);
268 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
272 void pgp_invoke_getkeys (ADDRESS *addr)
274 char buff[LONG_STRING];
275 char tmp[LONG_STRING];
276 char cmd[HUGE_STRING];
281 struct pgp_command_context cctx;
283 if (!PgpGetkeysCommand) return;
285 memset (&cctx, 0, sizeof (cctx));
287 personal = addr->personal;
288 addr->personal = NULL;
291 mutt_addrlist_to_local (addr);
292 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
293 mutt_quote_filename (buff, sizeof (buff), tmp);
295 addr->personal = personal;
299 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
301 devnull = open ("/dev/null", O_RDWR);
303 if (!isendwin ()) mutt_message _("Fetching PGP key...");
307 if (!isendwin ()) mutt_clear_error ();
312 pid_t pgp_invoke_export (FILE **pgpin, FILE **pgpout, FILE **pgperr,
313 int pgpinfd, int pgpoutfd, int pgperrfd,
316 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
317 0, NULL, NULL, NULL, uids,
321 pid_t pgp_invoke_verify_key (FILE **pgpin, FILE **pgpout, FILE **pgperr,
322 int pgpinfd, int pgpoutfd, int pgperrfd,
325 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
326 0, NULL, NULL, NULL, uids,
327 PgpVerifyKeyCommand);
330 pid_t pgp_invoke_list_keys (FILE **pgpin, FILE **pgpout, FILE **pgperr,
331 int pgpinfd, int pgpoutfd, int pgperrfd,
332 pgp_ring_t keyring, LIST *hints)
334 char uids[HUGE_STRING];
335 char tmpuids[HUGE_STRING];
336 char quoted[HUGE_STRING];
340 for (; hints; hints = hints->next)
342 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
343 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
344 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
347 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
348 0, NULL, NULL, NULL, uids,
349 keyring == PGP_SECRING ? PgpListSecringCommand :
350 PgpListPubringCommand);