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