1/* $OpenBSD: ldapclient.c,v 1.31 2014/11/16 23:24:44 tedu Exp $ */
2/* $FreeBSD: head/usr.sbin/ypldap/ldapclient.c 292273 2015-12-15 15:46:14Z 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 *);
61
62/*
63 * dummy wrapper to provide aldap_init with its fd's.
64 */
65struct aldap *
66client_aldap_open(struct ypldap_addr *addr)
67{
68 int fd = -1;
69 struct ypldap_addr *p;
70
71 for (p = addr; p != NULL; p = p->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 for (h = idm->idm_addr; h != NULL; h = h->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, *p;
129
130 if (idm->idm_addr == NULL)
131 return (-1);
132
133 for (h = idm->idm_addr; h != NULL; h = p) {
134 p = h->next;
135 free(h);
136 }
137
138 idm->idm_addr = NULL;
139
140 return (0);
141}
142
143void
144client_sig_handler(int sig, short event, void *p)
145{
146 switch (sig) {
147 case SIGINT:
--- 47 unchanged lines hidden (view full) ---
195 case IMSG_HOST_DNS:
196 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry)
197 if (idm->idm_id == imsg.hdr.peerid)
198 break;
199 if (idm == NULL) {
200 log_warnx("IMSG_HOST_DNS with invalid peerID");
201 break;
202 }
203 if (idm->idm_addr != NULL) {
204 log_warnx("IMSG_HOST_DNS but addr != NULL!");
205 break;
206 }
207
208 dlen = imsg.hdr.len - IMSG_HEADER_SIZE;
209 if (dlen == 0) { /* no data -> temp error */
210 idm->idm_state = STATE_DNS_TEMPFAIL;
211 break;
212 }
213
214 data = (u_char *)imsg.data;
215 while (dlen >= sizeof(struct sockaddr_storage)) {
216 if ((h = calloc(1, sizeof(struct ypldap_addr))) ==
217 NULL)
218 fatal(NULL);
219 memcpy(&h->ss, data, sizeof(h->ss));
220
221 if (idm->idm_addr == NULL)
222 h->next = NULL;
223 else
224 h->next = idm->idm_addr;
225
226 idm->idm_addr = h;
227
228 data += sizeof(h->ss);
229 dlen -= sizeof(h->ss);
230 }
231 if (dlen != 0)
232 fatalx("IMSG_HOST_DNS: dlen != 0");
233
234 client_addr_init(idm);
235
--- 347 unchanged lines hidden (view full) ---
583{
584 const char *where;
585 char *attrs[ATTR_MAX+1];
586 int i, j;
587 struct aldap_message *m;
588 struct aldap *al;
589
590 where = "connect";
591 if ((al = client_aldap_open(idm->idm_addr)) == NULL)
592 return (-1);
593
594 if (idm->idm_flags & F_NEEDAUTH) {
595 where = "binding";
596 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
597 goto bad;
598
599 where = "parsing";
--- 106 unchanged lines hidden ---
2/* $FreeBSD: head/usr.sbin/ypldap/ldapclient.c 292273 2015-12-15 15:46:14Z 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 *);
61
62/*
63 * dummy wrapper to provide aldap_init with its fd's.
64 */
65struct aldap *
66client_aldap_open(struct ypldap_addr *addr)
67{
68 int fd = -1;
69 struct ypldap_addr *p;
70
71 for (p = addr; p != NULL; p = p->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 for (h = idm->idm_addr; h != NULL; h = h->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, *p;
129
130 if (idm->idm_addr == NULL)
131 return (-1);
132
133 for (h = idm->idm_addr; h != NULL; h = p) {
134 p = h->next;
135 free(h);
136 }
137
138 idm->idm_addr = NULL;
139
140 return (0);
141}
142
143void
144client_sig_handler(int sig, short event, void *p)
145{
146 switch (sig) {
147 case SIGINT:
--- 47 unchanged lines hidden (view full) ---
195 case IMSG_HOST_DNS:
196 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry)
197 if (idm->idm_id == imsg.hdr.peerid)
198 break;
199 if (idm == NULL) {
200 log_warnx("IMSG_HOST_DNS with invalid peerID");
201 break;
202 }
203 if (idm->idm_addr != NULL) {
204 log_warnx("IMSG_HOST_DNS but addr != NULL!");
205 break;
206 }
207
208 dlen = imsg.hdr.len - IMSG_HEADER_SIZE;
209 if (dlen == 0) { /* no data -> temp error */
210 idm->idm_state = STATE_DNS_TEMPFAIL;
211 break;
212 }
213
214 data = (u_char *)imsg.data;
215 while (dlen >= sizeof(struct sockaddr_storage)) {
216 if ((h = calloc(1, sizeof(struct ypldap_addr))) ==
217 NULL)
218 fatal(NULL);
219 memcpy(&h->ss, data, sizeof(h->ss));
220
221 if (idm->idm_addr == NULL)
222 h->next = NULL;
223 else
224 h->next = idm->idm_addr;
225
226 idm->idm_addr = h;
227
228 data += sizeof(h->ss);
229 dlen -= sizeof(h->ss);
230 }
231 if (dlen != 0)
232 fatalx("IMSG_HOST_DNS: dlen != 0");
233
234 client_addr_init(idm);
235
--- 347 unchanged lines hidden (view full) ---
583{
584 const char *where;
585 char *attrs[ATTR_MAX+1];
586 int i, j;
587 struct aldap_message *m;
588 struct aldap *al;
589
590 where = "connect";
591 if ((al = client_aldap_open(idm->idm_addr)) == NULL)
592 return (-1);
593
594 if (idm->idm_flags & F_NEEDAUTH) {
595 where = "binding";
596 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1)
597 goto bad;
598
599 where = "parsing";
--- 106 unchanged lines hidden ---