3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993 Regents of the University of Michigan.
10 * getfilter.c -- optional add-on to libldap
17 #include <ac/stdlib.h>
22 #include <ac/string.h>
24 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_FILE_H
32 static int break_into_words LDAP_P((
33 /* LDAP_CONST */ char *str,
34 LDAP_CONST char *delims,
37 #define FILT_MAX_LINE_LEN 1024
40 ldap_init_getfilter( LDAP_CONST char *fname )
48 if (( fp = fopen( fname, "r" )) == NULL ) {
52 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
59 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
64 if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
69 rlen = fread( buf, 1, (size_t)len, fp );
73 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
79 lfdp = ldap_init_getfilter_buf( buf, rlen );
87 ldap_init_getfilter_buf( char *buf, ber_len_t buflen )
90 LDAPFiltList *flp, *nextflp;
91 LDAPFiltInfo *fip, *nextfip;
97 if (( lfdp = (LDAPFiltDesc *)LDAP_CALLOC( 1, sizeof( LDAPFiltDesc))) == NULL ) {
101 flp = nextflp = NULL;
105 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
109 case 1: /* tag line */
117 case 5: /* start of filter info. list */
118 if (( nextflp = (LDAPFiltList *)LDAP_CALLOC( 1, sizeof( LDAPFiltList )))
120 ldap_getfilter_free( lfdp );
123 nextflp->lfl_tag = LDAP_STRDUP( tag );
124 nextflp->lfl_pattern = tok[ 0 ];
125 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
128 regerror(rc, &re, error, sizeof(error));
129 ldap_getfilter_free( lfdp );
130 fprintf( stderr, "bad regular expression %s, %s\n",
131 nextflp->lfl_pattern, error );
133 #endif /* LDAP_LIBUI */
134 free_strarray( tok );
139 nextflp->lfl_delims = tok[ 1 ];
140 nextflp->lfl_ilist = NULL;
141 nextflp->lfl_next = NULL;
142 if ( flp == NULL ) { /* first one */
143 lfdp->lfd_filtlist = nextflp;
145 flp->lfl_next = nextflp;
149 for ( i = 2; i < 5; ++i ) {
150 tok[ i - 2 ] = tok[ i ];
155 case 3: /* filter, desc, and optional search scope */
156 if ( nextflp != NULL ) { /* add to info list */
157 if (( nextfip = (LDAPFiltInfo *)LDAP_CALLOC( 1,
158 sizeof( LDAPFiltInfo ))) == NULL ) {
159 ldap_getfilter_free( lfdp );
160 free_strarray( tok );
163 if ( fip == NULL ) { /* first one */
164 nextflp->lfl_ilist = nextfip;
166 fip->lfi_next = nextfip;
169 nextfip->lfi_next = NULL;
170 nextfip->lfi_filter = tok[ 0 ];
171 nextfip->lfi_desc = tok[ 1 ];
172 if ( tok[ 2 ] != NULL ) {
173 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
174 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
175 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
176 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
177 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
178 nextfip->lfi_scope = LDAP_SCOPE_BASE;
180 free_strarray( tok );
181 ldap_getfilter_free( lfdp );
185 LDAP_FREE( tok[ 2 ] );
188 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
190 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
191 strchr( tok[ 0 ], '~' ) == NULL );
197 free_strarray( tok );
198 ldap_getfilter_free( lfdp );
213 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, LDAP_CONST char *prefix, LDAP_CONST char *suffix )
215 if ( lfdp->lfd_filtprefix != NULL ) {
216 LDAP_FREE( lfdp->lfd_filtprefix );
218 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : LDAP_STRDUP( prefix );
220 if ( lfdp->lfd_filtsuffix != NULL ) {
221 LDAP_FREE( lfdp->lfd_filtsuffix );
223 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : LDAP_STRDUP( suffix );
230 /* LDAP_CONST */ char *tagpat,
231 /* LDAP_CONST */ char *value )
237 if ( lfdp->lfd_curvalcopy != NULL ) {
238 LDAP_FREE( lfdp->lfd_curvalcopy );
239 LDAP_FREE( lfdp->lfd_curvalwords );
242 lfdp->lfd_curval = value;
243 lfdp->lfd_curfip = NULL;
245 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
246 /* compile tagpat, continue if we fail */
247 if (regcomp(&re, tagpat, REG_EXTENDED|REG_NOSUB) != 0)
250 /* match tagpattern and tag, continue if we fail */
251 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
256 /* compile flp->ifl_pattern, continue if we fail */
257 if (regcomp(&re, flp->lfl_pattern, REG_EXTENDED|REG_NOSUB) != 0)
260 /* match ifl_pattern and lfd_curval, continue if we fail */
261 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
266 /* we successfully compiled both patterns and matched both values */
267 lfdp->lfd_curfip = flp->lfl_ilist;
271 if ( lfdp->lfd_curfip == NULL ) {
275 if (( lfdp->lfd_curvalcopy = LDAP_STRDUP( value )) == NULL ) {
279 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
280 &lfdp->lfd_curvalwords ) < 0 ) {
281 LDAP_FREE( lfdp->lfd_curvalcopy );
282 lfdp->lfd_curvalcopy = NULL;
286 return( ldap_getnextfilter( lfdp ));
291 ldap_getnextfilter( LDAPFiltDesc *lfdp )
295 fip = lfdp->lfd_curfip;
301 lfdp->lfd_curfip = fip->lfi_next;
303 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
304 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
305 lfdp->lfd_curval, lfdp->lfd_curvalwords );
306 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
307 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
308 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
309 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
311 return( &lfdp->lfd_retfi );
319 LDAP_CONST char *pattern,
320 LDAP_CONST char *prefix,
321 LDAP_CONST char *suffix,
322 LDAP_CONST char *attr,
323 LDAP_CONST char *value,
329 int i, wordcount, wordnum, endwordnum;
331 if ( valwords == NULL ) {
334 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
341 if ( prefix != NULL ) {
343 f += strlen( prefix );
346 for ( p = pattern; *p != '\0'; ++p ) {
350 if ( isdigit( (unsigned char) p[1] )) {
353 if ( *(p+1) == '-' ) {
355 if ( isdigit( (unsigned char) p[1] )) {
357 endwordnum = *p - '1'; /* e.g., "%v2-4" */
358 if ( endwordnum > wordcount - 1 ) {
359 endwordnum = wordcount - 1;
362 endwordnum = wordcount - 1; /* e.g., "%v2-" */
365 endwordnum = wordnum; /* e.g., "%v2" */
368 if ( wordcount > 0 ) {
369 for ( i = wordnum; i <= endwordnum; ++i ) {
370 if ( i > wordnum ) { /* add blank btw words */
373 slen = strlen( valwords[ i ] );
374 SAFEMEMCPY( f, valwords[ i ], slen );
378 } else if ( *(p+1) == '$' ) {
380 if ( wordcount > 0 ) {
381 wordnum = wordcount - 1;
382 slen = strlen( valwords[ wordnum ] );
383 SAFEMEMCPY( f, valwords[ wordnum ], slen );
386 } else if ( value != NULL ) {
387 slen = strlen( value );
388 SAFEMEMCPY( f, value, slen );
391 } else if ( *p == 'a' && attr != NULL ) {
392 slen = strlen( attr );
393 SAFEMEMCPY( f, attr, slen );
402 if ( (size_t) (f - filtbuf) > buflen ) {
409 if ( suffix != NULL && ( (size_t) (f - filtbuf) < buflen ) )
419 break_into_words( /* LDAP_CONST */ char *str, LDAP_CONST char *delims, char ***wordsp )
425 if (( words = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
429 words[ count ] = NULL;
431 word = ldap_pvt_strtok( str, delims, &tok_r );
432 while ( word != NULL ) {
433 if (( words = (char **)LDAP_REALLOC( words,
434 ( count + 2 ) * sizeof( char * ))) == NULL ) {
438 words[ count ] = word;
439 words[ ++count ] = NULL;
440 word = ldap_pvt_strtok( NULL, delims, &tok_r );