3 * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static BerVarray set_join (SetCookie *cp, BerVarray lset, int op, BerVarray rset);
16 static BerVarray set_chase (SLAP_SET_GATHER gatherer,
17 SetCookie *cookie, BerVarray set, struct berval *attr, int closure);
18 static int set_samedn (char *dn1, char *dn2);
21 slap_set_size (BerVarray set)
34 slap_set_dispose (SetCookie *cp, BerVarray set)
36 ber_bvarray_free_x(set, cp->op->o_tmpmemctx);
40 set_join (SetCookie *cp, BerVarray lset, int op, BerVarray rset)
47 if (lset == NULL || lset->bv_val == NULL) {
50 return(cp->op->o_tmpcalloc(1, sizeof(struct berval),
51 cp->op->o_tmpmemctx));
54 slap_set_dispose(cp, lset);
57 if (rset == NULL || rset->bv_val == NULL) {
58 slap_set_dispose(cp, rset);
62 i = slap_set_size(lset) + slap_set_size(rset) + 1;
63 set = cp->op->o_tmpcalloc(i, sizeof(struct berval), cp->op->o_tmpmemctx);
65 /* set_chase() depends on this routine to
66 * keep the first elements of the result
67 * set the same (and in the same order)
70 for (i = 0; lset[i].bv_val; i++)
72 cp->op->o_tmpfree(lset, cp->op->o_tmpmemctx);
73 for (i = 0; rset[i].bv_val; i++) {
74 for (j = 0; set[j].bv_val; j++) {
75 if (set_samedn(rset[i].bv_val, set[j].bv_val)) {
76 cp->op->o_tmpfree(rset[i].bv_val, cp->op->o_tmpmemctx);
77 rset[i].bv_val = NULL;
84 cp->op->o_tmpfree(rset, cp->op->o_tmpmemctx);
90 if (lset == NULL || lset->bv_val == NULL || rset == NULL || rset->bv_val == NULL) {
91 set = cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx);
95 last = slap_set_size(set) - 1;
96 for (i = 0; set[i].bv_val; i++) {
97 for (j = 0; rset[j].bv_val; j++) {
98 if (set_samedn(set[i].bv_val, rset[j].bv_val))
101 if (rset[j].bv_val == NULL) {
102 cp->op->o_tmpfree(set[i].bv_val, cp->op->o_tmpmemctx);
104 set[last].bv_val = NULL;
112 slap_set_dispose(cp, lset);
113 slap_set_dispose(cp, rset);
118 set_chase (SLAP_SET_GATHER gatherer,
119 SetCookie *cp, BerVarray set, struct berval *attr, int closure)
121 BerVarray vals, nset;
126 bv.bv_len = attr->bv_len;
130 return(cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx));
132 if (set->bv_val == NULL)
135 if (attr->bv_len > (sizeof(attrstr) - 1)) {
136 slap_set_dispose(cp, set);
139 AC_MEMCPY(attrstr, attr->bv_val, attr->bv_len);
140 attrstr[attr->bv_len] = 0;
142 nset = cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx);
144 slap_set_dispose(cp, set);
147 for (i = 0; set[i].bv_val; i++) {
148 vals = (gatherer)(cp, &set[i], &bv);
150 nset = set_join(cp, nset, '|', vals);
152 slap_set_dispose(cp, set);
155 for (i = 0; nset[i].bv_val; i++) {
156 vals = (gatherer)(cp, &nset[i], &bv);
158 nset = set_join(cp, nset, '|', vals);
168 set_samedn (char *dn1, char *dn2)
172 while (*dn1 == ' ') dn1++;
173 while (*dn2 == ' ') dn2++;
174 while (*dn1 || *dn2) {
175 if (*dn1 != '=' && *dn1 != ','
176 && *dn2 != '=' && *dn2 != ',')
180 if (c1 >= 'a' && c1 <= 'z')
182 if (c2 >= 'a' && c2 <= 'z')
187 while (*dn1 == ' ') dn1++;
188 while (*dn2 == ' ') dn2++;
189 if (*dn1++ != *dn2++)
191 while (*dn1 == ' ') dn1++;
192 while (*dn2 == ' ') dn2++;
199 slap_set_filter (SLAP_SET_GATHER gatherer,
200 SetCookie *cp, struct berval *fbv,
201 struct berval *user, struct berval *this, BerVarray *results)
203 #define IS_SET(x) ( (long)(x) >= 256 )
204 #define IS_OP(x) ( (long)(x) < 256 )
205 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
206 #define SF_TOP() ( (BerVarray)( (stp < 0) ? 0 : stack[stp] ) )
207 #define SF_POP() ( (BerVarray)( (stp < 0) ? 0 : stack[stp--] ) )
208 #define SF_PUSH(x) do { \
209 if (stp >= 63) SF_ERROR(overflow); \
210 stack[++stp] = (BerVarray)(long)(x); \
215 int len, op, rc, stp;
216 char c, *filter = fbv->bv_val;
222 while ((c = *filter++)) {
232 if (IS_SET(SF_TOP()))
241 if (SF_TOP() == (void *)'(') {
245 } else if (IS_OP(SF_TOP())) {
249 set = set_join(cp, lset, op, set);
264 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
267 } else if (IS_OP(SF_TOP())) {
270 set = set_join(cp, lset, op, set);
282 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
285 (c = *filter++) && (c != ']');
291 set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
294 set->bv_val = cp->op->o_tmpcalloc(len + 1, sizeof(char), cp->op->o_tmpmemctx);
295 if (set->bv_val == NULL)
297 AC_MEMCPY(set->bv_val, &filter[-len - 1], len);
307 /* fall through to next case */
317 && (c < 'A' || c > 'Z')
318 && (c < 'a' || c > 'z'))
325 && ((c >= '0' && c <= '9')
326 || (c >= 'A' && c <= 'Z')
327 || (c >= 'a' && c <= 'z'));
331 && memcmp("this", filter, len) == 0)
333 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
335 set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
338 ber_dupbv_x( set, this, cp->op->o_tmpmemctx );
339 if (set->bv_val == NULL)
342 && memcmp("user", filter, len) == 0)
344 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
346 set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
349 ber_dupbv_x( set, user, cp->op->o_tmpmemctx );
350 if (set->bv_val == NULL)
352 } else if (SF_TOP() != (void *)'/') {
359 set = set_chase(gatherer,
360 cp, SF_POP(), &fb2, c == '*');
378 } else if (IS_OP(SF_TOP())) {
381 set = set_join(cp, lset, op, set);
388 rc = slap_set_size(set) > 0 ? 1 : 0;
396 slap_set_dispose(cp, set);
397 while ((set = SF_POP())) {
399 slap_set_dispose(cp, set);