1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/capability.h>
27 #include <sys/statvfs.h>
31 #include <selinux/selinux.h>
34 #include <systemd/sd-id128.h>
36 #include "condition.h"
38 #include "path-util.h"
40 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
43 assert(type < _CONDITION_TYPE_MAX);
45 c = new0(Condition, 1);
54 c->parameter = strdup(parameter);
64 void condition_free(Condition *c) {
71 void condition_free_list(Condition *first) {
74 LIST_FOREACH_SAFE(conditions, c, n, first)
78 static bool test_kernel_command_line(const char *parameter) {
79 char *line, *w, *state, *word = NULL;
87 if (detect_container(NULL) > 0)
90 r = read_one_line_file("/proc/cmdline", &line);
92 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
96 equal = !!strchr(parameter, '=');
97 pl = strlen(parameter);
99 FOREACH_WORD_QUOTED(w, l, line, state) {
102 word = strndup(w, l);
107 if (streq(word, parameter)) {
112 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
126 static bool test_virtualization(const char *parameter) {
133 v = detect_virtualization(&id);
135 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v));
139 /* First, compare with yes/no */
140 b = parse_boolean(parameter);
145 if (v == 0 && b == 0)
148 /* Then, compare categorization */
149 if (v == VIRTUALIZATION_VM && streq(parameter, "vm"))
152 if (v == VIRTUALIZATION_CONTAINER && streq(parameter, "container"))
155 /* Finally compare id */
156 return v > 0 && streq(parameter, id);
159 static bool test_security(const char *parameter) {
161 if (streq(parameter, "selinux"))
162 return is_selinux_enabled() > 0;
167 static bool test_capability(const char *parameter) {
171 unsigned long long capabilities = (unsigned long long) -1;
173 /* If it's an invalid capability, we don't have it */
175 if (cap_from_name(parameter, &value) < 0)
178 /* If it's a valid capability we default to assume
181 f = fopen("/proc/self/status", "re");
185 while (fgets(line, sizeof(line), f)) {
188 if (startswith(line, "CapBnd:")) {
189 (void) sscanf(line+7, "%llx", &capabilities);
196 return !!(capabilities & (1ULL << value));
199 static bool test_host(const char *parameter) {
205 if (sd_id128_from_string(parameter, &x) >= 0) {
207 r = sd_id128_get_machine(&y);
211 return sd_id128_equal(x, y);
214 h = gethostname_malloc();
218 b = fnmatch(parameter, h, FNM_CASEFOLD) == 0;
224 bool condition_test(Condition *c) {
229 case CONDITION_PATH_EXISTS:
230 return (access(c->parameter, F_OK) >= 0) == !c->negate;
232 case CONDITION_PATH_EXISTS_GLOB:
233 return (glob_exists(c->parameter) > 0) == !c->negate;
235 case CONDITION_PATH_IS_DIRECTORY: {
238 if (stat(c->parameter, &st) < 0)
240 return S_ISDIR(st.st_mode) == !c->negate;
243 case CONDITION_PATH_IS_SYMBOLIC_LINK: {
246 if (lstat(c->parameter, &st) < 0)
248 return S_ISLNK(st.st_mode) == !c->negate;
251 case CONDITION_PATH_IS_MOUNT_POINT:
252 return (path_is_mount_point(c->parameter, true) > 0) == !c->negate;
254 case CONDITION_PATH_IS_READ_WRITE:
255 return (path_is_read_only_fs(c->parameter) > 0) == c->negate;
257 case CONDITION_DIRECTORY_NOT_EMPTY: {
260 k = dir_is_empty(c->parameter);
261 return !(k == -ENOENT || k > 0) == !c->negate;
264 case CONDITION_FILE_NOT_EMPTY: {
267 if (stat(c->parameter, &st) < 0)
270 return (S_ISREG(st.st_mode) && st.st_size > 0) == !c->negate;
273 case CONDITION_FILE_IS_EXECUTABLE: {
276 if (stat(c->parameter, &st) < 0)
279 return (S_ISREG(st.st_mode) && (st.st_mode & 0111)) == !c->negate;
282 case CONDITION_KERNEL_COMMAND_LINE:
283 return test_kernel_command_line(c->parameter) == !c->negate;
285 case CONDITION_VIRTUALIZATION:
286 return test_virtualization(c->parameter) == !c->negate;
288 case CONDITION_SECURITY:
289 return test_security(c->parameter) == !c->negate;
291 case CONDITION_CAPABILITY:
292 return test_capability(c->parameter) == !c->negate;
295 return test_host(c->parameter) == !c->negate;
301 assert_not_reached("Invalid condition type.");
305 bool condition_test_list(Condition *first) {
309 /* If the condition list is empty, then it is true */
313 /* Otherwise, if all of the non-trigger conditions apply and
314 * if any of the trigger conditions apply (unless there are
315 * none) we return true */
316 LIST_FOREACH(conditions, c, first) {
319 b = condition_test(c);
321 if (!c->trigger && !b)
324 if (c->trigger && triggered <= 0)
328 return triggered != 0;
331 void condition_dump(Condition *c, FILE *f, const char *prefix) {
341 condition_type_to_string(c->type),
342 c->trigger ? "|" : "",
343 c->negate ? "!" : "",
347 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
350 LIST_FOREACH(conditions, c, first)
351 condition_dump(c, f, prefix);
354 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
355 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
356 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
357 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
358 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
359 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
360 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
361 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
362 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
363 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
364 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
365 [CONDITION_SECURITY] = "ConditionSecurity",
366 [CONDITION_HOST] = "ConditionHost",
367 [CONDITION_NULL] = "ConditionNull"
370 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);