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
27 #include <sys/types.h>
40 int next_line_tokens();
42 static void free_searchobj();
43 static int read_next_searchobj();
44 #else /* !NEEDPROTOS */
45 int next_line_tokens( char **bufp, long *blenp, char ***toksp );
46 void free_strarray( char **sap );
47 static void free_searchobj( struct ldap_searchobj *so );
48 static int read_next_searchobj( char **bufp, long *blenp,
49 struct ldap_searchobj **sop, int soversion );
50 #endif /* !NEEDPROTOS */
53 static char *sobjoptions[] = {
59 static unsigned long sobjoptvals[] = {
60 LDAP_SEARCHOBJ_OPT_INTERNAL,
65 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
72 if (( fp = fopen( file, "r" )) == NULL ) {
73 return( LDAP_SEARCHPREF_ERR_FILE );
76 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
78 return( LDAP_SEARCHPREF_ERR_FILE );
83 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
85 return( LDAP_SEARCHPREF_ERR_FILE );
88 if (( buf = malloc( (size_t)len )) == NULL ) {
90 return( LDAP_SEARCHPREF_ERR_MEM );
93 rlen = fread( buf, 1, (size_t)len, fp );
97 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
99 return( LDAP_SEARCHPREF_ERR_FILE );
102 rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
110 ldap_init_searchprefs_buf( char *buf, long buflen,
111 struct ldap_searchobj **solistp )
115 struct ldap_searchobj *prevso, *so;
117 *solistp = prevso = NULLSEARCHOBJ;
119 if ( next_line_tokens( &buf, &buflen, &toks ) != 2 ||
120 strcasecmp( toks[ 0 ], "version" ) != 0 ) {
121 free_strarray( toks );
122 return( LDAP_SEARCHPREF_ERR_SYNTAX );
124 version = atoi( toks[ 1 ] );
125 free_strarray( toks );
126 if ( version != LDAP_SEARCHPREF_VERSION &&
127 version != LDAP_SEARCHPREF_VERSION_ZERO ) {
128 return( LDAP_SEARCHPREF_ERR_VERSION );
131 while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
132 version )) == 0 && so != NULLSEARCHOBJ ) {
133 if ( prevso == NULLSEARCHOBJ ) {
136 prevso->so_next = so;
142 ldap_free_searchprefs( *solistp );
151 ldap_free_searchprefs( struct ldap_searchobj *solist )
153 struct ldap_searchobj *so, *nextso;
155 if ( solist != NULL ) {
156 for ( so = solist; so != NULL; so = nextso ) {
157 nextso = so->so_next;
158 free_searchobj( so );
161 /* XXX XXX need to do some work here */
166 free_searchobj( struct ldap_searchobj *so )
169 if ( so->so_objtypeprompt != NULL ) {
170 free( so->so_objtypeprompt );
172 if ( so->so_prompt != NULL ) {
173 free( so->so_prompt );
175 if ( so->so_filterprefix != NULL ) {
176 free( so->so_filterprefix );
178 if ( so->so_filtertag != NULL ) {
179 free( so->so_filtertag );
181 if ( so->so_defaultselectattr != NULL ) {
182 free( so->so_defaultselectattr );
184 if ( so->so_defaultselecttext != NULL ) {
185 free( so->so_defaultselecttext );
187 if ( so->so_salist != NULL ) {
188 struct ldap_searchattr *sa, *nextsa;
189 for ( sa = so->so_salist; sa != NULL; sa = nextsa ) {
190 nextsa = sa->sa_next;
191 if ( sa->sa_attrlabel != NULL ) {
192 free( sa->sa_attrlabel );
194 if ( sa->sa_attr != NULL ) {
197 if ( sa->sa_selectattr != NULL ) {
198 free( sa->sa_selectattr );
200 if ( sa->sa_selecttext != NULL ) {
201 free( sa->sa_selecttext );
206 if ( so->so_smlist != NULL ) {
207 struct ldap_searchmatch *sm, *nextsm;
208 for ( sm = so->so_smlist; sm != NULL; sm = nextsm ) {
209 nextsm = sm->sm_next;
210 if ( sm->sm_matchprompt != NULL ) {
211 free( sm->sm_matchprompt );
213 if ( sm->sm_filter != NULL ) {
214 free( sm->sm_filter );
225 struct ldap_searchobj *
226 ldap_first_searchobj( struct ldap_searchobj *solist )
232 struct ldap_searchobj *
233 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
235 return( so == NULLSEARCHOBJ ? so : so->so_next );
241 read_next_searchobj( char **bufp, long *blenp, struct ldap_searchobj **sop,
246 struct ldap_searchobj *so;
247 struct ldap_searchattr **sa;
248 struct ldap_searchmatch **sm;
253 * Object type prompt comes first
255 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
256 free_strarray( toks );
257 return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
260 if (( so = (struct ldap_searchobj *)calloc( 1,
261 sizeof( struct ldap_searchobj ))) == NULL ) {
262 free_strarray( toks );
263 return( LDAP_SEARCHPREF_ERR_MEM );
265 so->so_objtypeprompt = toks[ 0 ];
266 free( (char *)toks );
269 * if this is post-version zero, options come next
271 if ( soversion > LDAP_SEARCHPREF_VERSION_ZERO ) {
272 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) < 1 ) {
273 free_strarray( toks );
274 ldap_free_searchprefs( so );
275 return( LDAP_SEARCHPREF_ERR_SYNTAX );
277 for ( i = 0; toks[ i ] != NULL; ++i ) {
278 for ( j = 0; sobjoptions[ j ] != NULL; ++j ) {
279 if ( strcasecmp( toks[ i ], sobjoptions[ j ] ) == 0 ) {
280 so->so_options |= sobjoptvals[ j ];
284 free_strarray( toks );
288 * "Fewer choices" prompt is next
290 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
291 free_strarray( toks );
292 ldap_free_searchprefs( so );
293 return( LDAP_SEARCHPREF_ERR_SYNTAX );
295 so->so_prompt = toks[ 0 ];
296 free( (char *)toks );
299 * Filter prefix for "More Choices" searching is next
301 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
302 free_strarray( toks );
303 ldap_free_searchprefs( so );
304 return( LDAP_SEARCHPREF_ERR_SYNTAX );
306 so->so_filterprefix = toks[ 0 ];
307 free( (char *)toks );
310 * "Fewer Choices" filter tag comes next
312 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
313 free_strarray( toks );
314 ldap_free_searchprefs( so );
315 return( LDAP_SEARCHPREF_ERR_SYNTAX );
317 so->so_filtertag = toks[ 0 ];
318 free( (char *)toks );
321 * Selection (disambiguation) attribute comes next
323 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
324 free_strarray( toks );
325 ldap_free_searchprefs( so );
326 return( LDAP_SEARCHPREF_ERR_SYNTAX );
328 so->so_defaultselectattr = toks[ 0 ];
329 free( (char *)toks );
332 * Label for selection (disambiguation) attribute
334 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
335 free_strarray( toks );
336 ldap_free_searchprefs( so );
337 return( LDAP_SEARCHPREF_ERR_SYNTAX );
339 so->so_defaultselecttext = toks[ 0 ];
340 free( (char *)toks );
343 * Search scope is next
345 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
346 free_strarray( toks );
347 ldap_free_searchprefs( so );
348 return( LDAP_SEARCHPREF_ERR_SYNTAX );
350 if ( !strcasecmp(toks[ 0 ], "subtree" )) {
351 so->so_defaultscope = LDAP_SCOPE_SUBTREE;
352 } else if ( !strcasecmp(toks[ 0 ], "onelevel" )) {
353 so->so_defaultscope = LDAP_SCOPE_ONELEVEL;
354 } else if ( !strcasecmp(toks[ 0 ], "base" )) {
355 so->so_defaultscope = LDAP_SCOPE_BASE;
357 ldap_free_searchprefs( so );
358 return( LDAP_SEARCHPREF_ERR_SYNTAX );
360 free_strarray( toks );
364 * "More Choices" search option list comes next
366 sa = &( so->so_salist );
367 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
369 free_strarray( toks );
370 ldap_free_searchprefs( so );
371 return( LDAP_SEARCHPREF_ERR_SYNTAX );
373 if (( *sa = ( struct ldap_searchattr * ) calloc( 1,
374 sizeof( struct ldap_searchattr ))) == NULL ) {
375 free_strarray( toks );
376 ldap_free_searchprefs( so );
377 return( LDAP_SEARCHPREF_ERR_MEM );
379 ( *sa )->sa_attrlabel = toks[ 0 ];
380 ( *sa )->sa_attr = toks[ 1 ];
381 ( *sa )->sa_selectattr = toks[ 3 ];
382 ( *sa )->sa_selecttext = toks[ 4 ];
383 /* Deal with bitmap */
384 ( *sa )->sa_matchtypebitmap = 0;
385 for ( i = strlen( toks[ 2 ] ) - 1, j = 0; i >= 0; i--, j++ ) {
386 if ( toks[ 2 ][ i ] == '1' ) {
387 ( *sa )->sa_matchtypebitmap |= (1 << j);
391 free( ( char * ) toks );
392 sa = &(( *sa )->sa_next);
397 * Match types are last
399 sm = &( so->so_smlist );
400 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
402 free_strarray( toks );
403 ldap_free_searchprefs( so );
404 return( LDAP_SEARCHPREF_ERR_SYNTAX );
406 if (( *sm = ( struct ldap_searchmatch * ) calloc( 1,
407 sizeof( struct ldap_searchmatch ))) == NULL ) {
408 free_strarray( toks );
409 ldap_free_searchprefs( so );
410 return( LDAP_SEARCHPREF_ERR_MEM );
412 ( *sm )->sm_matchprompt = toks[ 0 ];
413 ( *sm )->sm_filter = toks[ 1 ];
414 free( ( char * ) toks );
415 sm = &(( *sm )->sm_next );