3b82e3c1 |
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 | |
b6ad18ad |
17 | #include <unistd.h> |
18 | #include <limits.h> |
c12c41f4 |
19 | #include <signal.h> |
b6ad18ad |
20 | |
a3d2e50f |
21 | #include <sys/types.h> |
a8f71719 |
22 | #include <sys/time.h> |
a3d2e50f |
23 | #include <time.h> |
b6ad18ad |
24 | #include <string.h> |
25 | |
26 | #include <glib/gprintf.h> |
8caf1f61 |
27 | #include <glib-unix.h> |
1066bd36 |
28 | #include <glib/gstdio.h> |
904d744d |
29 | #include <polkit/polkit.h> |
b6ad18ad |
30 | |
1e8c7c88 |
31 | #include "timedated-gen.h" |
a33bcdfa |
32 | #include "timedated.h" |
1e8c7c88 |
33 | |
ed4cf3c8 |
34 | #include "../../util.h" |
483e90b7 |
35 | |
b6ad18ad |
36 | GPtrArray *timedated_freeable; |
37 | Timedate1 *timedated_interf; |
38 | |
c12c41f4 |
39 | GMainLoop *timedated_loop; |
40 | |
41 | guint bus_descriptor; |
42 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
43 | |
03f12ac7 |
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]; |
1066bd36 |
56 | |
1ce41045 |
57 | /* --- begin method/property/dbus signal code --- */ |
b6ad18ad |
58 | |
28b86015 |
59 | static gboolean |
a8f71719 |
60 | on_handle_set_time(Timedate1 *td1_passed_interf, |
28b86015 |
61 | GDBusMethodInvocation *invoc, |
62 | const gchar *greet, |
63 | gpointer data) { |
a8f71719 |
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(relative) { |
105 | |
106 | new_time = (struct timespec *) g_malloc0(sizeof(struct timespec)); |
107 | |
108 | cur_time = g_get_real_time(); |
109 | /* LEFT OFF HERE 9/13 */ return FALSE; |
110 | } else if(proposed_time >= 0) { |
111 | |
112 | new_time = (struct timespec *) g_malloc0(sizeof(struct timespec)); |
113 | new_time->tv_sec = proposed_time; |
114 | new_time->tv_nsec = 0; |
115 | g_ptr_array_add(timedated_freeable, new_time); |
116 | |
117 | if(!clock_settime(CLOCK_REALTIME, new_time)) { |
118 | |
119 | timedate1_complete_set_time(td1_passed_interf, invoc); |
120 | return TRUE; |
121 | |
122 | } else { |
123 | |
124 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
125 | return FALSE; |
126 | } |
127 | } else { |
128 | |
129 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.EDOM", "Provided time results in a negative clock value."); |
130 | return FALSE; |
131 | } |
28b86015 |
132 | } |
b6ad18ad |
133 | |
28b86015 |
134 | static gboolean |
a8f71719 |
135 | on_handle_set_timezone(Timedate1 *td1_passed_interf, |
28b86015 |
136 | GDBusMethodInvocation *invoc, |
137 | const gchar *greet, |
138 | gpointer data) { |
139 | return FALSE; |
140 | } |
141 | |
142 | static gboolean |
a8f71719 |
143 | on_handle_set_local_rtc(Timedate1 *td1_passed_interf, |
28b86015 |
144 | GDBusMethodInvocation *invoc, |
145 | const gchar *greet, |
146 | gpointer data) { |
147 | return FALSE; |
148 | } |
149 | |
150 | static gboolean |
a8f71719 |
151 | on_handle_set_ntp(Timedate1 *td1_passed_interf, |
28b86015 |
152 | GDBusMethodInvocation *invoc, |
153 | const gchar *greet, |
154 | gpointer data) { |
155 | return FALSE; |
156 | } |
157 | |
158 | const gchar * |
159 | our_get_timezone() { |
160 | |
1066bd36 |
161 | GStatBuf *stat_zoneinfo; |
03f12ac7 |
162 | gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match; |
163 | gint argcp; |
164 | GError *err; |
165 | struct timezone_checksum_pair tmp; |
1066bd36 |
166 | |
03f12ac7 |
167 | find_cmd = (gchar *) g_malloc0(2048); |
168 | stat_zoneinfo = (GStatBuf *) g_malloc0(8192); |
169 | err = (GError *) g_malloc0(2048); |
1066bd36 |
170 | |
171 | if(g_stat(OS_LOCALTIME, stat_zoneinfo)) { |
172 | |
173 | 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); |
174 | ret = NULL; |
175 | |
176 | } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) { |
177 | |
178 | readlink_path = g_file_read_link(OS_LOCALTIME, &err); |
03f12ac7 |
179 | |
180 | gchar *split[2] = { readlink_path, "" }; |
181 | tmp = parse_timezone_path(split); |
182 | |
183 | ret = tmp.path; |
1066bd36 |
184 | |
185 | if(readlink_path) |
186 | g_free(readlink_path); |
187 | |
188 | } else { |
189 | |
190 | g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH); |
03f12ac7 |
191 | hash_to_match = get_file_sha256(OS_LOCALTIME); |
192 | |
193 | ret = lookup_hash(hash_to_match); |
194 | |
195 | if(hash_to_match) |
196 | g_free(hash_to_match); |
1066bd36 |
197 | } |
198 | |
199 | return ret; |
28b86015 |
200 | } |
201 | |
202 | gboolean |
203 | our_get_local_rtc() { |
204 | |
205 | gboolean ret = FALSE; |
206 | |
207 | return ret; |
208 | } |
209 | |
210 | gboolean |
211 | our_get_can_ntp() { |
212 | |
213 | const gboolean ret = FALSE; |
214 | |
215 | return ret; |
216 | } |
217 | |
218 | gboolean |
219 | our_get_ntp() { |
220 | |
221 | const gboolean ret = FALSE; |
222 | |
223 | return ret; |
224 | } |
b6ad18ad |
225 | |
28b86015 |
226 | gboolean |
227 | our_get_ntpsynchronized() { |
228 | |
229 | const gboolean ret = FALSE; |
b6ad18ad |
230 | |
28b86015 |
231 | return ret; |
232 | } |
b6ad18ad |
233 | |
28b86015 |
234 | guint64 |
235 | our_get_time_usec() { |
b6ad18ad |
236 | |
28b86015 |
237 | guint64 ret = 0; |
b6ad18ad |
238 | |
28b86015 |
239 | return ret; |
240 | } |
241 | |
242 | guint64 |
243 | our_get_rtc_time_usec() { |
244 | |
245 | guint64 ret = 0; |
246 | |
247 | return ret; |
248 | } |
b6ad18ad |
249 | |
1ce41045 |
250 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
b6ad18ad |
251 | |
252 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
253 | const gchar *name, |
254 | gpointer user_data) { |
255 | |
254ceec0 |
256 | g_printf("got bus/name, exporting %s's interface...\n", name); |
b6ad18ad |
257 | |
258 | timedated_interf = timedate1_skeleton_new(); |
259 | |
28b86015 |
260 | /* attach function pointers to generated struct's method handlers */ |
261 | g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL); |
262 | g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL); |
263 | g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL); |
264 | g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL); |
03f12ac7 |
265 | |
28b86015 |
266 | /* set our properties before export */ |
267 | timedate1_set_timezone(timedated_interf, our_get_timezone()); |
268 | timedate1_set_local_rtc(timedated_interf, our_get_local_rtc()); |
269 | timedate1_set_can_ntp(timedated_interf, our_get_can_ntp()); |
270 | timedate1_set_ntp(timedated_interf, our_get_ntp()); |
271 | timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized()); |
272 | timedate1_set_time_usec(timedated_interf, our_get_time_usec()); |
273 | timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec()); |
274 | |
b6ad18ad |
275 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
276 | conn, |
277 | "/org/freedesktop/timedate1", |
278 | NULL)) { |
279 | |
509599f0 |
280 | g_printf("failed to export %s's interface!\n", name); |
281 | timedated_mem_clean(); |
282 | |
283 | } else { |
284 | |
285 | dbus_interface_exported = TRUE; |
286 | g_printf("exported %s's interface on the system bus...\n", name); |
b6ad18ad |
287 | } |
509599f0 |
288 | } |
289 | |
290 | static void timedated_on_name_acquired(GDBusConnection *conn, |
90f54407 |
291 | const gchar *name, |
509599f0 |
292 | gpointer user_data) { |
b6ad18ad |
293 | |
509599f0 |
294 | g_printf("success!\n"); |
b6ad18ad |
295 | } |
296 | |
b6ad18ad |
297 | static void timedated_on_name_lost(GDBusConnection *conn, |
298 | const gchar *name, |
299 | gpointer user_data) { |
300 | |
509599f0 |
301 | if(!conn) { |
b6ad18ad |
302 | |
509599f0 |
303 | 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); |
304 | timedated_mem_clean(); |
305 | } |
b6ad18ad |
306 | |
509599f0 |
307 | g_print("lost name %s, exiting...\n", name); |
308 | |
309 | timedated_mem_clean(); |
b6ad18ad |
310 | } |
311 | |
b70beb08 |
312 | /* --- end bus/name handlers, begin misc unix functions --- */ |
313 | |
509599f0 |
314 | /* safe call to clean and then exit |
315 | * this stops our GMainLoop safely before letting main() return */ |
b70beb08 |
316 | void timedated_mem_clean() { |
317 | |
341587db |
318 | g_printf("exiting...\n"); |
319 | |
320 | if(dbus_interface_exported) |
321 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
322 | |
323 | if(g_main_loop_is_running(timedated_loop)) |
324 | g_main_loop_quit(timedated_loop); |
325 | |
b70beb08 |
326 | } |
327 | |
712eb329 |
328 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
329 | gboolean unix_sig_terminate_handler(gpointer data) { |
330 | |
331 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
332 | |
333 | timedated_mem_clean(); |
334 | return G_SOURCE_REMOVE; |
335 | } |
336 | |
2ef2cfe1 |
337 | void set_signal_handlers() { |
338 | |
339 | /* we don't care about its descriptor, we never need to unregister these */ |
340 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
341 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
342 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
343 | } |
344 | |
1e8c7c88 |
345 | int main() { |
b6ad18ad |
346 | |
9728ae1f |
347 | set_signal_handlers(); |
348 | |
03f12ac7 |
349 | if(!build_lookup_table()) |
350 | return 1; |
351 | |
90f54407 |
352 | timedated_loop = g_main_loop_new(NULL, TRUE); |
353 | timedated_freeable = g_ptr_array_new(); |
b6ad18ad |
354 | |
90f54407 |
355 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
b6ad18ad |
356 | "org.freedesktop.timedate1", |
357 | G_BUS_NAME_OWNER_FLAGS_NONE, |
358 | timedated_on_bus_acquired, |
359 | timedated_on_name_acquired, |
360 | timedated_on_name_lost, |
361 | NULL, |
362 | NULL); |
363 | |
90f54407 |
364 | g_main_loop_run(timedated_loop); |
9728ae1f |
365 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
90f54407 |
366 | g_main_loop_unref(timedated_loop); |
b6ad18ad |
367 | |
9728ae1f |
368 | /* guaranteed unownable */ |
90f54407 |
369 | g_bus_unown_name(bus_descriptor); |
b6ad18ad |
370 | |
9728ae1f |
371 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
372 | g_ptr_array_free(timedated_freeable, TRUE); |
b6ad18ad |
373 | |
90f54407 |
374 | return 0; |
1e8c7c88 |
375 | } |
1066bd36 |
376 | |
03f12ac7 |
377 | static struct timezone_checksum_pair parse_timezone_path(gchar **pair) { |
1066bd36 |
378 | |
03f12ac7 |
379 | gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path; |
1066bd36 |
380 | GRegex *prefix, *posix, *right; |
381 | GError *err = NULL; |
03f12ac7 |
382 | struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE }; |
1066bd36 |
383 | |
03f12ac7 |
384 | if(!pair[0]) |
385 | return ret; |
1066bd36 |
386 | |
387 | prefix_pattern = (gchar *) g_malloc0(4096); |
03f12ac7 |
388 | right_prefix_pattern = (gchar *) g_malloc0(4096); |
389 | posix_prefix_pattern = (gchar *) g_malloc0(4096); |
390 | |
391 | g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH); |
392 | g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH); |
393 | g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH); |
1066bd36 |
394 | |
395 | prefix = g_regex_new(prefix_pattern, 0, 0, &err); |
03f12ac7 |
396 | posix = g_regex_new(posix_prefix_pattern, 0, 0, &err); |
397 | right = g_regex_new(right_prefix_pattern, 0, 0, &err); |
398 | |
399 | if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
400 | |
401 | ret.posix = TRUE; |
402 | lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
403 | |
404 | } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
405 | |
406 | ret.right = TRUE; |
407 | lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
408 | |
409 | } else |
410 | lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
411 | |
412 | ret.path = lean_path; |
413 | |
414 | ret.sum = g_malloc0(256); |
415 | g_strlcpy(ret.sum, pair[1], 66); |
1066bd36 |
416 | |
417 | g_regex_unref(prefix); |
418 | g_regex_unref(right); |
419 | g_regex_unref(posix); |
420 | |
03f12ac7 |
421 | return ret; |
422 | } |
423 | |
424 | /* TODO need to deconstruct tz_table on exit */ |
425 | static gboolean build_lookup_table() { |
426 | |
427 | gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf; |
428 | GError *err; |
429 | gboolean ret; |
430 | gint i; |
431 | |
432 | i = 0; |
433 | err = NULL; |
434 | ret = TRUE; |
435 | |
436 | find_cmd = (gchar *) g_malloc0(4096); |
437 | find_output = (gchar *) g_malloc0(1000000); |
438 | |
439 | 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); |
440 | |
441 | if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) { |
442 | |
443 | g_printf("error running `%s`\n", find_cmd); |
444 | ret = FALSE; |
445 | } |
446 | |
447 | map_pairs = g_strsplit(find_output, "\n", INT_MAX); |
448 | |
449 | while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) { |
450 | |
451 | tz_table[i] = parse_timezone_path(entry_buf); |
452 | |
453 | g_strfreev(entry_buf); |
454 | i++; |
455 | } |
456 | |
457 | g_free(find_output); |
458 | g_free(find_cmd); |
459 | g_free(map_pairs); |
460 | |
461 | return ret; |
462 | } |
463 | |
464 | static gchar *lookup_hash(gchar *hash) { |
465 | |
466 | gint i = 0; |
467 | |
468 | while(tz_table[i].sum) |
469 | if(!g_strcmp0(tz_table[i].sum, hash)) |
470 | return tz_table[i].path; |
471 | else |
472 | i++; |
473 | |
474 | return NULL; |
1066bd36 |
475 | } |