]> git.sur5r.net Git - openldap/blob - libraries/libldap/getfilter.c
e6570187ccb74dcd48b43f952e0e5a477f909cd3
[openldap] / libraries / libldap / getfilter.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1993 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  getfilter.c -- optional add-on to libldap
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/errno.h>
20 #include <ac/regex.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/unistd.h>
24
25 #ifdef HAVE_SYS_FILE_H
26 #include <sys/file.h>
27 #endif
28
29 #include "ldap-int.h"
30
31 static int break_into_words LDAP_P((
32         /* LDAP_CONST */ char *str,
33         LDAP_CONST char *delims,
34         char ***wordsp ));
35
36 #define FILT_MAX_LINE_LEN       1024
37
38 LDAPFiltDesc *
39 ldap_init_getfilter( LDAP_CONST char *fname )
40 {
41     FILE                *fp;
42     char                *buf;
43     long                rlen, len;
44     int                 eof;
45     LDAPFiltDesc        *lfdp;
46
47     if (( fp = fopen( fname, "r" )) == NULL ) {
48         return( NULL );
49     }
50
51     if ( fseek( fp, 0L, SEEK_END ) != 0 ) {     /* move to end to get len */
52         fclose( fp );
53         return( NULL );
54     }
55
56     len = ftell( fp );
57
58     if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {     /* back to start of file */
59         fclose( fp );
60         return( NULL );
61     }
62
63     if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
64         fclose( fp );
65         return( NULL );
66     }
67
68     rlen = fread( buf, 1, (size_t)len, fp );
69     eof = feof( fp );
70     fclose( fp );
71
72     if ( rlen != len && !eof ) {        /* error:  didn't get the whole file */
73         LDAP_FREE( buf );
74         return( NULL );
75     }
76
77
78     lfdp = ldap_init_getfilter_buf( buf, rlen );
79     LDAP_FREE( buf );
80
81     return( lfdp );
82 }
83
84
85 LDAPFiltDesc *
86 ldap_init_getfilter_buf( char *buf, ber_len_t buflen )
87 {
88     LDAPFiltDesc        *lfdp;
89     LDAPFiltList        *flp, *nextflp;
90     LDAPFiltInfo        *fip, *nextfip;
91     char                        *tag, **tok;
92     int                         tokcnt, i;
93         int                             rc;
94         regex_t                 re;
95
96     if (( lfdp = (LDAPFiltDesc *)LDAP_CALLOC( 1, sizeof( LDAPFiltDesc))) == NULL ) {
97         return( NULL );
98     }
99
100     flp = nextflp = NULL;
101     fip = NULL;
102     tag = NULL;
103
104     while ( buflen > 0 && ( tokcnt = ldap_int_next_line_tokens( &buf, &buflen, &tok ))
105             > 0 ) {
106
107         switch( tokcnt ) {
108         case 1:         /* tag line */
109             if ( tag != NULL ) {
110                 LDAP_FREE( tag );
111             }
112             tag = tok[ 0 ];
113             LDAP_FREE( tok );
114             break;
115         case 4:
116         case 5:         /* start of filter info. list */
117             if (( nextflp = (LDAPFiltList *)LDAP_CALLOC( 1, sizeof( LDAPFiltList )))
118                     == NULL ) {
119                 ldap_getfilter_free( lfdp );
120                 return( NULL );
121             }
122             nextflp->lfl_tag = LDAP_STRDUP( tag );
123             nextflp->lfl_pattern = tok[ 0 ];
124             if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
125                 char error[512];
126                 regerror(rc, &re, error, sizeof(error));
127                 ldap_getfilter_free( lfdp );
128                 Debug( LDAP_DEBUG_ANY, "ldap_init_get_filter_buf: "
129                         "bad regular expression %s, %s\n",
130                         nextflp->lfl_pattern, error, 0 );
131                 errno = EINVAL;
132                 LDAP_VFREE( tok );
133                 return( NULL );
134             }
135                 regfree(&re);
136                 
137             nextflp->lfl_delims = tok[ 1 ];
138             nextflp->lfl_ilist = NULL;
139             nextflp->lfl_next = NULL;
140             if ( flp == NULL ) {        /* first one */
141                 lfdp->lfd_filtlist = nextflp;
142             } else {
143                 flp->lfl_next = nextflp;
144             }
145             flp = nextflp;
146             fip = NULL;
147             for ( i = 2; i < 5; ++i ) {
148                 tok[ i - 2 ] = tok[ i ];
149             }
150             /* fall through */
151
152         case 2:
153         case 3:         /* filter, desc, and optional search scope */
154             if ( nextflp != NULL ) { /* add to info list */
155                 if (( nextfip = (LDAPFiltInfo *)LDAP_CALLOC( 1,
156                         sizeof( LDAPFiltInfo ))) == NULL ) {
157                     ldap_getfilter_free( lfdp );
158                     LDAP_VFREE( tok );
159                     return( NULL );
160                 }
161                 if ( fip == NULL ) {    /* first one */
162                     nextflp->lfl_ilist = nextfip;
163                 } else {
164                     fip->lfi_next = nextfip;
165                 }
166                 fip = nextfip;
167                 nextfip->lfi_next = NULL;
168                 nextfip->lfi_filter = tok[ 0 ];
169                 nextfip->lfi_desc = tok[ 1 ];
170                 if ( tok[ 2 ] != NULL ) {
171                     if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
172                         nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
173                     } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
174                         nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
175                     } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
176                         nextfip->lfi_scope = LDAP_SCOPE_BASE;
177                     } else {
178                         LDAP_VFREE( tok );
179                         ldap_getfilter_free( lfdp );
180                         errno = EINVAL;
181                         return( NULL );
182                     }
183                     LDAP_FREE( tok[ 2 ] );
184                     tok[ 2 ] = NULL;
185                 } else {
186                     nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;    /* default */
187                 }
188                 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
189                         strchr( tok[ 0 ], '~' ) == NULL );
190                 LDAP_FREE( tok );
191             }
192             break;
193
194         default:
195             LDAP_VFREE( tok );
196             ldap_getfilter_free( lfdp );
197             errno = EINVAL;
198             return( NULL );
199         }
200     }
201
202     if ( tag != NULL ) {
203         LDAP_FREE( tag );
204     }
205
206     return( lfdp );
207 }
208
209
210 void
211 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, LDAP_CONST char *prefix, LDAP_CONST char *suffix )
212 {
213     if ( lfdp->lfd_filtprefix != NULL ) {
214         LDAP_FREE( lfdp->lfd_filtprefix );
215     }
216     lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : LDAP_STRDUP( prefix );
217
218     if ( lfdp->lfd_filtsuffix != NULL ) {
219         LDAP_FREE( lfdp->lfd_filtsuffix );
220     }
221     lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : LDAP_STRDUP( suffix );
222 }
223
224
225 LDAPFiltInfo *
226 ldap_getfirstfilter(
227         LDAPFiltDesc *lfdp,
228         /* LDAP_CONST */ char *tagpat,
229         /* LDAP_CONST */ char *value )
230 {
231     LDAPFiltList        *flp;
232         int                             rc;
233         regex_t                 re;
234
235     if ( lfdp->lfd_curvalcopy != NULL ) {
236         LDAP_FREE( lfdp->lfd_curvalcopy );
237         LDAP_FREE( lfdp->lfd_curvalwords );
238     }
239
240     lfdp->lfd_curval = value;
241     lfdp->lfd_curfip = NULL;
242
243         for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
244                 /* compile tagpat, continue if we fail */
245                 if (regcomp(&re, tagpat, REG_EXTENDED|REG_NOSUB) != 0)
246                         continue;
247
248                 /* match tagpattern and tag, continue if we fail */
249                 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
250                 regfree(&re);
251                 if (rc != 0)
252                         continue;
253
254                 /* compile flp->ifl_pattern, continue if we fail */
255                 if (regcomp(&re, flp->lfl_pattern, REG_EXTENDED|REG_NOSUB) != 0)
256                         continue;
257
258                 /* match ifl_pattern and lfd_curval, continue if we fail */
259                 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
260                 regfree(&re);
261                 if (rc != 0)
262                         continue;
263
264                 /* we successfully compiled both patterns and matched both values */
265                 lfdp->lfd_curfip = flp->lfl_ilist;
266                 break;
267     }
268
269     if ( lfdp->lfd_curfip == NULL ) {
270         return( NULL );
271     }
272
273     if (( lfdp->lfd_curvalcopy = LDAP_STRDUP( value )) == NULL ) {
274         return( NULL );
275     }
276
277     if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
278                 &lfdp->lfd_curvalwords ) < 0 ) {
279         LDAP_FREE( lfdp->lfd_curvalcopy );
280         lfdp->lfd_curvalcopy = NULL;
281         return( NULL );
282     }
283
284     return( ldap_getnextfilter( lfdp ));
285 }
286
287
288 LDAPFiltInfo *
289 ldap_getnextfilter( LDAPFiltDesc *lfdp )
290 {
291     LDAPFiltInfo        *fip;
292
293     fip = lfdp->lfd_curfip;
294
295     if ( fip == NULL ) {
296         return( NULL );
297     }
298
299     lfdp->lfd_curfip = fip->lfi_next;
300
301     ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
302             lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
303             lfdp->lfd_curval, lfdp->lfd_curvalwords );
304     lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
305     lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
306     lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
307     lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
308
309     return( &lfdp->lfd_retfi );
310 }
311
312
313 void
314 ldap_build_filter(
315         char *filtbuf,
316         ber_len_t buflen,
317         LDAP_CONST char *pattern,
318         LDAP_CONST char *prefix,
319         LDAP_CONST char *suffix,
320         LDAP_CONST char *attr,
321         LDAP_CONST char *value,
322         char **valwords )
323 {
324         const char *p;
325         char *f;
326         size_t  slen;
327         int     i, wordcount, wordnum, endwordnum;
328         
329         if ( valwords == NULL ) {
330             wordcount = 0;
331         } else {
332             for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
333                 ;
334             }
335         }
336
337         f = filtbuf;
338
339         if ( prefix != NULL ) {
340             strcpy( f, prefix );
341             f += strlen( prefix );
342         }
343
344         for ( p = pattern; *p != '\0'; ++p ) {
345             if ( *p == '%' ) {
346                 ++p;
347                 if ( *p == 'v' ) {
348                     if ( LDAP_DIGIT( (unsigned char) p[1] )) {
349                         ++p;
350                         wordnum = *p - '1';
351                         if ( *(p+1) == '-' ) {
352                             ++p;
353                             if ( LDAP_DIGIT( (unsigned char) p[1] )) {
354                                 ++p;
355                                 endwordnum = *p - '1';  /* e.g., "%v2-4" */
356                                 if ( endwordnum > wordcount - 1 ) {
357                                     endwordnum = wordcount - 1;
358                                 }
359                             } else {
360                                 endwordnum = wordcount - 1;  /* e.g., "%v2-" */
361                             }
362                         } else {
363                             endwordnum = wordnum;       /* e.g., "%v2" */
364                         }
365
366                         if ( wordcount > 0 ) {
367                             for ( i = wordnum; i <= endwordnum; ++i ) {
368                                 if ( i > wordnum ) {  /* add blank btw words */
369                                     *f++ = ' ';
370                                 }
371                                 slen = strlen( valwords[ i ] );
372                                 AC_MEMCPY( f, valwords[ i ], slen );
373                                 f += slen;
374                             }
375                         }
376                     } else if ( *(p+1) == '$' ) {
377                         ++p;
378                         if ( wordcount > 0 ) {
379                             wordnum = wordcount - 1;
380                             slen = strlen( valwords[ wordnum ] );
381                             AC_MEMCPY( f, valwords[ wordnum ], slen );
382                             f += slen;
383                         }
384                     } else if ( value != NULL ) {
385                         slen = strlen( value );
386                         AC_MEMCPY( f, value, slen );
387                         f += slen;
388                     }
389                 } else if ( *p == 'a' && attr != NULL ) {
390                     slen = strlen( attr );
391                     AC_MEMCPY( f, attr, slen );
392                     f += slen;
393                 } else {
394                     *f++ = *p;
395                 }
396             } else {
397                 *f++ = *p;
398             }
399                 
400             if ( (size_t) (f - filtbuf) > buflen ) {
401                 /* sanity check */
402                 --f;
403                 break;
404             }
405         }
406
407         if ( suffix != NULL && ( (size_t) (f - filtbuf) < buflen ) )
408         {
409             strcpy( f, suffix );
410         } else {
411             *f = '\0';
412         }
413 }
414
415
416 static int
417 break_into_words( /* LDAP_CONST */ char *str, LDAP_CONST char *delims, char ***wordsp )
418 {
419     char        *word, **words;
420     int         count;
421     char        *tok_r; 
422
423     if (( words = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
424         return( -1 );
425     }
426     count = 0;
427     words[ count ] = NULL;
428
429     word = ldap_pvt_strtok( str, delims, &tok_r );
430     while ( word != NULL ) {
431         if (( words = (char **)LDAP_REALLOC( words,
432                 ( count + 2 ) * sizeof( char * ))) == NULL ) {
433             return( -1 );
434         }
435
436         words[ count ] = word;
437         words[ ++count ] = NULL;
438         word = ldap_pvt_strtok( NULL, delims, &tok_r );
439     }
440         
441     *wordsp = words;
442     return( count );
443 }