1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
11 #include <sys/utsname.h>
17 #include "alloc-util.h"
18 #include "apparmor-util.h"
19 #include "architecture.h"
20 #include "audit-util.h"
22 #include "cgroup-util.h"
23 #include "condition.h"
25 #include "extract-word.h"
28 #include "glob-util.h"
29 #include "hostname-util.h"
33 #include "mountpoint-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "proc-cmdline.h"
38 #include "process-util.h"
39 #include "selinux-util.h"
40 #include "smack-util.h"
41 #include "stat-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
44 #include "tomoyo-util.h"
45 #include "user-util.h"
49 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
54 assert(type
< _CONDITION_TYPE_MAX
);
55 assert((!parameter
) == (type
== CONDITION_NULL
));
57 c
= new0(Condition
, 1);
65 r
= free_and_strdup(&c
->parameter
, parameter
);
73 void condition_free(Condition
*c
) {
80 Condition
* condition_free_list(Condition
*first
) {
83 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
89 static int condition_test_kernel_command_line(Condition
*c
) {
90 _cleanup_free_
char *line
= NULL
;
97 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
99 r
= proc_cmdline(&line
);
103 equal
= strchr(c
->parameter
, '=');
106 _cleanup_free_
char *word
= NULL
;
109 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
116 found
= streq(word
, c
->parameter
);
120 f
= startswith(word
, c
->parameter
);
121 found
= f
&& IN_SET(*f
, 0, '=');
131 static int condition_test_kernel_version(Condition
*c
) {
133 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
134 * should be listed first. */
143 static const char *const prefix
[_ORDER_MAX
] = {
144 [LOWER_OR_EQUAL
] = "<=",
145 [GREATER_OR_EQUAL
] = ">=",
150 const char *p
= NULL
;
156 assert(c
->parameter
);
157 assert(c
->type
== CONDITION_KERNEL_VERSION
);
159 assert_se(uname(&u
) >= 0);
161 for (i
= 0; i
< _ORDER_MAX
; i
++) {
162 p
= startswith(c
->parameter
, prefix
[i
]);
167 /* No prefix? Then treat as glob string */
169 return fnmatch(skip_leading_chars(c
->parameter
, NULL
), u
.release
, 0) == 0;
171 k
= str_verscmp(u
.release
, skip_leading_chars(p
, NULL
));
184 case GREATER_OR_EQUAL
:
191 assert_not_reached("Can't compare");
195 static int condition_test_user(Condition
*c
) {
198 _cleanup_free_
char *username
= NULL
;
202 assert(c
->parameter
);
203 assert(c
->type
== CONDITION_USER
);
205 r
= parse_uid(c
->parameter
, &id
);
207 return id
== getuid() || id
== geteuid();
209 if (streq("@system", c
->parameter
))
210 return uid_is_system(getuid()) || uid_is_system(geteuid());
212 username
= getusername_malloc();
216 if (streq(username
, c
->parameter
))
219 if (getpid_cached() == 1)
220 return streq(c
->parameter
, "root");
223 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
227 return id
== getuid() || id
== geteuid();
230 static int condition_test_control_group_controller(Condition
*c
) {
232 CGroupMask system_mask
, wanted_mask
= 0;
235 assert(c
->parameter
);
236 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
238 r
= cg_mask_supported(&system_mask
);
240 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
242 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
243 if (r
< 0 || wanted_mask
<= 0) {
244 /* This won't catch the case that we have an unknown controller
245 * mixed in with valid ones -- these are only assessed on the
246 * validity of the valid controllers found. */
247 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
251 return FLAGS_SET(system_mask
, wanted_mask
);
254 static int condition_test_group(Condition
*c
) {
259 assert(c
->parameter
);
260 assert(c
->type
== CONDITION_GROUP
);
262 r
= parse_gid(c
->parameter
, &id
);
266 /* Avoid any NSS lookups if we are PID1 */
267 if (getpid_cached() == 1)
268 return streq(c
->parameter
, "root");
270 return in_group(c
->parameter
) > 0;
273 static int condition_test_virtualization(Condition
*c
) {
277 assert(c
->parameter
);
278 assert(c
->type
== CONDITION_VIRTUALIZATION
);
280 if (streq(c
->parameter
, "private-users"))
281 return running_in_userns();
283 v
= detect_virtualization();
287 /* First, compare with yes/no */
288 b
= parse_boolean(c
->parameter
);
292 /* Then, compare categorization */
293 if (streq(c
->parameter
, "vm"))
294 return VIRTUALIZATION_IS_VM(v
);
296 if (streq(c
->parameter
, "container"))
297 return VIRTUALIZATION_IS_CONTAINER(v
);
299 /* Finally compare id */
300 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
303 static int condition_test_architecture(Condition
*c
) {
307 assert(c
->parameter
);
308 assert(c
->type
== CONDITION_ARCHITECTURE
);
310 a
= uname_architecture();
314 if (streq(c
->parameter
, "native"))
315 b
= native_architecture();
317 b
= architecture_from_string(c
->parameter
);
318 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
325 static int condition_test_host(Condition
*c
) {
326 _cleanup_free_
char *h
= NULL
;
331 assert(c
->parameter
);
332 assert(c
->type
== CONDITION_HOST
);
334 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
336 r
= sd_id128_get_machine(&y
);
340 return sd_id128_equal(x
, y
);
343 h
= gethostname_malloc();
347 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
350 static int condition_test_ac_power(Condition
*c
) {
354 assert(c
->parameter
);
355 assert(c
->type
== CONDITION_AC_POWER
);
357 r
= parse_boolean(c
->parameter
);
361 return (on_ac_power() != 0) == !!r
;
364 static int condition_test_security(Condition
*c
) {
366 assert(c
->parameter
);
367 assert(c
->type
== CONDITION_SECURITY
);
369 if (streq(c
->parameter
, "selinux"))
370 return mac_selinux_use();
371 if (streq(c
->parameter
, "smack"))
372 return mac_smack_use();
373 if (streq(c
->parameter
, "apparmor"))
374 return mac_apparmor_use();
375 if (streq(c
->parameter
, "audit"))
377 if (streq(c
->parameter
, "ima"))
379 if (streq(c
->parameter
, "tomoyo"))
380 return mac_tomoyo_use();
381 if (streq(c
->parameter
, "uefi-secureboot"))
382 return is_efi_secure_boot();
387 static int condition_test_capability(Condition
*c
) {
388 unsigned long long capabilities
= (unsigned long long) -1;
389 _cleanup_fclose_
FILE *f
= NULL
;
393 assert(c
->parameter
);
394 assert(c
->type
== CONDITION_CAPABILITY
);
396 /* If it's an invalid capability, we don't have it */
397 value
= capability_from_name(c
->parameter
);
401 /* If it's a valid capability we default to assume
404 f
= fopen("/proc/self/status", "re");
409 _cleanup_free_
char *line
= NULL
;
412 r
= read_line(f
, LONG_LINE_MAX
, &line
);
418 p
= startswith(line
, "CapBnd:");
420 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
427 return !!(capabilities
& (1ULL << value
));
430 static int condition_test_needs_update(Condition
*c
) {
432 struct stat usr
, other
;
435 assert(c
->parameter
);
436 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
438 /* If the file system is read-only we shouldn't suggest an update */
439 if (path_is_read_only_fs(c
->parameter
) > 0)
442 /* Any other failure means we should allow the condition to be true,
443 * so that we rather invoke too many update tools than too
446 if (!path_is_absolute(c
->parameter
))
449 p
= strjoina(c
->parameter
, "/.updated");
450 if (lstat(p
, &other
) < 0)
453 if (lstat("/usr/", &usr
) < 0)
457 * First, compare seconds as they are always accurate...
459 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
460 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
463 * ...then compare nanoseconds.
465 * A false positive is only possible when /usr's nanoseconds > 0
466 * (otherwise /usr cannot be strictly newer than the target file)
467 * AND the target file's nanoseconds == 0
468 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
470 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
471 _cleanup_free_
char *timestamp_str
= NULL
;
475 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
477 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
480 log_debug("No data in timestamp file '%s', using mtime", p
);
484 r
= safe_atou64(timestamp_str
, ×tamp
);
486 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
490 timespec_store(&other
.st_mtim
, timestamp
);
493 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
496 static int condition_test_first_boot(Condition
*c
) {
500 assert(c
->parameter
);
501 assert(c
->type
== CONDITION_FIRST_BOOT
);
503 r
= parse_boolean(c
->parameter
);
507 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
510 static int condition_test_path_exists(Condition
*c
) {
512 assert(c
->parameter
);
513 assert(c
->type
== CONDITION_PATH_EXISTS
);
515 return access(c
->parameter
, F_OK
) >= 0;
518 static int condition_test_path_exists_glob(Condition
*c
) {
520 assert(c
->parameter
);
521 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
523 return glob_exists(c
->parameter
) > 0;
526 static int condition_test_path_is_directory(Condition
*c
) {
528 assert(c
->parameter
);
529 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
531 return is_dir(c
->parameter
, true) > 0;
534 static int condition_test_path_is_symbolic_link(Condition
*c
) {
536 assert(c
->parameter
);
537 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
539 return is_symlink(c
->parameter
) > 0;
542 static int condition_test_path_is_mount_point(Condition
*c
) {
544 assert(c
->parameter
);
545 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
547 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
550 static int condition_test_path_is_read_write(Condition
*c
) {
552 assert(c
->parameter
);
553 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
555 return path_is_read_only_fs(c
->parameter
) <= 0;
558 static int condition_test_directory_not_empty(Condition
*c
) {
562 assert(c
->parameter
);
563 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
565 r
= dir_is_empty(c
->parameter
);
566 return r
<= 0 && r
!= -ENOENT
;
569 static int condition_test_file_not_empty(Condition
*c
) {
573 assert(c
->parameter
);
574 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
576 return (stat(c
->parameter
, &st
) >= 0 &&
577 S_ISREG(st
.st_mode
) &&
581 static int condition_test_file_is_executable(Condition
*c
) {
585 assert(c
->parameter
);
586 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
588 return (stat(c
->parameter
, &st
) >= 0 &&
589 S_ISREG(st
.st_mode
) &&
590 (st
.st_mode
& 0111));
593 static int condition_test_null(Condition
*c
) {
595 assert(c
->type
== CONDITION_NULL
);
597 /* Note that during parsing we already evaluate the string and
598 * store it in c->negate */
602 int condition_test(Condition
*c
) {
604 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
605 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
606 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
607 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
608 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
609 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
610 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
611 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
612 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
613 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
614 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
615 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
616 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
617 [CONDITION_SECURITY
] = condition_test_security
,
618 [CONDITION_CAPABILITY
] = condition_test_capability
,
619 [CONDITION_HOST
] = condition_test_host
,
620 [CONDITION_AC_POWER
] = condition_test_ac_power
,
621 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
622 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
623 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
624 [CONDITION_USER
] = condition_test_user
,
625 [CONDITION_GROUP
] = condition_test_group
,
626 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
627 [CONDITION_NULL
] = condition_test_null
,
633 assert(c
->type
>= 0);
634 assert(c
->type
< _CONDITION_TYPE_MAX
);
636 r
= condition_tests
[c
->type
](c
);
638 c
->result
= CONDITION_ERROR
;
642 b
= (r
> 0) == !c
->negate
;
643 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
647 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
651 prefix
= strempty(prefix
);
654 "%s\t%s: %s%s%s %s\n",
657 c
->trigger
? "|" : "",
658 c
->negate
? "!" : "",
660 condition_result_to_string(c
->result
));
663 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
666 LIST_FOREACH(conditions
, c
, first
)
667 condition_dump(c
, f
, prefix
, to_string
);
670 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
671 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
672 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
673 [CONDITION_HOST
] = "ConditionHost",
674 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
675 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
676 [CONDITION_SECURITY
] = "ConditionSecurity",
677 [CONDITION_CAPABILITY
] = "ConditionCapability",
678 [CONDITION_AC_POWER
] = "ConditionACPower",
679 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
680 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
681 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
682 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
683 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
684 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
685 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
686 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
687 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
688 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
689 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
690 [CONDITION_USER
] = "ConditionUser",
691 [CONDITION_GROUP
] = "ConditionGroup",
692 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
693 [CONDITION_NULL
] = "ConditionNull"
696 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
698 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
699 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
700 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
701 [CONDITION_HOST
] = "AssertHost",
702 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
703 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
704 [CONDITION_SECURITY
] = "AssertSecurity",
705 [CONDITION_CAPABILITY
] = "AssertCapability",
706 [CONDITION_AC_POWER
] = "AssertACPower",
707 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
708 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
709 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
710 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
711 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
712 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
713 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
714 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
715 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
716 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
717 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
718 [CONDITION_USER
] = "AssertUser",
719 [CONDITION_GROUP
] = "AssertGroup",
720 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
721 [CONDITION_NULL
] = "AssertNull"
724 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
726 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
727 [CONDITION_UNTESTED
] = "untested",
728 [CONDITION_SUCCEEDED
] = "succeeded",
729 [CONDITION_FAILED
] = "failed",
730 [CONDITION_ERROR
] = "error",
733 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);