From 892bc1056b54e9cd9a19447b2ee2f3b27ff25f07 Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 30 Aug 2002 22:36:59 +0000 Subject: [PATCH] fix binary attributes based on code from Thomas Walpuski fix ldapclient buffering fix potential segfault in server error handling --- Makefile | 4 ++-- TODO | 1 + idx2ldif.c | 2 +- ldap_match_mapped.c | 1 - ldapclient.c | 39 +++++++++++++++++++++++++++++++-------- ldif_parse.c | 4 ++-- mduptab_add.c | 28 +++++++--------------------- mstorage.h | 7 +++++++ parse.c | 5 +++-- scan_ldapsearchrequest.c | 3 ++- tinyldap.c | 4 ++-- 11 files changed, 58 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 842c21b..241b693 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ free_ldapadl.o free_ldappal.o free_ldapsearchfilter.o ldif.a: ldif_parse.o ldap_match.o ldap_match_mapped.o storage.a: strstorage.o strduptab.o mstorage_add.o mduptab_add.o \ -bstr_diff.o mduptab_adds.o bstr_diff2.o +bstr_diff.o mduptab_adds.o bstr_diff2.o mstorage_add_bin.o auth.a: auth.o @@ -63,7 +63,7 @@ tinyldap_debug: tinyldap.c clean: rm -f t t[1-9] *.[ao] bindrequest tinyldap ldapclient data \ parse tinyldap_standalone tinyldap_debug ldapclient_str addindex \ -dumpidx idx2ldif *.da *.bbg *.bb *.gcov gmon.out +dumpidx idx2ldif md5password *.da *.bbg *.bb *.gcov gmon.out tar: clean cd ..; tar cvvf tinyldap.tar.bz2 tinyldap --use=bzip2 --exclude capture --exclude CVS --exclude exp.ldif --exclude polyp* --exclude rfc* diff --git a/TODO b/TODO index 5fe9264..8ca14fa 100644 --- a/TODO +++ b/TODO @@ -2,3 +2,4 @@ - add auth method (openldap md5: base64, 4 bytes salt, direct hash) - add write support with an external journal - add ACLs + diff --git a/idx2ldif.c b/idx2ldif.c index 50e292c..901e85c 100644 --- a/idx2ldif.c +++ b/idx2ldif.c @@ -13,7 +13,7 @@ static void dumpbstr(const char* c) { l=bstrlen(c); d=bstrfirst(c); for (i=0; is.l>strlen(attr)) return 0; for (i=0; is.l-strlen(attr); ++i) { if (!diff(x->s.s,x->s.l,attr+i)) goto found; } diff --git a/ldapclient.c b/ldapclient.c index e72c8d4..d7061ea 100644 --- a/ldapclient.c +++ b/ldapclient.c @@ -104,22 +104,39 @@ usage: char* max; struct SearchResultEntry sre; int matches=0; + len=0; for (;;) { long slen,mid,op; - len=0; + int cur; + tmp=read(sock,buf+len,sizeof(buf)-len); + +#if 0 + buffer_puts(buffer_2,"DEBUG: read "); + buffer_putulong(buffer_2,tmp); + buffer_putsflush(buffer_2," bytes.\n"); +#endif + if (tmp<=0) { buffer_putsflush(buffer_2,"read error.\n"); return 0; } len+=tmp; + cur=0; nextmessage: - if ((tmp2=scan_ldapmessage(buf,buf+len,&mid,&op,&slen))) { - max=buf+slen+tmp2; + if ((tmp2=scan_ldapmessage(buf+cur,buf+len,&mid,&op,&slen))) { + max=buf+cur+slen+tmp2; if (op==SearchResultEntry) { ++matches; - if ((tmp=scan_ldapsearchresultentry(buf+tmp2,max,&sre))) { + if ((tmp=scan_ldapsearchresultentry(buf+cur+tmp2,max,&sre))) { struct PartialAttributeList* pal=sre.attributes; + +#if 0 + buffer_puts(buffer_2,"DEBUG: sre size "); + buffer_putulong(buffer_2,tmp); + buffer_putsflush(buffer_2,".\n"); +#endif + buffer_puts(buffer_1,"objectName \""); buffer_put(buffer_1,sre.objectName.s,sre.objectName.l); buffer_puts(buffer_1,"\"\n"); @@ -137,7 +154,7 @@ nextmessage: pal=pal->next; } } else - buffer_putsflush(buffer_1,"punt!\n"); + goto copypartialandcontinue; } else if (op==SearchResultDone) { if (!matches) buffer_putsflush(buffer_2,"no matches.\n"); @@ -147,11 +164,17 @@ nextmessage: return 0; } if (maxnext=m; @@ -178,7 +178,7 @@ lookagain: char* newdn=alloca(n-i+1); if ((val=mstorage_add(&stringtable,newdn,normalize_dn(newdn,payload.s,len)))<0) goto nomem; } else - if ((val=mstorage_add(&stringtable,payload.s,len))<0) goto nomem; + if ((val=mstorage_add_bin(&stringtable,payload.s,len))<0) goto nomem; addattribute(l,tmp,val); #endif } while (!eof); diff --git a/mduptab_add.c b/mduptab_add.c index ad08ab9..8ea661c 100644 --- a/mduptab_add.c +++ b/mduptab_add.c @@ -9,29 +9,15 @@ long mduptab_add(mduptab_t* t,const char* s,unsigned int len) { unsigned int i; unsigned long* l=(unsigned long*)t->table.root; - static char zero; - int binary=0; + long x,bak; for (i=0; istrings.used/sizeof(unsigned long); ++i) if (bstr_equal2(t->strings.root+l[i],s,len)) return l[i]; - for (i=0; istrings,&zero,1); - uint32_pack(intbuf,len); - mstorage_add(&t->strings,intbuf,4); - mstorage_add(&t->strings,s,len); - } else { - x=mstorage_add(&t->strings,s,len); - mstorage_add(&t->strings,&zero,1); - } - if (mstorage_add(&t->table,(const char*)&x,sizeof(x))<0) { - t->strings.used-=strlen(s)+1; - return -1; - } - return x; + bak=t->strings.used; + if ((x=mstorage_add_bin(&t->strings,s,len))<0) return -1; + if (mstorage_add(&t->table,(const char*)&x,sizeof(x))<0) { + t->strings.used=bak; + return -1; } + return x; } diff --git a/mstorage.h b/mstorage.h index e227099..3170ebd 100644 --- a/mstorage.h +++ b/mstorage.h @@ -18,4 +18,11 @@ long mstorage_add(mstorage_t* p,const char* s,unsigned long n); /* undo mapping */ void mstorage_unmap(mstorage_t* p); +/* this is tinyldap specific. If the data contains at least one 0-byte, + * it is stored in a tinyldap specific encoding: + * char 0; + * uint32 len; + * char data[len] */ +long mstorage_add_bin(mstorage_t* p,const char* s,unsigned long n); + #endif diff --git a/parse.c b/parse.c index a5e5dde..721154d 100644 --- a/parse.c +++ b/parse.c @@ -40,13 +40,13 @@ void dumprec(struct ldaprec* l) { buffer_putsflush(buffer_1,"\n"); } -int main() { +int main(int argc,char* argv[]) { int fd; long len; unsigned long size_of_string_table,indices_offset,record_count; long offset_stringtable,offset_classes,offset_attributes; char* map,* dest; - ldif_parse("exp.ldif"); + ldif_parse(argc<2?"exp.ldif":argv[1]); if (!first) { buffer_putsflush(buffer_2,"no data?!"); return 1; @@ -84,6 +84,7 @@ int main() { buffer_puts(buffer_2,"record \""); buffer_puts(buffer_2,x->dn+stringtable.root); buffer_putsflush(buffer_2,"\" has no objectClass?!\n"); + dumprec(x); return 1; } ++record_count; diff --git a/scan_ldapsearchrequest.c b/scan_ldapsearchrequest.c index d79e5fb..d7ff998 100644 --- a/scan_ldapsearchrequest.c +++ b/scan_ldapsearchrequest.c @@ -47,6 +47,7 @@ error: } void free_ldapsearchrequest(struct SearchRequest* s) { - free_ldapadl(s->attributes->next); + if (s->attributes) + free_ldapadl(s->attributes->next); free_ldapsearchfilter(s->filter); } diff --git a/tinyldap.c b/tinyldap.c index 0a60eec..72e22dc 100644 --- a/tinyldap.c +++ b/tinyldap.c @@ -17,8 +17,8 @@ #endif #include -#define verbose 1 -#define debug 1 +#define verbose 0 +#define debug 0 char* map; long filelen;