2015-03-19 11:02:46 +01:00
|
|
|
|
/* ks-engine-ldap.c - talk to a LDAP keyserver
|
|
|
|
|
* Copyright (C) 2001, 2002, 2004, 2005, 2006
|
|
|
|
|
* 2007 Free Software Foundation, Inc.
|
2023-03-20 19:24:49 +01:00
|
|
|
|
* Copyright (C) 2015, 2020, 2023 g10 Code GmbH
|
2015-03-19 11:02:46 +01:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2015-03-19 11:02:46 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
2020-12-18 11:56:15 +01:00
|
|
|
|
#include <npth.h>
|
2023-06-07 10:03:48 +02:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
# ifndef WINVER
|
|
|
|
|
# define WINVER 0x0500 /* Same as in common/sysutils.c */
|
|
|
|
|
# endif
|
|
|
|
|
# include <winsock2.h>
|
|
|
|
|
# include <sddl.h>
|
|
|
|
|
#endif
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
#include "dirmngr.h"
|
|
|
|
|
#include "misc.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
|
#include "../common/userids.h"
|
2020-12-15 08:55:36 +01:00
|
|
|
|
#include "../common/mbox-util.h"
|
2023-03-20 19:24:49 +01:00
|
|
|
|
#include "ks-action.h"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
#include "ks-engine.h"
|
2021-06-08 08:46:59 +02:00
|
|
|
|
#include "ldap-misc.h"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
#include "ldap-parse-uri.h"
|
2021-05-26 14:48:27 +02:00
|
|
|
|
#include "ldapserver.h"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
|
|
|
|
|
/* Flags with infos from the connected server. */
|
|
|
|
|
#define SERVERINFO_REALLDAP 1 /* This is not the PGP keyserver. */
|
2022-10-04 12:44:29 +02:00
|
|
|
|
#define SERVERINFO_PGPKEYV2 2 /* Needs "pgpKeyV2" instead of "pgpKey"*/
|
2020-12-14 19:28:25 +01:00
|
|
|
|
#define SERVERINFO_SCHEMAV2 4 /* Version 2 of the Schema. */
|
|
|
|
|
#define SERVERINFO_NTDS 8 /* Server is an Active Directory. */
|
2023-03-20 19:24:49 +01:00
|
|
|
|
#define SERVERINFO_GENERIC 16 /* Connected in genric mode. */
|
2020-12-14 19:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-05 15:15:14 +02:00
|
|
|
|
/* The page size requested from the server. */
|
|
|
|
|
#define PAGE_SIZE 100
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
#ifndef HAVE_TIMEGM
|
|
|
|
|
time_t timegm(struct tm *tm);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
/* Object to keep state pertaining to this module. */
|
|
|
|
|
struct ks_engine_ldap_local_s
|
|
|
|
|
{
|
|
|
|
|
LDAP *ldap_conn;
|
|
|
|
|
LDAPMessage *message;
|
|
|
|
|
LDAPMessage *msg_iter; /* Iterator for message. */
|
|
|
|
|
unsigned int serverinfo;
|
2023-03-20 19:24:49 +01:00
|
|
|
|
int scope;
|
2022-10-05 15:15:14 +02:00
|
|
|
|
char *basedn;
|
|
|
|
|
char *keyspec;
|
|
|
|
|
char *filter;
|
|
|
|
|
struct berval *pagecookie;
|
|
|
|
|
unsigned int pageno; /* Current page number (starting at 1). */
|
|
|
|
|
unsigned int total; /* Total number of attributes read. */
|
|
|
|
|
int more_pages; /* More pages announced by server. */
|
2022-10-04 12:44:29 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-06-07 10:03:48 +02:00
|
|
|
|
/*-- prototypes --*/
|
|
|
|
|
static char *map_rid_to_dn (ctrl_t ctrl, const char *rid);
|
|
|
|
|
static char *basedn_from_rootdse (ctrl_t ctrl, parsed_uri_t uri);
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
static time_t
|
|
|
|
|
ldap2epochtime (const char *timestr)
|
|
|
|
|
{
|
|
|
|
|
struct tm pgptime;
|
|
|
|
|
time_t answer;
|
|
|
|
|
|
|
|
|
|
memset (&pgptime, 0, sizeof(pgptime));
|
|
|
|
|
|
|
|
|
|
/* YYYYMMDDHHmmssZ */
|
|
|
|
|
|
|
|
|
|
sscanf (timestr, "%4d%2d%2d%2d%2d%2d",
|
|
|
|
|
&pgptime.tm_year,
|
|
|
|
|
&pgptime.tm_mon,
|
|
|
|
|
&pgptime.tm_mday,
|
|
|
|
|
&pgptime.tm_hour,
|
|
|
|
|
&pgptime.tm_min,
|
|
|
|
|
&pgptime.tm_sec);
|
|
|
|
|
|
|
|
|
|
pgptime.tm_year -= 1900;
|
|
|
|
|
pgptime.tm_isdst = -1;
|
|
|
|
|
pgptime.tm_mon--;
|
|
|
|
|
|
|
|
|
|
/* mktime() takes the timezone into account, so we use timegm() */
|
|
|
|
|
|
|
|
|
|
answer = timegm (&pgptime);
|
|
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Caller must free the result. */
|
|
|
|
|
static char *
|
|
|
|
|
tm2ldaptime (struct tm *tm)
|
|
|
|
|
{
|
|
|
|
|
struct tm tmp = *tm;
|
|
|
|
|
char buf[16];
|
|
|
|
|
|
|
|
|
|
/* YYYYMMDDHHmmssZ */
|
|
|
|
|
|
|
|
|
|
tmp.tm_year += 1900;
|
|
|
|
|
tmp.tm_mon ++;
|
|
|
|
|
|
2015-03-25 19:33:59 +01:00
|
|
|
|
snprintf (buf, sizeof buf, "%04d%02d%02d%02d%02d%02dZ",
|
2015-03-19 11:02:46 +01:00
|
|
|
|
tmp.tm_year,
|
|
|
|
|
tmp.tm_mon,
|
|
|
|
|
tmp.tm_mday,
|
|
|
|
|
tmp.tm_hour,
|
|
|
|
|
tmp.tm_min,
|
|
|
|
|
tmp.tm_sec);
|
|
|
|
|
|
|
|
|
|
return xstrdup (buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
/* Caller must free */
|
|
|
|
|
static char *
|
|
|
|
|
epoch2ldaptime (time_t stamp)
|
|
|
|
|
{
|
|
|
|
|
struct tm tm;
|
|
|
|
|
if (gmtime_r (&stamp, &tm))
|
|
|
|
|
return tm2ldaptime (&tm);
|
|
|
|
|
else
|
|
|
|
|
return xstrdup ("INVALID TIME");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-05-19 17:18:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
my_ldap_value_free (char **vals)
|
|
|
|
|
{
|
|
|
|
|
if (vals)
|
|
|
|
|
ldap_value_free (vals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-07 10:03:48 +02:00
|
|
|
|
/* Print a description of supported variables. */
|
|
|
|
|
void
|
|
|
|
|
ks_ldap_help_variables (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
const char data[] =
|
|
|
|
|
"Supported variables in LDAP filter expressions:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"domain - The defaultNamingContext.\n"
|
|
|
|
|
"domain_admins - Group of domain admins.\n"
|
|
|
|
|
"domain_users - Group with all user accounts.\n"
|
|
|
|
|
"domain_guests - Group with the builtin gues account.\n"
|
|
|
|
|
"domain_computers - Group with all clients and servers.\n"
|
|
|
|
|
"cert_publishers - Group with all cert issuing computers.\n"
|
|
|
|
|
"protected_users - Group of users with extra protection.\n"
|
|
|
|
|
"key_admins - Group for delegated access to msdsKeyCredentialLink.\n"
|
|
|
|
|
"enterprise_key_admins - Similar to key_admins.\n"
|
|
|
|
|
"domain_domain_controllers - Group with all domain controllers.\n"
|
|
|
|
|
"sid_domain - SubAuthority numbers.\n";
|
|
|
|
|
|
|
|
|
|
ks_print_help (ctrl, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper function for substitute_vars. */
|
|
|
|
|
static const char *
|
|
|
|
|
getval_for_filter (void *cookie, const char *name)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = cookie;
|
|
|
|
|
const char *result = NULL;
|
|
|
|
|
|
|
|
|
|
if (!strcmp (name, "sid_domain"))
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
PSID mysid;
|
|
|
|
|
static char *sidstr;
|
|
|
|
|
char *s, *s0;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!sidstr)
|
|
|
|
|
{
|
|
|
|
|
mysid = w32_get_user_sid ();
|
|
|
|
|
if (!mysid)
|
|
|
|
|
{
|
|
|
|
|
gpg_err_set_errno (ENOENT);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ConvertSidToStringSid (mysid, &sidstr))
|
|
|
|
|
{
|
|
|
|
|
gpg_err_set_errno (EINVAL);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* Example for SIDSTR:
|
|
|
|
|
* S-1-5-21-3636969917-2569447256-918939550-1127 */
|
|
|
|
|
for (s0=NULL,s=sidstr,i=0; (s=strchr (s, '-')); i++)
|
|
|
|
|
{
|
|
|
|
|
s++;
|
|
|
|
|
if (i == 3)
|
|
|
|
|
s0 = s;
|
|
|
|
|
else if (i==6)
|
|
|
|
|
{
|
|
|
|
|
s[-1] = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!s0)
|
|
|
|
|
{
|
|
|
|
|
log_error ("oops: invalid SID received from OS");
|
|
|
|
|
gpg_err_set_errno (EINVAL);
|
|
|
|
|
LocalFree (sidstr);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
sidstr = s0; /* (We never release SIDSTR thus no memmove.) */
|
|
|
|
|
}
|
|
|
|
|
result = sidstr;
|
|
|
|
|
#else
|
|
|
|
|
gpg_err_set_errno (ENOSYS);
|
|
|
|
|
goto leave;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (name, "domain"))
|
|
|
|
|
result = basedn_from_rootdse (ctrl, NULL);
|
|
|
|
|
else if (!strcmp (name, "domain_admins"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "512");
|
|
|
|
|
else if (!strcmp (name, "domain_users"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "513");
|
|
|
|
|
else if (!strcmp (name, "domain_guests"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "514");
|
|
|
|
|
else if (!strcmp (name, "domain_computers"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "515");
|
|
|
|
|
else if (!strcmp (name, "domain_domain_controllers"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "516");
|
|
|
|
|
else if (!strcmp (name, "cert_publishers"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "517");
|
|
|
|
|
else if (!strcmp (name, "protected_users"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "525");
|
|
|
|
|
else if (!strcmp (name, "key_admins"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "526");
|
|
|
|
|
else if (!strcmp (name, "enterprise_key_admins"))
|
|
|
|
|
result = map_rid_to_dn (ctrl, "527");
|
|
|
|
|
else
|
|
|
|
|
result = ""; /* Unknown variables are empty. */
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* Print a help output for the schemata supported by this module. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_ldap_help (ctrl_t ctrl, parsed_uri_t uri)
|
|
|
|
|
{
|
2017-01-23 16:32:44 +01:00
|
|
|
|
const char data[] =
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"Handler for LDAP URLs:\n"
|
2021-05-26 19:06:10 +02:00
|
|
|
|
" ldap://HOST:PORT/[BASEDN]????[bindname=BINDNAME,password=PASSWORD]\n"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"\n"
|
|
|
|
|
"Note: basedn, bindname and password need to be percent escaped. In\n"
|
|
|
|
|
"particular, spaces need to be replaced with %20 and commas with %2c.\n"
|
2021-05-26 14:48:27 +02:00
|
|
|
|
"Thus bindname will typically be of the form:\n"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"\n"
|
|
|
|
|
" uid=user%2cou=PGP%20Users%2cdc=EXAMPLE%2cdc=ORG\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"The ldaps:// and ldapi:// schemes are also supported. If ldaps is used\n"
|
|
|
|
|
"then the server's certificate will be checked. If it is not valid, any\n"
|
2021-05-25 16:08:56 +02:00
|
|
|
|
"operation will be aborted. Note that ldaps means LDAP with STARTTLS\n"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"\n"
|
2021-05-26 14:48:27 +02:00
|
|
|
|
"As an alternative to an URL a string in this form may be used:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" HOST:PORT:BINDNAME:PASSWORD:BASEDN:FLAGS:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"The use of the percent sign or a colon in one of the string values is\n"
|
|
|
|
|
"currently not supported.\n"
|
|
|
|
|
"\n"
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"Supported methods: search, get, put\n";
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
2015-04-12 01:11:07 +02:00
|
|
|
|
if(!uri)
|
|
|
|
|
err = ks_print_help (ctrl, " ldap");
|
2021-05-26 14:48:27 +02:00
|
|
|
|
else if (uri->is_ldap || uri->opaque)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = ks_print_help (ctrl, data);
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2019-11-26 13:09:35 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
|
|
|
|
/* Create a new empty state object. Returns NULL on error */
|
|
|
|
|
static struct ks_engine_ldap_local_s *
|
|
|
|
|
ks_ldap_new_state (void)
|
|
|
|
|
{
|
2023-03-20 19:24:49 +01:00
|
|
|
|
struct ks_engine_ldap_local_s *state;
|
|
|
|
|
|
|
|
|
|
state = xtrycalloc (1, sizeof(struct ks_engine_ldap_local_s));
|
|
|
|
|
if (state)
|
|
|
|
|
state->scope = LDAP_SCOPE_SUBTREE;
|
|
|
|
|
return state;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear the state object STATE. Returns the STATE object. */
|
|
|
|
|
static struct ks_engine_ldap_local_s *
|
|
|
|
|
ks_ldap_clear_state (struct ks_engine_ldap_local_s *state)
|
|
|
|
|
{
|
|
|
|
|
if (state->ldap_conn)
|
|
|
|
|
{
|
|
|
|
|
ldap_unbind (state->ldap_conn);
|
|
|
|
|
state->ldap_conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (state->message)
|
|
|
|
|
{
|
|
|
|
|
ldap_msgfree (state->message);
|
|
|
|
|
state->message = NULL;
|
|
|
|
|
}
|
2022-10-05 15:15:14 +02:00
|
|
|
|
if (state->pagecookie)
|
|
|
|
|
{
|
|
|
|
|
ber_bvfree (state->pagecookie);
|
|
|
|
|
state->pagecookie = NULL;
|
|
|
|
|
}
|
2022-10-04 12:44:29 +02:00
|
|
|
|
state->serverinfo = 0;
|
2022-10-05 15:15:14 +02:00
|
|
|
|
xfree (state->basedn);
|
2023-03-20 19:24:49 +01:00
|
|
|
|
state->scope = LDAP_SCOPE_SUBTREE;
|
2022-10-05 15:15:14 +02:00
|
|
|
|
state->basedn = NULL;
|
|
|
|
|
xfree (state->keyspec);
|
|
|
|
|
state->keyspec = NULL;
|
|
|
|
|
xfree (state->filter);
|
|
|
|
|
state->filter = NULL;
|
|
|
|
|
state->pageno = 0;
|
|
|
|
|
state->total = 0;
|
|
|
|
|
state->more_pages = 0;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Release a state object. */
|
|
|
|
|
void
|
|
|
|
|
ks_ldap_free_state (struct ks_engine_ldap_local_s *state)
|
|
|
|
|
{
|
|
|
|
|
if (!state)
|
|
|
|
|
return;
|
|
|
|
|
ks_ldap_clear_state (state);
|
|
|
|
|
xfree (state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
/* Helper for ks_ldap_get and ks_ldap_query. On return first_mode and
|
|
|
|
|
* next_mode are set accordingly. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
ks_ldap_prepare_my_state (ctrl_t ctrl, unsigned int ks_get_flags,
|
|
|
|
|
int *first_mode, int *next_mode)
|
|
|
|
|
{
|
|
|
|
|
*first_mode = *next_mode = 0;
|
|
|
|
|
|
|
|
|
|
if ((ks_get_flags & KS_GET_FLAG_FIRST))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->ks_get_state)
|
|
|
|
|
ks_ldap_clear_state (ctrl->ks_get_state);
|
|
|
|
|
else if (!(ctrl->ks_get_state = ks_ldap_new_state ()))
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
*first_mode = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ks_get_flags & KS_GET_FLAG_NEXT))
|
|
|
|
|
{
|
|
|
|
|
if (!ctrl->ks_get_state || !ctrl->ks_get_state->ldap_conn
|
|
|
|
|
|| !ctrl->ks_get_state->message)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ks-ldap: --next requested but no state\n");
|
|
|
|
|
return gpg_error (GPG_ERR_INV_STATE);
|
|
|
|
|
}
|
|
|
|
|
*next_mode = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do not keep an old state around if not needed. */
|
|
|
|
|
if (!(*first_mode || *next_mode))
|
|
|
|
|
{
|
|
|
|
|
ks_ldap_free_state (ctrl->ks_get_state);
|
|
|
|
|
ctrl->ks_get_state = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* Convert a keyspec to a filter. Return an error if the keyspec is
|
|
|
|
|
bad or is not supported. The filter is escaped and returned in
|
|
|
|
|
*filter. It is the caller's responsibility to free *filter.
|
|
|
|
|
*filter is only set if this function returns success (i.e., 0). */
|
|
|
|
|
static gpg_error_t
|
2020-12-17 10:20:28 +01:00
|
|
|
|
keyspec_to_ldap_filter (const char *keyspec, char **filter, int only_exact,
|
|
|
|
|
unsigned int serverinfo)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
/* Remove search type indicator and adjust PATTERN accordingly.
|
|
|
|
|
Note: don't include a preceding 0x when searching by keyid. */
|
|
|
|
|
|
|
|
|
|
/* XXX: Should we include disabled / revoke options? */
|
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
|
|
|
|
char *f = NULL;
|
2015-03-25 19:39:27 +01:00
|
|
|
|
char *freeme = NULL;
|
2020-12-17 18:18:52 +01:00
|
|
|
|
char *p;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
gpg_error_t err = classify_user_id (keyspec, &desc, 1);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
switch (desc.mode)
|
|
|
|
|
{
|
|
|
|
|
case KEYDB_SEARCH_MODE_EXACT:
|
|
|
|
|
f = xasprintf ("(pgpUserID=%s)",
|
2015-03-25 19:39:27 +01:00
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_SUBSTR:
|
|
|
|
|
if (! only_exact)
|
|
|
|
|
f = xasprintf ("(pgpUserID=*%s*)",
|
2015-03-25 19:39:27 +01:00
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAIL:
|
2020-12-17 18:18:52 +01:00
|
|
|
|
freeme = ldap_escape_filter (desc.u.name);
|
|
|
|
|
if (!freeme)
|
2020-12-17 10:20:28 +01:00
|
|
|
|
break;
|
2020-12-17 18:18:52 +01:00
|
|
|
|
if (*freeme == '<' && freeme[1] && freeme[2])
|
|
|
|
|
{
|
|
|
|
|
/* Strip angle brackets. Note that it is does not
|
|
|
|
|
* matter whether we work on the plan or LDAP escaped
|
|
|
|
|
* version of the mailbox. */
|
|
|
|
|
p = freeme + 1;
|
|
|
|
|
if (p[strlen(p)-1] == '>')
|
|
|
|
|
p[strlen(p)-1] = 0;
|
|
|
|
|
}
|
2020-12-17 10:20:28 +01:00
|
|
|
|
else
|
2020-12-17 18:18:52 +01:00
|
|
|
|
p = freeme;
|
|
|
|
|
if ((serverinfo & SERVERINFO_SCHEMAV2))
|
2021-05-17 15:35:27 +02:00
|
|
|
|
f = xasprintf ("(&(gpgMailbox=%s)(!(|(pgpRevoked=1)(pgpDisabled=1))))",
|
|
|
|
|
p);
|
2020-12-17 18:18:52 +01:00
|
|
|
|
else if (!only_exact)
|
|
|
|
|
f = xasprintf ("(pgpUserID=*<%s>*)", p);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILSUB:
|
2022-09-26 14:38:11 +02:00
|
|
|
|
if ((serverinfo & SERVERINFO_SCHEMAV2))
|
|
|
|
|
f = xasprintf("(&(gpgMailbox=*%s*)(!(|(pgpRevoked=1)(pgpDisabled=1))))",
|
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
|
|
|
|
else if (!only_exact)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
f = xasprintf ("(pgpUserID=*<*%s*>*)",
|
2015-03-25 19:39:27 +01:00
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILEND:
|
2022-09-26 14:38:11 +02:00
|
|
|
|
if ((serverinfo & SERVERINFO_SCHEMAV2))
|
|
|
|
|
f = xasprintf("(&(gpgMailbox=*%s)(!(|(pgpRevoked=1)(pgpDisabled=1))))",
|
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
|
|
|
|
else if (!only_exact)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
f = xasprintf ("(pgpUserID=*<*%s>*)",
|
2015-03-25 19:39:27 +01:00
|
|
|
|
(freeme = ldap_escape_filter (desc.u.name)));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
|
|
|
|
f = xasprintf ("(pgpKeyID=%08lX)", (ulong) desc.u.kid[1]);
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
|
|
|
|
f = xasprintf ("(pgpCertID=%08lX%08lX)",
|
|
|
|
|
(ulong) desc.u.kid[0], (ulong) desc.u.kid[1]);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR:
|
2020-12-17 10:20:28 +01:00
|
|
|
|
if ((serverinfo & SERVERINFO_SCHEMAV2))
|
|
|
|
|
{
|
|
|
|
|
freeme = bin2hex (desc.u.fpr, desc.fprlen, NULL);
|
|
|
|
|
if (!freeme)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
f = xasprintf ("(|(gpgFingerprint=%s)(gpgSubFingerprint=%s))",
|
|
|
|
|
freeme, freeme);
|
|
|
|
|
/* FIXME: For an exact search and in case of a match on
|
|
|
|
|
* gpgSubFingerprint we need to check that there is only one
|
|
|
|
|
* matching value. */
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_ISSUER:
|
|
|
|
|
case KEYDB_SEARCH_MODE_ISSUER_SN:
|
|
|
|
|
case KEYDB_SEARCH_MODE_SN:
|
|
|
|
|
case KEYDB_SEARCH_MODE_SUBJECT:
|
|
|
|
|
case KEYDB_SEARCH_MODE_KEYGRIP:
|
|
|
|
|
case KEYDB_SEARCH_MODE_WORDS:
|
|
|
|
|
case KEYDB_SEARCH_MODE_FIRST:
|
|
|
|
|
case KEYDB_SEARCH_MODE_NEXT:
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 19:39:27 +01:00
|
|
|
|
xfree (freeme);
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (! f)
|
|
|
|
|
{
|
|
|
|
|
log_error ("Unsupported search mode.\n");
|
|
|
|
|
return gpg_error (GPG_ERR_NOT_SUPPORTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*filter = f;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-03-25 19:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
|
|
|
|
/* Helper for my_ldap_connect. */
|
2022-09-29 09:48:30 +09:00
|
|
|
|
static char *
|
2022-09-26 15:53:27 +09:00
|
|
|
|
interrogate_ldap_dn (LDAP *ldap_conn, const char *basedn_search,
|
2022-09-29 09:48:30 +09:00
|
|
|
|
unsigned int *r_serverinfo)
|
2022-09-26 15:53:27 +09:00
|
|
|
|
{
|
|
|
|
|
int lerr;
|
|
|
|
|
char **vals;
|
|
|
|
|
LDAPMessage *si_res;
|
|
|
|
|
int is_gnupg = 0;
|
|
|
|
|
char *basedn = NULL;
|
|
|
|
|
char *attr2[] = { "pgpBaseKeySpaceDN", "pgpVersion", "pgpSoftware", NULL };
|
2022-09-29 15:59:43 +02:00
|
|
|
|
char *object;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object = xasprintf ("cn=pgpServerInfo,%s", basedn_search);
|
2022-09-26 15:53:27 +09:00
|
|
|
|
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
lerr = ldap_search_s (ldap_conn, object, LDAP_SCOPE_BASE,
|
|
|
|
|
"(objectClass=*)", attr2, 0, &si_res);
|
|
|
|
|
npth_protect ();
|
|
|
|
|
xfree (object);
|
|
|
|
|
|
|
|
|
|
if (lerr == LDAP_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "pgpBaseKeySpaceDN");
|
|
|
|
|
if (vals && vals[0])
|
2022-09-27 20:20:53 -04:00
|
|
|
|
basedn = xtrystrdup (vals[0]);
|
2022-09-26 15:53:27 +09:00
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "pgpSoftware");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("Server: \t%s\n", vals[0]);
|
|
|
|
|
if (!ascii_strcasecmp (vals[0], "GnuPG"))
|
|
|
|
|
is_gnupg = 1;
|
|
|
|
|
}
|
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "pgpVersion");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("Version:\t%s\n", vals[0]);
|
|
|
|
|
if (is_gnupg)
|
|
|
|
|
{
|
|
|
|
|
const char *fields[2];
|
|
|
|
|
int nfields;
|
|
|
|
|
nfields = split_fields (vals[0], fields, DIM(fields));
|
|
|
|
|
if (nfields > 0 && atoi(fields[0]) > 1)
|
|
|
|
|
*r_serverinfo |= SERVERINFO_SCHEMAV2;
|
|
|
|
|
if (nfields > 1
|
|
|
|
|
&& !ascii_strcasecmp (fields[1], "ntds"))
|
|
|
|
|
*r_serverinfo |= SERVERINFO_NTDS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* From man ldap_search_s: "res parameter of
|
|
|
|
|
ldap_search_ext_s() and ldap_search_s() should be
|
|
|
|
|
freed with ldap_msgfree() regardless of return
|
|
|
|
|
value of these functions. */
|
|
|
|
|
ldap_msgfree (si_res);
|
2022-09-29 09:48:30 +09:00
|
|
|
|
return basedn;
|
2022-09-26 15:53:27 +09:00
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
|
2022-09-29 15:59:43 +02:00
|
|
|
|
|
|
|
|
|
/* Connect to an LDAP server and interrogate it.
|
|
|
|
|
*
|
|
|
|
|
* URI describes the server to connect to and various options
|
|
|
|
|
* including whether to use TLS and the username and password (see
|
2023-03-20 19:24:49 +01:00
|
|
|
|
* ldap_parse_uri for a description of the various fields). Be
|
|
|
|
|
* default a PGP keyserver is assumed; if GENERIC is true a generic
|
|
|
|
|
* ldap conenction is instead established.
|
2022-09-29 15:59:43 +02:00
|
|
|
|
*
|
|
|
|
|
* Returns: The ldap connection handle in *LDAP_CONNP, R_BASEDN is set
|
|
|
|
|
* to the base DN for the PGP key space, several flags will be stored
|
|
|
|
|
* at SERVERINFO, If you pass NULL, then the value won't be returned.
|
|
|
|
|
* It is the caller's responsibility to release *LDAP_CONNP with
|
|
|
|
|
* ldap_unbind and to xfree *BASEDNP. On error these variables are
|
|
|
|
|
* cleared.
|
|
|
|
|
*
|
|
|
|
|
* Note: On success, you still need to check that *BASEDNP is valid.
|
|
|
|
|
* If it is NULL, then the server does not appear to be an OpenPGP
|
|
|
|
|
* keyserver. */
|
2021-05-26 14:48:27 +02:00
|
|
|
|
static gpg_error_t
|
2023-03-20 19:24:49 +01:00
|
|
|
|
my_ldap_connect (parsed_uri_t uri, unsigned int generic, LDAP **ldap_connp,
|
2021-05-26 14:48:27 +02:00
|
|
|
|
char **r_basedn, char **r_host, int *r_use_tls,
|
|
|
|
|
unsigned int *r_serverinfo)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
int lerr;
|
|
|
|
|
ldap_server_t server = NULL;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
LDAP *ldap_conn = NULL;
|
|
|
|
|
char *basedn = NULL;
|
2021-05-26 14:48:27 +02:00
|
|
|
|
char *host = NULL; /* Host to use. */
|
|
|
|
|
int port; /* Port to use. */
|
|
|
|
|
int use_tls; /* 1 = starttls, 2 = ldap-over-tls */
|
|
|
|
|
int use_ntds; /* Use Active Directory authentication. */
|
2022-09-28 09:43:25 +02:00
|
|
|
|
int use_areconly; /* Lookup only via A record (Windows). */
|
2021-05-26 14:48:27 +02:00
|
|
|
|
const char *bindname;
|
|
|
|
|
const char *password;
|
|
|
|
|
const char *basedn_arg;
|
2021-05-26 15:49:14 +02:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2021-05-26 14:48:27 +02:00
|
|
|
|
char *tmpstr;
|
2021-05-26 15:49:14 +02:00
|
|
|
|
#endif
|
2021-05-26 14:48:27 +02:00
|
|
|
|
|
|
|
|
|
if (r_basedn)
|
|
|
|
|
*r_basedn = NULL;
|
|
|
|
|
if (r_host)
|
|
|
|
|
*r_host = NULL;
|
|
|
|
|
if (r_use_tls)
|
|
|
|
|
*r_use_tls = 0;
|
2020-12-14 19:28:25 +01:00
|
|
|
|
*r_serverinfo = 0;
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (uri->opaque)
|
|
|
|
|
{
|
|
|
|
|
server = ldapserver_parse_one (uri->path, NULL, 0);
|
|
|
|
|
if (!server)
|
|
|
|
|
return gpg_error (GPG_ERR_LDAP_OTHER);
|
|
|
|
|
host = server->host;
|
|
|
|
|
port = server->port;
|
|
|
|
|
bindname = server->user;
|
|
|
|
|
password = bindname? server->pass : NULL;
|
|
|
|
|
basedn_arg = server->base;
|
|
|
|
|
use_tls = server->starttls? 1 : server->ldap_over_tls? 2 : 0;
|
|
|
|
|
use_ntds = server->ntds;
|
2022-09-28 09:43:25 +02:00
|
|
|
|
use_areconly = server->areconly;
|
2021-05-26 14:48:27 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
host = uri->host;
|
|
|
|
|
port = uri->port;
|
|
|
|
|
bindname = uri->auth;
|
|
|
|
|
password = bindname? uri_query_value (uri, "password") : NULL;
|
|
|
|
|
basedn_arg = uri->path;
|
|
|
|
|
use_tls = uri->use_tls ? 1 : 0;
|
|
|
|
|
use_ntds = uri->ad_current;
|
2022-09-28 09:43:25 +02:00
|
|
|
|
use_areconly = 0;
|
2021-05-26 14:48:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!port)
|
|
|
|
|
port = use_tls == 2? 636 : 389;
|
|
|
|
|
|
|
|
|
|
if (host)
|
|
|
|
|
{
|
|
|
|
|
host = xtrystrdup (host);
|
|
|
|
|
if (!host)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-14 19:28:25 +01:00
|
|
|
|
|
2021-05-28 15:20:57 +02:00
|
|
|
|
if (opt.verbose)
|
2023-03-20 19:24:49 +01:00
|
|
|
|
log_info ("ldap connect to '%s:%d:%s:%s:%s:%s%s%s'%s\n",
|
2021-05-28 15:20:57 +02:00
|
|
|
|
host, port,
|
|
|
|
|
basedn_arg ? basedn_arg : "",
|
|
|
|
|
bindname ? bindname : "",
|
|
|
|
|
password ? "*****" : "",
|
|
|
|
|
use_tls == 1? "starttls" : use_tls == 2? "ldaptls" : "plain",
|
2022-09-28 09:43:25 +02:00
|
|
|
|
use_ntds ? ",ntds":"",
|
2023-03-20 19:24:49 +01:00
|
|
|
|
use_areconly? ",areconly":"",
|
|
|
|
|
generic? " (generic)":"");
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* If the uri specifies a secure connection and we don't support
|
|
|
|
|
TLS, then fail; don't silently revert to an insecure
|
|
|
|
|
connection. */
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (use_tls)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
#ifndef HAVE_LDAP_START_TLS_S
|
2023-03-20 19:24:49 +01:00
|
|
|
|
log_error ("ks-ldap: can't connect to the server: no TLS support.");
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = GPG_ERR_LDAP_NOT_SUPPORTED;
|
|
|
|
|
goto out;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2021-05-28 15:20:57 +02:00
|
|
|
|
/* Note that host==NULL uses the default domain controller. */
|
2021-05-26 14:48:27 +02:00
|
|
|
|
npth_unprotect ();
|
|
|
|
|
ldap_conn = ldap_sslinit (host, port, (use_tls == 2));
|
|
|
|
|
npth_protect ();
|
2020-12-17 16:09:31 +01:00
|
|
|
|
if (!ldap_conn)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = LdapGetLastError ();
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
|
log_error ("error initializing LDAP '%s:%d': %s\n",
|
|
|
|
|
host, port, ldap_err2string (lerr));
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2022-09-28 09:43:25 +02:00
|
|
|
|
if (use_areconly)
|
|
|
|
|
{
|
|
|
|
|
lerr = ldap_set_option (ldap_conn, LDAP_OPT_AREC_EXCLUSIVE, LDAP_OPT_ON);
|
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ks-ldap: unable to set LDAP_OPT_AREC_EXLUSIVE: %s\n",
|
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
#else /* Unix */
|
|
|
|
|
tmpstr = xtryasprintf ("%s://%s:%d",
|
|
|
|
|
use_tls == 2? "ldaps" : "ldap",
|
|
|
|
|
host, port);
|
|
|
|
|
if (!tmpstr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
lerr = ldap_initialize (&ldap_conn, tmpstr);
|
|
|
|
|
npth_protect ();
|
2021-05-28 15:20:57 +02:00
|
|
|
|
if (lerr != LDAP_SUCCESS || !ldap_conn)
|
2021-05-26 14:48:27 +02:00
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
|
log_error ("error initializing LDAP '%s': %s\n",
|
|
|
|
|
tmpstr, ldap_err2string (lerr));
|
|
|
|
|
xfree (tmpstr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2021-05-26 14:48:27 +02:00
|
|
|
|
xfree (tmpstr);
|
|
|
|
|
#endif /* Unix */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LDAP_SET_OPTION
|
|
|
|
|
{
|
|
|
|
|
int ver = LDAP_VERSION3;
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_set_option (ldap_conn, LDAP_OPT_PROTOCOL_VERSION, &ver);
|
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_error ("ks-ldap: unable to go to LDAP 3: %s\n",
|
2021-05-26 14:48:27 +02:00
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-26 19:06:10 +02:00
|
|
|
|
if (opt.ldaptimeout)
|
|
|
|
|
{
|
|
|
|
|
int ver = opt.ldaptimeout;
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
/* fixme: also use LDAP_OPT_SEND_TIMEOUT? */
|
|
|
|
|
|
2021-05-26 19:06:10 +02:00
|
|
|
|
lerr = ldap_set_option (ldap_conn, LDAP_OPT_TIMELIMIT, &ver);
|
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ks-ldap: unable to set LDAP timelimit to %us: %s\n",
|
|
|
|
|
opt.ldaptimeout, ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2021-05-28 15:20:57 +02:00
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("ldap timeout set to %us\n", opt.ldaptimeout);
|
2021-05-26 19:06:10 +02:00
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2016-09-29 14:17:24 +02:00
|
|
|
|
#ifdef HAVE_LDAP_START_TLS_S
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (use_tls == 1)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2016-09-30 10:57:32 +02:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2015-03-25 19:33:59 +01:00
|
|
|
|
int check_cert = LDAP_OPT_X_TLS_HARD; /* LDAP_OPT_X_TLS_NEVER */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_set_option (ldap_conn,
|
|
|
|
|
LDAP_OPT_X_TLS_REQUIRE_CERT, &check_cert);
|
|
|
|
|
if (lerr)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
log_error ("ldap: error setting an TLS option: %s\n",
|
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2016-09-30 10:57:32 +02:00
|
|
|
|
#else
|
|
|
|
|
/* On Windows, the certificates are checked by default. If the
|
|
|
|
|
option to disable checking mentioned above is ever
|
|
|
|
|
implemented, the way to do that on Windows is to install a
|
|
|
|
|
callback routine using ldap_set_option (..,
|
|
|
|
|
LDAP_OPT_SERVER_CERTIFICATE, ..); */
|
|
|
|
|
#endif
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_start_tls_s (ldap_conn,
|
2016-09-30 10:57:32 +02:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
/* ServerReturnValue, result */
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
#endif
|
|
|
|
|
/* ServerControls, ClientControls */
|
|
|
|
|
NULL, NULL);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (lerr)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
log_error ("ldap: error switching to STARTTLS mode: %s\n",
|
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (use_ntds)
|
2020-12-17 16:09:31 +01:00
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_bind_s (ldap_conn, NULL, NULL, LDAP_AUTH_NEGOTIATE);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
2020-12-17 16:09:31 +01:00
|
|
|
|
{
|
|
|
|
|
log_error ("error binding to LDAP via AD: %s\n",
|
2021-05-26 14:48:27 +02:00
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
2020-12-17 16:09:31 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2021-02-17 17:31:36 +01:00
|
|
|
|
#else
|
2021-05-28 15:20:57 +02:00
|
|
|
|
log_error ("ldap: no Active Directory support but 'ntds' requested\n");
|
2021-02-17 17:31:36 +01:00
|
|
|
|
err = gpg_error (GPG_ERR_NOT_SUPPORTED);
|
|
|
|
|
goto out;
|
|
|
|
|
#endif
|
2020-12-17 16:09:31 +01:00
|
|
|
|
}
|
2021-05-26 14:48:27 +02:00
|
|
|
|
else if (bindname)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_simple_bind_s (ldap_conn, bindname, password);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
log_error ("error binding to LDAP: %s\n", ldap_err2string (lerr));
|
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-17 16:09:31 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* By default we don't bind as there is usually no need to. */
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
if (generic)
|
|
|
|
|
{
|
|
|
|
|
/* Generic use of this function for arbitrary LDAP servers. */
|
|
|
|
|
*r_serverinfo |= SERVERINFO_GENERIC;
|
|
|
|
|
if (basedn_arg && *basedn_arg)
|
|
|
|
|
{
|
|
|
|
|
basedn = xtrystrdup (basedn_arg);
|
|
|
|
|
if (!basedn)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (basedn_arg && *basedn_arg)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
/* User specified base DN. In this case we know the server is a
|
2020-12-14 19:28:25 +01:00
|
|
|
|
* real LDAP server. */
|
2022-09-29 09:48:30 +09:00
|
|
|
|
const char *user_basedn = basedn_arg;
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
*r_serverinfo |= SERVERINFO_REALLDAP;
|
2022-09-27 20:20:53 -04:00
|
|
|
|
|
|
|
|
|
/* First try with provided basedn, else retry up one level.
|
|
|
|
|
* Retry assumes that provided entry is for keyspace,
|
|
|
|
|
* matching old behavior */
|
2022-09-29 09:48:30 +09:00
|
|
|
|
basedn = interrogate_ldap_dn (ldap_conn, user_basedn, r_serverinfo);
|
|
|
|
|
if (!basedn)
|
2022-09-27 20:20:53 -04:00
|
|
|
|
{
|
2022-09-29 09:48:30 +09:00
|
|
|
|
const char *basedn_parent = strchr (user_basedn, ',');
|
2022-09-29 15:59:43 +02:00
|
|
|
|
if (basedn_parent && *basedn_parent)
|
2022-09-29 09:48:30 +09:00
|
|
|
|
basedn = interrogate_ldap_dn (ldap_conn, basedn_parent + 1,
|
|
|
|
|
r_serverinfo);
|
2022-09-27 20:20:53 -04:00
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2020-12-14 19:28:25 +01:00
|
|
|
|
{ /* Look for namingContexts. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
LDAPMessage *res = NULL;
|
|
|
|
|
char *attr[] = { "namingContexts", NULL };
|
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_search_s (ldap_conn, "", LDAP_SCOPE_BASE,
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"(objectClass=*)", attr, 0, &res);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (lerr == LDAP_SUCCESS)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-18 11:56:15 +01:00
|
|
|
|
char **context;
|
|
|
|
|
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
context = ldap_get_values (ldap_conn, res, "namingContexts");
|
|
|
|
|
npth_protect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (context)
|
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
/* We found some, so try each namingContext as the
|
|
|
|
|
* search base and look for pgpBaseKeySpaceDN. Because
|
|
|
|
|
* we found this, we know we're talking to a regular-ish
|
|
|
|
|
* LDAP server and not an LDAP keyserver. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
int i;
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
*r_serverinfo |= SERVERINFO_REALLDAP;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
for (i = 0; context[i] && !basedn; i++)
|
2022-09-29 09:48:30 +09:00
|
|
|
|
basedn = interrogate_ldap_dn (ldap_conn, context[i],
|
|
|
|
|
r_serverinfo);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
ldap_value_free (context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-26 14:48:27 +02:00
|
|
|
|
else /* ldap_search failed. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
/* We don't have an answer yet, which means the server might
|
2020-12-14 19:28:25 +01:00
|
|
|
|
be a PGP.com keyserver. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char **vals;
|
|
|
|
|
LDAPMessage *si_res = NULL;
|
|
|
|
|
|
|
|
|
|
char *attr2[] = { "pgpBaseKeySpaceDN", "version", "software", NULL };
|
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
lerr = ldap_search_s (ldap_conn, "cn=pgpServerInfo", LDAP_SCOPE_BASE,
|
2015-03-19 11:02:46 +01:00
|
|
|
|
"(objectClass=*)", attr2, 0, &si_res);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (lerr == LDAP_SUCCESS)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
/* For the PGP LDAP keyserver, this is always
|
|
|
|
|
* "OU=ACTIVE,O=PGP KEYSPACE,C=US", but it might not be
|
|
|
|
|
* in the future. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "baseKeySpaceDN");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2015-03-25 19:33:59 +01:00
|
|
|
|
basedn = xtrystrdup (vals[0]);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "software");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("ks-ldap: PGP Server: \t%s\n", vals[0]);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, si_res, "version");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("ks-ldap: PGP Server Version:\t%s\n", vals[0]);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* If the version is high enough, use the new
|
|
|
|
|
pgpKeyV2 attribute. This design is iffy at best,
|
|
|
|
|
but it matches how PGP does it. I figure the NAI
|
|
|
|
|
folks assumed that there would never be an LDAP
|
|
|
|
|
keyserver vendor with a different numbering
|
|
|
|
|
scheme. */
|
|
|
|
|
if (atoi (vals[0]) > 1)
|
2020-12-14 19:28:25 +01:00
|
|
|
|
*r_serverinfo |= SERVERINFO_PGPKEYV2;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ldap_msgfree (si_res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* From man ldap_search_s: "res parameter of ldap_search_ext_s()
|
|
|
|
|
and ldap_search_s() should be freed with ldap_msgfree()
|
|
|
|
|
regardless of return value of these functions. */
|
|
|
|
|
ldap_msgfree (res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (!err && opt.debug)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
log_debug ("ldap_conn: %p\n", ldap_conn);
|
2023-03-20 19:24:49 +01:00
|
|
|
|
log_debug ("server_type: %s\n",
|
|
|
|
|
((*r_serverinfo & SERVERINFO_GENERIC)
|
|
|
|
|
? "Generic" :
|
|
|
|
|
(*r_serverinfo & SERVERINFO_REALLDAP)
|
|
|
|
|
? "LDAP" : "PGP.com keyserver") );
|
2015-03-19 11:02:46 +01:00
|
|
|
|
log_debug ("basedn: %s\n", basedn);
|
2023-03-20 19:24:49 +01:00
|
|
|
|
if (!(*r_serverinfo & SERVERINFO_GENERIC))
|
|
|
|
|
log_debug ("pgpkeyattr: %s\n",
|
|
|
|
|
(*r_serverinfo & SERVERINFO_PGPKEYV2)? "pgpKeyV2":"pgpKey");
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:48:27 +02:00
|
|
|
|
ldapserver_list_free (server);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
xfree (basedn);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (ldap_conn)
|
|
|
|
|
ldap_unbind (ldap_conn);
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-05-26 14:48:27 +02:00
|
|
|
|
{
|
|
|
|
|
if (r_basedn)
|
|
|
|
|
*r_basedn = basedn;
|
|
|
|
|
else
|
|
|
|
|
xfree (basedn);
|
|
|
|
|
if (r_host)
|
|
|
|
|
*r_host = host;
|
|
|
|
|
else
|
|
|
|
|
xfree (host);
|
|
|
|
|
|
|
|
|
|
*ldap_connp = ldap_conn;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Extract keys from an LDAP reply and write them out to the output
|
|
|
|
|
stream OUTPUT in a format GnuPG can import (either the OpenPGP
|
|
|
|
|
binary format or armored format). */
|
|
|
|
|
static void
|
|
|
|
|
extract_keys (estream_t output,
|
|
|
|
|
LDAP *ldap_conn, const char *certid, LDAPMessage *message)
|
|
|
|
|
{
|
|
|
|
|
char **vals;
|
|
|
|
|
|
|
|
|
|
es_fprintf (output, "INFO %s BEGIN\n", certid);
|
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
/* Note: ldap_get_values returns a NULL terminated array of
|
2015-03-19 11:02:46 +01:00
|
|
|
|
strings. */
|
2021-05-19 17:18:15 +02:00
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "gpgfingerprint");
|
|
|
|
|
if (vals && vals[0] && vals[0][0])
|
|
|
|
|
es_fprintf (output, "pub:%s:", vals[0]);
|
|
|
|
|
else
|
|
|
|
|
es_fprintf (output, "pub:%s:", certid);
|
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgpkeytype");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (vals[0], "RSA") == 0)
|
|
|
|
|
es_fprintf (output, "1");
|
|
|
|
|
else if (strcmp (vals[0],"DSS/DH") == 0)
|
|
|
|
|
es_fprintf (output, "17");
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
es_fprintf (output, ":");
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgpkeysize");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
int v = atoi (vals[0]);
|
|
|
|
|
if (v > 0)
|
|
|
|
|
es_fprintf (output, "%d", v);
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
es_fprintf (output, ":");
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgpkeycreatetime");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (strlen (vals[0]) == 15)
|
|
|
|
|
es_fprintf (output, "%u", (unsigned int) ldap2epochtime (vals[0]));
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
es_fprintf (output, ":");
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgpkeyexpiretime");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (strlen (vals[0]) == 15)
|
|
|
|
|
es_fprintf (output, "%u", (unsigned int) ldap2epochtime (vals[0]));
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
es_fprintf (output, ":");
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgprevoked");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
if (atoi (vals[0]) == 1)
|
|
|
|
|
es_fprintf (output, "r");
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
es_fprintf (output, "\n");
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "pgpuserid");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; vals[i]; i++)
|
|
|
|
|
es_fprintf (output, "uid:%s\n", vals[i]);
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2023-04-04 08:49:55 +02:00
|
|
|
|
vals = ldap_get_values (ldap_conn, message, "modifyTimestamp");
|
|
|
|
|
if (vals && vals[0])
|
|
|
|
|
{
|
|
|
|
|
gnupg_isotime_t atime;
|
|
|
|
|
if (!rfc4517toisotime (atime, vals[0]))
|
|
|
|
|
es_fprintf (output, "chg:%s:\n", atime);
|
|
|
|
|
}
|
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
es_fprintf (output, "INFO %s END\n", certid);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 17:57:14 +02:00
|
|
|
|
|
|
|
|
|
/* For now we do not support LDAP over Tor. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
no_ldap_due_to_tor (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = gpg_error (GPG_ERR_NOT_SUPPORTED);
|
|
|
|
|
const char *msg = _("LDAP access not possible due to Tor mode");
|
|
|
|
|
|
|
|
|
|
log_error ("%s", msg);
|
|
|
|
|
dirmngr_status_printf (ctrl, "NOTE", "no_ldap_due_to_tor %u %s", err, msg);
|
|
|
|
|
return gpg_error (GPG_ERR_NOT_SUPPORTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
/* Helper for ks_ldap_get. Returns 0 if a key was fetched and printed
|
|
|
|
|
* to FP. The error code GPG_ERR_NO_DATA is returned if no key was
|
|
|
|
|
* printed. Note that FP is updated by this function. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
return_one_keyblock (LDAP *ldap_conn, LDAPMessage *msg, unsigned int serverinfo,
|
|
|
|
|
estream_t *fp, strlist_t *seenp)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
char **vals;
|
|
|
|
|
char **certid;
|
|
|
|
|
|
|
|
|
|
/* Use the long keyid to remove duplicates. The LDAP server returns
|
|
|
|
|
* the same keyid more than once if there are multiple user IDs on
|
|
|
|
|
* the key. Note that this does NOT mean that a keyid that exists
|
|
|
|
|
* multiple times on the keyserver will not be fetched. It means
|
|
|
|
|
* that each KEY, no matter how many user IDs share its keyid, will
|
|
|
|
|
* be fetched only once. If a keyid that belongs to more than one
|
|
|
|
|
* key is fetched, the server quite properly responds with all
|
|
|
|
|
* matching keys. -ds
|
|
|
|
|
*
|
|
|
|
|
* Note that in --first/--next mode we don't do any duplicate
|
|
|
|
|
* detection.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
certid = ldap_get_values (ldap_conn, msg, "pgpcertid");
|
|
|
|
|
if (certid && certid[0])
|
|
|
|
|
{
|
|
|
|
|
if (!seenp || !strlist_find (*seenp, certid[0]))
|
|
|
|
|
{
|
|
|
|
|
/* It's not a duplicate, add it */
|
|
|
|
|
if (seenp)
|
|
|
|
|
add_to_strlist (seenp, certid[0]);
|
|
|
|
|
|
|
|
|
|
if (!*fp)
|
|
|
|
|
{
|
|
|
|
|
*fp = es_fopenmem(0, "rw");
|
|
|
|
|
if (!*fp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extract_keys (*fp, ldap_conn, certid[0], msg);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, msg,
|
|
|
|
|
(serverinfo & SERVERINFO_PGPKEYV2)?
|
|
|
|
|
"pgpKeyV2" : "pgpKey");
|
|
|
|
|
if (!vals)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_to_gpg_err (ldap_conn);
|
|
|
|
|
log_error("ks-ldap: unable to retrieve key %s "
|
|
|
|
|
"from keyserver\n", certid[0]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* We should strip the new lines. */
|
|
|
|
|
es_fprintf (*fp, "KEY 0x%s BEGIN\n", certid[0]);
|
|
|
|
|
es_fputs (vals[0], *fp);
|
|
|
|
|
es_fprintf (*fp, "\nKEY 0x%s END\n", certid[0]);
|
|
|
|
|
|
|
|
|
|
ldap_value_free (vals);
|
|
|
|
|
err = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else /* Duplicate. */
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
my_ldap_value_free (certid);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
/* Helper for ks_ldap_query. Returns 0 if an attr was fetched and
|
|
|
|
|
* printed to FP. The error code GPG_ERR_NO_DATA is returned if no
|
|
|
|
|
* data was printed. Note that FP is updated by this function. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
return_all_attributes (LDAP *ld, LDAPMessage *msg, estream_t *fp)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
BerElement *berctx = NULL;
|
|
|
|
|
char *attr = NULL;
|
|
|
|
|
const char *attrprefix;
|
|
|
|
|
struct berval **values = NULL;
|
|
|
|
|
int idx;
|
|
|
|
|
int any = 0;
|
|
|
|
|
const char *s;
|
|
|
|
|
const char *val;
|
|
|
|
|
size_t len;
|
|
|
|
|
char *mydn;
|
|
|
|
|
|
|
|
|
|
mydn = ldap_get_dn (ld, msg);
|
|
|
|
|
if (!*fp)
|
|
|
|
|
{
|
|
|
|
|
*fp = es_fopenmem(0, "rw");
|
|
|
|
|
if (!*fp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Always print the DN - note that by using only unbkown attributes
|
|
|
|
|
* it is pissible to list just the DNs with out addiional
|
|
|
|
|
* linefeeds. */
|
|
|
|
|
es_fprintf (*fp, "Dn: %s\n", mydn? mydn : "[oops DN missing]");
|
|
|
|
|
|
|
|
|
|
for (npth_unprotect (), attr = ldap_first_attribute (ld, msg, &berctx),
|
|
|
|
|
npth_protect ();
|
|
|
|
|
attr;
|
|
|
|
|
npth_unprotect (), attr = ldap_next_attribute (ld, msg, berctx),
|
|
|
|
|
npth_protect ())
|
|
|
|
|
{
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
values = ldap_get_values_len (ld, msg, attr);
|
|
|
|
|
npth_protect ();
|
|
|
|
|
|
|
|
|
|
if (!values)
|
|
|
|
|
{
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("attribute '%s' not found\n", attr);
|
|
|
|
|
ldap_memfree (attr);
|
|
|
|
|
attr = NULL;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
any = 1;
|
|
|
|
|
|
|
|
|
|
if (opt.verbose > 1)
|
|
|
|
|
{
|
|
|
|
|
log_info ("found attribute '%s'\n", attr);
|
|
|
|
|
for (idx=0; values[idx]; idx++)
|
|
|
|
|
log_info (" length[%d]=%d\n",
|
|
|
|
|
idx, (int)values[0]->bv_len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ascii_strcasecmp (attr, "Dn"))
|
|
|
|
|
attrprefix = "X-";
|
|
|
|
|
else if (*attr == '#')
|
|
|
|
|
attrprefix = "X-hash-";
|
|
|
|
|
else if (*attr == ' ')
|
|
|
|
|
attrprefix = "X-blank-";
|
|
|
|
|
else
|
|
|
|
|
attrprefix = "";
|
|
|
|
|
/* FIXME: We should remap all invalid chars in ATTR. */
|
|
|
|
|
|
|
|
|
|
for (idx=0; values[idx]; idx++)
|
|
|
|
|
{
|
|
|
|
|
es_fprintf (*fp, "%s%s: ", attrprefix, attr);
|
|
|
|
|
val = values[idx]->bv_val;
|
|
|
|
|
len = values[idx]->bv_len;
|
|
|
|
|
while (len && (s = memchr (val, '\n', len)))
|
|
|
|
|
{
|
|
|
|
|
s++; /* We als want to print the LF. */
|
|
|
|
|
if (es_fwrite (val, s - val, 1, *fp) != 1)
|
|
|
|
|
goto fwrite_failed;
|
|
|
|
|
len -= (s-val);
|
|
|
|
|
val = s;
|
|
|
|
|
if (len && es_fwrite (" ", 1, 1, *fp) != 1)
|
|
|
|
|
goto fwrite_failed;
|
|
|
|
|
}
|
|
|
|
|
if (len && es_fwrite (val, len, 1, *fp) != 1)
|
|
|
|
|
goto fwrite_failed;
|
|
|
|
|
if (es_fwrite ("\n", 1, 1, *fp) != 1) /* Final LF. */
|
|
|
|
|
goto fwrite_failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ldap_value_free_len (values);
|
|
|
|
|
values = NULL;
|
|
|
|
|
ldap_memfree (attr);
|
|
|
|
|
attr = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* One final linefeed to prettify the output. */
|
|
|
|
|
if (any && es_fwrite ("\n", 1, 1, *fp) != 1)
|
|
|
|
|
goto fwrite_failed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
if (values)
|
|
|
|
|
ldap_value_free_len (values);
|
|
|
|
|
ldap_memfree (attr);
|
|
|
|
|
if (mydn)
|
|
|
|
|
ldap_memfree (mydn);
|
|
|
|
|
ber_free (berctx, 0);
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
fwrite_failed:
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error writing to stdout: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for ks_ldap_get and ks_ldap_query. Note that KEYSPEC is
|
|
|
|
|
* only used for diagnostics. */
|
2022-10-05 15:15:14 +02:00
|
|
|
|
static gpg_error_t
|
|
|
|
|
search_and_parse (ctrl_t ctrl, const char *keyspec,
|
2023-03-20 19:24:49 +01:00
|
|
|
|
LDAP *ldap_conn, char *basedn, int scope, char *filter,
|
2022-10-05 15:15:14 +02:00
|
|
|
|
char **attrs, LDAPMessage **r_message)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
int l_err, l_reserr;
|
|
|
|
|
LDAPControl *srvctrls[2] = { NULL, NULL };
|
|
|
|
|
int count;
|
|
|
|
|
unsigned int totalcount = 0;
|
|
|
|
|
LDAPControl *pagectrl = NULL;
|
|
|
|
|
LDAPControl **resctrls = NULL;
|
|
|
|
|
|
|
|
|
|
/* first/next mode is used to retrieve many entries; thus we should
|
|
|
|
|
* use paged results. We assume first/next mode if we have a state.
|
|
|
|
|
* We make the paged mode non-critical so that we get at least as
|
|
|
|
|
* many entries the server delivers anyway. */
|
|
|
|
|
if (ctrl->ks_get_state)
|
|
|
|
|
{
|
|
|
|
|
l_err = ldap_create_page_control (ldap_conn, PAGE_SIZE,
|
|
|
|
|
ctrl->ks_get_state->pagecookie, 0,
|
|
|
|
|
&pagectrl);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (l_err);
|
|
|
|
|
log_error ("ks-ldap: create_page_control failed: %s\n",
|
|
|
|
|
ldap_err2string (l_err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctrl->ks_get_state->more_pages = 0;
|
|
|
|
|
srvctrls[0] = pagectrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
npth_unprotect ();
|
2023-03-20 19:24:49 +01:00
|
|
|
|
l_err = ldap_search_ext_s (ldap_conn, basedn, scope,
|
2022-10-05 15:15:14 +02:00
|
|
|
|
filter, attrs, 0,
|
|
|
|
|
srvctrls[0]? srvctrls : NULL, NULL, NULL, 0,
|
|
|
|
|
r_message);
|
|
|
|
|
npth_protect ();
|
|
|
|
|
if (l_err)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (l_err);
|
|
|
|
|
log_error ("ks-ldap: LDAP search error: %s\n", ldap_err2string (l_err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ctrl->ks_get_state)
|
|
|
|
|
{
|
|
|
|
|
l_err = ldap_parse_result (ldap_conn, *r_message, &l_reserr,
|
|
|
|
|
NULL, NULL, NULL, &resctrls, 0);
|
|
|
|
|
if (l_err)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (l_err);
|
|
|
|
|
log_error ("ks-ldap: LDAP parse result error: %s\n",
|
|
|
|
|
ldap_err2string (l_err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* Get the current cookie. */
|
|
|
|
|
if (ctrl->ks_get_state->pagecookie)
|
|
|
|
|
{
|
|
|
|
|
ber_bvfree (ctrl->ks_get_state->pagecookie);
|
|
|
|
|
ctrl->ks_get_state->pagecookie = NULL;
|
|
|
|
|
}
|
|
|
|
|
l_err = ldap_parse_page_control (ldap_conn, resctrls,
|
|
|
|
|
&totalcount,
|
|
|
|
|
&ctrl->ks_get_state->pagecookie);
|
|
|
|
|
if (l_err)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (l_err);
|
|
|
|
|
log_error ("ks-ldap: LDAP parse page control error: %s\n",
|
|
|
|
|
ldap_err2string (l_err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctrl->ks_get_state->pageno++;
|
|
|
|
|
|
|
|
|
|
/* Decide whether there will be more pages. */
|
|
|
|
|
ctrl->ks_get_state->more_pages =
|
|
|
|
|
(ctrl->ks_get_state->pagecookie
|
|
|
|
|
&& ctrl->ks_get_state->pagecookie->bv_val
|
|
|
|
|
&& *ctrl->ks_get_state->pagecookie->bv_val);
|
|
|
|
|
|
|
|
|
|
srvctrls[0] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count = ldap_count_entries (ldap_conn, *r_message);
|
|
|
|
|
if (ctrl->ks_get_state)
|
|
|
|
|
{
|
|
|
|
|
if (count >= 0)
|
|
|
|
|
ctrl->ks_get_state->total += count;
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("ks-ldap: received result page %u%s (%d/%u/%u)\n",
|
|
|
|
|
ctrl->ks_get_state->pageno,
|
|
|
|
|
ctrl->ks_get_state->more_pages? "":" (last)",
|
|
|
|
|
count, ctrl->ks_get_state->total, totalcount);
|
|
|
|
|
}
|
|
|
|
|
if (count < 1)
|
|
|
|
|
{
|
|
|
|
|
if (!ctrl->ks_get_state || ctrl->ks_get_state->pageno == 1)
|
2023-03-20 19:24:49 +01:00
|
|
|
|
log_info ("ks-ldap: '%s' not found on LDAP server\n", keyspec);
|
2022-10-05 15:15:14 +02:00
|
|
|
|
|
|
|
|
|
if (count == -1)
|
|
|
|
|
err = ldap_to_gpg_err (ldap_conn);
|
|
|
|
|
else
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
if (resctrls)
|
|
|
|
|
ldap_controls_free (resctrls);
|
|
|
|
|
if (pagectrl)
|
|
|
|
|
ldap_control_free (pagectrl);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
/* Fetch all entries from the RootDSE and return them as a name value
|
|
|
|
|
* object. */
|
|
|
|
|
static nvc_t
|
|
|
|
|
fetch_rootdse (ctrl_t ctrl, parsed_uri_t uri)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
estream_t infp = NULL;
|
|
|
|
|
uri_item_t puri; /* The broken down URI (only one item used). */
|
|
|
|
|
nvc_t nvc = NULL;
|
|
|
|
|
|
|
|
|
|
/* FIXME: We need the unparsed URI here - use uri_item_t instead
|
|
|
|
|
* of fix the parser to fill in original */
|
|
|
|
|
err = ks_action_parse_uri (uri && uri->original? uri->original : "ldap://",
|
|
|
|
|
&puri);
|
|
|
|
|
if (err)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Reset authentication for a serverless. */
|
|
|
|
|
puri->parsed_uri->ad_current = 0;
|
|
|
|
|
puri->parsed_uri->auth = NULL;
|
|
|
|
|
|
|
|
|
|
if (!strcmp (puri->parsed_uri->scheme, "ldap")
|
|
|
|
|
|| !strcmp (puri->parsed_uri->scheme, "ldaps")
|
|
|
|
|
|| !strcmp (puri->parsed_uri->scheme, "ldapi")
|
|
|
|
|
|| puri->parsed_uri->opaque)
|
|
|
|
|
{
|
|
|
|
|
err = ks_ldap_query (ctrl, puri->parsed_uri, KS_GET_FLAG_ROOTDSE,
|
2023-04-04 08:49:55 +02:00
|
|
|
|
"^&base&(objectclass=*)", NULL, NULL, &infp);
|
2023-03-20 19:24:49 +01:00
|
|
|
|
if (err)
|
|
|
|
|
log_error ("ldap: reading the rootDES failed: %s\n",
|
|
|
|
|
gpg_strerror (err));
|
|
|
|
|
else if ((err = nvc_parse (&nvc, NULL, infp)))
|
|
|
|
|
log_error ("parsing the rootDES failed: %s\n", gpg_strerror (err));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es_fclose (infp);
|
|
|
|
|
release_uri_item_list (puri);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
nvc_release (nvc);
|
|
|
|
|
nvc = NULL;
|
|
|
|
|
}
|
|
|
|
|
return nvc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-07 10:03:48 +02:00
|
|
|
|
/* Return the DN for the given RID. This is used with the Active
|
|
|
|
|
* Directory. */
|
|
|
|
|
static char *
|
|
|
|
|
map_rid_to_dn (ctrl_t ctrl, const char *rid)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
char *result = NULL;
|
|
|
|
|
estream_t infp = NULL;
|
|
|
|
|
uri_item_t puri; /* The broken down URI. */
|
|
|
|
|
nvc_t nvc = NULL;
|
|
|
|
|
char *filter = NULL;
|
|
|
|
|
const char *s;
|
|
|
|
|
char *attr[2] = {"dn", NULL};
|
|
|
|
|
|
|
|
|
|
err = ks_action_parse_uri ("ldap:///", &puri);
|
|
|
|
|
if (err)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
filter = strconcat ("(objectSid=S-1-5-21-$sid_domain-", rid, ")", NULL);
|
|
|
|
|
if (!filter)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
err = ks_ldap_query (ctrl, puri->parsed_uri, KS_GET_FLAG_SUBST,
|
|
|
|
|
filter, attr, NULL, &infp);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ldap: AD query '%s' failed: %s\n", filter,gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if ((err = nvc_parse (&nvc, NULL, infp)))
|
|
|
|
|
{
|
|
|
|
|
log_error ("ldap: parsing the result failed: %s\n",gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (!(s = nvc_get_string (nvc, "Dn:")))
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_NOT_FOUND);
|
|
|
|
|
log_error ("ldap: mapping rid '%s'failed: %s\n", rid, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
result = xtrystrdup (s);
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("ldap: strdup failed: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
es_fclose (infp);
|
|
|
|
|
release_uri_item_list (puri);
|
|
|
|
|
xfree (filter);
|
|
|
|
|
nvc_release (nvc);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
/* Return the baseDN for URI which might have already been cached for
|
|
|
|
|
* this session. */
|
|
|
|
|
static char *
|
|
|
|
|
basedn_from_rootdse (ctrl_t ctrl, parsed_uri_t uri)
|
|
|
|
|
{
|
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
|
|
if (!ctrl->rootdse && !ctrl->rootdse_tried)
|
|
|
|
|
{
|
|
|
|
|
ctrl->rootdse = fetch_rootdse (ctrl, uri);
|
|
|
|
|
ctrl->rootdse_tried = 1;
|
|
|
|
|
if (ctrl->rootdse)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("Dump of all rootDSE attributes:\n");
|
|
|
|
|
nvc_write (ctrl->rootdse, log_get_stream ());
|
|
|
|
|
log_debug ("End of dump\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s = nvc_get_string (ctrl->rootdse, "defaultNamingContext:");
|
|
|
|
|
return s? xtrystrdup (s): NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
/* Get the key described key the KEYSPEC string from the keyserver
|
2022-10-04 12:44:29 +02:00
|
|
|
|
* identified by URI. On success R_FP has an open stream to read the
|
|
|
|
|
* data. KS_GET_FLAGS conveys flags from the client. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_ldap_get (ctrl_t ctrl, parsed_uri_t uri, const char *keyspec,
|
2023-04-04 08:49:55 +02:00
|
|
|
|
unsigned int ks_get_flags, gnupg_isotime_t newer, estream_t *r_fp)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2023-03-20 19:24:49 +01:00
|
|
|
|
gpg_error_t err;
|
2020-12-14 19:28:25 +01:00
|
|
|
|
unsigned int serverinfo;
|
2021-05-26 14:48:27 +02:00
|
|
|
|
char *host = NULL;
|
|
|
|
|
int use_tls;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char *filter = NULL;
|
|
|
|
|
LDAP *ldap_conn = NULL;
|
|
|
|
|
char *basedn = NULL;
|
2023-03-20 19:24:49 +01:00
|
|
|
|
int scope = LDAP_SCOPE_SUBTREE;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
estream_t fp = NULL;
|
|
|
|
|
LDAPMessage *message = NULL;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
LDAPMessage *msg;
|
|
|
|
|
int anykey = 0;
|
|
|
|
|
int first_mode = 0;
|
|
|
|
|
int next_mode = 0;
|
2022-10-05 15:15:14 +02:00
|
|
|
|
int get_first;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
strlist_t seen = NULL; /* The set of entries that we've seen. */
|
|
|
|
|
/* The ordering is significant. Specifically, "pgpcertid" needs to
|
|
|
|
|
* be the second item in the list, since everything after it may be
|
|
|
|
|
* discarded if we aren't in verbose mode. */
|
|
|
|
|
char *attrs[] =
|
|
|
|
|
{
|
|
|
|
|
"dummy", /* (to be be replaced.) */
|
|
|
|
|
"pgpcertid", "pgpuserid", "pgpkeyid", "pgprevoked", "pgpdisabled",
|
2023-04-04 08:49:55 +02:00
|
|
|
|
"pgpkeycreatetime", "modifyTimestamp", "pgpkeysize", "pgpkeytype",
|
2022-10-04 12:44:29 +02:00
|
|
|
|
"gpgfingerprint",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2017-02-01 17:54:14 +01:00
|
|
|
|
if (dirmngr_use_tor ())
|
2015-09-18 16:17:11 +02:00
|
|
|
|
{
|
2022-04-11 17:57:14 +02:00
|
|
|
|
return no_ldap_due_to_tor (ctrl);
|
2015-09-18 16:17:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = ks_ldap_prepare_my_state (ctrl, ks_get_flags, &first_mode, &next_mode);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (next_mode)
|
|
|
|
|
{
|
2022-10-05 15:15:14 +02:00
|
|
|
|
next_again:
|
|
|
|
|
if (!ctrl->ks_get_state->msg_iter && ctrl->ks_get_state->more_pages)
|
|
|
|
|
{
|
|
|
|
|
/* Get the next page of results. */
|
|
|
|
|
if (ctrl->ks_get_state->message)
|
|
|
|
|
{
|
|
|
|
|
ldap_msgfree (ctrl->ks_get_state->message);
|
|
|
|
|
ctrl->ks_get_state->message = NULL;
|
|
|
|
|
}
|
|
|
|
|
attrs[0] = ((ctrl->ks_get_state->serverinfo & SERVERINFO_PGPKEYV2)?
|
|
|
|
|
"pgpKeyV2" : "pgpKey");
|
|
|
|
|
err = search_and_parse (ctrl, ctrl->ks_get_state->keyspec,
|
|
|
|
|
ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->basedn,
|
2023-03-20 19:24:49 +01:00
|
|
|
|
ctrl->ks_get_state->scope,
|
2022-10-05 15:15:14 +02:00
|
|
|
|
ctrl->ks_get_state->filter,
|
|
|
|
|
attrs,
|
|
|
|
|
&ctrl->ks_get_state->message);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
ctrl->ks_get_state->msg_iter = ctrl->ks_get_state->message;
|
|
|
|
|
get_first = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
get_first = 0;
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
while (ctrl->ks_get_state->msg_iter)
|
|
|
|
|
{
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
ctrl->ks_get_state->msg_iter
|
2022-10-05 15:15:14 +02:00
|
|
|
|
= get_first? ldap_first_entry (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter)
|
|
|
|
|
/* */ : ldap_next_entry (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter);
|
2022-10-04 12:44:29 +02:00
|
|
|
|
npth_protect ();
|
2022-10-05 15:15:14 +02:00
|
|
|
|
get_first = 0;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (ctrl->ks_get_state->msg_iter)
|
|
|
|
|
{
|
|
|
|
|
err = return_one_keyblock (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter,
|
|
|
|
|
ctrl->ks_get_state->serverinfo,
|
|
|
|
|
&fp, NULL);
|
|
|
|
|
if (!err)
|
|
|
|
|
break; /* Found. */
|
|
|
|
|
else if (gpg_err_code (err) == GPG_ERR_NO_DATA)
|
2022-10-05 15:15:14 +02:00
|
|
|
|
err = 0; /* Skip empty attributes. */
|
2022-10-04 12:44:29 +02:00
|
|
|
|
else
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-05 15:15:14 +02:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (!ctrl->ks_get_state->msg_iter || !fp)
|
2022-10-05 15:15:14 +02:00
|
|
|
|
{
|
|
|
|
|
ctrl->ks_get_state->msg_iter = NULL;
|
|
|
|
|
if (ctrl->ks_get_state->more_pages)
|
|
|
|
|
goto next_again;
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
else /* Not in --next mode. */
|
|
|
|
|
{
|
|
|
|
|
/* Make sure we are talking to an OpenPGP LDAP server. */
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = my_ldap_connect (uri, 0, &ldap_conn,
|
2022-10-04 12:44:29 +02:00
|
|
|
|
&basedn, &host, &use_tls, &serverinfo);
|
|
|
|
|
if (err || !basedn)
|
|
|
|
|
{
|
|
|
|
|
if (!err)
|
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
/* Now that we have information about the server we can construct a
|
|
|
|
|
* query best suited for the capabilities of the server. */
|
2022-10-05 15:15:14 +02:00
|
|
|
|
if (first_mode && !*keyspec)
|
|
|
|
|
{
|
|
|
|
|
filter = xtrystrdup("(!(|(pgpRevoked=1)(pgpDisabled=1)))");
|
|
|
|
|
err = filter? 0 : gpg_error_from_syserror ();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = keyspec_to_ldap_filter (keyspec, &filter, 1, serverinfo);
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2023-04-04 08:49:55 +02:00
|
|
|
|
if (*newer)
|
|
|
|
|
{
|
|
|
|
|
char *tstr, *fstr;
|
|
|
|
|
|
|
|
|
|
tstr = isotime2rfc4517 (newer);
|
|
|
|
|
if (!tstr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
fstr = strconcat ("(&", filter,
|
|
|
|
|
"(modifyTimestamp>=", tstr, "))", NULL);
|
|
|
|
|
xfree (tstr);
|
|
|
|
|
if (!fstr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
xfree (filter);
|
|
|
|
|
filter = fstr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("ks-ldap: using filter: %s\n", filter);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
/* Replace "dummy". */
|
|
|
|
|
attrs[0] = (serverinfo & SERVERINFO_PGPKEYV2)? "pgpKeyV2" : "pgpKey";
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = search_and_parse (ctrl, keyspec, ldap_conn, basedn, scope,
|
|
|
|
|
filter, attrs, &message);
|
2022-10-05 15:15:14 +02:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
for (npth_unprotect (),
|
2022-10-04 12:44:29 +02:00
|
|
|
|
msg = ldap_first_entry (ldap_conn, message),
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2022-10-04 12:44:29 +02:00
|
|
|
|
msg;
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect (),
|
2022-10-04 12:44:29 +02:00
|
|
|
|
msg = ldap_next_entry (ldap_conn, msg),
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ())
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2022-10-04 12:44:29 +02:00
|
|
|
|
err = return_one_keyblock (ldap_conn, msg, serverinfo,
|
|
|
|
|
&fp, first_mode? NULL : &seen);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
anykey = 1;
|
|
|
|
|
if (first_mode)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (gpg_err_code (err) == GPG_ERR_NO_DATA)
|
|
|
|
|
err = 0; /* Skip empty/duplicate attributes. */
|
|
|
|
|
else
|
|
|
|
|
goto leave;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (ctrl->ks_get_state) /* Save the iterator. */
|
|
|
|
|
ctrl->ks_get_state->msg_iter = msg;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
if (!fp) /* Nothing was found. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
2021-04-13 14:25:16 +02:00
|
|
|
|
|
|
|
|
|
if (!err && anykey)
|
|
|
|
|
err = dirmngr_status_printf (ctrl, "SOURCE", "%s://%s",
|
2021-05-26 14:48:27 +02:00
|
|
|
|
use_tls? "ldaps" : "ldap",
|
|
|
|
|
host? host:"");
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
/* Store our state if needed. */
|
|
|
|
|
if (!err && (ks_get_flags & KS_GET_FLAG_FIRST))
|
|
|
|
|
{
|
|
|
|
|
log_assert (!ctrl->ks_get_state->ldap_conn);
|
|
|
|
|
ctrl->ks_get_state->ldap_conn = ldap_conn;
|
|
|
|
|
ldap_conn = NULL;
|
|
|
|
|
log_assert (!ctrl->ks_get_state->message);
|
|
|
|
|
ctrl->ks_get_state->message = message;
|
|
|
|
|
message = NULL;
|
|
|
|
|
ctrl->ks_get_state->serverinfo = serverinfo;
|
2023-03-20 19:24:49 +01:00
|
|
|
|
ctrl->ks_get_state->scope = scope;
|
2022-10-05 15:15:14 +02:00
|
|
|
|
ctrl->ks_get_state->basedn = basedn;
|
|
|
|
|
basedn = NULL;
|
|
|
|
|
ctrl->ks_get_state->keyspec = keyspec? xtrystrdup (keyspec) : NULL;
|
|
|
|
|
ctrl->ks_get_state->filter = filter;
|
|
|
|
|
filter = NULL;
|
2022-10-04 12:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
if ((ks_get_flags & KS_GET_FLAG_NEXT))
|
|
|
|
|
{
|
|
|
|
|
/* Keep the state in --next mode even with errors. */
|
|
|
|
|
ldap_conn = NULL;
|
|
|
|
|
message = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (message)
|
|
|
|
|
ldap_msgfree (message);
|
|
|
|
|
|
|
|
|
|
if (err)
|
2022-10-05 15:15:14 +02:00
|
|
|
|
es_fclose (fp);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (fp)
|
|
|
|
|
es_fseek (fp, 0, SEEK_SET);
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 12:44:29 +02:00
|
|
|
|
free_strlist (seen);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
xfree (basedn);
|
2021-05-26 14:48:27 +02:00
|
|
|
|
xfree (host);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (ldap_conn)
|
|
|
|
|
ldap_unbind (ldap_conn);
|
|
|
|
|
|
|
|
|
|
xfree (filter);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
/* Search the keyserver identified by URI for keys matching PATTERN.
|
|
|
|
|
On success R_FP has an open stream to read the data. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_ldap_search (ctrl_t ctrl, parsed_uri_t uri, const char *pattern,
|
|
|
|
|
estream_t *r_fp)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
int ldap_err;
|
2020-12-14 19:28:25 +01:00
|
|
|
|
unsigned int serverinfo;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char *filter = NULL;
|
|
|
|
|
LDAP *ldap_conn = NULL;
|
|
|
|
|
char *basedn = NULL;
|
|
|
|
|
estream_t fp = NULL;
|
|
|
|
|
|
|
|
|
|
(void) ctrl;
|
|
|
|
|
|
2017-02-01 17:54:14 +01:00
|
|
|
|
if (dirmngr_use_tor ())
|
2015-09-18 16:17:11 +02:00
|
|
|
|
{
|
2022-04-11 17:57:14 +02:00
|
|
|
|
return no_ldap_due_to_tor (ctrl);
|
2015-09-18 16:17:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
/* Make sure we are talking to an OpenPGP LDAP server. */
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = my_ldap_connect (uri, 0, &ldap_conn, &basedn, NULL, NULL, &serverinfo);
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (err || !basedn)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (!err)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = GPG_ERR_GENERAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 10:20:28 +01:00
|
|
|
|
/* Now that we have information about the server we can construct a
|
|
|
|
|
* query best suited for the capabilities of the server. */
|
|
|
|
|
err = keyspec_to_ldap_filter (pattern, &filter, 0, serverinfo);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("Bad search pattern: '%s'\n", pattern);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
/* Even if we have no results, we want to return a stream. */
|
|
|
|
|
fp = es_fopenmem(0, "rw");
|
2015-03-25 19:39:27 +01:00
|
|
|
|
if (!fp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
char **vals;
|
|
|
|
|
LDAPMessage *res, *each;
|
|
|
|
|
int count = 0;
|
|
|
|
|
strlist_t dupelist = NULL;
|
|
|
|
|
|
|
|
|
|
/* The maximum size of the search, including the optional stuff
|
|
|
|
|
and the trailing \0 */
|
|
|
|
|
char *attrs[] =
|
|
|
|
|
{
|
|
|
|
|
"pgpcertid", "pgpuserid", "pgprevoked", "pgpdisabled",
|
2023-04-04 08:49:55 +02:00
|
|
|
|
"pgpkeycreatetime", "pgpkeyexpiretime", "modifyTimestamp",
|
2021-05-19 17:18:15 +02:00
|
|
|
|
"pgpkeysize", "pgpkeytype", "gpgfingerprint",
|
|
|
|
|
NULL
|
2015-03-19 11:02:46 +01:00
|
|
|
|
};
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("SEARCH '%s' => '%s' BEGIN\n", pattern, filter);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
ldap_err = ldap_search_s (ldap_conn, basedn,
|
|
|
|
|
LDAP_SCOPE_SUBTREE, filter, attrs, 0, &res);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
xfree (filter);
|
|
|
|
|
filter = NULL;
|
|
|
|
|
|
2015-03-25 19:39:27 +01:00
|
|
|
|
if (ldap_err != LDAP_SUCCESS && ldap_err != LDAP_SIZELIMIT_EXCEEDED)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (ldap_err);
|
|
|
|
|
|
|
|
|
|
log_error ("SEARCH %s FAILED %d\n", pattern, err);
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_error ("ks-ldap: LDAP search error: %s\n",
|
2015-03-19 11:02:46 +01:00
|
|
|
|
ldap_err2string (err));
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The LDAP server doesn't return a real count of unique keys, so we
|
|
|
|
|
can't use ldap_count_entries here. */
|
2020-12-18 11:56:15 +01:00
|
|
|
|
for (npth_unprotect (),
|
|
|
|
|
each = ldap_first_entry (ldap_conn, res),
|
|
|
|
|
npth_protect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
each;
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect (),
|
|
|
|
|
each = ldap_next_entry (ldap_conn, each),
|
|
|
|
|
npth_protect ())
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
char **certid = ldap_get_values (ldap_conn, each, "pgpcertid");
|
|
|
|
|
if (certid && certid[0] && ! strlist_find (dupelist, certid[0]))
|
|
|
|
|
{
|
|
|
|
|
add_to_strlist (&dupelist, certid[0]);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (certid);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 19:39:27 +01:00
|
|
|
|
if (ldap_err == LDAP_SIZELIMIT_EXCEEDED)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
if (count == 1)
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_error ("ks-ldap: search results exceeded server limit."
|
2015-03-19 11:02:46 +01:00
|
|
|
|
" First 1 result shown.\n");
|
|
|
|
|
else
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_error ("ks-ldap: search results exceeded server limit."
|
2015-03-19 11:02:46 +01:00
|
|
|
|
" First %d results shown.\n", count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free_strlist (dupelist);
|
|
|
|
|
dupelist = NULL;
|
|
|
|
|
|
|
|
|
|
if (count < 1)
|
|
|
|
|
es_fputs ("info:1:0\n", fp);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
es_fprintf (fp, "info:1:%d\n", count);
|
|
|
|
|
|
|
|
|
|
for (each = ldap_first_entry (ldap_conn, res);
|
|
|
|
|
each;
|
|
|
|
|
each = ldap_next_entry (ldap_conn, each))
|
|
|
|
|
{
|
|
|
|
|
char **certid;
|
|
|
|
|
LDAPMessage *uids;
|
|
|
|
|
|
|
|
|
|
certid = ldap_get_values (ldap_conn, each, "pgpcertid");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (!certid || !certid[0])
|
|
|
|
|
{
|
|
|
|
|
my_ldap_value_free (certid);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* Have we seen this certid before? */
|
|
|
|
|
if (! strlist_find (dupelist, certid[0]))
|
|
|
|
|
{
|
|
|
|
|
add_to_strlist (&dupelist, certid[0]);
|
|
|
|
|
|
2021-05-19 17:18:15 +02:00
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "gpgfingerprint");
|
|
|
|
|
if (vals && vals[0] && vals[0][0])
|
|
|
|
|
es_fprintf (fp, "pub:%s:", vals[0]);
|
|
|
|
|
else
|
|
|
|
|
es_fprintf (fp, "pub:%s:", certid[0]);
|
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2015-03-31 12:26:59 +02:00
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgpkeytype");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
/* The LDAP server doesn't exactly handle this
|
|
|
|
|
well. */
|
|
|
|
|
if (strcasecmp (vals[0], "RSA") == 0)
|
|
|
|
|
es_fputs ("1", fp);
|
|
|
|
|
else if (strcasecmp (vals[0], "DSS/DH") == 0)
|
|
|
|
|
es_fputs ("17", fp);
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
es_fputc (':', fp);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgpkeysize");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
/* Not sure why, but some keys are listed with a
|
|
|
|
|
key size of 0. Treat that like an unknown. */
|
|
|
|
|
if (atoi (vals[0]) > 0)
|
|
|
|
|
es_fprintf (fp, "%d", atoi (vals[0]));
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
es_fputc (':', fp);
|
|
|
|
|
|
|
|
|
|
/* YYYYMMDDHHmmssZ */
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgpkeycreatetime");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if(vals && vals[0] && strlen (vals[0]) == 15)
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
es_fprintf (fp, "%u",
|
|
|
|
|
(unsigned int) ldap2epochtime(vals[0]));
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
es_fputc (':', fp);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgpkeyexpiretime");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0] && strlen (vals[0]) == 15)
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
es_fprintf (fp, "%u",
|
|
|
|
|
(unsigned int) ldap2epochtime (vals[0]));
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
es_fputc (':', fp);
|
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgprevoked");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (atoi (vals[0]) == 1)
|
|
|
|
|
es_fprintf (fp, "r");
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "pgpdisabled");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (vals && vals[0])
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (atoi (vals[0]) ==1)
|
|
|
|
|
es_fprintf (fp, "d");
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2015-03-31 12:26:59 +02:00
|
|
|
|
es_fputc (':', fp);
|
|
|
|
|
|
2023-04-04 08:49:55 +02:00
|
|
|
|
vals = ldap_get_values (ldap_conn, each, "modifyTimestamp");
|
|
|
|
|
if(vals && vals[0])
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
2023-04-04 08:49:55 +02:00
|
|
|
|
gnupg_isotime_t atime;
|
|
|
|
|
if (rfc4517toisotime (atime, vals[0]))
|
|
|
|
|
*atime = 0;
|
|
|
|
|
es_fprintf (fp, "%s", atime);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (vals);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2015-03-31 12:26:59 +02:00
|
|
|
|
es_fprintf (fp, "\n");
|
|
|
|
|
|
|
|
|
|
/* Now print all the uids that have this certid */
|
|
|
|
|
for (uids = ldap_first_entry (ldap_conn, res);
|
|
|
|
|
uids;
|
|
|
|
|
uids = ldap_next_entry (ldap_conn, uids))
|
|
|
|
|
{
|
|
|
|
|
vals = ldap_get_values (ldap_conn, uids, "pgpcertid");
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (!vals || !vals[0])
|
|
|
|
|
{
|
|
|
|
|
my_ldap_value_free (vals);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
2021-05-19 17:18:15 +02:00
|
|
|
|
if (!ascii_strcasecmp (certid[0], vals[0]))
|
2015-03-31 12:26:59 +02:00
|
|
|
|
{
|
|
|
|
|
char **uidvals;
|
|
|
|
|
|
|
|
|
|
es_fprintf (fp, "uid:");
|
|
|
|
|
|
|
|
|
|
uidvals = ldap_get_values (ldap_conn,
|
|
|
|
|
uids, "pgpuserid");
|
|
|
|
|
if (uidvals)
|
|
|
|
|
{
|
2021-05-19 17:18:15 +02:00
|
|
|
|
/* Need to percent escape any colons */
|
|
|
|
|
char *quoted = try_percent_escape (uidvals[0],
|
|
|
|
|
NULL);
|
|
|
|
|
if (quoted)
|
|
|
|
|
es_fputs (quoted, fp);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
xfree (quoted);
|
|
|
|
|
}
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (uidvals);
|
2015-03-31 12:26:59 +02:00
|
|
|
|
|
|
|
|
|
es_fprintf (fp, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ldap_value_free(vals);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 17:18:15 +02:00
|
|
|
|
my_ldap_value_free (certid);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ldap_msgfree (res);
|
|
|
|
|
free_strlist (dupelist);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("SEARCH %s END\n", pattern);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
2021-05-19 17:18:15 +02:00
|
|
|
|
es_fclose (fp);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Return the read stream. */
|
|
|
|
|
if (fp)
|
|
|
|
|
es_fseek (fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xfree (basedn);
|
|
|
|
|
|
|
|
|
|
if (ldap_conn)
|
|
|
|
|
ldap_unbind (ldap_conn);
|
|
|
|
|
|
|
|
|
|
xfree (filter);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2015-03-25 19:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* A modlist describes a set of changes to an LDAP entry. (An entry
|
|
|
|
|
consists of 1 or more attributes. Attributes are <name, value>
|
|
|
|
|
pairs. Note: an attribute may be multi-valued in which case
|
|
|
|
|
multiple values are associated with a single name.)
|
|
|
|
|
|
|
|
|
|
A modlist is a NULL terminated array of struct LDAPMod's.
|
|
|
|
|
|
|
|
|
|
Thus, if we have:
|
|
|
|
|
|
|
|
|
|
LDAPMod **modlist;
|
|
|
|
|
|
|
|
|
|
Then:
|
|
|
|
|
|
|
|
|
|
modlist[i]
|
|
|
|
|
|
|
|
|
|
Is the ith modification.
|
|
|
|
|
|
|
|
|
|
Each LDAPMod describes a change to a single attribute. Further,
|
|
|
|
|
there is one modification for each attribute that we want to
|
|
|
|
|
change. The attribute's new value is stored in LDAPMod.mod_values.
|
|
|
|
|
If the attribute is multi-valued, we still only use a single
|
|
|
|
|
LDAPMod structure: mod_values is a NULL-terminated array of
|
|
|
|
|
strings. To delete an attribute from an entry, we set mod_values
|
|
|
|
|
to NULL.
|
|
|
|
|
|
|
|
|
|
Thus, if:
|
|
|
|
|
|
|
|
|
|
modlist[i]->mod_values == NULL
|
|
|
|
|
|
|
|
|
|
then we remove the attribute.
|
|
|
|
|
|
|
|
|
|
(Using LDAP_MOD_DELETE doesn't work here as we don't know if the
|
|
|
|
|
attribute in question exists or not.)
|
|
|
|
|
|
|
|
|
|
Note: this function does NOT copy or free ATTR. It does copy
|
|
|
|
|
VALUE. */
|
|
|
|
|
static void
|
|
|
|
|
modlist_add (LDAPMod ***modlistp, char *attr, const char *value)
|
|
|
|
|
{
|
|
|
|
|
LDAPMod **modlist = *modlistp;
|
|
|
|
|
|
|
|
|
|
LDAPMod **m;
|
|
|
|
|
int nummods = 0;
|
|
|
|
|
|
|
|
|
|
/* Search modlist for the attribute we're playing with. If modlist
|
|
|
|
|
is NULL, then the list is empty. Recall: modlist is a NULL
|
|
|
|
|
terminated array. */
|
|
|
|
|
for (m = modlist; m && *m; m++, nummods ++)
|
|
|
|
|
{
|
|
|
|
|
/* The attribute is already on the list. */
|
|
|
|
|
char **ptr;
|
|
|
|
|
int numvalues = 0;
|
|
|
|
|
|
|
|
|
|
if (strcasecmp ((*m)->mod_type, attr) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* We have this attribute already, so when the REPLACE happens,
|
|
|
|
|
the server attributes will be replaced anyway. */
|
|
|
|
|
if (! value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Attributes can be multi-valued. See if the value is already
|
|
|
|
|
present. mod_values is a NULL terminated array of pointers.
|
|
|
|
|
Note: mod_values can be NULL. */
|
|
|
|
|
for (ptr = (*m)->mod_values; ptr && *ptr; ptr++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (*ptr, value) == 0)
|
|
|
|
|
/* Duplicate value, we're done. */
|
|
|
|
|
return;
|
|
|
|
|
numvalues ++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Append the value. */
|
|
|
|
|
ptr = xrealloc ((*m)->mod_values, sizeof (char *) * (numvalues + 2));
|
|
|
|
|
|
|
|
|
|
(*m)->mod_values = ptr;
|
|
|
|
|
ptr[numvalues] = xstrdup (value);
|
|
|
|
|
|
|
|
|
|
ptr[numvalues + 1] = NULL;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We didn't find the attr, so make one and add it to the end */
|
|
|
|
|
|
|
|
|
|
/* Like attribute values, the list of attributes is NULL terminated
|
|
|
|
|
array of pointers. */
|
|
|
|
|
modlist = xrealloc (modlist, sizeof (LDAPMod *) * (nummods + 2));
|
|
|
|
|
|
|
|
|
|
*modlistp = modlist;
|
|
|
|
|
modlist[nummods] = xmalloc (sizeof (LDAPMod));
|
|
|
|
|
|
|
|
|
|
modlist[nummods]->mod_op = LDAP_MOD_REPLACE;
|
|
|
|
|
modlist[nummods]->mod_type = attr;
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
modlist[nummods]->mod_values = xmalloc (sizeof(char *) * 2);
|
|
|
|
|
|
|
|
|
|
modlist[nummods]->mod_values[0] = xstrdup (value);
|
|
|
|
|
modlist[nummods]->mod_values[1] = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
modlist[nummods]->mod_values = NULL;
|
|
|
|
|
|
|
|
|
|
modlist[nummods + 1] = NULL;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Look up the value of an attribute in the specified modlist. If the
|
|
|
|
|
attribute is not on the mod list, returns NULL. The result is a
|
|
|
|
|
NULL-terminated array of strings. Don't change it. */
|
|
|
|
|
static char **
|
|
|
|
|
modlist_lookup (LDAPMod **modlist, const char *attr)
|
|
|
|
|
{
|
|
|
|
|
LDAPMod **m;
|
|
|
|
|
for (m = modlist; m && *m; m++)
|
|
|
|
|
{
|
|
|
|
|
if (strcasecmp ((*m)->mod_type, attr) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return (*m)->mod_values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Dump a modlist to a file. This is useful for debugging. */
|
|
|
|
|
static estream_t modlist_dump (LDAPMod **modlist, estream_t output)
|
2015-07-26 12:50:16 +02:00
|
|
|
|
GPGRT_ATTR_USED;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
static estream_t
|
|
|
|
|
modlist_dump (LDAPMod **modlist, estream_t output)
|
|
|
|
|
{
|
|
|
|
|
LDAPMod **m;
|
|
|
|
|
|
|
|
|
|
int opened = 0;
|
2015-03-25 19:39:27 +01:00
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (! output)
|
|
|
|
|
{
|
|
|
|
|
output = es_fopenmem (0, "rw");
|
2015-03-25 19:39:27 +01:00
|
|
|
|
if (!output)
|
|
|
|
|
return NULL;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
opened = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (m = modlist; m && *m; m++)
|
|
|
|
|
{
|
|
|
|
|
es_fprintf (output, " %s:", (*m)->mod_type);
|
|
|
|
|
|
|
|
|
|
if (! (*m)->mod_values)
|
|
|
|
|
es_fprintf(output, " delete.\n");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char **ptr;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
int multi = 0;
|
|
|
|
|
if ((*m)->mod_values[0] && (*m)->mod_values[1])
|
|
|
|
|
/* Have at least 2. */
|
|
|
|
|
multi = 1;
|
|
|
|
|
|
|
|
|
|
if (multi)
|
|
|
|
|
es_fprintf (output, "\n");
|
|
|
|
|
|
|
|
|
|
for ((ptr = (*m)->mod_values), (i = 1); ptr && *ptr; ptr++, i ++)
|
|
|
|
|
{
|
2015-03-31 14:23:13 +02:00
|
|
|
|
/* Assuming terminals are about 80 characters wide,
|
2017-02-20 16:19:50 -05:00
|
|
|
|
display at most about 10 lines of debugging
|
2015-03-31 14:23:13 +02:00
|
|
|
|
output. If we do trim the buffer, append '...' to
|
|
|
|
|
the end. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
const int max_len = 10 * 70;
|
|
|
|
|
size_t value_len = strlen (*ptr);
|
2015-03-31 14:23:13 +02:00
|
|
|
|
int elide = value_len > max_len;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (multi)
|
|
|
|
|
es_fprintf (output, " %d. ", i);
|
2015-03-31 14:23:13 +02:00
|
|
|
|
es_fprintf (output, "`%.*s", max_len, *ptr);
|
|
|
|
|
if (elide)
|
|
|
|
|
es_fprintf (output, "...' (%zd bytes elided)",
|
2015-03-19 11:02:46 +01:00
|
|
|
|
value_len - max_len);
|
2015-03-31 14:23:13 +02:00
|
|
|
|
else
|
|
|
|
|
es_fprintf (output, "'");
|
2015-03-19 11:02:46 +01:00
|
|
|
|
es_fprintf (output, "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opened)
|
|
|
|
|
es_fseek (output, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free all of the memory allocated by the mod list. This assumes
|
|
|
|
|
that the attribute names don't have to be freed, but the attributes
|
|
|
|
|
values do. (Which is what modlist_add does.) */
|
|
|
|
|
static void
|
|
|
|
|
modlist_free (LDAPMod **modlist)
|
|
|
|
|
{
|
|
|
|
|
LDAPMod **ml;
|
|
|
|
|
|
|
|
|
|
if (! modlist)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Unwind and free the whole modlist structure */
|
|
|
|
|
|
|
|
|
|
/* The modlist is a NULL terminated array of pointers. */
|
|
|
|
|
for (ml = modlist; *ml; ml++)
|
|
|
|
|
{
|
|
|
|
|
LDAPMod *mod = *ml;
|
|
|
|
|
char **ptr;
|
|
|
|
|
|
|
|
|
|
/* The list of values is a NULL termianted array of pointers.
|
|
|
|
|
If the list is NULL, there are no values. */
|
|
|
|
|
|
|
|
|
|
if (mod->mod_values)
|
|
|
|
|
{
|
|
|
|
|
for (ptr = mod->mod_values; *ptr; ptr++)
|
2015-03-25 19:33:59 +01:00
|
|
|
|
xfree (*ptr);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2015-03-25 19:33:59 +01:00
|
|
|
|
xfree (mod->mod_values);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 19:33:59 +01:00
|
|
|
|
xfree (mod);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
2015-03-25 19:33:59 +01:00
|
|
|
|
xfree (modlist);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Append two onto the end of one. Two is not freed, but its pointers
|
|
|
|
|
are now part of one. Make sure you don't free them both!
|
|
|
|
|
|
|
|
|
|
As long as you don't add anything to ONE, TWO is still valid.
|
|
|
|
|
After that all bets are off. */
|
|
|
|
|
static void
|
|
|
|
|
modlists_join (LDAPMod ***one, LDAPMod **two)
|
|
|
|
|
{
|
|
|
|
|
int i, one_count = 0, two_count = 0;
|
|
|
|
|
LDAPMod **grow;
|
|
|
|
|
|
|
|
|
|
if (!*two)
|
|
|
|
|
/* two is empty. Nothing to do. */
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!*one)
|
|
|
|
|
/* one is empty. Just set it equal to *two. */
|
|
|
|
|
{
|
|
|
|
|
*one = two;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (grow = *one; *grow; grow++)
|
|
|
|
|
one_count ++;
|
|
|
|
|
|
|
|
|
|
for (grow = two; *grow; grow++)
|
|
|
|
|
two_count ++;
|
|
|
|
|
|
|
|
|
|
grow = xrealloc (*one, sizeof(LDAPMod *) * (one_count + two_count + 1));
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < two_count; i++)
|
|
|
|
|
grow[one_count + i] = two[i];
|
|
|
|
|
|
|
|
|
|
grow[one_count + i] = NULL;
|
|
|
|
|
|
|
|
|
|
*one = grow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Given a string, unescape C escapes. In particular, \xXX. This
|
|
|
|
|
modifies the string in place. */
|
|
|
|
|
static void
|
|
|
|
|
uncescape (char *str)
|
|
|
|
|
{
|
2015-03-25 19:39:27 +01:00
|
|
|
|
size_t r = 0;
|
|
|
|
|
size_t w = 0;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
char *first = strchr (str, '\\');
|
|
|
|
|
if (! first)
|
|
|
|
|
/* No backslashes => no escaping. We're done. */
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Start at the first '\\'. */
|
|
|
|
|
r = w = (uintptr_t) first - (uintptr_t) str;
|
|
|
|
|
|
|
|
|
|
while (str[r])
|
|
|
|
|
{
|
2015-03-25 19:39:27 +01:00
|
|
|
|
/* XXX: What to do about bad escapes?
|
|
|
|
|
XXX: hextobyte already checks the string thus the hexdigitp
|
|
|
|
|
could be removed. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
if (str[r] == '\\' && str[r + 1] == 'x'
|
2015-03-25 19:39:27 +01:00
|
|
|
|
&& str[r+2] && str[r+3]
|
|
|
|
|
&& hexdigitp (str + r + 2)
|
|
|
|
|
&& hexdigitp (str + r + 3))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
int x = hextobyte (&str[r + 2]);
|
2021-06-08 08:46:59 +02:00
|
|
|
|
log_assert (0 <= x && x <= 0xff);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
str[w] = x;
|
|
|
|
|
|
|
|
|
|
/* We consumed 4 characters and wrote 1. */
|
|
|
|
|
r += 4;
|
|
|
|
|
w ++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
str[w ++] = str[r ++];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str[w] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Given one line from an info block (`gpg --list-{keys,sigs}
|
|
|
|
|
--with-colons KEYID'), pull it apart and fill in the modlist with
|
2020-12-15 08:55:36 +01:00
|
|
|
|
the relevant (for the LDAP schema) attributes. EXTRACT_STATE
|
|
|
|
|
should initally be set to 0 by the caller. SCHEMAV2 is set if the
|
|
|
|
|
server supports the version 2 schema. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
static void
|
2020-12-15 08:55:36 +01:00
|
|
|
|
extract_attributes (LDAPMod ***modlist, int *extract_state,
|
|
|
|
|
char *line, int schemav2)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
int field_count;
|
|
|
|
|
char **fields;
|
|
|
|
|
char *keyid;
|
|
|
|
|
int is_pub, is_sub, is_uid, is_sig;
|
|
|
|
|
|
|
|
|
|
/* Remove trailing whitespace */
|
2015-03-25 19:39:27 +01:00
|
|
|
|
trim_trailing_spaces (line);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
fields = strsplit (line, ':', '\0', &field_count);
|
|
|
|
|
if (field_count == 1)
|
2015-11-16 12:41:46 +01:00
|
|
|
|
/* We only have a single field. There is definitely nothing to
|
2015-03-19 11:02:46 +01:00
|
|
|
|
do. */
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if (field_count < 7)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2020-12-15 08:55:36 +01:00
|
|
|
|
is_pub = !ascii_strcasecmp ("pub", fields[0]);
|
|
|
|
|
is_sub = !ascii_strcasecmp ("sub", fields[0]);
|
|
|
|
|
is_uid = !ascii_strcasecmp ("uid", fields[0]);
|
|
|
|
|
is_sig = !ascii_strcasecmp ("sig", fields[0]);
|
|
|
|
|
if (!ascii_strcasecmp ("fpr", fields[0]))
|
|
|
|
|
{
|
|
|
|
|
/* Special treatment for a fingerprint. */
|
|
|
|
|
if (!(*extract_state & 1))
|
|
|
|
|
goto out; /* Stray fingerprint line - ignore. */
|
|
|
|
|
*extract_state &= ~1;
|
|
|
|
|
if (field_count >= 10 && schemav2)
|
|
|
|
|
{
|
|
|
|
|
if ((*extract_state & 2))
|
|
|
|
|
modlist_add (modlist, "gpgFingerprint", fields[9]);
|
|
|
|
|
else
|
|
|
|
|
modlist_add (modlist, "gpgSubFingerprint", fields[9]);
|
|
|
|
|
}
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*extract_state &= ~(1|2);
|
|
|
|
|
if (is_pub)
|
|
|
|
|
*extract_state |= (1|2);
|
|
|
|
|
else if (is_sub)
|
|
|
|
|
*extract_state |= 1;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (!is_pub && !is_sub && !is_uid && !is_sig)
|
2020-12-15 08:55:36 +01:00
|
|
|
|
goto out; /* Not a relevant line. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
keyid = fields[4];
|
|
|
|
|
|
|
|
|
|
if (is_uid && strlen (keyid) == 0)
|
2020-12-15 08:55:36 +01:00
|
|
|
|
; /* The uid record type can have an empty keyid. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
else if (strlen (keyid) == 16
|
|
|
|
|
&& strspn (keyid, "0123456789aAbBcCdDeEfF") == 16)
|
2020-12-15 08:55:36 +01:00
|
|
|
|
; /* Otherwise, we expect exactly 16 hex characters. */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error ("malformed record!\n");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
2015-03-25 19:33:59 +01:00
|
|
|
|
int disabled = 0;
|
|
|
|
|
int revoked = 0;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char *flags;
|
|
|
|
|
for (flags = fields[1]; *flags; flags ++)
|
|
|
|
|
switch (*flags)
|
|
|
|
|
{
|
|
|
|
|
case 'r':
|
|
|
|
|
case 'R':
|
|
|
|
|
revoked = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
|
case 'D':
|
|
|
|
|
disabled = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Note: we always create the pgpDisabled and pgpRevoked
|
|
|
|
|
attributes, regardless of whether the key is disabled/revoked
|
|
|
|
|
or not. This is because a very common search is like
|
|
|
|
|
"(&(pgpUserID=*isabella*)(pgpDisabled=0))" */
|
|
|
|
|
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
|
|
|
|
modlist_add (modlist,"pgpDisabled", disabled ? "1" : "0");
|
|
|
|
|
modlist_add (modlist,"pgpRevoked", revoked ? "1" : "0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub || is_sub)
|
|
|
|
|
{
|
2018-10-25 17:21:52 +02:00
|
|
|
|
char padded[6];
|
|
|
|
|
int val;
|
|
|
|
|
|
|
|
|
|
val = atoi (fields[2]);
|
|
|
|
|
if (val < 99999 && val > 0)
|
|
|
|
|
{
|
|
|
|
|
/* We zero pad this on the left to make PGP happy. */
|
|
|
|
|
snprintf (padded, sizeof padded, "%05u", val);
|
|
|
|
|
modlist_add (modlist, "pgpKeySize", padded);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
|
|
|
|
char *algo = fields[3];
|
|
|
|
|
int val = atoi (algo);
|
|
|
|
|
switch (val)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
algo = "RSA";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 17:
|
|
|
|
|
algo = "DSS/DH";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
algo = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (algo)
|
2018-10-25 17:21:52 +02:00
|
|
|
|
modlist_add (modlist, "pgpKeyType", algo);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub || is_sub || is_sig)
|
|
|
|
|
{
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
2020-12-15 08:55:36 +01:00
|
|
|
|
modlist_add (modlist, "pgpCertID", keyid); /* Long keyid(!) */
|
|
|
|
|
modlist_add (modlist, "pgpKeyID", &keyid[8]); /* Short keyid */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_sub)
|
2020-12-15 08:55:36 +01:00
|
|
|
|
modlist_add (modlist, "pgpSubKeyID", keyid); /* Long keyid(!) */
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
|
|
|
|
char *create_time = fields[5];
|
|
|
|
|
|
|
|
|
|
if (strlen (create_time) == 0)
|
|
|
|
|
create_time = NULL;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *create_time_orig = create_time;
|
|
|
|
|
struct tm tm;
|
|
|
|
|
time_t t;
|
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
|
|
memset (&tm, 0, sizeof (tm));
|
|
|
|
|
|
|
|
|
|
/* parse_timestamp handles both seconds fromt he epoch and
|
|
|
|
|
ISO 8601 format. We also need to handle YYYY-MM-DD
|
|
|
|
|
format (as generated by gpg1 --with-colons --list-key).
|
|
|
|
|
Check that first and then if it fails, then try
|
|
|
|
|
parse_timestamp. */
|
|
|
|
|
|
2015-04-10 13:05:38 +02:00
|
|
|
|
if (!isodate_human_to_tm (create_time, &tm))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
create_time = tm2ldaptime (&tm);
|
|
|
|
|
else if ((t = parse_timestamp (create_time, &end)) != (time_t) -1
|
|
|
|
|
&& *end == '\0')
|
|
|
|
|
{
|
2015-04-10 13:05:38 +02:00
|
|
|
|
|
|
|
|
|
if (!gnupg_gmtime (&t, &tm))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
create_time = NULL;
|
|
|
|
|
else
|
|
|
|
|
create_time = tm2ldaptime (&tm);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
create_time = NULL;
|
|
|
|
|
|
|
|
|
|
if (! create_time)
|
|
|
|
|
/* Failed to parse string. */
|
|
|
|
|
log_error ("Failed to parse creation time ('%s')",
|
|
|
|
|
create_time_orig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (create_time)
|
|
|
|
|
{
|
|
|
|
|
modlist_add (modlist, "pgpKeyCreateTime", create_time);
|
|
|
|
|
xfree (create_time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_pub)
|
|
|
|
|
{
|
|
|
|
|
char *expire_time = fields[6];
|
|
|
|
|
|
|
|
|
|
if (strlen (expire_time) == 0)
|
|
|
|
|
expire_time = NULL;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *expire_time_orig = expire_time;
|
|
|
|
|
struct tm tm;
|
|
|
|
|
time_t t;
|
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
|
|
memset (&tm, 0, sizeof (tm));
|
|
|
|
|
|
|
|
|
|
/* parse_timestamp handles both seconds fromt he epoch and
|
|
|
|
|
ISO 8601 format. We also need to handle YYYY-MM-DD
|
|
|
|
|
format (as generated by gpg1 --with-colons --list-key).
|
|
|
|
|
Check that first and then if it fails, then try
|
|
|
|
|
parse_timestamp. */
|
|
|
|
|
|
2015-04-10 13:05:38 +02:00
|
|
|
|
if (!isodate_human_to_tm (expire_time, &tm))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
expire_time = tm2ldaptime (&tm);
|
|
|
|
|
else if ((t = parse_timestamp (expire_time, &end)) != (time_t) -1
|
|
|
|
|
&& *end == '\0')
|
|
|
|
|
{
|
2015-04-10 13:05:38 +02:00
|
|
|
|
if (!gnupg_gmtime (&t, &tm))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
expire_time = NULL;
|
|
|
|
|
else
|
|
|
|
|
expire_time = tm2ldaptime (&tm);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
expire_time = NULL;
|
|
|
|
|
|
|
|
|
|
if (! expire_time)
|
|
|
|
|
/* Failed to parse string. */
|
|
|
|
|
log_error ("Failed to parse creation time ('%s')",
|
|
|
|
|
expire_time_orig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expire_time)
|
|
|
|
|
{
|
|
|
|
|
modlist_add (modlist, "pgpKeyExpireTime", expire_time);
|
|
|
|
|
xfree (expire_time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 08:55:36 +01:00
|
|
|
|
if (is_uid && field_count >= 10)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
char *uid = fields[9];
|
2020-12-15 08:55:36 +01:00
|
|
|
|
char *mbox;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-15 08:55:36 +01:00
|
|
|
|
uncescape (uid);
|
|
|
|
|
modlist_add (modlist, "pgpUserID", uid);
|
|
|
|
|
if (schemav2 && (mbox = mailbox_from_userid (uid, 0)))
|
|
|
|
|
{
|
|
|
|
|
modlist_add (modlist, "gpgMailbox", mbox);
|
|
|
|
|
xfree (mbox);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
2020-12-15 08:55:36 +01:00
|
|
|
|
xfree (fields);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Send the key in {KEY,KEYLEN} with the metadata {INFO,INFOLEN} to
|
|
|
|
|
the keyserver identified by URI. See server.c:cmd_ks_put for the
|
|
|
|
|
format of the data and metadata. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_ldap_put (ctrl_t ctrl, parsed_uri_t uri,
|
|
|
|
|
void *data, size_t datalen,
|
|
|
|
|
void *info, size_t infolen)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
int ldap_err;
|
2020-12-14 19:28:25 +01:00
|
|
|
|
unsigned int serverinfo;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
LDAP *ldap_conn = NULL;
|
|
|
|
|
char *basedn = NULL;
|
|
|
|
|
LDAPMod **modlist = NULL;
|
|
|
|
|
LDAPMod **addlist = NULL;
|
|
|
|
|
char *data_armored = NULL;
|
2020-12-15 08:55:36 +01:00
|
|
|
|
int extract_state;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* The last byte of the info block. */
|
|
|
|
|
const char *infoend = (const char *) info + infolen - 1;
|
|
|
|
|
|
|
|
|
|
/* Enable this code to dump the modlist to /tmp/modlist.txt. */
|
2020-12-17 10:20:28 +01:00
|
|
|
|
#if 0
|
2015-03-19 11:02:46 +01:00
|
|
|
|
# warning Disable debug code before checking in.
|
|
|
|
|
const int dump_modlist = 1;
|
|
|
|
|
#else
|
|
|
|
|
const int dump_modlist = 0;
|
|
|
|
|
#endif
|
|
|
|
|
estream_t dump = NULL;
|
|
|
|
|
|
|
|
|
|
/* Elide a warning. */
|
|
|
|
|
(void) ctrl;
|
|
|
|
|
|
2017-02-01 17:54:14 +01:00
|
|
|
|
if (dirmngr_use_tor ())
|
2015-09-18 16:17:11 +02:00
|
|
|
|
{
|
2022-04-11 17:57:14 +02:00
|
|
|
|
return no_ldap_due_to_tor (ctrl);
|
2015-09-18 16:17:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = my_ldap_connect (uri, 0, &ldap_conn, &basedn, NULL, NULL, &serverinfo);
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (err || !basedn)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2021-05-26 14:48:27 +02:00
|
|
|
|
if (!err)
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = GPG_ERR_GENERAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (!(serverinfo & SERVERINFO_REALLDAP))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
/* We appear to have a PGP.com Keyserver, which can unpack the
|
|
|
|
|
* key on its own (not just a dump LDAP server). This will
|
|
|
|
|
* rarely be the case these days. */
|
|
|
|
|
LDAPMod mod;
|
|
|
|
|
LDAPMod *attrs[2];
|
|
|
|
|
char *key[2];
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char *dn;
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
key[0] = data;
|
|
|
|
|
key[1] = NULL;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
memset (&mod, 0, sizeof (mod));
|
|
|
|
|
mod.mod_op = LDAP_MOD_ADD;
|
2020-12-14 19:28:25 +01:00
|
|
|
|
mod.mod_type = (serverinfo & SERVERINFO_PGPKEYV2)? "pgpKeyV2":"pgpKey";
|
2015-03-19 11:02:46 +01:00
|
|
|
|
mod.mod_values = key;
|
|
|
|
|
attrs[0] = &mod;
|
|
|
|
|
attrs[1] = NULL;
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
dn = xtryasprintf ("pgpCertid=virtual,%s", basedn);
|
|
|
|
|
if (!dn)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
ldap_err = ldap_add_s (ldap_conn, dn, attrs);
|
|
|
|
|
xfree (dn);
|
|
|
|
|
|
|
|
|
|
if (ldap_err != LDAP_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
err = ldap_err_to_gpg_err (err);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
modlist = xtrymalloc (sizeof (LDAPMod *));
|
|
|
|
|
if (!modlist)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
*modlist = NULL;
|
|
|
|
|
|
|
|
|
|
if (dump_modlist)
|
|
|
|
|
{
|
|
|
|
|
dump = es_fopen("/tmp/modlist.txt", "w");
|
|
|
|
|
if (! dump)
|
2021-06-08 08:46:59 +02:00
|
|
|
|
log_error ("failed to open /tmp/modlist.txt: %s\n",
|
|
|
|
|
gpg_strerror (gpg_error_from_syserror ()));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (dump)
|
|
|
|
|
{
|
|
|
|
|
es_fprintf(dump, "data (%zd bytes)\n", datalen);
|
|
|
|
|
es_fprintf(dump, "info (%zd bytes): '\n", infolen);
|
|
|
|
|
es_fwrite(info, infolen, 1, dump);
|
|
|
|
|
es_fprintf(dump, "'\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Start by nulling out all attributes. We try and do a modify
|
|
|
|
|
operation first, so this ensures that we don't leave old
|
|
|
|
|
attributes lying around. */
|
|
|
|
|
modlist_add (&modlist, "pgpDisabled", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpKeyID", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpKeyType", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpUserID", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpKeyCreateTime", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpRevoked", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpSubKeyID", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpKeySize", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpKeyExpireTime", NULL);
|
|
|
|
|
modlist_add (&modlist, "pgpCertID", NULL);
|
2020-12-15 08:55:36 +01:00
|
|
|
|
if ((serverinfo & SERVERINFO_SCHEMAV2))
|
|
|
|
|
{
|
|
|
|
|
modlist_add (&modlist, "gpgFingerprint", NULL);
|
|
|
|
|
modlist_add (&modlist, "gpgSubFingerprint", NULL);
|
|
|
|
|
modlist_add (&modlist, "gpgMailbox", NULL);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* Assemble the INFO stuff into LDAP attributes */
|
2020-12-15 08:55:36 +01:00
|
|
|
|
extract_state = 0;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
while (infolen > 0)
|
|
|
|
|
{
|
|
|
|
|
char *temp = NULL;
|
|
|
|
|
|
|
|
|
|
char *newline = memchr (info, '\n', infolen);
|
|
|
|
|
if (! newline)
|
|
|
|
|
/* The last line is not \n terminated! Make a copy so we can
|
|
|
|
|
add a NUL terminator. */
|
|
|
|
|
{
|
2015-03-31 14:48:31 +02:00
|
|
|
|
temp = xmalloc (infolen + 1);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
memcpy (temp, info, infolen);
|
|
|
|
|
info = temp;
|
|
|
|
|
newline = (char *) info + infolen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*newline = '\0';
|
|
|
|
|
|
2020-12-15 08:55:36 +01:00
|
|
|
|
extract_attributes (&addlist, &extract_state, info,
|
|
|
|
|
(serverinfo & SERVERINFO_SCHEMAV2));
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
infolen = infolen - ((uintptr_t) newline - (uintptr_t) info + 1);
|
|
|
|
|
info = newline + 1;
|
|
|
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
|
if (! temp)
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_assert ((char *) info + infolen - 1 == infoend);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
else
|
2015-03-31 14:48:31 +02:00
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_assert (infolen == -1);
|
2015-03-31 14:48:31 +02:00
|
|
|
|
xfree (temp);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modlist_add (&addlist, "objectClass", "pgpKeyInfo");
|
|
|
|
|
|
|
|
|
|
err = armor_data (&data_armored, data, datalen);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2020-12-14 19:28:25 +01:00
|
|
|
|
modlist_add (&addlist,
|
|
|
|
|
(serverinfo & SERVERINFO_PGPKEYV2)? "pgpKeyV2":"pgpKey",
|
|
|
|
|
data_armored);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
/* Now append addlist onto modlist. */
|
|
|
|
|
modlists_join (&modlist, addlist);
|
|
|
|
|
|
|
|
|
|
if (dump)
|
|
|
|
|
{
|
|
|
|
|
estream_t input = modlist_dump (modlist, NULL);
|
2015-03-25 19:39:27 +01:00
|
|
|
|
if (input)
|
|
|
|
|
{
|
|
|
|
|
copy_stream (input, dump);
|
|
|
|
|
es_fclose (input);
|
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Going on the assumption that modify operations are more frequent
|
|
|
|
|
than adds, we try a modify first. If it's not there, we just
|
|
|
|
|
turn around and send an add command for the same key. Otherwise,
|
|
|
|
|
the modify brings the server copy into compliance with our copy.
|
|
|
|
|
Note that unlike the LDAP keyserver (and really, any other
|
|
|
|
|
keyserver) this does NOT merge signatures, but replaces the whole
|
|
|
|
|
key. This should make some people very happy. */
|
|
|
|
|
{
|
2020-12-15 08:55:36 +01:00
|
|
|
|
char **attrval;
|
2015-03-19 11:02:46 +01:00
|
|
|
|
char *dn;
|
|
|
|
|
|
2020-12-15 08:55:36 +01:00
|
|
|
|
if ((serverinfo & SERVERINFO_NTDS))
|
2015-03-19 11:02:46 +01:00
|
|
|
|
{
|
2020-12-15 08:55:36 +01:00
|
|
|
|
/* The modern way using a CN RDN with the fingerprint. This
|
|
|
|
|
* has the advantage that we won't have duplicate 64 bit
|
|
|
|
|
* keyids in the store. In particular NTDS requires the
|
|
|
|
|
* DN to be unique. */
|
|
|
|
|
attrval = modlist_lookup (addlist, "gpgFingerprint");
|
|
|
|
|
/* We should have exactly one value. */
|
|
|
|
|
if (!attrval || !(attrval[0] && !attrval[1]))
|
|
|
|
|
{
|
|
|
|
|
log_error ("ks-ldap: bad gpgFingerprint provided\n");
|
|
|
|
|
err = GPG_ERR_GENERAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
dn = xtryasprintf ("CN=%s,%s", attrval[0], basedn);
|
|
|
|
|
}
|
|
|
|
|
else /* The old style way. */
|
|
|
|
|
{
|
|
|
|
|
attrval = modlist_lookup (addlist, "pgpCertID");
|
|
|
|
|
/* We should have exactly one value. */
|
|
|
|
|
if (!attrval || !(attrval[0] && !attrval[1]))
|
|
|
|
|
{
|
|
|
|
|
log_error ("ks-ldap: bad pgpCertID provided\n");
|
|
|
|
|
err = GPG_ERR_GENERAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
dn = xtryasprintf ("pgpCertID=%s,%s", attrval[0], basedn);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
}
|
2020-12-14 19:28:25 +01:00
|
|
|
|
if (!dn)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (opt.debug)
|
|
|
|
|
log_debug ("ks-ldap: using DN: %s\n", dn);
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_unprotect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
err = ldap_modify_s (ldap_conn, dn, modlist);
|
|
|
|
|
if (err == LDAP_NO_SUCH_OBJECT)
|
|
|
|
|
err = ldap_add_s (ldap_conn, dn, addlist);
|
2020-12-18 11:56:15 +01:00
|
|
|
|
npth_protect ();
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
|
|
xfree (dn);
|
|
|
|
|
|
|
|
|
|
if (err != LDAP_SUCCESS)
|
|
|
|
|
{
|
2020-12-14 19:28:25 +01:00
|
|
|
|
log_error ("ks-ldap: error adding key to keyserver: %s\n",
|
2015-03-19 11:02:46 +01:00
|
|
|
|
ldap_err2string (err));
|
|
|
|
|
err = ldap_err_to_gpg_err (err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (dump)
|
|
|
|
|
es_fclose (dump);
|
|
|
|
|
|
|
|
|
|
if (ldap_conn)
|
|
|
|
|
ldap_unbind (ldap_conn);
|
|
|
|
|
|
|
|
|
|
xfree (basedn);
|
|
|
|
|
|
|
|
|
|
modlist_free (modlist);
|
|
|
|
|
xfree (addlist);
|
|
|
|
|
|
|
|
|
|
xfree (data_armored);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2023-03-20 19:24:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the data described by FILTER_ARG from URI. On success R_FP has
|
|
|
|
|
* an open stream to read the data. KS_GET_FLAGS conveys flags from
|
|
|
|
|
* the client. ATTRS is a NULL terminated list of attributes to
|
|
|
|
|
* return or NULL for all. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_ldap_query (ctrl_t ctrl, parsed_uri_t uri, unsigned int ks_get_flags,
|
2023-04-04 08:49:55 +02:00
|
|
|
|
const char *filter_arg, char **attrs,
|
|
|
|
|
gnupg_isotime_t newer, estream_t *r_fp)
|
2023-03-20 19:24:49 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
unsigned int serverinfo;
|
|
|
|
|
char *host = NULL;
|
|
|
|
|
int use_tls;
|
|
|
|
|
LDAP *ldap_conn = NULL;
|
|
|
|
|
char *basedn = NULL;
|
|
|
|
|
estream_t fp = NULL;
|
2023-06-07 10:03:48 +02:00
|
|
|
|
char *filter_arg_buffer = NULL;
|
2023-03-20 19:24:49 +01:00
|
|
|
|
char *filter = NULL;
|
|
|
|
|
int scope = LDAP_SCOPE_SUBTREE;
|
|
|
|
|
LDAPMessage *message = NULL;
|
|
|
|
|
LDAPMessage *msg;
|
|
|
|
|
int anydata = 0;
|
|
|
|
|
int first_mode = 0;
|
|
|
|
|
int next_mode = 0;
|
|
|
|
|
int get_first;
|
|
|
|
|
|
|
|
|
|
if (dirmngr_use_tor ())
|
|
|
|
|
return no_ldap_due_to_tor (ctrl);
|
|
|
|
|
|
|
|
|
|
if ((!filter_arg || !*filter_arg) && (ks_get_flags & KS_GET_FLAG_ROOTDSE))
|
|
|
|
|
filter_arg = "^&base&(objectclass=*)";
|
|
|
|
|
|
2023-06-07 10:03:48 +02:00
|
|
|
|
if ((ks_get_flags & KS_GET_FLAG_SUBST)
|
|
|
|
|
&& filter_arg && strchr (filter_arg, '$'))
|
|
|
|
|
{
|
|
|
|
|
filter_arg_buffer = substitute_vars (filter_arg, getval_for_filter, ctrl);
|
|
|
|
|
if (!filter_arg_buffer)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("substituting filter variables failed: %s\n",
|
|
|
|
|
gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
filter_arg = filter_arg_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 19:24:49 +01:00
|
|
|
|
err = ks_ldap_prepare_my_state (ctrl, ks_get_flags, &first_mode, &next_mode);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
if (!next_mode) /* (In --next mode the filter is ignored.) */
|
|
|
|
|
{
|
|
|
|
|
if (!filter_arg || !*filter_arg)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_LDAP_FILTER);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
err = ldap_parse_extfilter (filter_arg, 0, &basedn, &scope, &filter);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2023-04-04 08:49:55 +02:00
|
|
|
|
if (newer && *newer)
|
|
|
|
|
{
|
|
|
|
|
char *tstr, *fstr;
|
|
|
|
|
|
|
|
|
|
tstr = isotime2rfc4517 (newer);
|
|
|
|
|
if (!tstr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (filter && *filter)
|
|
|
|
|
fstr = strconcat ("(&", filter,
|
|
|
|
|
"(modifyTimestamp>=", tstr, "))", NULL);
|
|
|
|
|
else
|
|
|
|
|
fstr = strconcat ("(modifyTimestamp>=", tstr, ")", NULL);
|
|
|
|
|
xfree (tstr);
|
|
|
|
|
if (!fstr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
xfree (filter);
|
|
|
|
|
filter = fstr;
|
|
|
|
|
}
|
2023-03-20 19:24:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (next_mode)
|
|
|
|
|
{
|
|
|
|
|
next_again:
|
|
|
|
|
if (!ctrl->ks_get_state->msg_iter && ctrl->ks_get_state->more_pages)
|
|
|
|
|
{
|
|
|
|
|
/* Get the next page of results. */
|
|
|
|
|
if (ctrl->ks_get_state->message)
|
|
|
|
|
{
|
|
|
|
|
ldap_msgfree (ctrl->ks_get_state->message);
|
|
|
|
|
ctrl->ks_get_state->message = NULL;
|
|
|
|
|
}
|
|
|
|
|
err = search_and_parse (ctrl, ctrl->ks_get_state->keyspec,
|
|
|
|
|
ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->basedn,
|
|
|
|
|
ctrl->ks_get_state->scope,
|
|
|
|
|
ctrl->ks_get_state->filter,
|
|
|
|
|
attrs,
|
|
|
|
|
&ctrl->ks_get_state->message);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
ctrl->ks_get_state->msg_iter = ctrl->ks_get_state->message;
|
|
|
|
|
get_first = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
get_first = 0;
|
|
|
|
|
|
|
|
|
|
while (ctrl->ks_get_state->msg_iter)
|
|
|
|
|
{
|
|
|
|
|
npth_unprotect ();
|
|
|
|
|
ctrl->ks_get_state->msg_iter
|
|
|
|
|
= get_first? ldap_first_entry (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter)
|
|
|
|
|
/* */ : ldap_next_entry (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter);
|
|
|
|
|
npth_protect ();
|
|
|
|
|
get_first = 0;
|
|
|
|
|
if (ctrl->ks_get_state->msg_iter)
|
|
|
|
|
{
|
|
|
|
|
err = return_all_attributes (ctrl->ks_get_state->ldap_conn,
|
|
|
|
|
ctrl->ks_get_state->msg_iter,
|
|
|
|
|
&fp);
|
|
|
|
|
if (!err)
|
|
|
|
|
break; /* Found. */
|
|
|
|
|
else if (gpg_err_code (err) == GPG_ERR_NO_DATA)
|
|
|
|
|
err = 0; /* Skip empty attributes. */
|
|
|
|
|
else
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ctrl->ks_get_state->msg_iter || !fp)
|
|
|
|
|
{
|
|
|
|
|
ctrl->ks_get_state->msg_iter = NULL;
|
|
|
|
|
if (ctrl->ks_get_state->more_pages)
|
|
|
|
|
goto next_again;
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else /* Not in --next mode. */
|
|
|
|
|
{
|
|
|
|
|
/* Connect to the LDAP server in generic mode. */
|
|
|
|
|
char *tmpbasedn;
|
|
|
|
|
|
|
|
|
|
err = my_ldap_connect (uri, 1 /*generic*/, &ldap_conn,
|
|
|
|
|
&tmpbasedn, &host, &use_tls, &serverinfo);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
if (basedn)
|
|
|
|
|
xfree (tmpbasedn); /* Extended syntax overrides. */
|
|
|
|
|
else if (tmpbasedn)
|
|
|
|
|
basedn = tmpbasedn;
|
|
|
|
|
else if (!(ks_get_flags & KS_GET_FLAG_ROOTDSE))
|
|
|
|
|
{
|
|
|
|
|
/* No BaseDN known - get one. */
|
|
|
|
|
basedn = basedn_from_rootdse (ctrl, uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("ks-ldap: using basedn: %s\n", basedn);
|
|
|
|
|
log_debug ("ks-ldap: using filter: %s\n", filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = search_and_parse (ctrl, filter, ldap_conn, basedn, scope, filter,
|
|
|
|
|
attrs, &message);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (npth_unprotect (),
|
|
|
|
|
msg = ldap_first_entry (ldap_conn, message),
|
|
|
|
|
npth_protect ();
|
|
|
|
|
msg;
|
|
|
|
|
npth_unprotect (),
|
|
|
|
|
msg = ldap_next_entry (ldap_conn, msg),
|
|
|
|
|
npth_protect ())
|
|
|
|
|
{
|
|
|
|
|
err = return_all_attributes (ldap_conn, msg, &fp);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
anydata = 1;
|
|
|
|
|
if (first_mode)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (gpg_err_code (err) == GPG_ERR_NO_DATA)
|
|
|
|
|
err = 0; /* Skip empty/duplicate attributes. */
|
|
|
|
|
else
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ctrl->ks_get_state) /* Save the iterator. */
|
|
|
|
|
ctrl->ks_get_state->msg_iter = msg;
|
|
|
|
|
|
|
|
|
|
if (!fp) /* Nothing was found. */
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
|
|
|
|
|
if (!err && anydata)
|
|
|
|
|
err = dirmngr_status_printf (ctrl, "SOURCE", "%s://%s",
|
|
|
|
|
use_tls? "ldaps" : "ldap",
|
|
|
|
|
host? host:"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
/* Store our state if needed. */
|
|
|
|
|
if (!err && (ks_get_flags & KS_GET_FLAG_FIRST))
|
|
|
|
|
{
|
|
|
|
|
log_assert (!ctrl->ks_get_state->ldap_conn);
|
|
|
|
|
ctrl->ks_get_state->ldap_conn = ldap_conn;
|
|
|
|
|
ldap_conn = NULL;
|
|
|
|
|
log_assert (!ctrl->ks_get_state->message);
|
|
|
|
|
ctrl->ks_get_state->message = message;
|
|
|
|
|
message = NULL;
|
|
|
|
|
ctrl->ks_get_state->serverinfo = serverinfo;
|
|
|
|
|
ctrl->ks_get_state->scope = scope;
|
|
|
|
|
ctrl->ks_get_state->basedn = basedn;
|
|
|
|
|
basedn = NULL;
|
|
|
|
|
ctrl->ks_get_state->keyspec = filter? xtrystrdup (filter) : NULL;
|
|
|
|
|
ctrl->ks_get_state->filter = filter;
|
|
|
|
|
filter = NULL;
|
|
|
|
|
}
|
|
|
|
|
if ((ks_get_flags & KS_GET_FLAG_NEXT))
|
|
|
|
|
{
|
|
|
|
|
/* Keep the state in --next mode even with errors. */
|
|
|
|
|
ldap_conn = NULL;
|
|
|
|
|
message = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message)
|
|
|
|
|
ldap_msgfree (message);
|
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
es_fclose (fp);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (fp)
|
|
|
|
|
es_fseek (fp, 0, SEEK_SET);
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xfree (basedn);
|
|
|
|
|
xfree (host);
|
|
|
|
|
|
|
|
|
|
if (ldap_conn)
|
|
|
|
|
ldap_unbind (ldap_conn);
|
|
|
|
|
|
|
|
|
|
xfree (filter);
|
2023-06-07 10:03:48 +02:00
|
|
|
|
xfree (filter_arg_buffer);
|
2023-03-20 19:24:49 +01:00
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|