From 51d65b578156802bcee715798fd720db0810cab9 Mon Sep 17 00:00:00 2001 From: leitner Date: Mon, 15 Apr 2002 14:34:20 +0000 Subject: [PATCH] add case insensitive matching and dn normalizing in parse. --- README | 18 ++++++++++++------ addindex.c | 12 ++++++++++-- ldif_parse.c | 31 +++++++++++++++++++++++++++++++ tinyldap.c | 4 ++-- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/README b/README index a2f6a65..50da5f3 100644 --- a/README +++ b/README @@ -11,11 +11,17 @@ some simple forms of SearchRequest, and it can even answer simple queries. tinyldap now supports an external database representation with indexes. -However, the indexes are not used yet by tinyldap. It still iterates -through all records. Use "parse" to create the file "data" from an LDIF -file called "exp.ldif" (I can't give you my test data, sorry). Then use -"addindex" to add indexes if you like. Use "dumpidx" to have the -contents of data displayed on screen. tinyldap has been modified to use -data instead of the in-memory linked list. +Use "parse" to create the file "data" from an LDIF file called +"exp.ldif" (I can't give you my test data, sorry). Then use "addindex" +to add indexes if you like. To make an index case insentive (and the +corresponding attribute, too), give a third argument to addindex (e.g. +"./addindex data sn i"; in case I extend this later, stick with "i"). +Use "dumpidx" to have the contents of data displayed on screen. +tinyldap has been modified to use data instead of the in-memory linked +list. Do _not_ add an index for objectClass! It will not work! + +parse will now normalize dn before writing it to the index. That means +that the attribute names in dn are lowercased, ';' is converted to ',' +and spaces after ';' or ',' are removed. diff --git a/addindex.c b/addindex.c index ca3bde5..277cd0c 100644 --- a/addindex.c +++ b/addindex.c @@ -12,11 +12,19 @@ mstorage_t idx; char* map; int compar(const void* a,const void* b) { - return strcmp(map+*(uint32*)a,map+*(uint32*)b); + int i; + if ((i=strcmp(map+*(uint32*)a,map+*(uint32*)b))) + return i; + else + return *(uint32*)b-*(uint32*)a; } int compari(const void* a,const void* b) { - return strcasecmp(map+*(uint32*)a,map+*(uint32*)b); + int i; + if ((i=strcasecmp(map+*(uint32*)a,map+*(uint32*)b))) + return i; + else + return *(uint32*)b-*(uint32*)a; } int main(int argc,char* argv[]) { diff --git a/ldif_parse.c b/ldif_parse.c index 7e8bd2f..10a5c19 100644 --- a/ldif_parse.c +++ b/ldif_parse.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "mduptab.h" #include "mstorage.h" #include "str.h" @@ -31,6 +32,30 @@ static void addattribute(struct ldaprec** l,long name,long val) { } } +/* "ou=fnord; O=fefe; c=de" -> "ou=fnord,o=fefe,c=de" */ +/* returns the length of the new string */ +static int normalize_dn(char* dest,const char* src,int len) { + int makelower=1; + char* orig=dest; + while (len) { + if (*src==';' || *src==',') { + *dest=','; + while (len>1 && src[1]==' ') { ++src; --len; } + makelower=1; + } else { + if (makelower) + *dest=tolower(*src); + else + *dest=*src; + if (*dest=='=') makelower=0; + } + ++dest; + ++src; + --len; + } + return dest-orig; +} + static int parserec(buffer* b, struct ldaprec** l) { char buf[8192]; int n,i,eof=0,ofs=0; @@ -69,6 +94,9 @@ lookagain: if (tmp==objectClass) { if ((val=mduptab_add(&classes,buf+i))<0) goto nomem; + } else if (tmp==dn) { + char* newdn=alloca(n-i+1); + if ((val=mstorage_add(&stringtable,newdn,normalize_dn(newdn,buf+i,n-i+1)))<0) goto nomem; } else if ((val=mstorage_add(&stringtable,buf+i,n-i+1))<0) goto nomem; addattribute(l,tmp,val); @@ -89,6 +117,9 @@ lookagain: #if 1 if (tmp==objectClass) { if ((val=mduptab_add(&classes,buf+i))<0) goto nomem; + } else if (tmp==dn) { + char* newdn=alloca(n-i+1); + if ((val=mstorage_add(&stringtable,newdn,normalize_dn(newdn,buf+i,n-i+1)))<0) goto nomem; } else if ((val=mstorage_add(&stringtable,buf+i,n-i+1))<0) goto nomem; addattribute(l,tmp,val); diff --git a/tinyldap.c b/tinyldap.c index 5b0bbfe..a84de3a 100644 --- a/tinyldap.c +++ b/tinyldap.c @@ -14,7 +14,7 @@ #endif #define verbose 0 -#define debug 0 +#define debug 1 char* map; long filelen; @@ -28,7 +28,7 @@ uint32 dn_ofs,objectClass_ofs; #define BUFSIZE 8192 -#if (verbose != 0) +#if (debug != 0) /* debugging support functions, adapted from t2.c */ static void printava(struct AttributeValueAssertion* a,const char* rel) { buffer_puts(buffer_2,"[");