summaryrefslogtreecommitdiff
path: root/src/polkit-grant/polkit-authorization-db-write.c
blob: fec91a15be61d8b1111048093f1307a59ff1bb7d (plain)
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
 *
 * polkit-authorization-db.c : Represents the authorization database
 *
 * Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

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

#include <glib.h>

#include <polkit/polkit-debug.h>
#include <polkit/polkit-authorization-db.h>
#include <polkit/polkit-utils.h>
#include <polkit/polkit-private.h>

/**
 * SECTION:polkit-authorization-db
 **/


static polkit_bool_t
_write_to_fd (int fd, const char *str, ssize_t str_len)
{
        polkit_bool_t ret;
        ssize_t written;

        ret = FALSE;

        written = 0;
        while (written < str_len) {
                ssize_t ret;
                ret = write (fd, str + written, str_len - written);
                if (ret < 0) {
                        if (errno == EAGAIN || errno == EINTR) {
                                continue;
                        } else {
                                goto out;
                        }
                }
                written += ret;
        }

        ret = TRUE;

out:
        return ret;
}

polkit_bool_t 
_polkit_authorization_db_auth_file_add (polkit_bool_t transient, uid_t uid, char *str_to_add)
{
        int fd;
        char *contents;
        gsize contents_size;
        char *path;
        char *path_tmp;
        GError *error;
        polkit_bool_t ret;
        struct stat statbuf;
        struct passwd *pw;
        const char *root;
        char *newline = "\n";

        if (transient)
                root = PACKAGE_LOCALSTATE_DIR "/run/polkit-1";
        else
                root = PACKAGE_LOCALSTATE_DIR "/lib/polkit-1";

        ret = FALSE;
        path = NULL;
        path_tmp = NULL;
        contents = NULL;

        pw = getpwuid (uid);
        if (pw == NULL) {
                g_warning ("cannot lookup user name for uid %d\n", uid);
                goto out;
        }

        path = g_strdup_printf ("%s/user-%s.auths", root, pw->pw_name);
        path_tmp = g_strdup_printf ("%s.XXXXXX", path);

        if (stat (path, &statbuf) != 0 && errno == ENOENT) {
                //fprintf (stderr, "path=%s does not exist (egid=%d): %m!\n", path, getegid ());

                g_free (path_tmp);
                path_tmp = path;
                path = NULL;

                /* Write a nice blurb if we're creating the file for the first time */

                contents = g_strdup_printf (
                        "# This file lists authorizations for user %s\n"
                        "%s"
                        "# \n"
                        "# File format may change at any time; do not rely on it. To manage\n"
                        "# authorizations use polkit-auth(1) instead.\n"
                        "\n",
                        pw->pw_name,
                        transient ? "# (these are temporary and will be removed on the next system boot)\n" : "");
                contents_size = strlen (contents);
        } else {
                error = NULL;
                if (!g_file_get_contents (path, &contents, &contents_size, &error)) {
                        g_warning ("Cannot read authorizations file %s: %s", path, error->message);
                        g_error_free (error);
                        goto out;
                }
        }

        if (path != NULL) {
                fd = mkstemp (path_tmp);
                if (fd < 0) {
                        fprintf (stderr, "Cannot create file '%s': %m\n", path_tmp);
                        goto out;
                }
                if (fchmod (fd, 0464) != 0) {
                        fprintf (stderr, "Cannot change mode for '%s' to 0460: %m\n", path_tmp);
                        close (fd);
                        unlink (path_tmp);
                        goto out;
                }
        } else {
                fd = open (path_tmp, O_RDWR|O_CREAT, 0464);
                if (fd < 0) {
                        fprintf (stderr, "Cannot create file '%s': %m\n", path_tmp);
                        goto out;
                }
        }

        if (!_write_to_fd (fd, contents, contents_size)) {
                g_warning ("Cannot write to temporary authorizations file %s: %m", path_tmp);
                close (fd);
                if (unlink (path_tmp) != 0) {
                        g_warning ("Cannot unlink %s: %m", path_tmp);
                }
                goto out;
        }
        if (!_write_to_fd (fd, str_to_add, strlen (str_to_add))) {
                g_warning ("Cannot write to temporary authorizations file %s: %m", path_tmp);
                close (fd);
                if (unlink (path_tmp) != 0) {
                        g_warning ("Cannot unlink %s: %m", path_tmp);
                }
                goto out;
        }
        if (!_write_to_fd (fd, newline, 1)) {
                g_warning ("Cannot write to temporary authorizations file %s: %m", path_tmp);
                close (fd);
                if (unlink (path_tmp) != 0) {
                        g_warning ("Cannot unlink %s: %m", path_tmp);
                }
                goto out;
        }
        close (fd);

        if (path != NULL) {
                if (rename (path_tmp, path) != 0) {
                        g_warning ("Cannot rename %s to %s: %m", path_tmp, path);
                        if (unlink (path_tmp) != 0) {
                                g_warning ("Cannot unlink %s: %m", path_tmp);
                        }
                        goto out;
                }
        }

        /* trigger a reload */
        if (utimes (PACKAGE_LOCALSTATE_DIR "/lib/misc/polkit-1.reload", NULL) != 0) {
                g_warning ("Error updating access+modification time on file '%s': %m\n", 
                           PACKAGE_LOCALSTATE_DIR "/lib/misc/polkit-1.reload");
        }

        ret = TRUE;

out:
        if (contents != NULL)
                g_free (contents);
        if (path != NULL)
                g_free (path);
        if (path_tmp != NULL)
                g_free (path_tmp);
        return ret;
}

