]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a6cc569e | 2 | /*** |
810adae9 | 3 | Copyright © 2013 Intel Corporation. All rights reserved. |
a6cc569e TG |
4 | ***/ |
5 | ||
07630cea | 6 | #include <arpa/inet.h> |
a6cc569e | 7 | #include <errno.h> |
07630cea | 8 | #include <stdlib.h> |
ca78ad1d ZJS |
9 | #include <sys/stat.h> |
10 | #include <sys/types.h> | |
11 | #include <unistd.h> | |
07630cea LP |
12 | |
13 | #include "sd-dhcp-lease.h" | |
a6cc569e | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
07630cea LP |
16 | #include "dhcp-lease-internal.h" |
17 | #include "dhcp-protocol.h" | |
18 | #include "dns-domain.h" | |
686d13b9 | 19 | #include "env-file.h" |
3ffd4af2 | 20 | #include "fd-util.h" |
fe8db0c5 | 21 | #include "fileio.h" |
abcf60e7 | 22 | #include "fs-util.h" |
b11d6a7b | 23 | #include "hexdecoct.h" |
958b66ea | 24 | #include "hostname-util.h" |
07630cea | 25 | #include "in-addr-util.h" |
0339cd77 | 26 | #include "network-internal.h" |
6bedfcbb | 27 | #include "parse-util.h" |
d054f0a4 | 28 | #include "stdio-util.h" |
b11d6a7b | 29 | #include "string-util.h" |
51517f9e | 30 | #include "strv.h" |
e4de7287 | 31 | #include "tmpfile-util.h" |
07630cea | 32 | #include "unaligned.h" |
a6cc569e TG |
33 | |
34 | int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) { | |
35 | assert_return(lease, -EINVAL); | |
36 | assert_return(addr, -EINVAL); | |
37 | ||
0339cd77 LP |
38 | if (lease->address == 0) |
39 | return -ENODATA; | |
40 | ||
a6cc569e | 41 | addr->s_addr = lease->address; |
0339cd77 LP |
42 | return 0; |
43 | } | |
a6cc569e | 44 | |
0339cd77 LP |
45 | int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr) { |
46 | assert_return(lease, -EINVAL); | |
47 | assert_return(addr, -EINVAL); | |
48 | ||
49 | if (!lease->have_broadcast) | |
50 | return -ENODATA; | |
51 | ||
52 | addr->s_addr = lease->broadcast; | |
a6cc569e TG |
53 | return 0; |
54 | } | |
55 | ||
68ceb9df PF |
56 | int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime) { |
57 | assert_return(lease, -EINVAL); | |
1c6eb4e3 | 58 | assert_return(lifetime, -EINVAL); |
68ceb9df | 59 | |
0339cd77 LP |
60 | if (lease->lifetime <= 0) |
61 | return -ENODATA; | |
62 | ||
68ceb9df | 63 | *lifetime = lease->lifetime; |
0339cd77 LP |
64 | return 0; |
65 | } | |
68ceb9df | 66 | |
0339cd77 LP |
67 | int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1) { |
68 | assert_return(lease, -EINVAL); | |
69 | assert_return(t1, -EINVAL); | |
70 | ||
71 | if (lease->t1 <= 0) | |
72 | return -ENODATA; | |
73 | ||
74 | *t1 = lease->t1; | |
75 | return 0; | |
76 | } | |
77 | ||
78 | int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint32_t *t2) { | |
79 | assert_return(lease, -EINVAL); | |
80 | assert_return(t2, -EINVAL); | |
81 | ||
82 | if (lease->t2 <= 0) | |
83 | return -ENODATA; | |
84 | ||
85 | *t2 = lease->t2; | |
68ceb9df PF |
86 | return 0; |
87 | } | |
88 | ||
a6cc569e TG |
89 | int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) { |
90 | assert_return(lease, -EINVAL); | |
91 | assert_return(mtu, -EINVAL); | |
92 | ||
0339cd77 LP |
93 | if (lease->mtu <= 0) |
94 | return -ENODATA; | |
a6cc569e | 95 | |
0339cd77 | 96 | *mtu = lease->mtu; |
a6cc569e TG |
97 | return 0; |
98 | } | |
99 | ||
8541db8f ZJS |
100 | int sd_dhcp_lease_get_servers( |
101 | sd_dhcp_lease *lease, | |
2324fd3a | 102 | sd_dhcp_lease_server_type_t what, |
8541db8f ZJS |
103 | const struct in_addr **addr) { |
104 | ||
a6cc569e | 105 | assert_return(lease, -EINVAL); |
ddb82ec2 LP |
106 | assert_return(what >= 0, -EINVAL); |
107 | assert_return(what < _SD_DHCP_LEASE_SERVER_TYPE_MAX, -EINVAL); | |
a6cc569e | 108 | assert_return(addr, -EINVAL); |
a6cc569e | 109 | |
ddb82ec2 LP |
110 | if (lease->servers[what].size <= 0) |
111 | return -ENODATA; | |
50018bfa | 112 | |
ddb82ec2 LP |
113 | *addr = lease->servers[what].addr; |
114 | return (int) lease->servers[what].size; | |
f678ac7e SS |
115 | } |
116 | ||
8541db8f | 117 | int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) { |
ddb82ec2 | 118 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_DNS, addr); |
8541db8f ZJS |
119 | } |
120 | int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
ddb82ec2 | 121 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_NTP, addr); |
8541db8f ZJS |
122 | } |
123 | int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
ddb82ec2 | 124 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SIP, addr); |
8541db8f | 125 | } |
ddb82ec2 LP |
126 | int sd_dhcp_lease_get_pop3(sd_dhcp_lease *lease, const struct in_addr **addr) { |
127 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_POP3, addr); | |
8541db8f | 128 | } |
ddb82ec2 LP |
129 | int sd_dhcp_lease_get_smtp(sd_dhcp_lease *lease, const struct in_addr **addr) { |
130 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SMTP, addr); | |
2c649ca1 | 131 | } |
ddb82ec2 LP |
132 | int sd_dhcp_lease_get_lpr(sd_dhcp_lease *lease, const struct in_addr **addr) { |
133 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_LPR, addr); | |
50018bfa | 134 | } |
2c649ca1 | 135 | |
a6cc569e TG |
136 | int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) { |
137 | assert_return(lease, -EINVAL); | |
138 | assert_return(domainname, -EINVAL); | |
139 | ||
0339cd77 LP |
140 | if (!lease->domainname) |
141 | return -ENODATA; | |
a6cc569e | 142 | |
0339cd77 | 143 | *domainname = lease->domainname; |
a6cc569e TG |
144 | return 0; |
145 | } | |
146 | ||
147 | int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) { | |
148 | assert_return(lease, -EINVAL); | |
149 | assert_return(hostname, -EINVAL); | |
150 | ||
0339cd77 LP |
151 | if (!lease->hostname) |
152 | return -ENODATA; | |
a6cc569e | 153 | |
0339cd77 | 154 | *hostname = lease->hostname; |
a6cc569e TG |
155 | return 0; |
156 | } | |
157 | ||
ce78df79 TG |
158 | int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path) { |
159 | assert_return(lease, -EINVAL); | |
160 | assert_return(root_path, -EINVAL); | |
161 | ||
0339cd77 LP |
162 | if (!lease->root_path) |
163 | return -ENODATA; | |
ce78df79 | 164 | |
0339cd77 | 165 | *root_path = lease->root_path; |
ce78df79 TG |
166 | return 0; |
167 | } | |
168 | ||
f8862395 | 169 | int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, const struct in_addr **addr) { |
a6cc569e TG |
170 | assert_return(lease, -EINVAL); |
171 | assert_return(addr, -EINVAL); | |
172 | ||
f8862395 | 173 | if (lease->router_size <= 0) |
0339cd77 | 174 | return -ENODATA; |
a6cc569e | 175 | |
f8862395 TH |
176 | *addr = lease->router; |
177 | return (int) lease->router_size; | |
a6cc569e TG |
178 | } |
179 | ||
180 | int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) { | |
181 | assert_return(lease, -EINVAL); | |
182 | assert_return(addr, -EINVAL); | |
183 | ||
0339cd77 LP |
184 | if (!lease->have_subnet_mask) |
185 | return -ENODATA; | |
a6cc569e | 186 | |
0339cd77 | 187 | addr->s_addr = lease->subnet_mask; |
a6cc569e TG |
188 | return 0; |
189 | } | |
190 | ||
0ad853bc TG |
191 | int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) { |
192 | assert_return(lease, -EINVAL); | |
193 | assert_return(addr, -EINVAL); | |
194 | ||
0339cd77 LP |
195 | if (lease->server_address == 0) |
196 | return -ENODATA; | |
0ad853bc | 197 | |
0339cd77 | 198 | addr->s_addr = lease->server_address; |
0ad853bc TG |
199 | return 0; |
200 | } | |
201 | ||
8e34a618 TG |
202 | int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) { |
203 | assert_return(lease, -EINVAL); | |
204 | assert_return(addr, -EINVAL); | |
205 | ||
0339cd77 LP |
206 | if (lease->next_server == 0) |
207 | return -ENODATA; | |
8e34a618 | 208 | |
0339cd77 | 209 | addr->s_addr = lease->next_server; |
8e34a618 TG |
210 | return 0; |
211 | } | |
212 | ||
f8693fc7 BG |
213 | /* |
214 | * The returned routes array must be freed by the caller. | |
215 | * Route objects have the same lifetime of the lease and must not be freed. | |
216 | */ | |
217 | int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, sd_dhcp_route ***routes) { | |
218 | sd_dhcp_route **ret; | |
219 | unsigned i; | |
220 | ||
e1ea665e EY |
221 | assert_return(lease, -EINVAL); |
222 | assert_return(routes, -EINVAL); | |
e1ea665e | 223 | |
0339cd77 LP |
224 | if (lease->static_route_size <= 0) |
225 | return -ENODATA; | |
e1ea665e | 226 | |
f8693fc7 BG |
227 | ret = new(sd_dhcp_route *, lease->static_route_size); |
228 | if (!ret) | |
229 | return -ENOMEM; | |
230 | ||
231 | for (i = 0; i < lease->static_route_size; i++) | |
232 | ret[i] = &lease->static_route[i]; | |
233 | ||
234 | *routes = ret; | |
0339cd77 | 235 | return (int) lease->static_route_size; |
e1ea665e EY |
236 | } |
237 | ||
b85bc551 | 238 | int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains) { |
da6053d0 | 239 | size_t r; |
b85bc551 DW |
240 | |
241 | assert_return(lease, -EINVAL); | |
242 | assert_return(domains, -EINVAL); | |
243 | ||
244 | r = strv_length(lease->search_domains); | |
245 | if (r > 0) { | |
246 | *domains = lease->search_domains; | |
247 | return (int) r; | |
248 | } | |
249 | ||
250 | return -ENODATA; | |
251 | } | |
252 | ||
0339cd77 | 253 | int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len) { |
e43a8393 BG |
254 | assert_return(lease, -EINVAL); |
255 | assert_return(data, -EINVAL); | |
256 | assert_return(data_len, -EINVAL); | |
257 | ||
0339cd77 LP |
258 | if (lease->vendor_specific_len <= 0) |
259 | return -ENODATA; | |
e43a8393 BG |
260 | |
261 | *data = lease->vendor_specific; | |
262 | *data_len = lease->vendor_specific_len; | |
e43a8393 BG |
263 | return 0; |
264 | } | |
265 | ||
8301aa0b YW |
266 | static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) { |
267 | assert(lease); | |
3733eec3 LP |
268 | |
269 | while (lease->private_options) { | |
270 | struct sd_dhcp_raw_option *option = lease->private_options; | |
271 | ||
272 | LIST_REMOVE(options, lease->private_options, option); | |
273 | ||
274 | free(option->data); | |
275 | free(option); | |
a6cc569e TG |
276 | } |
277 | ||
e2975f85 | 278 | free(lease->root_path); |
f8862395 | 279 | free(lease->router); |
e2975f85 | 280 | free(lease->timezone); |
3733eec3 LP |
281 | free(lease->hostname); |
282 | free(lease->domainname); | |
ddb82ec2 | 283 | |
2324fd3a | 284 | for (sd_dhcp_lease_server_type_t i = 0; i < _SD_DHCP_LEASE_SERVER_TYPE_MAX; i++) |
ddb82ec2 LP |
285 | free(lease->servers[i].addr); |
286 | ||
3733eec3 LP |
287 | free(lease->static_route); |
288 | free(lease->client_id); | |
289 | free(lease->vendor_specific); | |
b85bc551 | 290 | strv_free(lease->search_domains); |
6b430fdb | 291 | return mfree(lease); |
a6cc569e TG |
292 | } |
293 | ||
8301aa0b YW |
294 | DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free); |
295 | ||
0339cd77 | 296 | static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) { |
e140ae58 TG |
297 | assert(option); |
298 | assert(ret); | |
299 | ||
0339cd77 LP |
300 | if (len != 4) |
301 | return -EINVAL; | |
e140ae58 | 302 | |
0339cd77 LP |
303 | *ret = unaligned_read_be32((be32_t*) option); |
304 | if (*ret < min) | |
305 | *ret = min; | |
e140ae58 | 306 | |
0339cd77 | 307 | return 0; |
e140ae58 TG |
308 | } |
309 | ||
0339cd77 | 310 | static int lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) { |
e140ae58 TG |
311 | assert(option); |
312 | assert(ret); | |
313 | ||
0339cd77 LP |
314 | if (len != 2) |
315 | return -EINVAL; | |
e140ae58 | 316 | |
0339cd77 LP |
317 | *ret = unaligned_read_be16((be16_t*) option); |
318 | if (*ret < min) | |
319 | *ret = min; | |
f5c0c00f | 320 | |
0339cd77 | 321 | return 0; |
f5c0c00f TG |
322 | } |
323 | ||
0339cd77 | 324 | static int lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) { |
f5c0c00f TG |
325 | assert(option); |
326 | assert(ret); | |
327 | ||
0339cd77 LP |
328 | if (len != 4) |
329 | return -EINVAL; | |
f5c0c00f | 330 | |
0339cd77 LP |
331 | memcpy(ret, option, 4); |
332 | return 0; | |
f5c0c00f TG |
333 | } |
334 | ||
e140ae58 TG |
335 | static int lease_parse_string(const uint8_t *option, size_t len, char **ret) { |
336 | assert(option); | |
337 | assert(ret); | |
338 | ||
0339cd77 LP |
339 | if (len <= 0) |
340 | *ret = mfree(*ret); | |
341 | else { | |
e140ae58 TG |
342 | char *string; |
343 | ||
e989fd9b LP |
344 | /* |
345 | * One trailing NUL byte is OK, we don't mind. See: | |
346 | * https://github.com/systemd/systemd/issues/1337 | |
347 | */ | |
348 | if (memchr(option, 0, len - 1)) | |
43f447b1 LP |
349 | return -EINVAL; |
350 | ||
ea082dda | 351 | string = memdup_suffix0((const char *) option, len); |
e140ae58 | 352 | if (!string) |
43f447b1 | 353 | return -ENOMEM; |
e140ae58 | 354 | |
09348d40 | 355 | free_and_replace(*ret, string); |
0339cd77 | 356 | } |
e140ae58 TG |
357 | |
358 | return 0; | |
359 | } | |
360 | ||
978c6477 LP |
361 | static int lease_parse_domain(const uint8_t *option, size_t len, char **ret) { |
362 | _cleanup_free_ char *name = NULL, *normalized = NULL; | |
363 | int r; | |
364 | ||
365 | assert(option); | |
366 | assert(ret); | |
367 | ||
368 | r = lease_parse_string(option, len, &name); | |
369 | if (r < 0) | |
370 | return r; | |
371 | if (!name) { | |
372 | *ret = mfree(*ret); | |
373 | return 0; | |
374 | } | |
375 | ||
7470cc4c | 376 | r = dns_name_normalize(name, 0, &normalized); |
978c6477 LP |
377 | if (r < 0) |
378 | return r; | |
379 | ||
380 | if (is_localhost(normalized)) | |
381 | return -EINVAL; | |
382 | ||
383 | if (dns_name_is_root(normalized)) | |
384 | return -EINVAL; | |
385 | ||
f9ecfd3b | 386 | free_and_replace(*ret, normalized); |
978c6477 LP |
387 | |
388 | return 0; | |
389 | } | |
390 | ||
072320ea | 391 | static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { |
ddb82ec2 | 392 | assert(option || len == 0); |
e140ae58 | 393 | assert(ret); |
0339cd77 | 394 | assert(n_ret); |
e140ae58 | 395 | |
0339cd77 LP |
396 | if (len <= 0) { |
397 | *ret = mfree(*ret); | |
398 | *n_ret = 0; | |
399 | } else { | |
400 | size_t n_addresses; | |
e140ae58 TG |
401 | struct in_addr *addresses; |
402 | ||
0339cd77 LP |
403 | if (len % 4 != 0) |
404 | return -EINVAL; | |
e140ae58 | 405 | |
0339cd77 LP |
406 | n_addresses = len / 4; |
407 | ||
408 | addresses = newdup(struct in_addr, option, n_addresses); | |
e140ae58 TG |
409 | if (!addresses) |
410 | return -ENOMEM; | |
411 | ||
412 | free(*ret); | |
413 | *ret = addresses; | |
0339cd77 | 414 | *n_ret = n_addresses; |
e140ae58 TG |
415 | } |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
299d578f | 420 | static int lease_parse_sip_server(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { |
ddb82ec2 | 421 | assert(option || len == 0); |
299d578f SS |
422 | assert(ret); |
423 | assert(n_ret); | |
424 | ||
ddb82ec2 LP |
425 | if (len <= 0) |
426 | return -EINVAL; | |
299d578f | 427 | |
ddb82ec2 LP |
428 | /* The SIP record is like the other, regular server records, but prefixed with a single "encoding" |
429 | * byte that is either 0 or 1. We only support it to be 1 for now. Let's drop it and parse it like | |
430 | * the other fields */ | |
299d578f | 431 | |
ddb82ec2 LP |
432 | if (option[0] != 1) { /* We only support IP address encoding for now */ |
433 | *ret = mfree(*ret); | |
434 | *n_ret = 0; | |
435 | return 0; | |
299d578f SS |
436 | } |
437 | ||
ddb82ec2 | 438 | return lease_parse_in_addrs(option + 1, len - 1, ret, n_ret); |
299d578f SS |
439 | } |
440 | ||
0339cd77 LP |
441 | static int lease_parse_routes( |
442 | const uint8_t *option, size_t len, | |
443 | struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { | |
e1ea665e EY |
444 | |
445 | struct in_addr addr; | |
446 | ||
0339cd77 | 447 | assert(option || len <= 0); |
e1ea665e EY |
448 | assert(routes); |
449 | assert(routes_size); | |
450 | assert(routes_allocated); | |
451 | ||
0339cd77 | 452 | if (len <= 0) |
e1ea665e EY |
453 | return 0; |
454 | ||
455 | if (len % 8 != 0) | |
456 | return -EINVAL; | |
457 | ||
458 | if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + (len / 8))) | |
459 | return -ENOMEM; | |
460 | ||
461 | while (len >= 8) { | |
462 | struct sd_dhcp_route *route = *routes + *routes_size; | |
1caa12d0 | 463 | int r; |
e1ea665e | 464 | |
8cdc46e7 | 465 | route->option = SD_DHCP_OPTION_STATIC_ROUTE; |
5a941f5f | 466 | r = in4_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen); |
1caa12d0 | 467 | if (r < 0) { |
0339cd77 | 468 | log_debug("Failed to determine destination prefix length from class based IP, ignoring"); |
e1ea665e EY |
469 | continue; |
470 | } | |
471 | ||
0339cd77 | 472 | assert_se(lease_parse_be32(option, 4, &addr.s_addr) >= 0); |
e1ea665e EY |
473 | route->dst_addr = inet_makeaddr(inet_netof(addr), 0); |
474 | option += 4; | |
475 | ||
0339cd77 | 476 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
477 | option += 4; |
478 | ||
479 | len -= 8; | |
480 | (*routes_size)++; | |
481 | } | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | /* parses RFC3442 Classless Static Route Option */ | |
0339cd77 LP |
487 | static int lease_parse_classless_routes( |
488 | const uint8_t *option, size_t len, | |
489 | struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { | |
e1ea665e | 490 | |
0339cd77 | 491 | assert(option || len <= 0); |
e1ea665e EY |
492 | assert(routes); |
493 | assert(routes_size); | |
494 | assert(routes_allocated); | |
495 | ||
0339cd77 LP |
496 | if (len <= 0) |
497 | return 0; | |
498 | ||
e1ea665e EY |
499 | /* option format: (subnet-mask-width significant-subnet-octets gateway-ip)* */ |
500 | ||
501 | while (len > 0) { | |
502 | uint8_t dst_octets; | |
503 | struct sd_dhcp_route *route; | |
504 | ||
505 | if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + 1)) | |
0339cd77 | 506 | return -ENOMEM; |
e1ea665e EY |
507 | |
508 | route = *routes + *routes_size; | |
8cdc46e7 | 509 | route->option = SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE; |
e1ea665e EY |
510 | |
511 | dst_octets = (*option == 0 ? 0 : ((*option - 1) / 8) + 1); | |
512 | route->dst_prefixlen = *option; | |
513 | option++; | |
514 | len--; | |
515 | ||
516 | /* can't have more than 4 octets in IPv4 */ | |
517 | if (dst_octets > 4 || len < dst_octets) | |
518 | return -EINVAL; | |
519 | ||
520 | route->dst_addr.s_addr = 0; | |
521 | memcpy(&route->dst_addr.s_addr, option, dst_octets); | |
522 | option += dst_octets; | |
523 | len -= dst_octets; | |
524 | ||
525 | if (len < 4) | |
526 | return -EINVAL; | |
527 | ||
b14fff6e | 528 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
529 | option += 4; |
530 | len -= 4; | |
531 | ||
532 | (*routes_size)++; | |
533 | } | |
534 | ||
535 | return 0; | |
536 | } | |
537 | ||
e4735228 | 538 | int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void *userdata) { |
2d03c0b8 | 539 | sd_dhcp_lease *lease = userdata; |
e140ae58 TG |
540 | int r; |
541 | ||
542 | assert(lease); | |
a6cc569e TG |
543 | |
544 | switch(code) { | |
545 | ||
22805d92 | 546 | case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME: |
0339cd77 LP |
547 | r = lease_parse_u32(option, len, &lease->lifetime, 1); |
548 | if (r < 0) | |
549 | log_debug_errno(r, "Failed to parse lease time, ignoring: %m"); | |
550 | ||
a6cc569e TG |
551 | break; |
552 | ||
22805d92 | 553 | case SD_DHCP_OPTION_SERVER_IDENTIFIER: |
0339cd77 LP |
554 | r = lease_parse_be32(option, len, &lease->server_address); |
555 | if (r < 0) | |
556 | log_debug_errno(r, "Failed to parse server identifier, ignoring: %m"); | |
557 | ||
a6cc569e TG |
558 | break; |
559 | ||
22805d92 | 560 | case SD_DHCP_OPTION_SUBNET_MASK: |
0339cd77 LP |
561 | r = lease_parse_be32(option, len, &lease->subnet_mask); |
562 | if (r < 0) | |
563 | log_debug_errno(r, "Failed to parse subnet mask, ignoring: %m"); | |
564 | else | |
565 | lease->have_subnet_mask = true; | |
a6cc569e TG |
566 | break; |
567 | ||
22805d92 | 568 | case SD_DHCP_OPTION_BROADCAST: |
0339cd77 LP |
569 | r = lease_parse_be32(option, len, &lease->broadcast); |
570 | if (r < 0) | |
571 | log_debug_errno(r, "Failed to parse broadcast address, ignoring: %m"); | |
572 | else | |
573 | lease->have_broadcast = true; | |
f5c0c00f TG |
574 | break; |
575 | ||
22805d92 | 576 | case SD_DHCP_OPTION_ROUTER: |
072320ea | 577 | r = lease_parse_in_addrs(option, len, &lease->router, &lease->router_size); |
f8862395 TH |
578 | if (r < 0) |
579 | log_debug_errno(r, "Failed to parse router addresses, ignoring: %m"); | |
a6cc569e TG |
580 | break; |
581 | ||
22805d92 | 582 | case SD_DHCP_OPTION_DOMAIN_NAME_SERVER: |
ddb82ec2 | 583 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_DNS].addr, &lease->servers[SD_DHCP_LEASE_DNS].size); |
0339cd77 LP |
584 | if (r < 0) |
585 | log_debug_errno(r, "Failed to parse DNS server, ignoring: %m"); | |
a6cc569e TG |
586 | break; |
587 | ||
22805d92 | 588 | case SD_DHCP_OPTION_NTP_SERVER: |
ddb82ec2 | 589 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_NTP].addr, &lease->servers[SD_DHCP_LEASE_NTP].size); |
0339cd77 LP |
590 | if (r < 0) |
591 | log_debug_errno(r, "Failed to parse NTP server, ignoring: %m"); | |
f5c0c00f TG |
592 | break; |
593 | ||
299d578f | 594 | case SD_DHCP_OPTION_SIP_SERVER: |
ddb82ec2 | 595 | r = lease_parse_sip_server(option, len, &lease->servers[SD_DHCP_LEASE_SIP].addr, &lease->servers[SD_DHCP_LEASE_SIP].size); |
299d578f SS |
596 | if (r < 0) |
597 | log_debug_errno(r, "Failed to parse SIP server, ignoring: %m"); | |
598 | break; | |
599 | ||
f678ac7e | 600 | case SD_DHCP_OPTION_POP3_SERVER: |
ddb82ec2 | 601 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_POP3].addr, &lease->servers[SD_DHCP_LEASE_POP3].size); |
f678ac7e SS |
602 | if (r < 0) |
603 | log_debug_errno(r, "Failed to parse POP3 server, ignoring: %m"); | |
604 | break; | |
605 | ||
2c649ca1 | 606 | case SD_DHCP_OPTION_SMTP_SERVER: |
ddb82ec2 | 607 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_SMTP].addr, &lease->servers[SD_DHCP_LEASE_SMTP].size); |
2c649ca1 SS |
608 | if (r < 0) |
609 | log_debug_errno(r, "Failed to parse SMTP server, ignoring: %m"); | |
610 | break; | |
611 | ||
50018bfa | 612 | case SD_DHCP_OPTION_LPR_SERVER: |
ddb82ec2 | 613 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_LPR].addr, &lease->servers[SD_DHCP_LEASE_LPR].size); |
50018bfa SS |
614 | if (r < 0) |
615 | log_debug_errno(r, "Failed to parse LPR server, ignoring: %m"); | |
616 | break; | |
617 | ||
22805d92 | 618 | case SD_DHCP_OPTION_STATIC_ROUTE: |
0339cd77 LP |
619 | r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated); |
620 | if (r < 0) | |
621 | log_debug_errno(r, "Failed to parse static routes, ignoring: %m"); | |
f5c0c00f TG |
622 | break; |
623 | ||
22805d92 | 624 | case SD_DHCP_OPTION_INTERFACE_MTU: |
0339cd77 LP |
625 | r = lease_parse_u16(option, len, &lease->mtu, 68); |
626 | if (r < 0) | |
627 | log_debug_errno(r, "Failed to parse MTU, ignoring: %m"); | |
955d99ed | 628 | if (lease->mtu < DHCP_DEFAULT_MIN_SIZE) { |
4dd53da9 | 629 | log_debug("MTU value of %" PRIu16 " too small. Using default MTU value of %d instead.", lease->mtu, DHCP_DEFAULT_MIN_SIZE); |
955d99ed MG |
630 | lease->mtu = DHCP_DEFAULT_MIN_SIZE; |
631 | } | |
632 | ||
f5c0c00f TG |
633 | break; |
634 | ||
978c6477 LP |
635 | case SD_DHCP_OPTION_DOMAIN_NAME: |
636 | r = lease_parse_domain(option, len, &lease->domainname); | |
0339cd77 LP |
637 | if (r < 0) { |
638 | log_debug_errno(r, "Failed to parse domain name, ignoring: %m"); | |
639 | return 0; | |
640 | } | |
a6cc569e | 641 | |
784d9b9c | 642 | break; |
784d9b9c | 643 | |
b85bc551 DW |
644 | case SD_DHCP_OPTION_DOMAIN_SEARCH_LIST: |
645 | r = dhcp_lease_parse_search_domains(option, len, &lease->search_domains); | |
646 | if (r < 0) | |
647 | log_debug_errno(r, "Failed to parse Domain Search List, ignoring: %m"); | |
648 | break; | |
649 | ||
978c6477 LP |
650 | case SD_DHCP_OPTION_HOST_NAME: |
651 | r = lease_parse_domain(option, len, &lease->hostname); | |
0339cd77 | 652 | if (r < 0) { |
38b38500 | 653 | log_debug_errno(r, "Failed to parse hostname, ignoring: %m"); |
0339cd77 LP |
654 | return 0; |
655 | } | |
a6cc569e | 656 | |
784d9b9c | 657 | break; |
ce78df79 | 658 | |
22805d92 | 659 | case SD_DHCP_OPTION_ROOT_PATH: |
0339cd77 LP |
660 | r = lease_parse_string(option, len, &lease->root_path); |
661 | if (r < 0) | |
662 | log_debug_errno(r, "Failed to parse root path, ignoring: %m"); | |
663 | break; | |
ce78df79 | 664 | |
22805d92 | 665 | case SD_DHCP_OPTION_RENEWAL_T1_TIME: |
0339cd77 LP |
666 | r = lease_parse_u32(option, len, &lease->t1, 1); |
667 | if (r < 0) | |
668 | log_debug_errno(r, "Failed to parse T1 time, ignoring: %m"); | |
a6cc569e TG |
669 | break; |
670 | ||
22805d92 | 671 | case SD_DHCP_OPTION_REBINDING_T2_TIME: |
0339cd77 LP |
672 | r = lease_parse_u32(option, len, &lease->t2, 1); |
673 | if (r < 0) | |
674 | log_debug_errno(r, "Failed to parse T2 time, ignoring: %m"); | |
a6cc569e | 675 | break; |
e1ea665e | 676 | |
22805d92 | 677 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
0339cd77 | 678 | r = lease_parse_classless_routes( |
2d03c0b8 LP |
679 | option, len, |
680 | &lease->static_route, | |
681 | &lease->static_route_size, | |
682 | &lease->static_route_allocated); | |
0339cd77 LP |
683 | if (r < 0) |
684 | log_debug_errno(r, "Failed to parse classless routes, ignoring: %m"); | |
685 | break; | |
e43a8393 | 686 | |
22805d92 | 687 | case SD_DHCP_OPTION_NEW_TZDB_TIMEZONE: { |
8eb9058d LP |
688 | _cleanup_free_ char *tz = NULL; |
689 | ||
690 | r = lease_parse_string(option, len, &tz); | |
0339cd77 LP |
691 | if (r < 0) { |
692 | log_debug_errno(r, "Failed to parse timezone option, ignoring: %m"); | |
693 | return 0; | |
694 | } | |
8eb9058d | 695 | |
089fb865 | 696 | if (!timezone_is_valid(tz, LOG_DEBUG)) { |
0339cd77 LP |
697 | log_debug_errno(r, "Timezone is not valid, ignoring: %m"); |
698 | return 0; | |
699 | } | |
8eb9058d | 700 | |
f9ecfd3b | 701 | free_and_replace(lease->timezone, tz); |
0339cd77 | 702 | |
8eb9058d LP |
703 | break; |
704 | } | |
705 | ||
22805d92 | 706 | case SD_DHCP_OPTION_VENDOR_SPECIFIC: |
2d03c0b8 | 707 | |
0339cd77 LP |
708 | if (len <= 0) |
709 | lease->vendor_specific = mfree(lease->vendor_specific); | |
710 | else { | |
711 | void *p; | |
712 | ||
713 | p = memdup(option, len); | |
714 | if (!p) | |
e43a8393 | 715 | return -ENOMEM; |
e43a8393 | 716 | |
0339cd77 LP |
717 | free(lease->vendor_specific); |
718 | lease->vendor_specific = p; | |
719 | } | |
7e753d9d | 720 | |
0339cd77 LP |
721 | lease->vendor_specific_len = len; |
722 | break; | |
7e753d9d | 723 | |
22805d92 | 724 | case SD_DHCP_OPTION_PRIVATE_BASE ... SD_DHCP_OPTION_PRIVATE_LAST: |
7e753d9d AC |
725 | r = dhcp_lease_insert_private_option(lease, code, option, len); |
726 | if (r < 0) | |
727 | return r; | |
0339cd77 LP |
728 | |
729 | break; | |
730 | ||
731 | default: | |
f693e9b3 | 732 | log_debug("Ignoring option DHCP option %"PRIu8" while parsing.", code); |
0339cd77 | 733 | break; |
a6cc569e TG |
734 | } |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
b85bc551 DW |
739 | /* Parses compressed domain names. */ |
740 | int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***domains) { | |
741 | _cleanup_strv_free_ char **names = NULL; | |
742 | size_t pos = 0, cnt = 0; | |
743 | int r; | |
744 | ||
745 | assert(domains); | |
746 | assert_return(option && len > 0, -ENODATA); | |
747 | ||
748 | while (pos < len) { | |
749 | _cleanup_free_ char *name = NULL; | |
750 | size_t n = 0, allocated = 0; | |
751 | size_t jump_barrier = pos, next_chunk = 0; | |
752 | bool first = true; | |
753 | ||
754 | for (;;) { | |
755 | uint8_t c; | |
756 | c = option[pos++]; | |
757 | ||
758 | if (c == 0) { | |
759 | /* End of name */ | |
760 | break; | |
761 | } else if (c <= 63) { | |
762 | const char *label; | |
763 | ||
764 | /* Literal label */ | |
765 | label = (const char*) (option + pos); | |
766 | pos += c; | |
767 | if (pos >= len) | |
768 | return -EBADMSG; | |
769 | ||
770 | if (!GREEDY_REALLOC(name, allocated, n + !first + DNS_LABEL_ESCAPED_MAX)) | |
771 | return -ENOMEM; | |
772 | ||
773 | if (first) | |
774 | first = false; | |
775 | else | |
776 | name[n++] = '.'; | |
777 | ||
778 | r = dns_label_escape(label, c, name + n, DNS_LABEL_ESCAPED_MAX); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
782 | n += r; | |
1d6cc5d0 | 783 | } else if (FLAGS_SET(c, 0xc0)) { |
b85bc551 DW |
784 | /* Pointer */ |
785 | ||
786 | uint8_t d; | |
787 | uint16_t ptr; | |
788 | ||
789 | if (pos >= len) | |
790 | return -EBADMSG; | |
791 | ||
792 | d = option[pos++]; | |
793 | ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d; | |
794 | ||
795 | /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */ | |
796 | if (ptr >= jump_barrier) | |
797 | return -EBADMSG; | |
798 | jump_barrier = ptr; | |
799 | ||
800 | /* Save current location so we don't end up re-parsing what's parsed so far. */ | |
801 | if (next_chunk == 0) | |
802 | next_chunk = pos; | |
803 | ||
804 | pos = ptr; | |
805 | } else | |
806 | return -EBADMSG; | |
807 | } | |
808 | ||
809 | if (!GREEDY_REALLOC(name, allocated, n + 1)) | |
810 | return -ENOMEM; | |
811 | name[n] = 0; | |
812 | ||
813 | r = strv_extend(&names, name); | |
814 | if (r < 0) | |
815 | return r; | |
816 | ||
817 | cnt++; | |
818 | ||
819 | if (next_chunk != 0) | |
820 | pos = next_chunk; | |
821 | } | |
822 | ||
ae2a15bc | 823 | *domains = TAKE_PTR(names); |
b85bc551 DW |
824 | |
825 | return cnt; | |
826 | } | |
827 | ||
0339cd77 | 828 | int dhcp_lease_insert_private_option(sd_dhcp_lease *lease, uint8_t tag, const void *data, uint8_t len) { |
7e753d9d AC |
829 | struct sd_dhcp_raw_option *cur, *option; |
830 | ||
0339cd77 LP |
831 | assert(lease); |
832 | ||
7e753d9d AC |
833 | LIST_FOREACH(options, cur, lease->private_options) { |
834 | if (tag < cur->tag) | |
835 | break; | |
0339cd77 LP |
836 | if (tag == cur->tag) { |
837 | log_debug("Ignoring duplicate option, tagged %i.", tag); | |
7e753d9d AC |
838 | return 0; |
839 | } | |
840 | } | |
841 | ||
842 | option = new(struct sd_dhcp_raw_option, 1); | |
843 | if (!option) | |
844 | return -ENOMEM; | |
845 | ||
846 | option->tag = tag; | |
847 | option->length = len; | |
848 | option->data = memdup(data, len); | |
849 | if (!option->data) { | |
850 | free(option); | |
851 | return -ENOMEM; | |
852 | } | |
853 | ||
854 | LIST_INSERT_BEFORE(options, lease->private_options, cur, option); | |
7e753d9d AC |
855 | return 0; |
856 | } | |
857 | ||
a6cc569e | 858 | int dhcp_lease_new(sd_dhcp_lease **ret) { |
6e00a806 | 859 | sd_dhcp_lease *lease; |
a6cc569e TG |
860 | |
861 | lease = new0(sd_dhcp_lease, 1); | |
862 | if (!lease) | |
863 | return -ENOMEM; | |
864 | ||
3733eec3 | 865 | lease->n_ref = 1; |
a6cc569e TG |
866 | |
867 | *ret = lease; | |
a6cc569e TG |
868 | return 0; |
869 | } | |
fe8db0c5 | 870 | |
bd91b83e | 871 | int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { |
ab7153b3 | 872 | _cleanup_(unlink_and_freep) char *temp_path = NULL; |
fe8db0c5 | 873 | _cleanup_fclose_ FILE *f = NULL; |
a073309f | 874 | struct sd_dhcp_raw_option *option; |
fe8db0c5 | 875 | struct in_addr address; |
a2ba62c7 | 876 | const struct in_addr *addresses; |
e4735228 | 877 | const void *client_id, *data; |
e43a8393 | 878 | size_t client_id_len, data_len; |
189255d2 | 879 | char sbuf[INET_ADDRSTRLEN]; |
fe8db0c5 TG |
880 | const char *string; |
881 | uint16_t mtu; | |
f8693fc7 | 882 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
b85bc551 | 883 | char **search_domains = NULL; |
0339cd77 | 884 | uint32_t t1, t2, lifetime; |
fe8db0c5 TG |
885 | int r; |
886 | ||
887 | assert(lease); | |
888 | assert(lease_file); | |
889 | ||
fe8db0c5 TG |
890 | r = fopen_temporary(lease_file, &f, &temp_path); |
891 | if (r < 0) | |
ab7153b3 | 892 | return r; |
fe8db0c5 | 893 | |
0d536673 | 894 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 | 895 | |
fe8db0c5 | 896 | fprintf(f, |
0339cd77 | 897 | "# This is private data. Do not parse.\n"); |
fe8db0c5 | 898 | |
0339cd77 LP |
899 | r = sd_dhcp_lease_get_address(lease, &address); |
900 | if (r >= 0) | |
189255d2 | 901 | fprintf(f, "ADDRESS=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf))); |
fe8db0c5 | 902 | |
0339cd77 LP |
903 | r = sd_dhcp_lease_get_netmask(lease, &address); |
904 | if (r >= 0) | |
189255d2 | 905 | fprintf(f, "NETMASK=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf))); |
fe8db0c5 | 906 | |
f8862395 TH |
907 | r = sd_dhcp_lease_get_router(lease, &addresses); |
908 | if (r > 0) { | |
909 | fputs("ROUTER=", f); | |
072320ea | 910 | serialize_in_addrs(f, addresses, r, false, NULL); |
f8862395 TH |
911 | fputc('\n', f); |
912 | } | |
8ddbeaa2 | 913 | |
0ad853bc | 914 | r = sd_dhcp_lease_get_server_identifier(lease, &address); |
109731eb | 915 | if (r >= 0) |
189255d2 | 916 | fprintf(f, "SERVER_ADDRESS=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf))); |
0ad853bc | 917 | |
8e34a618 | 918 | r = sd_dhcp_lease_get_next_server(lease, &address); |
109731eb | 919 | if (r >= 0) |
189255d2 | 920 | fprintf(f, "NEXT_SERVER=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf))); |
8e34a618 | 921 | |
0339cd77 LP |
922 | r = sd_dhcp_lease_get_broadcast(lease, &address); |
923 | if (r >= 0) | |
189255d2 | 924 | fprintf(f, "BROADCAST=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf))); |
0339cd77 | 925 | |
fe8db0c5 TG |
926 | r = sd_dhcp_lease_get_mtu(lease, &mtu); |
927 | if (r >= 0) | |
928 | fprintf(f, "MTU=%" PRIu16 "\n", mtu); | |
929 | ||
0339cd77 LP |
930 | r = sd_dhcp_lease_get_t1(lease, &t1); |
931 | if (r >= 0) | |
932 | fprintf(f, "T1=%" PRIu32 "\n", t1); | |
933 | ||
934 | r = sd_dhcp_lease_get_t2(lease, &t2); | |
109731eb | 935 | if (r >= 0) |
0339cd77 LP |
936 | fprintf(f, "T2=%" PRIu32 "\n", t2); |
937 | ||
938 | r = sd_dhcp_lease_get_lifetime(lease, &lifetime); | |
939 | if (r >= 0) | |
940 | fprintf(f, "LIFETIME=%" PRIu32 "\n", lifetime); | |
941 | ||
942 | r = sd_dhcp_lease_get_dns(lease, &addresses); | |
943 | if (r > 0) { | |
0d536673 | 944 | fputs("DNS=", f); |
072320ea | 945 | serialize_in_addrs(f, addresses, r, false, NULL); |
f8862395 | 946 | fputc('\n', f); |
0339cd77 | 947 | } |
109731eb | 948 | |
a2ba62c7 | 949 | r = sd_dhcp_lease_get_ntp(lease, &addresses); |
0339cd77 | 950 | if (r > 0) { |
0d536673 | 951 | fputs("NTP=", f); |
072320ea | 952 | serialize_in_addrs(f, addresses, r, false, NULL); |
f8862395 | 953 | fputc('\n', f); |
0339cd77 | 954 | } |
fe8db0c5 | 955 | |
299d578f SS |
956 | r = sd_dhcp_lease_get_sip(lease, &addresses); |
957 | if (r > 0) { | |
958 | fputs("SIP=", f); | |
959 | serialize_in_addrs(f, addresses, r, false, NULL); | |
960 | fputc('\n', f); | |
961 | } | |
962 | ||
fe8db0c5 TG |
963 | r = sd_dhcp_lease_get_domainname(lease, &string); |
964 | if (r >= 0) | |
965 | fprintf(f, "DOMAINNAME=%s\n", string); | |
966 | ||
b85bc551 DW |
967 | r = sd_dhcp_lease_get_search_domains(lease, &search_domains); |
968 | if (r > 0) { | |
0d536673 | 969 | fputs("DOMAIN_SEARCH_LIST=", f); |
b85bc551 | 970 | fputstrv(f, search_domains, NULL, NULL); |
f8862395 | 971 | fputc('\n', f); |
b85bc551 DW |
972 | } |
973 | ||
fe8db0c5 TG |
974 | r = sd_dhcp_lease_get_hostname(lease, &string); |
975 | if (r >= 0) | |
976 | fprintf(f, "HOSTNAME=%s\n", string); | |
977 | ||
ce78df79 TG |
978 | r = sd_dhcp_lease_get_root_path(lease, &string); |
979 | if (r >= 0) | |
980 | fprintf(f, "ROOT_PATH=%s\n", string); | |
981 | ||
a2ba62c7 | 982 | r = sd_dhcp_lease_get_routes(lease, &routes); |
0339cd77 | 983 | if (r > 0) |
a2ba62c7 | 984 | serialize_dhcp_routes(f, "ROUTES", routes, r); |
e1ea665e | 985 | |
8eb9058d LP |
986 | r = sd_dhcp_lease_get_timezone(lease, &string); |
987 | if (r >= 0) | |
988 | fprintf(f, "TIMEZONE=%s\n", string); | |
989 | ||
e37f74a6 DW |
990 | r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len); |
991 | if (r >= 0) { | |
3587161a | 992 | _cleanup_free_ char *client_id_hex = NULL; |
e37f74a6 | 993 | |
dde8bb32 | 994 | client_id_hex = hexmem(client_id, client_id_len); |
ab7153b3 YW |
995 | if (!client_id_hex) |
996 | return -ENOMEM; | |
e37f74a6 DW |
997 | fprintf(f, "CLIENTID=%s\n", client_id_hex); |
998 | } | |
999 | ||
e43a8393 BG |
1000 | r = sd_dhcp_lease_get_vendor_specific(lease, &data, &data_len); |
1001 | if (r >= 0) { | |
1002 | _cleanup_free_ char *option_hex = NULL; | |
1003 | ||
1004 | option_hex = hexmem(data, data_len); | |
ab7153b3 YW |
1005 | if (!option_hex) |
1006 | return -ENOMEM; | |
e43a8393 BG |
1007 | fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex); |
1008 | } | |
1009 | ||
a073309f | 1010 | LIST_FOREACH(options, option, lease->private_options) { |
dbcb4a90 | 1011 | char key[STRLEN("OPTION_000")+1]; |
0339cd77 | 1012 | |
d054f0a4 | 1013 | xsprintf(key, "OPTION_%" PRIu8, option->tag); |
a073309f AC |
1014 | r = serialize_dhcp_option(f, key, option->data, option->length); |
1015 | if (r < 0) | |
ab7153b3 | 1016 | return r; |
a073309f AC |
1017 | } |
1018 | ||
dacd6cee LP |
1019 | r = fflush_and_check(f); |
1020 | if (r < 0) | |
ab7153b3 | 1021 | return r; |
fe8db0c5 | 1022 | |
abcf60e7 YW |
1023 | r = conservative_rename(temp_path, lease_file); |
1024 | if (r < 0) | |
ab7153b3 | 1025 | return r; |
dacd6cee | 1026 | |
ab7153b3 | 1027 | temp_path = mfree(temp_path); |
fe8db0c5 | 1028 | |
ab7153b3 | 1029 | return 0; |
fe8db0c5 TG |
1030 | } |
1031 | ||
bd91b83e | 1032 | int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { |
4afd3348 | 1033 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
0339cd77 LP |
1034 | _cleanup_free_ char |
1035 | *address = NULL, | |
1036 | *router = NULL, | |
1037 | *netmask = NULL, | |
1038 | *server_address = NULL, | |
1039 | *next_server = NULL, | |
1040 | *broadcast = NULL, | |
1041 | *dns = NULL, | |
1042 | *ntp = NULL, | |
299d578f | 1043 | *sip = NULL, |
ddb82ec2 LP |
1044 | *pop3 = NULL, |
1045 | *smtp = NULL, | |
1046 | *lpr = NULL, | |
0339cd77 LP |
1047 | *mtu = NULL, |
1048 | *routes = NULL, | |
b85bc551 | 1049 | *domains = NULL, |
0339cd77 LP |
1050 | *client_id_hex = NULL, |
1051 | *vendor_specific_hex = NULL, | |
1052 | *lifetime = NULL, | |
1053 | *t1 = NULL, | |
1054 | *t2 = NULL, | |
22805d92 | 1055 | *options[SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE + 1] = {}; |
0339cd77 | 1056 | |
a073309f | 1057 | int r, i; |
fe8db0c5 TG |
1058 | |
1059 | assert(lease_file); | |
1060 | assert(ret); | |
1061 | ||
1062 | r = dhcp_lease_new(&lease); | |
1063 | if (r < 0) | |
1064 | return r; | |
1065 | ||
aa8fbc74 | 1066 | r = parse_env_file(NULL, lease_file, |
fe8db0c5 TG |
1067 | "ADDRESS", &address, |
1068 | "ROUTER", &router, | |
1069 | "NETMASK", &netmask, | |
fd1f3b3e | 1070 | "SERVER_ADDRESS", &server_address, |
8e34a618 | 1071 | "NEXT_SERVER", &next_server, |
0339cd77 | 1072 | "BROADCAST", &broadcast, |
109731eb TG |
1073 | "DNS", &dns, |
1074 | "NTP", &ntp, | |
299d578f | 1075 | "SIP", &sip, |
ddb82ec2 LP |
1076 | "POP3", &pop3, |
1077 | "SMTP", &smtp, | |
1078 | "LPR", &lpr, | |
fe8db0c5 TG |
1079 | "MTU", &mtu, |
1080 | "DOMAINNAME", &lease->domainname, | |
1081 | "HOSTNAME", &lease->hostname, | |
b85bc551 | 1082 | "DOMAIN_SEARCH_LIST", &domains, |
ce78df79 | 1083 | "ROOT_PATH", &lease->root_path, |
e1ea665e | 1084 | "ROUTES", &routes, |
e37f74a6 | 1085 | "CLIENTID", &client_id_hex, |
8eb9058d | 1086 | "TIMEZONE", &lease->timezone, |
e43a8393 | 1087 | "VENDOR_SPECIFIC", &vendor_specific_hex, |
0339cd77 LP |
1088 | "LIFETIME", &lifetime, |
1089 | "T1", &t1, | |
1090 | "T2", &t2, | |
a073309f AC |
1091 | "OPTION_224", &options[0], |
1092 | "OPTION_225", &options[1], | |
1093 | "OPTION_226", &options[2], | |
1094 | "OPTION_227", &options[3], | |
1095 | "OPTION_228", &options[4], | |
1096 | "OPTION_229", &options[5], | |
1097 | "OPTION_230", &options[6], | |
1098 | "OPTION_231", &options[7], | |
1099 | "OPTION_232", &options[8], | |
1100 | "OPTION_233", &options[9], | |
1101 | "OPTION_234", &options[10], | |
1102 | "OPTION_235", &options[11], | |
1103 | "OPTION_236", &options[12], | |
1104 | "OPTION_237", &options[13], | |
1105 | "OPTION_238", &options[14], | |
1106 | "OPTION_239", &options[15], | |
1107 | "OPTION_240", &options[16], | |
1108 | "OPTION_241", &options[17], | |
1109 | "OPTION_242", &options[18], | |
1110 | "OPTION_243", &options[19], | |
1111 | "OPTION_244", &options[20], | |
1112 | "OPTION_245", &options[21], | |
1113 | "OPTION_246", &options[22], | |
1114 | "OPTION_247", &options[23], | |
1115 | "OPTION_248", &options[24], | |
1116 | "OPTION_249", &options[25], | |
1117 | "OPTION_250", &options[26], | |
1118 | "OPTION_251", &options[27], | |
1119 | "OPTION_252", &options[28], | |
1120 | "OPTION_253", &options[29], | |
13df9c39 | 1121 | "OPTION_254", &options[30]); |
fe8db0c5 TG |
1122 | if (r < 0) |
1123 | return r; | |
1124 | ||
0339cd77 LP |
1125 | if (address) { |
1126 | r = inet_pton(AF_INET, address, &lease->address); | |
1127 | if (r <= 0) | |
e26ea7fc | 1128 | log_debug("Failed to parse address %s, ignoring.", address); |
0339cd77 | 1129 | } |
fe8db0c5 | 1130 | |
8ddbeaa2 | 1131 | if (router) { |
f8862395 TH |
1132 | r = deserialize_in_addrs(&lease->router, router); |
1133 | if (r < 0) | |
1134 | log_debug_errno(r, "Failed to deserialize router addresses %s, ignoring: %m", router); | |
1135 | else | |
1136 | lease->router_size = r; | |
8ddbeaa2 | 1137 | } |
fe8db0c5 | 1138 | |
0339cd77 LP |
1139 | if (netmask) { |
1140 | r = inet_pton(AF_INET, netmask, &lease->subnet_mask); | |
1141 | if (r <= 0) | |
e26ea7fc | 1142 | log_debug("Failed to parse netmask %s, ignoring.", netmask); |
0339cd77 LP |
1143 | else |
1144 | lease->have_subnet_mask = true; | |
1145 | } | |
fe8db0c5 | 1146 | |
0ad853bc | 1147 | if (server_address) { |
0339cd77 LP |
1148 | r = inet_pton(AF_INET, server_address, &lease->server_address); |
1149 | if (r <= 0) | |
e26ea7fc | 1150 | log_debug("Failed to parse server address %s, ignoring.", server_address); |
0ad853bc TG |
1151 | } |
1152 | ||
8e34a618 | 1153 | if (next_server) { |
0339cd77 LP |
1154 | r = inet_pton(AF_INET, next_server, &lease->next_server); |
1155 | if (r <= 0) | |
e26ea7fc | 1156 | log_debug("Failed to parse next server %s, ignoring.", next_server); |
0339cd77 | 1157 | } |
8e34a618 | 1158 | |
0339cd77 LP |
1159 | if (broadcast) { |
1160 | r = inet_pton(AF_INET, broadcast, &lease->broadcast); | |
1161 | if (r <= 0) | |
e26ea7fc | 1162 | log_debug("Failed to parse broadcast address %s, ignoring.", broadcast); |
0339cd77 LP |
1163 | else |
1164 | lease->have_broadcast = true; | |
8e34a618 TG |
1165 | } |
1166 | ||
109731eb | 1167 | if (dns) { |
ddb82ec2 | 1168 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_DNS].addr, dns); |
109731eb | 1169 | if (r < 0) |
0339cd77 LP |
1170 | log_debug_errno(r, "Failed to deserialize DNS servers %s, ignoring: %m", dns); |
1171 | else | |
ddb82ec2 | 1172 | lease->servers[SD_DHCP_LEASE_DNS].size = r; |
109731eb TG |
1173 | } |
1174 | ||
1175 | if (ntp) { | |
ddb82ec2 | 1176 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_NTP].addr, ntp); |
109731eb | 1177 | if (r < 0) |
0339cd77 LP |
1178 | log_debug_errno(r, "Failed to deserialize NTP servers %s, ignoring: %m", ntp); |
1179 | else | |
ddb82ec2 | 1180 | lease->servers[SD_DHCP_LEASE_NTP].size = r; |
109731eb TG |
1181 | } |
1182 | ||
299d578f | 1183 | if (sip) { |
ddb82ec2 | 1184 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_SIP].addr, sip); |
299d578f | 1185 | if (r < 0) |
850fd9b8 | 1186 | log_debug_errno(r, "Failed to deserialize SIP servers %s, ignoring: %m", sip); |
299d578f | 1187 | else |
ddb82ec2 | 1188 | lease->servers[SD_DHCP_LEASE_SIP].size = r; |
299d578f SS |
1189 | } |
1190 | ||
ddb82ec2 LP |
1191 | if (pop3) { |
1192 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_POP3].addr, pop3); | |
f678ac7e | 1193 | if (r < 0) |
ddb82ec2 | 1194 | log_debug_errno(r, "Failed to deserialize POP3 server %s, ignoring: %m", pop3); |
f678ac7e | 1195 | else |
ddb82ec2 | 1196 | lease->servers[SD_DHCP_LEASE_POP3].size = r; |
f678ac7e SS |
1197 | } |
1198 | ||
ddb82ec2 LP |
1199 | if (smtp) { |
1200 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_SMTP].addr, smtp); | |
2c649ca1 | 1201 | if (r < 0) |
ddb82ec2 | 1202 | log_debug_errno(r, "Failed to deserialize SMTP server %s, ignoring: %m", smtp); |
2c649ca1 | 1203 | else |
ddb82ec2 | 1204 | lease->servers[SD_DHCP_LEASE_SMTP].size = r; |
2c649ca1 SS |
1205 | } |
1206 | ||
ddb82ec2 LP |
1207 | if (lpr) { |
1208 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_LPR].addr, lpr); | |
50018bfa | 1209 | if (r < 0) |
ddb82ec2 | 1210 | log_debug_errno(r, "Failed to deserialize LPR server %s, ignoring: %m", lpr); |
50018bfa | 1211 | else |
ddb82ec2 | 1212 | lease->servers[SD_DHCP_LEASE_LPR].size = r; |
50018bfa SS |
1213 | } |
1214 | ||
fe8db0c5 | 1215 | if (mtu) { |
0339cd77 LP |
1216 | r = safe_atou16(mtu, &lease->mtu); |
1217 | if (r < 0) | |
1218 | log_debug_errno(r, "Failed to parse MTU %s, ignoring: %m", mtu); | |
fe8db0c5 TG |
1219 | } |
1220 | ||
b85bc551 DW |
1221 | if (domains) { |
1222 | _cleanup_strv_free_ char **a = NULL; | |
1223 | a = strv_split(domains, " "); | |
1224 | if (!a) | |
1225 | return -ENOMEM; | |
1226 | ||
d46b79bb | 1227 | if (!strv_isempty(a)) |
d7a0f1f4 | 1228 | lease->search_domains = TAKE_PTR(a); |
b85bc551 DW |
1229 | } |
1230 | ||
e1ea665e | 1231 | if (routes) { |
0339cd77 LP |
1232 | r = deserialize_dhcp_routes( |
1233 | &lease->static_route, | |
1234 | &lease->static_route_size, | |
1235 | &lease->static_route_allocated, | |
1236 | routes); | |
1237 | if (r < 0) | |
1238 | log_debug_errno(r, "Failed to parse DHCP routes %s, ignoring: %m", routes); | |
1239 | } | |
1240 | ||
1241 | if (lifetime) { | |
1242 | r = safe_atou32(lifetime, &lease->lifetime); | |
1243 | if (r < 0) | |
1244 | log_debug_errno(r, "Failed to parse lifetime %s, ignoring: %m", lifetime); | |
1245 | } | |
1246 | ||
1247 | if (t1) { | |
1248 | r = safe_atou32(t1, &lease->t1); | |
1249 | if (r < 0) | |
1250 | log_debug_errno(r, "Failed to parse T1 %s, ignoring: %m", t1); | |
1251 | } | |
1252 | ||
1253 | if (t2) { | |
1254 | r = safe_atou32(t2, &lease->t2); | |
e1ea665e | 1255 | if (r < 0) |
0339cd77 | 1256 | log_debug_errno(r, "Failed to parse T2 %s, ignoring: %m", t2); |
e1ea665e EY |
1257 | } |
1258 | ||
e37f74a6 | 1259 | if (client_id_hex) { |
f5fbe71d | 1260 | r = unhexmem(client_id_hex, SIZE_MAX, &lease->client_id, &lease->client_id_len); |
30494563 | 1261 | if (r < 0) |
0339cd77 | 1262 | log_debug_errno(r, "Failed to parse client ID %s, ignoring: %m", client_id_hex); |
e37f74a6 DW |
1263 | } |
1264 | ||
e43a8393 | 1265 | if (vendor_specific_hex) { |
f5fbe71d | 1266 | r = unhexmem(vendor_specific_hex, SIZE_MAX, &lease->vendor_specific, &lease->vendor_specific_len); |
e43a8393 | 1267 | if (r < 0) |
0339cd77 | 1268 | log_debug_errno(r, "Failed to parse vendor specific data %s, ignoring: %m", vendor_specific_hex); |
e43a8393 BG |
1269 | } |
1270 | ||
22805d92 | 1271 | for (i = 0; i <= SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE; i++) { |
e4735228 | 1272 | _cleanup_free_ void *data = NULL; |
a073309f AC |
1273 | size_t len; |
1274 | ||
1275 | if (!options[i]) | |
1276 | continue; | |
1277 | ||
f5fbe71d | 1278 | r = unhexmem(options[i], SIZE_MAX, &data, &len); |
0339cd77 LP |
1279 | if (r < 0) { |
1280 | log_debug_errno(r, "Failed to parse private DHCP option %s, ignoring: %m", options[i]); | |
1281 | continue; | |
1282 | } | |
a073309f | 1283 | |
22805d92 | 1284 | r = dhcp_lease_insert_private_option(lease, SD_DHCP_OPTION_PRIVATE_BASE + i, data, len); |
626be147 | 1285 | if (r < 0) |
a073309f AC |
1286 | return r; |
1287 | } | |
1288 | ||
1cc6c93a | 1289 | *ret = TAKE_PTR(lease); |
fe8db0c5 TG |
1290 | |
1291 | return 0; | |
1292 | } | |
9e64dd72 TG |
1293 | |
1294 | int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) { | |
0339cd77 | 1295 | struct in_addr address, mask; |
df40eee8 | 1296 | int r; |
9e64dd72 TG |
1297 | |
1298 | assert(lease); | |
9e64dd72 | 1299 | |
0339cd77 LP |
1300 | if (lease->address == 0) |
1301 | return -ENODATA; | |
1302 | ||
df40eee8 | 1303 | address.s_addr = lease->address; |
9e64dd72 TG |
1304 | |
1305 | /* fall back to the default subnet masks based on address class */ | |
5a941f5f | 1306 | r = in4_addr_default_subnet_mask(&address, &mask); |
df40eee8 TG |
1307 | if (r < 0) |
1308 | return r; | |
9e64dd72 | 1309 | |
df40eee8 | 1310 | lease->subnet_mask = mask.s_addr; |
0339cd77 | 1311 | lease->have_subnet_mask = true; |
9e64dd72 TG |
1312 | |
1313 | return 0; | |
1314 | } | |
e37f74a6 | 1315 | |
0339cd77 | 1316 | int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const void **client_id, size_t *client_id_len) { |
e37f74a6 DW |
1317 | assert_return(lease, -EINVAL); |
1318 | assert_return(client_id, -EINVAL); | |
1319 | assert_return(client_id_len, -EINVAL); | |
1320 | ||
0339cd77 LP |
1321 | if (!lease->client_id) |
1322 | return -ENODATA; | |
1323 | ||
e37f74a6 DW |
1324 | *client_id = lease->client_id; |
1325 | *client_id_len = lease->client_id_len; | |
0339cd77 | 1326 | |
e37f74a6 DW |
1327 | return 0; |
1328 | } | |
1329 | ||
0339cd77 | 1330 | int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const void *client_id, size_t client_id_len) { |
e37f74a6 | 1331 | assert_return(lease, -EINVAL); |
0339cd77 LP |
1332 | assert_return(client_id || client_id_len <= 0, -EINVAL); |
1333 | ||
1334 | if (client_id_len <= 0) | |
1335 | lease->client_id = mfree(lease->client_id); | |
1336 | else { | |
1337 | void *p; | |
e37f74a6 | 1338 | |
0339cd77 LP |
1339 | p = memdup(client_id, client_id_len); |
1340 | if (!p) | |
1341 | return -ENOMEM; | |
e37f74a6 | 1342 | |
0339cd77 LP |
1343 | free(lease->client_id); |
1344 | lease->client_id = p; | |
e37f74a6 DW |
1345 | lease->client_id_len = client_id_len; |
1346 | } | |
1347 | ||
1348 | return 0; | |
1349 | } | |
8eb9058d | 1350 | |
64d6c229 | 1351 | int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **tz) { |
8eb9058d | 1352 | assert_return(lease, -EINVAL); |
64d6c229 | 1353 | assert_return(tz, -EINVAL); |
8eb9058d LP |
1354 | |
1355 | if (!lease->timezone) | |
0339cd77 | 1356 | return -ENODATA; |
8eb9058d | 1357 | |
64d6c229 | 1358 | *tz = lease->timezone; |
8eb9058d LP |
1359 | return 0; |
1360 | } | |
f8693fc7 BG |
1361 | |
1362 | int sd_dhcp_route_get_destination(sd_dhcp_route *route, struct in_addr *destination) { | |
1363 | assert_return(route, -EINVAL); | |
1364 | assert_return(destination, -EINVAL); | |
1365 | ||
1366 | *destination = route->dst_addr; | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | int sd_dhcp_route_get_destination_prefix_length(sd_dhcp_route *route, uint8_t *length) { | |
1371 | assert_return(route, -EINVAL); | |
1372 | assert_return(length, -EINVAL); | |
1373 | ||
1374 | *length = route->dst_prefixlen; | |
1375 | return 0; | |
1376 | } | |
1377 | ||
1378 | int sd_dhcp_route_get_gateway(sd_dhcp_route *route, struct in_addr *gateway) { | |
1379 | assert_return(route, -EINVAL); | |
1380 | assert_return(gateway, -EINVAL); | |
1381 | ||
1382 | *gateway = route->gw_addr; | |
1383 | return 0; | |
1384 | } | |
cf6f5bb5 TH |
1385 | |
1386 | int sd_dhcp_route_get_option(sd_dhcp_route *route) { | |
1387 | assert_return(route, -EINVAL); | |
1388 | ||
1389 | return route->option; | |
1390 | } |