2 * Copyright (c) 2003-2005 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>
56 # include "emul/err.h"
57 #endif /* HAVE_ERR_H */
68 __unused static const char rcsid[] = "$Sudo: ldap.c,v 1.11.2.16 2007/09/04 14:58:46 millert Exp $";
72 # define LINE_MAX 2048
75 #ifndef LDAP_OPT_SUCCESS
76 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
79 #if defined(LDAP_X_OPT_CONNECT_TIMEOUT) && !defined(LDAP_OPT_X_CONNECT_TIMEOUT)
80 #define LDAP_OPT_X_CONNECT_TIMEOUT LDAP_OPT_X_CONNECT_TIMEOUT
83 #define DPRINTF(args, level) if (ldap_conf.debug >= level) warnx args
85 /* ldap configuration structure */
100 char *tls_cacertfile;
102 char *tls_random_file;
103 char *tls_cipher_suite;
108 static void sudo_ldap_update_defaults __P((LDAP *));
109 static void sudo_ldap_close __P((LDAP *));
110 static LDAP *sudo_ldap_open __P((void));
113 * Walk through search results and return TRUE if we have a matching
114 * netgroup, else FALSE.
117 sudo_ldap_check_user_netgroup(ld, entry)
121 char **v = NULL, **p = NULL;
127 /* get the values from the entry */
128 v = ldap_get_values(ld, entry, "sudoUser");
130 /* walk through values */
131 for (p = v; p && *p && !ret; p++) {
133 if (netgr_matches(*p, NULL, NULL, user_name))
135 DPRINTF(("ldap sudoUser netgroup '%s' ... %s", *p,
136 ret ? "MATCH!" : "not"), 2);
140 ldap_value_free(v); /* cleanup */
146 * Walk through search results and return TRUE if we have a
147 * host match, else FALSE.
150 sudo_ldap_check_host(ld, entry)
154 char **v = NULL, **p = NULL;
160 /* get the values from the entry */
161 v = ldap_get_values(ld, entry, "sudoHost");
163 /* walk through values */
164 for (p = v; p && *p && !ret; p++) {
165 /* match any or address or netgroup or hostname */
166 if (!strcasecmp(*p, "ALL") || addr_matches(*p) ||
167 netgr_matches(*p, user_host, user_shost, NULL) ||
168 !hostname_matches(user_shost, user_host, *p))
170 DPRINTF(("ldap sudoHost '%s' ... %s", *p,
171 ret ? "MATCH!" : "not"), 2);
175 ldap_value_free(v); /* cleanup */
181 * Walk through search results and return TRUE if we have a runas match,
183 * Since the runas directive in /etc/sudoers is optional, so is sudoRunAs.
186 sudo_ldap_check_runas(ld, entry)
190 char **v = NULL, **p = NULL;
196 /* get the values from the entry */
197 v = ldap_get_values(ld, entry, "sudoRunAs");
202 * if runas is not specified on the command line, the only information
203 * as to which user to run as is in the runas_default option. We should
204 * check to see if we have the local option present. Unfortunately we
205 * don't parse these options until after this routine says yes or no.
206 * The query has already returned, so we could peek at the attribute
207 * values here though.
209 * For now just require users to always use -u option unless its set
210 * in the global defaults. This behaviour is no different than the global
213 * Sigh - maybe add this feature later
218 * If there are no runas entries, match runas_default against
219 * what the user specified on the command line.
222 ret = !strcasecmp(runas_pw->pw_name, def_runas_default);
224 /* walk through values returned, looking for a match */
225 for (p = v; p && *p && !ret; p++) {
228 if (netgr_matches(*p, NULL, NULL, runas_pw->pw_name))
232 if (usergr_matches(*p, runas_pw->pw_name, runas_pw))
236 if (strcmp(*p, "ALL") == 0) {
242 if (strcasecmp(*p, runas_pw->pw_name) == 0)
246 DPRINTF(("ldap sudoRunAs '%s' ... %s", *p,
247 ret ? "MATCH!" : "not"), 2);
251 ldap_value_free(v); /* cleanup */
257 * Walk through search results and return TRUE if we have a command match.
260 sudo_ldap_check_command(ld, entry)
264 char *allowed_cmnd, *allowed_args, **v = NULL, **p = NULL;
265 int foundbang, ret = FALSE;
270 v = ldap_get_values(ld, entry, "sudoCommand");
272 /* get_first_entry */
273 for (p = v; p && *p && ret >= 0; p++) {
274 /* Match against ALL ? */
275 if (!strcasecmp(*p, "ALL")) {
277 DPRINTF(("ldap sudoCommand '%s' ... MATCH!", *p), 2);
281 /* check for !command */
284 allowed_cmnd = estrdup(1 + *p); /* !command */
287 allowed_cmnd = estrdup(*p); /* command */
290 /* split optional args away from command */
291 allowed_args = strchr(allowed_cmnd, ' ');
293 *allowed_args++ = '\0';
295 /* check the command like normal */
296 if (command_matches(allowed_cmnd, allowed_args)) {
298 * If allowed (no bang) set ret but keep on checking.
299 * If disallowed (bang), exit loop.
301 ret = foundbang ? -1 : TRUE;
303 DPRINTF(("ldap sudoCommand '%s' ... %s", *p,
304 ret == TRUE ? "MATCH!" : "not"), 2);
306 efree(allowed_cmnd); /* cleanup */
310 ldap_value_free(v); /* more cleanup */
312 /* return TRUE if we found at least one ALLOW and no DENY */
317 * Read sudoOption and modify the defaults as we go. This is used once
318 * from the cn=defaults entry and also once when a final sudoRole is matched.
321 sudo_ldap_parse_options(ld, entry)
325 char op, *var, *val, **v = NULL, **p = NULL;
330 v = ldap_get_values(ld, entry, "sudoOption");
332 /* walk through options */
333 for (p = v; p && *p; p++) {
335 DPRINTF(("ldap sudoOption: '%s'", *p), 2);
338 /* check for equals sign past first char */
339 val = strchr(var, '=');
341 *val++ = '\0'; /* split on = and truncate var */
342 op = *(val - 2); /* peek for += or -= cases */
343 if (op == '+' || op == '-') {
344 *(val - 2) = '\0'; /* found, remove extra char */
345 /* case var+=val or var-=val */
346 set_default(var, val, (int) op);
349 set_default(var, val, TRUE);
351 } else if (*var == '!') {
352 /* case !var Boolean False */
353 set_default(var + 1, NULL, FALSE);
355 /* case var Boolean True */
356 set_default(var, NULL, TRUE);
366 * Concatenate strings, dynamically growing them as necessary.
367 * Strings can be arbitrarily long and are allocated/reallocated on
368 * the fly. Make sure to free them when you are done.
375 * ncat(&s,&sz,"This ");
376 * ncat(&s,&sz,"is ");
377 * ncat(&s,&sz,"an ");
378 * ncat(&s,&sz,"arbitrarily ");
379 * ncat(&s,&sz,"long ");
380 * ncat(&s,&sz,"string!");
382 * printf("String Value='%s', but has %d bytes allocated\n",s,sz);
393 /* handle initial alloc */
396 *sz = strlen(src) + 1;
400 nsz = strlen(*s) + strlen(src) + 1;
402 *s = erealloc((void *) *s, *sz = nsz * 2);
403 strlcat(*s, src, *sz);
407 * builds together a filter to check against ldap
410 sudo_ldap_build_pass1()
420 /* build filter sudoUser=user_name */
421 ncat(&b, &sz, "(sudoUser=");
422 ncat(&b, &sz, user_name);
425 /* Append primary group */
426 grp = getgrgid(user_gid);
428 ncat(&b, &sz, "(sudoUser=%");
429 ncat(&b, &sz, grp -> gr_name);
433 /* Append supplementary groups */
434 for (i = 0; i < user_ngroups; i++) {
435 if ((grp = getgrgid(user_groups[i])) != NULL) {
436 ncat(&b, &sz, "(sudoUser=%");
437 ncat(&b, &sz, grp -> gr_name);
442 /* Add ALL to list */
443 ncat(&b, &sz, "(sudoUser=ALL)");
452 * Map yes/true/on to TRUE, no/false/off to FALSE, else -1
461 if (strcasecmp(s, "yes") == 0)
466 if (strcasecmp(s, "true") == 0)
471 if (strcasecmp(s, "on") == 0)
473 if (strcasecmp(s, "off") == 0)
478 if (strcasecmp(s, "no") == 0)
483 if (strcasecmp(s, "false") == 0)
491 sudo_ldap_read_config()
494 char buf[LINE_MAX], *c, *keyword, *value;
497 ldap_conf.version = 3;
498 ldap_conf.port = 389;
499 ldap_conf.tls_checkpeer = -1;
500 ldap_conf.timelimit = -1;
501 ldap_conf.bind_timelimit = -1;
503 if ((f = fopen(_PATH_LDAP_CONF, "r")) == NULL)
505 while (fgets(buf, sizeof(buf), f)) {
506 /* ignore text after comment character */
507 if ((c = strchr(buf, '#')) != NULL)
510 /* skip leading whitespace */
511 for (c = buf; isspace((unsigned char) *c); c++)
514 if (*c == '\0' || *c == '\n')
515 continue; /* skip empty line */
517 /* properly terminate keyword string */
519 while (*c && !isspace((unsigned char) *c))
522 *c++ = '\0'; /* terminate keyword */
524 /* skip whitespace before value */
525 while (isspace((unsigned char) *c))
529 /* trim whitespace after value */
531 c++; /* wind to end */
532 while (--c > value && isspace((unsigned char) *c))
535 /* The following macros make the code much more readable */
537 #define MATCH_S(x,y) if (!strcasecmp(keyword,x)) \
538 { efree(y); y=estrdup(value); }
539 #define MATCH_I(x,y) if (!strcasecmp(keyword,x)) { y=atoi(value); }
540 #define MATCH_B(x,y) if (!strcasecmp(keyword,x)) { y=_atobool(value); }
543 * Parse values using a continues chain of if else if else if else if
546 MATCH_S("host", ldap_conf.host)
548 MATCH_I("port", ldap_conf.port)
550 MATCH_S("ssl", ldap_conf.ssl)
552 MATCH_B("tls_checkpeer", ldap_conf.tls_checkpeer)
554 MATCH_S("tls_cacertfile", ldap_conf.tls_cacertfile)
556 MATCH_S("tls_cacertdir", ldap_conf.tls_cacertdir)
558 MATCH_S("tls_randfile", ldap_conf.tls_random_file)
560 MATCH_S("tls_ciphers", ldap_conf.tls_cipher_suite)
562 MATCH_S("tls_cert", ldap_conf.tls_certfile)
564 MATCH_S("tls_key", ldap_conf.tls_keyfile)
566 MATCH_I("ldap_version", ldap_conf.version)
568 MATCH_I("bind_timelimit", ldap_conf.bind_timelimit)
570 MATCH_I("timelimit", ldap_conf.timelimit)
572 MATCH_S("uri", ldap_conf.uri)
574 MATCH_S("binddn", ldap_conf.binddn)
576 MATCH_S("bindpw", ldap_conf.bindpw)
578 MATCH_S("rootbinddn", ldap_conf.rootbinddn)
580 MATCH_S("sudoers_base", ldap_conf.base)
582 MATCH_I("sudoers_debug", ldap_conf.debug)
586 * The keyword was unrecognized. Since this config file is
587 * shared by multiple programs, it is appropriate to silently
588 * ignore options this program does not understand
596 ldap_conf.host = estrdup("localhost");
598 if (ldap_conf.bind_timelimit > 0)
599 ldap_conf.bind_timelimit *= 1000; /* convert to ms */
601 if (ldap_conf.debug > 1) {
602 fprintf(stderr, "LDAP Config Summary\n");
603 fprintf(stderr, "===================\n");
604 #ifdef HAVE_LDAP_INITIALIZE
606 fprintf(stderr, "uri %s\n", ldap_conf.uri);
610 fprintf(stderr, "host %s\n", ldap_conf.host ?
611 ldap_conf.host : "(NONE)");
612 fprintf(stderr, "port %d\n", ldap_conf.port);
614 fprintf(stderr, "ldap_version %d\n", ldap_conf.version);
616 fprintf(stderr, "sudoers_base %s\n", ldap_conf.base ?
617 ldap_conf.base : "(NONE) <---Sudo will ignore ldap)");
618 fprintf(stderr, "binddn %s\n", ldap_conf.binddn ?
619 ldap_conf.binddn : "(anonymous)");
620 fprintf(stderr, "bindpw %s\n", ldap_conf.bindpw ?
621 ldap_conf.bindpw : "(anonymous)");
622 fprintf(stderr, "bind_timelimit %d\n", ldap_conf.bind_timelimit);
623 fprintf(stderr, "timelimit %d\n", ldap_conf.timelimit);
624 #ifdef HAVE_LDAP_START_TLS_S
625 fprintf(stderr, "ssl %s\n", ldap_conf.ssl ?
626 ldap_conf.ssl : "(no)");
628 fprintf(stderr, "===================\n");
631 return(FALSE); /* if no base is defined, ignore LDAP */
633 /* If rootbinddn set, read in /etc/ldap.secret if it exists. */
634 if (ldap_conf.rootbinddn) {
635 if ((f = fopen(_PATH_LDAP_SECRET, "r")) != NULL) {
636 if (fgets(buf, sizeof(buf), f) != NULL) {
637 /* removing trailing newlines */
638 for (c = buf; *c != '\0'; c++)
640 while (--c > buf && *c == '\n')
642 /* copy to bindpw and binddn */
643 efree(ldap_conf.bindpw);
644 ldap_conf.bindpw = estrdup(buf);
645 efree(ldap_conf.binddn);
646 ldap_conf.binddn = ldap_conf.rootbinddn;
647 ldap_conf.rootbinddn = NULL;
656 * like perl's join(sep,@ARGS)
659 _ldap_join_values(sep, v)
663 char *b = NULL, **p = NULL;
666 /* paste values together */
667 for (p = v; p && *p; p++) {
668 if (p != v && sep != NULL)
669 ncat(&b, &sz, sep); /* append seperator */
670 ncat(&b, &sz, *p); /* append value */
675 /* something went wrong, put something here */
676 ncat(&b, &sz, "(empty list)"); /* append value */
682 char *sudo_ldap_cm_list = NULL;
683 size_t sudo_ldap_cm_list_size;
685 #define SAVE_LIST(x) ncat(&sudo_ldap_cm_list,&sudo_ldap_cm_list_size,(x))
687 * Walks through search result and returns TRUE if we have a
691 sudo_ldap_add_match(ld, entry, pwflag)
696 char *dn, **edn, **v = NULL;
698 /* if we are not collecting matches, then don't save them */
699 if (pwflag != I_LISTPW)
702 /* collect the dn, only show the rdn */
703 dn = ldap_get_dn(ld, entry);
704 edn = dn ? ldap_explode_dn(dn, 1) : NULL;
705 SAVE_LIST("\nLDAP Role: ");
706 SAVE_LIST((edn && *edn) ? *edn : "UNKNOWN");
711 ldap_value_free(edn);
713 /* get the Runas Values from the entry */
714 v = ldap_get_values(ld, entry, "sudoRunAs");
716 SAVE_LIST(" RunAs: (");
717 SAVE_LIST(_ldap_join_values(", ", v));
723 /* get the Command Values from the entry */
724 v = ldap_get_values(ld, entry, "sudoCommand");
726 SAVE_LIST(" Commands:\n ");
727 SAVE_LIST(_ldap_join_values("\n ", v));
730 SAVE_LIST(" Commands: NONE\n");
735 return(FALSE); /* Don't stop at the first match */
740 sudo_ldap_list_matches()
742 if (sudo_ldap_cm_list != NULL)
743 printf("%s", sudo_ldap_cm_list);
746 /* macros to set option, error on failure plus consistent debugging */
747 #define SET_OPTS(opt, val) do { \
748 if (ldap_conf.val != NULL) { \
749 if (ldap_conf.debug > 1) \
751 "ldap_set_option(LDAP_OPT_%s, \"%s\")\n", #opt, ldap_conf.val);\
752 rc = ldap_set_option(ld, LDAP_OPT_ ## opt, ldap_conf.val); \
753 if (rc != LDAP_OPT_SUCCESS) { \
754 fprintf(stderr,"ldap_set_option(LDAP_OPT_%s, \"%s\")=%d: %s\n", \
755 #opt, ldap_conf.val, rc, ldap_err2string(rc)); \
760 #define SET_OPTI(opt, val) do { \
761 if (ldap_conf.val >= 0) { \
762 if (ldap_conf.debug > 1) \
764 "ldap_set_option(LDAP_OPT_%s, %d)\n", #opt, ldap_conf.val); \
765 rc = ldap_set_option(ld, LDAP_OPT_ ## opt, &ldap_conf.val); \
766 if (rc != LDAP_OPT_SUCCESS) { \
767 fprintf(stderr,"ldap_set_option(LDAP_OPT_%s, %d)=%d: %s\n", \
768 #opt, ldap_conf.val, rc, ldap_err2string(rc)); \
775 * Open a connection to the LDAP server.
783 if (!sudo_ldap_read_config())
786 /* attempt to setup ssl options */
787 #ifdef LDAP_OPT_X_TLS_CACERTFILE
788 SET_OPTS(X_TLS_CACERTFILE, tls_cacertfile);
789 #endif /* LDAP_OPT_X_TLS_CACERTFILE */
791 #ifdef LDAP_OPT_X_TLS_CACERTDIR
792 SET_OPTS(X_TLS_CACERTDIR, tls_cacertdir);
793 #endif /* LDAP_OPT_X_TLS_CACERTDIR */
795 #ifdef LDAP_OPT_X_TLS_CERTFILE
796 SET_OPTS(X_TLS_CERTFILE, tls_certfile);
797 #endif /* LDAP_OPT_X_TLS_CERTFILE */
799 #ifdef LDAP_OPT_X_TLS_KEYFILE
800 SET_OPTS(X_TLS_KEYFILE, tls_keyfile);
801 #endif /* LDAP_OPT_X_TLS_KEYFILE */
803 #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
804 SET_OPTS(X_TLS_CIPHER_SUITE, tls_cipher_suite);
805 #endif /* LDAP_OPT_X_TLS_CIPHER_SUITE */
807 #ifdef LDAP_OPT_X_TLS_RANDOM_FILE
808 SET_OPTS(X_TLS_RANDOM_FILE, tls_random_file);
809 #endif /* LDAP_OPT_X_TLS_RANDOM_FILE */
811 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
812 /* check the server certificate? */
813 SET_OPTI(X_TLS_REQUIRE_CERT, tls_checkpeer);
814 #endif /* LDAP_OPT_X_TLS_REQUIRE_CERT */
816 /* set timelimit options */
817 SET_OPTI(TIMELIMIT, timelimit);
819 #ifdef LDAP_OPT_NETWORK_TIMEOUT
820 if (ldap_conf.bind_timelimit > 0) {
822 tv.tv_sec = ldap_conf.bind_timelimit / 1000;
824 if (ldap_conf.debug > 1)
825 fprintf(stderr, "ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)\n",
827 rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
828 if (rc != LDAP_OPT_SUCCESS) {
829 fprintf(stderr,"ldap_set_option(NETWORK_TIMEOUT, %ld)=%d: %s\n",
830 tv.tv_sec, rc, ldap_err2string(rc));
836 /* attempt connect */
837 #ifdef HAVE_LDAP_INITIALIZE
840 DPRINTF(("ldap_initialize(ld,%s)", ldap_conf.uri), 2);
842 rc = ldap_initialize(&ld, ldap_conf.uri);
844 fprintf(stderr, "ldap_initialize()=%d : %s\n",
845 rc, ldap_err2string(rc));
849 #endif /* HAVE_LDAP_INITIALIZE */
850 if (ldap_conf.host) {
852 DPRINTF(("ldap_init(%s,%d)", ldap_conf.host, ldap_conf.port), 2);
854 if ((ld = ldap_init(ldap_conf.host, ldap_conf.port)) == NULL) {
855 fprintf(stderr, "ldap_init(): errno=%d : %s\n",
856 errno, strerror(errno));
860 #ifdef LDAP_OPT_PROTOCOL_VERSION
862 /* Set the LDAP Protocol version */
863 SET_OPTI(PROTOCOL_VERSION, version);
865 #endif /* LDAP_OPT_PROTOCOL_VERSION */
867 #ifdef HAVE_LDAP_START_TLS_S
869 if (ldap_conf.ssl && !strcasecmp(ldap_conf.ssl, "start_tls")) {
870 rc = ldap_start_tls_s(ld, NULL, NULL);
871 if (rc != LDAP_SUCCESS) {
872 fprintf(stderr, "ldap_start_tls_s(): %d: %s\n", rc,
873 ldap_err2string(rc));
877 DPRINTF(("ldap_start_tls_s() ok"), 1);
879 #endif /* HAVE_LDAP_START_TLS_S */
881 /* Actually connect */
882 if ((rc = ldap_simple_bind_s(ld, ldap_conf.binddn, ldap_conf.bindpw))) {
883 fprintf(stderr, "ldap_simple_bind_s()=%d : %s\n",
884 rc, ldap_err2string(rc));
887 DPRINTF(("ldap_bind() ok"), 1);
893 sudo_ldap_update_defaults(ld)
896 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
897 int rc; /* temp return value */
899 rc = ldap_search_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE,
900 "cn=defaults", NULL, 0, &result);
901 if (!rc && (entry = ldap_first_entry(ld, result))) {
902 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
903 sudo_ldap_parse_options(ld, entry);
905 DPRINTF(("no default options found!"), 1);
908 ldap_msgfree(result);
912 * like sudoers_lookup() - only LDAP style
915 sudo_ldap_check(pwflag)
919 LDAPMessage *entry = NULL, *result = NULL; /* used for searches */
920 char *filt; /* used to parse attributes */
921 int rc, ret = FALSE, do_netgr; /* temp/final return values */
922 int ldap_user_matches = FALSE, ldap_host_matches = FALSE; /* flags */
924 /* Open a connection to the LDAP server. */
925 if ((ld = sudo_ldap_open()) == NULL)
926 return(VALIDATE_ERROR);
928 /* Parse Default options. */
929 sudo_ldap_update_defaults(ld);
932 * Okay - time to search for anything that matches this user
933 * Lets limit it to only two queries of the LDAP server
935 * The first pass will look by the username, groups, and
936 * the keyword ALL. We will then inspect the results that
937 * came back from the query. We don't need to inspect the
938 * sudoUser in this pass since the LDAP server already scanned
941 * The second pass will return all the entries that contain
942 * user netgroups. Then we take the netgroups returned and
943 * try to match them against the username.
946 for (do_netgr = 0; !ret && do_netgr < 2; do_netgr++) {
947 filt = do_netgr ? estrdup("sudoUser=+*") : sudo_ldap_build_pass1();
948 DPRINTF(("ldap search '%s'", filt), 1);
949 rc = ldap_search_s(ld, ldap_conf.base, LDAP_SCOPE_SUBTREE, filt,
952 DPRINTF(("nothing found for '%s'", filt), 1);
955 /* parse each entry returned from this most recent search */
956 entry = rc ? NULL : ldap_first_entry(ld, result);
957 while (entry != NULL) {
958 DPRINTF(("found:%s", ldap_get_dn(ld, entry)), 1);
960 /* first verify user netgroup matches - only if in pass 2 */
961 (!do_netgr || sudo_ldap_check_user_netgroup(ld, entry)) &&
962 /* remember that user matched */
963 (ldap_user_matches = -1) &&
964 /* verify host match */
965 sudo_ldap_check_host(ld, entry) &&
966 /* remember that host matched */
967 (ldap_host_matches = -1) &&
968 /* add matches for listing later */
969 sudo_ldap_add_match(ld, entry, pwflag) &&
970 /* verify command match */
971 sudo_ldap_check_command(ld, entry) &&
972 /* verify runas match */
973 sudo_ldap_check_runas(ld, entry)
975 /* We have a match! */
976 DPRINTF(("Perfect Matched!"), 1);
977 /* pick up any options */
978 sudo_ldap_parse_options(ld, entry);
979 /* make sure we don't reenter loop */
981 /* break from inside for loop */
984 entry = ldap_next_entry(ld, entry);
987 ldap_msgfree(result);
991 sudo_ldap_close(ld); /* shut down connection */
993 DPRINTF(("user_matches=%d", ldap_user_matches), 1);
994 DPRINTF(("host_matches=%d", ldap_host_matches), 1);
996 /* Check for special case for -v, -k, -l options */
997 if (pwflag && ldap_user_matches && ldap_host_matches) {
999 * Handle verifypw & listpw
1001 * To be extra paranoid, since we haven't read any NOPASSWD options
1002 * in /etc/sudoers yet, but we have to make the decission now, lets
1003 * assume the worst and prefer to prompt for password unless the setting
1004 * is "never". (example verifypw=never or listpw=never)
1009 SET(ret, FLAG_NOPASS); /* -k or -K */
1011 switch (sudo_defs_table[pwflag].sd_un.tuple) {
1013 SET(ret, FLAG_NOPASS);
1016 if (def_authenticate)
1017 SET(ret, FLAG_CHECK_USER);
1024 if (ISSET(ret, VALIDATE_OK)) {
1025 /* we have a match, should we check the password? */
1026 if (!def_authenticate)
1027 SET(ret, FLAG_NOPASS);
1029 SET(ret, FLAG_NOEXEC);
1031 SET(ret, FLAG_SETENV);
1033 /* we do not have a match */
1034 ret = VALIDATE_NOT_OK;
1036 SET(ret, FLAG_NO_CHECK);
1037 else if (!ldap_user_matches)
1038 SET(ret, FLAG_NO_USER);
1039 else if (!ldap_host_matches)
1040 SET(ret, FLAG_NO_HOST);
1042 DPRINTF(("sudo_ldap_check(%d)=0x%02x", pwflag, ret), 1);
1048 * shut down LDAP connection
1051 sudo_ldap_close(LDAP *ld)