summaryrefslogtreecommitdiffstats
path: root/scan_ldapsearchrequest.c
blob: 94163db61c7ac8d383dbf5830bd8371f888ed500 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdlib.h>
#include "ldap.h"

size_t scan_ldapsearchrequest(const char* src,const char* max,
			      struct SearchRequest* s) {
  size_t res,tmp,elen;
  unsigned long etmp;
  signed long ltmp;
  s->attributes=0;
  s->filter=0;
  if (!(res=scan_ldapstring(src,max,&s->baseObject))) goto error;
  if (!(tmp=scan_asn1ENUMERATED(src+res,max,&etmp))) goto error;
  if (etmp>2) goto error; s->scope=etmp; res+=tmp;
  if (!(tmp=scan_asn1ENUMERATED(src+res,max,&etmp))) goto error;
  if (etmp>3) goto error; s->derefAliases=etmp; res+=tmp;
  if (!(tmp=scan_asn1INTEGER(src+res,max,&ltmp)) || ltmp<0) goto error;
  s->sizeLimit=(unsigned long)ltmp;
  res+=tmp;
  if (!(tmp=scan_asn1INTEGER(src+res,max,&ltmp)) || ltmp<0) goto error;
  s->timeLimit=(unsigned long)ltmp;
  res+=tmp;
  if (!(tmp=scan_asn1BOOLEAN(src+res,max,&s->typesOnly))) goto error;
  res+=tmp;
  if (!(tmp=scan_ldapsearchfilter(src+res,max,&s->filter))) goto error;
  res+=tmp;
  /* now for the attributelist */
  if (!(tmp=scan_asn1SEQUENCE(src+res,max,&elen))) goto error;
  res+=tmp;
  {
    const char* nmax=src+res+elen;
//#define nmax max
    struct AttributeDescriptionList** a=&s->attributes;
    if (nmax>max) goto error;
    for (;;) {
      if (src+res>nmax) goto error;
      if (src+res==nmax) break;
      if (!*a) *a=malloc(sizeof(struct AttributeDescriptionList));
      if (!*a) goto error;
      (*a)->next=0;
      if (!(tmp=scan_ldapstring(src+res,nmax,&(*a)->a))) goto error;
      res+=tmp;
      a=&(*a)->next;
    }
    return res;
  }
error:
  free_ldapsearchrequest(s);
  return 0;
}

void free_ldapsearchrequest(struct SearchRequest* s) {
  if (s->attributes)
    free_ldapadl(s->attributes); // ->next !?
  free_ldapsearchfilter(s->filter);
}