Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
getfilter.c File Reference
#include "ldap-int.h"
#include "regex.h"
#include <stdio.h>

Go to the source code of this file.

Defines

#define FILT_MAX_LINE_LEN   1024

Functions

static int break_into_words (char *str, char *delims, char ***wordsp)
int nsldapi_next_line_tokens (char **bufp, long *blenp, char ***toksp)
void nsldapi_free_strarray (char **sap)
char *LDAP_CALL re_comp ()
LDAPFiltDesc *LDAP_CALL ldap_init_getfilter (char *fname)
LDAPFiltDesc *LDAP_CALL ldap_init_getfilter_buf (char *buf, long buflen)
int LDAP_CALL ldap_set_filter_additions (LDAPFiltDesc *lfdp, char *prefix, char *suffix)
void LDAP_CALL ldap_setfilteraffixes (LDAPFiltDesc *lfdp, char *prefix, char *suffix)
LDAPFiltInfo *LDAP_CALL ldap_getfirstfilter (LDAPFiltDesc *lfdp, char *tagpat, char *value)
LDAPFiltInfo *LDAP_CALL ldap_getnextfilter (LDAPFiltDesc *lfdp)
static char * filter_add_strn (char *f, char *flimit, char *v, size_t vlen)
static char * filter_add_value (char *f, char *flimit, char *v, int escape_all)
int LDAP_CALL ldap_create_filter (char *filtbuf, unsigned long buflen, char *pattern, char *prefix, char *suffix, char *attr, char *value, char **valwords)
void LDAP_CALL ldap_build_filter (char *filtbuf, unsigned long buflen, char *pattern, char *prefix, char *suffix, char *attr, char *value, char **valwords)

Define Documentation

Definition at line 63 of file getfilter.c.


Function Documentation

static int break_into_words ( char *  str,
char *  delims,
char ***  wordsp 
) [static]

Definition at line 525 of file getfilter.c.

