17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (c) 2001-2005 Sendmail, Inc. and its suppliers. 37c478bd9Sstevel@tonic-gate * All rights reserved. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 67c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 77c478bd9Sstevel@tonic-gate * the sendmail distribution. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #include <sm/gen.h> 13*49218d4fSjbeck SM_RCSID("@(#)$Id: ldap.c,v 1.65 2005/06/23 23:11:22 ca Exp $") 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate #if LDAPMAP 167c478bd9Sstevel@tonic-gate # include <sys/types.h> 177c478bd9Sstevel@tonic-gate # include <errno.h> 187c478bd9Sstevel@tonic-gate # include <setjmp.h> 197c478bd9Sstevel@tonic-gate # include <stdlib.h> 207c478bd9Sstevel@tonic-gate # include <unistd.h> 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate # include <sm/bitops.h> 237c478bd9Sstevel@tonic-gate # include <sm/clock.h> 247c478bd9Sstevel@tonic-gate # include <sm/conf.h> 257c478bd9Sstevel@tonic-gate # include <sm/debug.h> 267c478bd9Sstevel@tonic-gate # include <sm/errstring.h> 277c478bd9Sstevel@tonic-gate # include <sm/ldap.h> 287c478bd9Sstevel@tonic-gate # include <sm/string.h> 297c478bd9Sstevel@tonic-gate # ifdef EX_OK 307c478bd9Sstevel@tonic-gate # undef EX_OK /* for SVr4.2 SMP */ 317c478bd9Sstevel@tonic-gate # endif /* EX_OK */ 327c478bd9Sstevel@tonic-gate # include <sm/sysexits.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate SM_DEBUG_T SmLDAPTrace = SM_DEBUG_INITIALIZER("sm_trace_ldap", 357c478bd9Sstevel@tonic-gate "@(#)$Debug: sm_trace_ldap - trace LDAP operations $"); 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate static void ldaptimeout __P((int)); 387c478bd9Sstevel@tonic-gate static bool sm_ldap_has_objectclass __P((SM_LDAP_STRUCT *, LDAPMessage *, char *)); 397c478bd9Sstevel@tonic-gate static SM_LDAP_RECURSE_ENTRY *sm_ldap_add_recurse __P((SM_LDAP_RECURSE_LIST **, char *, int, SM_RPOOL_T *)); 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate ** SM_LDAP_CLEAR -- set default values for SM_LDAP_STRUCT 437c478bd9Sstevel@tonic-gate ** 447c478bd9Sstevel@tonic-gate ** Parameters: 457c478bd9Sstevel@tonic-gate ** lmap -- pointer to SM_LDAP_STRUCT to clear 467c478bd9Sstevel@tonic-gate ** 477c478bd9Sstevel@tonic-gate ** Returns: 487c478bd9Sstevel@tonic-gate ** None. 497c478bd9Sstevel@tonic-gate ** 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate void 537c478bd9Sstevel@tonic-gate sm_ldap_clear(lmap) 547c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 557c478bd9Sstevel@tonic-gate { 567c478bd9Sstevel@tonic-gate if (lmap == NULL) 577c478bd9Sstevel@tonic-gate return; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate lmap->ldap_host = NULL; 607c478bd9Sstevel@tonic-gate lmap->ldap_port = LDAP_PORT; 617c478bd9Sstevel@tonic-gate lmap->ldap_uri = NULL; 627c478bd9Sstevel@tonic-gate lmap->ldap_version = 0; 637c478bd9Sstevel@tonic-gate lmap->ldap_deref = LDAP_DEREF_NEVER; 647c478bd9Sstevel@tonic-gate lmap->ldap_timelimit = LDAP_NO_LIMIT; 657c478bd9Sstevel@tonic-gate lmap->ldap_sizelimit = LDAP_NO_LIMIT; 667c478bd9Sstevel@tonic-gate # ifdef LDAP_REFERRALS 677c478bd9Sstevel@tonic-gate lmap->ldap_options = LDAP_OPT_REFERRALS; 687c478bd9Sstevel@tonic-gate # else /* LDAP_REFERRALS */ 697c478bd9Sstevel@tonic-gate lmap->ldap_options = 0; 707c478bd9Sstevel@tonic-gate # endif /* LDAP_REFERRALS */ 717c478bd9Sstevel@tonic-gate lmap->ldap_attrsep = '\0'; 727c478bd9Sstevel@tonic-gate lmap->ldap_binddn = NULL; 737c478bd9Sstevel@tonic-gate lmap->ldap_secret = NULL; 747c478bd9Sstevel@tonic-gate lmap->ldap_method = LDAP_AUTH_SIMPLE; 757c478bd9Sstevel@tonic-gate lmap->ldap_base = NULL; 767c478bd9Sstevel@tonic-gate lmap->ldap_scope = LDAP_SCOPE_SUBTREE; 777c478bd9Sstevel@tonic-gate lmap->ldap_attrsonly = LDAPMAP_FALSE; 787c478bd9Sstevel@tonic-gate lmap->ldap_timeout.tv_sec = 0; 797c478bd9Sstevel@tonic-gate lmap->ldap_timeout.tv_usec = 0; 807c478bd9Sstevel@tonic-gate lmap->ldap_ld = NULL; 817c478bd9Sstevel@tonic-gate lmap->ldap_filter = NULL; 827c478bd9Sstevel@tonic-gate lmap->ldap_attr[0] = NULL; 837c478bd9Sstevel@tonic-gate lmap->ldap_attr_type[0] = SM_LDAP_ATTR_NONE; 847c478bd9Sstevel@tonic-gate lmap->ldap_attr_needobjclass[0] = NULL; 857c478bd9Sstevel@tonic-gate lmap->ldap_res = NULL; 867c478bd9Sstevel@tonic-gate lmap->ldap_next = NULL; 877c478bd9Sstevel@tonic-gate lmap->ldap_pid = 0; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate ** SM_LDAP_START -- actually connect to an LDAP server 927c478bd9Sstevel@tonic-gate ** 937c478bd9Sstevel@tonic-gate ** Parameters: 947c478bd9Sstevel@tonic-gate ** name -- name of map for debug output. 957c478bd9Sstevel@tonic-gate ** lmap -- the LDAP map being opened. 967c478bd9Sstevel@tonic-gate ** 977c478bd9Sstevel@tonic-gate ** Returns: 987c478bd9Sstevel@tonic-gate ** true if connection is successful, false otherwise. 997c478bd9Sstevel@tonic-gate ** 1007c478bd9Sstevel@tonic-gate ** Side Effects: 1017c478bd9Sstevel@tonic-gate ** Populates lmap->ldap_ld. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate static jmp_buf LDAPTimeout; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate #define SM_LDAP_SETTIMEOUT(to) \ 1077c478bd9Sstevel@tonic-gate do \ 1087c478bd9Sstevel@tonic-gate { \ 1097c478bd9Sstevel@tonic-gate if (to != 0) \ 1107c478bd9Sstevel@tonic-gate { \ 1117c478bd9Sstevel@tonic-gate if (setjmp(LDAPTimeout) != 0) \ 1127c478bd9Sstevel@tonic-gate { \ 1137c478bd9Sstevel@tonic-gate errno = ETIMEDOUT; \ 1147c478bd9Sstevel@tonic-gate return false; \ 1157c478bd9Sstevel@tonic-gate } \ 1167c478bd9Sstevel@tonic-gate ev = sm_setevent(to, ldaptimeout, 0); \ 1177c478bd9Sstevel@tonic-gate } \ 1187c478bd9Sstevel@tonic-gate } while (0) 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate #define SM_LDAP_CLEARTIMEOUT() \ 1217c478bd9Sstevel@tonic-gate do \ 1227c478bd9Sstevel@tonic-gate { \ 1237c478bd9Sstevel@tonic-gate if (ev != NULL) \ 1247c478bd9Sstevel@tonic-gate sm_clrevent(ev); \ 1257c478bd9Sstevel@tonic-gate } while (0) 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate bool 1287c478bd9Sstevel@tonic-gate sm_ldap_start(name, lmap) 1297c478bd9Sstevel@tonic-gate char *name; 1307c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate int bind_result; 1337c478bd9Sstevel@tonic-gate int save_errno = 0; 1347c478bd9Sstevel@tonic-gate char *id; 1357c478bd9Sstevel@tonic-gate SM_EVENT *ev = NULL; 1367c478bd9Sstevel@tonic-gate LDAP *ld = NULL; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate if (sm_debug_active(&SmLDAPTrace, 2)) 1397c478bd9Sstevel@tonic-gate sm_dprintf("ldapmap_start(%s)\n", name == NULL ? "" : name); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (lmap->ldap_host != NULL) 1427c478bd9Sstevel@tonic-gate id = lmap->ldap_host; 1437c478bd9Sstevel@tonic-gate else if (lmap->ldap_uri != NULL) 1447c478bd9Sstevel@tonic-gate id = lmap->ldap_uri; 1457c478bd9Sstevel@tonic-gate else 1467c478bd9Sstevel@tonic-gate id = "localhost"; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if (sm_debug_active(&SmLDAPTrace, 9)) 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate /* Don't print a port number for LDAP URIs */ 1517c478bd9Sstevel@tonic-gate if (lmap->ldap_uri != NULL) 1527c478bd9Sstevel@tonic-gate sm_dprintf("ldapmap_start(%s)\n", id); 1537c478bd9Sstevel@tonic-gate else 1547c478bd9Sstevel@tonic-gate sm_dprintf("ldapmap_start(%s, %d)\n", id, 1557c478bd9Sstevel@tonic-gate lmap->ldap_port); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (lmap->ldap_uri != NULL) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_INITIALIZE 1617c478bd9Sstevel@tonic-gate /* LDAP server supports URIs so use them directly */ 1627c478bd9Sstevel@tonic-gate save_errno = ldap_initialize(&ld, lmap->ldap_uri); 1637c478bd9Sstevel@tonic-gate #else /* SM_CONF_LDAP_INITIALIZE */ 1647c478bd9Sstevel@tonic-gate int err; 1657c478bd9Sstevel@tonic-gate LDAPURLDesc *ludp = NULL; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* Blast apart URL and use the ldap_init/ldap_open below */ 1687c478bd9Sstevel@tonic-gate err = ldap_url_parse(lmap->ldap_uri, &ludp); 1697c478bd9Sstevel@tonic-gate if (err != 0) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate errno = err + E_LDAPURLBASE; 1727c478bd9Sstevel@tonic-gate return false; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate lmap->ldap_host = sm_strdup_x(ludp->lud_host); 1757c478bd9Sstevel@tonic-gate if (lmap->ldap_host == NULL) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate save_errno = errno; 1787c478bd9Sstevel@tonic-gate ldap_free_urldesc(ludp); 1797c478bd9Sstevel@tonic-gate errno = save_errno; 1807c478bd9Sstevel@tonic-gate return false; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate lmap->ldap_port = ludp->lud_port; 1837c478bd9Sstevel@tonic-gate ldap_free_urldesc(ludp); 1847c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_INITIALIZE */ 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate if (ld == NULL) 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT 1907c478bd9Sstevel@tonic-gate ld = ldap_init(lmap->ldap_host, lmap->ldap_port); 1917c478bd9Sstevel@tonic-gate save_errno = errno; 1927c478bd9Sstevel@tonic-gate # else /* USE_LDAP_INIT */ 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate ** If using ldap_open(), the actual connection to the server 1957c478bd9Sstevel@tonic-gate ** happens now so we need the timeout here. For ldap_init(), 1967c478bd9Sstevel@tonic-gate ** the connection happens at bind time. 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec); 2007c478bd9Sstevel@tonic-gate ld = ldap_open(lmap->ldap_host, lmap->ldap_port); 2017c478bd9Sstevel@tonic-gate save_errno = errno; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* clear the event if it has not sprung */ 2047c478bd9Sstevel@tonic-gate SM_LDAP_CLEARTIMEOUT(); 2057c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */ 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate errno = save_errno; 2097c478bd9Sstevel@tonic-gate if (ld == NULL) 2107c478bd9Sstevel@tonic-gate return false; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate sm_ldap_setopts(ld, lmap); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate ** If using ldap_init(), the actual connection to the server 2177c478bd9Sstevel@tonic-gate ** happens at ldap_bind_s() so we need the timeout here. 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec); 2217c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */ 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate # ifdef LDAP_AUTH_KRBV4 2247c478bd9Sstevel@tonic-gate if (lmap->ldap_method == LDAP_AUTH_KRBV4 && 2257c478bd9Sstevel@tonic-gate lmap->ldap_secret != NULL) 2267c478bd9Sstevel@tonic-gate { 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate ** Need to put ticket in environment here instead of 2297c478bd9Sstevel@tonic-gate ** during parseargs as there may be different tickets 2307c478bd9Sstevel@tonic-gate ** for different LDAP connections. 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate (void) putenv(lmap->ldap_secret); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate # endif /* LDAP_AUTH_KRBV4 */ 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate bind_result = ldap_bind_s(ld, lmap->ldap_binddn, 2387c478bd9Sstevel@tonic-gate lmap->ldap_secret, lmap->ldap_method); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT 2417c478bd9Sstevel@tonic-gate /* clear the event if it has not sprung */ 2427c478bd9Sstevel@tonic-gate SM_LDAP_CLEARTIMEOUT(); 2437c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */ 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate if (bind_result != LDAP_SUCCESS) 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate errno = bind_result + E_LDAPBASE; 2487c478bd9Sstevel@tonic-gate return false; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* Save PID to make sure only this PID closes the LDAP connection */ 2527c478bd9Sstevel@tonic-gate lmap->ldap_pid = getpid(); 2537c478bd9Sstevel@tonic-gate lmap->ldap_ld = ld; 2547c478bd9Sstevel@tonic-gate return true; 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2587c478bd9Sstevel@tonic-gate static void 2597c478bd9Sstevel@tonic-gate ldaptimeout(unused) 2607c478bd9Sstevel@tonic-gate int unused; 2617c478bd9Sstevel@tonic-gate { 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 2647c478bd9Sstevel@tonic-gate ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 2657c478bd9Sstevel@tonic-gate ** DOING. 2667c478bd9Sstevel@tonic-gate */ 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate errno = ETIMEDOUT; 2697c478bd9Sstevel@tonic-gate longjmp(LDAPTimeout, 1); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate ** SM_LDAP_SEARCH -- initiate LDAP search 2747c478bd9Sstevel@tonic-gate ** 2757c478bd9Sstevel@tonic-gate ** Initiate an LDAP search, return the msgid. 2767c478bd9Sstevel@tonic-gate ** The calling function must collect the results. 2777c478bd9Sstevel@tonic-gate ** 2787c478bd9Sstevel@tonic-gate ** Parameters: 2797c478bd9Sstevel@tonic-gate ** lmap -- LDAP map information 2807c478bd9Sstevel@tonic-gate ** key -- key to substitute in LDAP filter 2817c478bd9Sstevel@tonic-gate ** 2827c478bd9Sstevel@tonic-gate ** Returns: 2837c478bd9Sstevel@tonic-gate ** -1 on failure, msgid on success 2847c478bd9Sstevel@tonic-gate ** 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate int 2887c478bd9Sstevel@tonic-gate sm_ldap_search(lmap, key) 2897c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 2907c478bd9Sstevel@tonic-gate char *key; 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate int msgid; 2937c478bd9Sstevel@tonic-gate char *fp, *p, *q; 2947c478bd9Sstevel@tonic-gate char filter[LDAPMAP_MAX_FILTER + 1]; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* substitute key into filter, perhaps multiple times */ 2977c478bd9Sstevel@tonic-gate memset(filter, '\0', sizeof filter); 2987c478bd9Sstevel@tonic-gate fp = filter; 2997c478bd9Sstevel@tonic-gate p = lmap->ldap_filter; 3007c478bd9Sstevel@tonic-gate while ((q = strchr(p, '%')) != NULL) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate if (q[1] == 's') 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate (void) sm_snprintf(fp, SPACELEFT(filter, fp), 3057c478bd9Sstevel@tonic-gate "%.*s%s", (int) (q - p), p, key); 3067c478bd9Sstevel@tonic-gate fp += strlen(fp); 3077c478bd9Sstevel@tonic-gate p = q + 2; 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate else if (q[1] == '0') 3107c478bd9Sstevel@tonic-gate { 3117c478bd9Sstevel@tonic-gate char *k = key; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate (void) sm_snprintf(fp, SPACELEFT(filter, fp), 3147c478bd9Sstevel@tonic-gate "%.*s", (int) (q - p), p); 3157c478bd9Sstevel@tonic-gate fp += strlen(fp); 3167c478bd9Sstevel@tonic-gate p = q + 2; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* Properly escape LDAP special characters */ 3197c478bd9Sstevel@tonic-gate while (SPACELEFT(filter, fp) > 0 && 3207c478bd9Sstevel@tonic-gate *k != '\0') 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate if (*k == '*' || *k == '(' || 3237c478bd9Sstevel@tonic-gate *k == ')' || *k == '\\') 3247c478bd9Sstevel@tonic-gate { 3257c478bd9Sstevel@tonic-gate (void) sm_strlcat(fp, 3267c478bd9Sstevel@tonic-gate (*k == '*' ? "\\2A" : 3277c478bd9Sstevel@tonic-gate (*k == '(' ? "\\28" : 3287c478bd9Sstevel@tonic-gate (*k == ')' ? "\\29" : 3297c478bd9Sstevel@tonic-gate (*k == '\\' ? "\\5C" : 3307c478bd9Sstevel@tonic-gate "\00")))), 3317c478bd9Sstevel@tonic-gate SPACELEFT(filter, fp)); 3327c478bd9Sstevel@tonic-gate fp += strlen(fp); 3337c478bd9Sstevel@tonic-gate k++; 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate else 3367c478bd9Sstevel@tonic-gate *fp++ = *k++; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate else 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate (void) sm_snprintf(fp, SPACELEFT(filter, fp), 3427c478bd9Sstevel@tonic-gate "%.*s", (int) (q - p + 1), p); 3437c478bd9Sstevel@tonic-gate p = q + (q[1] == '%' ? 2 : 1); 3447c478bd9Sstevel@tonic-gate fp += strlen(fp); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate (void) sm_strlcpy(fp, p, SPACELEFT(filter, fp)); 3487c478bd9Sstevel@tonic-gate if (sm_debug_active(&SmLDAPTrace, 20)) 3497c478bd9Sstevel@tonic-gate sm_dprintf("ldap search filter=%s\n", filter); 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate lmap->ldap_res = NULL; 3527c478bd9Sstevel@tonic-gate msgid = ldap_search(lmap->ldap_ld, lmap->ldap_base, 3537c478bd9Sstevel@tonic-gate lmap->ldap_scope, filter, 3547c478bd9Sstevel@tonic-gate (lmap->ldap_attr[0] == NULL ? NULL : 3557c478bd9Sstevel@tonic-gate lmap->ldap_attr), 3567c478bd9Sstevel@tonic-gate lmap->ldap_attrsonly); 3577c478bd9Sstevel@tonic-gate return msgid; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* 3617c478bd9Sstevel@tonic-gate ** SM_LDAP_HAS_OBJECTCLASS -- determine if an LDAP entry is part of a 3627c478bd9Sstevel@tonic-gate ** particular objectClass 3637c478bd9Sstevel@tonic-gate ** 3647c478bd9Sstevel@tonic-gate ** Parameters: 3657c478bd9Sstevel@tonic-gate ** lmap -- pointer to SM_LDAP_STRUCT in use 3667c478bd9Sstevel@tonic-gate ** entry -- current LDAP entry struct 3677c478bd9Sstevel@tonic-gate ** ocvalue -- particular objectclass in question. 3687c478bd9Sstevel@tonic-gate ** may be of form (fee|foo|fum) meaning 3697c478bd9Sstevel@tonic-gate ** any entry can be part of either fee, 3707c478bd9Sstevel@tonic-gate ** foo or fum objectclass 3717c478bd9Sstevel@tonic-gate ** 3727c478bd9Sstevel@tonic-gate ** Returns: 3737c478bd9Sstevel@tonic-gate ** true if item has that objectClass 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate static bool 3777c478bd9Sstevel@tonic-gate sm_ldap_has_objectclass(lmap, entry, ocvalue) 3787c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 3797c478bd9Sstevel@tonic-gate LDAPMessage *entry; 3807c478bd9Sstevel@tonic-gate char *ocvalue; 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate char **vals = NULL; 3837c478bd9Sstevel@tonic-gate int i; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate if (ocvalue == NULL) 3867c478bd9Sstevel@tonic-gate return false; 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate vals = ldap_get_values(lmap->ldap_ld, entry, "objectClass"); 3897c478bd9Sstevel@tonic-gate if (vals == NULL) 3907c478bd9Sstevel@tonic-gate return false; 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate for (i = 0; vals[i] != NULL; i++) 3937c478bd9Sstevel@tonic-gate { 3947c478bd9Sstevel@tonic-gate char *p; 3957c478bd9Sstevel@tonic-gate char *q; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate p = q = ocvalue; 3987c478bd9Sstevel@tonic-gate while (*p != '\0') 3997c478bd9Sstevel@tonic-gate { 4007c478bd9Sstevel@tonic-gate while (*p != '\0' && *p != '|') 4017c478bd9Sstevel@tonic-gate p++; 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if ((p - q) == strlen(vals[i]) && 4047c478bd9Sstevel@tonic-gate sm_strncasecmp(vals[i], q, p - q) == 0) 4057c478bd9Sstevel@tonic-gate { 4067c478bd9Sstevel@tonic-gate ldap_value_free(vals); 4077c478bd9Sstevel@tonic-gate return true; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate while (*p == '|') 4117c478bd9Sstevel@tonic-gate p++; 4127c478bd9Sstevel@tonic-gate q = p; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate ldap_value_free(vals); 4177c478bd9Sstevel@tonic-gate return false; 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate ** SM_LDAP_RESULTS -- return results from an LDAP lookup in result 4227c478bd9Sstevel@tonic-gate ** 4237c478bd9Sstevel@tonic-gate ** Parameters: 4247c478bd9Sstevel@tonic-gate ** lmap -- pointer to SM_LDAP_STRUCT in use 4257c478bd9Sstevel@tonic-gate ** msgid -- msgid returned by sm_ldap_search() 4267c478bd9Sstevel@tonic-gate ** flags -- flags for the lookup 4277c478bd9Sstevel@tonic-gate ** delim -- delimiter for result concatenation 4287c478bd9Sstevel@tonic-gate ** rpool -- memory pool for storage 4297c478bd9Sstevel@tonic-gate ** result -- return string 4307c478bd9Sstevel@tonic-gate ** recurse -- recursion list 4317c478bd9Sstevel@tonic-gate ** 4327c478bd9Sstevel@tonic-gate ** Returns: 4337c478bd9Sstevel@tonic-gate ** status (sysexit) 4347c478bd9Sstevel@tonic-gate */ 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate # define SM_LDAP_ERROR_CLEANUP() \ 4377c478bd9Sstevel@tonic-gate { \ 4387c478bd9Sstevel@tonic-gate if (lmap->ldap_res != NULL) \ 4397c478bd9Sstevel@tonic-gate { \ 4407c478bd9Sstevel@tonic-gate ldap_msgfree(lmap->ldap_res); \ 4417c478bd9Sstevel@tonic-gate lmap->ldap_res = NULL; \ 4427c478bd9Sstevel@tonic-gate } \ 4437c478bd9Sstevel@tonic-gate (void) ldap_abandon(lmap->ldap_ld, msgid); \ 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate static SM_LDAP_RECURSE_ENTRY * 4477c478bd9Sstevel@tonic-gate sm_ldap_add_recurse(top, item, type, rpool) 4487c478bd9Sstevel@tonic-gate SM_LDAP_RECURSE_LIST **top; 4497c478bd9Sstevel@tonic-gate char *item; 4507c478bd9Sstevel@tonic-gate int type; 4517c478bd9Sstevel@tonic-gate SM_RPOOL_T *rpool; 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate int n; 4547c478bd9Sstevel@tonic-gate int m; 4557c478bd9Sstevel@tonic-gate int p; 4567c478bd9Sstevel@tonic-gate int insertat; 4577c478bd9Sstevel@tonic-gate int moveb; 4587c478bd9Sstevel@tonic-gate int oldsizeb; 4597c478bd9Sstevel@tonic-gate int rc; 4607c478bd9Sstevel@tonic-gate SM_LDAP_RECURSE_ENTRY *newe; 4617c478bd9Sstevel@tonic-gate SM_LDAP_RECURSE_ENTRY **olddata; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate ** This code will maintain a list of 4657c478bd9Sstevel@tonic-gate ** SM_LDAP_RECURSE_ENTRY structures 4667c478bd9Sstevel@tonic-gate ** in ascending order. 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate if (*top == NULL) 4707c478bd9Sstevel@tonic-gate { 4717c478bd9Sstevel@tonic-gate /* Allocate an initial SM_LDAP_RECURSE_LIST struct */ 4727c478bd9Sstevel@tonic-gate *top = sm_rpool_malloc_x(rpool, sizeof **top); 4737c478bd9Sstevel@tonic-gate (*top)->lr_cnt = 0; 4747c478bd9Sstevel@tonic-gate (*top)->lr_size = 0; 4757c478bd9Sstevel@tonic-gate (*top)->lr_data = NULL; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if ((*top)->lr_cnt >= (*top)->lr_size) 4797c478bd9Sstevel@tonic-gate { 4807c478bd9Sstevel@tonic-gate /* Grow the list of SM_LDAP_RECURSE_ENTRY ptrs */ 4817c478bd9Sstevel@tonic-gate olddata = (*top)->lr_data; 4827c478bd9Sstevel@tonic-gate if ((*top)->lr_size == 0) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate oldsizeb = 0; 4857c478bd9Sstevel@tonic-gate (*top)->lr_size = 256; 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate else 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate oldsizeb = (*top)->lr_size * sizeof *((*top)->lr_data); 4907c478bd9Sstevel@tonic-gate (*top)->lr_size *= 2; 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate (*top)->lr_data = sm_rpool_malloc_x(rpool, 4937c478bd9Sstevel@tonic-gate (*top)->lr_size * sizeof *((*top)->lr_data)); 4947c478bd9Sstevel@tonic-gate if (oldsizeb > 0) 4957c478bd9Sstevel@tonic-gate memcpy((*top)->lr_data, olddata, oldsizeb); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate ** Binary search/insert item:type into list. 5007c478bd9Sstevel@tonic-gate ** Return current entry pointer if already exists. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate n = 0; 5047c478bd9Sstevel@tonic-gate m = (*top)->lr_cnt - 1; 5057c478bd9Sstevel@tonic-gate if (m < 0) 5067c478bd9Sstevel@tonic-gate insertat = 0; 5077c478bd9Sstevel@tonic-gate else 5087c478bd9Sstevel@tonic-gate insertat = -1; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate while (insertat == -1) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate p = (m + n) / 2; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate rc = sm_strcasecmp(item, (*top)->lr_data[p]->lr_search); 5157c478bd9Sstevel@tonic-gate if (rc == 0) 5167c478bd9Sstevel@tonic-gate rc = type - (*top)->lr_data[p]->lr_type; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate if (rc < 0) 5197c478bd9Sstevel@tonic-gate m = p - 1; 5207c478bd9Sstevel@tonic-gate else if (rc > 0) 5217c478bd9Sstevel@tonic-gate n = p + 1; 5227c478bd9Sstevel@tonic-gate else 5237c478bd9Sstevel@tonic-gate return (*top)->lr_data[p]; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if (m == -1) 5267c478bd9Sstevel@tonic-gate insertat = 0; 5277c478bd9Sstevel@tonic-gate else if (n >= (*top)->lr_cnt) 5287c478bd9Sstevel@tonic-gate insertat = (*top)->lr_cnt; 5297c478bd9Sstevel@tonic-gate else if (m < n) 5307c478bd9Sstevel@tonic-gate insertat = m + 1; 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* 5347c478bd9Sstevel@tonic-gate ** Not found in list, make room 5357c478bd9Sstevel@tonic-gate ** at insert point and add it. 5367c478bd9Sstevel@tonic-gate */ 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate newe = sm_rpool_malloc_x(rpool, sizeof *newe); 5397c478bd9Sstevel@tonic-gate if (newe != NULL) 5407c478bd9Sstevel@tonic-gate { 5417c478bd9Sstevel@tonic-gate moveb = ((*top)->lr_cnt - insertat) * sizeof *((*top)->lr_data); 5427c478bd9Sstevel@tonic-gate if (moveb > 0) 5437c478bd9Sstevel@tonic-gate memmove(&((*top)->lr_data[insertat + 1]), 5447c478bd9Sstevel@tonic-gate &((*top)->lr_data[insertat]), 5457c478bd9Sstevel@tonic-gate moveb); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate newe->lr_search = sm_rpool_strdup_x(rpool, item); 5487c478bd9Sstevel@tonic-gate newe->lr_type = type; 5497c478bd9Sstevel@tonic-gate newe->lr_ludp = NULL; 5507c478bd9Sstevel@tonic-gate newe->lr_attrs = NULL; 5517c478bd9Sstevel@tonic-gate newe->lr_done = false; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate ((*top)->lr_data)[insertat] = newe; 5547c478bd9Sstevel@tonic-gate (*top)->lr_cnt++; 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate return newe; 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate int 5607c478bd9Sstevel@tonic-gate sm_ldap_results(lmap, msgid, flags, delim, rpool, result, 5617c478bd9Sstevel@tonic-gate resultln, resultsz, recurse) 5627c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 5637c478bd9Sstevel@tonic-gate int msgid; 5647c478bd9Sstevel@tonic-gate int flags; 5657c478bd9Sstevel@tonic-gate int delim; 5667c478bd9Sstevel@tonic-gate SM_RPOOL_T *rpool; 5677c478bd9Sstevel@tonic-gate char **result; 5687c478bd9Sstevel@tonic-gate int *resultln; 5697c478bd9Sstevel@tonic-gate int *resultsz; 5707c478bd9Sstevel@tonic-gate SM_LDAP_RECURSE_LIST *recurse; 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate bool toplevel; 5737c478bd9Sstevel@tonic-gate int i; 5747c478bd9Sstevel@tonic-gate int statp; 5757c478bd9Sstevel@tonic-gate int vsize; 5767c478bd9Sstevel@tonic-gate int ret; 5777c478bd9Sstevel@tonic-gate int save_errno; 5787c478bd9Sstevel@tonic-gate char *p; 5797c478bd9Sstevel@tonic-gate SM_LDAP_RECURSE_ENTRY *rl; 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* Are we the top top level of the search? */ 5827c478bd9Sstevel@tonic-gate toplevel = (recurse == NULL); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate /* Get results */ 5857c478bd9Sstevel@tonic-gate statp = EX_NOTFOUND; 5867c478bd9Sstevel@tonic-gate while ((ret = ldap_result(lmap->ldap_ld, msgid, 0, 5877c478bd9Sstevel@tonic-gate (lmap->ldap_timeout.tv_sec == 0 ? NULL : 5887c478bd9Sstevel@tonic-gate &(lmap->ldap_timeout)), 5897c478bd9Sstevel@tonic-gate &(lmap->ldap_res))) == LDAP_RES_SEARCH_ENTRY) 5907c478bd9Sstevel@tonic-gate { 5917c478bd9Sstevel@tonic-gate LDAPMessage *entry; 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate /* If we don't want multiple values and we have one, break */ 5947c478bd9Sstevel@tonic-gate if ((char) delim == '\0' && 5957c478bd9Sstevel@tonic-gate !bitset(SM_LDAP_SINGLEMATCH, flags) && 5967c478bd9Sstevel@tonic-gate *result != NULL) 5977c478bd9Sstevel@tonic-gate break; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate /* Cycle through all entries */ 6007c478bd9Sstevel@tonic-gate for (entry = ldap_first_entry(lmap->ldap_ld, lmap->ldap_res); 6017c478bd9Sstevel@tonic-gate entry != NULL; 6027c478bd9Sstevel@tonic-gate entry = ldap_next_entry(lmap->ldap_ld, lmap->ldap_res)) 6037c478bd9Sstevel@tonic-gate { 6047c478bd9Sstevel@tonic-gate BerElement *ber; 6057c478bd9Sstevel@tonic-gate char *attr; 6067c478bd9Sstevel@tonic-gate char **vals = NULL; 6077c478bd9Sstevel@tonic-gate char *dn; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate /* 6107c478bd9Sstevel@tonic-gate ** If matching only and found an entry, 6117c478bd9Sstevel@tonic-gate ** no need to spin through attributes 6127c478bd9Sstevel@tonic-gate */ 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate if (bitset(SM_LDAP_MATCHONLY, flags)) 6157c478bd9Sstevel@tonic-gate { 6167c478bd9Sstevel@tonic-gate statp = EX_OK; 6177c478bd9Sstevel@tonic-gate continue; 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 620*49218d4fSjbeck #if _FFR_LDAP_SINGLEDN 621*49218d4fSjbeck if (bitset(SM_LDAP_SINGLEDN, flags) && *result != NULL) 622*49218d4fSjbeck { 623*49218d4fSjbeck /* only wanted one match */ 624*49218d4fSjbeck SM_LDAP_ERROR_CLEANUP(); 625*49218d4fSjbeck errno = ENOENT; 626*49218d4fSjbeck return EX_NOTFOUND; 627*49218d4fSjbeck } 628*49218d4fSjbeck #endif /* _FFR_LDAP_SINGLEDN */ 629*49218d4fSjbeck 6307c478bd9Sstevel@tonic-gate /* record completed DN's to prevent loops */ 6317c478bd9Sstevel@tonic-gate dn = ldap_get_dn(lmap->ldap_ld, entry); 6327c478bd9Sstevel@tonic-gate if (dn == NULL) 6337c478bd9Sstevel@tonic-gate { 6347c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 6357c478bd9Sstevel@tonic-gate save_errno += E_LDAPBASE; 6367c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 6377c478bd9Sstevel@tonic-gate errno = save_errno; 6387c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate rl = sm_ldap_add_recurse(&recurse, dn, 6427c478bd9Sstevel@tonic-gate SM_LDAP_ATTR_DN, 6437c478bd9Sstevel@tonic-gate rpool); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate if (rl == NULL) 6467c478bd9Sstevel@tonic-gate { 6477c478bd9Sstevel@tonic-gate ldap_memfree(dn); 6487c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 6497c478bd9Sstevel@tonic-gate errno = ENOMEM; 6507c478bd9Sstevel@tonic-gate return EX_OSERR; 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate else if (rl->lr_done) 6537c478bd9Sstevel@tonic-gate { 6547c478bd9Sstevel@tonic-gate /* already on list, skip it */ 6557c478bd9Sstevel@tonic-gate ldap_memfree(dn); 6567c478bd9Sstevel@tonic-gate continue; 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate ldap_memfree(dn); 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT) 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate ** Reset value to prevent lingering 6637c478bd9Sstevel@tonic-gate ** LDAP_DECODING_ERROR due to 6647c478bd9Sstevel@tonic-gate ** OpenLDAP 1.X's hack (see below) 6657c478bd9Sstevel@tonic-gate */ 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate lmap->ldap_ld->ld_errno = LDAP_SUCCESS; 6687c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */ 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate for (attr = ldap_first_attribute(lmap->ldap_ld, entry, 6717c478bd9Sstevel@tonic-gate &ber); 6727c478bd9Sstevel@tonic-gate attr != NULL; 6737c478bd9Sstevel@tonic-gate attr = ldap_next_attribute(lmap->ldap_ld, entry, 6747c478bd9Sstevel@tonic-gate ber)) 6757c478bd9Sstevel@tonic-gate { 6767c478bd9Sstevel@tonic-gate char *tmp, *vp_tmp; 6777c478bd9Sstevel@tonic-gate int type; 6787c478bd9Sstevel@tonic-gate char *needobjclass = NULL; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate type = SM_LDAP_ATTR_NONE; 6817c478bd9Sstevel@tonic-gate for (i = 0; lmap->ldap_attr[i] != NULL; i++) 6827c478bd9Sstevel@tonic-gate { 6837c478bd9Sstevel@tonic-gate if (sm_strcasecmp(lmap->ldap_attr[i], 6847c478bd9Sstevel@tonic-gate attr) == 0) 6857c478bd9Sstevel@tonic-gate { 6867c478bd9Sstevel@tonic-gate type = lmap->ldap_attr_type[i]; 6877c478bd9Sstevel@tonic-gate needobjclass = lmap->ldap_attr_needobjclass[i]; 6887c478bd9Sstevel@tonic-gate break; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate if (bitset(SM_LDAP_USE_ALLATTR, flags) && 6937c478bd9Sstevel@tonic-gate type == SM_LDAP_ATTR_NONE) 6947c478bd9Sstevel@tonic-gate { 6957c478bd9Sstevel@tonic-gate /* URL lookups specify attrs to use */ 6967c478bd9Sstevel@tonic-gate type = SM_LDAP_ATTR_NORMAL; 6977c478bd9Sstevel@tonic-gate needobjclass = NULL; 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate if (type == SM_LDAP_ATTR_NONE) 7017c478bd9Sstevel@tonic-gate { 7027c478bd9Sstevel@tonic-gate /* attribute not requested */ 7037c478bd9Sstevel@tonic-gate ldap_memfree(attr); 7047c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 7057c478bd9Sstevel@tonic-gate errno = EFAULT; 7067c478bd9Sstevel@tonic-gate return EX_SOFTWARE; 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate ** For recursion on a particular attribute, 7117c478bd9Sstevel@tonic-gate ** we may need to see if this entry is 7127c478bd9Sstevel@tonic-gate ** part of a particular objectclass. 7137c478bd9Sstevel@tonic-gate ** Also, ignore objectClass attribute. 7147c478bd9Sstevel@tonic-gate ** Otherwise we just ignore this attribute. 7157c478bd9Sstevel@tonic-gate */ 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate if (type == SM_LDAP_ATTR_OBJCLASS || 7187c478bd9Sstevel@tonic-gate (needobjclass != NULL && 7197c478bd9Sstevel@tonic-gate !sm_ldap_has_objectclass(lmap, entry, 7207c478bd9Sstevel@tonic-gate needobjclass))) 7217c478bd9Sstevel@tonic-gate { 7227c478bd9Sstevel@tonic-gate ldap_memfree(attr); 7237c478bd9Sstevel@tonic-gate continue; 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsonly == LDAPMAP_FALSE) 7277c478bd9Sstevel@tonic-gate { 7287c478bd9Sstevel@tonic-gate vals = ldap_get_values(lmap->ldap_ld, 7297c478bd9Sstevel@tonic-gate entry, 7307c478bd9Sstevel@tonic-gate attr); 7317c478bd9Sstevel@tonic-gate if (vals == NULL) 7327c478bd9Sstevel@tonic-gate { 7337c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 7347c478bd9Sstevel@tonic-gate if (save_errno == LDAP_SUCCESS) 7357c478bd9Sstevel@tonic-gate { 7367c478bd9Sstevel@tonic-gate ldap_memfree(attr); 7377c478bd9Sstevel@tonic-gate continue; 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate /* Must be an error */ 7417c478bd9Sstevel@tonic-gate save_errno += E_LDAPBASE; 7427c478bd9Sstevel@tonic-gate ldap_memfree(attr); 7437c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 7447c478bd9Sstevel@tonic-gate errno = save_errno; 7457c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate statp = EX_OK; 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT) 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate ** Reset value to prevent lingering 7547c478bd9Sstevel@tonic-gate ** LDAP_DECODING_ERROR due to 7557c478bd9Sstevel@tonic-gate ** OpenLDAP 1.X's hack (see below) 7567c478bd9Sstevel@tonic-gate */ 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate lmap->ldap_ld->ld_errno = LDAP_SUCCESS; 7597c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */ 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate /* 7627c478bd9Sstevel@tonic-gate ** If matching only, 7637c478bd9Sstevel@tonic-gate ** no need to spin through entries 7647c478bd9Sstevel@tonic-gate */ 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate if (bitset(SM_LDAP_MATCHONLY, flags)) 7677c478bd9Sstevel@tonic-gate { 7687c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsonly == LDAPMAP_FALSE) 7697c478bd9Sstevel@tonic-gate ldap_value_free(vals); 7707c478bd9Sstevel@tonic-gate ldap_memfree(attr); 7717c478bd9Sstevel@tonic-gate continue; 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate ** If we don't want multiple values, 7767c478bd9Sstevel@tonic-gate ** return first found. 7777c478bd9Sstevel@tonic-gate */ 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate if ((char) delim == '\0') 7807c478bd9Sstevel@tonic-gate { 7817c478bd9Sstevel@tonic-gate if (*result != NULL) 7827c478bd9Sstevel@tonic-gate { 7837c478bd9Sstevel@tonic-gate /* already have a value */ 7847c478bd9Sstevel@tonic-gate if (bitset(SM_LDAP_SINGLEMATCH, 7857c478bd9Sstevel@tonic-gate flags)) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate /* only wanted one match */ 7887c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 7897c478bd9Sstevel@tonic-gate errno = ENOENT; 7907c478bd9Sstevel@tonic-gate return EX_NOTFOUND; 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate break; 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsonly == LDAPMAP_TRUE) 7967c478bd9Sstevel@tonic-gate { 7977c478bd9Sstevel@tonic-gate *result = sm_rpool_strdup_x(rpool, 7987c478bd9Sstevel@tonic-gate attr); 7997c478bd9Sstevel@tonic-gate ldap_memfree(attr); 8007c478bd9Sstevel@tonic-gate break; 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate if (vals[0] == NULL) 8047c478bd9Sstevel@tonic-gate { 8057c478bd9Sstevel@tonic-gate ldap_value_free(vals); 8067c478bd9Sstevel@tonic-gate ldap_memfree(attr); 8077c478bd9Sstevel@tonic-gate continue; 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate vsize = strlen(vals[0]) + 1; 8117c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsep != '\0') 8127c478bd9Sstevel@tonic-gate vsize += strlen(attr) + 1; 8137c478bd9Sstevel@tonic-gate *result = sm_rpool_malloc_x(rpool, 8147c478bd9Sstevel@tonic-gate vsize); 8157c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsep != '\0') 8167c478bd9Sstevel@tonic-gate sm_snprintf(*result, vsize, 8177c478bd9Sstevel@tonic-gate "%s%c%s", 8187c478bd9Sstevel@tonic-gate attr, 8197c478bd9Sstevel@tonic-gate lmap->ldap_attrsep, 8207c478bd9Sstevel@tonic-gate vals[0]); 8217c478bd9Sstevel@tonic-gate else 8227c478bd9Sstevel@tonic-gate sm_strlcpy(*result, vals[0], 8237c478bd9Sstevel@tonic-gate vsize); 8247c478bd9Sstevel@tonic-gate ldap_value_free(vals); 8257c478bd9Sstevel@tonic-gate ldap_memfree(attr); 8267c478bd9Sstevel@tonic-gate break; 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate /* attributes only */ 8307c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsonly == LDAPMAP_TRUE) 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate if (*result == NULL) 8337c478bd9Sstevel@tonic-gate *result = sm_rpool_strdup_x(rpool, 8347c478bd9Sstevel@tonic-gate attr); 8357c478bd9Sstevel@tonic-gate else 8367c478bd9Sstevel@tonic-gate { 8377c478bd9Sstevel@tonic-gate if (bitset(SM_LDAP_SINGLEMATCH, 8387c478bd9Sstevel@tonic-gate flags) && 8397c478bd9Sstevel@tonic-gate *result != NULL) 8407c478bd9Sstevel@tonic-gate { 8417c478bd9Sstevel@tonic-gate /* only wanted one match */ 8427c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 8437c478bd9Sstevel@tonic-gate errno = ENOENT; 8447c478bd9Sstevel@tonic-gate return EX_NOTFOUND; 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate vsize = strlen(*result) + 8487c478bd9Sstevel@tonic-gate strlen(attr) + 2; 8497c478bd9Sstevel@tonic-gate tmp = sm_rpool_malloc_x(rpool, 8507c478bd9Sstevel@tonic-gate vsize); 8517c478bd9Sstevel@tonic-gate (void) sm_snprintf(tmp, 8527c478bd9Sstevel@tonic-gate vsize, "%s%c%s", 8537c478bd9Sstevel@tonic-gate *result, (char) delim, 8547c478bd9Sstevel@tonic-gate attr); 8557c478bd9Sstevel@tonic-gate *result = tmp; 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate ldap_memfree(attr); 8587c478bd9Sstevel@tonic-gate continue; 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate /* 8627c478bd9Sstevel@tonic-gate ** If there is more than one, munge then 8637c478bd9Sstevel@tonic-gate ** into a map_coldelim separated string. 8647c478bd9Sstevel@tonic-gate ** If we are recursing we may have an entry 8657c478bd9Sstevel@tonic-gate ** with no 'normal' values to put in the 8667c478bd9Sstevel@tonic-gate ** string. 8677c478bd9Sstevel@tonic-gate ** This is not an error. 8687c478bd9Sstevel@tonic-gate */ 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate if (type == SM_LDAP_ATTR_NORMAL && 8717c478bd9Sstevel@tonic-gate bitset(SM_LDAP_SINGLEMATCH, flags) && 8727c478bd9Sstevel@tonic-gate *result != NULL) 8737c478bd9Sstevel@tonic-gate { 8747c478bd9Sstevel@tonic-gate /* only wanted one match */ 8757c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 8767c478bd9Sstevel@tonic-gate errno = ENOENT; 8777c478bd9Sstevel@tonic-gate return EX_NOTFOUND; 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate vsize = 0; 8817c478bd9Sstevel@tonic-gate for (i = 0; vals[i] != NULL; i++) 8827c478bd9Sstevel@tonic-gate { 8837c478bd9Sstevel@tonic-gate if (type == SM_LDAP_ATTR_DN || 8847c478bd9Sstevel@tonic-gate type == SM_LDAP_ATTR_FILTER || 8857c478bd9Sstevel@tonic-gate type == SM_LDAP_ATTR_URL) 8867c478bd9Sstevel@tonic-gate { 8877c478bd9Sstevel@tonic-gate /* add to recursion */ 8887c478bd9Sstevel@tonic-gate if (sm_ldap_add_recurse(&recurse, 8897c478bd9Sstevel@tonic-gate vals[i], 8907c478bd9Sstevel@tonic-gate type, 8917c478bd9Sstevel@tonic-gate rpool) == NULL) 8927c478bd9Sstevel@tonic-gate { 8937c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 8947c478bd9Sstevel@tonic-gate errno = ENOMEM; 8957c478bd9Sstevel@tonic-gate return EX_OSERR; 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate continue; 8987c478bd9Sstevel@tonic-gate } 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate vsize += strlen(vals[i]) + 1; 9017c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsep != '\0') 9027c478bd9Sstevel@tonic-gate vsize += strlen(attr) + 1; 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate /* 9067c478bd9Sstevel@tonic-gate ** Create/Append to string any normal 9077c478bd9Sstevel@tonic-gate ** attribute values. Otherwise, just free 9087c478bd9Sstevel@tonic-gate ** memory and move on to the next 9097c478bd9Sstevel@tonic-gate ** attribute in this entry. 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate if (type == SM_LDAP_ATTR_NORMAL && vsize > 0) 9137c478bd9Sstevel@tonic-gate { 9147c478bd9Sstevel@tonic-gate char *pe; 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate /* Grow result string if needed */ 9177c478bd9Sstevel@tonic-gate if ((*resultln + vsize) >= *resultsz) 9187c478bd9Sstevel@tonic-gate { 9197c478bd9Sstevel@tonic-gate while ((*resultln + vsize) >= *resultsz) 9207c478bd9Sstevel@tonic-gate { 9217c478bd9Sstevel@tonic-gate if (*resultsz == 0) 9227c478bd9Sstevel@tonic-gate *resultsz = 1024; 9237c478bd9Sstevel@tonic-gate else 9247c478bd9Sstevel@tonic-gate *resultsz *= 2; 9257c478bd9Sstevel@tonic-gate } 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate vp_tmp = sm_rpool_malloc_x(rpool, *resultsz); 9287c478bd9Sstevel@tonic-gate *vp_tmp = '\0'; 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate if (*result != NULL) 9317c478bd9Sstevel@tonic-gate sm_strlcpy(vp_tmp, 9327c478bd9Sstevel@tonic-gate *result, 9337c478bd9Sstevel@tonic-gate *resultsz); 9347c478bd9Sstevel@tonic-gate *result = vp_tmp; 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate p = *result + *resultln; 9387c478bd9Sstevel@tonic-gate pe = *result + *resultsz; 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate for (i = 0; vals[i] != NULL; i++) 9417c478bd9Sstevel@tonic-gate { 9427c478bd9Sstevel@tonic-gate if (*resultln > 0 && 9437c478bd9Sstevel@tonic-gate p < pe) 9447c478bd9Sstevel@tonic-gate *p++ = (char) delim; 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate if (lmap->ldap_attrsep != '\0') 9477c478bd9Sstevel@tonic-gate { 9487c478bd9Sstevel@tonic-gate p += sm_strlcpy(p, attr, 9497c478bd9Sstevel@tonic-gate pe - p); 9507c478bd9Sstevel@tonic-gate if (p < pe) 9517c478bd9Sstevel@tonic-gate *p++ = lmap->ldap_attrsep; 9527c478bd9Sstevel@tonic-gate } 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate p += sm_strlcpy(p, vals[i], 9557c478bd9Sstevel@tonic-gate pe - p); 9567c478bd9Sstevel@tonic-gate *resultln = p - (*result); 9577c478bd9Sstevel@tonic-gate if (p >= pe) 9587c478bd9Sstevel@tonic-gate { 9597c478bd9Sstevel@tonic-gate /* Internal error: buffer too small for LDAP values */ 9607c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 9617c478bd9Sstevel@tonic-gate errno = ENOMEM; 9627c478bd9Sstevel@tonic-gate return EX_OSERR; 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate ldap_value_free(vals); 9687c478bd9Sstevel@tonic-gate ldap_memfree(attr); 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate ** We check save_errno != LDAP_DECODING_ERROR since 9747c478bd9Sstevel@tonic-gate ** OpenLDAP 1.X has a very ugly *undocumented* 9757c478bd9Sstevel@tonic-gate ** hack of returning this error code from 9767c478bd9Sstevel@tonic-gate ** ldap_next_attribute() if the library freed the 9777c478bd9Sstevel@tonic-gate ** ber attribute. See: 9787c478bd9Sstevel@tonic-gate ** http://www.openldap.org/lists/openldap-devel/9901/msg00064.html 9797c478bd9Sstevel@tonic-gate */ 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate if (save_errno != LDAP_SUCCESS && 9827c478bd9Sstevel@tonic-gate save_errno != LDAP_DECODING_ERROR) 9837c478bd9Sstevel@tonic-gate { 9847c478bd9Sstevel@tonic-gate /* Must be an error */ 9857c478bd9Sstevel@tonic-gate save_errno += E_LDAPBASE; 9867c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 9877c478bd9Sstevel@tonic-gate errno = save_errno; 9887c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 9897c478bd9Sstevel@tonic-gate } 9907c478bd9Sstevel@tonic-gate 9917c478bd9Sstevel@tonic-gate /* mark this DN as done */ 9927c478bd9Sstevel@tonic-gate rl->lr_done = true; 9937c478bd9Sstevel@tonic-gate if (rl->lr_ludp != NULL) 9947c478bd9Sstevel@tonic-gate { 9957c478bd9Sstevel@tonic-gate ldap_free_urldesc(rl->lr_ludp); 9967c478bd9Sstevel@tonic-gate rl->lr_ludp = NULL; 9977c478bd9Sstevel@tonic-gate } 9987c478bd9Sstevel@tonic-gate if (rl->lr_attrs != NULL) 9997c478bd9Sstevel@tonic-gate { 10007c478bd9Sstevel@tonic-gate free(rl->lr_attrs); 10017c478bd9Sstevel@tonic-gate rl->lr_attrs = NULL; 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate /* We don't want multiple values and we have one */ 10057c478bd9Sstevel@tonic-gate if ((char) delim == '\0' && 10067c478bd9Sstevel@tonic-gate !bitset(SM_LDAP_SINGLEMATCH, flags) && 10077c478bd9Sstevel@tonic-gate *result != NULL) 10087c478bd9Sstevel@tonic-gate break; 10097c478bd9Sstevel@tonic-gate } 10107c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 10117c478bd9Sstevel@tonic-gate if (save_errno != LDAP_SUCCESS && 10127c478bd9Sstevel@tonic-gate save_errno != LDAP_DECODING_ERROR) 10137c478bd9Sstevel@tonic-gate { 10147c478bd9Sstevel@tonic-gate /* Must be an error */ 10157c478bd9Sstevel@tonic-gate save_errno += E_LDAPBASE; 10167c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 10177c478bd9Sstevel@tonic-gate errno = save_errno; 10187c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate ldap_msgfree(lmap->ldap_res); 10217c478bd9Sstevel@tonic-gate lmap->ldap_res = NULL; 10227c478bd9Sstevel@tonic-gate } 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate if (ret == 0) 10257c478bd9Sstevel@tonic-gate save_errno = ETIMEDOUT; 10267c478bd9Sstevel@tonic-gate else 10277c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 10287c478bd9Sstevel@tonic-gate if (save_errno != LDAP_SUCCESS) 10297c478bd9Sstevel@tonic-gate { 10307c478bd9Sstevel@tonic-gate statp = EX_TEMPFAIL; 10317c478bd9Sstevel@tonic-gate if (ret != 0) 10327c478bd9Sstevel@tonic-gate { 10337c478bd9Sstevel@tonic-gate switch (save_errno) 10347c478bd9Sstevel@tonic-gate { 10357c478bd9Sstevel@tonic-gate #ifdef LDAP_SERVER_DOWN 10367c478bd9Sstevel@tonic-gate case LDAP_SERVER_DOWN: 10377c478bd9Sstevel@tonic-gate #endif /* LDAP_SERVER_DOWN */ 10387c478bd9Sstevel@tonic-gate case LDAP_TIMEOUT: 10397c478bd9Sstevel@tonic-gate case LDAP_UNAVAILABLE: 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate /* 10427c478bd9Sstevel@tonic-gate ** server disappeared, 10437c478bd9Sstevel@tonic-gate ** try reopen on next search 10447c478bd9Sstevel@tonic-gate */ 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate statp = EX_RESTART; 10477c478bd9Sstevel@tonic-gate break; 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate save_errno += E_LDAPBASE; 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate SM_LDAP_ERROR_CLEANUP(); 10527c478bd9Sstevel@tonic-gate errno = save_errno; 10537c478bd9Sstevel@tonic-gate return statp; 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate if (lmap->ldap_res != NULL) 10577c478bd9Sstevel@tonic-gate { 10587c478bd9Sstevel@tonic-gate ldap_msgfree(lmap->ldap_res); 10597c478bd9Sstevel@tonic-gate lmap->ldap_res = NULL; 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate if (toplevel) 10637c478bd9Sstevel@tonic-gate { 10647c478bd9Sstevel@tonic-gate int rlidx; 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate /* 10677c478bd9Sstevel@tonic-gate ** Spin through the built-up recurse list at the top 10687c478bd9Sstevel@tonic-gate ** of the recursion. Since new items are added at the 10697c478bd9Sstevel@tonic-gate ** end of the shared list, we actually only ever get 10707c478bd9Sstevel@tonic-gate ** one level of recursion before things pop back to the 10717c478bd9Sstevel@tonic-gate ** top. Any items added to the list during that recursion 10727c478bd9Sstevel@tonic-gate ** will be expanded by the top level. 10737c478bd9Sstevel@tonic-gate */ 10747c478bd9Sstevel@tonic-gate 10757c478bd9Sstevel@tonic-gate for (rlidx = 0; recurse != NULL && rlidx < recurse->lr_cnt; rlidx++) 10767c478bd9Sstevel@tonic-gate { 10777c478bd9Sstevel@tonic-gate int newflags; 10787c478bd9Sstevel@tonic-gate int sid; 10797c478bd9Sstevel@tonic-gate int status; 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate rl = recurse->lr_data[rlidx]; 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate newflags = flags; 10847c478bd9Sstevel@tonic-gate if (rl->lr_done) 10857c478bd9Sstevel@tonic-gate { 10867c478bd9Sstevel@tonic-gate /* already expanded */ 10877c478bd9Sstevel@tonic-gate continue; 10887c478bd9Sstevel@tonic-gate } 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate if (rl->lr_type == SM_LDAP_ATTR_DN) 10917c478bd9Sstevel@tonic-gate { 10927c478bd9Sstevel@tonic-gate /* do DN search */ 10937c478bd9Sstevel@tonic-gate sid = ldap_search(lmap->ldap_ld, 10947c478bd9Sstevel@tonic-gate rl->lr_search, 10957c478bd9Sstevel@tonic-gate lmap->ldap_scope, 10967c478bd9Sstevel@tonic-gate "(objectClass=*)", 10977c478bd9Sstevel@tonic-gate (lmap->ldap_attr[0] == NULL ? 10987c478bd9Sstevel@tonic-gate NULL : lmap->ldap_attr), 10997c478bd9Sstevel@tonic-gate lmap->ldap_attrsonly); 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate else if (rl->lr_type == SM_LDAP_ATTR_FILTER) 11027c478bd9Sstevel@tonic-gate { 11037c478bd9Sstevel@tonic-gate /* do new search */ 11047c478bd9Sstevel@tonic-gate sid = ldap_search(lmap->ldap_ld, 11057c478bd9Sstevel@tonic-gate lmap->ldap_base, 11067c478bd9Sstevel@tonic-gate lmap->ldap_scope, 11077c478bd9Sstevel@tonic-gate rl->lr_search, 11087c478bd9Sstevel@tonic-gate (lmap->ldap_attr[0] == NULL ? 11097c478bd9Sstevel@tonic-gate NULL : lmap->ldap_attr), 11107c478bd9Sstevel@tonic-gate lmap->ldap_attrsonly); 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate else if (rl->lr_type == SM_LDAP_ATTR_URL) 11137c478bd9Sstevel@tonic-gate { 11147c478bd9Sstevel@tonic-gate /* Parse URL */ 11157c478bd9Sstevel@tonic-gate sid = ldap_url_parse(rl->lr_search, 11167c478bd9Sstevel@tonic-gate &rl->lr_ludp); 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate if (sid != 0) 11197c478bd9Sstevel@tonic-gate { 11207c478bd9Sstevel@tonic-gate errno = sid + E_LDAPURLBASE; 11217c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate /* We need to add objectClass */ 11257c478bd9Sstevel@tonic-gate if (rl->lr_ludp->lud_attrs != NULL) 11267c478bd9Sstevel@tonic-gate { 11277c478bd9Sstevel@tonic-gate int attrnum = 0; 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate while (rl->lr_ludp->lud_attrs[attrnum] != NULL) 11307c478bd9Sstevel@tonic-gate { 11317c478bd9Sstevel@tonic-gate if (strcasecmp(rl->lr_ludp->lud_attrs[attrnum], 11327c478bd9Sstevel@tonic-gate "objectClass") == 0) 11337c478bd9Sstevel@tonic-gate { 11347c478bd9Sstevel@tonic-gate /* already requested */ 11357c478bd9Sstevel@tonic-gate attrnum = -1; 11367c478bd9Sstevel@tonic-gate break; 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate attrnum++; 11397c478bd9Sstevel@tonic-gate } 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate if (attrnum >= 0) 11427c478bd9Sstevel@tonic-gate { 11437c478bd9Sstevel@tonic-gate int i; 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate rl->lr_attrs = (char **)malloc(sizeof(char *) * (attrnum + 2)); 11467c478bd9Sstevel@tonic-gate if (rl->lr_attrs == NULL) 11477c478bd9Sstevel@tonic-gate { 11487c478bd9Sstevel@tonic-gate save_errno = errno; 11497c478bd9Sstevel@tonic-gate ldap_free_urldesc(rl->lr_ludp); 11507c478bd9Sstevel@tonic-gate errno = save_errno; 11517c478bd9Sstevel@tonic-gate return EX_TEMPFAIL; 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate for (i = 0 ; i < attrnum; i++) 11547c478bd9Sstevel@tonic-gate { 11557c478bd9Sstevel@tonic-gate rl->lr_attrs[i] = rl->lr_ludp->lud_attrs[i]; 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate rl->lr_attrs[i++] = "objectClass"; 11587c478bd9Sstevel@tonic-gate rl->lr_attrs[i++] = NULL; 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate /* 11637c478bd9Sstevel@tonic-gate ** Use the existing connection 11647c478bd9Sstevel@tonic-gate ** for this search. It really 11657c478bd9Sstevel@tonic-gate ** should use lud_scheme://lud_host:lud_port/ 11667c478bd9Sstevel@tonic-gate ** instead but that would require 11677c478bd9Sstevel@tonic-gate ** opening a new connection. 11687c478bd9Sstevel@tonic-gate ** This should be fixed ASAP. 11697c478bd9Sstevel@tonic-gate */ 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate sid = ldap_search(lmap->ldap_ld, 11727c478bd9Sstevel@tonic-gate rl->lr_ludp->lud_dn, 11737c478bd9Sstevel@tonic-gate rl->lr_ludp->lud_scope, 11747c478bd9Sstevel@tonic-gate rl->lr_ludp->lud_filter, 11757c478bd9Sstevel@tonic-gate rl->lr_attrs, 11767c478bd9Sstevel@tonic-gate lmap->ldap_attrsonly); 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate /* Use the attributes specified by URL */ 11797c478bd9Sstevel@tonic-gate newflags |= SM_LDAP_USE_ALLATTR; 11807c478bd9Sstevel@tonic-gate } 11817c478bd9Sstevel@tonic-gate else 11827c478bd9Sstevel@tonic-gate { 11837c478bd9Sstevel@tonic-gate /* unknown or illegal attribute type */ 11847c478bd9Sstevel@tonic-gate errno = EFAULT; 11857c478bd9Sstevel@tonic-gate return EX_SOFTWARE; 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate /* Collect results */ 11897c478bd9Sstevel@tonic-gate if (sid == -1) 11907c478bd9Sstevel@tonic-gate { 11917c478bd9Sstevel@tonic-gate save_errno = sm_ldap_geterrno(lmap->ldap_ld); 11927c478bd9Sstevel@tonic-gate statp = EX_TEMPFAIL; 11937c478bd9Sstevel@tonic-gate switch (save_errno) 11947c478bd9Sstevel@tonic-gate { 11957c478bd9Sstevel@tonic-gate #ifdef LDAP_SERVER_DOWN 11967c478bd9Sstevel@tonic-gate case LDAP_SERVER_DOWN: 11977c478bd9Sstevel@tonic-gate #endif /* LDAP_SERVER_DOWN */ 11987c478bd9Sstevel@tonic-gate case LDAP_TIMEOUT: 11997c478bd9Sstevel@tonic-gate case LDAP_UNAVAILABLE: 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate /* 12027c478bd9Sstevel@tonic-gate ** server disappeared, 12037c478bd9Sstevel@tonic-gate ** try reopen on next search 12047c478bd9Sstevel@tonic-gate */ 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate statp = EX_RESTART; 12077c478bd9Sstevel@tonic-gate break; 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate errno = save_errno + E_LDAPBASE; 12107c478bd9Sstevel@tonic-gate return statp; 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate status = sm_ldap_results(lmap, sid, newflags, delim, 12147c478bd9Sstevel@tonic-gate rpool, result, resultln, 12157c478bd9Sstevel@tonic-gate resultsz, recurse); 12167c478bd9Sstevel@tonic-gate save_errno = errno; 12177c478bd9Sstevel@tonic-gate if (status != EX_OK && status != EX_NOTFOUND) 12187c478bd9Sstevel@tonic-gate { 12197c478bd9Sstevel@tonic-gate errno = save_errno; 12207c478bd9Sstevel@tonic-gate return status; 12217c478bd9Sstevel@tonic-gate } 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate /* Mark as done */ 12247c478bd9Sstevel@tonic-gate rl->lr_done = true; 12257c478bd9Sstevel@tonic-gate if (rl->lr_ludp != NULL) 12267c478bd9Sstevel@tonic-gate { 12277c478bd9Sstevel@tonic-gate ldap_free_urldesc(rl->lr_ludp); 12287c478bd9Sstevel@tonic-gate rl->lr_ludp = NULL; 12297c478bd9Sstevel@tonic-gate } 12307c478bd9Sstevel@tonic-gate if (rl->lr_attrs != NULL) 12317c478bd9Sstevel@tonic-gate { 12327c478bd9Sstevel@tonic-gate free(rl->lr_attrs); 12337c478bd9Sstevel@tonic-gate rl->lr_attrs = NULL; 12347c478bd9Sstevel@tonic-gate } 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate /* Reset rlidx as new items may have been added */ 12377c478bd9Sstevel@tonic-gate rlidx = -1; 12387c478bd9Sstevel@tonic-gate } 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate return statp; 12417c478bd9Sstevel@tonic-gate } 12427c478bd9Sstevel@tonic-gate 12437c478bd9Sstevel@tonic-gate /* 12447c478bd9Sstevel@tonic-gate ** SM_LDAP_CLOSE -- close LDAP connection 12457c478bd9Sstevel@tonic-gate ** 12467c478bd9Sstevel@tonic-gate ** Parameters: 12477c478bd9Sstevel@tonic-gate ** lmap -- LDAP map information 12487c478bd9Sstevel@tonic-gate ** 12497c478bd9Sstevel@tonic-gate ** Returns: 12507c478bd9Sstevel@tonic-gate ** None. 12517c478bd9Sstevel@tonic-gate ** 12527c478bd9Sstevel@tonic-gate */ 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate void 12557c478bd9Sstevel@tonic-gate sm_ldap_close(lmap) 12567c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 12577c478bd9Sstevel@tonic-gate { 12587c478bd9Sstevel@tonic-gate if (lmap->ldap_ld == NULL) 12597c478bd9Sstevel@tonic-gate return; 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate if (lmap->ldap_pid == getpid()) 12627c478bd9Sstevel@tonic-gate ldap_unbind(lmap->ldap_ld); 12637c478bd9Sstevel@tonic-gate lmap->ldap_ld = NULL; 12647c478bd9Sstevel@tonic-gate lmap->ldap_pid = 0; 12657c478bd9Sstevel@tonic-gate } 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate /* 12687c478bd9Sstevel@tonic-gate ** SM_LDAP_SETOPTS -- set LDAP options 12697c478bd9Sstevel@tonic-gate ** 12707c478bd9Sstevel@tonic-gate ** Parameters: 12717c478bd9Sstevel@tonic-gate ** ld -- LDAP session handle 12727c478bd9Sstevel@tonic-gate ** lmap -- LDAP map information 12737c478bd9Sstevel@tonic-gate ** 12747c478bd9Sstevel@tonic-gate ** Returns: 12757c478bd9Sstevel@tonic-gate ** None. 12767c478bd9Sstevel@tonic-gate ** 12777c478bd9Sstevel@tonic-gate */ 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate void 12807c478bd9Sstevel@tonic-gate sm_ldap_setopts(ld, lmap) 12817c478bd9Sstevel@tonic-gate LDAP *ld; 12827c478bd9Sstevel@tonic-gate SM_LDAP_STRUCT *lmap; 12837c478bd9Sstevel@tonic-gate { 12847c478bd9Sstevel@tonic-gate # if USE_LDAP_SET_OPTION 12857c478bd9Sstevel@tonic-gate if (lmap->ldap_version != 0) 12867c478bd9Sstevel@tonic-gate { 12877c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, 12887c478bd9Sstevel@tonic-gate &lmap->ldap_version); 12897c478bd9Sstevel@tonic-gate } 12907c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_DEREF, &lmap->ldap_deref); 12917c478bd9Sstevel@tonic-gate if (bitset(LDAP_OPT_REFERRALS, lmap->ldap_options)) 12927c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON); 12937c478bd9Sstevel@tonic-gate else 12947c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF); 12957c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &lmap->ldap_sizelimit); 12967c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &lmap->ldap_timelimit); 12977c478bd9Sstevel@tonic-gate # ifdef LDAP_OPT_RESTART 12987c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_RESTART, LDAP_OPT_ON); 12997c478bd9Sstevel@tonic-gate # endif /* LDAP_OPT_RESTART */ 13007c478bd9Sstevel@tonic-gate # else /* USE_LDAP_SET_OPTION */ 13017c478bd9Sstevel@tonic-gate /* From here on in we can use ldap internal timelimits */ 13027c478bd9Sstevel@tonic-gate ld->ld_deref = lmap->ldap_deref; 13037c478bd9Sstevel@tonic-gate ld->ld_options = lmap->ldap_options; 13047c478bd9Sstevel@tonic-gate ld->ld_sizelimit = lmap->ldap_sizelimit; 13057c478bd9Sstevel@tonic-gate ld->ld_timelimit = lmap->ldap_timelimit; 13067c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_SET_OPTION */ 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate /* 13107c478bd9Sstevel@tonic-gate ** SM_LDAP_GETERRNO -- get ldap errno value 13117c478bd9Sstevel@tonic-gate ** 13127c478bd9Sstevel@tonic-gate ** Parameters: 13137c478bd9Sstevel@tonic-gate ** ld -- LDAP session handle 13147c478bd9Sstevel@tonic-gate ** 13157c478bd9Sstevel@tonic-gate ** Returns: 13167c478bd9Sstevel@tonic-gate ** LDAP errno. 13177c478bd9Sstevel@tonic-gate ** 13187c478bd9Sstevel@tonic-gate */ 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate int 13217c478bd9Sstevel@tonic-gate sm_ldap_geterrno(ld) 13227c478bd9Sstevel@tonic-gate LDAP *ld; 13237c478bd9Sstevel@tonic-gate { 13247c478bd9Sstevel@tonic-gate int err = LDAP_SUCCESS; 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate # if defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 13277c478bd9Sstevel@tonic-gate (void) ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &err); 13287c478bd9Sstevel@tonic-gate # else /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */ 13297c478bd9Sstevel@tonic-gate # ifdef LDAP_OPT_SIZELIMIT 13307c478bd9Sstevel@tonic-gate err = ldap_get_lderrno(ld, NULL, NULL); 13317c478bd9Sstevel@tonic-gate # else /* LDAP_OPT_SIZELIMIT */ 13327c478bd9Sstevel@tonic-gate err = ld->ld_errno; 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate /* 13357c478bd9Sstevel@tonic-gate ** Reset value to prevent lingering LDAP_DECODING_ERROR due to 13367c478bd9Sstevel@tonic-gate ** OpenLDAP 1.X's hack (see above) 13377c478bd9Sstevel@tonic-gate */ 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate ld->ld_errno = LDAP_SUCCESS; 13407c478bd9Sstevel@tonic-gate # endif /* LDAP_OPT_SIZELIMIT */ 13417c478bd9Sstevel@tonic-gate # endif /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */ 13427c478bd9Sstevel@tonic-gate return err; 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate # endif /* LDAPMAP */ 1345