/* returns -1 on error */
static int
_write_constraints (char *buf, size_t buf_size, PolKitAuthorizationConstraint **constraints)
{
        unsigned int n;
        unsigned int m;

        kit_return_val_if_fail (constraints != NULL, 0);

        for (n = 0, m = 0; constraints[n] != NULL; n++) {
                PolKitAuthorizationConstraint *c;
                const char *key;
                char value[1024];

                c = constraints[n];

                key = "constraint";

                if (polkit_authorization_constraint_to_string (c, value, sizeof (value)) >= sizeof (value)) {
                        kit_warning ("Constraint %d is too large!", n);
                        m = -1;
                        goto out;
                }

                if (m < buf_size)
                        buf[m] = ':';
                m++;

                m += kit_string_percent_encode (buf + m, buf_size - m > 0 ? buf_size - m : 0, key);

                if (m < buf_size)
                        buf[m] = '=';
                m++;

                m += kit_string_percent_encode (buf + m, buf_size - m > 0 ? buf_size - m : 0, value);
        }

        if (m < buf_size)
                buf[m] = '\0';

out:
        return m;
}

static polkit_bool_t
_add_caller_constraints (char *buf, size_t buf_size, PolKitCaller *caller)
{
        PolKitAuthorizationConstraint *constraints[64];
        int num_constraints;
        polkit_bool_t ret;
        int num_written;
        int n;

        ret = FALSE;

        num_constraints = polkit_authorization_constraint_get_from_caller (caller, constraints, 64);
        if (num_constraints == -1)
                goto out;

        if (num_constraints >= 64) {
                goto out;
        }

        num_written = _write_constraints (buf, buf_size, constraints);
        if (num_written == -1) {
                goto out;
        }
        
        if ((size_t) num_written >= buf_size) {
                goto out;
        }
        
        ret = TRUE;

out:
        for (n = 0; n < num_constraints && n < 64 && constraints[n] != NULL; n++) {
                polkit_authorization_constraint_unref (constraints[n]);
        }
        return ret;
}

