| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "bus-get-properties.h" |
| 6 |
#include "bus-log-control-api.h" |
| 7 |
#include "bus-object.h" |
| 8 |
#include "bus-polkit.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "dns-domain.h" |
| 11 |
#include "in-addr-util.h" |
| 12 |
#include "log.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "time-util.h" |
| 15 |
#include "timesyncd-bus.h" |
| 16 |
#include "timesyncd-manager.h" |
| 17 |
#include "timesyncd-server.h" |
| 18 |
|
| 19 |
static int property_get_servers( |
| 20 |
sd_bus *bus, |
| 21 |
const char *path, |
| 22 |
const char *interface, |
| 23 |
const char *property, |
| 24 |
sd_bus_message *reply, |
| 25 |
void *userdata, |
| 26 |
sd_bus_error *error) { |
| 27 |
|
| 28 |
ServerName **s = ASSERT_PTR(userdata); |
| 29 |
int r; |
| 30 |
|
| 31 |
assert(bus); |
| 32 |
assert(reply); |
| 33 |
|
| 34 |
r = sd_bus_message_open_container(reply, 'a', "s"); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
LIST_FOREACH(names, p, *s) { |
| 39 |
r = sd_bus_message_append(reply, "s", p->string); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
} |
| 43 |
|
| 44 |
return sd_bus_message_close_container(reply); |
| 45 |
} |
| 46 |
|
| 47 |
static int method_set_runtime_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 48 |
_cleanup_strv_free_ char **msg_names = NULL; |
| 49 |
Manager *m = ASSERT_PTR(userdata); |
| 50 |
int r; |
| 51 |
|
| 52 |
assert(message); |
| 53 |
|
| 54 |
r = sd_bus_message_read_strv(message, &msg_names); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
STRV_FOREACH(name, msg_names) { |
| 59 |
r = dns_name_is_valid_or_address(*name); |
| 60 |
if (r < 0) |
| 61 |
return log_error_errno(r, "Failed to check validity of NTP server name or address '%s': %m", *name); |
| 62 |
if (r == 0) |
| 63 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NTP server name or address, refusing: %s", *name); |
| 64 |
} |
| 65 |
|
| 66 |
r = bus_verify_polkit_async( |
| 67 |
message, |
| 68 |
"org.freedesktop.timesync1.set-runtime-servers", |
| 69 |
/* details= */ NULL, |
| 70 |
&m->polkit_registry, |
| 71 |
error); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
if (r == 0) |
| 75 |
/* Polkit will call us back */ |
| 76 |
return 1; |
| 77 |
|
| 78 |
manager_flush_runtime_servers(m); |
| 79 |
|
| 80 |
STRV_FOREACH(name, msg_names) { |
| 81 |
r = server_name_new(m, NULL, SERVER_RUNTIME, *name); |
| 82 |
if (r < 0) { |
| 83 |
manager_flush_runtime_servers(m); |
| 84 |
|
| 85 |
return log_error_errno(r, "Failed to add runtime server '%s': %m", *name); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
m->exhausted_servers = true; |
| 90 |
manager_set_server_name(m, NULL); |
| 91 |
(void) manager_connect(m); |
| 92 |
|
| 93 |
return sd_bus_reply_method_return(message, NULL); |
| 94 |
} |
| 95 |
|
| 96 |
static int property_get_current_server_name( |
| 97 |
sd_bus *bus, |
| 98 |
const char *path, |
| 99 |
const char *interface, |
| 100 |
const char *property, |
| 101 |
sd_bus_message *reply, |
| 102 |
void *userdata, |
| 103 |
sd_bus_error *error) { |
| 104 |
|
| 105 |
ServerName **s = ASSERT_PTR(userdata); |
| 106 |
|
| 107 |
assert(bus); |
| 108 |
assert(reply); |
| 109 |
|
| 110 |
return sd_bus_message_append(reply, "s", *s ? (*s)->string : NULL); |
| 111 |
} |
| 112 |
|
| 113 |
static int property_get_current_server_address( |
| 114 |
sd_bus *bus, |
| 115 |
const char *path, |
| 116 |
const char *interface, |
| 117 |
const char *property, |
| 118 |
sd_bus_message *reply, |
| 119 |
void *userdata, |
| 120 |
sd_bus_error *error) { |
| 121 |
|
| 122 |
ServerAddress *a; |
| 123 |
int r; |
| 124 |
|
| 125 |
assert(bus); |
| 126 |
assert(reply); |
| 127 |
assert(userdata); |
| 128 |
|
| 129 |
a = *(ServerAddress **) userdata; |
| 130 |
|
| 131 |
if (!a) |
| 132 |
return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0); |
| 133 |
|
| 134 |
assert(IN_SET(a->sockaddr.sa.sa_family, AF_INET, AF_INET6)); |
| 135 |
|
| 136 |
r = sd_bus_message_open_container(reply, 'r', "iay"); |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
r = sd_bus_message_append(reply, "i", a->sockaddr.sa.sa_family); |
| 141 |
if (r < 0) |
| 142 |
return r; |
| 143 |
|
| 144 |
r = sd_bus_message_append_array(reply, 'y', |
| 145 |
a->sockaddr.sa.sa_family == AF_INET ? (void*) &a->sockaddr.in.sin_addr : (void*) &a->sockaddr.in6.sin6_addr, |
| 146 |
FAMILY_ADDRESS_SIZE(a->sockaddr.sa.sa_family)); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
return sd_bus_message_close_container(reply); |
| 151 |
} |
| 152 |
|
| 153 |
static usec_t ntp_ts_short_to_usec(const struct ntp_ts_short *ts) { |
| 154 |
return be16toh(ts->sec) * USEC_PER_SEC + (be16toh(ts->frac) * USEC_PER_SEC) / (usec_t) 0x10000ULL; |
| 155 |
} |
| 156 |
|
| 157 |
static usec_t ntp_ts_to_usec(const struct ntp_ts *ts) { |
| 158 |
return (be32toh(ts->sec) - OFFSET_1900_1970) * USEC_PER_SEC + (be32toh(ts->frac) * USEC_PER_SEC) / (usec_t) 0x100000000ULL; |
| 159 |
} |
| 160 |
|
| 161 |
static int property_get_ntp_message( |
| 162 |
sd_bus *bus, |
| 163 |
const char *path, |
| 164 |
const char *interface, |
| 165 |
const char *property, |
| 166 |
sd_bus_message *reply, |
| 167 |
void *userdata, |
| 168 |
sd_bus_error *error) { |
| 169 |
|
| 170 |
Manager *m = ASSERT_PTR(userdata); |
| 171 |
int r; |
| 172 |
|
| 173 |
assert(reply); |
| 174 |
|
| 175 |
r = sd_bus_message_open_container(reply, 'r', "uuuuittayttttbtt"); |
| 176 |
if (r < 0) |
| 177 |
return r; |
| 178 |
|
| 179 |
r = sd_bus_message_append(reply, "uuuuitt", |
| 180 |
NTP_FIELD_LEAP(m->ntpmsg.field), |
| 181 |
NTP_FIELD_VERSION(m->ntpmsg.field), |
| 182 |
NTP_FIELD_MODE(m->ntpmsg.field), |
| 183 |
m->ntpmsg.stratum, |
| 184 |
m->ntpmsg.precision, |
| 185 |
ntp_ts_short_to_usec(&m->ntpmsg.root_delay), |
| 186 |
ntp_ts_short_to_usec(&m->ntpmsg.root_dispersion)); |
| 187 |
if (r < 0) |
| 188 |
return r; |
| 189 |
|
| 190 |
r = sd_bus_message_append_array(reply, 'y', m->ntpmsg.refid, 4); |
| 191 |
if (r < 0) |
| 192 |
return r; |
| 193 |
|
| 194 |
r = sd_bus_message_append(reply, "ttttbtt", |
| 195 |
timespec_load(&m->origin_time), |
| 196 |
ntp_ts_to_usec(&m->ntpmsg.recv_time), |
| 197 |
ntp_ts_to_usec(&m->ntpmsg.trans_time), |
| 198 |
timespec_load(&m->dest_time), |
| 199 |
m->spike, |
| 200 |
m->packet_count, |
| 201 |
(usec_t) (m->samples_jitter * USEC_PER_SEC)); |
| 202 |
if (r < 0) |
| 203 |
return r; |
| 204 |
|
| 205 |
return sd_bus_message_close_container(reply); |
| 206 |
} |
| 207 |
|
| 208 |
static const sd_bus_vtable manager_vtable[] = { |
| 209 |
SD_BUS_VTABLE_START(0), |
| 210 |
|
| 211 |
SD_BUS_PROPERTY("LinkNTPServers", "as", property_get_servers, offsetof(Manager, link_servers), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 212 |
SD_BUS_PROPERTY("SystemNTPServers", "as", property_get_servers, offsetof(Manager, system_servers), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 213 |
SD_BUS_PROPERTY("RuntimeNTPServers", "as", property_get_servers, offsetof(Manager, runtime_servers), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 214 |
SD_BUS_PROPERTY("FallbackNTPServers", "as", property_get_servers, offsetof(Manager, fallback_servers), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 215 |
SD_BUS_PROPERTY("ServerName", "s", property_get_current_server_name, offsetof(Manager, current_server_name), 0), |
| 216 |
SD_BUS_PROPERTY("ServerAddress", "(iay)", property_get_current_server_address, offsetof(Manager, current_server_address), 0), |
| 217 |
SD_BUS_PROPERTY("RootDistanceMaxUSec", "t", bus_property_get_usec, offsetof(Manager, root_distance_max_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 218 |
SD_BUS_PROPERTY("PollIntervalMinUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_min_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 219 |
SD_BUS_PROPERTY("PollIntervalMaxUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_max_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 220 |
SD_BUS_PROPERTY("PollIntervalUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_usec), 0), |
| 221 |
SD_BUS_PROPERTY("NTPMessage", "(uuuuittayttttbtt)", property_get_ntp_message, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 222 |
SD_BUS_PROPERTY("Frequency", "x", NULL, offsetof(Manager, drift_freq), 0), |
| 223 |
|
| 224 |
SD_BUS_METHOD_WITH_ARGS("SetRuntimeNTPServers", |
| 225 |
SD_BUS_ARGS("as", runtime_servers), |
| 226 |
SD_BUS_NO_RESULT, |
| 227 |
method_set_runtime_servers, |
| 228 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 229 |
|
| 230 |
SD_BUS_VTABLE_END |
| 231 |
}; |
| 232 |
|
| 233 |
int manager_connect_bus(Manager *m) { |
| 234 |
int r; |
| 235 |
|
| 236 |
assert(m); |
| 237 |
|
| 238 |
if (m->bus) |
| 239 |
return 0; |
| 240 |
|
| 241 |
r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-timesync"); |
| 242 |
if (r < 0) |
| 243 |
return log_error_errno(r, "Failed to connect to bus: %m"); |
| 244 |
|
| 245 |
r = bus_add_implementation(m->bus, &manager_object, m); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
|
| 249 |
r = bus_log_control_api_register(m->bus); |
| 250 |
if (r < 0) |
| 251 |
return r; |
| 252 |
|
| 253 |
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.timesync1", 0, NULL, NULL); |
| 254 |
if (r < 0) |
| 255 |
return log_error_errno(r, "Failed to request name: %m"); |
| 256 |
|
| 257 |
r = sd_bus_attach_event(m->bus, m->event, 0); |
| 258 |
if (r < 0) |
| 259 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
const BusObjectImplementation manager_object = { |
| 265 |
"/org/freedesktop/timesync1", |
| 266 |
"org.freedesktop.timesync1.Manager", |
| 267 |
.vtables = BUS_VTABLES(manager_vtable), |
| 268 |
}; |