41483a4ac162d7b46534c9d4fb406dd64f5a61c8
[systembsd.git] / src / interfaces / timedated / timedated.c
1 /*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <unistd.h>
18 #include <limits.h>
19 #include <signal.h>
20
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <string.h>
25
26 #include <glib/gprintf.h>
27 #include <glib-unix.h>
28 #include <glib/gstdio.h>
29 #include <polkit/polkit.h>
30
31 #include "timedated-gen.h"
32 #include "timedated.h"
33
34 #include "../../util.h"
35
36 GPtrArray *timedated_freeable;
37 Timedate1 *timedated_interf;
38
39 GMainLoop *timedated_loop;
40
41 guint bus_descriptor;
42 gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
43
44 const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */
45 const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */
46
47 struct timezone_checksum_pair {
48
49 gchar *path;
50 gchar *sum;
51 gboolean posix;
52 gboolean right;
53 };
54
55 static struct timezone_checksum_pair tz_table[5000];
56
57 /* --- begin method/property/dbus signal code --- */
58
59 static gboolean
60 on_handle_set_time(Timedate1 *td1_passed_interf,
61 GDBusMethodInvocation *invoc,
62 const gchar *greet,
63 gpointer data) {
64
65 GVariant *params;
66 gint64 proposed_time, cur_time;
67 const gchar *bus_name;
68 gboolean policykit_auth;
69 check_auth_result is_authed;
70 gboolean relative; /* relative if passed time_t is meant to be added to current time */
71 struct timespec *new_time;
72
73 params = g_dbus_method_invocation_get_parameters(invoc);
74 g_variant_get(params, "(xbb)", &proposed_time, &relative, &policykit_auth);
75 bus_name = g_dbus_method_invocation_get_sender(invoc);
76
77 is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-time", policykit_auth);
78
79 switch(is_authed) {
80
81 case AUTHORIZED_NATIVELY:
82 case AUTHORIZED_BY_PROMPT:
83 break;
84
85 case UNAUTHORIZED_NATIVELY:
86 case UNAUTHORIZED_FAILED_PROMPT:
87 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set system time.");
88 return FALSE;
89
90 case ERROR_BADBUS:
91 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid.");
92 return FALSE;
93
94 case ERROR_BADACTION:
95 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid.");
96 return FALSE;
97
98 case ERROR_GENERIC:
99 default:
100 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
101 return FALSE;
102 }
103
104 if(!proposed_time) {
105
106 timedate1_complete_set_time(td1_passed_interf, invoc);
107 return TRUE;
108
109 } else if(relative) {
110
111 new_time = (struct timespec *) g_malloc0(sizeof(struct timespec));
112 cur_time = g_get_real_time();
113
114 if(proposed_time < 0 && cur_time + proposed_time > proposed_time) {
115
116 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
117 return FALSE;
118
119 } else if(cur_time + proposed_time < proposed_time) {
120
121 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
122 return FALSE;
123 }
124
125 new_time = (struct timespec *) g_malloc0(sizeof(struct timespec));
126 new_time->tv_sec = proposed_time;
127 new_time->tv_nsec = 0;
128 g_ptr_array_add(timedated_freeable, new_time);
129
130 if(!clock_settime(CLOCK_REALTIME, new_time)) {
131
132 timedate1_complete_set_time(td1_passed_interf, invoc);
133 return TRUE;
134
135 } else {
136
137 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
138 return FALSE;
139 }
140
141 } else if(proposed_time >= 0) {
142
143 new_time = (struct timespec *) g_malloc0(sizeof(struct timespec));
144 new_time->tv_sec = proposed_time;
145 new_time->tv_nsec = 0;
146 g_ptr_array_add(timedated_freeable, new_time);
147
148 if(!clock_settime(CLOCK_REALTIME, new_time)) {
149
150 timedate1_complete_set_time(td1_passed_interf, invoc);
151 return TRUE;
152
153 } else {
154
155 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
156 return FALSE;
157 }
158
159 } else {
160
161 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
162 return FALSE;
163 }
164 }
165
166 static gboolean
167 on_handle_set_timezone(Timedate1 *td1_passed_interf,
168 GDBusMethodInvocation *invoc,
169 const gchar *greet,
170 gpointer data) {
171 return FALSE;
172 }
173
174 static gboolean
175 on_handle_set_local_rtc(Timedate1 *td1_passed_interf,
176 GDBusMethodInvocation *invoc,
177 const gchar *greet,
178 gpointer data) {
179 return FALSE;
180 }
181
182 static gboolean
183 on_handle_set_ntp(Timedate1 *td1_passed_interf,
184 GDBusMethodInvocation *invoc,
185 const gchar *greet,
186 gpointer data) {
187 return FALSE;
188 }
189
190 const gchar *
191 our_get_timezone() {
192
193 GStatBuf *stat_zoneinfo;
194 gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match;
195 gint argcp;
196 GError *err;
197 struct timezone_checksum_pair tmp;
198
199 find_cmd = (gchar *) g_malloc0(2048);
200 stat_zoneinfo = (GStatBuf *) g_malloc0(8192);
201 err = (GError *) g_malloc0(2048);
202
203 if(g_stat(OS_LOCALTIME, stat_zoneinfo)) {
204
205 g_printf("could not read from %s! please symlink or copy a timezone file from %s to %s!\n", OS_LOCALTIME, OS_TIMEZONE_PATH, OS_LOCALTIME);
206 ret = NULL;
207
208 } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) {
209
210 readlink_path = g_file_read_link(OS_LOCALTIME, &err);
211
212 gchar *split[2] = { readlink_path, "" };
213 tmp = parse_timezone_path(split);
214
215 ret = tmp.path;
216
217 if(readlink_path)
218 g_free(readlink_path);
219
220 } else {
221
222 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH);
223 hash_to_match = get_file_sha256(OS_LOCALTIME);
224
225 ret = lookup_hash(hash_to_match);
226
227 if(hash_to_match)
228 g_free(hash_to_match);
229 }
230
231 return ret;
232 }
233
234 gboolean
235 our_get_local_rtc() {
236
237 gboolean ret = FALSE;
238
239 return ret;
240 }
241
242 gboolean
243 our_get_can_ntp() {
244
245 const gboolean ret = FALSE;
246
247 return ret;
248 }
249
250 gboolean
251 our_get_ntp() {
252
253 const gboolean ret = FALSE;
254
255 return ret;
256 }
257
258 gboolean
259 our_get_ntpsynchronized() {
260
261 const gboolean ret = FALSE;
262
263 return ret;
264 }
265
266 guint64
267 our_get_time_usec() {
268
269 guint64 ret = 0;
270
271 return ret;
272 }
273
274 guint64
275 our_get_rtc_time_usec() {
276
277 guint64 ret = 0;
278
279 return ret;
280 }
281
282 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
283
284 static void timedated_on_bus_acquired(GDBusConnection *conn,
285 const gchar *name,
286 gpointer user_data) {
287
288 g_printf("got bus/name, exporting %s's interface...\n", name);
289
290 timedated_interf = timedate1_skeleton_new();
291
292 /* attach function pointers to generated struct's method handlers */
293 g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL);
294 g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL);
295 g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL);
296 g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL);
297
298 /* set our properties before export */
299 timedate1_set_timezone(timedated_interf, our_get_timezone());
300 timedate1_set_local_rtc(timedated_interf, our_get_local_rtc());
301 timedate1_set_can_ntp(timedated_interf, our_get_can_ntp());
302 timedate1_set_ntp(timedated_interf, our_get_ntp());
303 timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized());
304 timedate1_set_time_usec(timedated_interf, our_get_time_usec());
305 timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec());
306
307 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
308 conn,
309 "/org/freedesktop/timedate1",
310 NULL)) {
311
312 g_printf("failed to export %s's interface!\n", name);
313 timedated_mem_clean();
314
315 } else {
316
317 dbus_interface_exported = TRUE;
318 g_printf("exported %s's interface on the system bus...\n", name);
319 }
320 }
321
322 static void timedated_on_name_acquired(GDBusConnection *conn,
323 const gchar *name,
324 gpointer user_data) {
325
326 g_printf("success!\n");
327 }
328
329 static void timedated_on_name_lost(GDBusConnection *conn,
330 const gchar *name,
331 gpointer user_data) {
332
333 if(!conn) {
334
335 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name);
336 timedated_mem_clean();
337 }
338
339 g_print("lost name %s, exiting...\n", name);
340
341 timedated_mem_clean();
342 }
343
344 /* --- end bus/name handlers, begin misc unix functions --- */
345
346 /* safe call to clean and then exit
347 * this stops our GMainLoop safely before letting main() return */
348 void timedated_mem_clean() {
349
350 g_printf("exiting...\n");
351
352 if(dbus_interface_exported)
353 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
354
355 if(g_main_loop_is_running(timedated_loop))
356 g_main_loop_quit(timedated_loop);
357
358 }
359
360 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
361 gboolean unix_sig_terminate_handler(gpointer data) {
362
363 g_printf("caught SIGINT/HUP/TERM, exiting\n");
364
365 timedated_mem_clean();
366 return G_SOURCE_REMOVE;
367 }
368
369 void set_signal_handlers() {
370
371 /* we don't care about its descriptor, we never need to unregister these */
372 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
373 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
374 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
375 }
376
377 int main() {
378
379 set_signal_handlers();
380
381 if(!build_lookup_table())
382 return 1;
383
384 timedated_loop = g_main_loop_new(NULL, TRUE);
385 timedated_freeable = g_ptr_array_new();
386
387 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
388 "org.freedesktop.timedate1",
389 G_BUS_NAME_OWNER_FLAGS_NONE,
390 timedated_on_bus_acquired,
391 timedated_on_name_acquired,
392 timedated_on_name_lost,
393 NULL,
394 NULL);
395
396 g_main_loop_run(timedated_loop);
397 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
398 g_main_loop_unref(timedated_loop);
399
400 /* guaranteed unownable */
401 g_bus_unown_name(bus_descriptor);
402
403 /* at this point no operations can occur with our data, it is safe to free it + its container */
404 g_ptr_array_free(timedated_freeable, TRUE);
405
406 return 0;
407 }
408
409 static struct timezone_checksum_pair parse_timezone_path(gchar **pair) {
410
411 gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path;
412 GRegex *prefix, *posix, *right;
413 GError *err = NULL;
414 struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE };
415
416 if(!pair[0])
417 return ret;
418
419 prefix_pattern = (gchar *) g_malloc0(4096);
420 right_prefix_pattern = (gchar *) g_malloc0(4096);
421 posix_prefix_pattern = (gchar *) g_malloc0(4096);
422
423 g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH);
424 g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH);
425 g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH);
426
427 prefix = g_regex_new(prefix_pattern, 0, 0, &err);
428 posix = g_regex_new(posix_prefix_pattern, 0, 0, &err);
429 right = g_regex_new(right_prefix_pattern, 0, 0, &err);
430
431 if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
432
433 ret.posix = TRUE;
434 lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
435
436 } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
437
438 ret.right = TRUE;
439 lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
440
441 } else
442 lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
443
444 ret.path = lean_path;
445
446 ret.sum = g_malloc0(256);
447 g_strlcpy(ret.sum, pair[1], 66);
448
449 g_regex_unref(prefix);
450 g_regex_unref(right);
451 g_regex_unref(posix);
452
453 return ret;
454 }
455
456 /* TODO need to deconstruct tz_table on exit */
457 static gboolean build_lookup_table() {
458
459 gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf;
460 GError *err;
461 gboolean ret;
462 gint i;
463
464 i = 0;
465 err = NULL;
466 ret = TRUE;
467
468 find_cmd = (gchar *) g_malloc0(4096);
469 find_output = (gchar *) g_malloc0(1000000);
470
471 g_sprintf(find_cmd, "/bin/sh -c \"find %s -type f -exec cksum -a sha256 {} \\; | sed -E 's/SHA256 \\(//g' | sed -E 's/\\) = /=/g'\"", OS_TIMEZONE_PATH);
472
473 if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) {
474
475 g_printf("error running `%s`\n", find_cmd);
476 ret = FALSE;
477 }
478
479 map_pairs = g_strsplit(find_output, "\n", INT_MAX);
480
481 while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) {
482
483 tz_table[i] = parse_timezone_path(entry_buf);
484
485 g_strfreev(entry_buf);
486 i++;
487 }
488
489 g_free(find_output);
490 g_free(find_cmd);
491 g_free(map_pairs);
492
493 return ret;
494 }
495
496 static gchar *lookup_hash(gchar *hash) {
497
498 gint i = 0;
499
500 while(tz_table[i].sum)
501 if(!g_strcmp0(tz_table[i].sum, hash))
502 return tz_table[i].path;
503 else
504 i++;
505
506 return NULL;
507 }