1 /* server.c - LDAP and Keyserver access server
2 * Copyright (C) 2002 Klarälvdalens Datakonsult AB
3 * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2011, 2015 g10 Code GmbH
4 * Copyright (C) 2014 Werner Koch
6 * This file is part of GnuPG.
8 * GnuPG is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuPG is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
39 # include "ldapserver.h"
42 #include "certcache.h"
46 # include "ldap-wrapper.h"
48 #include "ks-action.h"
49 #include "ks-engine.h" /* (ks_hkp_print_hosttable) */
51 # include "ldap-parse-uri.h"
54 #include "mbox-util.h"
56 /* To avoid DoS attacks we limit the size of a certificate to
57 something reasonable. The DoS was actually only an issue back when
58 Dirmngr was a system service and not a user service. */
59 #define MAX_CERT_LENGTH (16*1024)
61 /* The same goes for OpenPGP keyblocks, but here we need to allow for
62 much longer blocks; a 200k keyblock is not too unusual for keys
63 with a lot of signatures (e.g. 0x5b0358a2). 9C31503C6D866396 even
64 has 770 KiB as of 2015-08-23. To avoid adding a runtime option we
65 now use 20MiB which should really be enough. Well, a key with
66 several pictures could be larger (the parser as a 18MiB limit for
67 attribute packets) but it won't be nice to the keyservers to send
68 them such large blobs. */
69 #define MAX_KEYBLOCK_LENGTH (20*1024*1024)
72 #define PARM_ERROR(t) assuan_set_error (ctx, \
73 gpg_error (GPG_ERR_ASS_PARAMETER), (t))
74 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
78 /* Control structure per connection. */
81 /* Data used to associate an Assuan context with local server data */
82 assuan_context_t assuan_ctx;
84 /* Per-session LDAP servers. */
85 ldap_server_t ldapservers;
87 /* Per-session list of keyservers. */
88 uri_item_t keyservers;
90 /* If this flag is set to true this dirmngr process will be
91 terminated after the end of this session. */
96 /* Cookie definition for assuan data line output. */
97 static ssize_t data_line_cookie_write (void *cookie,
98 const void *buffer, size_t size);
99 static int data_line_cookie_close (void *cookie);
100 static es_cookie_io_functions_t data_line_cookie_functions =
103 data_line_cookie_write,
105 data_line_cookie_close
112 /* Accessor for the local ldapservers variable. */
114 get_ldapservers_from_ctrl (ctrl_t ctrl)
116 if (ctrl && ctrl->server_local)
117 return ctrl->server_local->ldapservers;
123 /* Release all configured keyserver info from CTRL. */
125 release_ctrl_keyservers (ctrl_t ctrl)
127 if (! ctrl->server_local)
130 while (ctrl->server_local->keyservers)
132 uri_item_t tmp = ctrl->server_local->keyservers->next;
133 http_release_parsed_uri (ctrl->server_local->keyservers->parsed_uri);
134 xfree (ctrl->server_local->keyservers);
135 ctrl->server_local->keyservers = tmp;
141 /* Helper to print a message while leaving a command. */
143 leave_cmd (assuan_context_t ctx, gpg_error_t err)
147 const char *name = assuan_get_command_name (ctx);
150 if (gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
151 log_error ("command '%s' failed: %s\n", name,
154 log_error ("command '%s' failed: %s <%s>\n", name,
155 gpg_strerror (err), gpg_strsource (err));
161 /* This is a wrapper around assuan_send_data which makes debugging the
162 output in verbose mode easier. */
164 data_line_write (assuan_context_t ctx, const void *buffer_arg, size_t size)
166 const char *buffer = buffer_arg;
169 if (opt.verbose && buffer && size)
171 /* Ease reading of output by sending a physical line at each LF. */
178 p = memchr (buffer, '\n', nbytes);
179 n = p ? (p - buffer) + 1 : nbytes;
180 err = assuan_send_data (ctx, buffer, n);
183 gpg_err_set_errno (EIO);
188 if (nbytes && (err=assuan_send_data (ctx, NULL, 0))) /* Flush line. */
190 gpg_err_set_errno (EIO);
198 err = assuan_send_data (ctx, buffer, size);
201 gpg_err_set_errno (EIO); /* For use by data_line_cookie_write. */
210 /* A write handler used by es_fopencookie to write assuan data
213 data_line_cookie_write (void *cookie, const void *buffer, size_t size)
215 assuan_context_t ctx = cookie;
217 if (data_line_write (ctx, buffer, size))
219 return (ssize_t)size;
224 data_line_cookie_close (void *cookie)
226 assuan_context_t ctx = cookie;
228 if (assuan_send_data (ctx, NULL, 0))
230 gpg_err_set_errno (EIO);
238 /* Copy the % and + escaped string S into the buffer D and replace the
239 escape sequences. Note, that it is sufficient to allocate the
240 target string D as long as the source string S, i.e.: strlen(s)+1.
241 Note further that if S contains an escaped binary Nul the resulting
242 string D will contain the 0 as well as all other characters but it
243 will be impossible to know whether this is the original EOS or a
246 strcpy_escaped_plus (char *d, const unsigned char *s)
250 if (*s == '%' && s[1] && s[2])
265 /* Check whether the option NAME appears in LINE */
267 has_option (const char *line, const char *name)
270 int n = strlen (name);
272 s = strstr (line, name);
273 return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
276 /* Same as has_option but only considers options at the begin of the
277 line. This is useful for commands which allow arbitrary strings on
280 has_leading_option (const char *line, const char *name)
285 if (name[0] != '-' || name[1] != '-' || !name[2] || spacep (name+2))
288 while ( *line == '-' && line[1] == '-' )
291 while (*line && !spacep (line))
293 if (n == (line - s) && !strncmp (s, name, n))
295 while (spacep (line))
302 /* Same as has_option but does only test for the name of the option
303 and ignores an argument, i.e. with NAME being "--hash" it would
304 return a pointer for "--hash" as well as for "--hash=foo". If
305 thhere is no such option NULL is returned. The pointer returned
306 points right behind the option name, this may be an equal sign, Nul
308 /* static const char * */
309 /* has_option_name (const char *line, const char *name) */
312 /* int n = strlen (name); */
314 /* s = strstr (line, name); */
315 /* return (s && (s == line || spacep (s-1)) */
316 /* && (!s[n] || spacep (s+n) || s[n] == '=')) ? (s+n) : NULL; */
320 /* Skip over options. It is assumed that leading spaces have been
321 removed (this is the case for lines passed to a handler from
322 assuan). Blanks after the options are also removed. */
324 skip_options (char *line)
326 while ( *line == '-' && line[1] == '-' )
328 while (*line && !spacep (line))
330 while (spacep (line))
337 /* Return an error if the assuan context does not belong to the owner
338 of the process or to root. On error FAILTEXT is set as Assuan
341 check_owner_permission (assuan_context_t ctx, const char *failtext)
343 #ifdef HAVE_W32_SYSTEM
344 /* Under Windows the dirmngr is always run under the control of the
350 assuan_peercred_t cred;
352 ec = gpg_err_code (assuan_get_peercred (ctx, &cred));
353 if (!ec && cred->uid && cred->uid != getuid ())
356 return set_error (ec, failtext);
363 /* Common code for get_cert_local and get_issuer_cert_local. */
365 do_get_cert_local (ctrl_t ctrl, const char *name, const char *command)
367 unsigned char *value;
375 buf = xmalloc ( strlen (command) + 1 + strlen(name) + 1);
376 strcpy (stpcpy (stpcpy (buf, command), " "), name);
379 buf = xstrdup (command);
381 rc = assuan_inquire (ctrl->server_local->assuan_ctx, buf,
382 &value, &valuelen, MAX_CERT_LENGTH);
386 log_error (_("assuan_inquire(%s) failed: %s\n"),
387 command, gpg_strerror (rc));
397 rc = ksba_cert_new (&cert);
400 rc = ksba_cert_init_from_mem (cert, value, valuelen);
403 ksba_cert_release (cert);
413 /* Ask back to return a certificate for name, given as a regular
414 gpgsm certificate indentificates (e.g. fingerprint or one of the
415 other methods). Alternatively, NULL may be used for NAME to
416 return the current target certificate. Either return the certificate
417 in a KSBA object or NULL if it is not available.
420 get_cert_local (ctrl_t ctrl, const char *name)
422 if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
425 log_debug ("get_cert_local called w/o context\n");
428 return do_get_cert_local (ctrl, name, "SENDCERT");
432 /* Ask back to return the issuing certificate for name, given as a
433 regular gpgsm certificate indentificates (e.g. fingerprint or one
434 of the other methods). Alternatively, NULL may be used for NAME to
435 return thecurrent target certificate. Either return the certificate
436 in a KSBA object or NULL if it is not available.
440 get_issuing_cert_local (ctrl_t ctrl, const char *name)
442 if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
445 log_debug ("get_issuing_cert_local called w/o context\n");
448 return do_get_cert_local (ctrl, name, "SENDISSUERCERT");
451 /* Ask back to return a certificate with subject NAME and a
452 subjectKeyIdentifier of KEYID. */
454 get_cert_local_ski (ctrl_t ctrl, const char *name, ksba_sexp_t keyid)
456 unsigned char *value;
463 if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
466 log_debug ("get_cert_local_ski called w/o context\n");
471 log_debug ("get_cert_local_ski called with insufficient arguments\n");
475 hexkeyid = serial_hex (keyid);
478 log_debug ("serial_hex() failed\n");
482 buf = xtrymalloc (15 + strlen (hexkeyid) + 2 + strlen(name) + 1);
486 log_error ("can't allocate enough memory: %s\n", strerror (errno));
490 strcpy (stpcpy (stpcpy (stpcpy (buf, "SENDCERT_SKI "), hexkeyid)," /"),name);
493 rc = assuan_inquire (ctrl->server_local->assuan_ctx, buf,
494 &value, &valuelen, MAX_CERT_LENGTH);
498 log_error (_("assuan_inquire(%s) failed: %s\n"), "SENDCERT_SKI",
509 rc = ksba_cert_new (&cert);
512 rc = ksba_cert_init_from_mem (cert, value, valuelen);
515 ksba_cert_release (cert);
524 /* Ask the client via an inquiry to check the istrusted status of the
525 certificate specified by the hexified fingerprint HEXFPR. Returns
526 0 if the certificate is trusted by the client or an error code. */
528 get_istrusted_from_client (ctrl_t ctrl, const char *hexfpr)
530 unsigned char *value;
535 if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx
537 return gpg_error (GPG_ERR_INV_ARG);
539 snprintf (request, sizeof request, "ISTRUSTED %s", hexfpr);
540 rc = assuan_inquire (ctrl->server_local->assuan_ctx, request,
541 &value, &valuelen, 100);
544 log_error (_("assuan_inquire(%s) failed: %s\n"),
545 request, gpg_strerror (rc));
548 /* The expected data is: "1" or "1 cruft" (not a C-string). */
549 if (valuelen && *value == '1' && (valuelen == 1 || spacep (value+1)))
552 rc = gpg_error (GPG_ERR_NOT_TRUSTED);
560 /* Ask the client to return the certificate associated with the
561 current command. This is sometimes needed because the client usually
562 sends us just the cert ID, assuming that the request can be
563 satisfied from the cache, where the cert ID is used as key. */
565 inquire_cert_and_load_crl (assuan_context_t ctx)
567 ctrl_t ctrl = assuan_get_pointer (ctx);
569 unsigned char *value = NULL;
571 ksba_cert_t cert = NULL;
573 err = assuan_inquire( ctx, "SENDCERT", &value, &valuelen, 0);
578 /* FILE *fp = fopen ("foo.der", "r"); */
579 /* value = xmalloc (2000); */
580 /* valuelen = fread (value, 1, 2000, fp); */
584 if (!valuelen) /* No data returned; return a comprehensible error. */
585 return gpg_error (GPG_ERR_MISSING_CERT);
587 err = ksba_cert_new (&cert);
590 err = ksba_cert_init_from_mem (cert, value, valuelen);
593 xfree (value); value = NULL;
595 err = crl_cache_reload_crl (ctrl, cert);
598 ksba_cert_release (cert);
604 /* Handle OPTION commands. */
606 option_handler (assuan_context_t ctx, const char *key, const char *value)
608 ctrl_t ctrl = assuan_get_pointer (ctx);
611 if (!strcmp (key, "force-crl-refresh"))
613 int i = *value? atoi (value) : 0;
614 ctrl->force_crl_refresh = i;
616 else if (!strcmp (key, "audit-events"))
618 int i = *value? atoi (value) : 0;
619 ctrl->audit_events = i;
621 else if (!strcmp (key, "http-proxy"))
623 xfree (ctrl->http_proxy);
624 if (!*value || !strcmp (value, "none"))
625 ctrl->http_proxy = NULL;
626 else if (!(ctrl->http_proxy = xtrystrdup (value)))
627 err = gpg_error_from_syserror ();
630 err = gpg_error (GPG_ERR_UNKNOWN_OPTION);
637 static const char hlp_dns_cert[] =
638 "DNS_CERT <subtype> <name>\n"
639 "DNS_CERT --pka <user_id>\n"
641 "Return the CERT record for <name>. <subtype> is one of\n"
642 " * Return the first record of any supported subtype\n"
643 " PGP Return the first record of subtype PGP (3)\n"
644 " IPGP Return the first record of subtype IPGP (6)\n"
645 "If the content of a certifciate is available (PGP) it is returned\n"
646 "by data lines. Fingerprints and URLs are returned via status lines.\n"
647 "In --pka mode the fingerprint and if available an URL is returned.";
649 cmd_dns_cert (assuan_context_t ctx, char *line)
651 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
655 char *namebuf = NULL;
656 char *encodedhash = NULL;
662 unsigned char *fpr = NULL;
666 pka_mode = has_option (line, "--pka");
667 line = skip_options (line);
669 ; /* No need to parse here - we do this later. */
672 p = strchr (line, ' ');
675 err = PARM_ERROR ("missing arguments");
679 if (!strcmp (line, "*"))
680 certtype = DNS_CERTTYPE_ANY;
681 else if (!strcmp (line, "IPGP"))
682 certtype = DNS_CERTTYPE_IPGP;
683 else if (!strcmp (line, "PGP"))
684 certtype = DNS_CERTTYPE_PGP;
687 err = PARM_ERROR ("unknown subtype");
695 err = PARM_ERROR ("name missing");
702 char *domain; /* Points to mbox. */
705 mbox = mailbox_from_userid (line);
706 if (!mbox || !(domain = strchr (mbox, '@')))
708 err = set_error (GPG_ERR_INV_USER_ID, "no mailbox in user id");
713 gcry_md_hash_buffer (GCRY_MD_SHA1, hashbuf, mbox, strlen (mbox));
714 encodedhash = zb32_encode (hashbuf, 8*20);
717 err = gpg_error_from_syserror ();
720 namebuf = strconcat (encodedhash, "._pka.", domain, NULL);
723 err = gpg_error_from_syserror ();
727 certtype = DNS_CERTTYPE_IPGP;
732 err = get_dns_cert (name, certtype, &key, &keylen, &fpr, &fprlen, &url);
738 err = data_line_write (ctx, key, keylen);
747 tmpstr = bin2hex (fpr, fprlen, NULL);
749 err = gpg_error_from_syserror ();
752 err = assuan_write_status (ctx, "FPR", tmpstr);
761 err = assuan_write_status (ctx, "URL", url);
774 return leave_cmd (ctx, err);
779 static const char hlp_ldapserver[] =
780 "LDAPSERVER <data>\n"
782 "Add a new LDAP server to the list of configured LDAP servers.\n"
783 "DATA is in the same format as expected in the configure file.";
785 cmd_ldapserver (assuan_context_t ctx, char *line)
788 ctrl_t ctrl = assuan_get_pointer (ctx);
789 ldap_server_t server;
790 ldap_server_t *last_next_p;
792 while (spacep (line))
795 return leave_cmd (ctx, PARM_ERROR (_("ldapserver missing")));
797 server = ldapserver_parse_one (line, "", 0);
799 return leave_cmd (ctx, gpg_error (GPG_ERR_INV_ARG));
801 last_next_p = &ctrl->server_local->ldapservers;
803 last_next_p = &(*last_next_p)->next;
804 *last_next_p = server;
805 return leave_cmd (ctx, 0);
808 return leave_cmd (ctx, gpg_error (GPG_ERR_NOT_IMPLEMENTED));
813 static const char hlp_isvalid[] =
814 "ISVALID [--only-ocsp] [--force-default-responder]"
815 " <certificate_id>|<certificate_fpr>\n"
817 "This command checks whether the certificate identified by the\n"
818 "certificate_id is valid. This is done by consulting CRLs or\n"
819 "whatever has been configured. Note, that the returned error codes\n"
820 "are from gpg-error.h. The command may callback using the inquire\n"
821 "function. See the manual for details.\n"
823 "The CERTIFICATE_ID is a hex encoded string consisting of two parts,\n"
824 "delimited by a single dot. The first part is the SHA-1 hash of the\n"
825 "issuer name and the second part the serial number.\n"
827 "Alternatively the certificate's fingerprint may be given in which\n"
828 "case an OCSP request is done before consulting the CRL.\n"
830 "If the option --only-ocsp is given, no fallback to a CRL check will\n"
833 "If the option --force-default-responder is given, only the default\n"
834 "OCSP responder will be used and any other methods of obtaining an\n"
835 "OCSP responder URL won't be used.";
837 cmd_isvalid (assuan_context_t ctx, char *line)
839 ctrl_t ctrl = assuan_get_pointer (ctx);
840 char *issuerhash, *serialno;
845 int force_default_responder;
847 only_ocsp = has_option (line, "--only-ocsp");
848 force_default_responder = has_option (line, "--force-default-responder");
849 line = skip_options (line);
851 issuerhash = xstrdup (line); /* We need to work on a copy of the
852 line because that same Assuan
853 context may be used for an inquiry.
854 That is because Assuan reuses its
858 serialno = strchr (issuerhash, '.');
863 char *endp = strchr (issuerhash, ' ');
866 if (strlen (issuerhash) != 40)
869 return leave_cmd (ctx, PARM_ERROR (_("serialno missing in cert ID")));
878 /* Note, that we ignore the given issuer hash and instead rely
879 on the current certificate semantics used with this
882 err = gpg_error (GPG_ERR_NOT_SUPPORTED);
884 err = ocsp_isvalid (ctrl, NULL, NULL, force_default_responder);
885 /* Fixme: If we got no ocsp response and --only-ocsp is not used
886 we should fall back to CRL mode. Thus we need to clear
887 OCSP_MODE, get the issuerhash and the serialno from the
888 current certificate and jump to again. */
891 err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
894 switch (crl_cache_isvalid (ctrl,
895 issuerhash, serialno,
896 ctrl->force_crl_refresh))
898 case CRL_CACHE_VALID:
901 case CRL_CACHE_INVALID:
902 err = gpg_error (GPG_ERR_CERT_REVOKED);
904 case CRL_CACHE_DONTKNOW:
906 err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
907 else if (!(err = inquire_cert_and_load_crl (ctx)))
913 case CRL_CACHE_CANTUSE:
914 err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
917 log_fatal ("crl_cache_isvalid returned invalid code\n");
922 return leave_cmd (ctx, err);
926 /* If the line contains a SHA-1 fingerprint as the first argument,
927 return the FPR vuffer on success. The function checks that the
928 fingerprint consists of valid characters and prints and error
929 message if it does not and returns NULL. Fingerprints are
930 considered optional and thus no explicit error is returned. NULL is
931 also returned if there is no fingerprint at all available.
932 FPR must be a caller provided buffer of at least 20 bytes.
934 Note that colons within the fingerprint are allowed to separate 2
935 hex digits; this allows for easier cutting and pasting using the
936 usual fingerprint rendering.
938 static unsigned char *
939 get_fingerprint_from_line (const char *line, unsigned char *fpr)
944 for (s=line, i=0; *s && *s != ' '; s++ )
946 if ( hexdigitp (s) && hexdigitp (s+1) )
949 return NULL; /* Fingerprint too long. */
950 fpr[i++] = xtoi_2 (s);
953 else if ( *s != ':' )
954 return NULL; /* Invalid. */
957 return NULL; /* Fingerprint to short. */
963 static const char hlp_checkcrl[] =
964 "CHECKCRL [<fingerprint>]\n"
966 "Check whether the certificate with FINGERPRINT (SHA-1 hash of the\n"
967 "entire X.509 certificate blob) is valid or not by consulting the\n"
968 "CRL responsible for this certificate. If the fingerprint has not\n"
969 "been given or the certificate is not known, the function \n"
970 "inquires the certificate using an\n"
972 " INQUIRE TARGETCERT\n"
974 "and the caller is expected to return the certificate for the\n"
975 "request (which should match FINGERPRINT) as a binary blob.\n"
976 "Processing then takes place without further interaction; in\n"
977 "particular dirmngr tries to locate other required certificate by\n"
978 "its own mechanism which includes a local certificate store as well\n"
979 "as a list of trusted root certificates.\n"
981 "The return value is the usual gpg-error code or 0 for ducesss;\n"
982 "i.e. the certificate validity has been confirmed by a valid CRL.";
984 cmd_checkcrl (assuan_context_t ctx, char *line)
986 ctrl_t ctrl = assuan_get_pointer (ctx);
988 unsigned char fprbuffer[20], *fpr;
991 fpr = get_fingerprint_from_line (line, fprbuffer);
992 cert = fpr? get_cert_byfpr (fpr) : NULL;
996 /* We do not have this certificate yet or the fingerprint has
997 not been given. Inquire it from the client. */
998 unsigned char *value = NULL;
1001 err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
1002 &value, &valuelen, MAX_CERT_LENGTH);
1005 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1009 if (!valuelen) /* No data returned; return a comprehensible error. */
1010 err = gpg_error (GPG_ERR_MISSING_CERT);
1013 err = ksba_cert_new (&cert);
1015 err = ksba_cert_init_from_mem (cert, value, valuelen);
1024 err = crl_cache_cert_isvalid (ctrl, cert, ctrl->force_crl_refresh);
1025 if (gpg_err_code (err) == GPG_ERR_NO_CRL_KNOWN)
1027 err = crl_cache_reload_crl (ctrl, cert);
1029 err = crl_cache_cert_isvalid (ctrl, cert, 0);
1033 ksba_cert_release (cert);
1034 return leave_cmd (ctx, err);
1038 static const char hlp_checkocsp[] =
1039 "CHECKOCSP [--force-default-responder] [<fingerprint>]\n"
1041 "Check whether the certificate with FINGERPRINT (SHA-1 hash of the\n"
1042 "entire X.509 certificate blob) is valid or not by asking an OCSP\n"
1043 "responder responsible for this certificate. The optional\n"
1044 "fingerprint may be used for a quick check in case an OCSP check has\n"
1045 "been done for this certificate recently (we always cache OCSP\n"
1046 "responses for a couple of minutes). If the fingerprint has not been\n"
1047 "given or there is no cached result, the function inquires the\n"
1048 "certificate using an\n"
1050 " INQUIRE TARGETCERT\n"
1052 "and the caller is expected to return the certificate for the\n"
1053 "request (which should match FINGERPRINT) as a binary blob.\n"
1054 "Processing then takes place without further interaction; in\n"
1055 "particular dirmngr tries to locate other required certificates by\n"
1056 "its own mechanism which includes a local certificate store as well\n"
1057 "as a list of trusted root certifciates.\n"
1059 "If the option --force-default-responder is given, only the default\n"
1060 "OCSP responder will be used and any other methods of obtaining an\n"
1061 "OCSP responder URL won't be used.\n"
1063 "The return value is the usual gpg-error code or 0 for ducesss;\n"
1064 "i.e. the certificate validity has been confirmed by a valid CRL.";
1066 cmd_checkocsp (assuan_context_t ctx, char *line)
1068 ctrl_t ctrl = assuan_get_pointer (ctx);
1070 unsigned char fprbuffer[20], *fpr;
1072 int force_default_responder;
1074 force_default_responder = has_option (line, "--force-default-responder");
1075 line = skip_options (line);
1077 fpr = get_fingerprint_from_line (line, fprbuffer);
1078 cert = fpr? get_cert_byfpr (fpr) : NULL;
1082 /* We do not have this certificate yet or the fingerprint has
1083 not been given. Inquire it from the client. */
1084 unsigned char *value = NULL;
1087 err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
1088 &value, &valuelen, MAX_CERT_LENGTH);
1091 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1095 if (!valuelen) /* No data returned; return a comprehensible error. */
1096 err = gpg_error (GPG_ERR_MISSING_CERT);
1099 err = ksba_cert_new (&cert);
1101 err = ksba_cert_init_from_mem (cert, value, valuelen);
1110 if (!opt.allow_ocsp)
1111 err = gpg_error (GPG_ERR_NOT_SUPPORTED);
1113 err = ocsp_isvalid (ctrl, cert, NULL, force_default_responder);
1116 ksba_cert_release (cert);
1117 return leave_cmd (ctx, err);
1123 lookup_cert_by_url (assuan_context_t ctx, const char *url)
1125 ctrl_t ctrl = assuan_get_pointer (ctx);
1126 gpg_error_t err = 0;
1127 unsigned char *value = NULL;
1130 /* Fetch single certificate given it's URL. */
1131 err = fetch_cert_by_url (ctrl, url, &value, &valuelen);
1134 log_error (_("fetch_cert_by_url failed: %s\n"), gpg_strerror (err));
1138 /* Send the data, flush the buffer and then send an END. */
1139 err = assuan_send_data (ctx, value, valuelen);
1141 err = assuan_send_data (ctx, NULL, 0);
1143 err = assuan_write_line (ctx, "END");
1146 log_error (_("error sending data: %s\n"), gpg_strerror (err));
1156 /* Send the certificate, flush the buffer and then send an END. */
1158 return_one_cert (void *opaque, ksba_cert_t cert)
1160 assuan_context_t ctx = opaque;
1162 const unsigned char *der;
1165 der = ksba_cert_get_image (cert, &derlen);
1167 err = gpg_error (GPG_ERR_INV_CERT_OBJ);
1170 err = assuan_send_data (ctx, der, derlen);
1172 err = assuan_send_data (ctx, NULL, 0);
1174 err = assuan_write_line (ctx, "END");
1177 log_error (_("error sending data: %s\n"), gpg_strerror (err));
1182 /* Lookup certificates from the internal cache or using the ldap
1185 lookup_cert_by_pattern (assuan_context_t ctx, char *line,
1186 int single, int cache_only)
1188 gpg_error_t err = 0;
1190 strlist_t sl, list = NULL;
1191 int truncated = 0, truncation_forced = 0;
1193 int local_count = 0;
1195 ctrl_t ctrl = assuan_get_pointer (ctx);
1196 unsigned char *value = NULL;
1198 struct ldapserver_iter ldapserver_iter;
1199 cert_fetch_context_t fetch_context;
1201 int any_no_data = 0;
1203 /* Break the line down into an STRLIST */
1204 for (p=line; *p; line = p)
1206 while (*p && *p != ' ')
1213 sl = xtrymalloc (sizeof *sl + strlen (line));
1216 err = gpg_error_from_errno (errno);
1219 memset (sl, 0, sizeof *sl);
1220 strcpy_escaped_plus (sl->d, line);
1226 /* First look through the internal cache. The certifcates retruned
1227 here are not counted towards the truncation limit. */
1228 if (single && !cache_only)
1229 ; /* Do not read from the local cache in this case. */
1232 for (sl=list; sl; sl = sl->next)
1234 err = get_certs_bypattern (sl->d, return_one_cert, ctx);
1240 if (gpg_err_code (err) == GPG_ERR_NO_DATA)
1246 else if (gpg_err_code (err) == GPG_ERR_INV_NAME && !cache_only)
1248 /* No real fault because the internal pattern lookup
1249 can't yet cope with all types of pattern. */
1257 /* Loop over all configured servers unless we want only the
1258 certificates from the cache. */
1260 for (ldapserver_iter_begin (&ldapserver_iter, ctrl);
1261 !cache_only && !ldapserver_iter_end_p (&ldapserver_iter)
1262 && ldapserver_iter.server->host && !truncation_forced;
1263 ldapserver_iter_next (&ldapserver_iter))
1265 ldap_server_t ldapserver = ldapserver_iter.server;
1268 log_debug ("cmd_lookup: trying %s:%d base=%s\n",
1269 ldapserver->host, ldapserver->port,
1270 ldapserver->base?ldapserver->base : "[default]");
1272 /* Fetch certificates matching pattern */
1273 err = start_cert_fetch (ctrl, &fetch_context, list, ldapserver);
1274 if ( gpg_err_code (err) == GPG_ERR_NO_DATA )
1277 log_debug ("cmd_lookup: no data\n");
1284 log_error (_("start_cert_fetch failed: %s\n"), gpg_strerror (err));
1288 /* Fetch the certificates for this query. */
1289 while (!truncation_forced)
1291 xfree (value); value = NULL;
1292 err = fetch_next_cert (fetch_context, &value, &valuelen);
1293 if (gpg_err_code (err) == GPG_ERR_NO_DATA )
1299 if (gpg_err_code (err) == GPG_ERR_TRUNCATED)
1305 if (gpg_err_code (err) == GPG_ERR_EOF)
1312 err = gpg_error (GPG_ERR_BUG);
1317 log_error (_("fetch_next_cert failed: %s\n"),
1318 gpg_strerror (err));
1319 end_cert_fetch (fetch_context);
1324 log_debug ("cmd_lookup: returning one cert%s\n",
1325 truncated? " (truncated)":"");
1327 /* Send the data, flush the buffer and then send an END line
1328 as a certificate delimiter. */
1329 err = assuan_send_data (ctx, value, valuelen);
1331 err = assuan_send_data (ctx, NULL, 0);
1333 err = assuan_write_line (ctx, "END");
1336 log_error (_("error sending data: %s\n"), gpg_strerror (err));
1337 end_cert_fetch (fetch_context);
1341 if (++count >= opt.max_replies )
1343 truncation_forced = 1;
1344 log_info (_("max_replies %d exceeded\n"), opt.max_replies );
1350 end_cert_fetch (fetch_context);
1355 if (truncated || truncation_forced)
1359 sprintf (str, "%d", count);
1360 assuan_write_status (ctx, "TRUNCATED", str);
1363 if (!err && !count && !local_count && any_no_data)
1364 err = gpg_error (GPG_ERR_NO_DATA);
1367 free_strlist (list);
1372 static const char hlp_lookup[] =
1373 "LOOKUP [--url] [--single] [--cache-only] <pattern>\n"
1375 "Lookup certificates matching PATTERN. With --url the pattern is\n"
1376 "expected to be one URL.\n"
1378 "If --url is not given: To allow for multiple patterns (which are ORed)\n"
1379 "quoting is required: Spaces are translated to \"+\" or \"%20\";\n"
1380 "obviously this requires that the usual escape quoting rules are applied.\n"
1382 "If --url is given no special escaping is required because URLs are\n"
1383 "already escaped this way.\n"
1385 "If --single is given the first and only the first match will be\n"
1386 "returned. If --cache-only is _not_ given, no local query will be\n"
1389 "If --cache-only is given no external lookup is done so that only\n"
1390 "certificates from the cache may get returned.";
1392 cmd_lookup (assuan_context_t ctx, char *line)
1395 int lookup_url, single, cache_only;
1397 lookup_url = has_leading_option (line, "--url");
1398 single = has_leading_option (line, "--single");
1399 cache_only = has_leading_option (line, "--cache-only");
1400 line = skip_options (line);
1402 if (lookup_url && cache_only)
1403 err = gpg_error (GPG_ERR_NOT_FOUND);
1404 else if (lookup_url && single)
1405 err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
1406 else if (lookup_url)
1407 err = lookup_cert_by_url (ctx, line);
1409 err = lookup_cert_by_pattern (ctx, line, single, cache_only);
1411 return leave_cmd (ctx, err);
1415 static const char hlp_loadcrl[] =
1416 "LOADCRL [--url] <filename|url>\n"
1418 "Load the CRL in the file with name FILENAME into our cache. Note\n"
1419 "that FILENAME should be given with an absolute path because\n"
1420 "Dirmngrs cwd is not known. With --url the CRL is directly loaded\n"
1421 "from the given URL.\n"
1423 "This command is usually used by gpgsm using the invocation \"gpgsm\n"
1424 "--call-dirmngr loadcrl <filename>\". A direct invocation of Dirmngr\n"
1425 "is not useful because gpgsm might need to callback gpgsm to ask for\n"
1426 "the CA's certificate.";
1428 cmd_loadcrl (assuan_context_t ctx, char *line)
1430 ctrl_t ctrl = assuan_get_pointer (ctx);
1431 gpg_error_t err = 0;
1432 int use_url = has_leading_option (line, "--url");
1434 line = skip_options (line);
1438 ksba_reader_t reader;
1440 err = crl_fetch (ctrl, line, &reader);
1442 log_error (_("fetching CRL from '%s' failed: %s\n"),
1443 line, gpg_strerror (err));
1446 err = crl_cache_insert (ctrl, line, reader);
1448 log_error (_("processing CRL from '%s' failed: %s\n"),
1449 line, gpg_strerror (err));
1450 crl_close_reader (reader);
1457 buf = xtrymalloc (strlen (line)+1);
1459 err = gpg_error_from_syserror ();
1462 strcpy_escaped_plus (buf, line);
1463 err = crl_cache_load (ctrl, buf);
1468 return leave_cmd (ctx, err);
1472 static const char hlp_listcrls[] =
1475 "List the content of all CRLs in a readable format. This command is\n"
1476 "usually used by gpgsm using the invocation \"gpgsm --call-dirmngr\n"
1477 "listcrls\". It may also be used directly using \"dirmngr\n"
1480 cmd_listcrls (assuan_context_t ctx, char *line)
1487 fp = es_fopencookie (ctx, "w", data_line_cookie_functions);
1489 err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
1492 err = crl_cache_list (fp);
1495 return leave_cmd (ctx, err);
1499 static const char hlp_cachecert[] =
1502 "Put a certificate into the internal cache. This command might be\n"
1503 "useful if a client knows in advance certificates required for a\n"
1504 "test and wants to make sure they get added to the internal cache.\n"
1505 "It is also helpful for debugging. To get the actual certificate,\n"
1506 "this command immediately inquires it using\n"
1508 " INQUIRE TARGETCERT\n"
1510 "and the caller is expected to return the certificate for the\n"
1511 "request as a binary blob.";
1513 cmd_cachecert (assuan_context_t ctx, char *line)
1515 ctrl_t ctrl = assuan_get_pointer (ctx);
1517 ksba_cert_t cert = NULL;
1518 unsigned char *value = NULL;
1523 err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
1524 &value, &valuelen, MAX_CERT_LENGTH);
1527 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1531 if (!valuelen) /* No data returned; return a comprehensible error. */
1532 err = gpg_error (GPG_ERR_MISSING_CERT);
1535 err = ksba_cert_new (&cert);
1537 err = ksba_cert_init_from_mem (cert, value, valuelen);
1543 err = cache_cert (cert);
1546 ksba_cert_release (cert);
1547 return leave_cmd (ctx, err);
1551 static const char hlp_validate[] =
1554 "Validate a certificate using the certificate validation function\n"
1555 "used internally by dirmngr. This command is only useful for\n"
1556 "debugging. To get the actual certificate, this command immediately\n"
1557 "inquires it using\n"
1559 " INQUIRE TARGETCERT\n"
1561 "and the caller is expected to return the certificate for the\n"
1562 "request as a binary blob.";
1564 cmd_validate (assuan_context_t ctx, char *line)
1566 ctrl_t ctrl = assuan_get_pointer (ctx);
1568 ksba_cert_t cert = NULL;
1569 unsigned char *value = NULL;
1574 err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
1575 &value, &valuelen, MAX_CERT_LENGTH);
1578 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1582 if (!valuelen) /* No data returned; return a comprehensible error. */
1583 err = gpg_error (GPG_ERR_MISSING_CERT);
1586 err = ksba_cert_new (&cert);
1588 err = ksba_cert_init_from_mem (cert, value, valuelen);
1594 /* If we have this certificate already in our cache, use the cached
1595 version for validation because this will take care of any cached
1598 unsigned char fpr[20];
1599 ksba_cert_t tmpcert;
1601 cert_compute_fpr (cert, fpr);
1602 tmpcert = get_cert_byfpr (fpr);
1605 ksba_cert_release (cert);
1610 err = validate_cert_chain (ctrl, cert, NULL, VALIDATE_MODE_CERT, NULL);
1613 ksba_cert_release (cert);
1614 return leave_cmd (ctx, err);
1618 static const char hlp_keyserver[] =
1619 "KEYSERVER [<options>] [<uri>|<host>]\n"
1622 " --clear Remove all configured keyservers\n"
1623 " --resolve Resolve HKP host names and rotate\n"
1624 " --hosttable Print table of known hosts and pools\n"
1625 " --dead Mark <host> as dead\n"
1626 " --alive Mark <host> as alive\n"
1628 "If called without arguments list all configured keyserver URLs.\n"
1629 "If called with an URI add this as keyserver. Note that keyservers\n"
1630 "are configured on a per-session base. A default keyserver may already be\n"
1631 "present, thus the \"--clear\" option must be used to get full control.\n"
1632 "If \"--clear\" and an URI are used together the clear command is\n"
1633 "obviously executed first. A RESET command does not change the list\n"
1634 "of configured keyservers.";
1636 cmd_keyserver (assuan_context_t ctx, char *line)
1638 ctrl_t ctrl = assuan_get_pointer (ctx);
1639 gpg_error_t err = 0;
1640 int clear_flag, add_flag, help_flag, host_flag, resolve_flag;
1641 int dead_flag, alive_flag;
1642 uri_item_t item = NULL; /* gcc 4.4.5 is not able to detect that it
1643 is always initialized. */
1645 clear_flag = has_option (line, "--clear");
1646 help_flag = has_option (line, "--help");
1647 resolve_flag = has_option (line, "--resolve");
1648 host_flag = has_option (line, "--hosttable");
1649 dead_flag = has_option (line, "--dead");
1650 alive_flag = has_option (line, "--alive");
1651 line = skip_options (line);
1656 err = ks_action_help (ctrl, line);
1662 err = ks_action_resolve (ctrl, ctrl->server_local->keyservers);
1667 if (alive_flag && dead_flag)
1669 err = set_error (GPG_ERR_ASS_PARAMETER, "no support for zombies");
1674 err = check_owner_permission (ctx, "no permission to use --dead");
1678 if (alive_flag || dead_flag)
1682 err = set_error (GPG_ERR_ASS_PARAMETER, "name of host missing");
1686 err = ks_hkp_mark_host (ctrl, line, alive_flag);
1693 err = ks_hkp_print_hosttable (ctrl);
1697 if (resolve_flag || host_flag || alive_flag || dead_flag)
1702 item = xtrymalloc (sizeof *item + strlen (line));
1705 err = gpg_error_from_syserror ();
1709 item->parsed_uri = NULL;
1710 strcpy (item->uri, line);
1713 if (ldap_uri_p (item->uri))
1714 err = ldap_parse_uri (&item->parsed_uri, line);
1718 err = http_parse_uri (&item->parsed_uri, line, 1);
1727 release_ctrl_keyservers (ctrl);
1730 item->next = ctrl->server_local->keyservers;
1731 ctrl->server_local->keyservers = item;
1734 if (!add_flag && !clear_flag && !help_flag) /* List configured keyservers. */
1738 for (u=ctrl->server_local->keyservers; u; u = u->next)
1739 dirmngr_status (ctrl, "KEYSERVER", u->uri, NULL);
1744 return leave_cmd (ctx, err);
1749 static const char hlp_ks_search[] =
1750 "KS_SEARCH {<pattern>}\n"
1752 "Search the configured OpenPGP keyservers (see command KEYSERVER)\n"
1753 "for keys matching PATTERN";
1755 cmd_ks_search (assuan_context_t ctx, char *line)
1757 ctrl_t ctrl = assuan_get_pointer (ctx);
1763 /* No options for now. */
1764 line = skip_options (line);
1766 /* Break the line down into an strlist. Each pattern is
1767 percent-plus escaped. */
1769 for (p=line; *p; line = p)
1771 while (*p && *p != ' ')
1777 sl = xtrymalloc (sizeof *sl + strlen (line));
1780 err = gpg_error_from_syserror ();
1784 strcpy_escaped_plus (sl->d, line);
1790 /* Setup an output stream and perform the search. */
1791 outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
1793 err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
1796 err = ks_action_search (ctrl, ctrl->server_local->keyservers,
1802 free_strlist (list);
1803 return leave_cmd (ctx, err);
1808 static const char hlp_ks_get[] =
1809 "KS_GET {<pattern>}\n"
1811 "Get the keys matching PATTERN from the configured OpenPGP keyservers\n"
1812 "(see command KEYSERVER). Each pattern should be a keyid, a fingerprint,\n"
1813 "or an exact name indicated by the '=' prefix.";
1815 cmd_ks_get (assuan_context_t ctx, char *line)
1817 ctrl_t ctrl = assuan_get_pointer (ctx);
1823 /* No options for now. */
1824 line = skip_options (line);
1826 /* Break the line into a strlist. Each pattern is by
1827 definition percent-plus escaped. However we only support keyids
1828 and fingerprints and thus the client has no need to apply the
1831 for (p=line; *p; line = p)
1833 while (*p && *p != ' ')
1839 sl = xtrymalloc (sizeof *sl + strlen (line));
1842 err = gpg_error_from_syserror ();
1846 strcpy_escaped_plus (sl->d, line);
1852 /* Setup an output stream and perform the get. */
1853 outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
1855 err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
1858 err = ks_action_get (ctrl, ctrl->server_local->keyservers, list, outfp);
1863 free_strlist (list);
1864 return leave_cmd (ctx, err);
1868 static const char hlp_ks_fetch[] =
1871 "Get the key(s) from URL.";
1873 cmd_ks_fetch (assuan_context_t ctx, char *line)
1875 ctrl_t ctrl = assuan_get_pointer (ctx);
1879 /* No options for now. */
1880 line = skip_options (line);
1882 /* Setup an output stream and perform the get. */
1883 outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
1885 err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
1888 err = ks_action_fetch (ctrl, line, outfp);
1892 return leave_cmd (ctx, err);
1897 static const char hlp_ks_put[] =
1900 "Send a key to the configured OpenPGP keyservers. The actual key material\n"
1901 "is then requested by Dirmngr using\n"
1903 " INQUIRE KEYBLOCK\n"
1905 "The client shall respond with a binary version of the keyblock (e.g.,\n"
1906 "the output of `gpg --export KEYID'). For LDAP\n"
1907 "keyservers Dirmngr may ask for meta information of the provided keyblock\n"
1910 " INQUIRE KEYBLOCK_INFO\n"
1912 "The client shall respond with a colon delimited info lines (the output\n"
1913 "of 'for x in keys sigs; do gpg --list-$x --with-colons KEYID; done').\n";
1915 cmd_ks_put (assuan_context_t ctx, char *line)
1917 ctrl_t ctrl = assuan_get_pointer (ctx);
1919 unsigned char *value = NULL;
1921 unsigned char *info = NULL;
1924 /* No options for now. */
1925 line = skip_options (line);
1927 /* Ask for the key material. */
1928 err = assuan_inquire (ctx, "KEYBLOCK",
1929 &value, &valuelen, MAX_KEYBLOCK_LENGTH);
1932 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1936 if (!valuelen) /* No data returned; return a comprehensible error. */
1938 err = gpg_error (GPG_ERR_MISSING_CERT);
1942 /* Ask for the key meta data. Not actually needed for HKP servers
1943 but we do it anyway to test the client implementaion. */
1944 err = assuan_inquire (ctx, "KEYBLOCK_INFO",
1945 &info, &infolen, MAX_KEYBLOCK_LENGTH);
1948 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
1953 err = ks_action_put (ctrl, ctrl->server_local->keyservers,
1954 value, valuelen, info, infolen);
1959 return leave_cmd (ctx, err);
1965 static const char hlp_getinfo[] =
1968 "Multi purpose command to return certain information. \n"
1969 "Supported values of WHAT are:\n"
1971 "version - Return the version of the program.\n"
1972 "pid - Return the process id of the server.\n"
1974 "socket_name - Return the name of the socket.\n";
1976 cmd_getinfo (assuan_context_t ctx, char *line)
1980 if (!strcmp (line, "version"))
1982 const char *s = VERSION;
1983 err = assuan_send_data (ctx, s, strlen (s));
1985 else if (!strcmp (line, "pid"))
1989 snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
1990 err = assuan_send_data (ctx, numbuf, strlen (numbuf));
1992 else if (!strcmp (line, "socket_name"))
1994 const char *s = dirmngr_user_socket_name ();
1997 s = dirmngr_sys_socket_name ();
2000 err = assuan_send_data (ctx, s, strlen (s));
2002 err = gpg_error (GPG_ERR_NO_DATA);
2005 err = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
2007 return leave_cmd (ctx, err);
2012 static const char hlp_killdirmngr[] =
2015 "This command allows a user - given sufficient permissions -\n"
2016 "to kill this dirmngr process.\n";
2018 cmd_killdirmngr (assuan_context_t ctx, char *line)
2020 ctrl_t ctrl = assuan_get_pointer (ctx);
2025 if (opt.system_daemon)
2027 if (opt.system_service)
2028 err = set_error (GPG_ERR_NOT_SUPPORTED,
2029 "can't do that whilst running as system service");
2031 err = check_owner_permission (ctx,
2032 "no permission to kill this process");
2039 ctrl->server_local->stopme = 1;
2040 err = gpg_error (GPG_ERR_EOF);
2046 static const char hlp_reloaddirmngr[] =
2049 "This command is an alternative to SIGHUP\n"
2050 "to reload the configuration.";
2052 cmd_reloaddirmngr (assuan_context_t ctx, char *line)
2057 if (opt.system_daemon)
2059 #ifndef HAVE_W32_SYSTEM
2062 assuan_peercred_t cred;
2064 ec = gpg_err_code (assuan_get_peercred (ctx, &cred));
2065 if (!ec && cred->uid)
2066 ec = GPG_ERR_EPERM; /* Only root may terminate. */
2068 return set_error (ec, "no permission to reload this process");
2073 dirmngr_sighup_action ();
2080 /* Tell the assuan library about our commands. */
2082 register_commands (assuan_context_t ctx)
2086 assuan_handler_t handler;
2087 const char * const help;
2089 { "DNS_CERT", cmd_dns_cert, hlp_dns_cert },
2090 { "LDAPSERVER", cmd_ldapserver, hlp_ldapserver },
2091 { "ISVALID", cmd_isvalid, hlp_isvalid },
2092 { "CHECKCRL", cmd_checkcrl, hlp_checkcrl },
2093 { "CHECKOCSP", cmd_checkocsp, hlp_checkocsp },
2094 { "LOOKUP", cmd_lookup, hlp_lookup },
2095 { "LOADCRL", cmd_loadcrl, hlp_loadcrl },
2096 { "LISTCRLS", cmd_listcrls, hlp_listcrls },
2097 { "CACHECERT", cmd_cachecert, hlp_cachecert },
2098 { "VALIDATE", cmd_validate, hlp_validate },
2099 { "KEYSERVER", cmd_keyserver, hlp_keyserver },
2100 { "KS_SEARCH", cmd_ks_search, hlp_ks_search },
2101 { "KS_GET", cmd_ks_get, hlp_ks_get },
2102 { "KS_FETCH", cmd_ks_fetch, hlp_ks_fetch },
2103 { "KS_PUT", cmd_ks_put, hlp_ks_put },
2104 { "GETINFO", cmd_getinfo, hlp_getinfo },
2105 { "KILLDIRMNGR",cmd_killdirmngr,hlp_killdirmngr },
2106 { "RELOADDIRMNGR",cmd_reloaddirmngr,hlp_reloaddirmngr },
2111 for (i=j=0; table[i].name; i++)
2113 rc = assuan_register_command (ctx, table[i].name, table[i].handler,
2122 /* Note that we do not reset the list of configured keyservers. */
2124 reset_notify (assuan_context_t ctx, char *line)
2126 ctrl_t ctrl = assuan_get_pointer (ctx);
2130 ldapserver_list_free (ctrl->server_local->ldapservers);
2132 ctrl->server_local->ldapservers = NULL;
2137 /* Startup the server and run the main command loop. With FD = -1,
2138 use stdin/stdout. */
2140 start_command_handler (assuan_fd_t fd)
2142 static const char hello[] = "Dirmngr " VERSION " at your service";
2143 static char *hello_line;
2145 assuan_context_t ctx;
2148 ctrl = xtrycalloc (1, sizeof *ctrl);
2150 ctrl->server_local = xtrycalloc (1, sizeof *ctrl->server_local);
2151 if (!ctrl || !ctrl->server_local)
2153 log_error (_("can't allocate control structure: %s\n"),
2159 dirmngr_init_default_ctrl (ctrl);
2161 rc = assuan_new (&ctx);
2164 log_error (_("failed to allocate assuan context: %s\n"),
2169 if (fd == ASSUAN_INVALID_FD)
2171 assuan_fd_t filedes[2];
2173 filedes[0] = assuan_fdopen (0);
2174 filedes[1] = assuan_fdopen (1);
2175 rc = assuan_init_pipe_server (ctx, filedes);
2179 rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED);
2184 assuan_release (ctx);
2185 log_error (_("failed to initialize the server: %s\n"),
2190 rc = register_commands (ctx);
2193 log_error (_("failed to the register commands with Assuan: %s\n"),
2202 const char *cfgname;
2204 cfgname = opt.config_filename? opt.config_filename : "[none]";
2206 n = (30 + strlen (opt.homedir) + strlen (cfgname)
2207 + strlen (hello) + 1);
2208 hello_line = xmalloc (n+1);
2209 snprintf (hello_line, n,
2219 ctrl->server_local->assuan_ctx = ctx;
2220 assuan_set_pointer (ctx, ctrl);
2222 assuan_set_hello_line (ctx, hello_line);
2223 assuan_register_option_handler (ctx, option_handler);
2224 assuan_register_reset_notify (ctx, reset_notify);
2228 rc = assuan_accept (ctx);
2233 log_info (_("Assuan accept problem: %s\n"), gpg_strerror (rc));
2237 #ifndef HAVE_W32_SYSTEM
2240 assuan_peercred_t peercred;
2242 if (!assuan_get_peercred (ctx, &peercred))
2243 log_info ("connection from process %ld (%ld:%ld)\n",
2244 (long)peercred->pid, (long)peercred->uid,
2245 (long)peercred->gid);
2249 rc = assuan_process (ctx);
2252 log_info (_("Assuan processing failed: %s\n"), gpg_strerror (rc));
2258 ldap_wrapper_connection_cleanup (ctrl);
2260 ldapserver_list_free (ctrl->server_local->ldapservers);
2262 ctrl->server_local->ldapservers = NULL;
2264 ctrl->server_local->assuan_ctx = NULL;
2265 assuan_release (ctx);
2267 if (ctrl->server_local->stopme)
2271 log_error ("oops: connection control structure still referenced (%d)\n",
2275 release_ctrl_ocsp_certs (ctrl);
2276 xfree (ctrl->server_local);
2277 dirmngr_deinit_default_ctrl (ctrl);
2283 /* Send a status line back to the client. KEYWORD is the status
2284 keyword, the optional string arguments are blank separated added to
2285 the line, the last argument must be a NULL. */
2287 dirmngr_status (ctrl_t ctrl, const char *keyword, ...)
2289 gpg_error_t err = 0;
2293 va_start (arg_ptr, keyword);
2295 if (ctrl->server_local)
2297 assuan_context_t ctx = ctrl->server_local->assuan_ctx;
2303 while ( (text = va_arg (arg_ptr, const char *)) )
2310 for ( ; *text && n < DIM (buf)-2; n++)
2314 err = assuan_write_status (ctx, keyword, buf);
2322 /* Print a help status line. TEXTLEN gives the length of the text
2323 from TEXT to be printed. The function splits text at LFs. */
2325 dirmngr_status_help (ctrl_t ctrl, const char *text)
2327 gpg_error_t err = 0;
2329 if (ctrl->server_local)
2331 assuan_context_t ctx = ctrl->server_local->assuan_ctx;
2339 for ( ; *text && *text != '\n' && n < DIM (buf)-2; n++)
2344 err = assuan_write_status (ctx, "#", buf);
2346 while (!err && *text);
2352 /* Send a tick progress indicator back. Fixme: This is only done for
2353 the currently active channel. */
2355 dirmngr_tick (ctrl_t ctrl)
2357 static time_t next_tick = 0;
2358 gpg_error_t err = 0;
2359 time_t now = time (NULL);
2363 next_tick = now + 1;
2365 else if ( now > next_tick )
2369 err = dirmngr_status (ctrl, "PROGRESS", "tick", "? 0 0", NULL);
2372 /* Take this as in indication for a cancel request. */
2373 err = gpg_error (GPG_ERR_CANCELED);
2378 next_tick = now + 1;