17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
4695ef821SGordon Ross *
5695ef821SGordon Ross * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
67c478bd9Sstevel@tonic-gate */
77c478bd9Sstevel@tonic-gate
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
117c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
127c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
137c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
167c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
177c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
187c478bd9Sstevel@tonic-gate * rights and limitations under the License.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
217c478bd9Sstevel@tonic-gate * March 31, 1998.
227c478bd9Sstevel@tonic-gate *
237c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
247c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
257c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
267c478bd9Sstevel@tonic-gate * Rights Reserved.
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate * Contributor(s):
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan.
327c478bd9Sstevel@tonic-gate * All rights reserved.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * search.c
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #if 0
39*55fea89dSDan Cross #ifndef lint
407c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include "ldap-int.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static int nsldapi_timeval2ldaplimit( struct timeval *timeoutp,
477c478bd9Sstevel@tonic-gate int defaultvalue );
487c478bd9Sstevel@tonic-gate static int nsldapi_search( LDAP *ld, const char *base, int scope,
497c478bd9Sstevel@tonic-gate const char *filter, char **attrs, int attrsonly,
507c478bd9Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls,
517c478bd9Sstevel@tonic-gate int timelimit, int sizelimit, int *msgidp );
527c478bd9Sstevel@tonic-gate static char *find_right_paren( char *s );
537c478bd9Sstevel@tonic-gate static char *put_complex_filter( BerElement *ber, char *str,
547c478bd9Sstevel@tonic-gate ber_tag_t tag, int not );
557c478bd9Sstevel@tonic-gate static int unescape_filterval( char *str );
567c478bd9Sstevel@tonic-gate static int hexchar2int( char c );
577c478bd9Sstevel@tonic-gate static int is_valid_attr( char *a );
587c478bd9Sstevel@tonic-gate static int put_simple_filter( BerElement *ber, char *str );
597c478bd9Sstevel@tonic-gate static int put_substring_filter( BerElement *ber, char *type,
607c478bd9Sstevel@tonic-gate char *str );
617c478bd9Sstevel@tonic-gate static int put_filter_list( BerElement *ber, char *str );
62*55fea89dSDan Cross static int nsldapi_search_s( LDAP *ld, const char *base, int scope,
637c478bd9Sstevel@tonic-gate const char *filter, char **attrs, int attrsonly,
647c478bd9Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls,
657c478bd9Sstevel@tonic-gate struct timeval *localtimeoutp, int timelimit, int sizelimit,
667c478bd9Sstevel@tonic-gate LDAPMessage **res );
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate * ldap_search - initiate an ldap search operation. Parameters:
707c478bd9Sstevel@tonic-gate *
717c478bd9Sstevel@tonic-gate * ld LDAP descriptor
727c478bd9Sstevel@tonic-gate * base DN of the base object
737c478bd9Sstevel@tonic-gate * scope the search scope - one of LDAP_SCOPE_BASE,
747c478bd9Sstevel@tonic-gate * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
757c478bd9Sstevel@tonic-gate * filter a string containing the search filter
767c478bd9Sstevel@tonic-gate * (e.g., "(|(cn=bob)(sn=bob))")
777c478bd9Sstevel@tonic-gate * attrs list of attribute types to return for matches
787c478bd9Sstevel@tonic-gate * attrsonly 1 => attributes only 0 => attributes and values
797c478bd9Sstevel@tonic-gate *
807c478bd9Sstevel@tonic-gate * Example:
817c478bd9Sstevel@tonic-gate * char *attrs[] = { "mail", "title", 0 };
827c478bd9Sstevel@tonic-gate * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
837c478bd9Sstevel@tonic-gate * attrs, attrsonly );
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate int
867c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly)877c478bd9Sstevel@tonic-gate ldap_search(
887c478bd9Sstevel@tonic-gate LDAP *ld,
897c478bd9Sstevel@tonic-gate const char *base,
907c478bd9Sstevel@tonic-gate int scope,
917c478bd9Sstevel@tonic-gate const char *filter,
927c478bd9Sstevel@tonic-gate char **attrs,
937c478bd9Sstevel@tonic-gate int attrsonly
947c478bd9Sstevel@tonic-gate )
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate int msgid;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate if ( ldap_search_ext( ld, base, scope, filter, attrs, attrsonly, NULL,
1017c478bd9Sstevel@tonic-gate NULL, NULL, -1, &msgid ) == LDAP_SUCCESS ) {
1027c478bd9Sstevel@tonic-gate return( msgid );
1037c478bd9Sstevel@tonic-gate } else {
1047c478bd9Sstevel@tonic-gate return( -1 ); /* error is in ld handle */
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * LDAPv3 extended search.
1117c478bd9Sstevel@tonic-gate * Returns an LDAP error code.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate int
1147c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search_ext(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,struct timeval * timeoutp,int sizelimit,int * msgidp)1157c478bd9Sstevel@tonic-gate ldap_search_ext(
1167c478bd9Sstevel@tonic-gate LDAP *ld,
1177c478bd9Sstevel@tonic-gate const char *base,
1187c478bd9Sstevel@tonic-gate int scope,
1197c478bd9Sstevel@tonic-gate const char *filter,
1207c478bd9Sstevel@tonic-gate char **attrs,
1217c478bd9Sstevel@tonic-gate int attrsonly,
1227c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
1237c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
1247c478bd9Sstevel@tonic-gate struct timeval *timeoutp, /* NULL means use ld->ld_timelimit */
1257c478bd9Sstevel@tonic-gate int sizelimit,
1267c478bd9Sstevel@tonic-gate int *msgidp
1277c478bd9Sstevel@tonic-gate )
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * It is an error to pass in a zero'd timeval.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate if ( timeoutp != NULL && timeoutp->tv_sec == 0 &&
1337c478bd9Sstevel@tonic-gate timeoutp->tv_usec == 0 ) {
1347c478bd9Sstevel@tonic-gate if ( ld != NULL ) {
1357c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate return( nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
1417c478bd9Sstevel@tonic-gate serverctrls, clientctrls,
1427c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, msgidp ));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * Like ldap_search_ext() except an integer timelimit is passed instead of
1487c478bd9Sstevel@tonic-gate * using the overloaded struct timeval *timeoutp.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate static int
nsldapi_search(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int timelimit,int sizelimit,int * msgidp)1517c478bd9Sstevel@tonic-gate nsldapi_search(
1527c478bd9Sstevel@tonic-gate LDAP *ld,
1537c478bd9Sstevel@tonic-gate const char *base,
1547c478bd9Sstevel@tonic-gate int scope,
1557c478bd9Sstevel@tonic-gate const char *filter,
1567c478bd9Sstevel@tonic-gate char **attrs,
1577c478bd9Sstevel@tonic-gate int attrsonly,
1587c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
1597c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
1607c478bd9Sstevel@tonic-gate int timelimit, /* -1 means use ld->ld_timelimit */
1617c478bd9Sstevel@tonic-gate int sizelimit, /* -1 means use ld->ld_sizelimit */
1627c478bd9Sstevel@tonic-gate int *msgidp
1637c478bd9Sstevel@tonic-gate )
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate BerElement *ber;
1667c478bd9Sstevel@tonic-gate int rc, rc_key;
1677c478bd9Sstevel@tonic-gate unsigned long key; /* XXXmcs: memcache */
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1727c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if ( base == NULL ) {
1767c478bd9Sstevel@tonic-gate base = "";
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if ( filter == NULL ) {
1807c478bd9Sstevel@tonic-gate filter = "(objectclass=*)";
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if ( msgidp == NULL || ( scope != LDAP_SCOPE_BASE
1847c478bd9Sstevel@tonic-gate && scope != LDAP_SCOPE_ONELEVEL && scope != LDAP_SCOPE_SUBTREE )
1857c478bd9Sstevel@tonic-gate || ( sizelimit < -1 )) {
1867c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1877c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1907c478bd9Sstevel@tonic-gate *msgidp = ++ld->ld_msgid;
1917c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * XXXmcs: should use cache function pointers to hook in memcache
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate if ( ld->ld_memcache == NULL ) {
1977c478bd9Sstevel@tonic-gate rc_key = LDAP_NOT_SUPPORTED;
1987c478bd9Sstevel@tonic-gate } else if (( rc_key = ldap_memcache_createkey( ld, base, scope, filter,
1997c478bd9Sstevel@tonic-gate attrs, attrsonly, serverctrls, clientctrls, &key)) == LDAP_SUCCESS
2007c478bd9Sstevel@tonic-gate && ldap_memcache_result( ld, *msgidp, key ) == LDAP_SUCCESS ) {
2017c478bd9Sstevel@tonic-gate return LDAP_SUCCESS;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate /* check the cache */
2057c478bd9Sstevel@tonic-gate if ( ld->ld_cache_on && ld->ld_cache_search != NULL ) {
2067c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
2077c478bd9Sstevel@tonic-gate if ( (rc = (ld->ld_cache_search)( ld, *msgidp, LDAP_REQ_SEARCH,
2087c478bd9Sstevel@tonic-gate base, scope, filter, attrs, attrsonly )) != 0 ) {
2097c478bd9Sstevel@tonic-gate *msgidp = rc;
2107c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
2117c478bd9Sstevel@tonic-gate return( LDAP_SUCCESS );
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /* caching off or did not find it in the cache - check the net */
2177c478bd9Sstevel@tonic-gate if (( rc = nsldapi_build_search_req( ld, base, scope, filter, attrs,
2187c478bd9Sstevel@tonic-gate attrsonly, serverctrls, clientctrls, timelimit, sizelimit,
2197c478bd9Sstevel@tonic-gate *msgidp, &ber )) != LDAP_SUCCESS ) {
2207c478bd9Sstevel@tonic-gate return( rc );
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /* send the message */
2247c478bd9Sstevel@tonic-gate rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_SEARCH,
2257c478bd9Sstevel@tonic-gate (char *) base, ber );
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * XXXmcs: should use cache function pointers to hook in memcache
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate if ( (rc_key == LDAP_SUCCESS) && (rc >= 0) ) {
2317c478bd9Sstevel@tonic-gate ldap_memcache_new( ld, rc, key, base );
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate *msgidp = rc;
2357c478bd9Sstevel@tonic-gate return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * Convert a non-NULL timeoutp to a value in seconds that is appropriate to
2417c478bd9Sstevel@tonic-gate * send in an LDAP search request. If timeoutp is NULL, return defaultvalue.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate static int
nsldapi_timeval2ldaplimit(struct timeval * timeoutp,int defaultvalue)2447c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( struct timeval *timeoutp, int defaultvalue )
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate int timelimit;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if ( NULL == timeoutp ) {
2497c478bd9Sstevel@tonic-gate timelimit = defaultvalue;
2507c478bd9Sstevel@tonic-gate } else if ( timeoutp->tv_sec > 0 ) {
2517c478bd9Sstevel@tonic-gate timelimit = timeoutp->tv_sec;
2527c478bd9Sstevel@tonic-gate } else if ( timeoutp->tv_usec > 0 ) {
2537c478bd9Sstevel@tonic-gate timelimit = 1; /* minimum we can express in LDAP */
2547c478bd9Sstevel@tonic-gate } else {
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * both tv_sec and tv_usec are less than one (zero?) so
2577c478bd9Sstevel@tonic-gate * to maintain compatiblity with our "zero means no limit"
2587c478bd9Sstevel@tonic-gate * convention we pass no limit to the server.
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate timelimit = 0; /* no limit */
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate return( timelimit );
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /* returns an LDAP error code and also sets it in ld */
2687c478bd9Sstevel@tonic-gate int
nsldapi_build_search_req(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int timelimit,int sizelimit,int msgid,BerElement ** berp)2697c478bd9Sstevel@tonic-gate nsldapi_build_search_req(
270*55fea89dSDan Cross LDAP *ld,
271*55fea89dSDan Cross const char *base,
272*55fea89dSDan Cross int scope,
2737c478bd9Sstevel@tonic-gate const char *filter,
274*55fea89dSDan Cross char **attrs,
2757c478bd9Sstevel@tonic-gate int attrsonly,
2767c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
2777c478bd9Sstevel@tonic-gate LDAPControl **clientctrls, /* not used for anything yet */
2787c478bd9Sstevel@tonic-gate int timelimit, /* if -1, ld->ld_timelimit is used */
2797c478bd9Sstevel@tonic-gate int sizelimit, /* if -1, ld->ld_sizelimit is used */
2807c478bd9Sstevel@tonic-gate int msgid,
2817c478bd9Sstevel@tonic-gate BerElement **berp
2827c478bd9Sstevel@tonic-gate )
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate BerElement *ber;
2857c478bd9Sstevel@tonic-gate int err;
2867c478bd9Sstevel@tonic-gate char *fdup;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate * Create the search request. It looks like this:
2907c478bd9Sstevel@tonic-gate * SearchRequest := [APPLICATION 3] SEQUENCE {
2917c478bd9Sstevel@tonic-gate * baseObject DistinguishedName,
2927c478bd9Sstevel@tonic-gate * scope ENUMERATED {
2937c478bd9Sstevel@tonic-gate * baseObject (0),
2947c478bd9Sstevel@tonic-gate * singleLevel (1),
2957c478bd9Sstevel@tonic-gate * wholeSubtree (2)
2967c478bd9Sstevel@tonic-gate * },
2977c478bd9Sstevel@tonic-gate * derefAliases ENUMERATED {
2987c478bd9Sstevel@tonic-gate * neverDerefaliases (0),
2997c478bd9Sstevel@tonic-gate * derefInSearching (1),
3007c478bd9Sstevel@tonic-gate * derefFindingBaseObj (2),
3017c478bd9Sstevel@tonic-gate * alwaysDerefAliases (3)
3027c478bd9Sstevel@tonic-gate * },
3037c478bd9Sstevel@tonic-gate * sizelimit INTEGER (0 .. 65535),
3047c478bd9Sstevel@tonic-gate * timelimit INTEGER (0 .. 65535),
3057c478bd9Sstevel@tonic-gate * attrsOnly BOOLEAN,
3067c478bd9Sstevel@tonic-gate * filter Filter,
3077c478bd9Sstevel@tonic-gate * attributes SEQUENCE OF AttributeType
3087c478bd9Sstevel@tonic-gate * }
3097c478bd9Sstevel@tonic-gate * wrapped in an ldap message.
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /* create a message to send */
3137c478bd9Sstevel@tonic-gate if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
3147c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
3157c478bd9Sstevel@tonic-gate return( err );
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate if ( base == NULL ) {
3197c478bd9Sstevel@tonic-gate base = "";
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate if ( sizelimit == -1 ) {
3237c478bd9Sstevel@tonic-gate sizelimit = ld->ld_sizelimit;
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate if ( timelimit == -1 ) {
3277c478bd9Sstevel@tonic-gate timelimit = ld->ld_timelimit;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate #ifdef CLDAP
3317c478bd9Sstevel@tonic-gate if ( ld->ld_sbp->sb_naddr > 0 ) {
3327c478bd9Sstevel@tonic-gate err = ber_printf( ber, "{ist{seeiib", msgid,
3337c478bd9Sstevel@tonic-gate ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
3347c478bd9Sstevel@tonic-gate sizelimit, timelimit, attrsonly );
3357c478bd9Sstevel@tonic-gate } else {
3367c478bd9Sstevel@tonic-gate #endif /* CLDAP */
3377c478bd9Sstevel@tonic-gate err = ber_printf( ber, "{it{seeiib", msgid,
3387c478bd9Sstevel@tonic-gate LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
3397c478bd9Sstevel@tonic-gate sizelimit, timelimit, attrsonly );
3407c478bd9Sstevel@tonic-gate #ifdef CLDAP
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate #endif /* CLDAP */
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate if ( err == -1 ) {
3457c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
3467c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
3477c478bd9Sstevel@tonic-gate return( LDAP_ENCODING_ERROR );
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate fdup = nsldapi_strdup( filter );
3517c478bd9Sstevel@tonic-gate if (fdup == NULL) {
3527c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
3537c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
3547c478bd9Sstevel@tonic-gate return( LDAP_NO_MEMORY );
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate err = ldap_put_filter( ber, fdup );
3577c478bd9Sstevel@tonic-gate NSLDAPI_FREE( fdup );
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate if ( err == -1 ) {
3607c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_FILTER_ERROR, NULL, NULL );
3617c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
3627c478bd9Sstevel@tonic-gate return( LDAP_FILTER_ERROR );
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "{v}}", attrs ) == -1 ) {
3667c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
3677c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
3687c478bd9Sstevel@tonic-gate return( LDAP_ENCODING_ERROR );
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate if ( (err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
3727c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
3737c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
3747c478bd9Sstevel@tonic-gate return( err );
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate *berp = ber;
3787c478bd9Sstevel@tonic-gate return( LDAP_SUCCESS );
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate static char *
find_right_paren(char * s)3827c478bd9Sstevel@tonic-gate find_right_paren( char *s )
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate int balance, escape;
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate balance = 1;
3877c478bd9Sstevel@tonic-gate escape = 0;
3887c478bd9Sstevel@tonic-gate while ( *s && balance ) {
3897c478bd9Sstevel@tonic-gate if ( escape == 0 ) {
3907c478bd9Sstevel@tonic-gate if ( *s == '(' )
3917c478bd9Sstevel@tonic-gate balance++;
3927c478bd9Sstevel@tonic-gate else if ( *s == ')' )
3937c478bd9Sstevel@tonic-gate balance--;
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate if ( *s == '\\' && ! escape )
3967c478bd9Sstevel@tonic-gate escape = 1;
3977c478bd9Sstevel@tonic-gate else
3987c478bd9Sstevel@tonic-gate escape = 0;
3997c478bd9Sstevel@tonic-gate if ( balance )
4007c478bd9Sstevel@tonic-gate s++;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate return( *s ? s : NULL );
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate static char *
put_complex_filter(BerElement * ber,char * str,ber_tag_t tag,int not)4077c478bd9Sstevel@tonic-gate put_complex_filter(
408*55fea89dSDan Cross BerElement *ber,
409*55fea89dSDan Cross char *str,
410*55fea89dSDan Cross ber_tag_t tag,
411*55fea89dSDan Cross int not
4127c478bd9Sstevel@tonic-gate )
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate char *next;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate * We have (x(filter)...) with str sitting on
4187c478bd9Sstevel@tonic-gate * the x. We have to find the paren matching
4197c478bd9Sstevel@tonic-gate * the one before the x and put the intervening
4207c478bd9Sstevel@tonic-gate * filters by calling put_filter_list().
4217c478bd9Sstevel@tonic-gate */
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /* put explicit tag */
4247c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "t{", tag ) == -1 )
4257c478bd9Sstevel@tonic-gate return( NULL );
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate str++;
4287c478bd9Sstevel@tonic-gate if ( (next = find_right_paren( str )) == NULL )
4297c478bd9Sstevel@tonic-gate return( NULL );
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate *next = '\0';
4327c478bd9Sstevel@tonic-gate if ( put_filter_list( ber, str ) == -1 )
4337c478bd9Sstevel@tonic-gate return( NULL );
4347c478bd9Sstevel@tonic-gate *next++ = ')';
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /* flush explicit tagged thang */
4377c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "}" ) == -1 )
4387c478bd9Sstevel@tonic-gate return( NULL );
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate return( next );
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate int
ldap_put_filter(BerElement * ber,char * str)4447c478bd9Sstevel@tonic-gate ldap_put_filter( BerElement *ber, char *str )
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate char *next;
4477c478bd9Sstevel@tonic-gate int parens, balance, escape;
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * A Filter looks like this:
4517c478bd9Sstevel@tonic-gate * Filter ::= CHOICE {
4527c478bd9Sstevel@tonic-gate * and [0] SET OF Filter,
4537c478bd9Sstevel@tonic-gate * or [1] SET OF Filter,
4547c478bd9Sstevel@tonic-gate * not [2] Filter,
4557c478bd9Sstevel@tonic-gate * equalityMatch [3] AttributeValueAssertion,
4567c478bd9Sstevel@tonic-gate * substrings [4] SubstringFilter,
4577c478bd9Sstevel@tonic-gate * greaterOrEqual [5] AttributeValueAssertion,
4587c478bd9Sstevel@tonic-gate * lessOrEqual [6] AttributeValueAssertion,
4597c478bd9Sstevel@tonic-gate * present [7] AttributeType,,
4607c478bd9Sstevel@tonic-gate * approxMatch [8] AttributeValueAssertion
4617c478bd9Sstevel@tonic-gate * }
4627c478bd9Sstevel@tonic-gate *
4637c478bd9Sstevel@tonic-gate * SubstringFilter ::= SEQUENCE {
4647c478bd9Sstevel@tonic-gate * type AttributeType,
4657c478bd9Sstevel@tonic-gate * SEQUENCE OF CHOICE {
4667c478bd9Sstevel@tonic-gate * initial [0] IA5String,
4677c478bd9Sstevel@tonic-gate * any [1] IA5String,
4687c478bd9Sstevel@tonic-gate * final [2] IA5String
4697c478bd9Sstevel@tonic-gate * }
4707c478bd9Sstevel@tonic-gate * }
4717c478bd9Sstevel@tonic-gate * Note: tags in a choice are always explicit
4727c478bd9Sstevel@tonic-gate */
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate parens = 0;
4777c478bd9Sstevel@tonic-gate while ( *str ) {
4787c478bd9Sstevel@tonic-gate switch ( *str ) {
4797c478bd9Sstevel@tonic-gate case '(':
4807c478bd9Sstevel@tonic-gate str++;
4817c478bd9Sstevel@tonic-gate parens++;
4827c478bd9Sstevel@tonic-gate switch ( *str ) {
4837c478bd9Sstevel@tonic-gate case '&':
4847c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
4857c478bd9Sstevel@tonic-gate 0, 0, 0 );
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
4887c478bd9Sstevel@tonic-gate LDAP_FILTER_AND, 0 )) == NULL )
4897c478bd9Sstevel@tonic-gate return( -1 );
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate parens--;
4927c478bd9Sstevel@tonic-gate break;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate case '|':
4957c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
4967c478bd9Sstevel@tonic-gate 0, 0, 0 );
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
4997c478bd9Sstevel@tonic-gate LDAP_FILTER_OR, 0 )) == NULL )
5007c478bd9Sstevel@tonic-gate return( -1 );
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate parens--;
5037c478bd9Sstevel@tonic-gate break;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate case '!':
5067c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
5077c478bd9Sstevel@tonic-gate 0, 0, 0 );
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
5107c478bd9Sstevel@tonic-gate LDAP_FILTER_NOT, 1 )) == NULL )
5117c478bd9Sstevel@tonic-gate return( -1 );
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate parens--;
5147c478bd9Sstevel@tonic-gate break;
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate default:
5177c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
5187c478bd9Sstevel@tonic-gate "put_filter: simple\n", 0, 0, 0 );
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate balance = 1;
5217c478bd9Sstevel@tonic-gate escape = 0;
5227c478bd9Sstevel@tonic-gate next = str;
5237c478bd9Sstevel@tonic-gate while ( *next && balance ) {
5247c478bd9Sstevel@tonic-gate if ( escape == 0 ) {
5257c478bd9Sstevel@tonic-gate if ( *next == '(' )
5267c478bd9Sstevel@tonic-gate balance++;
5277c478bd9Sstevel@tonic-gate else if ( *next == ')' )
5287c478bd9Sstevel@tonic-gate balance--;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate if ( *next == '\\' && ! escape )
5317c478bd9Sstevel@tonic-gate escape = 1;
5327c478bd9Sstevel@tonic-gate else
5337c478bd9Sstevel@tonic-gate escape = 0;
5347c478bd9Sstevel@tonic-gate if ( balance )
5357c478bd9Sstevel@tonic-gate next++;
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate if ( balance != 0 )
5387c478bd9Sstevel@tonic-gate return( -1 );
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate *next = '\0';
5417c478bd9Sstevel@tonic-gate if ( put_simple_filter( ber, str ) == -1 ) {
5427c478bd9Sstevel@tonic-gate return( -1 );
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate *next++ = ')';
5457c478bd9Sstevel@tonic-gate str = next;
5467c478bd9Sstevel@tonic-gate parens--;
5477c478bd9Sstevel@tonic-gate break;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate break;
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate case ')':
5527c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
5537c478bd9Sstevel@tonic-gate 0 );
5547c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "]" ) == -1 )
5557c478bd9Sstevel@tonic-gate return( -1 );
5567c478bd9Sstevel@tonic-gate str++;
5577c478bd9Sstevel@tonic-gate parens--;
5587c478bd9Sstevel@tonic-gate break;
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate case ' ':
5617c478bd9Sstevel@tonic-gate str++;
5627c478bd9Sstevel@tonic-gate break;
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate default: /* assume it's a simple type=value filter */
5657c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
5667c478bd9Sstevel@tonic-gate 0 );
5677c478bd9Sstevel@tonic-gate next = strchr( str, '\0' );
5687c478bd9Sstevel@tonic-gate if ( put_simple_filter( ber, str ) == -1 ) {
5697c478bd9Sstevel@tonic-gate return( -1 );
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate str = next;
5727c478bd9Sstevel@tonic-gate break;
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate return( parens ? -1 : 0 );
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Put a list of filters like this "(filter1)(filter2)..."
5827c478bd9Sstevel@tonic-gate */
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate static int
put_filter_list(BerElement * ber,char * str)5857c478bd9Sstevel@tonic-gate put_filter_list( BerElement *ber, char *str )
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate char *next;
5887c478bd9Sstevel@tonic-gate char save;
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate while ( *str ) {
5937c478bd9Sstevel@tonic-gate while ( *str && isspace( *str ) )
5947c478bd9Sstevel@tonic-gate str++;
5957c478bd9Sstevel@tonic-gate if ( *str == '\0' )
5967c478bd9Sstevel@tonic-gate break;
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate if ( (next = find_right_paren( str + 1 )) == NULL )
5997c478bd9Sstevel@tonic-gate return( -1 );
6007c478bd9Sstevel@tonic-gate save = *++next;
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate /* now we have "(filter)" with str pointing to it */
6037c478bd9Sstevel@tonic-gate *next = '\0';
6047c478bd9Sstevel@tonic-gate if ( ldap_put_filter( ber, str ) == -1 )
6057c478bd9Sstevel@tonic-gate return( -1 );
6067c478bd9Sstevel@tonic-gate *next = save;
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate str = next;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate return( 0 );
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate * is_valid_attr - returns 1 if a is a syntactically valid left-hand side
6177c478bd9Sstevel@tonic-gate * of a filter expression, 0 otherwise. A valid string may contain only
6187c478bd9Sstevel@tonic-gate * letters, numbers, hyphens, semi-colons, colons and periods. examples:
6197c478bd9Sstevel@tonic-gate * cn
6207c478bd9Sstevel@tonic-gate * cn;lang-fr
6217c478bd9Sstevel@tonic-gate * 1.2.3.4;binary;dynamic
6227c478bd9Sstevel@tonic-gate * mail;dynamic
6237c478bd9Sstevel@tonic-gate * cn:dn:1.2.3.4
6247c478bd9Sstevel@tonic-gate *
6257c478bd9Sstevel@tonic-gate * For compatibility with older servers, we also allow underscores in
6267c478bd9Sstevel@tonic-gate * attribute types, even through they are not allowed by the LDAPv3 RFCs.
6277c478bd9Sstevel@tonic-gate */
6287c478bd9Sstevel@tonic-gate static int
is_valid_attr(char * a)6297c478bd9Sstevel@tonic-gate is_valid_attr( char *a )
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate for ( ; *a; a++ ) {
6327c478bd9Sstevel@tonic-gate if ( !isascii( *a ) ) {
6337c478bd9Sstevel@tonic-gate return( 0 );
6347c478bd9Sstevel@tonic-gate } else if ( !isalnum( *a ) ) {
6357c478bd9Sstevel@tonic-gate switch ( *a ) {
6367c478bd9Sstevel@tonic-gate case '-':
6377c478bd9Sstevel@tonic-gate case '.':
6387c478bd9Sstevel@tonic-gate case ';':
6397c478bd9Sstevel@tonic-gate case ':':
6407c478bd9Sstevel@tonic-gate case '_':
6417c478bd9Sstevel@tonic-gate break; /* valid */
6427c478bd9Sstevel@tonic-gate default:
6437c478bd9Sstevel@tonic-gate return( 0 );
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate return( 1 );
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate static char *
find_star(char * s)6527c478bd9Sstevel@tonic-gate find_star( char *s )
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate for ( ; *s; ++s ) {
6557c478bd9Sstevel@tonic-gate switch ( *s ) {
6567c478bd9Sstevel@tonic-gate case '*': return s;
6577c478bd9Sstevel@tonic-gate case '\\':
6587c478bd9Sstevel@tonic-gate ++s;
6597c478bd9Sstevel@tonic-gate if ( hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0 ) ++s;
6607c478bd9Sstevel@tonic-gate default: break;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate return NULL;
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate static int
put_simple_filter(BerElement * ber,char * str)6677c478bd9Sstevel@tonic-gate put_simple_filter( BerElement *ber, char *str )
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate char *s, *s2, *s3, filterop;
6707c478bd9Sstevel@tonic-gate char *value;
6717c478bd9Sstevel@tonic-gate ber_uint_t ftype;
6727c478bd9Sstevel@tonic-gate int rc, len;
6737c478bd9Sstevel@tonic-gate char *oid; /* for v3 extended filter */
6747c478bd9Sstevel@tonic-gate int dnattr; /* for v3 extended filter */
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate rc = -1; /* pessimistic */
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate if (( str = nsldapi_strdup( str )) == NULL ) {
6817c478bd9Sstevel@tonic-gate return( rc );
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate if ( (s = strchr( str, '=' )) == NULL ) {
6857c478bd9Sstevel@tonic-gate goto free_and_return;
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate value = s + 1;
6887c478bd9Sstevel@tonic-gate *s-- = '\0';
6897c478bd9Sstevel@tonic-gate filterop = *s;
6907c478bd9Sstevel@tonic-gate if ( filterop == '<' || filterop == '>' || filterop == '~' ||
6917c478bd9Sstevel@tonic-gate filterop == ':' ) {
6927c478bd9Sstevel@tonic-gate *s = '\0';
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate if ( ! is_valid_attr( str ) ) {
6967c478bd9Sstevel@tonic-gate goto free_and_return;
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate switch ( filterop ) {
7007c478bd9Sstevel@tonic-gate case '<':
7017c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_LE;
7027c478bd9Sstevel@tonic-gate break;
7037c478bd9Sstevel@tonic-gate case '>':
7047c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_GE;
7057c478bd9Sstevel@tonic-gate break;
7067c478bd9Sstevel@tonic-gate case '~':
7077c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_APPROX;
7087c478bd9Sstevel@tonic-gate break;
7097c478bd9Sstevel@tonic-gate case ':': /* extended filter - v3 only */
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate * extended filter looks like this:
7127c478bd9Sstevel@tonic-gate *
7137c478bd9Sstevel@tonic-gate * [type][':dn'][':'oid]':='value
7147c478bd9Sstevel@tonic-gate *
7157c478bd9Sstevel@tonic-gate * where one of type or :oid is required.
7167c478bd9Sstevel@tonic-gate *
7177c478bd9Sstevel@tonic-gate */
7187c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_EXTENDED;
7197c478bd9Sstevel@tonic-gate s2 = s3 = NULL;
7207c478bd9Sstevel@tonic-gate if ( (s2 = strrchr( str, ':' )) == NULL ) {
7217c478bd9Sstevel@tonic-gate goto free_and_return;
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate if ( strcasecmp( s2, ":dn" ) == 0 ) {
7247c478bd9Sstevel@tonic-gate oid = NULL;
7257c478bd9Sstevel@tonic-gate dnattr = 1;
7267c478bd9Sstevel@tonic-gate *s2 = '\0';
7277c478bd9Sstevel@tonic-gate } else {
7287c478bd9Sstevel@tonic-gate oid = s2 + 1;
7297c478bd9Sstevel@tonic-gate dnattr = 0;
7307c478bd9Sstevel@tonic-gate *s2 = '\0';
7317c478bd9Sstevel@tonic-gate if ( (s3 = strrchr( str, ':' )) != NULL ) {
7327c478bd9Sstevel@tonic-gate if ( strcasecmp( s3, ":dn" ) == 0 ) {
7337c478bd9Sstevel@tonic-gate dnattr = 1;
7347c478bd9Sstevel@tonic-gate } else {
7357c478bd9Sstevel@tonic-gate goto free_and_return;
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate *s3 = '\0';
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "t{", ftype )) == -1 ) {
7417c478bd9Sstevel@tonic-gate goto free_and_return;
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate if ( oid != NULL ) {
7447c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "ts", LDAP_TAG_MRA_OID,
7457c478bd9Sstevel@tonic-gate oid )) == -1 ) {
7467c478bd9Sstevel@tonic-gate goto free_and_return;
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate if ( *str != '\0' ) {
7507c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "ts",
7517c478bd9Sstevel@tonic-gate LDAP_TAG_MRA_TYPE, str )) == -1 ) {
7527c478bd9Sstevel@tonic-gate goto free_and_return;
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate if (( len = unescape_filterval( value )) < 0 ||
7567c478bd9Sstevel@tonic-gate ( rc = ber_printf( ber, "totb}", LDAP_TAG_MRA_VALUE,
7577c478bd9Sstevel@tonic-gate value, len, LDAP_TAG_MRA_DNATTRS, dnattr )) == -1 ) {
7587c478bd9Sstevel@tonic-gate goto free_and_return;
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate rc = 0;
7617c478bd9Sstevel@tonic-gate goto free_and_return;
7627c478bd9Sstevel@tonic-gate /* break; */
7637c478bd9Sstevel@tonic-gate default:
7647c478bd9Sstevel@tonic-gate if ( find_star( value ) == NULL ) {
7657c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_EQUALITY;
7667c478bd9Sstevel@tonic-gate } else if ( strcmp( value, "*" ) == 0 ) {
7677c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_PRESENT;
7687c478bd9Sstevel@tonic-gate } else {
7697c478bd9Sstevel@tonic-gate rc = put_substring_filter( ber, str, value );
7707c478bd9Sstevel@tonic-gate goto free_and_return;
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate break;
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate if ( ftype == LDAP_FILTER_PRESENT ) {
7767c478bd9Sstevel@tonic-gate rc = ber_printf( ber, "ts", ftype, str );
7777c478bd9Sstevel@tonic-gate } else if (( len = unescape_filterval( value )) >= 0 ) {
7787c478bd9Sstevel@tonic-gate rc = ber_printf( ber, "t{so}", ftype, str, value, len );
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate if ( rc != -1 ) {
7817c478bd9Sstevel@tonic-gate rc = 0;
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate free_and_return:
7857c478bd9Sstevel@tonic-gate NSLDAPI_FREE( str );
7867c478bd9Sstevel@tonic-gate return( rc );
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate * Undo in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape
7927c478bd9Sstevel@tonic-gate * sequences within the null-terminated string 'val'. The resulting value
7937c478bd9Sstevel@tonic-gate * may contain null characters.
7947c478bd9Sstevel@tonic-gate *
7957c478bd9Sstevel@tonic-gate * If 'val' contains invalid escape sequences we return -1.
7967c478bd9Sstevel@tonic-gate * Otherwise the length of the unescaped value is returned.
7977c478bd9Sstevel@tonic-gate */
7987c478bd9Sstevel@tonic-gate static int
unescape_filterval(char * val)7997c478bd9Sstevel@tonic-gate unescape_filterval( char *val )
8007c478bd9Sstevel@tonic-gate {
801*55fea89dSDan Cross int escape, firstdigit, ival;
8027c478bd9Sstevel@tonic-gate char *s, *d;
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate escape = 0;
8057c478bd9Sstevel@tonic-gate for ( s = d = val; *s; s++ ) {
8067c478bd9Sstevel@tonic-gate if ( escape ) {
807695ef821SGordon Ross /*
808695ef821SGordon Ross * need to leave escaped comma as-is, i.e.
809695ef821SGordon Ross * val="CN=Last\, First,OU=..."
810695ef821SGordon Ross */
811695ef821SGordon Ross if (*s == ',') {
812695ef821SGordon Ross *d++ = '\\';
813695ef821SGordon Ross *d++ = *s;
814695ef821SGordon Ross escape = 0;
815695ef821SGordon Ross continue;
816695ef821SGordon Ross }
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate * first try LDAPv3 escape (hexadecimal) sequence
8197c478bd9Sstevel@tonic-gate */
8207c478bd9Sstevel@tonic-gate if (( ival = hexchar2int( *s )) < 0 ) {
8217c478bd9Sstevel@tonic-gate if ( firstdigit ) {
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate * LDAPv2 (RFC1960) escape sequence
8247c478bd9Sstevel@tonic-gate */
8257c478bd9Sstevel@tonic-gate *d++ = *s;
8267c478bd9Sstevel@tonic-gate escape = 0;
8277c478bd9Sstevel@tonic-gate } else {
8287c478bd9Sstevel@tonic-gate return(-1);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate if ( firstdigit ) {
8327c478bd9Sstevel@tonic-gate *d = ( ival<<4 );
8337c478bd9Sstevel@tonic-gate firstdigit = 0;
8347c478bd9Sstevel@tonic-gate } else {
8357c478bd9Sstevel@tonic-gate *d++ |= ival;
8367c478bd9Sstevel@tonic-gate escape = 0;
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate } else if ( *s != '\\' ) {
8407c478bd9Sstevel@tonic-gate *d++ = *s;
8417c478bd9Sstevel@tonic-gate escape = 0;
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate } else {
8447c478bd9Sstevel@tonic-gate escape = 1;
8457c478bd9Sstevel@tonic-gate firstdigit = 1;
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate return( d - val );
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate /*
8547c478bd9Sstevel@tonic-gate * convert character 'c' that represents a hexadecimal digit to an integer.
8557c478bd9Sstevel@tonic-gate * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned.
8567c478bd9Sstevel@tonic-gate * otherwise the converted value is returned.
8577c478bd9Sstevel@tonic-gate */
8587c478bd9Sstevel@tonic-gate static int
hexchar2int(char c)8597c478bd9Sstevel@tonic-gate hexchar2int( char c )
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate if ( c >= '0' && c <= '9' ) {
8627c478bd9Sstevel@tonic-gate return( c - '0' );
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate if ( c >= 'A' && c <= 'F' ) {
8657c478bd9Sstevel@tonic-gate return( c - 'A' + 10 );
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate if ( c >= 'a' && c <= 'f' ) {
8687c478bd9Sstevel@tonic-gate return( c - 'a' + 10 );
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate return( -1 );
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate static int
put_substring_filter(BerElement * ber,char * type,char * val)8747c478bd9Sstevel@tonic-gate put_substring_filter( BerElement *ber, char *type, char *val )
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate char *nextstar, gotstar = 0;
8777c478bd9Sstevel@tonic-gate ber_uint_t ftype;
8787c478bd9Sstevel@tonic-gate int len;
8797c478bd9Sstevel@tonic-gate
8807c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
8817c478bd9Sstevel@tonic-gate val, 0 );
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 ) {
8847c478bd9Sstevel@tonic-gate return( -1 );
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate for ( ; val != NULL; val = nextstar ) {
8887c478bd9Sstevel@tonic-gate if ( (nextstar = find_star( val )) != NULL ) {
8897c478bd9Sstevel@tonic-gate *nextstar++ = '\0';
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate if ( gotstar == 0 ) {
8937c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_INITIAL;
8947c478bd9Sstevel@tonic-gate } else if ( nextstar == NULL ) {
8957c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_FINAL;
8967c478bd9Sstevel@tonic-gate } else {
8977c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_ANY;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate if ( *val != '\0' ) {
9007c478bd9Sstevel@tonic-gate if (( len = unescape_filterval( val )) < 0 ||
9017c478bd9Sstevel@tonic-gate ber_printf( ber, "to", ftype, val, len ) == -1 ) {
9027c478bd9Sstevel@tonic-gate return( -1 );
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate gotstar = 1;
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "}}" ) == -1 ) {
9107c478bd9Sstevel@tonic-gate return( -1 );
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate
9137c478bd9Sstevel@tonic-gate return( 0 );
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate int
9177c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search_st(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,struct timeval * timeout,LDAPMessage ** res)9187c478bd9Sstevel@tonic-gate ldap_search_st(
919*55fea89dSDan Cross LDAP *ld,
920*55fea89dSDan Cross const char *base,
921*55fea89dSDan Cross int scope,
922*55fea89dSDan Cross const char *filter,
9237c478bd9Sstevel@tonic-gate char **attrs,
924*55fea89dSDan Cross int attrsonly,
925*55fea89dSDan Cross struct timeval *timeout,
9267c478bd9Sstevel@tonic-gate LDAPMessage **res
9277c478bd9Sstevel@tonic-gate )
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
9307c478bd9Sstevel@tonic-gate NULL, NULL, timeout, -1, -1, res ));
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate
9337c478bd9Sstevel@tonic-gate int
9347c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search_s(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPMessage ** res)9357c478bd9Sstevel@tonic-gate ldap_search_s(
936*55fea89dSDan Cross LDAP *ld,
937*55fea89dSDan Cross const char *base,
938*55fea89dSDan Cross int scope,
939*55fea89dSDan Cross const char *filter,
9407c478bd9Sstevel@tonic-gate char **attrs,
941*55fea89dSDan Cross int attrsonly,
9427c478bd9Sstevel@tonic-gate LDAPMessage **res
9437c478bd9Sstevel@tonic-gate )
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
9467c478bd9Sstevel@tonic-gate NULL, NULL, NULL, -1, -1, res ));
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate int LDAP_CALL
ldap_search_ext_s(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,struct timeval * timeoutp,int sizelimit,LDAPMessage ** res)9507c478bd9Sstevel@tonic-gate ldap_search_ext_s(
951*55fea89dSDan Cross LDAP *ld,
952*55fea89dSDan Cross const char *base,
953*55fea89dSDan Cross int scope,
954*55fea89dSDan Cross const char *filter,
9557c478bd9Sstevel@tonic-gate char **attrs,
956*55fea89dSDan Cross int attrsonly,
9577c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
9587c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
9597c478bd9Sstevel@tonic-gate struct timeval *timeoutp,
9607c478bd9Sstevel@tonic-gate int sizelimit,
9617c478bd9Sstevel@tonic-gate LDAPMessage **res
9627c478bd9Sstevel@tonic-gate )
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
9657c478bd9Sstevel@tonic-gate serverctrls, clientctrls, timeoutp,
9667c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, res ));
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate
9697c478bd9Sstevel@tonic-gate
970*55fea89dSDan Cross static int
nsldapi_search_s(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,struct timeval * localtimeoutp,int timelimit,int sizelimit,LDAPMessage ** res)9717c478bd9Sstevel@tonic-gate nsldapi_search_s(
972*55fea89dSDan Cross LDAP *ld,
973*55fea89dSDan Cross const char *base,
974*55fea89dSDan Cross int scope,
975*55fea89dSDan Cross const char *filter,
9767c478bd9Sstevel@tonic-gate char **attrs,
977*55fea89dSDan Cross int attrsonly,
9787c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
9797c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
9807c478bd9Sstevel@tonic-gate struct timeval *localtimeoutp,
9817c478bd9Sstevel@tonic-gate int timelimit, /* -1 means use ld->ld_timelimit */
9827c478bd9Sstevel@tonic-gate int sizelimit, /* -1 means use ld->ld_sizelimit */
9837c478bd9Sstevel@tonic-gate LDAPMessage **res
9847c478bd9Sstevel@tonic-gate )
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate int err, msgid;
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate * It is an error to pass in a zero'd timeval.
9907c478bd9Sstevel@tonic-gate */
9917c478bd9Sstevel@tonic-gate if ( localtimeoutp != NULL && localtimeoutp->tv_sec == 0 &&
9927c478bd9Sstevel@tonic-gate localtimeoutp->tv_usec == 0 ) {
9937c478bd9Sstevel@tonic-gate if ( ld != NULL ) {
9947c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate if ( res != NULL ) {
9977c478bd9Sstevel@tonic-gate *res = NULL;
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate if (( err = nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
10037c478bd9Sstevel@tonic-gate serverctrls, clientctrls, timelimit, sizelimit, &msgid ))
10047c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
10057c478bd9Sstevel@tonic-gate if ( res != NULL ) {
10067c478bd9Sstevel@tonic-gate *res = NULL;
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate return( err );
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, localtimeoutp, res ) == -1 ) {
10127c478bd9Sstevel@tonic-gate /*
10137c478bd9Sstevel@tonic-gate * Error. ldap_result() sets *res to NULL for us.
10147c478bd9Sstevel@tonic-gate */
10157c478bd9Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate if ( LDAP_GET_LDERRNO( ld, NULL, NULL ) == LDAP_TIMEOUT ) {
10197c478bd9Sstevel@tonic-gate (void) ldap_abandon( ld, msgid );
10207c478bd9Sstevel@tonic-gate err = LDAP_TIMEOUT;
10217c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
10227c478bd9Sstevel@tonic-gate if ( res != NULL ) {
10237c478bd9Sstevel@tonic-gate *res = NULL;
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate return( err );
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate return( ldap_result2error( ld, *res, 0 ) );
10297c478bd9Sstevel@tonic-gate }
1030