xref: /onnv-gate/usr/src/lib/libldap5/sources/ldap/common/ufn.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*0Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*0Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*0Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*0Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*0Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*0Sstevel@tonic-gate  * rights and limitations under the License.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*0Sstevel@tonic-gate  * March 31, 1998.
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*0Sstevel@tonic-gate  * Rights Reserved.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * Contributor(s):
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
31*0Sstevel@tonic-gate  *  All rights reserved.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *  ufn.c
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #if 0
38*0Sstevel@tonic-gate #ifndef lint
39*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
40*0Sstevel@tonic-gate #endif
41*0Sstevel@tonic-gate #endif
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "ldap-int.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate typedef int (LDAP_CALL *cancelptype)( void *cancelparm );
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static int ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp,
48*0Sstevel@tonic-gate 	char *prefix, char **attrs, int attrsonly,
49*0Sstevel@tonic-gate 	LDAPMessage **res, LDAP_CANCELPROC_CALLBACK *cancelproc, void *cancelparm,
50*0Sstevel@tonic-gate 	char *tag1, char *tag2, char *tag3 );
51*0Sstevel@tonic-gate static LDAPMessage *ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b );
52*0Sstevel@tonic-gate static LDAPMessage *ldap_ufn_expand( LDAP *ld,
53*0Sstevel@tonic-gate 	LDAP_CANCELPROC_CALLBACK *cancelproc, void *cancelparm, char **dns,
54*0Sstevel@tonic-gate 	char *filter, int scope, char **attrs, int aonly, int *err );
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
58*0Sstevel@tonic-gate  *			specify ldapfilter.conf tags for each phase of search
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  *	ld		LDAP descriptor
61*0Sstevel@tonic-gate  *	ufncomp		the exploded user friendly name to look for
62*0Sstevel@tonic-gate  *	ncomp		number of elements in ufncomp
63*0Sstevel@tonic-gate  *	prefix		where to start searching
64*0Sstevel@tonic-gate  *	attrs		list of attribute types to return for matches
65*0Sstevel@tonic-gate  *	attrsonly	1 => attributes only 0 => attributes and values
66*0Sstevel@tonic-gate  *	res		will contain the result of the search
67*0Sstevel@tonic-gate  *	cancelproc	routine that returns non-zero if operation should be
68*0Sstevel@tonic-gate  *			cancelled.  This can be NULL.  If it is non-NULL, the
69*0Sstevel@tonic-gate  *			routine will be called periodically.
70*0Sstevel@tonic-gate  *	cancelparm	void * that is passed to cancelproc
71*0Sstevel@tonic-gate  *	tag[123]	the ldapfilter.conf tag that will be used in phases
72*0Sstevel@tonic-gate  *			1, 2, and 3 of the search, respectively
73*0Sstevel@tonic-gate  *
74*0Sstevel@tonic-gate  * Example:
75*0Sstevel@tonic-gate  *	char		*attrs[] = { "mail", "title", 0 };
76*0Sstevel@tonic-gate  *	char		*ufncomp[] = { "howes", "umich", "us", 0 }
77*0Sstevel@tonic-gate  *	LDAPMessage	*res;
78*0Sstevel@tonic-gate  *	error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
79*0Sstevel@tonic-gate  *			&res, acancelproc, along, "ufn first",
80*0Sstevel@tonic-gate  *			"ufn intermediate", "ufn last" );
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static int
ldap_ufn_search_ctx(LDAP * ld,char ** ufncomp,int ncomp,char * prefix,char ** attrs,int attrsonly,LDAPMessage ** res,LDAP_CANCELPROC_CALLBACK * cancelproc,void * cancelparm,char * tag1,char * tag2,char * tag3)84*0Sstevel@tonic-gate ldap_ufn_search_ctx(
85*0Sstevel@tonic-gate     LDAP 	*ld,
86*0Sstevel@tonic-gate     char 	**ufncomp,
87*0Sstevel@tonic-gate     int 	ncomp,
88*0Sstevel@tonic-gate     char 	*prefix,
89*0Sstevel@tonic-gate     char 	**attrs,
90*0Sstevel@tonic-gate     int 	attrsonly,
91*0Sstevel@tonic-gate     LDAPMessage **res,
92*0Sstevel@tonic-gate     LDAP_CANCELPROC_CALLBACK *cancelproc,
93*0Sstevel@tonic-gate     void 	*cancelparm,
94*0Sstevel@tonic-gate     char 	*tag1,
95*0Sstevel@tonic-gate     char 	*tag2,
96*0Sstevel@tonic-gate     char 	*tag3
97*0Sstevel@tonic-gate )
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	char		*dn, *ftag = NULL;
100*0Sstevel@tonic-gate 	char		**dns = NULL;
101*0Sstevel@tonic-gate 	int		max, i, err, scope = 0, phase, tries;
102*0Sstevel@tonic-gate 	LDAPFiltInfo	*fi;
103*0Sstevel@tonic-gate 	LDAPMessage	*tmpcand;
104*0Sstevel@tonic-gate 	LDAPMessage	*candidates;
105*0Sstevel@tonic-gate 	static char	*objattrs[] = { "objectClass", NULL };
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/*
108*0Sstevel@tonic-gate 	 * look up ufn components from most to least significant.
109*0Sstevel@tonic-gate 	 * there are 3 phases.
110*0Sstevel@tonic-gate 	 * 	phase 1	search the root for orgs or countries
111*0Sstevel@tonic-gate 	 * 	phase 2	search for orgs
112*0Sstevel@tonic-gate 	 * 	phase 3	search for a person
113*0Sstevel@tonic-gate 	 * in phases 1 and 2, we are building a list of candidate DNs,
114*0Sstevel@tonic-gate 	 * below which we will search for the final component of the ufn.
115*0Sstevel@tonic-gate 	 * for each component we try the filters listed in the
116*0Sstevel@tonic-gate 	 * filterconfig file, first one-level (except the last compoment),
117*0Sstevel@tonic-gate 	 * then subtree.  if any of them produce any results, we go on to
118*0Sstevel@tonic-gate 	 * the next component.
119*0Sstevel@tonic-gate 	 */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	*res = NULL;
122*0Sstevel@tonic-gate 	candidates = NULL;
123*0Sstevel@tonic-gate 	phase = 1;
124*0Sstevel@tonic-gate 	for ( ncomp--; ncomp != -1; ncomp-- ) {
125*0Sstevel@tonic-gate 		if ( *ufncomp[ncomp] == '"' ) {
126*0Sstevel@tonic-gate 			char	*quote;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 			if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
129*0Sstevel@tonic-gate 				*quote = '\0';
130*0Sstevel@tonic-gate 			strcpy( ufncomp[ncomp], ufncomp[ncomp] + 1 );
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 		if ( ncomp == 0 )
133*0Sstevel@tonic-gate 			phase = 3;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		switch ( phase ) {
136*0Sstevel@tonic-gate 		case 1:
137*0Sstevel@tonic-gate 			ftag = tag1;
138*0Sstevel@tonic-gate 			scope = LDAP_SCOPE_ONELEVEL;
139*0Sstevel@tonic-gate 			break;
140*0Sstevel@tonic-gate 		case 2:
141*0Sstevel@tonic-gate 			ftag = tag2;
142*0Sstevel@tonic-gate 			scope = LDAP_SCOPE_ONELEVEL;
143*0Sstevel@tonic-gate 			break;
144*0Sstevel@tonic-gate 		case 3:
145*0Sstevel@tonic-gate 			ftag = tag3;
146*0Sstevel@tonic-gate 			scope = LDAP_SCOPE_SUBTREE;
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		/*
151*0Sstevel@tonic-gate 		 * construct an array of DN's to search below from the
152*0Sstevel@tonic-gate 		 * list of candidates.
153*0Sstevel@tonic-gate 		 */
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		if ( candidates == NULL ) {
156*0Sstevel@tonic-gate 			if ( prefix != NULL ) {
157*0Sstevel@tonic-gate 				if ( (dns = (char **)NSLDAPI_MALLOC(
158*0Sstevel@tonic-gate 				    sizeof(char *) * 2 )) == NULL ) {
159*0Sstevel@tonic-gate 					err = LDAP_NO_MEMORY;
160*0Sstevel@tonic-gate 					LDAP_SET_LDERRNO( ld, err, NULL, NULL );
161*0Sstevel@tonic-gate 					return( err );
162*0Sstevel@tonic-gate 				}
163*0Sstevel@tonic-gate 				dns[0] = nsldapi_strdup( prefix );
164*0Sstevel@tonic-gate 				dns[1] = NULL;
165*0Sstevel@tonic-gate 			} else {
166*0Sstevel@tonic-gate 				dns = NULL;
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 		} else {
169*0Sstevel@tonic-gate 			i = 0, max = 0;
170*0Sstevel@tonic-gate 			for ( tmpcand = candidates; tmpcand != NULL &&
171*0Sstevel@tonic-gate 			    tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
172*0Sstevel@tonic-gate 			    tmpcand = tmpcand->lm_chain )
173*0Sstevel@tonic-gate 			{
174*0Sstevel@tonic-gate 				if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
175*0Sstevel@tonic-gate 					continue;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 				if ( dns == NULL ) {
178*0Sstevel@tonic-gate 					if ( (dns = (char **)NSLDAPI_MALLOC(
179*0Sstevel@tonic-gate 					    sizeof(char *) * 8 )) == NULL ) {
180*0Sstevel@tonic-gate 						err = LDAP_NO_MEMORY;
181*0Sstevel@tonic-gate 						LDAP_SET_LDERRNO( ld, err,
182*0Sstevel@tonic-gate 						    NULL, NULL );
183*0Sstevel@tonic-gate 						return( err );
184*0Sstevel@tonic-gate 					}
185*0Sstevel@tonic-gate 					max = 8;
186*0Sstevel@tonic-gate 				} else if ( i >= max ) {
187*0Sstevel@tonic-gate 					if ( (dns = (char **)NSLDAPI_REALLOC(
188*0Sstevel@tonic-gate 					    dns, sizeof(char *) * 2 * max ))
189*0Sstevel@tonic-gate 					    == NULL ) {
190*0Sstevel@tonic-gate 						err = LDAP_NO_MEMORY;
191*0Sstevel@tonic-gate 						LDAP_SET_LDERRNO( ld, err,
192*0Sstevel@tonic-gate 						    NULL, NULL );
193*0Sstevel@tonic-gate 						return( err );
194*0Sstevel@tonic-gate 					}
195*0Sstevel@tonic-gate 					max *= 2;
196*0Sstevel@tonic-gate 				}
197*0Sstevel@tonic-gate 				dns[i++] = dn;
198*0Sstevel@tonic-gate 				dns[i] = NULL;
199*0Sstevel@tonic-gate 			}
200*0Sstevel@tonic-gate 			ldap_msgfree( candidates );
201*0Sstevel@tonic-gate 			candidates = NULL;
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 		tries = 0;
204*0Sstevel@tonic-gate 	tryagain:
205*0Sstevel@tonic-gate 		tries++;
206*0Sstevel@tonic-gate 		for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
207*0Sstevel@tonic-gate 		    ufncomp[ncomp] ); fi != NULL;
208*0Sstevel@tonic-gate 		    fi = ldap_getnextfilter( ld->ld_filtd ) )
209*0Sstevel@tonic-gate 		{
210*0Sstevel@tonic-gate 			if ( (candidates = ldap_ufn_expand( ld, cancelproc,
211*0Sstevel@tonic-gate 			    cancelparm, dns, fi->lfi_filter, scope,
212*0Sstevel@tonic-gate 			    phase == 3 ? attrs : objattrs,
213*0Sstevel@tonic-gate 			    phase == 3 ? attrsonly : 1, &err )) != NULL )
214*0Sstevel@tonic-gate 			{
215*0Sstevel@tonic-gate 				break;
216*0Sstevel@tonic-gate 			}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 			if ( err == -1 || err == LDAP_USER_CANCELLED ) {
219*0Sstevel@tonic-gate 				if ( dns != NULL ) {
220*0Sstevel@tonic-gate 					ldap_value_free( dns );
221*0Sstevel@tonic-gate 					dns = NULL;
222*0Sstevel@tonic-gate 				}
223*0Sstevel@tonic-gate 				return( err );
224*0Sstevel@tonic-gate 			}
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		if ( candidates == NULL ) {
228*0Sstevel@tonic-gate 			if ( tries < 2 && phase != 3 ) {
229*0Sstevel@tonic-gate 				scope = LDAP_SCOPE_SUBTREE;
230*0Sstevel@tonic-gate 				goto tryagain;
231*0Sstevel@tonic-gate 			} else {
232*0Sstevel@tonic-gate 				if ( dns != NULL ) {
233*0Sstevel@tonic-gate 					ldap_value_free( dns );
234*0Sstevel@tonic-gate 					dns = NULL;
235*0Sstevel@tonic-gate 				}
236*0Sstevel@tonic-gate 				return( err );
237*0Sstevel@tonic-gate 			}
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		/* go on to the next component */
241*0Sstevel@tonic-gate 		if ( phase == 1 )
242*0Sstevel@tonic-gate 			phase++;
243*0Sstevel@tonic-gate 		if ( dns != NULL ) {
244*0Sstevel@tonic-gate 			ldap_value_free( dns );
245*0Sstevel@tonic-gate 			dns = NULL;
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 	*res = candidates;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	return( err );
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate int
254*0Sstevel@tonic-gate LDAP_CALL
ldap_ufn_search_ct(LDAP * ld,char * ufn,char ** attrs,int attrsonly,LDAPMessage ** res,LDAP_CANCELPROC_CALLBACK * cancelproc,void * cancelparm,char * tag1,char * tag2,char * tag3)255*0Sstevel@tonic-gate ldap_ufn_search_ct( LDAP *ld, char *ufn, char **attrs, int attrsonly,
256*0Sstevel@tonic-gate 	LDAPMessage **res, LDAP_CANCELPROC_CALLBACK *cancelproc, void *cancelparm,
257*0Sstevel@tonic-gate 	char *tag1, char *tag2, char *tag3 )
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	char	**ufncomp, **prefixcomp;
260*0Sstevel@tonic-gate 	char	*pbuf;
261*0Sstevel@tonic-gate 	int	ncomp, pcomp, i, err = 0;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	/* getfilter stuff must be inited before we are called */
264*0Sstevel@tonic-gate 	if ( ld->ld_filtd == NULL ) {
265*0Sstevel@tonic-gate 		err = LDAP_PARAM_ERROR;
266*0Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
267*0Sstevel@tonic-gate 		return( err );
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/* call ldap_explode_dn() to break the ufn into its components */
271*0Sstevel@tonic-gate 	if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL ) {
272*0Sstevel@tonic-gate 		err = LDAP_LOCAL_ERROR;
273*0Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
274*0Sstevel@tonic-gate 		return( err );
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 	for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
277*0Sstevel@tonic-gate 		;	/* NULL */
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	/* more than two components => try it fully qualified first */
280*0Sstevel@tonic-gate 	if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
281*0Sstevel@tonic-gate 		err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
282*0Sstevel@tonic-gate 		    attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 		if ( ldap_count_entries( ld, *res ) > 0 ) {
285*0Sstevel@tonic-gate 			ldap_value_free( ufncomp );
286*0Sstevel@tonic-gate 			return( err );
287*0Sstevel@tonic-gate 		} else {
288*0Sstevel@tonic-gate 			ldap_msgfree( *res );
289*0Sstevel@tonic-gate 			*res = NULL;
290*0Sstevel@tonic-gate 		}
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if ( ld->ld_ufnprefix == NULL ) {
294*0Sstevel@tonic-gate 		ldap_value_free( ufncomp );
295*0Sstevel@tonic-gate 		return( err );
296*0Sstevel@tonic-gate 	}
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	/* if that failed, or < 2 components, use the prefix */
299*0Sstevel@tonic-gate 	if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
300*0Sstevel@tonic-gate 		ldap_value_free( ufncomp );
301*0Sstevel@tonic-gate 		err = LDAP_LOCAL_ERROR;
302*0Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
303*0Sstevel@tonic-gate 		return( err );
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 	for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
306*0Sstevel@tonic-gate 		;	/* NULL */
307*0Sstevel@tonic-gate 	if ( (pbuf = (char *)NSLDAPI_MALLOC( strlen( ld->ld_ufnprefix ) + 1 ))
308*0Sstevel@tonic-gate 	    == NULL ) {
309*0Sstevel@tonic-gate 		ldap_value_free( ufncomp );
310*0Sstevel@tonic-gate 		ldap_value_free( prefixcomp );
311*0Sstevel@tonic-gate 		err = LDAP_NO_MEMORY;
312*0Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
313*0Sstevel@tonic-gate 		return( err );
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	for ( i = 0; i < pcomp; i++ ) {
317*0Sstevel@tonic-gate 		int	j;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		*pbuf = '\0';
320*0Sstevel@tonic-gate 		for ( j = i; j < pcomp; j++ ) {
321*0Sstevel@tonic-gate 			strcat( pbuf, prefixcomp[j] );
322*0Sstevel@tonic-gate 			if ( j + 1 < pcomp )
323*0Sstevel@tonic-gate 				strcat( pbuf, "," );
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 		err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
326*0Sstevel@tonic-gate 		    attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		if ( ldap_count_entries( ld, *res ) > 0 ) {
329*0Sstevel@tonic-gate 			break;
330*0Sstevel@tonic-gate 		} else {
331*0Sstevel@tonic-gate 			ldap_msgfree( *res );
332*0Sstevel@tonic-gate 			*res = NULL;
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	ldap_value_free( ufncomp );
337*0Sstevel@tonic-gate 	ldap_value_free( prefixcomp );
338*0Sstevel@tonic-gate 	NSLDAPI_FREE( pbuf );
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	return( err );
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate  * same as ldap_ufn_search_ct, except without the ability to specify
345*0Sstevel@tonic-gate  * ldapfilter.conf tags.
346*0Sstevel@tonic-gate  */
347*0Sstevel@tonic-gate int
348*0Sstevel@tonic-gate LDAP_CALL
ldap_ufn_search_c(LDAP * ld,char * ufn,char ** attrs,int attrsonly,LDAPMessage ** res,LDAP_CANCELPROC_CALLBACK * cancelproc,void * cancelparm)349*0Sstevel@tonic-gate ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly,
350*0Sstevel@tonic-gate 	LDAPMessage **res, LDAP_CANCELPROC_CALLBACK *cancelproc, void *cancelparm )
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate 	return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
353*0Sstevel@tonic-gate 	    cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate /*
357*0Sstevel@tonic-gate  * same as ldap_ufn_search_c without the cancel function
358*0Sstevel@tonic-gate  */
359*0Sstevel@tonic-gate int
360*0Sstevel@tonic-gate LDAP_CALL
ldap_ufn_search_s(LDAP * ld,char * ufn,char ** attrs,int attrsonly,LDAPMessage ** res)361*0Sstevel@tonic-gate ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly,
362*0Sstevel@tonic-gate 	LDAPMessage **res )
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	struct timeval	tv;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	tv.tv_sec = ld->ld_timelimit;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
369*0Sstevel@tonic-gate 		ld->ld_timelimit ? ldap_ufn_timeout : NULL,
370*0Sstevel@tonic-gate 		ld->ld_timelimit ? (void *) &tv : NULL,
371*0Sstevel@tonic-gate 		"ufn first", "ufn intermediate", "ufn last" ) );
372*0Sstevel@tonic-gate }
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate /*
376*0Sstevel@tonic-gate  * ldap_msg_merge - merge two ldap search result chains.  the more
377*0Sstevel@tonic-gate  * serious of the two error result codes is kept.
378*0Sstevel@tonic-gate  */
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate static LDAPMessage *
ldap_msg_merge(LDAP * ld,LDAPMessage * a,LDAPMessage * b)381*0Sstevel@tonic-gate ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	LDAPMessage	*end, *aprev, *aend, *bprev, *bend;
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	if ( a == NULL )
386*0Sstevel@tonic-gate 		return( b );
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	if ( b == NULL )
389*0Sstevel@tonic-gate 		return( a );
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	/* find the ends of the a and b chains */
392*0Sstevel@tonic-gate 	aprev = NULL;
393*0Sstevel@tonic-gate 	for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
394*0Sstevel@tonic-gate 		aprev = aend;
395*0Sstevel@tonic-gate 	bprev = NULL;
396*0Sstevel@tonic-gate 	for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
397*0Sstevel@tonic-gate 		bprev = bend;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	/* keep result a */
400*0Sstevel@tonic-gate 	if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
401*0Sstevel@tonic-gate 		/* remove result b */
402*0Sstevel@tonic-gate 		ldap_msgfree( bend );
403*0Sstevel@tonic-gate 		if ( bprev != NULL )
404*0Sstevel@tonic-gate 			bprev->lm_chain = NULL;
405*0Sstevel@tonic-gate 		else
406*0Sstevel@tonic-gate 			b = NULL;
407*0Sstevel@tonic-gate 		end = aend;
408*0Sstevel@tonic-gate 		if ( aprev != NULL )
409*0Sstevel@tonic-gate 			aprev->lm_chain = NULL;
410*0Sstevel@tonic-gate 		else
411*0Sstevel@tonic-gate 			a = NULL;
412*0Sstevel@tonic-gate 	/* keep result b */
413*0Sstevel@tonic-gate 	} else {
414*0Sstevel@tonic-gate 		/* remove result a */
415*0Sstevel@tonic-gate 		ldap_msgfree( aend );
416*0Sstevel@tonic-gate 		if ( aprev != NULL )
417*0Sstevel@tonic-gate 			aprev->lm_chain = NULL;
418*0Sstevel@tonic-gate 		else
419*0Sstevel@tonic-gate 			a = NULL;
420*0Sstevel@tonic-gate 		end = bend;
421*0Sstevel@tonic-gate 		if ( bprev != NULL )
422*0Sstevel@tonic-gate 			bprev->lm_chain = NULL;
423*0Sstevel@tonic-gate 		else
424*0Sstevel@tonic-gate 			b = NULL;
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
428*0Sstevel@tonic-gate 	    (b == NULL && aprev == NULL) )
429*0Sstevel@tonic-gate 		return( end );
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	if ( a == NULL ) {
432*0Sstevel@tonic-gate 		bprev->lm_chain = end;
433*0Sstevel@tonic-gate 		return( b );
434*0Sstevel@tonic-gate 	} else if ( b == NULL ) {
435*0Sstevel@tonic-gate 		aprev->lm_chain = end;
436*0Sstevel@tonic-gate 		return( a );
437*0Sstevel@tonic-gate 	} else {
438*0Sstevel@tonic-gate 		bprev->lm_chain = end;
439*0Sstevel@tonic-gate 		aprev->lm_chain = b;
440*0Sstevel@tonic-gate 		return( a );
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate }
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate static LDAPMessage *
ldap_ufn_expand(LDAP * ld,LDAP_CANCELPROC_CALLBACK * cancelproc,void * cancelparm,char ** dns,char * filter,int scope,char ** attrs,int aonly,int * err)445*0Sstevel@tonic-gate ldap_ufn_expand( LDAP *ld, LDAP_CANCELPROC_CALLBACK *cancelproc,
446*0Sstevel@tonic-gate 	void *cancelparm, char **dns, char *filter, int scope,
447*0Sstevel@tonic-gate 	char **attrs, int aonly, int *err )
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate 	LDAPMessage	*tmpcand, *tmpres;
450*0Sstevel@tonic-gate 	char		*dn;
451*0Sstevel@tonic-gate 	int		i, msgid;
452*0Sstevel@tonic-gate 	struct timeval	tv;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	/* search for this component below the current candidates */
455*0Sstevel@tonic-gate 	tmpcand = NULL;
456*0Sstevel@tonic-gate 	i = 0;
457*0Sstevel@tonic-gate 	do {
458*0Sstevel@tonic-gate 		if ( dns != NULL )
459*0Sstevel@tonic-gate 			dn = dns[i];
460*0Sstevel@tonic-gate 		else
461*0Sstevel@tonic-gate 			dn = "";
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 		if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
464*0Sstevel@tonic-gate 		    aonly )) == -1 ) {
465*0Sstevel@tonic-gate 			ldap_msgfree( tmpcand );
466*0Sstevel@tonic-gate 			*err = LDAP_GET_LDERRNO( ld, NULL, NULL );
467*0Sstevel@tonic-gate 			return( NULL );
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		tv.tv_sec = 0;
471*0Sstevel@tonic-gate 		tv.tv_usec = 100000;	/* 1/10 of a second */
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 		do {
474*0Sstevel@tonic-gate 			*err = ldap_result( ld, msgid, 1, &tv, &tmpres );
475*0Sstevel@tonic-gate 			if ( *err == 0 && cancelproc != NULL &&
476*0Sstevel@tonic-gate 			    (*cancelproc)( cancelparm ) != 0 ) {
477*0Sstevel@tonic-gate 				ldap_abandon( ld, msgid );
478*0Sstevel@tonic-gate 				*err = LDAP_USER_CANCELLED;
479*0Sstevel@tonic-gate 				LDAP_SET_LDERRNO( ld, *err, NULL, NULL );
480*0Sstevel@tonic-gate 			}
481*0Sstevel@tonic-gate 		} while ( *err == 0 );
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 		if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
484*0Sstevel@tonic-gate 		    ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
485*0Sstevel@tonic-gate 			ldap_msgfree( tmpcand );
486*0Sstevel@tonic-gate 			return( NULL );
487*0Sstevel@tonic-gate 		}
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 		tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 		i++;
492*0Sstevel@tonic-gate 	} while ( dns != NULL && dns[i] != NULL );
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
495*0Sstevel@tonic-gate 		return( tmpcand );
496*0Sstevel@tonic-gate 	} else {
497*0Sstevel@tonic-gate 		ldap_msgfree( tmpcand );
498*0Sstevel@tonic-gate 		return( NULL );
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate }
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate /*
503*0Sstevel@tonic-gate  * ldap_ufn_setfilter - set the filter config file used in ufn searching
504*0Sstevel@tonic-gate  */
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate LDAPFiltDesc *
507*0Sstevel@tonic-gate LDAP_CALL
ldap_ufn_setfilter(LDAP * ld,char * fname)508*0Sstevel@tonic-gate ldap_ufn_setfilter( LDAP *ld, char *fname )
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	if ( ld->ld_filtd != NULL )
511*0Sstevel@tonic-gate 		ldap_getfilter_free( ld->ld_filtd );
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	return( ld->ld_filtd = ldap_init_getfilter( fname ) );
514*0Sstevel@tonic-gate }
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate void
517*0Sstevel@tonic-gate LDAP_CALL
ldap_ufn_setprefix(LDAP * ld,char * prefix)518*0Sstevel@tonic-gate ldap_ufn_setprefix( LDAP *ld, char *prefix )
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate 	if ( ld->ld_ufnprefix != NULL )
521*0Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_ufnprefix );
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	ld->ld_ufnprefix = nsldapi_strdup( prefix );
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate int
527*0Sstevel@tonic-gate LDAP_C
ldap_ufn_timeout(void * tvparam)528*0Sstevel@tonic-gate ldap_ufn_timeout( void *tvparam )
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate 	struct timeval	*tv;
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	tv = (struct timeval *)tvparam;
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	if ( tv->tv_sec != 0 ) {
535*0Sstevel@tonic-gate 		tv->tv_usec = tv->tv_sec * 1000000;	/* sec => micro sec */
536*0Sstevel@tonic-gate 		tv->tv_sec = 0;
537*0Sstevel@tonic-gate 	}
538*0Sstevel@tonic-gate 	tv->tv_usec -= 100000;	/* 1/10 of a second */
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	return( tv->tv_usec <= 0 ? 1 : 0 );
541*0Sstevel@tonic-gate }
542