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>
84 #ifndef LDAP_OPT_SUCCESS
85 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
89 # define LDAPS_PORT 636
92 #if defined(HAVE_LDAP_SASL_INTERACTIVE_BIND_S) && !defined(LDAP_SASL_QUIET)
93 # define LDAP_SASL_QUIET 0
96 #ifndef HAVE_LDAP_UNBIND_EXT_S
97 #define ldap_unbind_ext_s(a, b, c) ldap_unbind_s(a)
100 #ifndef HAVE_LDAP_SEARCH_EXT_S
101 #define ldap_search_ext_s(a, b, c, d, e, f, g, h, i, j, k) \
102 ldap_search_s(a, b, c, d, e, f, k)
105 #define LDAP_FOREACH(var, ld, res) \
106 for ((var) = ldap_first_entry((ld), (res)); \
108 (var) = ldap_next_entry((ld), (var)))
110 #define DPRINTF(args, level) if (ldap_conf.debug >= level) warningx args
116 #define SUDO_LDAP_SSL 1
117 #define SUDO_LDAP_STARTTLS 2
119 struct ldap_config_table {
120 const char *conf_str; /* config file string */
121 short type; /* CONF_BOOL, CONF_INT, CONF_STR */
122 short connected; /* connection-specific value? */
123 int opt_val; /* LDAP_OPT_* (or -1 for sudo internal) */
124 void *valp; /* pointer into ldap_conf */
127 /* ldap configuration structure */
128 static struct ldap_config {
146 char *tls_cacertfile;
148 char *tls_random_file;
149 char *tls_cipher_suite;
153 char *rootsasl_auth_id;
158 static struct ldap_config_table ldap_conf_table[] = {
159 { "sudoers_debug", CONF_INT, FALSE, -1, &ldap_conf.debug },
160 { "host", CONF_STR, FALSE, -1, &ldap_conf.host },
161 { "port", CONF_INT, FALSE, -1, &ldap_conf.port },
162 { "ssl", CONF_STR, FALSE, -1, &ldap_conf.ssl },
163 { "sslpath", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
164 { "uri", CONF_STR, FALSE, -1, &ldap_conf.uri },
165 #ifdef LDAP_OPT_DEBUG_LEVEL
166 { "debug", CONF_INT, FALSE, LDAP_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug },
168 #ifdef LDAP_OPT_PROTOCOL_VERSION
169 { "ldap_version", CONF_INT, TRUE, LDAP_OPT_PROTOCOL_VERSION,
170 &ldap_conf.version },
172 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
173 { "tls_checkpeer", CONF_BOOL, FALSE, LDAP_OPT_X_TLS_REQUIRE_CERT,
174 &ldap_conf.tls_checkpeer },
176 { "tls_checkpeer", CONF_BOOL, FALSE, -1, &ldap_conf.tls_checkpeer },
178 #ifdef LDAP_OPT_X_TLS_CACERTFILE
179 { "tls_cacertfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTFILE,
180 &ldap_conf.tls_cacertfile },
182 #ifdef LDAP_OPT_X_TLS_CACERTDIR
183 { "tls_cacertdir", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTDIR,
184 &ldap_conf.tls_cacertdir },
186 #ifdef LDAP_OPT_X_TLS_RANDOM_FILE
187 { "tls_randfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_RANDOM_FILE,
188 &ldap_conf.tls_random_file },
190 #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
191 { "tls_ciphers", CONF_STR, FALSE, LDAP_OPT_X_TLS_CIPHER_SUITE,
192 &ldap_conf.tls_cipher_suite },
194 #ifdef LDAP_OPT_X_TLS_CERTFILE
195 { "tls_cert", CONF_STR, FALSE, LDAP_OPT_X_TLS_CERTFILE,
196 &ldap_conf.tls_certfile },
198 { "tls_cert", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
200 #ifdef LDAP_OPT_X_TLS_KEYFILE
201 { "tls_key", CONF_STR, FALSE, LDAP_OPT_X_TLS_KEYFILE,
202 &ldap_conf.tls_keyfile },
204 { "tls_key", CONF_STR, FALSE, -1, &ldap_conf.tls_keyfile },
206 #ifdef LDAP_OPT_NETWORK_TIMEOUT
207 { "bind_timelimit", CONF_INT, TRUE, -1 /* needs timeval, set manually */,
208 &ldap_conf.bind_timelimit },
209 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
210 { "bind_timelimit", CONF_INT, TRUE, LDAP_X_OPT_CONNECT_TIMEOUT,
211 &ldap_conf.bind_timelimit },
213 { "timelimit", CONF_INT, TRUE, LDAP_OPT_TIMELIMIT, &ldap_conf.timelimit },
214 { "binddn", CONF_STR, FALSE, -1, &ldap_conf.binddn },
215 { "bindpw", CONF_STR, FALSE, -1, &ldap_conf.bindpw },
216 { "rootbinddn", CONF_STR, FALSE, -1, &ldap_conf.rootbinddn },
217 { "sudoers_base", CONF_STR, FALSE, -1, &ldap_conf.base },
218 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
219 { "use_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.use_sasl },
220 { "sasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.sasl_auth_id },
221 { "rootuse_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.rootuse_sasl },
222 { "rootsasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.rootsasl_auth_id },
223 # ifdef LDAP_OPT_X_SASL_SECPROPS
224 { "sasl_secprops", CONF_STR, TRUE, LDAP_OPT_X_SASL_SECPROPS,
225 &ldap_conf.sasl_secprops },
227 { "krb5_ccname", CONF_STR, FALSE, -1, &ldap_conf.krb5_ccname },
228 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
232 struct sudo_nss sudo_nss_ldap = {
240 sudo_ldap_display_cmnd,
241 sudo_ldap_display_defaults,
242 sudo_ldap_display_bound_defaults,
243 sudo_ldap_display_privs
246 #ifdef HAVE_LDAP_CREATE
248 * Rebuild the hosts list and include a specific port for each host.
249 * ldap_create() does not take a default port parameter so we must
250 * append one if we want something other than LDAP_PORT.
253 sudo_ldap_conf_add_ports()
256 char *host, *port, defport[13];
257 char hostbuf[LINE_MAX * 2];
260 if (snprintf(defport, sizeof(defport), ":%d", ldap_conf.port) >= sizeof(defport))
261 errorx(1, "sudo_ldap_conf_add_ports: port too large");
263 for ((host = strtok(ldap_conf.host, " \t")); host; (host = strtok(NULL, " \t"))) {
264 if (hostbuf[0] != '\0') {
265 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
269 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
271 /* Append port if there is not one already. */
272 if ((port = strrchr(host, ':')) == NULL || !isdigit(port[1])) {
273 if (strlcat(hostbuf, defport, sizeof(hostbuf)) >= sizeof(hostbuf))
278 free(ldap_conf.host);
279 ldap_conf.host = estrdup(hostbuf);
283 errorx(1, "sudo_ldap_conf_add_ports: out of space expanding hostbuf");
287 #ifndef HAVE_LDAP_INITIALIZE
289 * For each uri, convert to host:port pairs. For ldaps:// enable SSL
290 * Accepts: uris of the form ldap:/// or ldap://hostname:portnum/
291 * where the trailing slash is optional.
294 sudo_ldap_parse_uri(uri_list)
295 const char *uri_list;
297 char *buf, *uri, *host, *cp, *port;
298 char hostbuf[LINE_MAX];
299 int nldap = 0, nldaps = 0;
302 buf = estrdup(uri_list);
304 for ((uri = strtok(buf, " \t")); uri != NULL; (uri = strtok(NULL, " \t"))) {
305 if (strncasecmp(uri, "ldap://", 7) == 0) {
308 } else if (strncasecmp(uri, "ldaps://", 8) == 0) {
312 warningx("unsupported LDAP uri type: %s", uri);
316 /* trim optional trailing slash */
317 if ((cp = strrchr(host, '/')) != NULL && cp[1] == '\0') {
321 if (hostbuf[0] != '\0') {
322 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
327 host = "localhost"; /* no host specified, use localhost */
329 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
332 /* If using SSL and no port specified, add port 636 */
334 if ((port = strrchr(host, ':')) == NULL || !isdigit(port[1]))
335 if (strlcat(hostbuf, ":636", sizeof(hostbuf)) >= sizeof(hostbuf))
339 if (hostbuf[0] == '\0') {
340 warningx("invalid uri: %s", uri_list);
346 warningx("cannot mix ldap and ldaps URIs");
349 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
350 warningx("cannot mix ldaps and starttls");
353 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
356 free(ldap_conf.host);
357 ldap_conf.host = estrdup(hostbuf);
365 errorx(1, "sudo_ldap_parse_uri: out of space building hostbuf");
367 #endif /* HAVE_LDAP_INITIALIZE */
370 sudo_ldap_init(ldp, host, port)
376 int rc = LDAP_CONNECT_ERROR;
378 #ifdef HAVE_LDAPSSL_INIT
379 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
380 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
381 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
382 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
383 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
384 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
386 * Mozilla-derived SDKs have a bug starting with version 5.0
387 * where the path can no longer be a file name and must be a dir.
389 if (rc != LDAP_SUCCESS) {
391 if (ldap_conf.tls_certfile) {
392 cp = strrchr(ldap_conf.tls_certfile, '/');
393 if (cp != NULL && strncmp(cp + 1, "cert", 4) == 0)
396 if (ldap_conf.tls_keyfile) {
397 cp = strrchr(ldap_conf.tls_keyfile, '/');
398 if (cp != NULL && strncmp(cp + 1, "key", 3) == 0)
401 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
402 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
403 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
404 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
405 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
406 if (rc != LDAP_SUCCESS) {
407 warningx("unable to initialize SSL cert and key db: %s",
408 ldapssl_err2string(rc));
413 DPRINTF(("ldapssl_init(%s, %d, 1)", host, port), 2);
414 if ((ld = ldapssl_init(host, port, 1)) != NULL)
419 #ifdef HAVE_LDAP_CREATE
420 DPRINTF(("ldap_create()"), 2);
421 if ((rc = ldap_create(&ld)) != LDAP_SUCCESS)
423 DPRINTF(("ldap_set_option(LDAP_OPT_HOST_NAME, %s)", host), 2);
424 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, host);
426 DPRINTF(("ldap_init(%s, %d)", host, port), 2);
427 if ((ld = ldap_init(host, port)) != NULL)
438 * Walk through search results and return TRUE if we have a matching
439 * netgroup, else FALSE.
442 sudo_ldap_check_user_netgroup(ld, entry, user)
447 struct berval **bv, **p;
454 /* get the values from the entry */
455 bv = ldap_get_values_len(ld, entry, "sudoUser");
459 /* walk through values */
460 for (p = bv; *p != NULL && !ret; p++) {
463 if (netgr_matches(val, NULL, NULL, user))
465 DPRINTF(("ldap sudoUser netgroup '%s' ... %s", val,
466 ret ? "MATCH!" : "not"), 2);
469 ldap_value_free_len(bv); /* cleanup */
475 * Walk through search results and return TRUE if we have a
476 * host match, else FALSE.
479 sudo_ldap_check_host(ld, entry)
483 struct berval **bv, **p;
490 /* get the values from the entry */
491 bv = ldap_get_values_len(ld, entry, "sudoHost");
495 /* walk through values */
496 for (p = bv; *p != NULL && !ret; p++) {
498 /* match any or address or netgroup or hostname */
499 if (!strcmp(val, "ALL") || addr_matches(val) ||
500 netgr_matches(val, user_host, user_shost, NULL) ||
501 hostname_matches(user_shost, user_host, val))
503 DPRINTF(("ldap sudoHost '%s' ... %s", val,
504 ret ? "MATCH!" : "not"), 2);
507 ldap_value_free_len(bv); /* cleanup */
513 sudo_ldap_check_runas_user(ld, entry)
517 struct berval **bv, **p;
524 /* get the runas user from the entry */
525 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
527 bv = ldap_get_values_len(ld, entry, "sudoRunAs"); /* old style */
532 * if runas is not specified on the command line, the only information
533 * as to which user to run as is in the runas_default option. We should
534 * check to see if we have the local option present. Unfortunately we
535 * don't parse these options until after this routine says yes or no.
536 * The query has already returned, so we could peek at the attribute
537 * values here though.
539 * For now just require users to always use -u option unless its set
540 * in the global defaults. This behaviour is no different than the global
543 * Sigh - maybe add this feature later
547 * If there are no runas entries, match runas_default against
548 * what the user specified on the command line.
551 return(!strcasecmp(runas_pw->pw_name, def_runas_default));
553 /* walk through values returned, looking for a match */
554 for (p = bv; *p != NULL && !ret; p++) {
558 if (netgr_matches(val, NULL, NULL, runas_pw->pw_name))
562 if (usergr_matches(val, runas_pw->pw_name, runas_pw))
566 if (strcmp(val, "ALL") == 0) {
572 if (strcasecmp(val, runas_pw->pw_name) == 0)
576 DPRINTF(("ldap sudoRunAsUser '%s' ... %s", val,
577 ret ? "MATCH!" : "not"), 2);
580 ldap_value_free_len(bv); /* cleanup */
586 sudo_ldap_check_runas_group(ld, entry)
590 struct berval **bv, **p;
594 /* runas_gr is only set if the user specified the -g flag */
598 /* get the values from the entry */
599 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
603 /* walk through values returned, looking for a match */
604 for (p = bv; *p != NULL && !ret; p++) {
606 if (strcmp(val, "ALL") == 0 || group_matches(val, runas_gr))
608 DPRINTF(("ldap sudoRunAsGroup '%s' ... %s", val,
609 ret ? "MATCH!" : "not"), 2);
612 ldap_value_free_len(bv); /* cleanup */
618 * Walk through search results and return TRUE if we have a runas match,
619 * else FALSE. RunAs info is optional.
622 sudo_ldap_check_runas(ld, entry)
631 ret = sudo_ldap_check_runas_user(ld, entry) != FALSE &&
632 sudo_ldap_check_runas_group(ld, entry) != FALSE;
638 * Walk through search results and return TRUE if we have a command match,
639 * FALSE if disallowed and UNSPEC if not matched.
642 sudo_ldap_check_command(ld, entry, setenv_implied)
647 struct berval **bv, **p;
648 char *allowed_cmnd, *allowed_args, *val;
649 int foundbang, ret = UNSPEC;
654 bv = ldap_get_values_len(ld, entry, "sudoCommand");
658 for (p = bv; *p != NULL && ret != FALSE; p++) {
660 /* Match against ALL ? */
661 if (!strcmp(val, "ALL")) {
663 if (setenv_implied != NULL)
664 *setenv_implied = TRUE;
665 DPRINTF(("ldap sudoCommand '%s' ... MATCH!", val), 2);
669 /* check for !command */
672 allowed_cmnd = estrdup(1 + val); /* !command */
675 allowed_cmnd = estrdup(val); /* command */
678 /* split optional args away from command */
679 allowed_args = strchr(allowed_cmnd, ' ');
681 *allowed_args++ = '\0';
683 /* check the command like normal */
684 if (command_matches(allowed_cmnd, allowed_args)) {
686 * If allowed (no bang) set ret but keep on checking.
687 * If disallowed (bang), exit loop.
689 ret = foundbang ? FALSE : TRUE;
691 DPRINTF(("ldap sudoCommand '%s' ... %s", val,
692 ret == TRUE ? "MATCH!" : "not"), 2);
694 efree(allowed_cmnd); /* cleanup */
697 ldap_value_free_len(bv); /* more cleanup */
703 * Search for boolean "option" in sudoOption.
704 * Returns TRUE if found and allowed, FALSE if negated, else UNSPEC.
707 sudo_ldap_check_bool(ld, entry, option)
712 struct berval **bv, **p;
719 bv = ldap_get_values_len(ld, entry, "sudoOption");
723 /* walk through options */
724 for (p = bv; *p != NULL; p++) {
726 DPRINTF(("ldap sudoOption: '%s'", var), 2);
728 if ((ch = *var) == '!')
730 if (strcmp(var, option) == 0)
734 ldap_value_free_len(bv);
740 * Read sudoOption and modify the defaults as we go. This is used once
741 * from the cn=defaults entry and also once when a final sudoRole is matched.
744 sudo_ldap_parse_options(ld, entry)
748 struct berval **bv, **p;
754 bv = ldap_get_values_len(ld, entry, "sudoOption");
758 /* walk through options */
759 for (p = bv; *p != NULL; p++) {
760 var = estrdup((*p)->bv_val);
761 DPRINTF(("ldap sudoOption: '%s'", var), 2);
763 /* check for equals sign past first char */
764 val = strchr(var, '=');
766 *val++ = '\0'; /* split on = and truncate var */
767 op = *(val - 2); /* peek for += or -= cases */
768 if (op == '+' || op == '-') {
769 *(val - 2) = '\0'; /* found, remove extra char */
770 /* case var+=val or var-=val */
771 set_default(var, val, (int) op);
774 set_default(var, val, TRUE);
776 } else if (*var == '!') {
777 /* case !var Boolean False */
778 set_default(var + 1, NULL, FALSE);
780 /* case var Boolean True */
781 set_default(var, NULL, TRUE);
786 ldap_value_free_len(bv);
790 * builds together a filter to check against ldap
793 sudo_ldap_build_pass1(pw)
801 /* Start with (|(sudoUser=USERNAME)(sudoUser=ALL)) + NUL */
802 sz = 29 + strlen(pw->pw_name);
804 /* Add space for groups */
805 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL)
806 sz += 12 + strlen(grp->gr_name); /* primary group */
807 for (i = 0; i < user_ngroups; i++) {
808 if (user_groups[i] == pw->pw_gid)
810 if ((grp = sudo_getgrgid(user_groups[i])) != NULL)
811 sz += 12 + strlen(grp->gr_name); /* supplementary group */
815 /* Global OR + sudoUser=user_name filter */
816 (void) strlcpy(buf, "(|(sudoUser=", sz);
817 (void) strlcat(buf, pw->pw_name, sz);
818 (void) strlcat(buf, ")", sz);
820 /* Append primary group */
821 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
822 (void) strlcat(buf, "(sudoUser=%", sz);
823 (void) strlcat(buf, grp->gr_name, sz);
824 (void) strlcat(buf, ")", sz);
827 /* Append supplementary groups */
828 for (i = 0; i < user_ngroups; i++) {
829 if (user_groups[i] == pw->pw_gid)
831 if ((grp = sudo_getgrgid(user_groups[i])) != NULL) {
832 (void) strlcat(buf, "(sudoUser=%", sz);
833 (void) strlcat(buf, grp->gr_name, sz);
834 (void) strlcat(buf, ")", sz);
838 /* Add ALL to list and end the global OR */
839 if (strlcat(buf, "(sudoUser=ALL))", sz) >= sz)
840 errorx(1, "sudo_ldap_build_pass1 allocation mismatch");
846 * Map yes/true/on to TRUE, no/false/off to FALSE, else -1
855 if (strcasecmp(s, "yes") == 0)
860 if (strcasecmp(s, "true") == 0)
865 if (strcasecmp(s, "on") == 0)
867 if (strcasecmp(s, "off") == 0)
872 if (strcasecmp(s, "no") == 0)
877 if (strcasecmp(s, "false") == 0)
885 sudo_ldap_read_secret(path)
889 char buf[LINE_MAX], *cp;
891 if ((fp = fopen(_PATH_LDAP_SECRET, "r")) != NULL) {
892 if (fgets(buf, sizeof(buf), fp) != NULL) {
893 if ((cp = strchr(buf, '\n')) != NULL)
895 /* copy to bindpw and binddn */
896 efree(ldap_conf.bindpw);
897 ldap_conf.bindpw = estrdup(buf);
898 efree(ldap_conf.binddn);
899 ldap_conf.binddn = ldap_conf.rootbinddn;
900 ldap_conf.rootbinddn = NULL;
907 sudo_ldap_read_config()
910 char *cp, *keyword, *value;
911 struct ldap_config_table *cur;
914 ldap_conf.version = 3;
916 ldap_conf.tls_checkpeer = -1;
917 ldap_conf.timelimit = -1;
918 ldap_conf.bind_timelimit = -1;
919 ldap_conf.use_sasl = -1;
920 ldap_conf.rootuse_sasl = -1;
922 if ((fp = fopen(_PATH_LDAP_CONF, "r")) == NULL)
925 while ((cp = sudo_parseln(fp)) != NULL) {
927 continue; /* skip empty line */
929 /* split into keyword and value */
931 while (*cp && !isblank((unsigned char) *cp))
934 *cp++ = '\0'; /* terminate keyword */
936 /* skip whitespace before value */
937 while (isblank((unsigned char) *cp))
941 /* Look up keyword in config table. */
942 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
943 if (strcasecmp(keyword, cur->conf_str) == 0) {
946 *(int *)(cur->valp) = _atobool(value);
949 *(int *)(cur->valp) = atoi(value);
952 efree(*(char **)(cur->valp));
953 *(char **)(cur->valp) = estrdup(value);
963 ldap_conf.host = estrdup("localhost");
965 if (ldap_conf.bind_timelimit > 0)
966 ldap_conf.bind_timelimit *= 1000; /* convert to ms */
968 if (ldap_conf.debug > 1) {
969 fprintf(stderr, "LDAP Config Summary\n");
970 fprintf(stderr, "===================\n");
972 fprintf(stderr, "uri %s\n", ldap_conf.uri);
974 fprintf(stderr, "host %s\n", ldap_conf.host ?
975 ldap_conf.host : "(NONE)");
976 fprintf(stderr, "port %d\n", ldap_conf.port);
978 fprintf(stderr, "ldap_version %d\n", ldap_conf.version);
980 fprintf(stderr, "sudoers_base %s\n", ldap_conf.base ?
981 ldap_conf.base : "(NONE) <---Sudo will ignore ldap)");
982 fprintf(stderr, "binddn %s\n", ldap_conf.binddn ?
983 ldap_conf.binddn : "(anonymous)");
984 fprintf(stderr, "bindpw %s\n", ldap_conf.bindpw ?
985 ldap_conf.bindpw : "(anonymous)");
986 if (ldap_conf.bind_timelimit > 0)
987 fprintf(stderr, "bind_timelimit %d\n", ldap_conf.bind_timelimit);
988 if (ldap_conf.timelimit > 0)
989 fprintf(stderr, "timelimit %d\n", ldap_conf.timelimit);
990 fprintf(stderr, "ssl %s\n", ldap_conf.ssl ?
991 ldap_conf.ssl : "(no)");
992 if (ldap_conf.tls_checkpeer != -1)
993 fprintf(stderr, "tls_checkpeer %s\n", ldap_conf.tls_checkpeer ?
995 if (ldap_conf.tls_cacertfile != NULL)
996 fprintf(stderr, "tls_cacertfile %s\n", ldap_conf.tls_cacertfile);
997 if (ldap_conf.tls_cacertdir != NULL)
998 fprintf(stderr, "tls_cacertdir %s\n", ldap_conf.tls_cacertdir);
999 if (ldap_conf.tls_random_file != NULL)
1000 fprintf(stderr, "tls_random_file %s\n", ldap_conf.tls_random_file);
1001 if (ldap_conf.tls_cipher_suite != NULL)
1002 fprintf(stderr, "tls_cipher_suite %s\n", ldap_conf.tls_cipher_suite);
1003 if (ldap_conf.tls_certfile != NULL)
1004 fprintf(stderr, "tls_certfile %s\n", ldap_conf.tls_certfile);
1005 if (ldap_conf.tls_keyfile != NULL)
1006 fprintf(stderr, "tls_keyfile %s\n", ldap_conf.tls_keyfile);
1007 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1008 if (ldap_conf.use_sasl != -1) {
1009 fprintf(stderr, "use_sasl %s\n",
1010 ldap_conf.use_sasl ? "yes" : "no");
1011 fprintf(stderr, "sasl_auth_id %s\n", ldap_conf.sasl_auth_id ?
1012 ldap_conf.sasl_auth_id : "(NONE)");
1013 fprintf(stderr, "rootuse_sasl %d\n", ldap_conf.rootuse_sasl);
1014 fprintf(stderr, "rootsasl_auth_id %s\n", ldap_conf.rootsasl_auth_id ?
1015 ldap_conf.rootsasl_auth_id : "(NONE)");
1016 fprintf(stderr, "sasl_secprops %s\n", ldap_conf.sasl_secprops ?
1017 ldap_conf.sasl_secprops : "(NONE)");
1018 fprintf(stderr, "krb5_ccname %s\n", ldap_conf.krb5_ccname ?
1019 ldap_conf.krb5_ccname : "(NONE)");
1022 fprintf(stderr, "===================\n");
1024 if (!ldap_conf.base)
1025 return(FALSE); /* if no base is defined, ignore LDAP */
1028 * Interpret SSL option
1030 if (ldap_conf.ssl != NULL) {
1031 if (strcasecmp(ldap_conf.ssl, "start_tls") == 0)
1032 ldap_conf.ssl_mode = SUDO_LDAP_STARTTLS;
1033 else if (_atobool(ldap_conf.ssl))
1034 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
1037 #if defined(HAVE_LDAPSSL_SET_STRENGTH) && !defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
1038 if (ldap_conf.tls_checkpeer != -1) {
1039 ldapssl_set_strength(NULL,
1040 ldap_conf.tls_checkpeer ? LDAPSSL_AUTH_CERT : LDAPSSL_AUTH_WEAK);
1044 #ifndef HAVE_LDAP_INITIALIZE
1045 /* Convert uri list to host list if no ldap_initialize(). */
1046 if (ldap_conf.uri) {
1047 if (sudo_ldap_parse_uri(ldap_conf.uri) != 0)
1049 free(ldap_conf.uri);
1050 ldap_conf.uri = NULL;
1051 ldap_conf.port = LDAP_PORT;
1055 if (!ldap_conf.uri) {
1056 /* Use port 389 for plaintext LDAP and port 636 for SSL LDAP */
1057 if (ldap_conf.port < 0)
1059 ldap_conf.ssl_mode == SUDO_LDAP_SSL ? LDAPS_PORT : LDAP_PORT;
1061 #ifdef HAVE_LDAP_CREATE
1063 * Cannot specify port directly to ldap_create(), each host must
1064 * include :port to override the default.
1066 if (ldap_conf.port != LDAP_PORT)
1067 sudo_ldap_conf_add_ports();
1071 /* If rootbinddn set, read in /etc/ldap.secret if it exists. */
1072 if (ldap_conf.rootbinddn)
1073 sudo_ldap_read_secret(_PATH_LDAP_SECRET);
1075 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1077 * Make sure we can open the file specified by krb5_ccname.
1079 if (ldap_conf.krb5_ccname != NULL) {
1080 if (strncasecmp(ldap_conf.krb5_ccname, "FILE:", 5) == 0 ||
1081 strncasecmp(ldap_conf.krb5_ccname, "WRFILE:", 7) == 0) {
1082 value = ldap_conf.krb5_ccname +
1083 (ldap_conf.krb5_ccname[4] == ':' ? 5 : 7);
1084 if ((fp = fopen(value, "r")) != NULL) {
1085 DPRINTF(("using krb5 credential cache: %s", value), 1);
1088 /* Can't open it, just ignore the entry. */
1089 DPRINTF(("unable to open krb5 credential cache: %s", value), 1);
1090 efree(ldap_conf.krb5_ccname);
1091 ldap_conf.krb5_ccname = NULL;
1100 * Extract the dn from an entry and return the first rdn from it.
1103 sudo_ldap_get_first_rdn(ld, entry)
1107 #ifdef HAVE_LDAP_STR2DN
1108 char *dn, *rdn = NULL;
1111 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1113 if (ldap_str2dn(dn, &tmpDN, LDAP_DN_FORMAT_LDAP) == LDAP_SUCCESS) {
1114 ldap_rdn2str(tmpDN[0], &rdn, LDAP_DN_FORMAT_UFN);
1122 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1124 edn = ldap_explode_dn(dn, 1);
1126 return(edn ? edn[0] : NULL);
1131 * Fetch and display the global Options.
1134 sudo_ldap_display_defaults(nss, pw, lbuf)
1135 struct sudo_nss *nss;
1139 struct berval **bv, **p;
1140 LDAP *ld = (LDAP *) nss->handle;
1141 LDAPMessage *entry = NULL, *result = NULL;
1142 char *prefix = NULL;
1148 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE,
1149 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1150 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
1151 bv = ldap_get_values_len(ld, entry, "sudoOption");
1157 for (p = bv; *p != NULL; p++) {
1158 lbuf_append(lbuf, prefix, (*p)->bv_val, NULL);
1162 ldap_value_free_len(bv);
1166 ldap_msgfree(result);
1174 sudo_ldap_display_bound_defaults(nss, pw, lbuf)
1175 struct sudo_nss *nss;
1183 * Print a record in the short form, ala file sudoers.
1186 sudo_ldap_display_entry_short(ld, entry, lbuf)
1191 struct berval **bv, **p;
1194 lbuf_append(lbuf, " (", NULL);
1196 /* get the RunAsUser Values from the entry */
1197 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1199 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1201 for (p = bv; *p != NULL; p++) {
1203 lbuf_append(lbuf, ", ", NULL);
1204 lbuf_append(lbuf, (*p)->bv_val, NULL);
1206 ldap_value_free_len(bv);
1208 lbuf_append(lbuf, def_runas_default, NULL);
1210 /* get the RunAsGroup Values from the entry */
1211 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1213 lbuf_append(lbuf, " : ", NULL);
1214 for (p = bv; *p != NULL; p++) {
1216 lbuf_append(lbuf, ", ", NULL);
1217 lbuf_append(lbuf, (*p)->bv_val, NULL);
1219 ldap_value_free_len(bv);
1221 lbuf_append(lbuf, ") ", NULL);
1223 /* get the Option Values from the entry */
1224 bv = ldap_get_values_len(ld, entry, "sudoOption");
1228 for (p = bv; *p != NULL; p++) {
1233 if (strcmp(cp, "authenticate") == 0)
1234 tag = (*p)->bv_val[0] == '!' ?
1235 "NOPASSWD: " : "PASSWD: ";
1236 else if (strcmp(cp, "noexec") == 0)
1237 tag = (*p)->bv_val[0] == '!' ?
1238 "EXEC: " : "NOEXEC: ";
1239 else if (strcmp(cp, "setenv") == 0)
1240 tag = (*p)->bv_val[0] == '!' ?
1241 "NOSETENV: " : "SETENV: ";
1243 lbuf_append(lbuf, tag, NULL);
1244 /* XXX - ignores other options */
1246 ldap_value_free_len(bv);
1249 /* get the Command Values from the entry */
1250 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1252 for (p = bv; *p != NULL; p++) {
1254 lbuf_append(lbuf, ", ", NULL);
1255 lbuf_append(lbuf, (*p)->bv_val, NULL);
1258 ldap_value_free_len(bv);
1261 lbuf_print(lbuf); /* forces a newline */
1266 * Print a record in the long form.
1269 sudo_ldap_display_entry_long(ld, entry, lbuf)
1274 struct berval **bv, **p;
1278 /* extract the dn, only show the first rdn */
1279 rdn = sudo_ldap_get_first_rdn(ld, entry);
1280 lbuf_print(lbuf); /* force a newline */
1281 lbuf_append(lbuf, "LDAP Role: ", rdn ? rdn : "UNKNOWN", NULL);
1286 /* get the RunAsUser Values from the entry */
1287 lbuf_append(lbuf, " RunAsUsers: ", NULL);
1288 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1290 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1292 for (p = bv; *p != NULL; p++) {
1294 lbuf_append(lbuf, ", ", NULL);
1295 lbuf_append(lbuf, (*p)->bv_val, NULL);
1297 ldap_value_free_len(bv);
1299 lbuf_append(lbuf, def_runas_default, NULL);
1302 /* get the RunAsGroup Values from the entry */
1303 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1305 lbuf_append(lbuf, " RunAsGroups: ", NULL);
1306 for (p = bv; *p != NULL; p++) {
1308 lbuf_append(lbuf, ", ", NULL);
1309 lbuf_append(lbuf, (*p)->bv_val, NULL);
1311 ldap_value_free_len(bv);
1315 /* get the Option Values from the entry */
1316 bv = ldap_get_values_len(ld, entry, "sudoOption");
1318 lbuf_append(lbuf, " Options: ", NULL);
1319 for (p = bv; *p != NULL; p++) {
1321 lbuf_append(lbuf, ", ", NULL);
1322 lbuf_append(lbuf, (*p)->bv_val, NULL);
1324 ldap_value_free_len(bv);
1328 /* get the Command Values from the entry */
1329 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1331 lbuf_append(lbuf, " Commands:", NULL);
1333 for (p = bv; *p != NULL; p++) {
1334 lbuf_append(lbuf, "\t", (*p)->bv_val, NULL);
1338 ldap_value_free_len(bv);
1345 * Like sudo_ldap_lookup(), except we just print entries.
1348 sudo_ldap_display_privs(nss, pw, lbuf)
1349 struct sudo_nss *nss;
1353 LDAP *ld = (LDAP *) nss->handle;
1354 LDAPMessage *entry = NULL, *result = NULL;
1356 int rc, do_netgr, count = 0;
1362 * Okay - time to search for anything that matches this user
1363 * Lets limit it to only two queries of the LDAP server
1365 * The first pass will look by the username, groups, and
1366 * the keyword ALL. We will then inspect the results that
1367 * came back from the query. We don't need to inspect the
1368 * sudoUser in this pass since the LDAP server already scanned
1371 * The second pass will return all the entries that contain
1372 * user netgroups. Then we take the netgroups returned and
1373 * try to match them against the username.
1375 for (do_netgr = 0; do_netgr < 2; do_netgr++) {
1376 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1377 DPRINTF(("ldap search '%s'", filt), 1);
1378 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1379 NULL, 0, NULL, NULL, NULL, 0, &result);
1381 if (rc != LDAP_SUCCESS)
1382 continue; /* no entries for this pass */
1384 /* print each matching entry */
1385 LDAP_FOREACH(entry, ld, result) {
1387 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1388 sudo_ldap_check_host(ld, entry)) {
1391 count += sudo_ldap_display_entry_long(ld, entry, lbuf);
1393 count += sudo_ldap_display_entry_short(ld, entry, lbuf);
1396 ldap_msgfree(result);
1403 sudo_ldap_display_cmnd(nss, pw)
1404 struct sudo_nss *nss;
1407 LDAP *ld = (LDAP *) nss->handle;
1408 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
1409 char *filt; /* used to parse attributes */
1410 int rc, found, do_netgr; /* temp/final return values */
1416 * Okay - time to search for anything that matches this user
1417 * Lets limit it to only two queries of the LDAP server
1419 * The first pass will look by the username, groups, and
1420 * the keyword ALL. We will then inspect the results that
1421 * came back from the query. We don't need to inspect the
1422 * sudoUser in this pass since the LDAP server already scanned
1425 * The second pass will return all the entries that contain
1426 * user netgroups. Then we take the netgroups returned and
1427 * try to match them against the username.
1429 for (found = FALSE, do_netgr = 0; !found && do_netgr < 2; do_netgr++) {
1430 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1431 DPRINTF(("ldap search '%s'", filt), 1);
1432 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1433 NULL, 0, NULL, NULL, NULL, 0, &result);
1435 if (rc != LDAP_SUCCESS)
1436 continue; /* no entries for this pass */
1438 LDAP_FOREACH(entry, ld, result) {
1440 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1441 sudo_ldap_check_host(ld, entry) &&
1442 sudo_ldap_check_command(ld, entry, NULL) &&
1443 sudo_ldap_check_runas(ld, entry)) {
1449 ldap_msgfree(result);
1454 printf("%s%s%s\n", safe_cmnd ? safe_cmnd : user_cmnd,
1455 user_args ? " " : "", user_args ? user_args : "");
1459 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1461 sudo_ldap_sasl_interact(ld, flags, _auth_id, _interact)
1467 char *auth_id = (char *)_auth_id;
1468 sasl_interact_t *interact = (sasl_interact_t *)_interact;
1470 for (; interact->id != SASL_CB_LIST_END; interact++) {
1471 if (interact->id != SASL_CB_USER)
1472 return(LDAP_PARAM_ERROR);
1474 if (auth_id != NULL)
1475 interact->result = auth_id;
1476 else if (interact->defresult != NULL)
1477 interact->result = interact->defresult;
1479 interact->result = "";
1481 interact->len = strlen(interact->result);
1482 #if SASL_VERSION_MAJOR < 2
1483 interact->result = estrdup(interact->result);
1484 #endif /* SASL_VERSION_MAJOR < 2 */
1486 return(LDAP_SUCCESS);
1488 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1491 * Set LDAP options based on the config table.
1494 sudo_ldap_set_options(ld)
1497 struct ldap_config_table *cur;
1500 /* Set ber options */
1501 #ifdef LBER_OPT_DEBUG_LEVEL
1502 if (ldap_conf.ldap_debug)
1503 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug);
1506 /* Set simple LDAP options */
1507 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
1512 if (cur->opt_val == -1)
1515 conn = cur->connected ? ld : NULL;
1516 switch (cur->type) {
1519 ival = *(int *)(cur->valp);
1521 rc = ldap_set_option(conn, cur->opt_val, &ival);
1522 if (rc != LDAP_OPT_SUCCESS) {
1523 warningx("ldap_set_option: %s -> %d: %s",
1524 cur->conf_str, ival, ldap_err2string(rc));
1527 DPRINTF(("ldap_set_option: %s -> %d", cur->conf_str, ival), 1);
1531 sval = *(char **)(cur->valp);
1533 rc = ldap_set_option(conn, cur->opt_val, sval);
1534 if (rc != LDAP_OPT_SUCCESS) {
1535 warningx("ldap_set_option: %s -> %s: %s",
1536 cur->conf_str, sval, ldap_err2string(rc));
1539 DPRINTF(("ldap_set_option: %s -> %s", cur->conf_str, sval), 1);
1545 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1546 /* Convert bind_timelimit to a timeval */
1547 if (ldap_conf.bind_timelimit > 0) {
1549 tv.tv_sec = ldap_conf.bind_timelimit / 1000;
1551 rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
1552 if (rc != LDAP_OPT_SUCCESS) {
1553 warningx("ldap_set_option(NETWORK_TIMEOUT, %ld): %s",
1554 (long)tv.tv_sec, ldap_err2string(rc));
1557 DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)\n",
1558 (long)tv.tv_sec), 1);
1562 #if defined(LDAP_OPT_X_TLS) && !defined(HAVE_LDAPSSL_INIT)
1563 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
1564 int val = LDAP_OPT_X_TLS_HARD;
1565 rc = ldap_set_option(ld, LDAP_OPT_X_TLS, &val);
1566 if (rc != LDAP_SUCCESS) {
1567 warningx("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD): %s",
1568 ldap_err2string(rc));
1571 DPRINTF(("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD)\n"), 1);
1578 * Connect to the LDAP server specified by ld
1581 sudo_ldap_bind_s(ld)
1585 const char *old_ccname = user_ccname;
1586 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1587 unsigned int status;
1590 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1591 if (ldap_conf.rootuse_sasl == TRUE ||
1592 (ldap_conf.rootuse_sasl != FALSE && ldap_conf.use_sasl == TRUE)) {
1593 void *auth_id = ldap_conf.rootsasl_auth_id ?
1594 ldap_conf.rootsasl_auth_id : ldap_conf.sasl_auth_id;
1596 if (ldap_conf.krb5_ccname != NULL) {
1597 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1598 if (gss_krb5_ccache_name(&status, ldap_conf.krb5_ccname, &old_ccname)
1599 != GSS_S_COMPLETE) {
1601 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1604 setenv("KRB5CCNAME", ldap_conf.krb5_ccname, TRUE);
1607 rc = ldap_sasl_interactive_bind_s(ld, ldap_conf.binddn, "GSSAPI",
1608 NULL, NULL, LDAP_SASL_QUIET, sudo_ldap_sasl_interact, auth_id);
1609 if (ldap_conf.krb5_ccname != NULL) {
1610 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
1611 if (gss_krb5_ccache_name(&status, old_ccname, NULL) != GSS_S_COMPLETE)
1612 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1614 if (old_ccname != NULL)
1615 setenv("KRB5CCNAME", old_ccname, TRUE);
1617 unsetenv("KRB5CCNAME");
1620 if (rc != LDAP_SUCCESS) {
1621 warningx("ldap_sasl_interactive_bind_s(): %s", ldap_err2string(rc));
1624 DPRINTF(("ldap_sasl_interactive_bind_s() ok"), 1);
1626 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1627 #ifdef HAVE_LDAP_SASL_BIND_S
1631 bv.bv_val = ldap_conf.bindpw ? ldap_conf.bindpw : "";
1632 bv.bv_len = strlen(bv.bv_val);
1634 rc = ldap_sasl_bind_s(ld, ldap_conf.binddn, LDAP_SASL_SIMPLE, &bv,
1636 if (rc != LDAP_SUCCESS) {
1637 warningx("ldap_sasl_bind_s(): %s", ldap_err2string(rc));
1640 DPRINTF(("ldap_sasl_bind_s() ok"), 1);
1644 rc = ldap_simple_bind_s(ld, ldap_conf.binddn, ldap_conf.bindpw);
1645 if (rc != LDAP_SUCCESS) {
1646 warningx("ldap_simple_bind_s(): %s", ldap_err2string(rc));
1649 DPRINTF(("ldap_simple_bind_s() ok"), 1);
1656 * Open a connection to the LDAP server.
1657 * Returns 0 on success and non-zero on failure.
1661 struct sudo_nss *nss;
1664 int rc, ldapnoinit = FALSE;
1666 if (!sudo_ldap_read_config())
1669 /* Prevent reading of user ldaprc and system defaults. */
1670 if (getenv("LDAPNOINIT") == NULL) {
1672 setenv("LDAPNOINIT", "1", TRUE);
1675 /* Connect to LDAP server */
1676 #ifdef HAVE_LDAP_INITIALIZE
1677 if (ldap_conf.uri != NULL) {
1678 DPRINTF(("ldap_initialize(ld, %s)", ldap_conf.uri), 2);
1679 rc = ldap_initialize(&ld, ldap_conf.uri);
1682 rc = sudo_ldap_init(&ld, ldap_conf.host, ldap_conf.port);
1683 if (rc != LDAP_SUCCESS) {
1684 warningx("unable to initialize LDAP: %s", ldap_err2string(rc));
1689 unsetenv("LDAPNOINIT");
1691 /* Set LDAP options */
1692 if (sudo_ldap_set_options(ld) < 0)
1695 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
1696 #if defined(HAVE_LDAP_START_TLS_S)
1697 rc = ldap_start_tls_s(ld, NULL, NULL);
1698 if (rc != LDAP_SUCCESS) {
1699 warningx("ldap_start_tls_s(): %s", ldap_err2string(rc));
1702 DPRINTF(("ldap_start_tls_s() ok"), 1);
1703 #elif defined(HAVE_LDAP_SSL_CLIENT_INIT) && defined(HAVE_LDAP_START_TLS_S_NP)
1704 if (ldap_ssl_client_init(NULL, NULL, 0, &rc) != LDAP_SUCCESS) {
1705 warningx("ldap_ssl_client_init(): %s", ldap_err2string(rc));
1708 rc = ldap_start_tls_s_np(ld, NULL);
1709 if (rc != LDAP_SUCCESS) {
1710 warningx("ldap_start_tls_s_np(): %s", ldap_err2string(rc));
1713 DPRINTF(("ldap_start_tls_s_np() ok"), 1);
1715 warningx("start_tls specified but LDAP libs do not support ldap_start_tls_s() or ldap_start_tls_s_np()");
1716 #endif /* !HAVE_LDAP_START_TLS_S && !HAVE_LDAP_START_TLS_S_NP */
1719 /* Actually connect */
1720 if (sudo_ldap_bind_s(ld) != 0)
1728 sudo_ldap_setdefs(nss)
1729 struct sudo_nss *nss;
1731 LDAP *ld = (LDAP *) nss->handle;
1732 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
1733 int rc; /* temp return value */
1738 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE,
1739 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1740 if (rc == 0 && (entry = ldap_first_entry(ld, result))) {
1741 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1742 sudo_ldap_parse_options(ld, entry);
1744 DPRINTF(("no default options found!"), 1);
1747 ldap_msgfree(result);
1753 * like sudoers_lookup() - only LDAP style
1756 sudo_ldap_lookup(nss, ret, pwflag)
1757 struct sudo_nss *nss;
1761 LDAP *ld = (LDAP *) nss->handle;
1762 LDAPMessage *entry = NULL, *result = NULL;
1764 int do_netgr, rc, matched;
1766 int ldap_user_matches = FALSE, ldap_host_matches = FALSE;
1767 struct passwd *pw = list_pw ? list_pw : sudo_user.pw;
1773 int doauth = UNSPEC;
1774 enum def_tupple pwcheck =
1775 (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
1777 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1778 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1779 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1780 NULL, 0, NULL, NULL, NULL, 0, &result);
1782 if (rc != LDAP_SUCCESS)
1785 LDAP_FOREACH(entry, ld, result) {
1786 /* only verify netgroup matches in pass 2 */
1787 if (do_netgr && !sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name))
1790 ldap_user_matches = TRUE;
1791 if (sudo_ldap_check_host(ld, entry)) {
1792 ldap_host_matches = TRUE;
1793 if ((pwcheck == any && doauth != FALSE) ||
1794 (pwcheck == all && doauth == FALSE))
1795 doauth = sudo_ldap_check_bool(ld, entry, "authenticate");
1796 /* Only check the command when listing another user. */
1797 if (user_uid == 0 || list_pw == NULL ||
1798 user_uid == list_pw->pw_uid ||
1799 sudo_ldap_check_command(ld, entry, NULL)) {
1801 break; /* end foreach */
1805 ldap_msgfree(result);
1808 if (matched || user_uid == 0) {
1809 SET(ret, VALIDATE_OK);
1810 CLR(ret, VALIDATE_NOT_OK);
1811 if (def_authenticate) {
1814 SET(ret, FLAG_CHECK_USER);
1818 if (doauth == FALSE)
1819 def_authenticate = FALSE;
1822 def_authenticate = FALSE;
1833 * Okay - time to search for anything that matches this user
1834 * Lets limit it to only two queries of the LDAP server
1836 * The first pass will look by the username, groups, and
1837 * the keyword ALL. We will then inspect the results that
1838 * came back from the query. We don't need to inspect the
1839 * sudoUser in this pass since the LDAP server already scanned
1842 * The second pass will return all the entries that contain
1843 * user netgroups. Then we take the netgroups returned and
1844 * try to match them against the username.
1846 setenv_implied = FALSE;
1847 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1848 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1849 DPRINTF(("ldap search '%s'", filt), 1);
1850 rc = ldap_search_ext_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
1851 NULL, 0, NULL, NULL, NULL, 0, &result);
1852 if (rc != LDAP_SUCCESS)
1853 DPRINTF(("nothing found for '%s'", filt), 1);
1856 /* parse each entry returned from this most recent search */
1857 if (rc == LDAP_SUCCESS) {
1858 LDAP_FOREACH(entry, ld, result) {
1859 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1861 /* first verify user netgroup matches - only if in pass 2 */
1862 (!do_netgr || sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1863 /* remember that user matched */
1864 (ldap_user_matches = TRUE) &&
1865 /* verify host match */
1866 sudo_ldap_check_host(ld, entry) &&
1867 /* remember that host matched */
1868 (ldap_host_matches = TRUE) &&
1869 /* verify runas match */
1870 sudo_ldap_check_runas(ld, entry) &&
1871 /* verify command match */
1872 (rc = sudo_ldap_check_command(ld, entry, &setenv_implied)) != UNSPEC
1874 /* We have a match! */
1875 DPRINTF(("Command %sallowed", rc == TRUE ? "" : "NOT "), 1);
1878 /* pick up any options */
1881 sudo_ldap_parse_options(ld, entry);
1883 /* Set role and type if not specified on command line. */
1884 if (user_role == NULL)
1885 user_role = def_role;
1886 if (user_type == NULL)
1887 user_type = def_type;
1888 #endif /* HAVE_SELINUX */
1889 /* make sure we don't reenter loop */
1890 SET(ret, VALIDATE_OK);
1891 CLR(ret, VALIDATE_NOT_OK);
1893 SET(ret, VALIDATE_NOT_OK);
1894 CLR(ret, VALIDATE_OK);
1896 /* break from inside for loop */
1900 ldap_msgfree(result);
1906 DPRINTF(("user_matches=%d", ldap_user_matches), 1);
1907 DPRINTF(("host_matches=%d", ldap_host_matches), 1);
1909 if (!ISSET(ret, VALIDATE_OK)) {
1910 /* we do not have a match */
1911 if (pwflag && list_pw == NULL)
1912 SET(ret, FLAG_NO_CHECK);
1914 if (ldap_user_matches)
1915 CLR(ret, FLAG_NO_USER);
1916 if (ldap_host_matches)
1917 CLR(ret, FLAG_NO_HOST);
1918 DPRINTF(("sudo_ldap_lookup(%d)=0x%02x", pwflag, ret), 1);
1924 * shut down LDAP connection
1927 sudo_ldap_close(nss)
1928 struct sudo_nss *nss;
1930 if (nss->handle != NULL) {
1931 ldap_unbind_ext_s((LDAP *) nss->handle, NULL, NULL);
1941 sudo_ldap_parse(nss)
1942 struct sudo_nss *nss;