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