summaryrefslogtreecommitdiffstats
path: root/ldap_match_mapped.c
blob: d457a1ac203665fc9ee968ae440918aa314698a6 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "ldif.h"
#include "byte.h"
#include <str.h>
#include "uint32.h"
#include "case.h"
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

extern char* map;
extern long filelen;
extern uint32 magic,attribute_count,record_count,indices_offset,size_of_string_table;
extern uint32 dn_ofs,objectClass_ofs;

int substringmatch(struct Substring* x,const char* attr,int ignorecase) {
  int (*diff)(const void* a, unsigned long len, const void* b);
  if (ignorecase)
    diff=case_diffb;
  else
    diff=byte_diff;
  while (x) {
    unsigned long i;
    if (x->s.l>str_len(attr)) return 0;
    switch (x->substrtype) {
    case prefix:
      if (diff(x->s.s,x->s.l,attr)) return 0;
found:
      break;
    case any:
      {
	unsigned long len=str_len(attr);
	if (len<x->s.l) return 0;
	for (i=0; i<=len-x->s.l; ++i)
	  if (!diff(x->s.s,x->s.l,attr+i))
	    goto found;
      }
      return 0;
    case suffix:
      if (diff(x->s.s,x->s.l,attr+str_len(attr)-x->s.l)) return 0;
    }
    x=x->next;
  }
  return 1;
}

int ldap_match_present(uint32 ofs,uint32 attrofs) {
  uint32 j,k;
  if (attrofs==dn_ofs || attrofs==objectClass_ofs) return 1;
  uint32_unpack(map+ofs,&j);
  for (k=2; k<j; ++k)
    if (uint32_read(map+ofs+k*8)==attrofs)
      return 1;
  return 0;
}

uint32 ldap_find_attr_value(uint32 ofs,uint32 attrofs) {
  uint32 j,k;
  if (attrofs==dn_ofs) return uint32_read(map+ofs+8);
  if (attrofs==objectClass_ofs) return uint32_read(map+ofs+12);
  uint32_unpack(map+ofs,&j);
  for (k=2; k<j; ++k)
    if (uint32_read(map+ofs+k*8)==attrofs)
      return uint32_read(map+ofs+k*8+4);
  return 0;
}

int matchint(struct Filter* f,const char* t) {
  int r;

  if (f->attrflag&1)
    r=matchcasestring(&f->ava.value,t);
  else
    r=matchstring(&f->ava.value,t);
  if (f->type==EQUAL) return (r==0);
  if (f->type==LESSEQUAL) return (r>0);
  return (r<0);
}

/* return non-zero if the record matches the search filter */
int ldap_matchfilter_mapped(uint32 ofs,struct Filter* f) {
  struct Filter* y=f->x;
  if (!f) return 1;
  switch (f->type) {
  case AND:
    while (y) {
      if (!ldap_matchfilter_mapped(ofs,y)) return 0;
      y=y->next;
    }
    return 1;
  case OR:
    while (y) {
      if (ldap_matchfilter_mapped(ofs,y)) return 1;
      y=y->next;
    }
    return 0;
  case NOT:
    return !ldap_matchfilter_mapped(ofs,y);
  case PRESENT:
    return ldap_match_present(ofs,f->attrofs);
  case EQUAL:
  case LESSEQUAL:
  case GREATEQUAL:
    {
      uint32 i,j,k;
      uint32_unpack(map+ofs,&j);
//      if (!matchstring(&f->ava.desc,"dn")) {
      if (f->attrofs==dn_ofs) {
	uint32_unpack(map+ofs+8,&k);
	return matchint(f,map+k);
//      } else if (!matchstring(&f->ava.desc,"objectName")) {
      } else if (f->attrofs==objectClass_ofs) {
	uint32_unpack(map+ofs+12,&k);
	if (matchint(f,map+k)) return 1;
      }
      for (i=2; i<j; ++i) {
	uint32_unpack(map+ofs+i*8,&k);
//	if (!matchstring(&f->ava.desc,map+k)) {
	if (f->attrofs==k) {
	  uint32_unpack(map+ofs+i*8+4,&k);
	  if (matchint(f,map+k)) return 1;
	}
      }
      return 0;
    }
    break;
  case SUBSTRING:
    {
      uint32 i,j,k;
      uint32_unpack(map+ofs,&j);
//      if (matchstring(&f->ava.desc,"dn")) {
      if (f->attrofs==dn_ofs) {
	uint32_unpack(map+ofs+8,&k);
	if (substringmatch(f->substrings,map+k,f->attrflag&1)) return 1;
	return 0;
//      } else if (matchstring(&f->ava.desc,"objectName")) {
      } else if (f->attrofs==objectClass_ofs) {
	uint32_unpack(map+ofs+12,&k);
	if (substringmatch(f->substrings,map+k,f->attrflag&1)) return 1;
      }
      for (i=2; i<j; ++i) {
	uint32_unpack(map+ofs+i*8,&k);
//	if (!matchstring(&f->ava.desc,map+k)) {
	if (f->attrofs==k) {
	  uint32_unpack(map+ofs+i*8+4,&k);
	  if (substringmatch(f->substrings,map+k,f->attrflag&1))
	    return 1;
	}
      }
      return 0;
    }
    break;
  default:
    write(2,"unsupported query type\n",23);
    return 0;
  }
  return 1;
}

/* return 0 if they didn't match, otherwise return length in b */
static int match(const char* a,int len,const char* b) {
  const char* A=a+len;
  const char* B=b+str_len(b);
  while (len>0 && A>a && B>b) {
    --A; --B; --len;
    while (*A==' ' && A>a) { --A; --len; }
    while (*B==' ' && B>b) --B;
    if (tolower(*A) != tolower(*B))
      return 0;
  }
  return str_len(B);
}

/* return non-zero if the record matches the search request */
int ldap_match_mapped(uint32 ofs,struct SearchRequest* sr) {
  unsigned int l,i;
  uint32 k;
  uint32_unpack(map+ofs+8,&k);
  l=str_len(map+k);
  /* first see if baseObject is a suffix of dn */
  if (sr->baseObject.l>l) {
//    puts("fail: baseObject longer than dn");
    return 0;
  }
  /* we want "o=foo, o=bar" and "o=FOO,o=baR" to be equal */
  if (sr->baseObject.l && !match(sr->baseObject.s,sr->baseObject.l,map+k)) {
//    puts("fail: not suffix");
    return 0;
  }
  /* it is.  If scope==wholeSubtree, the scope check is also done */
  switch (sr->scope) {
  case wholeSubtree: break;
  case baseObject: if (l==sr->baseObject.l) break; return 0;
  default:
    i=str_chr(map+k,',');
    if (i+2>=l-sr->baseObject.l) break;
    return 0;
  }
  return ldap_matchfilter_mapped(ofs,sr->filter);
}