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