3 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static BVarray set_join (BVarray lset, int op, BVarray rset);
16 static BVarray set_chase (SET_GATHER gatherer,
17 void *cookie, BVarray set, struct berval *attr, int closure);
18 static int set_samedn (char *dn1, char *dn2);
21 set_size (BVarray set)
34 set_dispose (BVarray set)
40 set_join (BVarray lset, int op, BVarray rset)
47 if (lset == NULL || lset->bv_val == NULL) {
50 return(ch_calloc(1, sizeof(struct berval)));
56 if (rset == NULL || rset->bv_val == NULL) {
61 i = set_size(lset) + set_size(rset) + 1;
62 set = ch_calloc(i, sizeof(struct berval));
64 /* set_chase() depends on this routine to
65 * keep the first elements of the result
66 * set the same (and in the same order)
69 for (i = 0; lset[i].bv_val; i++)
72 for (i = 0; rset[i].bv_val; i++) {
73 for (j = 0; set[j].bv_val; j++) {
74 if (set_samedn(rset[i].bv_val, set[j].bv_val)) {
75 ch_free(rset[i].bv_val);
76 rset[i].bv_val = NULL;
89 if (lset == NULL || lset->bv_val == NULL || rset == NULL || rset->bv_val == NULL) {
90 set = ch_calloc(1, sizeof(struct berval));
94 last = set_size(set) - 1;
95 for (i = 0; set[i].bv_val; i++) {
96 for (j = 0; rset[j].bv_val; j++) {
97 if (set_samedn(set[i].bv_val, rset[j].bv_val))
100 if (rset[j].bv_val == NULL) {
101 ch_free(set[i].bv_val);
103 set[last].bv_val = NULL;
117 set_chase (SET_GATHER gatherer,
118 void *cookie, BVarray set, struct berval *attr, int closure)
122 struct berval bv = {attr->bv_len, attrstr};
126 return(ch_calloc(1, sizeof(struct berval)));
128 if (set->bv_val == NULL)
131 if (attr->bv_len > (sizeof(attrstr) - 1)) {
135 AC_MEMCPY(attrstr, attr->bv_val, attr->bv_len);
136 attrstr[attr->bv_len] = 0;
138 nset = ch_calloc(1, sizeof(struct berval));
143 for (i = 0; set[i].bv_val; i++) {
144 vals = (gatherer)(cookie, set[i].bv_val, &bv);
146 nset = set_join(nset, '|', vals);
151 for (i = 0; nset[i].bv_val; i++) {
152 vals = (gatherer)(cookie, nset[i].bv_val, &bv);
154 nset = set_join(nset, '|', vals);
164 set_samedn (char *dn1, char *dn2)
168 while (*dn1 == ' ') dn1++;
169 while (*dn2 == ' ') dn2++;
170 while (*dn1 || *dn2) {
171 if (*dn1 != '=' && *dn1 != ','
172 && *dn2 != '=' && *dn2 != ',')
176 if (c1 >= 'a' && c1 <= 'z')
178 if (c2 >= 'a' && c2 <= 'z')
183 while (*dn1 == ' ') dn1++;
184 while (*dn2 == ' ') dn2++;
185 if (*dn1++ != *dn2++)
187 while (*dn1 == ' ') dn1++;
188 while (*dn2 == ' ') dn2++;
195 set_filter (SET_GATHER gatherer,
196 void *cookie, struct berval *fbv, char *user, char *this, BVarray *results)
198 #define IS_SET(x) ( (long)(x) >= 256 )
199 #define IS_OP(x) ( (long)(x) < 256 )
200 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
201 #define SF_TOP() ( (BVarray)( (stp < 0) ? 0 : stack[stp] ) )
202 #define SF_POP() ( (BVarray)( (stp < 0) ? 0 : stack[stp--] ) )
203 #define SF_PUSH(x) do { \
204 if (stp >= 63) SF_ERROR(overflow); \
205 stack[++stp] = (BVarray)(long)(x); \
210 int len, op, rc, stp;
211 char c, *filter = fbv->bv_val;
217 while ((c = *filter++)) {
227 if (IS_SET(SF_TOP()))
236 if (SF_TOP() == (void *)'(') {
240 } else if (IS_OP(SF_TOP())) {
244 set = set_join(lset, op, set);
259 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
262 } else if (IS_OP(SF_TOP())) {
265 set = set_join(lset, op, set);
277 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
280 (c = *filter++) && (c != ']');
286 set = ch_calloc(2, sizeof(struct berval));
289 set->bv_val = ch_calloc(len + 1, sizeof(char));
290 if (set->bv_val == NULL)
292 AC_MEMCPY(set->bv_val, &filter[-len - 1], len);
302 /* fall through to next case */
312 && (c < 'A' || c > 'Z')
313 && (c < 'a' || c > 'z'))
320 && ((c >= '0' && c <= '9')
321 || (c >= 'A' && c <= 'Z')
322 || (c >= 'a' && c <= 'z'));
326 && memcmp("this", filter, len) == 0)
328 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
330 set = ch_calloc(2, sizeof(struct berval));
333 ber_str2bv( this, 0, 1, set );
334 if (set->bv_val == NULL)
337 && memcmp("user", filter, len) == 0)
339 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
341 set = ch_calloc(2, sizeof(struct berval));
344 ber_str2bv( user, 0, 1, set );
345 if (set->bv_val == NULL)
347 } else if (SF_TOP() != (void *)'/') {
354 set = set_chase(gatherer,
355 cookie, SF_POP(), &fb2, c == '*');
373 } else if (IS_OP(SF_TOP())) {
376 set = set_join(lset, op, set);
392 while ((set = SF_POP())) {