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