]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/condition.c
Merge pull request #4123 from keszybz/network-file-dropins
[thirdparty/systemd.git] / src / shared / condition.c
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
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <fnmatch.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "sd-id128.h"
31
32 #include "alloc-util.h"
33 #include "apparmor-util.h"
34 #include "architecture.h"
35 #include "audit-util.h"
36 #include "cap-list.h"
37 #include "condition.h"
38 #include "extract-word.h"
39 #include "fd-util.h"
40 #include "fileio.h"
41 #include "glob-util.h"
42 #include "hostname-util.h"
43 #include "ima-util.h"
44 #include "list.h"
45 #include "macro.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"
55 #include "util.h"
56 #include "virt.h"
57
58 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
59 Condition *c;
60 int r;
61
62 assert(type >= 0);
63 assert(type < _CONDITION_TYPE_MAX);
64 assert((!parameter) == (type == CONDITION_NULL));
65
66 c = new0(Condition, 1);
67 if (!c)
68 return NULL;
69
70 c->type = type;
71 c->trigger = trigger;
72 c->negate = negate;
73
74 r = free_and_strdup(&c->parameter, parameter);
75 if (r < 0) {
76 free(c);
77 return NULL;
78 }
79
80 return c;
81 }
82
83 void condition_free(Condition *c) {
84 assert(c);
85
86 free(c->parameter);
87 free(c);
88 }
89
90 Condition* condition_free_list(Condition *first) {
91 Condition *c, *n;
92
93 LIST_FOREACH_SAFE(conditions, c, n, first)
94 condition_free(c);
95
96 return NULL;
97 }
98
99 static int condition_test_kernel_command_line(Condition *c) {
100 _cleanup_free_ char *line = NULL;
101 const char *p;
102 bool equal;
103 int r;
104
105 assert(c);
106 assert(c->parameter);
107 assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
108
109 r = proc_cmdline(&line);
110 if (r < 0)
111 return r;
112
113 equal = !!strchr(c->parameter, '=');
114 p = line;
115
116 for (;;) {
117 _cleanup_free_ char *word = NULL;
118 bool found;
119
120 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
121 if (r < 0)
122 return r;
123 if (r == 0)
124 break;
125
126 if (equal)
127 found = streq(word, c->parameter);
128 else {
129 const char *f;
130
131 f = startswith(word, c->parameter);
132 found = f && (*f == '=' || *f == 0);
133 }
134
135 if (found)
136 return true;
137 }
138
139 return false;
140 }
141
142 static int condition_test_virtualization(Condition *c) {
143 int b, v;
144
145 assert(c);
146 assert(c->parameter);
147 assert(c->type == CONDITION_VIRTUALIZATION);
148
149 v = detect_virtualization();
150 if (v < 0)
151 return v;
152
153 /* First, compare with yes/no */
154 b = parse_boolean(c->parameter);
155
156 if (v > 0 && b > 0)
157 return true;
158
159 if (v == 0 && b == 0)
160 return true;
161
162 /* Then, compare categorization */
163 if (VIRTUALIZATION_IS_VM(v) && streq(c->parameter, "vm"))
164 return true;
165
166 if (VIRTUALIZATION_IS_CONTAINER(v) && streq(c->parameter, "container"))
167 return true;
168
169 /* Finally compare id */
170 return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
171 }
172
173 static int condition_test_architecture(Condition *c) {
174 int a, b;
175
176 assert(c);
177 assert(c->parameter);
178 assert(c->type == CONDITION_ARCHITECTURE);
179
180 a = uname_architecture();
181 if (a < 0)
182 return a;
183
184 if (streq(c->parameter, "native"))
185 b = native_architecture();
186 else {
187 b = architecture_from_string(c->parameter);
188 if (b < 0) /* unknown architecture? Then it's definitely not ours */
189 return false;
190 }
191
192 return a == b;
193 }
194
195 static int condition_test_host(Condition *c) {
196 _cleanup_free_ char *h = NULL;
197 sd_id128_t x, y;
198 int r;
199
200 assert(c);
201 assert(c->parameter);
202 assert(c->type == CONDITION_HOST);
203
204 if (sd_id128_from_string(c->parameter, &x) >= 0) {
205
206 r = sd_id128_get_machine(&y);
207 if (r < 0)
208 return r;
209
210 return sd_id128_equal(x, y);
211 }
212
213 h = gethostname_malloc();
214 if (!h)
215 return -ENOMEM;
216
217 return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
218 }
219
220 static int condition_test_ac_power(Condition *c) {
221 int r;
222
223 assert(c);
224 assert(c->parameter);
225 assert(c->type == CONDITION_AC_POWER);
226
227 r = parse_boolean(c->parameter);
228 if (r < 0)
229 return r;
230
231 return (on_ac_power() != 0) == !!r;
232 }
233
234 static int condition_test_security(Condition *c) {
235 assert(c);
236 assert(c->parameter);
237 assert(c->type == CONDITION_SECURITY);
238
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"))
246 return use_audit();
247 if (streq(c->parameter, "ima"))
248 return use_ima();
249
250 return false;
251 }
252
253 static int condition_test_capability(Condition *c) {
254 _cleanup_fclose_ FILE *f = NULL;
255 int value;
256 char line[LINE_MAX];
257 unsigned long long capabilities = -1;
258
259 assert(c);
260 assert(c->parameter);
261 assert(c->type == CONDITION_CAPABILITY);
262
263 /* If it's an invalid capability, we don't have it */
264 value = capability_from_name(c->parameter);
265 if (value < 0)
266 return -EINVAL;
267
268 /* If it's a valid capability we default to assume
269 * that we have it */
270
271 f = fopen("/proc/self/status", "re");
272 if (!f)
273 return -errno;
274
275 while (fgets(line, sizeof(line), f)) {
276 truncate_nl(line);
277
278 if (startswith(line, "CapBnd:")) {
279 (void) sscanf(line+7, "%llx", &capabilities);
280 break;
281 }
282 }
283
284 return !!(capabilities & (1ULL << value));
285 }
286
287 static int condition_test_needs_update(Condition *c) {
288 const char *p;
289 struct stat usr, other;
290
291 assert(c);
292 assert(c->parameter);
293 assert(c->type == CONDITION_NEEDS_UPDATE);
294
295 /* If the file system is read-only we shouldn't suggest an update */
296 if (path_is_read_only_fs(c->parameter) > 0)
297 return false;
298
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
301 * few. */
302
303 if (!path_is_absolute(c->parameter))
304 return true;
305
306 p = strjoina(c->parameter, "/.updated");
307 if (lstat(p, &other) < 0)
308 return true;
309
310 if (lstat("/usr/", &usr) < 0)
311 return true;
312
313 /*
314 * First, compare seconds as they are always accurate...
315 */
316 if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec)
317 return usr.st_mtim.tv_sec > other.st_mtim.tv_sec;
318
319 /*
320 * ...then compare nanoseconds.
321 *
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)).
326 */
327 if (usr.st_mtim.tv_nsec > 0 && other.st_mtim.tv_nsec == 0) {
328 _cleanup_free_ char *timestamp_str = NULL;
329 uint64_t timestamp;
330 int r;
331
332 r = parse_env_file(p, NULL, "TimestampNSec", &timestamp_str, NULL);
333 if (r < 0) {
334 log_error_errno(-r, "Failed to parse timestamp file '%s', using mtime: %m", p);
335 return true;
336 } else if (r == 0) {
337 log_debug("No data in timestamp file '%s', using mtime", p);
338 return true;
339 }
340
341 r = safe_atou64(timestamp_str, &timestamp);
342 if (r < 0) {
343 log_error_errno(-r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m",
344 timestamp_str, p);
345 return true;
346 }
347
348 other.st_mtim.tv_nsec = timestamp % NSEC_PER_SEC;
349 }
350
351 return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec;
352 }
353
354 static int condition_test_first_boot(Condition *c) {
355 int r;
356
357 assert(c);
358 assert(c->parameter);
359 assert(c->type == CONDITION_FIRST_BOOT);
360
361 r = parse_boolean(c->parameter);
362 if (r < 0)
363 return r;
364
365 return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
366 }
367
368 static int condition_test_path_exists(Condition *c) {
369 assert(c);
370 assert(c->parameter);
371 assert(c->type == CONDITION_PATH_EXISTS);
372
373 return access(c->parameter, F_OK) >= 0;
374 }
375
376 static int condition_test_path_exists_glob(Condition *c) {
377 assert(c);
378 assert(c->parameter);
379 assert(c->type == CONDITION_PATH_EXISTS_GLOB);
380
381 return glob_exists(c->parameter) > 0;
382 }
383
384 static int condition_test_path_is_directory(Condition *c) {
385 assert(c);
386 assert(c->parameter);
387 assert(c->type == CONDITION_PATH_IS_DIRECTORY);
388
389 return is_dir(c->parameter, true) > 0;
390 }
391
392 static int condition_test_path_is_symbolic_link(Condition *c) {
393 assert(c);
394 assert(c->parameter);
395 assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
396
397 return is_symlink(c->parameter) > 0;
398 }
399
400 static int condition_test_path_is_mount_point(Condition *c) {
401 assert(c);
402 assert(c->parameter);
403 assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
404
405 return path_is_mount_point(c->parameter, AT_SYMLINK_FOLLOW) > 0;
406 }
407
408 static int condition_test_path_is_read_write(Condition *c) {
409 assert(c);
410 assert(c->parameter);
411 assert(c->type == CONDITION_PATH_IS_READ_WRITE);
412
413 return path_is_read_only_fs(c->parameter) <= 0;
414 }
415
416 static int condition_test_directory_not_empty(Condition *c) {
417 int r;
418
419 assert(c);
420 assert(c->parameter);
421 assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
422
423 r = dir_is_empty(c->parameter);
424 return r <= 0 && r != -ENOENT;
425 }
426
427 static int condition_test_file_not_empty(Condition *c) {
428 struct stat st;
429
430 assert(c);
431 assert(c->parameter);
432 assert(c->type == CONDITION_FILE_NOT_EMPTY);
433
434 return (stat(c->parameter, &st) >= 0 &&
435 S_ISREG(st.st_mode) &&
436 st.st_size > 0);
437 }
438
439 static int condition_test_file_is_executable(Condition *c) {
440 struct stat st;
441
442 assert(c);
443 assert(c->parameter);
444 assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
445
446 return (stat(c->parameter, &st) >= 0 &&
447 S_ISREG(st.st_mode) &&
448 (st.st_mode & 0111));
449 }
450
451 static int condition_test_null(Condition *c) {
452 assert(c);
453 assert(c->type == CONDITION_NULL);
454
455 /* Note that during parsing we already evaluate the string and
456 * store it in c->negate */
457 return true;
458 }
459
460 int condition_test(Condition *c) {
461
462 static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = {
463 [CONDITION_PATH_EXISTS] = condition_test_path_exists,
464 [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
465 [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
466 [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
467 [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
468 [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
469 [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
470 [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
471 [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
472 [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
473 [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
474 [CONDITION_SECURITY] = condition_test_security,
475 [CONDITION_CAPABILITY] = condition_test_capability,
476 [CONDITION_HOST] = condition_test_host,
477 [CONDITION_AC_POWER] = condition_test_ac_power,
478 [CONDITION_ARCHITECTURE] = condition_test_architecture,
479 [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
480 [CONDITION_FIRST_BOOT] = condition_test_first_boot,
481 [CONDITION_NULL] = condition_test_null,
482 };
483
484 int r, b;
485
486 assert(c);
487 assert(c->type >= 0);
488 assert(c->type < _CONDITION_TYPE_MAX);
489
490 r = condition_tests[c->type](c);
491 if (r < 0) {
492 c->result = CONDITION_ERROR;
493 return r;
494 }
495
496 b = (r > 0) == !c->negate;
497 c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
498 return b;
499 }
500
501 void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
502 assert(c);
503 assert(f);
504
505 if (!prefix)
506 prefix = "";
507
508 fprintf(f,
509 "%s\t%s: %s%s%s %s\n",
510 prefix,
511 to_string(c->type),
512 c->trigger ? "|" : "",
513 c->negate ? "!" : "",
514 c->parameter,
515 condition_result_to_string(c->result));
516 }
517
518 void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
519 Condition *c;
520
521 LIST_FOREACH(conditions, c, first)
522 condition_dump(c, f, prefix, to_string);
523 }
524
525 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
526 [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
527 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
528 [CONDITION_HOST] = "ConditionHost",
529 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
530 [CONDITION_SECURITY] = "ConditionSecurity",
531 [CONDITION_CAPABILITY] = "ConditionCapability",
532 [CONDITION_AC_POWER] = "ConditionACPower",
533 [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
534 [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
535 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
536 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
537 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
538 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
539 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
540 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
541 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
542 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
543 [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
544 [CONDITION_NULL] = "ConditionNull"
545 };
546
547 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
548
549 static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
550 [CONDITION_ARCHITECTURE] = "AssertArchitecture",
551 [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
552 [CONDITION_HOST] = "AssertHost",
553 [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
554 [CONDITION_SECURITY] = "AssertSecurity",
555 [CONDITION_CAPABILITY] = "AssertCapability",
556 [CONDITION_AC_POWER] = "AssertACPower",
557 [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
558 [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
559 [CONDITION_PATH_EXISTS] = "AssertPathExists",
560 [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
561 [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
562 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
563 [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
564 [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
565 [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
566 [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
567 [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
568 [CONDITION_NULL] = "AssertNull"
569 };
570
571 DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
572
573 static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
574 [CONDITION_UNTESTED] = "untested",
575 [CONDITION_SUCCEEDED] = "succeeded",
576 [CONDITION_FAILED] = "failed",
577 [CONDITION_ERROR] = "error",
578 };
579
580 DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);