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
);
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)
311 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
||
312 (usr
.st_mtim
.tv_sec
== other
.st_mtim
.tv_sec
&& usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
);
315 static int condition_test_first_boot(Condition
*c
) {
319 assert(c
->parameter
);
320 assert(c
->type
== CONDITION_FIRST_BOOT
);
322 r
= parse_boolean(c
->parameter
);
326 return (access("/run/systemd/first-boot", F_OK
) >= 0) == !!r
;
329 static int condition_test_path_exists(Condition
*c
) {
331 assert(c
->parameter
);
332 assert(c
->type
== CONDITION_PATH_EXISTS
);
334 return access(c
->parameter
, F_OK
) >= 0;
337 static int condition_test_path_exists_glob(Condition
*c
) {
339 assert(c
->parameter
);
340 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
342 return glob_exists(c
->parameter
) > 0;
345 static int condition_test_path_is_directory(Condition
*c
) {
347 assert(c
->parameter
);
348 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
350 return is_dir(c
->parameter
, true) > 0;
353 static int condition_test_path_is_symbolic_link(Condition
*c
) {
355 assert(c
->parameter
);
356 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
358 return is_symlink(c
->parameter
) > 0;
361 static int condition_test_path_is_mount_point(Condition
*c
) {
363 assert(c
->parameter
);
364 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
366 return path_is_mount_point(c
->parameter
, AT_SYMLINK_FOLLOW
) > 0;
369 static int condition_test_path_is_read_write(Condition
*c
) {
371 assert(c
->parameter
);
372 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
374 return path_is_read_only_fs(c
->parameter
) <= 0;
377 static int condition_test_directory_not_empty(Condition
*c
) {
381 assert(c
->parameter
);
382 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
384 r
= dir_is_empty(c
->parameter
);
385 return r
<= 0 && r
!= -ENOENT
;
388 static int condition_test_file_not_empty(Condition
*c
) {
392 assert(c
->parameter
);
393 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
395 return (stat(c
->parameter
, &st
) >= 0 &&
396 S_ISREG(st
.st_mode
) &&
400 static int condition_test_file_is_executable(Condition
*c
) {
404 assert(c
->parameter
);
405 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
407 return (stat(c
->parameter
, &st
) >= 0 &&
408 S_ISREG(st
.st_mode
) &&
409 (st
.st_mode
& 0111));
412 static int condition_test_null(Condition
*c
) {
414 assert(c
->type
== CONDITION_NULL
);
416 /* Note that during parsing we already evaluate the string and
417 * store it in c->negate */
421 int condition_test(Condition
*c
) {
423 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
) = {
424 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
425 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
426 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
427 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
428 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
429 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
430 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
431 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
432 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
433 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
434 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
435 [CONDITION_SECURITY
] = condition_test_security
,
436 [CONDITION_CAPABILITY
] = condition_test_capability
,
437 [CONDITION_HOST
] = condition_test_host
,
438 [CONDITION_AC_POWER
] = condition_test_ac_power
,
439 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
440 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
441 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
442 [CONDITION_NULL
] = condition_test_null
,
448 assert(c
->type
>= 0);
449 assert(c
->type
< _CONDITION_TYPE_MAX
);
451 r
= condition_tests
[c
->type
](c
);
453 c
->result
= CONDITION_ERROR
;
457 b
= (r
> 0) == !c
->negate
;
458 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
462 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
470 "%s\t%s: %s%s%s %s\n",
473 c
->trigger
? "|" : "",
474 c
->negate
? "!" : "",
476 condition_result_to_string(c
->result
));
479 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, const char *(*to_string
)(ConditionType t
)) {
482 LIST_FOREACH(conditions
, c
, first
)
483 condition_dump(c
, f
, prefix
, to_string
);
486 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
487 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
488 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
489 [CONDITION_HOST
] = "ConditionHost",
490 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
491 [CONDITION_SECURITY
] = "ConditionSecurity",
492 [CONDITION_CAPABILITY
] = "ConditionCapability",
493 [CONDITION_AC_POWER
] = "ConditionACPower",
494 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
495 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
496 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
497 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
498 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
499 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
500 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
501 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
502 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
503 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
504 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
505 [CONDITION_NULL
] = "ConditionNull"
508 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
510 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
511 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
512 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
513 [CONDITION_HOST
] = "AssertHost",
514 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
515 [CONDITION_SECURITY
] = "AssertSecurity",
516 [CONDITION_CAPABILITY
] = "AssertCapability",
517 [CONDITION_AC_POWER
] = "AssertACPower",
518 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
519 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
520 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
521 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
522 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
523 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
524 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
525 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
526 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
527 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
528 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
529 [CONDITION_NULL
] = "AssertNull"
532 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
534 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
535 [CONDITION_UNTESTED
] = "untested",
536 [CONDITION_SUCCEEDED
] = "succeeded",
537 [CONDITION_FAILED
] = "failed",
538 [CONDITION_ERROR
] = "error",
541 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);