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