2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include "alloc-util.h"
33 #include "apparmor-util.h"
34 #include "architecture.h"
35 #include "audit-util.h"
37 #include "condition.h"
38 #include "extract-word.h"
41 #include "glob-util.h"
42 #include "hostname-util.h"
46 #include "mount-util.h"
47 #include "parse-util.h"
48 #include "path-util.h"
49 #include "proc-cmdline.h"
50 #include "selinux-util.h"
51 #include "smack-util.h"
52 #include "stat-util.h"
53 #include "string-table.h"
54 #include "string-util.h"
58 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
63 assert(type
< _CONDITION_TYPE_MAX
);
64 assert((!parameter
) == (type
== CONDITION_NULL
));
66 c
= new0(Condition
, 1);
74 r
= free_and_strdup(&c
->parameter
, parameter
);
83 void condition_free(Condition
*c
) {
90 Condition
* condition_free_list(Condition
*first
) {
93 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
99 static int condition_test_kernel_command_line(Condition
*c
) {
100 _cleanup_free_
char *line
= NULL
;
106 assert(c
->parameter
);
107 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
109 r
= proc_cmdline(&line
);
113 equal
= !!strchr(c
->parameter
, '=');
116 _cleanup_free_
char *word
= NULL
;
119 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
126 found
= streq(word
, c
->parameter
);
130 f
= startswith(word
, c
->parameter
);
131 found
= f
&& (*f
== '=' || *f
== 0);
141 static int condition_test_virtualization(Condition
*c
) {
145 assert(c
->parameter
);
146 assert(c
->type
== CONDITION_VIRTUALIZATION
);
148 if (streq(c
->parameter
, "private-users"))
149 return running_in_userns();
151 v
= detect_virtualization();
155 /* First, compare with yes/no */
156 b
= parse_boolean(c
->parameter
);
160 /* Then, compare categorization */
161 if (streq(c
->parameter
, "vm"))
162 return VIRTUALIZATION_IS_VM(v
);
164 if (streq(c
->parameter
, "container"))
165 return VIRTUALIZATION_IS_CONTAINER(v
);
167 /* Finally compare id */
168 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
171 static int condition_test_architecture(Condition
*c
) {
175 assert(c
->parameter
);
176 assert(c
->type
== CONDITION_ARCHITECTURE
);
178 a
= uname_architecture();
182 if (streq(c
->parameter
, "native"))
183 b
= native_architecture();
185 b
= architecture_from_string(c
->parameter
);
186 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
193 static int condition_test_host(Condition
*c
) {
194 _cleanup_free_
char *h
= NULL
;
199 assert(c
->parameter
);
200 assert(c
->type
== CONDITION_HOST
);
202 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
204 r
= sd_id128_get_machine(&y
);
208 return sd_id128_equal(x
, y
);
211 h
= gethostname_malloc();
215 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
218 static int condition_test_ac_power(Condition
*c
) {
222 assert(c
->parameter
);
223 assert(c
->type
== CONDITION_AC_POWER
);
225 r
= parse_boolean(c
->parameter
);
229 return (on_ac_power() != 0) == !!r
;
232 static int condition_test_security(Condition
*c
) {
234 assert(c
->parameter
);
235 assert(c
->type
== CONDITION_SECURITY
);
237 if (streq(c
->parameter
, "selinux"))
238 return mac_selinux_have();
239 if (streq(c
->parameter
, "smack"))
240 return mac_smack_use();
241 if (streq(c
->parameter
, "apparmor"))
242 return mac_apparmor_use();
243 if (streq(c
->parameter
, "audit"))
245 if (streq(c
->parameter
, "ima"))
251 static int condition_test_capability(Condition
*c
) {
252 _cleanup_fclose_
FILE *f
= NULL
;
255 unsigned long long capabilities
= -1;
258 assert(c
->parameter
);
259 assert(c
->type
== CONDITION_CAPABILITY
);
261 /* If it's an invalid capability, we don't have it */
262 value
= capability_from_name(c
->parameter
);
266 /* If it's a valid capability we default to assume
269 f
= fopen("/proc/self/status", "re");
273 while (fgets(line
, sizeof(line
), f
)) {
276 if (startswith(line
, "CapBnd:")) {
277 (void) sscanf(line
+7, "%llx", &capabilities
);
282 return !!(capabilities
& (1ULL << value
));
285 static int condition_test_needs_update(Condition
*c
) {
287 struct stat usr
, other
;
290 assert(c
->parameter
);
291 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
293 /* If the file system is read-only we shouldn't suggest an update */
294 if (path_is_read_only_fs(c
->parameter
) > 0)
297 /* Any other failure means we should allow the condition to be true,
298 * so that we rather invoke too many update tools than too
301 if (!path_is_absolute(c
->parameter
))
304 p
= strjoina(c
->parameter
, "/.updated");
305 if (lstat(p
, &other
) < 0)
308 if (lstat("/usr/", &usr
) < 0)
312 * First, compare seconds as they are always accurate...
314 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
315 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
318 * ...then compare nanoseconds.
320 * A false positive is only possible when /usr's nanoseconds > 0
321 * (otherwise /usr cannot be strictly newer than the target file)
322 * AND the target file's nanoseconds == 0
323 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
325 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
326 _cleanup_free_
char *timestamp_str
= NULL
;
330 r
= parse_env_file(p
, NULL
, "TIMESTAMP_NSEC", ×tamp_str
, NULL
);
332 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
335 log_debug("No data in timestamp file '%s', using mtime", p
);
339 r
= safe_atou64(timestamp_str
, ×tamp
);
341 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
345 timespec_store(&other
.st_mtim
, timestamp
);
348 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
351 static int condition_test_first_boot(Condition
*c
) {
355 assert(c
->parameter
);
356 assert(c
->type
== CONDITION_FIRST_BOOT
);
358 r
= parse_boolean(c
->parameter
);
362 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
365 static int condition_test_path_exists(Condition
*c
) {
367 assert(c
->parameter
);
368 assert(c
->type
== CONDITION_PATH_EXISTS
);
370 return access(c
->parameter
, F_OK
) >= 0;
373 static int condition_test_path_exists_glob(Condition
*c
) {
375 assert(c
->parameter
);
376 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
378 return glob_exists(c
->parameter
) > 0;
381 static int condition_test_path_is_directory(Condition
*c
) {
383 assert(c
->parameter
);
384 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
386 return is_dir(c
->parameter
, true) > 0;
389 static int condition_test_path_is_symbolic_link(Condition
*c
) {
391 assert(c
->parameter
);
392 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
394 return is_symlink(c
->parameter
) > 0;
397 static int condition_test_path_is_mount_point(Condition
*c
) {
399 assert(c
->parameter
);
400 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
402 return path_is_mount_point(c
->parameter
, AT_SYMLINK_FOLLOW
) > 0;
405 static int condition_test_path_is_read_write(Condition
*c
) {
407 assert(c
->parameter
);
408 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
410 return path_is_read_only_fs(c
->parameter
) <= 0;
413 static int condition_test_directory_not_empty(Condition
*c
) {
417 assert(c
->parameter
);
418 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
420 r
= dir_is_empty(c
->parameter
);
421 return r
<= 0 && r
!= -ENOENT
;
424 static int condition_test_file_not_empty(Condition
*c
) {
428 assert(c
->parameter
);
429 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
431 return (stat(c
->parameter
, &st
) >= 0 &&
432 S_ISREG(st
.st_mode
) &&
436 static int condition_test_file_is_executable(Condition
*c
) {
440 assert(c
->parameter
);
441 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
443 return (stat(c
->parameter
, &st
) >= 0 &&
444 S_ISREG(st
.st_mode
) &&
445 (st
.st_mode
& 0111));
448 static int condition_test_null(Condition
*c
) {
450 assert(c
->type
== CONDITION_NULL
);
452 /* Note that during parsing we already evaluate the string and
453 * store it in c->negate */
457 int condition_test(Condition
*c
) {
459 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
460 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
461 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
462 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
463 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
464 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
465 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
466 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
467 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
468 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
469 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
470 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
471 [CONDITION_SECURITY
] = condition_test_security
,
472 [CONDITION_CAPABILITY
] = condition_test_capability
,
473 [CONDITION_HOST
] = condition_test_host
,
474 [CONDITION_AC_POWER
] = condition_test_ac_power
,
475 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
476 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
477 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
478 [CONDITION_NULL
] = condition_test_null
,
484 assert(c
->type
>= 0);
485 assert(c
->type
< _CONDITION_TYPE_MAX
);
487 r
= condition_tests
[c
->type
](c
);
489 c
->result
= CONDITION_ERROR
;
493 b
= (r
> 0) == !c
->negate
;
494 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
498 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
506 "%s\t%s: %s%s%s %s\n",
509 c
->trigger
? "|" : "",
510 c
->negate
? "!" : "",
512 condition_result_to_string(c
->result
));
515 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
518 LIST_FOREACH(conditions
, c
, first
)
519 condition_dump(c
, f
, prefix
, to_string
);
522 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
523 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
524 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
525 [CONDITION_HOST
] = "ConditionHost",
526 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
527 [CONDITION_SECURITY
] = "ConditionSecurity",
528 [CONDITION_CAPABILITY
] = "ConditionCapability",
529 [CONDITION_AC_POWER
] = "ConditionACPower",
530 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
531 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
532 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
533 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
534 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
535 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
536 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
537 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
538 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
539 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
540 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
541 [CONDITION_NULL
] = "ConditionNull"
544 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
546 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
547 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
548 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
549 [CONDITION_HOST
] = "AssertHost",
550 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
551 [CONDITION_SECURITY
] = "AssertSecurity",
552 [CONDITION_CAPABILITY
] = "AssertCapability",
553 [CONDITION_AC_POWER
] = "AssertACPower",
554 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
555 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
556 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
557 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
558 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
559 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
560 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
561 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
562 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
563 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
564 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
565 [CONDITION_NULL
] = "AssertNull"
568 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
570 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
571 [CONDITION_UNTESTED
] = "untested",
572 [CONDITION_SUCCEEDED
] = "succeeded",
573 [CONDITION_FAILED
] = "failed",
574 [CONDITION_ERROR
] = "error",
577 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);