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