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";
26 #include <sys/types.h>
29 #include <sys/errno.h>
41 static int break_into_words( char *str, char *delims, char ***wordsp );
42 int next_line_tokens( char **bufp, long *blenp, char ***toksp );
43 void free_strarray( char **sap );
44 #else /* NEEDPROTOS */
45 static int break_into_words();
46 int next_line_tokens();
48 #endif /* NEEDPROTOS */
50 #if !defined( MACOS ) && !defined( DOS )
52 extern char *re_comp();
55 #define FILT_MAX_LINE_LEN 1024
58 ldap_init_getfilter( char *fname )
66 if (( fp = fopen( fname, "r" )) == NULL ) {
70 if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
77 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
82 if (( buf = malloc( (size_t)len )) == NULL ) {
87 rlen = fread( buf, 1, (size_t)len, fp );
91 if ( rlen != len && !eof ) { /* error: didn't get the whole file */
97 lfdp = ldap_init_getfilter_buf( buf, rlen );
105 ldap_init_getfilter_buf( char *buf, long buflen )
108 LDAPFiltList *flp, *nextflp;
109 LDAPFiltInfo *fip, *nextfip;
113 if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
117 flp = nextflp = NULL;
121 while ( buflen > 0 && ( tokcnt = next_line_tokens( &buf, &buflen, &tok ))
125 case 1: /* tag line */
133 case 5: /* start of filter info. list */
134 if (( nextflp = (LDAPFiltList *)calloc( 1, sizeof( LDAPFiltList )))
136 ldap_getfilter_free( lfdp );
139 nextflp->lfl_tag = strdup( tag );
140 nextflp->lfl_pattern = tok[ 0 ];
141 if ( re_comp( nextflp->lfl_pattern ) != NULL ) {
142 #ifndef NO_USERINTERFACE
143 ldap_getfilter_free( lfdp );
144 fprintf( stderr, "bad regular expresssion %s\n",
145 nextflp->lfl_pattern );
146 #if !defined( MACOS ) && !defined( DOS )
149 #endif /* NO_USERINTERFACE */
150 free_strarray( tok );
154 nextflp->lfl_delims = tok[ 1 ];
155 nextflp->lfl_ilist = NULL;
156 nextflp->lfl_next = NULL;
157 if ( flp == NULL ) { /* first one */
158 lfdp->lfd_filtlist = nextflp;
160 flp->lfl_next = nextflp;
164 for ( i = 2; i < 5; ++i ) {
165 tok[ i - 2 ] = tok[ i ];
170 case 3: /* filter, desc, and optional search scope */
171 if ( nextflp != NULL ) { /* add to info list */
172 if (( nextfip = (LDAPFiltInfo *)calloc( 1,
173 sizeof( LDAPFiltInfo ))) == NULL ) {
174 ldap_getfilter_free( lfdp );
175 free_strarray( tok );
178 if ( fip == NULL ) { /* first one */
179 nextflp->lfl_ilist = nextfip;
181 fip->lfi_next = nextfip;
184 nextfip->lfi_next = NULL;
185 nextfip->lfi_filter = tok[ 0 ];
186 nextfip->lfi_desc = tok[ 1 ];
187 if ( tok[ 2 ] != NULL ) {
188 if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
189 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
190 } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
191 nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
192 } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
193 nextfip->lfi_scope = LDAP_SCOPE_BASE;
195 free_strarray( tok );
196 ldap_getfilter_free( lfdp );
197 #if !defined( MACOS ) && !defined( DOS )
205 nextfip->lfi_scope = LDAP_SCOPE_SUBTREE; /* default */
207 nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
208 strchr( tok[ 0 ], '~' ) == NULL );
214 free_strarray( tok );
215 ldap_getfilter_free( lfdp );
216 #if !defined( MACOS ) && !defined( DOS )
232 ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
234 if ( lfdp->lfd_filtprefix != NULL ) {
235 free( lfdp->lfd_filtprefix );
237 lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
239 if ( lfdp->lfd_filtsuffix != NULL ) {
240 free( lfdp->lfd_filtsuffix );
242 lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
247 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
251 if ( lfdp->lfd_curvalcopy != NULL ) {
252 free( lfdp->lfd_curvalcopy );
253 free( lfdp->lfd_curvalwords );
256 lfdp->lfd_curval = value;
257 lfdp->lfd_curfip = NULL;
259 for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
260 if ( re_comp( tagpat ) == NULL && re_exec( flp->lfl_tag ) == 1
261 && re_comp( flp->lfl_pattern ) == NULL
262 && re_exec( lfdp->lfd_curval ) == 1 ) {
263 lfdp->lfd_curfip = flp->lfl_ilist;
268 if ( lfdp->lfd_curfip == NULL ) {
272 if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
276 if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
277 &lfdp->lfd_curvalwords ) < 0 ) {
278 free( lfdp->lfd_curvalcopy );
279 lfdp->lfd_curvalcopy = NULL;
283 return( ldap_getnextfilter( lfdp ));
288 ldap_getnextfilter( LDAPFiltDesc *lfdp )
292 fip = lfdp->lfd_curfip;
298 lfdp->lfd_curfip = fip->lfi_next;
300 ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
301 lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
302 lfdp->lfd_curval, lfdp->lfd_curvalwords );
303 lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
304 lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
305 lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
306 lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
308 return( &lfdp->lfd_retfi );
313 ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
314 char *prefix, char *suffix, char *attr, char *value, char **valwords )
318 int i, wordcount, wordnum, endwordnum;
320 if ( valwords == NULL ) {
323 for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
330 if ( prefix != NULL ) {
332 f += strlen( prefix );
335 for ( p = pattern; *p != '\0'; ++p ) {
339 if ( isdigit( *(p+1))) {
342 if ( *(p+1) == '-' ) {
344 if ( isdigit( *(p+1))) {
346 endwordnum = *p - '1'; /* e.g., "%v2-4" */
347 if ( endwordnum > wordcount - 1 ) {
348 endwordnum = wordcount - 1;
351 endwordnum = wordcount - 1; /* e.g., "%v2-" */
354 endwordnum = wordnum; /* e.g., "%v2" */
357 if ( wordcount > 0 ) {
358 for ( i = wordnum; i <= endwordnum; ++i ) {
359 if ( i > wordnum ) { /* add blank btw words */
362 slen = strlen( valwords[ i ] );
363 SAFEMEMCPY( f, valwords[ i ], slen );
367 } else if ( *(p+1) == '$' ) {
369 if ( wordcount > 0 ) {
370 wordnum = wordcount - 1;
371 slen = strlen( valwords[ wordnum ] );
372 SAFEMEMCPY( f, valwords[ wordnum ], slen );
375 } else if ( value != NULL ) {
376 slen = strlen( value );
377 SAFEMEMCPY( f, value, slen );
380 } else if ( *p == 'a' && attr != NULL ) {
381 slen = strlen( attr );
382 SAFEMEMCPY( f, attr, slen );
391 if ( f - filtbuf > buflen ) {
398 if ( suffix != NULL && ( f - filtbuf ) < buflen ) {
407 break_into_words( char *str, char *delims, char ***wordsp )
412 if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
416 words[ count ] = NULL;
418 word = strtok( str, delims );
419 while ( word != NULL ) {
420 if (( words = (char **)realloc( words,
421 ( count + 2 ) * sizeof( char * ))) == NULL ) {
425 words[ count ] = word;
426 words[ ++count ] = NULL;
427 word = strtok( NULL, delims );