-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresolve_host.c
304 lines (263 loc) · 7.57 KB
/
resolve_host.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* resolving DNS hostname routine
*
* Copyright (C) 2010 Jeff Layton ([email protected])
* Copyright (C) 2010 Igor Druzhinin ([email protected])
* Copyright (C) 2024 David Voit ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <resolv.h>
#include "mount.h"
#include "util.h"
#include "cldap_ping.h"
#include "resolve_host.h"
/*
* resolve hostname to comma-separated list of address(es)
*/
int resolve_host(const char *host, char *addrstr) {
int rc;
/* 10 for max width of decimal scopeid */
char tmpbuf[NI_MAXHOST + 1 + 10 + 1];
const char *ipaddr;
size_t len;
struct addrinfo *addrlist, *addr;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
size_t count_v4 = 0, count_v6 = 0;
rc = getaddrinfo(host, NULL, NULL, &addrlist);
if (rc != 0)
return EX_USAGE;
addr = addrlist;
while (addr) {
/* skip non-TCP entries */
if (addr->ai_socktype != SOCK_STREAM ||
addr->ai_protocol != IPPROTO_TCP) {
addr = addr->ai_next;
continue;
}
switch (addr->ai_addr->sa_family) {
case AF_INET6:
count_v6++;
if (count_v6 + count_v4 > MAX_ADDRESSES) {
addr = addr->ai_next;
continue;
}
sin6 = (struct sockaddr_in6 *) addr->ai_addr;
ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
if (sin6->sin6_scope_id) {
len = strnlen(tmpbuf, sizeof(tmpbuf));
snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%%%u",
sin6->sin6_scope_id);
}
break;
case AF_INET:
count_v4++;
if (count_v6 + count_v4 > MAX_ADDRESSES) {
addr = addr->ai_next;
continue;
}
sin = (struct sockaddr_in *) addr->ai_addr;
ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
break;
default:
addr = addr->ai_next;
continue;
}
if (addr == addrlist)
*addrstr = '\0';
else
strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
addr = addr->ai_next;
}
// Is this a DFS domain where we need to do a cldap ping to find the closest node?
if (count_v4 > 1 || count_v6 > 1) {
int res;
ns_msg global_domain_handle;
unsigned char global_domain_lookup[4096];
ns_msg site_domain_handle;
unsigned char site_domain_lookup[4096];
char dname[MAXCDNAME];
int srv_cnt;
res = res_init();
if (res != 0)
goto resolve_host_out;
res = snprintf(dname, MAXCDNAME, "_ldap._tcp.dc._msdcs.%s", host);
if (res < 0)
goto resolve_host_out;
res = res_query(dname, C_IN, ns_t_srv, global_domain_lookup, sizeof(global_domain_lookup));
if (res < 0)
goto resolve_host_out;
// res is also the size of the response_buffer
res = ns_initparse(global_domain_lookup, res, &global_domain_handle);
if (res < 0)
goto resolve_host_out;
srv_cnt = ns_msg_count (global_domain_handle, ns_s_an);
// No or just one DC we are done
if (srv_cnt < 2)
goto resolve_host_out;
char site_name[MAXCDNAME];
// We assume that AD always sends the ip addresses in the addtional data block
for (int i = 0; i < ns_msg_count(global_domain_handle, ns_s_ar); i++) {
ns_rr rr;
res = ns_parserr(&global_domain_handle, ns_s_ar, i, &rr);
if (res < 0)
goto resolve_host_out;
switch (ns_rr_type(rr)) {
case ns_t_aaaa:
if (ns_rr_rdlen(rr) != NS_IN6ADDRSZ)
continue;
res = cldap_ping((char *) host, AF_INET6, (void *)ns_rr_rdata(rr), site_name);
break;
case ns_t_a:
if (ns_rr_rdlen(rr) != NS_INADDRSZ)
continue;
res = cldap_ping((char *) host, AF_INET, (void *)ns_rr_rdata(rr), site_name);
break;
default:
continue;
}
if (res == CLDAP_PING_TRYNEXT) {
continue;
}
if (res < 0) {
goto resolve_host_out;
}
if (site_name[0] == '\0') {
goto resolve_host_out;
} else {
// site found - leave loop
break;
}
}
res = snprintf(dname, MAXCDNAME, "_ldap._tcp.%s._sites.dc._msdcs.%s", site_name, host);
if (res < 0) {
goto resolve_host_out;
}
res = res_query(dname, C_IN, ns_t_srv, site_domain_lookup, sizeof(site_domain_lookup));
if (res < 0)
goto resolve_host_out;
// res is also the size of the response_buffer
res = ns_initparse(site_domain_lookup, res, &site_domain_handle);
if (res < 0)
goto resolve_host_out;
int number_addresses = 0;
for (int i = 0; i < ns_msg_count(site_domain_handle, ns_s_ar); i++) {
if (i > MAX_ADDRESSES)
break;
ns_rr rr;
res = ns_parserr(&site_domain_handle, ns_s_ar, i, &rr);
if (res < 0)
goto resolve_host_out;
switch (ns_rr_type(rr)) {
case ns_t_aaaa:
if (ns_rr_rdlen(rr) != NS_IN6ADDRSZ)
continue;
ipaddr = inet_ntop(AF_INET6, ns_rr_rdata(rr), tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
break;
case ns_t_a:
if (ns_rr_rdlen(rr) != NS_INADDRSZ)
continue;
ipaddr = inet_ntop(AF_INET, ns_rr_rdata(rr), tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
break;
default:
continue;
}
number_addresses++;
if (i == 0)
*addrstr = '\0';
else
strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
}
// Preferred site ips is now the first entry in addrstr, fill up with other sites till MAX_ADDRESS
for (int i = 0; i < ns_msg_count(global_domain_handle, ns_s_ar); i++) {
if (number_addresses > MAX_ADDRESSES)
break;
ns_rr rr;
res = ns_parserr(&global_domain_handle, ns_s_ar, i, &rr);
if (res < 0)
goto resolve_host_out;
switch (ns_rr_type(rr)) {
case ns_t_aaaa:
if (ns_rr_rdlen(rr) != NS_IN6ADDRSZ)
continue;
ipaddr = inet_ntop(AF_INET6, ns_rr_rdata(rr), tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
break;
case ns_t_a:
if (ns_rr_rdlen(rr) != NS_INADDRSZ)
continue;
ipaddr = inet_ntop(AF_INET, ns_rr_rdata(rr), tmpbuf,
sizeof(tmpbuf));
if (!ipaddr) {
rc = EX_SYSERR;
goto resolve_host_out;
}
break;
default:
continue;
}
char *found = strstr(addrstr, tmpbuf);
if (found) {
// We only have a real match if the substring is between ',' or it's the last/first entry in the list
char previous_seperator = found > addrstr ? *(found-1) : '\0';
char next_seperator = *(found+strlen(tmpbuf));
if ((next_seperator == ',' || next_seperator == '\0')
&& (previous_seperator == ',' || previous_seperator == '\0')) {
continue;
}
}
number_addresses++;
strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
}
}
resolve_host_out:
freeaddrinfo(addrlist);
return rc;
}