]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/condition.c
Merge pull request #4458 from keszybz/man-nonewprivileges
[thirdparty/systemd.git] / src / shared / condition.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <fnmatch.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "sd-id128.h"
31
32 #include "alloc-util.h"
33 #include "apparmor-util.h"
34 #include "architecture.h"
35 #include "audit-util.h"
36 #include "cap-list.h"
37 #include "condition.h"
38 #include "extract-word.h"
39 #include "fd-util.h"
40 #include "fileio.h"
41 #include "glob-util.h"
42 #include "hostname-util.h"
43 #include "ima-util.h"
44 #include "list.h"
45 #include "macro.h"
46 #include "mount-util.h"
47 #include "parse-util.h"
48 #include "path-util.h"
49 #include "proc-cmdline.h"
50 #include "selinux-util.h"
51 #include "smack-util.h"
52 #include "stat-util.h"
53 #include "string-table.h"
54 #include "string-util.h"
55 #include "util.h"
56 #include "virt.h"
57
58 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
59 Condition *c;
60 int r;
61
62 assert(type >= 0);
63 assert(type < _CONDITION_TYPE_MAX);
64 assert((!parameter) == (type == CONDITION_NULL));
65
66 c = new0(Condition, 1);
67 if (!c)
68 return NULL;
69
70 c->type = type;
71 c->trigger = trigger;
72 c->negate = negate;
73
74 r = free_and_strdup(&c->parameter, parameter);
75 if (r < 0) {
76 free(c);
77 return NULL;
78 }
79
80 return c;
81 }
82
83 void condition_free(Condition *c) {
84 assert(c);
85
86 free(c->parameter);
87 free(c);
88 }
89
90 Condition* condition_free_list(Condition *first) {
91 Condition *c, *n;
92
93 LIST_FOREACH_SAFE(conditions, c, n, first)
94 condition_free(c);
95
96 return NULL;
97 }
98
99 static int condition_test_kernel_command_line(Condition *c) {
100 _cleanup_free_ char *line = NULL;
101 const char *p;
102 bool equal;
103 int r;
104
105 assert(c);
106 assert(c->parameter);
107 assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
108
109 r = proc_cmdline(&line);
110 if (r < 0)
111 return r;
112
113 equal = !!strchr(c->parameter, '=');
114 p = line;
115
116 for (;;) {
117 _cleanup_free_ char *word = NULL;
118 bool found;
119
120 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
121 if (r < 0)
122 return r;
123 if (r == 0)
124 break;
125
126 if (equal)
127 found = streq(word, c->parameter);
128 else {
129 const char *f;
130
131 f = startswith(word, c->parameter);
132 found = f && (*f == '=' || *f == 0);
133 }
134
135 if (found)
136 return true;
137 }
138
139 return false;
140 }
141
142 static int condition_test_virtualization(Condition *c) {
143 int b, v;
144
145 assert(c);
146 assert(c->parameter);
147 assert(c->type == CONDITION_VIRTUALIZATION);
148
149 if (streq(c->parameter, "private-users"))
150 return running_in_userns();
151
152 v = detect_virtualization();
153 if (v < 0)
154 return v;
155
156 /* First, compare with yes/no */
157 b = parse_boolean(c->parameter);
158 if (b >= 0)
159 return b == !!v;
160
161 /* Then, compare categorization */
162 if (streq(c->parameter, "vm"))
163 return VIRTUALIZATION_IS_VM(v);
164
165 if (streq(c->parameter, "container"))
166 return VIRTUALIZATION_IS_CONTAINER(v);
167
168 /* Finally compare id */
169 return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
170 }
171
172 static int condition_test_architecture(Condition *c) {
173 int a, b;
174
175 assert(c);
176 assert(c->parameter);
177 assert(c->type == CONDITION_ARCHITECTURE);
178
179 a = uname_architecture();
180 if (a < 0)
181 return a;
182
183 if (streq(c->parameter, "native"))
184 b = native_architecture();
185 else {
186 b = architecture_from_string(c->parameter);
187 if (b < 0) /* unknown architecture? Then it's definitely not ours */
188 return false;
189 }
190
191 return a == b;
192 }
193
194 static int condition_test_host(Condition *c) {
195 _cleanup_free_ char *h = NULL;
196 sd_id128_t x, y;
197 int r;
198
199 assert(c);
200 assert(c->parameter);
201 assert(c->type == CONDITION_HOST);
202
203 if (sd_id128_from_string(c->parameter, &x) >= 0) {
204
205 r = sd_id128_get_machine(&y);
206 if (r < 0)
207 return r;
208
209 return sd_id128_equal(x, y);
210 }
211
212 h = gethostname_malloc();
213 if (!h)
214 return -ENOMEM;
215
216 return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
217 }
218
219 static int condition_test_ac_power(Condition *c) {
220 int r;
221
222 assert(c);
223 assert(c->parameter);
224 assert(c->type == CONDITION_AC_POWER);
225
226 r = parse_boolean(c->parameter);
227 if (r < 0)
228 return r;
229
230 return (on_ac_power() != 0) == !!r;
231 }
232
233 static int condition_test_security(Condition *c) {
234 assert(c);
235 assert(c->parameter);
236 assert(c->type == CONDITION_SECURITY);
237
238 if (streq(c->parameter, "selinux"))
239 return mac_selinux_have();
240 if (streq(c->parameter, "smack"))
241 return mac_smack_use();
242 if (streq(c->parameter, "apparmor"))
243 return mac_apparmor_use();
244 if (streq(c->parameter, "audit"))
245 return use_audit();
246 if (streq(c->parameter, "ima"))
247 return use_ima();
248
249 return false;
250 }
251
252 static int condition_test_capability(Condition *c) {
253 _cleanup_fclose_ FILE *f = NULL;
254 int value;
255 char line[LINE_MAX];
256 unsigned long long capabilities = -1;
257
258 assert(c);
259 assert(c->parameter);
260 assert(c->type == CONDITION_CAPABILITY);
261
262 /* If it's an invalid capability, we don't have it */
263 value = capability_from_name(c->parameter);
264 if (value < 0)
265 return -EINVAL;
266
267 /* If it's a valid capability we default to assume
268 * that we have it */
269
270 f = fopen("/proc/self/status", "re");
271 if (!f)
272 return -errno;
273
274 while (fgets(line, sizeof(line), f)) {
275 truncate_nl(line);
276
277 if (startswith(line, "CapBnd:")) {
278 (void) sscanf(line+7, "%llx", &capabilities);
279 break;
280 }
281 }
282
283 return !!(capabilities & (1ULL << value));
284 }
285
286 static int condition_test_needs_update(Condition *c) {
287 const char *p;
288 struct stat usr, other;
289
290 assert(c);
291 assert(c->parameter);
292 assert(c->type == CONDITION_NEEDS_UPDATE);
293
294 /* If the file system is read-only we shouldn't suggest an update */
295 if (path_is_read_only_fs(c->parameter) > 0)
296 return false;
297
298 /* Any other failure means we should allow the condition to be true,
299 * so that we rather invoke too many update tools than too
300 * few. */
301
302 if (!path_is_absolute(c->parameter))
303 return true;
304
305 p = strjoina(c->parameter, "/.updated");
306 if (lstat(p, &other) < 0)
307 return true;
308
309 if (lstat("/usr/", &usr) < 0)
310 return true;
311
312 /*
313 * First, compare seconds as they are always accurate...
314 */
315 if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec)
316 return usr.st_mtim.tv_sec > other.st_mtim.tv_sec;
317
318 /*
319 * ...then compare nanoseconds.
320 *
321 * A false positive is only possible when /usr's nanoseconds > 0
322 * (otherwise /usr cannot be strictly newer than the target file)
323 * AND the target file's nanoseconds == 0
324 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
325 */
326 if (usr.st_mtim.tv_nsec > 0 && other.st_mtim.tv_nsec == 0) {
327 _cleanup_free_ char *timestamp_str = NULL;
328 uint64_t timestamp;
329 int r;
330
331 r = parse_env_file(p, NULL, "TIMESTAMP_NSEC", &timestamp_str, NULL);
332 if (r < 0) {
333 log_error_errno(r, "Failed to parse timestamp file '%s', using mtime: %m", p);
334 return true;
335 } else if (r == 0) {
336 log_debug("No data in timestamp file '%s', using mtime", p);
337 return true;
338 }
339
340 r = safe_atou64(timestamp_str, &timestamp);
341 if (r < 0) {
342 log_error_errno(r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str, p);
343 return true;
344 }
345
346 timespec_store(&other.st_mtim, timestamp);
347 }
348
349 return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec;
350 }
351
352 static int condition_test_first_boot(Condition *c) {
353 int r;
354
355 assert(c);
356 assert(c->parameter);
357 assert(c->type == CONDITION_FIRST_BOOT);
358
359 r = parse_boolean(c->parameter);
360 if (r < 0)
361 return r;
362
363 return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
364 }
365
366 static int condition_test_path_exists(Condition *c) {
367 assert(c);
368 assert(c->parameter);
369 assert(c->type == CONDITION_PATH_EXISTS);
370
371 return access(c->parameter, F_OK) >= 0;
372 }
373
374 static int condition_test_path_exists_glob(Condition *c) {
375 assert(c);
376 assert(c->parameter);
377 assert(c->type == CONDITION_PATH_EXISTS_GLOB);
378
379 return glob_exists(c->parameter) > 0;
380 }
381
382 static int condition_test_path_is_directory(Condition *c) {
383 assert(c);
384 assert(c->parameter);
385 assert(c->type == CONDITION_PATH_IS_DIRECTORY);
386
387 return is_dir(c->parameter, true) > 0;
388 }
389
390 static int condition_test_path_is_symbolic_link(Condition *c) {
391 assert(c);
392 assert(c->parameter);
393 assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
394
395 return is_symlink(c->parameter) > 0;
396 }
397
398 static int condition_test_path_is_mount_point(Condition *c) {
399 assert(c);
400 assert(c->parameter);
401 assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
402
403 return path_is_mount_point(c->parameter, AT_SYMLINK_FOLLOW) > 0;
404 }
405
406 static int condition_test_path_is_read_write(Condition *c) {
407 assert(c);
408 assert(c->parameter);
409 assert(c->type == CONDITION_PATH_IS_READ_WRITE);
410
411 return path_is_read_only_fs(c->parameter) <= 0;
412 }
413
414 static int condition_test_directory_not_empty(Condition *c) {
415 int r;
416
417 assert(c);
418 assert(c->parameter);
419 assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
420
421 r = dir_is_empty(c->parameter);
422 return r <= 0 && r != -ENOENT;
423 }
424
425 static int condition_test_file_not_empty(Condition *c) {
426 struct stat st;
427
428 assert(c);
429 assert(c->parameter);
430 assert(c->type == CONDITION_FILE_NOT_EMPTY);
431
432 return (stat(c->parameter, &st) >= 0 &&
433 S_ISREG(st.st_mode) &&
434 st.st_size > 0);
435 }
436
437 static int condition_test_file_is_executable(Condition *c) {
438 struct stat st;
439
440 assert(c);
441 assert(c->parameter);
442 assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
443
444 return (stat(c->parameter, &st) >= 0 &&
445 S_ISREG(st.st_mode) &&
446 (st.st_mode & 0111));
447 }
448
449 static int condition_test_null(Condition *c) {
450 assert(c);
451 assert(c->type == CONDITION_NULL);
452
453 /* Note that during parsing we already evaluate the string and
454 * store it in c->negate */
455 return true;
456 }
457
458 int condition_test(Condition *c) {
459
460 static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = {
461 [CONDITION_PATH_EXISTS] = condition_test_path_exists,
462 [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
463 [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
464 [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
465 [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
466 [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
467 [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
468 [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
469 [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
470 [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
471 [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
472 [CONDITION_SECURITY] = condition_test_security,
473 [CONDITION_CAPABILITY] = condition_test_capability,
474 [CONDITION_HOST] = condition_test_host,
475 [CONDITION_AC_POWER] = condition_test_ac_power,
476 [CONDITION_ARCHITECTURE] = condition_test_architecture,
477 [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
478 [CONDITION_FIRST_BOOT] = condition_test_first_boot,
479 [CONDITION_NULL] = condition_test_null,
480 };
481
482 int r, b;
483
484 assert(c);
485 assert(c->type >= 0);
486 assert(c->type < _CONDITION_TYPE_MAX);
487
488 r = condition_tests[c->type](c);
489 if (r < 0) {
490 c->result = CONDITION_ERROR;
491 return r;
492 }
493
494 b = (r > 0) == !c->negate;
495 c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
496 return b;
497 }
498
499 void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
500 assert(c);
501 assert(f);
502
503 if (!prefix)
504 prefix = "";
505
506 fprintf(f,
507 "%s\t%s: %s%s%s %s\n",
508 prefix,
509 to_string(c->type),
510 c->trigger ? "|" : "",
511 c->negate ? "!" : "",
512 c->parameter,
513 condition_result_to_string(c->result));
514 }
515
516 void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
517 Condition *c;
518
519 LIST_FOREACH(conditions, c, first)
520 condition_dump(c, f, prefix, to_string);
521 }
522
523 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
524 [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
525 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
526 [CONDITION_HOST] = "ConditionHost",
527 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
528 [CONDITION_SECURITY] = "ConditionSecurity",
529 [CONDITION_CAPABILITY] = "ConditionCapability",
530 [CONDITION_AC_POWER] = "ConditionACPower",
531 [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
532 [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
533 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
534 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
535 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
536 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
537 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
538 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
539 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
540 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
541 [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
542 [CONDITION_NULL] = "ConditionNull"
543 };
544
545 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
546
547 static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
548 [CONDITION_ARCHITECTURE] = "AssertArchitecture",
549 [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
550 [CONDITION_HOST] = "AssertHost",
551 [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
552 [CONDITION_SECURITY] = "AssertSecurity",
553 [CONDITION_CAPABILITY] = "AssertCapability",
554 [CONDITION_AC_POWER] = "AssertACPower",
555 [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
556 [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
557 [CONDITION_PATH_EXISTS] = "AssertPathExists",
558 [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
559 [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
560 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
561 [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
562 [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
563 [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
564 [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
565 [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
566 [CONDITION_NULL] = "AssertNull"
567 };
568
569 DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
570
571 static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
572 [CONDITION_UNTESTED] = "untested",
573 [CONDITION_SUCCEEDED] = "succeeded",
574 [CONDITION_FAILED] = "failed",
575 [CONDITION_ERROR] = "error",
576 };
577
578 DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);