File: csd-datetime-mechanism.c

package info (click to toggle)
cinnamon-settings-daemon 2.2.4.repack-7%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 14,896 kB
  • ctags: 5,438
  • sloc: ansic: 46,048; makefile: 1,693; xml: 245; sh: 58; python: 21
file content (776 lines) | stat: -rw-r--r-- 26,926 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Suite 500, Boston, MA 02110-1335, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/time.h>

#include <glib.h>
#include <glib-object.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <polkit/polkit.h>

#include "system-timezone.h"

#include "csd-datetime-mechanism.h"
#include "csd-datetime-mechanism-glue.h"

/* NTP helper functions for various distributions */
#include "csd-datetime-mechanism-fedora.h"
#include "csd-datetime-mechanism-debian.h"
#include "csd-datetime-mechanism-suse.h"

static gboolean
do_exit (gpointer user_data)
{
        g_debug ("Exiting due to inactivity");
        exit (1);
        return FALSE;
}

static void
reset_killtimer (void)
{
        static guint timer_id = 0;

        if (timer_id > 0) {
                g_source_remove (timer_id);
                timer_id = 0;
        }
        g_debug ("Setting killtimer to 30 seconds...");
        timer_id = g_timeout_add_seconds (30, do_exit, NULL);
}

struct CsdDatetimeMechanismPrivate
{
        DBusGConnection *system_bus_connection;
        DBusGProxy      *system_bus_proxy;
        PolkitAuthority *auth;
};

static void     csd_datetime_mechanism_finalize    (GObject     *object);

G_DEFINE_TYPE (CsdDatetimeMechanism, csd_datetime_mechanism, G_TYPE_OBJECT)

#define CSD_DATETIME_MECHANISM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CSD_DATETIME_TYPE_MECHANISM, CsdDatetimeMechanismPrivate))

GQuark
csd_datetime_mechanism_error_quark (void)
{
        static GQuark ret = 0;

        if (ret == 0) {
                ret = g_quark_from_static_string ("csd_datetime_mechanism_error");
        }

        return ret;
}


#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }

GType
csd_datetime_mechanism_error_get_type (void)
{
        static GType etype = 0;
        
        if (etype == 0)
        {
                static const GEnumValue values[] =
                        {
                                ENUM_ENTRY (CSD_DATETIME_MECHANISM_ERROR_GENERAL, "GeneralError"),
                                ENUM_ENTRY (CSD_DATETIME_MECHANISM_ERROR_NOT_PRIVILEGED, "NotPrivileged"),
                                ENUM_ENTRY (CSD_DATETIME_MECHANISM_ERROR_INVALID_TIMEZONE_FILE, "InvalidTimezoneFile"),
                                { 0, 0, 0 }
                        };
                
                g_assert (CSD_DATETIME_MECHANISM_NUM_ERRORS == G_N_ELEMENTS (values) - 1);
                
                etype = g_enum_register_static ("CsdDatetimeMechanismError", values);
        }
        
        return etype;
}


static GObject *
csd_datetime_mechanism_constructor (GType                  type,
                                    guint                  n_construct_properties,
                                    GObjectConstructParam *construct_properties)
{
        CsdDatetimeMechanism      *mechanism;

        mechanism = CSD_DATETIME_MECHANISM (G_OBJECT_CLASS (csd_datetime_mechanism_parent_class)->constructor (
                                                type,
                                                n_construct_properties,
                                                construct_properties));

        return G_OBJECT (mechanism);
}

static void
csd_datetime_mechanism_class_init (CsdDatetimeMechanismClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->constructor = csd_datetime_mechanism_constructor;
        object_class->finalize = csd_datetime_mechanism_finalize;

        g_type_class_add_private (klass, sizeof (CsdDatetimeMechanismPrivate));

        dbus_g_object_type_install_info (CSD_DATETIME_TYPE_MECHANISM, &dbus_glib_csd_datetime_mechanism_object_info);

        dbus_g_error_domain_register (CSD_DATETIME_MECHANISM_ERROR, NULL, CSD_DATETIME_MECHANISM_TYPE_ERROR);

}

static void
csd_datetime_mechanism_init (CsdDatetimeMechanism *mechanism)
{
        mechanism->priv = CSD_DATETIME_MECHANISM_GET_PRIVATE (mechanism);

}

