2 * Copyright (c) 2003-2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * This code is derived from software contributed by Aaron Spangler.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
23 #include <sys/param.h>
33 #endif /* STDC_HEADERS */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
43 #if TIME_WITH_SYS_TIME
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
56 #if defined(HAVE_LDAP_SSL_H)
57 # include <ldap_ssl.h>
58 #elif defined(HAVE_MPS_LDAP_SSL_H)
59 # include <mps/ldap_ssl.h>
61 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
62 # ifdef HAVE_SASL_SASL_H
63 # include <sasl/sasl.h>
67 # if HAVE_GSS_KRB5_CCACHE_NAME
68 # if defined(HAVE_GSSAPI_GSSAPI_KRB5_H)
69 # include <gssapi/gssapi.h>
70 # include <gssapi/gssapi_krb5.h>
71 # elif defined(HAVE_GSSAPI_GSSAPI_H)
72 # include <gssapi/gssapi.h>
83 /* Older Netscape LDAP SDKs don't prototype ldapssl_set_strength() */
84 #if defined(HAVE_LDAPSSL_SET_STRENGTH) && !defined(HAVE_LDAP_SSL_H) && !defined(HAVE_MPS_LDAP_SSL_H)
85 extern int ldapssl_set_strength(LDAP *ldap, int strength);
88 #if !defined(LDAP_OPT_NETWORK_TIMEOUT) && defined(LDAP_OPT_CONNECT_TIMEOUT)
89 # define LDAP_OPT_NETWORK_TIMEOUT LDAP_OPT_CONNECT_TIMEOUT
92 #ifndef LDAP_OPT_SUCCESS
93 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
97 # define LDAPS_PORT 636
100 #if defined(HAVE_LDAP_SASL_INTERACTIVE_BIND_S) && !defined(LDAP_SASL_QUIET)
101 # define LDAP_SASL_QUIET 0
104 #ifndef HAVE_LDAP_UNBIND_EXT_S
105 #define ldap_unbind_ext_s(a, b, c) ldap_unbind_s(a)
108 #ifndef HAVE_LDAP_SEARCH_EXT_S
109 # ifdef HAVE_LDAP_SEARCH_ST
110 # define ldap_search_ext_s(a, b, c, d, e, f, g, h, i, j, k) \
111 ldap_search_st(a, b, c, d, e, f, i, k)
113 # define ldap_search_ext_s(a, b, c, d, e, f, g, h, i, j, k) \
114 ldap_search_s(a, b, c, d, e, f, k)
118 #define LDAP_FOREACH(var, ld, res) \
119 for ((var) = ldap_first_entry((ld), (res)); \
121 (var) = ldap_next_entry((ld), (var)))
123 #define DPRINTF(args, level) if (ldap_conf.debug >= level) warningx args
128 #define CONF_LIST_STR 4
129 #define CONF_DEREF_VAL 5
131 #define SUDO_LDAP_CLEAR 0
132 #define SUDO_LDAP_SSL 1
133 #define SUDO_LDAP_STARTTLS 2
135 /* The TIMEFILTER_LENGTH is the length of the filter when timed entries
136 are used. The length is computed as follows:
137 81 for the filter itself
138 + 2 * 17 for the now timestamp
140 #define TIMEFILTER_LENGTH 115
143 * The ldap_search structure implements a linked list of ldap and
144 * search result pointers, which allows us to remove them after
145 * all search results have been combined in memory.
146 * XXX - should probably be a tailq since we do appends
148 struct ldap_search_list {
150 LDAPMessage *searchresult;
151 struct ldap_search_list *next;
155 * The ldap_entry_wrapper structure is used to implement sorted result entries.
156 * A double is used for the order to allow for insertion of new entries
157 * without having to renumber everything.
158 * Note: there is no standard floating point type in LDAP.
159 * As a result, some LDAP servers will only allow an integer.
161 struct ldap_entry_wrapper {
167 * The ldap_result structure contains the list of matching searches as
168 * well as an array of all result entries sorted by the sudoOrder attribute.
171 struct ldap_search_list *searches;
172 struct ldap_entry_wrapper *entries;
173 int allocated_entries;
178 #define ALLOCATION_INCREMENT 100
180 struct ldap_config_table {
181 const char *conf_str; /* config file string */
182 int type; /* CONF_BOOL, CONF_INT, CONF_STR */
183 int opt_val; /* LDAP_OPT_* (or -1 for sudo internal) */
184 void *valp; /* pointer into ldap_conf */
187 struct ldap_config_list_str {
188 struct ldap_config_list_str *next;
192 /* LDAP configuration structure */
193 static struct ldap_config {
208 struct ldap_config_list_str *uri;
212 struct ldap_config_list_str *base;
215 char *tls_cacertfile;
217 char *tls_random_file;
218 char *tls_cipher_suite;
223 char *rootsasl_auth_id;
228 static struct ldap_config_table ldap_conf_global[] = {
229 { "sudoers_debug", CONF_INT, -1, &ldap_conf.debug },
230 { "host", CONF_STR, -1, &ldap_conf.host },
231 { "port", CONF_INT, -1, &ldap_conf.port },
232 { "ssl", CONF_STR, -1, &ldap_conf.ssl },
233 { "sslpath", CONF_STR, -1, &ldap_conf.tls_certfile },
234 { "uri", CONF_LIST_STR, -1, &ldap_conf.uri },
235 #ifdef LDAP_OPT_DEBUG_LEVEL
236 { "debug", CONF_INT, LDAP_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug },
238 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
239 { "tls_checkpeer", CONF_BOOL, LDAP_OPT_X_TLS_REQUIRE_CERT,
240 &ldap_conf.tls_checkpeer },
242 { "tls_checkpeer", CONF_BOOL, -1, &ldap_conf.tls_checkpeer },
244 #ifdef LDAP_OPT_X_TLS_CACERTFILE
245 { "tls_cacertfile", CONF_STR, LDAP_OPT_X_TLS_CACERTFILE,
246 &ldap_conf.tls_cacertfile },
247 { "tls_cacert", CONF_STR, LDAP_OPT_X_TLS_CACERTFILE,
248 &ldap_conf.tls_cacertfile },
250 #ifdef LDAP_OPT_X_TLS_CACERTDIR
251 { "tls_cacertdir", CONF_STR, LDAP_OPT_X_TLS_CACERTDIR,
252 &ldap_conf.tls_cacertdir },
254 #ifdef LDAP_OPT_X_TLS_RANDOM_FILE
255 { "tls_randfile", CONF_STR, LDAP_OPT_X_TLS_RANDOM_FILE,
256 &ldap_conf.tls_random_file },
258 #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
259 { "tls_ciphers", CONF_STR, LDAP_OPT_X_TLS_CIPHER_SUITE,
260 &ldap_conf.tls_cipher_suite },
261 #elif defined(LDAP_OPT_SSL_CIPHER)
262 { "tls_ciphers", CONF_STR, LDAP_OPT_SSL_CIPHER,
263 &ldap_conf.tls_cipher_suite },
265 #ifdef LDAP_OPT_X_TLS_CERTFILE
266 { "tls_cert", CONF_STR, LDAP_OPT_X_TLS_CERTFILE,
267 &ldap_conf.tls_certfile },
269 { "tls_cert", CONF_STR, -1, &ldap_conf.tls_certfile },
271 #ifdef LDAP_OPT_X_TLS_KEYFILE
272 { "tls_key", CONF_STR, LDAP_OPT_X_TLS_KEYFILE,
273 &ldap_conf.tls_keyfile },
275 { "tls_key", CONF_STR, -1, &ldap_conf.tls_keyfile },
277 #ifdef HAVE_LDAP_SSL_CLIENT_INIT
278 { "tls_keypw", CONF_STR, -1, &ldap_conf.tls_keypw },
280 { "binddn", CONF_STR, -1, &ldap_conf.binddn },
281 { "bindpw", CONF_STR, -1, &ldap_conf.bindpw },
282 { "rootbinddn", CONF_STR, -1, &ldap_conf.rootbinddn },
283 { "sudoers_base", CONF_LIST_STR, -1, &ldap_conf.base },
284 { "sudoers_timed", CONF_BOOL, -1, &ldap_conf.timed },
285 { "sudoers_search_filter", CONF_STR, -1, &ldap_conf.search_filter },
286 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
287 { "use_sasl", CONF_BOOL, -1, &ldap_conf.use_sasl },
288 { "sasl_auth_id", CONF_STR, -1, &ldap_conf.sasl_auth_id },
289 { "rootuse_sasl", CONF_BOOL, -1, &ldap_conf.rootuse_sasl },
290 { "rootsasl_auth_id", CONF_STR, -1, &ldap_conf.rootsasl_auth_id },
291 { "krb5_ccname", CONF_STR, -1, &ldap_conf.krb5_ccname },
292 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
296 static struct ldap_config_table ldap_conf_conn[] = {
297 #ifdef LDAP_OPT_PROTOCOL_VERSION
298 { "ldap_version", CONF_INT, LDAP_OPT_PROTOCOL_VERSION,
299 &ldap_conf.version },
301 #ifdef LDAP_OPT_NETWORK_TIMEOUT
302 { "bind_timelimit", CONF_INT, -1 /* needs timeval, set manually */,
303 &ldap_conf.bind_timelimit },
304 { "network_timeout", CONF_INT, -1 /* needs timeval, set manually */,
305 &ldap_conf.bind_timelimit },
306 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
307 { "bind_timelimit", CONF_INT, LDAP_X_OPT_CONNECT_TIMEOUT,
308 &ldap_conf.bind_timelimit },
309 { "network_timeout", CONF_INT, LDAP_X_OPT_CONNECT_TIMEOUT,
310 &ldap_conf.bind_timelimit },
312 { "timelimit", CONF_INT, LDAP_OPT_TIMELIMIT, &ldap_conf.timelimit },
313 #ifdef LDAP_OPT_TIMEOUT
314 { "timeout", CONF_INT, -1 /* needs timeval, set manually */,
315 &ldap_conf.timeout },
317 #ifdef LDAP_OPT_DEREF
318 { "deref", CONF_DEREF_VAL, LDAP_OPT_DEREF, &ldap_conf.deref },
320 #ifdef LDAP_OPT_X_SASL_SECPROPS
321 { "sasl_secprops", CONF_STR, LDAP_OPT_X_SASL_SECPROPS,
322 &ldap_conf.sasl_secprops },
327 /* sudo_nss implementation */
328 static int sudo_ldap_open(struct sudo_nss *nss);
329 static int sudo_ldap_close(struct sudo_nss *nss);
330 static int sudo_ldap_parse(struct sudo_nss *nss);
331 static int sudo_ldap_setdefs(struct sudo_nss *nss);
332 static int sudo_ldap_lookup(struct sudo_nss *nss, int ret, int pwflag);
333 static int sudo_ldap_display_cmnd(struct sudo_nss *nss, struct passwd *pw);
334 static int sudo_ldap_display_defaults(struct sudo_nss *nss, struct passwd *pw,
336 static int sudo_ldap_display_bound_defaults(struct sudo_nss *nss,
337 struct passwd *pw, struct lbuf *lbuf);
338 static int sudo_ldap_display_privs(struct sudo_nss *nss, struct passwd *pw,
340 static struct ldap_result *sudo_ldap_result_get(struct sudo_nss *nss,
344 * LDAP sudo_nss handle.
345 * We store the connection to the LDAP server, the cached ldap_result object
346 * (if any), and the name of the user the query was performed for.
347 * If a new query is launched with sudo_ldap_result_get() that specifies a
348 * different user, the old cached result is freed before the new query is run.
350 struct sudo_ldap_handle {
352 struct ldap_result *result;
354 struct group_list *grlist;
357 struct sudo_nss sudo_nss_ldap = {
365 sudo_ldap_display_cmnd,
366 sudo_ldap_display_defaults,
367 sudo_ldap_display_bound_defaults,
368 sudo_ldap_display_privs
371 #ifdef HAVE_LDAP_CREATE
373 * Rebuild the hosts list and include a specific port for each host.
374 * ldap_create() does not take a default port parameter so we must
375 * append one if we want something other than LDAP_PORT.
378 sudo_ldap_conf_add_ports(void)
381 char *host, *port, defport[13];
382 char hostbuf[LINE_MAX * 2];
383 debug_decl(sudo_ldap_conf_add_ports, SUDO_DEBUG_LDAP)
386 if (snprintf(defport, sizeof(defport), ":%d", ldap_conf.port) >= sizeof(defport))
387 errorx(1, _("sudo_ldap_conf_add_ports: port too large"));
389 for ((host = strtok(ldap_conf.host, " \t")); host; (host = strtok(NULL, " \t"))) {
390 if (hostbuf[0] != '\0') {
391 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
395 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
397 /* Append port if there is not one already. */
398 if ((port = strrchr(host, ':')) == NULL ||
399 !isdigit((unsigned char)port[1])) {
400 if (strlcat(hostbuf, defport, sizeof(hostbuf)) >= sizeof(hostbuf))
405 efree(ldap_conf.host);
406 ldap_conf.host = estrdup(hostbuf);
410 errorx(1, _("sudo_ldap_conf_add_ports: out of space expanding hostbuf"));
414 #ifndef HAVE_LDAP_INITIALIZE
416 * For each uri, convert to host:port pairs. For ldaps:// enable SSL
417 * Accepts: uris of the form ldap:/// or ldap://hostname:portnum/
418 * where the trailing slash is optional.
421 sudo_ldap_parse_uri(const struct ldap_config_list_str *uri_list)
423 char *buf, *uri, *host, *cp, *port;
424 char hostbuf[LINE_MAX];
425 int nldap = 0, nldaps = 0;
427 debug_decl(sudo_ldap_parse_uri, SUDO_DEBUG_LDAP)
430 buf = estrdup(uri_list->val);
432 for ((uri = strtok(buf, " \t")); uri != NULL; (uri = strtok(NULL, " \t"))) {
433 if (strncasecmp(uri, "ldap://", 7) == 0) {
436 } else if (strncasecmp(uri, "ldaps://", 8) == 0) {
440 warningx(_("unsupported LDAP uri type: %s"), uri);
444 /* trim optional trailing slash */
445 if ((cp = strrchr(host, '/')) != NULL && cp[1] == '\0') {
449 if (hostbuf[0] != '\0') {
450 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
455 host = "localhost"; /* no host specified, use localhost */
457 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
460 /* If using SSL and no port specified, add port 636 */
462 if ((port = strrchr(host, ':')) == NULL ||
463 !isdigit((unsigned char)port[1]))
464 if (strlcat(hostbuf, ":636", sizeof(hostbuf)) >= sizeof(hostbuf))
468 if (hostbuf[0] == '\0') {
469 warningx(_("invalid uri: %s"), uri_list->val);
475 warningx(_("unable to mix ldap and ldaps URIs"));
478 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
479 warningx(_("unable to mix ldaps and starttls"));
482 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
485 efree(ldap_conf.host);
486 ldap_conf.host = estrdup(hostbuf);
488 } while ((uri_list = uri_list->next));
495 debug_return_int(rc);
498 errorx(1, _("sudo_ldap_parse_uri: out of space building hostbuf"));
502 sudo_ldap_join_uri(struct ldap_config_list_str *uri_list)
504 struct ldap_config_list_str *uri;
507 debug_decl(sudo_ldap_join_uri, SUDO_DEBUG_LDAP)
509 /* Usually just a single entry. */
510 if (uri_list->next == NULL)
511 debug_return_str(estrdup(uri_list->val));
513 for (uri = uri_list; uri != NULL; uri = uri->next) {
514 len += strlen(uri->val) + 1;
516 buf = cp = emalloc(len);
518 for (uri = uri_list; uri != NULL; uri = uri->next) {
519 cp += strlcpy(cp, uri->val, len - (cp - buf));
523 debug_return_str(buf);
525 #endif /* HAVE_LDAP_INITIALIZE */
528 sudo_ldap_init(LDAP **ldp, const char *host, int port)
531 int rc = LDAP_CONNECT_ERROR;
532 debug_decl(sudo_ldap_init, SUDO_DEBUG_LDAP)
534 #ifdef HAVE_LDAPSSL_INIT
535 if (ldap_conf.ssl_mode != SUDO_LDAP_CLEAR) {
536 const int defsecure = ldap_conf.ssl_mode == SUDO_LDAP_SSL;
537 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
538 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
539 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
540 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
541 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
543 * Starting with version 5.0, Mozilla-derived LDAP SDKs require
544 * the cert and key paths to be a directory, not a file.
545 * If the user specified a file and it fails, try the parent dir.
547 if (rc != LDAP_SUCCESS) {
549 if (ldap_conf.tls_certfile != NULL) {
550 char *cp = strrchr(ldap_conf.tls_certfile, '/');
551 if (cp != NULL && strncmp(cp + 1, "cert", 4) == 0) {
556 if (ldap_conf.tls_keyfile != NULL) {
557 char *cp = strrchr(ldap_conf.tls_keyfile, '/');
558 if (cp != NULL && strncmp(cp + 1, "key", 3) == 0) {
564 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
565 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
566 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
567 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
568 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
571 if (rc != LDAP_SUCCESS) {
572 warningx(_("unable to initialize SSL cert and key db: %s"),
573 ldapssl_err2string(rc));
574 if (ldap_conf.tls_certfile == NULL)
575 warningx(_("you must set TLS_CERT in %s to use SSL"),
580 DPRINTF(("ldapssl_init(%s, %d, %d)", host, port, defsecure), 2);
581 if ((ld = ldapssl_init(host, port, defsecure)) != NULL)
584 #elif defined(HAVE_LDAP_SSL_INIT) && defined(HAVE_LDAP_SSL_CLIENT_INIT)
585 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
586 if (ldap_ssl_client_init(ldap_conf.tls_keyfile, ldap_conf.tls_keypw, 0, &rc) != LDAP_SUCCESS) {
587 warningx("ldap_ssl_client_init(): %s", ldap_err2string(rc));
588 debug_return_int(-1);
590 DPRINTF(("ldap_ssl_init(%s, %d, NULL)", host, port), 2);
591 if ((ld = ldap_ssl_init((char *)host, port, NULL)) != NULL)
596 #ifdef HAVE_LDAP_CREATE
597 DPRINTF(("ldap_create()"), 2);
598 if ((rc = ldap_create(&ld)) != LDAP_SUCCESS)
600 DPRINTF(("ldap_set_option(LDAP_OPT_HOST_NAME, %s)", host), 2);
601 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, host);
603 DPRINTF(("ldap_init(%s, %d)", host, port), 2);
604 if ((ld = ldap_init((char *)host, port)) != NULL)
611 debug_return_int(rc);
615 * Walk through search results and return true if we have a matching
616 * netgroup, else false.
619 sudo_ldap_check_user_netgroup(LDAP *ld, LDAPMessage *entry, char *user)
621 struct berval **bv, **p;
624 debug_decl(sudo_ldap_check_user_netgroup, SUDO_DEBUG_LDAP)
627 debug_return_bool(ret);
629 /* get the values from the entry */
630 bv = ldap_get_values_len(ld, entry, "sudoUser");
632 debug_return_bool(ret);
634 /* walk through values */
635 for (p = bv; *p != NULL && !ret; p++) {
638 if (netgr_matches(val, NULL, NULL, user))
640 DPRINTF(("ldap sudoUser netgroup '%s' ... %s", val,
641 ret ? "MATCH!" : "not"), 2 + ((ret) ? 0 : 1));
644 ldap_value_free_len(bv); /* cleanup */
646 debug_return_bool(ret);
650 * Walk through search results and return true if we have a
651 * host match, else false.
654 sudo_ldap_check_host(LDAP *ld, LDAPMessage *entry)
656 struct berval **bv, **p;
659 debug_decl(sudo_ldap_check_host, SUDO_DEBUG_LDAP)
662 debug_return_bool(ret);
664 /* get the values from the entry */
665 bv = ldap_get_values_len(ld, entry, "sudoHost");
667 debug_return_bool(ret);
669 /* walk through values */
670 for (p = bv; *p != NULL && !ret; p++) {
672 /* match any or address or netgroup or hostname */
673 if (!strcmp(val, "ALL") || addr_matches(val) ||
674 netgr_matches(val, user_host, user_shost, NULL) ||
675 hostname_matches(user_shost, user_host, val))
677 DPRINTF(("ldap sudoHost '%s' ... %s", val,
678 ret ? "MATCH!" : "not"), 2);
681 ldap_value_free_len(bv); /* cleanup */
683 debug_return_bool(ret);
687 sudo_ldap_check_runas_user(LDAP *ld, LDAPMessage *entry)
689 struct berval **bv, **p;
692 debug_decl(sudo_ldap_check_runas_user, SUDO_DEBUG_LDAP)
695 debug_return_bool(UNSPEC);
697 /* get the runas user from the entry */
698 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
700 bv = ldap_get_values_len(ld, entry, "sudoRunAs"); /* old style */
705 * if runas is not specified on the command line, the only information
706 * as to which user to run as is in the runas_default option. We should
707 * check to see if we have the local option present. Unfortunately we
708 * don't parse these options until after this routine says yes or no.
709 * The query has already returned, so we could peek at the attribute
710 * values here though.
712 * For now just require users to always use -u option unless its set
713 * in the global defaults. This behaviour is no different than the global
716 * Sigh - maybe add this feature later
720 * If there are no runas entries, match runas_default against
721 * what the user specified on the command line.
724 debug_return_bool(!strcasecmp(runas_pw->pw_name, def_runas_default));
726 /* walk through values returned, looking for a match */
727 for (p = bv; *p != NULL && !ret; p++) {
731 if (netgr_matches(val, NULL, NULL, runas_pw->pw_name))
735 if (usergr_matches(val, runas_pw->pw_name, runas_pw))
739 if (strcmp(val, "ALL") == 0) {
745 if (strcasecmp(val, runas_pw->pw_name) == 0)
749 DPRINTF(("ldap sudoRunAsUser '%s' ... %s", val,
750 ret ? "MATCH!" : "not"), 2);
753 ldap_value_free_len(bv); /* cleanup */
755 debug_return_bool(ret);
759 sudo_ldap_check_runas_group(LDAP *ld, LDAPMessage *entry)
761 struct berval **bv, **p;
764 debug_decl(sudo_ldap_check_runas_group, SUDO_DEBUG_LDAP)
766 /* runas_gr is only set if the user specified the -g flag */
768 debug_return_bool(UNSPEC);
770 /* get the values from the entry */
771 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
773 debug_return_bool(ret);
775 /* walk through values returned, looking for a match */
776 for (p = bv; *p != NULL && !ret; p++) {
778 if (strcmp(val, "ALL") == 0 || group_matches(val, runas_gr))
780 DPRINTF(("ldap sudoRunAsGroup '%s' ... %s", val,
781 ret ? "MATCH!" : "not"), 2);
784 ldap_value_free_len(bv); /* cleanup */
786 debug_return_bool(ret);
790 * Walk through search results and return true if we have a runas match,
791 * else false. RunAs info is optional.
794 sudo_ldap_check_runas(LDAP *ld, LDAPMessage *entry)
797 debug_decl(sudo_ldap_check_runas, SUDO_DEBUG_LDAP)
800 debug_return_bool(false);
802 ret = sudo_ldap_check_runas_user(ld, entry) != false &&
803 sudo_ldap_check_runas_group(ld, entry) != false;
805 debug_return_bool(ret);
809 * Walk through search results and return true if we have a command match,
810 * false if disallowed and UNSPEC if not matched.
813 sudo_ldap_check_command(LDAP *ld, LDAPMessage *entry, int *setenv_implied)
815 struct berval **bv, **p;
816 char *allowed_cmnd, *allowed_args, *val;
819 debug_decl(sudo_ldap_check_command, SUDO_DEBUG_LDAP)
822 debug_return_bool(ret);
824 bv = ldap_get_values_len(ld, entry, "sudoCommand");
826 debug_return_bool(ret);
828 for (p = bv; *p != NULL && ret != false; p++) {
830 /* Match against ALL ? */
831 if (!strcmp(val, "ALL")) {
833 if (setenv_implied != NULL)
834 *setenv_implied = true;
835 DPRINTF(("ldap sudoCommand '%s' ... MATCH!", val), 2);
839 /* check for !command */
842 allowed_cmnd = estrdup(1 + val); /* !command */
845 allowed_cmnd = estrdup(val); /* command */
848 /* split optional args away from command */
849 allowed_args = strchr(allowed_cmnd, ' ');
851 *allowed_args++ = '\0';
853 /* check the command like normal */
854 if (command_matches(allowed_cmnd, allowed_args)) {
856 * If allowed (no bang) set ret but keep on checking.
857 * If disallowed (bang), exit loop.
859 ret = foundbang ? false : true;
861 DPRINTF(("ldap sudoCommand '%s' ... %s", val,
862 ret == true ? "MATCH!" : "not"), 2);
864 efree(allowed_cmnd); /* cleanup */
867 ldap_value_free_len(bv); /* more cleanup */
869 debug_return_bool(ret);
873 * Search for boolean "option" in sudoOption.
874 * Returns true if found and allowed, false if negated, else UNSPEC.
877 sudo_ldap_check_bool(LDAP *ld, LDAPMessage *entry, char *option)
879 struct berval **bv, **p;
882 debug_decl(sudo_ldap_check_bool, SUDO_DEBUG_LDAP)
885 debug_return_bool(ret);
887 bv = ldap_get_values_len(ld, entry, "sudoOption");
889 debug_return_bool(ret);
891 /* walk through options */
892 for (p = bv; *p != NULL; p++) {
894 DPRINTF(("ldap sudoOption: '%s'", var), 2);
896 if ((ch = *var) == '!')
898 if (strcmp(var, option) == 0)
902 ldap_value_free_len(bv);
904 debug_return_bool(ret);
908 * Read sudoOption and modify the defaults as we go. This is used once
909 * from the cn=defaults entry and also once when a final sudoRole is matched.
912 sudo_ldap_parse_options(LDAP *ld, LDAPMessage *entry)
914 struct berval **bv, **p;
916 debug_decl(sudo_ldap_parse_options, SUDO_DEBUG_LDAP)
921 bv = ldap_get_values_len(ld, entry, "sudoOption");
925 /* walk through options */
926 for (p = bv; *p != NULL; p++) {
927 var = estrdup((*p)->bv_val);
928 DPRINTF(("ldap sudoOption: '%s'", var), 2);
930 /* check for equals sign past first char */
931 val = strchr(var, '=');
933 *val++ = '\0'; /* split on = and truncate var */
934 op = *(val - 2); /* peek for += or -= cases */
935 if (op == '+' || op == '-') {
936 *(val - 2) = '\0'; /* found, remove extra char */
937 /* case var+=val or var-=val */
938 set_default(var, val, (int) op);
941 set_default(var, val, true);
943 } else if (*var == '!') {
944 /* case !var Boolean False */
945 set_default(var + 1, NULL, false);
947 /* case var Boolean True */
948 set_default(var, NULL, true);
953 ldap_value_free_len(bv);
959 * Build an LDAP timefilter.
961 * Stores a filter in the buffer that makes sure only entries
962 * are selected that have a sudoNotBefore in the past and a
963 * sudoNotAfter in the future, i.e. a filter of the following
964 * structure (spaced out a little more for better readability:
968 * (!(sudoNotAfter=*))
969 * (sudoNotAfter>__now__)
972 * (!(sudoNotBefore=*))
973 * (sudoNotBefore<__now__)
977 * If either the sudoNotAfter or sudoNotBefore attributes are missing,
978 * no time restriction shall be imposed.
981 sudo_ldap_timefilter(char *buffer, size_t buffersize)
985 char timebuffer[sizeof("20120727121554.0Z")];
987 debug_decl(sudo_ldap_timefilter, SUDO_DEBUG_LDAP)
989 /* Make sure we have a formatted timestamp for __now__. */
991 if ((tp = gmtime(&now)) == NULL) {
992 warning(_("unable to get GMT time"));
996 /* Format the timestamp according to the RFC. */
997 if (strftime(timebuffer, sizeof(timebuffer), "%Y%m%d%H%M%S.0Z", tp) == 0) {
998 warningx(_("unable to format timestamp"));
1003 bytes = snprintf(buffer, buffersize, "(&(|(!(sudoNotAfter=*))(sudoNotAfter>=%s))(|(!(sudoNotBefore=*))(sudoNotBefore<=%s)))",
1004 timebuffer, timebuffer);
1005 if (bytes < 0 || bytes >= buffersize) {
1006 warning(_("unable to build time filter"));
1011 debug_return_int(bytes);
1015 * Builds up a filter to search for default settings
1018 sudo_ldap_build_default_filter(void)
1021 debug_decl(sudo_ldap_build_default_filter, SUDO_DEBUG_LDAP)
1023 if (ldap_conf.search_filter)
1024 easprintf(&filt, "(&%s(cn=defaults))", ldap_conf.search_filter);
1026 filt = estrdup("cn=defaults");
1027 debug_return_str(filt);
1031 * Determine length of query value after escaping characters
1035 sudo_ldap_value_len(const char *value)
1040 for (s = value; *s != '\0'; s++) {
1050 len += (size_t)(s - value);
1055 * Like strlcat() but escapes characters as per RFC 4515.
1058 sudo_ldap_value_cat(char *dst, const char *src, size_t size)
1061 const char *s = src;
1065 /* Find the end of dst and adjust bytes left but don't go past end */
1066 while (n-- != 0 && *d != '\0')
1072 return dlen + strlen(s);
1073 while (*s != '\0') {
1120 return dlen + (s - src); /* count does not include NUL */
1124 * Builds up a filter to check against LDAP.
1127 sudo_ldap_build_pass1(struct passwd *pw)
1130 char *buf, timebuffer[TIMEFILTER_LENGTH + 1], gidbuf[MAX_UID_T_LEN + 1];
1131 struct group_list *grlist;
1134 debug_decl(sudo_ldap_build_pass1, SUDO_DEBUG_LDAP)
1136 /* If there is a filter, allocate space for the global AND. */
1137 if (ldap_conf.timed || ldap_conf.search_filter)
1140 /* Add LDAP search filter if present. */
1141 if (ldap_conf.search_filter)
1142 sz += strlen(ldap_conf.search_filter);
1144 /* Then add (|(sudoUser=USERNAME)(sudoUser=ALL)) + NUL */
1145 sz += 29 + sudo_ldap_value_len(pw->pw_name);
1147 /* Add space for primary and supplementary groups and gids */
1148 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
1149 sz += 12 + sudo_ldap_value_len(grp->gr_name);
1151 sz += 13 + MAX_UID_T_LEN;
1152 if ((grlist = sudo_get_grlist(pw)) != NULL) {
1153 for (i = 0; i < grlist->ngroups; i++) {
1154 if (grp != NULL && strcasecmp(grlist->groups[i], grp->gr_name) == 0)
1156 sz += 12 + sudo_ldap_value_len(grlist->groups[i]);
1158 for (i = 0; i < grlist->ngids; i++) {
1159 if (pw->pw_gid == grlist->gids[i])
1161 sz += 13 + MAX_UID_T_LEN;
1165 /* If timed, add space for time limits. */
1166 if (ldap_conf.timed)
1167 sz += TIMEFILTER_LENGTH;
1172 * If timed or using a search filter, start a global AND clause to
1173 * contain the search filter, search criteria, and time restriction.
1175 if (ldap_conf.timed || ldap_conf.search_filter)
1176 (void) strlcpy(buf, "(&", sz);
1178 if (ldap_conf.search_filter)
1179 (void) strlcat(buf, ldap_conf.search_filter, sz);
1181 /* Global OR + sudoUser=user_name filter */
1182 (void) strlcat(buf, "(|(sudoUser=", sz);
1183 (void) sudo_ldap_value_cat(buf, pw->pw_name, sz);
1184 (void) strlcat(buf, ")", sz);
1186 /* Append primary group and gid */
1188 (void) strlcat(buf, "(sudoUser=%", sz);
1189 (void) sudo_ldap_value_cat(buf, grp->gr_name, sz);
1190 (void) strlcat(buf, ")", sz);
1192 (void) snprintf(gidbuf, sizeof(gidbuf), "%u", (unsigned int)pw->pw_gid);
1193 (void) strlcat(buf, "(sudoUser=%#", sz);
1194 (void) strlcat(buf, gidbuf, sz);
1195 (void) strlcat(buf, ")", sz);
1197 /* Append supplementary groups and gids */
1198 if (grlist != NULL) {
1199 for (i = 0; i < grlist->ngroups; i++) {
1200 if (grp != NULL && strcasecmp(grlist->groups[i], grp->gr_name) == 0)
1202 (void) strlcat(buf, "(sudoUser=%", sz);
1203 (void) sudo_ldap_value_cat(buf, grlist->groups[i], sz);
1204 (void) strlcat(buf, ")", sz);
1206 for (i = 0; i < grlist->ngids; i++) {
1207 if (pw->pw_gid == grlist->gids[i])
1209 (void) snprintf(gidbuf, sizeof(gidbuf), "%u",
1210 (unsigned int)grlist->gids[i]);
1211 (void) strlcat(buf, "(sudoUser=%#", sz);
1212 (void) strlcat(buf, gidbuf, sz);
1213 (void) strlcat(buf, ")", sz);
1217 /* Done with groups. */
1219 sudo_grlist_delref(grlist);
1221 sudo_gr_delref(grp);
1223 /* Add ALL to list and end the global OR */
1224 if (strlcat(buf, "(sudoUser=ALL)", sz) >= sz)
1225 errorx(1, _("sudo_ldap_build_pass1 allocation mismatch"));
1227 /* Add the time restriction, or simply end the global OR. */
1228 if (ldap_conf.timed) {
1229 strlcat(buf, ")", sz); /* closes the global OR */
1230 sudo_ldap_timefilter(timebuffer, sizeof(timebuffer));
1231 strlcat(buf, timebuffer, sz);
1232 } else if (ldap_conf.search_filter) {
1233 strlcat(buf, ")", sz); /* closes the global OR */
1235 strlcat(buf, ")", sz); /* closes the global OR or the global AND */
1237 debug_return_str(buf);
1241 * Builds up a filter to check against netgroup entries in LDAP.
1244 sudo_ldap_build_pass2(void)
1246 char *filt, timebuffer[TIMEFILTER_LENGTH + 1];
1247 debug_decl(sudo_ldap_build_pass2, SUDO_DEBUG_LDAP)
1249 if (ldap_conf.timed)
1250 sudo_ldap_timefilter(timebuffer, sizeof(timebuffer));
1253 * Match all sudoUsers beginning with a '+'.
1254 * If a search filter or time restriction is specified,
1255 * those get ANDed in to the expression.
1257 easprintf(&filt, "%s%s(sudoUser=+*)%s%s",
1258 (ldap_conf.timed || ldap_conf.search_filter) ? "(&" : "",
1259 ldap_conf.search_filter ? ldap_conf.search_filter : "",
1260 ldap_conf.timed ? timebuffer : "",
1261 (ldap_conf.timed || ldap_conf.search_filter) ? ")" : "");
1263 debug_return_str(filt);
1267 sudo_ldap_read_secret(const char *path)
1270 char buf[LINE_MAX], *cp;
1271 debug_decl(sudo_ldap_read_secret, SUDO_DEBUG_LDAP)
1273 if ((fp = fopen(_PATH_LDAP_SECRET, "r")) != NULL) {
1274 if (fgets(buf, sizeof(buf), fp) != NULL) {
1275 if ((cp = strchr(buf, '\n')) != NULL)
1277 /* copy to bindpw and binddn */
1278 efree(ldap_conf.bindpw);
1279 ldap_conf.bindpw = estrdup(buf);
1280 efree(ldap_conf.binddn);
1281 ldap_conf.binddn = ldap_conf.rootbinddn;
1282 ldap_conf.rootbinddn = NULL;
1290 * Look up keyword in config tables.
1291 * Returns true if found, else false.
1294 sudo_ldap_parse_keyword(const char *keyword, const char *value,
1295 struct ldap_config_table *table)
1297 struct ldap_config_table *cur;
1298 debug_decl(sudo_ldap_parse_keyword, SUDO_DEBUG_LDAP)
1300 /* Look up keyword in config tables */
1301 for (cur = table; cur->conf_str != NULL; cur++) {
1302 if (strcasecmp(keyword, cur->conf_str) == 0) {
1303 switch (cur->type) {
1304 case CONF_DEREF_VAL:
1305 if (strcasecmp(value, "searching") == 0)
1306 *(int *)(cur->valp) = LDAP_DEREF_SEARCHING;
1307 else if (strcasecmp(value, "finding") == 0)
1308 *(int *)(cur->valp) = LDAP_DEREF_FINDING;
1309 else if (strcasecmp(value, "always") == 0)
1310 *(int *)(cur->valp) = LDAP_DEREF_ALWAYS;
1312 *(int *)(cur->valp) = LDAP_DEREF_NEVER;
1315 *(int *)(cur->valp) = atobool(value) == true;
1318 *(int *)(cur->valp) = atoi(value);
1321 efree(*(char **)(cur->valp));
1322 *(char **)(cur->valp) = estrdup(value);
1326 struct ldap_config_list_str **p;
1327 size_t len = strlen(value);
1330 p = (struct ldap_config_list_str **)cur->valp;
1333 *p = emalloc(sizeof(struct ldap_config_list_str) + len);
1334 memcpy((*p)->val, value, len + 1);
1340 debug_return_bool(true);
1343 debug_return_bool(false);
1347 sudo_ldap_read_config(void)
1350 char *cp, *keyword, *value;
1351 debug_decl(sudo_ldap_read_config, SUDO_DEBUG_LDAP)
1354 ldap_conf.version = 3;
1355 ldap_conf.port = -1;
1356 ldap_conf.tls_checkpeer = -1;
1357 ldap_conf.timelimit = -1;
1358 ldap_conf.timeout = -1;
1359 ldap_conf.bind_timelimit = -1;
1360 ldap_conf.use_sasl = -1;
1361 ldap_conf.rootuse_sasl = -1;
1362 ldap_conf.deref = -1;
1364 if ((fp = fopen(_PATH_LDAP_CONF, "r")) == NULL)
1365 debug_return_bool(false);
1367 while ((cp = sudo_parseln(fp)) != NULL) {
1369 continue; /* skip empty line */
1371 /* split into keyword and value */
1373 while (*cp && !isblank((unsigned char) *cp))
1376 *cp++ = '\0'; /* terminate keyword */
1378 /* skip whitespace before value */
1379 while (isblank((unsigned char) *cp))
1383 /* Look up keyword in config tables */
1384 if (!sudo_ldap_parse_keyword(keyword, value, ldap_conf_global))
1385 sudo_ldap_parse_keyword(keyword, value, ldap_conf_conn);
1389 if (!ldap_conf.host)
1390 ldap_conf.host = estrdup("localhost");
1392 if (ldap_conf.debug > 1) {
1393 sudo_printf(SUDO_CONV_ERROR_MSG, "LDAP Config Summary\n");
1394 sudo_printf(SUDO_CONV_ERROR_MSG, "===================\n");
1395 if (ldap_conf.uri) {
1396 struct ldap_config_list_str *uri = ldap_conf.uri;
1399 sudo_printf(SUDO_CONV_ERROR_MSG, "uri %s\n",
1401 } while ((uri = uri->next) != NULL);
1403 sudo_printf(SUDO_CONV_ERROR_MSG, "host %s\n",
1404 ldap_conf.host ? ldap_conf.host : "(NONE)");
1405 sudo_printf(SUDO_CONV_ERROR_MSG, "port %d\n",
1408 sudo_printf(SUDO_CONV_ERROR_MSG, "ldap_version %d\n",
1411 if (ldap_conf.base) {
1412 struct ldap_config_list_str *base = ldap_conf.base;
1414 sudo_printf(SUDO_CONV_ERROR_MSG, "sudoers_base %s\n",
1416 } while ((base = base->next) != NULL);
1418 sudo_printf(SUDO_CONV_ERROR_MSG, "sudoers_base %s\n",
1419 "(NONE: LDAP disabled)");
1421 if (ldap_conf.search_filter) {
1422 sudo_printf(SUDO_CONV_ERROR_MSG, "search_filter %s\n",
1423 ldap_conf.search_filter);
1425 sudo_printf(SUDO_CONV_ERROR_MSG, "binddn %s\n",
1426 ldap_conf.binddn ? ldap_conf.binddn : "(anonymous)");
1427 sudo_printf(SUDO_CONV_ERROR_MSG, "bindpw %s\n",
1428 ldap_conf.bindpw ? ldap_conf.bindpw : "(anonymous)");
1429 if (ldap_conf.bind_timelimit > 0) {
1430 sudo_printf(SUDO_CONV_ERROR_MSG, "bind_timelimit %d\n",
1431 ldap_conf.bind_timelimit);
1433 if (ldap_conf.timelimit > 0) {
1434 sudo_printf(SUDO_CONV_ERROR_MSG, "timelimit %d\n",
1435 ldap_conf.timelimit);
1437 if (ldap_conf.deref != -1) {
1438 sudo_printf(SUDO_CONV_ERROR_MSG, "deref %d\n",
1441 sudo_printf(SUDO_CONV_ERROR_MSG, "ssl %s\n",
1442 ldap_conf.ssl ? ldap_conf.ssl : "(no)");
1443 if (ldap_conf.tls_checkpeer != -1) {
1444 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_checkpeer %s\n",
1445 ldap_conf.tls_checkpeer ? "(yes)" : "(no)");
1447 if (ldap_conf.tls_cacertfile != NULL) {
1448 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_cacertfile %s\n",
1449 ldap_conf.tls_cacertfile);
1451 if (ldap_conf.tls_cacertdir != NULL) {
1452 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_cacertdir %s\n",
1453 ldap_conf.tls_cacertdir);
1455 if (ldap_conf.tls_random_file != NULL) {
1456 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_random_file %s\n",
1457 ldap_conf.tls_random_file);
1459 if (ldap_conf.tls_cipher_suite != NULL) {
1460 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_cipher_suite %s\n",
1461 ldap_conf.tls_cipher_suite);
1463 if (ldap_conf.tls_certfile != NULL) {
1464 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_certfile %s\n",
1465 ldap_conf.tls_certfile);
1467 if (ldap_conf.tls_keyfile != NULL) {
1468 sudo_printf(SUDO_CONV_ERROR_MSG, "tls_keyfile %s\n",
1469 ldap_conf.tls_keyfile);
1471 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1472 if (ldap_conf.use_sasl != -1) {
1473 sudo_printf(SUDO_CONV_ERROR_MSG, "use_sasl %s\n",
1474 ldap_conf.use_sasl ? "yes" : "no");
1475 sudo_printf(SUDO_CONV_ERROR_MSG, "sasl_auth_id %s\n",
1476 ldap_conf.sasl_auth_id ? ldap_conf.sasl_auth_id : "(NONE)");
1477 sudo_printf(SUDO_CONV_ERROR_MSG, "rootuse_sasl %d\n",
1478 ldap_conf.rootuse_sasl);
1479 sudo_printf(SUDO_CONV_ERROR_MSG, "rootsasl_auth_id %s\n",
1480 ldap_conf.rootsasl_auth_id ? ldap_conf.rootsasl_auth_id : "(NONE)");
1481 sudo_printf(SUDO_CONV_ERROR_MSG, "sasl_secprops %s\n",
1482 ldap_conf.sasl_secprops ? ldap_conf.sasl_secprops : "(NONE)");
1483 sudo_printf(SUDO_CONV_ERROR_MSG, "krb5_ccname %s\n",
1484 ldap_conf.krb5_ccname ? ldap_conf.krb5_ccname : "(NONE)");
1487 sudo_printf(SUDO_CONV_ERROR_MSG, "===================\n");
1489 if (!ldap_conf.base)
1490 debug_return_bool(false); /* if no base is defined, ignore LDAP */
1492 if (ldap_conf.bind_timelimit > 0)
1493 ldap_conf.bind_timelimit *= 1000; /* convert to ms */
1496 * Interpret SSL option
1498 if (ldap_conf.ssl != NULL) {
1499 if (strcasecmp(ldap_conf.ssl, "start_tls") == 0)
1500 ldap_conf.ssl_mode = SUDO_LDAP_STARTTLS;
1501 else if (atobool(ldap_conf.ssl) == true)
1502 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
1505 #if defined(HAVE_LDAPSSL_SET_STRENGTH) && !defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
1506 if (ldap_conf.tls_checkpeer != -1) {
1507 ldapssl_set_strength(NULL,
1508 ldap_conf.tls_checkpeer ? LDAPSSL_AUTH_CERT : LDAPSSL_AUTH_WEAK);
1512 #ifndef HAVE_LDAP_INITIALIZE
1513 /* Convert uri list to host list if no ldap_initialize(). */
1514 if (ldap_conf.uri) {
1515 struct ldap_config_list_str *uri = ldap_conf.uri;
1516 if (sudo_ldap_parse_uri(uri) != 0)
1517 debug_return_bool(false);
1519 ldap_conf.uri = uri->next;
1521 } while ((uri = ldap_conf.uri));
1522 ldap_conf.port = LDAP_PORT;
1526 if (!ldap_conf.uri) {
1527 /* Use port 389 for plaintext LDAP and port 636 for SSL LDAP */
1528 if (ldap_conf.port < 0)
1530 ldap_conf.ssl_mode == SUDO_LDAP_SSL ? LDAPS_PORT : LDAP_PORT;
1532 #ifdef HAVE_LDAP_CREATE
1534 * Cannot specify port directly to ldap_create(), each host must
1535 * include :port to override the default.
1537 if (ldap_conf.port != LDAP_PORT)
1538 sudo_ldap_conf_add_ports();
1542 /* If search filter is not parenthesized, make it so. */
1543 if (ldap_conf.search_filter && ldap_conf.search_filter[0] != '(') {
1544 size_t len = strlen(ldap_conf.search_filter);
1545 cp = ldap_conf.search_filter;
1546 ldap_conf.search_filter = emalloc(len + 3);
1547 ldap_conf.search_filter[0] = '(';
1548 memcpy(ldap_conf.search_filter + 1, cp, len);
1549 ldap_conf.search_filter[len + 1] = ')';
1550 ldap_conf.search_filter[len + 2] = '\0';
1554 /* If rootbinddn set, read in /etc/ldap.secret if it exists. */
1555 if (ldap_conf.rootbinddn)
1556 sudo_ldap_read_secret(_PATH_LDAP_SECRET);
1558 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1560 * Make sure we can open the file specified by krb5_ccname.
1562 if (ldap_conf.krb5_ccname != NULL) {
1563 if (strncasecmp(ldap_conf.krb5_ccname, "FILE:", 5) == 0 ||
1564 strncasecmp(ldap_conf.krb5_ccname, "WRFILE:", 7) == 0) {
1565 value = ldap_conf.krb5_ccname +
1566 (ldap_conf.krb5_ccname[4] == ':' ? 5 : 7);
1567 if ((fp = fopen(value, "r")) != NULL) {
1568 DPRINTF(("using krb5 credential cache: %s", value), 1);
1571 /* Can't open it, just ignore the entry. */
1572 DPRINTF(("unable to open krb5 credential cache: %s", value), 1);
1573 efree(ldap_conf.krb5_ccname);
1574 ldap_conf.krb5_ccname = NULL;
1579 debug_return_bool(true);
1583 * Extract the dn from an entry and return the first rdn from it.
1586 sudo_ldap_get_first_rdn(LDAP *ld, LDAPMessage *entry)
1588 #ifdef HAVE_LDAP_STR2DN
1589 char *dn, *rdn = NULL;
1591 debug_decl(sudo_ldap_get_first_rdn, SUDO_DEBUG_LDAP)
1593 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1594 debug_return_str(NULL);
1595 if (ldap_str2dn(dn, &tmpDN, LDAP_DN_FORMAT_LDAP) == LDAP_SUCCESS) {
1596 ldap_rdn2str(tmpDN[0], &rdn, LDAP_DN_FORMAT_UFN);
1600 debug_return_str(rdn);
1603 debug_decl(sudo_ldap_get_first_rdn, SUDO_DEBUG_LDAP)
1605 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1607 edn = ldap_explode_dn(dn, 1);
1609 debug_return_str(edn ? edn[0] : NULL);
1614 * Fetch and display the global Options.
1617 sudo_ldap_display_defaults(struct sudo_nss *nss, struct passwd *pw,
1620 struct berval **bv, **p;
1621 struct timeval tv, *tvp = NULL;
1622 struct ldap_config_list_str *base;
1623 struct sudo_ldap_handle *handle = nss->handle;
1625 LDAPMessage *entry, *result;
1626 char *prefix, *filt;
1628 debug_decl(sudo_ldap_display_defaults, SUDO_DEBUG_LDAP)
1630 if (handle == NULL || handle->ld == NULL)
1634 filt = sudo_ldap_build_default_filter();
1635 for (base = ldap_conf.base; base != NULL; base = base->next) {
1636 if (ldap_conf.timeout > 0) {
1637 tv.tv_sec = ldap_conf.timeout;
1642 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE,
1643 filt, NULL, 0, NULL, NULL, tvp, 0, &result);
1644 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
1645 bv = ldap_get_values_len(ld, entry, "sudoOption");
1647 if (lbuf->len == 0 || isspace((unsigned char)lbuf->buf[lbuf->len - 1]))
1651 for (p = bv; *p != NULL; p++) {
1652 lbuf_append(lbuf, "%s%s", prefix, (*p)->bv_val);
1656 ldap_value_free_len(bv);
1660 ldap_msgfree(result);
1664 debug_return_int(count);
1671 sudo_ldap_display_bound_defaults(struct sudo_nss *nss, struct passwd *pw,
1674 debug_decl(sudo_ldap_display_bound_defaults, SUDO_DEBUG_LDAP)
1675 debug_return_int(0);
1679 * Print a record in the short form, ala file sudoers.
1682 sudo_ldap_display_entry_short(LDAP *ld, LDAPMessage *entry, struct lbuf *lbuf)
1684 struct berval **bv, **p;
1686 debug_decl(sudo_ldap_display_entry_short, SUDO_DEBUG_LDAP)
1688 lbuf_append(lbuf, " (");
1690 /* get the RunAsUser Values from the entry */
1691 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1693 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1695 for (p = bv; *p != NULL; p++) {
1696 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1698 ldap_value_free_len(bv);
1700 lbuf_append(lbuf, "%s", def_runas_default);
1702 /* get the RunAsGroup Values from the entry */
1703 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1705 lbuf_append(lbuf, " : ");
1706 for (p = bv; *p != NULL; p++) {
1707 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1709 ldap_value_free_len(bv);
1711 lbuf_append(lbuf, ") ");
1713 /* get the Option Values from the entry */
1714 bv = ldap_get_values_len(ld, entry, "sudoOption");
1716 for (p = bv; *p != NULL; p++) {
1717 char *cp = (*p)->bv_val;
1720 if (strcmp(cp, "authenticate") == 0)
1721 lbuf_append(lbuf, (*p)->bv_val[0] == '!' ?
1722 "NOPASSWD: " : "PASSWD: ");
1723 else if (strcmp(cp, "noexec") == 0)
1724 lbuf_append(lbuf, (*p)->bv_val[0] == '!' ?
1725 "EXEC: " : "NOEXEC: ");
1726 else if (strcmp(cp, "setenv") == 0)
1727 lbuf_append(lbuf, (*p)->bv_val[0] == '!' ?
1728 "NOSETENV: " : "SETENV: ");
1730 ldap_value_free_len(bv);
1733 /* get the Command Values from the entry */
1734 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1736 for (p = bv; *p != NULL; p++) {
1737 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1740 ldap_value_free_len(bv);
1742 lbuf_append(lbuf, "\n");
1744 debug_return_int(count);
1748 * Print a record in the long form.
1751 sudo_ldap_display_entry_long(LDAP *ld, LDAPMessage *entry, struct lbuf *lbuf)
1753 struct berval **bv, **p;
1756 debug_decl(sudo_ldap_display_entry_long, SUDO_DEBUG_LDAP)
1758 /* extract the dn, only show the first rdn */
1759 rdn = sudo_ldap_get_first_rdn(ld, entry);
1761 lbuf_append(lbuf, _("\nLDAP Role: %s\n"), rdn);
1763 lbuf_append(lbuf, _("\nLDAP Role: UNKNOWN\n"));
1767 /* get the RunAsUser Values from the entry */
1768 lbuf_append(lbuf, " RunAsUsers: ");
1769 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1771 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1773 for (p = bv; *p != NULL; p++) {
1774 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1776 ldap_value_free_len(bv);
1778 lbuf_append(lbuf, "%s", def_runas_default);
1779 lbuf_append(lbuf, "\n");
1781 /* get the RunAsGroup Values from the entry */
1782 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1784 lbuf_append(lbuf, " RunAsGroups: ");
1785 for (p = bv; *p != NULL; p++) {
1786 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1788 ldap_value_free_len(bv);
1789 lbuf_append(lbuf, "\n");
1792 /* get the Option Values from the entry */
1793 bv = ldap_get_values_len(ld, entry, "sudoOption");
1795 lbuf_append(lbuf, " Options: ");
1796 for (p = bv; *p != NULL; p++) {
1797 lbuf_append(lbuf, "%s%s", p != bv ? ", " : "", (*p)->bv_val);
1799 ldap_value_free_len(bv);
1800 lbuf_append(lbuf, "\n");
1804 * Display order attribute if present. This attribute is single valued,
1805 * so there is no need for a loop.
1807 bv = ldap_get_values_len(ld, entry, "sudoOrder");
1810 lbuf_append(lbuf, _(" Order: %s\n"), (*bv)->bv_val);
1812 ldap_value_free_len(bv);
1815 /* Get the command values from the entry. */
1816 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1818 lbuf_append(lbuf, _(" Commands:\n"));
1819 for (p = bv; *p != NULL; p++) {
1820 lbuf_append(lbuf, "\t%s\n", (*p)->bv_val);
1823 ldap_value_free_len(bv);
1826 debug_return_int(count);
1830 * Like sudo_ldap_lookup(), except we just print entries.
1833 sudo_ldap_display_privs(struct sudo_nss *nss, struct passwd *pw,
1836 struct sudo_ldap_handle *handle = nss->handle;
1838 struct ldap_result *lres;
1841 debug_decl(sudo_ldap_display_privs, SUDO_DEBUG_LDAP)
1843 if (handle == NULL || handle->ld == NULL)
1847 DPRINTF(("ldap search for command list"), 1);
1848 lres = sudo_ldap_result_get(nss, pw);
1850 /* Display all matching entries. */
1851 for (i = 0; i < lres->nentries; i++) {
1852 entry = lres->entries[i].entry;
1854 count += sudo_ldap_display_entry_long(ld, entry, lbuf);
1856 count += sudo_ldap_display_entry_short(ld, entry, lbuf);
1860 debug_return_int(count);
1864 sudo_ldap_display_cmnd(struct sudo_nss *nss, struct passwd *pw)
1866 struct sudo_ldap_handle *handle = nss->handle;
1868 struct ldap_result *lres;
1872 debug_decl(sudo_ldap_display_cmnd, SUDO_DEBUG_LDAP)
1874 if (handle == NULL || handle->ld == NULL)
1879 * The sudo_ldap_result_get() function returns all nodes that match
1880 * the user and the host.
1882 DPRINTF(("ldap search for command list"), 1);
1883 lres = sudo_ldap_result_get(nss, pw);
1884 for (i = 0; i < lres->nentries; i++) {
1885 entry = lres->entries[i].entry;
1886 if (sudo_ldap_check_command(ld, entry, NULL) &&
1887 sudo_ldap_check_runas(ld, entry)) {
1895 printf("%s%s%s\n", safe_cmnd ? safe_cmnd : user_cmnd,
1896 user_args ? " " : "", user_args ? user_args : "");
1897 debug_return_bool(!found);
1900 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1902 sudo_ldap_sasl_interact(LDAP *ld, unsigned int flags, void *_auth_id,
1905 char *auth_id = (char *)_auth_id;
1906 sasl_interact_t *interact = (sasl_interact_t *)_interact;
1907 debug_decl(sudo_ldap_sasl_interact, SUDO_DEBUG_LDAP)
1909 for (; interact->id != SASL_CB_LIST_END; interact++) {
1910 if (interact->id != SASL_CB_USER)
1911 debug_return_int(LDAP_PARAM_ERROR);
1913 if (auth_id != NULL)
1914 interact->result = auth_id;
1915 else if (interact->defresult != NULL)
1916 interact->result = interact->defresult;
1918 interact->result = "";
1920 interact->len = strlen(interact->result);
1921 #if SASL_VERSION_MAJOR < 2
1922 interact->result = estrdup(interact->result);
1923 #endif /* SASL_VERSION_MAJOR < 2 */
1925 debug_return_int(LDAP_SUCCESS);
1927 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1931 * Set LDAP options from the specified options table
1934 sudo_ldap_set_options_table(LDAP *ld, struct ldap_config_table *table)
1936 struct ldap_config_table *cur;
1937 int ival, rc, errors = 0;
1939 debug_decl(sudo_ldap_set_options_table, SUDO_DEBUG_LDAP)
1941 for (cur = table; cur->conf_str != NULL; cur++) {
1942 if (cur->opt_val == -1)
1945 switch (cur->type) {
1948 ival = *(int *)(cur->valp);
1950 DPRINTF(("ldap_set_option: %s -> %d", cur->conf_str, ival), 1);
1951 rc = ldap_set_option(ld, cur->opt_val, &ival);
1952 if (rc != LDAP_OPT_SUCCESS) {
1953 warningx("ldap_set_option: %s -> %d: %s",
1954 cur->conf_str, ival, ldap_err2string(rc));
1960 sval = *(char **)(cur->valp);
1962 DPRINTF(("ldap_set_option: %s -> %s", cur->conf_str, sval), 1);
1963 rc = ldap_set_option(ld, cur->opt_val, sval);
1964 if (rc != LDAP_OPT_SUCCESS) {
1965 warningx("ldap_set_option: %s -> %s: %s",
1966 cur->conf_str, sval, ldap_err2string(rc));
1973 debug_return_int(errors ? -1 : 0);
1977 * Set LDAP options based on the global config table.
1980 sudo_ldap_set_options_global(void)
1983 debug_decl(sudo_ldap_set_options_global, SUDO_DEBUG_LDAP)
1985 /* Set ber options */
1986 #ifdef LBER_OPT_DEBUG_LEVEL
1987 if (ldap_conf.ldap_debug)
1988 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug);
1991 /* Parse global LDAP options table. */
1992 rc = sudo_ldap_set_options_table(NULL, ldap_conf_global);
1994 debug_return_int(-1);
1995 debug_return_int(0);
1999 * Set LDAP options based on the per-connection config table.
2002 sudo_ldap_set_options_conn(LDAP *ld)
2005 debug_decl(sudo_ldap_set_options_conn, SUDO_DEBUG_LDAP)
2007 /* Parse per-connection LDAP options table. */
2008 rc = sudo_ldap_set_options_table(ld, ldap_conf_conn);
2010 debug_return_int(-1);
2012 #ifdef LDAP_OPT_TIMEOUT
2013 /* Convert timeout to a timeval */
2014 if (ldap_conf.timeout > 0) {
2016 tv.tv_sec = ldap_conf.timeout;
2018 DPRINTF(("ldap_set_option(LDAP_OPT_TIMEOUT, %ld)",
2019 (long)tv.tv_sec), 1);
2020 rc = ldap_set_option(ld, LDAP_OPT_TIMEOUT, &tv);
2021 if (rc != LDAP_OPT_SUCCESS) {
2022 warningx("ldap_set_option(TIMEOUT, %ld): %s",
2023 (long)tv.tv_sec, ldap_err2string(rc));
2027 #ifdef LDAP_OPT_NETWORK_TIMEOUT
2028 /* Convert bind_timelimit to a timeval */
2029 if (ldap_conf.bind_timelimit > 0) {
2031 tv.tv_sec = ldap_conf.bind_timelimit / 1000;
2033 DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)",
2034 (long)tv.tv_sec), 1);
2035 rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
2036 # if !defined(LDAP_OPT_CONNECT_TIMEOUT) || LDAP_VENDOR_VERSION != 510
2037 /* Tivoli Directory Server 6.3 libs always return a (bogus) error. */
2038 if (rc != LDAP_OPT_SUCCESS) {
2039 warningx("ldap_set_option(NETWORK_TIMEOUT, %ld): %s",
2040 (long)tv.tv_sec, ldap_err2string(rc));
2046 #if defined(LDAP_OPT_X_TLS) && !defined(HAVE_LDAPSSL_INIT)
2047 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
2048 int val = LDAP_OPT_X_TLS_HARD;
2049 DPRINTF(("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD)"), 1);
2050 rc = ldap_set_option(ld, LDAP_OPT_X_TLS, &val);
2051 if (rc != LDAP_SUCCESS) {
2052 warningx("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD): %s",
2053 ldap_err2string(rc));
2054 debug_return_int(-1);
2058 debug_return_int(0);
2062 * Create a new sudo_ldap_result structure.
2064 static struct ldap_result *
2065 sudo_ldap_result_alloc(void)
2067 struct ldap_result *result;
2068 debug_decl(sudo_ldap_result_alloc, SUDO_DEBUG_LDAP)
2070 debug_return_ptr(ecalloc(1, sizeof(*result)));
2074 * Free the ldap result structure
2077 sudo_ldap_result_free(struct ldap_result *lres)
2079 struct ldap_search_list *s;
2080 debug_decl(sudo_ldap_result_free, SUDO_DEBUG_LDAP)
2083 if (lres->nentries) {
2084 efree(lres->entries);
2085 lres->entries = NULL;
2087 if (lres->searches) {
2088 while ((s = lres->searches) != NULL) {
2089 ldap_msgfree(s->searchresult);
2090 lres->searches = s->next;
2100 * Add a search result to the ldap_result structure.
2102 static struct ldap_search_list *
2103 sudo_ldap_result_add_search(struct ldap_result *lres, LDAP *ldap,
2104 LDAPMessage *searchresult)
2106 struct ldap_search_list *s, *news;
2107 debug_decl(sudo_ldap_result_add_search, SUDO_DEBUG_LDAP)
2109 news = ecalloc(1, sizeof(struct ldap_search_list));
2111 news->searchresult = searchresult;
2112 /* news->next = NULL; */
2114 /* Add entry to the end of the chain (XXX - tailq instead?). */
2115 if (lres->searches) {
2116 for (s = lres->searches; s->next != NULL; s = s->next)
2120 lres->searches = news;
2122 debug_return_ptr(news);
2126 * Connect to the LDAP server specified by ld
2129 sudo_ldap_bind_s(LDAP *ld)
2132 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
2133 const char *old_ccname = user_ccname;
2134 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
2135 unsigned int status;
2138 debug_decl(sudo_ldap_bind_s, SUDO_DEBUG_LDAP)
2140 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
2141 if (ldap_conf.rootuse_sasl == true ||
2142 (ldap_conf.rootuse_sasl != false && ldap_conf.use_sasl == true)) {
2143 void *auth_id = ldap_conf.rootsasl_auth_id ?
2144 ldap_conf.rootsasl_auth_id : ldap_conf.sasl_auth_id;
2146 if (ldap_conf.krb5_ccname != NULL) {
2147 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
2148 if (gss_krb5_ccache_name(&status, ldap_conf.krb5_ccname, &old_ccname)
2149 != GSS_S_COMPLETE) {
2151 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
2154 sudo_setenv("KRB5CCNAME", ldap_conf.krb5_ccname, true);
2157 rc = ldap_sasl_interactive_bind_s(ld, ldap_conf.binddn, "GSSAPI",
2158 NULL, NULL, LDAP_SASL_QUIET, sudo_ldap_sasl_interact, auth_id);
2159 if (ldap_conf.krb5_ccname != NULL) {
2160 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
2161 if (gss_krb5_ccache_name(&status, old_ccname, NULL) != GSS_S_COMPLETE)
2162 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
2164 if (old_ccname != NULL)
2165 sudo_setenv("KRB5CCNAME", old_ccname, true);
2167 sudo_unsetenv("KRB5CCNAME");
2170 if (rc != LDAP_SUCCESS) {
2171 warningx("ldap_sasl_interactive_bind_s(): %s",
2172 ldap_err2string(rc));
2173 debug_return_int(-1);
2175 DPRINTF(("ldap_sasl_interactive_bind_s() ok"), 1);
2177 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
2178 #ifdef HAVE_LDAP_SASL_BIND_S
2182 bv.bv_val = ldap_conf.bindpw ? ldap_conf.bindpw : "";
2183 bv.bv_len = strlen(bv.bv_val);
2185 rc = ldap_sasl_bind_s(ld, ldap_conf.binddn, LDAP_SASL_SIMPLE, &bv,
2187 if (rc != LDAP_SUCCESS) {
2188 warningx("ldap_sasl_bind_s(): %s", ldap_err2string(rc));
2189 debug_return_int(-1);
2191 DPRINTF(("ldap_sasl_bind_s() ok"), 1);
2195 rc = ldap_simple_bind_s(ld, ldap_conf.binddn, ldap_conf.bindpw);
2196 if (rc != LDAP_SUCCESS) {
2197 warningx("ldap_simple_bind_s(): %s", ldap_err2string(rc));
2198 debug_return_int(-1);
2200 DPRINTF(("ldap_simple_bind_s() ok"), 1);
2203 debug_return_int(0);
2207 * Open a connection to the LDAP server.
2208 * Returns 0 on success and non-zero on failure.
2211 sudo_ldap_open(struct sudo_nss *nss)
2215 bool ldapnoinit = false;
2216 struct sudo_ldap_handle *handle;
2217 debug_decl(sudo_ldap_open, SUDO_DEBUG_LDAP)
2219 if (!sudo_ldap_read_config())
2220 debug_return_int(-1);
2222 /* Prevent reading of user ldaprc and system defaults. */
2223 if (sudo_getenv("LDAPNOINIT") == NULL) {
2225 sudo_setenv("LDAPNOINIT", "1", true);
2228 /* Set global LDAP options */
2229 if (sudo_ldap_set_options_global() < 0)
2230 debug_return_int(-1);
2232 /* Connect to LDAP server */
2233 #ifdef HAVE_LDAP_INITIALIZE
2234 if (ldap_conf.uri != NULL) {
2235 char *buf = sudo_ldap_join_uri(ldap_conf.uri);
2236 DPRINTF(("ldap_initialize(ld, %s)", buf), 2);
2237 rc = ldap_initialize(&ld, buf);
2239 if (rc != LDAP_SUCCESS)
2240 warningx(_("unable to initialize LDAP: %s"), ldap_err2string(rc));
2243 rc = sudo_ldap_init(&ld, ldap_conf.host, ldap_conf.port);
2244 if (rc != LDAP_SUCCESS)
2245 debug_return_int(-1);
2247 /* Set LDAP per-connection options */
2248 if (sudo_ldap_set_options_conn(ld) < 0)
2249 debug_return_int(-1);
2252 sudo_unsetenv("LDAPNOINIT");
2254 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
2255 #if defined(HAVE_LDAP_START_TLS_S)
2256 rc = ldap_start_tls_s(ld, NULL, NULL);
2257 if (rc != LDAP_SUCCESS) {
2258 warningx("ldap_start_tls_s(): %s", ldap_err2string(rc));
2259 debug_return_int(-1);
2261 DPRINTF(("ldap_start_tls_s() ok"), 1);
2262 #elif defined(HAVE_LDAP_SSL_CLIENT_INIT) && defined(HAVE_LDAP_START_TLS_S_NP)
2263 if (ldap_ssl_client_init(ldap_conf.tls_keyfile, ldap_conf.tls_keypw, 0, &rc) != LDAP_SUCCESS) {
2264 warningx("ldap_ssl_client_init(): %s", ldap_err2string(rc));
2265 debug_return_int(-1);
2267 rc = ldap_start_tls_s_np(ld, NULL);
2268 if (rc != LDAP_SUCCESS) {
2269 warningx("ldap_start_tls_s_np(): %s", ldap_err2string(rc));
2270 debug_return_int(-1);
2272 DPRINTF(("ldap_start_tls_s_np() ok"), 1);
2274 warningx(_("start_tls specified but LDAP libs do not support ldap_start_tls_s() or ldap_start_tls_s_np()"));
2275 #endif /* !HAVE_LDAP_START_TLS_S && !HAVE_LDAP_START_TLS_S_NP */
2278 /* Actually connect */
2279 if (sudo_ldap_bind_s(ld) != 0)
2280 debug_return_int(-1);
2282 /* Create a handle container. */
2283 handle = ecalloc(1, sizeof(struct sudo_ldap_handle));
2285 /* handle->result = NULL; */
2286 /* handle->username = NULL; */
2287 /* handle->grlist = NULL; */
2288 nss->handle = handle;
2290 debug_return_int(0);
2294 sudo_ldap_setdefs(struct sudo_nss *nss)
2296 struct ldap_config_list_str *base;
2297 struct sudo_ldap_handle *handle = nss->handle;
2298 struct timeval tv, *tvp = NULL;
2300 LDAPMessage *entry, *result;
2303 debug_decl(sudo_ldap_setdefs, SUDO_DEBUG_LDAP)
2305 if (handle == NULL || handle->ld == NULL)
2306 debug_return_int(-1);
2309 filt = sudo_ldap_build_default_filter();
2310 DPRINTF(("Looking for cn=defaults: %s", filt), 1);
2312 for (base = ldap_conf.base; base != NULL; base = base->next) {
2313 if (ldap_conf.timeout > 0) {
2314 tv.tv_sec = ldap_conf.timeout;
2319 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE,
2320 filt, NULL, 0, NULL, NULL, tvp, 0, &result);
2321 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
2322 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
2323 sudo_ldap_parse_options(ld, entry);
2325 DPRINTF(("no default options found in %s", base->val), 1);
2328 ldap_msgfree(result);
2332 debug_return_int(0);
2336 * like sudoers_lookup() - only LDAP style
2339 sudo_ldap_lookup(struct sudo_nss *nss, int ret, int pwflag)
2341 struct sudo_ldap_handle *handle = nss->handle;
2344 int i, rc, setenv_implied;
2345 struct ldap_result *lres = NULL;
2346 debug_decl(sudo_ldap_lookup, SUDO_DEBUG_LDAP)
2348 if (handle == NULL || handle->ld == NULL)
2349 debug_return_int(ret);
2352 /* Fetch list of sudoRole entries that match user and host. */
2353 lres = sudo_ldap_result_get(nss, sudo_user.pw);
2356 * The following queries are only determine whether or not a
2357 * password is required, so the order of the entries doesn't matter.
2360 int doauth = UNSPEC;
2361 int matched = UNSPEC;
2362 enum def_tuple pwcheck =
2363 (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
2365 DPRINTF(("perform search for pwflag %d", pwflag), 1);
2366 for (i = 0; i < lres->nentries; i++) {
2367 entry = lres->entries[i].entry;
2368 if ((pwcheck == any && doauth != false) ||
2369 (pwcheck == all && doauth == false)) {
2370 doauth = sudo_ldap_check_bool(ld, entry, "authenticate");
2372 /* Only check the command when listing another user. */
2373 if (user_uid == 0 || list_pw == NULL ||
2374 user_uid == list_pw->pw_uid ||
2375 sudo_ldap_check_command(ld, entry, NULL)) {
2380 if (matched || user_uid == 0) {
2381 SET(ret, VALIDATE_OK);
2382 CLR(ret, VALIDATE_NOT_OK);
2383 if (def_authenticate) {
2386 SET(ret, FLAG_CHECK_USER);
2390 if (doauth == false)
2391 def_authenticate = false;
2394 def_authenticate = false;
2404 DPRINTF(("searching LDAP for sudoers entries"), 1);
2406 setenv_implied = false;
2407 for (i = 0; i < lres->nentries; i++) {
2408 entry = lres->entries[i].entry;
2409 if (!sudo_ldap_check_runas(ld, entry))
2411 rc = sudo_ldap_check_command(ld, entry, &setenv_implied);
2413 /* We have a match. */
2414 DPRINTF(("Command %sallowed", rc == true ? "" : "NOT "), 1);
2416 DPRINTF(("LDAP entry: %p", entry), 1);
2417 /* Apply entry-specific options. */
2420 sudo_ldap_parse_options(ld, entry);
2422 /* Set role and type if not specified on command line. */
2423 if (user_role == NULL)
2424 user_role = def_role;
2425 if (user_type == NULL)
2426 user_type = def_type;
2427 #endif /* HAVE_SELINUX */
2428 SET(ret, VALIDATE_OK);
2429 CLR(ret, VALIDATE_NOT_OK);
2431 SET(ret, VALIDATE_NOT_OK);
2432 CLR(ret, VALIDATE_OK);
2439 DPRINTF(("done with LDAP searches"), 1);
2440 DPRINTF(("user_matches=%d", lres->user_matches), 1);
2441 DPRINTF(("host_matches=%d", lres->host_matches), 1);
2443 if (!ISSET(ret, VALIDATE_OK)) {
2444 /* No matching entries. */
2445 if (pwflag && list_pw == NULL)
2446 SET(ret, FLAG_NO_CHECK);
2448 if (lres->user_matches)
2449 CLR(ret, FLAG_NO_USER);
2450 if (lres->host_matches)
2451 CLR(ret, FLAG_NO_HOST);
2452 DPRINTF(("sudo_ldap_lookup(%d)=0x%02x", pwflag, ret), 1);
2454 debug_return_int(ret);
2458 * Comparison function for ldap_entry_wrapper structures, descending order.
2461 ldap_entry_compare(const void *a, const void *b)
2463 const struct ldap_entry_wrapper *aw = a;
2464 const struct ldap_entry_wrapper *bw = b;
2465 debug_decl(ldap_entry_compare, SUDO_DEBUG_LDAP)
2467 debug_return_int(bw->order < aw->order ? -1 :
2468 (bw->order > aw->order ? 1 : 0));
2472 * Find the last entry in the list of searches, usually the
2473 * one currently being used to add entries.
2474 * XXX - use a tailq instead?
2476 static struct ldap_search_list *
2477 sudo_ldap_result_last_search(struct ldap_result *lres)
2479 struct ldap_search_list *result = lres->searches;
2480 debug_decl(sudo_ldap_result_last_search, SUDO_DEBUG_LDAP)
2483 while (result->next)
2484 result = result->next;
2486 debug_return_ptr(result);
2490 * Add an entry to the result structure.
2492 static struct ldap_entry_wrapper *
2493 sudo_ldap_result_add_entry(struct ldap_result *lres, LDAPMessage *entry)
2495 struct ldap_search_list *last;
2499 debug_decl(sudo_ldap_result_add_entry, SUDO_DEBUG_LDAP)
2501 /* Determine whether the entry has the sudoOrder attribute. */
2502 last = sudo_ldap_result_last_search(lres);
2503 bv = ldap_get_values_len(last->ldap, entry, "sudoOrder");
2505 if (ldap_count_values_len(bv) > 0) {
2506 /* Get the value of this attribute, 0 if not present. */
2507 DPRINTF(("order attribute raw: %s", (*bv)->bv_val), 1);
2508 order = strtod((*bv)->bv_val, &ep);
2509 if (ep == (*bv)->bv_val || *ep != '\0') {
2510 warningx(_("invalid sudoOrder attribute: %s"), (*bv)->bv_val);
2513 DPRINTF(("order attribute: %f", order), 1);
2515 ldap_value_free_len(bv);
2519 * Enlarge the array of entry wrappers as needed, preallocating blocks
2520 * of 100 entries to save on allocation time.
2522 if (++lres->nentries > lres->allocated_entries) {
2523 lres->allocated_entries += ALLOCATION_INCREMENT;
2524 lres->entries = erealloc3(lres->entries, lres->allocated_entries,
2525 sizeof(lres->entries[0]));
2528 /* Fill in the new entry and return it. */
2529 lres->entries[lres->nentries - 1].entry = entry;
2530 lres->entries[lres->nentries - 1].order = order;
2532 debug_return_ptr(&lres->entries[lres->nentries - 1]);
2536 * Free the ldap result structure in the sudo_nss handle.
2539 sudo_ldap_result_free_nss(struct sudo_nss *nss)
2541 struct sudo_ldap_handle *handle = nss->handle;
2542 debug_decl(sudo_ldap_result_free_nss, SUDO_DEBUG_LDAP)
2544 if (handle->result != NULL) {
2545 DPRINTF(("removing reusable search result"), 1);
2546 sudo_ldap_result_free(handle->result);
2547 if (handle->username) {
2548 efree(handle->username);
2549 handle->username = NULL;
2551 handle->grlist = NULL;
2552 handle->result = NULL;
2558 * Perform the LDAP query for the user or return a cached query if
2559 * there is one for this user.
2561 static struct ldap_result *
2562 sudo_ldap_result_get(struct sudo_nss *nss, struct passwd *pw)
2564 struct sudo_ldap_handle *handle = nss->handle;
2565 struct ldap_config_list_str *base;
2566 struct ldap_result *lres;
2567 struct timeval tv, *tvp = NULL;
2568 LDAPMessage *entry, *result;
2569 LDAP *ld = handle->ld;
2572 debug_decl(sudo_ldap_result_get, SUDO_DEBUG_LDAP)
2575 * If we already have a cached result, return it so we don't have to
2576 * have to contact the LDAP server again.
2578 if (handle->result) {
2579 if (handle->grlist == user_group_list &&
2580 strcmp(pw->pw_name, handle->username) == 0) {
2581 DPRINTF(("reusing previous result (user %s) with %d entries",
2582 handle->username, handle->result->nentries), 1);
2583 debug_return_ptr(handle->result);
2585 /* User mismatch, cached result cannot be used. */
2586 DPRINTF(("removing result (user %s), new search (user %s)",
2587 handle->username, pw->pw_name), 1);
2588 sudo_ldap_result_free_nss(nss);
2592 * Okay - time to search for anything that matches this user
2593 * Lets limit it to only two queries of the LDAP server
2595 * The first pass will look by the username, groups, and
2596 * the keyword ALL. We will then inspect the results that
2597 * came back from the query. We don't need to inspect the
2598 * sudoUser in this pass since the LDAP server already scanned
2601 * The second pass will return all the entries that contain
2602 * user netgroups. Then we take the netgroups returned and
2603 * try to match them against the username.
2605 * Since we have to sort the possible entries before we make a
2606 * decision, we perform the queries and store all of the results in
2607 * an ldap_result object. The results are then sorted by sudoOrder.
2609 lres = sudo_ldap_result_alloc();
2610 for (do_netgr = 0; do_netgr < 2; do_netgr++) {
2611 filt = do_netgr ? sudo_ldap_build_pass2() : sudo_ldap_build_pass1(pw);
2612 DPRINTF(("ldap search '%s'", filt), 1);
2613 for (base = ldap_conf.base; base != NULL; base = base->next) {
2614 DPRINTF(("searching from base '%s'", base->val), 1);
2615 if (ldap_conf.timeout > 0) {
2616 tv.tv_sec = ldap_conf.timeout;
2621 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE, filt,
2622 NULL, 0, NULL, NULL, tvp, 0, &result);
2623 if (rc != LDAP_SUCCESS) {
2624 DPRINTF(("nothing found for '%s'", filt), 1);
2627 lres->user_matches = true;
2629 /* Add the seach result to list of search results. */
2630 DPRINTF(("adding search result"), 1);
2631 sudo_ldap_result_add_search(lres, ld, result);
2632 LDAP_FOREACH(entry, ld, result) {
2634 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
2635 sudo_ldap_check_host(ld, entry)) {
2636 lres->host_matches = true;
2637 sudo_ldap_result_add_entry(lres, entry);
2640 DPRINTF(("result now has %d entries", lres->nentries), 1);
2645 /* Sort the entries by the sudoOrder attribute. */
2646 DPRINTF(("sorting remaining %d entries", lres->nentries), 1);
2647 qsort(lres->entries, lres->nentries, sizeof(lres->entries[0]),
2648 ldap_entry_compare);
2650 /* Store everything in the sudo_nss handle. */
2651 handle->result = lres;
2652 handle->username = estrdup(pw->pw_name);
2653 handle->grlist = user_group_list;
2655 debug_return_ptr(lres);
2659 * Shut down the LDAP connection.
2662 sudo_ldap_close(struct sudo_nss *nss)
2664 struct sudo_ldap_handle *handle = nss->handle;
2665 debug_decl(sudo_ldap_close, SUDO_DEBUG_LDAP)
2667 if (handle != NULL) {
2668 /* Free the result before unbinding; it may use the LDAP connection. */
2669 sudo_ldap_result_free_nss(nss);
2671 /* Unbind and close the LDAP connection. */
2672 if (handle->ld != NULL) {
2673 ldap_unbind_ext_s(handle->ld, NULL, NULL);
2677 /* Free the handle container. */
2681 debug_return_int(0);
2688 sudo_ldap_parse(struct sudo_nss *nss)
2695 * Create an ldap_result from an LDAP search result.
2697 * This function is currently not used anywhere, it is left here as
2698 * an example of how to use the cached searches.
2700 static struct ldap_result *
2701 sudo_ldap_result_from_search(LDAP *ldap, LDAPMessage *searchresult)
2704 * An ldap_result is built from several search results, which are
2705 * organized in a list. The head of the list is maintained in the
2706 * ldap_result structure, together with the wrappers that point
2707 * to individual entries, this has to be initialized first.
2709 struct ldap_result *result = sudo_ldap_result_alloc();
2712 * Build a new list node for the search result, this creates the
2715 struct ldap_search_list *last = sudo_ldap_result_add_search(result,
2716 ldap, searchresult);
2719 * Now add each entry in the search result to the array of of entries
2720 * in the ldap_result object.
2723 LDAP_FOREACH(entry, last->ldap, last->searchresult) {
2724 sudo_ldap_result_add_entry(result, entry);
2726 DPRINTF(("sudo_ldap_result_from_search: %d entries found",
2727 result->nentries), 2);