2 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
12 * searchpref.c: search preferences library routines for LDAP clients
13 * 17 May 1994 by Gordon Good
22 #include <ac/string.h>
24 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_FILE_H
35 static void free_searchobj LDAP_P(( struct ldap_searchobj *so ));
36 static int read_next_searchobj LDAP_P(( char **bufp, long *blenp,
37 struct ldap_searchobj **sop, int soversion ));
40 static char *sobjoptions[] = {
46 static unsigned long sobjoptvals[] = {
47 LDAP_SEARCHOBJ_OPT_INTERNAL,
52 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
59 if (( fp = fopen( file, "r" )) == NULL ) {
60 return( LDAP_SEARCHPREF_ERR_FILE );
63 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
65 return( LDAP_SEARCHPREF_ERR_FILE );
70 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
72 return( LDAP_SEARCHPREF_ERR_FILE );
75 if (( buf = malloc( (size_t)len )) == NULL ) {
77 return( LDAP_SEARCHPREF_ERR_MEM );
80 rlen = fread( buf, 1, (size_t)len, fp );
84 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
86 return( LDAP_SEARCHPREF_ERR_FILE );
89 rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
97 ldap_init_searchprefs_buf( char *buf, long buflen,
98 struct ldap_searchobj **solistp )
100 int rc = -1, version;
102 struct ldap_searchobj *prevso, *so;
104 *solistp = prevso = NULLSEARCHOBJ;
106 if ( next_line_tokens( &buf, &buflen, &toks ) != 2 ||
107 strcasecmp( toks[ 0 ], "version" ) != 0 ) {
108 free_strarray( toks );
109 return( LDAP_SEARCHPREF_ERR_SYNTAX );
111 version = atoi( toks[ 1 ] );
112 free_strarray( toks );
113 if ( version != LDAP_SEARCHPREF_VERSION &&
114 version != LDAP_SEARCHPREF_VERSION_ZERO ) {
115 return( LDAP_SEARCHPREF_ERR_VERSION );
118 while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
119 version )) == 0 && so != NULLSEARCHOBJ ) {
120 if ( prevso == NULLSEARCHOBJ ) {
123 prevso->so_next = so;
129 ldap_free_searchprefs( *solistp );
138 ldap_free_searchprefs( struct ldap_searchobj *solist )
140 struct ldap_searchobj *so, *nextso;
142 if ( solist != NULL ) {
143 for ( so = solist; so != NULL; so = nextso ) {
144 nextso = so->so_next;
145 free_searchobj( so );
148 /* XXX XXX need to do some work here */
153 free_searchobj( struct ldap_searchobj *so )
156 if ( so->so_objtypeprompt != NULL ) {
157 free( so->so_objtypeprompt );
159 if ( so->so_prompt != NULL ) {
160 free( so->so_prompt );
162 if ( so->so_filterprefix != NULL ) {
163 free( so->so_filterprefix );
165 if ( so->so_filtertag != NULL ) {
166 free( so->so_filtertag );
168 if ( so->so_defaultselectattr != NULL ) {
169 free( so->so_defaultselectattr );
171 if ( so->so_defaultselecttext != NULL ) {
172 free( so->so_defaultselecttext );
174 if ( so->so_salist != NULL ) {
175 struct ldap_searchattr *sa, *nextsa;
176 for ( sa = so->so_salist; sa != NULL; sa = nextsa ) {
177 nextsa = sa->sa_next;
178 if ( sa->sa_attrlabel != NULL ) {
179 free( sa->sa_attrlabel );
181 if ( sa->sa_attr != NULL ) {
184 if ( sa->sa_selectattr != NULL ) {
185 free( sa->sa_selectattr );
187 if ( sa->sa_selecttext != NULL ) {
188 free( sa->sa_selecttext );
193 if ( so->so_smlist != NULL ) {
194 struct ldap_searchmatch *sm, *nextsm;
195 for ( sm = so->so_smlist; sm != NULL; sm = nextsm ) {
196 nextsm = sm->sm_next;
197 if ( sm->sm_matchprompt != NULL ) {
198 free( sm->sm_matchprompt );
200 if ( sm->sm_filter != NULL ) {
201 free( sm->sm_filter );
212 struct ldap_searchobj *
213 ldap_first_searchobj( struct ldap_searchobj *solist )
219 struct ldap_searchobj *
220 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
222 return( so == NULLSEARCHOBJ ? so : so->so_next );
228 read_next_searchobj( char **bufp, long *blenp, struct ldap_searchobj **sop,
233 struct ldap_searchobj *so;
234 struct ldap_searchattr **sa;
235 struct ldap_searchmatch **sm;
240 * Object type prompt comes first
242 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
243 free_strarray( toks );
244 return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
247 if (( so = (struct ldap_searchobj *)calloc( 1,
248 sizeof( struct ldap_searchobj ))) == NULL ) {
249 free_strarray( toks );
250 return( LDAP_SEARCHPREF_ERR_MEM );
252 so->so_objtypeprompt = toks[ 0 ];
253 free( (char *)toks );
256 * if this is post-version zero, options come next
258 if ( soversion > LDAP_SEARCHPREF_VERSION_ZERO ) {
259 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) < 1 ) {
260 free_strarray( toks );
261 ldap_free_searchprefs( so );
262 return( LDAP_SEARCHPREF_ERR_SYNTAX );
264 for ( i = 0; toks[ i ] != NULL; ++i ) {
265 for ( j = 0; sobjoptions[ j ] != NULL; ++j ) {
266 if ( strcasecmp( toks[ i ], sobjoptions[ j ] ) == 0 ) {
267 so->so_options |= sobjoptvals[ j ];
271 free_strarray( toks );
275 * "Fewer choices" prompt is next
277 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
278 free_strarray( toks );
279 ldap_free_searchprefs( so );
280 return( LDAP_SEARCHPREF_ERR_SYNTAX );
282 so->so_prompt = toks[ 0 ];
283 free( (char *)toks );
286 * Filter prefix for "More Choices" searching is next
288 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
289 free_strarray( toks );
290 ldap_free_searchprefs( so );
291 return( LDAP_SEARCHPREF_ERR_SYNTAX );
293 so->so_filterprefix = toks[ 0 ];
294 free( (char *)toks );
297 * "Fewer Choices" filter tag comes next
299 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
300 free_strarray( toks );
301 ldap_free_searchprefs( so );
302 return( LDAP_SEARCHPREF_ERR_SYNTAX );
304 so->so_filtertag = toks[ 0 ];
305 free( (char *)toks );
308 * Selection (disambiguation) attribute comes next
310 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
311 free_strarray( toks );
312 ldap_free_searchprefs( so );
313 return( LDAP_SEARCHPREF_ERR_SYNTAX );
315 so->so_defaultselectattr = toks[ 0 ];
316 free( (char *)toks );
319 * Label for selection (disambiguation) attribute
321 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
322 free_strarray( toks );
323 ldap_free_searchprefs( so );
324 return( LDAP_SEARCHPREF_ERR_SYNTAX );
326 so->so_defaultselecttext = toks[ 0 ];
327 free( (char *)toks );
330 * Search scope is next
332 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
333 free_strarray( toks );
334 ldap_free_searchprefs( so );
335 return( LDAP_SEARCHPREF_ERR_SYNTAX );
337 if ( !strcasecmp(toks[ 0 ], "subtree" )) {
338 so->so_defaultscope = LDAP_SCOPE_SUBTREE;
339 } else if ( !strcasecmp(toks[ 0 ], "onelevel" )) {
340 so->so_defaultscope = LDAP_SCOPE_ONELEVEL;
341 } else if ( !strcasecmp(toks[ 0 ], "base" )) {
342 so->so_defaultscope = LDAP_SCOPE_BASE;
344 ldap_free_searchprefs( so );
345 return( LDAP_SEARCHPREF_ERR_SYNTAX );
347 free_strarray( toks );
351 * "More Choices" search option list comes next
353 sa = &( so->so_salist );
354 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
356 free_strarray( toks );
357 ldap_free_searchprefs( so );
358 return( LDAP_SEARCHPREF_ERR_SYNTAX );
360 if (( *sa = ( struct ldap_searchattr * ) calloc( 1,
361 sizeof( struct ldap_searchattr ))) == NULL ) {
362 free_strarray( toks );
363 ldap_free_searchprefs( so );
364 return( LDAP_SEARCHPREF_ERR_MEM );
366 ( *sa )->sa_attrlabel = toks[ 0 ];
367 ( *sa )->sa_attr = toks[ 1 ];
368 ( *sa )->sa_selectattr = toks[ 3 ];
369 ( *sa )->sa_selecttext = toks[ 4 ];
370 /* Deal with bitmap */
371 ( *sa )->sa_matchtypebitmap = 0;
372 for ( i = strlen( toks[ 2 ] ) - 1, j = 0; i >= 0; i--, j++ ) {
373 if ( toks[ 2 ][ i ] == '1' ) {
374 ( *sa )->sa_matchtypebitmap |= (1 << j);
378 free( ( char * ) toks );
379 sa = &(( *sa )->sa_next);
384 * Match types are last
386 sm = &( so->so_smlist );
387 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
389 free_strarray( toks );
390 ldap_free_searchprefs( so );
391 return( LDAP_SEARCHPREF_ERR_SYNTAX );
393 if (( *sm = ( struct ldap_searchmatch * ) calloc( 1,
394 sizeof( struct ldap_searchmatch ))) == NULL ) {
395 free_strarray( toks );
396 ldap_free_searchprefs( so );
397 return( LDAP_SEARCHPREF_ERR_MEM );
399 ( *sm )->sm_matchprompt = toks[ 0 ];
400 ( *sm )->sm_filter = toks[ 1 ];
401 free( ( char * ) toks );
402 sm = &(( *sm )->sm_next );