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"
40 #include "glob-util.h"
41 #include "hostname-util.h"
45 #include "mount-util.h"
46 #include "parse-util.h"
47 #include "path-util.h"
48 #include "proc-cmdline.h"
49 #include "selinux-util.h"
50 #include "smack-util.h"
51 #include "stat-util.h"
52 #include "string-table.h"
53 #include "string-util.h"
57 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
62 assert(type
< _CONDITION_TYPE_MAX
);
63 assert((!parameter
) == (type
== CONDITION_NULL
));
65 c
= new0(Condition
, 1);
73 r
= free_and_strdup(&c
->parameter
, parameter
);
82 void condition_free(Condition
*c
) {
89 Condition
* condition_free_list(Condition
*first
) {
92 LIST_FOREACH_SAFE(conditions
, c
, n
, first
)
98 static int condition_test_kernel_command_line(Condition
*c
) {
99 _cleanup_free_
char *line
= NULL
;
105 assert(c
->parameter
);
106 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
108 r
= proc_cmdline(&line
);
112 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 v
= detect_virtualization();
152 /* First, compare with yes/no */
153 b
= parse_boolean(c
->parameter
);
158 if (v
== 0 && b
== 0)
161 /* Then, compare categorization */
162 if (VIRTUALIZATION_IS_VM(v
) && streq(c
->parameter
, "vm"))
165 if (VIRTUALIZATION_IS_CONTAINER(v
) && streq(c
->parameter
, "container"))
168 /* Finally compare id */
169 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
172 static int condition_test_architecture(Condition
*c
) {
176 assert(c
->parameter
);
177 assert(c
->type
== CONDITION_ARCHITECTURE
);
179 a
= uname_architecture();
183 if (streq(c
->parameter
, "native"))
184 b
= native_architecture();
186 b
= architecture_from_string(c
->parameter
);
187 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
194 static int condition_test_host(Condition
*c
) {
195 _cleanup_free_
char *h
= NULL
;
200 assert(c
->parameter
);
201 assert(c
->type
== CONDITION_HOST
);
203 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
205 r
= sd_id128_get_machine(&y
);
209 return sd_id128_equal(x
, y
);
212 h
= gethostname_malloc();
216 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
219 static int condition_test_ac_power(Condition
*c
) {
223 assert(c
->parameter
);
224 assert(c
->type
== CONDITION_AC_POWER
);
226 r
= parse_boolean(c
->parameter
);
230 return (on_ac_power() != 0) == !!r
;
233 static int condition_test_security(Condition
*c
) {
235 assert(c
->parameter
);
236 assert(c
->type
== CONDITION_SECURITY
);
238 if (streq(c
->parameter
, "selinux"))
239 return mac_selinux_have();
240 if (streq(c
->parameter
, "smack"))
241 return mac_smack_use();
242 if (streq(c
->parameter
, "apparmor"))
243 return mac_apparmor_use();
244 if (streq(c
->parameter
, "audit"))
246 if (streq(c
->parameter
, "ima"))
252 static int condition_test_capability(Condition
*c
) {
253 _cleanup_fclose_
FILE *f
= NULL
;
256 unsigned long long capabilities
= -1;
259 assert(c
->parameter
);
260 assert(c
->type
== CONDITION_CAPABILITY
);
262 /* If it's an invalid capability, we don't have it */
263 value
= capability_from_name(c
->parameter
);
267 /* If it's a valid capability we default to assume
270 f
= fopen("/proc/self/status", "re");
274 while (fgets(line
, sizeof(line
), f
)) {
277 if (startswith(line
, "CapBnd:")) {
278 (void) sscanf(line
+7, "%llx", &capabilities
);
283 return !!(capabilities
& (1ULL << value
));
286 static int condition_test_needs_update(Condition
*c
) {
288 struct stat usr
, other
;
291 assert(c
->parameter
);
292 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
294 /* If the file system is read-only we shouldn't suggest an update */
295 if (path_is_read_only_fs(c
->parameter
) > 0)
298 /* Any other failure means we should allow the condition to be true,
299 * so that we rather invoke too many update tools than too
302 if (!path_is_absolute(c
->parameter
))
305 p
= strjoina(c
->parameter
, "/.updated");
306 if (lstat(p
, &other
) < 0)
309 if (lstat("/usr/", &usr
) < 0)
312 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
||
313 (usr
.st_mtim
.tv_sec
== other
.st_mtim
.tv_sec
&& usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
);
316 static int condition_test_first_boot(Condition
*c
) {
320 assert(c
->parameter
);
321 assert(c
->type
== CONDITION_FIRST_BOOT
);
323 r
= parse_boolean(c
->parameter
);
327 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
330 static int condition_test_path_exists(Condition
*c
) {
332 assert(c
->parameter
);
333 assert(c
->type
== CONDITION_PATH_EXISTS
);
335 return access(c
->parameter
, F_OK
) >= 0;
338 static int condition_test_path_exists_glob(Condition
*c
) {
340 assert(c
->parameter
);
341 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
343 return glob_exists(c
->parameter
) > 0;
346 static int condition_test_path_is_directory(Condition
*c
) {
348 assert(c
->parameter
);
349 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
351 return is_dir(c
->parameter
, true) > 0;
354 static int condition_test_path_is_symbolic_link(Condition
*c
) {
356 assert(c
->parameter
);
357 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
359 return is_symlink(c
->parameter
) > 0;
362 static int condition_test_path_is_mount_point(Condition
*c
) {
364 assert(c
->parameter
);
365 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
367 return path_is_mount_point(c
->parameter
, AT_SYMLINK_FOLLOW
) > 0;
370 static int condition_test_path_is_read_write(Condition
*c
) {
372 assert(c
->parameter
);
373 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
375 return path_is_read_only_fs(c
->parameter
) <= 0;
378 static int condition_test_directory_not_empty(Condition
*c
) {
382 assert(c
->parameter
);
383 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
385 r
= dir_is_empty(c
->parameter
);
386 return r
<= 0 && r
!= -ENOENT
;
389 static int condition_test_file_not_empty(Condition
*c
) {
393 assert(c
->parameter
);
394 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
396 return (stat(c
->parameter
, &st
) >= 0 &&
397 S_ISREG(st
.st_mode
) &&
401 static int condition_test_file_is_executable(Condition
*c
) {
405 assert(c
->parameter
);
406 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
408 return (stat(c
->parameter
, &st
) >= 0 &&
409 S_ISREG(st
.st_mode
) &&
410 (st
.st_mode
& 0111));
413 static int condition_test_null(Condition
*c
) {
415 assert(c
->type
== CONDITION_NULL
);
417 /* Note that during parsing we already evaluate the string and
418 * store it in c->negate */
422 int condition_test(Condition
*c
) {
424 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
425 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
426 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
427 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
428 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
429 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
430 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
431 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
432 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
433 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
434 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
435 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
436 [CONDITION_SECURITY
] = condition_test_security
,
437 [CONDITION_CAPABILITY
] = condition_test_capability
,
438 [CONDITION_HOST
] = condition_test_host
,
439 [CONDITION_AC_POWER
] = condition_test_ac_power
,
440 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
441 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
442 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
443 [CONDITION_NULL
] = condition_test_null
,
449 assert(c
->type
>= 0);
450 assert(c
->type
< _CONDITION_TYPE_MAX
);
452 r
= condition_tests
[c
->type
](c
);
454 c
->result
= CONDITION_ERROR
;
458 b
= (r
> 0) == !c
->negate
;
459 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
463 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
471 "%s\t%s: %s%s%s %s\n",
474 c
->trigger
? "|" : "",
475 c
->negate
? "!" : "",
477 condition_result_to_string(c
->result
));
480 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
483 LIST_FOREACH(conditions
, c
, first
)
484 condition_dump(c
, f
, prefix
, to_string
);
487 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
488 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
489 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
490 [CONDITION_HOST
] = "ConditionHost",
491 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
492 [CONDITION_SECURITY
] = "ConditionSecurity",
493 [CONDITION_CAPABILITY
] = "ConditionCapability",
494 [CONDITION_AC_POWER
] = "ConditionACPower",
495 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
496 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
497 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
498 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
499 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
500 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
501 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
502 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
503 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
504 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
505 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
506 [CONDITION_NULL
] = "ConditionNull"
509 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
511 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
512 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
513 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
514 [CONDITION_HOST
] = "AssertHost",
515 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
516 [CONDITION_SECURITY
] = "AssertSecurity",
517 [CONDITION_CAPABILITY
] = "AssertCapability",
518 [CONDITION_AC_POWER
] = "AssertACPower",
519 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
520 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
521 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
522 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
523 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
524 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
525 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
526 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
527 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
528 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
529 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
530 [CONDITION_NULL
] = "AssertNull"
533 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
535 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
536 [CONDITION_UNTESTED
] = "untested",
537 [CONDITION_SUCCEEDED
] = "succeeded",
538 [CONDITION_FAILED
] = "failed",
539 [CONDITION_ERROR
] = "error",
542 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);