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