summaryrefslogtreecommitdiff
path: root/usr.sbin/ypldap
diff options
context:
space:
mode:
authoraschrijver <aschrijver@cvs.openbsd.org>2008-10-28 13:47:23 +0000
committeraschrijver <aschrijver@cvs.openbsd.org>2008-10-28 13:47:23 +0000
commitbb52e8e0a28aa60453f82547e9ea7fa7f0576d3f (patch)
tree4424a5f6dc3dcdbdc67896746bcbb2c4613e03f3 /usr.sbin/ypldap
parent641f1e38739ea17743b8a110652a1f5c52bdca3e (diff)
Do the DNS resolution in a seperate process. A lot of code is copied from ntpd.
This is necessary because DNS resolution is not possible from a chroot. Cleanup the aldap interface a bit.
Diffstat (limited to 'usr.sbin/ypldap')
-rw-r--r--usr.sbin/ypldap/Makefile6
-rw-r--r--usr.sbin/ypldap/aldap.c30
-rw-r--r--usr.sbin/ypldap/ldapclient.c576
-rw-r--r--usr.sbin/ypldap/parse.y48
-rw-r--r--usr.sbin/ypldap/ypldap.h29
-rw-r--r--usr.sbin/ypldap/ypldap_dns.c241
6 files changed, 589 insertions, 341 deletions
diff --git a/usr.sbin/ypldap/Makefile b/usr.sbin/ypldap/Makefile
index 3ed76efd99d..bdc093a615f 100644
--- a/usr.sbin/ypldap/Makefile
+++ b/usr.sbin/ypldap/Makefile
@@ -1,9 +1,11 @@
-# $OpenBSD: Makefile,v 1.4 2008/09/30 16:24:16 aschrijver Exp $
+# $OpenBSD: Makefile,v 1.5 2008/10/28 13:47:22 aschrijver Exp $
PROG= ypldap
SRCS= parse.y ypldap.c imsg.c buffer.c log.c \
ldapclient.c entries.c yp.c \
- aldap.c ber.c
+ aldap.c ber.c \
+ ypldap_dns.c
+
MAN= ypldap.8 ypldap.conf.5
DPADD= ${LIBEVENT}
diff --git a/usr.sbin/ypldap/aldap.c b/usr.sbin/ypldap/aldap.c
index c2b69a7c5c1..63ca8e1477b 100644
--- a/usr.sbin/ypldap/aldap.c
+++ b/usr.sbin/ypldap/aldap.c
@@ -1,5 +1,5 @@
-/* $Id: aldap.c,v 1.5 2008/10/14 21:41:03 aschrijver Exp $ */
-/* $OpenBSD: aldap.c,v 1.5 2008/10/14 21:41:03 aschrijver Exp $ */
+/* $Id: aldap.c,v 1.6 2008/10/28 13:47:22 aschrijver Exp $ */
+/* $OpenBSD: aldap.c,v 1.6 2008/10/28 13:47:22 aschrijver Exp $ */
/*
* Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org>
@@ -259,20 +259,28 @@ aldap_first_entry(struct aldap_message *msg, char **outkey, char ***outvalues)
{
struct ber_element *b, *c;
char *key;
+ char **ret;
if(msg->body.search.entries == NULL)
- return (-1);
+ goto fail;
if(ber_scanf_elements(msg->body.search.entries, "{s(e)}e", &key, &b,
&c) != 0)
- return (-1);
+ goto fail;
msg->body.search.iter = msg->body.search.entries->be_next;
- (*outvalues) = aldap_get_stringset(b);
+ if((ret = aldap_get_stringset(b)) == NULL)
+ goto fail;
+
+ (*outvalues) = ret;
(*outkey) = utoa(key);
return (1);
+fail:
+ (*outkey) = NULL;
+ (*outvalues) = NULL;
+ return (-1);
}
int
@@ -280,6 +288,7 @@ aldap_next_entry(struct aldap_message *msg, char **outkey, char ***outvalues)
{
struct ber_element *a, *b;
char *key;
+ char **ret;
LDAP_DEBUG("entry", msg->body.search.iter);
@@ -294,7 +303,10 @@ aldap_next_entry(struct aldap_message *msg, char **outkey, char ***outvalues)
msg->body.search.iter = msg->body.search.iter->be_next;
- (*outvalues) = aldap_get_stringset(a);
+ if((ret = aldap_get_stringset(a)) == NULL)
+ goto fail;
+
+ (*outvalues) = ret;
(*outkey) = utoa(key);
return (1);
@@ -310,6 +322,7 @@ aldap_match_entry(struct aldap_message *msg, char *inkey, char ***outvalues)
{
struct ber_element *a, *b;
char *descr = NULL;
+ char **ret;
LDAP_DEBUG("entry", msg->search_entries);
@@ -329,7 +342,10 @@ aldap_match_entry(struct aldap_message *msg, char *inkey, char ***outvalues)
}
attrfound:
- (*outvalues) = aldap_get_stringset(b);
+ if((ret = aldap_get_stringset(b)) == NULL)
+ goto fail;
+
+ (*outvalues) = ret;
return (1);
fail:
diff --git a/usr.sbin/ypldap/ldapclient.c b/usr.sbin/ypldap/ldapclient.c
index c707523b191..98e74becc1c 100644
--- a/usr.sbin/ypldap/ldapclient.c
+++ b/usr.sbin/ypldap/ldapclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldapclient.c,v 1.8 2008/10/21 11:33:36 aschrijver Exp $ */
+/* $OpenBSD: ldapclient.c,v 1.9 2008/10/28 13:47:22 aschrijver Exp $ */
/*
* Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org>
@@ -41,26 +41,20 @@
#include "ypldap.h"
void client_sig_handler(int, short, void *);
+void client_dispatch_dns(int, short, void *);
void client_dispatch_parent(int, short, void *);
void client_shutdown(void);
void client_connect(int, short, void *);
void client_configure(struct env *);
-void client_configure_wrapper(int, short, void *);
+void client_periodic_update(int, short, void *);
int client_try_idm(struct env *, struct idm *);
void client_try_idm_wrapper(int, short, void *);
void client_try_server_wrapper(int, short, void *);
+int client_addr_init(struct idm *);
+int client_addr_free(struct idm *);
-int do_build_group(struct env *, struct idm *, struct aldap *, char *, enum
- scope, char *);
-int do_build_passwd(struct env *, struct idm *, struct aldap *, char *, enum
- scope, char *);
-
-struct aldap *aldap_openidm(struct idm *idm);
+struct aldap *aldap_open(struct ypldap_addr *);
int aldap_close(struct aldap *);
-#ifdef REFERRALS
-struct aldap *connect_to_referral(struct aldap_message *, struct aldap_url *);
-struct aldap *aldap_openhost(char *, char *);
-#endif
int
aldap_close(struct aldap *al)
@@ -74,31 +68,28 @@ aldap_close(struct aldap *al)
}
struct aldap *
-aldap_openidm(struct idm *idm)
+aldap_open(struct ypldap_addr *addr)
{
- int fd;
- struct addrinfo *res0;
+ int fd = -1;
+ struct ypldap_addr *p;
- res0 = idm->idm_addrinfo;
- do {
- char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+ for(p = addr; p != NULL; p = p->next) {
+ char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+ struct sockaddr *sa = (struct sockaddr *)&p->ss;
- if (getnameinfo(res0->ai_addr, res0->ai_addrlen, hbuf, sizeof(hbuf), sbuf,
+ if (getnameinfo(sa, SA_LEN(sa), hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV))
errx(1, "could not get numeric hostname");
- if ((fd = socket(res0->ai_family, res0->ai_socktype,
- res0->ai_protocol)) < 0)
- continue;
+ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ return NULL;
- if (connect(fd, res0->ai_addr, res0->ai_addrlen) == 0)
+ if (connect(fd, sa, SA_LEN(sa)) == 0)
break;
- else
- warn("connect to %s port %s (%s) failed", hbuf, sbuf, "tcp");
+ warn("connect to %s port %s (%s) failed", hbuf, sbuf, "tcp");
close(fd);
- fd = -1;
- } while ((res0 = res0->ai_next) != NULL);
+ }
if(fd == -1)
return NULL;
@@ -106,6 +97,51 @@ aldap_openidm(struct idm *idm)
return aldap_init(fd);
}
+int
+client_addr_init(struct idm *idm)
+{
+ struct sockaddr_in *sa_in;
+ struct sockaddr_in6 *sa_in6;
+ struct ypldap_addr *h;
+
+ for (h = idm->idm_addr; h != NULL; h = h->next) {
+ switch (h->ss.ss_family) {
+ case AF_INET:
+ sa_in = (struct sockaddr_in *)&h->ss;
+ if (ntohs(sa_in->sin_port) == 0)
+ sa_in->sin_port = htons(389);
+ idm->idm_state = STATE_DNS_DONE;
+ break;
+ case AF_INET6:
+ sa_in6 = (struct sockaddr_in6 *)&h->ss;
+ if (ntohs(sa_in6->sin6_port) == 0)
+ sa_in6->sin6_port = htons(389);
+ idm->idm_state = STATE_DNS_DONE;
+ break;
+ default:
+ fatalx("king bula sez: wrong AF in client_addr_init");
+ /* not reached */
+ }
+ }
+
+ return (0);
+}
+
+int
+client_addr_free(struct idm *idm)
+{
+ struct ypldap_addr *h;
+
+ if(idm->idm_addr == NULL)
+ return (-1);
+
+ for (h = idm->idm_addr; h != NULL; h = h->next)
+ free(h);
+
+ idm->idm_addr = NULL;
+
+ return (0);
+}
void
client_sig_handler(int sig, short event, void *p)
@@ -121,6 +157,110 @@ client_sig_handler(int sig, short event, void *p)
}
void
+client_dispatch_dns(int fd, short event, void *p)
+{
+ struct imsg imsg;
+ u_int16_t dlen;
+ u_char *data;
+ struct ypldap_addr *h;
+ int n, wait_cnt = 0;
+ struct idm *idm;
+ int shut = 0;
+
+ struct env *env = p;
+ struct imsgbuf *ibuf = env->sc_ibuf_dns;
+
+ switch (event) {
+ case EV_READ:
+ if ((n = imsg_read(ibuf)) == -1)
+ fatal("imsg_read error");
+ if (n == 0)
+ shut = 1;
+ break;
+ case EV_WRITE:
+ if (msgbuf_write(&ibuf->w) == -1)
+ fatal("msgbuf_write");
+ imsg_event_add(ibuf);
+ return;
+ default:
+ fatalx("unknown event");
+ }
+
+ for (;;) {
+ if ((n = imsg_get(ibuf, &imsg)) == -1)
+ fatal("client_dispatch_parent: imsg_read_error");
+ if (n == 0)
+ break;
+
+ switch (imsg.hdr.type) {
+ case IMSG_HOST_DNS:
+ TAILQ_FOREACH(idm, &env->sc_idms, idm_entry)
+ if (idm->idm_id == imsg.hdr.peerid)
+ break;
+ if (idm == NULL) {
+ log_warnx("IMSG_HOST_DNS with invalid peerID");
+ break;
+ }
+ if (idm->idm_addr != NULL) {
+ log_warnx("IMSG_HOST_DNS but addr != NULL!");
+ break;
+ }
+
+ dlen = imsg.hdr.len - IMSG_HEADER_SIZE;
+ if (dlen == 0) { /* no data -> temp error */
+ idm->idm_state = STATE_DNS_TEMPFAIL;
+ break;
+ }
+
+ data = (u_char *)imsg.data;
+ while (dlen >= sizeof(struct sockaddr_storage)) {
+ if ((h = calloc(1, sizeof(struct ypldap_addr))) ==
+ NULL)
+ fatal(NULL);
+ memcpy(&h->ss, data, sizeof(h->ss));
+
+ if(idm->idm_addr == NULL)
+ h->next = NULL;
+ else
+ h->next = idm->idm_addr;
+
+ idm->idm_addr = h;
+
+ data += sizeof(h->ss);
+ dlen -= sizeof(h->ss);
+ }
+ if (dlen != 0)
+ fatalx("IMSG_HOST_DNS: dlen != 0");
+
+ client_addr_init(idm);
+
+ break;
+ default:
+ break;
+ }
+ imsg_free(&imsg);
+ }
+
+ TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
+ if(client_try_idm(env, idm) == -1)
+ idm->idm_state = STATE_LDAP_FAIL;
+
+ if(idm->idm_state < STATE_LDAP_DONE)
+ wait_cnt++;
+ }
+ if(wait_cnt == 0)
+ imsg_compose(env->sc_ibuf, IMSG_END_UPDATE, 0, 0, NULL, 0);
+
+ if (!shut)
+ imsg_event_add(ibuf);
+ else {
+ /* this pipe is dead, so remove the event handler */
+ event_del(&ibuf->ev);
+ event_loopexit(NULL);
+ }
+}
+
+void
client_dispatch_parent(int fd, short event, void *p)
{
int n;
@@ -213,7 +353,8 @@ client_shutdown(void)
pid_t
ldapclient(int pipe_main2client[2])
{
- pid_t pid;
+ pid_t pid, dns_pid;
+ int pipe_dns[2];
struct passwd *pw;
struct event ev_sigint;
struct event ev_sigterm;
@@ -235,6 +376,11 @@ ldapclient(int pipe_main2client[2])
if ((pw = getpwnam(YPLDAP_USER)) == NULL)
fatal("getpwnam");
+ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_dns) == -1)
+ fatal("socketpair");
+ dns_pid = ypldap_dns(pipe_dns, pw);
+ close(pipe_dns[1]);
+
#ifndef DEBUG
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
@@ -264,6 +410,8 @@ ldapclient(int pipe_main2client[2])
close(pipe_main2client[0]);
if ((env.sc_ibuf = calloc(1, sizeof(*env.sc_ibuf))) == NULL)
fatal(NULL);
+ if ((env.sc_ibuf_dns = calloc(1, sizeof(*env.sc_ibuf_dns))) == NULL)
+ fatal(NULL);
env.sc_ibuf->events = EV_READ;
env.sc_ibuf->data = &env;
@@ -272,6 +420,13 @@ ldapclient(int pipe_main2client[2])
env.sc_ibuf->handler, &env);
event_add(&env.sc_ibuf->ev, NULL);
+ env.sc_ibuf_dns->events = EV_READ;
+ env.sc_ibuf_dns->data = &env;
+ imsg_init(env.sc_ibuf_dns, pipe_dns[0], client_dispatch_dns);
+ event_set(&env.sc_ibuf_dns->ev, env.sc_ibuf_dns->fd, env.sc_ibuf_dns->events,
+ env.sc_ibuf_dns->handler, &env);
+ event_add(&env.sc_ibuf_dns->ev, NULL);
+
event_dispatch();
client_shutdown();
@@ -279,105 +434,39 @@ ldapclient(int pipe_main2client[2])
}
-void
-client_configure_wrapper(int fd, short event, void *p)
-{
- struct env *env = p;
-
- client_configure(env);
-}
-
-#ifdef REFERRALS
-struct aldap *
-aldap_openhost(char *host, char *port)
+int
+client_try_idm(struct env *env, struct idm *idm)
{
- struct addrinfo hints;
+ const char *where;
+ char *attrs[ATTR_MAX+1];
+ char **ldap_attrs;
+ int i, j, k;
+ struct idm_req ir;
+ struct aldap_message *m;
struct aldap *al;
- int fd;
-
- struct addrinfo *res, *res0;
- int error;
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- log_debug("trying directory: %s", host);
-
- if ((error = getaddrinfo(host, port, &hints, &res)))
- errx(1, "getaddrinfo: %s", gai_strerror(error));
-
- res0 = res;
- do {
- if ((fd = socket(res0->ai_family, res0->ai_socktype,
- res0->ai_protocol)) < 0)
- continue;
-
- if (connect(fd, res0->ai_addr, res0->ai_addrlen) == 0)
- break;
- else
- warn("connect to %s port %s (%s) failed", host, port, "tcp");
-
- close(fd);
- fd = -1;
- } while ((res0 = res0->ai_next) != NULL);
-
- freeaddrinfo(res);
-
- if(fd == -1)
- return NULL;
-
- return aldap_init(fd);
-}
-
-struct aldap *
-connect_to_referral(struct aldap_message *m, struct aldap_url *lu)
-{
- int i;
- char **refs, *port;
- struct aldap *al = NULL;
-
- if((refs = aldap_get_references(m)) == NULL)
- return NULL;
+ where = "connect";
+ if((al = aldap_open(idm->idm_addr)) == NULL)
+ return (-1);
- for(i = 0; i >= 0 && refs[i] != NULL; i++) {
- aldap_parse_url(refs[i], lu);
- asprintf(&port, "%d", lu->port ? lu->port : 389);
+ if (idm->idm_flags & F_NEEDAUTH) {
+ where = "binding";
+ if(aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
+ goto bad;
- if((al = aldap_openhost(lu->host, port)) != NULL) {
- free(port);
- break;
+ where = "parsing";
+ if((m = aldap_parse(al)) == NULL)
+ goto bad;
+ where = "verifying msgid";
+ if(al->msgid != m->msgid) {
+ aldap_freemsg(m);
+ goto bad;
}
-
- free(port);
+ aldap_freemsg(m);
}
- aldap_free_references(refs);
-
- return al;
-}
-#endif
-
-#define MAX_REFERRALS 10
-int
-do_build_group(struct env *env, struct idm *idm, struct aldap *al, char
- *basedn, enum scope scope, char *filter)
-{
- char *attrs[ATTR_MAX+1];
- char **ldap_attrs;
- struct idm_req ir;
- struct aldap_message *m;
- int i, j, k;
- const char *where;
-#ifdef REFERRALS
- static int refcnt = 0;
- struct aldap_url lu;
- struct aldap *al_ref;
-#endif
-
bzero(attrs, sizeof(attrs));
- for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
+ for (i = 0, j = 0; i < ATTR_MAX; i++) {
if (idm->idm_flags & F_FIXED_ATTR(i))
continue;
attrs[j++] = idm->idm_attrs[i];
@@ -385,11 +474,12 @@ do_build_group(struct env *env, struct idm *idm, struct aldap *al, char
attrs[j] = NULL;
where = "search";
- if(aldap_search(al, basedn, scope, filter, attrs, 0, 0, 0) == -1)
+ if(aldap_search(al, idm->idm_basedn, LDAP_SCOPE_SUBTREE,
+ idm->idm_filters[FILTER_USER], attrs, 0, 0, 0) == -1)
goto bad;
/*
- * build group line.
+ * build password line.
*/
while((m = aldap_parse(al)) != NULL) {
where = "verifying msgid";
@@ -397,36 +487,6 @@ do_build_group(struct env *env, struct idm *idm, struct aldap *al, char
aldap_freemsg(m);
goto bad;
}
-#ifdef REFERRALS
- /* continuation referral */
- if (m->message_type == LDAP_RES_SEARCH_REFERENCE) {
- if(refcnt++ >= MAX_REFERRALS)
- goto next_entry;
-
- if((al_ref = connect_to_referral(m, &lu)) == NULL)
- goto next_entry;
- do_build_group(env, idm, al_ref, lu.dn,
- lu.scope, idm->idm_filters[FILTER_GROUP]);
- aldap_close(al_ref);
- goto next_entry;
- }
- /* normal referral */
- if(m->message_type == LDAP_RES_SEARCH_RESULT &&
- aldap_get_resultcode(m) == LDAP_REFERRAL) {
- if(refcnt++ == MAX_REFERRALS) {
- aldap_freemsg(m);
- break;
- }
-
- if((al_ref = connect_to_referral(m, &lu)) == NULL) {
- aldap_freemsg(m);
- break;
- }
- do_build_group(env, idm, al_ref, lu.dn,
- lu.scope, idm->idm_filters[FILTER_GROUP]);
- aldap_close(al_ref);
- }
-#endif
/* end of the search result chain */
if (m->message_type == LDAP_RES_SEARCH_RESULT) {
aldap_freemsg(m);
@@ -440,24 +500,24 @@ do_build_group(struct env *env, struct idm *idm, struct aldap *al, char
}
/* search entry */
bzero(&ir, sizeof(ir));
- for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
+ for (i = 0, j = 0; i < ATTR_MAX; i++) {
if (idm->idm_flags & F_FIXED_ATTR(i)) {
if (strlcat(ir.ir_line, idm->idm_attrs[i],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
/*
* entry yields a line > 1024, trash it.
*/
- goto next_entry;
- if (i == ATTR_GR_GID) {
- ir.ir_key.ik_gid = strtonum(
+ goto next_pwdentry;
+ if (i == ATTR_UID) {
+ ir.ir_key.ik_uid = strtonum(
idm->idm_attrs[i], 0,
- GID_MAX, NULL);
+ UID_MAX, NULL);
}
} else if (idm->idm_list & F_LIST(i)) {
if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_entry;
- if (ldap_attrs == NULL || ldap_attrs[0] == NULL)
- goto next_entry;
+ goto next_pwdentry;
+ if (ldap_attrs[0] == NULL)
+ goto next_pwdentry;
for(k = 0; k >= 0 && ldap_attrs[k] != NULL; k++) {
if (strlcat(ir.ir_line, ldap_attrs[k],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
@@ -467,61 +527,39 @@ do_build_group(struct env *env, struct idm *idm, struct aldap *al, char
sizeof(ir.ir_line))
>= sizeof(ir.ir_line)) {
aldap_free_entry(ldap_attrs);
- goto next_entry;
+ goto next_pwdentry;
}
}
aldap_free_entry(ldap_attrs);
} else {
- if(aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_entry;
- if (ldap_attrs == NULL || ldap_attrs[0] == NULL)
- goto next_entry;
+ if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
+ goto next_pwdentry;
+ if (ldap_attrs[0] == NULL)
+ goto next_pwdentry;
if (strlcat(ir.ir_line, ldap_attrs[0],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line)) {
aldap_free_entry(ldap_attrs);
- goto next_entry;
+ goto next_pwdentry;
}
- if (i == ATTR_GR_GID) {
+ if (i == ATTR_UID) {
ir.ir_key.ik_uid = strtonum(
- ldap_attrs[0], 0, GID_MAX, NULL);
+ ldap_attrs[0], 0, UID_MAX, NULL);
}
aldap_free_entry(ldap_attrs);
}
- if (i != ATTR_GR_MEMBERS)
+ if (i != ATTR_SHELL)
if (strlcat(ir.ir_line, ":",
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- goto next_entry;
+ goto next_pwdentry;
}
- imsg_compose(env->sc_ibuf, IMSG_GRP_ENTRY, 0, 0,
+ imsg_compose(env->sc_ibuf, IMSG_PW_ENTRY, 0, 0,
&ir, sizeof(ir));
-next_entry:
+next_pwdentry:
aldap_freemsg(m);
}
- return (0);
-bad:
- log_debug("directory %s errored out in %s", idm->idm_name, where);
- return (-1);
-}
-
-int
-do_build_passwd(struct env *env, struct idm *idm, struct aldap *al, char
- *basedn, enum scope scope, char *filter)
-{
- char *attrs[ATTR_MAX+1];
- char **ldap_attrs;
- struct idm_req ir;
- struct aldap_message *m;
- int i, j, k;
- const char *where;
-#ifdef REFERRALS
- static int refcnt = 0;
- struct aldap_url lu;
- struct aldap *al_ref;
-#endif
-
bzero(attrs, sizeof(attrs));
- for (i = 0, j = 0; i < ATTR_MAX; i++) {
+ for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
if (idm->idm_flags & F_FIXED_ATTR(i))
continue;
attrs[j++] = idm->idm_attrs[i];
@@ -529,11 +567,12 @@ do_build_passwd(struct env *env, struct idm *idm, struct aldap *al, char
attrs[j] = NULL;
where = "search";
- if(aldap_search(al, basedn, scope, filter, attrs, 0, 0, 0) == -1)
+ if(aldap_search(al, idm->idm_basedn, LDAP_SCOPE_SUBTREE,
+ idm->idm_filters[FILTER_GROUP], attrs, 0, 0, 0) == -1)
goto bad;
/*
- * build password line.
+ * build group line.
*/
while((m = aldap_parse(al)) != NULL) {
where = "verifying msgid";
@@ -541,36 +580,6 @@ do_build_passwd(struct env *env, struct idm *idm, struct aldap *al, char
aldap_freemsg(m);
goto bad;
}
-#ifdef REFERRALS
- /* continuation referral */
- if (m->message_type == LDAP_RES_SEARCH_REFERENCE) {
- if(refcnt++ >= MAX_REFERRALS)
- goto next_entry;
-
- if((al_ref = connect_to_referral(m, &lu)) == NULL)
- goto next_entry;
- do_build_passwd(env, idm, al_ref, lu.dn,
- lu.scope, idm->idm_filters[FILTER_USER]);
- aldap_close(al_ref);
- goto next_entry;
- }
- /* normal referral */
- if(m->message_type == LDAP_RES_SEARCH_RESULT &&
- aldap_get_resultcode(m) == LDAP_REFERRAL) {
- if(refcnt++ == MAX_REFERRALS) {
- aldap_freemsg(m);
- break;
- }
-
- if((al_ref = connect_to_referral(m, &lu)) == NULL) {
- aldap_freemsg(m);
- break;
- }
- do_build_passwd(env, idm, al_ref, lu.dn,
- lu.scope, idm->idm_filters[FILTER_USER]);
- aldap_close(al_ref);
- }
-#endif
/* end of the search result chain */
if (m->message_type == LDAP_RES_SEARCH_RESULT) {
aldap_freemsg(m);
@@ -584,24 +593,24 @@ do_build_passwd(struct env *env, struct idm *idm, struct aldap *al, char
}
/* search entry */
bzero(&ir, sizeof(ir));
- for (i = 0, j = 0; i < ATTR_MAX; i++) {
+ for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) {
if (idm->idm_flags & F_FIXED_ATTR(i)) {
if (strlcat(ir.ir_line, idm->idm_attrs[i],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
/*
* entry yields a line > 1024, trash it.
*/
- goto next_entry;
- if (i == ATTR_UID) {
- ir.ir_key.ik_uid = strtonum(
+ goto next_grpentry;
+ if (i == ATTR_GR_GID) {
+ ir.ir_key.ik_gid = strtonum(
idm->idm_attrs[i], 0,
- UID_MAX, NULL);
+ GID_MAX, NULL);
}
} else if (idm->idm_list & F_LIST(i)) {
if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_entry;
- if (ldap_attrs == NULL || ldap_attrs[0] == NULL)
- goto next_entry;
+ goto next_grpentry;
+ if (ldap_attrs[0] == NULL)
+ goto next_grpentry;
for(k = 0; k >= 0 && ldap_attrs[k] != NULL; k++) {
if (strlcat(ir.ir_line, ldap_attrs[k],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
@@ -611,106 +620,93 @@ do_build_passwd(struct env *env, struct idm *idm, struct aldap *al, char
sizeof(ir.ir_line))
>= sizeof(ir.ir_line)) {
aldap_free_entry(ldap_attrs);
- goto next_entry;
+ goto next_grpentry;
}
}
aldap_free_entry(ldap_attrs);
} else {
- if (aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
- goto next_entry;
- if (ldap_attrs == NULL || ldap_attrs[0] == NULL)
- goto next_entry;
+ if(aldap_match_entry(m, attrs[j++], &ldap_attrs) == -1)
+ goto next_grpentry;
+ if (ldap_attrs[0] == NULL)
+ goto next_grpentry;
if (strlcat(ir.ir_line, ldap_attrs[0],
sizeof(ir.ir_line)) >= sizeof(ir.ir_line)) {
aldap_free_entry(ldap_attrs);
- goto next_entry;
+ goto next_grpentry;
}
- if (i == ATTR_UID) {
+ if (i == ATTR_GR_GID) {
ir.ir_key.ik_uid = strtonum(
- ldap_attrs[0], 0, UID_MAX, NULL);
+ ldap_attrs[0], 0, GID_MAX, NULL);
}
aldap_free_entry(ldap_attrs);
}
- if (i != ATTR_SHELL)
+ if (i != ATTR_GR_MEMBERS)
if (strlcat(ir.ir_line, ":",
sizeof(ir.ir_line)) >= sizeof(ir.ir_line))
- goto next_entry;
+ goto next_grpentry;
}
- imsg_compose(env->sc_ibuf, IMSG_PW_ENTRY, 0, 0,
+ imsg_compose(env->sc_ibuf, IMSG_GRP_ENTRY, 0, 0,
&ir, sizeof(ir));
-next_entry:
+next_grpentry:
aldap_freemsg(m);
}
+ aldap_close(al);
+
+ idm->idm_state = STATE_LDAP_DONE;
+
return (0);
bad:
log_debug("directory %s errored out in %s", idm->idm_name, where);
return (-1);
}
-
-int
-client_try_idm(struct env *env, struct idm *idm)
+void
+client_periodic_update(int fd, short event, void *p)
{
- const char *where;
- struct idm_req ir;
- struct aldap_message *m;
- struct aldap *al;
+ struct env *env = p;
- bzero(&ir, sizeof(ir));
- imsg_compose(env->sc_ibuf, IMSG_START_UPDATE, 0, 0, &ir, sizeof(ir));
+ struct idm *idm;
+ int fail_cnt = 0;
- where = "connect";
- if((al = aldap_openidm(idm)) == NULL)
- goto bad;
+ /* If LDAP isn't finished, notify the master process to trash the
+ * update. */
+ TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
+ if(idm->idm_state < STATE_LDAP_DONE)
+ fail_cnt++;
- if (idm->idm_flags & F_NEEDAUTH) {
- where = "binding";
- if(aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
- goto bad;
+ idm->idm_state = STATE_NONE;
- where = "parsing";
- if((m = aldap_parse(al)) == NULL)
- goto bad;
- where = "verifying msgid";
- if(al->msgid != m->msgid) {
- aldap_freemsg(m);
- goto bad;
- }
- aldap_freemsg(m);
+ client_addr_free(idm);
+ }
+ if(fail_cnt > 0) {
+ log_debug("trash the update");
+ imsg_compose(env->sc_ibuf, IMSG_TRASH_UPDATE, 0, 0, NULL, 0);
}
- do_build_passwd(env, idm, al, idm->idm_basedn, LDAP_SCOPE_SUBTREE,
- idm->idm_filters[FILTER_USER]);
- do_build_group(env, idm, al, idm->idm_basedn, LDAP_SCOPE_SUBTREE,
- idm->idm_filters[FILTER_GROUP]);
-
- aldap_close(al);
-
- return (0);
-bad:
- log_debug("directory %s errored out in %s", idm->idm_name, where);
- return (-1);
+ client_configure(env);
}
void
client_configure(struct env *env)
{
- enum imsg_type finish;
struct timeval tv;
struct idm *idm;
+ u_int16_t dlen;
log_debug("connecting to directories");
- finish = IMSG_END_UPDATE;
+
imsg_compose(env->sc_ibuf, IMSG_START_UPDATE, 0, 0, NULL, 0);
- TAILQ_FOREACH(idm, &env->sc_idms, idm_entry)
- if (client_try_idm(env, idm) == -1) {
- finish = IMSG_TRASH_UPDATE;
- break;
- }
- imsg_compose(env->sc_ibuf, finish, 0, 0, NULL, 0);
+
+ /* Start the DNS lookups */
+ TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) {
+ dlen = strlen(idm->idm_name) + 1;
+ imsg_compose(env->sc_ibuf_dns, IMSG_HOST_DNS, idm->idm_id, 0,
+ idm->idm_name, dlen);
+ }
+
tv.tv_sec = env->sc_conf_tv.tv_sec;
tv.tv_usec = env->sc_conf_tv.tv_usec;
- evtimer_set(&env->sc_conf_ev, client_configure_wrapper, env);
+ evtimer_set(&env->sc_conf_ev, client_periodic_update, env);
evtimer_add(&env->sc_conf_ev, &tv);
}
diff --git a/usr.sbin/ypldap/parse.y b/usr.sbin/ypldap/parse.y
index a74da98f077..f7d6cd9f72f 100644
--- a/usr.sbin/ypldap/parse.y
+++ b/usr.sbin/ypldap/parse.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.y,v 1.5 2008/10/19 12:00:54 aschrijver Exp $ */
+/* $OpenBSD: parse.y,v 1.6 2008/10/28 13:47:22 aschrijver Exp $ */
/*
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -71,8 +71,6 @@ int lgetc(int);
int lungetc(int);
int findeol(void);
-struct addrinfo *host(const char *, const char *);
-
TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
struct sym {
TAILQ_ENTRY(sym) entry;
@@ -253,19 +251,14 @@ diropt : BINDDN STRING {
directory : DIRECTORY STRING port {
if ((idm = calloc(1, sizeof(*idm))) == NULL)
fatal(NULL);
+ idm->idm_id = conf->sc_maxid++;
- if($3 != NULL) {
- if((idm->idm_addrinfo = host($2, $3)) == NULL) {
- yyerror("domain not found");
- free($2);
- YYERROR;
- }
- } else {
- if((idm->idm_addrinfo = host($2, "389")) == NULL) {
- yyerror("domain not found");
- free($2);
- YYERROR;
- }
+ if (strlcpy(idm->idm_name, $2,
+ sizeof(idm->idm_name)) >=
+ sizeof(idm->idm_name)) {
+ yyerror("attribute truncated");
+ free($2);
+ YYERROR;
}
free($2);
@@ -816,28 +809,3 @@ symget(const char *nam)
}
return (NULL);
}
-
-struct addrinfo *
-host(const char *s, const char *port)
-{
- int error;
- struct addrinfo *res, hints;
-
- if (s == NULL)
- return NULL;
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- if ((error = getaddrinfo(s, port, &hints, &res)) != 0) {
- errx(1, "getaddrinfo: %s", gai_strerror(error));
-
- freeaddrinfo(res);
-
- return NULL;
- }
-
- return res;
-}
diff --git a/usr.sbin/ypldap/ypldap.h b/usr.sbin/ypldap/ypldap.h
index 63fc567b19e..758e551d5de 100644
--- a/usr.sbin/ypldap/ypldap.h
+++ b/usr.sbin/ypldap/ypldap.h
@@ -21,6 +21,8 @@
#define FILTER_WIDTH 128
#define ATTR_WIDTH 32
+#define MAX_SERVERS_DNS 8
+
/* buffer */
struct buf {
TAILQ_ENTRY(buf) entry;
@@ -73,7 +75,13 @@ enum imsg_type {
IMSG_END_UPDATE,
IMSG_TRASH_UPDATE,
IMSG_PW_ENTRY,
- IMSG_GRP_ENTRY
+ IMSG_GRP_ENTRY,
+ IMSG_HOST_DNS
+};
+
+struct ypldap_addr {
+ struct ypldap_addr *next;
+ struct sockaddr_storage ss;
};
struct imsg_hdr {
@@ -107,20 +115,31 @@ struct groupent {
char *ge_line;
};
+enum client_state {
+ STATE_NONE,
+ STATE_DNS_INPROGRESS,
+ STATE_DNS_TEMPFAIL,
+ STATE_DNS_DONE,
+ STATE_LDAP_FAIL,
+ STATE_LDAP_DONE
+};
+
/*
* beck, djm, dlg: pay attention to the struct name
*/
struct idm {
TAILQ_ENTRY(idm) idm_entry;
+ u_int32_t idm_id;
char idm_name[MAXHOSTNAMELEN];
#define F_SSL 0x00100000
#define F_CONFIGURING 0x00200000
#define F_NEEDAUTH 0x00400000
#define F_FIXED_ATTR(n) (1<<n)
#define F_LIST(n) (1<<n)
+ enum client_state idm_state;
u_int32_t idm_flags; /* lower 20 reserved */
u_int32_t idm_list;
- struct addrinfo *idm_addrinfo;
+ struct ypldap_addr *idm_addr;
in_port_t idm_port;
char idm_binddn[LINE_WIDTH];
char idm_bindcred[LINE_WIDTH];
@@ -173,11 +192,14 @@ struct env {
#define YPMAP_GROUP_BYGID 0x00000020
u_int32_t sc_flags;
+ u_int32_t sc_maxid;
+
char sc_domainname[MAXHOSTNAMELEN];
struct timeval sc_conf_tv;
struct event sc_conf_ev;
TAILQ_HEAD(idm_list, idm) sc_idms;
struct imsgbuf *sc_ibuf;
+ struct imsgbuf *sc_ibuf_dns;
RB_HEAD(user_name_tree,userent) *sc_user_names;
RB_HEAD(user_uid_tree,userent) sc_user_uids;
@@ -255,3 +277,6 @@ RB_PROTOTYPE( group_gid_tree, groupent, ge_gid_node, groupent_gid_cmp);
/* yp.c */
void yp_init(struct env *);
void yp_enable_events(void);
+
+/* ypldap_dns.c */
+pid_t ypldap_dns(int[2], struct passwd *);
diff --git a/usr.sbin/ypldap/ypldap_dns.c b/usr.sbin/ypldap/ypldap_dns.c
new file mode 100644
index 00000000000..b3c7a27159b
--- /dev/null
+++ b/usr.sbin/ypldap/ypldap_dns.c
@@ -0,0 +1,241 @@
+/* $OpenBSD: ypldap_dns.c,v 1.1 2008/10/28 13:47:22 aschrijver Exp $ */
+
+/*
+ * Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/tree.h>
+#include <sys/queue.h>
+
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+
+#include <netdb.h>
+#include <pwd.h>
+#include <errno.h>
+#include <event.h>
+#include <resolv.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ypldap.h"
+
+volatile sig_atomic_t quit_dns = 0;
+struct imsgbuf *ibuf_dns;
+
+void dns_dispatch_imsg(int, short, void *);
+void dns_sig_handler(int, short, void *);
+void dns_shutdown(void);
+int host_dns(const char *s, struct ypldap_addr **hn);
+
+void
+dns_sig_handler(int sig, short event, void *p)
+{
+ switch (sig) {
+ case SIGINT:
+ case SIGTERM:
+ dns_shutdown();
+ break;
+ default:
+ fatalx("unexpected signal");
+ }
+}
+
+void
+dns_shutdown(void)
+{
+ log_info("dns engine exiting");
+ _exit(0);
+}
+
+pid_t
+ypldap_dns(int pipe_ntp[2], struct passwd *pw)
+{
+ pid_t pid;
+ struct event ev_sigint;
+ struct event ev_sigterm;
+ struct event ev_sighup;
+ struct env env;
+
+ switch (pid = fork()) {
+ case -1:
+ fatal("cannot fork");
+ break;
+ case 0:
+ break;
+ default:
+ return (pid);
+ }
+
+ setproctitle("dns engine");
+ close(pipe_ntp[0]);
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
+ setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
+ fatal("can't drop privileges");
+ endservent();
+
+ event_init();
+ signal_set(&ev_sigint, SIGINT, dns_sig_handler, NULL);
+ signal_set(&ev_sigterm, SIGTERM, dns_sig_handler, NULL);
+ signal_set(&ev_sighup, SIGHUP, dns_sig_handler, NULL);
+ signal_add(&ev_sigint, NULL);
+ signal_add(&ev_sigterm, NULL);
+ signal_add(&ev_sighup, NULL);
+
+ if ((env.sc_ibuf = calloc(1, sizeof(*env.sc_ibuf))) == NULL)
+ fatal(NULL);
+
+ env.sc_ibuf->events = EV_READ;
+ env.sc_ibuf->data = &env;
+ imsg_init(env.sc_ibuf, pipe_ntp[1], dns_dispatch_imsg);
+ event_set(&env.sc_ibuf->ev, env.sc_ibuf->fd, env.sc_ibuf->events,
+ env.sc_ibuf->handler, &env);
+ event_add(&env.sc_ibuf->ev, NULL);
+
+ event_dispatch();
+ dns_shutdown();
+
+ return (0);
+}
+
+void
+dns_dispatch_imsg(int fd, short event, void *p)
+{
+ struct imsg imsg;
+ int n, cnt;
+ char *name;
+ struct ypldap_addr *h, *hn;
+ struct buf *buf;
+ struct env *env = p;
+ struct imsgbuf *ibuf = env->sc_ibuf;
+ int shut = 0;
+
+ switch (event) {
+ case EV_READ:
+ if ((n = imsg_read(ibuf)) == -1)
+ fatal("imsg_read error");
+ if (n == 0)
+ shut = 1;
+ break;
+ case EV_WRITE:
+ if (msgbuf_write(&ibuf->w) == -1)
+ fatal("msgbuf_write");
+ imsg_event_add(ibuf);
+ return;
+ default:
+ fatalx("unknown event");
+ }
+
+ for (;;) {
+ if ((n = imsg_get(ibuf, &imsg)) == -1)
+ fatal("client_dispatch_parent: imsg_read_error");
+ if (n == 0)
+ break;
+
+ switch (imsg.hdr.type) {
+ case IMSG_HOST_DNS:
+ name = imsg.data;
+ if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
+ fatalx("invalid IMSG_HOST_DNS received");
+ imsg.hdr.len -= 1 + IMSG_HEADER_SIZE;
+ if (name[imsg.hdr.len] != '\0' ||
+ strlen(name) != imsg.hdr.len)
+ fatalx("invalid IMSG_HOST_DNS received");
+ if ((cnt = host_dns(name, &hn)) == -1)
+ break;
+ buf = imsg_create(ibuf, IMSG_HOST_DNS,
+ imsg.hdr.peerid, 0,
+ cnt * sizeof(struct sockaddr_storage));
+ if (buf == NULL)
+ break;
+ if (cnt > 0)
+ for (h = hn; h != NULL; h = h->next)
+ imsg_add(buf, &h->ss, sizeof(h->ss));
+
+ imsg_close(ibuf, buf);
+ break;
+ default:
+ break;
+ }
+ imsg_free(&imsg);
+ }
+ if (!shut)
+ imsg_event_add(ibuf);
+ else {
+ /* this pipe is dead, so remove the event handler */
+ event_del(&ibuf->ev);
+ event_loopexit(NULL);
+ }
+}
+
+int
+host_dns(const char *s, struct ypldap_addr **hn)
+{
+ struct addrinfo hints, *res0, *res;
+ int error, cnt = 0;
+ struct sockaddr_in *sa_in;
+ struct sockaddr_in6 *sa_in6;
+ struct ypldap_addr *h, *hh = NULL;
+
+ bzero(&hints, sizeof(hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
+ error = getaddrinfo(s, NULL, &hints, &res0);
+ if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
+ return (0);
+ if (error) {
+ log_warnx("could not parse \"%s\": %s", s,
+ gai_strerror(error));
+ return (-1);
+ }
+
+ for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
+ if (res->ai_family != AF_INET &&
+ res->ai_family != AF_INET6)
+ continue;
+ if ((h = calloc(1, sizeof(struct ypldap_addr))) == NULL)
+ fatal(NULL);
+ h->ss.ss_family = res->ai_family;
+ if (res->ai_family == AF_INET) {
+ sa_in = (struct sockaddr_in *)&h->ss;
+ sa_in->sin_len = sizeof(struct sockaddr_in);
+ sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
+ res->ai_addr)->sin_addr.s_addr;
+ } else {
+ sa_in6 = (struct sockaddr_in6 *)&h->ss;
+ sa_in6->sin6_len = sizeof(struct sockaddr_in6);
+ memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
+ res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
+ }
+
+ h->next = hh;
+ hh = h;
+ cnt++;
+ }
+ freeaddrinfo(res0);
+
+ *hn = hh;
+ return (cnt);
+}