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