]> git.sur5r.net Git - openldap/blob - libraries/libldap/srchpref.c
Initial revision
[openldap] / libraries / libldap / srchpref.c
1 /*
2  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  *
12  * searchpref.c:  search preferences library routines for LDAP clients
13  * 17 May 1994 by Gordon Good
14  */
15
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #ifdef MACOS
21 #include "macos.h"
22 #else /* MACOS */
23 #ifdef DOS
24 #include <malloc.h>
25 #include "msdos.h"
26 #else /* DOS */
27 #include <sys/types.h>
28 #include <sys/file.h>
29 #ifndef VMS
30 #include <unistd.h>
31 #endif /* VMS */
32 #endif /* DOS */
33 #endif /* MACOS */
34
35 #include "lber.h"
36 #include "ldap.h"
37 #include "srchpref.h"
38
39 #ifndef NEEDPROTOS
40 int next_line_tokens();
41 void free_strarray();
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 */
51
52
53 static char             *sobjoptions[] = {
54     "internal",
55     NULL
56 };
57
58
59 static unsigned long    sobjoptvals[] = {
60     LDAP_SEARCHOBJ_OPT_INTERNAL,
61 };
62
63
64 int
65 ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
66 {
67     FILE        *fp;
68     char        *buf;
69     long        rlen, len;
70     int         rc, eof;
71
72     if (( fp = fopen( file, "r" )) == NULL ) {
73         return( LDAP_SEARCHPREF_ERR_FILE );
74     }
75
76     if ( fseek( fp, 0L, SEEK_END ) != 0 ) {     /* move to end to get len */
77         fclose( fp );
78         return( LDAP_SEARCHPREF_ERR_FILE );
79     }
80
81     len = ftell( fp );
82
83     if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {     /* back to start of file */
84         fclose( fp );
85         return( LDAP_SEARCHPREF_ERR_FILE );
86     }
87
88     if (( buf = malloc( (size_t)len )) == NULL ) {
89         fclose( fp );
90         return( LDAP_SEARCHPREF_ERR_MEM );
91     }
92
93     rlen = fread( buf, 1, (size_t)len, fp );
94     eof = feof( fp );
95     fclose( fp );
96
97     if ( rlen != len && !eof ) {        /* error:  didn't get the whole file */
98         free( buf );
99         return( LDAP_SEARCHPREF_ERR_FILE );
100     }
101
102     rc = ldap_init_searchprefs_buf( buf, rlen, solistp );
103     free( buf );
104
105     return( rc );
106 }
107
108
109 int
110 ldap_init_searchprefs_buf( char *buf, long buflen,
111         struct ldap_searchobj **solistp )
112 {
113     int                         rc, version;
114     char                        **toks;
115     struct ldap_searchobj       *prevso, *so;
116
117     *solistp = prevso = NULLSEARCHOBJ;
118
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 );
123     }
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 );
129     }
130
131     while ( buflen > 0 && ( rc = read_next_searchobj( &buf, &buflen, &so,
132             version )) == 0 && so != NULLSEARCHOBJ ) {
133         if ( prevso == NULLSEARCHOBJ ) {
134             *solistp = so;
135         } else {
136             prevso->so_next = so;
137         }
138         prevso = so;
139     }
140
141     if ( rc != 0 ) {
142         ldap_free_searchprefs( *solistp );
143     }
144
145     return( rc );
146 }
147             
148
149
150 void
151 ldap_free_searchprefs( struct ldap_searchobj *solist )
152 {
153     struct ldap_searchobj       *so, *nextso;
154
155     if ( solist != NULL ) {
156         for ( so = solist; so != NULL; so = nextso ) {
157             nextso = so->so_next;
158             free_searchobj( so );
159         }
160     }
161     /* XXX XXX need to do some work here */
162 }
163
164
165 static void
166 free_searchobj( struct ldap_searchobj *so )
167 {
168     if ( so != NULL ) {
169         if ( so->so_objtypeprompt != NULL ) {
170             free(  so->so_objtypeprompt );
171         }
172         if ( so->so_prompt != NULL ) {
173             free(  so->so_prompt );
174         }
175         if ( so->so_filterprefix != NULL ) {
176             free(  so->so_filterprefix );
177         }
178         if ( so->so_filtertag != NULL ) {
179             free(  so->so_filtertag );
180         }
181         if ( so->so_defaultselectattr != NULL ) {
182             free(  so->so_defaultselectattr );
183         }
184         if ( so->so_defaultselecttext != NULL ) {
185             free(  so->so_defaultselecttext );
186         }
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 );
193                 }
194                 if ( sa->sa_attr != NULL ) {
195                     free( sa->sa_attr );
196                 }
197                 if ( sa->sa_selectattr != NULL ) {
198                     free( sa->sa_selectattr );
199                 }
200                 if ( sa->sa_selecttext != NULL ) {
201                     free( sa->sa_selecttext );
202                 }
203                 free( sa );
204             }
205         }
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 );
212                 }
213                 if ( sm->sm_filter != NULL ) {
214                     free( sm->sm_filter );
215                 }
216                 free( sm );
217             }
218         }
219         free( so );
220     }
221 }
222
223
224
225 struct ldap_searchobj *
226 ldap_first_searchobj( struct ldap_searchobj *solist )
227 {
228     return( solist );
229 }
230
231
232 struct ldap_searchobj *
233 ldap_next_searchobj( struct ldap_searchobj *solist, struct ldap_searchobj *so )
234 {
235     return( so == NULLSEARCHOBJ ? so : so->so_next );
236 }
237
238
239
240 static int
241 read_next_searchobj( char **bufp, long *blenp, struct ldap_searchobj **sop,
242         int soversion )
243 {
244     int                         i, j, tokcnt;
245     char                        **toks;
246     struct ldap_searchobj       *so;
247     struct ldap_searchattr      **sa;
248     struct ldap_searchmatch     **sm;
249
250     *sop = NULL;
251
252     /*
253      * Object type prompt comes first
254      */
255     if (( tokcnt = next_line_tokens( bufp, blenp, &toks )) != 1 ) {
256         free_strarray( toks );
257         return( tokcnt == 0 ? 0 : LDAP_SEARCHPREF_ERR_SYNTAX );
258     }
259
260     if (( so = (struct ldap_searchobj *)calloc( 1,
261             sizeof( struct ldap_searchobj ))) == NULL ) {
262         free_strarray( toks );
263         return(  LDAP_SEARCHPREF_ERR_MEM );
264     }
265     so->so_objtypeprompt = toks[ 0 ];
266     free( (char *)toks );
267
268     /*
269      * if this is post-version zero, options come next
270      */
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 );
276         }
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 ];
281                 }
282             }
283         }
284         free_strarray( toks );
285     }
286
287     /*
288      * "Fewer choices" prompt is next
289      */
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 );
294     }
295     so->so_prompt = toks[ 0 ];
296     free( (char *)toks );
297
298     /*
299      * Filter prefix for "More Choices" searching is next
300      */
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 );
305     }
306     so->so_filterprefix = toks[ 0 ];
307     free( (char *)toks );
308
309     /*
310      * "Fewer Choices" filter tag comes next
311      */
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 );
316     }
317     so->so_filtertag = toks[ 0 ];
318     free( (char *)toks );
319
320     /*
321      * Selection (disambiguation) attribute comes next
322      */
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 );
327     }
328     so->so_defaultselectattr = toks[ 0 ];
329     free( (char *)toks );
330
331     /*
332      * Label for selection (disambiguation) attribute
333      */
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 );
338     }
339     so->so_defaultselecttext = toks[ 0 ];
340     free( (char *)toks );
341
342     /*
343      * Search scope is next
344      */
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 );
349     }
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;
356     } else {
357         ldap_free_searchprefs( so );
358         return( LDAP_SEARCHPREF_ERR_SYNTAX );
359     }
360     free_strarray( toks );
361
362
363     /*
364      * "More Choices" search option list comes next
365      */
366     sa = &( so->so_salist );
367     while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
368         if ( tokcnt < 5 ) {
369             free_strarray( toks );
370             ldap_free_searchprefs( so );
371             return( LDAP_SEARCHPREF_ERR_SYNTAX );
372         }
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 );
378         }
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);
388             }
389         }
390         free( toks[ 2 ] );
391         free( ( char * ) toks );
392         sa = &(( *sa )->sa_next);
393     }
394     *sa = NULL;
395
396     /*
397      * Match types are last
398      */
399     sm = &( so->so_smlist );
400     while (( tokcnt = next_line_tokens( bufp, blenp, &toks )) > 0 ) {
401         if ( tokcnt < 2 ) {
402             free_strarray( toks );
403             ldap_free_searchprefs( so );
404             return( LDAP_SEARCHPREF_ERR_SYNTAX );
405         }
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 );
411         }
412         ( *sm )->sm_matchprompt = toks[ 0 ];
413         ( *sm )->sm_filter = toks[ 1 ];
414         free( ( char * ) toks );
415         sm = &(( *sm )->sm_next );
416     }
417     *sm = NULL;
418
419     *sop = so;
420     return( 0 );
421 }