]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4d1cf1e2 | 2 | |
4d1cf1e2 | 3 | #include <errno.h> |
07630cea LP |
4 | #include <netdb.h> |
5 | #include <nss.h> | |
55679e29 | 6 | #include <pthread.h> |
4d1cf1e2 | 7 | #include <stdlib.h> |
ca78ad1d ZJS |
8 | #include <sys/types.h> |
9 | #include <unistd.h> | |
4d1cf1e2 | 10 | |
aee9d18c | 11 | #include "env-util.h" |
2b2fec7d | 12 | #include "errno-util.h" |
07630cea | 13 | #include "in-addr-util.h" |
4d1cf1e2 LP |
14 | #include "macro.h" |
15 | #include "nss-util.h" | |
4cbfd62b | 16 | #include "resolved-def.h" |
0c5eb056 | 17 | #include "signal-util.h" |
2b2fec7d | 18 | #include "string-util.h" |
0c73f4f0 LP |
19 | #include "strv.h" |
20 | #include "varlink.h" | |
4d1cf1e2 | 21 | |
55679e29 ZJS |
22 | static JsonDispatchFlags json_dispatch_flags = 0; |
23 | ||
24 | static void setup_logging(void) { | |
25 | log_parse_environment(); | |
26 | ||
27 | if (DEBUG_LOGGING) | |
28 | json_dispatch_flags = JSON_LOG; | |
29 | } | |
30 | ||
31 | static void setup_logging_once(void) { | |
32 | static pthread_once_t once = PTHREAD_ONCE_INIT; | |
33 | assert_se(pthread_once(&once, setup_logging) == 0); | |
34 | } | |
35 | ||
36 | #define NSS_ENTRYPOINT_BEGIN \ | |
37 | BLOCK_SIGNALS(NSS_SIGNALS_BLOCK); \ | |
38 | setup_logging_once() | |
39 | ||
4d1cf1e2 LP |
40 | NSS_GETHOSTBYNAME_PROTOTYPES(resolve); |
41 | NSS_GETHOSTBYADDR_PROTOTYPES(resolve); | |
42 | ||
0c73f4f0 LP |
43 | static bool error_shall_fallback(const char *error_id) { |
44 | return STR_IN_SET(error_id, | |
45 | VARLINK_ERROR_DISCONNECTED, | |
46 | VARLINK_ERROR_TIMEOUT, | |
47 | VARLINK_ERROR_PROTOCOL, | |
48 | VARLINK_ERROR_INTERFACE_NOT_FOUND, | |
49 | VARLINK_ERROR_METHOD_NOT_FOUND, | |
50 | VARLINK_ERROR_METHOD_NOT_IMPLEMENTED); | |
7c2a5e26 LP |
51 | } |
52 | ||
0c73f4f0 LP |
53 | static int connect_to_resolved(Varlink **ret) { |
54 | _cleanup_(varlink_unrefp) Varlink *link = NULL; | |
55 | int r; | |
309e9d86 | 56 | |
0c73f4f0 | 57 | r = varlink_connect_address(&link, "/run/systemd/resolve/io.systemd.Resolve"); |
4d1cf1e2 LP |
58 | if (r < 0) |
59 | return r; | |
60 | ||
0c73f4f0 | 61 | r = varlink_set_relative_timeout(link, SD_RESOLVED_QUERY_TIMEOUT_USEC); |
309e9d86 LP |
62 | if (r < 0) |
63 | return r; | |
64 | ||
0c73f4f0 LP |
65 | *ret = TAKE_PTR(link); |
66 | return 0; | |
4d1cf1e2 LP |
67 | } |
68 | ||
27007eff LP |
69 | static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) { |
70 | struct in6_addr in6; | |
71 | ||
425ed139 | 72 | if (family != AF_INET6 || ifindex == 0) |
27007eff LP |
73 | return 0; |
74 | ||
75 | /* Some apps can't deal with the scope ID attached to non-link-local addresses. Hence, let's suppress that. */ | |
76 | ||
11814bbb | 77 | assert(sizeof(in6) == FAMILY_ADDRESS_SIZE(AF_INET6)); |
27007eff LP |
78 | memcpy(&in6, a, sizeof(struct in6_addr)); |
79 | ||
94876904 | 80 | return in6_addr_is_link_local(&in6) ? ifindex : 0; |
27007eff LP |
81 | } |
82 | ||
0c73f4f0 LP |
83 | static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { |
84 | int *ifi = userdata; | |
85 | intmax_t t; | |
2f28018c | 86 | |
0c73f4f0 LP |
87 | assert(variant); |
88 | assert(ifi); | |
2f28018c | 89 | |
0c73f4f0 LP |
90 | if (!json_variant_is_integer(variant)) |
91 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); | |
2f28018c | 92 | |
0c73f4f0 | 93 | t = json_variant_integer(variant); |
6da52ca8 | 94 | if (t > INT_MAX) |
0c73f4f0 LP |
95 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is out of bounds for an interface index.", strna(name)); |
96 | ||
97 | *ifi = (int) t; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { | |
102 | int *family = userdata; | |
103 | intmax_t t; | |
104 | ||
105 | assert(variant); | |
106 | assert(family); | |
107 | ||
108 | if (!json_variant_is_integer(variant)) | |
109 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); | |
110 | ||
111 | t = json_variant_integer(variant); | |
112 | if (t < 0 || t > INT_MAX) | |
113 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid family.", strna(name)); | |
114 | ||
115 | *family = (int) t; | |
116 | return 0; | |
117 | } | |
118 | ||
119 | typedef struct ResolveHostnameReply { | |
120 | JsonVariant *addresses; | |
121 | char *name; | |
122 | uint64_t flags; | |
123 | } ResolveHostnameReply; | |
124 | ||
125 | static void resolve_hostname_reply_destroy(ResolveHostnameReply *p) { | |
126 | assert(p); | |
127 | ||
128 | json_variant_unref(p->addresses); | |
129 | free(p->name); | |
2f28018c LP |
130 | } |
131 | ||
0c73f4f0 LP |
132 | static const JsonDispatch resolve_hostname_reply_dispatch_table[] = { |
133 | { "addresses", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveHostnameReply, addresses), JSON_MANDATORY }, | |
134 | { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(ResolveHostnameReply, name), 0 }, | |
135 | { "flags", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(ResolveHostnameReply, flags), 0 }, | |
136 | {} | |
137 | }; | |
138 | ||
139 | typedef struct AddressParameters { | |
140 | int ifindex; | |
141 | int family; | |
142 | union in_addr_union address; | |
143 | size_t address_size; | |
144 | } AddressParameters; | |
145 | ||
146 | static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { | |
147 | AddressParameters *p = userdata; | |
148 | union in_addr_union buf = {}; | |
149 | JsonVariant *i; | |
150 | size_t n, k = 0; | |
151 | ||
152 | assert(variant); | |
153 | assert(p); | |
154 | ||
155 | if (!json_variant_is_array(variant)) | |
156 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); | |
157 | ||
158 | n = json_variant_elements(variant); | |
159 | if (!IN_SET(n, 4, 16)) | |
160 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); | |
161 | ||
162 | JSON_VARIANT_ARRAY_FOREACH(i, variant) { | |
163 | intmax_t b; | |
164 | ||
165 | if (!json_variant_is_integer(i)) | |
166 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name)); | |
167 | ||
168 | b = json_variant_integer(i); | |
169 | if (b < 0 || b > 0xff) | |
170 | return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is out of range 0…255.", k, strna(name)); | |
171 | ||
172 | buf.bytes[k++] = (uint8_t) b; | |
173 | } | |
174 | ||
175 | p->address = buf; | |
176 | p->address_size = k; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static const JsonDispatch address_parameters_dispatch_table[] = { | |
182 | { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(AddressParameters, ifindex), 0 }, | |
183 | { "family", JSON_VARIANT_INTEGER, json_dispatch_family, offsetof(AddressParameters, family), JSON_MANDATORY }, | |
184 | { "address", JSON_VARIANT_ARRAY, json_dispatch_address, 0, JSON_MANDATORY }, | |
185 | {} | |
186 | }; | |
187 | ||
aee9d18c LP |
188 | static uint64_t query_flags(void) { |
189 | uint64_t f = 0; | |
190 | int r; | |
191 | ||
192 | /* Allow callers to turn off validation, when we resolve via nss-resolve */ | |
193 | ||
194 | r = getenv_bool_secure("SYSTEMD_NSS_RESOLVE_VALIDATE"); | |
195 | if (r < 0 && r != -ENXIO) | |
196 | log_debug_errno(r, "Failed to parse $SYSTEMD_NSS_RESOLVE_VALIDATE value, ignoring."); | |
197 | else if (r == 0) | |
198 | f |= SD_RESOLVED_NO_VALIDATE; | |
199 | ||
200 | return f; | |
201 | } | |
202 | ||
4d1cf1e2 LP |
203 | enum nss_status _nss_resolve_gethostbyname4_r( |
204 | const char *name, | |
205 | struct gaih_addrtuple **pat, | |
206 | char *buffer, size_t buflen, | |
207 | int *errnop, int *h_errnop, | |
208 | int32_t *ttlp) { | |
209 | ||
0c73f4f0 LP |
210 | _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; |
211 | _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; | |
212 | struct gaih_addrtuple *r_tuple = NULL, *r_tuple_first = NULL; | |
213 | _cleanup_(varlink_unrefp) Varlink *link = NULL; | |
214 | const char *canonical = NULL, *error_id = NULL; | |
215 | JsonVariant *entry, *rparams; | |
216 | size_t l, ms, idx, c = 0; | |
4d1cf1e2 | 217 | char *r_name; |
0c73f4f0 | 218 | int r; |
4d1cf1e2 | 219 | |
06202b9e | 220 | PROTECT_ERRNO; |
55679e29 | 221 | NSS_ENTRYPOINT_BEGIN; |
0c5eb056 | 222 | |
4d1cf1e2 LP |
223 | assert(name); |
224 | assert(pat); | |
225 | assert(buffer); | |
226 | assert(errnop); | |
227 | assert(h_errnop); | |
228 | ||
0c73f4f0 | 229 | r = connect_to_resolved(&link); |
4d1cf1e2 LP |
230 | if (r < 0) |
231 | goto fail; | |
232 | ||
0c73f4f0 | 233 | r = json_build(&cparams, JSON_BUILD_OBJECT( |
aee9d18c LP |
234 | JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), |
235 | JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); | |
4d1cf1e2 LP |
236 | if (r < 0) |
237 | goto fail; | |
238 | ||
3c229a9a YW |
239 | /* Return NSS_STATUS_UNAVAIL when communication with systemd-resolved fails, allowing falling |
240 | * back to other nss modules. Treat all other error conditions as NOTFOUND. This includes | |
241 | * DNSSEC errors and suchlike. (We don't use UNAVAIL in this case so that the nsswitch.conf | |
242 | * configuration can distinguish such executed but negative replies from complete failure to | |
243 | * talk to resolved). */ | |
0c73f4f0 | 244 | r = varlink_call(link, "io.systemd.Resolve.ResolveHostname", cparams, &rparams, &error_id, NULL); |
3c229a9a YW |
245 | if (r < 0) |
246 | goto fail; | |
247 | if (!isempty(error_id)) { | |
0c73f4f0 | 248 | if (!error_shall_fallback(error_id)) |
06202b9e | 249 | goto not_found; |
a464cf80 | 250 | goto fail; |
4d1cf1e2 LP |
251 | } |
252 | ||
55679e29 | 253 | r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, NULL, json_dispatch_flags, &p); |
0c73f4f0 | 254 | if (r < 0) |
4d1cf1e2 | 255 | goto fail; |
0c73f4f0 | 256 | if (json_variant_is_blank_object(p.addresses)) |
06202b9e | 257 | goto not_found; |
4d1cf1e2 | 258 | |
0c73f4f0 LP |
259 | JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { |
260 | AddressParameters q = {}; | |
261 | ||
55679e29 | 262 | r = json_dispatch(entry, address_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
0c73f4f0 LP |
263 | if (r < 0) |
264 | goto fail; | |
265 | ||
266 | if (!IN_SET(q.family, AF_INET, AF_INET6)) | |
267 | continue; | |
268 | ||
269 | if (q.address_size != FAMILY_ADDRESS_SIZE(q.family)) { | |
270 | r = -EINVAL; | |
271 | goto fail; | |
272 | } | |
273 | ||
274 | c++; | |
275 | } | |
276 | ||
277 | canonical = p.name ?: name; | |
309e9d86 LP |
278 | |
279 | l = strlen(canonical); | |
4d1cf1e2 LP |
280 | ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c; |
281 | if (buflen < ms) { | |
cdccd29f | 282 | UNPROTECT_ERRNO; |
0192cbdb | 283 | *errnop = ERANGE; |
e36c6e48 | 284 | *h_errnop = NETDB_INTERNAL; |
4d1cf1e2 LP |
285 | return NSS_STATUS_TRYAGAIN; |
286 | } | |
287 | ||
288 | /* First, append name */ | |
289 | r_name = buffer; | |
309e9d86 | 290 | memcpy(r_name, canonical, l+1); |
4d1cf1e2 LP |
291 | idx = ALIGN(l+1); |
292 | ||
293 | /* Second, append addresses */ | |
294 | r_tuple_first = (struct gaih_addrtuple*) (buffer + idx); | |
309e9d86 | 295 | |
0c73f4f0 LP |
296 | JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { |
297 | AddressParameters q = {}; | |
78c6a153 | 298 | |
55679e29 | 299 | r = json_dispatch(entry, address_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
4d1cf1e2 LP |
300 | if (r < 0) |
301 | goto fail; | |
302 | ||
0c73f4f0 | 303 | if (!IN_SET(q.family, AF_INET, AF_INET6)) |
4d1cf1e2 LP |
304 | continue; |
305 | ||
4d1cf1e2 | 306 | r_tuple = (struct gaih_addrtuple*) (buffer + idx); |
0c73f4f0 | 307 | r_tuple->next = (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple))); |
4d1cf1e2 | 308 | r_tuple->name = r_name; |
0c73f4f0 LP |
309 | r_tuple->family = q.family; |
310 | r_tuple->scopeid = ifindex_to_scopeid(q.family, &q.address, q.ifindex); | |
311 | memcpy(r_tuple->addr, &q.address, q.address_size); | |
4d1cf1e2 LP |
312 | |
313 | idx += ALIGN(sizeof(struct gaih_addrtuple)); | |
4d1cf1e2 | 314 | } |
4d1cf1e2 | 315 | |
0c73f4f0 LP |
316 | assert(r_tuple); |
317 | r_tuple->next = NULL; /* Override last next pointer */ | |
318 | ||
4d1cf1e2 LP |
319 | assert(idx == ms); |
320 | ||
321 | if (*pat) | |
322 | **pat = *r_tuple_first; | |
323 | else | |
324 | *pat = r_tuple_first; | |
325 | ||
326 | if (ttlp) | |
327 | *ttlp = 0; | |
328 | ||
06202b9e YW |
329 | /* Explicitly reset both *h_errnop and h_errno to work around |
330 | * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */ | |
e70df46b LP |
331 | *h_errnop = NETDB_SUCCESS; |
332 | h_errno = 0; | |
333 | ||
4d1cf1e2 LP |
334 | return NSS_STATUS_SUCCESS; |
335 | ||
336 | fail: | |
cdccd29f | 337 | UNPROTECT_ERRNO; |
0192cbdb | 338 | *errnop = -r; |
a464cf80 | 339 | *h_errnop = NO_RECOVERY; |
954cba66 | 340 | return NSS_STATUS_UNAVAIL; |
06202b9e YW |
341 | |
342 | not_found: | |
343 | *h_errnop = HOST_NOT_FOUND; | |
344 | return NSS_STATUS_NOTFOUND; | |
4d1cf1e2 LP |
345 | } |
346 | ||
347 | enum nss_status _nss_resolve_gethostbyname3_r( | |
348 | const char *name, | |
349 | int af, | |
350 | struct hostent *result, | |
351 | char *buffer, size_t buflen, | |
352 | int *errnop, int *h_errnop, | |
353 | int32_t *ttlp, | |
354 | char **canonp) { | |
355 | ||
0c73f4f0 LP |
356 | _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; |
357 | _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; | |
4d1cf1e2 | 358 | char *r_name, *r_aliases, *r_addr, *r_addr_list; |
0c73f4f0 LP |
359 | _cleanup_(varlink_unrefp) Varlink *link = NULL; |
360 | const char *canonical, *error_id = NULL; | |
361 | size_t l, idx, ms, alen, i = 0, c = 0; | |
362 | JsonVariant *entry, *rparams; | |
363 | int r; | |
4d1cf1e2 | 364 | |
06202b9e | 365 | PROTECT_ERRNO; |
55679e29 | 366 | NSS_ENTRYPOINT_BEGIN; |
0c5eb056 | 367 | |
4d1cf1e2 LP |
368 | assert(name); |
369 | assert(result); | |
370 | assert(buffer); | |
371 | assert(errnop); | |
372 | assert(h_errnop); | |
373 | ||
374 | if (af == AF_UNSPEC) | |
375 | af = AF_INET; | |
376 | ||
4c701096 | 377 | if (!IN_SET(af, AF_INET, AF_INET6)) { |
4d1cf1e2 LP |
378 | r = -EAFNOSUPPORT; |
379 | goto fail; | |
380 | } | |
381 | ||
0c73f4f0 | 382 | r = connect_to_resolved(&link); |
4d1cf1e2 LP |
383 | if (r < 0) |
384 | goto fail; | |
385 | ||
0c73f4f0 | 386 | r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), |
aee9d18c LP |
387 | JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), |
388 | JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); | |
4d1cf1e2 LP |
389 | if (r < 0) |
390 | goto fail; | |
391 | ||
0c73f4f0 | 392 | r = varlink_call(link, "io.systemd.Resolve.ResolveHostname", cparams, &rparams, &error_id, NULL); |
3c229a9a YW |
393 | if (r < 0) |
394 | goto fail; | |
395 | if (!isempty(error_id)) { | |
0c73f4f0 | 396 | if (!error_shall_fallback(error_id)) |
06202b9e | 397 | goto not_found; |
a464cf80 | 398 | goto fail; |
4d1cf1e2 LP |
399 | } |
400 | ||
55679e29 | 401 | r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, NULL, json_dispatch_flags, &p); |
0c73f4f0 | 402 | if (r < 0) |
4d1cf1e2 | 403 | goto fail; |
0c73f4f0 | 404 | if (json_variant_is_blank_object(p.addresses)) |
06202b9e | 405 | goto not_found; |
4d1cf1e2 | 406 | |
0c73f4f0 LP |
407 | JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { |
408 | AddressParameters q = {}; | |
409 | ||
55679e29 | 410 | r = json_dispatch(entry, address_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
0c73f4f0 LP |
411 | if (r < 0) |
412 | goto fail; | |
413 | ||
414 | if (!IN_SET(q.family, AF_INET, AF_INET6)) | |
415 | continue; | |
416 | ||
417 | if (q.address_size != FAMILY_ADDRESS_SIZE(q.family)) { | |
418 | r = -EINVAL; | |
419 | goto fail; | |
420 | } | |
421 | ||
422 | c++; | |
423 | } | |
424 | ||
425 | canonical = p.name ?: name; | |
309e9d86 | 426 | |
9d485985 | 427 | alen = FAMILY_ADDRESS_SIZE(af); |
309e9d86 | 428 | l = strlen(canonical); |
4d1cf1e2 | 429 | |
0c73f4f0 | 430 | ms = ALIGN(l+1) + c*ALIGN(alen) + (c+2) * sizeof(char*); |
4d1cf1e2 LP |
431 | |
432 | if (buflen < ms) { | |
cdccd29f | 433 | UNPROTECT_ERRNO; |
0192cbdb | 434 | *errnop = ERANGE; |
e36c6e48 | 435 | *h_errnop = NETDB_INTERNAL; |
4d1cf1e2 LP |
436 | return NSS_STATUS_TRYAGAIN; |
437 | } | |
438 | ||
439 | /* First, append name */ | |
440 | r_name = buffer; | |
309e9d86 | 441 | memcpy(r_name, canonical, l+1); |
4d1cf1e2 LP |
442 | idx = ALIGN(l+1); |
443 | ||
309e9d86 | 444 | /* Second, create empty aliases array */ |
4d1cf1e2 LP |
445 | r_aliases = buffer + idx; |
446 | ((char**) r_aliases)[0] = NULL; | |
447 | idx += sizeof(char*); | |
448 | ||
449 | /* Third, append addresses */ | |
450 | r_addr = buffer + idx; | |
309e9d86 | 451 | |
0c73f4f0 LP |
452 | JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { |
453 | AddressParameters q = {}; | |
4d1cf1e2 | 454 | |
55679e29 | 455 | r = json_dispatch(entry, address_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
4d1cf1e2 LP |
456 | if (r < 0) |
457 | goto fail; | |
458 | ||
0c73f4f0 | 459 | if (q.family != af) |
4d1cf1e2 LP |
460 | continue; |
461 | ||
0c73f4f0 | 462 | if (q.address_size != alen) { |
4d1cf1e2 LP |
463 | r = -EINVAL; |
464 | goto fail; | |
465 | } | |
466 | ||
0c73f4f0 | 467 | memcpy(r_addr + i*ALIGN(alen), &q.address, alen); |
4d1cf1e2 LP |
468 | i++; |
469 | } | |
470 | ||
471 | assert(i == c); | |
472 | idx += c * ALIGN(alen); | |
473 | ||
309e9d86 | 474 | /* Fourth, append address pointer array */ |
4d1cf1e2 LP |
475 | r_addr_list = buffer + idx; |
476 | for (i = 0; i < c; i++) | |
477 | ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen); | |
478 | ||
479 | ((char**) r_addr_list)[i] = NULL; | |
480 | idx += (c+1) * sizeof(char*); | |
481 | ||
482 | assert(idx == ms); | |
483 | ||
484 | result->h_name = r_name; | |
485 | result->h_aliases = (char**) r_aliases; | |
486 | result->h_addrtype = af; | |
487 | result->h_length = alen; | |
488 | result->h_addr_list = (char**) r_addr_list; | |
489 | ||
490 | if (ttlp) | |
491 | *ttlp = 0; | |
492 | ||
493 | if (canonp) | |
494 | *canonp = r_name; | |
495 | ||
06202b9e YW |
496 | /* Explicitly reset both *h_errnop and h_errno to work around |
497 | * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */ | |
498 | *h_errnop = NETDB_SUCCESS; | |
499 | h_errno = 0; | |
500 | ||
4d1cf1e2 LP |
501 | return NSS_STATUS_SUCCESS; |
502 | ||
503 | fail: | |
cdccd29f | 504 | UNPROTECT_ERRNO; |
0192cbdb | 505 | *errnop = -r; |
a464cf80 | 506 | *h_errnop = NO_RECOVERY; |
954cba66 | 507 | return NSS_STATUS_UNAVAIL; |
06202b9e YW |
508 | |
509 | not_found: | |
510 | *h_errnop = HOST_NOT_FOUND; | |
511 | return NSS_STATUS_NOTFOUND; | |
4d1cf1e2 LP |
512 | } |
513 | ||
0c73f4f0 LP |
514 | typedef struct ResolveAddressReply { |
515 | JsonVariant *names; | |
516 | uint64_t flags; | |
517 | } ResolveAddressReply; | |
518 | ||
519 | static void resolve_address_reply_destroy(ResolveAddressReply *p) { | |
520 | assert(p); | |
521 | ||
522 | json_variant_unref(p->names); | |
523 | } | |
524 | ||
525 | static const JsonDispatch resolve_address_reply_dispatch_table[] = { | |
526 | { "names", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveAddressReply, names), JSON_MANDATORY }, | |
527 | { "flags", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(ResolveAddressReply, flags), 0 }, | |
528 | {} | |
529 | }; | |
530 | ||
531 | typedef struct NameParameters { | |
532 | int ifindex; | |
533 | char *name; | |
534 | } NameParameters; | |
535 | ||
536 | static void name_parameters_destroy(NameParameters *p) { | |
537 | assert(p); | |
538 | ||
539 | free(p->name); | |
540 | } | |
541 | ||
542 | static const JsonDispatch name_parameters_dispatch_table[] = { | |
543 | { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(NameParameters, ifindex), 0 }, | |
544 | { "name", JSON_VARIANT_UNSIGNED, json_dispatch_string, offsetof(NameParameters, name), JSON_MANDATORY }, | |
545 | {} | |
546 | }; | |
547 | ||
4d1cf1e2 LP |
548 | enum nss_status _nss_resolve_gethostbyaddr2_r( |
549 | const void* addr, socklen_t len, | |
550 | int af, | |
551 | struct hostent *result, | |
552 | char *buffer, size_t buflen, | |
553 | int *errnop, int *h_errnop, | |
554 | int32_t *ttlp) { | |
555 | ||
0c73f4f0 LP |
556 | _cleanup_(resolve_address_reply_destroy) ResolveAddressReply p = {}; |
557 | _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; | |
4d1cf1e2 | 558 | char *r_name, *r_aliases, *r_addr, *r_addr_list; |
0c73f4f0 LP |
559 | _cleanup_(varlink_unrefp) Varlink *link = NULL; |
560 | JsonVariant *entry, *rparams; | |
561 | const char *n, *error_id; | |
4d1cf1e2 LP |
562 | unsigned c = 0, i = 0; |
563 | size_t ms = 0, idx; | |
0c73f4f0 | 564 | int r; |
4d1cf1e2 | 565 | |
06202b9e | 566 | PROTECT_ERRNO; |
55679e29 | 567 | NSS_ENTRYPOINT_BEGIN; |
0c5eb056 | 568 | |
4d1cf1e2 LP |
569 | assert(addr); |
570 | assert(result); | |
571 | assert(buffer); | |
572 | assert(errnop); | |
573 | assert(h_errnop); | |
574 | ||
575 | if (!IN_SET(af, AF_INET, AF_INET6)) { | |
cdccd29f | 576 | UNPROTECT_ERRNO; |
0192cbdb | 577 | *errnop = EAFNOSUPPORT; |
4d1cf1e2 LP |
578 | *h_errnop = NO_DATA; |
579 | return NSS_STATUS_UNAVAIL; | |
580 | } | |
581 | ||
9d485985 | 582 | if (len != FAMILY_ADDRESS_SIZE(af)) { |
a62fc245 LP |
583 | r = -EINVAL; |
584 | goto fail; | |
4d1cf1e2 LP |
585 | } |
586 | ||
0c73f4f0 | 587 | r = connect_to_resolved(&link); |
4d1cf1e2 LP |
588 | if (r < 0) |
589 | goto fail; | |
590 | ||
0c73f4f0 | 591 | r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("address", JSON_BUILD_BYTE_ARRAY(addr, len)), |
aee9d18c LP |
592 | JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), |
593 | JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); | |
4d1cf1e2 LP |
594 | if (r < 0) |
595 | goto fail; | |
596 | ||
0c73f4f0 | 597 | r = varlink_call(link, "io.systemd.Resolve.ResolveAddress", cparams, &rparams, &error_id, NULL); |
3c229a9a YW |
598 | if (r < 0) |
599 | goto fail; | |
600 | if (!isempty(error_id)) { | |
0c73f4f0 | 601 | if (!error_shall_fallback(error_id)) |
06202b9e | 602 | goto not_found; |
46c7a7ac | 603 | goto fail; |
4d1cf1e2 LP |
604 | } |
605 | ||
55679e29 | 606 | r = json_dispatch(rparams, resolve_address_reply_dispatch_table, NULL, json_dispatch_flags, &p); |
51323288 LP |
607 | if (r < 0) |
608 | goto fail; | |
0c73f4f0 LP |
609 | if (json_variant_is_blank_object(p.names)) |
610 | goto not_found; | |
51323288 | 611 | |
0c73f4f0 LP |
612 | JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { |
613 | _cleanup_(name_parameters_destroy) NameParameters q = {}; | |
51323288 | 614 | |
55679e29 | 615 | r = json_dispatch(entry, name_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
0c73f4f0 | 616 | if (r < 0) |
78c6a153 | 617 | goto fail; |
4d1cf1e2 | 618 | |
0c73f4f0 | 619 | ms += ALIGN(strlen(q.name) + 1); |
4d1cf1e2 | 620 | } |
4d1cf1e2 | 621 | |
0c73f4f0 LP |
622 | ms += ALIGN(len) + /* the address */ |
623 | 2 * sizeof(char*) + /* pointers to the address, plus trailing NULL */ | |
624 | json_variant_elements(p.names) * sizeof(char*); /* pointers to aliases, plus trailing NULL */ | |
4d1cf1e2 LP |
625 | |
626 | if (buflen < ms) { | |
cdccd29f | 627 | UNPROTECT_ERRNO; |
0192cbdb | 628 | *errnop = ERANGE; |
e36c6e48 | 629 | *h_errnop = NETDB_INTERNAL; |
4d1cf1e2 LP |
630 | return NSS_STATUS_TRYAGAIN; |
631 | } | |
632 | ||
633 | /* First, place address */ | |
634 | r_addr = buffer; | |
635 | memcpy(r_addr, addr, len); | |
636 | idx = ALIGN(len); | |
637 | ||
638 | /* Second, place address list */ | |
639 | r_addr_list = buffer + idx; | |
640 | ((char**) r_addr_list)[0] = r_addr; | |
641 | ((char**) r_addr_list)[1] = NULL; | |
642 | idx += sizeof(char*) * 2; | |
643 | ||
644 | /* Third, reserve space for the aliases array */ | |
645 | r_aliases = buffer + idx; | |
646 | idx += sizeof(char*) * c; | |
647 | ||
648 | /* Fourth, place aliases */ | |
649 | i = 0; | |
650 | r_name = buffer + idx; | |
0c73f4f0 LP |
651 | JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { |
652 | _cleanup_(name_parameters_destroy) NameParameters q = {}; | |
4d1cf1e2 | 653 | size_t l; |
0c73f4f0 | 654 | char *z; |
4d1cf1e2 | 655 | |
55679e29 | 656 | r = json_dispatch(entry, name_parameters_dispatch_table, NULL, json_dispatch_flags, &q); |
0c73f4f0 LP |
657 | if (r < 0) |
658 | goto fail; | |
659 | ||
660 | l = strlen(q.name); | |
661 | z = buffer + idx; | |
662 | memcpy(z, n, l+1); | |
4d1cf1e2 | 663 | |
963783d7 | 664 | if (i > 0) |
0c73f4f0 | 665 | ((char**) r_aliases)[i-1] = z; |
4d1cf1e2 LP |
666 | i++; |
667 | ||
668 | idx += ALIGN(l+1); | |
669 | } | |
670 | ||
671 | ((char**) r_aliases)[c-1] = NULL; | |
672 | assert(idx == ms); | |
673 | ||
674 | result->h_name = r_name; | |
675 | result->h_aliases = (char**) r_aliases; | |
676 | result->h_addrtype = af; | |
677 | result->h_length = len; | |
678 | result->h_addr_list = (char**) r_addr_list; | |
679 | ||
680 | if (ttlp) | |
681 | *ttlp = 0; | |
682 | ||
06202b9e YW |
683 | /* Explicitly reset both *h_errnop and h_errno to work around |
684 | * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */ | |
e70df46b LP |
685 | *h_errnop = NETDB_SUCCESS; |
686 | h_errno = 0; | |
687 | ||
4d1cf1e2 LP |
688 | return NSS_STATUS_SUCCESS; |
689 | ||
690 | fail: | |
cdccd29f | 691 | UNPROTECT_ERRNO; |
0192cbdb | 692 | *errnop = -r; |
a464cf80 | 693 | *h_errnop = NO_RECOVERY; |
954cba66 | 694 | return NSS_STATUS_UNAVAIL; |
06202b9e YW |
695 | |
696 | not_found: | |
697 | *h_errnop = HOST_NOT_FOUND; | |
698 | return NSS_STATUS_NOTFOUND; | |
4d1cf1e2 LP |
699 | } |
700 | ||
701 | NSS_GETHOSTBYNAME_FALLBACKS(resolve); | |
702 | NSS_GETHOSTBYADDR_FALLBACKS(resolve); |