1 /* call-dirmngr.c - Interact with the Dirmngr.
2 * Copyright (C) 2016 g10 Code GmbH
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
35 #include "mbox-util.h"
36 #include "./call-dirmngr.h"
48 set_dirmngr_options (int verbose, int debug_ipc, int autostart)
50 opt.verbose = verbose;
51 opt.debug_ipc = debug_ipc;
52 opt.autostart = autostart;
56 /* Connect to the Dirmngr and return an assuan context. */
58 connect_dirmngr (assuan_context_t *r_ctx)
64 err = start_new_dirmngr (&ctx,
65 GPG_ERR_SOURCE_DEFAULT,
67 opt.autostart, opt.verbose, opt.debug_ipc,
69 if (!opt.autostart && gpg_err_code (err) == GPG_ERR_NO_DIRMNGR)
76 log_info (_("no dirmngr running in this session\n"));
93 /* Parameter structure used with the WKD_GET command. */
100 /* Data callback for the WKD_GET command. */
102 wkd_get_data_cb (void *opaque, const void *data, size_t datalen)
104 struct wkd_get_parm_s *parm = opaque;
109 return 0; /* Ignore END commands. */
111 return 0; /* Data is not required. */
113 if (es_write (parm->memfp, data, datalen, &nwritten))
114 err = gpg_error_from_syserror ();
120 /* Status callback for the WKD_GET command. */
122 wkd_get_status_cb (void *opaque, const char *line)
124 struct wkd_get_parm_s *parm = opaque;
134 /* Ask the dirmngr for the submission address of a WKD server for the
135 * mail address ADDRSPEC. On success the submission address is stored
138 wkd_get_submission_address (const char *addrspec, char **r_addrspec)
141 assuan_context_t ctx;
142 struct wkd_get_parm_s parm;
148 memset (&parm, 0, sizeof parm);
151 err = connect_dirmngr (&ctx);
155 line = es_bsprintf ("WKD_GET --submission-address -- %s", addrspec);
158 err = gpg_error_from_syserror ();
161 if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
163 err = gpg_error (GPG_ERR_TOO_LARGE);
167 parm.memfp = es_fopenmem (0, "rwb");
170 err = gpg_error_from_syserror ();
173 err = assuan_transact (ctx, line, wkd_get_data_cb, &parm,
174 NULL, NULL, wkd_get_status_cb, &parm);
178 es_fputc (0, parm.memfp);
179 if (es_fclose_snatch (parm.memfp, &vp, NULL))
181 err = gpg_error_from_syserror ();
186 p = strchr (buffer, '\n');
189 trim_spaces (buffer);
190 if (!is_valid_mailbox (buffer))
192 err = gpg_error (GPG_ERR_INV_USER_ID);
195 *r_addrspec = xtrystrdup (buffer);
197 err = gpg_error_from_syserror ();
201 es_fclose (parm.memfp);
203 assuan_release (ctx);
208 /* Ask the dirmngr for the policy flags and return them as an estream
209 * memory stream. If no policy flags are set, NULL is stored at
212 wkd_get_policy_flags (const char *addrspec, estream_t *r_buffer)
215 assuan_context_t ctx;
216 struct wkd_get_parm_s parm;
220 memset (&parm, 0, sizeof parm);
223 err = connect_dirmngr (&ctx);
227 line = es_bsprintf ("WKD_GET --policy-flags -- %s", addrspec);
230 err = gpg_error_from_syserror ();
233 if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
235 err = gpg_error (GPG_ERR_TOO_LARGE);
239 parm.memfp = es_fopenmem (0, "rwb");
242 err = gpg_error_from_syserror ();
245 err = assuan_transact (ctx, line, wkd_get_data_cb, &parm,
246 NULL, NULL, wkd_get_status_cb, &parm);
250 es_rewind (parm.memfp);
251 *r_buffer = parm.memfp;
256 es_fclose (parm.memfp);
258 assuan_release (ctx);
263 /* Ask the dirmngr for the key for ADDRSPEC. On success a stream with
264 * the key is stored at R_KEY. */
266 wkd_get_key (const char *addrspec, estream_t *r_key)
269 assuan_context_t ctx;
270 struct wkd_get_parm_s parm;
273 memset (&parm, 0, sizeof parm);
276 err = connect_dirmngr (&ctx);
280 line = es_bsprintf ("WKD_GET -- %s", addrspec);
283 err = gpg_error_from_syserror ();
286 if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
288 err = gpg_error (GPG_ERR_TOO_LARGE);
292 parm.memfp = es_fopenmem (0, "rwb");
295 err = gpg_error_from_syserror ();
298 err = assuan_transact (ctx, line, wkd_get_data_cb, &parm,
299 NULL, NULL, wkd_get_status_cb, &parm);
303 es_rewind (parm.memfp);
308 es_fclose (parm.memfp);
310 assuan_release (ctx);