static void
csd_datetime_mechanism_finalize (GObject *object)
{
        CsdDatetimeMechanism *mechanism;

        g_return_if_fail (object != NULL);
        g_return_if_fail (CSD_DATETIME_IS_MECHANISM (object));

        mechanism = CSD_DATETIME_MECHANISM (object);

        g_return_if_fail (mechanism->priv != NULL);

        g_object_unref (mechanism->priv->system_bus_proxy);

        G_OBJECT_CLASS (csd_datetime_mechanism_parent_class)->finalize (object);
}

static gboolean
register_mechanism (CsdDatetimeMechanism *mechanism)
{
        GError *error = NULL;

        mechanism->priv->auth = polkit_authority_get_sync (NULL, &error);
        if (mechanism->priv->auth == NULL) {
                if (error != NULL) {
                        g_critical ("error getting system bus: %s", error->message);
                        g_error_free (error);
                }
                goto error;
        }

        mechanism->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
        if (mechanism->priv->system_bus_connection == NULL) {
                if (error != NULL) {
                        g_critical ("error getting system bus: %s", error->message);
                        g_error_free (error);
                }
                goto error;
        }

        dbus_g_connection_register_g_object (mechanism->priv->system_bus_connection, "/", 
                                             G_OBJECT (mechanism));

        mechanism->priv->system_bus_proxy = dbus_g_proxy_new_for_name (mechanism->priv->system_bus_connection,
                                                                      DBUS_SERVICE_DBUS,
                                                                      DBUS_PATH_DBUS,
                                                                      DBUS_INTERFACE_DBUS);

        reset_killtimer ();

        return TRUE;

error:
        return FALSE;
}


CsdDatetimeMechanism *
csd_datetime_mechanism_new (void)
{
        GObject *object;
        gboolean res;

        object = g_object_new (CSD_DATETIME_TYPE_MECHANISM, NULL);

        res = register_mechanism (CSD_DATETIME_MECHANISM (object));
        if (! res) {
                g_object_unref (object);
                return NULL;
        }

        return CSD_DATETIME_MECHANISM (object);
}

static gboolean
_check_polkit_for_action (CsdDatetimeMechanism *mechanism, DBusGMethodInvocation *context)
{
        const char *action = "org.cinnamon.settingsdaemon.datetimemechanism.configure";
        const char *sender;
        GError *error;
        PolkitSubject *subject;
        PolkitAuthorizationResult *result;

        error = NULL;

        /* Check that caller is privileged */
        sender = dbus_g_method_get_sender (context);
        subject = polkit_system_bus_name_new (sender);

        result = polkit_authority_check_authorization_sync (mechanism->priv->auth,
                                                            subject,
                                                            action,
                                                            NULL,
                                                            POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                                                            NULL, &error);
        g_object_unref (subject);

        if (error) {
                dbus_g_method_return_error (context, error);
                g_error_free (error);

                return FALSE;
        }

        if (!polkit_authorization_result_get_is_authorized (result)) {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_NOT_PRIVILEGED,
                                     "Not Authorized for action %s", action);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                g_object_unref (result);

                return FALSE;
        }

        g_object_unref (result);

        return TRUE;
}

static gboolean
_sync_hwclock (DBusGMethodInvocation *context)
{
        GError *error;

        error = NULL;

        if (g_file_test ("/sbin/hwclock",
                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) {
                int exit_status;
                if (!g_spawn_command_line_sync ("/sbin/hwclock --systohc", NULL, NULL, &exit_status, &error)) {
                        GError *error2;
                        error2 = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                              CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                              "Error spawning /sbin/hwclock: %s", error->message);
                        g_error_free (error);
                        dbus_g_method_return_error (context, error2);
                        g_error_free (error2);
                        return FALSE;
                }
                if (WEXITSTATUS (exit_status) != 0) {
                        error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                             CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                             "/sbin/hwclock returned %d", exit_status);
                        dbus_g_method_return_error (context, error);
                        g_error_free (error);
                        return FALSE;
                }
        }

        return TRUE;
}

