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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* Copyright (c) 2001 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Sendmail: ldap.c,v 1.10 2001/09/11 04:04:48 gshapiro Exp $")
#if LDAPMAP
# include <sys/types.h>
# include <errno.h>
# include <setjmp.h>
# include <stdlib.h>
# include <unistd.h>
# include <sm/bitops.h>
# include <sm/clock.h>
# include <sm/conf.h>
# include <sm/debug.h>
# include <sm/errstring.h>
# include <sm/ldap.h>
# include <sm/string.h>
SM_DEBUG_T SmLDAPTrace = SM_DEBUG_INITIALIZER("sm_trace_ldap",
"@(#)$Debug: sm_trace_ldap - trace LDAP operations $");
static void ldaptimeout __P((int));
/*
** SM_LDAP_CLEAR -- set default values for SM_LDAP_STRUCT
**
** Parameters:
** lmap -- pointer to SM_LDAP_STRUCT to clear
**
** Returns:
** None.
**
*/
void
sm_ldap_clear(lmap)
SM_LDAP_STRUCT *lmap;
{
if (lmap == NULL)
return;
lmap->ldap_host = NULL;
lmap->ldap_port = LDAP_PORT;
lmap->ldap_deref = LDAP_DEREF_NEVER;
lmap->ldap_timelimit = LDAP_NO_LIMIT;
lmap->ldap_sizelimit = LDAP_NO_LIMIT;
# ifdef LDAP_REFERRALS
lmap->ldap_options = LDAP_OPT_REFERRALS;
# else /* LDAP_REFERRALS */
lmap->ldap_options = 0;
# endif /* LDAP_REFERRALS */
lmap->ldap_attrsep = '\0';
lmap->ldap_binddn = NULL;
lmap->ldap_secret = NULL;
lmap->ldap_method = LDAP_AUTH_SIMPLE;
lmap->ldap_base = NULL;
lmap->ldap_scope = LDAP_SCOPE_SUBTREE;
lmap->ldap_attrsonly = LDAPMAP_FALSE;
lmap->ldap_timeout.tv_sec = 0;
lmap->ldap_timeout.tv_usec = 0;
lmap->ldap_ld = NULL;
lmap->ldap_filter = NULL;
lmap->ldap_attr[0] = NULL;
lmap->ldap_res = NULL;
lmap->ldap_next = NULL;
lmap->ldap_pid = 0;
}
/*
** SM_LDAP_START -- actually connect to an LDAP server
**
** Parameters:
** name -- name of map for debug output.
** lmap -- the LDAP map being opened.
**
** Returns:
** true if connection is successful, false otherwise.
**
** Side Effects:
** Populates lmap->ldap_ld.
*/
static jmp_buf LDAPTimeout;
#define SM_LDAP_SETTIMEOUT(to) \
do \
{ \
if (to != 0) \
{ \
if (setjmp(LDAPTimeout) != 0) \
{ \
errno = ETIMEDOUT; \
return false; \
} \
ev = sm_setevent(to, ldaptimeout, 0); \
} \
} while (0)
#define SM_LDAP_CLEARTIMEOUT() \
do \
{ \
if (ev != NULL) \
sm_clrevent(ev); \
} while (0)
bool
sm_ldap_start(name, lmap)
char *name;
SM_LDAP_STRUCT *lmap;
{
int bind_result;
int save_errno;
SM_EVENT *ev = NULL;
LDAP *ld;
if (sm_debug_active(&SmLDAPTrace, 2))
sm_dprintf("ldapmap_start(%s)\n", name == NULL ? "" : name);
if (sm_debug_active(&SmLDAPTrace, 9))
sm_dprintf("ldapmap_start(%s, %d)\n",
lmap->ldap_host == NULL ? "localhost" : lmap->ldap_host,
lmap->ldap_port);
# if USE_LDAP_INIT
ld = ldap_init(lmap->ldap_host, lmap->ldap_port);
save_errno = errno;
# else /* USE_LDAP_INIT */
/*
** If using ldap_open(), the actual connection to the server
** happens now so we need the timeout here. For ldap_init(),
** the connection happens at bind time.
*/
SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec);
ld = ldap_open(lmap->ldap_host, lmap->ldap_port);
save_errno = errno;
/* clear the event if it has not sprung */
SM_LDAP_CLEARTIMEOUT();
# endif /* USE_LDAP_INIT */
errno = save_errno;
if (ld == NULL)
return false;
sm_ldap_setopts(ld, lmap);
# if USE_LDAP_INIT
/*
** If using ldap_init(), the actual connection to the server
** happens at ldap_bind_s() so we need the timeout here.
*/
SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec);
# endif /* USE_LDAP_INIT */
# ifdef LDAP_AUTH_KRBV4
if (lmap->ldap_method == LDAP_AUTH_KRBV4 &&
lmap->ldap_secret != NULL)
{
/*
** Need to put ticket in environment here instead of
** during parseargs as there may be different tickets
** for different LDAP connections.
*/
(void) putenv(lmap->ldap_secret);
}
# endif /* LDAP_AUTH_KRBV4 */
bind_result = ldap_bind_s(ld, lmap->ldap_binddn,
lmap->ldap_secret, lmap->ldap_method);
# if USE_LDAP_INIT
/* clear the event if it has not sprung */
SM_LDAP_CLEARTIMEOUT();
# endif /* USE_LDAP_INIT */
if (bind_result != LDAP_SUCCESS)
{
errno = bind_result + E_LDAPBASE;
return false;
}
/* Save PID to make sure only this PID closes the LDAP connection */
lmap->ldap_pid = getpid();
lmap->ldap_ld = ld;
return true;
}
/* ARGSUSED */
static void
ldaptimeout(unused)
int unused;
{
/*
** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
** DOING.
*/
errno = ETIMEDOUT;
longjmp(LDAPTimeout, 1);
}
/*
** SM_LDAP_SEARCH -- iniate LDAP search
**
** Initiate an LDAP search, return the msgid.
** The calling function must collect the results.
**
** Parameters:
** lmap -- LDAP map information
** key -- key to substitute in LDAP filter
**
** Returns:
** -1 on failure, msgid on success
**
*/
int
sm_ldap_search(lmap, key)
SM_LDAP_STRUCT *lmap;
char *key;
{
int msgid;
char *fp, *p, *q;
char filter[LDAPMAP_MAX_FILTER + 1];
/* substitute key into filter, perhaps multiple times */
memset(filter, '\0', sizeof filter);
fp = filter;
p = lmap->ldap_filter;
while ((q = strchr(p, '%')) != NULL)
{
if (q[1] == 's')
{
(void) sm_snprintf(fp, SPACELEFT(filter, fp),
"%.*s%s", (int) (q - p), p, key);
fp += strlen(fp);
p = q + 2;
}
else if (q[1] == '0')
{
char *k = key;
(void) sm_snprintf(fp, SPACELEFT(filter, fp),
"%.*s", (int) (q - p), p);
fp += strlen(fp);
p = q + 2;
/* Properly escape LDAP special characters */
while (SPACELEFT(filter, fp) > 0 &&
*k != '\0')
{
if (*k == '*' || *k == '(' ||
*k == ')' || *k == '\\')
{
(void) sm_strlcat(fp,
(*k == '*' ? "\\2A" :
(*k == '(' ? "\\28" :
(*k == ')' ? "\\29" :
(*k == '\\' ? "\\5C" :
"\00")))),
SPACELEFT(filter, fp));
fp += strlen(fp);
k++;
}
else
*fp++ = *k++;
}
}
else
{
(void) sm_snprintf(fp, SPACELEFT(filter, fp),
"%.*s", (int) (q - p + 1), p);
p = q + (q[1] == '%' ? 2 : 1);
fp += strlen(fp);
}
}
(void) sm_strlcpy(fp, p, SPACELEFT(filter, fp));
if (sm_debug_active(&SmLDAPTrace, 20))
sm_dprintf("ldap search filter=%s\n", filter);
lmap->ldap_res = NULL;
msgid = ldap_search(lmap->ldap_ld, lmap->ldap_base, lmap->ldap_scope,
filter,
(lmap->ldap_attr[0] == NULL ? NULL :
lmap->ldap_attr),
lmap->ldap_attrsonly);
return msgid;
}
/*
** SM_LDAP_CLOSE -- close LDAP connection
**
** Parameters:
** lmap -- LDAP map information
**
** Returns:
** None.
**
*/
void
sm_ldap_close(lmap)
SM_LDAP_STRUCT *lmap;
{
if (lmap->ldap_ld == NULL)
return;
if (lmap->ldap_pid == getpid())
ldap_unbind(lmap->ldap_ld);
lmap->ldap_ld = NULL;
lmap->ldap_pid = 0;
}
/*
** SM_LDAP_SETOPTS -- set LDAP options
**
** Parameters:
** ld -- LDAP session handle
** lmap -- LDAP map information
**
** Returns:
** None.
**
*/
void
sm_ldap_setopts(ld, lmap)
LDAP *ld;
SM_LDAP_STRUCT *lmap;
{
# if USE_LDAP_SET_OPTION
ldap_set_option(ld, LDAP_OPT_DEREF, &lmap->ldap_deref);
if (bitset(LDAP_OPT_REFERRALS, lmap->ldap_options))
ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
else
ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &lmap->ldap_sizelimit);
ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &lmap->ldap_timelimit);
# else /* USE_LDAP_SET_OPTION */
/* From here on in we can use ldap internal timelimits */
ld->ld_deref = lmap->ldap_deref;
ld->ld_options = lmap->ldap_options;
ld->ld_sizelimit = lmap->ldap_sizelimit;
ld->ld_timelimit = lmap->ldap_timelimit;
# endif /* USE_LDAP_SET_OPTION */
}
/*
** SM_LDAP_GETERRNO -- get ldap errno value
**
** Parameters:
** ld -- LDAP session handle
**
** Returns:
** LDAP errno.
**
*/
int
sm_ldap_geterrno(ld)
LDAP *ld;
{
int err = LDAP_SUCCESS;
# if defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3
(void) ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &err);
# else /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */
# ifdef LDAP_OPT_SIZELIMIT
err = ldap_get_lderrno(ld, NULL, NULL);
# else /* LDAP_OPT_SIZELIMIT */
err = ld->ld_errno;
/*
** Reset value to prevent lingering LDAP_DECODING_ERROR due to
** OpenLDAP 1.X's hack (see above)
*/
ld->ld_errno = LDAP_SUCCESS;
# endif /* LDAP_OPT_SIZELIMIT */
# endif /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */
return err;
}
# endif /* LDAPMAP */
|