]>
Commit | Line | Data |
---|---|---|
8041b5ba | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
4e8c8252 | 2 | |
4e8c8252 | 3 | /*** |
8041b5ba | 4 | This file is part of nss-myhostname. |
4e8c8252 | 5 | |
8041b5ba | 6 | Copyright 2008-2011 Lennart Poettering |
4e8c8252 | 7 | |
8041b5ba LP |
8 | nss-myhostname is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Lesser General Public License | |
10 | as published by the Free Software Foundation; either version 2.1 of | |
11 | the License, or (at your option) any later version. | |
4e8c8252 | 12 | |
8041b5ba LP |
13 | nss-myhostname is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
4e8c8252 | 17 | |
8041b5ba LP |
18 | You should have received a copy of the GNU Lesser General Public |
19 | License along with nss-myhostname; If not, see | |
20 | <http://www.gnu.org/licenses/>. | |
4e8c8252 | 21 | ***/ |
6b21f0cf LP |
22 | |
23 | #include <limits.h> | |
24 | #include <nss.h> | |
25 | #include <sys/types.h> | |
26 | #include <netdb.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
6b21f0cf LP |
29 | #include <assert.h> |
30 | #include <unistd.h> | |
4e8c8252 | 31 | #include <net/if.h> |
8041b5ba LP |
32 | #include <stdlib.h> |
33 | #include <arpa/inet.h> | |
34 | ||
1df2a0fc | 35 | #include "ifconf.h" |
4e8c8252 LP |
36 | |
37 | /* We use 127.0.0.2 as IPv4 address. This has the advantage over | |
38 | * 127.0.0.1 that it can be translated back to the local hostname. For | |
39 | * IPv6 we use ::1 which unfortunately will not translate back to the | |
40 | * hostname but instead something like "localhost6" or so. */ | |
41 | ||
42 | #define LOCALADDRESS_IPV4 (htonl(0x7F000002)) | |
43 | #define LOCALADDRESS_IPV6 &in6addr_loopback | |
44 | #define LOOPBACK_INTERFACE "lo" | |
45 | ||
46 | #define ALIGN(a) (((a+sizeof(void*)-1)/sizeof(void*))*sizeof(void*)) | |
47 | ||
8041b5ba LP |
48 | enum nss_status _nss_myhostname_gethostbyname4_r( |
49 | const char *name, | |
50 | struct gaih_addrtuple **pat, | |
51 | char *buffer, size_t buflen, | |
52 | int *errnop, int *h_errnop, | |
fe0fc11b | 53 | int32_t *ttlp) _public_; |
8041b5ba LP |
54 | |
55 | enum nss_status _nss_myhostname_gethostbyname3_r( | |
56 | const char *name, | |
57 | int af, | |
58 | struct hostent *host, | |
59 | char *buffer, size_t buflen, | |
60 | int *errnop, int *h_errnop, | |
61 | int32_t *ttlp, | |
fe0fc11b | 62 | char **canonp) _public_; |
8041b5ba LP |
63 | |
64 | enum nss_status _nss_myhostname_gethostbyname2_r( | |
65 | const char *name, | |
66 | int af, | |
67 | struct hostent *host, | |
68 | char *buffer, size_t buflen, | |
fe0fc11b | 69 | int *errnop, int *h_errnop) _public_; |
8041b5ba LP |
70 | |
71 | enum nss_status _nss_myhostname_gethostbyname_r( | |
72 | const char *name, | |
73 | struct hostent *host, | |
74 | char *buffer, size_t buflen, | |
fe0fc11b | 75 | int *errnop, int *h_errnop) _public_; |
8041b5ba LP |
76 | |
77 | enum nss_status _nss_myhostname_gethostbyaddr2_r( | |
78 | const void* addr, socklen_t len, | |
79 | int af, | |
80 | struct hostent *host, | |
81 | char *buffer, size_t buflen, | |
82 | int *errnop, int *h_errnop, | |
fe0fc11b | 83 | int32_t *ttlp) _public_; |
8041b5ba LP |
84 | |
85 | enum nss_status _nss_myhostname_gethostbyaddr_r( | |
86 | const void* addr, socklen_t len, | |
87 | int af, | |
88 | struct hostent *host, | |
89 | char *buffer, size_t buflen, | |
fe0fc11b | 90 | int *errnop, int *h_errnop) _public_; |
8041b5ba | 91 | |
4e8c8252 LP |
92 | enum nss_status _nss_myhostname_gethostbyname4_r( |
93 | const char *name, | |
94 | struct gaih_addrtuple **pat, | |
95 | char *buffer, size_t buflen, | |
96 | int *errnop, int *h_errnop, | |
97 | int32_t *ttlp) { | |
98 | ||
8041b5ba | 99 | unsigned lo_ifi; |
4e8c8252 LP |
100 | char hn[HOST_NAME_MAX+1]; |
101 | size_t l, idx, ms; | |
102 | char *r_name; | |
8041b5ba LP |
103 | struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL; |
104 | struct address *addresses = NULL, *a; | |
105 | unsigned n_addresses = 0, n; | |
4e8c8252 LP |
106 | |
107 | memset(hn, 0, sizeof(hn)); | |
108 | if (gethostname(hn, sizeof(hn)-1) < 0) { | |
109 | *errnop = errno; | |
110 | *h_errnop = NO_RECOVERY; | |
111 | return NSS_STATUS_UNAVAIL; | |
112 | } | |
113 | ||
114 | if (strcasecmp(name, hn) != 0) { | |
115 | *errnop = ENOENT; | |
116 | *h_errnop = HOST_NOT_FOUND; | |
117 | return NSS_STATUS_NOTFOUND; | |
118 | } | |
6b21f0cf | 119 | |
8041b5ba | 120 | /* If this fails, n_addresses is 0. Which is fine */ |
1df2a0fc | 121 | ifconf_acquire_addresses(&addresses, &n_addresses); |
8041b5ba | 122 | |
4e8c8252 | 123 | /* If this call fails we fill in 0 as scope. Which is fine */ |
8041b5ba | 124 | lo_ifi = if_nametoindex(LOOPBACK_INTERFACE); |
4e8c8252 LP |
125 | |
126 | l = strlen(hn); | |
8041b5ba | 127 | ms = ALIGN(l+1)+ALIGN(sizeof(struct gaih_addrtuple))*(n_addresses > 0 ? n_addresses : 2); |
4e8c8252 LP |
128 | if (buflen < ms) { |
129 | *errnop = ENOMEM; | |
130 | *h_errnop = NO_RECOVERY; | |
8041b5ba | 131 | free(addresses); |
4e8c8252 LP |
132 | return NSS_STATUS_TRYAGAIN; |
133 | } | |
134 | ||
135 | /* First, fill in hostname */ | |
136 | r_name = buffer; | |
137 | memcpy(r_name, hn, l+1); | |
138 | idx = ALIGN(l+1); | |
139 | ||
8041b5ba LP |
140 | if (n_addresses <= 0) { |
141 | /* Second, fill in IPv6 tuple */ | |
142 | r_tuple = (struct gaih_addrtuple*) (buffer + idx); | |
143 | r_tuple->next = r_tuple_prev; | |
144 | r_tuple->name = r_name; | |
145 | r_tuple->family = AF_INET6; | |
146 | memcpy(r_tuple->addr, LOCALADDRESS_IPV6, 16); | |
147 | r_tuple->scopeid = (uint32_t) lo_ifi; | |
148 | ||
149 | idx += ALIGN(sizeof(struct gaih_addrtuple)); | |
150 | r_tuple_prev = r_tuple; | |
151 | ||
152 | /* Third, fill in IPv4 tuple */ | |
153 | r_tuple = (struct gaih_addrtuple*) (buffer + idx); | |
154 | r_tuple->next = r_tuple_prev; | |
155 | r_tuple->name = r_name; | |
156 | r_tuple->family = AF_INET; | |
157 | *(uint32_t*) r_tuple->addr = LOCALADDRESS_IPV4; | |
158 | r_tuple->scopeid = (uint32_t) lo_ifi; | |
159 | ||
160 | idx += ALIGN(sizeof(struct gaih_addrtuple)); | |
161 | r_tuple_prev = r_tuple; | |
162 | } | |
163 | ||
164 | /* Fourth, fill actual addresses in, but in backwards order */ | |
165 | for (a = addresses + n_addresses - 1, n = 0; n < n_addresses; n++, a--) { | |
166 | r_tuple = (struct gaih_addrtuple*) (buffer + idx); | |
167 | r_tuple->next = r_tuple_prev; | |
168 | r_tuple->name = r_name; | |
169 | r_tuple->family = a->family; | |
170 | r_tuple->scopeid = a->ifindex; | |
171 | memcpy(r_tuple->addr, a->address, 16); | |
172 | ||
173 | idx += ALIGN(sizeof(struct gaih_addrtuple)); | |
174 | r_tuple_prev = r_tuple; | |
175 | } | |
4e8c8252 LP |
176 | |
177 | /* Verify the size matches */ | |
178 | assert(idx == ms); | |
179 | ||
8041b5ba | 180 | *pat = r_tuple_prev; |
4e8c8252 LP |
181 | |
182 | if (ttlp) | |
183 | *ttlp = 0; | |
184 | ||
8041b5ba LP |
185 | free(addresses); |
186 | ||
4e8c8252 LP |
187 | return NSS_STATUS_SUCCESS; |
188 | } | |
6b21f0cf LP |
189 | |
190 | static enum nss_status fill_in_hostent( | |
4e8c8252 LP |
191 | const char *hn, |
192 | int af, | |
193 | struct hostent *result, | |
194 | char *buffer, size_t buflen, | |
195 | int *errnop, int *h_errnop, | |
196 | int32_t *ttlp, | |
197 | char **canonp) { | |
198 | ||
199 | size_t l, idx, ms; | |
200 | char *r_addr, *r_name, *r_aliases, *r_addr_list; | |
201 | size_t alen; | |
8041b5ba LP |
202 | struct address *addresses = NULL, *a; |
203 | unsigned n_addresses = 0, n, c; | |
204 | ||
205 | alen = PROTO_ADDRESS_SIZE(af); | |
206 | ||
1df2a0fc | 207 | ifconf_acquire_addresses(&addresses, &n_addresses); |
4e8c8252 | 208 | |
8041b5ba LP |
209 | for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++) |
210 | if (af == a->family) | |
211 | c++; | |
4e8c8252 LP |
212 | |
213 | l = strlen(hn); | |
8041b5ba LP |
214 | ms = ALIGN(l+1)+ |
215 | sizeof(char*)+ | |
216 | (c > 0 ? c : 1)*ALIGN(alen)+ | |
217 | (c > 0 ? c+1 : 2)*sizeof(char*); | |
218 | ||
4e8c8252 LP |
219 | if (buflen < ms) { |
220 | *errnop = ENOMEM; | |
221 | *h_errnop = NO_RECOVERY; | |
8041b5ba | 222 | free(addresses); |
4e8c8252 LP |
223 | return NSS_STATUS_TRYAGAIN; |
224 | } | |
225 | ||
226 | /* First, fill in hostname */ | |
227 | r_name = buffer; | |
228 | memcpy(r_name, hn, l+1); | |
229 | idx = ALIGN(l+1); | |
230 | ||
8041b5ba | 231 | /* Second, create (empty) aliases array */ |
4e8c8252 LP |
232 | r_aliases = buffer + idx; |
233 | *(char**) r_aliases = NULL; | |
234 | idx += sizeof(char*); | |
235 | ||
8041b5ba | 236 | /* Third, add addresses */ |
4e8c8252 | 237 | r_addr = buffer + idx; |
8041b5ba LP |
238 | if (c > 0) { |
239 | unsigned i = 0; | |
240 | ||
241 | for (a = addresses, n = 0; n < n_addresses; a++, n++) { | |
242 | if (af != a->family) | |
243 | continue; | |
244 | ||
245 | memcpy(r_addr + i*ALIGN(alen), a->address, alen); | |
246 | i++; | |
247 | } | |
248 | ||
249 | assert(i == c); | |
250 | idx += c*ALIGN(alen); | |
251 | } else { | |
252 | if (af == AF_INET) | |
253 | *(uint32_t*) r_addr = LOCALADDRESS_IPV4; | |
254 | else | |
255 | memcpy(r_addr, LOCALADDRESS_IPV6, 16); | |
256 | ||
257 | idx += ALIGN(alen); | |
258 | } | |
4e8c8252 LP |
259 | |
260 | /* Fourth, add address pointer array */ | |
261 | r_addr_list = buffer + idx; | |
8041b5ba LP |
262 | if (c > 0) { |
263 | unsigned i = 0; | |
264 | ||
265 | for (a = addresses, n = 0; n < n_addresses; a++, n++) { | |
266 | if (af != a->family) | |
267 | continue; | |
268 | ||
269 | ((char**) r_addr_list)[i] = (r_addr + i*ALIGN(alen)); | |
270 | i++; | |
271 | } | |
272 | ||
273 | assert(i == c); | |
274 | ((char**) r_addr_list)[c] = NULL; | |
275 | idx += (c+1)*sizeof(char*); | |
276 | ||
277 | } else { | |
278 | ((char**) r_addr_list)[0] = r_addr; | |
279 | ((char**) r_addr_list)[1] = NULL; | |
280 | idx += 2*sizeof(char*); | |
281 | } | |
4e8c8252 LP |
282 | |
283 | /* Verify the size matches */ | |
284 | assert(idx == ms); | |
285 | ||
286 | result->h_name = r_name; | |
287 | result->h_aliases = (char**) r_aliases; | |
288 | result->h_addrtype = af; | |
289 | result->h_length = alen; | |
290 | result->h_addr_list = (char**) r_addr_list; | |
291 | ||
292 | if (ttlp) | |
293 | *ttlp = 0; | |
294 | ||
295 | if (canonp) | |
296 | *canonp = r_name; | |
297 | ||
8041b5ba LP |
298 | free(addresses); |
299 | ||
4e8c8252 | 300 | return NSS_STATUS_SUCCESS; |
6b21f0cf LP |
301 | } |
302 | ||
4e8c8252 LP |
303 | enum nss_status _nss_myhostname_gethostbyname3_r( |
304 | const char *name, | |
305 | int af, | |
306 | struct hostent *host, | |
307 | char *buffer, size_t buflen, | |
308 | int *errnop, int *h_errnop, | |
309 | int32_t *ttlp, | |
310 | char **canonp) { | |
6b21f0cf | 311 | |
4e8c8252 | 312 | char hn[HOST_NAME_MAX+1]; |
6b21f0cf | 313 | |
4e8c8252 LP |
314 | if (af == AF_UNSPEC) |
315 | af = AF_INET; | |
6b21f0cf | 316 | |
4e8c8252 LP |
317 | if (af != AF_INET && af != AF_INET6) { |
318 | *errnop = EAFNOSUPPORT; | |
319 | *h_errnop = NO_DATA; | |
320 | return NSS_STATUS_UNAVAIL; | |
321 | } | |
6b21f0cf | 322 | |
4e8c8252 LP |
323 | memset(hn, 0, sizeof(hn)); |
324 | if (gethostname(hn, sizeof(hn)-1) < 0) { | |
325 | *errnop = errno; | |
326 | *h_errnop = NO_RECOVERY; | |
327 | return NSS_STATUS_UNAVAIL; | |
328 | } | |
6b21f0cf | 329 | |
4e8c8252 LP |
330 | if (strcasecmp(name, hn) != 0) { |
331 | *errnop = ENOENT; | |
332 | *h_errnop = HOST_NOT_FOUND; | |
333 | return NSS_STATUS_NOTFOUND; | |
334 | } | |
6b21f0cf | 335 | |
4e8c8252 LP |
336 | return fill_in_hostent(hn, af, host, buffer, buflen, errnop, h_errnop, ttlp, canonp); |
337 | } | |
6b21f0cf | 338 | |
4e8c8252 LP |
339 | enum nss_status _nss_myhostname_gethostbyname2_r( |
340 | const char *name, | |
341 | int af, | |
342 | struct hostent *host, | |
343 | char *buffer, size_t buflen, | |
344 | int *errnop, int *h_errnop) { | |
6b21f0cf | 345 | |
4e8c8252 LP |
346 | return _nss_myhostname_gethostbyname3_r( |
347 | name, | |
348 | af, | |
349 | host, | |
350 | buffer, buflen, | |
351 | errnop, h_errnop, | |
352 | NULL, | |
353 | NULL); | |
6b21f0cf LP |
354 | } |
355 | ||
8041b5ba | 356 | enum nss_status _nss_myhostname_gethostbyname_r( |
4e8c8252 LP |
357 | const char *name, |
358 | struct hostent *host, | |
359 | char *buffer, size_t buflen, | |
360 | int *errnop, int *h_errnop) { | |
361 | ||
362 | return _nss_myhostname_gethostbyname3_r( | |
363 | name, | |
364 | AF_UNSPEC, | |
365 | host, | |
366 | buffer, buflen, | |
367 | errnop, h_errnop, | |
368 | NULL, | |
369 | NULL); | |
6b21f0cf LP |
370 | } |
371 | ||
4e8c8252 LP |
372 | enum nss_status _nss_myhostname_gethostbyaddr2_r( |
373 | const void* addr, socklen_t len, | |
374 | int af, | |
375 | struct hostent *host, | |
376 | char *buffer, size_t buflen, | |
377 | int *errnop, int *h_errnop, | |
378 | int32_t *ttlp) { | |
6b21f0cf | 379 | |
4e8c8252 | 380 | char hn[HOST_NAME_MAX+1]; |
8041b5ba LP |
381 | struct address *addresses = NULL, *a; |
382 | unsigned n_addresses = 0, n; | |
383 | ||
384 | if (len != PROTO_ADDRESS_SIZE(af)) { | |
385 | *errnop = EINVAL; | |
386 | *h_errnop = NO_RECOVERY; | |
387 | return NSS_STATUS_UNAVAIL; | |
388 | } | |
6b21f0cf | 389 | |
4e8c8252 | 390 | if (af == AF_INET) { |
8041b5ba LP |
391 | |
392 | if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4) | |
393 | goto found; | |
6b21f0cf | 394 | |
4e8c8252 | 395 | } else if (af == AF_INET6) { |
8041b5ba LP |
396 | |
397 | if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) | |
398 | goto found; | |
399 | ||
4e8c8252 LP |
400 | } else { |
401 | *errnop = EAFNOSUPPORT; | |
402 | *h_errnop = NO_DATA; | |
403 | return NSS_STATUS_UNAVAIL; | |
404 | } | |
405 | ||
1df2a0fc | 406 | ifconf_acquire_addresses(&addresses, &n_addresses); |
8041b5ba LP |
407 | |
408 | for (a = addresses, n = 0; n < n_addresses; n++, a++) { | |
409 | if (af != a->family) | |
410 | continue; | |
411 | ||
412 | if (memcmp(addr, a->address, PROTO_ADDRESS_SIZE(af)) == 0) | |
413 | goto found; | |
414 | } | |
415 | ||
416 | *errnop = ENOENT; | |
417 | *h_errnop = HOST_NOT_FOUND; | |
418 | ||
419 | free(addresses); | |
420 | return NSS_STATUS_NOTFOUND; | |
421 | ||
422 | found: | |
423 | free(addresses); | |
424 | ||
4e8c8252 LP |
425 | memset(hn, 0, sizeof(hn)); |
426 | if (gethostname(hn, sizeof(hn)-1) < 0) { | |
427 | *errnop = errno; | |
428 | *h_errnop = NO_RECOVERY; | |
8041b5ba | 429 | |
4e8c8252 LP |
430 | return NSS_STATUS_UNAVAIL; |
431 | } | |
432 | ||
433 | return fill_in_hostent(hn, af, host, buffer, buflen, errnop, h_errnop, ttlp, NULL); | |
434 | ||
4e8c8252 | 435 | } |
6b21f0cf | 436 | |
4e8c8252 LP |
437 | enum nss_status _nss_myhostname_gethostbyaddr_r( |
438 | const void* addr, socklen_t len, | |
439 | int af, | |
440 | struct hostent *host, | |
441 | char *buffer, size_t buflen, | |
442 | int *errnop, int *h_errnop) { | |
6b21f0cf | 443 | |
4e8c8252 LP |
444 | return _nss_myhostname_gethostbyaddr2_r( |
445 | addr, len, | |
446 | af, | |
447 | host, | |
448 | buffer, buflen, | |
449 | errnop, h_errnop, | |
450 | NULL); | |
6b21f0cf | 451 | } |