3 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static char **set_join (char **lset, int op, char **rset);
16 static char **set_chase (SET_GATHER gatherer,
17 void *cookie, char **set, char *attr, int attrlen, int closure);
18 static int set_samedn (char *dn1, char *dn2);
34 set_dispose (char **set)
39 for (i = 0; set[i]; i++)
46 set_join (char **lset, int op, char **rset)
53 if (lset == NULL || *lset == NULL) {
56 return(ch_calloc(1, sizeof(char *)));
62 if (rset == NULL || *rset == NULL) {
67 i = set_size(lset) + set_size(rset) + 1;
68 set = ch_calloc(i, sizeof(char *));
70 /* set_chase() depends on this routine to
71 * keep the first elements of the result
72 * set the same (and in the same order)
75 for (i = 0; lset[i]; i++)
78 for (i = 0; rset[i]; i++) {
79 for (j = 0; set[j]; j++) {
80 if (set_samedn(rset[i], set[j])) {
95 if (lset == NULL || *lset == NULL || rset == NULL || *rset == NULL) {
96 set = ch_calloc(1, sizeof(char *));
100 last = set_size(set) - 1;
101 for (i = 0; set[i]; i++) {
102 for (j = 0; rset[j]; j++) {
103 if (set_samedn(set[i], rset[j]))
106 if (rset[j] == NULL) {
123 set_chase (SET_GATHER gatherer,
124 void *cookie, char **set, char *attr, int attrlen, int closure)
128 struct berval bv = {attrlen, attrstr};
132 return(ch_calloc(1, sizeof(char *)));
137 if (attrlen > (sizeof(attrstr) - 1)) {
141 AC_MEMCPY(attrstr, attr, attrlen);
142 attrstr[attrlen] = 0;
144 nset = ch_calloc(1, sizeof(char *));
149 for (i = 0; set[i]; i++) {
150 vals = (gatherer)(cookie, set[i], &bv);
152 nset = set_join(nset, '|', vals);
157 for (i = 0; nset[i]; i++) {
158 vals = (gatherer)(cookie, nset[i], &bv);
160 nset = set_join(nset, '|', vals);
170 set_samedn (char *dn1, char *dn2)
174 while (*dn1 == ' ') dn1++;
175 while (*dn2 == ' ') dn2++;
176 while (*dn1 || *dn2) {
177 if (*dn1 != '=' && *dn1 != ','
178 && *dn2 != '=' && *dn2 != ',')
182 if (c1 >= 'a' && c1 <= 'z')
184 if (c2 >= 'a' && c2 <= 'z')
189 while (*dn1 == ' ') dn1++;
190 while (*dn2 == ' ') dn2++;
191 if (*dn1++ != *dn2++)
193 while (*dn1 == ' ') dn1++;
194 while (*dn2 == ' ') dn2++;
201 set_filter (SET_GATHER gatherer,
202 void *cookie, char *filter, char *user, char *this, char ***results)
204 #define IS_SET(x) ( (long)(x) >= 256 )
205 #define IS_OP(x) ( (long)(x) < 256 )
206 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
207 #define SF_TOP() ( (char **)( (stp < 0) ? 0 : stack[stp] ) )
208 #define SF_POP() ( (char **)( (stp < 0) ? 0 : stack[stp--] ) )
209 #define SF_PUSH(x) do { \
210 if (stp >= 63) SF_ERROR(overflow); \
211 stack[++stp] = (char **)(long)(x); \
216 int len, op, rc, stp;
223 while ((c = *filter++)) {
233 if (IS_SET(SF_TOP()))
242 if (SF_TOP() == (void *)'(') {
246 } else if (IS_OP(SF_TOP())) {
250 set = set_join(lset, op, set);
265 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
268 } else if (IS_OP(SF_TOP())) {
271 set = set_join(lset, op, set);
283 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
286 (c = *filter++) && (c != ']');
292 set = ch_calloc(2, sizeof(char *));
295 *set = ch_calloc(len + 1, sizeof(char));
298 AC_MEMCPY(*set, &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 = ch_calloc(2, sizeof(char *));
338 *set = ch_strdup(this);
342 && memcmp("user", filter, len) == 0)
344 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
346 set = ch_calloc(2, sizeof(char *));
349 *set = ch_strdup(user);
352 } else if (SF_TOP() != (void *)'/') {
356 set = set_chase(gatherer,
357 cookie, SF_POP(), filter, len, c == '*');
375 } else if (IS_OP(SF_TOP())) {
378 set = set_join(lset, op, set);
394 while ((set = SF_POP())) {