1 /* $OpenBSD: ypldap.c,v 1.16 2015/11/02 10:06:06 jmatthew Exp $ */
5 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
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.
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.
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <sys/signal.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
43 __dead2 void usage(void);
44 int check_child(pid_t, const char *);
45 void main_sig_handler(int, short, void *);
46 void main_shutdown(void);
47 void main_dispatch_client(int, short, void *);
48 void main_configure_client(struct env *);
49 void main_init_timer(int, short, void *);
50 void main_start_update(struct env *);
51 void main_trash_update(struct env *);
52 void main_end_update(struct env *);
53 int main_create_user_groups(struct env *);
54 void purge_config(struct env *);
55 void reconfigure(struct env *);
57 int pipe_main2client[2];
60 char *conffile = YPLDAP_CONF_FILE;
66 extern const char *__progname;
68 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
74 check_child(pid_t pid, const char *pname)
78 if (waitpid(pid, &status, WNOHANG) > 0) {
79 if (WIFEXITED(status)) {
80 log_warnx("check_child: lost child %s exited", pname);
83 if (WIFSIGNALED(status)) {
84 log_warnx("check_child: lost child %s terminated; "
85 "signal %d", pname, WTERMSIG(status));
94 main_sig_handler(int sig, short event, void *p)
104 if (check_child(client_pid, "ldap client")) {
115 fatalx("unexpected signal");
126 main_start_update(struct env *env)
128 env->update_trashed = 0;
130 log_debug("starting directory update");
131 env->sc_user_line_len = 0;
132 env->sc_group_line_len = 0;
133 if ((env->sc_user_names_t = calloc(1,
134 sizeof(*env->sc_user_names_t))) == NULL ||
135 (env->sc_group_names_t = calloc(1,
136 sizeof(*env->sc_group_names_t))) == NULL)
138 RB_INIT(env->sc_user_names_t);
139 RB_INIT(env->sc_group_names_t);
143 * XXX: Currently this function should only be called when updating is
144 * finished. A notification should be send to ldapclient that it should stop
145 * sending new pwd/grp entries before it can be called from different places.
148 main_trash_update(struct env *env)
153 env->update_trashed = 1;
155 while ((ue = RB_ROOT(env->sc_user_names_t)) != NULL) {
156 RB_REMOVE(user_name_tree,
157 env->sc_user_names_t, ue);
159 free(ue->ue_netid_line);
162 free(env->sc_user_names_t);
163 env->sc_user_names_t = NULL;
164 while ((ge = RB_ROOT(env->sc_group_names_t))
166 RB_REMOVE(group_name_tree,
167 env->sc_group_names_t, ge);
171 free(env->sc_group_names_t);
172 env->sc_group_names_t = NULL;
176 main_create_user_groups(struct env *env)
184 const char *errstr = NULL;
187 RB_FOREACH(ue, user_name_tree, env->sc_user_names_t) {
188 bp = cp = ue->ue_line;
191 bp += strlen(bp) + 1;
194 bp += strcspn(bp, ":") + 1;
197 bp += strcspn(bp, ":") + 1;
200 bp[strcspn(bp, ":")] = '\0';
202 pw_gid = (gid_t)strtonum(bp, 0, GID_MAX, &errstr);
204 log_warnx("main: failed to parse gid for uid: %d\n", ue->ue_uid);
208 /* bring gid column back to its proper state */
209 bp[strlen(bp)] = ':';
211 if ((ue->ue_netid_line = calloc(1, LINE_WIDTH)) == NULL) {
215 if (snprintf(ue->ue_netid_line, LINE_WIDTH-1, "%d:%d", ue->ue_uid, pw_gid) >= LINE_WIDTH) {
223 RB_FOREACH(ge, group_name_tree, env->sc_group_names_t) {
224 bp = cp = ge->ge_line;
227 bp += strlen(bp) + 1;
230 bp += strcspn(bp, ":") + 1;
233 bp += strcspn(bp, ":") + 1;
240 if (!(cp = strsep(&bp, ",")))
243 if ((ue = RB_FIND(user_name_tree, env->sc_user_names_t,
246 log_warnx("main: user: %s is referenced as a "
247 "group member, but can't be found in the "
248 "users map.\n", ukey.ue_line);
256 /* Make sure the new group doesn't equal to the main gid */
257 if (ge->ge_gid == ue->ue_gid)
260 len = strlen(ue->ue_netid_line);
261 p = ue->ue_netid_line + len;
263 if ((snprintf(p, LINE_WIDTH-len-1, ",%d",
264 ge->ge_gid)) >= (int)(LINE_WIDTH-len)) {
274 main_end_update(struct env *env)
279 if (env->update_trashed)
282 log_debug("updates are over, cleaning up trees now");
284 if (main_create_user_groups(env) == -1) {
285 main_trash_update(env);
289 if (env->sc_user_names == NULL) {
290 env->sc_user_names = env->sc_user_names_t;
291 env->sc_user_lines = NULL;
292 env->sc_user_names_t = NULL;
294 env->sc_group_names = env->sc_group_names_t;
295 env->sc_group_lines = NULL;
296 env->sc_group_names_t = NULL;
298 flatten_entries(env);
303 * clean previous tree.
305 while ((ue = RB_ROOT(env->sc_user_names)) != NULL) {
306 RB_REMOVE(user_name_tree, env->sc_user_names,
308 free(ue->ue_netid_line);
311 free(env->sc_user_names);
312 free(env->sc_user_lines);
314 env->sc_user_names = env->sc_user_names_t;
315 env->sc_user_lines = NULL;
316 env->sc_user_names_t = NULL;
318 while ((ge = RB_ROOT(env->sc_group_names)) != NULL) {
319 RB_REMOVE(group_name_tree,
320 env->sc_group_names, ge);
323 free(env->sc_group_names);
324 free(env->sc_group_lines);
326 env->sc_group_names = env->sc_group_names_t;
327 env->sc_group_lines = NULL;
328 env->sc_group_names_t = NULL;
331 flatten_entries(env);
334 * trees are flat now. build up uid, gid and netid trees.
338 RB_INIT(&env->sc_user_uids);
339 RB_INIT(&env->sc_group_gids);
340 RB_FOREACH(ue, user_name_tree, env->sc_user_names)
341 RB_INSERT(user_uid_tree,
342 &env->sc_user_uids, ue);
343 RB_FOREACH(ge, group_name_tree, env->sc_group_names)
344 RB_INSERT(group_gid_tree,
345 &env->sc_group_gids, ge);
350 main_dispatch_client(int fd, short events, void *p)
355 struct imsgev *iev = env->sc_iev;
356 struct imsgbuf *ibuf = &iev->ibuf;
360 if ((events & (EV_READ | EV_WRITE)) == 0)
361 fatalx("unknown event");
363 if (events & EV_READ) {
364 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
365 fatal("imsg_read error");
369 if (events & EV_WRITE) {
370 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
371 fatal("msgbuf_write");
378 if ((n = imsg_get(ibuf, &imsg)) == -1)
379 fatal("main_dispatch_client: imsg_get error");
383 switch (imsg.hdr.type) {
384 case IMSG_START_UPDATE:
385 main_start_update(env);
387 case IMSG_PW_ENTRY: {
391 if (env->update_trashed)
394 (void)memcpy(&ir, imsg.data, sizeof(ir));
395 if ((ue = calloc(1, sizeof(*ue))) == NULL ||
396 (ue->ue_line = strdup(ir.ir_line)) == NULL) {
398 * should cancel tree update instead.
400 fatal("out of memory");
402 ue->ue_uid = ir.ir_key.ik_uid;
403 len = strlen(ue->ue_line) + 1;
404 ue->ue_line[strcspn(ue->ue_line, ":")] = '\0';
405 if (RB_INSERT(user_name_tree, env->sc_user_names_t,
406 ue) != NULL) { /* dup */
410 env->sc_user_line_len += len;
413 case IMSG_GRP_ENTRY: {
417 if (env->update_trashed)
420 (void)memcpy(&ir, imsg.data, sizeof(ir));
421 if ((ge = calloc(1, sizeof(*ge))) == NULL ||
422 (ge->ge_line = strdup(ir.ir_line)) == NULL) {
424 * should cancel tree update instead.
426 fatal("out of memory");
428 ge->ge_gid = ir.ir_key.ik_gid;
429 len = strlen(ge->ge_line) + 1;
430 ge->ge_line[strcspn(ge->ge_line, ":")] = '\0';
431 if (RB_INSERT(group_name_tree, env->sc_group_names_t,
432 ge) != NULL) { /* dup */
436 env->sc_group_line_len += len;
439 case IMSG_TRASH_UPDATE:
440 main_trash_update(env);
442 case IMSG_END_UPDATE: {
443 main_end_update(env);
447 log_debug("main_dispatch_client: unexpected imsg %d",
458 log_debug("king bula sez: ran into dead pipe");
460 event_loopexit(NULL);
465 main_configure_client(struct env *env)
468 struct imsgev *iev = env->sc_iev;
470 imsg_compose_event(iev, IMSG_CONF_START, 0, 0, -1, env, sizeof(*env));
471 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
472 imsg_compose_event(iev, IMSG_CONF_IDM, 0, 0, -1,
475 imsg_compose_event(iev, IMSG_CONF_END, 0, 0, -1, NULL, 0);
479 main_init_timer(int fd, short event, void *p)
483 main_configure_client(env);
487 purge_config(struct env *env)
491 while ((idm = TAILQ_FIRST(&env->sc_idms)) != NULL) {
492 TAILQ_REMOVE(&env->sc_idms, idm, idm_entry);
498 main(int argc, char *argv[])
504 struct event ev_sigint;
505 struct event ev_sigterm;
506 struct event ev_sigchld;
507 struct event ev_sighup;
508 struct event ev_timer;
512 ypldap_process = PROC_MAIN;
516 while ((c = getopt(argc, argv, "dD:nf:v")) != -1) {
522 if (cmdline_symset(optarg) < 0)
523 log_warnx("could not parse macro definition %s",
528 opts |= YPLDAP_OPT_NOACTION;
534 opts |= YPLDAP_OPT_VERBOSE;
547 RB_INIT(&env.sc_user_uids);
548 RB_INIT(&env.sc_group_gids);
550 if (parse_config(&env, conffile, opts))
552 if (opts & YPLDAP_OPT_NOACTION) {
553 fprintf(stderr, "configuration OK\n");
558 errx(1, "need root privileges");
563 if (daemon(1, 0) == -1)
564 err(1, "failed to daemonize");
567 log_info("startup%s", (debug > 1)?" [debug mode]":"");
569 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
570 pipe_main2client) == -1)
573 client_pid = ldapclient(pipe_main2client);
575 setproctitle("parent");
578 signal_set(&ev_sigint, SIGINT, main_sig_handler, &env);
579 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, &env);
580 signal_set(&ev_sighup, SIGHUP, main_sig_handler, &env);
581 signal_set(&ev_sigchld, SIGCHLD, main_sig_handler, &env);
582 signal_add(&ev_sigint, NULL);
583 signal_add(&ev_sigterm, NULL);
584 signal_add(&ev_sighup, NULL);
585 signal_add(&ev_sigchld, NULL);
587 close(pipe_main2client[1]);
588 if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL)
590 imsg_init(&env.sc_iev->ibuf, pipe_main2client[0]);
591 env.sc_iev->handler = main_dispatch_client;
593 env.sc_iev->events = EV_READ;
594 env.sc_iev->data = &env;
595 event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events,
596 env.sc_iev->handler, &env);
597 event_add(&env.sc_iev->ev, NULL);
601 if ((pw = getpwnam(YPLDAP_USER)) == NULL)
605 if (setgroups(1, &pw->pw_gid) ||
606 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
607 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
608 fatal("cannot drop privileges");
610 #warning disabling privilege revocation in debug mode
613 bzero(&tv, sizeof(tv));
614 evtimer_set(&ev_timer, main_init_timer, &env);
615 evtimer_add(&ev_timer, &tv);
625 imsg_event_add(struct imsgev *iev)
627 if (iev->handler == NULL) {
628 imsg_flush(&iev->ibuf);
632 iev->events = EV_READ;
633 if (iev->ibuf.w.queued)
634 iev->events |= EV_WRITE;
637 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
638 event_add(&iev->ev, NULL);
642 imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
643 pid_t pid, int fd, void *data, u_int16_t datalen)
647 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
648 pid, fd, data, datalen)) != -1)