/**
 * polkit_authorization_db_add_entry_process_one_shot:
 * @authdb: the authorization database
 * @action: the action
 * @caller: the caller
 * @user_authenticated_as: the user that was authenticated
 *
 * Write an entry to the authorization database to indicate that the
 * given caller is authorized for the given action a single time.
 *
 * Note that this function should only be used by
 * <literal>libpolkit-grant</literal> or other sufficiently privileged
 * processes that deals with managing authorizations. It should never
 * be used by mechanisms or applications. The caller must have
 * egid=polkituser and umask set so creating files with mode 0460 will
 * work.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if an entry was written to the authorization
 * database, #FALSE if the caller of this function is not sufficiently
 * privileged.
 *
 * Since: 0.7
 */
polkit_bool_t
polkit_authorization_db_add_entry_process_one_shot (PolKitAuthorizationDB *authdb,
                                                    PolKitAction          *action,
                                                    PolKitCaller          *caller,
                                                    uid_t                  user_authenticated_as)
{
        char *action_id;
        uid_t caller_uid;
        pid_t caller_pid;
        polkit_bool_t ret;
        polkit_uint64_t pid_start_time;
        struct timeval now;

        g_return_val_if_fail (authdb != NULL, FALSE);
        g_return_val_if_fail (action != NULL, FALSE);
        g_return_val_if_fail (caller != NULL, FALSE);

        if (!polkit_action_get_action_id (action, &action_id))
                return FALSE;

        if (!polkit_caller_get_pid (caller, &caller_pid))
                return FALSE;

        if (!polkit_caller_get_uid (caller, &caller_uid))
                return FALSE;

        pid_start_time = polkit_sysdeps_get_start_time_for_pid (caller_pid);
        if (pid_start_time == 0)
                return FALSE;

        if (gettimeofday (&now, NULL) != 0) {
                g_warning ("Error calling gettimeofday: %m");
                return FALSE;
        }

        char pid_buf[32];
        char pid_st_buf[32];
        char now_buf[32];
        char uid_buf[32];
        char auth_buf[1024];
        snprintf (pid_buf, sizeof (pid_buf), "%d", caller_pid);
        snprintf (pid_st_buf, sizeof (pid_st_buf), "%Lu", pid_start_time);
        snprintf (now_buf, sizeof (now_buf), "%Lu", (polkit_uint64_t) now.tv_sec);
        snprintf (uid_buf, sizeof (uid_buf), "%d", user_authenticated_as);

        size_t len;
        if ((len = kit_string_entry_create (auth_buf, sizeof (auth_buf),
                                            "scope",          "process-one-shot",
                                            "pid",            pid_buf,
                                            "pid-start-time", pid_st_buf,
                                            "action-id",      action_id,
                                            "when",           now_buf,
                                            "auth-as",        uid_buf,
                                            NULL)) >= sizeof (auth_buf)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        if (!_add_caller_constraints (auth_buf + len, sizeof (auth_buf) - len, caller)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        ret = _polkit_authorization_db_auth_file_add (TRUE, 
                                                      caller_uid, 
                                                      auth_buf);
        return ret;
}

/**
 * polkit_authorization_db_add_entry_process:
 * @authdb: the authorization database
 * @action: the action
 * @caller: the caller
 * @user_authenticated_as: the user that was authenticated
 *
 * Write an entry to the authorization database to indicate that the
 * given caller is authorized for the given action.
 *
 * Note that this function should only be used by
 * <literal>libpolkit-grant</literal> or other sufficiently privileged
 * processes that deals with managing authorizations. It should never
 * be used by mechanisms or applications. The caller must have
 * egid=polkituser and umask set so creating files with mode 0460 will
 * work.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if an entry was written to the authorization
 * database, #FALSE if the caller of this function is not sufficiently
 * privileged.
 *
 * Since: 0.7
 */
polkit_bool_t
polkit_authorization_db_add_entry_process          (PolKitAuthorizationDB *authdb,
                                                    PolKitAction          *action,
                                                    PolKitCaller          *caller,
                                                    uid_t                  user_authenticated_as)
{
        char *action_id;
        uid_t caller_uid;
        pid_t caller_pid;
        polkit_bool_t ret;
        polkit_uint64_t pid_start_time;
        struct timeval now;

        g_return_val_if_fail (authdb != NULL, FALSE);
        g_return_val_if_fail (action != NULL, FALSE);
        g_return_val_if_fail (caller != NULL, FALSE);

        if (!polkit_action_get_action_id (action, &action_id))
                return FALSE;

        if (!polkit_caller_get_pid (caller, &caller_pid))
                return FALSE;

        if (!polkit_caller_get_uid (caller, &caller_uid))
                return FALSE;

        pid_start_time = polkit_sysdeps_get_start_time_for_pid (caller_pid);
        if (pid_start_time == 0)
                return FALSE;

        if (gettimeofday (&now, NULL) != 0) {
                g_warning ("Error calling gettimeofday: %m");
                return FALSE;
        }

        char pid_buf[32];
        char pid_st_buf[32];
        char now_buf[32];
        char uid_buf[32];
        char auth_buf[1024];
        snprintf (pid_buf, sizeof (pid_buf), "%d", caller_pid);
        snprintf (pid_st_buf, sizeof (pid_st_buf), "%Lu", pid_start_time);
        snprintf (now_buf, sizeof (now_buf), "%Lu", (polkit_uint64_t) now.tv_sec);
        snprintf (uid_buf, sizeof (uid_buf), "%d", user_authenticated_as);

        size_t len;
        if ((len = kit_string_entry_create (auth_buf, sizeof (auth_buf),
                                            "scope",          "process",
                                            "pid",            pid_buf,
                                            "pid-start-time", pid_st_buf,
                                            "action-id",      action_id,
                                            "when",           now_buf,
                                            "auth-as",        uid_buf,
                                            NULL)) >= sizeof (auth_buf)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        if (!_add_caller_constraints (auth_buf + len, sizeof (auth_buf) - len, caller)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        ret = _polkit_authorization_db_auth_file_add (TRUE, 
                                                      caller_uid, 
                                                      auth_buf);
        return ret;
}

/**
 * polkit_authorization_db_add_entry_session:
 * @authdb: the authorization database
 * @action: the action
 * @caller: the caller
 * @user_authenticated_as: the user that was authenticated
 *
 * Write an entry to the authorization database to indicate that the
 * session for the given caller is authorized for the given action for
 * the remainer of the session.
 *
 * Note that this function should only be used by
 * <literal>libpolkit-grant</literal> or other sufficiently privileged
 * processes that deals with managing authorizations. It should never
 * be used by mechanisms or applications. The caller must have
 * egid=polkituser and umask set so creating files with mode 0460 will
 * work.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if an entry was written to the authorization
 * database, #FALSE if the caller of this function is not sufficiently
 * privileged.
 *
 * Since: 0.7
 */
polkit_bool_t
polkit_authorization_db_add_entry_session          (PolKitAuthorizationDB *authdb,
                                                    PolKitAction          *action,
                                                    PolKitCaller          *caller,
                                                    uid_t                  user_authenticated_as)
{
        uid_t session_uid;
        char *action_id;
        PolKitSession *session;
        char *session_objpath;
        polkit_bool_t ret;
        struct timeval now;

        g_return_val_if_fail (authdb != NULL, FALSE);
        g_return_val_if_fail (action != NULL, FALSE);
        g_return_val_if_fail (caller != NULL, FALSE);

        if (!polkit_action_get_action_id (action, &action_id))
                return FALSE;

        if (!polkit_caller_get_ck_session (caller, &session))
                return FALSE;

        if (!polkit_session_get_ck_objref (session, &session_objpath))
                return FALSE;

        if (!polkit_session_get_uid (session, &session_uid))
                return FALSE;

        if (gettimeofday (&now, NULL) != 0) {
                g_warning ("Error calling gettimeofday: %m");
                return FALSE;
        }

        char now_buf[32];
        char uid_buf[32];
        char auth_buf[1024];
        snprintf (now_buf, sizeof (now_buf), "%Lu", (polkit_uint64_t) now.tv_sec);
        snprintf (uid_buf, sizeof (uid_buf), "%d", user_authenticated_as);

        size_t len;
        if ((len = kit_string_entry_create (auth_buf, sizeof (auth_buf),
                                            "scope",          "session",
                                            "session-id",     session_objpath,
                                            "action-id",      action_id,
                                            "when",           now_buf,
                                            "auth-as",        uid_buf,
                                            NULL)) >= sizeof (auth_buf)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        if (!_add_caller_constraints (auth_buf + len, sizeof (auth_buf) - len, caller)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        ret = _polkit_authorization_db_auth_file_add (TRUE, 
                                                      session_uid, 
                                                      auth_buf);
        return ret;
}

/**
 * polkit_authorization_db_add_entry_always:
 * @authdb: the authorization database
 * @action: the action
 * @caller: the caller
 * @user_authenticated_as: the user that was authenticated
 *
 * Write an entry to the authorization database to indicate that the
 * given user is authorized for the given action.
 *
 * Note that this function should only be used by
 * <literal>libpolkit-grant</literal> or other sufficiently privileged
 * processes that deals with managing authorizations. It should never
 * be used by mechanisms or applications. The caller must have
 * egid=polkituser and umask set so creating files with mode 0460 will
 * work.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if an entry was written to the authorization
 * database, #FALSE if the caller of this function is not sufficiently
 * privileged.
 *
 * Since: 0.7
 */
polkit_bool_t
polkit_authorization_db_add_entry_always           (PolKitAuthorizationDB *authdb,
                                                    PolKitAction          *action,
                                                    PolKitCaller          *caller,
                                                    uid_t                  user_authenticated_as)
{
        uid_t uid;
        char *action_id;
        polkit_bool_t ret;
        struct timeval now;

        g_return_val_if_fail (authdb != NULL, FALSE);
        g_return_val_if_fail (action != NULL, FALSE);
        g_return_val_if_fail (caller != NULL, FALSE);

        if (!polkit_caller_get_uid (caller, &uid))
                return FALSE;

        if (!polkit_action_get_action_id (action, &action_id))
                return FALSE;

        if (gettimeofday (&now, NULL) != 0) {
                g_warning ("Error calling gettimeofday: %m");
                return FALSE;
        }

        char now_buf[32];
        char uid_buf[32];
        char auth_buf[1024];
        snprintf (now_buf, sizeof (now_buf), "%Lu", (polkit_uint64_t) now.tv_sec);
        snprintf (uid_buf, sizeof (uid_buf), "%d", user_authenticated_as);

        size_t len;
        if ((len = kit_string_entry_create (auth_buf, sizeof (auth_buf),
                                            "scope",          "always",
                                            "action-id",      action_id,
                                            "when",           now_buf,
                                            "auth-as",        uid_buf,
                                            NULL)) >= sizeof (auth_buf)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }
        if (!_add_caller_constraints (auth_buf + len, sizeof (auth_buf) - len, caller)) {
                g_warning ("authbuf for is too small");
                return FALSE;
        }

        ret = _polkit_authorization_db_auth_file_add (FALSE, 
                                                      uid, 
                                                      auth_buf);
        return ret;
}


typedef struct {
        char *action_id;
        unsigned int _check_constraint_num;
        PolKitAuthorizationConstraint  **constraints;

        polkit_bool_t is_authorized;
        polkit_bool_t is_negative_authorized;
} CheckDataGrant;

static polkit_bool_t
_check_constraints_for_grant (PolKitAuthorization *auth, PolKitAuthorizationConstraint *authc, void *user_data)
{
        CheckDataGrant *cd = (CheckDataGrant *) user_data;
        polkit_bool_t ret;

        ret = FALSE;

        if (cd->constraints [cd->_check_constraint_num] == NULL)
                goto no_match;

        if (!polkit_authorization_constraint_equal (authc, cd->constraints[cd->_check_constraint_num]))
                goto no_match;

        cd->_check_constraint_num += 1;
        return FALSE;

no_match:
        return TRUE;
}

static polkit_bool_t 
_check_auth_for_grant (PolKitAuthorizationDB *authdb, PolKitAuthorization *auth, void *user_data)
{
        uid_t pimp;
        polkit_bool_t ret;
        polkit_bool_t is_negative;
        CheckDataGrant *cd = (CheckDataGrant *) user_data;

        ret = FALSE;

        if (strcmp (polkit_authorization_get_action_id (auth), cd->action_id) != 0)
                goto no_match;

        if (!polkit_authorization_was_granted_explicitly (auth, &pimp, &is_negative))
                goto no_match;

        /* This checks that the number of authorizations are the
         * same.. as well as that the authorizations are similar one
         * by one..
         *
         * TODO: FIXME: this relies on the ordering, e.g. we don't
         * catch local+active if there is an active+local one already.
         */
        cd->_check_constraint_num = 0;
        if (polkit_authorization_constraints_foreach (auth, _check_constraints_for_grant, cd) ||
            cd->constraints [cd->_check_constraint_num] != NULL)
                goto no_match;

        if (is_negative) {
                cd->is_authorized = FALSE;
                cd->is_negative_authorized = TRUE;
                /* it only takes a single negative auth to block things so stop iterating */
                ret = TRUE;
        } else {
                cd->is_authorized = TRUE;
                cd->is_negative_authorized = FALSE;
                /* keep iterating; we may find negative auths... */
        }

no_match:
        return ret;
}

static polkit_bool_t 
_grant_internal (PolKitAuthorizationDB          *authdb,
                 PolKitAction                   *action,
                 uid_t                           uid,
                 PolKitAuthorizationConstraint **constraints,
                 PolKitError                   **error,
                 polkit_bool_t                   is_negative)
{
        GError *g_error;
        char *helper_argv[6] = {PACKAGE_LIBEXEC_DIR "/polkit-explicit-grant-helper-1", NULL, NULL, NULL, NULL, NULL};
        gboolean ret;
        gint exit_status;
        char cbuf[1024];
        CheckDataGrant cd;
        polkit_bool_t did_exist;

        ret = FALSE;

        g_return_val_if_fail (authdb != NULL, FALSE);
        g_return_val_if_fail (action != NULL, FALSE);

        if (!polkit_action_get_action_id (action, &(cd.action_id))) {
                polkit_error_set_error (error, 
                                        POLKIT_ERROR_GENERAL_ERROR, 
                                        "Given action does not have action_id set");
                goto out;
        }

        if (constraints == NULL) {
                cbuf[0] = '\0';
        } else {
                int num_written;
                num_written = _write_constraints (cbuf, sizeof (cbuf), constraints);
                if (num_written == -1) {
                        polkit_error_set_error (error, 
                                                POLKIT_ERROR_GENERAL_ERROR, 
                                                "one of the given constraints did not fit");
                        goto out;
                }

                if ((size_t) num_written >= sizeof (cbuf)) {
                        g_warning ("buffer for auth constraint is too small");
                        polkit_error_set_error (error, 
                                                POLKIT_ERROR_GENERAL_ERROR, 
                                                "buffer for auth constraint is too small");
                        goto out;
                }
        }

        /* check if we have the auth already */
        cd.constraints = constraints;
        cd.is_authorized = FALSE;
        cd.is_negative_authorized = FALSE;
        polkit_authorization_db_foreach_for_uid (authdb,
                                                 uid, 
                                                 _check_auth_for_grant,
                                                 &cd,
                                                 error);

        /* happens if caller can't read auths of target user */
        if (error != NULL && polkit_error_is_set (*error)) {
                goto out;
        }

        did_exist = FALSE;
        if (is_negative) {
                if (cd.is_negative_authorized)
                        did_exist = TRUE;
        } else {
                if (cd.is_authorized)
                        did_exist = TRUE;
        }
        
        if (did_exist) {
                /* so it did exist.. */
                polkit_error_set_error (error, 
                                        POLKIT_ERROR_AUTHORIZATION_ALREADY_EXISTS, 
                                        "An authorization for uid %d for the action %s with constraint '%s' already exists",
                                        uid, cd.action_id, cbuf);
                goto out;
        }


        helper_argv[1] = cd.action_id;
        helper_argv[2] = cbuf;
        if (is_negative)
                helper_argv[3] = "uid-negative";
        else
                helper_argv[3] = "uid";
        helper_argv[4] = g_strdup_printf ("%d", uid);
        helper_argv[5] = NULL;

        g_error = NULL;
        if (!g_spawn_sync (NULL,         /* const gchar *working_directory */
                           helper_argv,  /* gchar **argv */
                           NULL,         /* gchar **envp */
                           0,            /* GSpawnFlags flags */
                           NULL,         /* GSpawnChildSetupFunc child_setup */
                           NULL,         /* gpointer user_data */
                           NULL,         /* gchar **standard_output */
                           NULL,         /* gchar **standard_error */
                           &exit_status, /* gint *exit_status */
                           &g_error)) {  /* GError **error */
                polkit_error_set_error (error, 
                                        POLKIT_ERROR_GENERAL_ERROR, 
                                        "Error spawning explicit grant helper: %s",
                                        g_error->message);
                g_error_free (g_error);
                goto out;
        }

        if (!WIFEXITED (exit_status)) {
                g_warning ("Explicit grant helper crashed!");
                polkit_error_set_error (error, 
                                        POLKIT_ERROR_GENERAL_ERROR, 
                                        "Explicit grant helper crashed!");
                goto out;
        } else if (WEXITSTATUS(exit_status) != 0) {
                polkit_error_set_error (error, 
                                        POLKIT_ERROR_NOT_AUTHORIZED_TO_GRANT_AUTHORIZATION, 
                                        "uid %d is not authorized to grant authorization for action %s to uid %d (requires org.freedesktop.policykit.grant)",
                                        getuid (), cd.action_id, uid);
        } else {
                ret = TRUE;
        }
        
out:
        g_free (helper_argv[4]);
        return ret;
}

/**
 * polkit_authorization_db_grant_to_uid:
 * @authdb: authorization database
 * @action: action
 * @uid: uid to grant to
 * @constraints: Either %NULL or a %NULL terminated list of constraint to put on the authorization
 * @error: return location for error
 *
 * Grants an authorization to a user for a specific action. This
 * requires the org.freedesktop.policykit.grant authorization.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if the authorization was granted, #FALSE otherwise
 * and error will be set
 *
 * Since: 0.7
 */
polkit_bool_t 
polkit_authorization_db_grant_to_uid (PolKitAuthorizationDB          *authdb,
                                      PolKitAction                   *action,
                                      uid_t                           uid,
                                      PolKitAuthorizationConstraint **constraints,
                                      PolKitError                   **error)
{
        return _grant_internal (authdb, action, uid, constraints, error, FALSE);
}

/**
 * polkit_authorization_db_grant_negative_to_uid:
 * @authdb: authorization database
 * @action: action
 * @uid: uid to grant to
 * @constraints: Either %NULL or a %NULL terminated list of constraint to put on the authorization
 * @error: return location for error
 *
 * Grants a negative authorization to a user for a specific action. If
 * @uid differs from the calling user, the
 * org.freedesktop.policykit.grant authorization is required. In other
 * words, users may "grant" negative authorizations to themselves.
 *
 * A negative authorization is normally used to block users that would
 * normally be authorized from an implicit authorization.
 *
 * This function is in <literal>libpolkit-grant</literal>.
 *
 * Returns: #TRUE if the authorization was granted, #FALSE otherwise
 * and error will be set
 *
 * Since: 0.7
 */
polkit_bool_t
polkit_authorization_db_grant_negative_to_uid           (PolKitAuthorizationDB          *authdb,
                                                         PolKitAction                   *action,
                                                         uid_t                           uid,
                                                         PolKitAuthorizationConstraint **constraints,
                                                         PolKitError                   **error)
{
        return _grant_internal (authdb, action, uid, constraints, error, TRUE);
}