3 * Copyright 1998-2002 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>
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_buf( char *buf, ber_len_t buflen )
42 LDAPFiltList *flp, *nextflp;
43 LDAPFiltInfo *fip, *nextfip;
49 if (( lfdp = (LDAPFiltDesc *)LDAP_CALLOC( 1, sizeof( LDAPFiltDesc))) == NULL ) {
57 while ( buflen > 0 && ( tokcnt = ldap_int_next_line_tokens( &buf, &buflen, &tok ))
61 case 1: /* tag line */
69 case 5: /* start of filter info. list */
70 if (( nextflp = (LDAPFiltList *)LDAP_CALLOC( 1, sizeof( LDAPFiltList )))
72 ldap_getfilter_free( lfdp );
75 nextflp->lfl_tag = LDAP_STRDUP( tag );
76 nextflp->lfl_pattern = tok[ 0 ];
77 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
79 regerror(rc, &re, error, sizeof(error));
80 ldap_getfilter_free( lfdp );
81 Debug( LDAP_DEBUG_ANY, "ldap_init_get_filter_buf: "
82 "bad regular expression %s, %s\n",
83 nextflp->lfl_pattern, error, 0 );
90 nextflp->lfl_delims = tok[ 1 ];
91 nextflp->lfl_ilist = NULL;
92 nextflp->lfl_next = NULL;
93 if ( flp == NULL ) { /* first one */
94 lfdp->lfd_filtlist = nextflp;
96 flp->lfl_next = nextflp;
100 for ( i = 2; i < 5; ++i ) {
101 tok[ i - 2 ] = tok[ i ];
106 case 3: /* filter, desc, and optional search scope */
107 if ( nextflp != NULL ) { /* add to info list */
108 if (( nextfip = (LDAPFiltInfo *)LDAP_CALLOC( 1,
109 sizeof( LDAPFiltInfo ))) == NULL ) {
110 ldap_getfilter_free( lfdp );
114 if ( fip == NULL ) { /* first one */
115 nextflp->lfl_ilist = nextfip;
117 fip->lfi_next = nextfip;
120 nextfip->lfi_next = NULL;
121 nextfip->lfi_filter = tok[ 0 ];
122 nextfip->lfi_desc = tok[ 1 ];
123 if ( tok[ 2 ] != NULL ) {
124 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
125 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
126 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
127 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
128 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
129 nextfip->lfi_scope = LDAP_SCOPE_BASE;
132 ldap_getfilter_free( lfdp );
136 LDAP_FREE( tok[ 2 ] );
139 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
141 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
142 strchr( tok[ 0 ], '~' ) == NULL );
149 ldap_getfilter_free( lfdp );
163 ldap_init_getfilter( LDAP_CONST char *fname )
171 if (( fp = fopen( fname, "r" )) == NULL ) {
175 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
182 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
187 if (( buf = LDAP_MALLOC( (size_t)len )) == NULL ) {
192 rlen = fread( buf, 1, (size_t)len, fp );
196 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
202 lfdp = ldap_init_getfilter_buf( buf, rlen );
211 /* LDAP_CONST */ char *tagpat,
212 /* LDAP_CONST */ char *value )
218 if ( lfdp->lfd_curvalcopy != NULL ) {
219 LDAP_FREE( lfdp->lfd_curvalcopy );
220 LDAP_FREE( lfdp->lfd_curvalwords );
223 lfdp->lfd_curval = value;
224 lfdp->lfd_curfip = NULL;
226 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
227 /* compile tagpat, continue if we fail */
228 if (regcomp(&re, tagpat, REG_EXTENDED|REG_NOSUB) != 0)
231 /* match tagpattern and tag, continue if we fail */
232 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
237 /* compile flp->ifl_pattern, continue if we fail */
238 if (regcomp(&re, flp->lfl_pattern, REG_EXTENDED|REG_NOSUB) != 0)
241 /* match ifl_pattern and lfd_curval, continue if we fail */
242 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
247 /* we successfully compiled both patterns and matched both values */
248 lfdp->lfd_curfip = flp->lfl_ilist;
252 if ( lfdp->lfd_curfip == NULL ) {
256 if (( lfdp->lfd_curvalcopy = LDAP_STRDUP( value )) == NULL ) {
260 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
261 &lfdp->lfd_curvalwords ) < 0 ) {
262 LDAP_FREE( lfdp->lfd_curvalcopy );
263 lfdp->lfd_curvalcopy = NULL;
267 return( ldap_getnextfilter( lfdp ));
274 LDAP_CONST char *pattern,
275 LDAP_CONST char *prefix,
276 LDAP_CONST char *suffix,
277 LDAP_CONST char *attr,
278 LDAP_CONST char *value,
282 ldap_getnextfilter( LDAPFiltDesc *lfdp )
286 fip = lfdp->lfd_curfip;
292 lfdp->lfd_curfip = fip->lfi_next;
294 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
295 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
296 lfdp->lfd_curval, lfdp->lfd_curvalwords );
297 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
298 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
299 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
300 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
302 return( &lfdp->lfd_retfi );
309 LDAP_CONST char *pattern,
310 LDAP_CONST char *prefix,
311 LDAP_CONST char *suffix,
312 LDAP_CONST char *attr,
313 LDAP_CONST char *value,
319 int i, wordcount, wordnum, endwordnum;
321 if ( valwords == NULL ) {
324 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
331 if ( prefix != NULL ) {
333 f += strlen( prefix );
336 for ( p = pattern; *p != '\0'; ++p ) {
340 if ( LDAP_DIGIT( (unsigned char) p[1] )) {
343 if ( *(p+1) == '-' ) {
345 if ( LDAP_DIGIT( (unsigned char) p[1] )) {
347 endwordnum = *p - '1'; /* e.g., "%v2-4" */
348 if ( endwordnum > wordcount - 1 ) {
349 endwordnum = wordcount - 1;
352 endwordnum = wordcount - 1; /* e.g., "%v2-" */
355 endwordnum = wordnum; /* e.g., "%v2" */
358 if ( wordcount > 0 ) {
359 for ( i = wordnum; i <= endwordnum; ++i ) {
360 if ( i > wordnum ) { /* add blank btw words */
363 slen = strlen( valwords[ i ] );
364 AC_MEMCPY( f, valwords[ i ], slen );
368 } else if ( *(p+1) == '$' ) {
370 if ( wordcount > 0 ) {
371 wordnum = wordcount - 1;
372 slen = strlen( valwords[ wordnum ] );
373 AC_MEMCPY( f, valwords[ wordnum ], slen );
376 } else if ( value != NULL ) {
377 slen = strlen( value );
378 AC_MEMCPY( f, value, slen );
381 } else if ( *p == 'a' && attr != NULL ) {
382 slen = strlen( attr );
383 AC_MEMCPY( f, attr, slen );
392 if ( (size_t) (f - filtbuf) > buflen ) {
399 if ( suffix != NULL && ( (size_t) (f - filtbuf) < buflen ) )
408 break_into_words( /* LDAP_CONST */ char *str, LDAP_CONST char *delims, char ***wordsp )
414 if (( words = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
418 words[ count ] = NULL;
420 word = ldap_pvt_strtok( str, delims, &tok_r );
421 while ( word != NULL ) {
422 if (( words = (char **)LDAP_REALLOC( words,
423 ( count + 2 ) * sizeof( char * ))) == NULL ) {
427 words[ count ] = word;
428 words[ ++count ] = NULL;
429 word = ldap_pvt_strtok( NULL, delims, &tok_r );