static gboolean
_set_time (CsdDatetimeMechanism  *mechanism,
           const struct timeval  *tv,
           DBusGMethodInvocation *context)
{
        GError *error;

        if (!_check_polkit_for_action (mechanism, context))
                return FALSE;

        if (settimeofday (tv, NULL) != 0) {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Error calling settimeofday({%lld,%lld}): %s",
                                     (gint64) tv->tv_sec, (gint64) tv->tv_usec,
                                     strerror (errno));
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        if (!_sync_hwclock (context))
                return FALSE;

        dbus_g_method_return (context);
        return TRUE;
}

static gboolean
_set_date (CsdDatetimeMechanism  *mechanism,
           guint                  day,
           guint                  month,
           guint                  year,
           DBusGMethodInvocation *context)
{
        GDateTime *time;
        char *date_str, *time_str;
        char *date_cmd;
        int exit_status;
        GError *error;

        if (!_check_polkit_for_action (mechanism, context))
                return FALSE;

        date_str = g_strdup_printf ("%02d/%02d/%d", month, day, year);
        error = NULL;

        time = g_date_time_new_now_local ();
        time_str = g_date_time_format (time, "%R:%S");
        g_date_time_unref (time);

        date_cmd = g_strdup_printf ("/bin/date -s \"%s %s\" +\"%%D %%R:%%S\"", date_str, time_str);
        g_free (date_str);
        g_free (time_str);

        if (!g_spawn_command_line_sync (date_cmd, NULL, NULL, &exit_status, &error)) {
                GError *error2;
                error2 = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                      CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                      "Error spawning /bin/date: %s", error->message);
                g_error_free (error);
                dbus_g_method_return_error (context, error2);
                g_error_free (error2);
                g_free (date_cmd);
                return FALSE;
        }
        g_free (date_cmd);
        if (WEXITSTATUS (exit_status) != 0) {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "/bin/date returned %d", exit_status);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        if (!_sync_hwclock (context))
                return FALSE;

        return TRUE;
}

/* exported methods */

gboolean
csd_datetime_mechanism_set_time (CsdDatetimeMechanism  *mechanism,
                                 gint64                 seconds_since_epoch,
                                 DBusGMethodInvocation *context)
{
        struct timeval tv;

        reset_killtimer ();
        g_debug ("SetTime(%" G_GINT64_FORMAT ") called", seconds_since_epoch);

        tv.tv_sec = (time_t) seconds_since_epoch;
        tv.tv_usec = 0;
        return _set_time (mechanism, &tv, context);
}

gboolean
csd_datetime_mechanism_set_date (CsdDatetimeMechanism  *mechanism,
                                 guint                  day,
                                 guint                  month,
                                 guint                  year,
                                 DBusGMethodInvocation *context)
{
        reset_killtimer ();
        g_debug ("SetDate(%d, %d, %d) called", day, month, year);

        return _set_date (mechanism, day, month, year, context);
}

gboolean
csd_datetime_mechanism_adjust_time (CsdDatetimeMechanism  *mechanism,
                                    gint64                 seconds_to_add,
                                    DBusGMethodInvocation *context)
{
        struct timeval tv;

        reset_killtimer ();
        g_debug ("AdjustTime(%" G_GINT64_FORMAT " ) called", seconds_to_add);

        if (gettimeofday (&tv, NULL) != 0) {
                GError *error;
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Error calling gettimeofday(): %s", strerror (errno));
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        tv.tv_sec += (time_t) seconds_to_add;
        return _set_time (mechanism, &tv, context);
}

static gboolean
csd_datetime_check_tz_name (const char *tz,
                            GError    **error)
{
        GFile *file;
        char *tz_path, *actual_path;
        gboolean retval;

        retval = TRUE;
        tz_path = g_build_filename (SYSTEM_ZONEINFODIR, tz, NULL);

        /* Get the actual resolved path */
        file = g_file_new_for_path (tz_path);
        actual_path = g_file_get_path (file);
        g_object_unref (file);

        /* The tz name passed had relative paths in it */
        if (g_strcmp0 (tz_path, actual_path) != 0) {
                g_set_error (error, CSD_DATETIME_MECHANISM_ERROR,
                             CSD_DATETIME_MECHANISM_ERROR_INVALID_TIMEZONE_FILE,
                             "Timezone file '%s' was invalid.",
                             tz);
                retval = FALSE;
        }

        g_free (tz_path);
        g_free (actual_path);

        return retval;
}

gboolean
csd_datetime_mechanism_set_timezone (CsdDatetimeMechanism  *mechanism,
                                     const char            *tz,
                                     DBusGMethodInvocation *context)
{
        GError *error;

        reset_killtimer ();
        g_debug ("SetTimezone('%s') called", tz);

        if (!_check_polkit_for_action (mechanism, context))
                return FALSE;

        error = NULL;

        if (!csd_datetime_check_tz_name (tz, &error))
                return FALSE;

        if (!system_timezone_set (tz, &error)) {
                GError *error2;
                int     code;

                if (error->code == SYSTEM_TIMEZONE_ERROR_INVALID_TIMEZONE_FILE)
                        code = CSD_DATETIME_MECHANISM_ERROR_INVALID_TIMEZONE_FILE;
                else
                        code = CSD_DATETIME_MECHANISM_ERROR_GENERAL;

                error2 = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                      code, "%s", error->message);

                g_error_free (error);

                dbus_g_method_return_error (context, error2);
                g_error_free (error2);

                return FALSE;
        }

        dbus_g_method_return (context);
        return TRUE;
}


