-
Notifications
You must be signed in to change notification settings - Fork 2
/
nss-ubdns.c
399 lines (331 loc) · 8.81 KB
/
nss-ubdns.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (C) 2011 Robert S. Edmonds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/***
This file is part of nss-myhostname.
Copyright 2008-2011 Lennart Poettering
nss-myhostname is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
nss-myhostname 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with nss-myhostname; If not, see
<http://www.gnu.org/licenses/>.
***/
#include <limits.h>
#include <nss.h>
#include <sys/types.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <stdio.h>
#include "nss-ubdns.h"
#define ALIGN(a) (((a+sizeof(void*)-1)/sizeof(void*))*sizeof(void*))
enum nss_status _nss_ubdns_gethostbyname4_r(
const char *name,
struct gaih_addrtuple **pat,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp);
enum nss_status _nss_ubdns_gethostbyname3_r(
const char *name,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp,
char **canonp);
enum nss_status _nss_ubdns_gethostbyname2_r(
const char *name,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop);
enum nss_status _nss_ubdns_gethostbyname_r(
const char *name,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop);
enum nss_status _nss_ubdns_gethostbyaddr2_r(
const void* addr, socklen_t len,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp);
enum nss_status _nss_ubdns_gethostbyaddr_r(
const void* addr, socklen_t len,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop);
enum nss_status _nss_ubdns_gethostbyname4_r(
const char *hn,
struct gaih_addrtuple **pat,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp)
{
size_t l, idx, ms;
char *r_name;
struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
struct address *addresses = NULL, *a;
unsigned n_addresses = 0, n;
/* If this fails, n_addresses is 0. Which is fine */
nss_ubdns_lookup_forward(hn, AF_UNSPEC, &addresses, &n_addresses);
if (n_addresses == 0) {
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
return (NSS_STATUS_NOTFOUND);
}
l = strlen(hn);
ms = ALIGN(l+1)+ALIGN(sizeof(struct gaih_addrtuple))*(n_addresses > 0 ? n_addresses : 2);
if (buflen < ms) {
*errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
free(addresses);
return NSS_STATUS_TRYAGAIN;
}
/* First, fill in hostname */
r_name = buffer;
memcpy(r_name, hn, l+1);
idx = ALIGN(l+1);
/* Second, fill actual addresses in, but in backwards order */
for (a = addresses + n_addresses - 1, n = 0; n < n_addresses; n++, a--) {
r_tuple = (struct gaih_addrtuple*) (buffer + idx);
r_tuple->next = r_tuple_prev;
r_tuple->name = r_name;
r_tuple->family = a->family;
r_tuple->scopeid = 0;
memcpy(r_tuple->addr, a->address, 16);
idx += ALIGN(sizeof(struct gaih_addrtuple));
r_tuple_prev = r_tuple;
}
/* Verify the size matches */
assert(idx == ms);
*pat = r_tuple_prev;
if (ttlp)
*ttlp = 0;
free(addresses);
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_ubdns_gethostbyname3_r(
const char *hn,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp,
char **canonp)
{
size_t l, idx, ms;
char *r_addr, *r_name, *r_aliases, *r_addr_list;
size_t alen;
struct address *addresses = NULL, *a;
unsigned n_addresses = 0, n, c;
unsigned i = 0;
if (af != AF_INET && af != AF_INET6) {
*errnop = EAFNOSUPPORT;
*h_errnop = NO_DATA;
return (NSS_STATUS_UNAVAIL);
}
alen = PROTO_ADDRESS_SIZE(af);
nss_ubdns_lookup_forward(hn, af, &addresses, &n_addresses);
for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
if (af == a->family)
c++;
if (c == 0) {
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
return (NSS_STATUS_NOTFOUND);
}
l = strlen(hn);
ms = ALIGN(l + 1) +
sizeof(char *) +
c * ALIGN(alen) +
(c + 1) * sizeof(char *);
if (buflen < ms) {
*errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
free(addresses);
return NSS_STATUS_TRYAGAIN;
}
/* First, fill in hostname */
r_name = buffer;
memcpy(r_name, hn, l+1);
idx = ALIGN(l+1);
/* Second, create (empty) aliases array */
r_aliases = buffer + idx;
*(char**) r_aliases = NULL;
idx += sizeof(char*);
/* Third, add addresses */
r_addr = buffer + idx;
i = 0;
for (a = addresses, n = 0; n < n_addresses; a++, n++) {
if (af != a->family)
continue;
memcpy(r_addr + i*ALIGN(alen), a->address, alen);
i++;
}
assert(i == c);
idx += c*ALIGN(alen);
/* Fourth, add address pointer array */
r_addr_list = buffer + idx;
i = 0;
for (a = addresses, n = 0; n < n_addresses; a++, n++) {
if (af != a->family)
continue;
((char**) r_addr_list)[i] = (r_addr + i*ALIGN(alen));
i++;
}
assert(i == c);
((char**) r_addr_list)[c] = NULL;
idx += (c+1)*sizeof(char*);
/* Verify the size matches */
assert(idx == ms);
result->h_name = r_name;
result->h_aliases = (char**) r_aliases;
result->h_addrtype = af;
result->h_length = alen;
result->h_addr_list = (char**) r_addr_list;
if (ttlp)
*ttlp = 0;
if (canonp)
*canonp = r_name;
free(addresses);
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_ubdns_gethostbyname2_r(
const char *name,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
return _nss_ubdns_gethostbyname3_r(
name,
af,
host,
buffer, buflen,
errnop, h_errnop,
NULL,
NULL);
}
enum nss_status _nss_ubdns_gethostbyname_r(
const char *name,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
return _nss_ubdns_gethostbyname3_r(
name,
AF_INET,
host,
buffer, buflen,
errnop, h_errnop,
NULL,
NULL);
}
enum nss_status _nss_ubdns_gethostbyaddr2_r(
const void* addr, socklen_t len,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp)
{
char *hn = NULL;
char *r_name, *r_addr, *r_aliases, *r_addr_list;
size_t l, idx, ms, alen;
alen = PROTO_ADDRESS_SIZE(af);
if (len != alen) {
*errnop = EINVAL;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
}
if (af != AF_INET && af != AF_INET6) {
*errnop = EAFNOSUPPORT;
*h_errnop = NO_DATA;
return NSS_STATUS_UNAVAIL;
}
hn = nss_ubdns_lookup_reverse(addr, af);
if (!hn) {
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
return NSS_STATUS_NOTFOUND;
}
l = strlen(hn);
ms = ALIGN(l + 1) +
sizeof(char *) +
ALIGN(alen) +
2 * sizeof(char *);
if (buflen < ms) {
*errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
free(hn);
return (NSS_STATUS_TRYAGAIN);
}
/* First, fill in hostname */
r_name = buffer;
memcpy(r_name, hn, l + 1);
idx = ALIGN(l + 1);
/* Second, create (empty) aliases array */
r_aliases = buffer + idx;
*(char **) r_aliases = NULL;
idx += sizeof(char *);
/* Third, add address */
r_addr = buffer + idx;
memcpy(r_addr, addr, alen);
idx += ALIGN(alen);
/* Fourth, add address pointer array */
r_addr_list = buffer + idx;
((char **) r_addr_list)[0] = r_addr;
((char **) r_addr_list)[1] = NULL;
idx += 2*sizeof(char *);
assert(idx == ms);
result->h_name = r_name;
result->h_aliases = (char **) r_aliases;
result->h_addrtype = af;
result->h_length = alen;
result->h_addr_list = (char **) r_addr_list;
if (ttlp)
*ttlp = 0;
free(hn);
return (NSS_STATUS_SUCCESS);
}
enum nss_status _nss_ubdns_gethostbyaddr_r(
const void* addr, socklen_t len,
int af,
struct hostent *host,
char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
return _nss_ubdns_gethostbyaddr2_r(
addr, len,
af,
host,
buffer, buflen,
errnop, h_errnop,
NULL);
}