]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getfilter.c
Commit preliminary fix for ldap.conf base usage.
[openldap] / libraries / libldap / getfilter.c
index e9ebc6aa294457ceed11058ff4df09140a697704..e466adc243cd70df73960a0ae09591ace8992777 100644 (file)
@@ -5,53 +5,30 @@
  *  getfilter.c -- optional add-on to libldap
  */
 
+#include "portable.h"
+
 #ifndef lint 
 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
 #endif
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#if defined(NeXT)
-#include <regex.h>
-#endif
-#ifdef MACOS
 #include <stdlib.h>
-#include "macos.h"
-#else /* MACOS */
-#ifdef DOS
-#include <malloc.h>
-#include "msdos.h"
-#else /* DOS */
-#include <sys/types.h>
+
+#include <ac/ctype.h>
+#include <ac/errno.h>
+#include <ac/regex.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <stdlib.h>
-#include <sys/errno.h>
-#ifndef VMS
-#include <unistd.h>
-#endif /* VMS */
-#endif /* DOS */
-#endif /* MACOS */
-
-#include "lber.h"
-#include "ldap.h"
-#include "regex.h"
-
-#ifdef NEEDPROTOS
-static int break_into_words( char *str, char *delims, char ***wordsp );
-int next_line_tokens( char **bufp, long *blenp, char ***toksp );
-void free_strarray( char **sap );
-#else /* NEEDPROTOS */
-static int break_into_words();
-int next_line_tokens();
-void free_strarray();
-#endif /* NEEDPROTOS */
-
-#if !defined( MACOS ) && !defined( DOS )
-extern int     errno;
-extern char    *re_comp();
 #endif
 
+#include "ldap-int.h"
+
+static int break_into_words LDAP_P(( char *str, char *delims, char ***wordsp ));
+
 #define FILT_MAX_LINE_LEN      1024
 
 LDAPFiltDesc *
@@ -107,8 +84,10 @@ ldap_init_getfilter_buf( char *buf, long buflen )
     LDAPFiltDesc       *lfdp;
     LDAPFiltList       *flp, *nextflp;
     LDAPFiltInfo       *fip, *nextfip;
-    char               *tag, **tok;
-    int                        tokcnt, i;
+    char                       *tag, **tok;
+    int                                tokcnt, i;
+       int                             rc;
+       regex_t                 re;
 
     if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
        return( NULL );
@@ -136,20 +115,21 @@ ldap_init_getfilter_buf( char *buf, long buflen )
                ldap_getfilter_free( lfdp );
                return( NULL );
            }
-           nextflp->lfl_tag = strdup( tag );
+           nextflp->lfl_tag = ldap_strdup( tag );
            nextflp->lfl_pattern = tok[ 0 ];
-           if ( re_comp( nextflp->lfl_pattern ) != NULL ) {
-#ifndef NO_USERINTERFACE
+           if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
+#ifdef LDAP_LIBUI
+               char error[512];
+               regerror(rc, &re, error, sizeof(error));
                ldap_getfilter_free( lfdp );
-               fprintf( stderr, "bad regular expresssion %s\n",
-                       nextflp->lfl_pattern );
-#if !defined( MACOS ) && !defined( DOS )
+               fprintf( stderr, "bad regular expresssion %s, %s\n",
+                       nextflp->lfl_pattern, error );
                errno = EINVAL;
-#endif
-#endif /* NO_USERINTERFACE */
+#endif /* LDAP_LIBUI */
                free_strarray( tok );
                return( NULL );
            }
+               regfree(&re);
                
            nextflp->lfl_delims = tok[ 1 ];
            nextflp->lfl_ilist = NULL;
@@ -194,9 +174,7 @@ ldap_init_getfilter_buf( char *buf, long buflen )
                    } else {
                        free_strarray( tok );
                        ldap_getfilter_free( lfdp );
-#if !defined( MACOS ) && !defined( DOS )
                        errno = EINVAL;
-#endif
                        return( NULL );
                    }
                    free( tok[ 2 ] );
@@ -213,9 +191,7 @@ ldap_init_getfilter_buf( char *buf, long buflen )
        default:
            free_strarray( tok );
            ldap_getfilter_free( lfdp );
-#if !defined( MACOS ) && !defined( DOS )
            errno = EINVAL;
-#endif
            return( NULL );
        }
     }
@@ -234,12 +210,12 @@ ldap_setfilteraffixes( LDAPFiltDesc *lfdp, char *prefix, char *suffix )
     if ( lfdp->lfd_filtprefix != NULL ) {
        free( lfdp->lfd_filtprefix );
     }
-    lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : strdup( prefix );
+    lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : ldap_strdup( prefix );
 
     if ( lfdp->lfd_filtsuffix != NULL ) {
        free( lfdp->lfd_filtsuffix );
     }
-    lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : strdup( suffix );
+    lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : ldap_strdup( suffix );
 }
 
 
@@ -247,6 +223,8 @@ LDAPFiltInfo *
 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
 {
     LDAPFiltList       *flp;
+       int                             rc;
+       regex_t                 re;
 
     if ( lfdp->lfd_curvalcopy != NULL ) {
        free( lfdp->lfd_curvalcopy );
@@ -256,20 +234,37 @@ ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
     lfdp->lfd_curval = value;
     lfdp->lfd_curfip = NULL;
 
-    for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
-       if ( re_comp( tagpat ) == NULL && re_exec( flp->lfl_tag ) == 1
-               && re_comp( flp->lfl_pattern ) == NULL
-               && re_exec( lfdp->lfd_curval ) == 1 ) {
-           lfdp->lfd_curfip = flp->lfl_ilist;
-           break;
-       }
+       for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
+               /* compile tagpat, continue if we fail */
+               if (regcomp(&re, tagpat, 0) != 0)
+                       continue;
+
+               /* match tagpatern and tag, continue if we fail */
+               rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
+               regfree(&re);
+               if (rc != 0)
+                       continue;
+
+               /* compile flp->ifl_pattern, continue if we fail */
+               if (regcomp(&re, flp->lfl_pattern, 0) != 0)
+                       continue;
+
+               /* match ifl_pattern and lfd_curval, continue if we fail */
+               rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
+               regfree(&re);
+               if (rc != 0)
+                       continue;
+
+               /* we successfully compiled both patterns and matched both values */
+               lfdp->lfd_curfip = flp->lfl_ilist;
+               break;
     }
 
     if ( lfdp->lfd_curfip == NULL ) {
        return( NULL );
     }
 
-    if (( lfdp->lfd_curvalcopy = strdup( value )) == NULL ) {
+    if (( lfdp->lfd_curvalcopy = ldap_strdup( value )) == NULL ) {
        return( NULL );
     }
 
@@ -388,14 +383,16 @@ ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
                *f++ = *p;
            }
                
-           if ( f - filtbuf > buflen ) {
+           if ( (unsigned long) (f - filtbuf) > buflen ) {
                /* sanity check */
                --f;
                break;
            }
        }
 
-       if ( suffix != NULL && ( f - filtbuf ) < buflen ) {
+       if ( suffix != NULL && (
+               (unsigned long) ( f - filtbuf ) < buflen ) )
+       {
            strcpy( f, suffix );
        } else {
            *f = '\0';