2 * Copyright (c) 2003-2010 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 */
40 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
42 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
45 #endif /* HAVE_UNISTD_H */
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 #ifndef LDAP_OPT_SUCCESS
84 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
88 # define LDAPS_PORT 636
91 #if defined(HAVE_LDAP_SASL_INTERACTIVE_BIND_S) && !defined(LDAP_SASL_QUIET)
92 # define LDAP_SASL_QUIET 0
95 #ifndef HAVE_LDAP_UNBIND_EXT_S
96 #define ldap_unbind_ext_s(a, b, c) ldap_unbind_s(a)
99 #ifndef HAVE_LDAP_SEARCH_EXT_S
100 #define ldap_search_ext_s(a, b, c, d, e, f, g, h, i, j, k) \
101 ldap_search_s(a, b, c, d, e, f, k)
104 #define LDAP_FOREACH(var, ld, res) \
105 for ((var) = ldap_first_entry((ld), (res)); \
107 (var) = ldap_next_entry((ld), (var)))
109 #define DPRINTF(args, level) if (ldap_conf.debug >= level) warningx args
114 #define CONF_LIST_STR 4
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 struct ldap_config_list_str {
128 struct ldap_config_list_str *next;
132 /* ldap configuration structure */
133 static struct ldap_config {
145 struct ldap_config_list_str *uri;
149 struct ldap_config_list_str *base;
151 char *tls_cacertfile;
153 char *tls_random_file;
154 char *tls_cipher_suite;
158 char *rootsasl_auth_id;
163 static struct ldap_config_table ldap_conf_table[] = {
164 { "sudoers_debug", CONF_INT, FALSE, -1, &ldap_conf.debug },
165 { "host", CONF_STR, FALSE, -1, &ldap_conf.host },
166 { "port", CONF_INT, FALSE, -1, &ldap_conf.port },
167 { "ssl", CONF_STR, FALSE, -1, &ldap_conf.ssl },
168 { "sslpath", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
169 { "uri", CONF_LIST_STR, FALSE, -1, &ldap_conf.uri },
170 #ifdef LDAP_OPT_DEBUG_LEVEL
171 { "debug", CONF_INT, FALSE, LDAP_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug },
173 #ifdef LDAP_OPT_PROTOCOL_VERSION
174 { "ldap_version", CONF_INT, TRUE, LDAP_OPT_PROTOCOL_VERSION,
175 &ldap_conf.version },
177 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
178 { "tls_checkpeer", CONF_BOOL, FALSE, LDAP_OPT_X_TLS_REQUIRE_CERT,
179 &ldap_conf.tls_checkpeer },
181 { "tls_checkpeer", CONF_BOOL, FALSE, -1, &ldap_conf.tls_checkpeer },
183 #ifdef LDAP_OPT_X_TLS_CACERTFILE
184 { "tls_cacertfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTFILE,
185 &ldap_conf.tls_cacertfile },
186 { "tls_cacert", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTFILE,
187 &ldap_conf.tls_cacertfile },
189 #ifdef LDAP_OPT_X_TLS_CACERTDIR
190 { "tls_cacertdir", CONF_STR, FALSE, LDAP_OPT_X_TLS_CACERTDIR,
191 &ldap_conf.tls_cacertdir },
193 #ifdef LDAP_OPT_X_TLS_RANDOM_FILE
194 { "tls_randfile", CONF_STR, FALSE, LDAP_OPT_X_TLS_RANDOM_FILE,
195 &ldap_conf.tls_random_file },
197 #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
198 { "tls_ciphers", CONF_STR, FALSE, LDAP_OPT_X_TLS_CIPHER_SUITE,
199 &ldap_conf.tls_cipher_suite },
201 #ifdef LDAP_OPT_X_TLS_CERTFILE
202 { "tls_cert", CONF_STR, FALSE, LDAP_OPT_X_TLS_CERTFILE,
203 &ldap_conf.tls_certfile },
205 { "tls_cert", CONF_STR, FALSE, -1, &ldap_conf.tls_certfile },
207 #ifdef LDAP_OPT_X_TLS_KEYFILE
208 { "tls_key", CONF_STR, FALSE, LDAP_OPT_X_TLS_KEYFILE,
209 &ldap_conf.tls_keyfile },
211 { "tls_key", CONF_STR, FALSE, -1, &ldap_conf.tls_keyfile },
213 #ifdef LDAP_OPT_NETWORK_TIMEOUT
214 { "bind_timelimit", CONF_INT, TRUE, -1 /* needs timeval, set manually */,
215 &ldap_conf.bind_timelimit },
216 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
217 { "bind_timelimit", CONF_INT, TRUE, LDAP_X_OPT_CONNECT_TIMEOUT,
218 &ldap_conf.bind_timelimit },
220 { "timelimit", CONF_INT, TRUE, LDAP_OPT_TIMELIMIT, &ldap_conf.timelimit },
221 { "binddn", CONF_STR, FALSE, -1, &ldap_conf.binddn },
222 { "bindpw", CONF_STR, FALSE, -1, &ldap_conf.bindpw },
223 { "rootbinddn", CONF_STR, FALSE, -1, &ldap_conf.rootbinddn },
224 { "sudoers_base", CONF_LIST_STR, FALSE, -1, &ldap_conf.base },
225 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
226 { "use_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.use_sasl },
227 { "sasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.sasl_auth_id },
228 { "rootuse_sasl", CONF_BOOL, FALSE, -1, &ldap_conf.rootuse_sasl },
229 { "rootsasl_auth_id", CONF_STR, FALSE, -1, &ldap_conf.rootsasl_auth_id },
230 # ifdef LDAP_OPT_X_SASL_SECPROPS
231 { "sasl_secprops", CONF_STR, TRUE, LDAP_OPT_X_SASL_SECPROPS,
232 &ldap_conf.sasl_secprops },
234 { "krb5_ccname", CONF_STR, FALSE, -1, &ldap_conf.krb5_ccname },
235 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
239 /* sudo_nss implementation */
240 static int sudo_ldap_open __P((struct sudo_nss *nss));
241 static int sudo_ldap_close __P((struct sudo_nss *nss));
242 static int sudo_ldap_parse __P((struct sudo_nss *nss));
243 static int sudo_ldap_setdefs __P((struct sudo_nss *nss));
244 static int sudo_ldap_lookup __P((struct sudo_nss *nss, int ret, int pwflag));
245 static int sudo_ldap_display_cmnd __P((struct sudo_nss *nss,
247 static int sudo_ldap_display_defaults __P((struct sudo_nss *nss,
248 struct passwd *pw, struct lbuf *lbuf));
249 static int sudo_ldap_display_bound_defaults __P((struct sudo_nss *nss,
250 struct passwd *pw, struct lbuf *lbuf));
251 static int sudo_ldap_display_privs __P((struct sudo_nss *nss,
252 struct passwd *pw, struct lbuf *lbuf));
254 struct sudo_nss sudo_nss_ldap = {
262 sudo_ldap_display_cmnd,
263 sudo_ldap_display_defaults,
264 sudo_ldap_display_bound_defaults,
265 sudo_ldap_display_privs
268 #ifdef HAVE_LDAP_CREATE
270 * Rebuild the hosts list and include a specific port for each host.
271 * ldap_create() does not take a default port parameter so we must
272 * append one if we want something other than LDAP_PORT.
275 sudo_ldap_conf_add_ports()
278 char *host, *port, defport[13];
279 char hostbuf[LINE_MAX * 2];
282 if (snprintf(defport, sizeof(defport), ":%d", ldap_conf.port) >= sizeof(defport))
283 errorx(1, "sudo_ldap_conf_add_ports: port too large");
285 for ((host = strtok(ldap_conf.host, " \t")); host; (host = strtok(NULL, " \t"))) {
286 if (hostbuf[0] != '\0') {
287 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
291 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
293 /* Append port if there is not one already. */
294 if ((port = strrchr(host, ':')) == NULL ||
295 !isdigit((unsigned char)port[1])) {
296 if (strlcat(hostbuf, defport, sizeof(hostbuf)) >= sizeof(hostbuf))
301 free(ldap_conf.host);
302 ldap_conf.host = estrdup(hostbuf);
306 errorx(1, "sudo_ldap_conf_add_ports: out of space expanding hostbuf");
310 #ifndef HAVE_LDAP_INITIALIZE
312 * For each uri, convert to host:port pairs. For ldaps:// enable SSL
313 * Accepts: uris of the form ldap:/// or ldap://hostname:portnum/
314 * where the trailing slash is optional.
317 sudo_ldap_parse_uri(uri_list)
318 const struct ldap_config_list_str *uri_list;
320 char *buf, *uri, *host, *cp, *port;
321 char hostbuf[LINE_MAX];
322 int nldap = 0, nldaps = 0;
326 buf = estrdup(uri_list->val);
328 for ((uri = strtok(buf, " \t")); uri != NULL; (uri = strtok(NULL, " \t"))) {
329 if (strncasecmp(uri, "ldap://", 7) == 0) {
332 } else if (strncasecmp(uri, "ldaps://", 8) == 0) {
336 warningx("unsupported LDAP uri type: %s", uri);
340 /* trim optional trailing slash */
341 if ((cp = strrchr(host, '/')) != NULL && cp[1] == '\0') {
345 if (hostbuf[0] != '\0') {
346 if (strlcat(hostbuf, " ", sizeof(hostbuf)) >= sizeof(hostbuf))
351 host = "localhost"; /* no host specified, use localhost */
353 if (strlcat(hostbuf, host, sizeof(hostbuf)) >= sizeof(hostbuf))
356 /* If using SSL and no port specified, add port 636 */
358 if ((port = strrchr(host, ':')) == NULL ||
359 !isdigit((unsigned char)port[1]))
360 if (strlcat(hostbuf, ":636", sizeof(hostbuf)) >= sizeof(hostbuf))
364 if (hostbuf[0] == '\0') {
365 warningx("invalid uri: %s", uri_list);
371 warningx("cannot mix ldap and ldaps URIs");
374 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
375 warningx("cannot mix ldaps and starttls");
378 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
381 free(ldap_conf.host);
382 ldap_conf.host = estrdup(hostbuf);
384 } while ((uri_list = uri_list->next));
394 errorx(1, "sudo_ldap_parse_uri: out of space building hostbuf");
398 sudo_ldap_join_uri(uri_list)
399 struct ldap_config_list_str *uri_list;
401 struct ldap_config_list_str *uri;
405 /* Usually just a single entry. */
406 if (uri_list->next == NULL)
407 return(estrdup(uri_list->val));
409 for (uri = uri_list; uri != NULL; uri = uri->next) {
410 len += strlen(uri->val) + 1;
412 buf = cp = emalloc(len);
414 for (uri = uri_list; uri != NULL; uri = uri->next) {
415 cp += strlcpy(cp, uri->val, len - (cp - buf));
421 #endif /* HAVE_LDAP_INITIALIZE */
424 sudo_ldap_init(ldp, host, port)
430 int rc = LDAP_CONNECT_ERROR;
432 #ifdef HAVE_LDAPSSL_INIT
433 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
434 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
435 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
436 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
437 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
438 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
440 * Mozilla-derived SDKs have a bug starting with version 5.0
441 * where the path can no longer be a file name and must be a dir.
443 if (rc != LDAP_SUCCESS) {
445 if (ldap_conf.tls_certfile) {
446 cp = strrchr(ldap_conf.tls_certfile, '/');
447 if (cp != NULL && strncmp(cp + 1, "cert", 4) == 0)
450 if (ldap_conf.tls_keyfile) {
451 cp = strrchr(ldap_conf.tls_keyfile, '/');
452 if (cp != NULL && strncmp(cp + 1, "key", 3) == 0)
455 DPRINTF(("ldapssl_clientauth_init(%s, %s)",
456 ldap_conf.tls_certfile ? ldap_conf.tls_certfile : "NULL",
457 ldap_conf.tls_keyfile ? ldap_conf.tls_keyfile : "NULL"), 2);
458 rc = ldapssl_clientauth_init(ldap_conf.tls_certfile, NULL,
459 ldap_conf.tls_keyfile != NULL, ldap_conf.tls_keyfile, NULL);
460 if (rc != LDAP_SUCCESS) {
461 warningx("unable to initialize SSL cert and key db: %s",
462 ldapssl_err2string(rc));
467 DPRINTF(("ldapssl_init(%s, %d, 1)", host, port), 2);
468 if ((ld = ldapssl_init(host, port, 1)) != NULL)
473 #ifdef HAVE_LDAP_CREATE
474 DPRINTF(("ldap_create()"), 2);
475 if ((rc = ldap_create(&ld)) != LDAP_SUCCESS)
477 DPRINTF(("ldap_set_option(LDAP_OPT_HOST_NAME, %s)", host), 2);
478 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, host);
480 DPRINTF(("ldap_init(%s, %d)", host, port), 2);
481 if ((ld = ldap_init(host, port)) != NULL)
492 * Walk through search results and return TRUE if we have a matching
493 * netgroup, else FALSE.
496 sudo_ldap_check_user_netgroup(ld, entry, user)
501 struct berval **bv, **p;
508 /* get the values from the entry */
509 bv = ldap_get_values_len(ld, entry, "sudoUser");
513 /* walk through values */
514 for (p = bv; *p != NULL && !ret; p++) {
517 if (netgr_matches(val, NULL, NULL, user))
519 DPRINTF(("ldap sudoUser netgroup '%s' ... %s", val,
520 ret ? "MATCH!" : "not"), 2);
523 ldap_value_free_len(bv); /* cleanup */
529 * Walk through search results and return TRUE if we have a
530 * host match, else FALSE.
533 sudo_ldap_check_host(ld, entry)
537 struct berval **bv, **p;
544 /* get the values from the entry */
545 bv = ldap_get_values_len(ld, entry, "sudoHost");
549 /* walk through values */
550 for (p = bv; *p != NULL && !ret; p++) {
552 /* match any or address or netgroup or hostname */
553 if (!strcmp(val, "ALL") || addr_matches(val) ||
554 netgr_matches(val, user_host, user_shost, NULL) ||
555 hostname_matches(user_shost, user_host, val))
557 DPRINTF(("ldap sudoHost '%s' ... %s", val,
558 ret ? "MATCH!" : "not"), 2);
561 ldap_value_free_len(bv); /* cleanup */
567 sudo_ldap_check_runas_user(ld, entry)
571 struct berval **bv, **p;
578 /* get the runas user from the entry */
579 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
581 bv = ldap_get_values_len(ld, entry, "sudoRunAs"); /* old style */
586 * if runas is not specified on the command line, the only information
587 * as to which user to run as is in the runas_default option. We should
588 * check to see if we have the local option present. Unfortunately we
589 * don't parse these options until after this routine says yes or no.
590 * The query has already returned, so we could peek at the attribute
591 * values here though.
593 * For now just require users to always use -u option unless its set
594 * in the global defaults. This behaviour is no different than the global
597 * Sigh - maybe add this feature later
601 * If there are no runas entries, match runas_default against
602 * what the user specified on the command line.
605 return(!strcasecmp(runas_pw->pw_name, def_runas_default));
607 /* walk through values returned, looking for a match */
608 for (p = bv; *p != NULL && !ret; p++) {
612 if (netgr_matches(val, NULL, NULL, runas_pw->pw_name))
616 if (usergr_matches(val, runas_pw->pw_name, runas_pw))
620 if (strcmp(val, "ALL") == 0) {
626 if (strcasecmp(val, runas_pw->pw_name) == 0)
630 DPRINTF(("ldap sudoRunAsUser '%s' ... %s", val,
631 ret ? "MATCH!" : "not"), 2);
634 ldap_value_free_len(bv); /* cleanup */
640 sudo_ldap_check_runas_group(ld, entry)
644 struct berval **bv, **p;
648 /* runas_gr is only set if the user specified the -g flag */
652 /* get the values from the entry */
653 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
657 /* walk through values returned, looking for a match */
658 for (p = bv; *p != NULL && !ret; p++) {
660 if (strcmp(val, "ALL") == 0 || group_matches(val, runas_gr))
662 DPRINTF(("ldap sudoRunAsGroup '%s' ... %s", val,
663 ret ? "MATCH!" : "not"), 2);
666 ldap_value_free_len(bv); /* cleanup */
672 * Walk through search results and return TRUE if we have a runas match,
673 * else FALSE. RunAs info is optional.
676 sudo_ldap_check_runas(ld, entry)
685 ret = sudo_ldap_check_runas_user(ld, entry) != FALSE &&
686 sudo_ldap_check_runas_group(ld, entry) != FALSE;
692 * Walk through search results and return TRUE if we have a command match,
693 * FALSE if disallowed and UNSPEC if not matched.
696 sudo_ldap_check_command(ld, entry, setenv_implied)
701 struct berval **bv, **p;
702 char *allowed_cmnd, *allowed_args, *val;
703 int foundbang, ret = UNSPEC;
708 bv = ldap_get_values_len(ld, entry, "sudoCommand");
712 for (p = bv; *p != NULL && ret != FALSE; p++) {
714 /* Match against ALL ? */
715 if (!strcmp(val, "ALL")) {
717 if (setenv_implied != NULL)
718 *setenv_implied = TRUE;
719 DPRINTF(("ldap sudoCommand '%s' ... MATCH!", val), 2);
723 /* check for !command */
726 allowed_cmnd = estrdup(1 + val); /* !command */
729 allowed_cmnd = estrdup(val); /* command */
732 /* split optional args away from command */
733 allowed_args = strchr(allowed_cmnd, ' ');
735 *allowed_args++ = '\0';
737 /* check the command like normal */
738 if (command_matches(allowed_cmnd, allowed_args)) {
740 * If allowed (no bang) set ret but keep on checking.
741 * If disallowed (bang), exit loop.
743 ret = foundbang ? FALSE : TRUE;
745 DPRINTF(("ldap sudoCommand '%s' ... %s", val,
746 ret == TRUE ? "MATCH!" : "not"), 2);
748 efree(allowed_cmnd); /* cleanup */
751 ldap_value_free_len(bv); /* more cleanup */
757 * Search for boolean "option" in sudoOption.
758 * Returns TRUE if found and allowed, FALSE if negated, else UNSPEC.
761 sudo_ldap_check_bool(ld, entry, option)
766 struct berval **bv, **p;
773 bv = ldap_get_values_len(ld, entry, "sudoOption");
777 /* walk through options */
778 for (p = bv; *p != NULL; p++) {
780 DPRINTF(("ldap sudoOption: '%s'", var), 2);
782 if ((ch = *var) == '!')
784 if (strcmp(var, option) == 0)
788 ldap_value_free_len(bv);
794 * Read sudoOption and modify the defaults as we go. This is used once
795 * from the cn=defaults entry and also once when a final sudoRole is matched.
798 sudo_ldap_parse_options(ld, entry)
802 struct berval **bv, **p;
808 bv = ldap_get_values_len(ld, entry, "sudoOption");
812 /* walk through options */
813 for (p = bv; *p != NULL; p++) {
814 var = estrdup((*p)->bv_val);
815 DPRINTF(("ldap sudoOption: '%s'", var), 2);
817 /* check for equals sign past first char */
818 val = strchr(var, '=');
820 *val++ = '\0'; /* split on = and truncate var */
821 op = *(val - 2); /* peek for += or -= cases */
822 if (op == '+' || op == '-') {
823 *(val - 2) = '\0'; /* found, remove extra char */
824 /* case var+=val or var-=val */
825 set_default(var, val, (int) op);
828 set_default(var, val, TRUE);
830 } else if (*var == '!') {
831 /* case !var Boolean False */
832 set_default(var + 1, NULL, FALSE);
834 /* case var Boolean True */
835 set_default(var, NULL, TRUE);
840 ldap_value_free_len(bv);
844 * builds together a filter to check against ldap
847 sudo_ldap_build_pass1(pw)
855 /* Start with (|(sudoUser=USERNAME)(sudoUser=ALL)) + NUL */
856 sz = 29 + strlen(pw->pw_name);
858 /* Add space for groups */
859 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL)
860 sz += 12 + strlen(grp->gr_name); /* primary group */
861 for (i = 0; i < user_ngroups; i++) {
862 if (user_groups[i] == pw->pw_gid)
864 if ((grp = sudo_getgrgid(user_groups[i])) != NULL)
865 sz += 12 + strlen(grp->gr_name); /* supplementary group */
869 /* Global OR + sudoUser=user_name filter */
870 (void) strlcpy(buf, "(|(sudoUser=", sz);
871 (void) strlcat(buf, pw->pw_name, sz);
872 (void) strlcat(buf, ")", sz);
874 /* Append primary group */
875 if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
876 (void) strlcat(buf, "(sudoUser=%", sz);
877 (void) strlcat(buf, grp->gr_name, sz);
878 (void) strlcat(buf, ")", sz);
881 /* Append supplementary groups */
882 for (i = 0; i < user_ngroups; i++) {
883 if (user_groups[i] == pw->pw_gid)
885 if ((grp = sudo_getgrgid(user_groups[i])) != NULL) {
886 (void) strlcat(buf, "(sudoUser=%", sz);
887 (void) strlcat(buf, grp->gr_name, sz);
888 (void) strlcat(buf, ")", sz);
892 /* Add ALL to list and end the global OR */
893 if (strlcat(buf, "(sudoUser=ALL))", sz) >= sz)
894 errorx(1, "sudo_ldap_build_pass1 allocation mismatch");
900 * Map yes/true/on to TRUE, no/false/off to FALSE, else -1
909 if (strcasecmp(s, "yes") == 0)
914 if (strcasecmp(s, "true") == 0)
919 if (strcasecmp(s, "on") == 0)
921 if (strcasecmp(s, "off") == 0)
926 if (strcasecmp(s, "no") == 0)
931 if (strcasecmp(s, "false") == 0)
939 sudo_ldap_read_secret(path)
943 char buf[LINE_MAX], *cp;
945 if ((fp = fopen(_PATH_LDAP_SECRET, "r")) != NULL) {
946 if (fgets(buf, sizeof(buf), fp) != NULL) {
947 if ((cp = strchr(buf, '\n')) != NULL)
949 /* copy to bindpw and binddn */
950 efree(ldap_conf.bindpw);
951 ldap_conf.bindpw = estrdup(buf);
952 efree(ldap_conf.binddn);
953 ldap_conf.binddn = ldap_conf.rootbinddn;
954 ldap_conf.rootbinddn = NULL;
961 sudo_ldap_read_config()
964 char *cp, *keyword, *value;
965 struct ldap_config_table *cur;
968 ldap_conf.version = 3;
970 ldap_conf.tls_checkpeer = -1;
971 ldap_conf.timelimit = -1;
972 ldap_conf.bind_timelimit = -1;
973 ldap_conf.use_sasl = -1;
974 ldap_conf.rootuse_sasl = -1;
976 if ((fp = fopen(_PATH_LDAP_CONF, "r")) == NULL)
979 while ((cp = sudo_parseln(fp)) != NULL) {
981 continue; /* skip empty line */
983 /* split into keyword and value */
985 while (*cp && !isblank((unsigned char) *cp))
988 *cp++ = '\0'; /* terminate keyword */
990 /* skip whitespace before value */
991 while (isblank((unsigned char) *cp))
995 /* Look up keyword in config table. */
996 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
997 if (strcasecmp(keyword, cur->conf_str) == 0) {
1000 *(int *)(cur->valp) = _atobool(value);
1003 *(int *)(cur->valp) = atoi(value);
1006 efree(*(char **)(cur->valp));
1007 *(char **)(cur->valp) = estrdup(value);
1011 struct ldap_config_list_str **p;
1012 size_t len = strlen(value);
1015 p = (struct ldap_config_list_str **)cur->valp;
1018 *p = emalloc(sizeof(struct ldap_config_list_str) + len);
1019 memcpy((*p)->val, value, len + 1);
1031 if (!ldap_conf.host)
1032 ldap_conf.host = estrdup("localhost");
1034 if (ldap_conf.bind_timelimit > 0)
1035 ldap_conf.bind_timelimit *= 1000; /* convert to ms */
1037 if (ldap_conf.debug > 1) {
1038 fprintf(stderr, "LDAP Config Summary\n");
1039 fprintf(stderr, "===================\n");
1040 if (ldap_conf.uri) {
1041 struct ldap_config_list_str *uri = ldap_conf.uri;
1044 fprintf(stderr, "uri %s\n", uri->val);
1045 } while ((uri = uri->next) != NULL);
1047 fprintf(stderr, "host %s\n", ldap_conf.host ?
1048 ldap_conf.host : "(NONE)");
1049 fprintf(stderr, "port %d\n", ldap_conf.port);
1051 fprintf(stderr, "ldap_version %d\n", ldap_conf.version);
1052 if (ldap_conf.base) {
1053 struct ldap_config_list_str *base = ldap_conf.base;
1056 fprintf(stderr, "sudoers_base %s\n", base->val);
1057 } while ((base = base->next) != NULL);
1059 fprintf(stderr, "sudoers_base %s\n",
1060 "(NONE) <---Sudo will ignore ldap)");
1062 fprintf(stderr, "binddn %s\n", ldap_conf.binddn ?
1063 ldap_conf.binddn : "(anonymous)");
1064 fprintf(stderr, "bindpw %s\n", ldap_conf.bindpw ?
1065 ldap_conf.bindpw : "(anonymous)");
1066 if (ldap_conf.bind_timelimit > 0)
1067 fprintf(stderr, "bind_timelimit %d\n", ldap_conf.bind_timelimit);
1068 if (ldap_conf.timelimit > 0)
1069 fprintf(stderr, "timelimit %d\n", ldap_conf.timelimit);
1070 fprintf(stderr, "ssl %s\n", ldap_conf.ssl ?
1071 ldap_conf.ssl : "(no)");
1072 if (ldap_conf.tls_checkpeer != -1)
1073 fprintf(stderr, "tls_checkpeer %s\n", ldap_conf.tls_checkpeer ?
1075 if (ldap_conf.tls_cacertfile != NULL)
1076 fprintf(stderr, "tls_cacertfile %s\n", ldap_conf.tls_cacertfile);
1077 if (ldap_conf.tls_cacertdir != NULL)
1078 fprintf(stderr, "tls_cacertdir %s\n", ldap_conf.tls_cacertdir);
1079 if (ldap_conf.tls_random_file != NULL)
1080 fprintf(stderr, "tls_random_file %s\n", ldap_conf.tls_random_file);
1081 if (ldap_conf.tls_cipher_suite != NULL)
1082 fprintf(stderr, "tls_cipher_suite %s\n", ldap_conf.tls_cipher_suite);
1083 if (ldap_conf.tls_certfile != NULL)
1084 fprintf(stderr, "tls_certfile %s\n", ldap_conf.tls_certfile);
1085 if (ldap_conf.tls_keyfile != NULL)
1086 fprintf(stderr, "tls_keyfile %s\n", ldap_conf.tls_keyfile);
1087 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1088 if (ldap_conf.use_sasl != -1) {
1089 fprintf(stderr, "use_sasl %s\n",
1090 ldap_conf.use_sasl ? "yes" : "no");
1091 fprintf(stderr, "sasl_auth_id %s\n", ldap_conf.sasl_auth_id ?
1092 ldap_conf.sasl_auth_id : "(NONE)");
1093 fprintf(stderr, "rootuse_sasl %d\n", ldap_conf.rootuse_sasl);
1094 fprintf(stderr, "rootsasl_auth_id %s\n", ldap_conf.rootsasl_auth_id ?
1095 ldap_conf.rootsasl_auth_id : "(NONE)");
1096 fprintf(stderr, "sasl_secprops %s\n", ldap_conf.sasl_secprops ?
1097 ldap_conf.sasl_secprops : "(NONE)");
1098 fprintf(stderr, "krb5_ccname %s\n", ldap_conf.krb5_ccname ?
1099 ldap_conf.krb5_ccname : "(NONE)");
1102 fprintf(stderr, "===================\n");
1104 if (!ldap_conf.base)
1105 return(FALSE); /* if no base is defined, ignore LDAP */
1108 * Interpret SSL option
1110 if (ldap_conf.ssl != NULL) {
1111 if (strcasecmp(ldap_conf.ssl, "start_tls") == 0)
1112 ldap_conf.ssl_mode = SUDO_LDAP_STARTTLS;
1113 else if (_atobool(ldap_conf.ssl))
1114 ldap_conf.ssl_mode = SUDO_LDAP_SSL;
1117 #if defined(HAVE_LDAPSSL_SET_STRENGTH) && !defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
1118 if (ldap_conf.tls_checkpeer != -1) {
1119 ldapssl_set_strength(NULL,
1120 ldap_conf.tls_checkpeer ? LDAPSSL_AUTH_CERT : LDAPSSL_AUTH_WEAK);
1124 #ifndef HAVE_LDAP_INITIALIZE
1125 /* Convert uri list to host list if no ldap_initialize(). */
1126 if (ldap_conf.uri) {
1127 struct ldap_config_list_str *uri = ldap_conf.uri;
1128 if (sudo_ldap_parse_uri(uri) != 0)
1131 ldap_conf.uri = uri->next;
1133 } while ((uri = ldap_conf.uri));
1134 ldap_conf.port = LDAP_PORT;
1138 if (!ldap_conf.uri) {
1139 /* Use port 389 for plaintext LDAP and port 636 for SSL LDAP */
1140 if (ldap_conf.port < 0)
1142 ldap_conf.ssl_mode == SUDO_LDAP_SSL ? LDAPS_PORT : LDAP_PORT;
1144 #ifdef HAVE_LDAP_CREATE
1146 * Cannot specify port directly to ldap_create(), each host must
1147 * include :port to override the default.
1149 if (ldap_conf.port != LDAP_PORT)
1150 sudo_ldap_conf_add_ports();
1154 /* If rootbinddn set, read in /etc/ldap.secret if it exists. */
1155 if (ldap_conf.rootbinddn)
1156 sudo_ldap_read_secret(_PATH_LDAP_SECRET);
1158 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1160 * Make sure we can open the file specified by krb5_ccname.
1162 if (ldap_conf.krb5_ccname != NULL) {
1163 if (strncasecmp(ldap_conf.krb5_ccname, "FILE:", 5) == 0 ||
1164 strncasecmp(ldap_conf.krb5_ccname, "WRFILE:", 7) == 0) {
1165 value = ldap_conf.krb5_ccname +
1166 (ldap_conf.krb5_ccname[4] == ':' ? 5 : 7);
1167 if ((fp = fopen(value, "r")) != NULL) {
1168 DPRINTF(("using krb5 credential cache: %s", value), 1);
1171 /* Can't open it, just ignore the entry. */
1172 DPRINTF(("unable to open krb5 credential cache: %s", value), 1);
1173 efree(ldap_conf.krb5_ccname);
1174 ldap_conf.krb5_ccname = NULL;
1183 * Extract the dn from an entry and return the first rdn from it.
1186 sudo_ldap_get_first_rdn(ld, entry)
1190 #ifdef HAVE_LDAP_STR2DN
1191 char *dn, *rdn = NULL;
1194 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1196 if (ldap_str2dn(dn, &tmpDN, LDAP_DN_FORMAT_LDAP) == LDAP_SUCCESS) {
1197 ldap_rdn2str(tmpDN[0], &rdn, LDAP_DN_FORMAT_UFN);
1205 if ((dn = ldap_get_dn(ld, entry)) == NULL)
1207 edn = ldap_explode_dn(dn, 1);
1209 return(edn ? edn[0] : NULL);
1214 * Fetch and display the global Options.
1217 sudo_ldap_display_defaults(nss, pw, lbuf)
1218 struct sudo_nss *nss;
1222 struct berval **bv, **p;
1223 struct ldap_config_list_str *base;
1224 LDAP *ld = (LDAP *) nss->handle;
1225 LDAPMessage *entry, *result;
1232 for (base = ldap_conf.base; base != NULL; base = base->next) {
1234 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE,
1235 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1236 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
1237 bv = ldap_get_values_len(ld, entry, "sudoOption");
1239 if (lbuf->len == 0 || isspace((unsigned char)lbuf->buf[lbuf->len - 1]))
1243 for (p = bv; *p != NULL; p++) {
1244 lbuf_append(lbuf, prefix, (*p)->bv_val, NULL);
1248 ldap_value_free_len(bv);
1252 ldap_msgfree(result);
1262 sudo_ldap_display_bound_defaults(nss, pw, lbuf)
1263 struct sudo_nss *nss;
1271 * Print a record in the short form, ala file sudoers.
1274 sudo_ldap_display_entry_short(ld, entry, lbuf)
1279 struct berval **bv, **p;
1282 lbuf_append(lbuf, " (", NULL);
1284 /* get the RunAsUser Values from the entry */
1285 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1287 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1289 for (p = bv; *p != NULL; p++) {
1291 lbuf_append(lbuf, ", ", NULL);
1292 lbuf_append(lbuf, (*p)->bv_val, NULL);
1294 ldap_value_free_len(bv);
1296 lbuf_append(lbuf, def_runas_default, NULL);
1298 /* get the RunAsGroup Values from the entry */
1299 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1301 lbuf_append(lbuf, " : ", NULL);
1302 for (p = bv; *p != NULL; p++) {
1304 lbuf_append(lbuf, ", ", NULL);
1305 lbuf_append(lbuf, (*p)->bv_val, NULL);
1307 ldap_value_free_len(bv);
1309 lbuf_append(lbuf, ") ", NULL);
1311 /* get the Option Values from the entry */
1312 bv = ldap_get_values_len(ld, entry, "sudoOption");
1316 for (p = bv; *p != NULL; p++) {
1321 if (strcmp(cp, "authenticate") == 0)
1322 tag = (*p)->bv_val[0] == '!' ?
1323 "NOPASSWD: " : "PASSWD: ";
1324 else if (strcmp(cp, "noexec") == 0)
1325 tag = (*p)->bv_val[0] == '!' ?
1326 "EXEC: " : "NOEXEC: ";
1327 else if (strcmp(cp, "setenv") == 0)
1328 tag = (*p)->bv_val[0] == '!' ?
1329 "NOSETENV: " : "SETENV: ";
1331 lbuf_append(lbuf, tag, NULL);
1332 /* XXX - ignores other options */
1334 ldap_value_free_len(bv);
1337 /* get the Command Values from the entry */
1338 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1340 for (p = bv; *p != NULL; p++) {
1342 lbuf_append(lbuf, ", ", NULL);
1343 lbuf_append(lbuf, (*p)->bv_val, NULL);
1346 ldap_value_free_len(bv);
1348 lbuf_append(lbuf, "\n", NULL);
1354 * Print a record in the long form.
1357 sudo_ldap_display_entry_long(ld, entry, lbuf)
1362 struct berval **bv, **p;
1366 /* extract the dn, only show the first rdn */
1367 rdn = sudo_ldap_get_first_rdn(ld, entry);
1368 lbuf_append(lbuf, "\nLDAP Role: ", rdn ? rdn : "UNKNOWN", "\n", NULL);
1372 /* get the RunAsUser Values from the entry */
1373 lbuf_append(lbuf, " RunAsUsers: ", NULL);
1374 bv = ldap_get_values_len(ld, entry, "sudoRunAsUser");
1376 bv = ldap_get_values_len(ld, entry, "sudoRunAs");
1378 for (p = bv; *p != NULL; p++) {
1380 lbuf_append(lbuf, ", ", NULL);
1381 lbuf_append(lbuf, (*p)->bv_val, NULL);
1383 ldap_value_free_len(bv);
1385 lbuf_append(lbuf, def_runas_default, NULL);
1386 lbuf_append(lbuf, "\n", NULL);
1388 /* get the RunAsGroup Values from the entry */
1389 bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup");
1391 lbuf_append(lbuf, " RunAsGroups: ", NULL);
1392 for (p = bv; *p != NULL; p++) {
1394 lbuf_append(lbuf, ", ", NULL);
1395 lbuf_append(lbuf, (*p)->bv_val, NULL);
1397 ldap_value_free_len(bv);
1398 lbuf_append(lbuf, "\n", NULL);
1401 /* get the Option Values from the entry */
1402 bv = ldap_get_values_len(ld, entry, "sudoOption");
1404 lbuf_append(lbuf, " Options: ", NULL);
1405 for (p = bv; *p != NULL; p++) {
1407 lbuf_append(lbuf, ", ", NULL);
1408 lbuf_append(lbuf, (*p)->bv_val, NULL);
1410 ldap_value_free_len(bv);
1411 lbuf_append(lbuf, "\n", NULL);
1414 /* get the Command Values from the entry */
1415 bv = ldap_get_values_len(ld, entry, "sudoCommand");
1417 lbuf_append(lbuf, " Commands:\n", NULL);
1418 for (p = bv; *p != NULL; p++) {
1419 lbuf_append(lbuf, "\t", (*p)->bv_val, "\n", NULL);
1422 ldap_value_free_len(bv);
1429 * Like sudo_ldap_lookup(), except we just print entries.
1432 sudo_ldap_display_privs(nss, pw, lbuf)
1433 struct sudo_nss *nss;
1437 struct ldap_config_list_str *base;
1438 LDAP *ld = (LDAP *) nss->handle;
1439 LDAPMessage *entry, *result;
1441 int rc, do_netgr, count = 0;
1447 * Okay - time to search for anything that matches this user
1448 * Lets limit it to only two queries of the LDAP server
1450 * The first pass will look by the username, groups, and
1451 * the keyword ALL. We will then inspect the results that
1452 * came back from the query. We don't need to inspect the
1453 * sudoUser in this pass since the LDAP server already scanned
1456 * The second pass will return all the entries that contain
1457 * user netgroups. Then we take the netgroups returned and
1458 * try to match them against the username.
1460 for (do_netgr = 0; do_netgr < 2; do_netgr++) {
1461 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1462 DPRINTF(("ldap search '%s'", filt), 1);
1463 for (base = ldap_conf.base; base != NULL; base = base->next) {
1465 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE, filt,
1466 NULL, 0, NULL, NULL, NULL, 0, &result);
1467 if (rc != LDAP_SUCCESS)
1468 continue; /* no entries for this pass */
1470 /* print each matching entry */
1471 LDAP_FOREACH(entry, ld, result) {
1473 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1474 sudo_ldap_check_host(ld, entry)) {
1477 count += sudo_ldap_display_entry_long(ld, entry, lbuf);
1479 count += sudo_ldap_display_entry_short(ld, entry, lbuf);
1482 ldap_msgfree(result);
1491 sudo_ldap_display_cmnd(nss, pw)
1492 struct sudo_nss *nss;
1495 struct ldap_config_list_str *base;
1496 LDAP *ld = (LDAP *) nss->handle;
1497 LDAPMessage *entry, *result; /* used for searches */
1498 char *filt; /* used to parse attributes */
1499 int rc, found, do_netgr; /* temp/final return values */
1505 * Okay - time to search for anything that matches this user
1506 * Lets limit it to only two queries of the LDAP server
1508 * The first pass will look by the username, groups, and
1509 * the keyword ALL. We will then inspect the results that
1510 * came back from the query. We don't need to inspect the
1511 * sudoUser in this pass since the LDAP server already scanned
1514 * The second pass will return all the entries that contain
1515 * user netgroups. Then we take the netgroups returned and
1516 * try to match them against the username.
1518 for (found = FALSE, do_netgr = 0; !found && do_netgr < 2; do_netgr++) {
1519 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1520 DPRINTF(("ldap search '%s'", filt), 1);
1521 for (base = ldap_conf.base; base != NULL; base = base->next) {
1523 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE, filt,
1524 NULL, 0, NULL, NULL, NULL, 0, &result);
1525 if (rc != LDAP_SUCCESS)
1526 continue; /* no entries for this pass */
1528 LDAP_FOREACH(entry, ld, result) {
1530 sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1531 sudo_ldap_check_host(ld, entry) &&
1532 sudo_ldap_check_command(ld, entry, NULL) &&
1533 sudo_ldap_check_runas(ld, entry)) {
1539 ldap_msgfree(result);
1545 printf("%s%s%s\n", safe_cmnd ? safe_cmnd : user_cmnd,
1546 user_args ? " " : "", user_args ? user_args : "");
1550 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1552 sudo_ldap_sasl_interact(ld, flags, _auth_id, _interact)
1558 char *auth_id = (char *)_auth_id;
1559 sasl_interact_t *interact = (sasl_interact_t *)_interact;
1561 for (; interact->id != SASL_CB_LIST_END; interact++) {
1562 if (interact->id != SASL_CB_USER)
1563 return(LDAP_PARAM_ERROR);
1565 if (auth_id != NULL)
1566 interact->result = auth_id;
1567 else if (interact->defresult != NULL)
1568 interact->result = interact->defresult;
1570 interact->result = "";
1572 interact->len = strlen(interact->result);
1573 #if SASL_VERSION_MAJOR < 2
1574 interact->result = estrdup(interact->result);
1575 #endif /* SASL_VERSION_MAJOR < 2 */
1577 return(LDAP_SUCCESS);
1579 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1582 * Set LDAP options based on the config table.
1585 sudo_ldap_set_options(ld)
1588 struct ldap_config_table *cur;
1591 /* Set ber options */
1592 #ifdef LBER_OPT_DEBUG_LEVEL
1593 if (ldap_conf.ldap_debug)
1594 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_conf.ldap_debug);
1597 /* Set simple LDAP options */
1598 for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) {
1603 if (cur->opt_val == -1)
1606 conn = cur->connected ? ld : NULL;
1607 switch (cur->type) {
1610 ival = *(int *)(cur->valp);
1612 rc = ldap_set_option(conn, cur->opt_val, &ival);
1613 if (rc != LDAP_OPT_SUCCESS) {
1614 warningx("ldap_set_option: %s -> %d: %s",
1615 cur->conf_str, ival, ldap_err2string(rc));
1618 DPRINTF(("ldap_set_option: %s -> %d", cur->conf_str, ival), 1);
1622 sval = *(char **)(cur->valp);
1624 rc = ldap_set_option(conn, cur->opt_val, sval);
1625 if (rc != LDAP_OPT_SUCCESS) {
1626 warningx("ldap_set_option: %s -> %s: %s",
1627 cur->conf_str, sval, ldap_err2string(rc));
1630 DPRINTF(("ldap_set_option: %s -> %s", cur->conf_str, sval), 1);
1636 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1637 /* Convert bind_timelimit to a timeval */
1638 if (ldap_conf.bind_timelimit > 0) {
1640 tv.tv_sec = ldap_conf.bind_timelimit / 1000;
1642 rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
1643 if (rc != LDAP_OPT_SUCCESS) {
1644 warningx("ldap_set_option(NETWORK_TIMEOUT, %ld): %s",
1645 (long)tv.tv_sec, ldap_err2string(rc));
1648 DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)",
1649 (long)tv.tv_sec), 1);
1653 #if defined(LDAP_OPT_X_TLS) && !defined(HAVE_LDAPSSL_INIT)
1654 if (ldap_conf.ssl_mode == SUDO_LDAP_SSL) {
1655 int val = LDAP_OPT_X_TLS_HARD;
1656 rc = ldap_set_option(ld, LDAP_OPT_X_TLS, &val);
1657 if (rc != LDAP_SUCCESS) {
1658 warningx("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD): %s",
1659 ldap_err2string(rc));
1662 DPRINTF(("ldap_set_option(LDAP_OPT_X_TLS, LDAP_OPT_X_TLS_HARD)"), 1);
1669 * Connect to the LDAP server specified by ld
1672 sudo_ldap_bind_s(ld)
1676 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1677 const char *old_ccname = user_ccname;
1678 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
1679 unsigned int status;
1683 #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S
1684 if (ldap_conf.rootuse_sasl == TRUE ||
1685 (ldap_conf.rootuse_sasl != FALSE && ldap_conf.use_sasl == TRUE)) {
1686 void *auth_id = ldap_conf.rootsasl_auth_id ?
1687 ldap_conf.rootsasl_auth_id : ldap_conf.sasl_auth_id;
1689 if (ldap_conf.krb5_ccname != NULL) {
1690 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
1691 if (gss_krb5_ccache_name(&status, ldap_conf.krb5_ccname, &old_ccname)
1692 != GSS_S_COMPLETE) {
1694 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1697 setenv("KRB5CCNAME", ldap_conf.krb5_ccname, TRUE);
1700 rc = ldap_sasl_interactive_bind_s(ld, ldap_conf.binddn, "GSSAPI",
1701 NULL, NULL, LDAP_SASL_QUIET, sudo_ldap_sasl_interact, auth_id);
1702 if (ldap_conf.krb5_ccname != NULL) {
1703 # ifdef HAVE_GSS_KRB5_CCACHE_NAME
1704 if (gss_krb5_ccache_name(&status, old_ccname, NULL) != GSS_S_COMPLETE)
1705 DPRINTF(("gss_krb5_ccache_name() failed: %d", status), 1);
1707 if (old_ccname != NULL)
1708 setenv("KRB5CCNAME", old_ccname, TRUE);
1710 unsetenv("KRB5CCNAME");
1713 if (rc != LDAP_SUCCESS) {
1714 warningx("ldap_sasl_interactive_bind_s(): %s", ldap_err2string(rc));
1717 DPRINTF(("ldap_sasl_interactive_bind_s() ok"), 1);
1719 #endif /* HAVE_LDAP_SASL_INTERACTIVE_BIND_S */
1720 #ifdef HAVE_LDAP_SASL_BIND_S
1724 bv.bv_val = ldap_conf.bindpw ? ldap_conf.bindpw : "";
1725 bv.bv_len = strlen(bv.bv_val);
1727 rc = ldap_sasl_bind_s(ld, ldap_conf.binddn, LDAP_SASL_SIMPLE, &bv,
1729 if (rc != LDAP_SUCCESS) {
1730 warningx("ldap_sasl_bind_s(): %s", ldap_err2string(rc));
1733 DPRINTF(("ldap_sasl_bind_s() ok"), 1);
1737 rc = ldap_simple_bind_s(ld, ldap_conf.binddn, ldap_conf.bindpw);
1738 if (rc != LDAP_SUCCESS) {
1739 warningx("ldap_simple_bind_s(): %s", ldap_err2string(rc));
1742 DPRINTF(("ldap_simple_bind_s() ok"), 1);
1749 * Open a connection to the LDAP server.
1750 * Returns 0 on success and non-zero on failure.
1754 struct sudo_nss *nss;
1757 int rc, ldapnoinit = FALSE;
1759 if (!sudo_ldap_read_config())
1762 /* Prevent reading of user ldaprc and system defaults. */
1763 if (getenv("LDAPNOINIT") == NULL) {
1765 setenv("LDAPNOINIT", "1", TRUE);
1768 /* Connect to LDAP server */
1769 #ifdef HAVE_LDAP_INITIALIZE
1770 if (ldap_conf.uri != NULL) {
1771 char *buf = sudo_ldap_join_uri(ldap_conf.uri);
1772 DPRINTF(("ldap_initialize(ld, %s)", buf), 2);
1773 rc = ldap_initialize(&ld, buf);
1777 rc = sudo_ldap_init(&ld, ldap_conf.host, ldap_conf.port);
1778 if (rc != LDAP_SUCCESS) {
1779 warningx("unable to initialize LDAP: %s", ldap_err2string(rc));
1784 unsetenv("LDAPNOINIT");
1786 /* Set LDAP options */
1787 if (sudo_ldap_set_options(ld) < 0)
1790 if (ldap_conf.ssl_mode == SUDO_LDAP_STARTTLS) {
1791 #if defined(HAVE_LDAP_START_TLS_S)
1792 rc = ldap_start_tls_s(ld, NULL, NULL);
1793 if (rc != LDAP_SUCCESS) {
1794 warningx("ldap_start_tls_s(): %s", ldap_err2string(rc));
1797 DPRINTF(("ldap_start_tls_s() ok"), 1);
1798 #elif defined(HAVE_LDAP_SSL_CLIENT_INIT) && defined(HAVE_LDAP_START_TLS_S_NP)
1799 if (ldap_ssl_client_init(NULL, NULL, 0, &rc) != LDAP_SUCCESS) {
1800 warningx("ldap_ssl_client_init(): %s", ldap_err2string(rc));
1803 rc = ldap_start_tls_s_np(ld, NULL);
1804 if (rc != LDAP_SUCCESS) {
1805 warningx("ldap_start_tls_s_np(): %s", ldap_err2string(rc));
1808 DPRINTF(("ldap_start_tls_s_np() ok"), 1);
1810 warningx("start_tls specified but LDAP libs do not support ldap_start_tls_s() or ldap_start_tls_s_np()");
1811 #endif /* !HAVE_LDAP_START_TLS_S && !HAVE_LDAP_START_TLS_S_NP */
1814 /* Actually connect */
1815 if (sudo_ldap_bind_s(ld) != 0)
1823 sudo_ldap_setdefs(nss)
1824 struct sudo_nss *nss;
1826 struct ldap_config_list_str *base;
1827 LDAP *ld = (LDAP *) nss->handle;
1828 LDAPMessage *entry, *result; /* used for searches */
1829 int rc; /* temp return value */
1834 for (base = ldap_conf.base; base != NULL; base = base->next) {
1836 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE,
1837 "cn=defaults", NULL, 0, NULL, NULL, NULL, 0, &result);
1838 if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) {
1839 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1840 sudo_ldap_parse_options(ld, entry);
1842 DPRINTF(("no default options found in %s", base->val), 1);
1845 ldap_msgfree(result);
1852 * like sudoers_lookup() - only LDAP style
1855 sudo_ldap_lookup(nss, ret, pwflag)
1856 struct sudo_nss *nss;
1860 struct ldap_config_list_str *base;
1861 LDAP *ld = (LDAP *) nss->handle;
1862 LDAPMessage *entry, *result;
1864 int do_netgr, rc, matched;
1866 int ldap_user_matches = FALSE, ldap_host_matches = FALSE;
1867 struct passwd *pw = list_pw ? list_pw : sudo_user.pw;
1873 int doauth = UNSPEC;
1874 enum def_tupple pwcheck =
1875 (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
1877 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1878 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1879 for (base = ldap_conf.base; base != NULL; base = base->next) {
1881 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE, filt,
1882 NULL, 0, NULL, NULL, NULL, 0, &result);
1883 if (rc != LDAP_SUCCESS)
1886 LDAP_FOREACH(entry, ld, result) {
1887 /* only verify netgroup matches in pass 2 */
1888 if (do_netgr && !sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name))
1891 ldap_user_matches = TRUE;
1892 if (sudo_ldap_check_host(ld, entry)) {
1893 ldap_host_matches = TRUE;
1894 if ((pwcheck == any && doauth != FALSE) ||
1895 (pwcheck == all && doauth == FALSE))
1896 doauth = sudo_ldap_check_bool(ld, entry, "authenticate");
1897 /* Only check the command when listing another user. */
1898 if (user_uid == 0 || list_pw == NULL ||
1899 user_uid == list_pw->pw_uid ||
1900 sudo_ldap_check_command(ld, entry, NULL)) {
1902 break; /* end foreach */
1906 ldap_msgfree(result);
1910 if (matched || user_uid == 0) {
1911 SET(ret, VALIDATE_OK);
1912 CLR(ret, VALIDATE_NOT_OK);
1913 if (def_authenticate) {
1916 SET(ret, FLAG_CHECK_USER);
1920 if (doauth == FALSE)
1921 def_authenticate = FALSE;
1924 def_authenticate = FALSE;
1935 * Okay - time to search for anything that matches this user
1936 * Lets limit it to only two queries of the LDAP server
1938 * The first pass will look by the username, groups, and
1939 * the keyword ALL. We will then inspect the results that
1940 * came back from the query. We don't need to inspect the
1941 * sudoUser in this pass since the LDAP server already scanned
1944 * The second pass will return all the entries that contain
1945 * user netgroups. Then we take the netgroups returned and
1946 * try to match them against the username.
1948 setenv_implied = FALSE;
1949 for (matched = 0, do_netgr = 0; !matched && do_netgr < 2; do_netgr++) {
1950 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1(pw);
1951 DPRINTF(("ldap search '%s'", filt), 1);
1952 for (base = ldap_conf.base; base != NULL; base = base->next) {
1954 rc = ldap_search_ext_s(ld, base->val, LDAP_SCOPE_SUBTREE, filt,
1955 NULL, 0, NULL, NULL, NULL, 0, &result);
1956 if (rc != LDAP_SUCCESS) {
1957 DPRINTF(("nothing found for '%s'", filt), 1);
1961 /* parse each entry returned from this most recent search */
1962 LDAP_FOREACH(entry, ld, result) {
1963 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
1965 /* first verify user netgroup matches - only if in pass 2 */
1966 (!do_netgr || sudo_ldap_check_user_netgroup(ld, entry, pw->pw_name)) &&
1967 /* remember that user matched */
1968 (ldap_user_matches = TRUE) &&
1969 /* verify host match */
1970 sudo_ldap_check_host(ld, entry) &&
1971 /* remember that host matched */
1972 (ldap_host_matches = TRUE) &&
1973 /* verify runas match */
1974 sudo_ldap_check_runas(ld, entry) &&
1975 /* verify command match */
1976 (rc = sudo_ldap_check_command(ld, entry, &setenv_implied)) != UNSPEC
1978 /* We have a match! */
1979 DPRINTF(("Command %sallowed", rc == TRUE ? "" : "NOT "), 1);
1982 /* pick up any options */
1985 sudo_ldap_parse_options(ld, entry);
1987 /* Set role and type if not specified on command line. */
1988 if (user_role == NULL)
1989 user_role = def_role;
1990 if (user_type == NULL)
1991 user_type = def_type;
1992 #endif /* HAVE_SELINUX */
1993 /* make sure we don't reenter loop */
1994 SET(ret, VALIDATE_OK);
1995 CLR(ret, VALIDATE_NOT_OK);
1997 SET(ret, VALIDATE_NOT_OK);
1998 CLR(ret, VALIDATE_OK);
2000 /* break from inside for loop */
2004 ldap_msgfree(result);
2010 DPRINTF(("user_matches=%d", ldap_user_matches), 1);
2011 DPRINTF(("host_matches=%d", ldap_host_matches), 1);
2013 if (!ISSET(ret, VALIDATE_OK)) {
2014 /* we do not have a match */
2015 if (pwflag && list_pw == NULL)
2016 SET(ret, FLAG_NO_CHECK);
2018 if (ldap_user_matches)
2019 CLR(ret, FLAG_NO_USER);
2020 if (ldap_host_matches)
2021 CLR(ret, FLAG_NO_HOST);
2022 DPRINTF(("sudo_ldap_lookup(%d)=0x%02x", pwflag, ret), 1);
2028 * shut down LDAP connection
2031 sudo_ldap_close(nss)
2032 struct sudo_nss *nss;
2034 if (nss->handle != NULL) {
2035 ldap_unbind_ext_s((LDAP *) nss->handle, NULL, NULL);
2045 sudo_ldap_parse(nss)
2046 struct sudo_nss *nss;