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
20 #include <ac/string.h>
22 #include <ac/unistd.h>
24 #ifdef HAVE_SYS_FILE_H
30 static int break_into_words LDAP_P((
31 /* LDAP_CONST */ char *str,
32 LDAP_CONST char *delims,
35 #define FILT_MAX_LINE_LEN 1024
38 ldap_init_getfilter( LDAP_CONST char *fname )
46 if (( fp = fopen( fname, "r" )) == NULL ) {
50 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
57 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
62 if (( buf = malloc( (size_t)len )) == NULL ) {
67 rlen = fread( buf, 1, (size_t)len, fp );
71 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
77 lfdp = ldap_init_getfilter_buf( buf, rlen );
85 ldap_init_getfilter_buf( char *buf, long buflen )
88 LDAPFiltList *flp, *nextflp;
89 LDAPFiltInfo *fip, *nextfip;
95 if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
103 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
107 case 1: /* tag line */
115 case 5: /* start of filter info. list */
116 if (( nextflp = (LDAPFiltList *)calloc( 1, sizeof( LDAPFiltList )))
118 ldap_getfilter_free( lfdp );
121 nextflp->lfl_tag = strdup( tag );
122 nextflp->lfl_pattern = tok[ 0 ];
123 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
126 regerror(rc, &re, error, sizeof(error));
127 ldap_getfilter_free( lfdp );
128 fprintf( stderr, "bad regular expression %s, %s\n",
129 nextflp->lfl_pattern, error );
131 #endif /* LDAP_LIBUI */
132 free_strarray( tok );
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;
143 flp->lfl_next = nextflp;
147 for ( i = 2; i < 5; ++i ) {
148 tok[ i - 2 ] = tok[ i ];
153 case 3: /* filter, desc, and optional search scope */
154 if ( nextflp != NULL ) { /* add to info list */
155 if (( nextfip = (LDAPFiltInfo *)calloc( 1,
156 sizeof( LDAPFiltInfo ))) == NULL ) {
157 ldap_getfilter_free( lfdp );
158 free_strarray( tok );
161 if ( fip == NULL ) { /* first one */
162 nextflp->lfl_ilist = nextfip;
164 fip->lfi_next = 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;
178 free_strarray( tok );
179 ldap_getfilter_free( lfdp );
186 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
188 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
189 strchr( tok[ 0 ], '~' ) == NULL );
195 free_strarray( tok );
196 ldap_getfilter_free( lfdp );
211 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, LDAP_CONST char *prefix, LDAP_CONST char *suffix )
213 if ( lfdp->lfd_filtprefix != NULL ) {
214 free( lfdp->lfd_filtprefix );
216 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
218 if ( lfdp->lfd_filtsuffix != NULL ) {
219 free( lfdp->lfd_filtsuffix );
221 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
228 /* LDAP_CONST */ char *tagpat,
229 /* LDAP_CONST */ char *value )
235 if ( lfdp->lfd_curvalcopy != NULL ) {
236 free( lfdp->lfd_curvalcopy );
237 free( lfdp->lfd_curvalwords );
240 lfdp->lfd_curval = value;
241 lfdp->lfd_curfip = NULL;
243 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
244 /* compile tagpat, continue if we fail */
245 if (regcomp(&re, tagpat, 0) != 0)
248 /* match tagpatern and tag, continue if we fail */
249 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
254 /* compile flp->ifl_pattern, continue if we fail */
255 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
258 /* match ifl_pattern and lfd_curval, continue if we fail */
259 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
264 /* we successfully compiled both patterns and matched both values */
265 lfdp->lfd_curfip = flp->lfl_ilist;
269 if ( lfdp->lfd_curfip == NULL ) {
273 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
277 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
278 &lfdp->lfd_curvalwords ) < 0 ) {
279 free( lfdp->lfd_curvalcopy );
280 lfdp->lfd_curvalcopy = NULL;
284 return( ldap_getnextfilter( lfdp ));
289 ldap_getnextfilter( LDAPFiltDesc *lfdp )
293 fip = lfdp->lfd_curfip;
299 lfdp->lfd_curfip = fip->lfi_next;
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;
309 return( &lfdp->lfd_retfi );
316 unsigned long 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,
327 int i, wordcount, wordnum, endwordnum;
329 if ( valwords == NULL ) {
332 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
339 if ( prefix != NULL ) {
341 f += strlen( prefix );
344 for ( p = pattern; *p != '\0'; ++p ) {
348 if ( isdigit( (unsigned char) p[1] )) {
351 if ( *(p+1) == '-' ) {
353 if ( isdigit( (unsigned char) p[1] )) {
355 endwordnum = *p - '1'; /* e.g., "%v2-4" */
356 if ( endwordnum > wordcount - 1 ) {
357 endwordnum = wordcount - 1;
360 endwordnum = wordcount - 1; /* e.g., "%v2-" */
363 endwordnum = wordnum; /* e.g., "%v2" */
366 if ( wordcount > 0 ) {
367 for ( i = wordnum; i <= endwordnum; ++i ) {
368 if ( i > wordnum ) { /* add blank btw words */
371 slen = strlen( valwords[ i ] );
372 SAFEMEMCPY( f, valwords[ i ], slen );
376 } else if ( *(p+1) == '$' ) {
378 if ( wordcount > 0 ) {
379 wordnum = wordcount - 1;
380 slen = strlen( valwords[ wordnum ] );
381 SAFEMEMCPY( f, valwords[ wordnum ], slen );
384 } else if ( value != NULL ) {
385 slen = strlen( value );
386 SAFEMEMCPY( f, value, slen );
389 } else if ( *p == 'a' && attr != NULL ) {
390 slen = strlen( attr );
391 SAFEMEMCPY( f, attr, slen );
400 if ( (unsigned long) (f - filtbuf) > buflen ) {
407 if ( suffix != NULL && (
408 (unsigned long) ( f - filtbuf ) < buflen ) )
418 break_into_words( /* LDAP_CONST */ char *str, LDAP_CONST char *delims, char ***wordsp )
424 if (( words = (char **)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 **)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 );