1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
13 #include <sys/types.h>
14 #include <sys/utsname.h>
20 #include "alloc-util.h"
21 #include "apparmor-util.h"
22 #include "architecture.h"
23 #include "audit-util.h"
25 #include "cgroup-util.h"
26 #include "condition.h"
27 #include "extract-word.h"
30 #include "glob-util.h"
31 #include "hostname-util.h"
35 #include "mount-util.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "proc-cmdline.h"
39 #include "process-util.h"
40 #include "selinux-util.h"
41 #include "smack-util.h"
42 #include "stat-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
45 #include "tomoyo-util.h"
46 #include "user-util.h"
50 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
55 assert(type
< _CONDITION_TYPE_MAX
);
56 assert((!parameter
) == (type
== CONDITION_NULL
));
58 c
= new0(Condition
, 1);
66 r
= free_and_strdup(&c
->parameter
, parameter
);
74 void condition_free(Condition
*c
) {
81 Condition
* condition_free_list(Condition
*first
) {
84 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
90 static int condition_test_kernel_command_line(Condition
*c
) {
91 _cleanup_free_
char *line
= NULL
;
98 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
100 r
= proc_cmdline(&line
);
104 equal
= strchr(c
->parameter
, '=');
107 _cleanup_free_
char *word
= NULL
;
110 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
117 found
= streq(word
, c
->parameter
);
121 f
= startswith(word
, c
->parameter
);
122 found
= f
&& IN_SET(*f
, 0, '=');
132 static int condition_test_kernel_version(Condition
*c
) {
134 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
135 * should be listed first. */
144 static const char *const prefix
[_ORDER_MAX
] = {
145 [LOWER_OR_EQUAL
] = "<=",
146 [GREATER_OR_EQUAL
] = ">=",
151 const char *p
= NULL
;
157 assert(c
->parameter
);
158 assert(c
->type
== CONDITION_KERNEL_VERSION
);
160 assert_se(uname(&u
) >= 0);
162 for (i
= 0; i
< _ORDER_MAX
; i
++) {
163 p
= startswith(c
->parameter
, prefix
[i
]);
168 /* No prefix? Then treat as glob string */
170 return fnmatch(skip_leading_chars(c
->parameter
, NULL
), u
.release
, 0) == 0;
172 k
= str_verscmp(u
.release
, skip_leading_chars(p
, NULL
));
185 case GREATER_OR_EQUAL
:
192 assert_not_reached("Can't compare");
196 static int condition_test_user(Condition
*c
) {
199 _cleanup_free_
char *username
= NULL
;
203 assert(c
->parameter
);
204 assert(c
->type
== CONDITION_USER
);
206 r
= parse_uid(c
->parameter
, &id
);
208 return id
== getuid() || id
== geteuid();
210 if (streq("@system", c
->parameter
))
211 return uid_is_system(getuid()) || uid_is_system(geteuid());
213 username
= getusername_malloc();
217 if (streq(username
, c
->parameter
))
220 if (getpid_cached() == 1)
221 return streq(c
->parameter
, "root");
224 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
);
228 return id
== getuid() || id
== geteuid();
231 static int condition_test_control_group_controller(Condition
*c
) {
233 CGroupMask system_mask
, wanted_mask
= 0;
236 assert(c
->parameter
);
237 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
239 r
= cg_mask_supported(&system_mask
);
241 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
243 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
244 if (r
< 0 || wanted_mask
<= 0) {
245 /* This won't catch the case that we have an unknown controller
246 * mixed in with valid ones -- these are only assessed on the
247 * validity of the valid controllers found. */
248 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
252 return FLAGS_SET(system_mask
, wanted_mask
);
255 static int condition_test_group(Condition
*c
) {
260 assert(c
->parameter
);
261 assert(c
->type
== CONDITION_GROUP
);
263 r
= parse_gid(c
->parameter
, &id
);
267 /* Avoid any NSS lookups if we are PID1 */
268 if (getpid_cached() == 1)
269 return streq(c
->parameter
, "root");
271 return in_group(c
->parameter
) > 0;
274 static int condition_test_virtualization(Condition
*c
) {
278 assert(c
->parameter
);
279 assert(c
->type
== CONDITION_VIRTUALIZATION
);
281 if (streq(c
->parameter
, "private-users"))
282 return running_in_userns();
284 v
= detect_virtualization();
288 /* First, compare with yes/no */
289 b
= parse_boolean(c
->parameter
);
293 /* Then, compare categorization */
294 if (streq(c
->parameter
, "vm"))
295 return VIRTUALIZATION_IS_VM(v
);
297 if (streq(c
->parameter
, "container"))
298 return VIRTUALIZATION_IS_CONTAINER(v
);
300 /* Finally compare id */
301 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
304 static int condition_test_architecture(Condition
*c
) {
308 assert(c
->parameter
);
309 assert(c
->type
== CONDITION_ARCHITECTURE
);
311 a
= uname_architecture();
315 if (streq(c
->parameter
, "native"))
316 b
= native_architecture();
318 b
= architecture_from_string(c
->parameter
);
319 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
326 static int condition_test_host(Condition
*c
) {
327 _cleanup_free_
char *h
= NULL
;
332 assert(c
->parameter
);
333 assert(c
->type
== CONDITION_HOST
);
335 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
337 r
= sd_id128_get_machine(&y
);
341 return sd_id128_equal(x
, y
);
344 h
= gethostname_malloc();
348 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
351 static int condition_test_ac_power(Condition
*c
) {
355 assert(c
->parameter
);
356 assert(c
->type
== CONDITION_AC_POWER
);
358 r
= parse_boolean(c
->parameter
);
362 return (on_ac_power() != 0) == !!r
;
365 static int condition_test_security(Condition
*c
) {
367 assert(c
->parameter
);
368 assert(c
->type
== CONDITION_SECURITY
);
370 if (streq(c
->parameter
, "selinux"))
371 return mac_selinux_use();
372 if (streq(c
->parameter
, "smack"))
373 return mac_smack_use();
374 if (streq(c
->parameter
, "apparmor"))
375 return mac_apparmor_use();
376 if (streq(c
->parameter
, "audit"))
378 if (streq(c
->parameter
, "ima"))
380 if (streq(c
->parameter
, "tomoyo"))
381 return mac_tomoyo_use();
386 static int condition_test_capability(Condition
*c
) {
387 _cleanup_fclose_
FILE *f
= NULL
;
390 unsigned long long capabilities
= -1;
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");
408 while (fgets(line
, sizeof(line
), f
)) {
411 if (startswith(line
, "CapBnd:")) {
412 (void) sscanf(line
+7, "%llx", &capabilities
);
417 return !!(capabilities
& (1ULL << value
));
420 static int condition_test_needs_update(Condition
*c
) {
422 struct stat usr
, other
;
425 assert(c
->parameter
);
426 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
428 /* If the file system is read-only we shouldn't suggest an update */
429 if (path_is_read_only_fs(c
->parameter
) > 0)
432 /* Any other failure means we should allow the condition to be true,
433 * so that we rather invoke too many update tools than too
436 if (!path_is_absolute(c
->parameter
))
439 p
= strjoina(c
->parameter
, "/.updated");
440 if (lstat(p
, &other
) < 0)
443 if (lstat("/usr/", &usr
) < 0)
447 * First, compare seconds as they are always accurate...
449 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
450 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
453 * ...then compare nanoseconds.
455 * A false positive is only possible when /usr's nanoseconds > 0
456 * (otherwise /usr cannot be strictly newer than the target file)
457 * AND the target file's nanoseconds == 0
458 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
460 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
461 _cleanup_free_
char *timestamp_str
= NULL
;
465 r
= parse_env_file(NULL
, p
, NULL
, "TIMESTAMP_NSEC", ×tamp_str
, NULL
);
467 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
470 log_debug("No data in timestamp file '%s', using mtime", p
);
474 r
= safe_atou64(timestamp_str
, ×tamp
);
476 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
480 timespec_store(&other
.st_mtim
, timestamp
);
483 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
486 static int condition_test_first_boot(Condition
*c
) {
490 assert(c
->parameter
);
491 assert(c
->type
== CONDITION_FIRST_BOOT
);
493 r
= parse_boolean(c
->parameter
);
497 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
500 static int condition_test_path_exists(Condition
*c
) {
502 assert(c
->parameter
);
503 assert(c
->type
== CONDITION_PATH_EXISTS
);
505 return access(c
->parameter
, F_OK
) >= 0;
508 static int condition_test_path_exists_glob(Condition
*c
) {
510 assert(c
->parameter
);
511 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
513 return glob_exists(c
->parameter
) > 0;
516 static int condition_test_path_is_directory(Condition
*c
) {
518 assert(c
->parameter
);
519 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
521 return is_dir(c
->parameter
, true) > 0;
524 static int condition_test_path_is_symbolic_link(Condition
*c
) {
526 assert(c
->parameter
);
527 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
529 return is_symlink(c
->parameter
) > 0;
532 static int condition_test_path_is_mount_point(Condition
*c
) {
534 assert(c
->parameter
);
535 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
537 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
540 static int condition_test_path_is_read_write(Condition
*c
) {
542 assert(c
->parameter
);
543 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
545 return path_is_read_only_fs(c
->parameter
) <= 0;
548 static int condition_test_directory_not_empty(Condition
*c
) {
552 assert(c
->parameter
);
553 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
555 r
= dir_is_empty(c
->parameter
);
556 return r
<= 0 && r
!= -ENOENT
;
559 static int condition_test_file_not_empty(Condition
*c
) {
563 assert(c
->parameter
);
564 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
566 return (stat(c
->parameter
, &st
) >= 0 &&
567 S_ISREG(st
.st_mode
) &&
571 static int condition_test_file_is_executable(Condition
*c
) {
575 assert(c
->parameter
);
576 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
578 return (stat(c
->parameter
, &st
) >= 0 &&
579 S_ISREG(st
.st_mode
) &&
580 (st
.st_mode
& 0111));
583 static int condition_test_null(Condition
*c
) {
585 assert(c
->type
== CONDITION_NULL
);
587 /* Note that during parsing we already evaluate the string and
588 * store it in c->negate */
592 int condition_test(Condition
*c
) {
594 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
595 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
596 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
597 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
598 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
599 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
600 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
601 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
602 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
603 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
604 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
605 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
606 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
607 [CONDITION_SECURITY
] = condition_test_security
,
608 [CONDITION_CAPABILITY
] = condition_test_capability
,
609 [CONDITION_HOST
] = condition_test_host
,
610 [CONDITION_AC_POWER
] = condition_test_ac_power
,
611 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
612 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
613 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
614 [CONDITION_USER
] = condition_test_user
,
615 [CONDITION_GROUP
] = condition_test_group
,
616 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
617 [CONDITION_NULL
] = condition_test_null
,
623 assert(c
->type
>= 0);
624 assert(c
->type
< _CONDITION_TYPE_MAX
);
626 r
= condition_tests
[c
->type
](c
);
628 c
->result
= CONDITION_ERROR
;
632 b
= (r
> 0) == !c
->negate
;
633 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
637 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
641 prefix
= strempty(prefix
);
644 "%s\t%s: %s%s%s %s\n",
647 c
->trigger
? "|" : "",
648 c
->negate
? "!" : "",
650 condition_result_to_string(c
->result
));
653 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
656 LIST_FOREACH(conditions
, c
, first
)
657 condition_dump(c
, f
, prefix
, to_string
);
660 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
661 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
662 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
663 [CONDITION_HOST
] = "ConditionHost",
664 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
665 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
666 [CONDITION_SECURITY
] = "ConditionSecurity",
667 [CONDITION_CAPABILITY
] = "ConditionCapability",
668 [CONDITION_AC_POWER
] = "ConditionACPower",
669 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
670 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
671 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
672 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
673 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
674 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
675 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
676 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
677 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
678 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
679 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
680 [CONDITION_USER
] = "ConditionUser",
681 [CONDITION_GROUP
] = "ConditionGroup",
682 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
683 [CONDITION_NULL
] = "ConditionNull"
686 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
688 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
689 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
690 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
691 [CONDITION_HOST
] = "AssertHost",
692 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
693 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
694 [CONDITION_SECURITY
] = "AssertSecurity",
695 [CONDITION_CAPABILITY
] = "AssertCapability",
696 [CONDITION_AC_POWER
] = "AssertACPower",
697 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
698 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
699 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
700 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
701 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
702 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
703 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
704 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
705 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
706 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
707 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
708 [CONDITION_USER
] = "AssertUser",
709 [CONDITION_GROUP
] = "AssertGroup",
710 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
711 [CONDITION_NULL
] = "AssertNull"
714 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
716 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
717 [CONDITION_UNTESTED
] = "untested",
718 [CONDITION_SUCCEEDED
] = "succeeded",
719 [CONDITION_FAILED
] = "failed",
720 [CONDITION_ERROR
] = "error",
723 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);