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_type(Condition
*first
, ConditionType type
) {
81 Condition
*c
, *n
, *r
= NULL
;
83 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
84 if (type
< 0 || c
->type
== type
)
89 assert(type
>= 0 || !r
);
93 static int condition_test_kernel_command_line(Condition
*c
) {
94 _cleanup_free_
char *line
= NULL
;
100 assert(c
->parameter
);
101 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
103 r
= proc_cmdline(&line
);
107 equal
= strchr(c
->parameter
, '=');
110 _cleanup_free_
char *word
= NULL
;
113 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
120 found
= streq(word
, c
->parameter
);
124 f
= startswith(word
, c
->parameter
);
125 found
= f
&& IN_SET(*f
, 0, '=');
135 static int condition_test_kernel_version(Condition
*c
) {
137 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
138 * should be listed first. */
147 static const char *const prefix
[_ORDER_MAX
] = {
148 [LOWER_OR_EQUAL
] = "<=",
149 [GREATER_OR_EQUAL
] = ">=",
154 const char *p
= NULL
;
160 assert(c
->parameter
);
161 assert(c
->type
== CONDITION_KERNEL_VERSION
);
163 assert_se(uname(&u
) >= 0);
165 for (i
= 0; i
< _ORDER_MAX
; i
++) {
166 p
= startswith(c
->parameter
, prefix
[i
]);
171 /* No prefix? Then treat as glob string */
173 return fnmatch(skip_leading_chars(c
->parameter
, NULL
), u
.release
, 0) == 0;
175 k
= str_verscmp(u
.release
, skip_leading_chars(p
, NULL
));
188 case GREATER_OR_EQUAL
:
195 assert_not_reached("Can't compare");
199 static int condition_test_user(Condition
*c
) {
202 _cleanup_free_
char *username
= NULL
;
206 assert(c
->parameter
);
207 assert(c
->type
== CONDITION_USER
);
209 r
= parse_uid(c
->parameter
, &id
);
211 return id
== getuid() || id
== geteuid();
213 if (streq("@system", c
->parameter
))
214 return uid_is_system(getuid()) || uid_is_system(geteuid());
216 username
= getusername_malloc();
220 if (streq(username
, c
->parameter
))
223 if (getpid_cached() == 1)
224 return streq(c
->parameter
, "root");
227 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
231 return id
== getuid() || id
== geteuid();
234 static int condition_test_control_group_controller(Condition
*c
) {
236 CGroupMask system_mask
, wanted_mask
= 0;
239 assert(c
->parameter
);
240 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
242 r
= cg_mask_supported(&system_mask
);
244 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
246 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
247 if (r
< 0 || wanted_mask
<= 0) {
248 /* This won't catch the case that we have an unknown controller
249 * mixed in with valid ones -- these are only assessed on the
250 * validity of the valid controllers found. */
251 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
255 return FLAGS_SET(system_mask
, wanted_mask
);
258 static int condition_test_group(Condition
*c
) {
263 assert(c
->parameter
);
264 assert(c
->type
== CONDITION_GROUP
);
266 r
= parse_gid(c
->parameter
, &id
);
270 /* Avoid any NSS lookups if we are PID1 */
271 if (getpid_cached() == 1)
272 return streq(c
->parameter
, "root");
274 return in_group(c
->parameter
) > 0;
277 static int condition_test_virtualization(Condition
*c
) {
281 assert(c
->parameter
);
282 assert(c
->type
== CONDITION_VIRTUALIZATION
);
284 if (streq(c
->parameter
, "private-users"))
285 return running_in_userns();
287 v
= detect_virtualization();
291 /* First, compare with yes/no */
292 b
= parse_boolean(c
->parameter
);
296 /* Then, compare categorization */
297 if (streq(c
->parameter
, "vm"))
298 return VIRTUALIZATION_IS_VM(v
);
300 if (streq(c
->parameter
, "container"))
301 return VIRTUALIZATION_IS_CONTAINER(v
);
303 /* Finally compare id */
304 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
307 static int condition_test_architecture(Condition
*c
) {
311 assert(c
->parameter
);
312 assert(c
->type
== CONDITION_ARCHITECTURE
);
314 a
= uname_architecture();
318 if (streq(c
->parameter
, "native"))
319 b
= native_architecture();
321 b
= architecture_from_string(c
->parameter
);
322 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
329 static int condition_test_host(Condition
*c
) {
330 _cleanup_free_
char *h
= NULL
;
335 assert(c
->parameter
);
336 assert(c
->type
== CONDITION_HOST
);
338 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
340 r
= sd_id128_get_machine(&y
);
344 return sd_id128_equal(x
, y
);
347 h
= gethostname_malloc();
351 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
354 static int condition_test_ac_power(Condition
*c
) {
358 assert(c
->parameter
);
359 assert(c
->type
== CONDITION_AC_POWER
);
361 r
= parse_boolean(c
->parameter
);
365 return (on_ac_power() != 0) == !!r
;
368 static int condition_test_security(Condition
*c
) {
370 assert(c
->parameter
);
371 assert(c
->type
== CONDITION_SECURITY
);
373 if (streq(c
->parameter
, "selinux"))
374 return mac_selinux_use();
375 if (streq(c
->parameter
, "smack"))
376 return mac_smack_use();
377 if (streq(c
->parameter
, "apparmor"))
378 return mac_apparmor_use();
379 if (streq(c
->parameter
, "audit"))
381 if (streq(c
->parameter
, "ima"))
383 if (streq(c
->parameter
, "tomoyo"))
384 return mac_tomoyo_use();
385 if (streq(c
->parameter
, "uefi-secureboot"))
386 return is_efi_secure_boot();
391 static int condition_test_capability(Condition
*c
) {
392 unsigned long long capabilities
= (unsigned long long) -1;
393 _cleanup_fclose_
FILE *f
= NULL
;
397 assert(c
->parameter
);
398 assert(c
->type
== CONDITION_CAPABILITY
);
400 /* If it's an invalid capability, we don't have it */
401 value
= capability_from_name(c
->parameter
);
405 /* If it's a valid capability we default to assume
408 f
= fopen("/proc/self/status", "re");
413 _cleanup_free_
char *line
= NULL
;
416 r
= read_line(f
, LONG_LINE_MAX
, &line
);
422 p
= startswith(line
, "CapBnd:");
424 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
431 return !!(capabilities
& (1ULL << value
));
434 static int condition_test_needs_update(Condition
*c
) {
436 struct stat usr
, other
;
439 assert(c
->parameter
);
440 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
442 /* If the file system is read-only we shouldn't suggest an update */
443 if (path_is_read_only_fs(c
->parameter
) > 0)
446 /* Any other failure means we should allow the condition to be true,
447 * so that we rather invoke too many update tools than too
450 if (!path_is_absolute(c
->parameter
))
453 p
= strjoina(c
->parameter
, "/.updated");
454 if (lstat(p
, &other
) < 0)
457 if (lstat("/usr/", &usr
) < 0)
461 * First, compare seconds as they are always accurate...
463 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
464 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
467 * ...then compare nanoseconds.
469 * A false positive is only possible when /usr's nanoseconds > 0
470 * (otherwise /usr cannot be strictly newer than the target file)
471 * AND the target file's nanoseconds == 0
472 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
474 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
475 _cleanup_free_
char *timestamp_str
= NULL
;
479 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
481 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
484 log_debug("No data in timestamp file '%s', using mtime", p
);
488 r
= safe_atou64(timestamp_str
, ×tamp
);
490 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
494 timespec_store(&other
.st_mtim
, timestamp
);
497 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
500 static int condition_test_first_boot(Condition
*c
) {
504 assert(c
->parameter
);
505 assert(c
->type
== CONDITION_FIRST_BOOT
);
507 r
= parse_boolean(c
->parameter
);
511 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
514 static int condition_test_path_exists(Condition
*c
) {
516 assert(c
->parameter
);
517 assert(c
->type
== CONDITION_PATH_EXISTS
);
519 return access(c
->parameter
, F_OK
) >= 0;
522 static int condition_test_path_exists_glob(Condition
*c
) {
524 assert(c
->parameter
);
525 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
527 return glob_exists(c
->parameter
) > 0;
530 static int condition_test_path_is_directory(Condition
*c
) {
532 assert(c
->parameter
);
533 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
535 return is_dir(c
->parameter
, true) > 0;
538 static int condition_test_path_is_symbolic_link(Condition
*c
) {
540 assert(c
->parameter
);
541 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
543 return is_symlink(c
->parameter
) > 0;
546 static int condition_test_path_is_mount_point(Condition
*c
) {
548 assert(c
->parameter
);
549 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
551 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
554 static int condition_test_path_is_read_write(Condition
*c
) {
556 assert(c
->parameter
);
557 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
559 return path_is_read_only_fs(c
->parameter
) <= 0;
562 static int condition_test_directory_not_empty(Condition
*c
) {
566 assert(c
->parameter
);
567 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
569 r
= dir_is_empty(c
->parameter
);
570 return r
<= 0 && r
!= -ENOENT
;
573 static int condition_test_file_not_empty(Condition
*c
) {
577 assert(c
->parameter
);
578 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
580 return (stat(c
->parameter
, &st
) >= 0 &&
581 S_ISREG(st
.st_mode
) &&
585 static int condition_test_file_is_executable(Condition
*c
) {
589 assert(c
->parameter
);
590 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
592 return (stat(c
->parameter
, &st
) >= 0 &&
593 S_ISREG(st
.st_mode
) &&
594 (st
.st_mode
& 0111));
597 static int condition_test_null(Condition
*c
) {
599 assert(c
->type
== CONDITION_NULL
);
601 /* Note that during parsing we already evaluate the string and
602 * store it in c->negate */
606 int condition_test(Condition
*c
) {
608 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
609 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
610 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
611 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
612 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
613 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
614 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
615 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
616 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
617 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
618 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
619 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
620 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
621 [CONDITION_SECURITY
] = condition_test_security
,
622 [CONDITION_CAPABILITY
] = condition_test_capability
,
623 [CONDITION_HOST
] = condition_test_host
,
624 [CONDITION_AC_POWER
] = condition_test_ac_power
,
625 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
626 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
627 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
628 [CONDITION_USER
] = condition_test_user
,
629 [CONDITION_GROUP
] = condition_test_group
,
630 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
631 [CONDITION_NULL
] = condition_test_null
,
637 assert(c
->type
>= 0);
638 assert(c
->type
< _CONDITION_TYPE_MAX
);
640 r
= condition_tests
[c
->type
](c
);
642 c
->result
= CONDITION_ERROR
;
646 b
= (r
> 0) == !c
->negate
;
647 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
651 bool condition_test_list(Condition
*first
, const char *(*to_string
)(ConditionType t
), condition_test_logger_t logger
, void *userdata
) {
655 assert(!!logger
== !!to_string
);
657 /* If the condition list is empty, then it is true */
661 /* Otherwise, if all of the non-trigger conditions apply and
662 * if any of the trigger conditions apply (unless there are
663 * none) we return true */
664 LIST_FOREACH(conditions
, c
, first
) {
667 r
= condition_test(c
);
671 logger(userdata
, LOG_WARNING
, r
, __FILE__
, __LINE__
, __func__
,
672 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
674 c
->trigger
? "|" : "",
675 c
->negate
? "!" : "",
678 logger(userdata
, LOG_DEBUG
, 0, __FILE__
, __LINE__
, __func__
,
681 c
->trigger
? "|" : "",
682 c
->negate
? "!" : "",
684 condition_result_to_string(c
->result
));
687 if (!c
->trigger
&& r
<= 0)
690 if (c
->trigger
&& triggered
<= 0)
694 return triggered
!= 0;
697 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
701 prefix
= strempty(prefix
);
704 "%s\t%s: %s%s%s %s\n",
707 c
->trigger
? "|" : "",
708 c
->negate
? "!" : "",
710 condition_result_to_string(c
->result
));
713 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
716 LIST_FOREACH(conditions
, c
, first
)
717 condition_dump(c
, f
, prefix
, to_string
);
720 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
721 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
722 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
723 [CONDITION_HOST
] = "ConditionHost",
724 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
725 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
726 [CONDITION_SECURITY
] = "ConditionSecurity",
727 [CONDITION_CAPABILITY
] = "ConditionCapability",
728 [CONDITION_AC_POWER
] = "ConditionACPower",
729 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
730 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
731 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
732 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
733 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
734 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
735 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
736 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
737 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
738 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
739 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
740 [CONDITION_USER
] = "ConditionUser",
741 [CONDITION_GROUP
] = "ConditionGroup",
742 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
743 [CONDITION_NULL
] = "ConditionNull"
746 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
748 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
749 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
750 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
751 [CONDITION_HOST
] = "AssertHost",
752 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
753 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
754 [CONDITION_SECURITY
] = "AssertSecurity",
755 [CONDITION_CAPABILITY
] = "AssertCapability",
756 [CONDITION_AC_POWER
] = "AssertACPower",
757 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
758 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
759 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
760 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
761 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
762 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
763 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
764 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
765 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
766 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
767 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
768 [CONDITION_USER
] = "AssertUser",
769 [CONDITION_GROUP
] = "AssertGroup",
770 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
771 [CONDITION_NULL
] = "AssertNull"
774 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
776 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
777 [CONDITION_UNTESTED
] = "untested",
778 [CONDITION_SUCCEEDED
] = "succeeded",
779 [CONDITION_FAILED
] = "failed",
780 [CONDITION_ERROR
] = "error",
783 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);