From 706ae2e927ba8d7178389319f78eb2c85e4155e8 Mon Sep 17 00:00:00 2001 From: leitner Date: Sun, 2 Sep 2018 12:11:21 +0000 Subject: [PATCH] add u flag to addindex (for hash index, only index records with userPassword for authentication) --- addindex.c | 117 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 46 deletions(-) diff --git a/addindex.c b/addindex.c index 763f8d9..d1348ce 100644 --- a/addindex.c +++ b/addindex.c @@ -68,11 +68,12 @@ int main(int argc,char* argv[]) { enum { SORTEDTABLE, HASHTABLE } mode; size_t filelen; char* filename=argv[1]; + const char* lookfor=argv[2]; uint32 magic,attribute_count,record_count,indices_offset,size_of_string_table; uint32 wanted,casesensitive,dn,objectClass; - int ignorecase,fastindex; + int ignorecase,fastindex,onlywithpassword; - ignorecase=fastindex=0; + ignorecase=fastindex=onlywithpassword=0; errmsg_iam("addindex"); mstorage_init(&idx); @@ -81,7 +82,8 @@ int main(int argc,char* argv[]) { buffer_putsflush(buffer_2,"usage: ./addindex filename attribute [i][f][h]\n" "if i is present, make index case insensitive.\n" "if f is present, make index twice as large, but quicker.\n" - "if h is present, make it a hash index (only accelerates direct lookups)\n"); + "if h is present, make it a hash index (only accelerates direct lookups)\n" + "if u is present with h, only hash entries with userPassword\n"); return 1; } @@ -90,7 +92,18 @@ int main(int argc,char* argv[]) { if (strchr(argv[3],'i')) ignorecase=1; if (strchr(argv[3],'f')) fastindex=1; if (strchr(argv[3],'h')) mode=HASHTABLE; + if (strchr(argv[3],'u')) onlywithpassword=1; } + + if (mode!=HASHTABLE && onlywithpassword) + die(111,"u only implemented with h\n"); + + if (onlywithpassword && strcmp(lookfor,"dn")) + die(111,"u only works if attribute is dn\n"); + + if (onlywithpassword) + lookfor="userPassword"; + map=(char*)mmap_read(filename,&filelen); if (!map) diesys(111,"Could not open \"",filename,"\""); @@ -112,7 +125,7 @@ int main(int argc,char* argv[]) { // buffer_puts(buffer_1,map+j); buffer_putsflush(buffer_1,"\n"); if (!strcasecmp(map+j,"dn")) dn=j; if (!strcasecmp(map+j,"objectClass")) objectClass=j; - if (!strcasecmp(map+j,argv[2])) { + if (!strcasecmp(map+j,lookfor)) { // buffer_putsflush(buffer_2,"found attribute!\n"); wanted=j; casesensitive=x+attribute_count*4-map; uint32_unpack(map+casesensitive,&j); @@ -216,7 +229,7 @@ int main(int argc,char* argv[]) { uint32 maxcoll,mincoll,cmaxcoll,nmaxcoll,nmincoll,cmincoll; uint32 indexsize; cmaxcoll=cmincoll=0; /* shut gcc up */ - if (wanted==dn) + if (wanted==dn && !onlywithpassword) counted=record_count; else { x=map+5*4+size_of_string_table+attribute_count*8; @@ -259,9 +272,16 @@ int main(int argc,char* argv[]) { for (; j>2; --j) { uint32_unpack(x,&k); if (k==wanted) { - y[cur].recnum=i; - y[cur].hashcode=hashmapped(k,ignorecase); - ++cur; + if (onlywithpassword) { + uint32_unpack(x-8,&k); + y[cur].recnum=i; + y[cur].hashcode=hashmapped(k,ignorecase); + ++cur; + } else { + y[cur].recnum=i; + y[cur].hashcode=hashmapped(k,ignorecase); + ++cur; + } } x+=8; } @@ -274,46 +294,48 @@ int main(int argc,char* argv[]) { maxtabsize=counted+counted/8; tab=malloc(maxtabsize*sizeof(struct htentry)); if (!tab) die(111,"out of memory"); - maxcoll=nmaxcoll=nmincoll=0; mincoll=-1; - for (; i1) ++k; - if (tab[l].count==2) ++chains; - } - if (k>maxcoll) { - nmaxcoll=i; - maxcoll=k; - cmaxcoll=chains; - } - if (k 100) { + maxcoll=nmaxcoll=nmincoll=0; mincoll=-1; + for (; i1) ++k; + if (tab[l].count==2) ++chains; + } + if (k>maxcoll) { + nmaxcoll=i; + maxcoll=k; + cmaxcoll=chains; + } + if (k