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