| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "dns-domain.h" |
| 5 |
#include "extract-word.h" |
| 6 |
#include "networkd-link.h" |
| 7 |
#include "networkd-network.h" |
| 8 |
#include "networkd-ntp.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "strv.h" |
| 11 |
|
| 12 |
/* Let's assume that anything above this number is a user misconfiguration. */ |
| 13 |
#define MAX_NTP_SERVERS 128U |
| 14 |
|
| 15 |
bool link_get_use_ntp(Link *link, NetworkConfigSource proto) { |
| 16 |
int n, c; |
| 17 |
|
| 18 |
assert(link); |
| 19 |
|
| 20 |
if (!link->network) |
| 21 |
return false; |
| 22 |
|
| 23 |
switch (proto) { |
| 24 |
case NETWORK_CONFIG_SOURCE_DHCP4: |
| 25 |
n = link->network->dhcp_use_ntp; |
| 26 |
c = link->network->compat_dhcp_use_ntp; |
| 27 |
break; |
| 28 |
case NETWORK_CONFIG_SOURCE_DHCP6: |
| 29 |
n = link->network->dhcp6_use_ntp; |
| 30 |
c = link->network->compat_dhcp_use_ntp; |
| 31 |
break; |
| 32 |
default: |
| 33 |
assert_not_reached(); |
| 34 |
} |
| 35 |
|
| 36 |
/* If per-network and per-protocol setting is specified, use it. */ |
| 37 |
if (n >= 0) |
| 38 |
return n; |
| 39 |
|
| 40 |
/* If compat setting is specified, use it. */ |
| 41 |
if (c >= 0) |
| 42 |
return c; |
| 43 |
|
| 44 |
/* Otherwise, defaults to yes. */ |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
int config_parse_ntp( |
| 49 |
const char *unit, |
| 50 |
const char *filename, |
| 51 |
unsigned line, |
| 52 |
const char *section, |
| 53 |
unsigned section_line, |
| 54 |
const char *lvalue, |
| 55 |
int ltype, |
| 56 |
const char *rvalue, |
| 57 |
void *data, |
| 58 |
void *userdata) { |
| 59 |
|
| 60 |
char ***l = ASSERT_PTR(data); |
| 61 |
int r; |
| 62 |
|
| 63 |
assert(filename); |
| 64 |
assert(lvalue); |
| 65 |
assert(rvalue); |
| 66 |
|
| 67 |
if (isempty(rvalue)) { |
| 68 |
*l = strv_free(*l); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
for (const char *p = rvalue;;) { |
| 73 |
_cleanup_free_ char *w = NULL; |
| 74 |
|
| 75 |
r = extract_first_word(&p, &w, NULL, 0); |
| 76 |
if (r == -ENOMEM) |
| 77 |
return log_oom(); |
| 78 |
if (r < 0) { |
| 79 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 80 |
"Failed to extract NTP server name, ignoring: %s", rvalue); |
| 81 |
return 0; |
| 82 |
} |
| 83 |
if (r == 0) |
| 84 |
return 0; |
| 85 |
|
| 86 |
r = dns_name_is_valid_or_address(w); |
| 87 |
if (r <= 0) { |
| 88 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 89 |
"%s is not a valid domain name or IP address, ignoring.", w); |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
if (strv_length(*l) > MAX_NTP_SERVERS) { |
| 94 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 95 |
"More than %u NTP servers specified, ignoring \"%s\" and any subsequent entries.", |
| 96 |
MAX_NTP_SERVERS, w); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
r = strv_consume(l, TAKE_PTR(w)); |
| 101 |
if (r < 0) |
| 102 |
return log_oom(); |
| 103 |
} |
| 104 |
} |