3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
17 * searchpref.c: search preferences library routines for LDAP clients
18 * 17 May 1994 by Gordon Good
25 #include <ac/stdlib.h>
28 #include <ac/string.h>
30 #include <ac/unistd.h>
32 #ifdef HAVE_SYS_FILE_H
41 static void free_searchobj LDAP_P(( struct ldap_searchobj *so ));
42 static int read_next_searchobj LDAP_P(( char **bufp, ber_len_t *blenp,
43 struct ldap_searchobj **sop, int soversion ));
46 static const char *const sobjoptions[] = {
52 static const unsigned long sobjoptvals[] = {
53 LDAP_SEARCHOBJ_OPT_INTERNAL,
58 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
65 if (( fp = fopen( file, "r" )) == NULL ) {
66 return( LDAP_SEARCHPREF_ERR_FILE );
69 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
71 return( LDAP_SEARCHPREF_ERR_FILE );
76 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
78 return( LDAP_SEARCHPREF_ERR_FILE );
81 if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
83 return( LDAP_SEARCHPREF_ERR_MEM );
86 rlen = fread( buf, 1, (size_t)len, fp );
90 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
92 return( LDAP_SEARCHPREF_ERR_FILE );
95 rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
103 ldap_init_searchprefs_buf(
106 struct ldap_searchobj **solistp )
108 int rc = -1, version;
110 struct ldap_searchobj *prevso, *so;
112 *solistp = prevso = NULL;
114 if ( ldap_int_next_line_tokens( &buf, &buflen, &toks ) != 2 ||
115 strcasecmp( toks[ 0 ], "version" ) != 0 ) {
117 return( LDAP_SEARCHPREF_ERR_SYNTAX );
119 version = atoi( toks[ 1 ] );
121 if ( version != LDAP_SEARCHPREF_VERSION &&
122 version != LDAP_SEARCHPREF_VERSION_ZERO ) {
123 return( LDAP_SEARCHPREF_ERR_VERSION );
126 while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
127 version )) == 0 && so != NULL ) {
128 if ( prevso == NULL ) {
131 prevso->so_next = so;
137 ldap_free_searchprefs( *solistp );
146 ldap_free_searchprefs( struct ldap_searchobj *solist )
148 struct ldap_searchobj *so, *nextso;
150 if ( solist != NULL ) {
151 for ( so = solist; so != NULL; so = nextso ) {
152 nextso = so->so_next;
153 free_searchobj( so );
156 /* XXX XXX need to do some work here */
161 free_searchobj( struct ldap_searchobj *so )
164 if ( so->so_objtypeprompt != NULL ) {
165 LDAP_FREE( so->so_objtypeprompt );
167 if ( so->so_prompt != NULL ) {
168 LDAP_FREE( so->so_prompt );
170 if ( so->so_filterprefix != NULL ) {
171 LDAP_FREE( so->so_filterprefix );
173 if ( so->so_filtertag != NULL ) {
174 LDAP_FREE( so->so_filtertag );
176 if ( so->so_defaultselectattr != NULL ) {
177 LDAP_FREE( so->so_defaultselectattr );
179 if ( so->so_defaultselecttext != NULL ) {
180 LDAP_FREE( so->so_defaultselecttext );
182 if ( so->so_salist != NULL ) {
183 struct ldap_searchattr *sa, *nextsa;
184 for ( sa = so->so_salist; sa != NULL; sa = nextsa ) {
185 nextsa = sa->sa_next;
186 if ( sa->sa_attrlabel != NULL ) {
187 LDAP_FREE( sa->sa_attrlabel );
189 if ( sa->sa_attr != NULL ) {
190 LDAP_FREE( sa->sa_attr );
192 if ( sa->sa_selectattr != NULL ) {
193 LDAP_FREE( sa->sa_selectattr );
195 if ( sa->sa_selecttext != NULL ) {
196 LDAP_FREE( sa->sa_selecttext );
201 if ( so->so_smlist != NULL ) {
202 struct ldap_searchmatch *sm, *nextsm;
203 for ( sm = so->so_smlist; sm != NULL; sm = nextsm ) {
204 nextsm = sm->sm_next;
205 if ( sm->sm_matchprompt != NULL ) {
206 LDAP_FREE( sm->sm_matchprompt );
208 if ( sm->sm_filter != NULL ) {
209 LDAP_FREE( sm->sm_filter );
220 struct ldap_searchobj *
221 ldap_first_searchobj( struct ldap_searchobj *solist )
227 struct ldap_searchobj *
228 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
230 return( so == NULL ? so : so->so_next );
239 struct ldap_searchobj **sop,
244 struct ldap_searchobj *so;
245 struct ldap_searchattr **sa;
246 struct ldap_searchmatch **sm;
251 * Object type prompt comes first
253 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
255 return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
258 if (( so = (struct ldap_searchobj *)LDAP_CALLOC( 1,
259 sizeof( struct ldap_searchobj ))) == NULL ) {
261 return( LDAP_SEARCHPREF_ERR_MEM );
263 so->so_objtypeprompt = toks[ 0 ];
264 LDAP_FREE( (char *)toks );
267 * if this is post-version zero, options come next
269 if ( soversion > LDAP_SEARCHPREF_VERSION_ZERO ) {
270 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) < 1 ) {
272 ldap_free_searchprefs( so );
273 return( LDAP_SEARCHPREF_ERR_SYNTAX );
275 for ( i = 0; toks[ i ] != NULL; ++i ) {
276 for ( j = 0; sobjoptions[ j ] != NULL; ++j ) {
277 if ( strcasecmp( toks[ i ], sobjoptions[ j ] ) == 0 ) {
278 so->so_options |= sobjoptvals[ j ];
286 * "Fewer choices" prompt is next
288 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
290 ldap_free_searchprefs( so );
291 return( LDAP_SEARCHPREF_ERR_SYNTAX );
293 so->so_prompt = toks[ 0 ];
294 LDAP_FREE( (char *)toks );
297 * Filter prefix for "More Choices" searching is next
299 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
301 ldap_free_searchprefs( so );
302 return( LDAP_SEARCHPREF_ERR_SYNTAX );
304 so->so_filterprefix = toks[ 0 ];
305 LDAP_FREE( (char *)toks );
308 * "Fewer Choices" filter tag comes next
310 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
312 ldap_free_searchprefs( so );
313 return( LDAP_SEARCHPREF_ERR_SYNTAX );
315 so->so_filtertag = toks[ 0 ];
316 LDAP_FREE( (char *)toks );
319 * Selection (disambiguation) attribute comes next
321 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
323 ldap_free_searchprefs( so );
324 return( LDAP_SEARCHPREF_ERR_SYNTAX );
326 so->so_defaultselectattr = toks[ 0 ];
327 LDAP_FREE( (char *)toks );
330 * Label for selection (disambiguation) attribute
332 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
334 ldap_free_searchprefs( so );
335 return( LDAP_SEARCHPREF_ERR_SYNTAX );
337 so->so_defaultselecttext = toks[ 0 ];
338 LDAP_FREE( (char *)toks );
341 * Search scope is next
343 if (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) != 1 ) {
345 ldap_free_searchprefs( so );
346 return( LDAP_SEARCHPREF_ERR_SYNTAX );
348 if ( !strcasecmp(toks[ 0 ], "subtree" )) {
349 so->so_defaultscope = LDAP_SCOPE_SUBTREE;
350 } else if ( !strcasecmp(toks[ 0 ], "onelevel" )) {
351 so->so_defaultscope = LDAP_SCOPE_ONELEVEL;
352 } else if ( !strcasecmp(toks[ 0 ], "base" )) {
353 so->so_defaultscope = LDAP_SCOPE_BASE;
355 ldap_free_searchprefs( so );
356 return( LDAP_SEARCHPREF_ERR_SYNTAX );
362 * "More Choices" search option list comes next
364 sa = &( so->so_salist );
365 while (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) > 0 ) {
368 ldap_free_searchprefs( so );
369 return( LDAP_SEARCHPREF_ERR_SYNTAX );
371 if (( *sa = ( struct ldap_searchattr * ) LDAP_CALLOC( 1,
372 sizeof( struct ldap_searchattr ))) == NULL ) {
374 ldap_free_searchprefs( so );
375 return( LDAP_SEARCHPREF_ERR_MEM );
377 ( *sa )->sa_attrlabel = toks[ 0 ];
378 ( *sa )->sa_attr = toks[ 1 ];
379 ( *sa )->sa_selectattr = toks[ 3 ];
380 ( *sa )->sa_selecttext = toks[ 4 ];
381 /* Deal with bitmap */
382 ( *sa )->sa_matchtypebitmap = 0;
383 for ( i = strlen( toks[ 2 ] ) - 1, j = 0; i >= 0; i--, j++ ) {
384 if ( toks[ 2 ][ i ] == '1' ) {
385 ( *sa )->sa_matchtypebitmap |= (1 << j);
388 LDAP_FREE( toks[ 2 ] );
389 LDAP_FREE( ( char * ) toks );
390 sa = &(( *sa )->sa_next);
395 * Match types are last
397 sm = &( so->so_smlist );
398 while (( tokcnt = ldap_int_next_line_tokens( bufp, blenp, &toks )) > 0 ) {
401 ldap_free_searchprefs( so );
402 return( LDAP_SEARCHPREF_ERR_SYNTAX );
404 if (( *sm = ( struct ldap_searchmatch * ) LDAP_CALLOC( 1,
405 sizeof( struct ldap_searchmatch ))) == NULL ) {
407 ldap_free_searchprefs( so );
408 return( LDAP_SEARCHPREF_ERR_MEM );
410 ( *sm )->sm_matchprompt = toks[ 0 ];
411 ( *sm )->sm_filter = toks[ 1 ];
412 LDAP_FREE( ( char * ) toks );
413 sm = &(( *sm )->sm_next );