2 * Copyright (c) 1993 Regents of the University of Michigan.
5 * getfilter.c -- optional add-on to libldap
12 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
17 #include <ac/string.h>
19 #include <sys/types.h>
29 #include <sys/types.h>
31 #include <sys/errno.h>
41 static int break_into_words LDAP_P(( char *str, char *delims, char ***wordsp ));
42 int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
43 void free_strarray LDAP_P(( char **sap ));
45 #define FILT_MAX_LINE_LEN 1024
48 ldap_init_getfilter( char *fname )
56 if (( fp = fopen( fname, "r" )) == NULL ) {
60 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
67 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
72 if (( buf = malloc( (size_t)len )) == NULL ) {
77 rlen = fread( buf, 1, (size_t)len, fp );
81 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
87 lfdp = ldap_init_getfilter_buf( buf, rlen );
95 ldap_init_getfilter_buf( char *buf, long buflen )
98 LDAPFiltList *flp, *nextflp;
99 LDAPFiltInfo *fip, *nextfip;
105 if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
109 flp = nextflp = NULL;
113 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
117 case 1: /* tag line */
125 case 5: /* start of filter info. list */
126 if (( nextflp = (LDAPFiltList *)calloc( 1, sizeof( LDAPFiltList )))
128 ldap_getfilter_free( lfdp );
131 nextflp->lfl_tag = strdup( tag );
132 nextflp->lfl_pattern = tok[ 0 ];
133 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
134 #ifndef NO_USERINTERFACE
136 regerror(rc, &re, error, sizeof(error));
137 ldap_getfilter_free( lfdp );
138 fprintf( stderr, "bad regular expresssion %s, %s\n",
139 nextflp->lfl_pattern, error );
140 #if !defined( MACOS ) && !defined( DOS )
143 #endif /* NO_USERINTERFACE */
144 free_strarray( tok );
149 nextflp->lfl_delims = tok[ 1 ];
150 nextflp->lfl_ilist = NULL;
151 nextflp->lfl_next = NULL;
152 if ( flp == NULL ) { /* first one */
153 lfdp->lfd_filtlist = nextflp;
155 flp->lfl_next = nextflp;
159 for ( i = 2; i < 5; ++i ) {
160 tok[ i - 2 ] = tok[ i ];
165 case 3: /* filter, desc, and optional search scope */
166 if ( nextflp != NULL ) { /* add to info list */
167 if (( nextfip = (LDAPFiltInfo *)calloc( 1,
168 sizeof( LDAPFiltInfo ))) == NULL ) {
169 ldap_getfilter_free( lfdp );
170 free_strarray( tok );
173 if ( fip == NULL ) { /* first one */
174 nextflp->lfl_ilist = nextfip;
176 fip->lfi_next = nextfip;
179 nextfip->lfi_next = NULL;
180 nextfip->lfi_filter = tok[ 0 ];
181 nextfip->lfi_desc = tok[ 1 ];
182 if ( tok[ 2 ] != NULL ) {
183 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
184 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
185 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
186 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
187 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
188 nextfip->lfi_scope = LDAP_SCOPE_BASE;
190 free_strarray( tok );
191 ldap_getfilter_free( lfdp );
192 #if !defined( MACOS ) && !defined( DOS )
200 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
202 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
203 strchr( tok[ 0 ], '~' ) == NULL );
209 free_strarray( tok );
210 ldap_getfilter_free( lfdp );
211 #if !defined( MACOS ) && !defined( DOS )
227 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
229 if ( lfdp->lfd_filtprefix != NULL ) {
230 free( lfdp->lfd_filtprefix );
232 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
234 if ( lfdp->lfd_filtsuffix != NULL ) {
235 free( lfdp->lfd_filtsuffix );
237 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
242 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
248 if ( lfdp->lfd_curvalcopy != NULL ) {
249 free( lfdp->lfd_curvalcopy );
250 free( lfdp->lfd_curvalwords );
253 lfdp->lfd_curval = value;
254 lfdp->lfd_curfip = NULL;
256 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
257 /* compile tagpat, continue if we fail */
258 if (regcomp(&re, tagpat, 0) != 0)
261 /* match tagpatern and tag, continue if we fail */
262 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
267 /* compile flp->ifl_pattern, continue if we fail */
268 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
271 /* match ifl_pattern and lfd_curval, continue if we fail */
272 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
277 /* we successfully compiled both patterns and matched both values */
278 lfdp->lfd_curfip = flp->lfl_ilist;
282 if ( lfdp->lfd_curfip == NULL ) {
286 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
290 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
291 &lfdp->lfd_curvalwords ) < 0 ) {
292 free( lfdp->lfd_curvalcopy );
293 lfdp->lfd_curvalcopy = NULL;
297 return( ldap_getnextfilter( lfdp ));
302 ldap_getnextfilter( LDAPFiltDesc *lfdp )
306 fip = lfdp->lfd_curfip;
312 lfdp->lfd_curfip = fip->lfi_next;
314 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
315 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
316 lfdp->lfd_curval, lfdp->lfd_curvalwords );
317 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
318 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
319 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
320 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
322 return( &lfdp->lfd_retfi );
327 ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
328 char *prefix, char *suffix, char *attr, char *value, char **valwords )
332 int i, wordcount, wordnum, endwordnum;
334 if ( valwords == NULL ) {
337 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
344 if ( prefix != NULL ) {
346 f += strlen( prefix );
349 for ( p = pattern; *p != '\0'; ++p ) {
353 if ( isdigit( *(p+1))) {
356 if ( *(p+1) == '-' ) {
358 if ( isdigit( *(p+1))) {
360 endwordnum = *p - '1'; /* e.g., "%v2-4" */
361 if ( endwordnum > wordcount - 1 ) {
362 endwordnum = wordcount - 1;
365 endwordnum = wordcount - 1; /* e.g., "%v2-" */
368 endwordnum = wordnum; /* e.g., "%v2" */
371 if ( wordcount > 0 ) {
372 for ( i = wordnum; i <= endwordnum; ++i ) {
373 if ( i > wordnum ) { /* add blank btw words */
376 slen = strlen( valwords[ i ] );
377 SAFEMEMCPY( f, valwords[ i ], slen );
381 } else if ( *(p+1) == '$' ) {
383 if ( wordcount > 0 ) {
384 wordnum = wordcount - 1;
385 slen = strlen( valwords[ wordnum ] );
386 SAFEMEMCPY( f, valwords[ wordnum ], slen );
389 } else if ( value != NULL ) {
390 slen = strlen( value );
391 SAFEMEMCPY( f, value, slen );
394 } else if ( *p == 'a' && attr != NULL ) {
395 slen = strlen( attr );
396 SAFEMEMCPY( f, attr, slen );
405 if ( f - filtbuf > buflen ) {
412 if ( suffix != NULL && ( f - filtbuf ) < buflen ) {
421 break_into_words( char *str, char *delims, char ***wordsp )
426 if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
430 words[ count ] = NULL;
432 word = strtok( str, delims );
433 while ( word != NULL ) {
434 if (( words = (char **)realloc( words,
435 ( count + 2 ) * sizeof( char * ))) == NULL ) {
439 words[ count ] = word;
440 words[ ++count ] = NULL;
441 word = strtok( NULL, delims );