Debian Codegraph @

nslcd/cfg.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 cfg.c - functions for configuration information
3 This file contains parts that were part of the nss_ldap
4 library which has been forked into the nss-pam-ldapd library.
5
6 Copyright (C) 1997-2005 Luke Howard
7 Copyright (C) 2007 West Consulting
8 Copyright (C) 2007-2025 Arthur de Jong
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301 USA
24 */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <netdb.h>
39 #include <sys/socket.h>
40 #ifdef HAVE_GSSAPI_H
41 #include <gssapi.h>
42 #endif /* HAVE_GSSAPI_H */
43 #ifdef HAVE_GSSAPI_GSSAPI_H
44 #include <gssapi/gssapi.h>
45 #endif /* HAVE_GSSAPI_GSSAPI_H */
46 #ifdef HAVE_GSSAPI_GSSAPI_KRB5_H
47 #include <gssapi/gssapi_krb5.h>
48 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
49 #ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
50 #include <gssapi/gssapi_generic.h>
51 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
52 #include <sys/types.h>
53 #include <pwd.h>
54 #include <grp.h>
55
56 #include "common.h"
57 #include "log.h"
58 #include "cfg.h"
59 #include "attmap.h"
60 #include "common/expr.h"
61
62 struct ldap_config *nslcd_cfg = NULL;
63
64 /* the maximum line length in the configuration file */
65 #define MAX_LINE_LENGTH 4096
66
67 /* the delimiters of tokens */
68 #define TOKEN_DELIM " \t\n\r"
69
70 /* convenient wrapper macro for ldap_set_option() */
71 #define LDAP_SET_OPTION(ld, option, invalue) \
72 rc = ldap_set_option(ld, option, invalue); \
73 if (rc != LDAP_SUCCESS) \
74 { \
75 log_log(LOG_ERR, "ldap_set_option(" #option ") failed: %s", \
76 ldap_err2string(rc)); \
77 exit(EXIT_FAILURE); \
78 }
79
80 /* simple strdup wrapper */
81 static char *xstrdup(const char *s)
82 {
83 char *tmp;
84 if (s == NULL)
85 {
86 log_log(LOG_CRIT, "xstrdup() called with NULL");
87 exit(EXIT_FAILURE);
88 }
89 tmp = strdup(s);
90 if (tmp == NULL)
91 {
92 log_log(LOG_CRIT, "strdup() failed to allocate memory");
93 exit(EXIT_FAILURE);
94 }
95 return tmp;
96 }
97
98 /* check that the condition is true and otherwise log an error
99 and bail out */
100 static inline void check_argumentcount(const char *filename, int lnr,
101 const char *keyword, int condition)
102 {
103 if (!condition)
104 {
105 log_log(LOG_ERR, "%s:%d: %s: wrong number of arguments",
106 filename, lnr, keyword);
107 exit(EXIT_FAILURE);
108 }
109 }
110
111 /* This function works like strtok() except that the original string is
112 not modified and a pointer within str to where the next token begins
113 is returned (this can be used to pass to the function on the next
114 iteration). If no more tokens are found or the token will not fit in
115 the buffer, NULL is returned. */
116 static char *get_token(char **line, char *buf, size_t buflen)
117 {
118 size_t len;
119 if ((line == NULL) || (*line == NULL) || (**line == '\0') || (buf == NULL))
120 return NULL;
121 /* find the beginning and length of the token */
122 *line += strspn(*line, TOKEN_DELIM);
123 len = strcspn(*line, TOKEN_DELIM);
124 /* check if there is a token */
125 if (len == 0)
126 {
127 *line = NULL;
128 return NULL;
129 }
130 /* limit the token length */
131 if (len >= buflen)
132 len = buflen - 1;
133 /* copy the token */
134 strncpy(buf, *line, len);
135 buf[len] = '\0';
136 /* skip to the next token */
137 *line += len;
138 *line += strspn(*line, TOKEN_DELIM);
139 /* return the token */
140 return buf;
141 }
142
143 static char *get_strdup(const char *filename, int lnr,
144 const char *keyword, char **line)
145 {
146 char token[MAX_LINE_LENGTH];
147 check_argumentcount(filename, lnr, keyword,
148 get_token(line, token, sizeof(token)) != NULL);
149 return xstrdup(token);
150 }
151
152 static char *get_linedup(const char *filename, int lnr,
153 const char *keyword, char **line)
154 {
155 char *var;
156 check_argumentcount(filename, lnr, keyword, (*line != NULL) && (**line != '\0'));
157 var = xstrdup(*line);
158 /* mark that we are at the end of the line */
159 *line = NULL;
160 return var;
161 }
162
163 static void get_eol(const char *filename, int lnr,
164 const char *keyword, char **line)
165 {
166 if ((line != NULL) && (*line != NULL) && (**line != '\0'))
167 {
168 log_log(LOG_ERR, "%s:%d: %s: too many arguments", filename, lnr, keyword);
169 exit(EXIT_FAILURE);
170 }
171 }
172
173 static int get_int(const char *filename, int lnr,
174 const char *keyword, char **line)
175 {
176 char token[32];
177 check_argumentcount(filename, lnr, keyword,
178 get_token(line, token, sizeof(token)) != NULL);
179 /* TODO: replace with correct numeric parse */
180 return atoi(token);
181 }
182
183 static int parse_boolean(const char *filename, int lnr, const char *value)
184 {
185 if ((strcasecmp(value, "on") == 0) ||
186 (strcasecmp(value, "yes") == 0) ||
187 (strcasecmp(value, "true") == 0) || (strcasecmp(value, "1") == 0))
188 return 1;
189 else if ((strcasecmp(value, "off") == 0) ||
190 (strcasecmp(value, "no") == 0) ||
191 (strcasecmp(value, "false") == 0) || (strcasecmp(value, "0") == 0))
192 return 0;
193 else
194 {
195 log_log(LOG_ERR, "%s:%d: not a boolean argument: '%s'",
196 filename, lnr, value);
197 exit(EXIT_FAILURE);
198 }
199 }
200
201 static int get_boolean(const char *filename, int lnr,
202 const char *keyword, char **line)
203 {
204 char token[32];
205 check_argumentcount(filename, lnr, keyword,
206 get_token(line, token, sizeof(token)) != NULL);
207 return parse_boolean(filename, lnr, token);
208 }
209
210 static const char *print_boolean(int bool)
211 {
212 if (bool) return "yes";
213 else return "no";
214 }
215
216 #define TIME_MINUTES 60
217 #define TIME_HOURS (60 * 60)
218 #define TIME_DAYS (60 * 60 * 24)
219
220 static time_t parse_time(const char *filename, int lnr, const char *value)
221 {
222 time_t t;
223 char *tmp = NULL;
224 if (strcasecmp(value, "off") == 0)
225 return 0;
226 errno = 0;
227 t = strtol(value, &tmp, 10);
228 if (errno != 0)
229 {
230 log_log(LOG_ERR, "%s:%d: value out of range: '%s'",
231 filename, lnr, value);
232 exit(EXIT_FAILURE);
233 }
234 if ((strcasecmp(tmp, "") == 0) || (strcasecmp(tmp, "s") == 0))
235 return t;
236 else if (strcasecmp(tmp, "m") == 0)
237 return t * TIME_MINUTES;
238 else if (strcasecmp(tmp, "h") == 0)
239 return t * TIME_HOURS;
240 else if (strcasecmp(tmp, "d") == 0)
241 return t * TIME_DAYS;
242 else
243 {
244 log_log(LOG_ERR, "%s:%d: invalid time value: '%s'",
245 filename, lnr, value);
246 exit(EXIT_FAILURE);
247 }
248 }
249
250 static time_t get_time(const char *filename, int lnr,
251 const char *keyword, char **line)
252 {
253 char token[32];
254 check_argumentcount(filename, lnr, keyword,
255 get_token(line, token, sizeof(token)) != NULL);
256 return parse_time(filename, lnr, token);
257 }
258
259 static void print_time(time_t t, char *buffer, size_t buflen)
260 {
261 if (t == 0)
262 mysnprintf(buffer, buflen, "off");
263 else if ((t % TIME_DAYS) == 0)
264 mysnprintf(buffer, buflen, "%ldd", (long)(t / TIME_DAYS));
265 else if ((t % TIME_HOURS) == 0)
266 mysnprintf(buffer, buflen, "%ldh", (long)(t / TIME_HOURS));
267 else if ((t % TIME_MINUTES) == 0)
268 mysnprintf(buffer, buflen, "%ldm", (long)(t / TIME_MINUTES));
269 else
270 mysnprintf(buffer, buflen, "%lds", (long)t);
271 }
272
273 static void handle_uid(const char *filename, int lnr,
274 const char *keyword, char *line,
275 struct ldap_config *cfg)
276 {
277 char token[32];
278 struct passwd *pwent;
279 char *tmp;
280 check_argumentcount(filename, lnr, keyword,
281 get_token(&line, token, sizeof(token)) != NULL);
282 get_eol(filename, lnr, keyword, &line);
283 /* check if it is a valid numerical uid */
284 errno = 0;
285 cfg->uid = strtouid(token, &tmp, 10);
286 if ((*token != '\0') && (*tmp == '\0') && (errno == 0) && (strchr(token, '-') == NULL))
287 {
288 /* get the name and gid from the passwd database */
289 pwent = getpwuid(cfg->uid);
290 if (pwent != NULL)
291 {
292 if (cfg->gid == NOGID)
293 cfg->gid = pwent->pw_gid;
294 cfg->uidname = strdup(pwent->pw_name);
295 return;
296 }
297 }
298 /* find by name */
299 pwent = getpwnam(token);
300 if (pwent != NULL)
301 {
302 cfg->uid = pwent->pw_uid;
303 if (cfg->gid == NOGID)
304 cfg->gid = pwent->pw_gid;
305 cfg->uidname = strdup(token);
306 return;
307 }
308 /* log an error */
309 log_log(LOG_ERR, "%s:%d: %s: not a valid uid: '%s'",
310 filename, lnr, keyword, token);
311 exit(EXIT_FAILURE);
312 }
313
314 static void handle_gid(const char *filename, int lnr,
315 const char *keyword, char *line,
316 gid_t *gid)
317 {
318 char token[32];
319 struct group *grent;
320 char *tmp;
321 check_argumentcount(filename, lnr, keyword,
322 get_token(&line, token, sizeof(token)) != NULL);
323 get_eol(filename, lnr, keyword, &line);
324 /* check if it is a valid numerical gid */
325 errno = 0;
326 *gid = strtogid(token, &tmp, 10);
327 if ((*token != '\0') && (*tmp == '\0') && (errno == 0) && (strchr(token, '-') == NULL))
328 return;
329 /* find by name */
330 grent = getgrnam(token);
331 if (grent != NULL)
332 {
333 *gid = grent->gr_gid;
334 return;
335 }
336 /* log an error */
337 log_log(LOG_ERR, "%s:%d: %s: not a valid gid: '%s'",
338 filename, lnr, keyword, token);
339 exit(EXIT_FAILURE);
340 }
341
342 static int parse_loglevel(const char *filename, int lnr, const char *value)
343 {
344 if (strcasecmp(value, "crit") == 0)
345 return LOG_CRIT;
346 else if ((strcasecmp(value, "error") == 0) || (strcasecmp(value, "err") == 0))
347 return LOG_ERR;
348 else if (strcasecmp(value, "warning")==0)
349 return LOG_WARNING;
350 else if (strcasecmp(value, "notice")==0)
351 return LOG_NOTICE;
352 else if (strcasecmp(value, "info")==0)
353 return LOG_INFO;
354 else if (strcasecmp(value, "debug")==0)
355 return LOG_DEBUG;
356 else
357 {
358 log_log(LOG_ERR, "%s:%d: not a log level '%s'",
359 filename, lnr, value);
360 exit(EXIT_FAILURE);
361 }
362 }
363
364 static void handle_log(const char *filename, int lnr,
365 const char *keyword, char *line)
366 {
367 int level = LOG_INFO;
368 char scheme[64];
369 char loglevel[32];
370 check_argumentcount(filename, lnr, keyword,
371 get_token(&line, scheme, sizeof(scheme)) != NULL);
372 if (get_token(&line, loglevel, sizeof(loglevel)) != NULL)
373 level = parse_loglevel(filename, lnr, loglevel);
374 get_eol(filename, lnr, keyword, &line);
375 if (strcasecmp(scheme, "none") == 0)
376 log_addlogging_none();
377 else if (strcasecmp(scheme, "syslog") == 0)
378 log_addlogging_syslog(level);
379 else if (scheme[0] == '/')
380 log_addlogging_file(level, scheme);
381 else
382 {
383 log_log(LOG_ERR, "%s:%d: %s: invalid argument '%s'",
384 filename, lnr, keyword, scheme);
385 exit(EXIT_FAILURE);
386 }
387 }
388
389 /* add a single URI to the list of URIs in the configuration */
390 static void add_uri(const char *filename, int lnr,
391 struct ldap_config *cfg, const char *uri)
392 {
393 int i;
394 /* find the place where to insert the URI */
395 for (i = 0; cfg->uris[i].uri != NULL; i++)
396 /* nothing */ ;
397 /* check for room */
398 if (i >= NSS_LDAP_CONFIG_MAX_URIS)
399 {
400 log_log(LOG_ERR, "%s:%d: maximum number of URIs exceeded",
401 filename, lnr);
402 exit(EXIT_FAILURE);
403 }
404 /* append URI to list */
405 cfg->uris[i].uri = xstrdup(uri);
406 }
407
408 #ifdef HAVE_LDAP_DOMAIN2HOSTLIST
409 /* return the domain name of the current host
410 the returned string must be freed by caller */
411 static const char *cfg_getdomainname(const char *filename, int lnr)
412 {
413 const char *fqdn, *domain;
414 fqdn = getfqdn();
415 if ((fqdn != NULL) && ((domain = strchr(fqdn, '.')) != NULL) && (domain[1] != '\0'))
416 return domain + 1;
417 log_log(LOG_ERR, "%s:%d: unable to determinate a domain name",
418 filename, lnr);
419 exit(EXIT_FAILURE);
420 }
421
422 /* add URIs by doing DNS queries for SRV records */
423 static void add_uris_from_dns(const char *filename, int lnr,
424 struct ldap_config *cfg, const char *domain,
425 int force_ldaps)
426 {
427 int rc;
428 char *hostlist = NULL, *nxt;
429 char buf[BUFLEN_HOSTNAME + sizeof("ldaps://")];
430 log_log(LOG_DEBUG, "query %s for SRV records", domain);
431 rc = ldap_domain2hostlist(domain, &hostlist);
432 if (rc != LDAP_SUCCESS)
433 {
434 log_log(LOG_ERR, "%s:%d: no servers found in DNS zone %s: %s",
435 filename, lnr, domain, ldap_err2string(rc));
436 exit(EXIT_FAILURE);
437 }
438 if ((hostlist == NULL) || (*hostlist == '\0'))
439 {
440 log_log(LOG_ERR, "%s:%d: no servers found in DNS zone %s",
441 filename, lnr, domain);
442 exit(EXIT_FAILURE);
443 }
444 /* hostlist is a space-separated list of host names that we use to build
445 URIs */
446 while (hostlist != NULL)
447 {
448 /* find the next space and split the string there */
449 nxt = strchr(hostlist, ' ');
450 if (nxt != NULL)
451 {
452 *nxt = '\0';
453 nxt++;
454 }
455 /* if port is 636, use ldaps:// URI */
456 if ((strlen(hostlist) > 4) && (strcmp(hostlist + strlen(hostlist) - 4, ":636") == 0))
457 {
458 hostlist[strlen(hostlist) - 4] = '\0';
459 if (mysnprintf(buf, sizeof(buf), "ldaps://%s", hostlist))
460 {
461 log_log(LOG_ERR, "add_uris_from_dns(): buf buffer too small (%lu required)",
462 (unsigned long) strlen(hostlist) + 8);
463 exit(EXIT_FAILURE);
464 }
465 }
466 else
467 {
468 /* strip default port number */
469 if ((strlen(hostlist) > 4) && (strcmp(hostlist + strlen(hostlist) - 4, ":389") == 0))
470 hostlist[strlen(hostlist) - 4] = '\0';
471 if (mysnprintf(buf, sizeof(buf), "ldap%s://%s", force_ldaps ? "s" : "", hostlist))
472 {
473 log_log(LOG_ERR, "add_uris_from_dns(): buf buffer too small (%lu required)",
474 (unsigned long) strlen(hostlist) + 7);
475 exit(EXIT_FAILURE);
476 }
477 }
478 log_log(LOG_DEBUG, "add_uris_from_dns(): found uri: %s", buf);
479 add_uri(filename, lnr, cfg, buf);
480 /* get next entry from list */
481 hostlist = nxt;
482 }
483 }
484 #endif /* HAVE_LDAP_DOMAIN2HOSTLIST */
485
486 /* check that the file is not world readable */
487 static void check_permissions(const char *filename, const char *keyword)
488 {
489 struct stat sb;
490 /* get file status */
491 if (stat(filename, &sb))
492 {
493 log_log(LOG_ERR, "cannot stat() %s: %s", filename, strerror(errno));
494 exit(EXIT_FAILURE);
495 }
496 /* check permissions */
497 if ((sb.st_mode & 0007) != 0)
498 {
499 if (keyword != NULL)
500 log_log(LOG_ERR, "%s: file should not be world readable if %s is set",
501 filename, keyword);
502 else
503 log_log(LOG_ERR, "%s: file should not be world readable", filename);
504 exit(EXIT_FAILURE);
505 }
506 }
507
508 /* check whether the specified path is readable */
509 static void check_readable(const char *filename, int lnr,
510 const char *keyword, const char *path)
511 {
512 if (access(path, R_OK) != 0)
513 {
514 log_log(LOG_ERR, "%s:%d: %s: error accessing %s: %s",
515 filename, lnr, keyword, path, strerror(errno));
516 exit(EXIT_FAILURE);
517 }
518 }
519
520 /* check whether the specified path is a directory */
521 static void check_dir(const char *filename, int lnr,
522 const char *keyword, const char *path)
523 {
524 struct stat sb;
525 if (stat(path, &sb))
526 {
527 log_log(LOG_ERR, "%s:%d: %s: cannot stat() %s: %s",
528 filename, lnr, keyword, path, strerror(errno));
529 exit(EXIT_FAILURE);
530 }
531 if (!S_ISDIR(sb.st_mode))
532 {
533 log_log(LOG_ERR, "%s:%d: %s: %s is not a directory",
534 filename, lnr, keyword, path);
535 exit(EXIT_FAILURE);
536 }
537 }
538
539 static void handle_krb5_ccname(const char *filename, int lnr,
540 const char *keyword, char *line)
541 {
542 char token[80];
543 const char *ccname;
544 const char *ccfile;
545 size_t ccenvlen;
546 char *ccenv;
547 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
548 OM_uint32 minor_status;
549 #endif /* HAVE_GSS_KRB5_CCACHE_NAME */
550 /* get token */
551 check_argumentcount(filename, lnr, keyword,
552 (get_token(&line, token, sizeof(token)) != NULL));
553 get_eol(filename, lnr, keyword, &line);
554 /* set default kerberos ticket cache for SASL-GSSAPI */
555 ccname = token;
556 /* check that cache exists and is readable if it is a file */
557 if ((strncasecmp(ccname, "FILE:", sizeof("FILE:") - 1) == 0) ||
558 (strncasecmp(ccname, "WRFILE:", sizeof("WRFILE:") - 1) == 0))
559 {
560 ccfile = strchr(ccname, ':') + 1;
561 check_readable(filename, lnr, keyword, ccfile);
562 }
563 /* set the environment variable (we have a memory leak if this option
564 is set multiple times) */
565 ccenvlen = strlen(ccname) + sizeof("KRB5CCNAME=");
566 ccenv = (char *)malloc(ccenvlen);
567 if (ccenv == NULL)
568 {
569 log_log(LOG_CRIT, "malloc() failed to allocate memory");
570 exit(EXIT_FAILURE);
571 }
572 mysnprintf(ccenv, ccenvlen, "KRB5CCNAME=%s", ccname);
573 putenv(ccenv);
574 #ifdef HAVE_GSS_KRB5_CCACHE_NAME
575 /* set the name with gss_krb5_ccache_name() */
576 if (gss_krb5_ccache_name(&minor_status, ccname, NULL) != GSS_S_COMPLETE)
577 {
578 log_log(LOG_ERR, "%s:%d: unable to set default credential cache: %s",
579 filename, lnr, ccname);
580 exit(EXIT_FAILURE);
581 }
582 #endif /* HAVE_GSS_KRB5_CCACHE_NAME */
583 }
584
585 static enum ldap_map_selector parse_map(const char *value)
586 {
587 if ((strcasecmp(value, "alias") == 0) || (strcasecmp(value, "aliases") == 0))
588 return LM_ALIASES;
589 else if ((strcasecmp(value, "ether") == 0) || (strcasecmp(value, "ethers") == 0))
590 return LM_ETHERS;
591 else if (strcasecmp(value, "group") == 0)
592 return LM_GROUP;
593 else if ((strcasecmp(value, "host") == 0) || (strcasecmp(value, "hosts") == 0))
594 return LM_HOSTS;
595 else if (strcasecmp(value, "netgroup") == 0)
596 return LM_NETGROUP;
597 else if ((strcasecmp(value, "network") == 0) || (strcasecmp(value, "networks") == 0))
598 return LM_NETWORKS;
599 else if (strcasecmp(value, "passwd") == 0)
600 return LM_PASSWD;
601 else if ((strcasecmp(value, "protocol") == 0) || (strcasecmp(value, "protocols") == 0))
602 return LM_PROTOCOLS;
603 else if (strcasecmp(value, "rpc") == 0)
604 return LM_RPC;
605 else if ((strcasecmp(value, "service") == 0) || (strcasecmp(value, "services") == 0))
606 return LM_SERVICES;
607 else if (strcasecmp(value, "shadow") == 0)
608 return LM_SHADOW;
609 else if (strcasecmp(value, "nfsidmap") == 0)
610 return LM_NFSIDMAP;
611 /* unknown map */
612 return LM_NONE;
613 }
614
615 /* check to see if the line begins with a named map */
616 static enum ldap_map_selector get_map(char **line)
617 {
618 char token[32];
619 char *old;
620 enum ldap_map_selector map;
621 /* get the token */
622 old = *line;
623 if (get_token(line, token, sizeof(token)) == NULL)
624 return LM_NONE;
625 /* see if we found a map */
626 map = parse_map(token);
627 /* unknown map, return to the previous state */
628 if (map == LM_NONE)
629 *line = old;
630 return map;
631 }
632
633 static const char *print_map(enum ldap_map_selector map)
634 {
635 switch (map)
636 {
637 case LM_ALIASES: return "aliases";
638 case LM_ETHERS: return "ethers";
639 case LM_GROUP: return "group";
640 case LM_HOSTS: return "hosts";
641 case LM_NETGROUP: return "netgroup";
642 case LM_NETWORKS: return "networks";
643 case LM_PASSWD: return "passwd";
644 case LM_PROTOCOLS: return "protocols";
645 case LM_RPC: return "rpc";
646 case LM_SERVICES: return "services";
647 case LM_SHADOW: return "shadow";
648 case LM_NFSIDMAP: return "nfsidmap";
649 case LM_NONE:
650 default: return "???";
651 }
652 }
653
654 static void handle_base(const char *filename, int lnr,
655 const char *keyword, char *line,
656 struct ldap_config *cfg)
657 {
658 const char **bases;
659 int i;
660 char *value;
661 #ifdef HAVE_LDAP_DOMAIN2DN
662 const char *domain = NULL;
663 char *domaindn = NULL;
664 #endif /* HAVE_LDAP_DOMAIN2DN */
665 /* get the list of bases to update */
666 bases = base_get_var(get_map(&line));
667 if (bases == NULL)
668 bases = cfg->bases;
669 /* rest of the line is the value */
670 value = get_linedup(filename, lnr, keyword, &line);
671 /* if the base is "DOMAIN" use the domain name */
672 if (strcasecmp(value, "domain") == 0)
673 {
674 #ifdef HAVE_LDAP_DOMAIN2DN
675 free(value);
676 domain = cfg_getdomainname(filename, lnr);
677 ldap_domain2dn(domain, &domaindn);
678 log_log(LOG_DEBUG, "set_base(): setting base to %s from domain",
679 domaindn);
680 value = xstrdup(domaindn);
681 #else /* not HAVE_LDAP_DOMAIN2DN */
682 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
683 filename, lnr, value);
684 exit(EXIT_FAILURE);
685 #endif /* not HAVE_LDAP_DOMAIN2DN */
686 }
687 if (strcasecmp(value, "\"\"") == 0)
688 {
689 free(value);
690 value = "";
691 }
692 /* find the spot in the list of bases */
693 for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
694 if (bases[i] == NULL)
695 {
696 bases[i] = value;
697 return;
698 }
699 /* no free spot found */
700 log_log(LOG_ERR, "%s:%d: maximum number of base options per map (%d) exceeded",
701 filename, lnr, NSS_LDAP_CONFIG_MAX_BASES);
702 exit(EXIT_FAILURE);
703 }
704
705 static void handle_scope(const char *filename, int lnr,
706 const char *keyword, char *line,
707 struct ldap_config *cfg)
708 {
709 char token[32];
710 int *var;
711 var = scope_get_var(get_map(&line));
712 check_argumentcount(filename, lnr, keyword,
713 get_token(&line, token, sizeof(token)) != NULL);
714 get_eol(filename, lnr, keyword, &line);
715 if (var == NULL)
716 var = &cfg->scope;
717 if ((strcasecmp(token, "sub") == 0) || (strcasecmp(token, "subtree") == 0))
718 *var = LDAP_SCOPE_SUBTREE;
719 else if ((strcasecmp(token, "one") == 0) || (strcasecmp(token, "onelevel") == 0))
720 *var = LDAP_SCOPE_ONELEVEL;
721 else if (strcasecmp(token, "base") == 0)
722 *var = LDAP_SCOPE_BASE;
723 #ifdef LDAP_SCOPE_CHILDREN
724 else if (strcasecmp(token, "children") == 0)
725 *var = LDAP_SCOPE_CHILDREN;
726 #endif /* LDAP_SCOPE_CHILDREN */
727 else
728 {
729 log_log(LOG_ERR, "%s:%d: not a scope argument: '%s'",
730 filename, lnr, token);
731 exit(EXIT_FAILURE);
732 }
733 }
734
735 static const char *print_scope(int scope)
736 {
737 switch (scope)
738 {
739 case LDAP_SCOPE_SUBTREE: return "sub";
740 case LDAP_SCOPE_ONELEVEL: return "one";
741 case LDAP_SCOPE_BASE: return "base";
742 #ifdef LDAP_SCOPE_CHILDREN
743 case LDAP_SCOPE_CHILDREN: return "children";
744 #endif /* LDAP_SCOPE_CHILDREN */
745 default: return "???";
746 }
747 }
748
749 static void handle_deref(const char *filename, int lnr,
750 const char *keyword, char *line,
751 struct ldap_config *cfg)
752 {
753 char token[32];
754 check_argumentcount(filename, lnr, keyword,
755 get_token(&line, token, sizeof(token)) != NULL);
756 get_eol(filename, lnr, keyword, &line);
757 if (strcasecmp(token, "never") == 0)
758 cfg->deref = LDAP_DEREF_NEVER;
759 else if (strcasecmp(token, "searching") == 0)
760 cfg->deref = LDAP_DEREF_SEARCHING;
761 else if (strcasecmp(token, "finding") == 0)
762 cfg->deref = LDAP_DEREF_FINDING;
763 else if (strcasecmp(token, "always") == 0)
764 cfg->deref = LDAP_DEREF_ALWAYS;
765 else
766 {
767 log_log(LOG_ERR, "%s:%d: wrong argument: '%s'", filename, lnr, token);
768 exit(EXIT_FAILURE);
769 }
770 }
771
772 static const char *print_deref(int deref)
773 {
774 switch (deref)
775 {
776 case LDAP_DEREF_NEVER: return "never";
777 case LDAP_DEREF_SEARCHING: return "searching";
778 case LDAP_DEREF_FINDING: return "finding";
779 case LDAP_DEREF_ALWAYS: return "always";
780 default: return "???";
781 }
782 }
783
784 static void handle_filter(const char *filename, int lnr,
785 const char *keyword, char *line)
786 {
787 const char **var;
788 const char *map = line;
789 var = filter_get_var(get_map(&line));
790 if (var == NULL)
791 {
792 log_log(LOG_ERR, "%s:%d: unknown map: '%s'", filename, lnr, map);
793 exit(EXIT_FAILURE);
794 }
795 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
796 /* check if the value will be changed */
797 if (strcmp(*var, line) != 0)
798 {
799 /* Note: we have a memory leak here if a single mapping is changed
800 multiple times in one config (deemed not a problem) */
801 *var = xstrdup(line);
802 }
803 }
804
805 /* this function modifies the statement argument passed */
806 static void handle_map(const char *filename, int lnr,
807 const char *keyword, char *line)
808 {
809 enum ldap_map_selector map;
810 const char **var;
811 char oldatt[32], *newatt;
812 /* get the map */
813 if ((map = get_map(&line)) == LM_NONE)
814 {
815 log_log(LOG_ERR, "%s:%d: unknown map: '%s'", filename, lnr, line);
816 exit(EXIT_FAILURE);
817 }
818 /* read the other tokens */
819 check_argumentcount(filename, lnr, keyword,
820 (get_token(&line, oldatt, sizeof(oldatt)) != NULL));
821 newatt = get_linedup(filename, lnr, keyword, &line);
822 /* change attribute mapping */
823 var = attmap_get_var(map, oldatt);
824 if (var == NULL)
825 {
826 log_log(LOG_ERR, "%s:%d: unknown attribute to map: '%s'",
827 filename, lnr, oldatt);
828 exit(EXIT_FAILURE);
829 }
830 if (attmap_set_mapping(var, newatt) == NULL)
831 {
832 log_log(LOG_ERR, "%s:%d: attribute %s cannot be an expression",
833 filename, lnr, oldatt);
834 exit(EXIT_FAILURE);
835 }
836 free(newatt);
837 }
838
839 #ifdef LDAP_OPT_X_TLS
840 static const char *print_ssl(int ssl)
841 {
842 switch (ssl)
843 {
844 case SSL_OFF: return "off";
845 case SSL_START_TLS: return "start_tls";
846 case SSL_LDAPS: return "on";
847 default: return "???";
848 }
849 }
850
851 static int get_tls_reqcertsan_value(const char *filename, int lnr,
852 const char *keyword, char **line)
853 {
854 char token[16];
855 check_argumentcount(filename, lnr, keyword,
856 get_token(line, token, sizeof(token)) != NULL);
857 /* check if it is a valid value for tls_reqcert option */
858 if ((strcasecmp(token, "never") == 0) || (strcasecmp(token, "no") == 0))
859 return LDAP_OPT_X_TLS_NEVER;
860 else if (strcasecmp(token, "allow") == 0)
861 return LDAP_OPT_X_TLS_ALLOW;
862 else if (strcasecmp(token, "try") == 0)
863 return LDAP_OPT_X_TLS_TRY;
864 else if ((strcasecmp(token, "demand") == 0) ||
865 (strcasecmp(token, "yes") == 0))
866 return LDAP_OPT_X_TLS_DEMAND;
867 else if (strcasecmp(token, "hard") == 0)
868 return LDAP_OPT_X_TLS_HARD;
869 else
870 {
871 log_log(LOG_ERR, "%s:%d: %s: invalid argument: '%s'",
872 filename, lnr, keyword, token);
873 exit(EXIT_FAILURE);
874 }
875 }
876
877 static const char *print_tls_reqcert(int value)
878 {
879 switch (value)
880 {
881 case LDAP_OPT_X_TLS_NEVER: return "never";
882 case LDAP_OPT_X_TLS_ALLOW: return "allow";
883 case LDAP_OPT_X_TLS_TRY: return "try";
884 case LDAP_OPT_X_TLS_DEMAND: return "demand";
885 case LDAP_OPT_X_TLS_HARD: return "hard";
886 default: return "???";
887 }
888 }
889
890 static void handle_tls_reqcert(const char *filename, int lnr,
891 const char *keyword, char *line)
892 {
893 int value, rc;
894 value = get_tls_reqcertsan_value(filename, lnr, keyword, &line);
895 get_eol(filename, lnr, keyword, &line);
896 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_REQUIRE_CERT,%s)",
897 print_tls_reqcert(value));
898 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &value);
899 }
900
901 #ifdef LDAP_OPT_X_TLS_REQUIRE_SAN
902 static void handle_tls_reqsan(const char *filename, int lnr,
903 const char *keyword, char *line)
904 {
905 int value, rc;
906 value = get_tls_reqcertsan_value(filename, lnr, keyword, &line);
907 get_eol(filename, lnr, keyword, &line);
908 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_REQUIRE_SAN,%s)",
909 print_tls_reqcert(value));
910 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_REQUIRE_SAN, &value);
911 }
912 #endif /* LDAP_OPT_X_TLS_REQUIRE_SAN */
913
914 #ifdef LDAP_OPT_X_TLS_CRLCHECK
915 static void handle_tls_crlcheck(const char *filename, int lnr,
916 const char *keyword, char *line)
917 {
918 char token[16];
919 int value, rc;
920 /* get token */
921 check_argumentcount(filename, lnr, keyword,
922 get_token(&line, token, sizeof(token)) != NULL);
923 get_eol(filename, lnr, keyword, &line);
924 /* check if it is a valid value for tls_crlcheck option */
925 if (strcasecmp(token, "none") == 0)
926 value = LDAP_OPT_X_TLS_CRL_NONE;
927 else if (strcasecmp(token, "peer") == 0)
928 value = LDAP_OPT_X_TLS_CRL_PEER;
929 else if (strcasecmp(token, "all") == 0)
930 value = LDAP_OPT_X_TLS_CRL_ALL;
931 else
932 {
933 log_log(LOG_ERR, "%s:%d: %s: invalid argument: '%s'",
934 filename, lnr, keyword, token);
935 exit(EXIT_FAILURE);
936 }
937 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CRLCHECK,%s)", token);
938 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CRLCHECK, &value);
939 }
940
941 static const char *print_tls_crlcheck(int value)
942 {
943 switch (value)
944 {
945 case LDAP_OPT_X_TLS_CRL_NONE: return "none";
946 case LDAP_OPT_X_TLS_CRL_PEER: return "peer";
947 case LDAP_OPT_X_TLS_CRL_ALL: return "all";
948 default: return "???";
949 }
950 }
951 #endif /* LDAP_OPT_X_TLS_CRLCHECK */
952 #endif /* LDAP_OPT_X_TLS */
953
954 /* this function modifies the line argument passed */
955 static void handle_nss_initgroups_ignoreusers(
956 const char *filename, int lnr,
957 const char *keyword, char *line, struct ldap_config *cfg)
958 {
959 char token[MAX_LINE_LENGTH];
960 char *username, *next;
961 struct passwd *pwent;
962 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
963 if (cfg->nss_initgroups_ignoreusers == NULL)
964 cfg->nss_initgroups_ignoreusers = set_new();
965 while (get_token(&line, token, sizeof(token)) != NULL)
966 {
967 if (strcasecmp(token, "alllocal") == 0)
968 {
969 /* go over all users (this will work because nslcd is not yet running) */
970 setpwent();
971 while ((pwent = getpwent()) != NULL)
972 set_add(cfg->nss_initgroups_ignoreusers, pwent->pw_name);
973 endpwent();
974 }
975 else
976 {
977 next = token;
978 while (*next != '\0')
979 {
980 username = next;
981 /* find the end of the current username */
982 while ((*next != '\0') && (*next != ','))
983 next++;
984 if (*next == ',')
985 {
986 *next = '\0';
987 next++;
988 }
989 /* check if user exists (but add anyway) */
990 pwent = getpwnam(username);
991 if (pwent == NULL)
992 log_log(LOG_ERR, "%s:%d: user '%s' does not exist",
993 filename, lnr, username);
994 set_add(cfg->nss_initgroups_ignoreusers, username);
995 }
996 }
997 }
998 }
999
1000 static void handle_validnames(const char *filename, int lnr,
1001 const char *keyword, char *line,
1002 struct ldap_config *cfg)
1003 {
1004 char *value;
1005 int i, l;
1006 int flags = REG_EXTENDED | REG_NOSUB;
1007 /* the rest of the line should be a regular expression */
1008 value = get_linedup(filename, lnr, keyword, &line);
1009 if (cfg->validnames_str != NULL)
1010 {
1011 free(cfg->validnames_str);
1012 regfree(&cfg->validnames);
1013 }
1014 cfg->validnames_str = strdup(value);
1015 /* check formatting and update flags */
1016 if (value[0] != '/')
1017 {
1018 log_log(LOG_ERR, "%s:%d: regular expression incorrectly delimited",
1019 filename, lnr);
1020 exit(EXIT_FAILURE);
1021 }
1022 l = strlen(value);
1023 if (value[l - 1] == 'i')
1024 {
1025 value[l - 1] = '\0';
1026 l--;
1027 flags |= REG_ICASE;
1028 }
1029 if (value[l - 1] != '/')
1030 {
1031 log_log(LOG_ERR, "%s:%d: regular expression incorrectly delimited",
1032 filename, lnr);
1033 exit(EXIT_FAILURE);
1034 }
1035 value[l - 1] = '\0';
1036 /* compile the regular expression */
1037 if ((i = regcomp(&cfg->validnames, value + 1, flags)) != 0)
1038 {
1039 /* get the error message */
1040 l = regerror(i, &cfg->validnames, NULL, 0);
1041 value = malloc(l);
1042 if (value == NULL)
1043 log_log(LOG_ERR, "%s:%d: invalid regular expression", filename, lnr);
1044 else
1045 {
1046 regerror(i, &cfg->validnames, value, l);
1047 log_log(LOG_ERR, "%s:%d: invalid regular expression: %s",
1048 filename, lnr, value);
1049 }
1050 exit(EXIT_FAILURE);
1051 }
1052 free(value);
1053 }
1054
1055 static void check_search_variables(
1056 const char *filename, int lnr,
1057 const char *expression)
1058 {
1059 SET *set;
1060 const char **list;
1061 int i;
1062 set = expr_vars(expression, NULL);
1063 list = set_tolist(set);
1064 if (list == NULL)
1065 {
1066 log_log(LOG_CRIT,
1067 "check_search_variables(): malloc() failed to allocate memory");
1068 exit(EXIT_FAILURE);
1069 }
1070 for (i = 0; list[i] != NULL; i++)
1071 {
1072 if ((strcmp(list[i], "username") != 0) &&
1073 (strcmp(list[i], "service") != 0) &&
1074 (strcmp(list[i], "ruser") != 0) &&
1075 (strcmp(list[i], "rhost") != 0) &&
1076 (strcmp(list[i], "tty") != 0) &&
1077 (strcmp(list[i], "hostname") != 0) &&
1078 (strcmp(list[i], "fqdn") != 0) &&
1079 (strcmp(list[i], "domain") != 0) &&
1080 (strcmp(list[i], "dn") != 0) &&
1081 (strcmp(list[i], "uid") != 0))
1082 {
1083 log_log(LOG_ERR, "%s:%d: unknown variable $%s", filename, lnr, list[i]);
1084 exit(EXIT_FAILURE);
1085 }
1086 }
1087 /* free memory */
1088 set_free(set);
1089 free(list);
1090 }
1091
1092 static void handle_pam_authc_search(
1093 const char *filename, int lnr,
1094 const char *keyword, char *line, struct ldap_config *cfg)
1095 {
1096 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
1097 cfg->pam_authc_search = xstrdup(line);
1098 /* check the variables used in the expression */
1099 check_search_variables(filename, lnr, cfg->pam_authc_search);
1100 }
1101
1102 static void handle_pam_authz_search(
1103 const char *filename, int lnr,
1104 const char *keyword, char *line, struct ldap_config *cfg)
1105 {
1106 int i;
1107 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
1108 /* find free spot for search filter */
1109 for (i = 0; (i < NSS_LDAP_CONFIG_MAX_AUTHZ_SEARCHES) && (cfg->pam_authz_searches[i] != NULL);
1110 i++)
1111 /* nothing */ ;
1112 if (i >= NSS_LDAP_CONFIG_MAX_AUTHZ_SEARCHES)
1113 {
1114 log_log(LOG_ERR, "%s:%d: maximum number of pam_authz_search options (%d) exceeded",
1115 filename, lnr, NSS_LDAP_CONFIG_MAX_AUTHZ_SEARCHES);
1116 exit(EXIT_FAILURE);
1117 }
1118 cfg->pam_authz_searches[i] = xstrdup(line);
1119 /* check the variables used in the expression */
1120 check_search_variables(filename, lnr, cfg->pam_authz_searches[i]);
1121 }
1122
1123 static void handle_pam_password_prohibit_message(
1124 const char *filename, int lnr,
1125 const char *keyword, char *line, struct ldap_config *cfg)
1126 {
1127 char *value;
1128 int l;
1129 /* the rest of the line should be a message */
1130 value = get_linedup(filename, lnr, keyword, &line);
1131 /* strip quotes if they are present */
1132 l = strlen(value);
1133 if ((value[0] == '\"') && (value[l - 1] == '\"'))
1134 {
1135 value[l - 1] = '\0';
1136 value++;
1137 }
1138 cfg->pam_password_prohibit_message = value;
1139 }
1140
1141 static void handle_reconnect_invalidate(
1142 const char *filename, int lnr,
1143 const char *keyword, char *line, struct ldap_config *cfg)
1144 {
1145 char token[MAX_LINE_LENGTH];
1146 char *name, *next;
1147 enum ldap_map_selector map;
1148 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
1149 while (get_token(&line, token, sizeof(token)) != NULL)
1150 {
1151 next = token;
1152 while (*next != '\0')
1153 {
1154 name = next;
1155 /* find the end of the current map name */
1156 while ((*next != '\0') && (*next != ','))
1157 next++;
1158 if (*next == ',')
1159 {
1160 *next = '\0';
1161 next++;
1162 }
1163 /* check if map name exists */
1164 map = parse_map(name);
1165 if (map == LM_NONE)
1166 {
1167 log_log(LOG_ERR, "%s:%d: unknown map: '%s'", filename, lnr, name);
1168 exit(EXIT_FAILURE);
1169 }
1170 cfg->reconnect_invalidate[map] = 1;
1171 }
1172 }
1173 }
1174
1175 static void handle_cache(const char *filename, int lnr,
1176 const char *keyword, char *line,
1177 struct ldap_config *cfg)
1178 {
1179 char cache[16];
1180 time_t value1, value2;
1181 /* get cache map and values */
1182 check_argumentcount(filename, lnr, keyword,
1183 get_token(&line, cache, sizeof(cache)) != NULL);
1184 value1 = get_time(filename, lnr, keyword, &line);
1185 if ((line != NULL) && (*line != '\0'))
1186 value2 = get_time(filename, lnr, keyword, &line);
1187 else
1188 value2 = value1;
1189 get_eol(filename, lnr, keyword, &line);
1190 /* check the cache */
1191 if (strcasecmp(cache, "dn2uid") == 0)
1192 {
1193 cfg->cache_dn2uid_positive = value1;
1194 cfg->cache_dn2uid_negative = value2;
1195 }
1196 else
1197 {
1198 log_log(LOG_ERR, "%s:%d: unknown cache: '%s'", filename, lnr, cache);
1199 exit(EXIT_FAILURE);
1200 }
1201 }
1202
1203 /* This function tries to get the LDAP search base from the LDAP server.
1204 Note that this returns a string that has been allocated with strdup().
1205 For this to work the myldap module needs enough configuration information
1206 to make an LDAP connection. */
1207 static MUST_USE char *get_base_from_rootdse(void)
1208 {
1209 MYLDAP_SESSION *session;
1210 MYLDAP_SEARCH *search;
1211 MYLDAP_ENTRY *entry;
1212 const char *attrs[] = { "+", NULL };
1213 int i;
1214 int rc;
1215 const char **values;
1216 char *base = NULL;
1217 /* initialize session */
1218 session = myldap_create_session();
1219 assert(session != NULL);
1220 /* perform search */
1221 search = myldap_search(session, "", LDAP_SCOPE_BASE, "(objectClass=*)",
1222 attrs, NULL);
1223 if (search == NULL)
1224 {
1225 myldap_session_close(session);
1226 return NULL;
1227 }
1228 /* go over results */
1229 for (i = 0; (entry = myldap_get_entry(search, &rc)) != NULL; i++)
1230 {
1231 /* get defaultNamingContext */
1232 values = myldap_get_values(entry, "defaultNamingContext");
1233 if ((values != NULL) && (values[0] != NULL))
1234 {
1235 base = xstrdup(values[0]);
1236 log_log(LOG_DEBUG, "get_basedn_from_rootdse(): found attribute defaultNamingContext with value %s",
1237 values[0]);
1238 break;
1239 }
1240 /* get namingContexts */
1241 values = myldap_get_values(entry, "namingContexts");
1242 if ((values != NULL) && (values[0] != NULL))
1243 {
1244 base = xstrdup(values[0]);
1245 log_log(LOG_DEBUG, "get_basedn_from_rootdse(): found attribute namingContexts with value %s",
1246 values[0]);
1247 break;
1248 }
1249 }
1250 /* clean up */
1251 myldap_session_close(session);
1252 return base;
1253 }
1254
1255 /* set the configuration information to the defaults */
1256 static void cfg_defaults(struct ldap_config *cfg)
1257 {
1258 int i;
1259 memset(cfg, 0, sizeof(struct ldap_config));
1260 cfg->threads = 5;
1261 cfg->uidname = NULL;
1262 cfg->uid = NOUID;
1263 cfg->gid = NOGID;
1264 for (i = 0; i < (NSS_LDAP_CONFIG_MAX_URIS + 1); i++)
1265 {
1266 cfg->uris[i].uri = NULL;
1267 cfg->uris[i].firstfail = 0;
1268 cfg->uris[i].lastfail = 0;
1269 }
1270 #ifdef LDAP_VERSION3
1271 cfg->ldap_version = LDAP_VERSION3;
1272 #else /* LDAP_VERSION3 */
1273 cfg->ldap_version = LDAP_VERSION2;
1274 #endif /* not LDAP_VERSION3 */
1275 cfg->binddn = NULL;
1276 cfg->bindpw = NULL;
1277 cfg->rootpwmoddn = NULL;
1278 cfg->rootpwmodpw = NULL;
1279 cfg->sasl_mech = NULL;
1280 cfg->sasl_realm = NULL;
1281 cfg->sasl_authcid = NULL;
1282 cfg->sasl_authzid = NULL;
1283 cfg->sasl_secprops = NULL;
1284 #ifdef LDAP_OPT_X_SASL_NOCANON
1285 cfg->sasl_canonicalize = -1;
1286 #endif /* LDAP_OPT_X_SASL_NOCANON */
1287 for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
1288 cfg->bases[i] = NULL;
1289 cfg->scope = LDAP_SCOPE_SUBTREE;
1290 cfg->deref = LDAP_DEREF_NEVER;
1291 cfg->referrals = 1;
1292 #if defined(HAVE_LDAP_SASL_BIND) && defined(LDAP_SASL_SIMPLE)
1293 cfg->pam_authc_ppolicy = 1;
1294 #endif
1295 cfg->bind_timelimit = 10;
1296 cfg->timelimit = LDAP_NO_LIMIT;
1297 cfg->idle_timelimit = 0;
1298 cfg->reconnect_sleeptime = 1;
1299 cfg->reconnect_retrytime = 10;
1300 #ifdef LDAP_OPT_X_TLS
1301 cfg->ssl = SSL_OFF;
1302 #endif /* LDAP_OPT_X_TLS */
1303 cfg->pagesize = 0;
1304 cfg->nss_initgroups_ignoreusers = NULL;
1305 cfg->nss_min_uid = 0;
1306 cfg->nss_uid_offset = 0;
1307 cfg->nss_gid_offset = 0;
1308 cfg->nss_nested_groups = 0;
1309 cfg->nss_getgrent_skipmembers = 0;
1310 cfg->nss_disable_enumeration = 0;
1311 cfg->validnames_str = NULL;
1312 handle_validnames(__FILE__, __LINE__, "",
1313 "/^[a-z0-9._@$()]([a-z0-9._@$() \\~-]*[a-z0-9._@$()~-])?$/i",
1314 cfg);
1315 cfg->ignorecase = 0;
1316 cfg->pam_authc_search = "BASE";
1317 for (i = 0; i < NSS_LDAP_CONFIG_MAX_AUTHZ_SEARCHES; i++)
1318 cfg->pam_authz_searches[i] = NULL;
1319 cfg->pam_password_prohibit_message = NULL;
1320 for (i = 0; i < LM_NONE; i++)
1321 cfg->reconnect_invalidate[i] = 0;
1322 cfg->cache_dn2uid_positive = 15 * TIME_MINUTES;
1323 cfg->cache_dn2uid_negative = 15 * TIME_MINUTES;
1324 }
1325
1326 static void cfg_read(const char *filename, struct ldap_config *cfg)
1327 {
1328 FILE *fp;
1329 int lnr = 0;
1330 char linebuf[MAX_LINE_LENGTH];
1331 char *line;
1332 char keyword[32];
1333 char token[256];
1334 int i;
1335 #ifdef LDAP_OPT_X_TLS
1336 int rc;
1337 char *value;
1338 #endif
1339 /* open config file */
1340 if ((fp = fopen(filename, "r")) == NULL)
1341 {
1342 log_log(LOG_ERR, "cannot open config file (%s): %s",
1343 filename, strerror(errno));
1344 exit(EXIT_FAILURE);
1345 }
1346 /* read file and parse lines */
1347 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
1348 {
1349 lnr++;
1350 line = linebuf;
1351 /* strip newline */
1352 i = (int)strlen(line);
1353 if ((i <= 0) || (line[i - 1] != '\n'))
1354 {
1355 log_log(LOG_ERR, "%s:%d: line too long or last line missing newline",
1356 filename, lnr);
1357 exit(EXIT_FAILURE);
1358 }
1359 line[i - 1] = '\0';
1360 /* ignore comment lines */
1361 if (line[0] == '#')
1362 continue;
1363 /* strip trailing spaces */
1364 for (i--; (i > 0) && isspace(line[i - 1]); i--)
1365 line[i - 1] = '\0';
1366 /* get keyword from line and ignore empty lines */
1367 if (get_token(&line, keyword, sizeof(keyword)) == NULL)
1368 continue;
1369 /* runtime options */
1370 if (strcasecmp(keyword, "threads") == 0)
1371 {
1372 cfg->threads = get_int(filename, lnr, keyword, &line);
1373 get_eol(filename, lnr, keyword, &line);
1374 }
1375 else if (strcasecmp(keyword, "uid") == 0)
1376 {
1377 handle_uid(filename, lnr, keyword, line, cfg);
1378 }
1379 else if (strcasecmp(keyword, "gid") == 0)
1380 {
1381 handle_gid(filename, lnr, keyword, line, &cfg->gid);
1382 }
1383 else if (strcasecmp(keyword, "log") == 0)
1384 {
1385 handle_log(filename, lnr, keyword, line);
1386 }
1387 /* general connection options */
1388 else if (strcasecmp(keyword, "uri") == 0)
1389 {
1390 check_argumentcount(filename, lnr, keyword, (line != NULL) && (*line != '\0'));
1391 while (get_token(&line, token, sizeof(token)) != NULL)
1392 {
1393 if (strcasecmp(token, "dns") == 0)
1394 {
1395 #ifdef HAVE_LDAP_DOMAIN2HOSTLIST
1396 add_uris_from_dns(filename, lnr, cfg, cfg_getdomainname(filename, lnr), 0);
1397 #else /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1398 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
1399 filename, lnr, token);
1400 exit(EXIT_FAILURE);
1401 #endif /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1402 }
1403 else if (strncasecmp(token, "dns:", 4) == 0)
1404 {
1405 #ifdef HAVE_LDAP_DOMAIN2HOSTLIST
1406 add_uris_from_dns(filename, lnr, cfg, strdup(token + 4), 0);
1407 #else /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1408 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
1409 filename, lnr, token);
1410 exit(EXIT_FAILURE);
1411 #endif /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1412 }
1413 else if (strcasecmp(token, "dnsldaps") == 0)
1414 {
1415 #ifdef HAVE_LDAP_DOMAIN2HOSTLIST
1416 add_uris_from_dns(filename, lnr, cfg, cfg_getdomainname(filename, lnr), 1);
1417 #else /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1418 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
1419 filename, lnr, token);
1420 exit(EXIT_FAILURE);
1421 #endif /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1422 }
1423 else if (strncasecmp(token, "dnsldaps:", 9) == 0)
1424 {
1425 #ifdef HAVE_LDAP_DOMAIN2HOSTLIST
1426 add_uris_from_dns(filename, lnr, cfg, strdup(token + 9), 1);
1427 #else /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1428 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
1429 filename, lnr, token);
1430 exit(EXIT_FAILURE);
1431 #endif /* not HAVE_LDAP_DOMAIN2HOSTLIST */
1432 }
1433 else
1434 add_uri(filename, lnr, cfg, token);
1435 }
1436 }
1437 else if (strcasecmp(keyword, "ldap_version") == 0)
1438 {
1439 cfg->ldap_version = get_int(filename, lnr, keyword, &line);
1440 get_eol(filename, lnr, keyword, &line);
1441 }
1442 else if (strcasecmp(keyword, "binddn") == 0)
1443 {
1444 cfg->binddn = get_linedup(filename, lnr, keyword, &line);
1445 }
1446 else if (strcasecmp(keyword, "bindpw") == 0)
1447 {
1448 check_permissions(filename, keyword);
1449 cfg->bindpw = get_linedup(filename, lnr, keyword, &line);
1450 }
1451 else if (strcasecmp(keyword, "rootpwmoddn") == 0)
1452 {
1453 cfg->rootpwmoddn = get_linedup(filename, lnr, keyword, &line);
1454 }
1455 else if (strcasecmp(keyword, "rootpwmodpw") == 0)
1456 {
1457 check_permissions(filename, keyword);
1458 cfg->rootpwmodpw = get_linedup(filename, lnr, keyword, &line);
1459 }
1460 /* SASL authentication options */
1461 else if (strcasecmp(keyword, "sasl_mech") == 0)
1462 {
1463 cfg->sasl_mech = get_strdup(filename, lnr, keyword, &line);
1464 get_eol(filename, lnr, keyword, &line);
1465 }
1466 else if (strcasecmp(keyword, "sasl_realm") == 0)
1467 {
1468 cfg->sasl_realm = get_strdup(filename, lnr, keyword, &line);
1469 get_eol(filename, lnr, keyword, &line);
1470 }
1471 else if (strcasecmp(keyword, "sasl_authcid") == 0)
1472 {
1473 cfg->sasl_authcid = get_strdup(filename, lnr, keyword, &line);
1474 get_eol(filename, lnr, keyword, &line);
1475 }
1476 else if (strcasecmp(keyword, "sasl_authzid") == 0)
1477 {
1478 cfg->sasl_authzid = get_strdup(filename, lnr, keyword, &line);
1479 get_eol(filename, lnr, keyword, &line);
1480 }
1481 else if (strcasecmp(keyword, "sasl_secprops") == 0)
1482 {
1483 cfg->sasl_secprops = get_strdup(filename, lnr, keyword, &line);
1484 get_eol(filename, lnr, keyword, &line);
1485 }
1486 #ifdef LDAP_OPT_X_SASL_NOCANON
1487 else if ((strcasecmp(keyword, "sasl_canonicalize") == 0) ||
1488 (strcasecmp(keyword, "sasl_canonicalise") == 0) ||
1489 (strcasecmp(keyword, "ldap_sasl_canonicalize") == 0) ||
1490 (strcasecmp(keyword, "sasl_canon") == 0))
1491 {
1492 cfg->sasl_canonicalize = get_boolean(filename, lnr, keyword, &line);
1493 get_eol(filename, lnr, keyword, &line);
1494 }
1495 else if (strcasecmp(keyword, "sasl_nocanon") == 0)
1496 {
1497 cfg->sasl_canonicalize = get_boolean(filename, lnr, keyword, &line);
1498 cfg->sasl_canonicalize = !cfg->sasl_canonicalize;
1499 get_eol(filename, lnr, keyword, &line);
1500 }
1501 #endif /* LDAP_OPT_X_SASL_NOCANON */
1502 /* Kerberos authentication options */
1503 else if (strcasecmp(keyword, "krb5_ccname") == 0)
1504 {
1505 handle_krb5_ccname(filename, lnr, keyword, line);
1506 }
1507 /* search/mapping options */
1508 else if (strcasecmp(keyword, "base") == 0)
1509 {
1510 handle_base(filename, lnr, keyword, line, cfg);
1511 }
1512 else if (strcasecmp(keyword, "scope") == 0)
1513 {
1514 handle_scope(filename, lnr, keyword, line, cfg);
1515 }
1516 else if (strcasecmp(keyword, "deref") == 0)
1517 {
1518 handle_deref(filename, lnr, keyword, line, cfg);
1519 }
1520 else if (strcasecmp(keyword, "referrals") == 0)
1521 {
1522 cfg->referrals = get_boolean(filename, lnr, keyword, &line);
1523 get_eol(filename, lnr, keyword, &line);
1524 }
1525 else if (strcasecmp(keyword, "filter") == 0)
1526 {
1527 handle_filter(filename, lnr, keyword, line);
1528 }
1529 else if (strcasecmp(keyword, "map") == 0)
1530 {
1531 handle_map(filename, lnr, keyword, line);
1532 }
1533 else if (strcasecmp(keyword, "pam_authc_ppolicy") == 0)
1534 {
1535 #if defined(HAVE_LDAP_SASL_BIND) && defined(LDAP_SASL_SIMPLE)
1536 cfg->pam_authc_ppolicy = get_boolean(filename, lnr, keyword, &line);
1537 get_eol(filename, lnr, keyword, &line);
1538 #else
1539 log_log(LOG_ERR, "%s:%d: value %s not supported on platform",
1540 filename, lnr, value);
1541 exit(EXIT_FAILURE);
1542 #endif
1543 }
1544 /* timing/reconnect options */
1545 else if (strcasecmp(keyword, "bind_timelimit") == 0)
1546 {
1547 cfg->bind_timelimit = get_int(filename, lnr, keyword, &line);
1548 get_eol(filename, lnr, keyword, &line);
1549 }
1550 else if (strcasecmp(keyword, "timelimit") == 0)
1551 {
1552 cfg->timelimit = get_int(filename, lnr, keyword, &line);
1553 get_eol(filename, lnr, keyword, &line);
1554 }
1555 else if (strcasecmp(keyword, "idle_timelimit") == 0)
1556 {
1557 cfg->idle_timelimit = get_int(filename, lnr, keyword, &line);
1558 get_eol(filename, lnr, keyword, &line);
1559 }
1560 else if (!strcasecmp(keyword, "reconnect_sleeptime"))
1561 {
1562 cfg->reconnect_sleeptime = get_int(filename, lnr, keyword, &line);
1563 get_eol(filename, lnr, keyword, &line);
1564 }
1565 else if (strcasecmp(keyword, "reconnect_retrytime") == 0)
1566 {
1567 cfg->reconnect_retrytime = get_int(filename, lnr, keyword, &line);
1568 get_eol(filename, lnr, keyword, &line);
1569 }
1570 #ifdef LDAP_OPT_X_TLS
1571 /* SSL/TLS options */
1572 else if (strcasecmp(keyword, "ssl") == 0)
1573 {
1574 check_argumentcount(filename, lnr, keyword,
1575 (get_token(&line, token, sizeof(token)) != NULL));
1576 if ((strcasecmp(token, "start_tls") == 0) ||
1577 (strcasecmp(token, "starttls") == 0))
1578 cfg->ssl = SSL_START_TLS;
1579 else if (parse_boolean(filename, lnr, token))
1580 cfg->ssl = SSL_LDAPS;
1581 get_eol(filename, lnr, keyword, &line);
1582 }
1583 else if (strcasecmp(keyword, "tls_reqcert") == 0)
1584 {
1585 handle_tls_reqcert(filename, lnr, keyword, line);
1586 }
1587 else if (strcasecmp(keyword, "tls_cacertdir") == 0)
1588 {
1589 value = get_strdup(filename, lnr, keyword, &line);
1590 get_eol(filename, lnr, keyword, &line);
1591 check_dir(filename, lnr, token, value);
1592 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CACERTDIR,\"%s\")",
1593 value);
1594 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CACERTDIR, value);
1595 free(value);
1596 }
1597 else if ((strcasecmp(keyword, "tls_cacertfile") == 0) ||
1598 (strcasecmp(keyword, "tls_cacert") == 0))
1599 {
1600 value = get_strdup(filename, lnr, keyword, &line);
1601 get_eol(filename, lnr, keyword, &line);
1602 check_readable(filename, lnr, keyword, value);
1603 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CACERTFILE,\"%s\")",
1604 value);
1605 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CACERTFILE, value);
1606 free(value);
1607 }
1608 else if (strcasecmp(keyword, "tls_randfile") == 0)
1609 {
1610 value = get_strdup(filename, lnr, keyword, &line);
1611 get_eol(filename, lnr, keyword, &line);
1612 check_readable(filename, lnr, keyword, value);
1613 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_RANDOM_FILE,\"%s\")",
1614 value);
1615 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_RANDOM_FILE, value);
1616 free(value);
1617 }
1618 else if (strcasecmp(keyword, "tls_ciphers") == 0)
1619 {
1620 value = get_linedup(filename, lnr, keyword, &line);
1621 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CIPHER_SUITE,\"%s\")",
1622 value);
1623 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CIPHER_SUITE, value);
1624 free(value);
1625 }
1626 else if (strcasecmp(keyword, "tls_cert") == 0)
1627 {
1628 value = get_strdup(filename, lnr, keyword, &line);
1629 get_eol(filename, lnr, keyword, &line);
1630 check_readable(filename, lnr, keyword, value);
1631 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CERTFILE,\"%s\")",
1632 value);
1633 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CERTFILE, value);
1634 free(value);
1635 }
1636 else if (strcasecmp(keyword, "tls_key") == 0)
1637 {
1638 value = get_strdup(filename, lnr, keyword, &line);
1639 get_eol(filename, lnr, keyword, &line);
1640 check_readable(filename, lnr, keyword, value);
1641 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_KEYFILE,\"%s\")",
1642 value);
1643 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_KEYFILE, value);
1644 free(value);
1645 }
1646 else if (strcasecmp(keyword, "tls_reqsan") == 0)
1647 {
1648 #ifdef LDAP_OPT_X_TLS_REQUIRE_SAN
1649 handle_tls_reqsan(filename, lnr, keyword, line);
1650 #else /* not LDAP_OPT_X_TLS_REQUIRE_SAN */
1651 log_log(LOG_ERR, "%s:%d: option %s not supported on platform",
1652 filename, lnr, keyword);
1653 exit(EXIT_FAILURE);
1654 #endif /* LDAP_OPT_X_TLS_REQUIRE_SAN */
1655 }
1656 else if (strcasecmp(keyword, "tls_crlcheck") == 0)
1657 {
1658 #ifdef LDAP_OPT_X_TLS_CRLCHECK
1659 handle_tls_crlcheck(filename, lnr, keyword, line);
1660 #else /* not LDAP_OPT_X_TLS_CRLCHECK */
1661 log_log(LOG_ERR, "%s:%d: option %s not supported on platform",
1662 filename, lnr, keyword);
1663 exit(EXIT_FAILURE);
1664 #endif /* LDAP_OPT_X_TLS_CRLCHECK */
1665 }
1666 else if (strcasecmp(keyword, "tls_crlfile") == 0)
1667 {
1668 #ifdef LDAP_OPT_X_TLS_CRLFILE
1669 value = get_strdup(filename, lnr, keyword, &line);
1670 get_eol(filename, lnr, keyword, &line);
1671 check_readable(filename, lnr, keyword, value);
1672 log_log(LOG_DEBUG, "ldap_set_option(LDAP_OPT_X_TLS_CRLFILE,\"%s\")",
1673 value);
1674 LDAP_SET_OPTION(NULL, LDAP_OPT_X_TLS_CRLFILE, value);
1675 free(value);
1676 #else /* not LDAP_OPT_X_TLS_CRLFILE */
1677 log_log(LOG_ERR, "%s:%d: option %s not supported on platform",
1678 filename, lnr, keyword);
1679 exit(EXIT_FAILURE);
1680 #endif /* LDAP_OPT_X_TLS_CRLFILE */
1681 }
1682 #endif /* LDAP_OPT_X_TLS */
1683 /* other options */
1684 else if (strcasecmp(keyword, "pagesize") == 0)
1685 {
1686 cfg->pagesize = get_int(filename, lnr, keyword, &line);
1687 get_eol(filename, lnr, keyword, &line);
1688 }
1689 else if (strcasecmp(keyword, "nss_initgroups_ignoreusers") == 0)
1690 {
1691 handle_nss_initgroups_ignoreusers(filename, lnr, keyword, line,
1692 cfg);
1693 }
1694 else if (strcasecmp(keyword, "nss_min_uid") == 0)
1695 {
1696 cfg->nss_min_uid = get_int(filename, lnr, keyword, &line);
1697 get_eol(filename, lnr, keyword, &line);
1698 }
1699 else if (strcasecmp(keyword, "nss_uid_offset") == 0)
1700 {
1701 cfg->nss_uid_offset = get_int(filename, lnr, keyword, &line);
1702 get_eol(filename, lnr, keyword, &line);
1703 }
1704 else if (strcasecmp(keyword, "nss_gid_offset") == 0)
1705 {
1706 cfg->nss_gid_offset = get_int(filename, lnr, keyword, &line);
1707 get_eol(filename, lnr, keyword, &line);
1708 }
1709 else if (strcasecmp(keyword, "nss_nested_groups") == 0)
1710 {
1711 cfg->nss_nested_groups = get_boolean(filename, lnr, keyword, &line);
1712 get_eol(filename, lnr, keyword, &line);
1713 }
1714 else if (strcasecmp(keyword, "nss_getgrent_skipmembers") == 0)
1715 {
1716 cfg->nss_getgrent_skipmembers = get_boolean(filename, lnr, keyword, &line);
1717 get_eol(filename, lnr, keyword, &line);
1718 }
1719 else if (strcasecmp(keyword, "nss_disable_enumeration") == 0)
1720 {
1721 cfg->nss_disable_enumeration = get_boolean(filename, lnr, keyword, &line);
1722 get_eol(filename, lnr, keyword, &line);
1723 }
1724 else if (strcasecmp(keyword, "validnames") == 0)
1725 {
1726 handle_validnames(filename, lnr, keyword, line, cfg);
1727 }
1728 else if (strcasecmp(keyword, "ignorecase") == 0)
1729 {
1730 cfg->ignorecase = get_boolean(filename, lnr, keyword, &line);
1731 get_eol(filename, lnr, keyword, &line);
1732 }
1733 else if (strcasecmp(keyword, "pam_authc_search") == 0)
1734 {
1735 handle_pam_authc_search(filename, lnr, keyword, line, cfg);
1736 }
1737 else if (strcasecmp(keyword, "pam_authz_search") == 0)
1738 {
1739 handle_pam_authz_search(filename, lnr, keyword, line, cfg);
1740 }
1741 else if (strcasecmp(keyword, "pam_password_prohibit_message") == 0)
1742 {
1743 handle_pam_password_prohibit_message(filename, lnr, keyword, line, cfg);
1744 }
1745 else if (strcasecmp(keyword, "reconnect_invalidate") == 0)
1746 {
1747 handle_reconnect_invalidate(filename, lnr, keyword, line, cfg);
1748 }
1749 else if (strcasecmp(keyword, "cache") == 0)
1750 {
1751 handle_cache(filename, lnr, keyword, line, cfg);
1752 }
1753 #ifdef ENABLE_CONFIGFILE_CHECKING
1754 /* fallthrough */
1755 else
1756 {
1757 log_log(LOG_ERR, "%s:%d: unknown keyword: '%s'", filename, lnr, keyword);
1758 exit(EXIT_FAILURE);
1759 }
1760 #endif
1761 }
1762 /* we're done reading file, close */
1763 fclose(fp);
1764 }
1765
1766 #ifdef NSLCD_BINDPW_PATH
1767 static void bindpw_read(const char *filename, struct ldap_config *cfg)
1768 {
1769 FILE *fp;
1770 char linebuf[MAX_LINE_LENGTH];
1771 int i;
1772 /* open config file */
1773 errno = 0;
1774 if ((fp = fopen(filename, "r")) == NULL)
1775 {
1776 if (errno == ENOENT)
1777 {
1778 log_log(LOG_DEBUG, "no bindpw file (%s)", filename);
1779 return; /* ignore */
1780 }
1781 else
1782 {
1783 log_log(LOG_ERR, "cannot open bindpw file (%s): %s",
1784 filename, strerror(errno));
1785 exit(EXIT_FAILURE);
1786 }
1787 }
1788 /* check permissions */
1789 check_permissions(filename, NULL);
1790 /* read the first line */
1791 if (fgets(linebuf, sizeof(linebuf), fp) == NULL)
1792 {
1793 log_log(LOG_ERR, "%s: error reading first line", filename);
1794 exit(EXIT_FAILURE);
1795 }
1796 /* chop the last char off and save the rest as bindpw */
1797 i = (int)strlen(linebuf);
1798 if ((i <= 0) || (linebuf[i - 1] != '\n'))
1799 {
1800 log_log(LOG_ERR, "%s:1: line too long or missing newline", filename);
1801 exit(EXIT_FAILURE);
1802 }
1803 linebuf[i - 1] = '\0';
1804 if (strlen(linebuf) == 0)
1805 {
1806 log_log(LOG_ERR, "%s:1: the password is empty", filename);
1807 exit(EXIT_FAILURE);
1808 }
1809 cfg->bindpw = strdup(linebuf);
1810 /* check if there is no more data in the file */
1811 if (fgets(linebuf, sizeof(linebuf), fp) != NULL)
1812 {
1813 log_log(LOG_ERR, "%s:2: there is more than one line in the bindpw file",
1814 filename);
1815 exit(EXIT_FAILURE);
1816 }
1817 fclose(fp);
1818 }
1819 #endif /* NSLCD_BINDPW_PATH */
1820
1821 /* dump configuration */
1822 static void cfg_dump(void)
1823 {
1824 int i;
1825 #ifdef LDAP_OPT_X_TLS
1826 int rc;
1827 #endif /* LDAP_OPT_X_TLS */
1828 enum ldap_map_selector map;
1829 char *str;
1830 const char **strp;
1831 char buffer[1024];
1832 int *scopep;
1833 log_log(LOG_DEBUG, "CFG: threads %d", nslcd_cfg->threads);
1834 if (nslcd_cfg->uidname != NULL)
1835 log_log(LOG_DEBUG, "CFG: uid %s", nslcd_cfg->uidname);
1836 else if (nslcd_cfg->uid != NOUID)
1837 log_log(LOG_DEBUG, "CFG: uid %lu", (unsigned long int)nslcd_cfg->uid);
1838 else
1839 log_log(LOG_DEBUG, "CFG: # uid not set");
1840 if (nslcd_cfg->gid != NOGID)
1841 log_log(LOG_DEBUG, "CFG: gid %lu", (unsigned long int)nslcd_cfg->gid);
1842 else
1843 log_log(LOG_DEBUG, "CFG: # gid not set");
1844 log_log_config();
1845 for (i = 0; i < (NSS_LDAP_CONFIG_MAX_URIS + 1); i++)
1846 if (nslcd_cfg->uris[i].uri != NULL)
1847 log_log(LOG_DEBUG, "CFG: uri %s", nslcd_cfg->uris[i].uri);
1848 log_log(LOG_DEBUG, "CFG: ldap_version %d", nslcd_cfg->ldap_version);
1849 if (nslcd_cfg->binddn != NULL)
1850 log_log(LOG_DEBUG, "CFG: binddn %s", nslcd_cfg->binddn);
1851 if (nslcd_cfg->bindpw != NULL)
1852 log_log(LOG_DEBUG, "CFG: bindpw ***");
1853 if (nslcd_cfg->rootpwmoddn != NULL)
1854 log_log(LOG_DEBUG, "CFG: rootpwmoddn %s", nslcd_cfg->rootpwmoddn);
1855 if (nslcd_cfg->rootpwmodpw != NULL)
1856 log_log(LOG_DEBUG, "CFG: rootpwmodpw ***");
1857 if (nslcd_cfg->sasl_mech != NULL)
1858 log_log(LOG_DEBUG, "CFG: sasl_mech %s", nslcd_cfg->sasl_mech);
1859 if (nslcd_cfg->sasl_realm != NULL)
1860 log_log(LOG_DEBUG, "CFG: sasl_realm %s", nslcd_cfg->sasl_realm);
1861 if (nslcd_cfg->sasl_authcid != NULL)
1862 log_log(LOG_DEBUG, "CFG: sasl_authcid %s", nslcd_cfg->sasl_authcid);
1863 if (nslcd_cfg->sasl_authzid != NULL)
1864 log_log(LOG_DEBUG, "CFG: sasl_authzid %s", nslcd_cfg->sasl_authzid);
1865 if (nslcd_cfg->sasl_secprops != NULL)
1866 log_log(LOG_DEBUG, "CFG: sasl_secprops %s", nslcd_cfg->sasl_secprops);
1867 #ifdef LDAP_OPT_X_SASL_NOCANON
1868 if (nslcd_cfg->sasl_canonicalize >= 0)
1869 log_log(LOG_DEBUG, "CFG: sasl_canonicalize %s", print_boolean(nslcd_cfg->sasl_canonicalize));
1870 #endif /* LDAP_OPT_X_SASL_NOCANON */
1871 str = getenv("KRB5CCNAME");
1872 if (str != NULL)
1873 log_log(LOG_DEBUG, "CFG: krb5_ccname %s", str);
1874 for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
1875 if (nslcd_cfg->bases[i] != NULL)
1876 log_log(LOG_DEBUG, "CFG: base %s", nslcd_cfg->bases[i][0] == '\0' ? "\"\"" : nslcd_cfg->bases[i]);
1877 for (map = LM_ALIASES; map < LM_NONE; map++)
1878 {
1879 strp = base_get_var(map);
1880 if (strp != NULL)
1881 for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
1882 if (strp[i] != NULL)
1883 log_log(LOG_DEBUG, "CFG: base %s %s", print_map(map), strp[i][0] == '\0' ? "\"\"" : strp[i]);
1884 }
1885 log_log(LOG_DEBUG, "CFG: scope %s", print_scope(nslcd_cfg->scope));
1886 for (map = LM_ALIASES; map < LM_NONE; map++)
1887 {
1888 scopep = scope_get_var(map);
1889 if ((scopep != NULL) && (*scopep != LDAP_SCOPE_DEFAULT))
1890 log_log(LOG_DEBUG, "CFG: scope %s %s", print_map(map), print_scope(*scopep));
1891 }
1892 log_log(LOG_DEBUG, "CFG: deref %s", print_deref(nslcd_cfg->deref));
1893 log_log(LOG_DEBUG, "CFG: referrals %s", print_boolean(nslcd_cfg->referrals));
1894 for (map = LM_ALIASES; map < LM_NONE; map++)
1895 {
1896 strp = filter_get_var(map);
1897 if ((strp != NULL) && (*strp != NULL))
1898 log_log(LOG_DEBUG, "CFG: filter %s %s", print_map(map), *strp);
1899 }
1900 #define LOG_ATTMAP(map, mapl, att) \
1901 if (strcmp(attmap_##mapl##_##att, __STRING(att)) != 0) \
1902 log_log(LOG_DEBUG, "CFG: map %s %s %s", \
1903 print_map(map), __STRING(att), attmap_##mapl##_##att);
1904 LOG_ATTMAP(LM_ALIASES, alias, cn);
1905 LOG_ATTMAP(LM_ALIASES, alias, rfc822MailMember);
1906 LOG_ATTMAP(LM_ETHERS, ether, cn);
1907 LOG_ATTMAP(LM_ETHERS, ether, macAddress);
1908 LOG_ATTMAP(LM_GROUP, group, cn);
1909 LOG_ATTMAP(LM_GROUP, group, userPassword);
1910 LOG_ATTMAP(LM_GROUP, group, gidNumber);
1911 LOG_ATTMAP(LM_GROUP, group, memberUid);
1912 LOG_ATTMAP(LM_GROUP, group, member);
1913 LOG_ATTMAP(LM_HOSTS, host, cn);
1914 LOG_ATTMAP(LM_HOSTS, host, ipHostNumber);
1915 LOG_ATTMAP(LM_NETGROUP, netgroup, cn);
1916 LOG_ATTMAP(LM_NETGROUP, netgroup, nisNetgroupTriple);
1917 LOG_ATTMAP(LM_NETGROUP, netgroup, memberNisNetgroup);
1918 LOG_ATTMAP(LM_NETWORKS, network, cn);
1919 LOG_ATTMAP(LM_NETWORKS, network, ipNetworkNumber);
1920 LOG_ATTMAP(LM_PASSWD, passwd, uid);
1921 LOG_ATTMAP(LM_PASSWD, passwd, userPassword);
1922 LOG_ATTMAP(LM_PASSWD, passwd, uidNumber);
1923 LOG_ATTMAP(LM_PASSWD, passwd, gidNumber);
1924 LOG_ATTMAP(LM_PASSWD, passwd, gecos);
1925 LOG_ATTMAP(LM_PASSWD, passwd, homeDirectory);
1926 LOG_ATTMAP(LM_PASSWD, passwd, loginShell);
1927 LOG_ATTMAP(LM_PROTOCOLS, protocol, cn);
1928 LOG_ATTMAP(LM_PROTOCOLS, protocol, ipProtocolNumber);
1929 LOG_ATTMAP(LM_RPC, rpc, cn);
1930 LOG_ATTMAP(LM_RPC, rpc, oncRpcNumber);
1931 LOG_ATTMAP(LM_SERVICES, service, cn);
1932 LOG_ATTMAP(LM_SERVICES, service, ipServicePort);
1933 LOG_ATTMAP(LM_SERVICES, service, ipServiceProtocol);
1934 LOG_ATTMAP(LM_SHADOW, shadow, uid);
1935 LOG_ATTMAP(LM_SHADOW, shadow, userPassword);
1936 LOG_ATTMAP(LM_SHADOW, shadow, shadowLastChange);
1937 LOG_ATTMAP(LM_SHADOW, shadow, shadowMin);
1938 LOG_ATTMAP(LM_SHADOW, shadow, shadowMax);
1939 LOG_ATTMAP(LM_SHADOW, shadow, shadowWarning);
1940 LOG_ATTMAP(LM_SHADOW, shadow, shadowInactive);
1941 LOG_ATTMAP(LM_SHADOW, shadow, shadowExpire);
1942 LOG_ATTMAP(LM_SHADOW, shadow, shadowFlag);
1943 #if defined(HAVE_LDAP_SASL_BIND) && defined(LDAP_SASL_SIMPLE)
1944 log_log(LOG_DEBUG, "CFG: pam_authc_ppolicy %s", print_boolean(nslcd_cfg->pam_authc_ppolicy));
1945 #endif
1946 log_log(LOG_DEBUG, "CFG: bind_timelimit %d", nslcd_cfg->bind_timelimit);
1947 log_log(LOG_DEBUG, "CFG: timelimit %d", nslcd_cfg->timelimit);
1948 log_log(LOG_DEBUG, "CFG: idle_timelimit %d", nslcd_cfg->idle_timelimit);
1949 log_log(LOG_DEBUG, "CFG: reconnect_sleeptime %d", nslcd_cfg->reconnect_sleeptime);
1950 log_log(LOG_DEBUG, "CFG: reconnect_retrytime %d", nslcd_cfg->reconnect_retrytime);
1951 #ifdef LDAP_OPT_X_TLS
1952 log_log(LOG_DEBUG, "CFG: ssl %s", print_ssl(nslcd_cfg->ssl));
1953 rc = ldap_get_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
1954 if (rc != LDAP_SUCCESS)
1955 log_log(LOG_DEBUG, "CFG: # tls_reqcert ERROR: %s", ldap_err2string(rc));
1956 else
1957 log_log(LOG_DEBUG, "CFG: tls_reqcert %s", print_tls_reqcert(i));
1958 #define LOG_LDAP_OPT_STRING(cfg, option) \
1959 str = NULL; \
1960 rc = ldap_get_option(NULL, option, &str); \
1961 if (rc != LDAP_SUCCESS) \
1962 log_log(LOG_DEBUG, "CFG: # %s ERROR: %s", cfg, ldap_err2string(rc)); \
1963 else if ((str != NULL) && (*str != '\0')) \
1964 log_log(LOG_DEBUG, "CFG: %s %s", cfg, str); \
1965 if (str != NULL) \
1966 ldap_memfree(str);
1967 LOG_LDAP_OPT_STRING("tls_cacertdir", LDAP_OPT_X_TLS_CACERTDIR);
1968 LOG_LDAP_OPT_STRING("tls_cacertfile", LDAP_OPT_X_TLS_CACERTFILE);
1969 LOG_LDAP_OPT_STRING("tls_randfile", LDAP_OPT_X_TLS_RANDOM_FILE);
1970 LOG_LDAP_OPT_STRING("tls_ciphers", LDAP_OPT_X_TLS_CIPHER_SUITE);
1971 LOG_LDAP_OPT_STRING("tls_cert", LDAP_OPT_X_TLS_CERTFILE);
1972 LOG_LDAP_OPT_STRING("tls_key", LDAP_OPT_X_TLS_KEYFILE);
1973 #ifdef LDAP_OPT_X_TLS_REQUIRE_SAN
1974 rc = ldap_get_option(NULL, LDAP_OPT_X_TLS_REQUIRE_SAN, &i);
1975 if (rc != LDAP_SUCCESS)
1976 log_log(LOG_DEBUG, "CFG: # tls_reqsan ERROR: %s", ldap_err2string(rc));
1977 else
1978 log_log(LOG_DEBUG, "CFG: tls_reqsan %s", print_tls_reqcert(i));
1979 #endif /* LDAP_OPT_X_TLS_REQUIRE_SAN */
1980 #ifdef LDAP_OPT_X_TLS_CRLCHECK
1981 rc = ldap_get_option(NULL, LDAP_OPT_X_TLS_CRLCHECK, &i);
1982 if (rc != LDAP_SUCCESS)
1983 log_log(LOG_DEBUG, "CFG: # tls_crlcheck ERROR: %s", ldap_err2string(rc));
1984 else
1985 log_log(LOG_DEBUG, "CFG: tls_crlcheck %s", print_tls_crlcheck(i));
1986 #endif /* LDAP_OPT_X_TLS_CRLCHECK */
1987 #endif /* LDAP_OPT_X_TLS */
1988 log_log(LOG_DEBUG, "CFG: pagesize %d", nslcd_cfg->pagesize);
1989 if (nslcd_cfg->nss_initgroups_ignoreusers != NULL)
1990 {
1991 /* allocate memory for a comma-separated list */
1992 strp = set_tolist(nslcd_cfg->nss_initgroups_ignoreusers);
1993 if (strp == NULL)
1994 {
1995 log_log(LOG_CRIT, "malloc() failed to allocate memory");
1996 exit(EXIT_FAILURE);
1997 }
1998 /* turn the set into a comma-separated list */
1999 buffer[0] = '\0';
2000 for (i = 0; strp[i] != NULL; i++)
2001 {
2002 if (i > 0)
2003 strncat(buffer, ",", sizeof(buffer) - 1 - strlen(buffer));
2004 strncat(buffer, strp[i], sizeof(buffer) - 1 - strlen(buffer));
2005 }
2006 free(strp);
2007 if (strlen(buffer) >= (sizeof(buffer) - 4))
2008 strcpy(buffer + sizeof(buffer) - 4, "...");
2009 log_log(LOG_DEBUG, "CFG: nss_initgroups_ignoreusers %s", buffer);
2010 }
2011 log_log(LOG_DEBUG, "CFG: nss_min_uid %lu", (unsigned long int)nslcd_cfg->nss_min_uid);
2012 log_log(LOG_DEBUG, "CFG: nss_uid_offset %lu", (unsigned long int)nslcd_cfg->nss_uid_offset);
2013 log_log(LOG_DEBUG, "CFG: nss_gid_offset %lu", (unsigned long int)nslcd_cfg->nss_gid_offset);
2014 log_log(LOG_DEBUG, "CFG: nss_nested_groups %s", print_boolean(nslcd_cfg->nss_nested_groups));
2015 log_log(LOG_DEBUG, "CFG: nss_getgrent_skipmembers %s", print_boolean(nslcd_cfg->nss_getgrent_skipmembers));
2016 log_log(LOG_DEBUG, "CFG: nss_disable_enumeration %s", print_boolean(nslcd_cfg->nss_disable_enumeration));
2017 log_log(LOG_DEBUG, "CFG: validnames %s", nslcd_cfg->validnames_str);
2018 log_log(LOG_DEBUG, "CFG: ignorecase %s", print_boolean(nslcd_cfg->ignorecase));
2019 log_log(LOG_DEBUG, "CFG: pam_authc_search %s", nslcd_cfg->pam_authc_search);
2020 for (i = 0; i < NSS_LDAP_CONFIG_MAX_AUTHZ_SEARCHES; i++)
2021 if (nslcd_cfg->pam_authz_searches[i] != NULL)
2022 log_log(LOG_DEBUG, "CFG: pam_authz_search %s", nslcd_cfg->pam_authz_searches[i]);
2023 if (nslcd_cfg->pam_password_prohibit_message != NULL)
2024 log_log(LOG_DEBUG, "CFG: pam_password_prohibit_message \"%s\"", nslcd_cfg->pam_password_prohibit_message);
2025 /* build a comma-separated list */
2026 buffer[0] = '\0';
2027 for (i = 0; i < LM_NONE ; i++)
2028 if (nslcd_cfg->reconnect_invalidate[i])
2029 {
2030 if (buffer[0] != '\0')
2031 strncat(buffer, ",", sizeof(buffer) - 1 - strlen(buffer));
2032 strncat(buffer, print_map(i), sizeof(buffer) - 1 - strlen(buffer));
2033 }
2034 if (buffer[0] != '\0')
2035 log_log(LOG_DEBUG, "CFG: reconnect_invalidate %s", buffer);
2036 print_time(nslcd_cfg->cache_dn2uid_positive, buffer, sizeof(buffer) / 2);
2037 print_time(nslcd_cfg->cache_dn2uid_positive, buffer + (sizeof(buffer) / 2), sizeof(buffer) / 2);
2038 log_log(LOG_DEBUG, "CFG: cache dn2uid %s %s", buffer, buffer + (sizeof(buffer) / 2));
2039 }
2040
2041 void cfg_init(const char *fname)
2042 {
2043 #ifdef LDAP_OPT_X_TLS
2044 int i;
2045 #endif /* LDAP_OPT_X_TLS */
2046 /* check if we were called before */
2047 if (nslcd_cfg != NULL)
2048 {
2049 log_log(LOG_CRIT, "cfg_init() may only be called once");
2050 exit(EXIT_FAILURE);
2051 }
2052 /* allocate the memory (this memory is not freed anywhere) */
2053 nslcd_cfg = (struct ldap_config *)malloc(sizeof(struct ldap_config));
2054 if (nslcd_cfg == NULL)
2055 {
2056 log_log(LOG_CRIT, "malloc() failed to allocate memory");
2057 exit(EXIT_FAILURE);
2058 }
2059 /* clear configuration */
2060 cfg_defaults(nslcd_cfg);
2061 /* read configfile */
2062 cfg_read(fname, nslcd_cfg);
2063 #ifdef NSLCD_BINDPW_PATH
2064 bindpw_read(NSLCD_BINDPW_PATH, nslcd_cfg);
2065 #endif /* NSLCD_BINDPW_PATH */
2066 /* do some sanity checks */
2067 if (nslcd_cfg->uris[0].uri == NULL)
2068 {
2069 log_log(LOG_ERR, "no URIs defined in config");
2070 exit(EXIT_FAILURE);
2071 }
2072 /* if ssl is on each URI should start with ldaps */
2073 #ifdef LDAP_OPT_X_TLS
2074 if (nslcd_cfg->ssl == SSL_LDAPS)
2075 {
2076 for (i = 0; nslcd_cfg->uris[i].uri != NULL; i++)
2077 {
2078 if (strncasecmp(nslcd_cfg->uris[i].uri, "ldaps://", 8) != 0)
2079 log_log(LOG_WARNING, "%s doesn't start with ldaps:// and \"ssl on\" is specified",
2080 nslcd_cfg->uris[i].uri);
2081 }
2082 }
2083 /* TODO: check that if some tls options are set the ssl option should be set to on (just warn) */
2084 #endif /* LDAP_OPT_X_TLS */
2085 /* if basedn is not yet set, get if from the rootDSE */
2086 if (nslcd_cfg->bases[0] == NULL)
2087 nslcd_cfg->bases[0] = get_base_from_rootdse();
2088 /* TODO: handle the case gracefully when no LDAP server is available yet */
2089 /* dump configuration */
2090 cfg_dump();
2091 /* initialise all database modules */
2092 alias_init();
2093 ether_init();
2094 group_init();
2095 host_init();
2096 netgroup_init();
2097 network_init();
2098 passwd_init();
2099 protocol_init();
2100 rpc_init();
2101 service_init();
2102 shadow_init();
2103 }