]> git.ipfire.org Git - thirdparty/systemd.git/blob - nss-myhostname.c
83180ad80532713dc6bb9ca2c23e21629568b026
[thirdparty/systemd.git] / nss-myhostname.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of nss-myhostname.
5
6 Copyright 2008-2011 Lennart Poettering
7
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.
12
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.
17
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/>.
21 ***/
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>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <net/if.h>
32 #include <stdlib.h>
33 #include <arpa/inet.h>
34
35 #include "ifconf.h"
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
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,
53 int32_t *ttlp) _public_;
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,
62 char **canonp) _public_;
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,
69 int *errnop, int *h_errnop) _public_;
70
71 enum nss_status _nss_myhostname_gethostbyname_r(
72 const char *name,
73 struct hostent *host,
74 char *buffer, size_t buflen,
75 int *errnop, int *h_errnop) _public_;
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,
83 int32_t *ttlp) _public_;
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,
90 int *errnop, int *h_errnop) _public_;
91
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
99 unsigned lo_ifi;
100 char hn[HOST_NAME_MAX+1];
101 size_t l, idx, ms;
102 char *r_name;
103 struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
104 struct address *addresses = NULL, *a;
105 unsigned n_addresses = 0, n;
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 }
119
120 /* If this fails, n_addresses is 0. Which is fine */
121 ifconf_acquire_addresses(&addresses, &n_addresses);
122
123 /* If this call fails we fill in 0 as scope. Which is fine */
124 lo_ifi = if_nametoindex(LOOPBACK_INTERFACE);
125
126 l = strlen(hn);
127 ms = ALIGN(l+1)+ALIGN(sizeof(struct gaih_addrtuple))*(n_addresses > 0 ? n_addresses : 2);
128 if (buflen < ms) {
129 *errnop = ENOMEM;
130 *h_errnop = NO_RECOVERY;
131 free(addresses);
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
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 }
176
177 /* Verify the size matches */
178 assert(idx == ms);
179
180 *pat = r_tuple_prev;
181
182 if (ttlp)
183 *ttlp = 0;
184
185 free(addresses);
186
187 return NSS_STATUS_SUCCESS;
188 }
189
190 static enum nss_status fill_in_hostent(
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;
202 struct address *addresses = NULL, *a;
203 unsigned n_addresses = 0, n, c;
204
205 alen = PROTO_ADDRESS_SIZE(af);
206
207 ifconf_acquire_addresses(&addresses, &n_addresses);
208
209 for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
210 if (af == a->family)
211 c++;
212
213 l = strlen(hn);
214 ms = ALIGN(l+1)+
215 sizeof(char*)+
216 (c > 0 ? c : 1)*ALIGN(alen)+
217 (c > 0 ? c+1 : 2)*sizeof(char*);
218
219 if (buflen < ms) {
220 *errnop = ENOMEM;
221 *h_errnop = NO_RECOVERY;
222 free(addresses);
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
231 /* Second, create (empty) aliases array */
232 r_aliases = buffer + idx;
233 *(char**) r_aliases = NULL;
234 idx += sizeof(char*);
235
236 /* Third, add addresses */
237 r_addr = buffer + idx;
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 }
259
260 /* Fourth, add address pointer array */
261 r_addr_list = buffer + idx;
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 }
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
298 free(addresses);
299
300 return NSS_STATUS_SUCCESS;
301 }
302
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) {
311
312 char hn[HOST_NAME_MAX+1];
313
314 if (af == AF_UNSPEC)
315 af = AF_INET;
316
317 if (af != AF_INET && af != AF_INET6) {
318 *errnop = EAFNOSUPPORT;
319 *h_errnop = NO_DATA;
320 return NSS_STATUS_UNAVAIL;
321 }
322
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 }
329
330 if (strcasecmp(name, hn) != 0) {
331 *errnop = ENOENT;
332 *h_errnop = HOST_NOT_FOUND;
333 return NSS_STATUS_NOTFOUND;
334 }
335
336 return fill_in_hostent(hn, af, host, buffer, buflen, errnop, h_errnop, ttlp, canonp);
337 }
338
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) {
345
346 return _nss_myhostname_gethostbyname3_r(
347 name,
348 af,
349 host,
350 buffer, buflen,
351 errnop, h_errnop,
352 NULL,
353 NULL);
354 }
355
356 enum nss_status _nss_myhostname_gethostbyname_r(
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);
370 }
371
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) {
379
380 char hn[HOST_NAME_MAX+1];
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 }
389
390 if (af == AF_INET) {
391
392 if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
393 goto found;
394
395 } else if (af == AF_INET6) {
396
397 if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0)
398 goto found;
399
400 } else {
401 *errnop = EAFNOSUPPORT;
402 *h_errnop = NO_DATA;
403 return NSS_STATUS_UNAVAIL;
404 }
405
406 ifconf_acquire_addresses(&addresses, &n_addresses);
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
425 memset(hn, 0, sizeof(hn));
426 if (gethostname(hn, sizeof(hn)-1) < 0) {
427 *errnop = errno;
428 *h_errnop = NO_RECOVERY;
429
430 return NSS_STATUS_UNAVAIL;
431 }
432
433 return fill_in_hostent(hn, af, host, buffer, buflen, errnop, h_errnop, ttlp, NULL);
434
435 }
436
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) {
443
444 return _nss_myhostname_gethostbyaddr2_r(
445 addr, len,
446 af,
447 host,
448 buffer, buflen,
449 errnop, h_errnop,
450 NULL);
451 }