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