2 * Copyright (c) 1993 Regents of the University of Michigan.
5 * getfilter.c -- optional add-on to libldap
9 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
16 #include <sys/types.h>
26 #include <sys/types.h>
28 #include <sys/errno.h>
39 static int break_into_words( char *str, char *delims, char ***wordsp );
40 int next_line_tokens( char **bufp, long *blenp, char ***toksp );
41 void free_strarray( char **sap );
42 #else /* NEEDPROTOS */
43 static int break_into_words();
44 int next_line_tokens();
46 #endif /* NEEDPROTOS */
48 #if !defined( MACOS ) && !defined( DOS )
52 #define FILT_MAX_LINE_LEN 1024
55 ldap_init_getfilter( char *fname )
63 if (( fp = fopen( fname, "r" )) == NULL ) {
67 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
74 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
79 if (( buf = malloc( (size_t)len )) == NULL ) {
84 rlen = fread( buf, 1, (size_t)len, fp );
88 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
94 lfdp = ldap_init_getfilter_buf( buf, rlen );
102 ldap_init_getfilter_buf( char *buf, long buflen )
105 LDAPFiltList *flp, *nextflp;
106 LDAPFiltInfo *fip, *nextfip;
112 if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
116 flp = nextflp = NULL;
120 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
124 case 1: /* tag line */
132 case 5: /* start of filter info. list */
133 if (( nextflp = (LDAPFiltList *)calloc( 1, sizeof( LDAPFiltList )))
135 ldap_getfilter_free( lfdp );
138 nextflp->lfl_tag = strdup( tag );
139 nextflp->lfl_pattern = tok[ 0 ];
140 if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
141 #ifndef NO_USERINTERFACE
143 regerror(rc, &re, error, sizeof(error));
144 ldap_getfilter_free( lfdp );
145 fprintf( stderr, "bad regular expresssion %s, %s\n",
146 nextflp->lfl_pattern, error );
147 #if !defined( MACOS ) && !defined( DOS )
150 #endif /* NO_USERINTERFACE */
151 free_strarray( tok );
156 nextflp->lfl_delims = tok[ 1 ];
157 nextflp->lfl_ilist = NULL;
158 nextflp->lfl_next = NULL;
159 if ( flp == NULL ) { /* first one */
160 lfdp->lfd_filtlist = nextflp;
162 flp->lfl_next = nextflp;
166 for ( i = 2; i < 5; ++i ) {
167 tok[ i - 2 ] = tok[ i ];
172 case 3: /* filter, desc, and optional search scope */
173 if ( nextflp != NULL ) { /* add to info list */
174 if (( nextfip = (LDAPFiltInfo *)calloc( 1,
175 sizeof( LDAPFiltInfo ))) == NULL ) {
176 ldap_getfilter_free( lfdp );
177 free_strarray( tok );
180 if ( fip == NULL ) { /* first one */
181 nextflp->lfl_ilist = nextfip;
183 fip->lfi_next = nextfip;
186 nextfip->lfi_next = NULL;
187 nextfip->lfi_filter = tok[ 0 ];
188 nextfip->lfi_desc = tok[ 1 ];
189 if ( tok[ 2 ] != NULL ) {
190 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
191 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
192 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
193 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
194 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
195 nextfip->lfi_scope = LDAP_SCOPE_BASE;
197 free_strarray( tok );
198 ldap_getfilter_free( lfdp );
199 #if !defined( MACOS ) && !defined( DOS )
207 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
209 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
210 strchr( tok[ 0 ], '~' ) == NULL );
216 free_strarray( tok );
217 ldap_getfilter_free( lfdp );
218 #if !defined( MACOS ) && !defined( DOS )
234 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
236 if ( lfdp->lfd_filtprefix != NULL ) {
237 free( lfdp->lfd_filtprefix );
239 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
241 if ( lfdp->lfd_filtsuffix != NULL ) {
242 free( lfdp->lfd_filtsuffix );
244 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
249 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
255 if ( lfdp->lfd_curvalcopy != NULL ) {
256 free( lfdp->lfd_curvalcopy );
257 free( lfdp->lfd_curvalwords );
260 lfdp->lfd_curval = value;
261 lfdp->lfd_curfip = NULL;
263 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
264 /* compile tagpat, continue if we fail */
265 if (regcomp(&re, tagpat, 0) != 0)
268 /* match tagpatern and tag, continue if we fail */
269 rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
274 /* compile flp->ifl_pattern, continue if we fail */
275 if (regcomp(&re, flp->lfl_pattern, 0) != 0)
278 /* match ifl_pattern and lfd_curval, continue if we fail */
279 rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
284 /* we successfully compiled both patterns and matched both values */
285 lfdp->lfd_curfip = flp->lfl_ilist;
289 if ( lfdp->lfd_curfip == NULL ) {
293 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
297 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
298 &lfdp->lfd_curvalwords ) < 0 ) {
299 free( lfdp->lfd_curvalcopy );
300 lfdp->lfd_curvalcopy = NULL;
304 return( ldap_getnextfilter( lfdp ));
309 ldap_getnextfilter( LDAPFiltDesc *lfdp )
313 fip = lfdp->lfd_curfip;
319 lfdp->lfd_curfip = fip->lfi_next;
321 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
322 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
323 lfdp->lfd_curval, lfdp->lfd_curvalwords );
324 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
325 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
326 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
327 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
329 return( &lfdp->lfd_retfi );
334 ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
335 char *prefix, char *suffix, char *attr, char *value, char **valwords )
339 int i, wordcount, wordnum, endwordnum;
341 if ( valwords == NULL ) {
344 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
351 if ( prefix != NULL ) {
353 f += strlen( prefix );
356 for ( p = pattern; *p != '\0'; ++p ) {
360 if ( isdigit( *(p+1))) {
363 if ( *(p+1) == '-' ) {
365 if ( isdigit( *(p+1))) {
367 endwordnum = *p - '1'; /* e.g., "%v2-4" */
368 if ( endwordnum > wordcount - 1 ) {
369 endwordnum = wordcount - 1;
372 endwordnum = wordcount - 1; /* e.g., "%v2-" */
375 endwordnum = wordnum; /* e.g., "%v2" */
378 if ( wordcount > 0 ) {
379 for ( i = wordnum; i <= endwordnum; ++i ) {
380 if ( i > wordnum ) { /* add blank btw words */
383 slen = strlen( valwords[ i ] );
384 SAFEMEMCPY( f, valwords[ i ], slen );
388 } else if ( *(p+1) == '$' ) {
390 if ( wordcount > 0 ) {
391 wordnum = wordcount - 1;
392 slen = strlen( valwords[ wordnum ] );
393 SAFEMEMCPY( f, valwords[ wordnum ], slen );
396 } else if ( value != NULL ) {
397 slen = strlen( value );
398 SAFEMEMCPY( f, value, slen );
401 } else if ( *p == 'a' && attr != NULL ) {
402 slen = strlen( attr );
403 SAFEMEMCPY( f, attr, slen );
412 if ( f - filtbuf > buflen ) {
419 if ( suffix != NULL && ( f - filtbuf ) < buflen ) {
428 break_into_words( char *str, char *delims, char ***wordsp )
433 if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
437 words[ count ] = NULL;
439 word = strtok( str, delims );
440 while ( word != NULL ) {
441 if (( words = (char **)realloc( words,
442 ( count + 2 ) * sizeof( char * ))) == NULL ) {
446 words[ count ] = word;
447 words[ ++count ] = NULL;
448 word = strtok( NULL, delims );