1 /* call-dirmngr.c - GPG operations to the Dirmngr.
2 * Copyright (C) 2011 Free Software Foundation, Inc.
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 <http://www.gnu.org/licenses/>.
39 #include "keyserver.h"
40 #include "call-dirmngr.h"
43 /* Parameter structure used to gather status info. */
44 struct ks_status_parm_s
50 /* Parameter structure used with the KS_SEARCH command. */
51 struct ks_search_parm_s
53 gpg_error_t lasterr; /* Last error code. */
54 membuf_t saveddata; /* Buffer to build complete lines. */
55 char *helpbuf; /* NULL or malloced buffer. */
56 size_t helpbufsize; /* Allocated size of HELPBUF. */
57 gpg_error_t (*data_cb)(void*, int, char*); /* Callback. */
58 void *data_cb_value; /* First argument for DATA_CB. */
59 struct ks_status_parm_s *stparm; /* Link to the status parameter. */
63 /* Parameter structure used with the KS_GET command. */
70 /* Parameter structure used with the KS_PUT command. */
74 kbnode_t keyblock; /* The optional keyblock. */
75 const void *data; /* The key in OpenPGP binary format. */
76 size_t datalen; /* The length of DATA. */
80 /* Data used to associate an session with dirmngr contexts. We can't
81 use a simple one to one mapping because we sometimes need two
82 connections to the dirmngr; for example while doing a listing and
83 being in a data callback we may want to retrieve a key. The local
84 dirmngr data takes care of this. At the end of the session the
85 function dirmngr_deinit_session_data is called by gpg.c to cleanup
86 these resources. Note that gpg.h defines a typedef dirmngr_local_t
87 for this structure. */
88 struct dirmngr_local_s
90 /* Link to other contexts which are used simultaneously. */
91 struct dirmngr_local_s *next;
93 /* The active Assuan context. */
96 /* Flag set to true while an operation is running on CTX. */
102 /* Deinitialize all session data of dirmngr pertaining to CTRL. */
104 gpg_dirmngr_deinit_session_data (ctrl_t ctrl)
108 while ((dml = ctrl->dirmngr_local))
110 ctrl->dirmngr_local = dml->next;
112 log_error ("oops: trying to cleanup an active dirmngr context\n");
114 assuan_release (dml->ctx);
120 /* Try to connect to the Dirmngr via a socket or spawn it if possible.
121 Handle the server's initial greeting and set global options. */
123 create_context (ctrl_t ctrl, assuan_context_t *r_ctx)
126 assuan_context_t ctx;
129 err = start_new_dirmngr (&ctx,
130 GPG_ERR_SOURCE_DEFAULT,
133 opt.verbose, DBG_ASSUAN,
134 NULL /*gpg_status2*/, ctrl);
137 keyserver_spec_t ksi;
139 /* Tell the dirmngr that we want to collect audit event. */
140 /* err = assuan_transact (agent_ctx, "OPTION audit-events=1", */
141 /* NULL, NULL, NULL, NULL, NULL, NULL); */
143 /* Set all configured keyservers. We clear existing keyservers
144 so that any keyserver configured in GPG overrides keyservers
145 possibly still configured in Dirmngr for the session (Note
146 that the keyserver list of a session in Dirmngr survives a
148 for (ksi = opt.keyserver; !err && ksi; ksi = ksi->next)
152 line = xtryasprintf ("KEYSERVER%s %s",
153 ksi == opt.keyserver? " --clear":"", ksi->uri);
155 err = gpg_error_from_syserror ();
158 err = assuan_transact (ctx, line,
159 NULL, NULL, NULL, NULL, NULL, NULL);
166 assuan_release (ctx);
169 /* audit_log_ok (ctrl->audit, AUDIT_DIRMNGR_READY, err); */
177 /* Get a context for accessing dirmngr. If no context is available a
178 new one is created and - if required - dirmngr started. On success
179 an assuan context is stored at R_CTX. This context may only be
180 released by means of close_context. Note that NULL is stored at
183 open_context (ctrl_t ctrl, assuan_context_t *r_ctx)
191 for (dml = ctrl->dirmngr_local; dml && dml->is_active; dml = dml->next)
195 /* Found an inactive local session - return that. */
196 assert (!dml->is_active);
202 dml = xtrycalloc (1, sizeof *dml);
204 return gpg_error_from_syserror ();
205 err = create_context (ctrl, &dml->ctx);
211 /* To be on the nPth thread safe site we need to add it to a
212 list; this is far easier than to have a lock for this
213 function. It should not happen anyway but the code is free
214 because we need it for the is_active check above. */
215 dml->next = ctrl->dirmngr_local;
216 ctrl->dirmngr_local = dml;
221 /* Close the assuan context CTX or return it to a pool of unused
222 contexts. If CTX is NULL, the function does nothing. */
224 close_context (ctrl_t ctrl, assuan_context_t ctx)
231 for (dml = ctrl->dirmngr_local; dml; dml = dml->next)
236 log_fatal ("closing inactive dirmngr context %p\n", ctx);
241 log_fatal ("closing unknown dirmngr ctx %p\n", ctx);
246 /* Status callback for ks_get and ks_search. */
248 ks_status_cb (void *opaque, const char *line)
250 struct ks_status_parm_s *parm = opaque;
254 if ((s = has_leading_keyword (line, "SOURCE")))
258 parm->source = xtrystrdup (s);
260 err = gpg_error_from_syserror ();
269 /* Data callback for the KS_SEARCH command. */
271 ks_search_data_cb (void *opaque, const void *data, size_t datalen)
274 struct ks_search_parm_s *parm = opaque;
275 const char *line, *s;
276 size_t rawlen, linelen;
282 if (parm->stparm->source)
284 err = parm->data_cb (parm->data_cb_value, 1, parm->stparm->source);
290 /* Clear it so that we won't get back here unless the server
291 accidentally sends a second source status line. Note that
292 will not see all accidentally sent source lines because it
293 depends on whether data lines have been send in between. */
294 xfree (parm->stparm->source);
295 parm->stparm->source = NULL;
299 return 0; /* Ignore END commands. */
301 put_membuf (&parm->saveddata, data, datalen);
304 line = peek_membuf (&parm->saveddata, &rawlen);
307 parm->lasterr = gpg_error_from_syserror ();
308 return parm->lasterr; /* Tell the server about our problem. */
310 if ((s = memchr (line, '\n', rawlen)))
312 linelen = s - line; /* That is the length excluding the LF. */
313 if (linelen + 1 < sizeof fixedbuf)
315 /* We can use the static buffer. */
316 memcpy (fixedbuf, line, linelen);
317 fixedbuf[linelen] = 0;
318 if (linelen && fixedbuf[linelen-1] == '\r')
319 fixedbuf[linelen-1] = 0;
320 err = parm->data_cb (parm->data_cb_value, 0, fixedbuf);
324 if (linelen + 1 >= parm->helpbufsize)
326 xfree (parm->helpbuf);
327 parm->helpbufsize = linelen + 1 + 1024;
328 parm->helpbuf = xtrymalloc (parm->helpbufsize);
331 parm->lasterr = gpg_error_from_syserror ();
332 return parm->lasterr;
335 memcpy (parm->helpbuf, line, linelen);
336 parm->helpbuf[linelen] = 0;
337 if (linelen && parm->helpbuf[linelen-1] == '\r')
338 parm->helpbuf[linelen-1] = 0;
339 err = parm->data_cb (parm->data_cb_value, 0, parm->helpbuf);
345 clear_membuf (&parm->saveddata, linelen+1);
346 goto again; /* There might be another complete line. */
354 /* Run the KS_SEARCH command using the search string SEARCHSTR. All
355 data lines are passed to the CB function. That function is called
356 with CB_VALUE as its first argument, a 0 as second argument, and
357 the decoded data line as third argument. The callback function may
358 modify the data line and it is guaranteed that this data line is a
359 complete line with a terminating 0 character but without the
360 linefeed. NULL is passed to the callback to indicate EOF. */
362 gpg_dirmngr_ks_search (ctrl_t ctrl, const char *searchstr,
363 gpg_error_t (*cb)(void*, int, char *), void *cb_value)
366 assuan_context_t ctx;
367 struct ks_status_parm_s stparm;
368 struct ks_search_parm_s parm;
369 char line[ASSUAN_LINELENGTH];
371 err = open_context (ctrl, &ctx);
376 char *escsearchstr = percent_plus_escape (searchstr);
379 err = gpg_error_from_syserror ();
380 close_context (ctrl, ctx);
383 snprintf (line, sizeof line, "KS_SEARCH -- %s", escsearchstr);
384 xfree (escsearchstr);
387 memset (&stparm, 0, sizeof stparm);
388 memset (&parm, 0, sizeof parm);
389 init_membuf (&parm.saveddata, 1024);
391 parm.data_cb_value = cb_value;
392 parm.stparm = &stparm;
394 err = assuan_transact (ctx, line, ks_search_data_cb, &parm,
395 NULL, NULL, ks_status_cb, &stparm);
397 err = cb (cb_value, 0, NULL); /* Send EOF. */
399 xfree (get_membuf (&parm.saveddata, NULL));
400 xfree (parm.helpbuf);
401 xfree (stparm.source);
403 close_context (ctrl, ctx);
409 /* Data callback for the KS_GET and KS_FETCH commands. */
411 ks_get_data_cb (void *opaque, const void *data, size_t datalen)
414 struct ks_get_parm_s *parm = opaque;
418 return 0; /* Ignore END commands. */
420 if (es_write (parm->memfp, data, datalen, &nwritten))
421 err = gpg_error_from_syserror ();
427 /* Run the KS_GET command using the patterns in the array PATTERN. On
428 success an estream object is returned to retrieve the keys. On
429 error an error code is returned and NULL stored at R_FP.
431 The pattern may only use search specification which a keyserver can
432 use to retriev keys. Because we know the format of the pattern we
433 don't need to escape the patterns before sending them to the
436 If R_SOURCE is not NULL the source of the data is stored as a
437 malloced string there. If a source is not known NULL is stored.
439 If there are too many patterns the function returns an error. That
440 could be fixed by issuing several search commands or by
441 implementing a different interface. However with long keyids we
442 are able to ask for (1000-10-1)/(2+8+1) = 90 keys at once. */
444 gpg_dirmngr_ks_get (ctrl_t ctrl, char **pattern,
445 estream_t *r_fp, char **r_source)
448 assuan_context_t ctx;
449 struct ks_status_parm_s stparm;
450 struct ks_get_parm_s parm;
456 memset (&stparm, 0, sizeof stparm);
457 memset (&parm, 0, sizeof parm);
463 err = open_context (ctrl, &ctx);
467 /* Lump all patterns into one string. */
468 init_membuf (&mb, 1024);
469 put_membuf_str (&mb, "KS_GET --");
470 for (idx=0; pattern[idx]; idx++)
472 put_membuf (&mb, " ", 1); /* Append Delimiter. */
473 put_membuf_str (&mb, pattern[idx]);
475 put_membuf (&mb, "", 1); /* Append Nul. */
476 line = get_membuf (&mb, &linelen);
479 err = gpg_error_from_syserror ();
482 if (linelen + 2 >= ASSUAN_LINELENGTH)
484 err = gpg_error (GPG_ERR_TOO_MANY);
488 parm.memfp = es_fopenmem (0, "rwb");
491 err = gpg_error_from_syserror ();
494 err = assuan_transact (ctx, line, ks_get_data_cb, &parm,
495 NULL, NULL, ks_status_cb, &stparm);
499 es_rewind (parm.memfp);
505 *r_source = stparm.source;
506 stparm.source = NULL;
510 es_fclose (parm.memfp);
511 xfree (stparm.source);
513 close_context (ctrl, ctx);
518 /* Run the KS_FETCH and pass URL as argument. On success an estream
519 object is returned to retrieve the keys. On error an error code is
520 returned and NULL stored at R_FP.
522 The url is expected to point to a small set of keys; in many cases
523 only to one key. However, schemes like finger may return several
524 keys. Note that the configured keyservers are ignored by the
527 gpg_dirmngr_ks_fetch (ctrl_t ctrl, const char *url, estream_t *r_fp)
530 assuan_context_t ctx;
531 struct ks_get_parm_s parm;
534 memset (&parm, 0, sizeof parm);
538 err = open_context (ctrl, &ctx);
542 line = strconcat ("KS_FETCH -- ", url, NULL);
545 err = gpg_error_from_syserror ();
548 if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
550 err = gpg_error (GPG_ERR_TOO_LARGE);
554 parm.memfp = es_fopenmem (0, "rwb");
557 err = gpg_error_from_syserror ();
560 err = assuan_transact (ctx, line, ks_get_data_cb, &parm,
561 NULL, NULL, NULL, NULL);
565 es_rewind (parm.memfp);
570 es_fclose (parm.memfp);
572 close_context (ctrl, ctx);
578 /* Handle the KS_PUT inquiries. */
580 ks_put_inq_cb (void *opaque, const char *line)
582 struct ks_put_parm_s *parm = opaque;
585 if (has_leading_keyword (line, "KEYBLOCK"))
588 err = assuan_send_data (parm->ctx, parm->data, parm->datalen);
590 else if (has_leading_keyword (line, "KEYBLOCK_INFO"))
595 /* Parse the keyblock and send info lines back to the server. */
596 fp = es_fopenmem (0, "rw,samethread");
598 err = gpg_error_from_syserror ();
600 for (node = parm->keyblock; !err && node; node=node->next)
602 switch(node->pkt->pkttype)
605 case PKT_PUBLIC_SUBKEY:
607 PKT_public_key *pk = node->pkt->pkt.public_key;
609 keyid_from_pk (pk, NULL);
611 es_fprintf (fp, "%s:%08lX%08lX:%u:%u:%u:%u:%s%s:\n",
612 node->pkt->pkttype==PKT_PUBLIC_KEY? "pub" : "sub",
613 (ulong)pk->keyid[0], (ulong)pk->keyid[1],
618 pk->flags.revoked? "r":"",
619 pk->has_expired? "e":"");
625 PKT_user_id *uid = node->pkt->pkt.user_id;
628 if (!uid->attrib_data)
630 es_fprintf (fp, "uid:");
632 /* Quote ':', '%', and any 8-bit characters. */
633 for (r=0; r < uid->len; r++)
635 if (uid->name[r] == ':'
636 || uid->name[r]== '%'
637 || (uid->name[r]&0x80))
638 es_fprintf (fp, "%%%02X", (byte)uid->name[r]);
640 es_putc (uid->name[r], fp);
643 es_fprintf (fp, ":%u:%u:%s%s:\n",
644 uid->created,uid->expiredate,
645 uid->is_revoked? "r":"",
646 uid->is_expired? "e":"");
651 /* This bit is really for the benefit of people who
652 store their keys in LDAP servers. It makes it easy
653 to do queries for things like "all keys signed by
657 PKT_signature *sig = node->pkt->pkt.signature;
659 if (IS_UID_SIG (sig))
661 es_fprintf (fp, "sig:%08lX%08lX:%X:%u:%u:\n",
662 (ulong)sig->keyid[0],(ulong)sig->keyid[1],
663 sig->sig_class, sig->timestamp,
672 /* Given that the last operation was an es_fprintf we should
673 get the correct ERRNO if ferror indicates an error. */
675 err = gpg_error_from_syserror ();
678 /* Without an error and if we have an keyblock at all, send the
680 if (!err && parm->keyblock)
687 while (!(rc=es_read (fp, buffer, sizeof buffer, &nread)) && nread)
689 err = assuan_send_data (parm->ctx, buffer, nread);
694 err = gpg_error_from_syserror ();
699 return gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
705 /* Send a key to the configured server. {DATA,DATLEN} contains the
706 key in OpenPGP binary transport format. If KEYBLOCK is not NULL it
707 has the internal representaion of that key; this is for example
708 used to convey meta data to LDAP keyservers. */
710 gpg_dirmngr_ks_put (ctrl_t ctrl, void *data, size_t datalen, kbnode_t keyblock)
713 assuan_context_t ctx;
714 struct ks_put_parm_s parm;
716 memset (&parm, 0, sizeof parm);
718 /* We are going to parse the keyblock, thus we better make sure the
719 all information is readily available. */
721 merge_keys_and_selfsig (keyblock);
723 err = open_context (ctrl, &ctx);
728 parm.keyblock = keyblock;
730 parm.datalen = datalen;
732 err = assuan_transact (ctx, "KS_PUT", NULL, NULL,
733 ks_put_inq_cb, &parm, NULL, NULL);
735 close_context (ctrl, ctx);