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"
24 #include "extract-word.h"
27 #include "glob-util.h"
28 #include "hostname-util.h"
32 #include "mount-util.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "proc-cmdline.h"
36 #include "process-util.h"
37 #include "selinux-util.h"
38 #include "smack-util.h"
39 #include "stat-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
42 #include "tomoyo-util.h"
43 #include "user-util.h"
47 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
52 assert(type
< _CONDITION_TYPE_MAX
);
53 assert((!parameter
) == (type
== CONDITION_NULL
));
55 c
= new0(Condition
, 1);
63 r
= free_and_strdup(&c
->parameter
, parameter
);
71 void condition_free(Condition
*c
) {
78 Condition
* condition_free_list(Condition
*first
) {
81 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
87 static int condition_test_kernel_command_line(Condition
*c
) {
88 _cleanup_free_
char *line
= NULL
;
95 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
97 r
= proc_cmdline(&line
);
101 equal
= strchr(c
->parameter
, '=');
104 _cleanup_free_
char *word
= NULL
;
107 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
114 found
= streq(word
, c
->parameter
);
118 f
= startswith(word
, c
->parameter
);
119 found
= f
&& IN_SET(*f
, 0, '=');
129 static int condition_test_kernel_version(Condition
*c
) {
131 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
132 * should be listed first. */
141 static const char *const prefix
[_ORDER_MAX
] = {
142 [LOWER_OR_EQUAL
] = "<=",
143 [GREATER_OR_EQUAL
] = ">=",
148 const char *p
= NULL
;
154 assert(c
->parameter
);
155 assert(c
->type
== CONDITION_KERNEL_VERSION
);
157 assert_se(uname(&u
) >= 0);
159 for (i
= 0; i
< _ORDER_MAX
; i
++) {
160 p
= startswith(c
->parameter
, prefix
[i
]);
165 /* No prefix? Then treat as glob string */
167 return fnmatch(skip_leading_chars(c
->parameter
, NULL
), u
.release
, 0) == 0;
169 k
= str_verscmp(u
.release
, skip_leading_chars(p
, NULL
));
182 case GREATER_OR_EQUAL
:
189 assert_not_reached("Can't compare");
193 static int condition_test_user(Condition
*c
) {
196 _cleanup_free_
char *username
= NULL
;
200 assert(c
->parameter
);
201 assert(c
->type
== CONDITION_USER
);
203 r
= parse_uid(c
->parameter
, &id
);
205 return id
== getuid() || id
== geteuid();
207 if (streq("@system", c
->parameter
))
208 return uid_is_system(getuid()) || uid_is_system(geteuid());
210 username
= getusername_malloc();
214 if (streq(username
, c
->parameter
))
217 if (getpid_cached() == 1)
218 return streq(c
->parameter
, "root");
221 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
);
225 return id
== getuid() || id
== geteuid();
228 static int condition_test_control_group_controller(Condition
*c
) {
230 CGroupMask system_mask
, wanted_mask
= 0;
233 assert(c
->parameter
);
234 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
236 r
= cg_mask_supported(&system_mask
);
238 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
240 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
241 if (r
< 0 || wanted_mask
<= 0) {
242 /* This won't catch the case that we have an unknown controller
243 * mixed in with valid ones -- these are only assessed on the
244 * validity of the valid controllers found. */
245 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
249 return FLAGS_SET(system_mask
, wanted_mask
);
252 static int condition_test_group(Condition
*c
) {
257 assert(c
->parameter
);
258 assert(c
->type
== CONDITION_GROUP
);
260 r
= parse_gid(c
->parameter
, &id
);
264 /* Avoid any NSS lookups if we are PID1 */
265 if (getpid_cached() == 1)
266 return streq(c
->parameter
, "root");
268 return in_group(c
->parameter
) > 0;
271 static int condition_test_virtualization(Condition
*c
) {
275 assert(c
->parameter
);
276 assert(c
->type
== CONDITION_VIRTUALIZATION
);
278 if (streq(c
->parameter
, "private-users"))
279 return running_in_userns();
281 v
= detect_virtualization();
285 /* First, compare with yes/no */
286 b
= parse_boolean(c
->parameter
);
290 /* Then, compare categorization */
291 if (streq(c
->parameter
, "vm"))
292 return VIRTUALIZATION_IS_VM(v
);
294 if (streq(c
->parameter
, "container"))
295 return VIRTUALIZATION_IS_CONTAINER(v
);
297 /* Finally compare id */
298 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
301 static int condition_test_architecture(Condition
*c
) {
305 assert(c
->parameter
);
306 assert(c
->type
== CONDITION_ARCHITECTURE
);
308 a
= uname_architecture();
312 if (streq(c
->parameter
, "native"))
313 b
= native_architecture();
315 b
= architecture_from_string(c
->parameter
);
316 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
323 static int condition_test_host(Condition
*c
) {
324 _cleanup_free_
char *h
= NULL
;
329 assert(c
->parameter
);
330 assert(c
->type
== CONDITION_HOST
);
332 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
334 r
= sd_id128_get_machine(&y
);
338 return sd_id128_equal(x
, y
);
341 h
= gethostname_malloc();
345 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
348 static int condition_test_ac_power(Condition
*c
) {
352 assert(c
->parameter
);
353 assert(c
->type
== CONDITION_AC_POWER
);
355 r
= parse_boolean(c
->parameter
);
359 return (on_ac_power() != 0) == !!r
;
362 static int condition_test_security(Condition
*c
) {
364 assert(c
->parameter
);
365 assert(c
->type
== CONDITION_SECURITY
);
367 if (streq(c
->parameter
, "selinux"))
368 return mac_selinux_use();
369 if (streq(c
->parameter
, "smack"))
370 return mac_smack_use();
371 if (streq(c
->parameter
, "apparmor"))
372 return mac_apparmor_use();
373 if (streq(c
->parameter
, "audit"))
375 if (streq(c
->parameter
, "ima"))
377 if (streq(c
->parameter
, "tomoyo"))
378 return mac_tomoyo_use();
383 static int condition_test_capability(Condition
*c
) {
384 _cleanup_fclose_
FILE *f
= NULL
;
387 unsigned long long capabilities
= -1;
390 assert(c
->parameter
);
391 assert(c
->type
== CONDITION_CAPABILITY
);
393 /* If it's an invalid capability, we don't have it */
394 value
= capability_from_name(c
->parameter
);
398 /* If it's a valid capability we default to assume
401 f
= fopen("/proc/self/status", "re");
405 while (fgets(line
, sizeof(line
), f
)) {
408 if (startswith(line
, "CapBnd:")) {
409 (void) sscanf(line
+7, "%llx", &capabilities
);
414 return !!(capabilities
& (1ULL << value
));
417 static int condition_test_needs_update(Condition
*c
) {
419 struct stat usr
, other
;
422 assert(c
->parameter
);
423 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
425 /* If the file system is read-only we shouldn't suggest an update */
426 if (path_is_read_only_fs(c
->parameter
) > 0)
429 /* Any other failure means we should allow the condition to be true,
430 * so that we rather invoke too many update tools than too
433 if (!path_is_absolute(c
->parameter
))
436 p
= strjoina(c
->parameter
, "/.updated");
437 if (lstat(p
, &other
) < 0)
440 if (lstat("/usr/", &usr
) < 0)
444 * First, compare seconds as they are always accurate...
446 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
447 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
450 * ...then compare nanoseconds.
452 * A false positive is only possible when /usr's nanoseconds > 0
453 * (otherwise /usr cannot be strictly newer than the target file)
454 * AND the target file's nanoseconds == 0
455 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
457 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
458 _cleanup_free_
char *timestamp_str
= NULL
;
462 r
= parse_env_file(NULL
, p
, NULL
, "TIMESTAMP_NSEC", ×tamp_str
, NULL
);
464 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
467 log_debug("No data in timestamp file '%s', using mtime", p
);
471 r
= safe_atou64(timestamp_str
, ×tamp
);
473 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
477 timespec_store(&other
.st_mtim
, timestamp
);
480 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
483 static int condition_test_first_boot(Condition
*c
) {
487 assert(c
->parameter
);
488 assert(c
->type
== CONDITION_FIRST_BOOT
);
490 r
= parse_boolean(c
->parameter
);
494 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
497 static int condition_test_path_exists(Condition
*c
) {
499 assert(c
->parameter
);
500 assert(c
->type
== CONDITION_PATH_EXISTS
);
502 return access(c
->parameter
, F_OK
) >= 0;
505 static int condition_test_path_exists_glob(Condition
*c
) {
507 assert(c
->parameter
);
508 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
510 return glob_exists(c
->parameter
) > 0;
513 static int condition_test_path_is_directory(Condition
*c
) {
515 assert(c
->parameter
);
516 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
518 return is_dir(c
->parameter
, true) > 0;
521 static int condition_test_path_is_symbolic_link(Condition
*c
) {
523 assert(c
->parameter
);
524 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
526 return is_symlink(c
->parameter
) > 0;
529 static int condition_test_path_is_mount_point(Condition
*c
) {
531 assert(c
->parameter
);
532 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
534 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
537 static int condition_test_path_is_read_write(Condition
*c
) {
539 assert(c
->parameter
);
540 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
542 return path_is_read_only_fs(c
->parameter
) <= 0;
545 static int condition_test_directory_not_empty(Condition
*c
) {
549 assert(c
->parameter
);
550 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
552 r
= dir_is_empty(c
->parameter
);
553 return r
<= 0 && r
!= -ENOENT
;
556 static int condition_test_file_not_empty(Condition
*c
) {
560 assert(c
->parameter
);
561 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
563 return (stat(c
->parameter
, &st
) >= 0 &&
564 S_ISREG(st
.st_mode
) &&
568 static int condition_test_file_is_executable(Condition
*c
) {
572 assert(c
->parameter
);
573 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
575 return (stat(c
->parameter
, &st
) >= 0 &&
576 S_ISREG(st
.st_mode
) &&
577 (st
.st_mode
& 0111));
580 static int condition_test_null(Condition
*c
) {
582 assert(c
->type
== CONDITION_NULL
);
584 /* Note that during parsing we already evaluate the string and
585 * store it in c->negate */
589 int condition_test(Condition
*c
) {
591 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
592 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
593 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
594 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
595 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
596 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
597 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
598 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
599 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
600 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
601 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
602 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
603 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
604 [CONDITION_SECURITY
] = condition_test_security
,
605 [CONDITION_CAPABILITY
] = condition_test_capability
,
606 [CONDITION_HOST
] = condition_test_host
,
607 [CONDITION_AC_POWER
] = condition_test_ac_power
,
608 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
609 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
610 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
611 [CONDITION_USER
] = condition_test_user
,
612 [CONDITION_GROUP
] = condition_test_group
,
613 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
614 [CONDITION_NULL
] = condition_test_null
,
620 assert(c
->type
>= 0);
621 assert(c
->type
< _CONDITION_TYPE_MAX
);
623 r
= condition_tests
[c
->type
](c
);
625 c
->result
= CONDITION_ERROR
;
629 b
= (r
> 0) == !c
->negate
;
630 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
634 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
638 prefix
= strempty(prefix
);
641 "%s\t%s: %s%s%s %s\n",
644 c
->trigger
? "|" : "",
645 c
->negate
? "!" : "",
647 condition_result_to_string(c
->result
));
650 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
653 LIST_FOREACH(conditions
, c
, first
)
654 condition_dump(c
, f
, prefix
, to_string
);
657 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
658 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
659 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
660 [CONDITION_HOST
] = "ConditionHost",
661 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
662 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
663 [CONDITION_SECURITY
] = "ConditionSecurity",
664 [CONDITION_CAPABILITY
] = "ConditionCapability",
665 [CONDITION_AC_POWER
] = "ConditionACPower",
666 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
667 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
668 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
669 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
670 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
671 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
672 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
673 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
674 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
675 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
676 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
677 [CONDITION_USER
] = "ConditionUser",
678 [CONDITION_GROUP
] = "ConditionGroup",
679 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
680 [CONDITION_NULL
] = "ConditionNull"
683 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
685 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
686 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
687 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
688 [CONDITION_HOST
] = "AssertHost",
689 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
690 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
691 [CONDITION_SECURITY
] = "AssertSecurity",
692 [CONDITION_CAPABILITY
] = "AssertCapability",
693 [CONDITION_AC_POWER
] = "AssertACPower",
694 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
695 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
696 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
697 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
698 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
699 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
700 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
701 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
702 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
703 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
704 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
705 [CONDITION_USER
] = "AssertUser",
706 [CONDITION_GROUP
] = "AssertGroup",
707 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
708 [CONDITION_NULL
] = "AssertNull"
711 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
713 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
714 [CONDITION_UNTESTED
] = "untested",
715 [CONDITION_SUCCEEDED
] = "succeeded",
716 [CONDITION_FAILED
] = "failed",
717 [CONDITION_ERROR
] = "error",
720 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);