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