2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1993 Regents of the University of Michigan.
9 * getfilter.c -- optional add-on to libldap
16 #include <ac/stdlib.h>
21 #include <ac/string.h>
23 #include <ac/unistd.h>
25 #ifdef HAVE_SYS_FILE_H
31 static int break_into_words LDAP_P((
32 /* LDAP_CONST */ char *str,
33 LDAP_CONST char *delims,
36 #define FILT_MAX_LINE_LEN 1024
39 ldap_init_getfilter( LDAP_CONST char *fname )
47 if (( fp = fopen( fname, "r" )) == NULL ) {
51 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
58 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
63 if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
68 rlen = fread( buf, 1, (size_t)len, fp );
72 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
78 lfdp = ldap_init_getfilter_buf( buf, rlen );
86 ldap_init_getfilter_buf( char *buf, ber_len_t buflen )
89 LDAPFiltList *flp, *nextflp;
90 LDAPFiltInfo *fip, *nextfip;
96 if (( lfdp = (LDAPFiltDesc *)LDAP_CALLOC( 1, sizeof( LDAPFiltDesc))) == NULL ) {
100 flp = nextflp = NULL;
104 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
108 case 1: /* tag line */
116 case 5: /* start of filter info. list */
117 if (( nextflp = (LDAPFiltList *)LDAP_CALLOC( 1, sizeof( LDAPFiltList )))
119 ldap_getfilter_free( lfdp );
122 nextflp->lfl_tag = LDAP_STRDUP( tag );
123 nextflp->lfl_pattern = tok[ 0 ];
124 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
127 regerror(rc, &re, error, sizeof(error));
128 ldap_getfilter_free( lfdp );
129 fprintf( stderr, "bad regular expression %s, %s\n",
130 nextflp->lfl_pattern, error );
132 #endif /* LDAP_LIBUI */
133 free_strarray( tok );
138 nextflp->lfl_delims = tok[ 1 ];
139 nextflp->lfl_ilist = NULL;
140 nextflp->lfl_next = NULL;
141 if ( flp == NULL ) { /* first one */
142 lfdp->lfd_filtlist = nextflp;
144 flp->lfl_next = nextflp;
148 for ( i = 2; i < 5; ++i ) {
149 tok[ i - 2 ] = tok[ i ];
154 case 3: /* filter, desc, and optional search scope */
155 if ( nextflp != NULL ) { /* add to info list */
156 if (( nextfip = (LDAPFiltInfo *)LDAP_CALLOC( 1,
157 sizeof( LDAPFiltInfo ))) == NULL ) {
158 ldap_getfilter_free( lfdp );
159 free_strarray( tok );
162 if ( fip == NULL ) { /* first one */
163 nextflp->lfl_ilist = nextfip;
165 fip->lfi_next = nextfip;
168 nextfip->lfi_next = NULL;
169 nextfip->lfi_filter = tok[ 0 ];
170 nextfip->lfi_desc = tok[ 1 ];
171 if ( tok[ 2 ] != NULL ) {
172 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
173 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
174 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
175 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
176 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
177 nextfip->lfi_scope = LDAP_SCOPE_BASE;
179 free_strarray( tok );
180 ldap_getfilter_free( lfdp );
184 LDAP_FREE( tok[ 2 ] );
187 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
189 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
190 strchr( tok[ 0 ], '~' ) == NULL );
196 free_strarray( tok );
197 ldap_getfilter_free( lfdp );
212 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, LDAP_CONST char *prefix, LDAP_CONST char *suffix )
214 if ( lfdp->lfd_filtprefix != NULL ) {
215 LDAP_FREE( lfdp->lfd_filtprefix );
217 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : LDAP_STRDUP( prefix );
219 if ( lfdp->lfd_filtsuffix != NULL ) {
220 LDAP_FREE( lfdp->lfd_filtsuffix );
222 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : LDAP_STRDUP( suffix );
229 /* LDAP_CONST */ char *tagpat,
230 /* LDAP_CONST */ char *value )
236 if ( lfdp->lfd_curvalcopy != NULL ) {
237 LDAP_FREE( lfdp->lfd_curvalcopy );
238 LDAP_FREE( lfdp->lfd_curvalwords );
241 lfdp->lfd_curval = value;
242 lfdp->lfd_curfip = NULL;
244 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
245 /* compile tagpat, continue if we fail */
246 if (regcomp(&re, tagpat, 0) != 0)
249 /* match tagpatern and tag, continue if we fail */
250 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
255 /* compile flp->ifl_pattern, continue if we fail */
256 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
259 /* match ifl_pattern and lfd_curval, continue if we fail */
260 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
265 /* we successfully compiled both patterns and matched both values */
266 lfdp->lfd_curfip = flp->lfl_ilist;
270 if ( lfdp->lfd_curfip == NULL ) {
274 if (( lfdp->lfd_curvalcopy = LDAP_STRDUP( value )) == NULL ) {
278 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
279 &lfdp->lfd_curvalwords ) < 0 ) {
280 LDAP_FREE( lfdp->lfd_curvalcopy );
281 lfdp->lfd_curvalcopy = NULL;
285 return( ldap_getnextfilter( lfdp ));
290 ldap_getnextfilter( LDAPFiltDesc *lfdp )
294 fip = lfdp->lfd_curfip;
300 lfdp->lfd_curfip = fip->lfi_next;
302 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
303 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
304 lfdp->lfd_curval, lfdp->lfd_curvalwords );
305 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
306 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
307 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
308 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
310 return( &lfdp->lfd_retfi );
318 LDAP_CONST char *pattern,
319 LDAP_CONST char *prefix,
320 LDAP_CONST char *suffix,
321 LDAP_CONST char *attr,
322 LDAP_CONST char *value,
328 int i, wordcount, wordnum, endwordnum;
330 if ( valwords == NULL ) {
333 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
340 if ( prefix != NULL ) {
342 f += strlen( prefix );
345 for ( p = pattern; *p != '\0'; ++p ) {
349 if ( isdigit( (unsigned char) p[1] )) {
352 if ( *(p+1) == '-' ) {
354 if ( isdigit( (unsigned char) p[1] )) {
356 endwordnum = *p - '1'; /* e.g., "%v2-4" */
357 if ( endwordnum > wordcount - 1 ) {
358 endwordnum = wordcount - 1;
361 endwordnum = wordcount - 1; /* e.g., "%v2-" */
364 endwordnum = wordnum; /* e.g., "%v2" */
367 if ( wordcount > 0 ) {
368 for ( i = wordnum; i <= endwordnum; ++i ) {
369 if ( i > wordnum ) { /* add blank btw words */
372 slen = strlen( valwords[ i ] );
373 SAFEMEMCPY( f, valwords[ i ], slen );
377 } else if ( *(p+1) == '$' ) {
379 if ( wordcount > 0 ) {
380 wordnum = wordcount - 1;
381 slen = strlen( valwords[ wordnum ] );
382 SAFEMEMCPY( f, valwords[ wordnum ], slen );
385 } else if ( value != NULL ) {
386 slen = strlen( value );
387 SAFEMEMCPY( f, value, slen );
390 } else if ( *p == 'a' && attr != NULL ) {
391 slen = strlen( attr );
392 SAFEMEMCPY( f, attr, slen );
401 if ( (size_t) (f - filtbuf) > buflen ) {
408 if ( suffix != NULL && ( (size_t) (f - filtbuf) < buflen ) )
418 break_into_words( /* LDAP_CONST */ char *str, LDAP_CONST char *delims, char ***wordsp )
424 if (( words = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
428 words[ count ] = NULL;
430 word = ldap_pvt_strtok( str, delims, &tok_r );
431 while ( word != NULL ) {
432 if (( words = (char **)LDAP_REALLOC( words,
433 ( count + 2 ) * sizeof( char * ))) == NULL ) {
437 words[ count ] = word;
438 words[ ++count ] = NULL;
439 word = ldap_pvt_strtok( NULL, delims, &tok_r );