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>
21 #include <ac/unistd.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_FILE_H
31 static int break_into_words LDAP_P(( char *str, char *delims, char ***wordsp ));
32 int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
33 void free_strarray LDAP_P(( char **sap ));
35 #define FILT_MAX_LINE_LEN 1024
38 ldap_init_getfilter( 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 expresssion %s, %s\n",
129 nextflp->lfl_pattern, error );
130 #if !defined( MACOS ) && !defined( DOS )
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 *)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 );
182 #if !defined( MACOS ) && !defined( DOS )
190 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
192 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
193 strchr( tok[ 0 ], '~' ) == NULL );
199 free_strarray( tok );
200 ldap_getfilter_free( lfdp );
201 #if !defined( MACOS ) && !defined( DOS )
217 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
219 if ( lfdp->lfd_filtprefix != NULL ) {
220 free( lfdp->lfd_filtprefix );
222 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
224 if ( lfdp->lfd_filtsuffix != NULL ) {
225 free( lfdp->lfd_filtsuffix );
227 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
232 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
238 if ( lfdp->lfd_curvalcopy != NULL ) {
239 free( lfdp->lfd_curvalcopy );
240 free( lfdp->lfd_curvalwords );
243 lfdp->lfd_curval = value;
244 lfdp->lfd_curfip = NULL;
246 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
247 /* compile tagpat, continue if we fail */
248 if (regcomp(&re, tagpat, 0) != 0)
251 /* match tagpatern and tag, continue if we fail */
252 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
257 /* compile flp->ifl_pattern, continue if we fail */
258 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
261 /* match ifl_pattern and lfd_curval, continue if we fail */
262 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
267 /* we successfully compiled both patterns and matched both values */
268 lfdp->lfd_curfip = flp->lfl_ilist;
272 if ( lfdp->lfd_curfip == NULL ) {
276 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
280 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
281 &lfdp->lfd_curvalwords ) < 0 ) {
282 free( lfdp->lfd_curvalcopy );
283 lfdp->lfd_curvalcopy = NULL;
287 return( ldap_getnextfilter( lfdp ));
292 ldap_getnextfilter( LDAPFiltDesc *lfdp )
296 fip = lfdp->lfd_curfip;
302 lfdp->lfd_curfip = fip->lfi_next;
304 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
305 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
306 lfdp->lfd_curval, lfdp->lfd_curvalwords );
307 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
308 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
309 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
310 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
312 return( &lfdp->lfd_retfi );
317 ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
318 char *prefix, char *suffix, char *attr, char *value, char **valwords )
322 int i, wordcount, wordnum, endwordnum;
324 if ( valwords == NULL ) {
327 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
334 if ( prefix != NULL ) {
336 f += strlen( prefix );
339 for ( p = pattern; *p != '\0'; ++p ) {
343 if ( isdigit( *(p+1))) {
346 if ( *(p+1) == '-' ) {
348 if ( isdigit( *(p+1))) {
350 endwordnum = *p - '1'; /* e.g., "%v2-4" */
351 if ( endwordnum > wordcount - 1 ) {
352 endwordnum = wordcount - 1;
355 endwordnum = wordcount - 1; /* e.g., "%v2-" */
358 endwordnum = wordnum; /* e.g., "%v2" */
361 if ( wordcount > 0 ) {
362 for ( i = wordnum; i <= endwordnum; ++i ) {
363 if ( i > wordnum ) { /* add blank btw words */
366 slen = strlen( valwords[ i ] );
367 SAFEMEMCPY( f, valwords[ i ], slen );
371 } else if ( *(p+1) == '$' ) {
373 if ( wordcount > 0 ) {
374 wordnum = wordcount - 1;
375 slen = strlen( valwords[ wordnum ] );
376 SAFEMEMCPY( f, valwords[ wordnum ], slen );
379 } else if ( value != NULL ) {
380 slen = strlen( value );
381 SAFEMEMCPY( f, value, slen );
384 } else if ( *p == 'a' && attr != NULL ) {
385 slen = strlen( attr );
386 SAFEMEMCPY( f, attr, slen );
395 if ( (unsigned long) (f - filtbuf) > buflen ) {
402 if ( suffix != NULL && (
403 (unsigned long) ( f - filtbuf ) < buflen ) )
413 break_into_words( char *str, char *delims, char ***wordsp )
418 if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
422 words[ count ] = NULL;
424 word = strtok( str, delims );
425 while ( word != NULL ) {
426 if (( words = (char **)realloc( words,
427 ( count + 2 ) * sizeof( char * ))) == NULL ) {
431 words[ count ] = word;
432 words[ ++count ] = NULL;
433 word = strtok( NULL, delims );