gboolean
csd_datetime_mechanism_get_timezone (CsdDatetimeMechanism   *mechism,
                                     DBusGMethodInvocation  *context)
{
  gchar *timezone;

  reset_killtimer ();

  timezone = system_timezone_find ();

  dbus_g_method_return (context, timezone);

  return TRUE;
}

gboolean
csd_datetime_mechanism_get_hardware_clock_using_utc (CsdDatetimeMechanism  *mechanism,
                                                     DBusGMethodInvocation *context)
{
        char **lines;
        char *data;
        gsize len;
        GError *error;
        gboolean is_utc;

        error = NULL;

        if (!g_file_get_contents ("/etc/adjtime", &data, &len, &error)) {
                GError *error2;
                error2 = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                      CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                      "Error reading /etc/adjtime file: %s", error->message);
                g_error_free (error);
                dbus_g_method_return_error (context, error2);
                g_error_free (error2);
                return FALSE;
        }

        lines = g_strsplit (data, "\n", 0);
        g_free (data);

        if (g_strv_length (lines) < 3) {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Cannot parse /etc/adjtime");
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                g_strfreev (lines);
                return FALSE;
        }

        if (strcmp (lines[2], "UTC") == 0) {
                is_utc = TRUE;
        } else if (strcmp (lines[2], "LOCAL") == 0) {
                is_utc = FALSE;
        } else {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Expected UTC or LOCAL at line 3 of /etc/adjtime; found '%s'", lines[2]);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                g_strfreev (lines);
                return FALSE;
        }
        g_strfreev (lines);
        dbus_g_method_return (context, is_utc);
        return TRUE;
}

gboolean
csd_datetime_mechanism_set_hardware_clock_using_utc (CsdDatetimeMechanism  *mechanism,
                                                     gboolean               using_utc,
                                                     DBusGMethodInvocation *context)
{
        GError *error;

        error = NULL;

        if (!_check_polkit_for_action (mechanism, context))
                return FALSE;

        if (g_file_test ("/sbin/hwclock", 
                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) {
                int exit_status;
                char *cmd;
                cmd = g_strdup_printf ("/sbin/hwclock %s --systohc", using_utc ? "--utc" : "--localtime");
                if (!g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, &error)) {
                        GError *error2;
                        error2 = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                              CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                              "Error spawning /sbin/hwclock: %s", error->message);
                        g_error_free (error);
                        dbus_g_method_return_error (context, error2);
                        g_error_free (error2);
                        g_free (cmd);
                        return FALSE;
                }
                g_free (cmd);
                if (WEXITSTATUS (exit_status) != 0) {
                        error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                             CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                             "/sbin/hwclock returned %d", exit_status);
                        dbus_g_method_return_error (context, error);
                        g_error_free (error);
                        return FALSE;
                }

                if (g_file_test ("/etc/fedora-release", G_FILE_TEST_EXISTS)) { /* Fedora */
                        if (!_update_etc_sysconfig_clock_fedora (context, "UTC=", using_utc ? "true" : "false"))
                                return FALSE;
		} else if (g_file_test ("/etc/SuSE-release", G_FILE_TEST_EXISTS)) { /* SUSE variant */
                        if (!_update_etc_sysconfig_clock_suse (context, "HWCLOCK=", using_utc ? "-u" : "--localtime"))
                                return FALSE;
		}
        }
        dbus_g_method_return (context);
        return TRUE;
}

