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 "mount-util.h"
34 #include "parse-util.h"
35 #include "path-util.h"
36 #include "proc-cmdline.h"
37 #include "process-util.h"
38 #include "selinux-util.h"
39 #include "smack-util.h"
40 #include "stat-util.h"
41 #include "string-table.h"
42 #include "string-util.h"
43 #include "tomoyo-util.h"
44 #include "user-util.h"
48 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
53 assert(type
< _CONDITION_TYPE_MAX
);
54 assert((!parameter
) == (type
== CONDITION_NULL
));
56 c
= new0(Condition
, 1);
64 r
= free_and_strdup(&c
->parameter
, parameter
);
72 void condition_free(Condition
*c
) {
79 Condition
* condition_free_list(Condition
*first
) {
82 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
88 static int condition_test_kernel_command_line(Condition
*c
) {
89 _cleanup_free_
char *line
= NULL
;
96 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
98 r
= proc_cmdline(&line
);
102 equal
= strchr(c
->parameter
, '=');
105 _cleanup_free_
char *word
= NULL
;
108 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
115 found
= streq(word
, c
->parameter
);
119 f
= startswith(word
, c
->parameter
);
120 found
= f
&& IN_SET(*f
, 0, '=');
130 static int condition_test_kernel_version(Condition
*c
) {
132 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
133 * should be listed first. */
142 static const char *const prefix
[_ORDER_MAX
] = {
143 [LOWER_OR_EQUAL
] = "<=",
144 [GREATER_OR_EQUAL
] = ">=",
149 const char *p
= NULL
;
155 assert(c
->parameter
);
156 assert(c
->type
== CONDITION_KERNEL_VERSION
);
158 assert_se(uname(&u
) >= 0);
160 for (i
= 0; i
< _ORDER_MAX
; i
++) {
161 p
= startswith(c
->parameter
, prefix
[i
]);
166 /* No prefix? Then treat as glob string */
168 return fnmatch(skip_leading_chars(c
->parameter
, NULL
), u
.release
, 0) == 0;
170 k
= str_verscmp(u
.release
, skip_leading_chars(p
, NULL
));
183 case GREATER_OR_EQUAL
:
190 assert_not_reached("Can't compare");
194 static int condition_test_user(Condition
*c
) {
197 _cleanup_free_
char *username
= NULL
;
201 assert(c
->parameter
);
202 assert(c
->type
== CONDITION_USER
);
204 r
= parse_uid(c
->parameter
, &id
);
206 return id
== getuid() || id
== geteuid();
208 if (streq("@system", c
->parameter
))
209 return uid_is_system(getuid()) || uid_is_system(geteuid());
211 username
= getusername_malloc();
215 if (streq(username
, c
->parameter
))
218 if (getpid_cached() == 1)
219 return streq(c
->parameter
, "root");
222 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
226 return id
== getuid() || id
== geteuid();
229 static int condition_test_control_group_controller(Condition
*c
) {
231 CGroupMask system_mask
, wanted_mask
= 0;
234 assert(c
->parameter
);
235 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
237 r
= cg_mask_supported(&system_mask
);
239 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
241 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
242 if (r
< 0 || wanted_mask
<= 0) {
243 /* This won't catch the case that we have an unknown controller
244 * mixed in with valid ones -- these are only assessed on the
245 * validity of the valid controllers found. */
246 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
250 return FLAGS_SET(system_mask
, wanted_mask
);
253 static int condition_test_group(Condition
*c
) {
258 assert(c
->parameter
);
259 assert(c
->type
== CONDITION_GROUP
);
261 r
= parse_gid(c
->parameter
, &id
);
265 /* Avoid any NSS lookups if we are PID1 */
266 if (getpid_cached() == 1)
267 return streq(c
->parameter
, "root");
269 return in_group(c
->parameter
) > 0;
272 static int condition_test_virtualization(Condition
*c
) {
276 assert(c
->parameter
);
277 assert(c
->type
== CONDITION_VIRTUALIZATION
);
279 if (streq(c
->parameter
, "private-users"))
280 return running_in_userns();
282 v
= detect_virtualization();
286 /* First, compare with yes/no */
287 b
= parse_boolean(c
->parameter
);
291 /* Then, compare categorization */
292 if (streq(c
->parameter
, "vm"))
293 return VIRTUALIZATION_IS_VM(v
);
295 if (streq(c
->parameter
, "container"))
296 return VIRTUALIZATION_IS_CONTAINER(v
);
298 /* Finally compare id */
299 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
302 static int condition_test_architecture(Condition
*c
) {
306 assert(c
->parameter
);
307 assert(c
->type
== CONDITION_ARCHITECTURE
);
309 a
= uname_architecture();
313 if (streq(c
->parameter
, "native"))
314 b
= native_architecture();
316 b
= architecture_from_string(c
->parameter
);
317 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
324 static int condition_test_host(Condition
*c
) {
325 _cleanup_free_
char *h
= NULL
;
330 assert(c
->parameter
);
331 assert(c
->type
== CONDITION_HOST
);
333 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
335 r
= sd_id128_get_machine(&y
);
339 return sd_id128_equal(x
, y
);
342 h
= gethostname_malloc();
346 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
349 static int condition_test_ac_power(Condition
*c
) {
353 assert(c
->parameter
);
354 assert(c
->type
== CONDITION_AC_POWER
);
356 r
= parse_boolean(c
->parameter
);
360 return (on_ac_power() != 0) == !!r
;
363 static int condition_test_security(Condition
*c
) {
365 assert(c
->parameter
);
366 assert(c
->type
== CONDITION_SECURITY
);
368 if (streq(c
->parameter
, "selinux"))
369 return mac_selinux_use();
370 if (streq(c
->parameter
, "smack"))
371 return mac_smack_use();
372 if (streq(c
->parameter
, "apparmor"))
373 return mac_apparmor_use();
374 if (streq(c
->parameter
, "audit"))
376 if (streq(c
->parameter
, "ima"))
378 if (streq(c
->parameter
, "tomoyo"))
379 return mac_tomoyo_use();
380 if (streq(c
->parameter
, "uefi-secureboot"))
381 return is_efi_secure_boot();
386 static int condition_test_capability(Condition
*c
) {
387 unsigned long long capabilities
= (unsigned long long) -1;
388 _cleanup_fclose_
FILE *f
= NULL
;
392 assert(c
->parameter
);
393 assert(c
->type
== CONDITION_CAPABILITY
);
395 /* If it's an invalid capability, we don't have it */
396 value
= capability_from_name(c
->parameter
);
400 /* If it's a valid capability we default to assume
403 f
= fopen("/proc/self/status", "re");
408 _cleanup_free_
char *line
= NULL
;
411 r
= read_line(f
, LONG_LINE_MAX
, &line
);
417 p
= startswith(line
, "CapBnd:");
419 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
426 return !!(capabilities
& (1ULL << value
));
429 static int condition_test_needs_update(Condition
*c
) {
431 struct stat usr
, other
;
434 assert(c
->parameter
);
435 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
437 /* If the file system is read-only we shouldn't suggest an update */
438 if (path_is_read_only_fs(c
->parameter
) > 0)
441 /* Any other failure means we should allow the condition to be true,
442 * so that we rather invoke too many update tools than too
445 if (!path_is_absolute(c
->parameter
))
448 p
= strjoina(c
->parameter
, "/.updated");
449 if (lstat(p
, &other
) < 0)
452 if (lstat("/usr/", &usr
) < 0)
456 * First, compare seconds as they are always accurate...
458 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
459 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
462 * ...then compare nanoseconds.
464 * A false positive is only possible when /usr's nanoseconds > 0
465 * (otherwise /usr cannot be strictly newer than the target file)
466 * AND the target file's nanoseconds == 0
467 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
469 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
470 _cleanup_free_
char *timestamp_str
= NULL
;
474 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
476 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
479 log_debug("No data in timestamp file '%s', using mtime", p
);
483 r
= safe_atou64(timestamp_str
, ×tamp
);
485 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
489 timespec_store(&other
.st_mtim
, timestamp
);
492 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
495 static int condition_test_first_boot(Condition
*c
) {
499 assert(c
->parameter
);
500 assert(c
->type
== CONDITION_FIRST_BOOT
);
502 r
= parse_boolean(c
->parameter
);
506 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
509 static int condition_test_path_exists(Condition
*c
) {
511 assert(c
->parameter
);
512 assert(c
->type
== CONDITION_PATH_EXISTS
);
514 return access(c
->parameter
, F_OK
) >= 0;
517 static int condition_test_path_exists_glob(Condition
*c
) {
519 assert(c
->parameter
);
520 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
522 return glob_exists(c
->parameter
) > 0;
525 static int condition_test_path_is_directory(Condition
*c
) {
527 assert(c
->parameter
);
528 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
530 return is_dir(c
->parameter
, true) > 0;
533 static int condition_test_path_is_symbolic_link(Condition
*c
) {
535 assert(c
->parameter
);
536 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
538 return is_symlink(c
->parameter
) > 0;
541 static int condition_test_path_is_mount_point(Condition
*c
) {
543 assert(c
->parameter
);
544 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
546 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
549 static int condition_test_path_is_read_write(Condition
*c
) {
551 assert(c
->parameter
);
552 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
554 return path_is_read_only_fs(c
->parameter
) <= 0;
557 static int condition_test_directory_not_empty(Condition
*c
) {
561 assert(c
->parameter
);
562 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
564 r
= dir_is_empty(c
->parameter
);
565 return r
<= 0 && r
!= -ENOENT
;
568 static int condition_test_file_not_empty(Condition
*c
) {
572 assert(c
->parameter
);
573 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
575 return (stat(c
->parameter
, &st
) >= 0 &&
576 S_ISREG(st
.st_mode
) &&
580 static int condition_test_file_is_executable(Condition
*c
) {
584 assert(c
->parameter
);
585 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
587 return (stat(c
->parameter
, &st
) >= 0 &&
588 S_ISREG(st
.st_mode
) &&
589 (st
.st_mode
& 0111));
592 static int condition_test_null(Condition
*c
) {
594 assert(c
->type
== CONDITION_NULL
);
596 /* Note that during parsing we already evaluate the string and
597 * store it in c->negate */
601 int condition_test(Condition
*c
) {
603 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
604 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
605 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
606 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
607 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
608 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
609 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
610 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
611 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
612 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
613 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
614 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
615 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
616 [CONDITION_SECURITY
] = condition_test_security
,
617 [CONDITION_CAPABILITY
] = condition_test_capability
,
618 [CONDITION_HOST
] = condition_test_host
,
619 [CONDITION_AC_POWER
] = condition_test_ac_power
,
620 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
621 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
622 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
623 [CONDITION_USER
] = condition_test_user
,
624 [CONDITION_GROUP
] = condition_test_group
,
625 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
626 [CONDITION_NULL
] = condition_test_null
,
632 assert(c
->type
>= 0);
633 assert(c
->type
< _CONDITION_TYPE_MAX
);
635 r
= condition_tests
[c
->type
](c
);
637 c
->result
= CONDITION_ERROR
;
641 b
= (r
> 0) == !c
->negate
;
642 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
646 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
650 prefix
= strempty(prefix
);
653 "%s\t%s: %s%s%s %s\n",
656 c
->trigger
? "|" : "",
657 c
->negate
? "!" : "",
659 condition_result_to_string(c
->result
));
662 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
665 LIST_FOREACH(conditions
, c
, first
)
666 condition_dump(c
, f
, prefix
, to_string
);
669 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
670 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
671 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
672 [CONDITION_HOST
] = "ConditionHost",
673 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
674 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
675 [CONDITION_SECURITY
] = "ConditionSecurity",
676 [CONDITION_CAPABILITY
] = "ConditionCapability",
677 [CONDITION_AC_POWER
] = "ConditionACPower",
678 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
679 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
680 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
681 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
682 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
683 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
684 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
685 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
686 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
687 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
688 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
689 [CONDITION_USER
] = "ConditionUser",
690 [CONDITION_GROUP
] = "ConditionGroup",
691 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
692 [CONDITION_NULL
] = "ConditionNull"
695 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
697 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
698 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
699 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
700 [CONDITION_HOST
] = "AssertHost",
701 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
702 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
703 [CONDITION_SECURITY
] = "AssertSecurity",
704 [CONDITION_CAPABILITY
] = "AssertCapability",
705 [CONDITION_AC_POWER
] = "AssertACPower",
706 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
707 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
708 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
709 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
710 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
711 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
712 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
713 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
714 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
715 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
716 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
717 [CONDITION_USER
] = "AssertUser",
718 [CONDITION_GROUP
] = "AssertGroup",
719 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
720 [CONDITION_NULL
] = "AssertNull"
723 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
725 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
726 [CONDITION_UNTESTED
] = "untested",
727 [CONDITION_SUCCEEDED
] = "succeeded",
728 [CONDITION_FAILED
] = "failed",
729 [CONDITION_ERROR
] = "error",
732 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);