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
, '=');
117 _cleanup_free_
char *word
= NULL
;
120 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
127 found
= streq(word
, c
->parameter
);
131 f
= startswith(word
, c
->parameter
);
132 found
= f
&& (*f
== '=' || *f
== 0);
142 static int condition_test_virtualization(Condition
*c
) {
146 assert(c
->parameter
);
147 assert(c
->type
== CONDITION_VIRTUALIZATION
);
149 v
= detect_virtualization();
153 /* First, compare with yes/no */
154 b
= parse_boolean(c
->parameter
);
159 if (v
== 0 && b
== 0)
162 /* Then, compare categorization */
163 if (VIRTUALIZATION_IS_VM(v
) && streq(c
->parameter
, "vm"))
166 if (VIRTUALIZATION_IS_CONTAINER(v
) && streq(c
->parameter
, "container"))
169 /* Finally compare id */
170 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
173 static int condition_test_architecture(Condition
*c
) {
177 assert(c
->parameter
);
178 assert(c
->type
== CONDITION_ARCHITECTURE
);
180 a
= uname_architecture();
184 if (streq(c
->parameter
, "native"))
185 b
= native_architecture();
187 b
= architecture_from_string(c
->parameter
);
188 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
195 static int condition_test_host(Condition
*c
) {
196 _cleanup_free_
char *h
= NULL
;
201 assert(c
->parameter
);
202 assert(c
->type
== CONDITION_HOST
);
204 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
206 r
= sd_id128_get_machine(&y
);
210 return sd_id128_equal(x
, y
);
213 h
= gethostname_malloc();
217 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
220 static int condition_test_ac_power(Condition
*c
) {
224 assert(c
->parameter
);
225 assert(c
->type
== CONDITION_AC_POWER
);
227 r
= parse_boolean(c
->parameter
);
231 return (on_ac_power() != 0) == !!r
;
234 static int condition_test_security(Condition
*c
) {
236 assert(c
->parameter
);
237 assert(c
->type
== CONDITION_SECURITY
);
239 if (streq(c
->parameter
, "selinux"))
240 return mac_selinux_have();
241 if (streq(c
->parameter
, "smack"))
242 return mac_smack_use();
243 if (streq(c
->parameter
, "apparmor"))
244 return mac_apparmor_use();
245 if (streq(c
->parameter
, "audit"))
247 if (streq(c
->parameter
, "ima"))
253 static int condition_test_capability(Condition
*c
) {
254 _cleanup_fclose_
FILE *f
= NULL
;
257 unsigned long long capabilities
= -1;
260 assert(c
->parameter
);
261 assert(c
->type
== CONDITION_CAPABILITY
);
263 /* If it's an invalid capability, we don't have it */
264 value
= capability_from_name(c
->parameter
);
268 /* If it's a valid capability we default to assume
271 f
= fopen("/proc/self/status", "re");
275 while (fgets(line
, sizeof(line
), f
)) {
278 if (startswith(line
, "CapBnd:")) {
279 (void) sscanf(line
+7, "%llx", &capabilities
);
284 return !!(capabilities
& (1ULL << value
));
287 static int condition_test_needs_update(Condition
*c
) {
289 struct stat usr
, other
;
292 assert(c
->parameter
);
293 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
295 /* If the file system is read-only we shouldn't suggest an update */
296 if (path_is_read_only_fs(c
->parameter
) > 0)
299 /* Any other failure means we should allow the condition to be true,
300 * so that we rather invoke too many update tools than too
303 if (!path_is_absolute(c
->parameter
))
306 p
= strjoina(c
->parameter
, "/.updated");
307 if (lstat(p
, &other
) < 0)
310 if (lstat("/usr/", &usr
) < 0)
314 * First, compare seconds as they are always accurate...
316 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
317 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
320 * ...then compare nanoseconds.
322 * A false positive is only possible when /usr's nanoseconds > 0
323 * (otherwise /usr cannot be strictly newer than the target file)
324 * AND the target file's nanoseconds == 0
325 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
327 if (usr
.st_mtim
.tv_nsec
> 0 && other
.st_mtim
.tv_nsec
== 0) {
328 _cleanup_free_
char *timestamp_str
= NULL
;
332 r
= parse_env_file(p
, NULL
, "TIMESTAMP_NSEC", ×tamp_str
, NULL
);
334 log_error_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
337 log_debug("No data in timestamp file '%s', using mtime", p
);
341 r
= safe_atou64(timestamp_str
, ×tamp
);
343 log_error_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
347 timespec_store(&other
.st_mtim
, timestamp
);
350 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
353 static int condition_test_first_boot(Condition
*c
) {
357 assert(c
->parameter
);
358 assert(c
->type
== CONDITION_FIRST_BOOT
);
360 r
= parse_boolean(c
->parameter
);
364 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
367 static int condition_test_path_exists(Condition
*c
) {
369 assert(c
->parameter
);
370 assert(c
->type
== CONDITION_PATH_EXISTS
);
372 return access(c
->parameter
, F_OK
) >= 0;
375 static int condition_test_path_exists_glob(Condition
*c
) {
377 assert(c
->parameter
);
378 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
380 return glob_exists(c
->parameter
) > 0;
383 static int condition_test_path_is_directory(Condition
*c
) {
385 assert(c
->parameter
);
386 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
388 return is_dir(c
->parameter
, true) > 0;
391 static int condition_test_path_is_symbolic_link(Condition
*c
) {
393 assert(c
->parameter
);
394 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
396 return is_symlink(c
->parameter
) > 0;
399 static int condition_test_path_is_mount_point(Condition
*c
) {
401 assert(c
->parameter
);
402 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
404 return path_is_mount_point(c
->parameter
, AT_SYMLINK_FOLLOW
) > 0;
407 static int condition_test_path_is_read_write(Condition
*c
) {
409 assert(c
->parameter
);
410 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
412 return path_is_read_only_fs(c
->parameter
) <= 0;
415 static int condition_test_directory_not_empty(Condition
*c
) {
419 assert(c
->parameter
);
420 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
422 r
= dir_is_empty(c
->parameter
);
423 return r
<= 0 && r
!= -ENOENT
;
426 static int condition_test_file_not_empty(Condition
*c
) {
430 assert(c
->parameter
);
431 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
433 return (stat(c
->parameter
, &st
) >= 0 &&
434 S_ISREG(st
.st_mode
) &&
438 static int condition_test_file_is_executable(Condition
*c
) {
442 assert(c
->parameter
);
443 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
445 return (stat(c
->parameter
, &st
) >= 0 &&
446 S_ISREG(st
.st_mode
) &&
447 (st
.st_mode
& 0111));
450 static int condition_test_null(Condition
*c
) {
452 assert(c
->type
== CONDITION_NULL
);
454 /* Note that during parsing we already evaluate the string and
455 * store it in c->negate */
459 int condition_test(Condition
*c
) {
461 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
462 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
463 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
464 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
465 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
466 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
467 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
468 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
469 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
470 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
471 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
472 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
473 [CONDITION_SECURITY
] = condition_test_security
,
474 [CONDITION_CAPABILITY
] = condition_test_capability
,
475 [CONDITION_HOST
] = condition_test_host
,
476 [CONDITION_AC_POWER
] = condition_test_ac_power
,
477 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
478 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
479 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
480 [CONDITION_NULL
] = condition_test_null
,
486 assert(c
->type
>= 0);
487 assert(c
->type
< _CONDITION_TYPE_MAX
);
489 r
= condition_tests
[c
->type
](c
);
491 c
->result
= CONDITION_ERROR
;
495 b
= (r
> 0) == !c
->negate
;
496 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
500 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
508 "%s\t%s: %s%s%s %s\n",
511 c
->trigger
? "|" : "",
512 c
->negate
? "!" : "",
514 condition_result_to_string(c
->result
));
517 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
520 LIST_FOREACH(conditions
, c
, first
)
521 condition_dump(c
, f
, prefix
, to_string
);
524 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
525 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
526 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
527 [CONDITION_HOST
] = "ConditionHost",
528 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
529 [CONDITION_SECURITY
] = "ConditionSecurity",
530 [CONDITION_CAPABILITY
] = "ConditionCapability",
531 [CONDITION_AC_POWER
] = "ConditionACPower",
532 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
533 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
534 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
535 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
536 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
537 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
538 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
539 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
540 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
541 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
542 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
543 [CONDITION_NULL
] = "ConditionNull"
546 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
548 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
549 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
550 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
551 [CONDITION_HOST
] = "AssertHost",
552 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
553 [CONDITION_SECURITY
] = "AssertSecurity",
554 [CONDITION_CAPABILITY
] = "AssertCapability",
555 [CONDITION_AC_POWER
] = "AssertACPower",
556 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
557 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
558 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
559 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
560 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
561 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
562 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
563 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
564 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
565 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
566 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
567 [CONDITION_NULL
] = "AssertNull"
570 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
572 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
573 [CONDITION_UNTESTED
] = "untested",
574 [CONDITION_SUCCEEDED
] = "succeeded",
575 [CONDITION_FAILED
] = "failed",
576 [CONDITION_ERROR
] = "error",
579 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);