gboolean
csd_datetime_mechanism_get_using_ntp  (CsdDatetimeMechanism    *mechanism,
                                       DBusGMethodInvocation   *context)
{
        GError *error = NULL;
        gboolean ret;

        if (g_file_test ("/etc/fedora-release", G_FILE_TEST_EXISTS)) /* Fedora */
                ret = _get_using_ntp_fedora (context);
        else if (g_file_test ("/usr/sbin/update-rc.d", G_FILE_TEST_EXISTS)) /* Debian */
                ret = _get_using_ntp_debian (context);
	else if (g_file_test ("/etc/SuSE-release", G_FILE_TEST_EXISTS)) /* SUSE variant */
                ret = _get_using_ntp_suse (context);
        else {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Error enabling NTP: OS variant not supported");
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        return ret;
}

gboolean
csd_datetime_mechanism_set_using_ntp  (CsdDatetimeMechanism    *mechanism,
                                       gboolean                 using_ntp,
                                       DBusGMethodInvocation   *context)
{
        GError *error;
        gboolean ret;

        error = NULL;

        if (!_check_polkit_for_action (mechanism, context))
                return FALSE;

        if (g_file_test ("/etc/fedora-release", G_FILE_TEST_EXISTS)) /* Fedora */
                ret = _set_using_ntp_fedora (context, using_ntp);
        else if (g_file_test ("/usr/sbin/update-rc.d", G_FILE_TEST_EXISTS)) /* Debian */
                ret = _set_using_ntp_debian (context, using_ntp);
	else if (g_file_test ("/etc/SuSE-release", G_FILE_TEST_EXISTS)) /* SUSE variant */
                ret = _set_using_ntp_suse (context, using_ntp);
        else {
                error = g_error_new (CSD_DATETIME_MECHANISM_ERROR,
                                     CSD_DATETIME_MECHANISM_ERROR_GENERAL,
                                     "Error enabling NTP: OS variant not supported");
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        return ret;
}

static void
check_can_do (CsdDatetimeMechanism  *mechanism,
              const char            *action,
              DBusGMethodInvocation *context)
{
        const char *sender;
        PolkitSubject *subject;
        PolkitAuthorizationResult *result;
        GError *error;

        /* Check that caller is privileged */
        sender = dbus_g_method_get_sender (context);
        subject = polkit_system_bus_name_new (sender);

        error = NULL;
        result = polkit_authority_check_authorization_sync (mechanism->priv->auth,
                                                            subject,
                                                            action,
                                                            NULL,
                                                            0,
                                                            NULL,
                                                            &error);
        g_object_unref (subject);

        if (error) {
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return;
        }

        if (polkit_authorization_result_get_is_authorized (result)) {
                dbus_g_method_return (context, 2);
        }
        else if (polkit_authorization_result_get_is_challenge (result)) {
                dbus_g_method_return (context, 1);
        }
        else {
                dbus_g_method_return (context, 0);
        }

        g_object_unref (result);
}


gboolean
csd_datetime_mechanism_can_set_time (CsdDatetimeMechanism  *mechanism,
                                     DBusGMethodInvocation *context)
{
        check_can_do (mechanism,
                      "org.cinnamon.settingsdaemon.datetimemechanism.configure",
                      context);

        return TRUE;
}

gboolean
csd_datetime_mechanism_can_set_timezone (CsdDatetimeMechanism  *mechanism,
                                         DBusGMethodInvocation *context)
{
        check_can_do (mechanism,
                      "org.cinnamon.settingsdaemon.datetimemechanism.configure",
                      context);

        return TRUE;
}

gboolean
csd_datetime_mechanism_can_set_using_ntp (CsdDatetimeMechanism  *mechanism,
                                          DBusGMethodInvocation *context)
{
        check_can_do (mechanism,
                      "org.cinnamon.settingsdaemon.datetimemechanism.configure",
                      context);

        return TRUE;
}