summaryrefslogtreecommitdiff
path: root/src/bus-proxyd/test-bus-policy.c
blob: 37e66274f01d9cc61b12d382c4875d7bc3bb7573 (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
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2014 Daniel Mack

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/poll.h>
#include <stddef.h>
#include <getopt.h>

#include "log.h"
#include "util.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-util.h"
#include "bus-internal.h"
#include "build.h"
#include "strv.h"
#include "def.h"
#include "capability.h"

#include <bus-proxyd/bus-policy.h>

int main(int argc, char *argv[]) {

        Policy p = {};
        struct ucred ucred = {};
        char **names_strv;
        Hashmap *names_hash;

        /* Ownership tests */
        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/ownerships.conf")) == 0);

        ucred.uid = 0;
        assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
        ucred.uid = 1;
        assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);

        ucred.uid = 0;
        assert_se(policy_check_own(&p, &ucred, "org.test.test2") == true);
        ucred.uid = 1;
        assert_se(policy_check_own(&p, &ucred, "org.test.test2") == false);

        ucred.uid = 0;
        assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
        ucred.uid = 1;
        assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);

        ucred.uid = 0;
        assert_se(policy_check_own(&p, &ucred, "org.test.test4") == false);
        ucred.uid = 1;
        assert_se(policy_check_own(&p, &ucred, "org.test.test4") == true);

        policy_free(&p);

        /* Signaltest */
        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/signals.conf")) == 0);
        names_strv = STRV_MAKE("bli.bla.blubb");

        ucred.uid = 0;
        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == true);

        ucred.uid = 1;
        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == false);

        policy_free(&p);

        /* Method calls */
        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/methods.conf")) == 0);
        names_strv = STRV_MAKE("org.test.test1");
        policy_dump(&p);

        ucred.uid = 0;

        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int1", "Member") == true);
        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int2", "Member") == true);

        names_hash = hashmap_new(&string_hash_ops);
        assert(names_hash != NULL);
        assert_se(hashmap_put(names_hash, "org.test.test3", NULL) >= 0);
        assert_se(policy_check_recv(&p, &ucred, names_hash, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int3", "Member111") == true);

        policy_free(&p);

        /* User and groups */
        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/hello.conf")) == 0);
        policy_dump(&p);

        ucred.uid = 0;
        assert_se(policy_check_hello(&p, &ucred) == true);

        ucred.uid = 1;
        assert_se(policy_check_hello(&p, &ucred) == false);

        ucred.uid = 0;
        ucred.gid = 1;
        assert_se(policy_check_hello(&p, &ucred) == false);

        policy_free(&p);

        return EXIT_SUCCESS;
}