1/* $OpenBSD: ldapclient.c,v 1.31 2014/11/16 23:24:44 tedu Exp $ */
2/* $FreeBSD: head/usr.sbin/ypldap/ldapclient.c 297907 2016-04-13 03:36:34Z 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.
--- 41 unchanged lines hidden (view full) ---
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)
--- 14 unchanged lines hidden (view full) ---
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:
--- 9 unchanged lines hidden (view full) ---
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:
--- 47 unchanged lines hidden (view full) ---
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
--- 347 unchanged lines hidden (view full) ---
572{
573 const char *where;
574 char *attrs[ATTR_MAX+1];
575 int i, j;
576 struct aldap_message *m;
577 struct aldap *al;
578
579 where = "connect";
580 if ((al = client_aldap_open(&idm->idm_addr)) == NULL)
581 return (-1);
582
583 if (idm->idm_flags & F_NEEDAUTH) {
584 where = "binding";
585 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
586 goto bad;
587
588 where = "parsing";
--- 106 unchanged lines hidden ---
2/* $FreeBSD: head/usr.sbin/ypldap/ldapclient.c 297907 2016-04-13 03:36:34Z 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.
--- 41 unchanged lines hidden (view full) ---
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)
--- 14 unchanged lines hidden (view full) ---
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:
--- 9 unchanged lines hidden (view full) ---
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:
--- 47 unchanged lines hidden (view full) ---
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
--- 347 unchanged lines hidden (view full) ---
572{
573 const char *where;
574 char *attrs[ATTR_MAX+1];
575 int i, j;
576 struct aldap_message *m;
577 struct aldap *al;
578
579 where = "connect";
580 if ((al = client_aldap_open(&idm->idm_addr)) == NULL)
581 return (-1);
582
583 if (idm->idm_flags & F_NEEDAUTH) {
584 where = "binding";
585 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
586 goto bad;
587
588 where = "parsing";
--- 106 unchanged lines hidden ---