1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <sys/utsname.h>
10 #include "alloc-util.h"
11 #include "apparmor-util.h"
12 #include "architecture.h"
13 #include "audit-util.h"
14 #include "cgroup-util.h"
15 #include "condition.h"
16 #include "hostname-util.h"
17 #include "id128-util.h"
21 #include "selinux-util.h"
23 #include "smack-util.h"
24 #include "string-util.h"
26 #include "user-util.h"
30 static void test_condition_test_path(void) {
33 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/sh", false, false);
35 assert_se(condition_test(condition
));
36 condition_free(condition
);
38 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/s?", false, false);
40 assert_se(!condition_test(condition
));
41 condition_free(condition
);
43 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, false);
45 assert_se(condition_test(condition
));
46 condition_free(condition
);
48 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, true);
50 assert_se(!condition_test(condition
));
51 condition_free(condition
);
53 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, false);
55 assert_se(!condition_test(condition
));
56 condition_free(condition
);
58 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, true);
60 assert_se(condition_test(condition
));
61 condition_free(condition
);
63 condition
= condition_new(CONDITION_PATH_IS_DIRECTORY
, "/bin", false, false);
65 assert_se(condition_test(condition
));
66 condition_free(condition
);
68 condition
= condition_new(CONDITION_DIRECTORY_NOT_EMPTY
, "/bin", false, false);
70 assert_se(condition_test(condition
));
71 condition_free(condition
);
73 condition
= condition_new(CONDITION_FILE_NOT_EMPTY
, "/bin/sh", false, false);
75 assert_se(condition_test(condition
));
76 condition_free(condition
);
78 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/bin/sh", false, false);
80 assert_se(condition_test(condition
));
81 condition_free(condition
);
83 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/etc/passwd", false, false);
85 assert_se(!condition_test(condition
));
86 condition_free(condition
);
88 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/proc", false, false);
90 assert_se(condition_test(condition
));
91 condition_free(condition
);
93 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/", false, false);
95 assert_se(condition_test(condition
));
96 condition_free(condition
);
98 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/bin", false, false);
100 assert_se(!condition_test(condition
));
101 condition_free(condition
);
103 condition
= condition_new(CONDITION_PATH_IS_READ_WRITE
, "/tmp", false, false);
104 assert_se(condition
);
105 assert_se(condition_test(condition
));
106 condition_free(condition
);
108 condition
= condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK
, "/dev/stdout", false, false);
109 assert_se(condition
);
110 assert_se(condition_test(condition
));
111 condition_free(condition
);
114 static int test_condition_test_control_group_controller(void) {
115 Condition
*condition
;
116 CGroupMask system_mask
;
117 CGroupController controller
;
118 _cleanup_free_
char *controller_name
= NULL
;
121 r
= cg_unified_flush();
123 log_notice_errno(r
, "Skipping ConditionControlGroupController tests: %m");
124 return EXIT_TEST_SKIP
;
127 /* Invalid controllers are ignored */
128 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "thisisnotarealcontroller", false, false);
129 assert_se(condition
);
130 assert_se(condition_test(condition
));
131 condition_free(condition
);
133 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "thisisnotarealcontroller", false, true);
134 assert_se(condition
);
135 assert_se(!condition_test(condition
));
136 condition_free(condition
);
138 assert_se(cg_mask_supported(&system_mask
) >= 0);
140 /* Individual valid controllers one by one */
141 for (controller
= 0; controller
< _CGROUP_CONTROLLER_MAX
; controller
++) {
142 const char *local_controller_name
= cgroup_controller_to_string(controller
);
143 log_info("chosen controller is '%s'", local_controller_name
);
144 if (system_mask
& CGROUP_CONTROLLER_TO_MASK(controller
)) {
145 log_info("this controller is available");
146 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, false);
147 assert_se(condition
);
148 assert_se(condition_test(condition
));
149 condition_free(condition
);
151 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, true);
152 assert_se(condition
);
153 assert_se(!condition_test(condition
));
154 condition_free(condition
);
156 log_info("this controller is unavailable");
157 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, false);
158 assert_se(condition
);
159 assert_se(!condition_test(condition
));
160 condition_free(condition
);
162 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, true);
163 assert_se(condition
);
164 assert_se(condition_test(condition
));
165 condition_free(condition
);
169 /* Multiple valid controllers at the same time */
170 assert_se(cg_mask_to_string(system_mask
, &controller_name
) >= 0);
172 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, strempty(controller_name
), false, false);
173 assert_se(condition
);
174 assert_se(condition_test(condition
));
175 condition_free(condition
);
177 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, strempty(controller_name
), false, true);
178 assert_se(condition
);
179 assert_se(!condition_test(condition
));
180 condition_free(condition
);
185 static void test_condition_test_ac_power(void) {
186 Condition
*condition
;
188 condition
= condition_new(CONDITION_AC_POWER
, "true", false, false);
189 assert_se(condition
);
190 assert_se(condition_test(condition
) == on_ac_power());
191 condition_free(condition
);
193 condition
= condition_new(CONDITION_AC_POWER
, "false", false, false);
194 assert_se(condition
);
195 assert_se(condition_test(condition
) != on_ac_power());
196 condition_free(condition
);
198 condition
= condition_new(CONDITION_AC_POWER
, "false", false, true);
199 assert_se(condition
);
200 assert_se(condition_test(condition
) == on_ac_power());
201 condition_free(condition
);
204 static void test_condition_test_host(void) {
205 _cleanup_free_
char *hostname
= NULL
;
206 char sid
[SD_ID128_STRING_MAX
];
207 Condition
*condition
;
211 r
= sd_id128_get_machine(&id
);
213 assert_se(sd_id128_to_string(id
, sid
));
215 condition
= condition_new(CONDITION_HOST
, sid
, false, false);
216 assert_se(condition
);
217 assert_se(condition_test(condition
));
218 condition_free(condition
);
220 condition
= condition_new(CONDITION_HOST
, "garbage value jjjjjjjjjjjjjj", false, false);
221 assert_se(condition
);
222 assert_se(!condition_test(condition
));
223 condition_free(condition
);
225 condition
= condition_new(CONDITION_HOST
, sid
, false, true);
226 assert_se(condition
);
227 assert_se(!condition_test(condition
));
228 condition_free(condition
);
230 hostname
= gethostname_malloc();
233 /* if hostname looks like an id128 then skip testing it */
234 if (id128_is_valid(hostname
))
235 log_notice("hostname is an id128, skipping test");
237 condition
= condition_new(CONDITION_HOST
, hostname
, false, false);
238 assert_se(condition
);
239 assert_se(condition_test(condition
));
240 condition_free(condition
);
244 static void test_condition_test_architecture(void) {
245 Condition
*condition
;
249 a
= uname_architecture();
252 sa
= architecture_to_string(a
);
255 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, false);
256 assert_se(condition
);
257 assert_se(condition_test(condition
) > 0);
258 condition_free(condition
);
260 condition
= condition_new(CONDITION_ARCHITECTURE
, "garbage value", false, false);
261 assert_se(condition
);
262 assert_se(condition_test(condition
) == 0);
263 condition_free(condition
);
265 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, true);
266 assert_se(condition
);
267 assert_se(condition_test(condition
) == 0);
268 condition_free(condition
);
271 static void test_condition_test_kernel_command_line(void) {
272 Condition
*condition
;
274 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "thisreallyshouldntbeonthekernelcommandline", false, false);
275 assert_se(condition
);
276 assert_se(!condition_test(condition
));
277 condition_free(condition
);
279 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "andthis=neither", false, false);
280 assert_se(condition
);
281 assert_se(!condition_test(condition
));
282 condition_free(condition
);
285 static void test_condition_test_kernel_version(void) {
286 Condition
*condition
;
290 condition
= condition_new(CONDITION_KERNEL_VERSION
, "*thisreallyshouldntbeinthekernelversion*", false, false);
291 assert_se(condition
);
292 assert_se(!condition_test(condition
));
293 condition_free(condition
);
295 condition
= condition_new(CONDITION_KERNEL_VERSION
, "*", false, false);
296 assert_se(condition
);
297 assert_se(condition_test(condition
));
298 condition_free(condition
);
300 condition
= condition_new(CONDITION_KERNEL_VERSION
, "", false, false);
301 assert_se(condition
);
302 assert_se(!condition_test(condition
));
303 condition_free(condition
);
305 assert_se(uname(&u
) >= 0);
307 condition
= condition_new(CONDITION_KERNEL_VERSION
, u
.release
, false, false);
308 assert_se(condition
);
309 assert_se(condition_test(condition
));
310 condition_free(condition
);
312 strshorten(u
.release
, 4);
313 strcpy(strchr(u
.release
, 0), "*");
315 condition
= condition_new(CONDITION_KERNEL_VERSION
, u
.release
, false, false);
316 assert_se(condition
);
317 assert_se(condition_test(condition
));
318 condition_free(condition
);
320 /* 0.1.2 would be a very very very old kernel */
321 condition
= condition_new(CONDITION_KERNEL_VERSION
, "> 0.1.2", false, false);
322 assert_se(condition
);
323 assert_se(condition_test(condition
));
324 condition_free(condition
);
326 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">= 0.1.2", false, false);
327 assert_se(condition
);
328 assert_se(condition_test(condition
));
329 condition_free(condition
);
331 condition
= condition_new(CONDITION_KERNEL_VERSION
, "< 0.1.2", false, false);
332 assert_se(condition
);
333 assert_se(!condition_test(condition
));
334 condition_free(condition
);
336 condition
= condition_new(CONDITION_KERNEL_VERSION
, "<= 0.1.2", false, false);
337 assert_se(condition
);
338 assert_se(!condition_test(condition
));
339 condition_free(condition
);
341 condition
= condition_new(CONDITION_KERNEL_VERSION
, "= 0.1.2", false, false);
342 assert_se(condition
);
343 assert_se(!condition_test(condition
));
344 condition_free(condition
);
346 /* 4711.8.15 is a very very very future kernel */
347 condition
= condition_new(CONDITION_KERNEL_VERSION
, "< 4711.8.15", false, false);
348 assert_se(condition
);
349 assert_se(condition_test(condition
));
350 condition_free(condition
);
352 condition
= condition_new(CONDITION_KERNEL_VERSION
, "<= 4711.8.15", false, false);
353 assert_se(condition
);
354 assert_se(condition_test(condition
));
355 condition_free(condition
);
357 condition
= condition_new(CONDITION_KERNEL_VERSION
, "= 4711.8.15", false, false);
358 assert_se(condition
);
359 assert_se(!condition_test(condition
));
360 condition_free(condition
);
362 condition
= condition_new(CONDITION_KERNEL_VERSION
, "> 4711.8.15", false, false);
363 assert_se(condition
);
364 assert_se(!condition_test(condition
));
365 condition_free(condition
);
367 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">= 4711.8.15", false, false);
368 assert_se(condition
);
369 assert_se(!condition_test(condition
));
370 condition_free(condition
);
372 assert_se(uname(&u
) >= 0);
374 v
= strjoina(">=", u
.release
);
375 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
376 assert_se(condition
);
377 assert_se(condition_test(condition
));
378 condition_free(condition
);
380 v
= strjoina("= ", u
.release
);
381 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
382 assert_se(condition
);
383 assert_se(condition_test(condition
));
384 condition_free(condition
);
386 v
= strjoina("<=", u
.release
);
387 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
388 assert_se(condition
);
389 assert_se(condition_test(condition
));
390 condition_free(condition
);
392 v
= strjoina("> ", u
.release
);
393 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
394 assert_se(condition
);
395 assert_se(!condition_test(condition
));
396 condition_free(condition
);
398 v
= strjoina("< ", u
.release
);
399 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
400 assert_se(condition
);
401 assert_se(!condition_test(condition
));
402 condition_free(condition
);
405 static void test_condition_test_null(void) {
406 Condition
*condition
;
408 condition
= condition_new(CONDITION_NULL
, NULL
, false, false);
409 assert_se(condition
);
410 assert_se(condition_test(condition
));
411 condition_free(condition
);
413 condition
= condition_new(CONDITION_NULL
, NULL
, false, true);
414 assert_se(condition
);
415 assert_se(!condition_test(condition
));
416 condition_free(condition
);
419 static void test_condition_test_security(void) {
420 Condition
*condition
;
422 condition
= condition_new(CONDITION_SECURITY
, "garbage oifdsjfoidsjoj", false, false);
423 assert_se(condition
);
424 assert_se(!condition_test(condition
));
425 condition_free(condition
);
427 condition
= condition_new(CONDITION_SECURITY
, "selinux", false, true);
428 assert_se(condition
);
429 assert_se(condition_test(condition
) != mac_selinux_use());
430 condition_free(condition
);
432 condition
= condition_new(CONDITION_SECURITY
, "ima", false, false);
433 assert_se(condition
);
434 assert_se(condition_test(condition
) == use_ima());
435 condition_free(condition
);
437 condition
= condition_new(CONDITION_SECURITY
, "apparmor", false, false);
438 assert_se(condition
);
439 assert_se(condition_test(condition
) == mac_apparmor_use());
440 condition_free(condition
);
442 condition
= condition_new(CONDITION_SECURITY
, "smack", false, false);
443 assert_se(condition
);
444 assert_se(condition_test(condition
) == mac_smack_use());
445 condition_free(condition
);
447 condition
= condition_new(CONDITION_SECURITY
, "audit", false, false);
448 assert_se(condition
);
449 assert_se(condition_test(condition
) == use_audit());
450 condition_free(condition
);
453 static void test_condition_test_virtualization(void) {
454 Condition
*condition
;
458 condition
= condition_new(CONDITION_VIRTUALIZATION
, "garbage oifdsjfoidsjoj", false, false);
459 assert_se(condition
);
460 r
= condition_test(condition
);
461 log_info("ConditionVirtualization=garbage → %i", r
);
463 condition_free(condition
);
465 condition
= condition_new(CONDITION_VIRTUALIZATION
, "container", false, false);
466 assert_se(condition
);
467 r
= condition_test(condition
);
468 log_info("ConditionVirtualization=container → %i", r
);
469 assert_se(r
== !!detect_container());
470 condition_free(condition
);
472 condition
= condition_new(CONDITION_VIRTUALIZATION
, "vm", false, false);
473 assert_se(condition
);
474 r
= condition_test(condition
);
475 log_info("ConditionVirtualization=vm → %i", r
);
476 assert_se(r
== (detect_vm() && !detect_container()));
477 condition_free(condition
);
479 condition
= condition_new(CONDITION_VIRTUALIZATION
, "private-users", false, false);
480 assert_se(condition
);
481 r
= condition_test(condition
);
482 log_info("ConditionVirtualization=private-users → %i", r
);
483 assert_se(r
== !!running_in_userns());
484 condition_free(condition
);
500 condition
= condition_new(CONDITION_VIRTUALIZATION
, virt
, false, false);
501 assert_se(condition
);
502 r
= condition_test(condition
);
503 log_info("ConditionVirtualization=%s → %i", virt
, r
);
505 condition_free(condition
);
509 static void test_condition_test_user(void) {
510 Condition
*condition
;
515 condition
= condition_new(CONDITION_USER
, "garbage oifdsjfoidsjoj", false, false);
516 assert_se(condition
);
517 r
= condition_test(condition
);
518 log_info("ConditionUser=garbage → %i", r
);
520 condition_free(condition
);
522 assert_se(asprintf(&uid
, "%"PRIu32
, UINT32_C(0xFFFF)) > 0);
523 condition
= condition_new(CONDITION_USER
, uid
, false, false);
524 assert_se(condition
);
525 r
= condition_test(condition
);
526 log_info("ConditionUser=%s → %i", uid
, r
);
528 condition_free(condition
);
531 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()) > 0);
532 condition
= condition_new(CONDITION_USER
, uid
, false, false);
533 assert_se(condition
);
534 r
= condition_test(condition
);
535 log_info("ConditionUser=%s → %i", uid
, r
);
537 condition_free(condition
);
540 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()+1) > 0);
541 condition
= condition_new(CONDITION_USER
, uid
, false, false);
542 assert_se(condition
);
543 r
= condition_test(condition
);
544 log_info("ConditionUser=%s → %i", uid
, r
);
546 condition_free(condition
);
549 username
= getusername_malloc();
551 condition
= condition_new(CONDITION_USER
, username
, false, false);
552 assert_se(condition
);
553 r
= condition_test(condition
);
554 log_info("ConditionUser=%s → %i", username
, r
);
556 condition_free(condition
);
559 username
= (char*)(geteuid() == 0 ? NOBODY_USER_NAME
: "root");
560 condition
= condition_new(CONDITION_USER
, username
, false, false);
561 assert_se(condition
);
562 r
= condition_test(condition
);
563 log_info("ConditionUser=%s → %i", username
, r
);
565 condition_free(condition
);
567 condition
= condition_new(CONDITION_USER
, "@system", false, false);
568 assert_se(condition
);
569 r
= condition_test(condition
);
570 log_info("ConditionUser=@system → %i", r
);
571 if (uid_is_system(getuid()) || uid_is_system(geteuid()))
575 condition_free(condition
);
578 static void test_condition_test_group(void) {
579 Condition
*condition
;
582 gid_t
*gids
, max_gid
;
583 int ngroups_max
, ngroups
, r
, i
;
585 assert_se(0 < asprintf(&gid
, "%u", UINT32_C(0xFFFF)));
586 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
587 assert_se(condition
);
588 r
= condition_test(condition
);
589 log_info("ConditionGroup=%s → %i", gid
, r
);
591 condition_free(condition
);
594 assert_se(0 < asprintf(&gid
, "%u", getgid()));
595 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
596 assert_se(condition
);
597 r
= condition_test(condition
);
598 log_info("ConditionGroup=%s → %i", gid
, r
);
600 condition_free(condition
);
603 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
604 assert(ngroups_max
> 0);
606 gids
= newa(gid_t
, ngroups_max
);
608 ngroups
= getgroups(ngroups_max
, gids
);
609 assert(ngroups
>= 0);
612 for (i
= 0; i
< ngroups
; i
++) {
613 assert_se(0 < asprintf(&gid
, "%u", gids
[i
]));
614 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
615 assert_se(condition
);
616 r
= condition_test(condition
);
617 log_info("ConditionGroup=%s → %i", gid
, r
);
619 condition_free(condition
);
621 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
623 groupname
= gid_to_name(gids
[i
]);
624 assert_se(groupname
);
625 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
626 assert_se(condition
);
627 r
= condition_test(condition
);
628 log_info("ConditionGroup=%s → %i", groupname
, r
);
630 condition_free(condition
);
632 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
635 assert_se(0 < asprintf(&gid
, "%u", max_gid
+1));
636 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
637 assert_se(condition
);
638 r
= condition_test(condition
);
639 log_info("ConditionGroup=%s → %i", gid
, r
);
641 condition_free(condition
);
644 groupname
= (char*)(geteuid() == 0 ? NOBODY_GROUP_NAME
: "root");
645 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
646 assert_se(condition
);
647 r
= condition_test(condition
);
648 log_info("ConditionGroup=%s → %i", groupname
, r
);
650 condition_free(condition
);
653 int main(int argc
, char *argv
[]) {
654 log_set_max_level(LOG_DEBUG
);
655 log_parse_environment();
658 test_condition_test_path();
659 test_condition_test_ac_power();
660 test_condition_test_host();
661 test_condition_test_architecture();
662 test_condition_test_kernel_command_line();
663 test_condition_test_kernel_version();
664 test_condition_test_null();
665 test_condition_test_security();
666 test_condition_test_virtualization();
667 test_condition_test_user();
668 test_condition_test_group();
669 test_condition_test_control_group_controller();