]> git.sur5r.net Git - openldap/blob - libraries/libldap/srchpref.c
aec72a2f9a38c01fc00c390b506b4343ac7c04f1
[openldap] / libraries / libldap / srchpref.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* Portions
6  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that this notice is preserved and that due credit is given
11  * to the University of Michigan at Ann Arbor. The name of the University
12  * may not be used to endorse or promote products derived from this
13  * software without specific prior written permission. This software
14  * is provided ``as is'' without express or implied warranty.
15  *
16  * searchpref.c:  search preferences library routines for LDAP clients
17  * 17 May 1994 by Gordon Good
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <ac/ctype.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28 #include <ac/unistd.h>
29
30 #ifdef HAVE_SYS_FILE_H
31 #include <sys/file.h>
32 #endif
33
34 #include "ldap-int.h"
35 #include "srchpref.h"
36
37 #include "ldap-int.h"
38
39 static void free_searchobj LDAP_P(( struct ldap_searchobj *so ));
40 static int read_next_searchobj LDAP_P(( char **bufp, long *blenp,
41         struct ldap_searchobj **sop, int soversion ));
42
43
44 static const char *const        sobjoptions[] = {
45     "internal",
46     NULL
47 };
48
49
50 static const unsigned long      sobjoptvals[] = {
51     LDAP_SEARCHOBJ_OPT_INTERNAL,
52 };
53
54
55 int
56 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
57 {
58     FILE        *fp;
59     char        *buf;
60     long        rlen, len;
61     int         rc, eof;
62
63     if (( fp = fopen( file, "r" )) == NULL ) {
64         return( LDAP_SEARCHPREF_ERR_FILE );
65     }
66
67     if ( fseek( fp, 0L, SEEK_END ) != 0 ) {     /* move to end to get len */
68         fclose( fp );
69         return( LDAP_SEARCHPREF_ERR_FILE );
70     }
71
72     len = ftell( fp );
73
74     if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {     /* back to start of file */
75         fclose( fp );
76         return( LDAP_SEARCHPREF_ERR_FILE );
77     }
78
79     if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
80         fclose( fp );
81         return( LDAP_SEARCHPREF_ERR_MEM );
82     }
83
84     rlen = fread( buf, 1, (size_t)len, fp );
85     eof = feof( fp );
86     fclose( fp );
87
88     if ( rlen != len && !eof ) {        /* error:  didn't get the whole file */
89         LDAP_FREE( buf );
90         return( LDAP_SEARCHPREF_ERR_FILE );
91     }
92
93     rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
94     LDAP_FREE( buf );
95
96     return( rc );
97 }
98
99
100 int
101 ldap_init_searchprefs_buf( char *buf, long buflen,
102         struct ldap_searchobj **solistp )
103 {
104     int                         rc = -1, version;
105     char                        **toks;
106     struct ldap_searchobj       *prevso, *so;
107
108     *solistp = prevso = NULL;
109
110     if ( next_line_tokens( &buf, &buflen, &toks ) != 2 ||
111             strcasecmp( toks[ 0 ], "version" ) != 0 ) {
112         free_strarray( toks );
113         return( LDAP_SEARCHPREF_ERR_SYNTAX );
114     }
115     version = atoi( toks[ 1 ] );
116     free_strarray( toks );
117     if ( version != LDAP_SEARCHPREF_VERSION &&
118             version != LDAP_SEARCHPREF_VERSION_ZERO ) {
119         return( LDAP_SEARCHPREF_ERR_VERSION );
120     }
121
122     while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
123             version )) == 0 && so != NULL ) {
124         if ( prevso == NULL ) {
125             *solistp = so;
126         } else {
127             prevso->so_next = so;
128         }
129         prevso = so;
130     }
131
132     if ( rc != 0 ) {
133         ldap_free_searchprefs( *solistp );
134     }
135
136     return( rc );
137 }
138             
139
140
141 void
142 ldap_free_searchprefs( struct ldap_searchobj *solist )
143 {
144     struct ldap_searchobj       *so, *nextso;
145
146     if ( solist != NULL ) {
147         for ( so = solist; so != NULL; so = nextso ) {
148             nextso = so->so_next;
149             free_searchobj( so );
150         }
151     }
152     /* XXX XXX need to do some work here */
153 }
154
155
156 static void
157 free_searchobj( struct ldap_searchobj *so )
158 {
159     if ( so != NULL ) {
160         if ( so->so_objtypeprompt != NULL ) {
161             LDAP_FREE(  so->so_objtypeprompt );
162         }
163         if ( so->so_prompt != NULL ) {
164             LDAP_FREE(  so->so_prompt );
165         }
166         if ( so->so_filterprefix != NULL ) {
167             LDAP_FREE(  so->so_filterprefix );
168         }
169         if ( so->so_filtertag != NULL ) {
170             LDAP_FREE(  so->so_filtertag );
171         }
172         if ( so->so_defaultselectattr != NULL ) {
173             LDAP_FREE(  so->so_defaultselectattr );
174         }
175         if ( so->so_defaultselecttext != NULL ) {
176             LDAP_FREE(  so->so_defaultselecttext );
177         }
178         if ( so->so_salist != NULL ) {
179             struct ldap_searchattr *sa, *nextsa;
180             for ( sa = so->so_salist; sa != NULL; sa = nextsa ) {
181                 nextsa = sa->sa_next;
182                 if ( sa->sa_attrlabel != NULL ) {
183                     LDAP_FREE( sa->sa_attrlabel );
184                 }
185                 if ( sa->sa_attr != NULL ) {
186                     LDAP_FREE( sa->sa_attr );
187                 }
188                 if ( sa->sa_selectattr != NULL ) {
189                     LDAP_FREE( sa->sa_selectattr );
190                 }
191                 if ( sa->sa_selecttext != NULL ) {
192                     LDAP_FREE( sa->sa_selecttext );
193                 }
194                 LDAP_FREE( sa );
195             }
196         }
197         if ( so->so_smlist != NULL ) {
198             struct ldap_searchmatch *sm, *nextsm;
199             for ( sm = so->so_smlist; sm != NULL; sm = nextsm ) {
200                 nextsm = sm->sm_next;
201                 if ( sm->sm_matchprompt != NULL ) {
202                     LDAP_FREE( sm->sm_matchprompt );
203                 }
204                 if ( sm->sm_filter != NULL ) {
205                     LDAP_FREE( sm->sm_filter );
206                 }
207                 LDAP_FREE( sm );
208             }
209         }
210         LDAP_FREE( so );
211     }
212 }
213
214
215
216 struct ldap_searchobj *
217 ldap_first_searchobj( struct ldap_searchobj *solist )
218 {
219     return( solist );
220 }
221
222
223 struct ldap_searchobj *
224 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
225 {
226     return( so == NULL ? so : so->so_next );
227 }
228
229
230
231 static int
232 read_next_searchobj( char **bufp, long *blenp, struct ldap_searchobj **sop,
233         int soversion )
234 {
235     int                         i, j, tokcnt;
236     char                        **toks;
237     struct ldap_searchobj       *so;
238     struct ldap_searchattr      **sa;
239     struct ldap_searchmatch     **sm;
240
241     *sop = NULL;
242
243     /*
244      * Object type prompt comes first
245      */
246     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
247         free_strarray( toks );
248         return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
249     }
250
251     if (( so = (struct ldap_searchobj *)LDAP_CALLOC( 1,
252             sizeof( struct ldap_searchobj ))) == NULL ) {
253         free_strarray( toks );
254         return(  LDAP_SEARCHPREF_ERR_MEM );
255     }
256     so->so_objtypeprompt = toks[ 0 ];
257     LDAP_FREE( (char *)toks );
258
259     /*
260      * if this is post-version zero, options come next
261      */
262     if ( soversion > LDAP_SEARCHPREF_VERSION_ZERO ) {
263         if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) < 1 ) {
264             free_strarray( toks );
265             ldap_free_searchprefs( so );
266             return( LDAP_SEARCHPREF_ERR_SYNTAX );
267         }
268         for ( i = 0; toks[ i ] != NULL; ++i ) {
269             for ( j = 0; sobjoptions[ j ] != NULL; ++j ) {
270                 if ( strcasecmp( toks[ i ], sobjoptions[ j ] ) == 0 ) {
271                     so->so_options |= sobjoptvals[ j ];
272                 }
273             }
274         }
275         free_strarray( toks );
276     }
277
278     /*
279      * "Fewer choices" prompt is next
280      */
281     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
282         free_strarray( toks );
283         ldap_free_searchprefs( so );
284         return( LDAP_SEARCHPREF_ERR_SYNTAX );
285     }
286     so->so_prompt = toks[ 0 ];
287     LDAP_FREE( (char *)toks );
288
289     /*
290      * Filter prefix for "More Choices" searching is next
291      */
292     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
293         free_strarray( toks );
294         ldap_free_searchprefs( so );
295         return( LDAP_SEARCHPREF_ERR_SYNTAX );
296     }
297     so->so_filterprefix = toks[ 0 ];
298     LDAP_FREE( (char *)toks );
299
300     /*
301      * "Fewer Choices" filter tag comes next
302      */
303     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
304         free_strarray( toks );
305         ldap_free_searchprefs( so );
306         return( LDAP_SEARCHPREF_ERR_SYNTAX );
307     }
308     so->so_filtertag = toks[ 0 ];
309     LDAP_FREE( (char *)toks );
310
311     /*
312      * Selection (disambiguation) attribute comes next
313      */
314     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
315         free_strarray( toks );
316         ldap_free_searchprefs( so );
317         return( LDAP_SEARCHPREF_ERR_SYNTAX );
318     }
319     so->so_defaultselectattr = toks[ 0 ];
320     LDAP_FREE( (char *)toks );
321
322     /*
323      * Label for selection (disambiguation) attribute
324      */
325     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
326         free_strarray( toks );
327         ldap_free_searchprefs( so );
328         return( LDAP_SEARCHPREF_ERR_SYNTAX );
329     }
330     so->so_defaultselecttext = toks[ 0 ];
331     LDAP_FREE( (char *)toks );
332
333     /*
334      * Search scope is next
335      */
336     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
337         free_strarray( toks );
338         ldap_free_searchprefs( so );
339         return( LDAP_SEARCHPREF_ERR_SYNTAX );
340     }
341     if ( !strcasecmp(toks[ 0 ], "subtree" )) {
342         so->so_defaultscope = LDAP_SCOPE_SUBTREE;
343     } else if ( !strcasecmp(toks[ 0 ], "onelevel" )) {
344         so->so_defaultscope = LDAP_SCOPE_ONELEVEL;
345     } else if ( !strcasecmp(toks[ 0 ], "base" )) {
346         so->so_defaultscope = LDAP_SCOPE_BASE;
347     } else {
348         ldap_free_searchprefs( so );
349         return( LDAP_SEARCHPREF_ERR_SYNTAX );
350     }
351     free_strarray( toks );
352
353
354     /*
355      * "More Choices" search option list comes next
356      */
357     sa = &( so->so_salist );
358     while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
359         if ( tokcnt < 5 ) {
360             free_strarray( toks );
361             ldap_free_searchprefs( so );
362             return( LDAP_SEARCHPREF_ERR_SYNTAX );
363         }
364         if (( *sa = ( struct ldap_searchattr * ) LDAP_CALLOC( 1,
365                 sizeof( struct ldap_searchattr ))) == NULL ) {
366             free_strarray( toks );
367             ldap_free_searchprefs( so );
368             return(  LDAP_SEARCHPREF_ERR_MEM );
369         }
370         ( *sa )->sa_attrlabel = toks[ 0 ];
371         ( *sa )->sa_attr = toks[ 1 ];
372         ( *sa )->sa_selectattr = toks[ 3 ];
373         ( *sa )->sa_selecttext = toks[ 4 ];
374         /* Deal with bitmap */
375         ( *sa )->sa_matchtypebitmap = 0;
376         for ( i = strlen( toks[ 2 ] ) - 1, j = 0; i >= 0; i--, j++ ) {
377             if ( toks[ 2 ][ i ] == '1' ) {
378                 ( *sa )->sa_matchtypebitmap |= (1 << j);
379             }
380         }
381         LDAP_FREE( toks[ 2 ] );
382         LDAP_FREE( ( char * ) toks );
383         sa = &(( *sa )->sa_next);
384     }
385     *sa = NULL;
386
387     /*
388      * Match types are last
389      */
390     sm = &( so->so_smlist );
391     while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
392         if ( tokcnt < 2 ) {
393             free_strarray( toks );
394             ldap_free_searchprefs( so );
395             return( LDAP_SEARCHPREF_ERR_SYNTAX );
396         }
397         if (( *sm = ( struct ldap_searchmatch * ) LDAP_CALLOC( 1,
398                 sizeof( struct ldap_searchmatch ))) == NULL ) {
399             free_strarray( toks );
400             ldap_free_searchprefs( so );
401             return(  LDAP_SEARCHPREF_ERR_MEM );
402         }
403         ( *sm )->sm_matchprompt = toks[ 0 ];
404         ( *sm )->sm_filter = toks[ 1 ];
405         LDAP_FREE( ( char * ) toks );
406         sm = &(( *sm )->sm_next );
407     }
408     *sm = NULL;
409
410     *sop = so;
411     return( 0 );
412 }