2 * Copyright (c) 1993 Regents of the University of Michigan.
5 * getfilter.c -- optional add-on to libldap
11 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
20 #include <ac/string.h>
22 #include <ac/unistd.h>
24 #ifdef HAVE_SYS_FILE_H
30 static int break_into_words LDAP_P(( char *str, char *delims, char ***wordsp ));
31 int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
32 void free_strarray LDAP_P(( char **sap ));
34 #define FILT_MAX_LINE_LEN 1024
37 ldap_init_getfilter( char *fname )
45 if (( fp = fopen( fname, "r" )) == NULL ) {
49 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
56 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
61 if (( buf = malloc( (size_t)len )) == NULL ) {
66 rlen = fread( buf, 1, (size_t)len, fp );
70 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
76 lfdp = ldap_init_getfilter_buf( buf, rlen );
84 ldap_init_getfilter_buf( char *buf, long buflen )
87 LDAPFiltList *flp, *nextflp;
88 LDAPFiltInfo *fip, *nextfip;
94 if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
102 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
106 case 1: /* tag line */
114 case 5: /* start of filter info. list */
115 if (( nextflp = (LDAPFiltList *)calloc( 1, sizeof( LDAPFiltList )))
117 ldap_getfilter_free( lfdp );
120 nextflp->lfl_tag = strdup( tag );
121 nextflp->lfl_pattern = tok[ 0 ];
122 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
125 regerror(rc, &re, error, sizeof(error));
126 ldap_getfilter_free( lfdp );
127 fprintf( stderr, "bad regular expresssion %s, %s\n",
128 nextflp->lfl_pattern, error );
130 #endif /* LDAP_LIBUI */
131 free_strarray( tok );
136 nextflp->lfl_delims = tok[ 1 ];
137 nextflp->lfl_ilist = NULL;
138 nextflp->lfl_next = NULL;
139 if ( flp == NULL ) { /* first one */
140 lfdp->lfd_filtlist = nextflp;
142 flp->lfl_next = nextflp;
146 for ( i = 2; i < 5; ++i ) {
147 tok[ i - 2 ] = tok[ i ];
152 case 3: /* filter, desc, and optional search scope */
153 if ( nextflp != NULL ) { /* add to info list */
154 if (( nextfip = (LDAPFiltInfo *)calloc( 1,
155 sizeof( LDAPFiltInfo ))) == NULL ) {
156 ldap_getfilter_free( lfdp );
157 free_strarray( tok );
160 if ( fip == NULL ) { /* first one */
161 nextflp->lfl_ilist = nextfip;
163 fip->lfi_next = nextfip;
166 nextfip->lfi_next = NULL;
167 nextfip->lfi_filter = tok[ 0 ];
168 nextfip->lfi_desc = tok[ 1 ];
169 if ( tok[ 2 ] != NULL ) {
170 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
171 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
172 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
173 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
174 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
175 nextfip->lfi_scope = LDAP_SCOPE_BASE;
177 free_strarray( tok );
178 ldap_getfilter_free( lfdp );
185 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
187 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
188 strchr( tok[ 0 ], '~' ) == NULL );
194 free_strarray( tok );
195 ldap_getfilter_free( lfdp );
210 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
212 if ( lfdp->lfd_filtprefix != NULL ) {
213 free( lfdp->lfd_filtprefix );
215 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
217 if ( lfdp->lfd_filtsuffix != NULL ) {
218 free( lfdp->lfd_filtsuffix );
220 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
225 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
231 if ( lfdp->lfd_curvalcopy != NULL ) {
232 free( lfdp->lfd_curvalcopy );
233 free( lfdp->lfd_curvalwords );
236 lfdp->lfd_curval = value;
237 lfdp->lfd_curfip = NULL;
239 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
240 /* compile tagpat, continue if we fail */
241 if (regcomp(&re, tagpat, 0) != 0)
244 /* match tagpatern and tag, continue if we fail */
245 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
250 /* compile flp->ifl_pattern, continue if we fail */
251 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
254 /* match ifl_pattern and lfd_curval, continue if we fail */
255 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
260 /* we successfully compiled both patterns and matched both values */
261 lfdp->lfd_curfip = flp->lfl_ilist;
265 if ( lfdp->lfd_curfip == NULL ) {
269 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
273 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
274 &lfdp->lfd_curvalwords ) < 0 ) {
275 free( lfdp->lfd_curvalcopy );
276 lfdp->lfd_curvalcopy = NULL;
280 return( ldap_getnextfilter( lfdp ));
285 ldap_getnextfilter( LDAPFiltDesc *lfdp )
289 fip = lfdp->lfd_curfip;
295 lfdp->lfd_curfip = fip->lfi_next;
297 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
298 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
299 lfdp->lfd_curval, lfdp->lfd_curvalwords );
300 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
301 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
302 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
303 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
305 return( &lfdp->lfd_retfi );
310 ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
311 char *prefix, char *suffix, char *attr, char *value, char **valwords )
315 int i, wordcount, wordnum, endwordnum;
317 if ( valwords == NULL ) {
320 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
327 if ( prefix != NULL ) {
329 f += strlen( prefix );
332 for ( p = pattern; *p != '\0'; ++p ) {
336 if ( isdigit( *(p+1))) {
339 if ( *(p+1) == '-' ) {
341 if ( isdigit( *(p+1))) {
343 endwordnum = *p - '1'; /* e.g., "%v2-4" */
344 if ( endwordnum > wordcount - 1 ) {
345 endwordnum = wordcount - 1;
348 endwordnum = wordcount - 1; /* e.g., "%v2-" */
351 endwordnum = wordnum; /* e.g., "%v2" */
354 if ( wordcount > 0 ) {
355 for ( i = wordnum; i <= endwordnum; ++i ) {
356 if ( i > wordnum ) { /* add blank btw words */
359 slen = strlen( valwords[ i ] );
360 SAFEMEMCPY( f, valwords[ i ], slen );
364 } else if ( *(p+1) == '$' ) {
366 if ( wordcount > 0 ) {
367 wordnum = wordcount - 1;
368 slen = strlen( valwords[ wordnum ] );
369 SAFEMEMCPY( f, valwords[ wordnum ], slen );
372 } else if ( value != NULL ) {
373 slen = strlen( value );
374 SAFEMEMCPY( f, value, slen );
377 } else if ( *p == 'a' && attr != NULL ) {
378 slen = strlen( attr );
379 SAFEMEMCPY( f, attr, slen );
388 if ( (unsigned long) (f - filtbuf) > buflen ) {
395 if ( suffix != NULL && (
396 (unsigned long) ( f - filtbuf ) < buflen ) )
406 break_into_words( char *str, char *delims, char ***wordsp )
411 if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
415 words[ count ] = NULL;
417 word = strtok( str, delims );
418 while ( word != NULL ) {
419 if (( words = (char **)realloc( words,
420 ( count + 2 ) * sizeof( char * ))) == NULL ) {
424 words[ count ] = word;
425 words[ ++count ] = NULL;
426 word = strtok( NULL, delims );