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
33 static void free_searchobj LDAP_P(( struct ldap_searchobj *so ));
34 static int read_next_searchobj LDAP_P(( char **bufp, long *blenp,
35 struct ldap_searchobj **sop, int soversion ));
38 static char *sobjoptions[] = {
44 static unsigned long sobjoptvals[] = {
45 LDAP_SEARCHOBJ_OPT_INTERNAL,
50 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
57 if (( fp = fopen( file, "r" )) == NULL ) {
58 return( LDAP_SEARCHPREF_ERR_FILE );
61 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
63 return( LDAP_SEARCHPREF_ERR_FILE );
68 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
70 return( LDAP_SEARCHPREF_ERR_FILE );
73 if (( buf = malloc( (size_t)len )) == NULL ) {
75 return( LDAP_SEARCHPREF_ERR_MEM );
78 rlen = fread( buf, 1, (size_t)len, fp );
82 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
84 return( LDAP_SEARCHPREF_ERR_FILE );
87 rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
95 ldap_init_searchprefs_buf( char *buf, long buflen,
96 struct ldap_searchobj **solistp )
100 struct ldap_searchobj *prevso, *so;
102 *solistp = prevso = NULLSEARCHOBJ;
104 if ( next_line_tokens( &buf, &buflen, &toks ) != 2 ||
105 strcasecmp( toks[ 0 ], "version" ) != 0 ) {
106 free_strarray( toks );
107 return( LDAP_SEARCHPREF_ERR_SYNTAX );
109 version = atoi( toks[ 1 ] );
110 free_strarray( toks );
111 if ( version != LDAP_SEARCHPREF_VERSION &&
112 version != LDAP_SEARCHPREF_VERSION_ZERO ) {
113 return( LDAP_SEARCHPREF_ERR_VERSION );
116 while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
117 version )) == 0 && so != NULLSEARCHOBJ ) {
118 if ( prevso == NULLSEARCHOBJ ) {
121 prevso->so_next = so;
127 ldap_free_searchprefs( *solistp );
136 ldap_free_searchprefs( struct ldap_searchobj *solist )
138 struct ldap_searchobj *so, *nextso;
140 if ( solist != NULL ) {
141 for ( so = solist; so != NULL; so = nextso ) {
142 nextso = so->so_next;
143 free_searchobj( so );
146 /* XXX XXX need to do some work here */
151 free_searchobj( struct ldap_searchobj *so )
154 if ( so->so_objtypeprompt != NULL ) {
155 free( so->so_objtypeprompt );
157 if ( so->so_prompt != NULL ) {
158 free( so->so_prompt );
160 if ( so->so_filterprefix != NULL ) {
161 free( so->so_filterprefix );
163 if ( so->so_filtertag != NULL ) {
164 free( so->so_filtertag );
166 if ( so->so_defaultselectattr != NULL ) {
167 free( so->so_defaultselectattr );
169 if ( so->so_defaultselecttext != NULL ) {
170 free( so->so_defaultselecttext );
172 if ( so->so_salist != NULL ) {
173 struct ldap_searchattr *sa, *nextsa;
174 for ( sa = so->so_salist; sa != NULL; sa = nextsa ) {
175 nextsa = sa->sa_next;
176 if ( sa->sa_attrlabel != NULL ) {
177 free( sa->sa_attrlabel );
179 if ( sa->sa_attr != NULL ) {
182 if ( sa->sa_selectattr != NULL ) {
183 free( sa->sa_selectattr );
185 if ( sa->sa_selecttext != NULL ) {
186 free( sa->sa_selecttext );
191 if ( so->so_smlist != NULL ) {
192 struct ldap_searchmatch *sm, *nextsm;
193 for ( sm = so->so_smlist; sm != NULL; sm = nextsm ) {
194 nextsm = sm->sm_next;
195 if ( sm->sm_matchprompt != NULL ) {
196 free( sm->sm_matchprompt );
198 if ( sm->sm_filter != NULL ) {
199 free( sm->sm_filter );
210 struct ldap_searchobj *
211 ldap_first_searchobj( struct ldap_searchobj *solist )
217 struct ldap_searchobj *
218 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
220 return( so == NULLSEARCHOBJ ? so : so->so_next );
226 read_next_searchobj( char **bufp, long *blenp, struct ldap_searchobj **sop,
231 struct ldap_searchobj *so;
232 struct ldap_searchattr **sa;
233 struct ldap_searchmatch **sm;
238 * Object type prompt comes first
240 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
241 free_strarray( toks );
242 return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
245 if (( so = (struct ldap_searchobj *)calloc( 1,
246 sizeof( struct ldap_searchobj ))) == NULL ) {
247 free_strarray( toks );
248 return( LDAP_SEARCHPREF_ERR_MEM );
250 so->so_objtypeprompt = toks[ 0 ];
251 free( (char *)toks );
254 * if this is post-version zero, options come next
256 if ( soversion > LDAP_SEARCHPREF_VERSION_ZERO ) {
257 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) < 1 ) {
258 free_strarray( toks );
259 ldap_free_searchprefs( so );
260 return( LDAP_SEARCHPREF_ERR_SYNTAX );
262 for ( i = 0; toks[ i ] != NULL; ++i ) {
263 for ( j = 0; sobjoptions[ j ] != NULL; ++j ) {
264 if ( strcasecmp( toks[ i ], sobjoptions[ j ] ) == 0 ) {
265 so->so_options |= sobjoptvals[ j ];
269 free_strarray( toks );
273 * "Fewer choices" prompt is next
275 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
276 free_strarray( toks );
277 ldap_free_searchprefs( so );
278 return( LDAP_SEARCHPREF_ERR_SYNTAX );
280 so->so_prompt = toks[ 0 ];
281 free( (char *)toks );
284 * Filter prefix for "More Choices" searching is next
286 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
287 free_strarray( toks );
288 ldap_free_searchprefs( so );
289 return( LDAP_SEARCHPREF_ERR_SYNTAX );
291 so->so_filterprefix = toks[ 0 ];
292 free( (char *)toks );
295 * "Fewer Choices" filter tag comes next
297 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
298 free_strarray( toks );
299 ldap_free_searchprefs( so );
300 return( LDAP_SEARCHPREF_ERR_SYNTAX );
302 so->so_filtertag = toks[ 0 ];
303 free( (char *)toks );
306 * Selection (disambiguation) attribute comes next
308 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
309 free_strarray( toks );
310 ldap_free_searchprefs( so );
311 return( LDAP_SEARCHPREF_ERR_SYNTAX );
313 so->so_defaultselectattr = toks[ 0 ];
314 free( (char *)toks );
317 * Label for selection (disambiguation) attribute
319 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
320 free_strarray( toks );
321 ldap_free_searchprefs( so );
322 return( LDAP_SEARCHPREF_ERR_SYNTAX );
324 so->so_defaultselecttext = toks[ 0 ];
325 free( (char *)toks );
328 * Search scope is next
330 if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
331 free_strarray( toks );
332 ldap_free_searchprefs( so );
333 return( LDAP_SEARCHPREF_ERR_SYNTAX );
335 if ( !strcasecmp(toks[ 0 ], "subtree" )) {
336 so->so_defaultscope = LDAP_SCOPE_SUBTREE;
337 } else if ( !strcasecmp(toks[ 0 ], "onelevel" )) {
338 so->so_defaultscope = LDAP_SCOPE_ONELEVEL;
339 } else if ( !strcasecmp(toks[ 0 ], "base" )) {
340 so->so_defaultscope = LDAP_SCOPE_BASE;
342 ldap_free_searchprefs( so );
343 return( LDAP_SEARCHPREF_ERR_SYNTAX );
345 free_strarray( toks );
349 * "More Choices" search option list comes next
351 sa = &( so->so_salist );
352 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
354 free_strarray( toks );
355 ldap_free_searchprefs( so );
356 return( LDAP_SEARCHPREF_ERR_SYNTAX );
358 if (( *sa = ( struct ldap_searchattr * ) calloc( 1,
359 sizeof( struct ldap_searchattr ))) == NULL ) {
360 free_strarray( toks );
361 ldap_free_searchprefs( so );
362 return( LDAP_SEARCHPREF_ERR_MEM );
364 ( *sa )->sa_attrlabel = toks[ 0 ];
365 ( *sa )->sa_attr = toks[ 1 ];
366 ( *sa )->sa_selectattr = toks[ 3 ];
367 ( *sa )->sa_selecttext = toks[ 4 ];
368 /* Deal with bitmap */
369 ( *sa )->sa_matchtypebitmap = 0;
370 for ( i = strlen( toks[ 2 ] ) - 1, j = 0; i >= 0; i--, j++ ) {
371 if ( toks[ 2 ][ i ] == '1' ) {
372 ( *sa )->sa_matchtypebitmap |= (1 << j);
376 free( ( char * ) toks );
377 sa = &(( *sa )->sa_next);
382 * Match types are last
384 sm = &( so->so_smlist );
385 while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
387 free_strarray( toks );
388 ldap_free_searchprefs( so );
389 return( LDAP_SEARCHPREF_ERR_SYNTAX );
391 if (( *sm = ( struct ldap_searchmatch * ) calloc( 1,
392 sizeof( struct ldap_searchmatch ))) == NULL ) {
393 free_strarray( toks );
394 ldap_free_searchprefs( so );
395 return( LDAP_SEARCHPREF_ERR_MEM );
397 ( *sm )->sm_matchprompt = toks[ 0 ];
398 ( *sm )->sm_filter = toks[ 1 ];
399 free( ( char * ) toks );
400 sm = &(( *sm )->sm_next );