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