2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
26 #include "alloc-util.h"
27 #include "apparmor-util.h"
28 #include "architecture.h"
29 #include "audit-util.h"
30 #include "condition.h"
31 #include "hostname-util.h"
32 #include "id128-util.h"
36 #include "selinux-util.h"
37 #include "smack-util.h"
41 #include "user-util.h"
43 static void test_condition_test_path(void) {
46 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/sh", false, false);
48 assert_se(condition_test(condition
));
49 condition_free(condition
);
51 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/s?", false, false);
53 assert_se(!condition_test(condition
));
54 condition_free(condition
);
56 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, false);
58 assert_se(condition_test(condition
));
59 condition_free(condition
);
61 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, true);
63 assert_se(!condition_test(condition
));
64 condition_free(condition
);
66 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, false);
68 assert_se(!condition_test(condition
));
69 condition_free(condition
);
71 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, true);
73 assert_se(condition_test(condition
));
74 condition_free(condition
);
76 condition
= condition_new(CONDITION_PATH_IS_DIRECTORY
, "/bin", false, false);
78 assert_se(condition_test(condition
));
79 condition_free(condition
);
81 condition
= condition_new(CONDITION_DIRECTORY_NOT_EMPTY
, "/bin", false, false);
83 assert_se(condition_test(condition
));
84 condition_free(condition
);
86 condition
= condition_new(CONDITION_FILE_NOT_EMPTY
, "/bin/sh", false, false);
88 assert_se(condition_test(condition
));
89 condition_free(condition
);
91 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/bin/sh", false, false);
93 assert_se(condition_test(condition
));
94 condition_free(condition
);
96 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/etc/passwd", false, false);
98 assert_se(!condition_test(condition
));
99 condition_free(condition
);
101 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/proc", false, false);
102 assert_se(condition
);
103 assert_se(condition_test(condition
));
104 condition_free(condition
);
106 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/", false, false);
107 assert_se(condition
);
108 assert_se(condition_test(condition
));
109 condition_free(condition
);
111 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/bin", false, false);
112 assert_se(condition
);
113 assert_se(!condition_test(condition
));
114 condition_free(condition
);
116 condition
= condition_new(CONDITION_PATH_IS_READ_WRITE
, "/tmp", false, false);
117 assert_se(condition
);
118 assert_se(condition_test(condition
));
119 condition_free(condition
);
121 condition
= condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK
, "/dev/stdout", false, false);
122 assert_se(condition
);
123 assert_se(condition_test(condition
));
124 condition_free(condition
);
127 static void test_condition_test_ac_power(void) {
128 Condition
*condition
;
130 condition
= condition_new(CONDITION_AC_POWER
, "true", false, false);
131 assert_se(condition
);
132 assert_se(condition_test(condition
) == on_ac_power());
133 condition_free(condition
);
135 condition
= condition_new(CONDITION_AC_POWER
, "false", false, false);
136 assert_se(condition
);
137 assert_se(condition_test(condition
) != on_ac_power());
138 condition_free(condition
);
140 condition
= condition_new(CONDITION_AC_POWER
, "false", false, true);
141 assert_se(condition
);
142 assert_se(condition_test(condition
) == on_ac_power());
143 condition_free(condition
);
146 static void test_condition_test_host(void) {
147 _cleanup_free_
char *hostname
= NULL
;
148 char sid
[SD_ID128_STRING_MAX
];
149 Condition
*condition
;
153 r
= sd_id128_get_machine(&id
);
155 assert_se(sd_id128_to_string(id
, sid
));
157 condition
= condition_new(CONDITION_HOST
, sid
, false, false);
158 assert_se(condition
);
159 assert_se(condition_test(condition
));
160 condition_free(condition
);
162 condition
= condition_new(CONDITION_HOST
, "garbage value jjjjjjjjjjjjjj", false, false);
163 assert_se(condition
);
164 assert_se(!condition_test(condition
));
165 condition_free(condition
);
167 condition
= condition_new(CONDITION_HOST
, sid
, false, true);
168 assert_se(condition
);
169 assert_se(!condition_test(condition
));
170 condition_free(condition
);
172 hostname
= gethostname_malloc();
175 /* if hostname looks like an id128 then skip testing it */
176 if (id128_is_valid(hostname
))
177 log_notice("hostname is an id128, skipping test");
179 condition
= condition_new(CONDITION_HOST
, hostname
, false, false);
180 assert_se(condition
);
181 assert_se(condition_test(condition
));
182 condition_free(condition
);
186 static void test_condition_test_architecture(void) {
187 Condition
*condition
;
191 a
= uname_architecture();
194 sa
= architecture_to_string(a
);
197 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, false);
198 assert_se(condition
);
199 assert_se(condition_test(condition
) > 0);
200 condition_free(condition
);
202 condition
= condition_new(CONDITION_ARCHITECTURE
, "garbage value", false, false);
203 assert_se(condition
);
204 assert_se(condition_test(condition
) == 0);
205 condition_free(condition
);
207 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, true);
208 assert_se(condition
);
209 assert_se(condition_test(condition
) == 0);
210 condition_free(condition
);
213 static void test_condition_test_kernel_command_line(void) {
214 Condition
*condition
;
216 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "thisreallyshouldntbeonthekernelcommandline", false, false);
217 assert_se(condition
);
218 assert_se(!condition_test(condition
));
219 condition_free(condition
);
221 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "andthis=neither", false, false);
222 assert_se(condition
);
223 assert_se(!condition_test(condition
));
224 condition_free(condition
);
227 static void test_condition_test_null(void) {
228 Condition
*condition
;
230 condition
= condition_new(CONDITION_NULL
, NULL
, false, false);
231 assert_se(condition
);
232 assert_se(condition_test(condition
));
233 condition_free(condition
);
235 condition
= condition_new(CONDITION_NULL
, NULL
, false, true);
236 assert_se(condition
);
237 assert_se(!condition_test(condition
));
238 condition_free(condition
);
241 static void test_condition_test_security(void) {
242 Condition
*condition
;
244 condition
= condition_new(CONDITION_SECURITY
, "garbage oifdsjfoidsjoj", false, false);
245 assert_se(condition
);
246 assert_se(!condition_test(condition
));
247 condition_free(condition
);
249 condition
= condition_new(CONDITION_SECURITY
, "selinux", false, true);
250 assert_se(condition
);
251 assert_se(condition_test(condition
) != mac_selinux_use());
252 condition_free(condition
);
254 condition
= condition_new(CONDITION_SECURITY
, "ima", false, false);
255 assert_se(condition
);
256 assert_se(condition_test(condition
) == use_ima());
257 condition_free(condition
);
259 condition
= condition_new(CONDITION_SECURITY
, "apparmor", false, false);
260 assert_se(condition
);
261 assert_se(condition_test(condition
) == mac_apparmor_use());
262 condition_free(condition
);
264 condition
= condition_new(CONDITION_SECURITY
, "smack", false, false);
265 assert_se(condition
);
266 assert_se(condition_test(condition
) == mac_smack_use());
267 condition_free(condition
);
269 condition
= condition_new(CONDITION_SECURITY
, "audit", false, false);
270 assert_se(condition
);
271 assert_se(condition_test(condition
) == use_audit());
272 condition_free(condition
);
275 static void test_condition_test_virtualization(void) {
276 Condition
*condition
;
280 condition
= condition_new(CONDITION_VIRTUALIZATION
, "garbage oifdsjfoidsjoj", false, false);
281 assert_se(condition
);
282 r
= condition_test(condition
);
283 log_info("ConditionVirtualization=garbage → %i", r
);
285 condition_free(condition
);
287 condition
= condition_new(CONDITION_VIRTUALIZATION
, "container", false, false);
288 assert_se(condition
);
289 r
= condition_test(condition
);
290 log_info("ConditionVirtualization=container → %i", r
);
291 assert_se(r
== !!detect_container());
292 condition_free(condition
);
294 condition
= condition_new(CONDITION_VIRTUALIZATION
, "vm", false, false);
295 assert_se(condition
);
296 r
= condition_test(condition
);
297 log_info("ConditionVirtualization=vm → %i", r
);
298 assert_se(r
== (detect_vm() && !detect_container()));
299 condition_free(condition
);
301 condition
= condition_new(CONDITION_VIRTUALIZATION
, "private-users", false, false);
302 assert_se(condition
);
303 r
= condition_test(condition
);
304 log_info("ConditionVirtualization=private-users → %i", r
);
305 assert_se(r
== !!running_in_userns());
306 condition_free(condition
);
322 condition
= condition_new(CONDITION_VIRTUALIZATION
, virt
, false, false);
323 assert_se(condition
);
324 r
= condition_test(condition
);
325 log_info("ConditionVirtualization=%s → %i", virt
, r
);
327 condition_free(condition
);
331 static void test_condition_test_user(void) {
332 Condition
*condition
;
337 condition
= condition_new(CONDITION_USER
, "garbage oifdsjfoidsjoj", false, false);
338 assert_se(condition
);
339 r
= condition_test(condition
);
340 log_info("ConditionUser=garbage → %i", r
);
342 condition_free(condition
);
344 assert_se(asprintf(&uid
, "%"PRIu32
, UINT32_C(0xFFFF)) > 0);
345 condition
= condition_new(CONDITION_USER
, uid
, false, false);
346 assert_se(condition
);
347 r
= condition_test(condition
);
348 log_info("ConditionUser=%s → %i", uid
, r
);
350 condition_free(condition
);
353 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()) > 0);
354 condition
= condition_new(CONDITION_USER
, uid
, false, false);
355 assert_se(condition
);
356 r
= condition_test(condition
);
357 log_info("ConditionUser=%s → %i", uid
, r
);
359 condition_free(condition
);
362 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()+1) > 0);
363 condition
= condition_new(CONDITION_USER
, uid
, false, false);
364 assert_se(condition
);
365 r
= condition_test(condition
);
366 log_info("ConditionUser=%s → %i", uid
, r
);
368 condition_free(condition
);
371 username
= getusername_malloc();
373 condition
= condition_new(CONDITION_USER
, username
, false, false);
374 assert_se(condition
);
375 r
= condition_test(condition
);
376 log_info("ConditionUser=%s → %i", username
, r
);
378 condition_free(condition
);
381 username
= (char*)(geteuid() == 0 ? NOBODY_USER_NAME
: "root");
382 condition
= condition_new(CONDITION_USER
, username
, false, false);
383 assert_se(condition
);
384 r
= condition_test(condition
);
385 log_info("ConditionUser=%s → %i", username
, r
);
387 condition_free(condition
);
389 condition
= condition_new(CONDITION_USER
, "@system", false, false);
390 assert_se(condition
);
391 r
= condition_test(condition
);
392 log_info("ConditionUser=@system → %i", r
);
393 if (getuid() < SYSTEM_UID_MAX
|| geteuid() < SYSTEM_UID_MAX
)
397 condition_free(condition
);
400 static void test_condition_test_group(void) {
401 Condition
*condition
;
404 gid_t
*gids
, max_gid
;
405 int ngroups_max
, r
, i
;
407 assert_se(0 < asprintf(&gid
, "%u", UINT32_C(0xFFFF)));
408 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
409 assert_se(condition
);
410 r
= condition_test(condition
);
411 log_info("ConditionGroup=%s → %i", gid
, r
);
413 condition_free(condition
);
416 assert_se(0 < asprintf(&gid
, "%u", getgid()));
417 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
418 assert_se(condition
);
419 r
= condition_test(condition
);
420 log_info("ConditionGroup=%s → %i", gid
, r
);
422 condition_free(condition
);
425 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
426 assert(ngroups_max
> 0);
428 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
430 r
= getgroups(ngroups_max
, gids
);
434 for (i
= 0; i
< r
; i
++) {
435 assert_se(0 < asprintf(&gid
, "%u", gids
[i
]));
436 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
437 assert_se(condition
);
438 r
= condition_test(condition
);
439 log_info("ConditionGroup=%s → %i", gid
, r
);
441 condition_free(condition
);
443 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
445 groupname
= gid_to_name(gids
[i
]);
446 assert_se(groupname
);
447 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
448 assert_se(condition
);
449 r
= condition_test(condition
);
450 log_info("ConditionGroup=%s → %i", groupname
, r
);
452 condition_free(condition
);
454 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
457 assert_se(0 < asprintf(&gid
, "%u", max_gid
+1));
458 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
459 assert_se(condition
);
460 r
= condition_test(condition
);
461 log_info("ConditionGroup=%s → %i", gid
, r
);
463 condition_free(condition
);
466 groupname
= (char*)(geteuid() == 0 ? NOBODY_GROUP_NAME
: "root");
467 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
468 assert_se(condition
);
469 r
= condition_test(condition
);
470 log_info("ConditionGroup=%s → %i", groupname
, r
);
472 condition_free(condition
);
475 int main(int argc
, char *argv
[]) {
476 log_set_max_level(LOG_DEBUG
);
477 log_parse_environment();
480 test_condition_test_path();
481 test_condition_test_ac_power();
482 test_condition_test_host();
483 test_condition_test_architecture();
484 test_condition_test_kernel_command_line();
485 test_condition_test_null();
486 test_condition_test_security();
487 test_condition_test_virtualization();
488 test_condition_test_user();
489 test_condition_test_group();