3 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 static char **set_join (char **lset, int op, char **rset);
13 static char **set_chase (SET_GATHER gatherer, void *cookie, char **set, char *attr, int attrlen, int closure);
14 static int set_samedn (char *dn1, char *dn2);
30 set_dispose (char **set)
35 for (i = 0; set[i]; i++)
42 set_join (char **lset, int op, char **rset)
49 if (lset == NULL || *lset == NULL) {
52 return(ch_calloc(1, sizeof(char *)));
58 if (rset == NULL || *rset == NULL) {
63 i = set_size(lset) + set_size(rset) + 1;
64 set = ch_calloc(i, sizeof(char *));
66 /* set_chase() depends on this routine to
67 * keep the first elements of the result
68 * set the same (and in the same order)
71 for (i = 0; lset[i]; i++)
74 for (i = 0; rset[i]; i++) {
75 for (j = 0; set[j]; j++) {
76 if (set_samedn(rset[i], set[j])) {
91 if (lset == NULL || *lset == NULL || rset == NULL || *rset == NULL) {
92 set = ch_calloc(1, sizeof(char *));
96 last = set_size(set) - 1;
97 for (i = 0; set[i]; i++) {
98 for (j = 0; rset[j]; j++) {
99 if (set_samedn(set[i], rset[j]))
102 if (rset[j] == NULL) {
119 set_chase (SET_GATHER gatherer, void *cookie, char **set, char *attr, int attrlen, int closure)
126 return(ch_calloc(1, sizeof(char *)));
131 if (attrlen > (sizeof(attrstr) - 1)) {
135 memcpy(attrstr, attr, attrlen);
136 attrstr[attrlen] = 0;
138 nset = ch_calloc(1, sizeof(char *));
143 for (i = 0; set[i]; i++) {
144 vals = (gatherer)(cookie, set[i], attrstr);
146 nset = set_join(nset, '|', vals);
151 for (i = 0; nset[i]; i++) {
152 vals = (gatherer)(cookie, nset[i], attrstr);
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, void *cookie, char *filter, char *user, char *this, char ***results)
197 #define IS_SET(x) ( (long)(x) >= 256 )
198 #define IS_OP(x) ( (long)(x) < 256 )
199 #define SF_ERROR(x) { rc = -1; goto _error; }
200 #define SF_TOP() (char **)( (stp < 0) ? 0 : stack[stp] )
201 #define SF_POP() (char **)( (stp < 0) ? 0 : stack[stp--] )
202 #define SF_PUSH(x) { if (stp >= 63) SF_ERROR(overflow); stack[++stp] = (char **)(long)(x); }
205 int len, op, rc, stp;
212 while (c = *filter++) {
222 if (IS_SET(SF_TOP()))
231 if (SF_TOP() == (void *)'(') {
235 } else if (IS_OP(SF_TOP())) {
239 set = set_join(lset, op, set);
254 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
257 } else if (IS_OP(SF_TOP())) {
260 set = set_join(lset, op, set);
272 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
275 (c = *filter++) && (c != ']');
281 set = ch_calloc(2, sizeof(char *));
284 *set = ch_calloc(len + 1, sizeof(char));
287 memcpy(*set, &filter[-len - 1], len);
296 /* fall through to next case */
306 && (c < 'A' || c > 'Z')
307 && (c < 'a' || c > 'z'))
314 && ((c >= '0' && c <= '9')
315 || (c >= 'A' && c <= 'Z')
316 || (c >= 'a' && c <= 'z'));
320 && memcmp("this", filter, len) == 0)
322 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
324 set = ch_calloc(2, sizeof(char *));
327 *set = ch_strdup(this);
331 && memcmp("user", filter, len) == 0)
333 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
335 set = ch_calloc(2, sizeof(char *));
338 *set = ch_strdup(user);
341 } else if (SF_TOP() != (void *)'/') {
345 set = set_chase(gatherer, cookie, SF_POP(), filter, len, c == '*');
363 } else if (IS_OP(SF_TOP())) {
366 set = set_join(lset, op, set);
382 while (set = SF_POP()) {