chiark / gitweb /
bus-policy: add test utility
[elogind.git] / src / bus-proxyd / test-bus-policy.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Daniel Mack
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/poll.h>
30 #include <stddef.h>
31 #include <getopt.h>
32
33 #include "log.h"
34 #include "util.h"
35 #include "sd-bus.h"
36 #include "bus-internal.h"
37 #include "bus-message.h"
38 #include "bus-util.h"
39 #include "bus-internal.h"
40 #include "build.h"
41 #include "strv.h"
42 #include "def.h"
43 #include "capability.h"
44
45 #include <bus-proxyd/bus-policy.h>
46
47 static int make_name_request(sd_bus *bus,
48                              const char *name,
49                              sd_bus_message **ret) {
50
51         int r;
52         sd_bus_message *m = NULL;
53
54         r = sd_bus_message_new_method_call(bus, &m, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName");
55         if (r < 0)
56                 return r;
57
58         r = sd_bus_message_append_basic(m, 's', name);
59         if (r < 0)
60                 return r;
61
62         m->sealed = 1;
63         sd_bus_message_rewind(m, true);
64
65         *ret = m;
66         return 0;
67 }
68
69 int main(int argc, char *argv[]) {
70
71         Policy p = {};
72         sd_bus_message *m;
73         struct ucred ucred = {};
74         _cleanup_bus_close_unref_ sd_bus *bus = NULL;;
75
76         assert_se(sd_bus_default_system(&bus) >= 0);
77
78         /* Fake pid for policy checks */
79         ucred.pid = 1;
80
81         /* Ownership tests */
82         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/ownerships.conf")) == 0);
83
84         assert_se(make_name_request(bus, "org.test.test1", &m) == 0);
85         ucred.uid = 0;
86         assert_se(policy_check(&p, m, &ucred) == true);
87         ucred.uid = 1;
88         assert_se(policy_check(&p, m, &ucred) == true);
89         assert_se(sd_bus_message_unref(m) == 0);
90
91         assert_se(make_name_request(bus, "org.test.test2", &m) == 0);
92         ucred.uid = 0;
93         assert_se(policy_check(&p, m, &ucred) == true);
94         ucred.uid = 1;
95         assert_se(policy_check(&p, m, &ucred) == false);
96         assert_se(sd_bus_message_unref(m) == 0);
97
98         assert_se(make_name_request(bus, "org.test.test3", &m) == 0);
99         ucred.uid = 0;
100         assert_se(policy_check(&p, m, &ucred) == false);
101         ucred.uid = 1;
102         assert_se(policy_check(&p, m, &ucred) == false);
103         assert_se(sd_bus_message_unref(m) == 0);
104
105         assert_se(make_name_request(bus, "org.test.test4", &m) == 0);
106         ucred.uid = 0;
107         assert_se(policy_check(&p, m, &ucred) == false);
108         ucred.uid = 1;
109         assert_se(policy_check(&p, m, &ucred) == true);
110         assert_se(sd_bus_message_unref(m) == 0);
111
112         policy_free(&p);
113
114         /* Signal test */
115         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/signals.conf")) == 0);
116
117         assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "bli.bla.blubb", "Name") == 0);
118         ucred.uid = 0;
119         assert_se(policy_check(&p, m, &ucred) == true);
120
121         ucred.uid = 1;
122         assert_se(policy_check(&p, m, &ucred) == false);
123         assert_se(sd_bus_message_unref(m) == 0);
124
125         policy_free(&p);
126
127         /* Method calls */
128         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/methods.conf")) == 0);
129
130         ucred.uid = 0;
131         assert_se(sd_bus_message_new_method_call(bus, &m, "org.foo.bar", "/an/object/path", "bli.bla.blubb", "Member") == 0);
132         assert_se(policy_check(&p, m, &ucred) == false);
133
134         assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == 0);
135         assert_se(policy_check(&p, m, &ucred) == false);
136
137         bus->is_kernel = 1;
138         assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == 0);
139         assert_se(policy_check(&p, m, &ucred) == true);
140
141         assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == 0);
142         assert_se(policy_check(&p, m, &ucred) == true);
143
144         policy_free(&p);
145
146         /* User and groups */
147         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/hello.conf")) == 0);
148         assert_se(sd_bus_message_new_method_call(bus, &m, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "Hello") == 0);
149         policy_dump(&p);
150
151         ucred.uid = 0;
152         assert_se(policy_check(&p, m, &ucred) == true);
153
154         ucred.uid = 1;
155         assert_se(policy_check(&p, m, &ucred) == false);
156
157         ucred.uid = 0;
158         ucred.gid = 1;
159         assert_se(policy_check(&p, m, &ucred) == false);
160
161         policy_free(&p);
162
163
164         return EXIT_SUCCESS;
165 }