1/* $OpenBSD: ldapclient.c,v 1.31 2014/11/16 23:24:44 tedu Exp $ */
2/* $FreeBSD: stable/11/usr.sbin/ypldap/ldapclient.c 309872 2016-12-12 02:24:54Z araujo $ */
3
4/*
5 * Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org>
6 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21#include <sys/types.h>
22#include <sys/param.h>
23#include <sys/queue.h>
24#include <sys/socket.h>
25#include <sys/tree.h>
26
27#include <netinet/in.h>
28#include <arpa/inet.h>
29
30#include <netdb.h>
31#include <errno.h>
32#include <err.h>
33#include <event.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <pwd.h>
37#include <signal.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include "aldap.h"
43#include "ypldap.h"
44
45void    client_sig_handler(int, short, void *);
46void	client_dispatch_dns(int, short, void *);
47void    client_dispatch_parent(int, short, void *);
48void    client_shutdown(void);
49void    client_connect(int, short, void *);
50void    client_configure(struct env *);
51void    client_periodic_update(int, short, void *);
52int	client_build_req(struct idm *, struct idm_req *, struct aldap_message *,
53	    int, int);
54int	client_search_idm(struct env *, struct idm *, struct aldap *,
55	    char **, char *, int, int, enum imsg_type);
56int	client_try_idm(struct env *, struct idm *);
57int	client_addr_init(struct idm *);
58int	client_addr_free(struct idm *);
59
60struct aldap	*client_aldap_open(struct ypldap_addr_list *);
61
62/*
63 * dummy wrapper to provide aldap_init with its fd's.
64 */
65struct aldap *
66client_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->sa_len, 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->sa_len) == 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
95int
96client_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
125int
126client_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
139void
140client_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
152void
153client_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
243done:
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
253void
254client_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
329done:
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
339void
340client_shutdown(void)
341{
342	log_info("ldap client exiting");
343	_exit(0);
344}
345
346pid_t
347ldapclient(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
388#ifndef DEBUG
389	if (setgroups(1, &pw->pw_gid) ||
390	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
391	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
392		fatal("cannot drop privileges");
393#else
394#warning disabling privilege revocation in DEBUG mode
395#endif
396
397	event_init();
398	signal(SIGPIPE, SIG_IGN);
399	signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
400	signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
401	signal_add(&ev_sigint, NULL);
402	signal_add(&ev_sigterm, NULL);
403
404	close(pipe_main2client[0]);
405	if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL)
406		fatal(NULL);
407	if ((env.sc_iev_dns = calloc(1, sizeof(*env.sc_iev_dns))) == NULL)
408		fatal(NULL);
409
410	env.sc_iev->events = EV_READ;
411	env.sc_iev->data = &env;
412	imsg_init(&env.sc_iev->ibuf, pipe_main2client[1]);
413	env.sc_iev->handler = client_dispatch_parent;
414	event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events,
415	    env.sc_iev->handler, &env);
416	event_add(&env.sc_iev->ev, NULL);
417
418	env.sc_iev_dns->events = EV_READ;
419	env.sc_iev_dns->data = &env;
420	imsg_init(&env.sc_iev_dns->ibuf, pipe_dns[0]);
421	env.sc_iev_dns->handler = client_dispatch_dns;
422	event_set(&env.sc_iev_dns->ev, env.sc_iev_dns->ibuf.fd,
423	    env.sc_iev_dns->events, env.sc_iev_dns->handler, &env);
424	event_add(&env.sc_iev_dns->ev, NULL);
425
426	event_dispatch();
427	client_shutdown();
428
429	return (0);
430
431}
432
433int
434client_build_req(struct idm *idm, struct idm_req *ir, struct aldap_message *m,
435    int min_attr, int max_attr)
436{
437	char	**ldap_attrs;
438	int	 i, k;
439
440	memset(ir, 0, sizeof(*ir));
441	for (i = min_attr; i < max_attr; i++) {
442		if (idm->idm_flags & F_FIXED_ATTR(i)) {
443			if (strlcat(ir->ir_line, idm->idm_attrs[i],
444			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
445				/*
446				 * entry yields a line > 1024, trash it.
447				 */
448				return (-1);
449
450			if (i == ATTR_UID) {
451				ir->ir_key.ik_uid = strtonum(
452				    idm->idm_attrs[i], 0,
453				    UID_MAX, NULL);
454			} else if (i == ATTR_GR_GID) {
455				ir->ir_key.ik_gid = strtonum(
456				    idm->idm_attrs[i], 0,
457				    GID_MAX, NULL);
458			}
459		} else if (idm->idm_list & F_LIST(i)) {
460			aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs);
461			for (k = 0; k >= 0 && ldap_attrs && ldap_attrs[k] != NULL; k++) {
462				/* XXX: Fail when attributes have illegal characters e.g. ',' */
463				if (strlcat(ir->ir_line, ldap_attrs[k],
464				    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
465					continue;
466				if (ldap_attrs[k+1] != NULL)
467					if (strlcat(ir->ir_line, ",",
468						    sizeof(ir->ir_line))
469					    >= sizeof(ir->ir_line)) {
470						aldap_free_attr(ldap_attrs);
471						return (-1);
472					}
473			}
474			aldap_free_attr(ldap_attrs);
475		} else {
476			if (aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs) == -1)
477				return (-1);
478			if (strlcat(ir->ir_line, ldap_attrs[0],
479			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) {
480				aldap_free_attr(ldap_attrs);
481				return (-1);
482			}
483			if (i == ATTR_UID) {
484				ir->ir_key.ik_uid = strtonum(
485				    ldap_attrs[0], 0, UID_MAX, NULL);
486			} else if (i == ATTR_GR_GID) {
487				ir->ir_key.ik_uid = strtonum(
488				    ldap_attrs[0], 0, GID_MAX, NULL);
489			}
490			aldap_free_attr(ldap_attrs);
491		}
492
493		if (i + 1 != max_attr)
494			if (strlcat(ir->ir_line, ":",
495			    sizeof(ir->ir_line)) >= sizeof(ir->ir_line))
496				return (-1);
497	}
498
499	return (0);
500}
501
502int
503client_search_idm(struct env *env, struct idm *idm, struct aldap *al,
504    char **attrs, char *filter, int min_attr, int max_attr,
505    enum imsg_type type)
506{
507	struct idm_req		 ir;
508	struct aldap_message	*m;
509	struct aldap_page_control *pg = NULL;
510	const char		*errstr;
511	char			*dn;
512
513	dn = idm->idm_basedn;
514	if (type == IMSG_GRP_ENTRY && idm->idm_groupdn[0] != '\0')
515		dn = idm->idm_groupdn;
516
517	do {
518		if (aldap_search(al, dn, LDAP_SCOPE_SUBTREE,
519		    filter, attrs, 0, 0, 0, pg) == -1) {
520			aldap_get_errno(al, &errstr);
521			log_debug("%s", errstr);
522			return (-1);
523		}
524
525		if (pg != NULL) {
526			aldap_freepage(pg);
527			pg = NULL;
528		}
529
530		while ((m = aldap_parse(al)) != NULL) {
531			if (al->msgid != m->msgid) {
532				goto fail;
533			}
534
535			if (m->message_type == LDAP_RES_SEARCH_RESULT) {
536				if (m->page != NULL && m->page->cookie_len != 0)
537					pg = m->page;
538				else
539					pg = NULL;
540
541				aldap_freemsg(m);
542				break;
543			}
544
545			if (m->message_type != LDAP_RES_SEARCH_ENTRY) {
546				goto fail;
547			}
548
549			if (client_build_req(idm, &ir, m, min_attr, max_attr) == 0)
550				imsg_compose_event(env->sc_iev, type, 0, 0, -1,
551				    &ir, sizeof(ir));
552
553			aldap_freemsg(m);
554		}
555	} while (pg != NULL);
556
557	return (0);
558
559fail:
560	aldap_freemsg(m);
561	if (pg != NULL) {
562		aldap_freepage(pg);
563	}
564
565	return (-1);
566}
567
568int
569client_try_idm(struct env *env, struct idm *idm)
570{
571	const char		*where;
572	char			*attrs[ATTR_MAX+1];
573	int			 i, j;
574	struct aldap_message	*m;
575	struct aldap		*al;
576
577	where = "connect";
578	if ((al = client_aldap_open(&idm->idm_addr)) == NULL)
579		return (-1);
580
581	if (idm->idm_flags & F_NEEDAUTH) {
582		where = "binding";
583		if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
584			goto bad;
585
586		where = "parsing";
587		if ((m = aldap_parse(al)) == NULL)
588			goto bad;
589		where = "verifying msgid";
590		if (al->msgid != m->msgid) {
591			aldap_freemsg(m);
592			goto bad;
593		}
594		aldap_freemsg(m);
595	}
596
597	memset(attrs, 0, sizeof(attrs));
598	for (i = 0, j = 0; i < ATTR_MAX; i++) {
599		if (idm->idm_flags & F_FIXED_ATTR(i))
600			continue;
601		attrs[j++] = idm->idm_attrs[i];
602	}
603	attrs[j] = NULL;
604
605	/*
606	 * build password line.
607	 */
608	where = "search";
609	log_debug("searching password entries");
610	if (client_search_idm(env, idm, al, attrs,
611	    idm->idm_filters[FILTER_USER], 0, ATTR_MAX, IMSG_PW_ENTRY) == -1)
612		goto bad;
613
614	memset(attrs, 0, sizeof(attrs));
615	for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
616		if (idm->idm_flags & F_FIXED_ATTR(i))
617			continue;
618		attrs[j++] = idm->idm_attrs[i];
619	}
620	attrs[j] = NULL;
621
622	/*
623	 * build group line.
624	 */
625	where = "search";
626	log_debug("searching group entries");
627	if (client_search_idm(env, idm, al, attrs,
628	    idm->idm_filters[FILTER_GROUP], ATTR_GR_MIN, ATTR_GR_MAX,
629	    IMSG_GRP_ENTRY) == -1)
630		goto bad;
631
632	aldap_close(al);
633
634	idm->idm_state = STATE_LDAP_DONE;
635
636	return (0);
637bad:
638	aldap_close(al);
639	log_debug("directory %s errored out in %s", idm->idm_name, where);
640	return (-1);
641}
642
643void
644client_periodic_update(int fd, short event, void *p)
645{
646	struct env	*env = p;
647
648	struct idm	*idm;
649	int		 fail_cnt = 0;
650
651	/* If LDAP isn't finished, notify the master process to trash the
652	 * update. */
653	TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
654		if (idm->idm_state < STATE_LDAP_DONE)
655			fail_cnt++;
656
657		idm->idm_state = STATE_NONE;
658
659		client_addr_free(idm);
660	}
661	if (fail_cnt > 0) {
662		log_debug("trash the update");
663		imsg_compose_event(env->sc_iev, IMSG_TRASH_UPDATE, 0, 0, -1,
664		    NULL, 0);
665	}
666
667	client_configure(env);
668}
669
670void
671client_configure(struct env *env)
672{
673	struct timeval	 tv;
674	struct idm	*idm;
675        u_int16_t        dlen;
676
677	log_debug("connecting to directories");
678
679	imsg_compose_event(env->sc_iev, IMSG_START_UPDATE, 0, 0, -1, NULL, 0);
680
681	/* Start the DNS lookups */
682	TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
683		dlen = strlen(idm->idm_name) + 1;
684		imsg_compose_event(env->sc_iev_dns, IMSG_HOST_DNS, idm->idm_id,
685		    0, -1, idm->idm_name, dlen);
686	}
687
688	tv.tv_sec = env->sc_conf_tv.tv_sec;
689	tv.tv_usec = env->sc_conf_tv.tv_usec;
690	evtimer_set(&env->sc_conf_ev, client_periodic_update, env);
691	evtimer_add(&env->sc_conf_ev, &tv);
692}
693