summaryrefslogtreecommitdiff
path: root/usr.sbin/ldapd/filter.c
blob: 7725ca9cdf1110da2c131bf8a05596ab3a15f8e7 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*	$OpenBSD: filter.c,v 1.1 2010/05/31 17:36:31 martinh Exp $ */

/*
 * Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <sys/types.h>

#include <string.h>
#include <stdint.h>

#include "ldapd.h"

static int		 ldap_filt_eq(struct ber_element *root,
				struct ber_element *filter);
static int		 ldap_filt_subs(struct ber_element *root,
				struct ber_element *filter);
static int		 ldap_filt_and(struct ber_element *root,
				struct ber_element *filter);
static int		 ldap_filt_or(struct ber_element *root,
				struct ber_element *filter);
static int		 ldap_filt_not(struct ber_element *root,
				struct ber_element *filter);

static int
ldap_filt_eq(struct ber_element *root, struct ber_element *filter)
{
	const char		*key, *cmp;
	struct ber_element	*a, *vals, *v;

	if (ber_scanf_elements(filter, "{ss", &key, &cmp) != 0)
		return -1;

	a = ldap_get_attribute(root, key);
	if (a == NULL) {
		log_debug("no attribute [%s] found", key);
		return -1;
	}

	vals = a->be_next;
	if (vals == NULL)
		return -1;

	for (v = vals->be_sub; v; v = v->be_next) {
		char *vs;
		if (ber_get_string(v, &vs) != 0)
			continue;
		if (strcasecmp(cmp, vs) == 0)
			return 0;
	}

	return -1;
}

static int
ldap_filt_subs_value(struct ber_element *v, struct ber_element *sub)
{
	int		 class;
	unsigned long	 type;
	const char	*cmpval;
	char		*vs, *p, *end;

	if (ber_get_string(v, &vs) != 0)
		return -1;

	for (; sub; sub = sub->be_next) {
		if (ber_scanf_elements(sub, "ts", &class, &type, &cmpval) != 0)
			return -1;

		if (class != BER_CLASS_CONTEXT)
			return -1;

		switch (type) {
		case LDAP_FILT_SUBS_INIT:
			if (strncasecmp(cmpval, vs, strlen(cmpval)) == 0)
				vs += strlen(cmpval);
			else
				return 1; /* no match */
			break;
		case LDAP_FILT_SUBS_ANY:
			if ((p = strcasestr(vs, cmpval)) != NULL)
				vs = p + strlen(cmpval);
			else
				return 1; /* no match */
			break;
		case LDAP_FILT_SUBS_FIN:
			if (strlen(vs) < strlen(cmpval))
				return 1; /* no match */
			end = vs + strlen(vs) - strlen(cmpval);
			if (strcasecmp(end, cmpval) == 0)
				vs = end + strlen(cmpval);
			else
				return 1; /* no match */
			break;
		default:
			log_warnx("invalid subfilter type %d", type);
			return -1;
		}
	}

	return 0; /* match */
}

static int
ldap_filt_subs(struct ber_element *root, struct ber_element *filter)
{
	const char		*key, *attr;
	struct ber_element	*sub, *a, *v;

	if (ber_scanf_elements(filter, "{s{e", &key, &sub) != 0)
		return -1;

	a = ldap_get_attribute(root, key);
	if (a == NULL)
		return -1; /* attribute not found, false or undefined? */

	if (ber_scanf_elements(a, "s(e", &attr, &v) != 0)
		return -1; /* internal failure, false or undefined? */

	/* Loop through all values, stop if any matches.
	 */
	for (; v; v = v->be_next) {
		/* All substrings must match. */
		switch (ldap_filt_subs_value(v, sub)) {
		case 0:
			return 0;
		case -1:
			return -1;
		default:
			break;
		}
	}

	/* All values checked, no match. */
	return -1;
}

static int
ldap_filt_and(struct ber_element *root, struct ber_element *filter)
{
	struct ber_element	*elm;

	if (ber_scanf_elements(filter, "{e", &elm) != 0)
		return -1;

	for (; elm; elm = elm->be_next) {
		if (ldap_matches_filter(root, elm) != 0)
			return -1;
	}

	return 0;
}

static int
ldap_filt_or(struct ber_element *root, struct ber_element *filter)
{
	struct ber_element	*elm;

	if (ber_scanf_elements(filter, "{e", &elm) != 0) {
		log_warnx("failed to parse search filter");
		return -1;
	}

	for (; elm; elm = elm->be_next) {
		if (ldap_matches_filter(root, elm) == 0)
			return 0;
	}

	return -1;
}

static int
ldap_filt_not(struct ber_element *root, struct ber_element *filter)
{
	struct ber_element	*elm;

	if (ber_scanf_elements(filter, "{e", &elm) != 0) {
		log_warnx("failed to parse search filter");
		return -1;
	}

	for (; elm; elm = elm->be_next) {
		if (ldap_matches_filter(root, elm) != 0)
			return 0;
	}

	return -1;
}

static int
ldap_filt_presence(struct ber_element *root, struct ber_element *filter)
{
	const char		*key;
	struct ber_element	*a;

	if (ber_scanf_elements(filter, "s", &key) != 0) {
		log_warnx("failed to parse presence filter");
		return -1;
	}

	a = ldap_get_attribute(root, key);
	if (a == NULL) {
		log_debug("attribute %s not found", key);
		return -1; /* attribute not found */
	}

	return 0;
}

int
ldap_matches_filter(struct ber_element *root, struct ber_element *filter)
{
	if (filter == NULL)
		return 0;

	if (filter->be_class != BER_CLASS_CONTEXT) {
		log_warnx("invalid class %d in filter", filter->be_class);
		return -1;
	}

	switch (filter->be_type) {
	case LDAP_FILT_EQ:
	case LDAP_FILT_APPR:
		return ldap_filt_eq(root, filter);
	case LDAP_FILT_SUBS:
		return ldap_filt_subs(root, filter);
	case LDAP_FILT_AND:
		return ldap_filt_and(root, filter);
	case LDAP_FILT_OR:
		return ldap_filt_or(root, filter);
	case LDAP_FILT_NOT:
		return ldap_filt_not(root, filter);
	case LDAP_FILT_PRES:
		return ldap_filt_presence(root, filter);
	default:
		log_warnx("filter type %d not implemented", filter->be_type);
		return -1;
	}
}