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.
30 #include <sys/types.h>
41 #include "mutt_curses.h"
42 #include "mutt_idna.h"
47 * The actual command line formatter.
50 struct pgp_command_context {
51 short need_passphrase; /* %p */
52 const char *fname; /* %f */
53 const char *sig_fname; /* %s */
54 const char *signas; /* %a */
55 const char *ids; /* %r */
59 const char *_mutt_fmt_pgp_command (char *dest,
65 const char *elsestring,
70 struct pgp_command_context *cctx = (struct pgp_command_context *) data;
71 int optional = (flags & M_FORMAT_OPTIONAL);
79 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
80 snprintf (dest, destlen, fmt, NONULL (cctx->ids));
91 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
92 snprintf (dest, destlen, fmt, NONULL (cctx->signas));
94 else if (!cctx->signas)
103 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
104 snprintf (dest, destlen, fmt, NONULL (cctx->sig_fname));
106 else if (!cctx->sig_fname)
115 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
116 snprintf (dest, destlen, fmt, NONULL (cctx->fname));
118 else if (!cctx->fname)
127 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
128 snprintf (dest, destlen, fmt, cctx->need_passphrase ? "PGPPASSFD=0" : "");
130 else if (!cctx->need_passphrase || pgp_use_gpg_agent())
142 mutt_FormatString (dest, destlen, ifstring, _mutt_fmt_pgp_command, data, 0);
143 else if (flags & M_FORMAT_OPTIONAL)
144 mutt_FormatString (dest, destlen, elsestring, _mutt_fmt_pgp_command, data, 0);
149 void mutt_pgp_command (char *d, size_t dlen, struct pgp_command_context *cctx, const char *fmt)
151 mutt_FormatString (d, dlen, NONULL (fmt), _mutt_fmt_pgp_command, (unsigned long) cctx, 0);
152 dprint (2, (debugfile, "mutt_pgp_command: %s\n", d));
160 static pid_t pgp_invoke (FILE **pgpin, FILE **pgpout, FILE **pgperr,
161 int pgpinfd, int pgpoutfd, int pgperrfd,
162 short need_passphrase,
164 const char *sig_fname,
169 struct pgp_command_context cctx;
170 char cmd[HUGE_STRING];
172 memset (&cctx, 0, sizeof (cctx));
174 if (!format || !*format)
177 cctx.need_passphrase = need_passphrase;
179 cctx.sig_fname = sig_fname;
180 cctx.signas = signas;
183 mutt_pgp_command (cmd, sizeof (cmd), &cctx, format);
185 return mutt_create_filter_fd (cmd, pgpin, pgpout, pgperr,
186 pgpinfd, pgpoutfd, pgperrfd);
191 * The exported interface.
193 * This is historic and may be removed at some point.
198 pid_t pgp_invoke_decode (FILE **pgpin, FILE **pgpout, FILE **pgperr,
199 int pgpinfd, int pgpoutfd, int pgperrfd,
200 const char *fname, short need_passphrase)
202 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
203 need_passphrase, fname, NULL, NULL, NULL,
207 pid_t pgp_invoke_verify (FILE **pgpin, FILE **pgpout, FILE **pgperr,
208 int pgpinfd, int pgpoutfd, int pgperrfd,
209 const char *fname, const char *sig_fname)
211 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
212 0, fname, sig_fname, NULL, NULL, PgpVerifyCommand);
215 pid_t pgp_invoke_decrypt (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, NULL, NULL, PgpDecryptCommand);
223 pid_t pgp_invoke_sign (FILE **pgpin, FILE **pgpout, FILE **pgperr,
224 int pgpinfd, int pgpoutfd, int pgperrfd,
227 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
228 1, fname, NULL, PgpSignAs, NULL, PgpSignCommand);
232 pid_t pgp_invoke_encrypt (FILE **pgpin, FILE **pgpout, FILE **pgperr,
233 int pgpinfd, int pgpoutfd, int pgperrfd,
234 const char *fname, const char *uids, int sign)
237 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
238 1, fname, NULL, PgpSignAs, uids,
239 PgpEncryptSignCommand);
241 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
242 0, fname, NULL, NULL, uids,
243 PgpEncryptOnlyCommand);
246 pid_t pgp_invoke_traditional (FILE **pgpin, FILE **pgpout, FILE **pgperr,
247 int pgpinfd, int pgpoutfd, int pgperrfd,
248 const char *fname, const char *uids, int flags)
251 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
252 flags & SIGN ? 1 : 0, fname, NULL, PgpSignAs, uids,
253 flags & SIGN ? PgpEncryptSignCommand : PgpEncryptOnlyCommand);
255 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
256 1, fname, NULL, PgpSignAs, NULL,
257 PgpClearSignCommand);
261 void pgp_invoke_import (const char *fname)
263 char _fname[_POSIX_PATH_MAX + SHORT_STRING];
264 char cmd[HUGE_STRING];
265 struct pgp_command_context cctx;
267 memset (&cctx, 0, sizeof (cctx));
269 mutt_quote_filename (_fname, sizeof (_fname), fname);
272 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpImportCommand);
276 void pgp_invoke_getkeys (ADDRESS *addr)
278 char buff[LONG_STRING];
279 char tmp[LONG_STRING];
280 char cmd[HUGE_STRING];
285 struct pgp_command_context cctx;
287 if (!PgpGetkeysCommand) return;
289 memset (&cctx, 0, sizeof (cctx));
291 personal = addr->personal;
292 addr->personal = NULL;
295 mutt_addrlist_to_local (addr);
296 rfc822_write_address_single (tmp, sizeof (tmp), addr, 0);
297 mutt_quote_filename (buff, sizeof (buff), tmp);
299 addr->personal = personal;
303 mutt_pgp_command (cmd, sizeof (cmd), &cctx, PgpGetkeysCommand);
305 devnull = open ("/dev/null", O_RDWR);
307 if (!isendwin ()) mutt_message _("Fetching PGP key...");
311 if (!isendwin ()) mutt_clear_error ();
316 pid_t pgp_invoke_export (FILE **pgpin, FILE **pgpout, FILE **pgperr,
317 int pgpinfd, int pgpoutfd, int pgperrfd,
320 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
321 0, NULL, NULL, NULL, uids,
325 pid_t pgp_invoke_verify_key (FILE **pgpin, FILE **pgpout, FILE **pgperr,
326 int pgpinfd, int pgpoutfd, int pgperrfd,
329 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
330 0, NULL, NULL, NULL, uids,
331 PgpVerifyKeyCommand);
334 pid_t pgp_invoke_list_keys (FILE **pgpin, FILE **pgpout, FILE **pgperr,
335 int pgpinfd, int pgpoutfd, int pgperrfd,
336 pgp_ring_t keyring, LIST *hints)
338 char uids[HUGE_STRING];
339 char tmpuids[HUGE_STRING];
340 char quoted[HUGE_STRING];
344 for (; hints; hints = hints->next)
346 mutt_quote_filename (quoted, sizeof (quoted), (char *) hints->data);
347 snprintf (tmpuids, sizeof (tmpuids), "%s %s", uids, quoted);
348 strcpy (uids, tmpuids); /* __STRCPY_CHECKED__ */
351 return pgp_invoke (pgpin, pgpout, pgperr, pgpinfd, pgpoutfd, pgperrfd,
352 0, NULL, NULL, NULL, uids,
353 keyring == PGP_SECRING ? PgpListSecringCommand :
354 PgpListPubringCommand);