{
    char      *word, **words;
    int              count;
    char      *lasts;
       
    if (( words = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
       return( -1 );
    }
    count = 0;
    words[ count ] = NULL;

    word = ldap_utf8strtok_r( str, delims, &lasts );
    while ( word != NULL ) {
       if (( words = (char **)NSLDAPI_REALLOC( words,
              ( count + 2 ) * sizeof( char * ))) == NULL ) {
           return( -1 );
       }

       words[ count ] = word;
       words[ ++count ] = NULL;
       word = ldap_utf8strtok_r( NULL, delims, &lasts );
    }
       
    *wordsp = words;
    return( count );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* filter_add_strn ( char *  f,
char *  flimit,
char *  v,
size_t  vlen 
) [static]

Definition at line 337 of file getfilter.c.

{
    auto size_t flen = flimit - f;
    if ( vlen > flen ) { /* flimit is too small */
       if ( flen > 0 ) SAFEMEMCPY( f, v, flen );
       return NULL;
    }
    if ( vlen > 0 ) SAFEMEMCPY( f, v, vlen );
    return f + vlen;
}

Here is the caller graph for this function:

static char* filter_add_value ( char *  f,
char *  flimit,
char *  v,
int  escape_all 
) [static]

Definition at line 352 of file getfilter.c.

{
    auto char x[4];
    auto size_t slen;
    while ( f && *v ) {
       switch ( *v ) {
       case '*':
           if ( escape_all ) {
              f = filter_add_strn( f, flimit, "\\2a", 3 );
              v++;
           } else {
              if ( f < flimit ) {
                  *f++ = *v++;
              } else {
                  f = NULL; /* overflow */
              }
           }
           break;

       case '(':
       case ')':
           sprintf( x, "\\%02x", (unsigned)*v );
           f = filter_add_strn( f, flimit, x, 3 );
           v++;
           break;

       case '\\':
           if ( escape_all ) {
              f = filter_add_strn( f, flimit, "\\5c", 3 );
              v++;
           } else {
              slen = (ldap_utf8isxdigit( v+1 ) &&
                     ldap_utf8isxdigit( v+2 )) ? 3 : (v[1] ? 2 : 1);
              f = filter_add_strn( f, flimit, v, slen );
              v += slen;
           }
           break;
           
       default:
           if ( f < flimit ) {
              *f++ = *v++;
           } else {
              f = NULL; /* overflow */
           }
           break;
       }
    }
    return f;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void LDAP_CALL ldap_build_filter ( char *  filtbuf,
unsigned long  buflen,
char *  pattern,
char *  prefix,
char *  suffix,
char *  attr,
char *  value,
char **  valwords 
)

Definition at line 516 of file getfilter.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ldap_create_filter ( char *  filtbuf,
unsigned long  buflen,
char *  pattern,
char *  prefix,
char *  suffix,
char *  attr,
char *  value,
char **  valwords 
)

Definition at line 410 of file getfilter.c.

{
       char   *p, *f, *flimit;
       int    i, wordcount, wordnum, endwordnum, escape_all;

    /* 
     * there is some confusion on what to create for a filter if 
     * attr or value are null pointers.  For now we just leave them
     * as TO BE DEALT with
     */

       if ( filtbuf == NULL || buflen == 0 || pattern == NULL ){
              return( LDAP_PARAM_ERROR );
       }
       
       if ( valwords == NULL ) {
           wordcount = 0;
       } else {
           for ( wordcount = 0; valwords[ wordcount ] != NULL; ++wordcount ) {
              ;
           }
       }

       f = filtbuf;
       flimit = filtbuf + buflen - 1;

       if ( prefix != NULL ) {
           f = filter_add_strn( f, flimit, prefix, strlen( prefix ));
       }

       for ( p = pattern; f != NULL && *p != '\0'; ++p ) {
           if ( *p == '%' ) {
              ++p;
              if ( *p == 'v' || *p == 'e' ) {
                  escape_all = ( *p == 'e' );
                  if ( ldap_utf8isdigit( p+1 )) {
                     ++p;
                     wordnum = *p - '1';
                     if ( *(p+1) == '-' ) {
                         ++p;
                         if ( ldap_utf8isdigit( p+1 )) {
                            ++p;
                            endwordnum = *p - '1';      /* e.g., "%v2-4" */
                            if ( endwordnum > wordcount - 1 ) {
                                endwordnum = wordcount - 1;
                            }
                         } else {
                            endwordnum = wordcount - 1;  /* e.g., "%v2-" */
                         }
                     } else {
                         endwordnum = wordnum;   /* e.g., "%v2" */
                     }

                     if ( wordcount > 0 ) {
                         for ( i = wordnum; i <= endwordnum; ++i ) {
                            if ( i > wordnum ) {  /* add blank btw words */
                                f = filter_add_strn( f, flimit, " ", 1 );
                                if ( f == NULL ) break;
                            }
                            f = filter_add_value( f, flimit, valwords[ i ],
                                   escape_all );
                            if ( f == NULL ) break;
                         }
                     }
                  } else if ( *(p+1) == '$' ) {
                     ++p;
                     if ( wordcount > 0 ) {
                         wordnum = wordcount - 1;
                         f = filter_add_value( f, flimit,
                                valwords[ wordnum ], escape_all );
                     }
                  } else if ( value != NULL ) {
                     f = filter_add_value( f, flimit, value, escape_all );
                  }
              } else if ( *p == 'a' && attr != NULL ) {
                  f = filter_add_strn( f, flimit, attr, strlen( attr ));
              } else {
                  *f++ = *p;
              }
           } else {
              *f++ = *p;
           }
           if ( f > flimit ) { /* overflow */
              f = NULL;
           }
       }

       if ( suffix != NULL && f != NULL) {
           f = filter_add_strn( f, flimit, suffix, strlen( suffix ));
       }

       if ( f == NULL ) {
           *flimit = '\0';
           return( LDAP_SIZELIMIT_EXCEEDED );
       }
       *f = '\0';
       return( LDAP_SUCCESS );
}

Here is the call graph for this function:

Here is the caller graph for this function:

LDAPFiltInfo* LDAP_CALL ldap_getfirstfilter ( LDAPFiltDesc *  lfdp,
char *  tagpat,
char *  value 
)

Definition at line 268 of file getfilter.c.

{
    LDAPFiltList     *flp;

    if ( lfdp == NULL || tagpat == NULL || value == NULL ) {
       return( NULL );      /* punt */
    }

    if ( lfdp->lfd_curvalcopy != NULL ) {
       NSLDAPI_FREE( lfdp->lfd_curvalcopy );
       NSLDAPI_FREE( lfdp->lfd_curvalwords );
    }

    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;
       }
    }

    if ( lfdp->lfd_curfip == NULL ) {
       return( NULL );
    }

    if (( lfdp->lfd_curvalcopy = nsldapi_strdup( value )) == NULL ) {
       return( NULL );
    }

    if ( break_into_words( lfdp->lfd_curvalcopy, flp->lfl_delims,
              &lfdp->lfd_curvalwords ) < 0 ) {
       NSLDAPI_FREE( lfdp->lfd_curvalcopy );
       lfdp->lfd_curvalcopy = NULL;
       return( NULL );
    }

    return( ldap_getnextfilter( lfdp ));
}

Here is the call graph for this function:

Here is the caller graph for this function:

LDAPFiltInfo* LDAP_CALL ldap_getnextfilter ( LDAPFiltDesc *  lfdp)

Definition at line 314 of file getfilter.c.

{
    LDAPFiltInfo     *fip;

    if ( lfdp == NULL || ( fip = lfdp->lfd_curfip ) == NULL ) {
       return( NULL );
    }

    lfdp->lfd_curfip = fip->lfi_next;

    ldap_build_filter( lfdp->lfd_filter, LDAP_FILT_MAXSIZ, fip->lfi_filter,
           lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, NULL,
           lfdp->lfd_curval, lfdp->lfd_curvalwords );
    lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
    lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
    lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
    lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;

    return( &lfdp->lfd_retfi );
}

Here is the call graph for this function:

Here is the caller graph for this function:

LDAPFiltDesc* LDAP_CALL ldap_init_getfilter ( char *  fname)

Definition at line 67 of file getfilter.c.

{
    FILE             *fp;
    char             *buf;
    long             rlen, len;
    int              eof;
    LDAPFiltDesc     *lfdp;

    if (( fp = fopen( fname, "r" )) == NULL ) {
       return( NULL );
    }

    if ( fseek( fp, 0L, SEEK_END ) != 0 ) {      /* move to end to get len */
       fclose( fp );
       return( NULL );
    }

    len = ftell( fp );

    if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {      /* back to start of file */
       fclose( fp );
       return( NULL );
    }

    if (( buf = NSLDAPI_MALLOC( (size_t)len )) == NULL ) {
       fclose( fp );
       return( NULL );
    }

    rlen = fread( buf, 1, (size_t)len, fp );
    eof = feof( fp );
    fclose( fp );

    if ( rlen != len && !eof ) {   /* error:  didn't get the whole file */
       NSLDAPI_FREE( buf );
       return( NULL );
    }


    lfdp = ldap_init_getfilter_buf( buf, rlen );
    NSLDAPI_FREE( buf );

    return( lfdp );
}

Here is the call graph for this function:

Here is the caller graph for this function:

LDAPFiltDesc* LDAP_CALL ldap_init_getfilter_buf ( char *  buf,
long  buflen 
)

Definition at line 115 of file getfilter.c.

{
    LDAPFiltDesc     *lfdp;
    LDAPFiltList     *flp, *nextflp;
    LDAPFiltInfo     *fip, *nextfip;
    char             *tag, **tok;
    int                     tokcnt, i;

    if ( (buf == NULL) || (buflen < 0) ||
        ( lfdp = (LDAPFiltDesc *)NSLDAPI_CALLOC(1, sizeof( LDAPFiltDesc)))
        == NULL ) {
       return( NULL );
    }

    flp = nextflp = NULL;
    fip = NULL;
    tag = NULL;

    while ( buflen > 0 && ( tokcnt = nsldapi_next_line_tokens( &buf, &buflen,
           &tok )) > 0 ) {
       switch( tokcnt ) {
       case 1:              /* tag line */
           if ( tag != NULL ) {
              NSLDAPI_FREE( tag );
           }
           tag = tok[ 0 ];
           NSLDAPI_FREE( tok );
           break;
       case 4:
       case 5:              /* start of filter info. list */
           if (( nextflp = (LDAPFiltList *)NSLDAPI_CALLOC( 1,
                  sizeof( LDAPFiltList ))) == NULL ) {
              ldap_getfilter_free( lfdp );
              return( NULL );
           }
           nextflp->lfl_tag = nsldapi_strdup( tag );
           nextflp->lfl_pattern = tok[ 0 ];
           if ( re_comp( nextflp->lfl_pattern ) != NULL ) {
              char    msg[256];
              ldap_getfilter_free( lfdp );
              sprintf( msg, "bad regular expresssion %s\n",
                     nextflp->lfl_pattern );
              ber_err_print( msg );
              nsldapi_free_strarray( tok );
              return( NULL );
           }
              
           nextflp->lfl_delims = tok[ 1 ];
           nextflp->lfl_ilist = NULL;
           nextflp->lfl_next = NULL;
           if ( flp == NULL ) {    /* first one */
              lfdp->lfd_filtlist = nextflp;
           } else {
              flp->lfl_next = nextflp;
           }
           flp = nextflp;
           fip = NULL;
           for ( i = 2; i < 5; ++i ) {
              tok[ i - 2 ] = tok[ i ];
           }
           /* fall through */

       case 2:
       case 3:              /* filter, desc, and optional search scope */
           if ( nextflp != NULL ) { /* add to info list */
              if (( nextfip = (LDAPFiltInfo *)NSLDAPI_CALLOC( 1,
                     sizeof( LDAPFiltInfo ))) == NULL ) {
                  ldap_getfilter_free( lfdp );
                  nsldapi_free_strarray( tok );
                  return( NULL );
              }
              if ( fip == NULL ) { /* first one */
                  nextflp->lfl_ilist = nextfip;
              } else {
                  fip->lfi_next = nextfip;
              }
              fip = nextfip;
              nextfip->lfi_next = NULL;
              nextfip->lfi_filter = tok[ 0 ];
              nextfip->lfi_desc = tok[ 1 ];
              if ( tok[ 2 ] != NULL ) {
                  if ( strcasecmp( tok[ 2 ], "subtree" ) == 0 ) {
                     nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;
                  } else if ( strcasecmp( tok[ 2 ], "onelevel" ) == 0 ) {
                     nextfip->lfi_scope = LDAP_SCOPE_ONELEVEL;
                  } else if ( strcasecmp( tok[ 2 ], "base" ) == 0 ) {
                     nextfip->lfi_scope = LDAP_SCOPE_BASE;
                  } else {
                     nsldapi_free_strarray( tok );
                     ldap_getfilter_free( lfdp );
                     return( NULL );
                  }
                  NSLDAPI_FREE( tok[ 2 ] );
                  tok[ 2 ] = NULL;
              } else {
                  nextfip->lfi_scope = LDAP_SCOPE_SUBTREE;     /* default */
              }
              nextfip->lfi_isexact = ( strchr( tok[ 0 ], '*' ) == NULL &&
                     strchr( tok[ 0 ], '~' ) == NULL );
              NSLDAPI_FREE( tok );
           }
           break;

       default:
           nsldapi_free_strarray( tok );
           ldap_getfilter_free( lfdp );
           return( NULL );
       }
    }

    if ( tag != NULL ) {
       NSLDAPI_FREE( tag );
    }

    return( lfdp );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ldap_set_filter_additions ( LDAPFiltDesc *  lfdp,
char *  prefix,
char *  suffix 
)

Definition at line 235 of file getfilter.c.

{
    if ( lfdp == NULL ) {
       return( LDAP_PARAM_ERROR );
    }

    if ( lfdp->lfd_filtprefix != NULL ) {
       NSLDAPI_FREE( lfdp->lfd_filtprefix );
    }
    lfdp->lfd_filtprefix = ( prefix == NULL ) ? NULL : nsldapi_strdup( prefix );

    if ( lfdp->lfd_filtsuffix != NULL ) {
       NSLDAPI_FREE( lfdp->lfd_filtsuffix );
    }
    lfdp->lfd_filtsuffix = ( suffix == NULL ) ? NULL : nsldapi_strdup( suffix );

    return( LDAP_SUCCESS );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void LDAP_CALL ldap_setfilteraffixes ( LDAPFiltDesc *  lfdp,
char *  prefix,
char *  suffix 
)

Definition at line 260 of file getfilter.c.

Here is the call graph for this function:

void nsldapi_free_strarray ( char **  sap)

Definition at line 217 of file dsparse.c.

{
    int              i;

    if ( sap != NULL ) {
       for ( i = 0; sap[ i ] != NULL; ++i ) {
           NSLDAPI_FREE( sap[ i ] );
       }
       NSLDAPI_FREE( (char *)sap );
    }
}
int nsldapi_next_line_tokens ( char **  bufp,
long blenp,
char ***  toksp 
)

Definition at line 60 of file dsparse.c.

{
    char      *p, *line, *token, **toks;
    int              rc, tokcnt;

    *toksp = NULL;

    if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
       return( rc );
    }

    if (( toks = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
       NSLDAPI_FREE( line );
       return( -1 );
    }
    tokcnt = 0;

    p = line;
    while (( token = next_token( &p )) != NULL ) {
       if (( toks = (char **)NSLDAPI_REALLOC( toks, ( tokcnt + 2 ) *
              sizeof( char * ))) == NULL ) {
           NSLDAPI_FREE( (char *)toks );
           NSLDAPI_FREE( line );
           return( -1 );
       }
       toks[ tokcnt ] = token;
       toks[ ++tokcnt ] = NULL;
    }

    if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
       tokcnt = 0;
       nsldapi_free_strarray( toks );
       toks = NULL;
    }

    NSLDAPI_FREE( line );

    if ( tokcnt == 0 ) {
       if ( toks != NULL ) {
           NSLDAPI_FREE( (char *)toks );
       }
    } else {
       *toksp = toks;
    }

    return( tokcnt );
}
char* LDAP_CALL re_comp ( )

Here is the caller graph for this function: