]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/condition.c
tree-wide: make invocations of extract_first_word more uniform (#4627)
[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
115 for (p = line;;) {
116 _cleanup_free_ char *word = NULL;
117 bool found;
118
119 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
120 if (r < 0)
121 return r;
122 if (r == 0)
123 break;
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);
132 }
133
134 if (found)
135 return true;
136 }
137
138 return false;
139 }
140
141 static int condition_test_virtualization(Condition *c) {
142 int b, v;
143
144 assert(c);
145 assert(c->parameter);
146 assert(c->type == CONDITION_VIRTUALIZATION);
147
148 if (streq(c->parameter, "private-users"))
149 return running_in_userns();
150
151 v = detect_virtualization();
152 if (v < 0)
153 return v;
154
155 /* First, compare with yes/no */
156 b = parse_boolean(c->parameter);
157 if (b >= 0)
158 return b == !!v;
159
160 /* Then, compare categorization */
161 if (streq(c->parameter, "vm"))
162 return VIRTUALIZATION_IS_VM(v);
163
164 if (streq(c->parameter, "container"))
165 return VIRTUALIZATION_IS_CONTAINER(v);
166
167 /* Finally compare id */
168 return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
169 }
170
171 static int condition_test_architecture(Condition *c) {
172 int a, b;
173
174 assert(c);
175 assert(c->parameter);
176 assert(c->type == CONDITION_ARCHITECTURE);
177
178 a = uname_architecture();
179 if (a < 0)
180 return a;
181
182 if (streq(c->parameter, "native"))
183 b = native_architecture();
184 else {
185 b = architecture_from_string(c->parameter);
186 if (b < 0) /* unknown architecture? Then it's definitely not ours */
187 return false;
188 }
189
190 return a == b;
191 }
192
193 static int condition_test_host(Condition *c) {
194 _cleanup_free_ char *h = NULL;
195 sd_id128_t x, y;
196 int r;
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)
206 return r;
207
208 return sd_id128_equal(x, y);
209 }
210
211 h = gethostname_malloc();
212 if (!h)
213 return -ENOMEM;
214
215 return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
216 }
217
218 static int condition_test_ac_power(Condition *c) {
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)
227 return r;
228
229 return (on_ac_power() != 0) == !!r;
230 }
231
232 static 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"))
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"))
244 return use_audit();
245 if (streq(c->parameter, "ima"))
246 return use_ima();
247
248 return false;
249 }
250
251 static int condition_test_capability(Condition *c) {
252 _cleanup_fclose_ FILE *f = NULL;
253 int value;
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 */
262 value = capability_from_name(c->parameter);
263 if (value < 0)
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
282 return !!(capabilities & (1ULL << value));
283 }
284
285 static 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)
295 return false;
296
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
299 * few. */
300
301 if (!path_is_absolute(c->parameter))
302 return true;
303
304 p = strjoina(c->parameter, "/.updated");
305 if (lstat(p, &other) < 0)
306 return true;
307
308 if (lstat("/usr/", &usr) < 0)
309 return true;
310
311 /*
312 * First, compare seconds as they are always accurate...
313 */
314 if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec)
315 return usr.st_mtim.tv_sec > other.st_mtim.tv_sec;
316
317 /*
318 * ...then compare nanoseconds.
319 *
320 * A false positive is only possible when /usr's nanoseconds > 0
321 * (otherwise /usr cannot be strictly newer than the target file)
322 * AND the target file's nanoseconds == 0
323 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
324 */
325 if (usr.st_mtim.tv_nsec > 0 && other.st_mtim.tv_nsec == 0) {
326 _cleanup_free_ char *timestamp_str = NULL;
327 uint64_t timestamp;
328 int r;
329
330 r = parse_env_file(p, NULL, "TIMESTAMP_NSEC", &timestamp_str, NULL);
331 if (r < 0) {
332 log_error_errno(r, "Failed to parse timestamp file '%s', using mtime: %m", p);
333 return true;
334 } else if (r == 0) {
335 log_debug("No data in timestamp file '%s', using mtime", p);
336 return true;
337 }
338
339 r = safe_atou64(timestamp_str, &timestamp);
340 if (r < 0) {
341 log_error_errno(r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str, p);
342 return true;
343 }
344
345 timespec_store(&other.st_mtim, timestamp);
346 }
347
348 return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec;
349 }
350
351 static int condition_test_first_boot(Condition *c) {
352 int r;
353
354 assert(c);
355 assert(c->parameter);
356 assert(c->type == CONDITION_FIRST_BOOT);
357
358 r = parse_boolean(c->parameter);
359 if (r < 0)
360 return r;
361
362 return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
363 }
364
365 static int condition_test_path_exists(Condition *c) {
366 assert(c);
367 assert(c->parameter);
368 assert(c->type == CONDITION_PATH_EXISTS);
369
370 return access(c->parameter, F_OK) >= 0;
371 }
372
373 static int condition_test_path_exists_glob(Condition *c) {
374 assert(c);
375 assert(c->parameter);
376 assert(c->type == CONDITION_PATH_EXISTS_GLOB);
377
378 return glob_exists(c->parameter) > 0;
379 }
380
381 static int condition_test_path_is_directory(Condition *c) {
382 assert(c);
383 assert(c->parameter);
384 assert(c->type == CONDITION_PATH_IS_DIRECTORY);
385
386 return is_dir(c->parameter, true) > 0;
387 }
388
389 static int condition_test_path_is_symbolic_link(Condition *c) {
390 assert(c);
391 assert(c->parameter);
392 assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
393
394 return is_symlink(c->parameter) > 0;
395 }
396
397 static int condition_test_path_is_mount_point(Condition *c) {
398 assert(c);
399 assert(c->parameter);
400 assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
401
402 return path_is_mount_point(c->parameter, AT_SYMLINK_FOLLOW) > 0;
403 }
404
405 static int condition_test_path_is_read_write(Condition *c) {
406 assert(c);
407 assert(c->parameter);
408 assert(c->type == CONDITION_PATH_IS_READ_WRITE);
409
410 return path_is_read_only_fs(c->parameter) <= 0;
411 }
412
413 static int condition_test_directory_not_empty(Condition *c) {
414 int r;
415
416 assert(c);
417 assert(c->parameter);
418 assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
419
420 r = dir_is_empty(c->parameter);
421 return r <= 0 && r != -ENOENT;
422 }
423
424 static int condition_test_file_not_empty(Condition *c) {
425 struct stat st;
426
427 assert(c);
428 assert(c->parameter);
429 assert(c->type == CONDITION_FILE_NOT_EMPTY);
430
431 return (stat(c->parameter, &st) >= 0 &&
432 S_ISREG(st.st_mode) &&
433 st.st_size > 0);
434 }
435
436 static int condition_test_file_is_executable(Condition *c) {
437 struct stat st;
438
439 assert(c);
440 assert(c->parameter);
441 assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
442
443 return (stat(c->parameter, &st) >= 0 &&
444 S_ISREG(st.st_mode) &&
445 (st.st_mode & 0111));
446 }
447
448 static int condition_test_null(Condition *c) {
449 assert(c);
450 assert(c->type == CONDITION_NULL);
451
452 /* Note that during parsing we already evaluate the string and
453 * store it in c->negate */
454 return true;
455 }
456
457 int condition_test(Condition *c) {
458
459 static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = {
460 [CONDITION_PATH_EXISTS] = condition_test_path_exists,
461 [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
462 [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
463 [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
464 [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
465 [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
466 [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
467 [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
468 [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
469 [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
470 [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
471 [CONDITION_SECURITY] = condition_test_security,
472 [CONDITION_CAPABILITY] = condition_test_capability,
473 [CONDITION_HOST] = condition_test_host,
474 [CONDITION_AC_POWER] = condition_test_ac_power,
475 [CONDITION_ARCHITECTURE] = condition_test_architecture,
476 [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
477 [CONDITION_FIRST_BOOT] = condition_test_first_boot,
478 [CONDITION_NULL] = condition_test_null,
479 };
480
481 int r, b;
482
483 assert(c);
484 assert(c->type >= 0);
485 assert(c->type < _CONDITION_TYPE_MAX);
486
487 r = condition_tests[c->type](c);
488 if (r < 0) {
489 c->result = CONDITION_ERROR;
490 return r;
491 }
492
493 b = (r > 0) == !c->negate;
494 c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
495 return b;
496 }
497
498 void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
499 assert(c);
500 assert(f);
501
502 if (!prefix)
503 prefix = "";
504
505 fprintf(f,
506 "%s\t%s: %s%s%s %s\n",
507 prefix,
508 to_string(c->type),
509 c->trigger ? "|" : "",
510 c->negate ? "!" : "",
511 c->parameter,
512 condition_result_to_string(c->result));
513 }
514
515 void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
516 Condition *c;
517
518 LIST_FOREACH(conditions, c, first)
519 condition_dump(c, f, prefix, to_string);
520 }
521
522 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
523 [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
524 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
525 [CONDITION_HOST] = "ConditionHost",
526 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
527 [CONDITION_SECURITY] = "ConditionSecurity",
528 [CONDITION_CAPABILITY] = "ConditionCapability",
529 [CONDITION_AC_POWER] = "ConditionACPower",
530 [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
531 [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
532 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
533 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
534 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
535 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
536 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
537 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
538 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
539 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
540 [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
541 [CONDITION_NULL] = "ConditionNull"
542 };
543
544 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
545
546 static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
547 [CONDITION_ARCHITECTURE] = "AssertArchitecture",
548 [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
549 [CONDITION_HOST] = "AssertHost",
550 [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
551 [CONDITION_SECURITY] = "AssertSecurity",
552 [CONDITION_CAPABILITY] = "AssertCapability",
553 [CONDITION_AC_POWER] = "AssertACPower",
554 [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
555 [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
556 [CONDITION_PATH_EXISTS] = "AssertPathExists",
557 [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
558 [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
559 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
560 [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
561 [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
562 [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
563 [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
564 [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
565 [CONDITION_NULL] = "AssertNull"
566 };
567
568 DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
569
570 static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
571 [CONDITION_UNTESTED] = "untested",
572 [CONDITION_SUCCEEDED] = "succeeded",
573 [CONDITION_FAILED] = "failed",
574 [CONDITION_ERROR] = "error",
575 };
576
577 DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);