2 * Copyright (c) 2003-2009 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 */
37 # ifdef HAVE_STRINGS_H
40 #endif /* HAVE_STRING_H */
41 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
43 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
46 #endif /* HAVE_UNISTD_H */
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
57 #if defined(HAVE_LDAP_SSL_H)
58 # include <ldap_ssl.h>
59 #elif defined(HAVE_MPS_LDAP_SSL_H)
60 # include <mps/ldap_ssl.h>
62 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
63 # ifdef HAVE_SASL_SASL_H
64 # include <sasl/sasl.h>
68 # if HAVE_GSS_KRB5_CCACHE_NAME
69 # if defined(HAVE_GSSAPI_GSSAPI_KRB5_H)
70 # include <gssapi/gssapi.h>
71 # include <gssapi/gssapi_krb5.h>
72 # elif defined(HAVE_GSSAPI_GSSAPI_H)
73 # include <gssapi/gssapi.h>
85 __unused static const char rcsid[] = "$Sudo: ldap.c,v 1.108 2009/05/29 13:43:12 millert Exp $";
88 #ifndef LDAP_OPT_SUCCESS
89 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
93 # define LDAPS_PORT 636
96 #if defined(HAVE_LDAP_SASL_INTERACTIVE_BIND_S) && !defined(LDAP_SASL_QUIET)
97 # define LDAP_SASL_QUIET 0
100 #ifndef HAVE_LDAP_UNBIND_EXT_S
101 #define ldap_unbind_ext_s(a, b, c) ldap_unbind_s(a)
104 #ifndef HAVE_LDAP_SEARCH_EXT_S
105 #define ldap_search_ext_s(a, b, c, d, e, f, g, h, i, j, k) \
106 ldap_search_s(a, b, c, d, e, f, k)
109 #define LDAP_FOREACH(var, ld, res) \
110 for ((var) = ldap_first_entry((ld), (res)); \
112 (var) = ldap_next_entry((ld), (var)))
114 #define DPRINTF(args, level) if (ldap_conf.debug >= level) warningx args
120 #define SUDO_LDAP_SSL 1
121 #define SUDO_LDAP_STARTTLS 2
123 struct ldap_config_table {
124 const char *conf_str; /* config file string */
125 short type; /* CONF_BOOL, CONF_INT, CONF_STR */
126 short connected; /* connection-specific value? */
127 int opt_val; /* LDAP_OPT_* (or -1 for sudo internal) */
128 void *valp; /* pointer into ldap_conf */
131 /* ldap configuration structure */
132 static struct ldap_config {
150 char *tls_cacertfile;
152 char *tls_random_file;
153 char *tls_cipher_suite;
157 char *rootsasl_auth_id;
162 static struct ldap_config_table ldap_conf_table[] = {
163 { "sudoers_debug", CONF_INT, FALSE, -1, &ldap_conf.debug },
164 { "host", CONF_STR, FALSE, -1, &ldap_conf.host },
165 { "port", CONF_INT, FALSE, -1, &ldap_conf.port },
166 { "ssl", CONF_STR, FALSE, -1, &ldap_conf.ssl },
167 { "sslpath", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
168 { "uri", CONF_STR, FALSE, -1, &ldap_conf.uri },
169 #ifdef LDAP_OPT_DEBUG_LEVEL
170 { "debug", CONF_INT, FALSE, LDAP_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug },
172 #ifdef LDAP_OPT_PROTOCOL_VERSION
173 { "ldap_version", CONF_INT, TRUE, LDAP_OPT_PROTOCOL_VERSION,
174 &ldap_conf.version },
176 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
177 { "tls_checkpeer", CONF_BOOL, FALSE, LDAP_OPT_X_TLS_REQUIRE_CERT,
178 &ldap_conf.tls_checkpeer },
180 { "tls_checkpeer", CONF_BOOL, FALSE, -1, &ldap_conf.tls_checkpeer },
182 #ifdef LDAP_OPT_X_TLS_CACERTFILE
183 { "tls_cacertfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTFILE,
184 &ldap_conf.tls_cacertfile },
186 #ifdef LDAP_OPT_X_TLS_CACERTDIR
187 { "tls_cacertdir", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTDIR,
188 &ldap_conf.tls_cacertdir },
190 #ifdef LDAP_OPT_X_TLS_RANDOM_FILE
191 { "tls_randfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_RANDOM_FILE,
192 &ldap_conf.tls_random_file },
194 #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
195 { "tls_ciphers", CONF_STR, FALSE, LDAP_OPT_X_TLS_CIPHER_SUITE,
196 &ldap_conf.tls_cipher_suite },
198 #ifdef LDAP_OPT_X_TLS_CERTFILE
199 { "tls_cert", CONF_STR, FALSE, LDAP_OPT_X_TLS_CERTFILE,
200 &ldap_conf.tls_certfile },
202 { "tls_cert", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
204 #ifdef LDAP_OPT_X_TLS_KEYFILE
205 { "tls_key", CONF_STR, FALSE, LDAP_OPT_X_TLS_KEYFILE,
206 &ldap_conf.tls_keyfile },
208 { "tls_key", CONF_STR, FALSE, -1, &ldap_conf.tls_keyfile },
210 #ifdef LDAP_OPT_NETWORK_TIMEOUT
211 { "bind_timelimit", CONF_INT, TRUE, -1 /* needs timeval, set manually */,
212 &ldap_conf.bind_timelimit },
213 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
214 { "bind_timelimit", CONF_INT, TRUE, LDAP_X_OPT_CONNECT_TIMEOUT,
215 &ldap_conf.bind_timelimit },
217 { "timelimit", CONF_INT, TRUE, LDAP_OPT_TIMELIMIT, &ldap_conf.timelimit },
218 { "binddn", CONF_STR, FALSE, -1, &ldap_conf.binddn },
219 { "bindpw", CONF_STR, FALSE, -1, &ldap_conf.bindpw },
220 { "rootbinddn", CONF_STR, FALSE, -1, &ldap_conf.rootbinddn },
221 { "sudoers_base", CONF_STR, FALSE, -1, &ldap_conf.base },
222 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
223 { "use_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.use_sasl },
224 { "sasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.sasl_auth_id },
225 { "rootuse_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.rootuse_sasl },
226 { "rootsasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.rootsasl_auth_id },
227 # ifdef LDAP_OPT_X_SASL_SECPROPS
228 { "sasl_secprops", CONF_STR, TRUE, LDAP_OPT_X_SASL_SECPROPS,
229 &ldap_conf.sasl_secprops },
231 { "krb5_ccname", CONF_STR, FALSE, -1, &ldap_conf.krb5_ccname },
232 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
236 struct sudo_nss sudo_nss_ldap = {
244 sudo_ldap_display_cmnd,
245 sudo_ldap_display_defaults,
246 sudo_ldap_display_bound_defaults,
247 sudo_ldap_display_privs
250 #ifdef HAVE_LDAP_CREATE
252 * Rebuild the hosts list and include a specific port for each host.
253 * ldap_create() does not take a default port parameter so we must
254 * append one if we want something other than LDAP_PORT.
257 sudo_ldap_conf_add_ports()
260 char *host, *port, defport[13];
261 char hostbuf[LINE_MAX * 2];
264 if (snprintf(defport, sizeof(defport), ":%d", ldap_conf.port) >= sizeof(defport))
265 errorx(1, "sudo_ldap_conf_add_ports: port too large");
267 for ((host = strtok(ldap_conf.host, " \t")); host; (host = strtok(NULL, " \t"))) {
268 if (hostbuf[0] != '\0') {
269 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
273 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
275 /* Append port if there is not one already. */
276 if ((port = strrchr(host, ':')) == NULL || !isdigit(port[1])) {
277 if (strlcat(hostbuf, defport, sizeof(hostbuf)) >= sizeof(hostbuf))
282 free(ldap_conf.host);
283 ldap_conf.host = estrdup(hostbuf);
287 errorx(1, "sudo_ldap_conf_add_ports: out of space expanding hostbuf");
291 #ifndef HAVE_LDAP_INITIALIZE
293 * For each uri, convert to host:port pairs. For ldaps:// enable SSL
294 * Accepts: uris of the form ldap:/// or ldap://hostname:portnum/
295 * where the trailing slash is optional.
298 sudo_ldap_parse_uri(uri_list)
299 const char *uri_list;
301 char *buf, *uri, *host, *cp, *port;
302 char hostbuf[LINE_MAX];
303 int nldap = 0, nldaps = 0;
306 buf = estrdup(uri_list);
308 for ((uri = strtok(buf, " \t")); uri != NULL; (uri = strtok(NULL, " \t"))) {
309 if (strncasecmp(uri, "ldap://", 7) == 0) {
312 } else if (strncasecmp(uri, "ldaps://", 8) == 0) {
316 warningx("unsupported LDAP uri type: %s", uri);
320 /* trim optional trailing slash */
321 if ((cp = strrchr(host, '/')) != NULL && cp[1] == '\0') {
325 if (hostbuf[0] != '\0') {
326 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
331 host = "localhost"; /* no host specified, use localhost */
333 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
336 /* If using SSL and no port specified, add port 636 */
338 if ((port = strrchr(host, ':')) == NULL || !isdigit(port[1]))
339 if (strlcat(hostbuf, ":636", sizeof(hostbuf)) >= sizeof(hostbuf))
343 if (hostbuf[0] == '\0') {
344 warningx("invalid uri: %s", uri_list);
350 warningx("cannot mix ldap and ldaps URIs");
353 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
354 warningx("cannot mix ldaps and starttls");
357 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
360 free(ldap_conf.host);
361 ldap_conf.host = estrdup(hostbuf);
369 errorx(1, "sudo_ldap_parse_uri: out of space building hostbuf");
371 #endif /* HAVE_LDAP_INITIALIZE */
374 sudo_ldap_init(ldp, host, port)
380 int rc = LDAP_CONNECT_ERROR;
382 #ifdef HAVE_LDAPSSL_INIT
383 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
384 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
385 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
386 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
387 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
388 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
390 * Mozilla-derived SDKs have a bug starting with version 5.0
391 * where the path can no longer be a file name and must be a dir.
393 if (rc != LDAP_SUCCESS) {
395 if (ldap_conf.tls_certfile) {
396 cp = strrchr(ldap_conf.tls_certfile, '/');
397 if (cp != NULL && strncmp(cp + 1, "cert", 4) == 0)
400 if (ldap_conf.tls_keyfile) {
401 cp = strrchr(ldap_conf.tls_keyfile, '/');
402 if (cp != NULL && strncmp(cp + 1, "key", 3) == 0)
405 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
406 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
407 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
408 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
409 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
410 if (rc != LDAP_SUCCESS) {
411 warningx("unable to initialize SSL cert and key db: %s",
412 ldapssl_err2string(rc));
417 DPRINTF(("ldapssl_init(%s, %d, 1)", host, port), 2);
418 if ((ld = ldapssl_init(host, port, 1)) != NULL)
423 #ifdef HAVE_LDAP_CREATE
424 DPRINTF(("ldap_create()"), 2);
425 if ((rc = ldap_create(&ld)) != LDAP_SUCCESS)
427 DPRINTF(("ldap_set_option(LDAP_OPT_HOST_NAME, %s)", host), 2);
428 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, host);
430 DPRINTF(("ldap_init(%s, %d)", host, port), 2);
431 if ((ld = ldap_init(host, port)) != NULL)
442 * Walk through search results and return TRUE if we have a matching
443 * netgroup, else FALSE.
446 sudo_ldap_check_user_netgroup(ld, entry, user)
451 struct berval **bv, **p;
458 /* get the values from the entry */
459 bv = ldap_get_values_len(ld, entry, "sudoUser");
463 /* walk through values */
464 for (p = bv; *p != NULL && !ret; p++) {
467 if (netgr_matches(val, NULL, NULL, user))
469 DPRINTF(("ldap sudoUser netgroup '%s' ... %s", val,
470 ret ? "MATCH!" : "not"), 2);
473 ldap_value_free_len(bv); /* cleanup */
479 * Walk through search results and return TRUE if we have a
480 * host match, else FALSE.
483 sudo_ldap_check_host(ld, entry)
487 struct berval **bv, **p;
494 /* get the values from the entry */
495 bv = ldap_get_values_len(ld, entry, "sudoHost");
499 /* walk through values */
500 for (p = bv; *p != NULL && !ret; p++) {
502 /* match any or address or netgroup or hostname */
503 if (!strcmp(val, "ALL") || addr_matches(val) ||
504 netgr_matches(val, user_host, user_shost, NULL) ||
505 hostname_matches(user_shost, user_host, val))
507 DPRINTF(("ldap sudoHost '%s' ... %s", val,
508 ret ? "MATCH!" : "not"), 2);
511 ldap_value_free_len(bv); /* cleanup */
517 sudo_ldap_check_runas_user(ld, entry)
521 struct berval **bv, **p;
528 /* get the runas user from the entry */
529 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
531 bv = ldap_get_values_len(ld, entry, "sudoRunAs"); /* old style */
536 * if runas is not specified on the command line, the only information
537 * as to which user to run as is in the runas_default option. We should
538 * check to see if we have the local option present. Unfortunately we
539 * don't parse these options until after this routine says yes or no.
540 * The query has already returned, so we could peek at the attribute
541 * values here though.
543 * For now just require users to always use -u option unless its set
544 * in the global defaults. This behaviour is no different than the global
547 * Sigh - maybe add this feature later
551 * If there are no runas entries, match runas_default against
552 * what the user specified on the command line.
555 return(!strcasecmp(runas_pw->pw_name, def_runas_default));
557 /* walk through values returned, looking for a match */
558 for (p = bv; *p != NULL && !ret; p++) {
562 if (netgr_matches(val, NULL, NULL, runas_pw->pw_name))
566 if (usergr_matches(val, runas_pw->pw_name, runas_pw))
570 if (strcmp(val, "ALL") == 0) {
576 if (strcasecmp(val, runas_pw->pw_name) == 0)
580 DPRINTF(("ldap sudoRunAsUser '%s' ... %s", val,
581 ret ? "MATCH!" : "not"), 2);
584 ldap_value_free_len(bv); /* cleanup */
590 sudo_ldap_check_runas_group(ld, entry)
594 struct berval **bv, **p;
598 /* runas_gr is only set if the user specified the -g flag */
602 /* get the values from the entry */
603 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
607 /* walk through values returned, looking for a match */
608 for (p = bv; *p != NULL && !ret; p++) {
610 if (strcmp(val, "ALL") == 0 || group_matches(val, runas_gr))
612 DPRINTF(("ldap sudoRunAsGroup '%s' ... %s", val,
613 ret ? "MATCH!" : "not"), 2);
616 ldap_value_free_len(bv); /* cleanup */
622 * Walk through search results and return TRUE if we have a runas match,
623 * else FALSE. RunAs info is optional.
626 sudo_ldap_check_runas(ld, entry)
635 ret = sudo_ldap_check_runas_user(ld, entry) != FALSE &&
636 sudo_ldap_check_runas_group(ld, entry) != FALSE;
642 * Walk through search results and return TRUE if we have a command match,
643 * FALSE if disallowed and UNSPEC if not matched.
646 sudo_ldap_check_command(ld, entry, setenv_implied)
651 struct berval **bv, **p;
652 char *allowed_cmnd, *allowed_args, *val;
653 int foundbang, ret = UNSPEC;
658 bv = ldap_get_values_len(ld, entry, "sudoCommand");
662 for (p = bv; *p != NULL && ret != FALSE; p++) {
664 /* Match against ALL ? */
665 if (!strcmp(val, "ALL")) {
667 if (setenv_implied != NULL)
668 *setenv_implied = TRUE;
669 DPRINTF(("ldap sudoCommand '%s' ... MATCH!", val), 2);
673 /* check for !command */
676 allowed_cmnd = estrdup(1 + val); /* !command */
679 allowed_cmnd = estrdup(val); /* command */
682 /* split optional args away from command */
683 allowed_args = strchr(allowed_cmnd, ' ');
685 *allowed_args++ = '\0';
687 /* check the command like normal */
688 if (command_matches(allowed_cmnd, allowed_args)) {
690 * If allowed (no bang) set ret but keep on checking.
691 * If disallowed (bang), exit loop.
693 ret = foundbang ? FALSE : TRUE;
695 DPRINTF(("ldap sudoCommand '%s' ... %s", val,
696 ret == TRUE ? "MATCH!" : "not"), 2);
698 efree(allowed_cmnd); /* cleanup */
701 ldap_value_free_len(bv); /* more cleanup */
707 * Search for boolean "option" in sudoOption.
708 * Returns TRUE if found and allowed, FALSE if negated, else UNSPEC.
711 sudo_ldap_check_bool(ld, entry, option)
716 struct berval **bv, **p;
723 bv = ldap_get_values_len(ld, entry, "sudoOption");
727 /* walk through options */
728 for (p = bv; *p != NULL; p++) {
730 DPRINTF(("ldap sudoOption: '%s'", var), 2);
732 if ((ch = *var) == '!')
734 if (strcmp(var, option) == 0)
738 ldap_value_free_len(bv);
744 * Read sudoOption and modify the defaults as we go. This is used once
745 * from the cn=defaults entry and also once when a final sudoRole is matched.
748 sudo_ldap_parse_options(ld, entry)
752 struct berval **bv, **p;
758 bv = ldap_get_values_len(ld, entry, "sudoOption");
762 /* walk through options */
763 for (p = bv; *p != NULL; p++) {
764 var = estrdup((*p)->bv_val);
765 DPRINTF(("ldap sudoOption: '%s'", var), 2);
767 /* check for equals sign past first char */
768 val = strchr(var, '=');
770 *val++ = '\0'; /* split on = and truncate var */
771 op = *(val - 2); /* peek for += or -= cases */
772 if (op == '+' || op == '-') {
773 *(val - 2) = '\0'; /* found, remove extra char */
774 /* case var+=val or var-=val */
775 set_default(var, val, (int) op);
778 set_default(var, val, TRUE);
780 } else if (*var == '!') {
781 /* case !var Boolean False */
782 set_default(var + 1, NULL, FALSE);
784 /* case var Boolean True */
785 set_default(var, NULL, TRUE);
790 ldap_value_free_len(bv);
794 * builds together a filter to check against ldap
797 sudo_ldap_build_pass1(pw)
805 /* Start with (|(sudoUser=USERNAME)(sudoUser=ALL)) + NUL */
806 sz = 29 + strlen(pw->pw_name);
808 /* Add space for groups */
809 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL)
810 sz += 12 + strlen(grp->gr_name); /* primary group */
811 for (i = 0; i < user_ngroups; i++) {
812 if (user_groups[i] == pw->pw_gid)
814 if ((grp = sudo_getgrgid(user_groups[i])) != NULL)
815 sz += 12 + strlen(grp->gr_name); /* supplementary group */
819 /* Global OR + sudoUser=user_name filter */
820 (void) strlcpy(buf, "(|(sudoUser=", sz);
821 (void) strlcat(buf, pw->pw_name, sz);
822 (void) strlcat(buf, ")", sz);
824 /* Append primary group */
825 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
826 (void) strlcat(buf, "(sudoUser=%", sz);
827 (void) strlcat(buf, grp->gr_name, sz);
828 (void) strlcat(buf, ")", sz);
831 /* Append supplementary groups */
832 for (i = 0; i < user_ngroups; i++) {
833 if (user_groups[i] == pw->pw_gid)
835 if ((grp = sudo_getgrgid(user_groups[i])) != NULL) {
836 (void) strlcat(buf, "(sudoUser=%", sz);
837 (void) strlcat(buf, grp->gr_name, sz);
838 (void) strlcat(buf, ")", sz);
842 /* Add ALL to list and end the global OR */
843 if (strlcat(buf, "(sudoUser=ALL))", sz) >= sz)
844 errorx(1, "sudo_ldap_build_pass1 allocation mismatch");
850 * Map yes/true/on to TRUE, no/false/off to FALSE, else -1
859 if (strcasecmp(s, "yes") == 0)
864 if (strcasecmp(s, "true") == 0)
869 if (strcasecmp(s, "on") == 0)
871 if (strcasecmp(s, "off") == 0)
876 if (strcasecmp(s, "no") == 0)
881 if (strcasecmp(s, "false") == 0)
889 sudo_ldap_read_secret(path)
893 char buf[LINE_MAX], *cp;
895 if ((fp = fopen(_PATH_LDAP_SECRET, "r")) != NULL) {
896 if (fgets(buf, sizeof(buf), fp) != NULL) {
897 if ((cp = strchr(buf, '\n')) != NULL)
899 /* copy to bindpw and binddn */
900 efree(ldap_conf.bindpw);
901 ldap_conf.bindpw = estrdup(buf);
902 efree(ldap_conf.binddn);
903 ldap_conf.binddn = ldap_conf.rootbinddn;
904 ldap_conf.rootbinddn = NULL;
911 sudo_ldap_read_config()
914 char *cp, *keyword, *value;
915 struct ldap_config_table *cur;
918 ldap_conf.version = 3;
920 ldap_conf.tls_checkpeer = -1;
921 ldap_conf.timelimit = -1;
922 ldap_conf.bind_timelimit = -1;
923 ldap_conf.use_sasl = -1;
924 ldap_conf.rootuse_sasl = -1;
926 if ((fp = fopen(_PATH_LDAP_CONF, "r")) == NULL)
929 while ((cp = sudo_parseln(fp)) != NULL) {
931 continue; /* skip empty line */
933 /* split into keyword and value */
935 while (*cp && !isblank((unsigned char) *cp))
938 *cp++ = '\0'; /* terminate keyword */
940 /* skip whitespace before value */
941 while (isblank((unsigned char) *cp))
945 /* Look up keyword in config table. */
946 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
947 if (strcasecmp(keyword, cur->conf_str) == 0) {
950 *(int *)(cur->valp) = _atobool(value);
953 *(int *)(cur->valp) = atoi(value);
956 efree(*(char **)(cur->valp));
957 *(char **)(cur->valp) = estrdup(value);
967 ldap_conf.host = estrdup("localhost");
969 if (ldap_conf.bind_timelimit > 0)
970 ldap_conf.bind_timelimit *= 1000; /* convert to ms */
972 if (ldap_conf.debug > 1) {
973 fprintf(stderr, "LDAP Config Summary\n");
974 fprintf(stderr, "===================\n");
976 fprintf(stderr, "uri %s\n", ldap_conf.uri);
978 fprintf(stderr, "host %s\n", ldap_conf.host ?
979 ldap_conf.host : "(NONE)");
980 fprintf(stderr, "port %d\n", ldap_conf.port);
982 fprintf(stderr, "ldap_version %d\n", ldap_conf.version);
984 fprintf(stderr, "sudoers_base %s\n", ldap_conf.base ?
985 ldap_conf.base : "(NONE) <---Sudo will ignore ldap)");
986 fprintf(stderr, "binddn %s\n", ldap_conf.binddn ?
987 ldap_conf.binddn : "(anonymous)");
988 fprintf(stderr, "bindpw %s\n", ldap_conf.bindpw ?
989 ldap_conf.bindpw : "(anonymous)");
990 if (ldap_conf.bind_timelimit > 0)
991 fprintf(stderr, "bind_timelimit %d\n", ldap_conf.bind_timelimit);
992 if (ldap_conf.timelimit > 0)
993 fprintf(stderr, "timelimit %d\n", ldap_conf.timelimit);
994 fprintf(stderr, "ssl %s\n", ldap_conf.ssl ?
995 ldap_conf.ssl : "(no)");
996 if (ldap_conf.tls_checkpeer != -1)
997 fprintf(stderr, "tls_checkpeer %s\n", ldap_conf.tls_checkpeer ?
999 if (ldap_conf.tls_cacertfile != NULL)
1000 fprintf(stderr, "tls_cacertfile %s\n", ldap_conf.tls_cacertfile);
1001 if (ldap_conf.tls_cacertdir != NULL)
1002 fprintf(stderr, "tls_cacertdir %s\n", ldap_conf.tls_cacertdir);
1003 if (ldap_conf.tls_random_file != NULL)
1004 fprintf(stderr, "tls_random_file %s\n", ldap_conf.tls_random_file);
1005 if (ldap_conf.tls_cipher_suite != NULL)
1006 fprintf(stderr, "tls_cipher_suite %s\n", ldap_conf.tls_cipher_suite);
1007 if (ldap_conf.tls_certfile != NULL)
1008 fprintf(stderr, "tls_certfile %s\n", ldap_conf.tls_certfile);
1009 if (ldap_conf.tls_keyfile != NULL)
1010 fprintf(stderr, "tls_keyfile %s\n", ldap_conf.tls_keyfile);
1011 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1012 if (ldap_conf.use_sasl != -1) {
1013 fprintf(stderr, "use_sasl %s\n",
1014 ldap_conf.use_sasl ? "yes" : "no");
1015 fprintf(stderr, "sasl_auth_id %s\n", ldap_conf.sasl_auth_id ?
1016 ldap_conf.sasl_auth_id : "(NONE)");
1017 fprintf(stderr, "rootuse_sasl %d\n", ldap_conf.rootuse_sasl);
1018 fprintf(stderr, "rootsasl_auth_id %s\n", ldap_conf.rootsasl_auth_id ?
1019 ldap_conf.rootsasl_auth_id : "(NONE)");
1020 fprintf(stderr, "sasl_secprops %s\n", ldap_conf.sasl_secprops ?
1021 ldap_conf.sasl_secprops : "(NONE)");
1022 fprintf(stderr, "krb5_ccname %s\n", ldap_conf.krb5_ccname ?
1023 ldap_conf.krb5_ccname : "(NONE)");
1026 fprintf(stderr, "===================\n");
1028 if (!ldap_conf.base)
1029 return(FALSE); /* if no base is defined, ignore LDAP */
1032 * Interpret SSL option
1034 if (ldap_conf.ssl != NULL) {
1035 if (strcasecmp(ldap_conf.ssl, "start_tls") == 0)
1036 ldap_conf.ssl_mode = SUDO_LDAP_STARTTLS;
1037 else if (_atobool(ldap_conf.ssl))
1038 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
1041 #if defined(HAVE_LDAPSSL_SET_STRENGTH) && !defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
1042 if (ldap_conf.tls_checkpeer != -1) {
1043 ldapssl_set_strength(NULL,
1044 ldap_conf.tls_checkpeer ? LDAPSSL_AUTH_CERT : LDAPSSL_AUTH_WEAK);
1048 #ifndef HAVE_LDAP_INITIALIZE
1049 /* Convert uri list to host list if no ldap_initialize(). */
1050 if (ldap_conf.uri) {
1051 if (sudo_ldap_parse_uri(ldap_conf.uri) != 0)
1053 free(ldap_conf.uri);
1054 ldap_conf.uri = NULL;
1055 ldap_conf.port = LDAP_PORT;
1059 if (!ldap_conf.uri) {
1060 /* Use port 389 for plaintext LDAP and port 636 for SSL LDAP */
1061 if (ldap_conf.port < 0)
1063 ldap_conf.ssl_mode == SUDO_LDAP_SSL ? LDAPS_PORT : LDAP_PORT;
1065 #ifdef HAVE_LDAP_CREATE
1067 * Cannot specify port directly to ldap_create(), each host must
1068 * include :port to override the default.
1070 if (ldap_conf.port != LDAP_PORT)
1071 sudo_ldap_conf_add_ports();
1075 /* If rootbinddn set, read in /etc/ldap.secret if it exists. */
1076 if (ldap_conf.rootbinddn)
1077 sudo_ldap_read_secret(_PATH_LDAP_SECRET);
1079 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1081 * Make sure we can open the file specified by krb5_ccname.
1083 if (ldap_conf.krb5_ccname != NULL) {
1084 if (strncasecmp(ldap_conf.krb5_ccname, "FILE:", 5) == 0 ||
1085 strncasecmp(ldap_conf.krb5_ccname, "WRFILE:", 7) == 0) {
1086 value = ldap_conf.krb5_ccname +
1087 (ldap_conf.krb5_ccname[4] == ':' ? 5 : 7);
1088 if ((fp = fopen(value, "r")) != NULL) {
1089 DPRINTF(("using krb5 credential cache: %s", value), 1);
1092 /* Can't open it, just ignore the entry. */
1093 DPRINTF(("unable to open krb5 credential cache: %s", value), 1);
1094 efree(ldap_conf.krb5_ccname);
1095 ldap_conf.krb5_ccname = NULL;
1104 * Extract the dn from an entry and return the first rdn from it.
1107 sudo_ldap_get_first_rdn(ld, entry)
1111 #ifdef HAVE_LDAP_STR2DN
1112 char *dn, *rdn = NULL;
1115 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1117 if (ldap_str2dn(dn, &tmpDN, LDAP_DN_FORMAT_LDAP) == LDAP_SUCCESS) {
1118 ldap_rdn2str(tmpDN[0], &rdn, LDAP_DN_FORMAT_UFN);
1126 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1128 edn = ldap_explode_dn(dn, 1);
1130 return(edn ? edn[0] : NULL);
1135 * Fetch and display the global Options.
1138 sudo_ldap_display_defaults(nss, pw, lbuf)
1139 struct sudo_nss *nss;
1143 struct berval **bv, **p;
1144 LDAP *ld = (LDAP *) nss->handle;
1145 LDAPMessage *entry = NULL, *result = NULL;
1146 char *prefix = NULL;
1152 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE,
1153 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1154 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
1155 bv = ldap_get_values_len(ld, entry, "sudoOption");
1161 for (p = bv; *p != NULL; p++) {
1162 lbuf_append(lbuf, prefix, (*p)->bv_val, NULL);
1166 ldap_value_free_len(bv);
1170 ldap_msgfree(result);
1178 sudo_ldap_display_bound_defaults(nss, pw, lbuf)
1179 struct sudo_nss *nss;
1187 * Print a record in the short form, ala file sudoers.
1190 sudo_ldap_display_entry_short(ld, entry, lbuf)
1195 struct berval **bv, **p;
1198 lbuf_append(lbuf, " (", NULL);
1200 /* get the RunAsUser Values from the entry */
1201 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1203 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1205 for (p = bv; *p != NULL; p++) {
1207 lbuf_append(lbuf, ", ", NULL);
1208 lbuf_append(lbuf, (*p)->bv_val, NULL);
1210 ldap_value_free_len(bv);
1212 lbuf_append(lbuf, def_runas_default, NULL);
1214 /* get the RunAsGroup Values from the entry */
1215 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1217 lbuf_append(lbuf, " : ", NULL);
1218 for (p = bv; *p != NULL; p++) {
1220 lbuf_append(lbuf, ", ", NULL);
1221 lbuf_append(lbuf, (*p)->bv_val, NULL);
1223 ldap_value_free_len(bv);
1225 lbuf_append(lbuf, ") ", NULL);
1227 /* get the Option Values from the entry */
1228 bv = ldap_get_values_len(ld, entry, "sudoOption");
1232 for (p = bv; *p != NULL; p++) {
1237 if (strcmp(cp, "authenticate") == 0)
1238 tag = (*p)->bv_val[0] == '!' ?
1239 "NOPASSWD: " : "PASSWD: ";
1240 else if (strcmp(cp, "noexec") == 0)
1241 tag = (*p)->bv_val[0] == '!' ?
1242 "EXEC: " : "NOEXEC: ";
1243 else if (strcmp(cp, "setenv") == 0)
1244 tag = (*p)->bv_val[0] == '!' ?
1245 "NOSETENV: " : "SETENV: ";
1247 lbuf_append(lbuf, tag, NULL);
1248 /* XXX - ignores other options */
1250 ldap_value_free_len(bv);
1253 /* get the Command Values from the entry */
1254 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1256 for (p = bv; *p != NULL; p++) {
1258 lbuf_append(lbuf, ", ", NULL);
1259 lbuf_append(lbuf, (*p)->bv_val, NULL);
1262 ldap_value_free_len(bv);
1265 lbuf_print(lbuf); /* forces a newline */
1270 * Print a record in the long form.
1273 sudo_ldap_display_entry_long(ld, entry, lbuf)
1278 struct berval **bv, **p;
1282 /* extract the dn, only show the first rdn */
1283 rdn = sudo_ldap_get_first_rdn(ld, entry);
1284 lbuf_print(lbuf); /* force a newline */
1285 lbuf_append(lbuf, "LDAP Role: ", rdn ? rdn : "UNKNOWN", NULL);
1290 /* get the RunAsUser Values from the entry */
1291 lbuf_append(lbuf, " RunAsUsers: ", NULL);
1292 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1294 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1296 for (p = bv; *p != NULL; p++) {
1298 lbuf_append(lbuf, ", ", NULL);
1299 lbuf_append(lbuf, (*p)->bv_val, NULL);
1301 ldap_value_free_len(bv);
1303 lbuf_append(lbuf, def_runas_default, NULL);
1306 /* get the RunAsGroup Values from the entry */
1307 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1309 lbuf_append(lbuf, " RunAsGroups: ", NULL);
1310 for (p = bv; *p != NULL; p++) {
1312 lbuf_append(lbuf, ", ", NULL);
1313 lbuf_append(lbuf, (*p)->bv_val, NULL);
1315 ldap_value_free_len(bv);
1319 /* get the Option Values from the entry */
1320 bv = ldap_get_values_len(ld, entry, "sudoOption");
1322 lbuf_append(lbuf, " Options: ", NULL);
1323 for (p = bv; *p != NULL; p++) {
1325 lbuf_append(lbuf, ", ", NULL);
1326 lbuf_append(lbuf, (*p)->bv_val, NULL);
1328 ldap_value_free_len(bv);
1332 /* get the Command Values from the entry */
1333 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1335 lbuf_append(lbuf, " Commands:", NULL);
1337 for (p = bv; *p != NULL; p++) {
1338 lbuf_append(lbuf, "\t", (*p)->bv_val, NULL);
1342 ldap_value_free_len(bv);
1349 * Like sudo_ldap_lookup(), except we just print entries.
1352 sudo_ldap_display_privs(nss, pw, lbuf)
1353 struct sudo_nss *nss;
1357 LDAP *ld = (LDAP *) nss->handle;
1358 LDAPMessage *entry = NULL, *result = NULL;
1360 int rc, do_netgr, count = 0;
1366 * Okay - time to search for anything that matches this user
1367 * Lets limit it to only two queries of the LDAP server
1369 * The first pass will look by the username, groups, and
1370 * the keyword ALL. We will then inspect the results that
1371 * came back from the query. We don't need to inspect the
1372 * sudoUser in this pass since the LDAP server already scanned
1375 * The second pass will return all the entries that contain
1376 * user netgroups. Then we take the netgroups returned and
1377 * try to match them against the username.
1379 for (do_netgr = 0; do_netgr < 2; do_netgr++) {
1380 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1381 DPRINTF(("ldap search '%s'", filt), 1);
1382 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1383 NULL, 0, NULL, NULL, NULL, 0, &result);
1385 if (rc != LDAP_SUCCESS)
1386 continue; /* no entries for this pass */
1388 /* print each matching entry */
1389 LDAP_FOREACH(entry, ld, result) {
1391 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1392 sudo_ldap_check_host(ld, entry)) {
1395 count += sudo_ldap_display_entry_long(ld, entry, lbuf);
1397 count += sudo_ldap_display_entry_short(ld, entry, lbuf);
1400 ldap_msgfree(result);
1407 sudo_ldap_display_cmnd(nss, pw)
1408 struct sudo_nss *nss;
1411 LDAP *ld = (LDAP *) nss->handle;
1412 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
1413 char *filt; /* used to parse attributes */
1414 int rc, found, do_netgr; /* temp/final return values */
1420 * Okay - time to search for anything that matches this user
1421 * Lets limit it to only two queries of the LDAP server
1423 * The first pass will look by the username, groups, and
1424 * the keyword ALL. We will then inspect the results that
1425 * came back from the query. We don't need to inspect the
1426 * sudoUser in this pass since the LDAP server already scanned
1429 * The second pass will return all the entries that contain
1430 * user netgroups. Then we take the netgroups returned and
1431 * try to match them against the username.
1433 for (found = FALSE, do_netgr = 0; !found && do_netgr < 2; do_netgr++) {
1434 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1435 DPRINTF(("ldap search '%s'", filt), 1);
1436 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1437 NULL, 0, NULL, NULL, NULL, 0, &result);
1439 if (rc != LDAP_SUCCESS)
1440 continue; /* no entries for this pass */
1442 LDAP_FOREACH(entry, ld, result) {
1444 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1445 sudo_ldap_check_host(ld, entry) &&
1446 sudo_ldap_check_command(ld, entry, NULL) &&
1447 sudo_ldap_check_runas(ld, entry)) {
1453 ldap_msgfree(result);
1458 printf("%s%s%s\n", safe_cmnd ? safe_cmnd : user_cmnd,
1459 user_args ? " " : "", user_args ? user_args : "");
1463 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1465 sudo_ldap_sasl_interact(ld, flags, _auth_id, _interact)
1471 char *auth_id = (char *)_auth_id;
1472 sasl_interact_t *interact = (sasl_interact_t *)_interact;
1474 for (; interact->id != SASL_CB_LIST_END; interact++) {
1475 if (interact->id != SASL_CB_USER)
1476 return(LDAP_PARAM_ERROR);
1478 if (auth_id != NULL)
1479 interact->result = auth_id;
1480 else if (interact->defresult != NULL)
1481 interact->result = interact->defresult;
1483 interact->result = "";
1485 interact->len = strlen(interact->result);
1486 #if SASL_VERSION_MAJOR < 2
1487 interact->result = estrdup(interact->result);
1488 #endif /* SASL_VERSION_MAJOR < 2 */
1490 return(LDAP_SUCCESS);
1492 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1495 * Set LDAP options based on the config table.
1498 sudo_ldap_set_options(ld)
1501 struct ldap_config_table *cur;
1504 /* Set ber options */
1505 #ifdef LBER_OPT_DEBUG_LEVEL
1506 if (ldap_conf.ldap_debug)
1507 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug);
1510 /* Set simple LDAP options */
1511 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
1516 if (cur->opt_val == -1)
1519 conn = cur->connected ? ld : NULL;
1520 switch (cur->type) {
1523 ival = *(int *)(cur->valp);
1525 rc = ldap_set_option(conn, cur->opt_val, &ival);
1526 if (rc != LDAP_OPT_SUCCESS) {
1527 warningx("ldap_set_option: %s -> %d: %s",
1528 cur->conf_str, ival, ldap_err2string(rc));
1531 DPRINTF(("ldap_set_option: %s -> %d", cur->conf_str, ival), 1);
1535 sval = *(char **)(cur->valp);
1537 rc = ldap_set_option(conn, cur->opt_val, sval);
1538 if (rc != LDAP_OPT_SUCCESS) {
1539 warningx("ldap_set_option: %s -> %s: %s",
1540 cur->conf_str, sval, ldap_err2string(rc));
1543 DPRINTF(("ldap_set_option: %s -> %s", cur->conf_str, sval), 1);
1549 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1550 /* Convert bind_timelimit to a timeval */
1551 if (ldap_conf.bind_timelimit > 0) {
1553 tv.tv_sec = ldap_conf.bind_timelimit / 1000;
1555 rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
1556 if (rc != LDAP_OPT_SUCCESS) {
1557 warningx("ldap_set_option(NETWORK_TIMEOUT, %ld): %s",
1558 (long)tv.tv_sec, ldap_err2string(rc));
1561 DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)\n",
1562 (long)tv.tv_sec), 1);
1566 #if defined(LDAP_OPT_X_TLS) && !defined(HAVE_LDAPSSL_INIT)
1567 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
1568 int val = LDAP_OPT_X_TLS_HARD;
1569 rc = ldap_set_option(ld, LDAP_OPT_X_TLS, &val);
1570 if (rc != LDAP_SUCCESS) {
1571 warningx("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD): %s",
1572 ldap_err2string(rc));
1575 DPRINTF(("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD)\n"), 1);
1582 * Connect to the LDAP server specified by ld
1585 sudo_ldap_bind_s(ld)
1589 const char *old_ccname = user_ccname;
1590 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1591 unsigned int status;
1594 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1595 if (ldap_conf.rootuse_sasl == TRUE ||
1596 (ldap_conf.rootuse_sasl != FALSE && ldap_conf.use_sasl == TRUE)) {
1597 void *auth_id = ldap_conf.rootsasl_auth_id ?
1598 ldap_conf.rootsasl_auth_id : ldap_conf.sasl_auth_id;
1600 if (ldap_conf.krb5_ccname != NULL) {
1601 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1602 if (gss_krb5_ccache_name(&status, ldap_conf.krb5_ccname, &old_ccname)
1603 != GSS_S_COMPLETE) {
1605 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1608 setenv("KRB5CCNAME", ldap_conf.krb5_ccname, TRUE);
1611 rc = ldap_sasl_interactive_bind_s(ld, ldap_conf.binddn, "GSSAPI",
1612 NULL, NULL, LDAP_SASL_QUIET, sudo_ldap_sasl_interact, auth_id);
1613 if (ldap_conf.krb5_ccname != NULL) {
1614 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1615 if (gss_krb5_ccache_name(&status, old_ccname, NULL) != GSS_S_COMPLETE)
1616 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1618 if (old_ccname != NULL)
1619 setenv("KRB5CCNAME", old_ccname, TRUE);
1621 unsetenv("KRB5CCNAME");
1624 if (rc != LDAP_SUCCESS) {
1625 warningx("ldap_sasl_interactive_bind_s(): %s", ldap_err2string(rc));
1628 DPRINTF(("ldap_sasl_interactive_bind_s() ok"), 1);
1630 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1631 #ifdef HAVE_LDAP_SASL_BIND_S
1635 bv.bv_val = ldap_conf.bindpw ? ldap_conf.bindpw : "";
1636 bv.bv_len = strlen(bv.bv_val);
1638 rc = ldap_sasl_bind_s(ld, ldap_conf.binddn, LDAP_SASL_SIMPLE, &bv,
1640 if (rc != LDAP_SUCCESS) {
1641 warningx("ldap_sasl_bind_s(): %s", ldap_err2string(rc));
1644 DPRINTF(("ldap_sasl_bind_s() ok"), 1);
1648 rc = ldap_simple_bind_s(ld, ldap_conf.binddn, ldap_conf.bindpw);
1649 if (rc != LDAP_SUCCESS) {
1650 warningx("ldap_simple_bind_s(): %s", ldap_err2string(rc));
1653 DPRINTF(("ldap_simple_bind_s() ok"), 1);
1660 * Open a connection to the LDAP server.
1661 * Returns 0 on success and non-zero on failure.
1665 struct sudo_nss *nss;
1668 int rc, ldapnoinit = FALSE;
1670 if (!sudo_ldap_read_config())
1673 /* Prevent reading of user ldaprc and system defaults. */
1674 if (getenv("LDAPNOINIT") == NULL) {
1676 setenv("LDAPNOINIT", "1", TRUE);
1679 /* Connect to LDAP server */
1680 #ifdef HAVE_LDAP_INITIALIZE
1681 if (ldap_conf.uri != NULL) {
1682 DPRINTF(("ldap_initialize(ld, %s)", ldap_conf.uri), 2);
1683 rc = ldap_initialize(&ld, ldap_conf.uri);
1686 rc = sudo_ldap_init(&ld, ldap_conf.host, ldap_conf.port);
1687 if (rc != LDAP_SUCCESS) {
1688 warningx("unable to initialize LDAP: %s", ldap_err2string(rc));
1693 unsetenv("LDAPNOINIT");
1695 /* Set LDAP options */
1696 if (sudo_ldap_set_options(ld) < 0)
1699 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
1700 #if defined(HAVE_LDAP_START_TLS_S)
1701 rc = ldap_start_tls_s(ld, NULL, NULL);
1702 if (rc != LDAP_SUCCESS) {
1703 warningx("ldap_start_tls_s(): %s", ldap_err2string(rc));
1706 DPRINTF(("ldap_start_tls_s() ok"), 1);
1707 #elif defined(HAVE_LDAP_SSL_CLIENT_INIT) && defined(HAVE_LDAP_START_TLS_S_NP)
1708 if (ldap_ssl_client_init(NULL, NULL, 0, &rc) != LDAP_SUCCESS) {
1709 warningx("ldap_ssl_client_init(): %s", ldap_err2string(rc));
1712 rc = ldap_start_tls_s_np(ld, NULL);
1713 if (rc != LDAP_SUCCESS) {
1714 warningx("ldap_start_tls_s_np(): %s", ldap_err2string(rc));
1717 DPRINTF(("ldap_start_tls_s_np() ok"), 1);
1719 warningx("start_tls specified but LDAP libs do not support ldap_start_tls_s() or ldap_start_tls_s_np()");
1720 #endif /* !HAVE_LDAP_START_TLS_S && !HAVE_LDAP_START_TLS_S_NP */
1723 /* Actually connect */
1724 if (sudo_ldap_bind_s(ld) != 0)
1732 sudo_ldap_setdefs(nss)
1733 struct sudo_nss *nss;
1735 LDAP *ld = (LDAP *) nss->handle;
1736 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
1737 int rc; /* temp return value */
1742 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE,
1743 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1744 if (rc == 0 && (entry = ldap_first_entry(ld, result))) {
1745 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1746 sudo_ldap_parse_options(ld, entry);
1748 DPRINTF(("no default options found!"), 1);
1751 ldap_msgfree(result);
1757 * like sudoers_lookup() - only LDAP style
1760 sudo_ldap_lookup(nss, ret, pwflag)
1761 struct sudo_nss *nss;
1765 LDAP *ld = (LDAP *) nss->handle;
1766 LDAPMessage *entry = NULL, *result = NULL;
1768 int do_netgr, rc, matched;
1770 int ldap_user_matches = FALSE, ldap_host_matches = FALSE;
1771 struct passwd *pw = list_pw ? list_pw : sudo_user.pw;
1777 int doauth = UNSPEC;
1778 enum def_tupple pwcheck =
1779 (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
1781 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1782 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1783 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1784 NULL, 0, NULL, NULL, NULL, 0, &result);
1786 if (rc != LDAP_SUCCESS)
1789 LDAP_FOREACH(entry, ld, result) {
1790 /* only verify netgroup matches in pass 2 */
1791 if (do_netgr && !sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name))
1794 ldap_user_matches = TRUE;
1795 if (sudo_ldap_check_host(ld, entry)) {
1796 ldap_host_matches = TRUE;
1797 if ((pwcheck == any && doauth != FALSE) ||
1798 (pwcheck == all && doauth == FALSE))
1799 doauth = sudo_ldap_check_bool(ld, entry, "authenticate");
1800 /* Only check the command when listing another user. */
1801 if (user_uid == 0 || list_pw == NULL ||
1802 user_uid == list_pw->pw_uid ||
1803 sudo_ldap_check_command(ld, entry, NULL)) {
1805 break; /* end foreach */
1809 ldap_msgfree(result);
1812 if (matched || user_uid == 0) {
1813 SET(ret, VALIDATE_OK);
1814 CLR(ret, VALIDATE_NOT_OK);
1815 if (def_authenticate) {
1818 SET(ret, FLAG_CHECK_USER);
1822 if (doauth == FALSE)
1823 def_authenticate = FALSE;
1826 def_authenticate = FALSE;
1837 * Okay - time to search for anything that matches this user
1838 * Lets limit it to only two queries of the LDAP server
1840 * The first pass will look by the username, groups, and
1841 * the keyword ALL. We will then inspect the results that
1842 * came back from the query. We don't need to inspect the
1843 * sudoUser in this pass since the LDAP server already scanned
1846 * The second pass will return all the entries that contain
1847 * user netgroups. Then we take the netgroups returned and
1848 * try to match them against the username.
1850 setenv_implied = FALSE;
1851 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1852 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1853 DPRINTF(("ldap search '%s'", filt), 1);
1854 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1855 NULL, 0, NULL, NULL, NULL, 0, &result);
1856 if (rc != LDAP_SUCCESS)
1857 DPRINTF(("nothing found for '%s'", filt), 1);
1860 /* parse each entry returned from this most recent search */
1861 if (rc == LDAP_SUCCESS) {
1862 LDAP_FOREACH(entry, ld, result) {
1863 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1865 /* first verify user netgroup matches - only if in pass 2 */
1866 (!do_netgr || sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1867 /* remember that user matched */
1868 (ldap_user_matches = TRUE) &&
1869 /* verify host match */
1870 sudo_ldap_check_host(ld, entry) &&
1871 /* remember that host matched */
1872 (ldap_host_matches = TRUE) &&
1873 /* verify runas match */
1874 sudo_ldap_check_runas(ld, entry) &&
1875 /* verify command match */
1876 (rc = sudo_ldap_check_command(ld, entry, &setenv_implied)) != UNSPEC
1878 /* We have a match! */
1879 DPRINTF(("Command %sallowed", rc == TRUE ? "" : "NOT "), 1);
1882 /* pick up any options */
1885 sudo_ldap_parse_options(ld, entry);
1887 /* Set role and type if not specified on command line. */
1888 if (user_role == NULL)
1889 user_role = def_role;
1890 if (user_type == NULL)
1891 user_type = def_type;
1892 #endif /* HAVE_SELINUX */
1893 /* make sure we don't reenter loop */
1894 SET(ret, VALIDATE_OK);
1895 CLR(ret, VALIDATE_NOT_OK);
1897 SET(ret, VALIDATE_NOT_OK);
1898 CLR(ret, VALIDATE_OK);
1900 /* break from inside for loop */
1904 ldap_msgfree(result);
1910 DPRINTF(("user_matches=%d", ldap_user_matches), 1);
1911 DPRINTF(("host_matches=%d", ldap_host_matches), 1);
1913 if (!ISSET(ret, VALIDATE_OK)) {
1914 /* we do not have a match */
1915 if (pwflag && list_pw == NULL)
1916 SET(ret, FLAG_NO_CHECK);
1918 if (ldap_user_matches)
1919 CLR(ret, FLAG_NO_USER);
1920 if (ldap_host_matches)
1921 CLR(ret, FLAG_NO_HOST);
1922 DPRINTF(("sudo_ldap_lookup(%d)=0x%02x", pwflag, ret), 1);
1928 * shut down LDAP connection
1931 sudo_ldap_close(nss)
1932 struct sudo_nss *nss;
1934 if (nss->handle != NULL) {
1935 ldap_unbind_ext_s((LDAP *) nss->handle, NULL, NULL);
1945 sudo_ldap_parse(nss)
1946 struct sudo_nss *nss;