xref: /openbsd/usr.sbin/ypldap/ldapclient.c (revision b245f644)
1 /* $OpenBSD: ldapclient.c,v 1.45 2022/08/22 10:10:59 jmatthew Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org>
5  * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/tree.h>
24 
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 
28 #include <netdb.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <signal.h>
32 #include <event.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <limits.h>
40 
41 #include "aldap.h"
42 #include "log.h"
43 #include "ypldap.h"
44 
45 void    client_sig_handler(int, short, void *);
46 void	client_dispatch_dns(int, short, void *);
47 void    client_dispatch_parent(int, short, void *);
48 void    client_shutdown(void);
49 void    client_connect(int, short, void *);
50 void    client_configure(struct env *);
51 void    client_periodic_update(int, short, void *);
52 int	client_build_req(struct idm *, struct idm_req *, struct aldap_message *,
53 	    int, int);
54 int	client_search_idm(struct env *, struct idm *, struct aldap *,
55 	    char **, char *, int, int, enum imsg_type);
56 int	client_try_idm(struct env *, struct idm *);
57 void	client_addr_init(struct idm *);
58 int	client_addr_free(struct idm *);
59 
60 struct aldap	*client_aldap_open(struct ypldap_addr_list *);
61 
62 /*
63  * dummy wrapper to provide aldap_init with its fd's.
64  */
65 struct aldap *
66 client_aldap_open(struct ypldap_addr_list *addr)
67 {
68 	int			 fd = -1;
69 	struct ypldap_addr	*p;
70 	struct aldap		*al;
71 
72 	TAILQ_FOREACH(p, addr, next) {
73 		char			 hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
74 		struct sockaddr		*sa = (struct sockaddr *)&p->ss;
75 
76 		if (getnameinfo(sa, SA_LEN(sa), hbuf, sizeof(hbuf), sbuf,
77 			sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV))
78 				errx(1, "could not get numeric hostname");
79 
80 		if ((fd = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
81 			return NULL;
82 
83 		if (connect(fd, sa, SA_LEN(sa)) == 0)
84 			break;
85 
86 		log_warn("connect to %s port %s failed", hbuf, sbuf);
87 		close(fd);
88 		fd = -1;
89 	}
90 
91 	if (fd == -1)
92 		return NULL;
93 
94 	al = aldap_init(fd);
95 	if (al == NULL)
96 		close(fd);
97 	return al;
98 }
99 
100 void
101 client_addr_init(struct idm *idm)
102 {
103         struct sockaddr_in      *sa_in;
104         struct sockaddr_in6     *sa_in6;
105         struct ypldap_addr      *h;
106 	int                     defport;
107 
108 	if (idm->idm_port != 0)
109 		defport = idm->idm_port;
110 	else if (idm->idm_flags & F_SSL)
111 		defport = LDAPS_PORT;
112 	else
113 		defport = LDAP_PORT;
114 
115 	TAILQ_FOREACH(h, &idm->idm_addr, next) {
116                 switch (h->ss.ss_family) {
117                 case AF_INET:
118                         sa_in = (struct sockaddr_in *)&h->ss;
119                         if (ntohs(sa_in->sin_port) == 0)
120                                 sa_in->sin_port = htons(defport);
121                         idm->idm_state = STATE_DNS_DONE;
122                         break;
123                 case AF_INET6:
124                         sa_in6 = (struct sockaddr_in6 *)&h->ss;
125                         if (ntohs(sa_in6->sin6_port) == 0)
126                                 sa_in6->sin6_port = htons(defport);
127                         idm->idm_state = STATE_DNS_DONE;
128                         break;
129                 default:
130                         fatalx("king bula sez: wrong AF in client_addr_init");
131                         /* not reached */
132                 }
133         }
134 }
135 
136 int
137 client_addr_free(struct idm *idm)
138 {
139         struct ypldap_addr	*h;
140 
141 	while (!TAILQ_EMPTY(&idm->idm_addr)) {
142 		h = TAILQ_FIRST(&idm->idm_addr);
143 		TAILQ_REMOVE(&idm->idm_addr, h, next);
144 		free(h);
145 	}
146 
147 	return (0);
148 }
149 
150 void
151 client_sig_handler(int sig, short event, void *p)
152 {
153 	switch (sig) {
154 	case SIGINT:
155 	case SIGTERM:
156 		client_shutdown();
157 		break;
158 	default:
159 		fatalx("unexpected signal");
160 	}
161 }
162 
163 void
164 client_dispatch_dns(int fd, short events, void *p)
165 {
166 	struct imsg		 imsg;
167 	u_int16_t		 dlen;
168 	u_char			*data;
169 	struct ypldap_addr	*h;
170 	int			 n, wait_cnt = 0;
171 	struct idm		*idm;
172 	int			 shut = 0;
173 
174 	struct env		*env = p;
175 	struct imsgev		*iev = env->sc_iev_dns;
176 	struct imsgbuf		*ibuf = &iev->ibuf;
177 
178 	if ((events & (EV_READ | EV_WRITE)) == 0)
179 		fatalx("unknown event");
180 
181 	if (events & EV_READ) {
182 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
183 			fatal("imsg_read error");
184 		if (n == 0)
185 			shut = 1;
186 	}
187 	if (events & EV_WRITE) {
188 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
189 			fatal("msgbuf_write");
190 		if (n == 0)
191 			shut = 1;
192 		goto done;
193 	}
194 
195 	for (;;) {
196 		if ((n = imsg_get(ibuf, &imsg)) == -1)
197 			fatal("client_dispatch_dns: imsg_get error");
198 		if (n == 0)
199 			break;
200 
201 		switch (imsg.hdr.type) {
202 		case IMSG_HOST_DNS:
203 			TAILQ_FOREACH(idm, &env->sc_idms, idm_entry)
204 				if (idm->idm_id == imsg.hdr.peerid)
205 					break;
206 			if (idm == NULL) {
207 				log_warnx("IMSG_HOST_DNS with invalid peerID");
208 				break;
209 			}
210 			if (!TAILQ_EMPTY(&idm->idm_addr)) {
211 				log_warnx("IMSG_HOST_DNS but addrs set!");
212 				break;
213 			}
214 
215 			dlen = imsg.hdr.len - IMSG_HEADER_SIZE;
216 			if (dlen == 0) {	/* no data -> temp error */
217 				idm->idm_state = STATE_DNS_TEMPFAIL;
218 				break;
219 			}
220 
221 			data = (u_char *)imsg.data;
222 			while (dlen >= sizeof(struct sockaddr_storage)) {
223 				if ((h = calloc(1, sizeof(*h))) == NULL)
224 					fatal(NULL);
225 				memcpy(&h->ss, data, sizeof(h->ss));
226 				TAILQ_INSERT_HEAD(&idm->idm_addr, h, next);
227 
228 				data += sizeof(h->ss);
229 				dlen -= sizeof(h->ss);
230 			}
231 			if (dlen != 0)
232 				fatalx("IMSG_HOST_DNS: dlen != 0");
233 
234 			client_addr_init(idm);
235 
236 			break;
237 		default:
238 			break;
239 		}
240 		imsg_free(&imsg);
241 	}
242 
243 	TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
244 		if (client_try_idm(env, idm) == -1)
245 			idm->idm_state = STATE_LDAP_FAIL;
246 
247 		if (idm->idm_state < STATE_LDAP_DONE)
248 			wait_cnt++;
249 	}
250 	if (wait_cnt == 0)
251 		imsg_compose_event(env->sc_iev, IMSG_END_UPDATE, 0, 0, -1,
252 		    NULL, 0);
253 
254 done:
255 	if (!shut)
256 		imsg_event_add(iev);
257 	else {
258 		/* this pipe is dead, so remove the event handler */
259 		event_del(&iev->ev);
260 		event_loopexit(NULL);
261 	}
262 }
263 
264 void
265 client_dispatch_parent(int fd, short events, void *p)
266 {
267 	int			 n;
268 	int			 shut = 0;
269 	struct imsg		 imsg;
270 	struct env		*env = p;
271 	struct imsgev		*iev = env->sc_iev;
272 	struct imsgbuf		*ibuf = &iev->ibuf;
273 
274 	if ((events & (EV_READ | EV_WRITE)) == 0)
275 		fatalx("unknown event");
276 
277 	if (events & EV_READ) {
278 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
279 			fatal("imsg_read error");
280 		if (n == 0)
281 			shut = 1;
282 	}
283 	if (events & EV_WRITE) {
284 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
285 			fatal("msgbuf_write");
286 		if (n == 0)
287 			shut = 1;
288 		goto done;
289 	}
290 
291 	for (;;) {
292 		if ((n = imsg_get(ibuf, &imsg)) == -1)
293 			fatal("client_dispatch_parent: imsg_get error");
294 		if (n == 0)
295 			break;
296 
297 		switch (imsg.hdr.type) {
298 		case IMSG_CONF_START: {
299 			struct env	params;
300 
301 			if (env->sc_flags & F_CONFIGURING) {
302 				log_warnx("configuration already in progress");
303 				break;
304 			}
305 			memcpy(&params, imsg.data, sizeof(params));
306 			log_debug("configuration starting");
307 			env->sc_flags |= F_CONFIGURING;
308 			purge_config(env);
309 			memcpy(&env->sc_conf_tv, &params.sc_conf_tv,
310 			    sizeof(env->sc_conf_tv));
311 			env->sc_flags |= params.sc_flags;
312 			break;
313 		}
314 		case IMSG_CONF_IDM: {
315 			struct idm	*idm;
316 
317 			if (!(env->sc_flags & F_CONFIGURING))
318 				break;
319 			if ((idm = calloc(1, sizeof(*idm))) == NULL)
320 				fatal(NULL);
321 			memcpy(idm, imsg.data, sizeof(*idm));
322 			idm->idm_env = env;
323 			TAILQ_INSERT_TAIL(&env->sc_idms, idm, idm_entry);
324 			break;
325 		}
326 		case IMSG_CONF_END:
327 			env->sc_flags &= ~F_CONFIGURING;
328 			log_debug("applying configuration");
329 			client_configure(env);
330 			break;
331 		default:
332 			log_debug("client_dispatch_parent: unexpect imsg %d",
333 			    imsg.hdr.type);
334 
335 			break;
336 		}
337 		imsg_free(&imsg);
338 	}
339 
340 done:
341 	if (!shut)
342 		imsg_event_add(iev);
343 	else {
344 		/* this pipe is dead, so remove the event handler */
345 		event_del(&iev->ev);
346 		event_loopexit(NULL);
347 	}
348 }
349 
350 void
351 client_shutdown(void)
352 {
353 	log_info("ldap client exiting");
354 	_exit(0);
355 }
356 
357 pid_t
358 ldapclient(int pipe_main2client[2])
359 {
360 	pid_t            pid;
361 	int              pipe_dns[2];
362 	struct passwd	*pw;
363 	struct event	 ev_sigint;
364 	struct event	 ev_sigterm;
365 	struct env	 env;
366 
367 	switch (pid = fork()) {
368 	case -1:
369 		fatal("cannot fork");
370 		break;
371 	case 0:
372 		break;
373 	default:
374 		return (pid);
375 	}
376 
377 	memset(&env, 0, sizeof(env));
378 	TAILQ_INIT(&env.sc_idms);
379 
380 	if ((pw = getpwnam(YPLDAP_USER)) == NULL)
381 		fatal("getpwnam");
382 
383 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_dns) == -1)
384 		fatal("socketpair");
385 	ypldap_dns(pipe_dns, pw);
386 	close(pipe_dns[1]);
387 
388 #ifndef DEBUG
389 	if (chroot(pw->pw_dir) == -1)
390 		fatal("chroot");
391 	if (chdir("/") == -1)
392 		fatal("chdir");
393 #else
394 #warning disabling chrooting in DEBUG mode
395 #endif
396 	setproctitle("ldap client");
397 	ypldap_process = PROC_CLIENT;
398 	log_procname = log_procnames[ypldap_process];
399 
400 #ifndef DEBUG
401 	if (setgroups(1, &pw->pw_gid) ||
402 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
403 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
404 		fatal("cannot drop privileges");
405 #else
406 #warning disabling privilege revocation in DEBUG mode
407 #endif
408 
409 	if (pledge("stdio inet", NULL) == -1)
410 		fatal("pledge");
411 
412 	event_init();
413 	signal(SIGPIPE, SIG_IGN);
414 	signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
415 	signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
416 	signal_add(&ev_sigint, NULL);
417 	signal_add(&ev_sigterm, NULL);
418 
419 	close(pipe_main2client[0]);
420 	if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL)
421 		fatal(NULL);
422 	if ((env.sc_iev_dns = calloc(1, sizeof(*env.sc_iev_dns))) == NULL)
423 		fatal(NULL);
424 
425 	env.sc_iev->events = EV_READ;
426 	env.sc_iev->data = &env;
427 	imsg_init(&env.sc_iev->ibuf, pipe_main2client[1]);
428 	env.sc_iev->handler = client_dispatch_parent;
429 	event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events,
430 	    env.sc_iev->handler, &env);
431 	event_add(&env.sc_iev->ev, NULL);
432 
433 	env.sc_iev_dns->events = EV_READ;
434 	env.sc_iev_dns->data = &env;
435 	imsg_init(&env.sc_iev_dns->ibuf, pipe_dns[0]);
436 	env.sc_iev_dns->handler = client_dispatch_dns;
437 	event_set(&env.sc_iev_dns->ev, env.sc_iev_dns->ibuf.fd,
438 	    env.sc_iev_dns->events, env.sc_iev_dns->handler, &env);
439 	event_add(&env.sc_iev_dns->ev, NULL);
440 
441 	event_dispatch();
442 	client_shutdown();
443 
444 	return (0);
445 
446 }
447 
448 int
449 client_build_req(struct idm *idm, struct idm_req *ir, struct aldap_message *m,
450     int min_attr, int max_attr)
451 {
452 	struct aldap_stringset	*ldap_attrs;
453 	int	 i;
454 	size_t	 k;
455 
456 	memset(ir, 0, sizeof(*ir));
457 	for (i = min_attr; i < max_attr; i++) {
458 		if (idm->idm_flags & F_FIXED_ATTR(i)) {
459 			if (strlcat(ir->ir_line, idm->idm_attrs[i],
460 			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
461 				/*
462 				 * entry yields a line > 1024, trash it.
463 				 */
464 				return (-1);
465 
466 			if (i == ATTR_UID) {
467 				ir->ir_key.ik_uid = strtonum(
468 				    idm->idm_attrs[i], 0,
469 				    UID_MAX, NULL);
470 			} else if (i == ATTR_GR_GID) {
471 				ir->ir_key.ik_gid = strtonum(
472 				    idm->idm_attrs[i], 0,
473 				    GID_MAX, NULL);
474 			}
475 		} else if (idm->idm_list & F_LIST(i)) {
476 			aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs);
477 			for (k = 0; k >= 0 && ldap_attrs && k < ldap_attrs->len; k++) {
478 				/* XXX: Fail when attributes have illegal characters e.g. ',' */
479 				if (strlcat(ir->ir_line,
480 				    ldap_attrs->str[k].ostr_val,
481 				    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
482 					continue;
483 				if (k + 1 < ldap_attrs->len)
484 					if (strlcat(ir->ir_line, ",",
485 						    sizeof(ir->ir_line))
486 					    >= sizeof(ir->ir_line)) {
487 						aldap_free_attr(ldap_attrs);
488 						return (-1);
489 					}
490 			}
491 			aldap_free_attr(ldap_attrs);
492 		} else {
493 			if (aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs) == -1)
494 				return (-1);
495 			if (strlcat(ir->ir_line, ldap_attrs->str[0].ostr_val,
496 			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) {
497 				aldap_free_attr(ldap_attrs);
498 				return (-1);
499 			}
500 			if (i == ATTR_UID) {
501 				ir->ir_key.ik_uid = strtonum(
502 				    ldap_attrs->str[0].ostr_val, 0, UID_MAX,
503 				    NULL);
504 			} else if (i == ATTR_GR_GID) {
505 				ir->ir_key.ik_uid = strtonum(
506 				    ldap_attrs->str[0].ostr_val, 0, GID_MAX,
507 				    NULL);
508 			}
509 			aldap_free_attr(ldap_attrs);
510 		}
511 
512 		if (i + 1 != max_attr)
513 			if (strlcat(ir->ir_line, ":",
514 			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
515 				return (-1);
516 	}
517 
518 	return (0);
519 }
520 
521 int
522 client_search_idm(struct env *env, struct idm *idm, struct aldap *al,
523     char **attrs, char *filter, int min_attr, int max_attr,
524     enum imsg_type type)
525 {
526 	struct idm_req		 ir;
527 	struct aldap_message	*m;
528 	struct aldap_page_control *pg = NULL;
529 	const char		*errstr;
530 	char			*dn;
531 
532 	dn = idm->idm_basedn;
533 	if (type == IMSG_GRP_ENTRY && idm->idm_groupdn[0] != '\0')
534 		dn = idm->idm_groupdn;
535 
536 	do {
537 		if (aldap_search(al, dn, LDAP_SCOPE_SUBTREE,
538 		    filter, attrs, 0, 0, 0, pg) == -1) {
539 			aldap_get_errno(al, &errstr);
540 			log_debug("%s", errstr);
541 			return (-1);
542 		}
543 
544 		if (pg != NULL) {
545 			aldap_freepage(pg);
546 			pg = NULL;
547 		}
548 
549 		while ((m = aldap_parse(al)) != NULL) {
550 			if (al->msgid != m->msgid) {
551 				goto fail;
552 			}
553 
554 			if (m->message_type == LDAP_RES_SEARCH_RESULT) {
555 				if (m->page != NULL && m->page->cookie_len != 0)
556 					pg = m->page;
557 				else
558 					pg = NULL;
559 
560 				aldap_freemsg(m);
561 				break;
562 			}
563 
564 			if (m->message_type != LDAP_RES_SEARCH_ENTRY) {
565 				goto fail;
566 			}
567 
568 			if (client_build_req(idm, &ir, m, min_attr, max_attr) == 0)
569 				imsg_compose_event(env->sc_iev, type, 0, 0, -1,
570 				    &ir, sizeof(ir));
571 
572 			aldap_freemsg(m);
573 		}
574 	} while (pg != NULL);
575 
576 	return (0);
577 
578 fail:
579 	aldap_freemsg(m);
580 	if (pg != NULL) {
581 		aldap_freepage(pg);
582 	}
583 
584 	return (-1);
585 }
586 
587 int
588 client_try_idm(struct env *env, struct idm *idm)
589 {
590 	const char		*where;
591 	char			*attrs[ATTR_MAX+1];
592 	int			 i, j;
593 	struct aldap_message	*m;
594 	struct aldap		*al;
595 
596 	where = "connect";
597 	if ((al = client_aldap_open(&idm->idm_addr)) == NULL)
598 		return (-1);
599 
600 	if (idm->idm_flags & F_STARTTLS) {
601 		log_debug("requesting starttls");
602 		where = "starttls";
603 		if (aldap_req_starttls(al) == -1)
604 			goto bad;
605 
606 		where = "parsing";
607 		if ((m = aldap_parse(al)) == NULL)
608 			goto bad;
609 		where = "verifying msgid";
610 		if (al->msgid != m->msgid) {
611 			aldap_freemsg(m);
612 			goto bad;
613 		}
614 		where = "starttls result";
615 		if (aldap_get_resultcode(m) != LDAP_SUCCESS) {
616 			aldap_freemsg(m);
617 			goto bad;
618 		}
619 		aldap_freemsg(m);
620 	}
621 
622 	if (idm->idm_flags & (F_STARTTLS | F_SSL)) {
623 		log_debug("starting tls");
624 		where = "enabling tls";
625 		if (aldap_tls(al, idm->idm_tls_config, idm->idm_name) < 0) {
626 			const char *err;
627 			aldap_get_errno(al, &err);
628 			log_warnx("TLS handshake with %s failed: %s",
629 			    idm->idm_name, err);
630 			goto bad;
631 		}
632 	}
633 
634 	if (idm->idm_flags & F_NEEDAUTH) {
635 		int rc;
636 
637 		where = "binding";
638 		if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
639 			goto bad;
640 
641 		where = "parsing";
642 		if ((m = aldap_parse(al)) == NULL)
643 			goto bad;
644 		where = "verifying msgid";
645 		if (al->msgid != m->msgid) {
646 			aldap_freemsg(m);
647 			goto bad;
648 		}
649 		where = "bind response";
650 		rc = aldap_get_resultcode(m);
651 		if (rc != LDAP_SUCCESS) {
652 			log_warnx("LDAP bind with %s failed: result code %d",
653 			    idm->idm_name, rc);
654 			aldap_freemsg(m);
655 			goto bad;
656 		}
657 		aldap_freemsg(m);
658 	}
659 
660 	memset(attrs, 0, sizeof(attrs));
661 	for (i = 0, j = 0; i < ATTR_MAX; i++) {
662 		if (idm->idm_flags & F_FIXED_ATTR(i))
663 			continue;
664 		attrs[j++] = idm->idm_attrs[i];
665 	}
666 	attrs[j] = NULL;
667 
668 	/*
669 	 * build password line.
670 	 */
671 	where = "search";
672 	log_debug("searching password entries");
673 	if (client_search_idm(env, idm, al, attrs,
674 	    idm->idm_filters[FILTER_USER], 0, ATTR_MAX, IMSG_PW_ENTRY) == -1)
675 		goto bad;
676 
677 	memset(attrs, 0, sizeof(attrs));
678 	for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
679 		if (idm->idm_flags & F_FIXED_ATTR(i))
680 			continue;
681 		attrs[j++] = idm->idm_attrs[i];
682 	}
683 	attrs[j] = NULL;
684 
685 	/*
686 	 * build group line.
687 	 */
688 	where = "search";
689 	log_debug("searching group entries");
690 	if (client_search_idm(env, idm, al, attrs,
691 	    idm->idm_filters[FILTER_GROUP], ATTR_GR_MIN, ATTR_GR_MAX,
692 	    IMSG_GRP_ENTRY) == -1)
693 		goto bad;
694 
695 	aldap_close(al);
696 
697 	idm->idm_state = STATE_LDAP_DONE;
698 
699 	return (0);
700 bad:
701 	aldap_close(al);
702 	log_debug("directory %s errored out in %s", idm->idm_name, where);
703 	return (-1);
704 }
705 
706 void
707 client_periodic_update(int fd, short event, void *p)
708 {
709 	struct env	*env = p;
710 
711 	struct idm	*idm;
712 	int		 fail_cnt = 0;
713 
714 	/* If LDAP isn't finished, notify the master process to trash the
715 	 * update. */
716 	TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
717 		if (idm->idm_state < STATE_LDAP_DONE)
718 			fail_cnt++;
719 
720 		idm->idm_state = STATE_NONE;
721 
722 		client_addr_free(idm);
723 	}
724 	if (fail_cnt > 0) {
725 		log_debug("trash the update");
726 		imsg_compose_event(env->sc_iev, IMSG_TRASH_UPDATE, 0, 0, -1,
727 		    NULL, 0);
728 	}
729 
730 	client_configure(env);
731 }
732 
733 void
734 client_configure(struct env *env)
735 {
736 	struct timeval	 tv;
737 	struct idm	*idm;
738         u_int16_t        dlen;
739 
740 	log_debug("connecting to directories");
741 
742 	imsg_compose_event(env->sc_iev, IMSG_START_UPDATE, 0, 0, -1, NULL, 0);
743 
744 	/* Start the DNS lookups */
745 	TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
746 		dlen = strlen(idm->idm_name) + 1;
747 		imsg_compose_event(env->sc_iev_dns, IMSG_HOST_DNS, idm->idm_id,
748 		    0, -1, idm->idm_name, dlen);
749 	}
750 
751 	tv.tv_sec = env->sc_conf_tv.tv_sec;
752 	tv.tv_usec = env->sc_conf_tv.tv_usec;
753 	evtimer_set(&env->sc_conf_ev, client_periodic_update, env);
754 	evtimer_add(&env->sc_conf_ev, &tv);
755 }
756