/
qldap-filter.c
243 lines (220 loc) · 6.15 KB
/
qldap-filter.c
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
/*
* Copyright (c) 2003-2004 Claudio Jeker,
* Internet Business Solutions AG, CH-8005 Zürich, Switzerland
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Internet Business
* Solutions AG and its contributors.
* 4. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "auto_break.h"
#include "constmap.h"
#include "qldap.h"
#include "qmail-ldap.h"
#include "str.h"
#include "stralloc.h"
extern stralloc objectclass;
extern struct constmap ad_map;
extern int adok;
int filter_escape(stralloc *, char *, unsigned int);
int filter_start(stralloc *);
int filter_end(stralloc *);
/*
* For LDAP, '(', ')', '\', '*' and '\0' have to be escaped with '\'.
* We ignore the '\0' case because it is not possible to have a '\0' in s.
*/
int
filter_escape(stralloc *filter, char *s, unsigned int len)
{
char x;
/* pre reserve some space */
if (!stralloc_readyplus(filter, len))
return 0;
for (; len != 0; len--) {
x = *s++;
if (x == '*' || x == '(' || x == ')' || x == '\\')
if (!stralloc_append(filter, "\\"))
return 0;
if (!stralloc_append(filter, &x))
return 0;
}
return 1;
}
int
filter_start(stralloc *filter)
{
if (!stralloc_copys(filter, ""))
return 0;
if (objectclass.s != (char *)0 && objectclass.len != 0) {
/* (&(objectclass=...)%searchfilter%) */
if (!stralloc_copys(filter, "(&(") ||
!stralloc_cats(filter, LDAP_OBJECTCLASS) ||
!stralloc_cats(filter, "=") ||
!stralloc_cat(filter, &objectclass) ||
!stralloc_cats(filter, ")"))
return 0;
}
return 1;
}
int
filter_end(stralloc *filter)
{
if (objectclass.s != (char *)0 && objectclass.len != 0)
if (!stralloc_cats(filter, ")"))
return 0;
if (!stralloc_0(filter))
return 0;
return 1;
}
static stralloc filter = {0};
char *
filter_uid(char *uid)
{
if (uid == (char *)0)
return 0;
if (!filter_start(&filter) ||
!stralloc_copys(&filter,"(") ||
!stralloc_cats(&filter, LDAP_UID) ||
!stralloc_cats(&filter, "=") ||
!filter_escape(&filter, uid, str_len(uid)) ||
!stralloc_cats(&filter, ")") ||
!filter_end(&filter))
return (char *)0;
return filter.s;
}
static int extcnt;
static unsigned int ext = 0;
char *
filter_mail(char *mail, int *done)
{
char *domain, *alias;
unsigned int at;
int round;
#ifdef DASH_EXT
unsigned int i;
#endif
if (mail == (char *)0) {
ext = 0;
return 0;
}
at = str_rchr(mail, '@');
if (at == 0 || mail[at] != '@') {
ext = 0;
return 0;
}
domain = mail + at + 1;
if (adok) {
alias = constmap(&ad_map, domain, str_len(domain));
if (alias && *alias)
domain = alias;
}
if (ext == 0) {
ext = at;
extcnt = -1;
*done = 0;
} else {
if (extcnt == 0) {
*done = 1;
ext = 0;
return 0;
}
#ifdef DASH_EXT
/*
* limit ext to the first DASH_EXT_LEVELS extensions.
* We will only check for (DASH_EXT_LEVELS = 4):
* a-b-c-d-e-f-g-...@foobar.com
* a-b-c-d-catchall@foobar.com
* a-b-c-catchall@foobar.com
* a-b-catchall@foobar.com
* a-catchall@foobar.com
* catchall@foobar.com
*/
if (ext == at)
for (i = 0, ext = 0, extcnt = 1;
ext < at && extcnt <= DASH_EXT_LEVELS; ext++)
if (mail[ext] == *auto_break) extcnt++;
while (ext != 0 && --ext > 0) {
if (mail[ext] == *auto_break) break;
}
extcnt--;
#else
#error XXX XXX
/* basic qmail-ldap behavior test for username@domain.com and
catchall@domain.com */
ext = 0;
extcnt = 0;
#endif
}
for (round = 0; round < 2; round++) {
switch (round) {
case 0:
/* build the search string for the email address */
/* mail address */
if (!filter_start(&filter) ||
!stralloc_cats(&filter, "(|(") ||
!stralloc_cats(&filter, LDAP_MAIL) ||
!stralloc_cats(&filter, "="))
return 0;
break;
case 1:
/* mailalternate address */
if (!stralloc_cats(&filter, ")(") ||
!stralloc_cats(&filter, LDAP_MAILALTERNATE) ||
!stralloc_cats(&filter, "="))
return 0;
break;
}
/* username till current '-' or '@' */
if (!filter_escape(&filter, mail, ext))
return 0;
/* do not append catchall in the first round */
if (ext != at) {
/* catchall or default */
if (extcnt > 0) /* add '-' */
if (!stralloc_cats(&filter, auto_break))
return 0;
if (!stralloc_cats(&filter, LDAP_CATCH_ALL))
return 0;
}
/* @domain.com */
if (!stralloc_append(&filter, "@") ||
!filter_escape(&filter, domain, str_len(domain)))
return 0;
}
if (!stralloc_cats(&filter, "))") ||
!filter_end(&filter))
return 0;
if (extcnt == 0)
*done = 1;
if (mail[0] == *auto_break) *done = 1; // OSC: do not do DASH_EXT loop if first char is auto_break
return filter.s;
}
int
filter_mail_ext(void)
{
return extcnt;
}