Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <errno.h>
4 : #include <fcntl.h>
5 : #include <fnmatch.h>
6 : #include <limits.h>
7 : #include <stdlib.h>
8 : #include <string.h>
9 : #include <sys/stat.h>
10 : #include <sys/types.h>
11 : #include <sys/utsname.h>
12 : #include <time.h>
13 : #include <unistd.h>
14 :
15 : #include "sd-id128.h"
16 :
17 : #include "alloc-util.h"
18 : #include "apparmor-util.h"
19 : #include "architecture.h"
20 : #include "audit-util.h"
21 : #include "cap-list.h"
22 : #include "cgroup-util.h"
23 : #include "condition.h"
24 : #include "cpu-set-util.h"
25 : #include "efivars.h"
26 : #include "env-file.h"
27 : #include "extract-word.h"
28 : #include "fd-util.h"
29 : #include "fileio.h"
30 : #include "glob-util.h"
31 : #include "hostname-util.h"
32 : #include "ima-util.h"
33 : #include "limits-util.h"
34 : #include "list.h"
35 : #include "macro.h"
36 : #include "mountpoint-util.h"
37 : #include "parse-util.h"
38 : #include "path-util.h"
39 : #include "proc-cmdline.h"
40 : #include "process-util.h"
41 : #include "selinux-util.h"
42 : #include "smack-util.h"
43 : #include "stat-util.h"
44 : #include "string-table.h"
45 : #include "string-util.h"
46 : #include "tomoyo-util.h"
47 : #include "user-util.h"
48 : #include "util.h"
49 : #include "virt.h"
50 :
51 166 : Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
52 : Condition *c;
53 :
54 166 : assert(type >= 0);
55 166 : assert(type < _CONDITION_TYPE_MAX);
56 166 : assert((!parameter) == (type == CONDITION_NULL));
57 :
58 166 : c = new(Condition, 1);
59 166 : if (!c)
60 0 : return NULL;
61 :
62 166 : *c = (Condition) {
63 : .type = type,
64 : .trigger = trigger,
65 : .negate = negate,
66 : };
67 :
68 166 : if (parameter) {
69 164 : c->parameter = strdup(parameter);
70 164 : if (!c->parameter)
71 0 : return mfree(c);
72 : }
73 :
74 166 : return c;
75 : }
76 :
77 166 : void condition_free(Condition *c) {
78 166 : assert(c);
79 :
80 166 : free(c->parameter);
81 166 : free(c);
82 166 : }
83 :
84 4347 : Condition* condition_free_list_type(Condition *head, ConditionType type) {
85 : Condition *c, *n;
86 :
87 4348 : LIST_FOREACH_SAFE(conditions, c, n, head)
88 1 : if (type < 0 || c->type == type) {
89 1 : LIST_REMOVE(conditions, head, c);
90 1 : condition_free(c);
91 : }
92 :
93 4347 : assert(type >= 0 || !head);
94 4347 : return head;
95 : }
96 :
97 2 : static int condition_test_kernel_command_line(Condition *c) {
98 2 : _cleanup_free_ char *line = NULL;
99 : const char *p;
100 : bool equal;
101 : int r;
102 :
103 2 : assert(c);
104 2 : assert(c->parameter);
105 2 : assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
106 :
107 2 : r = proc_cmdline(&line);
108 2 : if (r < 0)
109 0 : return r;
110 :
111 2 : equal = strchr(c->parameter, '=');
112 :
113 20 : for (p = line;;) {
114 20 : _cleanup_free_ char *word = NULL;
115 : bool found;
116 :
117 20 : r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
118 20 : if (r < 0)
119 0 : return r;
120 20 : if (r == 0)
121 2 : break;
122 :
123 18 : if (equal)
124 9 : found = streq(word, c->parameter);
125 : else {
126 : const char *f;
127 :
128 9 : f = startswith(word, c->parameter);
129 9 : found = f && IN_SET(*f, 0, '=');
130 : }
131 :
132 18 : if (found)
133 0 : return true;
134 : }
135 :
136 2 : return false;
137 : }
138 :
139 : typedef enum {
140 : /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
141 : * should be listed first. */
142 : ORDER_LOWER_OR_EQUAL,
143 : ORDER_GREATER_OR_EQUAL,
144 : ORDER_LOWER,
145 : ORDER_GREATER,
146 : ORDER_EQUAL,
147 : ORDER_UNEQUAL,
148 : _ORDER_MAX,
149 : _ORDER_INVALID = -1
150 : } OrderOperator;
151 :
152 61 : static OrderOperator parse_order(const char **s) {
153 :
154 : static const char *const prefix[_ORDER_MAX] = {
155 : [ORDER_LOWER_OR_EQUAL] = "<=",
156 : [ORDER_GREATER_OR_EQUAL] = ">=",
157 : [ORDER_LOWER] = "<",
158 : [ORDER_GREATER] = ">",
159 : [ORDER_EQUAL] = "=",
160 : [ORDER_UNEQUAL] = "!=",
161 : };
162 :
163 : OrderOperator i;
164 :
165 221 : for (i = 0; i < _ORDER_MAX; i++) {
166 : const char *e;
167 :
168 217 : e = startswith(*s, prefix[i]);
169 217 : if (e) {
170 57 : *s = e;
171 57 : return i;
172 : }
173 : }
174 :
175 4 : return _ORDER_INVALID;
176 : }
177 :
178 55 : static bool test_order(int k, OrderOperator p) {
179 :
180 55 : switch (p) {
181 :
182 10 : case ORDER_LOWER:
183 10 : return k < 0;
184 :
185 9 : case ORDER_LOWER_OR_EQUAL:
186 9 : return k <= 0;
187 :
188 9 : case ORDER_EQUAL:
189 9 : return k == 0;
190 :
191 6 : case ORDER_UNEQUAL:
192 6 : return k != 0;
193 :
194 9 : case ORDER_GREATER_OR_EQUAL:
195 9 : return k >= 0;
196 :
197 12 : case ORDER_GREATER:
198 12 : return k > 0;
199 :
200 0 : default:
201 0 : assert_not_reached("unknown order");
202 :
203 : }
204 : }
205 :
206 24 : static int condition_test_kernel_version(Condition *c) {
207 : OrderOperator order;
208 : struct utsname u;
209 : const char *p;
210 24 : bool first = true;
211 :
212 24 : assert(c);
213 24 : assert(c->parameter);
214 24 : assert(c->type == CONDITION_KERNEL_VERSION);
215 :
216 24 : assert_se(uname(&u) >= 0);
217 :
218 24 : p = c->parameter;
219 :
220 14 : for (;;) {
221 38 : _cleanup_free_ char *word = NULL;
222 : const char *s;
223 : int r;
224 :
225 38 : r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
226 38 : if (r < 0)
227 0 : return log_debug_errno(r, "Failed to parse condition string \"%s\": %m", p);
228 38 : if (r == 0)
229 13 : break;
230 :
231 25 : s = strstrip(word);
232 25 : order = parse_order(&s);
233 25 : if (order >= 0) {
234 21 : s += strspn(s, WHITESPACE);
235 21 : if (isempty(s)) {
236 16 : if (first) {
237 : /* For backwards compatibility, allow whitespace between the operator and
238 : * value, without quoting, but only in the first expression. */
239 15 : word = mfree(word);
240 15 : r = extract_first_word(&p, &word, NULL, 0);
241 15 : if (r < 0)
242 0 : return log_debug_errno(r, "Failed to parse condition string \"%s\": %m", p);
243 15 : if (r == 0)
244 1 : return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected end of expression: %s", p);
245 14 : s = word;
246 : } else
247 1 : return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected end of expression: %s", p);
248 : }
249 :
250 19 : r = test_order(str_verscmp(u.release, s), order);
251 : } else
252 : /* No prefix? Then treat as glob string */
253 4 : r = fnmatch(s, u.release, 0) == 0;
254 :
255 23 : if (r == 0)
256 9 : return false;
257 :
258 14 : first = false;
259 : }
260 :
261 13 : return true;
262 : }
263 :
264 18 : static int condition_test_memory(Condition *c) {
265 : OrderOperator order;
266 : uint64_t m, k;
267 : const char *p;
268 : int r;
269 :
270 18 : assert(c);
271 18 : assert(c->parameter);
272 18 : assert(c->type == CONDITION_MEMORY);
273 :
274 18 : m = physical_memory();
275 :
276 18 : p = c->parameter;
277 18 : order = parse_order(&p);
278 18 : if (order < 0)
279 0 : order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
280 :
281 18 : r = safe_atou64(p, &k);
282 18 : if (r < 0)
283 0 : return log_debug_errno(r, "Failed to parse size: %m");
284 :
285 18 : return test_order(CMP(m, k), order);
286 : }
287 :
288 18 : static int condition_test_cpus(Condition *c) {
289 : OrderOperator order;
290 : const char *p;
291 : unsigned k;
292 : int r, n;
293 :
294 18 : assert(c);
295 18 : assert(c->parameter);
296 18 : assert(c->type == CONDITION_CPUS);
297 :
298 18 : n = cpus_in_affinity_mask();
299 18 : if (n < 0)
300 0 : return log_debug_errno(n, "Failed to determine CPUs in affinity mask: %m");
301 :
302 18 : p = c->parameter;
303 18 : order = parse_order(&p);
304 18 : if (order < 0)
305 0 : order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
306 :
307 18 : r = safe_atou(p, &k);
308 18 : if (r < 0)
309 0 : return log_debug_errno(r, "Failed to parse number of CPUs: %m");
310 :
311 18 : return test_order(CMP((unsigned) n, k), order);
312 : }
313 :
314 7 : static int condition_test_user(Condition *c) {
315 : uid_t id;
316 : int r;
317 7 : _cleanup_free_ char *username = NULL;
318 : const char *u;
319 :
320 7 : assert(c);
321 7 : assert(c->parameter);
322 7 : assert(c->type == CONDITION_USER);
323 :
324 7 : r = parse_uid(c->parameter, &id);
325 7 : if (r >= 0)
326 2 : return id == getuid() || id == geteuid();
327 :
328 5 : if (streq("@system", c->parameter))
329 1 : return uid_is_system(getuid()) || uid_is_system(geteuid());
330 :
331 4 : username = getusername_malloc();
332 4 : if (!username)
333 0 : return -ENOMEM;
334 :
335 4 : if (streq(username, c->parameter))
336 1 : return 1;
337 :
338 3 : if (getpid_cached() == 1)
339 0 : return streq(c->parameter, "root");
340 :
341 3 : u = c->parameter;
342 3 : r = get_user_creds(&u, &id, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
343 3 : if (r < 0)
344 2 : return 0;
345 :
346 1 : return id == getuid() || id == geteuid();
347 : }
348 :
349 22 : static int condition_test_control_group_controller(Condition *c) {
350 : int r;
351 22 : CGroupMask system_mask, wanted_mask = 0;
352 :
353 22 : assert(c);
354 22 : assert(c->parameter);
355 22 : assert(c->type == CONDITION_CONTROL_GROUP_CONTROLLER);
356 :
357 22 : r = cg_mask_supported(&system_mask);
358 22 : if (r < 0)
359 0 : return log_debug_errno(r, "Failed to determine supported controllers: %m");
360 :
361 22 : r = cg_mask_from_string(c->parameter, &wanted_mask);
362 22 : if (r < 0 || wanted_mask <= 0) {
363 : /* This won't catch the case that we have an unknown controller
364 : * mixed in with valid ones -- these are only assessed on the
365 : * validity of the valid controllers found. */
366 2 : log_debug("Failed to parse cgroup string: %s", c->parameter);
367 2 : return 1;
368 : }
369 :
370 20 : return FLAGS_SET(system_mask, wanted_mask);
371 : }
372 :
373 22 : static int condition_test_group(Condition *c) {
374 : gid_t id;
375 : int r;
376 :
377 22 : assert(c);
378 22 : assert(c->parameter);
379 22 : assert(c->type == CONDITION_GROUP);
380 :
381 22 : r = parse_gid(c->parameter, &id);
382 22 : if (r >= 0)
383 11 : return in_gid(id);
384 :
385 : /* Avoid any NSS lookups if we are PID1 */
386 11 : if (getpid_cached() == 1)
387 0 : return streq(c->parameter, "root");
388 :
389 11 : return in_group(c->parameter) > 0;
390 : }
391 :
392 17 : static int condition_test_virtualization(Condition *c) {
393 : int b, v;
394 :
395 17 : assert(c);
396 17 : assert(c->parameter);
397 17 : assert(c->type == CONDITION_VIRTUALIZATION);
398 :
399 17 : if (streq(c->parameter, "private-users"))
400 1 : return running_in_userns();
401 :
402 16 : v = detect_virtualization();
403 16 : if (v < 0)
404 0 : return v;
405 :
406 : /* First, compare with yes/no */
407 16 : b = parse_boolean(c->parameter);
408 16 : if (b >= 0)
409 0 : return b == !!v;
410 :
411 : /* Then, compare categorization */
412 16 : if (streq(c->parameter, "vm"))
413 1 : return VIRTUALIZATION_IS_VM(v);
414 :
415 15 : if (streq(c->parameter, "container"))
416 2 : return VIRTUALIZATION_IS_CONTAINER(v);
417 :
418 : /* Finally compare id */
419 13 : return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
420 : }
421 :
422 3 : static int condition_test_architecture(Condition *c) {
423 : int a, b;
424 :
425 3 : assert(c);
426 3 : assert(c->parameter);
427 3 : assert(c->type == CONDITION_ARCHITECTURE);
428 :
429 3 : a = uname_architecture();
430 3 : if (a < 0)
431 0 : return a;
432 :
433 3 : if (streq(c->parameter, "native"))
434 0 : b = native_architecture();
435 : else {
436 3 : b = architecture_from_string(c->parameter);
437 3 : if (b < 0) /* unknown architecture? Then it's definitely not ours */
438 1 : return false;
439 : }
440 :
441 2 : return a == b;
442 : }
443 :
444 4 : static int condition_test_host(Condition *c) {
445 4 : _cleanup_free_ char *h = NULL;
446 : sd_id128_t x, y;
447 : int r;
448 :
449 4 : assert(c);
450 4 : assert(c->parameter);
451 4 : assert(c->type == CONDITION_HOST);
452 :
453 4 : if (sd_id128_from_string(c->parameter, &x) >= 0) {
454 :
455 2 : r = sd_id128_get_machine(&y);
456 2 : if (r < 0)
457 0 : return r;
458 :
459 2 : return sd_id128_equal(x, y);
460 : }
461 :
462 2 : h = gethostname_malloc();
463 2 : if (!h)
464 0 : return -ENOMEM;
465 :
466 2 : return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
467 : }
468 :
469 3 : static int condition_test_ac_power(Condition *c) {
470 : int r;
471 :
472 3 : assert(c);
473 3 : assert(c->parameter);
474 3 : assert(c->type == CONDITION_AC_POWER);
475 :
476 3 : r = parse_boolean(c->parameter);
477 3 : if (r < 0)
478 0 : return r;
479 :
480 3 : return (on_ac_power() != 0) == !!r;
481 : }
482 :
483 8 : static int condition_test_security(Condition *c) {
484 8 : assert(c);
485 8 : assert(c->parameter);
486 8 : assert(c->type == CONDITION_SECURITY);
487 :
488 8 : if (streq(c->parameter, "selinux"))
489 1 : return mac_selinux_use();
490 7 : if (streq(c->parameter, "smack"))
491 1 : return mac_smack_use();
492 6 : if (streq(c->parameter, "apparmor"))
493 1 : return mac_apparmor_use();
494 5 : if (streq(c->parameter, "audit"))
495 1 : return use_audit();
496 4 : if (streq(c->parameter, "ima"))
497 1 : return use_ima();
498 3 : if (streq(c->parameter, "tomoyo"))
499 1 : return mac_tomoyo_use();
500 2 : if (streq(c->parameter, "uefi-secureboot"))
501 1 : return is_efi_secure_boot();
502 :
503 1 : return false;
504 : }
505 :
506 0 : static int condition_test_capability(Condition *c) {
507 0 : unsigned long long capabilities = (unsigned long long) -1;
508 0 : _cleanup_fclose_ FILE *f = NULL;
509 : int value, r;
510 :
511 0 : assert(c);
512 0 : assert(c->parameter);
513 0 : assert(c->type == CONDITION_CAPABILITY);
514 :
515 : /* If it's an invalid capability, we don't have it */
516 0 : value = capability_from_name(c->parameter);
517 0 : if (value < 0)
518 0 : return -EINVAL;
519 :
520 : /* If it's a valid capability we default to assume
521 : * that we have it */
522 :
523 0 : f = fopen("/proc/self/status", "re");
524 0 : if (!f)
525 0 : return -errno;
526 :
527 0 : for (;;) {
528 0 : _cleanup_free_ char *line = NULL;
529 : const char *p;
530 :
531 0 : r = read_line(f, LONG_LINE_MAX, &line);
532 0 : if (r < 0)
533 0 : return r;
534 0 : if (r == 0)
535 0 : break;
536 :
537 0 : p = startswith(line, "CapBnd:");
538 0 : if (p) {
539 0 : if (sscanf(line+7, "%llx", &capabilities) != 1)
540 0 : return -EIO;
541 :
542 0 : break;
543 : }
544 : }
545 :
546 0 : return !!(capabilities & (1ULL << value));
547 : }
548 :
549 0 : static int condition_test_needs_update(Condition *c) {
550 : const char *p;
551 : struct stat usr, other;
552 :
553 0 : assert(c);
554 0 : assert(c->parameter);
555 0 : assert(c->type == CONDITION_NEEDS_UPDATE);
556 :
557 : /* If the file system is read-only we shouldn't suggest an update */
558 0 : if (path_is_read_only_fs(c->parameter) > 0)
559 0 : return false;
560 :
561 : /* Any other failure means we should allow the condition to be true,
562 : * so that we rather invoke too many update tools than too
563 : * few. */
564 :
565 0 : if (!path_is_absolute(c->parameter))
566 0 : return true;
567 :
568 0 : p = strjoina(c->parameter, "/.updated");
569 0 : if (lstat(p, &other) < 0)
570 0 : return true;
571 :
572 0 : if (lstat("/usr/", &usr) < 0)
573 0 : return true;
574 :
575 : /*
576 : * First, compare seconds as they are always accurate...
577 : */
578 0 : if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec)
579 0 : return usr.st_mtim.tv_sec > other.st_mtim.tv_sec;
580 :
581 : /*
582 : * ...then compare nanoseconds.
583 : *
584 : * A false positive is only possible when /usr's nanoseconds > 0
585 : * (otherwise /usr cannot be strictly newer than the target file)
586 : * AND the target file's nanoseconds == 0
587 : * (otherwise the filesystem supports nsec timestamps, see stat(2)).
588 : */
589 0 : if (usr.st_mtim.tv_nsec > 0 && other.st_mtim.tv_nsec == 0) {
590 0 : _cleanup_free_ char *timestamp_str = NULL;
591 : uint64_t timestamp;
592 : int r;
593 :
594 0 : r = parse_env_file(NULL, p, "TIMESTAMP_NSEC", ×tamp_str);
595 0 : if (r < 0) {
596 0 : log_error_errno(r, "Failed to parse timestamp file '%s', using mtime: %m", p);
597 0 : return true;
598 0 : } else if (r == 0) {
599 0 : log_debug("No data in timestamp file '%s', using mtime", p);
600 0 : return true;
601 : }
602 :
603 0 : r = safe_atou64(timestamp_str, ×tamp);
604 0 : if (r < 0) {
605 0 : log_error_errno(r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str, p);
606 0 : return true;
607 : }
608 :
609 0 : timespec_store(&other.st_mtim, timestamp);
610 : }
611 :
612 0 : return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec;
613 : }
614 :
615 0 : static int condition_test_first_boot(Condition *c) {
616 : int r;
617 :
618 0 : assert(c);
619 0 : assert(c->parameter);
620 0 : assert(c->type == CONDITION_FIRST_BOOT);
621 :
622 0 : r = parse_boolean(c->parameter);
623 0 : if (r < 0)
624 0 : return r;
625 :
626 0 : return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
627 : }
628 :
629 4 : static int condition_test_path_exists(Condition *c) {
630 4 : assert(c);
631 4 : assert(c->parameter);
632 4 : assert(c->type == CONDITION_PATH_EXISTS);
633 :
634 4 : return access(c->parameter, F_OK) >= 0;
635 : }
636 :
637 2 : static int condition_test_path_exists_glob(Condition *c) {
638 2 : assert(c);
639 2 : assert(c->parameter);
640 2 : assert(c->type == CONDITION_PATH_EXISTS_GLOB);
641 :
642 2 : return glob_exists(c->parameter) > 0;
643 : }
644 :
645 1 : static int condition_test_path_is_directory(Condition *c) {
646 1 : assert(c);
647 1 : assert(c->parameter);
648 1 : assert(c->type == CONDITION_PATH_IS_DIRECTORY);
649 :
650 1 : return is_dir(c->parameter, true) > 0;
651 : }
652 :
653 1 : static int condition_test_path_is_symbolic_link(Condition *c) {
654 1 : assert(c);
655 1 : assert(c->parameter);
656 1 : assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
657 :
658 1 : return is_symlink(c->parameter) > 0;
659 : }
660 :
661 3 : static int condition_test_path_is_mount_point(Condition *c) {
662 3 : assert(c);
663 3 : assert(c->parameter);
664 3 : assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
665 :
666 3 : return path_is_mount_point(c->parameter, NULL, AT_SYMLINK_FOLLOW) > 0;
667 : }
668 :
669 1 : static int condition_test_path_is_read_write(Condition *c) {
670 1 : assert(c);
671 1 : assert(c->parameter);
672 1 : assert(c->type == CONDITION_PATH_IS_READ_WRITE);
673 :
674 1 : return path_is_read_only_fs(c->parameter) <= 0;
675 : }
676 :
677 1 : static int condition_test_directory_not_empty(Condition *c) {
678 : int r;
679 :
680 1 : assert(c);
681 1 : assert(c->parameter);
682 1 : assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
683 :
684 1 : r = dir_is_empty(c->parameter);
685 1 : return r <= 0 && r != -ENOENT;
686 : }
687 :
688 1 : static int condition_test_file_not_empty(Condition *c) {
689 : struct stat st;
690 :
691 1 : assert(c);
692 1 : assert(c->parameter);
693 1 : assert(c->type == CONDITION_FILE_NOT_EMPTY);
694 :
695 1 : return (stat(c->parameter, &st) >= 0 &&
696 2 : S_ISREG(st.st_mode) &&
697 1 : st.st_size > 0);
698 : }
699 :
700 2 : static int condition_test_file_is_executable(Condition *c) {
701 : struct stat st;
702 :
703 2 : assert(c);
704 2 : assert(c->parameter);
705 2 : assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
706 :
707 2 : return (stat(c->parameter, &st) >= 0 &&
708 4 : S_ISREG(st.st_mode) &&
709 2 : (st.st_mode & 0111));
710 : }
711 :
712 2 : static int condition_test_null(Condition *c) {
713 2 : assert(c);
714 2 : assert(c->type == CONDITION_NULL);
715 :
716 : /* Note that during parsing we already evaluate the string and
717 : * store it in c->negate */
718 2 : return true;
719 : }
720 :
721 166 : int condition_test(Condition *c) {
722 :
723 : static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = {
724 : [CONDITION_PATH_EXISTS] = condition_test_path_exists,
725 : [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
726 : [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
727 : [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
728 : [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
729 : [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
730 : [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
731 : [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
732 : [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
733 : [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
734 : [CONDITION_KERNEL_VERSION] = condition_test_kernel_version,
735 : [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
736 : [CONDITION_SECURITY] = condition_test_security,
737 : [CONDITION_CAPABILITY] = condition_test_capability,
738 : [CONDITION_HOST] = condition_test_host,
739 : [CONDITION_AC_POWER] = condition_test_ac_power,
740 : [CONDITION_ARCHITECTURE] = condition_test_architecture,
741 : [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
742 : [CONDITION_FIRST_BOOT] = condition_test_first_boot,
743 : [CONDITION_USER] = condition_test_user,
744 : [CONDITION_GROUP] = condition_test_group,
745 : [CONDITION_CONTROL_GROUP_CONTROLLER] = condition_test_control_group_controller,
746 : [CONDITION_NULL] = condition_test_null,
747 : [CONDITION_CPUS] = condition_test_cpus,
748 : [CONDITION_MEMORY] = condition_test_memory,
749 : };
750 :
751 : int r, b;
752 :
753 166 : assert(c);
754 166 : assert(c->type >= 0);
755 166 : assert(c->type < _CONDITION_TYPE_MAX);
756 :
757 166 : r = condition_tests[c->type](c);
758 166 : if (r < 0) {
759 2 : c->result = CONDITION_ERROR;
760 2 : return r;
761 : }
762 :
763 164 : b = (r > 0) == !c->negate;
764 164 : c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
765 164 : return b;
766 : }
767 :
768 88 : bool condition_test_list(Condition *first, const char *(*to_string)(ConditionType t), condition_test_logger_t logger, void *userdata) {
769 : Condition *c;
770 88 : int triggered = -1;
771 :
772 88 : assert(!!logger == !!to_string);
773 :
774 : /* If the condition list is empty, then it is true */
775 88 : if (!first)
776 87 : return true;
777 :
778 : /* Otherwise, if all of the non-trigger conditions apply and
779 : * if any of the trigger conditions apply (unless there are
780 : * none) we return true */
781 1 : LIST_FOREACH(conditions, c, first) {
782 : int r;
783 :
784 1 : r = condition_test(c);
785 :
786 1 : if (logger) {
787 0 : const char *p = c->type == CONDITION_NULL ? "true" : c->parameter;
788 0 : assert(p);
789 :
790 0 : if (r < 0)
791 0 : logger(userdata, LOG_WARNING, r, PROJECT_FILE, __LINE__, __func__,
792 : "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
793 0 : to_string(c->type),
794 0 : c->trigger ? "|" : "",
795 0 : c->negate ? "!" : "",
796 : p);
797 : else
798 0 : logger(userdata, LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__,
799 : "%s=%s%s%s %s.",
800 0 : to_string(c->type),
801 0 : c->trigger ? "|" : "",
802 0 : c->negate ? "!" : "",
803 : p,
804 0 : condition_result_to_string(c->result));
805 : }
806 :
807 1 : if (!c->trigger && r <= 0)
808 1 : return false;
809 :
810 0 : if (c->trigger && triggered <= 0)
811 0 : triggered = r > 0;
812 : }
813 :
814 0 : return triggered != 0;
815 : }
816 :
817 0 : void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
818 0 : assert(c);
819 0 : assert(f);
820 :
821 0 : prefix = strempty(prefix);
822 :
823 0 : fprintf(f,
824 : "%s\t%s: %s%s%s %s\n",
825 : prefix,
826 0 : to_string(c->type),
827 0 : c->trigger ? "|" : "",
828 0 : c->negate ? "!" : "",
829 : c->parameter,
830 0 : condition_result_to_string(c->result));
831 0 : }
832 :
833 2380 : void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
834 : Condition *c;
835 :
836 2380 : LIST_FOREACH(conditions, c, first)
837 0 : condition_dump(c, f, prefix, to_string);
838 2380 : }
839 :
840 : static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
841 : [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
842 : [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
843 : [CONDITION_HOST] = "ConditionHost",
844 : [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
845 : [CONDITION_KERNEL_VERSION] = "ConditionKernelVersion",
846 : [CONDITION_SECURITY] = "ConditionSecurity",
847 : [CONDITION_CAPABILITY] = "ConditionCapability",
848 : [CONDITION_AC_POWER] = "ConditionACPower",
849 : [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
850 : [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
851 : [CONDITION_PATH_EXISTS] = "ConditionPathExists",
852 : [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
853 : [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
854 : [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
855 : [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
856 : [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
857 : [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
858 : [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
859 : [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
860 : [CONDITION_USER] = "ConditionUser",
861 : [CONDITION_GROUP] = "ConditionGroup",
862 : [CONDITION_CONTROL_GROUP_CONTROLLER] = "ConditionControlGroupController",
863 : [CONDITION_NULL] = "ConditionNull",
864 : [CONDITION_CPUS] = "ConditionCPUs",
865 : [CONDITION_MEMORY] = "ConditionMemory",
866 : };
867 :
868 90 : DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
869 :
870 : static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
871 : [CONDITION_ARCHITECTURE] = "AssertArchitecture",
872 : [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
873 : [CONDITION_HOST] = "AssertHost",
874 : [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
875 : [CONDITION_KERNEL_VERSION] = "AssertKernelVersion",
876 : [CONDITION_SECURITY] = "AssertSecurity",
877 : [CONDITION_CAPABILITY] = "AssertCapability",
878 : [CONDITION_AC_POWER] = "AssertACPower",
879 : [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
880 : [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
881 : [CONDITION_PATH_EXISTS] = "AssertPathExists",
882 : [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
883 : [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
884 : [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
885 : [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
886 : [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
887 : [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
888 : [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
889 : [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
890 : [CONDITION_USER] = "AssertUser",
891 : [CONDITION_GROUP] = "AssertGroup",
892 : [CONDITION_CONTROL_GROUP_CONTROLLER] = "AssertControlGroupController",
893 : [CONDITION_NULL] = "AssertNull",
894 : [CONDITION_CPUS] = "AssertCPUs",
895 : [CONDITION_MEMORY] = "AssertMemory",
896 : };
897 :
898 54 : DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
899 :
900 : static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
901 : [CONDITION_UNTESTED] = "untested",
902 : [CONDITION_SUCCEEDED] = "succeeded",
903 : [CONDITION_FAILED] = "failed",
904 : [CONDITION_ERROR] = "error",
905 : };
906 :
907 12 : DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);
|