]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/condition.c
man: document what SIGUSR1 and SIGUSR2 do to resolved
[thirdparty/systemd.git] / src / shared / condition.c
CommitLineData
b77c08e0
TG
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
b77c08e0 20#include <errno.h>
a8fbdf54 21#include <fcntl.h>
84ac7bea 22#include <fnmatch.h>
a8fbdf54 23#include <limits.h>
84ac7bea 24#include <stdlib.h>
b77c08e0 25#include <string.h>
a8fbdf54
TA
26#include <sys/stat.h>
27#include <time.h>
b77c08e0 28#include <unistd.h>
b77c08e0 29
d1bddcec 30#include "sd-id128.h"
84ac7bea 31
b5efdb8a 32#include "alloc-util.h"
d1bddcec 33#include "apparmor-util.h"
84ac7bea 34#include "architecture.h"
430f0182 35#include "audit-util.h"
2822da4f 36#include "cap-list.h"
3ffd4af2 37#include "condition.h"
84ac7bea 38#include "extract-word.h"
3ffd4af2 39#include "fd-util.h"
7d50b32a 40#include "glob-util.h"
958b66ea 41#include "hostname-util.h"
84ac7bea 42#include "ima-util.h"
a8fbdf54
TA
43#include "list.h"
44#include "macro.h"
4349cd7c 45#include "mount-util.h"
6bedfcbb 46#include "parse-util.h"
84ac7bea 47#include "path-util.h"
4e731273 48#include "proc-cmdline.h"
84ac7bea
LP
49#include "selinux-util.h"
50#include "smack-util.h"
8fcde012 51#include "stat-util.h"
8b43440b 52#include "string-table.h"
07630cea 53#include "string-util.h"
84ac7bea
LP
54#include "util.h"
55#include "virt.h"
b77c08e0
TG
56
57Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
58 Condition *c;
d1bddcec 59 int r;
b77c08e0 60
b80ba1da 61 assert(type >= 0);
b77c08e0 62 assert(type < _CONDITION_TYPE_MAX);
8a9c6071 63 assert((!parameter) == (type == CONDITION_NULL));
b77c08e0
TG
64
65 c = new0(Condition, 1);
66 if (!c)
67 return NULL;
68
69 c->type = type;
70 c->trigger = trigger;
71 c->negate = negate;
72
d1bddcec
LP
73 r = free_and_strdup(&c->parameter, parameter);
74 if (r < 0) {
75 free(c);
76 return NULL;
b77c08e0
TG
77 }
78
79 return c;
80}
81
82void condition_free(Condition *c) {
83 assert(c);
84
85 free(c->parameter);
86 free(c);
87}
88
447021aa 89Condition* condition_free_list(Condition *first) {
b77c08e0
TG
90 Condition *c, *n;
91
92 LIST_FOREACH_SAFE(conditions, c, n, first)
93 condition_free(c);
447021aa
ZJS
94
95 return NULL;
b77c08e0
TG
96}
97
a4705396 98static int condition_test_kernel_command_line(Condition *c) {
07318c29
LP
99 _cleanup_free_ char *line = NULL;
100 const char *p;
b77c08e0
TG
101 bool equal;
102 int r;
b77c08e0
TG
103
104 assert(c);
105 assert(c->parameter);
106 assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
107
108 r = proc_cmdline(&line);
109 if (r < 0)
592fd144 110 return r;
b77c08e0
TG
111
112 equal = !!strchr(c->parameter, '=');
07318c29
LP
113 p = line;
114
115 for (;;) {
116 _cleanup_free_ char *word = NULL;
117 bool found;
118
12ba2c44 119 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
592fd144
LP
120 if (r < 0)
121 return r;
122 if (r == 0)
a4705396 123 break;
07318c29
LP
124
125 if (equal)
126 found = streq(word, c->parameter);
127 else {
128 const char *f;
129
130 f = startswith(word, c->parameter);
131 found = f && (*f == '=' || *f == 0);
b77c08e0
TG
132 }
133
07318c29 134 if (found)
a4705396 135 return true;
b77c08e0 136 }
b77c08e0 137
a4705396 138 return false;
b77c08e0
TG
139}
140
a4705396 141static int condition_test_virtualization(Condition *c) {
248fab74 142 int b, v;
b77c08e0
TG
143
144 assert(c);
145 assert(c->parameter);
146 assert(c->type == CONDITION_VIRTUALIZATION);
147
75f86906 148 v = detect_virtualization();
592fd144
LP
149 if (v < 0)
150 return v;
b77c08e0
TG
151
152 /* First, compare with yes/no */
153 b = parse_boolean(c->parameter);
154
155 if (v > 0 && b > 0)
a4705396 156 return true;
b77c08e0
TG
157
158 if (v == 0 && b == 0)
a4705396 159 return true;
b77c08e0
TG
160
161 /* Then, compare categorization */
75f86906 162 if (VIRTUALIZATION_IS_VM(v) && streq(c->parameter, "vm"))
a4705396 163 return true;
b77c08e0 164
75f86906 165 if (VIRTUALIZATION_IS_CONTAINER(v) && streq(c->parameter, "container"))
a4705396 166 return true;
b77c08e0
TG
167
168 /* Finally compare id */
75f86906 169 return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
b77c08e0
TG
170}
171
a4705396 172static int condition_test_architecture(Condition *c) {
592fd144 173 int a, b;
099524d7
LP
174
175 assert(c);
176 assert(c->parameter);
177 assert(c->type == CONDITION_ARCHITECTURE);
178
179 a = uname_architecture();
180 if (a < 0)
592fd144 181 return a;
099524d7
LP
182
183 if (streq(c->parameter, "native"))
184 b = native_architecture();
185 else
186 b = architecture_from_string(c->parameter);
099524d7 187 if (b < 0)
592fd144 188 return b;
099524d7 189
a4705396 190 return a == b;
099524d7
LP
191}
192
a4705396 193static int condition_test_host(Condition *c) {
dc92e62c 194 _cleanup_free_ char *h = NULL;
b77c08e0 195 sd_id128_t x, y;
b77c08e0 196 int r;
b77c08e0
TG
197
198 assert(c);
199 assert(c->parameter);
200 assert(c->type == CONDITION_HOST);
201
202 if (sd_id128_from_string(c->parameter, &x) >= 0) {
203
204 r = sd_id128_get_machine(&y);
205 if (r < 0)
592fd144 206 return r;
b77c08e0 207
a4705396 208 return sd_id128_equal(x, y);
b77c08e0
TG
209 }
210
211 h = gethostname_malloc();
212 if (!h)
592fd144 213 return -ENOMEM;
b77c08e0 214
a4705396 215 return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
b77c08e0
TG
216}
217
a4705396 218static int condition_test_ac_power(Condition *c) {
b77c08e0
TG
219 int r;
220
221 assert(c);
222 assert(c->parameter);
223 assert(c->type == CONDITION_AC_POWER);
224
225 r = parse_boolean(c->parameter);
226 if (r < 0)
592fd144 227 return r;
b77c08e0 228
a4705396 229 return (on_ac_power() != 0) == !!r;
b77c08e0
TG
230}
231
d1bddcec
LP
232static int condition_test_security(Condition *c) {
233 assert(c);
234 assert(c->parameter);
235 assert(c->type == CONDITION_SECURITY);
236
237 if (streq(c->parameter, "selinux"))
6355e756 238 return mac_selinux_have();
d1bddcec 239 if (streq(c->parameter, "smack"))
a4705396 240 return mac_smack_use();
d1bddcec 241 if (streq(c->parameter, "apparmor"))
a4705396 242 return mac_apparmor_use();
d1bddcec 243 if (streq(c->parameter, "audit"))
a4705396 244 return use_audit();
d1bddcec 245 if (streq(c->parameter, "ima"))
a4705396 246 return use_ima();
d1bddcec 247
a4705396 248 return false;
d1bddcec
LP
249}
250
251static int condition_test_capability(Condition *c) {
252 _cleanup_fclose_ FILE *f = NULL;
2822da4f 253 int value;
d1bddcec
LP
254 char line[LINE_MAX];
255 unsigned long long capabilities = -1;
256
257 assert(c);
258 assert(c->parameter);
259 assert(c->type == CONDITION_CAPABILITY);
260
261 /* If it's an invalid capability, we don't have it */
2822da4f
LP
262 value = capability_from_name(c->parameter);
263 if (value < 0)
d1bddcec
LP
264 return -EINVAL;
265
266 /* If it's a valid capability we default to assume
267 * that we have it */
268
269 f = fopen("/proc/self/status", "re");
270 if (!f)
271 return -errno;
272
273 while (fgets(line, sizeof(line), f)) {
274 truncate_nl(line);
275
276 if (startswith(line, "CapBnd:")) {
277 (void) sscanf(line+7, "%llx", &capabilities);
278 break;
279 }
280 }
281
a4705396 282 return !!(capabilities & (1ULL << value));
d1bddcec
LP
283}
284
285static int condition_test_needs_update(Condition *c) {
286 const char *p;
287 struct stat usr, other;
288
289 assert(c);
290 assert(c->parameter);
291 assert(c->type == CONDITION_NEEDS_UPDATE);
292
293 /* If the file system is read-only we shouldn't suggest an update */
294 if (path_is_read_only_fs(c->parameter) > 0)
a4705396 295 return false;
d1bddcec
LP
296
297 /* Any other failure means we should allow the condition to be true,
96d49011 298 * so that we rather invoke too many update tools than too
d1bddcec
LP
299 * few. */
300
301 if (!path_is_absolute(c->parameter))
a4705396 302 return true;
d1bddcec 303
63c372cb 304 p = strjoina(c->parameter, "/.updated");
d1bddcec 305 if (lstat(p, &other) < 0)
a4705396 306 return true;
d1bddcec
LP
307
308 if (lstat("/usr/", &usr) < 0)
a4705396 309 return true;
d1bddcec 310
a4705396
LP
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);
d1bddcec
LP
313}
314
315static int condition_test_first_boot(Condition *c) {
316 int r;
317
318 assert(c);
319 assert(c->parameter);
320 assert(c->type == CONDITION_FIRST_BOOT);
321
322 r = parse_boolean(c->parameter);
323 if (r < 0)
324 return r;
325
a4705396 326 return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
d1bddcec
LP
327}
328
329static int condition_test_path_exists(Condition *c) {
330 assert(c);
331 assert(c->parameter);
332 assert(c->type == CONDITION_PATH_EXISTS);
333
a4705396 334 return access(c->parameter, F_OK) >= 0;
d1bddcec
LP
335}
336
337static int condition_test_path_exists_glob(Condition *c) {
338 assert(c);
339 assert(c->parameter);
340 assert(c->type == CONDITION_PATH_EXISTS_GLOB);
341
a4705396 342 return glob_exists(c->parameter) > 0;
d1bddcec
LP
343}
344
345static int condition_test_path_is_directory(Condition *c) {
346 assert(c);
347 assert(c->parameter);
348 assert(c->type == CONDITION_PATH_IS_DIRECTORY);
349
a4705396 350 return is_dir(c->parameter, true) > 0;
d1bddcec
LP
351}
352
353static int condition_test_path_is_symbolic_link(Condition *c) {
354 assert(c);
355 assert(c->parameter);
356 assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
357
a4705396 358 return is_symlink(c->parameter) > 0;
d1bddcec
LP
359}
360
361static int condition_test_path_is_mount_point(Condition *c) {
362 assert(c);
363 assert(c->parameter);
364 assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
365
e26d6ce5 366 return path_is_mount_point(c->parameter, AT_SYMLINK_FOLLOW) > 0;
d1bddcec
LP
367}
368
369static int condition_test_path_is_read_write(Condition *c) {
370 assert(c);
371 assert(c->parameter);
372 assert(c->type == CONDITION_PATH_IS_READ_WRITE);
373
a4705396 374 return path_is_read_only_fs(c->parameter) <= 0;
d1bddcec
LP
375}
376
377static int condition_test_directory_not_empty(Condition *c) {
378 int r;
379
380 assert(c);
381 assert(c->parameter);
382 assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
383
384 r = dir_is_empty(c->parameter);
a4705396 385 return r <= 0 && r != -ENOENT;
d1bddcec
LP
386}
387
388static int condition_test_file_not_empty(Condition *c) {
389 struct stat st;
390
391 assert(c);
392 assert(c->parameter);
393 assert(c->type == CONDITION_FILE_NOT_EMPTY);
394
395 return (stat(c->parameter, &st) >= 0 &&
396 S_ISREG(st.st_mode) &&
a4705396 397 st.st_size > 0);
d1bddcec
LP
398}
399
400static int condition_test_file_is_executable(Condition *c) {
401 struct stat st;
402
403 assert(c);
404 assert(c->parameter);
405 assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
406
407 return (stat(c->parameter, &st) >= 0 &&
408 S_ISREG(st.st_mode) &&
a4705396 409 (st.st_mode & 0111));
d1bddcec
LP
410}
411
412static int condition_test_null(Condition *c) {
413 assert(c);
d1bddcec
LP
414 assert(c->type == CONDITION_NULL);
415
416 /* Note that during parsing we already evaluate the string and
417 * store it in c->negate */
a4705396 418 return true;
d1bddcec
LP
419}
420
421int condition_test(Condition *c) {
422
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,
443 };
cc50ef13
LP
444
445 int r, b;
d1bddcec
LP
446
447 assert(c);
448 assert(c->type >= 0);
449 assert(c->type < _CONDITION_TYPE_MAX);
450
a4705396 451 r = condition_tests[c->type](c);
cc50ef13
LP
452 if (r < 0) {
453 c->result = CONDITION_ERROR;
a4705396 454 return r;
cc50ef13 455 }
a4705396 456
cc50ef13
LP
457 b = (r > 0) == !c->negate;
458 c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
459 return b;
d1bddcec
LP
460}
461
59fccdc5 462void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
b77c08e0
TG
463 assert(c);
464 assert(f);
465
466 if (!prefix)
467 prefix = "";
468
469 fprintf(f,
470 "%s\t%s: %s%s%s %s\n",
471 prefix,
59fccdc5 472 to_string(c->type),
b77c08e0
TG
473 c->trigger ? "|" : "",
474 c->negate ? "!" : "",
475 c->parameter,
cc50ef13 476 condition_result_to_string(c->result));
b77c08e0
TG
477}
478
59fccdc5 479void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
b77c08e0
TG
480 Condition *c;
481
482 LIST_FOREACH(conditions, c, first)
59fccdc5 483 condition_dump(c, f, prefix, to_string);
b77c08e0
TG
484}
485
486static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
651c3318
LP
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",
b77c08e0
TG
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",
b77c08e0
TG
505 [CONDITION_NULL] = "ConditionNull"
506};
507
508DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
cc50ef13 509
59fccdc5 510static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
651c3318
LP
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",
59fccdc5
LP
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",
59fccdc5
LP
529 [CONDITION_NULL] = "AssertNull"
530};
531
532DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
533
cc50ef13
LP
534static 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",
539};
540
541DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);