1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.
3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate */
5*7c478bd9Sstevel@tonic-gate
6*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*7c478bd9Sstevel@tonic-gate
8*7c478bd9Sstevel@tonic-gate
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
11*7c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
12*7c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
13*7c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
14*7c478bd9Sstevel@tonic-gate *
15*7c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
16*7c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17*7c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
18*7c478bd9Sstevel@tonic-gate * rights and limitations under the License.
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
21*7c478bd9Sstevel@tonic-gate * March 31, 1998.
22*7c478bd9Sstevel@tonic-gate *
23*7c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
24*7c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
25*7c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
26*7c478bd9Sstevel@tonic-gate * Rights Reserved.
27*7c478bd9Sstevel@tonic-gate *
28*7c478bd9Sstevel@tonic-gate * Contributor(s):
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan.
32*7c478bd9Sstevel@tonic-gate * All rights reserved.
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate * search.c
36*7c478bd9Sstevel@tonic-gate */
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate #if 0
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate static int nsldapi_timeval2ldaplimit( struct timeval *timeoutp,
47*7c478bd9Sstevel@tonic-gate int defaultvalue );
48*7c478bd9Sstevel@tonic-gate static int nsldapi_search( LDAP *ld, const char *base, int scope,
49*7c478bd9Sstevel@tonic-gate const char *filter, char **attrs, int attrsonly,
50*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls,
51*7c478bd9Sstevel@tonic-gate int timelimit, int sizelimit, int *msgidp );
52*7c478bd9Sstevel@tonic-gate static char *find_right_paren( char *s );
53*7c478bd9Sstevel@tonic-gate static char *put_complex_filter( BerElement *ber, char *str,
54*7c478bd9Sstevel@tonic-gate ber_tag_t tag, int not );
55*7c478bd9Sstevel@tonic-gate static int unescape_filterval( char *str );
56*7c478bd9Sstevel@tonic-gate static int hexchar2int( char c );
57*7c478bd9Sstevel@tonic-gate static int is_valid_attr( char *a );
58*7c478bd9Sstevel@tonic-gate static int put_simple_filter( BerElement *ber, char *str );
59*7c478bd9Sstevel@tonic-gate static int put_substring_filter( BerElement *ber, char *type,
60*7c478bd9Sstevel@tonic-gate char *str );
61*7c478bd9Sstevel@tonic-gate static int put_filter_list( BerElement *ber, char *str );
62*7c478bd9Sstevel@tonic-gate static int nsldapi_search_s( LDAP *ld, const char *base, int scope,
63*7c478bd9Sstevel@tonic-gate const char *filter, char **attrs, int attrsonly,
64*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls,
65*7c478bd9Sstevel@tonic-gate struct timeval *localtimeoutp, int timelimit, int sizelimit,
66*7c478bd9Sstevel@tonic-gate LDAPMessage **res );
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate * ldap_search - initiate an ldap search operation. Parameters:
70*7c478bd9Sstevel@tonic-gate *
71*7c478bd9Sstevel@tonic-gate * ld LDAP descriptor
72*7c478bd9Sstevel@tonic-gate * base DN of the base object
73*7c478bd9Sstevel@tonic-gate * scope the search scope - one of LDAP_SCOPE_BASE,
74*7c478bd9Sstevel@tonic-gate * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
75*7c478bd9Sstevel@tonic-gate * filter a string containing the search filter
76*7c478bd9Sstevel@tonic-gate * (e.g., "(|(cn=bob)(sn=bob))")
77*7c478bd9Sstevel@tonic-gate * attrs list of attribute types to return for matches
78*7c478bd9Sstevel@tonic-gate * attrsonly 1 => attributes only 0 => attributes and values
79*7c478bd9Sstevel@tonic-gate *
80*7c478bd9Sstevel@tonic-gate * Example:
81*7c478bd9Sstevel@tonic-gate * char *attrs[] = { "mail", "title", 0 };
82*7c478bd9Sstevel@tonic-gate * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
83*7c478bd9Sstevel@tonic-gate * attrs, attrsonly );
84*7c478bd9Sstevel@tonic-gate */
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly)87*7c478bd9Sstevel@tonic-gate ldap_search(
88*7c478bd9Sstevel@tonic-gate LDAP *ld,
89*7c478bd9Sstevel@tonic-gate const char *base,
90*7c478bd9Sstevel@tonic-gate int scope,
91*7c478bd9Sstevel@tonic-gate const char *filter,
92*7c478bd9Sstevel@tonic-gate char **attrs,
93*7c478bd9Sstevel@tonic-gate int attrsonly
94*7c478bd9Sstevel@tonic-gate )
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate int msgid;
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate if ( ldap_search_ext( ld, base, scope, filter, attrs, attrsonly, NULL,
101*7c478bd9Sstevel@tonic-gate NULL, NULL, -1, &msgid ) == LDAP_SUCCESS ) {
102*7c478bd9Sstevel@tonic-gate return( msgid );
103*7c478bd9Sstevel@tonic-gate } else {
104*7c478bd9Sstevel@tonic-gate return( -1 ); /* error is in ld handle */
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate * LDAPv3 extended search.
111*7c478bd9Sstevel@tonic-gate * Returns an LDAP error code.
112*7c478bd9Sstevel@tonic-gate */
113*7c478bd9Sstevel@tonic-gate int
114*7c478bd9Sstevel@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)115*7c478bd9Sstevel@tonic-gate ldap_search_ext(
116*7c478bd9Sstevel@tonic-gate LDAP *ld,
117*7c478bd9Sstevel@tonic-gate const char *base,
118*7c478bd9Sstevel@tonic-gate int scope,
119*7c478bd9Sstevel@tonic-gate const char *filter,
120*7c478bd9Sstevel@tonic-gate char **attrs,
121*7c478bd9Sstevel@tonic-gate int attrsonly,
122*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
123*7c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
124*7c478bd9Sstevel@tonic-gate struct timeval *timeoutp, /* NULL means use ld->ld_timelimit */
125*7c478bd9Sstevel@tonic-gate int sizelimit,
126*7c478bd9Sstevel@tonic-gate int *msgidp
127*7c478bd9Sstevel@tonic-gate )
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate * It is an error to pass in a zero'd timeval.
131*7c478bd9Sstevel@tonic-gate */
132*7c478bd9Sstevel@tonic-gate if ( timeoutp != NULL && timeoutp->tv_sec == 0 &&
133*7c478bd9Sstevel@tonic-gate timeoutp->tv_usec == 0 ) {
134*7c478bd9Sstevel@tonic-gate if ( ld != NULL ) {
135*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate return( nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
141*7c478bd9Sstevel@tonic-gate serverctrls, clientctrls,
142*7c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, msgidp ));
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate /*
147*7c478bd9Sstevel@tonic-gate * Like ldap_search_ext() except an integer timelimit is passed instead of
148*7c478bd9Sstevel@tonic-gate * using the overloaded struct timeval *timeoutp.
149*7c478bd9Sstevel@tonic-gate */
150*7c478bd9Sstevel@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)151*7c478bd9Sstevel@tonic-gate nsldapi_search(
152*7c478bd9Sstevel@tonic-gate LDAP *ld,
153*7c478bd9Sstevel@tonic-gate const char *base,
154*7c478bd9Sstevel@tonic-gate int scope,
155*7c478bd9Sstevel@tonic-gate const char *filter,
156*7c478bd9Sstevel@tonic-gate char **attrs,
157*7c478bd9Sstevel@tonic-gate int attrsonly,
158*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
159*7c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
160*7c478bd9Sstevel@tonic-gate int timelimit, /* -1 means use ld->ld_timelimit */
161*7c478bd9Sstevel@tonic-gate int sizelimit, /* -1 means use ld->ld_sizelimit */
162*7c478bd9Sstevel@tonic-gate int *msgidp
163*7c478bd9Sstevel@tonic-gate )
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate BerElement *ber;
166*7c478bd9Sstevel@tonic-gate int rc, rc_key;
167*7c478bd9Sstevel@tonic-gate unsigned long key; /* XXXmcs: memcache */
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
172*7c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate if ( base == NULL ) {
176*7c478bd9Sstevel@tonic-gate base = "";
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate if ( filter == NULL ) {
180*7c478bd9Sstevel@tonic-gate filter = "(objectclass=*)";
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate if ( msgidp == NULL || ( scope != LDAP_SCOPE_BASE
184*7c478bd9Sstevel@tonic-gate && scope != LDAP_SCOPE_ONELEVEL && scope != LDAP_SCOPE_SUBTREE )
185*7c478bd9Sstevel@tonic-gate || ( sizelimit < -1 )) {
186*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
187*7c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
190*7c478bd9Sstevel@tonic-gate *msgidp = ++ld->ld_msgid;
191*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate * XXXmcs: should use cache function pointers to hook in memcache
195*7c478bd9Sstevel@tonic-gate */
196*7c478bd9Sstevel@tonic-gate if ( ld->ld_memcache == NULL ) {
197*7c478bd9Sstevel@tonic-gate rc_key = LDAP_NOT_SUPPORTED;
198*7c478bd9Sstevel@tonic-gate } else if (( rc_key = ldap_memcache_createkey( ld, base, scope, filter,
199*7c478bd9Sstevel@tonic-gate attrs, attrsonly, serverctrls, clientctrls, &key)) == LDAP_SUCCESS
200*7c478bd9Sstevel@tonic-gate && ldap_memcache_result( ld, *msgidp, key ) == LDAP_SUCCESS ) {
201*7c478bd9Sstevel@tonic-gate return LDAP_SUCCESS;
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate /* check the cache */
205*7c478bd9Sstevel@tonic-gate if ( ld->ld_cache_on && ld->ld_cache_search != NULL ) {
206*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
207*7c478bd9Sstevel@tonic-gate if ( (rc = (ld->ld_cache_search)( ld, *msgidp, LDAP_REQ_SEARCH,
208*7c478bd9Sstevel@tonic-gate base, scope, filter, attrs, attrsonly )) != 0 ) {
209*7c478bd9Sstevel@tonic-gate *msgidp = rc;
210*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
211*7c478bd9Sstevel@tonic-gate return( LDAP_SUCCESS );
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate /* caching off or did not find it in the cache - check the net */
217*7c478bd9Sstevel@tonic-gate if (( rc = nsldapi_build_search_req( ld, base, scope, filter, attrs,
218*7c478bd9Sstevel@tonic-gate attrsonly, serverctrls, clientctrls, timelimit, sizelimit,
219*7c478bd9Sstevel@tonic-gate *msgidp, &ber )) != LDAP_SUCCESS ) {
220*7c478bd9Sstevel@tonic-gate return( rc );
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate
223*7c478bd9Sstevel@tonic-gate /* send the message */
224*7c478bd9Sstevel@tonic-gate rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_SEARCH,
225*7c478bd9Sstevel@tonic-gate (char *) base, ber );
226*7c478bd9Sstevel@tonic-gate
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate * XXXmcs: should use cache function pointers to hook in memcache
229*7c478bd9Sstevel@tonic-gate */
230*7c478bd9Sstevel@tonic-gate if ( (rc_key == LDAP_SUCCESS) && (rc >= 0) ) {
231*7c478bd9Sstevel@tonic-gate ldap_memcache_new( ld, rc, key, base );
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate *msgidp = rc;
235*7c478bd9Sstevel@tonic-gate return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate /*
240*7c478bd9Sstevel@tonic-gate * Convert a non-NULL timeoutp to a value in seconds that is appropriate to
241*7c478bd9Sstevel@tonic-gate * send in an LDAP search request. If timeoutp is NULL, return defaultvalue.
242*7c478bd9Sstevel@tonic-gate */
243*7c478bd9Sstevel@tonic-gate static int
nsldapi_timeval2ldaplimit(struct timeval * timeoutp,int defaultvalue)244*7c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( struct timeval *timeoutp, int defaultvalue )
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate int timelimit;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate if ( NULL == timeoutp ) {
249*7c478bd9Sstevel@tonic-gate timelimit = defaultvalue;
250*7c478bd9Sstevel@tonic-gate } else if ( timeoutp->tv_sec > 0 ) {
251*7c478bd9Sstevel@tonic-gate timelimit = timeoutp->tv_sec;
252*7c478bd9Sstevel@tonic-gate } else if ( timeoutp->tv_usec > 0 ) {
253*7c478bd9Sstevel@tonic-gate timelimit = 1; /* minimum we can express in LDAP */
254*7c478bd9Sstevel@tonic-gate } else {
255*7c478bd9Sstevel@tonic-gate /*
256*7c478bd9Sstevel@tonic-gate * both tv_sec and tv_usec are less than one (zero?) so
257*7c478bd9Sstevel@tonic-gate * to maintain compatiblity with our "zero means no limit"
258*7c478bd9Sstevel@tonic-gate * convention we pass no limit to the server.
259*7c478bd9Sstevel@tonic-gate */
260*7c478bd9Sstevel@tonic-gate timelimit = 0; /* no limit */
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate return( timelimit );
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate
266*7c478bd9Sstevel@tonic-gate
267*7c478bd9Sstevel@tonic-gate /* returns an LDAP error code and also sets it in ld */
268*7c478bd9Sstevel@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)269*7c478bd9Sstevel@tonic-gate nsldapi_build_search_req(
270*7c478bd9Sstevel@tonic-gate LDAP *ld,
271*7c478bd9Sstevel@tonic-gate const char *base,
272*7c478bd9Sstevel@tonic-gate int scope,
273*7c478bd9Sstevel@tonic-gate const char *filter,
274*7c478bd9Sstevel@tonic-gate char **attrs,
275*7c478bd9Sstevel@tonic-gate int attrsonly,
276*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
277*7c478bd9Sstevel@tonic-gate LDAPControl **clientctrls, /* not used for anything yet */
278*7c478bd9Sstevel@tonic-gate int timelimit, /* if -1, ld->ld_timelimit is used */
279*7c478bd9Sstevel@tonic-gate int sizelimit, /* if -1, ld->ld_sizelimit is used */
280*7c478bd9Sstevel@tonic-gate int msgid,
281*7c478bd9Sstevel@tonic-gate BerElement **berp
282*7c478bd9Sstevel@tonic-gate )
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate BerElement *ber;
285*7c478bd9Sstevel@tonic-gate int err;
286*7c478bd9Sstevel@tonic-gate char *fdup;
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate /*
289*7c478bd9Sstevel@tonic-gate * Create the search request. It looks like this:
290*7c478bd9Sstevel@tonic-gate * SearchRequest := [APPLICATION 3] SEQUENCE {
291*7c478bd9Sstevel@tonic-gate * baseObject DistinguishedName,
292*7c478bd9Sstevel@tonic-gate * scope ENUMERATED {
293*7c478bd9Sstevel@tonic-gate * baseObject (0),
294*7c478bd9Sstevel@tonic-gate * singleLevel (1),
295*7c478bd9Sstevel@tonic-gate * wholeSubtree (2)
296*7c478bd9Sstevel@tonic-gate * },
297*7c478bd9Sstevel@tonic-gate * derefAliases ENUMERATED {
298*7c478bd9Sstevel@tonic-gate * neverDerefaliases (0),
299*7c478bd9Sstevel@tonic-gate * derefInSearching (1),
300*7c478bd9Sstevel@tonic-gate * derefFindingBaseObj (2),
301*7c478bd9Sstevel@tonic-gate * alwaysDerefAliases (3)
302*7c478bd9Sstevel@tonic-gate * },
303*7c478bd9Sstevel@tonic-gate * sizelimit INTEGER (0 .. 65535),
304*7c478bd9Sstevel@tonic-gate * timelimit INTEGER (0 .. 65535),
305*7c478bd9Sstevel@tonic-gate * attrsOnly BOOLEAN,
306*7c478bd9Sstevel@tonic-gate * filter Filter,
307*7c478bd9Sstevel@tonic-gate * attributes SEQUENCE OF AttributeType
308*7c478bd9Sstevel@tonic-gate * }
309*7c478bd9Sstevel@tonic-gate * wrapped in an ldap message.
310*7c478bd9Sstevel@tonic-gate */
311*7c478bd9Sstevel@tonic-gate
312*7c478bd9Sstevel@tonic-gate /* create a message to send */
313*7c478bd9Sstevel@tonic-gate if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
314*7c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
315*7c478bd9Sstevel@tonic-gate return( err );
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate if ( base == NULL ) {
319*7c478bd9Sstevel@tonic-gate base = "";
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate
322*7c478bd9Sstevel@tonic-gate if ( sizelimit == -1 ) {
323*7c478bd9Sstevel@tonic-gate sizelimit = ld->ld_sizelimit;
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate if ( timelimit == -1 ) {
327*7c478bd9Sstevel@tonic-gate timelimit = ld->ld_timelimit;
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate
330*7c478bd9Sstevel@tonic-gate #ifdef CLDAP
331*7c478bd9Sstevel@tonic-gate if ( ld->ld_sbp->sb_naddr > 0 ) {
332*7c478bd9Sstevel@tonic-gate err = ber_printf( ber, "{ist{seeiib", msgid,
333*7c478bd9Sstevel@tonic-gate ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
334*7c478bd9Sstevel@tonic-gate sizelimit, timelimit, attrsonly );
335*7c478bd9Sstevel@tonic-gate } else {
336*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */
337*7c478bd9Sstevel@tonic-gate err = ber_printf( ber, "{it{seeiib", msgid,
338*7c478bd9Sstevel@tonic-gate LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
339*7c478bd9Sstevel@tonic-gate sizelimit, timelimit, attrsonly );
340*7c478bd9Sstevel@tonic-gate #ifdef CLDAP
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */
343*7c478bd9Sstevel@tonic-gate
344*7c478bd9Sstevel@tonic-gate if ( err == -1 ) {
345*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
346*7c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
347*7c478bd9Sstevel@tonic-gate return( LDAP_ENCODING_ERROR );
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate
350*7c478bd9Sstevel@tonic-gate fdup = nsldapi_strdup( filter );
351*7c478bd9Sstevel@tonic-gate if (fdup == NULL) {
352*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
353*7c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
354*7c478bd9Sstevel@tonic-gate return( LDAP_NO_MEMORY );
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate err = ldap_put_filter( ber, fdup );
357*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( fdup );
358*7c478bd9Sstevel@tonic-gate
359*7c478bd9Sstevel@tonic-gate if ( err == -1 ) {
360*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_FILTER_ERROR, NULL, NULL );
361*7c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
362*7c478bd9Sstevel@tonic-gate return( LDAP_FILTER_ERROR );
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate
365*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "{v}}", attrs ) == -1 ) {
366*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
367*7c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
368*7c478bd9Sstevel@tonic-gate return( LDAP_ENCODING_ERROR );
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate
371*7c478bd9Sstevel@tonic-gate if ( (err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
372*7c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
373*7c478bd9Sstevel@tonic-gate ber_free( ber, 1 );
374*7c478bd9Sstevel@tonic-gate return( err );
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate
377*7c478bd9Sstevel@tonic-gate *berp = ber;
378*7c478bd9Sstevel@tonic-gate return( LDAP_SUCCESS );
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate
381*7c478bd9Sstevel@tonic-gate static char *
find_right_paren(char * s)382*7c478bd9Sstevel@tonic-gate find_right_paren( char *s )
383*7c478bd9Sstevel@tonic-gate {
384*7c478bd9Sstevel@tonic-gate int balance, escape;
385*7c478bd9Sstevel@tonic-gate
386*7c478bd9Sstevel@tonic-gate balance = 1;
387*7c478bd9Sstevel@tonic-gate escape = 0;
388*7c478bd9Sstevel@tonic-gate while ( *s && balance ) {
389*7c478bd9Sstevel@tonic-gate if ( escape == 0 ) {
390*7c478bd9Sstevel@tonic-gate if ( *s == '(' )
391*7c478bd9Sstevel@tonic-gate balance++;
392*7c478bd9Sstevel@tonic-gate else if ( *s == ')' )
393*7c478bd9Sstevel@tonic-gate balance--;
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate if ( *s == '\\' && ! escape )
396*7c478bd9Sstevel@tonic-gate escape = 1;
397*7c478bd9Sstevel@tonic-gate else
398*7c478bd9Sstevel@tonic-gate escape = 0;
399*7c478bd9Sstevel@tonic-gate if ( balance )
400*7c478bd9Sstevel@tonic-gate s++;
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate
403*7c478bd9Sstevel@tonic-gate return( *s ? s : NULL );
404*7c478bd9Sstevel@tonic-gate }
405*7c478bd9Sstevel@tonic-gate
406*7c478bd9Sstevel@tonic-gate static char *
put_complex_filter(BerElement * ber,char * str,ber_tag_t tag,int not)407*7c478bd9Sstevel@tonic-gate put_complex_filter(
408*7c478bd9Sstevel@tonic-gate BerElement *ber,
409*7c478bd9Sstevel@tonic-gate char *str,
410*7c478bd9Sstevel@tonic-gate ber_tag_t tag,
411*7c478bd9Sstevel@tonic-gate int not
412*7c478bd9Sstevel@tonic-gate )
413*7c478bd9Sstevel@tonic-gate {
414*7c478bd9Sstevel@tonic-gate char *next;
415*7c478bd9Sstevel@tonic-gate
416*7c478bd9Sstevel@tonic-gate /*
417*7c478bd9Sstevel@tonic-gate * We have (x(filter)...) with str sitting on
418*7c478bd9Sstevel@tonic-gate * the x. We have to find the paren matching
419*7c478bd9Sstevel@tonic-gate * the one before the x and put the intervening
420*7c478bd9Sstevel@tonic-gate * filters by calling put_filter_list().
421*7c478bd9Sstevel@tonic-gate */
422*7c478bd9Sstevel@tonic-gate
423*7c478bd9Sstevel@tonic-gate /* put explicit tag */
424*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "t{", tag ) == -1 )
425*7c478bd9Sstevel@tonic-gate return( NULL );
426*7c478bd9Sstevel@tonic-gate
427*7c478bd9Sstevel@tonic-gate str++;
428*7c478bd9Sstevel@tonic-gate if ( (next = find_right_paren( str )) == NULL )
429*7c478bd9Sstevel@tonic-gate return( NULL );
430*7c478bd9Sstevel@tonic-gate
431*7c478bd9Sstevel@tonic-gate *next = '\0';
432*7c478bd9Sstevel@tonic-gate if ( put_filter_list( ber, str ) == -1 )
433*7c478bd9Sstevel@tonic-gate return( NULL );
434*7c478bd9Sstevel@tonic-gate *next++ = ')';
435*7c478bd9Sstevel@tonic-gate
436*7c478bd9Sstevel@tonic-gate /* flush explicit tagged thang */
437*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "}" ) == -1 )
438*7c478bd9Sstevel@tonic-gate return( NULL );
439*7c478bd9Sstevel@tonic-gate
440*7c478bd9Sstevel@tonic-gate return( next );
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate
443*7c478bd9Sstevel@tonic-gate int
ldap_put_filter(BerElement * ber,char * str)444*7c478bd9Sstevel@tonic-gate ldap_put_filter( BerElement *ber, char *str )
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate char *next;
447*7c478bd9Sstevel@tonic-gate int parens, balance, escape;
448*7c478bd9Sstevel@tonic-gate
449*7c478bd9Sstevel@tonic-gate /*
450*7c478bd9Sstevel@tonic-gate * A Filter looks like this:
451*7c478bd9Sstevel@tonic-gate * Filter ::= CHOICE {
452*7c478bd9Sstevel@tonic-gate * and [0] SET OF Filter,
453*7c478bd9Sstevel@tonic-gate * or [1] SET OF Filter,
454*7c478bd9Sstevel@tonic-gate * not [2] Filter,
455*7c478bd9Sstevel@tonic-gate * equalityMatch [3] AttributeValueAssertion,
456*7c478bd9Sstevel@tonic-gate * substrings [4] SubstringFilter,
457*7c478bd9Sstevel@tonic-gate * greaterOrEqual [5] AttributeValueAssertion,
458*7c478bd9Sstevel@tonic-gate * lessOrEqual [6] AttributeValueAssertion,
459*7c478bd9Sstevel@tonic-gate * present [7] AttributeType,,
460*7c478bd9Sstevel@tonic-gate * approxMatch [8] AttributeValueAssertion
461*7c478bd9Sstevel@tonic-gate * }
462*7c478bd9Sstevel@tonic-gate *
463*7c478bd9Sstevel@tonic-gate * SubstringFilter ::= SEQUENCE {
464*7c478bd9Sstevel@tonic-gate * type AttributeType,
465*7c478bd9Sstevel@tonic-gate * SEQUENCE OF CHOICE {
466*7c478bd9Sstevel@tonic-gate * initial [0] IA5String,
467*7c478bd9Sstevel@tonic-gate * any [1] IA5String,
468*7c478bd9Sstevel@tonic-gate * final [2] IA5String
469*7c478bd9Sstevel@tonic-gate * }
470*7c478bd9Sstevel@tonic-gate * }
471*7c478bd9Sstevel@tonic-gate * Note: tags in a choice are always explicit
472*7c478bd9Sstevel@tonic-gate */
473*7c478bd9Sstevel@tonic-gate
474*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
475*7c478bd9Sstevel@tonic-gate
476*7c478bd9Sstevel@tonic-gate parens = 0;
477*7c478bd9Sstevel@tonic-gate while ( *str ) {
478*7c478bd9Sstevel@tonic-gate switch ( *str ) {
479*7c478bd9Sstevel@tonic-gate case '(':
480*7c478bd9Sstevel@tonic-gate str++;
481*7c478bd9Sstevel@tonic-gate parens++;
482*7c478bd9Sstevel@tonic-gate switch ( *str ) {
483*7c478bd9Sstevel@tonic-gate case '&':
484*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
485*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
486*7c478bd9Sstevel@tonic-gate
487*7c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
488*7c478bd9Sstevel@tonic-gate LDAP_FILTER_AND, 0 )) == NULL )
489*7c478bd9Sstevel@tonic-gate return( -1 );
490*7c478bd9Sstevel@tonic-gate
491*7c478bd9Sstevel@tonic-gate parens--;
492*7c478bd9Sstevel@tonic-gate break;
493*7c478bd9Sstevel@tonic-gate
494*7c478bd9Sstevel@tonic-gate case '|':
495*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
496*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
497*7c478bd9Sstevel@tonic-gate
498*7c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
499*7c478bd9Sstevel@tonic-gate LDAP_FILTER_OR, 0 )) == NULL )
500*7c478bd9Sstevel@tonic-gate return( -1 );
501*7c478bd9Sstevel@tonic-gate
502*7c478bd9Sstevel@tonic-gate parens--;
503*7c478bd9Sstevel@tonic-gate break;
504*7c478bd9Sstevel@tonic-gate
505*7c478bd9Sstevel@tonic-gate case '!':
506*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
507*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
508*7c478bd9Sstevel@tonic-gate
509*7c478bd9Sstevel@tonic-gate if ( (str = put_complex_filter( ber, str,
510*7c478bd9Sstevel@tonic-gate LDAP_FILTER_NOT, 1 )) == NULL )
511*7c478bd9Sstevel@tonic-gate return( -1 );
512*7c478bd9Sstevel@tonic-gate
513*7c478bd9Sstevel@tonic-gate parens--;
514*7c478bd9Sstevel@tonic-gate break;
515*7c478bd9Sstevel@tonic-gate
516*7c478bd9Sstevel@tonic-gate default:
517*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
518*7c478bd9Sstevel@tonic-gate "put_filter: simple\n", 0, 0, 0 );
519*7c478bd9Sstevel@tonic-gate
520*7c478bd9Sstevel@tonic-gate balance = 1;
521*7c478bd9Sstevel@tonic-gate escape = 0;
522*7c478bd9Sstevel@tonic-gate next = str;
523*7c478bd9Sstevel@tonic-gate while ( *next && balance ) {
524*7c478bd9Sstevel@tonic-gate if ( escape == 0 ) {
525*7c478bd9Sstevel@tonic-gate if ( *next == '(' )
526*7c478bd9Sstevel@tonic-gate balance++;
527*7c478bd9Sstevel@tonic-gate else if ( *next == ')' )
528*7c478bd9Sstevel@tonic-gate balance--;
529*7c478bd9Sstevel@tonic-gate }
530*7c478bd9Sstevel@tonic-gate if ( *next == '\\' && ! escape )
531*7c478bd9Sstevel@tonic-gate escape = 1;
532*7c478bd9Sstevel@tonic-gate else
533*7c478bd9Sstevel@tonic-gate escape = 0;
534*7c478bd9Sstevel@tonic-gate if ( balance )
535*7c478bd9Sstevel@tonic-gate next++;
536*7c478bd9Sstevel@tonic-gate }
537*7c478bd9Sstevel@tonic-gate if ( balance != 0 )
538*7c478bd9Sstevel@tonic-gate return( -1 );
539*7c478bd9Sstevel@tonic-gate
540*7c478bd9Sstevel@tonic-gate *next = '\0';
541*7c478bd9Sstevel@tonic-gate if ( put_simple_filter( ber, str ) == -1 ) {
542*7c478bd9Sstevel@tonic-gate return( -1 );
543*7c478bd9Sstevel@tonic-gate }
544*7c478bd9Sstevel@tonic-gate *next++ = ')';
545*7c478bd9Sstevel@tonic-gate str = next;
546*7c478bd9Sstevel@tonic-gate parens--;
547*7c478bd9Sstevel@tonic-gate break;
548*7c478bd9Sstevel@tonic-gate }
549*7c478bd9Sstevel@tonic-gate break;
550*7c478bd9Sstevel@tonic-gate
551*7c478bd9Sstevel@tonic-gate case ')':
552*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
553*7c478bd9Sstevel@tonic-gate 0 );
554*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "]" ) == -1 )
555*7c478bd9Sstevel@tonic-gate return( -1 );
556*7c478bd9Sstevel@tonic-gate str++;
557*7c478bd9Sstevel@tonic-gate parens--;
558*7c478bd9Sstevel@tonic-gate break;
559*7c478bd9Sstevel@tonic-gate
560*7c478bd9Sstevel@tonic-gate case ' ':
561*7c478bd9Sstevel@tonic-gate str++;
562*7c478bd9Sstevel@tonic-gate break;
563*7c478bd9Sstevel@tonic-gate
564*7c478bd9Sstevel@tonic-gate default: /* assume it's a simple type=value filter */
565*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
566*7c478bd9Sstevel@tonic-gate 0 );
567*7c478bd9Sstevel@tonic-gate next = strchr( str, '\0' );
568*7c478bd9Sstevel@tonic-gate if ( put_simple_filter( ber, str ) == -1 ) {
569*7c478bd9Sstevel@tonic-gate return( -1 );
570*7c478bd9Sstevel@tonic-gate }
571*7c478bd9Sstevel@tonic-gate str = next;
572*7c478bd9Sstevel@tonic-gate break;
573*7c478bd9Sstevel@tonic-gate }
574*7c478bd9Sstevel@tonic-gate }
575*7c478bd9Sstevel@tonic-gate
576*7c478bd9Sstevel@tonic-gate return( parens ? -1 : 0 );
577*7c478bd9Sstevel@tonic-gate }
578*7c478bd9Sstevel@tonic-gate
579*7c478bd9Sstevel@tonic-gate
580*7c478bd9Sstevel@tonic-gate /*
581*7c478bd9Sstevel@tonic-gate * Put a list of filters like this "(filter1)(filter2)..."
582*7c478bd9Sstevel@tonic-gate */
583*7c478bd9Sstevel@tonic-gate
584*7c478bd9Sstevel@tonic-gate static int
put_filter_list(BerElement * ber,char * str)585*7c478bd9Sstevel@tonic-gate put_filter_list( BerElement *ber, char *str )
586*7c478bd9Sstevel@tonic-gate {
587*7c478bd9Sstevel@tonic-gate char *next;
588*7c478bd9Sstevel@tonic-gate char save;
589*7c478bd9Sstevel@tonic-gate
590*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
591*7c478bd9Sstevel@tonic-gate
592*7c478bd9Sstevel@tonic-gate while ( *str ) {
593*7c478bd9Sstevel@tonic-gate while ( *str && isspace( *str ) )
594*7c478bd9Sstevel@tonic-gate str++;
595*7c478bd9Sstevel@tonic-gate if ( *str == '\0' )
596*7c478bd9Sstevel@tonic-gate break;
597*7c478bd9Sstevel@tonic-gate
598*7c478bd9Sstevel@tonic-gate if ( (next = find_right_paren( str + 1 )) == NULL )
599*7c478bd9Sstevel@tonic-gate return( -1 );
600*7c478bd9Sstevel@tonic-gate save = *++next;
601*7c478bd9Sstevel@tonic-gate
602*7c478bd9Sstevel@tonic-gate /* now we have "(filter)" with str pointing to it */
603*7c478bd9Sstevel@tonic-gate *next = '\0';
604*7c478bd9Sstevel@tonic-gate if ( ldap_put_filter( ber, str ) == -1 )
605*7c478bd9Sstevel@tonic-gate return( -1 );
606*7c478bd9Sstevel@tonic-gate *next = save;
607*7c478bd9Sstevel@tonic-gate
608*7c478bd9Sstevel@tonic-gate str = next;
609*7c478bd9Sstevel@tonic-gate }
610*7c478bd9Sstevel@tonic-gate
611*7c478bd9Sstevel@tonic-gate return( 0 );
612*7c478bd9Sstevel@tonic-gate }
613*7c478bd9Sstevel@tonic-gate
614*7c478bd9Sstevel@tonic-gate
615*7c478bd9Sstevel@tonic-gate /*
616*7c478bd9Sstevel@tonic-gate * is_valid_attr - returns 1 if a is a syntactically valid left-hand side
617*7c478bd9Sstevel@tonic-gate * of a filter expression, 0 otherwise. A valid string may contain only
618*7c478bd9Sstevel@tonic-gate * letters, numbers, hyphens, semi-colons, colons and periods. examples:
619*7c478bd9Sstevel@tonic-gate * cn
620*7c478bd9Sstevel@tonic-gate * cn;lang-fr
621*7c478bd9Sstevel@tonic-gate * 1.2.3.4;binary;dynamic
622*7c478bd9Sstevel@tonic-gate * mail;dynamic
623*7c478bd9Sstevel@tonic-gate * cn:dn:1.2.3.4
624*7c478bd9Sstevel@tonic-gate *
625*7c478bd9Sstevel@tonic-gate * For compatibility with older servers, we also allow underscores in
626*7c478bd9Sstevel@tonic-gate * attribute types, even through they are not allowed by the LDAPv3 RFCs.
627*7c478bd9Sstevel@tonic-gate */
628*7c478bd9Sstevel@tonic-gate static int
is_valid_attr(char * a)629*7c478bd9Sstevel@tonic-gate is_valid_attr( char *a )
630*7c478bd9Sstevel@tonic-gate {
631*7c478bd9Sstevel@tonic-gate for ( ; *a; a++ ) {
632*7c478bd9Sstevel@tonic-gate if ( !isascii( *a ) ) {
633*7c478bd9Sstevel@tonic-gate return( 0 );
634*7c478bd9Sstevel@tonic-gate } else if ( !isalnum( *a ) ) {
635*7c478bd9Sstevel@tonic-gate switch ( *a ) {
636*7c478bd9Sstevel@tonic-gate case '-':
637*7c478bd9Sstevel@tonic-gate case '.':
638*7c478bd9Sstevel@tonic-gate case ';':
639*7c478bd9Sstevel@tonic-gate case ':':
640*7c478bd9Sstevel@tonic-gate case '_':
641*7c478bd9Sstevel@tonic-gate break; /* valid */
642*7c478bd9Sstevel@tonic-gate default:
643*7c478bd9Sstevel@tonic-gate return( 0 );
644*7c478bd9Sstevel@tonic-gate }
645*7c478bd9Sstevel@tonic-gate }
646*7c478bd9Sstevel@tonic-gate }
647*7c478bd9Sstevel@tonic-gate
648*7c478bd9Sstevel@tonic-gate return( 1 );
649*7c478bd9Sstevel@tonic-gate }
650*7c478bd9Sstevel@tonic-gate
651*7c478bd9Sstevel@tonic-gate static char *
find_star(char * s)652*7c478bd9Sstevel@tonic-gate find_star( char *s )
653*7c478bd9Sstevel@tonic-gate {
654*7c478bd9Sstevel@tonic-gate for ( ; *s; ++s ) {
655*7c478bd9Sstevel@tonic-gate switch ( *s ) {
656*7c478bd9Sstevel@tonic-gate case '*': return s;
657*7c478bd9Sstevel@tonic-gate case '\\':
658*7c478bd9Sstevel@tonic-gate ++s;
659*7c478bd9Sstevel@tonic-gate if ( hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0 ) ++s;
660*7c478bd9Sstevel@tonic-gate default: break;
661*7c478bd9Sstevel@tonic-gate }
662*7c478bd9Sstevel@tonic-gate }
663*7c478bd9Sstevel@tonic-gate return NULL;
664*7c478bd9Sstevel@tonic-gate }
665*7c478bd9Sstevel@tonic-gate
666*7c478bd9Sstevel@tonic-gate static int
put_simple_filter(BerElement * ber,char * str)667*7c478bd9Sstevel@tonic-gate put_simple_filter( BerElement *ber, char *str )
668*7c478bd9Sstevel@tonic-gate {
669*7c478bd9Sstevel@tonic-gate char *s, *s2, *s3, filterop;
670*7c478bd9Sstevel@tonic-gate char *value;
671*7c478bd9Sstevel@tonic-gate ber_uint_t ftype;
672*7c478bd9Sstevel@tonic-gate int rc, len;
673*7c478bd9Sstevel@tonic-gate char *oid; /* for v3 extended filter */
674*7c478bd9Sstevel@tonic-gate int dnattr; /* for v3 extended filter */
675*7c478bd9Sstevel@tonic-gate
676*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
677*7c478bd9Sstevel@tonic-gate
678*7c478bd9Sstevel@tonic-gate rc = -1; /* pessimistic */
679*7c478bd9Sstevel@tonic-gate
680*7c478bd9Sstevel@tonic-gate if (( str = nsldapi_strdup( str )) == NULL ) {
681*7c478bd9Sstevel@tonic-gate return( rc );
682*7c478bd9Sstevel@tonic-gate }
683*7c478bd9Sstevel@tonic-gate
684*7c478bd9Sstevel@tonic-gate if ( (s = strchr( str, '=' )) == NULL ) {
685*7c478bd9Sstevel@tonic-gate goto free_and_return;
686*7c478bd9Sstevel@tonic-gate }
687*7c478bd9Sstevel@tonic-gate value = s + 1;
688*7c478bd9Sstevel@tonic-gate *s-- = '\0';
689*7c478bd9Sstevel@tonic-gate filterop = *s;
690*7c478bd9Sstevel@tonic-gate if ( filterop == '<' || filterop == '>' || filterop == '~' ||
691*7c478bd9Sstevel@tonic-gate filterop == ':' ) {
692*7c478bd9Sstevel@tonic-gate *s = '\0';
693*7c478bd9Sstevel@tonic-gate }
694*7c478bd9Sstevel@tonic-gate
695*7c478bd9Sstevel@tonic-gate if ( ! is_valid_attr( str ) ) {
696*7c478bd9Sstevel@tonic-gate goto free_and_return;
697*7c478bd9Sstevel@tonic-gate }
698*7c478bd9Sstevel@tonic-gate
699*7c478bd9Sstevel@tonic-gate switch ( filterop ) {
700*7c478bd9Sstevel@tonic-gate case '<':
701*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_LE;
702*7c478bd9Sstevel@tonic-gate break;
703*7c478bd9Sstevel@tonic-gate case '>':
704*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_GE;
705*7c478bd9Sstevel@tonic-gate break;
706*7c478bd9Sstevel@tonic-gate case '~':
707*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_APPROX;
708*7c478bd9Sstevel@tonic-gate break;
709*7c478bd9Sstevel@tonic-gate case ':': /* extended filter - v3 only */
710*7c478bd9Sstevel@tonic-gate /*
711*7c478bd9Sstevel@tonic-gate * extended filter looks like this:
712*7c478bd9Sstevel@tonic-gate *
713*7c478bd9Sstevel@tonic-gate * [type][':dn'][':'oid]':='value
714*7c478bd9Sstevel@tonic-gate *
715*7c478bd9Sstevel@tonic-gate * where one of type or :oid is required.
716*7c478bd9Sstevel@tonic-gate *
717*7c478bd9Sstevel@tonic-gate */
718*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_EXTENDED;
719*7c478bd9Sstevel@tonic-gate s2 = s3 = NULL;
720*7c478bd9Sstevel@tonic-gate if ( (s2 = strrchr( str, ':' )) == NULL ) {
721*7c478bd9Sstevel@tonic-gate goto free_and_return;
722*7c478bd9Sstevel@tonic-gate }
723*7c478bd9Sstevel@tonic-gate if ( strcasecmp( s2, ":dn" ) == 0 ) {
724*7c478bd9Sstevel@tonic-gate oid = NULL;
725*7c478bd9Sstevel@tonic-gate dnattr = 1;
726*7c478bd9Sstevel@tonic-gate *s2 = '\0';
727*7c478bd9Sstevel@tonic-gate } else {
728*7c478bd9Sstevel@tonic-gate oid = s2 + 1;
729*7c478bd9Sstevel@tonic-gate dnattr = 0;
730*7c478bd9Sstevel@tonic-gate *s2 = '\0';
731*7c478bd9Sstevel@tonic-gate if ( (s3 = strrchr( str, ':' )) != NULL ) {
732*7c478bd9Sstevel@tonic-gate if ( strcasecmp( s3, ":dn" ) == 0 ) {
733*7c478bd9Sstevel@tonic-gate dnattr = 1;
734*7c478bd9Sstevel@tonic-gate } else {
735*7c478bd9Sstevel@tonic-gate goto free_and_return;
736*7c478bd9Sstevel@tonic-gate }
737*7c478bd9Sstevel@tonic-gate *s3 = '\0';
738*7c478bd9Sstevel@tonic-gate }
739*7c478bd9Sstevel@tonic-gate }
740*7c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "t{", ftype )) == -1 ) {
741*7c478bd9Sstevel@tonic-gate goto free_and_return;
742*7c478bd9Sstevel@tonic-gate }
743*7c478bd9Sstevel@tonic-gate if ( oid != NULL ) {
744*7c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "ts", LDAP_TAG_MRA_OID,
745*7c478bd9Sstevel@tonic-gate oid )) == -1 ) {
746*7c478bd9Sstevel@tonic-gate goto free_and_return;
747*7c478bd9Sstevel@tonic-gate }
748*7c478bd9Sstevel@tonic-gate }
749*7c478bd9Sstevel@tonic-gate if ( *str != '\0' ) {
750*7c478bd9Sstevel@tonic-gate if ( (rc = ber_printf( ber, "ts",
751*7c478bd9Sstevel@tonic-gate LDAP_TAG_MRA_TYPE, str )) == -1 ) {
752*7c478bd9Sstevel@tonic-gate goto free_and_return;
753*7c478bd9Sstevel@tonic-gate }
754*7c478bd9Sstevel@tonic-gate }
755*7c478bd9Sstevel@tonic-gate if (( len = unescape_filterval( value )) < 0 ||
756*7c478bd9Sstevel@tonic-gate ( rc = ber_printf( ber, "totb}", LDAP_TAG_MRA_VALUE,
757*7c478bd9Sstevel@tonic-gate value, len, LDAP_TAG_MRA_DNATTRS, dnattr )) == -1 ) {
758*7c478bd9Sstevel@tonic-gate goto free_and_return;
759*7c478bd9Sstevel@tonic-gate }
760*7c478bd9Sstevel@tonic-gate rc = 0;
761*7c478bd9Sstevel@tonic-gate goto free_and_return;
762*7c478bd9Sstevel@tonic-gate /* break; */
763*7c478bd9Sstevel@tonic-gate default:
764*7c478bd9Sstevel@tonic-gate if ( find_star( value ) == NULL ) {
765*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_EQUALITY;
766*7c478bd9Sstevel@tonic-gate } else if ( strcmp( value, "*" ) == 0 ) {
767*7c478bd9Sstevel@tonic-gate ftype = LDAP_FILTER_PRESENT;
768*7c478bd9Sstevel@tonic-gate } else {
769*7c478bd9Sstevel@tonic-gate rc = put_substring_filter( ber, str, value );
770*7c478bd9Sstevel@tonic-gate goto free_and_return;
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate break;
773*7c478bd9Sstevel@tonic-gate }
774*7c478bd9Sstevel@tonic-gate
775*7c478bd9Sstevel@tonic-gate if ( ftype == LDAP_FILTER_PRESENT ) {
776*7c478bd9Sstevel@tonic-gate rc = ber_printf( ber, "ts", ftype, str );
777*7c478bd9Sstevel@tonic-gate } else if (( len = unescape_filterval( value )) >= 0 ) {
778*7c478bd9Sstevel@tonic-gate rc = ber_printf( ber, "t{so}", ftype, str, value, len );
779*7c478bd9Sstevel@tonic-gate }
780*7c478bd9Sstevel@tonic-gate if ( rc != -1 ) {
781*7c478bd9Sstevel@tonic-gate rc = 0;
782*7c478bd9Sstevel@tonic-gate }
783*7c478bd9Sstevel@tonic-gate
784*7c478bd9Sstevel@tonic-gate free_and_return:
785*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( str );
786*7c478bd9Sstevel@tonic-gate return( rc );
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate
789*7c478bd9Sstevel@tonic-gate
790*7c478bd9Sstevel@tonic-gate /*
791*7c478bd9Sstevel@tonic-gate * Undo in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape
792*7c478bd9Sstevel@tonic-gate * sequences within the null-terminated string 'val'. The resulting value
793*7c478bd9Sstevel@tonic-gate * may contain null characters.
794*7c478bd9Sstevel@tonic-gate *
795*7c478bd9Sstevel@tonic-gate * If 'val' contains invalid escape sequences we return -1.
796*7c478bd9Sstevel@tonic-gate * Otherwise the length of the unescaped value is returned.
797*7c478bd9Sstevel@tonic-gate */
798*7c478bd9Sstevel@tonic-gate static int
unescape_filterval(char * val)799*7c478bd9Sstevel@tonic-gate unescape_filterval( char *val )
800*7c478bd9Sstevel@tonic-gate {
801*7c478bd9Sstevel@tonic-gate int escape, firstdigit, ival;
802*7c478bd9Sstevel@tonic-gate char *s, *d;
803*7c478bd9Sstevel@tonic-gate
804*7c478bd9Sstevel@tonic-gate escape = 0;
805*7c478bd9Sstevel@tonic-gate for ( s = d = val; *s; s++ ) {
806*7c478bd9Sstevel@tonic-gate if ( escape ) {
807*7c478bd9Sstevel@tonic-gate /*
808*7c478bd9Sstevel@tonic-gate * first try LDAPv3 escape (hexadecimal) sequence
809*7c478bd9Sstevel@tonic-gate */
810*7c478bd9Sstevel@tonic-gate if (( ival = hexchar2int( *s )) < 0 ) {
811*7c478bd9Sstevel@tonic-gate if ( firstdigit ) {
812*7c478bd9Sstevel@tonic-gate /*
813*7c478bd9Sstevel@tonic-gate * LDAPv2 (RFC1960) escape sequence
814*7c478bd9Sstevel@tonic-gate */
815*7c478bd9Sstevel@tonic-gate *d++ = *s;
816*7c478bd9Sstevel@tonic-gate escape = 0;
817*7c478bd9Sstevel@tonic-gate } else {
818*7c478bd9Sstevel@tonic-gate return(-1);
819*7c478bd9Sstevel@tonic-gate }
820*7c478bd9Sstevel@tonic-gate }
821*7c478bd9Sstevel@tonic-gate if ( firstdigit ) {
822*7c478bd9Sstevel@tonic-gate *d = ( ival<<4 );
823*7c478bd9Sstevel@tonic-gate firstdigit = 0;
824*7c478bd9Sstevel@tonic-gate } else {
825*7c478bd9Sstevel@tonic-gate *d++ |= ival;
826*7c478bd9Sstevel@tonic-gate escape = 0;
827*7c478bd9Sstevel@tonic-gate }
828*7c478bd9Sstevel@tonic-gate
829*7c478bd9Sstevel@tonic-gate } else if ( *s != '\\' ) {
830*7c478bd9Sstevel@tonic-gate *d++ = *s;
831*7c478bd9Sstevel@tonic-gate escape = 0;
832*7c478bd9Sstevel@tonic-gate
833*7c478bd9Sstevel@tonic-gate } else {
834*7c478bd9Sstevel@tonic-gate escape = 1;
835*7c478bd9Sstevel@tonic-gate firstdigit = 1;
836*7c478bd9Sstevel@tonic-gate }
837*7c478bd9Sstevel@tonic-gate }
838*7c478bd9Sstevel@tonic-gate
839*7c478bd9Sstevel@tonic-gate return( d - val );
840*7c478bd9Sstevel@tonic-gate }
841*7c478bd9Sstevel@tonic-gate
842*7c478bd9Sstevel@tonic-gate
843*7c478bd9Sstevel@tonic-gate /*
844*7c478bd9Sstevel@tonic-gate * convert character 'c' that represents a hexadecimal digit to an integer.
845*7c478bd9Sstevel@tonic-gate * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned.
846*7c478bd9Sstevel@tonic-gate * otherwise the converted value is returned.
847*7c478bd9Sstevel@tonic-gate */
848*7c478bd9Sstevel@tonic-gate static int
hexchar2int(char c)849*7c478bd9Sstevel@tonic-gate hexchar2int( char c )
850*7c478bd9Sstevel@tonic-gate {
851*7c478bd9Sstevel@tonic-gate if ( c >= '0' && c <= '9' ) {
852*7c478bd9Sstevel@tonic-gate return( c - '0' );
853*7c478bd9Sstevel@tonic-gate }
854*7c478bd9Sstevel@tonic-gate if ( c >= 'A' && c <= 'F' ) {
855*7c478bd9Sstevel@tonic-gate return( c - 'A' + 10 );
856*7c478bd9Sstevel@tonic-gate }
857*7c478bd9Sstevel@tonic-gate if ( c >= 'a' && c <= 'f' ) {
858*7c478bd9Sstevel@tonic-gate return( c - 'a' + 10 );
859*7c478bd9Sstevel@tonic-gate }
860*7c478bd9Sstevel@tonic-gate return( -1 );
861*7c478bd9Sstevel@tonic-gate }
862*7c478bd9Sstevel@tonic-gate
863*7c478bd9Sstevel@tonic-gate static int
put_substring_filter(BerElement * ber,char * type,char * val)864*7c478bd9Sstevel@tonic-gate put_substring_filter( BerElement *ber, char *type, char *val )
865*7c478bd9Sstevel@tonic-gate {
866*7c478bd9Sstevel@tonic-gate char *nextstar, gotstar = 0;
867*7c478bd9Sstevel@tonic-gate ber_uint_t ftype;
868*7c478bd9Sstevel@tonic-gate int len;
869*7c478bd9Sstevel@tonic-gate
870*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
871*7c478bd9Sstevel@tonic-gate val, 0 );
872*7c478bd9Sstevel@tonic-gate
873*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 ) {
874*7c478bd9Sstevel@tonic-gate return( -1 );
875*7c478bd9Sstevel@tonic-gate }
876*7c478bd9Sstevel@tonic-gate
877*7c478bd9Sstevel@tonic-gate for ( ; val != NULL; val = nextstar ) {
878*7c478bd9Sstevel@tonic-gate if ( (nextstar = find_star( val )) != NULL ) {
879*7c478bd9Sstevel@tonic-gate *nextstar++ = '\0';
880*7c478bd9Sstevel@tonic-gate }
881*7c478bd9Sstevel@tonic-gate
882*7c478bd9Sstevel@tonic-gate if ( gotstar == 0 ) {
883*7c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_INITIAL;
884*7c478bd9Sstevel@tonic-gate } else if ( nextstar == NULL ) {
885*7c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_FINAL;
886*7c478bd9Sstevel@tonic-gate } else {
887*7c478bd9Sstevel@tonic-gate ftype = LDAP_SUBSTRING_ANY;
888*7c478bd9Sstevel@tonic-gate }
889*7c478bd9Sstevel@tonic-gate if ( *val != '\0' ) {
890*7c478bd9Sstevel@tonic-gate if (( len = unescape_filterval( val )) < 0 ||
891*7c478bd9Sstevel@tonic-gate ber_printf( ber, "to", ftype, val, len ) == -1 ) {
892*7c478bd9Sstevel@tonic-gate return( -1 );
893*7c478bd9Sstevel@tonic-gate }
894*7c478bd9Sstevel@tonic-gate }
895*7c478bd9Sstevel@tonic-gate
896*7c478bd9Sstevel@tonic-gate gotstar = 1;
897*7c478bd9Sstevel@tonic-gate }
898*7c478bd9Sstevel@tonic-gate
899*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "}}" ) == -1 ) {
900*7c478bd9Sstevel@tonic-gate return( -1 );
901*7c478bd9Sstevel@tonic-gate }
902*7c478bd9Sstevel@tonic-gate
903*7c478bd9Sstevel@tonic-gate return( 0 );
904*7c478bd9Sstevel@tonic-gate }
905*7c478bd9Sstevel@tonic-gate
906*7c478bd9Sstevel@tonic-gate int
907*7c478bd9Sstevel@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)908*7c478bd9Sstevel@tonic-gate ldap_search_st(
909*7c478bd9Sstevel@tonic-gate LDAP *ld,
910*7c478bd9Sstevel@tonic-gate const char *base,
911*7c478bd9Sstevel@tonic-gate int scope,
912*7c478bd9Sstevel@tonic-gate const char *filter,
913*7c478bd9Sstevel@tonic-gate char **attrs,
914*7c478bd9Sstevel@tonic-gate int attrsonly,
915*7c478bd9Sstevel@tonic-gate struct timeval *timeout,
916*7c478bd9Sstevel@tonic-gate LDAPMessage **res
917*7c478bd9Sstevel@tonic-gate )
918*7c478bd9Sstevel@tonic-gate {
919*7c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
920*7c478bd9Sstevel@tonic-gate NULL, NULL, timeout, -1, -1, res ));
921*7c478bd9Sstevel@tonic-gate }
922*7c478bd9Sstevel@tonic-gate
923*7c478bd9Sstevel@tonic-gate int
924*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_search_s(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPMessage ** res)925*7c478bd9Sstevel@tonic-gate ldap_search_s(
926*7c478bd9Sstevel@tonic-gate LDAP *ld,
927*7c478bd9Sstevel@tonic-gate const char *base,
928*7c478bd9Sstevel@tonic-gate int scope,
929*7c478bd9Sstevel@tonic-gate const char *filter,
930*7c478bd9Sstevel@tonic-gate char **attrs,
931*7c478bd9Sstevel@tonic-gate int attrsonly,
932*7c478bd9Sstevel@tonic-gate LDAPMessage **res
933*7c478bd9Sstevel@tonic-gate )
934*7c478bd9Sstevel@tonic-gate {
935*7c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
936*7c478bd9Sstevel@tonic-gate NULL, NULL, NULL, -1, -1, res ));
937*7c478bd9Sstevel@tonic-gate }
938*7c478bd9Sstevel@tonic-gate
939*7c478bd9Sstevel@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)940*7c478bd9Sstevel@tonic-gate ldap_search_ext_s(
941*7c478bd9Sstevel@tonic-gate LDAP *ld,
942*7c478bd9Sstevel@tonic-gate const char *base,
943*7c478bd9Sstevel@tonic-gate int scope,
944*7c478bd9Sstevel@tonic-gate const char *filter,
945*7c478bd9Sstevel@tonic-gate char **attrs,
946*7c478bd9Sstevel@tonic-gate int attrsonly,
947*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
948*7c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
949*7c478bd9Sstevel@tonic-gate struct timeval *timeoutp,
950*7c478bd9Sstevel@tonic-gate int sizelimit,
951*7c478bd9Sstevel@tonic-gate LDAPMessage **res
952*7c478bd9Sstevel@tonic-gate )
953*7c478bd9Sstevel@tonic-gate {
954*7c478bd9Sstevel@tonic-gate return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
955*7c478bd9Sstevel@tonic-gate serverctrls, clientctrls, timeoutp,
956*7c478bd9Sstevel@tonic-gate nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, res ));
957*7c478bd9Sstevel@tonic-gate }
958*7c478bd9Sstevel@tonic-gate
959*7c478bd9Sstevel@tonic-gate
960*7c478bd9Sstevel@tonic-gate 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)961*7c478bd9Sstevel@tonic-gate nsldapi_search_s(
962*7c478bd9Sstevel@tonic-gate LDAP *ld,
963*7c478bd9Sstevel@tonic-gate const char *base,
964*7c478bd9Sstevel@tonic-gate int scope,
965*7c478bd9Sstevel@tonic-gate const char *filter,
966*7c478bd9Sstevel@tonic-gate char **attrs,
967*7c478bd9Sstevel@tonic-gate int attrsonly,
968*7c478bd9Sstevel@tonic-gate LDAPControl **serverctrls,
969*7c478bd9Sstevel@tonic-gate LDAPControl **clientctrls,
970*7c478bd9Sstevel@tonic-gate struct timeval *localtimeoutp,
971*7c478bd9Sstevel@tonic-gate int timelimit, /* -1 means use ld->ld_timelimit */
972*7c478bd9Sstevel@tonic-gate int sizelimit, /* -1 means use ld->ld_sizelimit */
973*7c478bd9Sstevel@tonic-gate LDAPMessage **res
974*7c478bd9Sstevel@tonic-gate )
975*7c478bd9Sstevel@tonic-gate {
976*7c478bd9Sstevel@tonic-gate int err, msgid;
977*7c478bd9Sstevel@tonic-gate
978*7c478bd9Sstevel@tonic-gate /*
979*7c478bd9Sstevel@tonic-gate * It is an error to pass in a zero'd timeval.
980*7c478bd9Sstevel@tonic-gate */
981*7c478bd9Sstevel@tonic-gate if ( localtimeoutp != NULL && localtimeoutp->tv_sec == 0 &&
982*7c478bd9Sstevel@tonic-gate localtimeoutp->tv_usec == 0 ) {
983*7c478bd9Sstevel@tonic-gate if ( ld != NULL ) {
984*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
985*7c478bd9Sstevel@tonic-gate }
986*7c478bd9Sstevel@tonic-gate if ( res != NULL ) {
987*7c478bd9Sstevel@tonic-gate *res = NULL;
988*7c478bd9Sstevel@tonic-gate }
989*7c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
990*7c478bd9Sstevel@tonic-gate }
991*7c478bd9Sstevel@tonic-gate
992*7c478bd9Sstevel@tonic-gate if (( err = nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
993*7c478bd9Sstevel@tonic-gate serverctrls, clientctrls, timelimit, sizelimit, &msgid ))
994*7c478bd9Sstevel@tonic-gate != LDAP_SUCCESS ) {
995*7c478bd9Sstevel@tonic-gate if ( res != NULL ) {
996*7c478bd9Sstevel@tonic-gate *res = NULL;
997*7c478bd9Sstevel@tonic-gate }
998*7c478bd9Sstevel@tonic-gate return( err );
999*7c478bd9Sstevel@tonic-gate }
1000*7c478bd9Sstevel@tonic-gate
1001*7c478bd9Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, localtimeoutp, res ) == -1 ) {
1002*7c478bd9Sstevel@tonic-gate /*
1003*7c478bd9Sstevel@tonic-gate * Error. ldap_result() sets *res to NULL for us.
1004*7c478bd9Sstevel@tonic-gate */
1005*7c478bd9Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1006*7c478bd9Sstevel@tonic-gate }
1007*7c478bd9Sstevel@tonic-gate
1008*7c478bd9Sstevel@tonic-gate if ( LDAP_GET_LDERRNO( ld, NULL, NULL ) == LDAP_TIMEOUT ) {
1009*7c478bd9Sstevel@tonic-gate (void) ldap_abandon( ld, msgid );
1010*7c478bd9Sstevel@tonic-gate err = LDAP_TIMEOUT;
1011*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1012*7c478bd9Sstevel@tonic-gate if ( res != NULL ) {
1013*7c478bd9Sstevel@tonic-gate *res = NULL;
1014*7c478bd9Sstevel@tonic-gate }
1015*7c478bd9Sstevel@tonic-gate return( err );
1016*7c478bd9Sstevel@tonic-gate }
1017*7c478bd9Sstevel@tonic-gate
1018*7c478bd9Sstevel@tonic-gate return( ldap_result2error( ld, *res, 0 ) );
1019*7c478bd9Sstevel@tonic-gate }
1020