Debian Codegraph @

contrib/mod_ldap.c

raw symbols SCIP
1 /*
2 * mod_ldap - LDAP password lookup module for ProFTPD
3 * Copyright (c) 1999-2013, John Morrissey <[email protected]>
4 * Copyright (c) 2013-2025 The ProFTPD Project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * Furthermore, John Morrissey gives permission to link this program with
21 * OpenSSL, and distribute the resulting executable, without including the
22 * source code for OpenSSL in the source distribution.
23 *
24 * -----DO NOT EDIT BELOW THIS LINE-----
25 * $Libraries: -lldap -llber$
26 */
27
28 #include "conf.h"
29 #include "privs.h"
30
31 #define MOD_LDAP_VERSION "mod_ldap/2.9.5"
32
33 #if PROFTPD_VERSION_NUMBER < 0x0001030103
34 # error MOD_LDAP_VERSION " requires ProFTPD 1.3.4rc1 or later"
35 #endif
36
37 #if defined(HAVE_CRYPT_H) && !defined(AIX4) && !defined(AIX5)
38 # include <crypt.h>
39 #endif
40
41 #include <lber.h>
42 #include <ldap.h>
43 #if defined(HAVE_SASL_SASL_H)
44 # include <sasl/sasl.h>
45 #endif /* HAVE_SASL_SASL_H */
46
47 extern xaset_t *server_list;
48
49 module ldap_module;
50
51 static int ldap_logfd = -1;
52 static pool *ldap_pool = NULL;
53
54 static const char *trace_channel = "ldap";
55 #if defined(LBER_OPT_LOG_PRINT_FN)
56 static const char *libtrace_channel = "ldap.library";
57 #endif
58
59 #if LDAP_API_VERSION >= 2000
60 # define HAS_LDAP_SASL_BIND_S
61 #endif
62
63 #if defined(LDAP_SASL_QUIET)
64 # define HAS_LDAP_SASL_INTERACTIVE_BIND_S
65 #endif /* LDAP_SASL_QUIET */
66
67 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_VENDOR_VERSION >= 192)
68 # define HAS_LDAP_UNBIND_EXT_S
69 #endif
70
71 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_VENDOR_VERSION >= 19905)
72 # define HAS_LDAP_INITIALIZE
73 #endif
74
75 #ifdef HAS_LDAP_UNBIND_EXT_S
76 # define LDAP_UNBIND(ld) (ldap_unbind_ext_s(ld, NULL, NULL))
77 #else
78 # define LDAP_UNBIND(ld) (ldap_unbind_s(ld))
79 static char *ldap_server;
80 static int ldap_port = LDAP_PORT;
81 #endif
82
83 /* On some systems LDAP_OPT_DIAGNOSTIC_MESSAGE isn't there (e.g. OpenLDAP-2.3.x)
84 * but LDAP_OPT_ERROR_STRING is.
85 */
86 #ifndef LDAP_OPT_DIAGNOSTIC_MESSAGE
87 # ifdef LDAP_OPT_ERROR_STRING
88 # define LDAP_OPT_DIAGNOSTIC_MESSAGE LDAP_OPT_ERROR_STRING
89 # endif
90 #endif
91
92 #if LDAP_API_VERSION >= 2000
93 # define LDAP_VALUE_T struct berval
94 # define LDAP_GET_VALUES(ld, entry, attr) ldap_get_values_len(ld, entry, attr)
95 # define LDAP_VALUE(values, i) (values[i]->bv_val)
96 # define LDAP_COUNT_VALUES(values) (ldap_count_values_len(values))
97 # define LDAP_VALUE_FREE(values) (ldap_value_free_len(values))
98 # define LDAP_SEARCH(ld, base, scope, filter, attrs, timeout, sizelimit, res) \
99 ldap_search_ext_s(ld, base, scope, filter, attrs, 0, NULL, NULL, \
100 timeout, sizelimit, res)
101 #else /* LDAP_API_VERSION >= 2000 */
102 # define LDAP_VALUE_T char
103 # define LDAP_GET_VALUES(ld, entry, attr) ldap_get_values(ld, entry, attr)
104 # define LDAP_VALUE(values, i) (values[i])
105 # define LDAP_COUNT_VALUES(values) (ldap_count_values(values))
106 # define LDAP_VALUE_FREE(values) (ldap_value_free(values))
107
108 static void pr_ldap_set_sizelimit(LDAP *limit_ld, int limit) {
109 #ifdef LDAP_OPT_SIZELIMIT
110 int res;
111
112 res = ldap_set_option(limit_ld, LDAP_OPT_SIZELIMIT, (void *) &limit);
113 if (res != LDAP_OPT_SUCCESS) {
114 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
115 "failed to set LDAP option for search query size limit to %d entries: %s",
116 limit, ldap_err2string(res));
117
118 } else {
119 pr_trace_msg(trace_channel, 5,
120 "set search query size limit to %d entries", limit);
121 }
122
123 #else
124 limit_ld->ld_sizelimit = limit;
125
126 pr_trace_msg(trace_channel, 5,
127 "set search query size limit to %d entries", limit);
128 #endif
129 }
130
131 static int
132 LDAP_SEARCH(LDAP *ld, char *base, int scope, char *filter, char *attrs[],
133 struct timeval *timeout, int sizelimit, LDAPMessage **res) {
134 pr_ldap_set_sizelimit(ld, sizelimit);
135 return ldap_search_st(ld, base, scope, filter, attrs, 0, timeout, res);
136 }
137 #endif /* LDAP_API_VERSION >= 2000 */
138
139 /* Thanks, Sun. */
140 #ifndef LDAP_OPT_SUCCESS
141 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
142 #endif
143 #ifndef LDAP_URL_SUCCESS
144 # define LDAP_URL_SUCCESS LDAP_SUCCESS
145 #endif
146 #ifndef LDAP_SCOPE_DEFAULT
147 # define LDAP_SCOPE_DEFAULT LDAP_SCOPE_SUBTREE
148 #endif
149
150 /* Config entries */
151
152 struct server_info {
153 const char *info_text;
154 char *host;
155 int port;
156 char *port_text;
157 LDAPURLDesc *url_desc;
158 char *url_text;
159 int use_starttls;
160
161 /* For configuring the SSL/TLS session to the LDAP server. */
162 const char *ssl_cert_file;
163 const char *ssl_key_file;
164 const char *ssl_ca_file;
165 const char *ssl_ciphers;
166 int ssl_verify;
167 const char *ssl_verify_text;
168 };
169
170 struct sasl_info {
171 pool *pool;
172
173 /* SASL_AUTHCID from ldap.conf */
174 const char *authentication_id;
175
176 /* SASL_AUTHZID from ldap.conf */
177 const char *authorization_id;
178
179 const char *password;
180
181 /* SASL_REALM from ldap.conf */
182 const char *realm;
183 };
184
185 static array_header *ldap_servers = NULL;
186 static struct server_info *curr_server_info = NULL;
187 static unsigned int curr_server_index = 0;
188
189 static char *ldap_dn, *ldap_dnpass, *ldap_sasl_mechs = NULL,
190 *ldap_user_basedn = NULL, *ldap_user_name_filter = NULL,
191 *ldap_user_uid_filter = NULL,
192 *ldap_gid_basedn = NULL, *ldap_group_gid_filter = NULL,
193 *ldap_group_name_filter = NULL, *ldap_group_member_filter = NULL,
194 *ldap_defaultauthscheme = "crypt", *ldap_authbind_dn = NULL,
195 *ldap_genhdir_prefix = NULL, *ldap_default_quota = NULL,
196 *ldap_attr_uid = "uid",
197 *ldap_attr_uidnumber = "uidNumber",
198 *ldap_attr_gidnumber = "gidNumber",
199 *ldap_attr_homedirectory = "homeDirectory",
200 *ldap_attr_userpassword = "userPassword",
201 *ldap_attr_loginshell = "loginShell",
202 *ldap_attr_cn = "cn",
203 *ldap_attr_memberuid = "memberUid",
204 *ldap_attr_ftpquota = "ftpQuota",
205 *ldap_attr_ftpquota_profiledn = "ftpQuotaProfileDN",
206 *ldap_attr_ssh_pubkey = "sshPublicKey";
207
208 static int ldap_do_users = FALSE, ldap_do_groups = FALSE,
209 ldap_authbinds = TRUE, ldap_connecttimeout = 0,
210 ldap_querytimeout = 0,
211 ldap_genhdir = FALSE, ldap_genhdir_prefix_nouname = FALSE,
212 ldap_forcedefaultuid = FALSE, ldap_forcedefaultgid = FALSE,
213 ldap_forcegenhdir = FALSE, ldap_protocol_version = 3,
214 ldap_dereference = LDAP_DEREF_NEVER,
215 ldap_search_scope = LDAP_SCOPE_SUBTREE;
216
217 static size_t ldap_dnpasslen = 0;
218
219 static struct timeval ldap_connecttimeout_tv;
220
221 static struct timeval ldap_querytimeout_tv;
222 #define PR_LDAP_QUERY_TIMEOUT_DEFAULT 5
223
224 #define PR_LDAP_AUTO_DEFAULT_ID -2
225 static uid_t ldap_defaultuid = -1;
226 static gid_t ldap_defaultgid = -1;
227
228 static LDAP *ld = NULL;
229 static array_header *cached_quota = NULL;
230 static array_header *cached_ssh_pubkeys = NULL;
231
232 /* Necessary prototypes */
233 static int ldap_sess_init(void);
234 static struct sasl_info *sasl_info_create(pool *, LDAP *);
235 static void sasl_info_get_authcid_from_dn(struct sasl_info *, const char *);
236
237 static void pr_ldap_unbind(void) {
238 int res;
239
240 if (ld == NULL) {
241 return;
242 }
243
244 res = LDAP_UNBIND(ld);
245 if (res != LDAP_SUCCESS) {
246 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
247 "error unbinding connection: %s", ldap_err2string(res));
248
249 } else {
250 pr_trace_msg(trace_channel, 8, "connection successfully unbound");
251 }
252
253 ld = NULL;
254 }
255
256 static void log_sasl_mechs(LDAP *conn_ld, const char *url_text) {
257 #if defined(LDAP_OPT_X_SASL_MECHLIST)
258 char **sasl_mechs = NULL;
259
260 if (ldap_get_option(conn_ld, LDAP_OPT_X_SASL_MECHLIST,
261 &sasl_mechs) == LDAP_OPT_SUCCESS) {
262 if (sasl_mechs != NULL) {
263 register unsigned int i;
264
265 for (i = 0; sasl_mechs[i]; i++) {
266 pr_trace_msg(trace_channel, 22,
267 "%s: LDAP supported SASL mechanism = %s", url_text, sasl_mechs[i]);
268 }
269 }
270 }
271 #endif /* LDAP_OPT_X_SASL_MECHLIST */
272 }
273
274 static int do_ldap_prepare(LDAP **conn_ld) {
275 #if defined(HAS_LDAP_INITIALIZE)
276 int res;
277 char *url_text = NULL;
278
279 if (curr_server_info != NULL) {
280 url_text = curr_server_info->url_text;
281 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
282 "attempting connection to URL %s", url_text);
283
284 } else {
285 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
286 "attempting connection (see ldap.conf for details)");
287 }
288
289 res = ldap_initialize(conn_ld, url_text);
290 if (res != LDAP_SUCCESS) {
291 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
292 "ldap_initialize() to URL %s failed: %s",
293 url_text ? url_text : "(null)", ldap_err2string(res));
294
295 *conn_ld = NULL;
296 return -1;
297 }
298
299 ldap_search_scope = curr_server_info->url_desc->lud_scope;
300
301 #else
302 char *host = NULL;
303 int port = LDAP_PORT;
304
305 if (curr_server_info != NULL) {
306 host = curr_server_info->host;
307 port = curr_server_info->port;
308
309 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
310 "attempting connection to %s:%d", host, port);
311
312 } else {
313 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
314 "attempting connection (see ldap.conf for details)");
315 }
316
317 *conn_ld = ldap_init(host, port);
318 if (*conn_ld == NULL) {
319 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
320 "ldap_init() to %s:%d failed: %s", host ? host : "(null)", port,
321 strerror(errno));
322 return -1;
323 }
324 #endif /* HAS_LDAP_INITIALIZE */
325
326 log_sasl_mechs(*conn_ld, curr_server_info->info_text);
327 return 0;
328 }
329
330 static int sasl_interact_cb(LDAP *conn_ld, unsigned int flags, void *user_data,
331 void *interact_data) {
332 int res = LDAP_OPERATIONS_ERROR;
333
334 #if defined(HAS_LDAP_SASL_INTERACTIVE_BIND_S) && \
335 defined(HAVE_SASL_SASL_H)
336 sasl_interact_t *interacts;
337 struct sasl_info *sasl;
338
339 interacts = interact_data;
340 sasl = user_data;
341
342 while (interacts->id != SASL_CB_LIST_END) {
343 pr_signals_handle();
344
345 switch (interacts->id) {
346 case SASL_CB_AUTHNAME:
347 interacts->result = sasl->authentication_id;
348 interacts->len = strlen(interacts->result);
349 pr_trace_msg(trace_channel, 19, "SASL interaction: CB_AUTHNAME = '%s'",
350 (char *) interacts->result);
351 break;
352
353 case SASL_CB_GETREALM:
354 /* The cyrus-sasl library works just fine with an empty string for our
355 * currently supported mechanisms.
356 */
357 interacts->result = sasl->realm;
358 interacts->len = strlen(interacts->result);
359 pr_trace_msg(trace_channel, 19, "SASL interaction: CB_GETREALM = '%s'",
360 (char *) interacts->result);
361 break;
362
363 case SASL_CB_PASS:
364 interacts->result = sasl->password;
365 interacts->len = strlen(interacts->result);
366 pr_trace_msg(trace_channel, 19, "SASL interaction: CB_PASS = '...'");
367 break;
368
369 case SASL_CB_USER:
370 /* The cyrus-sasl library handles this as the "authorization ID",
371 * and works just fine with an empty string for our currently
372 * supported mechanisms.
373 */
374 interacts->result = sasl->authorization_id;
375 interacts->len = strlen(interacts->result);
376 pr_trace_msg(trace_channel, 19, "SASL interaction: CB_USER = '%s'",
377 (char *) interacts->result);
378 break;
379
380 case SASL_CB_ECHOPROMPT:
381 case SASL_CB_NOECHOPROMPT:
382 /* Ignore */
383 break;
384
385 default:
386 break;
387 }
388
389 interacts++;
390 }
391
392 res = LDAP_SUCCESS;
393 #else
394 pr_log_debug(DEBUG0, MOD_LDAP_VERSION
395 ": interactive SASL authentication unsupported");
396 res = LDAP_OPERATIONS_ERROR;
397 #endif /* HAS_LDAP_SASL_INTERACTIVE_BIND_S and HAVE_SASL_SASL_H */
398
399 return res;
400 }
401
402 static int do_ldap_bind(LDAP *conn_ld) {
403 int res;
404
405 #if defined(HAS_LDAP_SASL_INTERACTIVE_BIND_S)
406 if (ldap_sasl_mechs != NULL) {
407 int sasl_flags;
408 struct sasl_info *sasl;
409
410 pr_trace_msg(trace_channel, 17, "performing bind using SASL mechs: '%s'",
411 ldap_sasl_mechs);
412
413 sasl = sasl_info_create(session.pool, conn_ld);
414 sasl_info_get_authcid_from_dn(sasl, ldap_dn);
415 sasl->password = ldap_dnpass;
416
417 sasl_flags = LDAP_SASL_QUIET;
418
419 res = ldap_sasl_interactive_bind_s(conn_ld, ldap_dn, ldap_sasl_mechs,
420 NULL, NULL, sasl_flags, sasl_interact_cb, sasl);
421
422 destroy_pool(sasl->pool);
423
424 } else {
425 struct berval bindcred;
426
427 bindcred.bv_val = ldap_dnpass;
428 bindcred.bv_len = ldap_dnpasslen;
429
430 res = ldap_sasl_bind_s(conn_ld, ldap_dn, NULL, &bindcred, NULL, NULL, NULL);
431
432 if (res == LDAP_SUCCESS) {
433 if (ldap_dnpasslen > 0) {
434 pr_trace_msg(trace_channel, 9,
435 "bind to '%s' used simple authentication",
436 curr_server_info->info_text);
437
438 } else {
439 pr_trace_msg(trace_channel, 9,
440 "bind to '%s' used anonymous authentication",
441 curr_server_info->info_text);
442 }
443 }
444 }
445 #else /* HAS_LDAP_SASL_BIND_S */
446 res = ldap_simple_bind_s(conn_ld, ldap_dn, ldap_dnpass);
447 #endif /* HAS_LDAP_SASL_BIND_S */
448
449 if (res != LDAP_SUCCESS) {
450 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
451 "bind as DN '%s' failed for '%s': %s",
452 ldap_dn ? ldap_dn : "(anonymous)", curr_server_info->info_text,
453 ldap_err2string(res));
454 return -1;
455 }
456
457 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
458 "successfully bound as DN '%s' with password %s for '%s'",
459 ldap_dn ? ldap_dn : "(anonymous)",
460 ldap_dnpass ? "(see config)" : "(none)", curr_server_info->info_text);
461
462 return 0;
463 }
464
465 static int do_ldap_connect(LDAP **conn_ld, int do_bind) {
466 int res, version;
467
468 /* Note that because we use ldap_init(3)/ldap_initialize(3), the first
469 * actual TCP connection does not occur until the first operation is
470 * requested.
471 */
472 res = do_ldap_prepare(conn_ld);
473 if (res < 0) {
474 return -1;
475 }
476
477 version = LDAP_VERSION3;
478 if (ldap_protocol_version == 2) {
479 version = LDAP_VERSION2;
480 }
481
482 res = ldap_set_option(*conn_ld, LDAP_OPT_PROTOCOL_VERSION, &version);
483 if (res != LDAP_OPT_SUCCESS) {
484 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
485 "error setting LDAP protocol version option to %d: %s", version,
486 ldap_err2string(res));
487 pr_ldap_unbind();
488 return -1;
489 }
490
491 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
492 "set LDAP protocol version to %d", version);
493
494 if (ldap_connecttimeout_tv.tv_sec > 0) {
495 ldap_connecttimeout_tv.tv_usec = 0;
496
497 #if defined(LDAP_OPT_NETWORK_TIMEOUT)
498 res = ldap_set_option(*conn_ld, LDAP_OPT_NETWORK_TIMEOUT,
499 &ldap_connecttimeout_tv);
500 if (res != LDAP_OPT_SUCCESS) {
501 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
502 "error setting network timeout option to %d: %s", ldap_connecttimeout,
503 ldap_err2string(res));
504
505 } else {
506 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
507 "set connect timeout to %d %s", ldap_connecttimeout,
508 ldap_connecttimeout != 1 ? "secs" : "sec");
509 }
510 #else
511 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
512 "unable to set connect timeout %d due to lack of API support",
513 ldap_connecttimeout);
514 #endif
515 }
516
517 if (curr_server_info->use_starttls == TRUE) {
518 #if defined(LDAP_OPT_X_TLS)
519 # if defined(LDAP_OPT_X_TLS_CACERTFILE)
520 if (curr_server_info->ssl_ca_file != NULL) {
521 res = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
522 curr_server_info->ssl_ca_file);
523 if (res != LDAP_OPT_SUCCESS) {
524 pr_log_debug(DEBUG5, MOD_LDAP_VERSION
525 ": error setting LDAP_OPT_X_TLS_CACERTFILE = %s: %s",
526 curr_server_info->ssl_ca_file, ldap_err2string(res));
527
528 } else {
529 pr_trace_msg(trace_channel, 17,
530 "set LDAP_OPT_X_TLS_CACERTFILE = %s for '%s'",
531 curr_server_info->ssl_ca_file, curr_server_info->info_text);
532 }
533 }
534 # endif /* LDAP_OPT_X_TLS_CACERTFILE */
535
536 # if defined(LDAP_OPT_X_TLS_CERTFILE)
537 if (curr_server_info->ssl_cert_file != NULL) {
538 res = ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE,
539 curr_server_info->ssl_cert_file);
540 if (res != LDAP_OPT_SUCCESS) {
541 pr_log_debug(DEBUG5, MOD_LDAP_VERSION
542 ": error setting LDAP_OPT_X_TLS_CERTFILE = %s: %s",
543 curr_server_info->ssl_cert_file, ldap_err2string(res));
544
545 } else {
546 pr_trace_msg(trace_channel, 17,
547 "set LDAP_OPT_X_TLS_CERTFILE = %s for '%s'",
548 curr_server_info->ssl_cert_file, curr_server_info->info_text);
549 }
550 }
551 # endif /* LDAP_OPT_X_TLS_CERTFILE */
552
553 # if defined(LDAP_OPT_X_TLS_KEYFILE)
554 if (curr_server_info->ssl_key_file != NULL) {
555 res = ldap_set_option(NULL, LDAP_OPT_X_TLS_KEYFILE,
556 curr_server_info->ssl_key_file);
557 if (res != LDAP_OPT_SUCCESS) {
558 pr_log_debug(DEBUG5, MOD_LDAP_VERSION
559 ": error setting LDAP_OPT_X_TLS_KEYFILE = %s: %s",
560 curr_server_info->ssl_key_file, ldap_err2string(res));
561
562 } else {
563 pr_trace_msg(trace_channel, 17,
564 "set LDAP_OPT_X_TLS_KEYFILE = %s for '%s'",
565 curr_server_info->ssl_key_file, curr_server_info->info_text);
566 }
567 }
568 # endif /* LDAP_OPT_X_TLS_KEYFILE */
569
570 # if defined(LDAP_OPT_X_TLS_CIPHER_SUITE)
571 if (curr_server_info->ssl_ciphers != NULL) {
572 res = ldap_set_option(NULL, LDAP_OPT_X_TLS_CIPHER_SUITE,
573 curr_server_info->ssl_ciphers);
574 if (res != LDAP_OPT_SUCCESS) {
575 pr_log_debug(DEBUG5, MOD_LDAP_VERSION
576 ": error setting LDAP_OPT_X_TLS_CIPHER_SUITE = %s: %s",
577 curr_server_info->ssl_ciphers, ldap_err2string(res));
578
579 } else {
580 pr_trace_msg(trace_channel, 17,
581 "set LDAP_OPT_X_TLS_CIPHER_SUITE = %s for '%s'",
582 curr_server_info->ssl_ciphers, curr_server_info->info_text);
583 }
584 }
585 # endif /* LDAP_OPT_X_TLS_CIPHER_SUITE */
586
587 # if defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
588 if (curr_server_info->ssl_verify != -1) {
589 res = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
590 &(curr_server_info->ssl_verify));
591 if (res != LDAP_OPT_SUCCESS) {
592 pr_log_debug(DEBUG5, MOD_LDAP_VERSION
593 ": error setting LDAP_OPT_X_TLS_REQUIRE_CERT = %s: %s",
594 curr_server_info->ssl_verify_text, ldap_err2string(res));
595
596 } else {
597 pr_trace_msg(trace_channel, 17,
598 "set LDAP_OPT_X_TLS_REQUIRE_CERT = %s for '%s'",
599 curr_server_info->ssl_verify_text, curr_server_info->info_text);
600 }
601 }
602 # endif /* LDAP_OPT_X_TLS_REQUIRE_CERT */
603
604 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
605 "LDAPUseTLS in effect, performing STARTTLS handshake on '%s'",
606 curr_server_info->info_text);
607 res = ldap_start_tls_s(*conn_ld, NULL, NULL);
608 if (res != LDAP_SUCCESS) {
609 char *diag_msg = NULL;
610
611 (void) ldap_get_option(*conn_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE,
612 (void *) &diag_msg);
613
614 if (diag_msg != NULL) {
615 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
616 "failed to start TLS: %s: %s", ldap_err2string(res), diag_msg);
617 ldap_memfree(diag_msg);
618
619 } else {
620 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
621 "failed to start TLS: %s", ldap_err2string(res));
622 }
623
624 pr_ldap_unbind();
625 return -1;
626 }
627
628 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
629 "enabled TLS for connection to '%s'", curr_server_info->info_text);
630 }
631 #endif /* LDAP_OPT_X_TLS */
632
633 if (do_bind == TRUE) {
634 res = do_ldap_bind(*conn_ld);
635 if (res < 0) {
636 pr_ldap_unbind();
637 return -1;
638 }
639 }
640
641 #ifdef LDAP_OPT_DEREF
642 res = ldap_set_option(*conn_ld, LDAP_OPT_DEREF, (void *) &ldap_dereference);
643 if (res != LDAP_OPT_SUCCESS) {
644 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
645 "failed to set LDAP option for dereference to %d: %s", ldap_dereference,
646 ldap_err2string(res));
647 pr_ldap_unbind();
648 return -1;
649 }
650 #else
651 deref_ld->ld_deref = ldap_dereference;
652 #endif
653
654 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
655 "set dereferencing to %d", ldap_dereference);
656
657 ldap_querytimeout_tv.tv_sec = (ldap_querytimeout > 0 ? ldap_querytimeout :
658 PR_LDAP_QUERY_TIMEOUT_DEFAULT);
659 ldap_querytimeout_tv.tv_usec = 0;
660
661 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
662 "set query timeout to %u secs", (unsigned int) ldap_querytimeout_tv.tv_sec);
663
664 return 0;
665 }
666
667 #if defined(LBER_OPT_LOG_PRINT_FN)
668 static void ldap_tracelog_cb(const char *msg) {
669 (void) pr_trace_msg(libtrace_channel, 1, "%s", msg);
670 }
671 #endif /* no LBER_OPT_LOG_PRINT_FN */
672
673 static int pr_ldap_connect(LDAP **conn_ld, int do_bind) {
674 unsigned int start_server_index;
675
676 start_server_index = curr_server_index;
677 do {
678 int res, debug_level = -1;
679
680 pr_signals_handle();
681
682 /* Note that ldap_servers might be NULL if no LDAPServer directive was
683 * specified. We fall back to using the SDK default.
684 */
685 if (ldap_servers != NULL) {
686 curr_server_info = ((struct server_info **) ldap_servers->elts)[curr_server_index];
687 }
688
689 res = do_ldap_connect(conn_ld, do_bind);
690 if (res < 0) {
691 ++curr_server_index;
692 if (curr_server_index >= ldap_servers->nelts) {
693 curr_server_index = 0;
694 }
695
696 continue;
697 }
698
699 /* This debug level value should be LDAP_DEBUG_ANY, but that macro is, I
700 * think, OpenLDAP-specific.
701 */
702 res = ldap_set_option(*conn_ld, LDAP_OPT_DEBUG_LEVEL, &debug_level);
703 if (res != LDAP_OPT_SUCCESS) {
704 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
705 "error setting DEBUG_ANY debug level: %s", ldap_err2string(res));
706 }
707
708 return 0;
709
710 } while (curr_server_index != start_server_index);
711
712 return -1;
713 }
714
715 static const char *pr_ldap_interpolate_filter(pool *p, char *template,
716 const char *value) {
717 const char *escaped_value, *filter;
718
719 escaped_value = sreplace(p, (char *) value,
720 "\\", "\\\\",
721 "*", "\\*",
722 "(", "\\(",
723 ")", "\\)",
724 NULL
725 );
726
727 if (escaped_value == NULL) {
728 return NULL;
729 }
730
731 filter = sreplace(p, template,
732 "%u", escaped_value,
733 "%v", escaped_value,
734 NULL
735 );
736
737 if (filter == NULL) {
738 return NULL;
739 }
740
741 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
742 "generated filter %s from template %s and value %s", filter, template,
743 value);
744 return filter;
745 }
746
747 static LDAPMessage *pr_ldap_search(const char *basedn, const char *filter,
748 char *attrs[], int sizelimit, int retry) {
749 int res;
750 LDAPMessage *result;
751
752 if (basedn == NULL) {
753 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
754 "no LDAP base DN specified for search filter %s, declining request",
755 filter ? filter : "(null)");
756 return NULL;
757 }
758
759 /* If the LDAP connection has gone away or hasn't been established
760 * yet, attempt to establish it now.
761 */
762 if (ld == NULL) {
763 /* If we _still_ can't connect, give up and return NULL. */
764 if (pr_ldap_connect(&ld, TRUE) < 0) {
765 return NULL;
766 }
767 }
768
769 res = LDAP_SEARCH(ld, basedn, ldap_search_scope, filter, attrs,
770 &ldap_querytimeout_tv, sizelimit, &result);
771 if (res != LDAP_SUCCESS) {
772 if (res != LDAP_SERVER_DOWN) {
773 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
774 "LDAP search use DN '%s', filter '%s' failed: %s", basedn, filter,
775 ldap_err2string(res));
776 return NULL;
777 }
778
779 if (!retry) {
780 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
781 "LDAP connection went away, search failed");
782 pr_ldap_unbind();
783 return NULL;
784 }
785
786 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
787 "LDAP connection went away, retrying search operation");
788 pr_ldap_unbind();
789 return pr_ldap_search(basedn, filter, attrs, sizelimit, FALSE);
790 }
791
792 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
793 "searched under base DN %s using filter %s", basedn,
794 filter ? filter : "(null)");
795 return result;
796 }
797
798 static struct passwd *pr_ldap_user_lookup(pool *p, char *filter_template,
799 const char *replace, const char *basedn, char *attrs[], char **user_dn) {
800 const char *filter;
801 char *dn;
802 int i = 0;
803 struct passwd *pw;
804 LDAPMessage *result, *e;
805 LDAP_VALUE_T **values;
806
807 filter = pr_ldap_interpolate_filter(p, filter_template, replace);
808 if (filter == NULL) {
809 return NULL;
810 }
811
812 result = pr_ldap_search(basedn, filter, attrs, 2, TRUE);
813 if (result == NULL) {
814 return NULL;
815 }
816
817 if (ldap_count_entries(ld, result) > 1) {
818 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
819 "LDAP search returned multiple entries during user lookup, "
820 "aborting query");
821 ldap_msgfree(result);
822 return NULL;
823 }
824
825 e = ldap_first_entry(ld, result);
826 if (e == NULL) {
827 ldap_msgfree(result);
828
829 /* No LDAP entries for this user. */
830 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
831 "no entries for filter %s under base DN %s", filter, basedn);
832 return NULL;
833 }
834
835 pw = pcalloc(ldap_pool, sizeof(struct passwd));
836 while (attrs[i] != NULL) {
837 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
838 "fetching values for attribute %s", attrs[i]);
839
840 values = LDAP_GET_VALUES(ld, e, attrs[i]);
841 if (values == NULL) {
842 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
843 "no values for attribute %s, trying defaults", attrs[i]);
844
845 /* Apply default values for attrs with no explicit values. */
846
847 /* If we can't find the [ug]idNumber attrs, just fill the passwd
848 * struct in with default values from the config file.
849 */
850 if (strcasecmp(attrs[i], ldap_attr_uidnumber) == 0) {
851 if (ldap_defaultuid == (uid_t) -1) {
852 dn = ldap_get_dn(ld, e);
853
854 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
855 "no %s attribute for DN %s found, and LDAPDefaultUID not "
856 "configured", ldap_attr_uidnumber, dn);
857 free(dn);
858 return NULL;
859 }
860
861 if (ldap_defaultuid == (uid_t) PR_LDAP_AUTO_DEFAULT_ID) {
862 if (pw->pw_name != NULL) {
863 struct passwd *auto_pw;
864
865 auto_pw = getpwnam(pw->pw_name);
866 if (auto_pw != NULL) {
867 pw->pw_uid = auto_pw->pw_uid;
868
869 } else {
870 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
871 "LDAPDefaultUID Auto in effect but failed automatic user "
872 "'%s' lookup: %s", pw->pw_name, strerror(errno));
873 return NULL;
874 }
875
876 } else {
877 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
878 "LDAPDefaultUID Auto in effect but user name is missing");
879 return NULL;
880 }
881
882 } else {
883 pw->pw_uid = ldap_defaultuid;
884 }
885 ++i;
886
887 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
888 "using LDAPDefaultUID %s", pr_uid2str(NULL, pw->pw_uid));
889 continue;
890 }
891
892 if (strcasecmp(attrs[i], ldap_attr_gidnumber) == 0) {
893 if (ldap_defaultgid == (gid_t) -1) {
894 dn = ldap_get_dn(ld, e);
895
896 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
897 "no %s attribute found for DN %s, and LDAPDefaultGID not "
898 "configured", ldap_attr_gidnumber, dn);
899 free(dn);
900 return NULL;
901 }
902
903 if (ldap_defaultgid == (gid_t) PR_LDAP_AUTO_DEFAULT_ID) {
904 if (pw->pw_name != NULL) {
905 struct passwd *auto_pw;
906
907 auto_pw = getpwnam(pw->pw_name);
908 if (auto_pw != NULL) {
909 pw->pw_gid = auto_pw->pw_gid;
910
911 } else {
912 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
913 "LDAPDefaultGID Auto in effect but failed automatic user "
914 "'%s' lookup: %s", pw->pw_name, strerror(errno));
915 return NULL;
916 }
917
918 } else {
919 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
920 "LDAPDefaultGID Auto in effect but user name is missing");
921 return NULL;
922 }
923
924 } else {
925 pw->pw_gid = ldap_defaultgid;
926 }
927 ++i;
928
929 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
930 "using LDAPDefaultGID %s", pr_gid2str(NULL, pw->pw_gid));
931 continue;
932 }
933
934 if (strcasecmp(attrs[i], ldap_attr_homedirectory) == 0) {
935 if (ldap_genhdir == FALSE ||
936 ldap_genhdir_prefix == NULL) {
937 dn = ldap_get_dn(ld, e);
938
939 if (ldap_genhdir == FALSE) {
940 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
941 "no %s attribute for DN %s, LDAPGenerateHomedir not enabled",
942 ldap_attr_homedirectory, dn);
943
944 } else {
945 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
946 "no %s attribute for DN %s, LDAPGenerateHomedir enabled but "
947 "LDAPGenerateHomedirPrefix not configured",
948 ldap_attr_homedirectory, dn);
949 }
950
951 free(dn);
952 return NULL;
953 }
954
955 if (ldap_genhdir_prefix_nouname == TRUE) {
956 pw->pw_dir = pstrcat(session.pool, ldap_genhdir_prefix, NULL);
957
958 } else {
959 LDAP_VALUE_T **canon_username;
960 canon_username = LDAP_GET_VALUES(ld, e, ldap_attr_uid);
961 if (canon_username == NULL) {
962 dn = ldap_get_dn(ld, e);
963
964 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
965 "could not get %s attribute for canonical username for DN %s",
966 ldap_attr_uid, dn);
967 free(dn);
968 return NULL;
969 }
970
971 pw->pw_dir = pstrcat(session.pool, ldap_genhdir_prefix, "/",
972 LDAP_VALUE(canon_username, 0), NULL);
973 LDAP_VALUE_FREE(canon_username);
974 }
975
976 ++i;
977
978 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
979 "using default homedir %s", pw->pw_dir);
980 continue;
981 }
982
983 /* Don't worry if we don't have a loginShell attr. */
984 if (strcasecmp(attrs[i], ldap_attr_loginshell) == 0) {
985 /* Prevent a segfault if no loginShell attribute, and
986 * "RequireValidShell on" is in effect.
987 */
988 pw->pw_shell = pstrdup(session.pool, "");
989 ++i;
990 continue;
991 }
992
993 /* We only restart the while loop above if we can fill in alternate
994 * values for certain attributes. If something odd has happened, we
995 * fall through so we can complain.
996 */
997
998 dn = ldap_get_dn(ld, e);
999
1000 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1001 "could not get values for attribute %s for DN %s, ignoring request "
1002 "(perhaps this DN's entry does not have the attribute?)", attrs[i], dn);
1003 free(dn);
1004 ldap_msgfree(result);
1005 return NULL;
1006 }
1007
1008 /* Now that we've handled default values, fill in the struct as normal;
1009 * the if branches below for nonexistent attrs will just never be
1010 * called.
1011 */
1012
1013 if (strcasecmp(attrs[i], ldap_attr_uid) == 0) {
1014 pw->pw_name = pstrdup(session.pool, LDAP_VALUE(values, 0));
1015
1016 } else if (strcasecmp(attrs[i], ldap_attr_userpassword) == 0) {
1017 pw->pw_passwd = pstrdup(session.pool, LDAP_VALUE(values, 0));
1018
1019 } else if (strcasecmp(attrs[i], ldap_attr_uidnumber) == 0) {
1020 if (ldap_forcedefaultuid == TRUE &&
1021 ldap_defaultuid != (uid_t) -1) {
1022
1023 if (ldap_defaultuid == (uid_t) PR_LDAP_AUTO_DEFAULT_ID) {
1024 struct passwd *auto_pw;
1025
1026 /* Note that we are deliberately bypassing our Auth API, and calling
1027 * the system `getpwnam(3)` directly in order to "automatically"
1028 * obtain the best UID.
1029 */
1030 auto_pw = getpwnam(pw->pw_name);
1031 if (auto_pw != NULL) {
1032 pw->pw_uid = auto_pw->pw_uid;
1033
1034 } else {
1035 pr_trace_msg(trace_channel, 3,
1036 "error automatically determining UID for user '%s': %s",
1037 pw->pw_name, strerror(errno));
1038 pw->pw_uid = ldap_defaultuid;
1039 }
1040
1041 } else {
1042 pw->pw_uid = ldap_defaultuid;
1043 }
1044
1045 } else {
1046 pw->pw_uid = (uid_t) strtoul(LDAP_VALUE(values, 0), NULL, 10);
1047 }
1048
1049 } else if (strcasecmp(attrs[i], ldap_attr_gidnumber) == 0) {
1050 if (ldap_forcedefaultgid == TRUE &&
1051 ldap_defaultgid != (gid_t) -1) {
1052
1053 if (ldap_defaultgid == (gid_t) PR_LDAP_AUTO_DEFAULT_ID) {
1054 struct passwd *auto_pw;
1055
1056 /* Note that we are deliberately bypassing our Auth API, and calling
1057 * the system `getpwnam(3)` directly in order to "automatically"
1058 * obtain the best GID.
1059 */
1060 auto_pw = getpwnam(pw->pw_name);
1061 if (auto_pw != NULL) {
1062 pw->pw_gid = auto_pw->pw_gid;
1063
1064 } else {
1065 pr_trace_msg(trace_channel, 3,
1066 "error automatically determining primary GID for user '%s': %s",
1067 pw->pw_name, strerror(errno));
1068 pw->pw_gid = ldap_defaultgid;
1069 }
1070
1071 } else {
1072 pw->pw_gid = ldap_defaultgid;
1073 }
1074
1075 } else {
1076 pw->pw_gid = (gid_t) strtoul(LDAP_VALUE(values, 0), NULL, 10);
1077 }
1078
1079 } else if (strcasecmp(attrs[i], ldap_attr_homedirectory) == 0) {
1080 if (ldap_forcegenhdir == TRUE) {
1081 if (ldap_genhdir == FALSE ||
1082 ldap_genhdir_prefix == NULL) {
1083
1084 if (ldap_genhdir == FALSE) {
1085 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1086 "LDAPForceGeneratedHomedir enabled but LDAPGenerateHomedir is "
1087 "not enabled");
1088
1089 } else {
1090 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1091 "LDAPForceGeneratedHomedir and LDAPGenerateHomedir enabled, but "
1092 "missing required LDAPGenerateHomedirPrefix");
1093 }
1094
1095 return NULL;
1096 }
1097
1098 if (pw->pw_dir != NULL) {
1099 pr_trace_msg(trace_channel, 8, "LDAPForceGeneratedHomedir in effect, "
1100 "overriding current LDAP home directory '%s'", pw->pw_dir);
1101 }
1102
1103 if (ldap_genhdir_prefix_nouname == TRUE) {
1104 pw->pw_dir = pstrdup(session.pool, ldap_genhdir_prefix);
1105
1106 } else {
1107 LDAP_VALUE_T **canon_username;
1108 canon_username = LDAP_GET_VALUES(ld, e, ldap_attr_uid);
1109 if (canon_username == NULL) {
1110 dn = ldap_get_dn(ld, e);
1111
1112 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1113 "could not get %s attribute for canonical username for DN %s",
1114 ldap_attr_uid, dn);
1115 free(dn);
1116 return NULL;
1117 }
1118
1119 pw->pw_dir = pstrcat(session.pool, ldap_genhdir_prefix, "/",
1120 LDAP_VALUE(canon_username, 0), NULL);
1121 LDAP_VALUE_FREE(canon_username);
1122 }
1123
1124 } else {
1125 pw->pw_dir = pstrdup(session.pool, LDAP_VALUE(values, 0));
1126 }
1127
1128 pr_trace_msg(trace_channel, 8, "using LDAP home directory '%s'",
1129 pw->pw_dir);
1130
1131 } else if (strcasecmp(attrs[i], ldap_attr_loginshell) == 0) {
1132 pw->pw_shell = pstrdup(session.pool, LDAP_VALUE(values, 0));
1133
1134 } else {
1135 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1136 "user lookup attribute/value loop found unknown attribute %s",
1137 attrs[i]);
1138 }
1139
1140 LDAP_VALUE_FREE(values);
1141 ++i;
1142 }
1143
1144 /* If we're doing auth binds, save the DN of this entry so we can
1145 * bind to the LDAP server as it later.
1146 */
1147 if (user_dn) {
1148 *user_dn = ldap_get_dn(ld, e);
1149 }
1150
1151 ldap_msgfree(result);
1152
1153 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1154 "found user %s, UID %s, GID %s, homedir %s, shell %s",
1155 pw->pw_name, pr_uid2str(p, pw->pw_uid), pr_gid2str(p, pw->pw_gid),
1156 pw->pw_dir, pw->pw_shell);
1157 return pw;
1158 }
1159
1160 static struct group *pr_ldap_group_lookup(pool *p, char *filter_template,
1161 const char *replace, char *attrs[]) {
1162 const char *filter;
1163 char *dn;
1164 int i = 0, value_count = 0, value_offset;
1165 struct group *gr;
1166 LDAPMessage *result, *e;
1167 LDAP_VALUE_T **values;
1168
1169 if (ldap_gid_basedn == NULL) {
1170 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1171 "no LDAP base DN specified for group lookups");
1172 return NULL;
1173 }
1174
1175 filter = pr_ldap_interpolate_filter(p, filter_template, replace);
1176 if (filter == NULL) {
1177 return NULL;
1178 }
1179
1180 result = pr_ldap_search(ldap_gid_basedn, filter, attrs, 2, TRUE);
1181 if (result == NULL) {
1182 return NULL;
1183 }
1184
1185 e = ldap_first_entry(ld, result);
1186 if (e == NULL) {
1187 ldap_msgfree(result);
1188
1189 /* No LDAP entries found for this user. */
1190 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1191 "no group entries for filter %s", filter);
1192 return NULL;
1193 }
1194
1195 gr = pcalloc(session.pool, sizeof(struct group));
1196 while (attrs[i] != NULL) {
1197 pr_signals_handle();
1198
1199 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1200 "fetching values for attribute %s", attrs[i]);
1201
1202 values = LDAP_GET_VALUES(ld, e, attrs[i]);
1203 if (values == NULL) {
1204 if (strcasecmp(attrs[i], ldap_attr_memberuid) == 0) {
1205 gr->gr_mem = palloc(session.pool, 2 * sizeof(char *));
1206 gr->gr_mem[0] = pstrdup(session.pool, "");
1207 gr->gr_mem[1] = NULL;
1208
1209 ++i;
1210 continue;
1211 }
1212
1213 ldap_msgfree(result);
1214 dn = ldap_get_dn(ld, e);
1215
1216 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1217 "could not get values for attribute %s for DN %s, ignoring request "
1218 "(perhaps that DN does not have that attribute?)", attrs[i], dn);
1219 free(dn);
1220 return NULL;
1221 }
1222
1223 if (strcasecmp(attrs[i], ldap_attr_cn) == 0) {
1224 gr->gr_name = pstrdup(session.pool, LDAP_VALUE(values, 0));
1225
1226 } else if (strcasecmp(attrs[i], ldap_attr_gidnumber) == 0) {
1227 gr->gr_gid = strtoul(LDAP_VALUE(values, 0), NULL, 10);
1228
1229 } else if (strcasecmp(attrs[i], ldap_attr_memberuid) == 0) {
1230 value_count = LDAP_COUNT_VALUES(values);
1231
1232 /* Make sure we allocate enough memory for each member name, and one
1233 * more for the terminating NULL.
1234 */
1235 gr->gr_mem = (char **) pcalloc(session.pool, (value_count + 1) * sizeof(char *));
1236
1237 for (value_offset = 0; value_offset < value_count; ++value_offset) {
1238 gr->gr_mem[value_offset] =
1239 pstrdup(session.pool, LDAP_VALUE(values, value_offset));
1240 }
1241
1242 } else {
1243 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1244 "group lookup attribute/value loop found unknown attribute %s",
1245 attrs[i]);
1246 }
1247
1248 LDAP_VALUE_FREE(values);
1249 ++i;
1250 }
1251
1252 ldap_msgfree(result);
1253
1254 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1255 "found group %s, GID %s", gr->gr_name, pr_gid2str(NULL, gr->gr_gid));
1256 for (i = 0; i < value_count; ++i) {
1257 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1258 "+ member: %s", gr->gr_mem[i]);
1259 }
1260
1261 return gr;
1262 }
1263
1264 static void cached_quota_cleanup(void *event_data) {
1265 cached_quota = NULL;
1266 }
1267
1268 static void parse_quota(pool *p, const char *replace, char *str) {
1269 char **elts, *token;
1270
1271 if (cached_quota == NULL) {
1272 cached_quota = make_array(p, 9, sizeof(char *));
1273 register_cleanup2(p, NULL, cached_quota_cleanup);
1274 }
1275
1276 elts = (char **) cached_quota->elts;
1277 elts[0] = pstrdup(p, replace);
1278 cached_quota->nelts = 1;
1279
1280 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1281 "parsing ftpQuota attribute value '%s'", str);
1282
1283 while ((token = strsep(&str, ","))) {
1284 pr_signals_handle();
1285 *((char **) push_array(cached_quota)) = pstrdup(p, token);
1286 }
1287 }
1288
1289 static unsigned char pr_ldap_quota_lookup(pool *p, char *filter_template,
1290 const char *replace, const char *basedn) {
1291 const char *filter = NULL;
1292 char *attrs[] = {
1293 ldap_attr_ftpquota, ldap_attr_ftpquota_profiledn, NULL,
1294 };
1295 int orig_scope, res;
1296 LDAPMessage *result, *e;
1297 LDAP_VALUE_T **values;
1298
1299 if (basedn == NULL) {
1300 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1301 "no LDAP base DN specified for quota lookups, declining request");
1302 return FALSE;
1303 }
1304
1305 if (filter_template != NULL) {
1306 filter = pr_ldap_interpolate_filter(p, filter_template, replace);
1307 if (filter == NULL) {
1308 return FALSE;
1309 }
1310 }
1311
1312 result = pr_ldap_search(basedn, filter, attrs, 2, TRUE);
1313 if (result == NULL) {
1314 return FALSE;
1315 }
1316
1317 if (ldap_count_entries(ld, result) > 1) {
1318 ldap_msgfree(result);
1319
1320 if (ldap_default_quota != NULL) {
1321 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1322 "multiple entries found for DN %s, using default quota %s", basedn,
1323 ldap_default_quota);
1324 parse_quota(p, replace, pstrdup(p, ldap_default_quota));
1325 return TRUE;
1326
1327 } else {
1328 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1329 "multiple entries found for DN %s, aborting query", basedn);
1330 }
1331
1332 return FALSE;
1333 }
1334
1335 e = ldap_first_entry(ld, result);
1336 if (e == NULL) {
1337 ldap_msgfree(result);
1338 if (ldap_default_quota == NULL) {
1339 if (filter == NULL) {
1340 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1341 "no entries for DN %s, and no default quota defined", basedn);
1342
1343 } else {
1344 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1345 "no entries for filter %s, and no default quota defined", filter);
1346 }
1347
1348 return FALSE;
1349 }
1350
1351 if (filter == NULL) {
1352 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1353 "no entries for DN %s, using default quota %s", basedn,
1354 ldap_default_quota);
1355
1356 } else {
1357 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1358 "no entries for filter %s, using default quota %s", filter,
1359 ldap_default_quota);
1360 }
1361
1362 parse_quota(p, replace, pstrdup(p, ldap_default_quota));
1363 return TRUE;
1364 }
1365
1366 values = LDAP_GET_VALUES(ld, e, attrs[0]);
1367 if (values != NULL) {
1368 parse_quota(p, replace, pstrdup(p, LDAP_VALUE(values, 0)));
1369 LDAP_VALUE_FREE(values);
1370 ldap_msgfree(result);
1371 return TRUE;
1372 }
1373
1374 if (filter == NULL) {
1375 if (ldap_default_quota == NULL) {
1376 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1377 "referenced DN %s does not have an ftpQuota attribute, and no "
1378 "default quota defined", basedn);
1379 return FALSE;
1380 }
1381
1382 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1383 "no ftpQuota attribute found for DN %s, using default quota %s", basedn,
1384 ldap_default_quota);
1385 parse_quota(p, replace, pstrdup(p, ldap_default_quota));
1386 return TRUE;
1387 }
1388
1389 values = LDAP_GET_VALUES(ld, e, attrs[1]);
1390 if (values != NULL) {
1391 orig_scope = ldap_search_scope;
1392 ldap_search_scope = LDAP_SCOPE_BASE;
1393 res = pr_ldap_quota_lookup(p, NULL, replace, LDAP_VALUE(values, 0));
1394 ldap_search_scope = orig_scope;
1395 LDAP_VALUE_FREE(values);
1396 ldap_msgfree(result);
1397 return res;
1398 }
1399
1400 ldap_msgfree(result);
1401 if (ldap_default_quota != NULL) {
1402 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1403 "no %s or %s attribute, using default quota %s", attrs[0], attrs[1],
1404 ldap_default_quota);
1405 parse_quota(p, replace, pstrdup(p, ldap_default_quota));
1406 return TRUE;
1407 }
1408
1409 /* No quota attributes for this user. */
1410 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1411 "no %s or %s attribute, and no default quota defined", attrs[0], attrs[1]);
1412 return FALSE;
1413 }
1414
1415 static unsigned char pr_ldap_ssh_pubkey_lookup(pool *p, char *filter_template,
1416 const char *replace, char *basedn) {
1417 const char *filter;
1418 char *attrs[] = {
1419 ldap_attr_ssh_pubkey, NULL,
1420 };
1421 int num_keys, i;
1422 LDAPMessage *result, *e;
1423 LDAP_VALUE_T **values;
1424
1425 if (basedn == NULL) {
1426 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1427 "no LDAP base DN specified for user lookups, declining SSH publickey "
1428 "lookup request");
1429 return FALSE;
1430 }
1431
1432 filter = pr_ldap_interpolate_filter(p, filter_template, replace);
1433 if (filter == NULL) {
1434 return FALSE;
1435 }
1436
1437 result = pr_ldap_search(basedn, filter, attrs, 2, TRUE);
1438 if (result == NULL) {
1439 return FALSE;
1440 }
1441
1442 if (ldap_count_entries(ld, result) > 1) {
1443 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1444 "LDAP search for SSH publickey using DN %s, filter %s returned multiple "
1445 "entries, aborting query", basedn, filter);
1446 ldap_msgfree(result);
1447 return FALSE;
1448 }
1449
1450 e = ldap_first_entry(ld, result);
1451 if (e == NULL) {
1452 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1453 "LDAP search for SSH publickey using DN %s, filter %s returned "
1454 "no entries", basedn, filter);
1455 ldap_msgfree(result);
1456 return FALSE;
1457 }
1458
1459 values = LDAP_GET_VALUES(ld, e, attrs[0]);
1460 if (values == NULL) {
1461 return FALSE;
1462 }
1463
1464 num_keys = LDAP_COUNT_VALUES(values);
1465 cached_ssh_pubkeys = make_array(p, num_keys, sizeof(char *));
1466 for (i = 0; i < num_keys; ++i) {
1467 *((char **) push_array(cached_ssh_pubkeys)) = pstrdup(p,
1468 LDAP_VALUE(values, i));
1469 }
1470 LDAP_VALUE_FREE(values);
1471
1472 ldap_msgfree(result);
1473 return TRUE;
1474 }
1475
1476 static struct group *pr_ldap_getgrnam(pool *p, const char *group_name) {
1477 char *group_attrs[] = {
1478 ldap_attr_cn, ldap_attr_gidnumber, ldap_attr_memberuid, NULL,
1479 };
1480
1481 return pr_ldap_group_lookup(p, ldap_group_name_filter, group_name,
1482 group_attrs);
1483 }
1484
1485 static struct group *pr_ldap_getgrgid(pool *p, gid_t gid) {
1486 const char *gidstr;
1487 char *group_attrs[] = {
1488 ldap_attr_cn, ldap_attr_gidnumber, ldap_attr_memberuid, NULL,
1489 };
1490
1491 gidstr = pr_gid2str(p, gid);
1492 return pr_ldap_group_lookup(p, ldap_group_gid_filter, gidstr, group_attrs);
1493 }
1494
1495 static struct passwd *pr_ldap_getpwnam(pool *p, const char *username) {
1496 const char *filter;
1497 char *name_attrs[] = {
1498 ldap_attr_userpassword, ldap_attr_uid, ldap_attr_uidnumber,
1499 ldap_attr_gidnumber, ldap_attr_homedirectory,
1500 ldap_attr_loginshell, NULL,
1501 };
1502
1503 filter = pr_ldap_interpolate_filter(p, ldap_user_basedn, username);
1504 if (filter == NULL) {
1505 return NULL;
1506 }
1507
1508 /* pr_ldap_user_lookup() returns NULL if it doesn't find an entry or
1509 * encounters an error. If everything goes all right, it returns a
1510 * struct passwd, so we can just return its result directly.
1511 *
1512 * We also do some cute stuff here to work around lameness in LDAP servers
1513 * like Sun Directory Services (SDS) 1.x and 3.x. If you request an attr
1514 * that you don't have access to, SDS totally ignores any entries with
1515 * that attribute. Thank you, Sun; how very smart of you. So if we're
1516 * doing auth binds, we don't request the userPassword attr.
1517 *
1518 * NOTE: if the UserPassword directive is configured, mod_auth will pass
1519 * a crypted password to ldap_auth_check(), which will NOT do auth binds
1520 * in order to support UserPassword. (Otherwise, it would try binding to
1521 * the directory and would ignore UserPassword.)
1522 *
1523 * We're reasonably safe in making that assumption as long as we never
1524 * fetch userPassword from the directory if auth binds are enabled. If we
1525 * fetched userPassword, auth binds would never be done because
1526 * ldap_auth_check() would always get a crypted password.
1527 */
1528 return pr_ldap_user_lookup(p, ldap_user_name_filter, username, filter,
1529 ldap_authbinds ? name_attrs + 1 : name_attrs,
1530 ldap_authbinds ? &ldap_authbind_dn : NULL);
1531 }
1532
1533 static struct passwd *pr_ldap_getpwuid(pool *p, uid_t uid) {
1534 const char *uidstr;
1535 char *uid_attrs[] = {
1536 ldap_attr_uid, ldap_attr_uidnumber, ldap_attr_gidnumber,
1537 ldap_attr_homedirectory, ldap_attr_loginshell, NULL,
1538 };
1539
1540 uidstr = pr_uid2str(p, uid);
1541 return pr_ldap_user_lookup(p, ldap_user_uid_filter, uidstr,
1542 ldap_user_basedn, uid_attrs, ldap_authbinds ? &ldap_authbind_dn : NULL);
1543 }
1544
1545 MODRET handle_ldap_quota_lookup(cmd_rec *cmd) {
1546 const char *basedn;
1547
1548 basedn = pr_ldap_interpolate_filter(cmd->tmp_pool,
1549 ldap_user_basedn, cmd->argv[0]);
1550 if (basedn == NULL) {
1551 return PR_DECLINED(cmd);
1552 }
1553
1554 if (cached_quota == NULL ||
1555 strcasecmp(((char **) cached_quota->elts)[0], cmd->argv[0]) != 0) {
1556
1557 if (pr_ldap_quota_lookup(cmd->tmp_pool, ldap_user_name_filter,
1558 cmd->argv[0], basedn) == FALSE) {
1559 return PR_DECLINED(cmd);
1560 }
1561
1562 } else {
1563 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1564 "returning cached quota for user %s", (char *) cmd->argv[0]);
1565 }
1566
1567 return mod_create_data(cmd, cached_quota);
1568 }
1569
1570 MODRET handle_ldap_ssh_pubkey_lookup(cmd_rec *cmd) {
1571 char *user;
1572
1573 if (ldap_do_users == FALSE) {
1574 return PR_DECLINED(cmd);
1575 }
1576
1577 user = cmd->argv[0];
1578
1579 if (cached_ssh_pubkeys != NULL &&
1580 cached_ssh_pubkeys->nelts > 0 &&
1581 strcasecmp(((char **) cached_ssh_pubkeys->elts)[0], user) == 0) {
1582
1583 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1584 "returning cached SSH public keys for user %s", user);
1585 return mod_create_data(cmd, cached_ssh_pubkeys);
1586 }
1587
1588 if (pr_ldap_ssh_pubkey_lookup(cmd->tmp_pool, ldap_user_name_filter,
1589 user, ldap_user_basedn) == FALSE) {
1590 return PR_DECLINED(cmd);
1591 }
1592
1593 return mod_create_data(cmd, cached_ssh_pubkeys);
1594 }
1595
1596 /* Auth handlers */
1597 MODRET ldap_auth_setpwent(cmd_rec *cmd) {
1598 if (ldap_do_users == FALSE &&
1599 ldap_do_groups == FALSE) {
1600 return PR_DECLINED(cmd);
1601 }
1602
1603 if (ld == NULL) {
1604 (void) pr_ldap_connect(&ld, TRUE);
1605 }
1606
1607 return PR_HANDLED(cmd);
1608 }
1609
1610 MODRET ldap_auth_endpwent(cmd_rec *cmd) {
1611 if (ldap_do_users == FALSE &&
1612 ldap_do_groups == FALSE) {
1613 return PR_DECLINED(cmd);
1614 }
1615
1616 pr_ldap_unbind();
1617 return PR_HANDLED(cmd);
1618 }
1619
1620 MODRET ldap_auth_getpwuid(cmd_rec *cmd) {
1621 struct passwd *pw = NULL;
1622
1623 if (ldap_do_users == FALSE) {
1624 return PR_DECLINED(cmd);
1625 }
1626
1627 pw = pr_ldap_getpwuid(cmd->tmp_pool, *((uid_t *) cmd->argv[0]));
1628 if (pw != NULL) {
1629 return mod_create_data(cmd, pw);
1630 }
1631
1632 return PR_DECLINED(cmd);
1633 }
1634
1635 MODRET ldap_auth_getpwnam(cmd_rec *cmd) {
1636 struct passwd *pw = NULL;
1637
1638 if (ldap_do_users == FALSE) {
1639 return PR_DECLINED(cmd);
1640 }
1641
1642 pw = pr_ldap_getpwnam(cmd->tmp_pool, cmd->argv[0]);
1643 if (pw != NULL) {
1644 return mod_create_data(cmd, pw);
1645 }
1646
1647 return PR_DECLINED(cmd);
1648 }
1649
1650 MODRET ldap_auth_getgrnam(cmd_rec *cmd) {
1651 struct group *gr = NULL;
1652
1653 if (ldap_do_groups == FALSE) {
1654 return PR_DECLINED(cmd);
1655 }
1656
1657 gr = pr_ldap_getgrnam(cmd->tmp_pool, cmd->argv[0]);
1658 if (gr != NULL) {
1659 return mod_create_data(cmd, gr);
1660 }
1661
1662 return PR_DECLINED(cmd);
1663 }
1664
1665 MODRET ldap_auth_getgrgid(cmd_rec *cmd) {
1666 struct group *gr = NULL;
1667
1668 if (ldap_do_groups == FALSE) {
1669 return PR_DECLINED(cmd);
1670 }
1671
1672 gr = pr_ldap_getgrgid(cmd->tmp_pool, *((gid_t *) cmd->argv[0]));
1673 if (gr != NULL) {
1674 return mod_create_data(cmd, gr);
1675 }
1676
1677 return PR_DECLINED(cmd);
1678 }
1679
1680 MODRET ldap_auth_getgroups(cmd_rec *cmd) {
1681 const char *filter;
1682 char *w[] = {
1683 ldap_attr_gidnumber, ldap_attr_cn, NULL,
1684 };
1685 struct passwd *pw;
1686 struct group *gr;
1687 LDAPMessage *result = NULL, *e;
1688 LDAP_VALUE_T **gidNumber, **cn;
1689 array_header *gids = (array_header *)cmd->argv[1],
1690 *groups = (array_header *)cmd->argv[2];
1691
1692 if (ldap_do_groups == FALSE) {
1693 return PR_DECLINED(cmd);
1694 }
1695
1696 if (gids == NULL ||
1697 groups == NULL) {
1698 return PR_DECLINED(cmd);
1699 }
1700
1701 pw = pr_ldap_getpwnam(cmd->tmp_pool, cmd->argv[0]);
1702 if (pw != NULL) {
1703 gr = pr_ldap_getgrgid(cmd->tmp_pool, pw->pw_gid);
1704 if (gr != NULL) {
1705 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1706 "adding user %s primary group %s/%s", pw->pw_name, gr->gr_name,
1707 pr_gid2str(NULL, pw->pw_gid));
1708 *((gid_t *) push_array(gids)) = pw->pw_gid;
1709 *((char **) push_array(groups)) = pstrdup(session.pool, gr->gr_name);
1710
1711 } else {
1712 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1713 "unable to determine group name for user %s primary GID %s, skipping",
1714 pw->pw_name, pr_gid2str(NULL, pw->pw_gid));
1715 }
1716 }
1717
1718 if (ldap_gid_basedn == NULL) {
1719 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1720 "no LDAP base DN specified for group lookups");
1721 goto return_groups;
1722 }
1723
1724 filter = pr_ldap_interpolate_filter(cmd->tmp_pool,
1725 ldap_group_member_filter, cmd->argv[0]);
1726 if (filter == NULL) {
1727 return NULL;
1728 }
1729
1730 result = pr_ldap_search(ldap_gid_basedn, filter, w, 0, TRUE);
1731 if (result == NULL) {
1732 return FALSE;
1733 }
1734
1735 if (ldap_count_entries(ld, result) == 0) {
1736 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1737 "no entries found for filter %s", filter);
1738 goto return_groups;
1739 }
1740
1741 for (e = ldap_first_entry(ld, result); e; e = ldap_next_entry(ld, e)) {
1742 gidNumber = LDAP_GET_VALUES(ld, e, w[0]);
1743 if (gidNumber == NULL) {
1744 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1745 "could not get values for %s attribute for getgroups(2), skipping "
1746 "current group", ldap_attr_gidnumber);
1747 continue;
1748 }
1749
1750 cn = LDAP_GET_VALUES(ld, e, w[1]);
1751 if (cn == NULL) {
1752 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1753 "could not get values for %s attribute for getgroups(2), skipping "
1754 "current group", ldap_attr_cn);
1755 continue;
1756 }
1757
1758 if (pw == NULL ||
1759 strtoul(LDAP_VALUE(gidNumber, 0), NULL, 10) != pw->pw_gid) {
1760 *((gid_t *) push_array(gids)) = strtoul(LDAP_VALUE(gidNumber, 0), NULL, 10);
1761 *((char **) push_array(groups)) = pstrdup(session.pool, LDAP_VALUE(cn, 0));
1762
1763 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1764 "added user %s secondary group %s/%s",
1765 (pw && pw->pw_name) ? pw->pw_name : (char *) cmd->argv[0],
1766 LDAP_VALUE(cn, 0), LDAP_VALUE(gidNumber, 0));
1767 }
1768
1769 LDAP_VALUE_FREE(gidNumber);
1770 LDAP_VALUE_FREE(cn);
1771 }
1772
1773 return_groups:
1774 if (result) {
1775 ldap_msgfree(result);
1776 }
1777
1778 if (gids->nelts > 0) {
1779 return mod_create_data(cmd, (void *) &gids->nelts);
1780 }
1781
1782 return PR_DECLINED(cmd);
1783 }
1784
1785 /* cmd->argv[0] : user name
1786 * cmd->argv[1] : cleartext password
1787 */
1788 MODRET ldap_auth_auth(cmd_rec *cmd) {
1789 const char *filter = NULL, *username;
1790 char *pass_attrs[] = {
1791 ldap_attr_userpassword, ldap_attr_uid, ldap_attr_uidnumber,
1792 ldap_attr_gidnumber, ldap_attr_homedirectory,
1793 ldap_attr_loginshell, NULL,
1794 };
1795 struct passwd *pw = NULL;
1796 int res;
1797
1798 if (ldap_do_users == FALSE) {
1799 return PR_DECLINED(cmd);
1800 }
1801
1802 username = cmd->argv[0];
1803
1804 filter = pr_ldap_interpolate_filter(cmd->tmp_pool, ldap_user_basedn,
1805 username);
1806 if (filter == NULL) {
1807 return NULL;
1808 }
1809
1810 /* If anything here fails hard (IOW, we've found an LDAP entry for the
1811 * user, but they appear to have entered the wrong password), fail auth.
1812 * Normally, I'd DECLINE here so other modules could have a shot, but if
1813 * we've found their LDAP entry, chances are that nothing else will be
1814 * able to auth them.
1815 */
1816
1817 pw = pr_ldap_user_lookup(cmd->tmp_pool,
1818 ldap_user_name_filter, username, filter,
1819 ldap_authbinds ? pass_attrs + 1 : pass_attrs,
1820 ldap_authbinds ? &ldap_authbind_dn : NULL);
1821 if (pw == NULL) {
1822 /* Can't find the user in the LDAP directory. */
1823 return PR_DECLINED(cmd);
1824 }
1825
1826 if (ldap_authbinds == FALSE &&
1827 pw->pw_passwd == NULL) {
1828 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1829 "LDAPAuthBinds not enabled, and unable to retrieve password for user %s",
1830 pw->pw_name);
1831 return PR_ERROR_INT(cmd, PR_AUTH_NOPWD);
1832 }
1833
1834 res = pr_auth_check(cmd->tmp_pool, ldap_authbinds ? NULL : pw->pw_passwd,
1835 username, cmd->argv[1]);
1836 if (res != 0) {
1837 if (res == -1) {
1838 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1839 "bad password for user %s: %s", pw->pw_name, strerror(errno));
1840
1841 } else {
1842 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1843 "bad password for user %s", pw->pw_name);
1844 }
1845
1846 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
1847 }
1848
1849 session.auth_mech = "mod_ldap.c";
1850 return PR_HANDLED(cmd);
1851 }
1852
1853 /* cmd->argv[0] = hashed password
1854 * cmd->argv[1] = user
1855 * cmd->argv[2] = cleartext
1856 */
1857 MODRET ldap_auth_check(cmd_rec *cmd) {
1858 char *pass, *cryptpass, *hash_method, *crypted;
1859 int encname_len, res;
1860 LDAP *ld_auth;
1861 #ifdef HAS_LDAP_SASL_BIND_S
1862 struct berval bindcred;
1863 #endif
1864
1865 if (ldap_do_users == FALSE) {
1866 return PR_DECLINED(cmd);
1867 }
1868
1869 cryptpass = cmd->argv[0];
1870 pass = cmd->argv[2];
1871
1872 /* At this point, any encrypted password must have come from the UserPassword
1873 * directive. Don't perform auth binds in this case, since the crypted
1874 * password specified should override auth binds.
1875 */
1876 if (ldap_authbinds == TRUE &&
1877 cryptpass == NULL) {
1878 /* Don't try to do auth binds with a NULL/empty DN or password. */
1879 if (pass == NULL ||
1880 strlen(pass) == 0) {
1881 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1882 "LDAPAuthBinds is enabled, but no user-supplied cleartext password "
1883 "was found");
1884 return PR_DECLINED(cmd);
1885 }
1886
1887 if (ldap_authbind_dn == NULL ||
1888 strlen(ldap_authbind_dn) == 0) {
1889 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1890 "LDAPAuthBinds is enabled, but no LDAP DN was found");
1891 return PR_DECLINED(cmd);
1892 }
1893
1894 if (pr_ldap_connect(&ld_auth, FALSE) < 0) {
1895 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1896 "unable to check login: LDAP connection failed");
1897 return PR_DECLINED(cmd);
1898 }
1899
1900 #ifdef HAS_LDAP_SASL_BIND_S
1901 bindcred.bv_val = cmd->argv[2];
1902 bindcred.bv_len = strlen(cmd->argv[2]);
1903 res = ldap_sasl_bind_s(ld_auth, ldap_authbind_dn, NULL, &bindcred,
1904 NULL, NULL, NULL);
1905 #else /* HAS_LDAP_SASL_BIND_S */
1906 res = ldap_simple_bind_s(ld_auth, ldap_authbind_dn, cmd->argv[2]);
1907 #endif /* HAS_LDAP_SASL_BIND_S */
1908
1909 if (res != LDAP_SUCCESS) {
1910 if (res != LDAP_INVALID_CREDENTIALS) {
1911 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1912 "unable to check login: bind as %s failed: %s", ldap_authbind_dn,
1913 ldap_err2string(res));
1914 }
1915
1916 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
1917 "invalid credentials used for %s", ldap_authbind_dn);
1918 LDAP_UNBIND(ld_auth);
1919 return PR_ERROR(cmd);
1920 }
1921
1922 LDAP_UNBIND(ld_auth);
1923 session.auth_mech = "mod_ldap.c";
1924 return PR_HANDLED(cmd);
1925 }
1926
1927 /* Get the length of "scheme" in the leading {scheme} so we can skip it
1928 * in the password comparison.
1929 */
1930 encname_len = strcspn(cryptpass + 1, "}");
1931 hash_method = pstrndup(cmd->tmp_pool, cryptpass + 1, encname_len);
1932
1933 /* Check to see how the password is encrypted, and check accordingly. */
1934
1935 if ((size_t) encname_len == strlen(cryptpass + 1)) {
1936 /* No leading {scheme}. */
1937 hash_method = ldap_defaultauthscheme;
1938 encname_len = 0;
1939
1940 } else {
1941 encname_len += 2;
1942 }
1943
1944 /* The {crypt} scheme */
1945 if (strncasecmp(hash_method, "crypt", strlen(hash_method)) == 0) {
1946 crypted = crypt(pass, cryptpass + encname_len);
1947 if (crypted == NULL) {
1948 pr_trace_msg(trace_channel, 19,
1949 "using %s auth scheme, crypt(3) failed: %s", hash_method,
1950 strerror(errno));
1951 return PR_ERROR(cmd);
1952 }
1953
1954 if (strcmp(crypted, cryptpass + encname_len) != 0) {
1955 pr_trace_msg(trace_channel, 19,
1956 "using '%s' auth scheme, comparison failed", hash_method);
1957 return PR_ERROR(cmd);
1958 }
1959
1960 /* The {clear} scheme */
1961 } else if (strncasecmp(hash_method, "clear", strlen(hash_method)) == 0) {
1962 if (strcmp(pass, cryptpass + encname_len) != 0) {
1963 pr_trace_msg(trace_channel, 19,
1964 "using '%s' auth scheme, comparison failed", hash_method);
1965 return PR_ERROR(cmd);
1966 }
1967
1968 } else {
1969 /* Can't find a supported {scheme} */
1970 pr_trace_msg(trace_channel, 3,
1971 "unsupported userPassword auth scheme: %s", hash_method);
1972 return PR_DECLINED(cmd);
1973 }
1974
1975 session.auth_mech = "mod_ldap.c";
1976 return PR_HANDLED(cmd);
1977 }
1978
1979 MODRET ldap_auth_uid2name(cmd_rec *cmd) {
1980 struct passwd *pw = NULL;
1981
1982 if (ldap_do_users == FALSE) {
1983 return PR_DECLINED(cmd);
1984 }
1985
1986 pw = pr_ldap_getpwuid(cmd->tmp_pool, *((uid_t *) cmd->argv[0]));
1987 if (pw == NULL) {
1988 /* Can't find the user in the LDAP directory. */
1989 return PR_DECLINED(cmd);
1990 }
1991
1992 return mod_create_data(cmd, pstrdup(permanent_pool, pw->pw_name));
1993 }
1994
1995 MODRET ldap_auth_gid2name(cmd_rec *cmd) {
1996 struct group *gr = NULL;
1997
1998 if (ldap_do_groups == FALSE) {
1999 return PR_DECLINED(cmd);
2000 }
2001
2002 gr = pr_ldap_getgrgid(cmd->tmp_pool, *((gid_t *) cmd->argv[0]));
2003 if (gr == NULL) {
2004 /* Can't find the user in the LDAP directory. */
2005 return PR_DECLINED(cmd);
2006 }
2007
2008 return mod_create_data(cmd, pstrdup(permanent_pool, gr->gr_name));
2009 }
2010
2011 MODRET ldap_auth_name2uid(cmd_rec *cmd) {
2012 struct passwd *pw = NULL;
2013
2014 if (ldap_do_users == FALSE) {
2015 return PR_DECLINED(cmd);
2016 }
2017
2018 pw = pr_ldap_getpwnam(cmd->tmp_pool, cmd->argv[0]);
2019 if (pw == NULL) {
2020 return PR_DECLINED(cmd);
2021 }
2022
2023 return mod_create_data(cmd, (void *) &pw->pw_uid);
2024 }
2025
2026 MODRET ldap_auth_name2gid(cmd_rec *cmd) {
2027 struct group *gr = NULL;
2028
2029 if (ldap_do_groups == FALSE) {
2030 return PR_DECLINED(cmd);
2031 }
2032
2033 gr = pr_ldap_getgrnam(cmd->tmp_pool, cmd->argv[0]);
2034 if (gr == NULL) {
2035 return PR_DECLINED(cmd);
2036 }
2037
2038 return mod_create_data(cmd, (void *) &gr->gr_gid);
2039 }
2040
2041 /* sasl_info functions. */
2042
2043 /* Note: by proving empty strings, rather than NULLs, for the default values,
2044 * we make the logic in sasl_interact_cb() simpler.
2045 */
2046 static struct sasl_info *sasl_info_create(pool *p, LDAP *conn_ld) {
2047 pool *sasl_pool;
2048 struct sasl_info *sasl;
2049
2050 sasl_pool = make_sub_pool(p);
2051 pr_pool_tag(sasl_pool, "SASL Info Pool");
2052
2053 sasl = pcalloc(sasl_pool, sizeof(struct sasl_info));
2054 sasl->pool = sasl_pool;
2055
2056 #if defined(LDAP_OPT_X_SASL_AUTHCID)
2057 {
2058 int res;
2059 char *sasl_authcid = NULL;
2060
2061 res = ldap_get_option(conn_ld, LDAP_OPT_X_SASL_AUTHCID, &sasl_authcid);
2062 if (res == LDAP_OPT_SUCCESS) {
2063 if (sasl_authcid != NULL) {
2064 pr_trace_msg(trace_channel, 12,
2065 "LDAP SASL default authentication ID = %s (see SASL_AUTHCID in "
2066 "ldap.conf)", sasl_authcid);
2067 sasl->authentication_id = pstrdup(sasl_pool, sasl_authcid);
2068 ldap_memfree(sasl_authcid);
2069
2070 } else {
2071 sasl->authentication_id = pstrdup(sasl_pool, "");
2072 }
2073
2074 } else {
2075 pr_trace_msg(trace_channel, 3,
2076 "error retrieving LDAP_OPT_X_SASL_AUTHCID: %s", ldap_err2string(res));
2077 }
2078 }
2079 #endif /* LDAP_OPT_X_SASL_AUTHCID */
2080
2081 #if defined(LDAP_OPT_X_SASL_AUTHZID)
2082 {
2083 int res;
2084 char *sasl_authzid = NULL;
2085
2086 res = ldap_get_option(conn_ld, LDAP_OPT_X_SASL_AUTHZID, &sasl_authzid);
2087 if (res == LDAP_OPT_SUCCESS) {
2088 if (sasl_authzid != NULL) {
2089 pr_trace_msg(trace_channel, 12,
2090 "LDAP SASL default authorization ID = %s (see SASL_AUTHZID in "
2091 "ldap.conf)", sasl_authzid);
2092 sasl->authorization_id = pstrdup(sasl_pool, sasl_authzid);
2093 ldap_memfree(sasl_authzid);
2094
2095 } else {
2096 sasl->authorization_id = pstrdup(sasl_pool, "");
2097 }
2098
2099 } else {
2100 pr_trace_msg(trace_channel, 3,
2101 "error retrieving LDAP_OPT_X_SASL_AUTHZID: %s", ldap_err2string(res));
2102 }
2103 }
2104 #endif /* LDAP_OPT_X_SASL_AUTHZID */
2105
2106 #if defined(LDAP_OPT_X_SASL_REALM)
2107 {
2108 int res;
2109 char *sasl_realm = NULL;
2110
2111 res = ldap_get_option(conn_ld, LDAP_OPT_X_SASL_REALM, &sasl_realm);
2112 if (res == LDAP_OPT_SUCCESS) {
2113 if (sasl_realm != NULL) {
2114 pr_trace_msg(trace_channel, 12,
2115 "LDAP SASL default realm = %s (see SASL_REALM in ldap.conf)",
2116 sasl_realm);
2117 sasl->realm = pstrdup(sasl_pool, sasl_realm);
2118 ldap_memfree(sasl_realm);
2119
2120 } else {
2121 sasl->realm = pstrdup(sasl_pool, "");
2122 }
2123
2124 } else {
2125 pr_trace_msg(trace_channel, 3,
2126 "error retrieving LDAP_OPT_X_SASL_REALM: %s", ldap_err2string(res));
2127 }
2128 }
2129 #endif /* LDAP_OPT_X_SASL_REALM */
2130
2131 return sasl;
2132 }
2133
2134 static void sasl_info_get_authcid_from_dn(struct sasl_info *sasl,
2135 const char *dn_text) {
2136 register unsigned int i;
2137 LDAPDN dn;
2138 int flags = LDAP_DN_FORMAT_LDAPV3, res;
2139
2140 /* LDAPDN is actually LDAPRDN *, a pointer to a list of LDAPRDN. Which
2141 * makes sense, since a DN is a list of RDNs. And each LDAPRDN is actually
2142 * LDAPAVA *, a pointer to a list of LDAPAVA (Attribute Value Assertions).
2143 */
2144 res = ldap_str2dn(dn_text, &dn, flags);
2145 if (res != LDAP_SUCCESS) {
2146 pr_trace_msg(trace_channel, 3,
2147 "error parsing DN '%s': %s", dn_text, ldap_err2string(res));
2148 return;
2149 }
2150
2151 for (i = 0; dn[i]; i++) {
2152 LDAPRDN rdn;
2153 char *rdn_text = NULL;
2154
2155 rdn = dn[i];
2156 res = ldap_rdn2str(rdn, &rdn_text, flags);
2157 if (res == LDAP_SUCCESS) {
2158 if (strncasecmp(rdn_text, "CN=", 3) == 0) {
2159 sasl->authentication_id = pstrdup(sasl->pool, rdn_text + 3);
2160 }
2161 ldap_memfree(rdn_text);
2162
2163 if (sasl->authentication_id != NULL) {
2164 break;
2165 }
2166
2167 } else {
2168 pr_trace_msg(trace_channel, 3,
2169 "error converting RDN to text: %s", ldap_err2string(res));
2170 }
2171 }
2172
2173 ldap_dnfree(dn);
2174 }
2175
2176 /* server_info functions. */
2177 static struct server_info *server_info_create(pool *p) {
2178 struct server_info *info;
2179
2180 info = pcalloc(p, sizeof(struct server_info));
2181 info->ssl_verify = -1;
2182
2183 return info;
2184 }
2185
2186 static void server_info_get_ssl_defaults(struct server_info *info) {
2187 #if defined(LDAP_OPT_X_TLS)
2188 int res, ssl_verify;
2189 char *ssl_val = NULL;
2190
2191 /* Fill in the SSL defaults, if not explicitly configured. */
2192
2193 # if defined(LDAP_OPT_X_TLS_CACERTFILE)
2194 if (info->ssl_ca_file == NULL) {
2195 ssl_val = NULL;
2196 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, &ssl_val);
2197 if (res == LDAP_OPT_SUCCESS) {
2198 if (ssl_val != NULL) {
2199 pr_trace_msg(trace_channel, 17,
2200 "using default 'ssl-ca' value: %s", ssl_val);
2201 info->ssl_ca_file = ldap_strdup(ssl_val);
2202 }
2203 }
2204 }
2205 # endif /* LDAP_OPT_X_TLS_CACERTFILE */
2206
2207 # if defined(LDAP_OPT_X_TLS_CERTFILE)
2208 if (info->ssl_cert_file == NULL) {
2209 ssl_val = NULL;
2210 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_CERTFILE, &ssl_val);
2211 if (res == LDAP_OPT_SUCCESS) {
2212 if (ssl_val != NULL) {
2213 pr_trace_msg(trace_channel, 17,
2214 "using default 'ssl-cert' value: %s", ssl_val);
2215 info->ssl_cert_file = ldap_strdup(ssl_val);
2216 }
2217 }
2218 }
2219 # endif /* LDAP_OPT_X_TLS_CERTFILE */
2220
2221 # if defined(LDAP_OPT_X_TLS_KEYFILE)
2222 if (info->ssl_key_file == NULL) {
2223 ssl_val = NULL;
2224 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_KEYFILE, &ssl_val);
2225 if (res == LDAP_OPT_SUCCESS) {
2226 if (ssl_val != NULL) {
2227 pr_trace_msg(trace_channel, 17,
2228 "using default 'ssl-key' value: %s", ssl_val);
2229 info->ssl_key_file = ldap_strdup(ssl_val);
2230 }
2231 }
2232 }
2233 # endif /* LDAP_OPT_X_TLS_KEYFILE */
2234
2235 # if defined(LDAP_OPT_X_TLS_CIPHER_SUITE)
2236 if (info->ssl_ciphers == NULL) {
2237 ssl_val = NULL;
2238 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_CIPHER_SUITE, &ssl_val);
2239 if (res == LDAP_OPT_SUCCESS) {
2240 if (ssl_val != NULL) {
2241 pr_trace_msg(trace_channel, 17,
2242 "using default 'ssl-ciphers' value: %s", ssl_val);
2243 info->ssl_ciphers = ldap_strdup(ssl_val);
2244 }
2245 }
2246 }
2247 # endif /* LDAP_OPT_X_TLS_CIPHER_SUITE */
2248
2249 # if defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
2250 if (info->ssl_verify == -1) {
2251 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ssl_verify);
2252 if (res == LDAP_OPT_SUCCESS) {
2253 ssl_val = NULL;
2254
2255 switch (ssl_verify) {
2256 # if defined(LDAP_OPT_X_TLS_NEVER)
2257 case LDAP_OPT_X_TLS_NEVER:
2258 ssl_val = "never";
2259 break;
2260 # endif /* LDAP_OPT_X_TLS_NEVER */
2261
2262 # if defined(LDAP_OPT_X_TLS_HARD)
2263 case LDAP_OPT_X_TLS_HARD:
2264 ssl_val = "hard";
2265 break;
2266 # endif /* LDAP_OPT_X_TLS_HARD */
2267
2268 # if defined(LDAP_OPT_X_TLS_DEMAND)
2269 case LDAP_OPT_X_TLS_DEMAND:
2270 ssl_val = "demand";
2271 break;
2272 # endif /* LDAP_OPT_X_TLS_DEMAND */
2273
2274 # if defined(LDAP_OPT_X_TLS_ALLOW)
2275 case LDAP_OPT_X_TLS_ALLOW:
2276 ssl_val = "allow";
2277 break;
2278 # endif /* LDAP_OPT_X_TLS_ALLOW */
2279
2280 # if defined(LDAP_OPT_X_TLS_TRY)
2281 case LDAP_OPT_X_TLS_TRY:
2282 ssl_val = "try";
2283 break;
2284 # endif /* LDAP_OPT_X_TLS_TRY */
2285
2286 default:
2287 ssl_val = NULL;
2288 }
2289
2290 pr_trace_msg(trace_channel, 17,
2291 "using default 'ssl-verify' value: %s", ssl_val ? ssl_val : "UNKNOWN");
2292
2293 info->ssl_verify = ssl_verify;
2294
2295 if (ssl_val != NULL) {
2296 info->ssl_verify_text = ldap_strdup(ssl_val);
2297 }
2298 }
2299 }
2300 # endif /* LDAP_OPT_X_TLS_REQUIRE_CERT */
2301 #endif /* LDAP_OPT_X_TLS */
2302 }
2303
2304 /* Free up any library-allocated memory. */
2305 static void server_info_free(struct server_info *info) {
2306 if (info->url_desc != NULL) {
2307 ldap_free_urldesc(info->url_desc);
2308 info->url_desc = NULL;
2309 }
2310
2311 if (info->url_text != NULL) {
2312 ldap_memfree(info->url_text);
2313 info->url_text = NULL;
2314 }
2315
2316 if (info->ssl_ca_file != NULL) {
2317 ldap_memfree((char *) info->ssl_ca_file);
2318 info->ssl_ca_file = NULL;
2319 }
2320
2321 if (info->ssl_cert_file != NULL) {
2322 ldap_memfree((char *) info->ssl_cert_file);
2323 info->ssl_cert_file = NULL;
2324 }
2325
2326 if (info->ssl_key_file != NULL) {
2327 ldap_memfree((char *) info->ssl_key_file);
2328 info->ssl_key_file = NULL;
2329 }
2330
2331 if (info->ssl_ciphers != NULL) {
2332 ldap_memfree((char *) info->ssl_ciphers);
2333 info->ssl_ciphers = NULL;
2334 }
2335
2336 info->ssl_verify = -1;
2337
2338 if (info->ssl_verify_text != NULL) {
2339 ldap_memfree((char *) info->ssl_verify_text);
2340 info->ssl_verify_text = NULL;
2341 }
2342 }
2343
2344 static void server_infos_free(void) {
2345 server_rec *s;
2346
2347 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
2348 register unsigned int i;
2349 config_rec *c;
2350 array_header *infos;
2351
2352 c = find_config(s->conf, CONF_PARAM, "LDAPServer", FALSE);
2353 if (c == NULL) {
2354 continue;
2355 }
2356
2357 pr_signals_handle();
2358
2359 infos = c->argv[0];
2360 for (i = 0; i < infos->nelts; i++) {
2361 struct server_info *info;
2362
2363 info = ((struct server_info **) infos->elts)[i];
2364 server_info_free(info);
2365 }
2366 }
2367 }
2368
2369 /* Configuration handlers
2370 */
2371
2372 /* usage: LDAPLog path|"none" */
2373 MODRET set_ldaplog(cmd_rec *cmd) {
2374 CHECK_ARGS(cmd, 1);
2375 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2376
2377 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
2378 return PR_HANDLED(cmd);
2379 }
2380
2381 MODRET set_ldapprotoversion(cmd_rec *cmd) {
2382 int i = 0;
2383 config_rec *c;
2384 char *version;
2385
2386 CHECK_ARGS(cmd, 1);
2387 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2388
2389 version = cmd->argv[1];
2390 while (version[i]) {
2391 if (!PR_ISDIGIT((int) version[i])) {
2392 CONF_ERROR(cmd, "LDAPProtocolVersion: argument must be numeric!");
2393 }
2394
2395 ++i;
2396 }
2397
2398 c = add_config_param(cmd->argv[0], 1, NULL);
2399 c->argv[0] = pcalloc(c->pool, sizeof(int));
2400 *((int *) c->argv[0]) = atoi(version);
2401
2402 return PR_HANDLED(cmd);
2403 }
2404
2405 /* usage: LDAPServer info [ssl-cert:<path>] [ssl-key:<path>] [ssl-ca:<path>]
2406 * [ssl-ciphers:ciphers] [ssl-verify:verify]
2407 */
2408 MODRET set_ldapserver(cmd_rec *cmd) {
2409 register unsigned int i;
2410 struct server_info *info = NULL;
2411 array_header *infos, *items;
2412 config_rec *c;
2413
2414 if (cmd->argc < 2) {
2415 CONF_ERROR(cmd, "wrong number of parameters");
2416 }
2417 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2418
2419 c = add_config_param(cmd->argv[0], 1, NULL);
2420 infos = make_array(c->pool, cmd->argc - 1, sizeof(struct server_info *));
2421 c->argv[0] = infos;
2422
2423 /* For historical reasons (and leaky abstractions from underlying LDAP
2424 * libraries), the LDAPServer directive supports having multiple hosts/URLs
2425 * being passed as a single string, quoted and separated by whitespace.
2426 * That is not really necessary, but we need to handle such items as well.
2427 *
2428 * Order matters, since the ordering of these hosts/URLs dictates the
2429 * order in which they are tried when attempting to connect.
2430 */
2431
2432 items = make_array(cmd->tmp_pool, cmd->argc - 1, sizeof(char *));
2433 for (i = 1; i < cmd->argc; i++) {
2434 char *item;
2435
2436 item = cmd->argv[i];
2437
2438 /* Split non-URL arguments on whitespace and insert them as separate
2439 * servers.
2440 */
2441 while (*item) {
2442 size_t len;
2443
2444 pr_signals_handle();
2445
2446 len = strcspn(item, " \f\n\r\t\v");
2447 *((char **) push_array(items)) = pstrndup(cmd->tmp_pool, item, len);
2448
2449 item += len;
2450 while (PR_ISSPACE(*item)) {
2451 ++item;
2452 }
2453 }
2454 }
2455
2456 for (i = 0; i < items->nelts; i++) {
2457 char *item;
2458
2459 item = ((char **) items->elts)[i];
2460
2461 /* Is this an SSL option? If so, it needs to be applied to the
2462 * previously defined LDAP URL/host. Otherwise, it's a misconfiguration.
2463 */
2464 if (strncmp(item, "ssl-ca:", 7) == 0) {
2465 char *path;
2466
2467 if (info == NULL) {
2468 CONF_ERROR(cmd, "wrong order of parameters");
2469 }
2470
2471 path = item;
2472
2473 /* Advance past the "ssl-ca:" prefix. */
2474 path += 7;
2475
2476 if (file_exists2(cmd->tmp_pool, path) != TRUE) {
2477 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "SSL CA file '", path, "': ",
2478 strerror(ENOENT), NULL));
2479 }
2480
2481 info->ssl_ca_file = ldap_strdup(path);
2482 continue;
2483
2484 } else if (strncmp(item, "ssl-cert:", 9) == 0) {
2485 char *path;
2486
2487 if (info == NULL) {
2488 CONF_ERROR(cmd, "wrong order of parameters");
2489 }
2490
2491 path = item;
2492
2493 /* Advance past the "ssl-cert:" prefix. */
2494 path += 9;
2495
2496 if (file_exists2(cmd->tmp_pool, path) != TRUE) {
2497 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "SSL certificate file '",
2498 path, "': ", strerror(ENOENT), NULL));
2499 }
2500
2501 info->ssl_cert_file = ldap_strdup(path);
2502 continue;
2503
2504 } else if (strncmp(item, "ssl-key:", 8) == 0) {
2505 char *path;
2506
2507 if (info == NULL) {
2508 CONF_ERROR(cmd, "wrong order of parameters");
2509 }
2510
2511 path = item;
2512
2513 /* Advance past the "ssl-key:" prefix. */
2514 path += 8;
2515
2516 if (file_exists2(cmd->tmp_pool, path) != TRUE) {
2517 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "SSL certificate key file '",
2518 path, "': ", strerror(ENOENT), NULL));
2519 }
2520
2521 info->ssl_key_file = ldap_strdup(path);
2522 continue;
2523
2524 } else if (strncmp(item, "ssl-ciphers:", 12) == 0) {
2525 char *ciphers;
2526
2527 if (info == NULL) {
2528 CONF_ERROR(cmd, "wrong order of parameters");
2529 }
2530
2531 ciphers = item;
2532
2533 /* Advance past the "ssl-ciphers:" prefix. */
2534 ciphers += 12;
2535
2536 info->ssl_ciphers = ldap_strdup(ciphers);
2537 continue;
2538
2539 } else if (strncmp(item, "ssl-verify:", 11) == 0) {
2540 int b, ssl_verify = -1;
2541 char *verify_text;
2542
2543 if (info == NULL) {
2544 CONF_ERROR(cmd, "wrong order of parameters");
2545 }
2546
2547 verify_text = item;
2548
2549 /* Advance past the "ssl-verify:" prefix. */
2550 verify_text += 11;
2551
2552 /* For convenience, we accept the usual on/off values, AND the
2553 * LDAP specific names, for those who think they want the finer
2554 * control.
2555 */
2556
2557 b = pr_str_is_boolean(verify_text);
2558 if (b < 0) {
2559 #if defined(LDAP_OPT_X_TLS_ALLOW)
2560 if (strcasecmp(verify_text, "allow") == 0) {
2561 ssl_verify = LDAP_OPT_X_TLS_ALLOW;
2562 #else
2563 if (FALSE) {
2564 #endif /* LDAP_OPT_X_TLS_ALLOW */
2565
2566 #if defined(LDAP_OPT_X_TLS_DEMAND)
2567 } else if (strcasecmp(verify_text, "demand") == 0) {
2568 ssl_verify = LDAP_OPT_X_TLS_DEMAND;
2569 #endif /* LDAP_OPT_X_TLS_DEMAND */
2570
2571 #if defined(LDAP_OPT_X_TLS_NEVER)
2572 } else if (strcasecmp(verify_text, "never") == 0) {
2573 ssl_verify = LDAP_OPT_X_TLS_NEVER;
2574 #endif /* LDAP_OPT_X_TLS_NEVER */
2575
2576 #if defined(LDAP_OPT_X_TLS_TRY)
2577 } else if (strcasecmp(verify_text, "try") == 0) {
2578 ssl_verify = LDAP_OPT_X_TLS_TRY;
2579 #endif /* LDAP_OPT_X_TLS_TRY */
2580
2581 } else {
2582 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
2583 "unknown/unsupported 'ssl-verify' value: ", verify_text, NULL));
2584 }
2585
2586 } else {
2587 if (b == TRUE) {
2588 #if defined(LDAP_OPT_X_TLS_DEMAND)
2589 ssl_verify = LDAP_OPT_X_TLS_DEMAND;
2590 #endif /* LDAP_OPT_X_TLS_DEMAND */
2591 verify_text = "demand";
2592
2593 } else {
2594 #if defined(LDAP_OPT_X_TLS_NEVER)
2595 ssl_verify = LDAP_OPT_X_TLS_NEVER;
2596 #endif /* LDAP_OPT_X_TLS_NEVER */
2597 verify_text = "never";
2598 }
2599 }
2600
2601 info->ssl_verify = ssl_verify;
2602 info->ssl_verify_text = ldap_strdup(verify_text);
2603 continue;
2604
2605 } else {
2606 info = server_info_create(c->pool);
2607 }
2608
2609 info->info_text = pstrdup(c->pool, item);
2610
2611 if (ldap_is_ldap_url(item)) {
2612 int res;
2613 LDAPURLDesc *url_desc;
2614 char *url_text;
2615
2616 res = ldap_url_parse(item, &url_desc);
2617 if (res != LDAP_URL_SUCCESS) {
2618 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
2619 "invalid LDAP URL '", item, "': ", ldap_err2string(res), NULL));
2620 }
2621
2622 info->url_desc = url_desc;
2623
2624 url_text = ldap_url_desc2str(url_desc);
2625 if (url_text != NULL) {
2626 pr_log_debug(DEBUG0, "%s: parsed URL '%s' as '%s'",
2627 (char *) cmd->argv[0], item, url_text);
2628 info->url_text = url_text;
2629 }
2630
2631 #ifdef HAS_LDAP_INITIALIZE
2632 if (strcasecmp(url_desc->lud_scheme, "ldap") != 0 &&
2633 strcasecmp(url_desc->lud_scheme, "ldaps") != 0) {
2634 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid scheme specified by URL '", item, "': valid schemes are 'ldap' or 'ldaps'", NULL));
2635 }
2636 #else /* HAS_LDAP_INITIALIZE */
2637 if (strcasecmp(url_desc->lud_scheme, "ldap") != 0) {
2638 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported scheme specified by URL '", item, "': valid scheme is only 'ldap'", NULL));
2639 }
2640 #endif /* HAS_LDAP_INITIALIZE */
2641
2642 if (url_desc->lud_dn != NULL &&
2643 strcmp(url_desc->lud_dn, "") != 0) {
2644 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, item, ": a base DN may not be specified by an LDAPServer URL, only by LDAPUsers or LDAPGroups", NULL));
2645 }
2646
2647 if (url_desc->lud_filter != NULL &&
2648 strcmp(url_desc->lud_filter, "") != 0) {
2649 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, item, ": a search filter may not be specified by an LDAPServer URL, only by LDAPUsers or LDAPGroups", NULL));
2650 }
2651
2652 if (url_desc->lud_scope == LDAP_SCOPE_BASE) {
2653 pr_log_debug(DEBUG0, MOD_LDAP_VERSION
2654 ": WARNING: '%s': LDAP URL search scopes default to 'base', "
2655 "not 'subtree', and may not be what you want (see LDAPSearchScope)",
2656 item);
2657 }
2658
2659 /* Need to keep parsed host and port for pre-2000 ldap_init(3). */
2660 if (url_desc->lud_host != NULL) {
2661 info->host = pstrdup(c->pool, url_desc->lud_host);
2662 }
2663
2664 if (url_desc->lud_port != 0) {
2665 info->port = url_desc->lud_port;
2666 }
2667
2668 } else {
2669 char *ptr;
2670
2671 /* The given item is just a hostname/IP address, maybe a port. */
2672 ptr = strchr(item, ':');
2673 if (ptr == NULL) {
2674 info->host = pstrdup(c->pool, item);
2675 info->port = LDAP_PORT;
2676
2677 } else {
2678 int port;
2679
2680 info->host = pstrndup(c->pool, item, ptr - item);
2681
2682 port = atoi(ptr + 1);
2683 if (port == 0) {
2684 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to parse '", ptr + 1,
2685 "' as a port number for '", item, "'", NULL));
2686 }
2687
2688 info->port = port;
2689 info->port_text = pstrdup(c->pool, ptr + 1);
2690 }
2691 }
2692
2693 *((struct server_info **) push_array(infos)) = info;
2694 }
2695
2696 if (pr_module_exists("mod_ifsession.c")) {
2697 /* These are needed in case this directive is used with mod_ifsession
2698 * configuration.
2699 */
2700 c->flags |= CF_MULTI;
2701 }
2702
2703 return PR_HANDLED(cmd);
2704 }
2705
2706 /* usage: LDAPUseSASL mech1 ... */
2707 MODRET set_ldapusesasl(cmd_rec *cmd) {
2708 #if defined(HAVE_SASL_SASL_H)
2709 register unsigned int i;
2710 config_rec *c;
2711 char *sasl_mechs = "";
2712
2713 if (cmd->argc < 1) {
2714 CONF_ERROR(cmd, "wrong number of parameters");
2715 }
2716
2717 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2718
2719 c = add_config_param(cmd->argv[0], 1, NULL);
2720
2721 for (i = 1; i < cmd->argc; i++) {
2722 register unsigned int j;
2723 char *sasl_mech;
2724
2725 /* Only allow known supported (and tested) mechanisms for now. */
2726 if (strcasecmp(cmd->argv[i], "ANONYMOUS") == 0 ||
2727 strcasecmp(cmd->argv[i], "CRAM-MD5") == 0 ||
2728 strcasecmp(cmd->argv[i], "DIGEST-MD5") == 0 ||
2729 strcasecmp(cmd->argv[i], "PLAIN") == 0 ||
2730 strcasecmp(cmd->argv[i], "LOGIN") == 0 ||
2731 strncasecmp(cmd->argv[i], "SCRAM-SHA-", 10) == 0) {
2732 sasl_mech = cmd->argv[i];
2733
2734 } else {
2735 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported SASL mechanism: ",
2736 cmd->argv[i], NULL));
2737 }
2738
2739 /* Convert the mechanism name to all uppercase, per convention. */
2740 for (j = 0; j < strlen(sasl_mech); j++) {
2741 sasl_mech[j] = toupper(sasl_mech[j]);
2742 }
2743
2744 sasl_mechs = pstrcat(c->pool, sasl_mechs, *sasl_mechs ? " " : "",
2745 sasl_mech, NULL);
2746 }
2747
2748 c->argv[0] = sasl_mechs;
2749 return PR_HANDLED(cmd);
2750 #else
2751 CONF_ERROR(cmd, "Your system libraries do not appear to support SASL (missing <sasl/sasl.h>)");
2752 #endif /* HAVE_SASL_SASL_H */
2753 }
2754
2755 /* usage: LDAPUseTLS on|off */
2756 MODRET set_ldapusetls(cmd_rec *cmd) {
2757 #if defined(LDAP_OPT_X_TLS)
2758 int use_tls;
2759 config_rec *c;
2760
2761 CHECK_ARGS(cmd, 1);
2762 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2763
2764 use_tls = get_boolean(cmd, 1);
2765 if (use_tls == -1) {
2766 CONF_ERROR(cmd, "expected Boolean parameter");
2767 }
2768
2769 c = add_config_param(cmd->argv[0], 1, NULL);
2770 c->argv[0] = pcalloc(c->pool, sizeof(int));
2771 *((int *) c->argv[0]) = use_tls;
2772
2773 return PR_HANDLED(cmd);
2774
2775 #else /* LDAP_OPT_X_TLS */
2776 CONF_ERROR(cmd, "Your LDAP libraries do not appear to support TLS");
2777 #endif /* LDAP_OPT_X_TLS */
2778 }
2779
2780 MODRET set_ldapbinddn(cmd_rec *cmd) {
2781 CHECK_ARGS(cmd, 1);
2782 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2783
2784 add_config_param_str(cmd->argv[0], 2, cmd->argv[1], cmd->argv[2]);
2785 return PR_HANDLED(cmd);
2786 }
2787
2788 MODRET set_ldapsearchscope(cmd_rec *cmd) {
2789 config_rec *c;
2790 const char *scope_name;
2791 int search_scope;
2792
2793 CHECK_ARGS(cmd, 1);
2794 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2795
2796 scope_name = cmd->argv[1];
2797
2798 if (strcasecmp(scope_name, "base") == 0) {
2799 search_scope = LDAP_SCOPE_BASE;
2800
2801 } else if (strcasecmp(scope_name, "one") == 0 ||
2802 strcasecmp(scope_name, "onelevel") == 0) {
2803 search_scope = LDAP_SCOPE_ONELEVEL;
2804
2805 } else if (strcasecmp(scope_name, "sub") == 0 ||
2806 strcasecmp(scope_name, "subtree") == 0) {
2807 search_scope = LDAP_SCOPE_SUBTREE;
2808
2809 } else {
2810 CONF_ERROR(cmd, "search scope must be one of: base, onelevel, subtree");
2811 }
2812
2813 c = add_config_param(cmd->argv[0], 1, NULL);
2814 c->argv[0] = palloc(c->pool, sizeof(int));
2815 *((int *) c->argv[0]) = search_scope;
2816
2817 return PR_HANDLED(cmd);
2818 }
2819
2820 MODRET set_ldapconnecttimeout(cmd_rec *cmd) {
2821 config_rec *c;
2822 int timeout;
2823
2824 CHECK_ARGS(cmd, 1);
2825 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2826
2827 if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
2828 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
2829 cmd->argv[1], "': ", strerror(errno), NULL));
2830 }
2831
2832 c = add_config_param(cmd->argv[0], 1, NULL);
2833 c->argv[0] = pcalloc(c->pool, sizeof(int));
2834 *((int *) c->argv[0]) = timeout;
2835
2836 return PR_HANDLED(cmd);
2837 }
2838
2839 MODRET set_ldapquerytimeout(cmd_rec *cmd) {
2840 config_rec *c;
2841 int timeout;
2842
2843 CHECK_ARGS(cmd, 1);
2844 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2845
2846 if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
2847 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
2848 cmd->argv[1], "': ", strerror(errno), NULL));
2849 }
2850
2851 c = add_config_param(cmd->argv[0], 1, NULL);
2852 c->argv[0] = pcalloc(c->pool, sizeof(int));
2853 *((int *) c->argv[0]) = timeout;
2854
2855 return PR_HANDLED(cmd);
2856 }
2857
2858 MODRET set_ldapaliasdereference(cmd_rec *cmd) {
2859 int value;
2860 config_rec *c;
2861
2862 CHECK_ARGS(cmd, 1);
2863 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2864
2865 if (strcasecmp(cmd->argv[1], "never") == 0) {
2866 value = LDAP_DEREF_NEVER;
2867
2868 } else if (strcasecmp(cmd->argv[1], "search") == 0) {
2869 value = LDAP_DEREF_SEARCHING;
2870
2871 } else if (strcasecmp(cmd->argv[1], "find") == 0) {
2872 value = LDAP_DEREF_FINDING;
2873
2874 } else if (strcasecmp(cmd->argv[1], "always") == 0) {
2875 value = LDAP_DEREF_ALWAYS;
2876
2877 } else {
2878 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
2879 "expected a valid dereference (never, search, find, always): ",
2880 cmd->argv[1], NULL));
2881 }
2882
2883 c = add_config_param(cmd->argv[0], 1, NULL);
2884 c->argv[0] = pcalloc(c->pool, sizeof(int));
2885 *((int *) c->argv[0]) = value;
2886
2887 return PR_HANDLED(cmd);
2888 }
2889
2890 MODRET set_ldapauthbinds(cmd_rec *cmd) {
2891 int b;
2892 config_rec *c;
2893
2894 CHECK_ARGS(cmd, 1);
2895 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2896
2897 b = get_boolean(cmd, 1);
2898 if (b == -1) {
2899 CONF_ERROR(cmd, "expected Boolean parameter");
2900 }
2901
2902 c = add_config_param(cmd->argv[0], 1, NULL);
2903 c->argv[0] = pcalloc(c->pool, sizeof(int));
2904 *((int *) c->argv[0]) = b;
2905
2906 return PR_HANDLED(cmd);
2907 }
2908
2909 MODRET set_ldapdefaultauthscheme(cmd_rec *cmd) {
2910 CHECK_ARGS(cmd, 1);
2911 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2912
2913 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
2914 return PR_HANDLED(cmd);
2915 }
2916
2917 MODRET set_ldapattr(cmd_rec *cmd) {
2918 config_rec *c;
2919
2920 CHECK_ARGS(cmd, 2);
2921 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2922
2923 if (strcasecmp(cmd->argv[1], "uid") != 0 &&
2924 strcasecmp(cmd->argv[1], "uidNumber") != 0 &&
2925 strcasecmp(cmd->argv[1], "gidNumber") != 0 &&
2926 strcasecmp(cmd->argv[1], "homeDirectory") != 0 &&
2927 strcasecmp(cmd->argv[1], "userPassword") != 0 &&
2928 strcasecmp(cmd->argv[1], "loginShell") != 0 &&
2929 strcasecmp(cmd->argv[1], "cn") != 0 &&
2930 strcasecmp(cmd->argv[1], "memberUid") != 0 &&
2931 strcasecmp(cmd->argv[1], "ftpQuota") != 0 &&
2932 strcasecmp(cmd->argv[1], "ftpQuotaProfileDN") != 0) {
2933 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
2934 ": unknown attribute name: ", cmd->argv[1], NULL));
2935 }
2936
2937 c = add_config_param_str(cmd->argv[0], 2, cmd->argv[1], cmd->argv[2]);
2938
2939 if (pr_module_exists("mod_ifsession.c")) {
2940 /* These are needed in case this directive is used with mod_ifsession
2941 * configuration.
2942 */
2943 c->flags |= CF_MULTI;
2944 }
2945
2946 return PR_HANDLED(cmd);
2947 }
2948
2949 /* usage: LDAPUsers base-dn [name-filter-template [uid-filter-template]] */
2950 MODRET set_ldapusers(cmd_rec *cmd) {
2951 config_rec *c;
2952
2953 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2954
2955 if (get_boolean(cmd, 1) != -1) {
2956 CONF_ERROR(cmd, "first parameter must be the base DN, not on/off");
2957 }
2958
2959 c = add_config_param(cmd->argv[0], cmd->argc - 1, NULL, NULL, NULL);
2960 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
2961
2962 if (cmd->argc > 2) {
2963 const char *filter;
2964
2965 filter = cmd->argv[2];
2966 if (strcmp(filter, "") != 0) {
2967 c->argv[1] = pstrdup(c->pool, filter);
2968 }
2969 }
2970
2971 if (cmd->argc > 3) {
2972 const char *filter;
2973
2974 filter = cmd->argv[3];
2975 if (strcmp(filter, "") != 0) {
2976 c->argv[2] = pstrdup(c->pool, filter);
2977 }
2978 }
2979
2980 return PR_HANDLED(cmd);
2981 }
2982
2983 MODRET set_ldapdefaultuid(cmd_rec *cmd) {
2984 config_rec *c;
2985 uid_t uid;
2986
2987 CHECK_ARGS(cmd, 1);
2988 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2989
2990 c = add_config_param(cmd->argv[0], 1, NULL);
2991 c->argv[0] = pcalloc(c->pool, sizeof(uid_t));
2992
2993 if (strcasecmp(cmd->argv[1], "auto") == 0) {
2994 uid = PR_LDAP_AUTO_DEFAULT_ID;
2995
2996 } else {
2997 if (pr_str2uid(cmd->argv[1], &uid) < 0) {
2998 CONF_ERROR(cmd, "LDAPDefaultUID: UID argument must be numeric");
2999 }
3000 }
3001
3002 *((uid_t *) c->argv[0]) = uid;
3003 return PR_HANDLED(cmd);
3004 }
3005
3006 MODRET set_ldapdefaultgid(cmd_rec *cmd) {
3007 config_rec *c;
3008 gid_t gid;
3009
3010 CHECK_ARGS(cmd, 1);
3011 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3012
3013 c = add_config_param(cmd->argv[0], 1, NULL);
3014 c->argv[0] = pcalloc(c->pool, sizeof(gid_t));
3015
3016 if (strcasecmp(cmd->argv[1], "auto") == 0) {
3017 gid = PR_LDAP_AUTO_DEFAULT_ID;
3018
3019 } else {
3020 if (pr_str2gid(cmd->argv[1], &gid) < 0) {
3021 CONF_ERROR(cmd, "LDAPDefaultGID: GID argument must be numeric");
3022 }
3023 }
3024
3025 *((gid_t *) c->argv[0]) = gid;
3026 return PR_HANDLED(cmd);
3027 }
3028
3029 MODRET set_ldapforcedefaultuid(cmd_rec *cmd) {
3030 int b;
3031 config_rec *c;
3032
3033 CHECK_CONF(cmd,CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3034
3035 b = get_boolean(cmd, 1);
3036 if (b == -1) {
3037 CONF_ERROR(cmd, "expected Boolean parameter");
3038 }
3039
3040 c = add_config_param(cmd->argv[0], 1, NULL);
3041 c->argv[0] = pcalloc(c->pool, sizeof(int));
3042 *((int *) c->argv[0]) = b;
3043
3044 return PR_HANDLED(cmd);
3045 }
3046
3047 MODRET set_ldapforcedefaultgid(cmd_rec *cmd) {
3048 int b;
3049 config_rec *c;
3050
3051 CHECK_CONF(cmd,CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3052
3053 b = get_boolean(cmd, 1);
3054 if (b == -1) {
3055 CONF_ERROR(cmd, "expected Boolean parameter");
3056 }
3057
3058 c = add_config_param(cmd->argv[0], 1, NULL);
3059 c->argv[0] = pcalloc(c->pool, sizeof(int));
3060 *((int *) c->argv[0]) = b;
3061
3062 return PR_HANDLED(cmd);
3063 }
3064
3065 MODRET set_ldapgenhdir(cmd_rec *cmd) {
3066 int b;
3067 config_rec *c;
3068
3069 CHECK_ARGS(cmd, 1);
3070 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3071
3072 b = get_boolean(cmd, 1);
3073 if (b == -1) {
3074 CONF_ERROR(cmd, "expected Boolean parameter");
3075 }
3076
3077 c = add_config_param(cmd->argv[0], 1, NULL);
3078 c->argv[0] = pcalloc(c->pool, sizeof(int));
3079 *((int *) c->argv[0]) = b;
3080
3081 return PR_HANDLED(cmd);
3082 }
3083
3084 MODRET set_ldapgenhdirprefix(cmd_rec *cmd) {
3085 char *prefix;
3086
3087 CHECK_ARGS(cmd, 1);
3088 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3089
3090 prefix = cmd->argv[1];
3091 if (strlen(prefix) == 0) {
3092 CONF_ERROR(cmd, "must not be an empty string");
3093 }
3094
3095 add_config_param_str(cmd->argv[0], 1, prefix);
3096 return PR_HANDLED(cmd);
3097 }
3098
3099 MODRET set_ldapgenhdirprefixnouname(cmd_rec *cmd) {
3100 int b;
3101 config_rec *c;
3102
3103 CHECK_ARGS(cmd, 1);
3104 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3105
3106 b = get_boolean(cmd, 1);
3107 if (b == -1) {
3108 CONF_ERROR(cmd, "expected Boolean parameter");
3109 }
3110
3111 c = add_config_param(cmd->argv[0], 1, NULL);
3112 c->argv[0] = pcalloc(c->pool, sizeof(int));
3113 *((int *) c->argv[0]) = b;
3114
3115 return PR_HANDLED(cmd);
3116 }
3117
3118 MODRET set_ldapforcegenhdir(cmd_rec *cmd) {
3119 int b;
3120 config_rec *c;
3121
3122 CHECK_ARGS(cmd, 1);
3123 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3124
3125 b = get_boolean(cmd, 1);
3126 if (b == -1) {
3127 CONF_ERROR(cmd, "expected Boolean parameter");
3128 }
3129
3130 c = add_config_param(cmd->argv[0], 1, NULL);
3131 c->argv[0] = pcalloc(c->pool, sizeof(int));
3132 *((int *) c->argv[0]) = b;
3133
3134 return PR_HANDLED(cmd);
3135 }
3136
3137 /* usage: LDAPGroups base-dn [name-filter-template [uid-filter-template [member-filter-template]]] */
3138 MODRET set_ldapgroups(cmd_rec *cmd) {
3139 config_rec *c;
3140
3141 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3142
3143 if (get_boolean(cmd, 1) != -1) {
3144 CONF_ERROR(cmd, "first parameter must be the base DN, not on/off.");
3145 }
3146
3147 c = add_config_param(cmd->argv[0], cmd->argc - 1, NULL);
3148 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
3149
3150 if (cmd->argc > 2) {
3151 const char *filter;
3152
3153 filter = cmd->argv[2];
3154 if (strcmp(filter, "") != 0) {
3155 c->argv[1] = pstrdup(c->pool, filter);
3156
3157 } else {
3158 c->argv[1] = NULL;
3159 }
3160 }
3161
3162 if (cmd->argc > 3) {
3163 const char *filter;
3164
3165 filter = cmd->argv[3];
3166 if (strcmp(filter, "") != 0) {
3167 c->argv[2] = pstrdup(c->pool, filter);
3168
3169 } else {
3170 c->argv[2] = NULL;
3171 }
3172 }
3173
3174 if (cmd->argc > 4) {
3175 const char *filter;
3176
3177 filter = cmd->argv[4];
3178 if (strcmp(filter, "") != 0) {
3179 c->argv[3] = pstrdup(c->pool, filter);
3180
3181 } else {
3182 c->argv[3] = NULL;
3183 }
3184 }
3185
3186 return PR_HANDLED(cmd);
3187 }
3188
3189 MODRET set_ldapdefaultquota(cmd_rec *cmd) {
3190 CHECK_ARGS(cmd, 1);
3191 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3192
3193 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
3194 return PR_HANDLED(cmd);
3195 }
3196
3197 /* Event listeners
3198 */
3199
3200 #if defined(PR_SHARED_MODULE)
3201 static void ldap_mod_unload_ev(const void *event_data, void *user_data) {
3202 if (strcmp("mod_ldap.c", (const char *) event_data) != 0) {
3203 return;
3204 }
3205
3206 pr_event_unregister(&ldap_module, NULL, NULL);
3207 server_infos_free();
3208 }
3209 #endif /* PR_SHARED_MODULE */
3210
3211 static void ldap_postparse_ev(const void *event_data, void *user_data) {
3212 server_rec *s = NULL;
3213
3214 /* Check the configured LDAPServer directives. Specifically, if the URL
3215 * syntax has been used, we look for any LDAPSearchScope directive and
3216 * warn that they are ignored. Similarly, if there are "ldaps" URLs,
3217 * we warn that any LDAPUseTLS directive will be ignored. Otherwise,
3218 * cases where LDAP URLs have not been used, we construct them.
3219 */
3220 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
3221 register unsigned int i;
3222 config_rec *c;
3223 array_header *infos;
3224 int search_scope = -1, use_tls = -1;
3225
3226 pr_signals_handle();
3227
3228 c = find_config(s->conf, CONF_PARAM, "LDAPSearchScope", FALSE);
3229 if (c != NULL) {
3230 search_scope = *((int *) c->argv[0]);
3231
3232 } else {
3233 /* Per documentation, the default search scope is "subtree". */
3234 search_scope = LDAP_SCOPE_SUBTREE;
3235 }
3236
3237 #if defined(LDAP_OPT_X_TLS)
3238 c = find_config(s->conf, CONF_PARAM, "LDAPUseTLS", FALSE);
3239 if (c != NULL) {
3240 use_tls = *((int *) c->argv[0]);
3241 }
3242 #endif /* LDAP_OPT_X_TLS */
3243
3244 c = find_config(s->conf, CONF_PARAM, "LDAPServer", FALSE);
3245 if (c == NULL) {
3246 continue;
3247 }
3248
3249 infos = c->argv[0];
3250
3251 for (i = 0; i < infos->nelts; i++) {
3252 struct server_info *info;
3253
3254 info = ((struct server_info **) infos->elts)[i];
3255
3256 if (info->url_desc != NULL) {
3257 if (info->url_desc->lud_scope != search_scope) {
3258 /* When LDAP URLs are used, the search scope is part of the URL
3259 * syntax, which takes precedence over LDAPSearchScope.
3260 */
3261 pr_log_debug(DEBUG2, MOD_LDAP_VERSION
3262 ": ignoring configured LDAPSearchScope for LDAP URL '%s'",
3263 info->info_text);
3264 }
3265
3266 /* If the scheme is "ldaps", AND LDAPUseTLS has been explicitly
3267 * configured, warn that it will be ignored.
3268 */
3269 if (use_tls != -1) {
3270 if (strcasecmp(info->url_desc->lud_scheme, "ldaps") == 0) {
3271 pr_log_debug(DEBUG2, MOD_LDAP_VERSION
3272 ": ignoring configured LDAPUseTLS for explicit LDAPS URL '%s'",
3273 info->info_text);
3274 } else {
3275 info->use_starttls = use_tls;
3276 }
3277 }
3278
3279 } else {
3280 int res;
3281 char *url, *url_text;
3282 LDAPURLDesc *url_desc;
3283
3284 /* Construct an LDAP URL, and parse it. */
3285
3286 url = pstrcat(c->pool, "ldap://", info->host, NULL);
3287 if (info->port_text != NULL) {
3288 url = pstrcat(c->pool, url, ":", info->port_text, NULL);
3289 }
3290
3291 switch (search_scope) {
3292 case LDAP_SCOPE_SUBTREE:
3293 url = pstrcat(c->pool, url, "/??sub", NULL);
3294 break;
3295
3296 case LDAP_SCOPE_BASE:
3297 url = pstrcat(c->pool, url, "/??base", NULL);
3298 break;
3299
3300 case LDAP_SCOPE_ONELEVEL:
3301 url = pstrcat(c->pool, url, "/??one", NULL);
3302 break;
3303
3304 default:
3305 break;
3306 }
3307
3308 res = ldap_url_parse(url, &url_desc);
3309 if (res != LDAP_URL_SUCCESS) {
3310 /* This should only happen as a result of a coding bug. */
3311 pr_log_pri(PR_LOG_NOTICE, MOD_LDAP_VERSION
3312 ": invalid LDAP URL '%s': %s", url, ldap_err2string(res));
3313 pr_session_disconnect(&ldap_module, PR_SESS_DISCONNECT_BAD_CONFIG,
3314 NULL);
3315 }
3316
3317 info->url_desc = url_desc;
3318 info->port = url_desc->lud_port;
3319
3320 url_text = ldap_url_desc2str(url_desc);
3321 if (url_text != NULL) {
3322 pr_log_debug(DEBUG0, "%s: parsed URL '%s' as '%s'", c->name, url,
3323 url_text);
3324 info->url_text = url_text;
3325 }
3326
3327 if (use_tls != -1) {
3328 info->use_starttls = use_tls;
3329 }
3330 }
3331
3332 server_info_get_ssl_defaults(info);
3333 }
3334 }
3335 }
3336
3337 static void ldap_sess_reinit_ev(const void *event_data, void *user_data) {
3338 int res;
3339
3340 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
3341
3342 pr_event_unregister(&ldap_module, "core.session-reinit", ldap_sess_reinit_ev);
3343
3344 /* Restore defaults. */
3345 (void) close(ldap_logfd);
3346 ldap_logfd = -1;
3347 ldap_protocol_version = 3;
3348 ldap_servers = NULL;
3349 ldap_dn = NULL;
3350 ldap_dnpass = NULL;
3351 ldap_dnpasslen = 0;
3352 ldap_search_scope = LDAP_SCOPE_SUBTREE;
3353 ldap_sasl_mechs = NULL;
3354 ldap_connecttimeout = 0;
3355 ldap_querytimeout = 0;
3356 ldap_dereference = LDAP_DEREF_NEVER;
3357 ldap_authbinds = TRUE;
3358 ldap_defaultauthscheme = "crypt";
3359 ldap_attr_uid = "uid";
3360 ldap_attr_uidnumber = "uidNumber";
3361 ldap_attr_gidnumber = "gidNumber";
3362 ldap_attr_homedirectory = "homeDirectory";
3363 ldap_attr_userpassword = "userPassword";
3364 ldap_attr_loginshell = "loginShell";
3365 ldap_attr_cn = "cn";
3366 ldap_attr_memberuid = "memberUid";
3367 ldap_attr_ftpquota = "ftpQuota";
3368 ldap_attr_ftpquota_profiledn = "ftpQuotaProfileDN";
3369 ldap_do_users = FALSE;
3370 ldap_user_basedn = NULL;
3371 ldap_user_name_filter = NULL;
3372 ldap_user_uid_filter = NULL;
3373 ldap_do_groups = FALSE;
3374 ldap_group_name_filter = NULL;
3375 ldap_group_gid_filter = NULL;
3376 ldap_group_member_filter = NULL;
3377 ldap_default_quota = NULL;
3378 ldap_defaultuid = (uid_t) -1;
3379 ldap_defaultgid = (gid_t) -1;
3380 ldap_forcedefaultuid = FALSE;
3381 ldap_forcedefaultgid = FALSE;
3382 ldap_forcegenhdir = FALSE;
3383 ldap_genhdir = FALSE;
3384 ldap_genhdir_prefix = NULL;
3385 ldap_genhdir_prefix_nouname = FALSE;
3386
3387 curr_server_info = NULL;
3388 curr_server_index = 0;
3389
3390 destroy_pool(ldap_pool);
3391 ldap_pool = NULL;
3392
3393 res = ldap_sess_init();
3394 if (res < 0) {
3395 pr_session_disconnect(&ldap_module, PR_SESS_DISCONNECT_SESSION_INIT_FAILED,
3396 NULL);
3397 }
3398 }
3399
3400 static void ldap_shutdown_ev(const void *event_data, void *user_data) {
3401 server_infos_free();
3402 }
3403
3404 /* Initialization routines
3405 */
3406
3407 static int ldap_mod_init(void) {
3408 pr_log_debug(DEBUG2, MOD_LDAP_VERSION
3409 ": compiled using LDAP vendor '%s', LDAP API version %lu",
3410 LDAP_VENDOR_NAME, (unsigned long) LDAP_API_VERSION);
3411
3412 #if defined(LDAP_OPT_API_INFO)
3413 {
3414 int res;
3415 LDAPAPIInfo api_info;
3416
3417 api_info.ldapai_info_version = LDAP_API_INFO_VERSION;
3418 res = ldap_get_option(NULL, LDAP_OPT_API_INFO, &api_info);
3419 if (res == LDAP_OPT_SUCCESS) {
3420 pool *tmp_pool;
3421 char *feats = "";
3422
3423 tmp_pool = make_sub_pool(permanent_pool);
3424
3425 if (api_info.ldapai_extensions != NULL) {
3426 register unsigned int i;
3427
3428 for (i = 0; api_info.ldapai_extensions[i]; i++) {
3429 feats = pstrcat(tmp_pool, feats, i != 0 ? ", " : "",
3430 api_info.ldapai_extensions[i], NULL);
3431 ldap_memfree(api_info.ldapai_extensions[i]);
3432 }
3433
3434 ldap_memfree(api_info.ldapai_extensions);
3435 }
3436
3437 pr_log_debug(DEBUG10, MOD_LDAP_VERSION
3438 " linked with LDAP vendor '%s' (LDAP API version %d, "
3439 "vendor version %d), features: %s", api_info.ldapai_vendor_name,
3440 api_info.ldapai_api_version, api_info.ldapai_vendor_version,
3441 feats);
3442 ldap_memfree(api_info.ldapai_vendor_name);
3443 destroy_pool(tmp_pool);
3444
3445 } else {
3446 pr_trace_msg(trace_channel, 3,
3447 "error retrieving LDAP_OPT_API_INFO: %s", ldap_err2string(res));
3448 }
3449 }
3450 #endif /* LDAP_OPT_API_INFO */
3451
3452 #if defined(LDAP_OPT_X_TLS_PACKAGE)
3453 {
3454 int res;
3455 char *tls_package = NULL;
3456
3457 res = ldap_get_option(NULL, LDAP_OPT_X_TLS_PACKAGE, &tls_package);
3458 if (res == LDAP_OPT_SUCCESS) {
3459 pr_log_debug(DEBUG10, MOD_LDAP_VERSION
3460 ": LDAP TLS package = %s", tls_package);
3461
3462 } else {
3463 pr_trace_msg(trace_channel, 3,
3464 "error retrieving LDAP_OPT_X_TLS_PACKAGE: %s", ldap_err2string(res));
3465 }
3466 }
3467 #endif /* LDAP_OPT_X_TLS_PACKAGE */
3468
3469 #if defined(PR_SHARED_MODULE)
3470 pr_event_register(&ldap_module, "core.module-unload", ldap_mod_unload_ev,
3471 NULL);
3472 #endif /* PR_SHARED_MODULE */
3473 pr_event_register(&ldap_module, "core.postparse", ldap_postparse_ev, NULL);
3474 pr_event_register(&ldap_module, "core.shutdown", ldap_shutdown_ev, NULL);
3475
3476 return 0;
3477 }
3478
3479 static int ldap_sess_init(void) {
3480 config_rec *c;
3481 void *ptr;
3482
3483 pr_event_register(&ldap_module, "core.session-reinit", ldap_sess_reinit_ev,
3484 NULL);
3485
3486 ldap_pool = make_sub_pool(session.pool);
3487 pr_pool_tag(ldap_pool, MOD_LDAP_VERSION);
3488
3489 c = find_config(main_server->conf, CONF_PARAM, "LDAPLog", FALSE);
3490 if (c != NULL) {
3491 char *path;
3492
3493 path = c->argv[0];
3494
3495 if (strncasecmp(path, "none", 5) != 0) {
3496 int res, xerrno = 0;
3497
3498 pr_signals_block();
3499 PRIVS_ROOT
3500 res = pr_log_openfile(path, &ldap_logfd, PR_LOG_SYSTEM_MODE);
3501 xerrno = errno;
3502 PRIVS_RELINQUISH
3503 pr_signals_unblock();
3504
3505 if (res < 0) {
3506 if (res == -1) {
3507 pr_log_pri(PR_LOG_NOTICE, MOD_LDAP_VERSION
3508 ": notice: unable to open LDAPLog '%s': %s", path,
3509 strerror(xerrno));
3510
3511 } else if (res == PR_LOG_WRITABLE_DIR) {
3512 pr_log_pri(PR_LOG_WARNING, MOD_LDAP_VERSION
3513 ": notice: unable to open LDAPPLog '%s': parent directory is "
3514 "world-writable", path);
3515
3516 } else if (res == PR_LOG_SYMLINK) {
3517 pr_log_pri(PR_LOG_WARNING, MOD_LDAP_VERSION
3518 ": notice: unable to open LDAPLog '%s': cannot log to a symlink",
3519 path);
3520 }
3521 }
3522 }
3523 }
3524
3525 ptr = get_param_ptr(main_server->conf, "LDAPProtocolVersion", FALSE);
3526 if (ptr != NULL) {
3527 ldap_protocol_version = *((int *) ptr);
3528 }
3529
3530 /* Allow for multiple LDAPServer directives. */
3531 c = find_config(main_server->conf, CONF_PARAM, "LDAPServer", FALSE);
3532 while (c != NULL) {
3533 pr_signals_handle();
3534
3535 if (ldap_servers != NULL) {
3536 array_cat(ldap_servers, c->argv[0]);
3537
3538 } else {
3539 ldap_servers = c->argv[0];
3540 }
3541
3542 c = find_config_next(c, c->next, CONF_PARAM, "LDAPServer", FALSE);
3543 }
3544
3545 if (ldap_servers == NULL) {
3546 pr_log_pri(PR_LOG_NOTICE, MOD_LDAP_VERSION
3547 ": no LDAPServer configured, using LDAP library defaults");
3548 }
3549
3550 c = find_config(main_server->conf, CONF_PARAM, "LDAPBindDN", FALSE);
3551 if (c != NULL) {
3552 ldap_dn = pstrdup(ldap_pool, c->argv[0]);
3553 ldap_dnpass = pstrdup(ldap_pool, c->argv[1]);
3554 ldap_dnpasslen = strlen(ldap_dnpass);
3555 }
3556
3557 c = find_config(main_server->conf, CONF_PARAM, "LDAPSearchScope", FALSE);
3558 if (c != NULL) {
3559 ldap_search_scope = *((int *) c->argv[0]);
3560 }
3561
3562 ptr = get_param_ptr(main_server->conf, "LDAPConnectTimeout", FALSE);
3563 if (ptr != NULL) {
3564 ldap_connecttimeout = *((int *) ptr);
3565
3566 if (ldap_connecttimeout > 0) {
3567 ldap_connecttimeout_tv.tv_sec = ldap_connecttimeout;
3568 }
3569 }
3570
3571 ptr = get_param_ptr(main_server->conf, "LDAPQueryTimeout", FALSE);
3572 if (ptr != NULL) {
3573 ldap_querytimeout = *((int *) ptr);
3574 }
3575
3576 ptr = get_param_ptr(main_server->conf, "LDAPAliasDereference", FALSE);
3577 if (ptr != NULL) {
3578 ldap_dereference = *((int *) ptr);
3579 }
3580
3581 ptr = get_param_ptr(main_server->conf, "LDAPAuthBinds", FALSE);
3582 if (ptr != NULL) {
3583 ldap_authbinds = *((int *) ptr);
3584 }
3585
3586 ptr = get_param_ptr(main_server->conf, "LDAPDefaultAuthScheme", FALSE);
3587 if (ptr != NULL) {
3588 ldap_defaultauthscheme = (char *) ptr;
3589 }
3590
3591 /* Look up any attr redefinitions (LDAPAttr) before using those
3592 * variables, such as when generating the default search filters.
3593 */
3594 c = find_config(main_server->conf, CONF_PARAM, "LDAPAttr", FALSE);
3595 if (c != NULL) {
3596 do {
3597 if (strcasecmp(c->argv[0], "uid") == 0) {
3598 ldap_attr_uid = pstrdup(ldap_pool, c->argv[1]);
3599
3600 } else if (strcasecmp(c->argv[0], "uidNumber") == 0) {
3601 ldap_attr_uidnumber = pstrdup(ldap_pool, c->argv[1]);
3602
3603 } else if (strcasecmp(c->argv[0], "gidNumber") == 0) {
3604 ldap_attr_gidnumber = pstrdup(ldap_pool, c->argv[1]);
3605
3606 } else if (strcasecmp(c->argv[0], "homeDirectory") == 0) {
3607 ldap_attr_homedirectory = pstrdup(ldap_pool, c->argv[1]);
3608
3609 } else if (strcasecmp(c->argv[0], "userPassword") == 0) {
3610 ldap_attr_userpassword = pstrdup(ldap_pool, c->argv[1]);
3611
3612 } else if (strcasecmp(c->argv[0], "loginShell") == 0) {
3613 ldap_attr_loginshell = pstrdup(ldap_pool, c->argv[1]);
3614
3615 } else if (strcasecmp(c->argv[0], "cn") == 0) {
3616 ldap_attr_cn = pstrdup(ldap_pool, c->argv[1]);
3617
3618 } else if (strcasecmp(c->argv[0], "memberUid") == 0) {
3619 ldap_attr_memberuid = pstrdup(ldap_pool, c->argv[1]);
3620
3621 } else if (strcasecmp(c->argv[0], "ftpQuota") == 0) {
3622 ldap_attr_ftpquota = pstrdup(ldap_pool, c->argv[1]);
3623
3624 } else if (strcasecmp(c->argv[0], "ftpQuotaProfileDN") == 0) {
3625 ldap_attr_ftpquota_profiledn = pstrdup(ldap_pool, c->argv[1]);
3626 }
3627
3628 } while ((c = find_config_next(c, c->next, CONF_PARAM, "LDAPAttr", FALSE)));
3629 }
3630
3631 c = find_config(main_server->conf, CONF_PARAM, "LDAPUsers", FALSE);
3632 if (c != NULL) {
3633 const char *filter;
3634
3635 ldap_do_users = TRUE;
3636 ldap_user_basedn = pstrdup(ldap_pool, c->argv[0]);
3637
3638 filter = NULL;
3639 if (c->argc > 1) {
3640 filter = c->argv[1];
3641 }
3642
3643 if (filter != NULL &&
3644 strcmp(filter, "") != 0) {
3645 ldap_user_name_filter = pstrdup(ldap_pool, filter);
3646
3647 } else {
3648 ldap_user_name_filter = pstrcat(ldap_pool,
3649 "(&(", ldap_attr_uid, "=%v)(objectclass=posixAccount))", NULL);
3650 }
3651
3652 filter = NULL;
3653 if (c->argc > 2) {
3654 filter = c->argv[2];
3655 }
3656
3657 if (filter != NULL &&
3658 strcmp(filter, "") != 0) {
3659 ldap_user_uid_filter = pstrdup(ldap_pool, filter);
3660
3661 } else {
3662 ldap_user_uid_filter = pstrcat(ldap_pool,
3663 "(&(", ldap_attr_uidnumber, "=%v)(objectclass=posixAccount))", NULL);
3664 }
3665 }
3666
3667 ptr = get_param_ptr(main_server->conf, "LDAPDefaultUID", FALSE);
3668 if (ptr != NULL) {
3669 ldap_defaultuid = *((uid_t *) ptr);
3670 }
3671
3672 ptr = get_param_ptr(main_server->conf, "LDAPDefaultGID", FALSE);
3673 if (ptr != NULL) {
3674 ldap_defaultgid = *((gid_t *) ptr);
3675 }
3676
3677 ldap_default_quota = get_param_ptr(main_server->conf, "LDAPDefaultQuota",
3678 FALSE);
3679
3680 ptr = get_param_ptr(main_server->conf, "LDAPForceDefaultUID", FALSE);
3681 if (ptr != NULL) {
3682 ldap_forcedefaultuid = *((int *) ptr);
3683 }
3684
3685 ptr = get_param_ptr(main_server->conf, "LDAPForceDefaultGID", FALSE);
3686 if (ptr != NULL) {
3687 ldap_forcedefaultgid = *((int *) ptr);
3688 }
3689
3690 ptr = get_param_ptr(main_server->conf, "LDAPForceGeneratedHomedir", FALSE);
3691 if (ptr != NULL) {
3692 ldap_forcegenhdir = *((int *) ptr);
3693 }
3694
3695 ptr = get_param_ptr(main_server->conf, "LDAPGenerateHomedir", FALSE);
3696 if (ptr != NULL) {
3697 ldap_genhdir = *((int *) ptr);
3698 }
3699
3700 ldap_genhdir_prefix = get_param_ptr(main_server->conf,
3701 "LDAPGenerateHomedirPrefix", FALSE);
3702
3703 ptr = get_param_ptr(main_server->conf, "LDAPGenerateHomedirPrefixNoUsername",
3704 FALSE);
3705 if (ptr != NULL) {
3706 ldap_genhdir_prefix_nouname = *((int *) ptr);
3707 }
3708
3709 c = find_config(main_server->conf, CONF_PARAM, "LDAPGroups", FALSE);
3710 if (c != NULL) {
3711 const char *filter;
3712
3713 ldap_do_groups = TRUE;
3714 ldap_gid_basedn = pstrdup(ldap_pool, c->argv[0]);
3715
3716 filter = NULL;
3717 if (c->argc > 1) {
3718 filter = c->argv[1];
3719 }
3720
3721 if (filter != NULL &&
3722 strcmp(filter, "") != 0) {
3723 ldap_group_name_filter = pstrdup(ldap_pool, filter);
3724
3725 } else {
3726 ldap_group_name_filter = pstrcat(ldap_pool,
3727 "(&(", ldap_attr_cn, "=%v)(objectclass=posixGroup))", NULL);
3728 }
3729
3730 filter = NULL;
3731 if (c->argc > 2) {
3732 filter = c->argv[2];
3733 }
3734
3735 if (filter != NULL &&
3736 strcmp(filter, "") != 0) {
3737 ldap_group_gid_filter = pstrdup(ldap_pool, filter);
3738
3739 } else {
3740 ldap_group_gid_filter = pstrcat(ldap_pool,
3741 "(&(", ldap_attr_gidnumber, "=%v)(objectclass=posixGroup))", NULL);
3742 }
3743
3744 filter = NULL;
3745 if (c->argc > 3) {
3746 filter = c->argv[3];
3747 }
3748
3749 if (filter != NULL &&
3750 strcmp(filter, "") != 0) {
3751 ldap_group_member_filter = pstrdup(ldap_pool, filter);
3752
3753 } else {
3754 ldap_group_member_filter = pstrcat(ldap_pool,
3755 "(&(", ldap_attr_memberuid, "=%v)(objectclass=posixGroup))", NULL);
3756 }
3757 }
3758
3759 c = find_config(main_server->conf, CONF_PARAM, "LDAPUseSASL", FALSE);
3760 if (c != NULL) {
3761 ldap_sasl_mechs = c->argv[0];
3762 }
3763
3764 #if defined(LBER_OPT_LOG_PRINT_FN)
3765 /* If trace logging is enabled for the 'ldap.library' channel, direct
3766 * libldap (via liblber) to log to our trace logging.
3767 */
3768 if (pr_trace_get_level(libtrace_channel) >= 1) {
3769 int res, trace_level;
3770
3771 res = ber_set_option(NULL, LBER_OPT_LOG_PRINT_FN, ldap_tracelog_cb);
3772 if (res != LBER_OPT_SUCCESS) {
3773 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
3774 "error setting trace logging function: %s", strerror(EINVAL));
3775 }
3776
3777 /* Debug levels:
3778 * Trace (1)
3779 * Packets (2)
3780 * Arguments (4)
3781 * Filters (32)
3782 * Access control (128)
3783 *
3784 * See include/ldap_log.h in the OpenLDAP source.
3785 */
3786 trace_level = pr_trace_get_level(libtrace_channel);
3787 res = ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &trace_level);
3788 if (res != LDAP_OPT_SUCCESS) {
3789 (void) pr_log_writefile(ldap_logfd, MOD_LDAP_VERSION,
3790 "error setting LDAP debug level %d: %s", trace_level,
3791 strerror(EINVAL));
3792 }
3793 }
3794 #endif /* LBER_OPT_LOG_PRINT_FN */
3795
3796 if (ldap_do_users == FALSE) {
3797 pr_log_pri(PR_LOG_WARNING, MOD_LDAP_VERSION
3798 ": LDAPUsers not configured, skipping LDAP-based user authentication");
3799 }
3800
3801 if (ldap_do_groups == FALSE) {
3802 pr_log_pri(PR_LOG_NOTICE, MOD_LDAP_VERSION
3803 ": LDAPGroups not configured, skipping LDAP-based group memberships");
3804 }
3805
3806 return 0;
3807 }
3808
3809 /* Module API tables
3810 */
3811
3812 static conftable ldap_conftab[] = {
3813 { "LDAPAliasDereference", set_ldapaliasdereference, NULL },
3814 { "LDAPAttr", set_ldapattr, NULL },
3815 { "LDAPAuthBinds", set_ldapauthbinds, NULL },
3816 { "LDAPBindDN", set_ldapbinddn, NULL },
3817 { "LDAPConnectTimeout", set_ldapconnecttimeout, NULL },
3818 { "LDAPDefaultAuthScheme", set_ldapdefaultauthscheme, NULL },
3819 { "LDAPDefaultGID", set_ldapdefaultgid, NULL },
3820 { "LDAPDefaultQuota", set_ldapdefaultquota, NULL },
3821 { "LDAPDefaultUID", set_ldapdefaultuid, NULL },
3822 { "LDAPForceDefaultGID", set_ldapforcedefaultgid, NULL },
3823 { "LDAPForceDefaultUID", set_ldapforcedefaultuid, NULL },
3824 { "LDAPForceGeneratedHomedir",set_ldapforcegenhdir, NULL },
3825 { "LDAPGenerateHomedir", set_ldapgenhdir, NULL },
3826 { "LDAPGenerateHomedirPrefix",set_ldapgenhdirprefix, NULL },
3827 { "LDAPGenerateHomedirPrefixNoUsername",
3828 set_ldapgenhdirprefixnouname, NULL },
3829 { "LDAPGroups", set_ldapgroups, NULL },
3830 { "LDAPLog", set_ldaplog, NULL },
3831 { "LDAPProtocolVersion", set_ldapprotoversion, NULL },
3832 { "LDAPQueryTimeout", set_ldapquerytimeout, NULL },
3833 { "LDAPSearchScope", set_ldapsearchscope, NULL },
3834 { "LDAPServer", set_ldapserver, NULL },
3835 { "LDAPUsers", set_ldapusers, NULL },
3836 { "LDAPUseSASL", set_ldapusesasl, NULL },
3837 { "LDAPUseTLS", set_ldapusetls, NULL },
3838
3839 { NULL, NULL, NULL },
3840 };
3841
3842 static cmdtable ldap_cmdtab[] = {
3843 { HOOK, "ldap_quota_lookup", G_NONE, handle_ldap_quota_lookup, FALSE, FALSE},
3844 { HOOK, "ldap_ssh_publickey_lookup", G_NONE, handle_ldap_ssh_pubkey_lookup, FALSE, FALSE},
3845
3846 { 0, NULL}
3847 };
3848
3849 static authtable ldap_authtab[] = {
3850 { 0, "setpwent", ldap_auth_setpwent },
3851 { 0, "endpwent", ldap_auth_endpwent },
3852 { 0, "setgrent", ldap_auth_setpwent },
3853 { 0, "endgrent", ldap_auth_endpwent },
3854 { 0, "getpwnam", ldap_auth_getpwnam },
3855 { 0, "getpwuid", ldap_auth_getpwuid },
3856 { 0, "getgrnam", ldap_auth_getgrnam },
3857 { 0, "getgrgid", ldap_auth_getgrgid },
3858 { 0, "getgroups", ldap_auth_getgroups },
3859 { 0, "auth", ldap_auth_auth },
3860 { 0, "check", ldap_auth_check },
3861 { 0, "uid2name", ldap_auth_uid2name },
3862 { 0, "gid2name", ldap_auth_gid2name },
3863 { 0, "name2uid", ldap_auth_name2uid },
3864 { 0, "name2gid", ldap_auth_name2gid },
3865
3866 { 0, NULL }
3867 };
3868
3869 module ldap_module = {
3870 /* Always NULL */
3871 NULL, NULL,
3872
3873 /* Module API version */
3874 0x20,
3875
3876 /* Module name */
3877 "ldap",
3878
3879 /* Module configuration handler table */
3880 ldap_conftab,
3881
3882 /* Module command handler table */
3883 ldap_cmdtab,
3884
3885 /* Module authentication handler table */
3886 ldap_authtab,
3887
3888 /* Module initialization */
3889 ldap_mod_init,
3890
3891 /* Session initialization */
3892 ldap_sess_init,
3893
3894 /* Module version */
3895 MOD_LDAP_VERSION
3896 };