disable TZ checksumming, fix setTime
[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 cur_time = g_get_real_time();
112
113 if(proposed_time < 0 && cur_time + proposed_time > cur_time) {
114
115 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
116 return FALSE;
117
118 } else if(proposed_time > 0 && cur_time + proposed_time < cur_time) {
119
120 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
121 return FALSE;
122 }
123
124 new_time = mktimespec(proposed_time);
125
126 if(!clock_settime(CLOCK_REALTIME, new_time)) {
127
128 timedate1_complete_set_time(td1_passed_interf, invoc);
129 return TRUE;
130
131 } else {
132
133 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
134 return FALSE;
135 }
136
137 } else if(proposed_time > 0) {
138
139
140 new_time = mktimespec(proposed_time);
141
142 if(!clock_settime(CLOCK_REALTIME, new_time)) {
143
144 timedate1_complete_set_time(td1_passed_interf, invoc);
145 return TRUE;
146
147 } else {
148
149 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
150 return FALSE;
151 }
152
153 } else {
154
155 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
156 return FALSE;
157 }
158 }
159
160 static gboolean
161 on_handle_set_timezone(Timedate1 *td1_passed_interf,
162 GDBusMethodInvocation *invoc,
163 const gchar *greet,
164 gpointer data) {
165 return FALSE;
166 }
167
168 static gboolean
169 on_handle_set_local_rtc(Timedate1 *td1_passed_interf,
170 GDBusMethodInvocation *invoc,
171 const gchar *greet,
172 gpointer data) {
173 return FALSE;
174 }
175
176 static gboolean
177 on_handle_set_ntp(Timedate1 *td1_passed_interf,
178 GDBusMethodInvocation *invoc,
179 const gchar *greet,
180 gpointer data) {
181 return FALSE;
182 }
183
184 const gchar *
185 our_get_timezone() {
186
187 GStatBuf *stat_zoneinfo;
188 gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match;
189 gint argcp;
190 GError *err;
191 struct timezone_checksum_pair tmp;
192
193 find_cmd = (gchar *) g_malloc0(2048);
194 stat_zoneinfo = (GStatBuf *) g_malloc0(8192);
195 err = (GError *) g_malloc0(2048);
196
197 if(g_stat(OS_LOCALTIME, stat_zoneinfo)) {
198
199 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);
200 ret = NULL;
201
202 } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) {
203
204 readlink_path = g_file_read_link(OS_LOCALTIME, &err);
205
206 gchar *split[2] = { readlink_path, "" };
207 tmp = parse_timezone_path(split);
208
209 ret = tmp.path;
210
211 if(readlink_path)
212 g_free(readlink_path);
213
214 } else {
215
216 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH);
217 hash_to_match = get_file_sha256(OS_LOCALTIME);
218
219 /* ret = lookup_hash(hash_to_match); */
220 return FALSE; /* TODO fix me for real */
221
222 if(hash_to_match)
223 g_free(hash_to_match);
224 }
225
226 return ret;
227 }
228
229 gboolean
230 our_get_local_rtc() {
231
232 gboolean ret = FALSE;
233
234 return ret;
235 }
236
237 gboolean
238 our_get_can_ntp() {
239
240 const gboolean ret = FALSE;
241
242 return ret;
243 }
244
245 gboolean
246 our_get_ntp() {
247
248 const gboolean ret = FALSE;
249
250 return ret;
251 }
252
253 gboolean
254 our_get_ntpsynchronized() {
255
256 const gboolean ret = FALSE;
257
258 return ret;
259 }
260
261 guint64
262 our_get_time_usec() {
263
264 guint64 ret = 0;
265
266 return ret;
267 }
268
269 guint64
270 our_get_rtc_time_usec() {
271
272 guint64 ret = 0;
273
274 return ret;
275 }
276
277 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
278
279 static void timedated_on_bus_acquired(GDBusConnection *conn,
280 const gchar *name,
281 gpointer user_data) {
282
283 g_printf("got bus/name, exporting %s's interface...\n", name);
284
285 timedated_interf = timedate1_skeleton_new();
286
287 /* attach function pointers to generated struct's method handlers */
288 g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL);
289 g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL);
290 g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL);
291 g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL);
292
293 /* set our properties before export */
294 timedate1_set_timezone(timedated_interf, our_get_timezone());
295 timedate1_set_local_rtc(timedated_interf, our_get_local_rtc());
296 timedate1_set_can_ntp(timedated_interf, our_get_can_ntp());
297 timedate1_set_ntp(timedated_interf, our_get_ntp());
298 timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized());
299 timedate1_set_time_usec(timedated_interf, our_get_time_usec());
300 timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec());
301
302 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
303 conn,
304 "/org/freedesktop/timedate1",
305 NULL)) {
306
307 g_printf("failed to export %s's interface!\n", name);
308 timedated_mem_clean();
309
310 } else {
311
312 dbus_interface_exported = TRUE;
313 g_printf("exported %s's interface on the system bus...\n", name);
314 }
315 }
316
317 static void timedated_on_name_acquired(GDBusConnection *conn,
318 const gchar *name,
319 gpointer user_data) {
320
321 g_printf("success!\n");
322 }
323
324 static void timedated_on_name_lost(GDBusConnection *conn,
325 const gchar *name,
326 gpointer user_data) {
327
328 if(!conn) {
329
330 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);
331 timedated_mem_clean();
332 }
333
334 g_print("lost name %s, exiting...\n", name);
335
336 timedated_mem_clean();
337 }
338
339 /* --- end bus/name handlers, begin misc unix functions --- */
340
341 /* safe call to clean and then exit
342 * this stops our GMainLoop safely before letting main() return */
343 void timedated_mem_clean() {
344
345 g_printf("exiting...\n");
346
347 if(dbus_interface_exported)
348 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
349
350 if(g_main_loop_is_running(timedated_loop))
351 g_main_loop_quit(timedated_loop);
352
353 }
354
355 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
356 gboolean unix_sig_terminate_handler(gpointer data) {
357
358 g_printf("caught SIGINT/HUP/TERM, exiting\n");
359
360 timedated_mem_clean();
361 return G_SOURCE_REMOVE;
362 }
363
364 void set_signal_handlers() {
365
366 /* we don't care about its descriptor, we never need to unregister these */
367 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
368 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
369 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
370 }
371
372 int main() {
373
374 set_signal_handlers();
375
376 /*if(!build_lookup_table())
377 return 1; */
378
379 timedated_loop = g_main_loop_new(NULL, TRUE);
380 timedated_freeable = g_ptr_array_new();
381
382 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
383 "org.freedesktop.timedate1",
384 G_BUS_NAME_OWNER_FLAGS_NONE,
385 timedated_on_bus_acquired,
386 timedated_on_name_acquired,
387 timedated_on_name_lost,
388 NULL,
389 NULL);
390
391 g_main_loop_run(timedated_loop);
392 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
393 g_main_loop_unref(timedated_loop);
394
395 /* guaranteed unownable */
396 g_bus_unown_name(bus_descriptor);
397
398 /* at this point no operations can occur with our data, it is safe to free it + its container */
399 g_ptr_array_free(timedated_freeable, TRUE);
400
401 return 0;
402 }
403
404 static struct timezone_checksum_pair parse_timezone_path(gchar **pair) {
405
406 gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path;
407 GRegex *prefix, *posix, *right;
408 GError *err = NULL;
409 struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE };
410
411 if(!pair[0])
412 return ret;
413
414 prefix_pattern = (gchar *) g_malloc0(4096);
415 right_prefix_pattern = (gchar *) g_malloc0(4096);
416 posix_prefix_pattern = (gchar *) g_malloc0(4096);
417
418 g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH);
419 g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH);
420 g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH);
421
422 prefix = g_regex_new(prefix_pattern, 0, 0, &err);
423 posix = g_regex_new(posix_prefix_pattern, 0, 0, &err);
424 right = g_regex_new(right_prefix_pattern, 0, 0, &err);
425
426 if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
427
428 ret.posix = TRUE;
429 lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
430
431 } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
432
433 ret.right = TRUE;
434 lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
435
436 } else
437 lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
438
439 ret.path = lean_path;
440
441 ret.sum = g_malloc0(256);
442 g_strlcpy(ret.sum, pair[1], 66);
443
444 g_regex_unref(prefix);
445 g_regex_unref(right);
446 g_regex_unref(posix);
447
448 return ret;
449 }
450
451 /* TODO need to deconstruct tz_table on exit
452 static gboolean build_lookup_table() {
453
454 gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf;
455 GError *err;
456 gboolean ret;
457 gint i;
458
459 i = 0;
460 err = NULL;
461 ret = TRUE;
462
463 find_cmd = (gchar *) g_malloc0(4096);
464 find_output = (gchar *) g_malloc0(1000000);
465
466 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);
467
468 if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) {
469
470 g_printf("error running `%s`\n", find_cmd);
471 ret = FALSE;
472 }
473
474 map_pairs = g_strsplit(find_output, "\n", INT_MAX);
475
476 while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) {
477
478 tz_table[i] = parse_timezone_path(entry_buf);
479
480 g_strfreev(entry_buf);
481 i++;
482 }
483
484 g_free(find_output);
485 g_free(find_cmd);
486 g_free(map_pairs);
487
488 return ret;
489 }
490
491 static gchar *lookup_hash(gchar *hash) {
492
493 gint i = 0;
494
495 while(tz_table[i].sum)
496 if(!g_strcmp0(tz_table[i].sum, hash))
497 return tz_table[i].path;
498 else
499 i++;
500
501 return NULL;
502 }*/
503
504 /* takes number of microseconds since epoch and returns a
505 * ptr to a timespec suitable to be passed to clock_settime(3)
506 */
507 static struct timespec* mktimespec(gint64 us) {
508
509 long nanoseconds;
510 time_t seconds;
511
512 gint64 div_buf_remainder, div_buf_s, div_buf_ns;
513 struct timespec *ret;
514
515 div_buf_s = (us / 1000000); /* us / 10^6 = s */
516 div_buf_remainder = (us % 1000000); /* fraction of second lost from prev. line */
517 div_buf_ns = div_buf_remainder * 1000; /* us * 10^3 = ns */
518
519 seconds = (time_t) div_buf_s; /* porting note: most systems use 32 bit time, adjust accordingly */
520 nanoseconds = (long) div_buf_ns;
521
522 ret = (struct timespec *) calloc(1, sizeof(struct timespec));
523
524 ret->tv_sec = seconds;
525 ret->tv_nsec = nanoseconds;
526
527 g_ptr_array_add(timedated_freeable, ret);
528
529 return ret;
530 }