| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "fileio.h" |
| 8 |
#include "fstab-util.h" |
| 9 |
#include "generator.h" |
| 10 |
#include "integrity-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "specifier.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "unit-name.h" |
| 16 |
|
| 17 |
static const char *arg_dest = NULL; |
| 18 |
static const char *arg_integritytab = NULL; |
| 19 |
static char *arg_options = NULL; |
| 20 |
STATIC_DESTRUCTOR_REGISTER(arg_options, freep); |
| 21 |
|
| 22 |
static int create_disk( |
| 23 |
const char *name, |
| 24 |
const char *device, |
| 25 |
const char *key_file, |
| 26 |
const char *options) { |
| 27 |
|
| 28 |
_cleanup_free_ char *n = NULL, *dd = NULL, *e = NULL, *name_escaped = NULL, *key_file_escaped = NULL; |
| 29 |
_cleanup_fclose_ FILE *f = NULL; |
| 30 |
char *dmname = NULL; |
| 31 |
bool noauto, nofail, netdev; |
| 32 |
int r; |
| 33 |
|
| 34 |
assert(name); |
| 35 |
assert(device); |
| 36 |
|
| 37 |
noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
| 38 |
nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); |
| 39 |
netdev = fstab_test_option(options, "_netdev\0"); |
| 40 |
|
| 41 |
name_escaped = specifier_escape(name); |
| 42 |
if (!name_escaped) |
| 43 |
return log_oom(); |
| 44 |
|
| 45 |
e = unit_name_escape(name); |
| 46 |
if (!e) |
| 47 |
return log_oom(); |
| 48 |
|
| 49 |
r = unit_name_build("systemd-integritysetup", e, ".service", &n); |
| 50 |
if (r < 0) |
| 51 |
return log_error_errno(r, "Failed to generate unit name: %m"); |
| 52 |
|
| 53 |
r = unit_name_from_path(device, ".device", &dd); |
| 54 |
if (r < 0) |
| 55 |
return log_error_errno(r, "Failed to generate unit name: %m"); |
| 56 |
|
| 57 |
r = generator_open_unit_file(arg_dest, NULL, n, &f); |
| 58 |
if (r < 0) |
| 59 |
return r; |
| 60 |
|
| 61 |
if (key_file) { |
| 62 |
if (!path_is_absolute(key_file)) |
| 63 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "key file not absolute file path %s", key_file); |
| 64 |
|
| 65 |
key_file_escaped = specifier_escape(key_file); |
| 66 |
if (!key_file_escaped) |
| 67 |
return log_oom(); |
| 68 |
} |
| 69 |
|
| 70 |
if (options) { |
| 71 |
r = parse_integrity_options(options, NULL, NULL, NULL, NULL, NULL); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
} |
| 75 |
|
| 76 |
fprintf(f, |
| 77 |
"[Unit]\n" |
| 78 |
"Description=Integrity Setup for %%I\n" |
| 79 |
"Documentation=man:integritytab(5) man:systemd-integritysetup-generator(8) man:[email protected](8)\n" |
| 80 |
"SourcePath=%s\n" |
| 81 |
"DefaultDependencies=no\n" |
| 82 |
"IgnoreOnIsolate=true\n" |
| 83 |
"After=integritysetup-pre.target systemd-udevd-kernel.socket\n" |
| 84 |
"Before=blockdev@dev-mapper-%%i.target\n" |
| 85 |
"Wants=blockdev@dev-mapper-%%i.target\n" |
| 86 |
"Conflicts=umount.target\n" |
| 87 |
"BindsTo=%s\n" |
| 88 |
"After=%s\n" |
| 89 |
"Before=umount.target\n", |
| 90 |
arg_integritytab, |
| 91 |
dd, dd); |
| 92 |
|
| 93 |
if (netdev) |
| 94 |
fprintf(f, "After=remote-fs-pre.target\n"); |
| 95 |
|
| 96 |
if (!nofail) |
| 97 |
fprintf(f, |
| 98 |
"Before=%s\n", |
| 99 |
netdev ? "remote-integritysetup.target" : "integritysetup.target"); |
| 100 |
|
| 101 |
fprintf(f, |
| 102 |
"\n" |
| 103 |
"[Service]\n" |
| 104 |
"Type=oneshot\n" |
| 105 |
"RemainAfterExit=yes\n" |
| 106 |
"TimeoutSec=infinity\n" |
| 107 |
"ExecStart=" SYSTEMD_INTEGRITYSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
| 108 |
"ExecStop=" SYSTEMD_INTEGRITYSETUP_PATH " detach '%s'\n", |
| 109 |
name_escaped, device, empty_to_dash(key_file_escaped), empty_to_dash(options), |
| 110 |
name_escaped); |
| 111 |
|
| 112 |
r = fflush_and_check(f); |
| 113 |
if (r < 0) |
| 114 |
return log_error_errno(r, "Failed to write unit file %s: %m", n); |
| 115 |
|
| 116 |
if (!noauto) { |
| 117 |
r = generator_add_symlink( |
| 118 |
arg_dest, |
| 119 |
netdev ? "remote-integritysetup.target" : "integritysetup.target", |
| 120 |
nofail ? "wants" : "requires", |
| 121 |
n); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
} |
| 125 |
|
| 126 |
dmname = strjoina("dev-mapper-", e, ".device"); |
| 127 |
return generator_add_symlink(arg_dest, dmname, "requires", n); |
| 128 |
} |
| 129 |
|
| 130 |
static int add_integritytab_devices(void) { |
| 131 |
_cleanup_fclose_ FILE *f = NULL; |
| 132 |
unsigned integritytab_line = 0; |
| 133 |
int r; |
| 134 |
|
| 135 |
r = fopen_unlocked(arg_integritytab, "re", &f); |
| 136 |
if (r < 0) { |
| 137 |
if (errno != ENOENT) |
| 138 |
log_error_errno(errno, "Failed to open %s: %m", arg_integritytab); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
for (;;) { |
| 143 |
_cleanup_free_ char *line = NULL, *name = NULL, *device_id = NULL, *device_path = NULL, *key_file = NULL, *options = NULL; |
| 144 |
|
| 145 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 146 |
if (r < 0) |
| 147 |
return log_error_errno(r, "Failed to read %s: %m", arg_integritytab); |
| 148 |
if (r == 0) |
| 149 |
break; |
| 150 |
|
| 151 |
integritytab_line++; |
| 152 |
|
| 153 |
if (IN_SET(line[0], 0, '#')) |
| 154 |
continue; |
| 155 |
|
| 156 |
/* The key file and the options are optional */ |
| 157 |
r = sscanf(line, "%ms %ms %ms %ms", &name, &device_id, &key_file, &options); |
| 158 |
if (!IN_SET(r, 2, 3, 4)) { |
| 159 |
log_error("Failed to parse %s:%u, ignoring.", arg_integritytab, integritytab_line); |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
device_path = fstab_node_to_udev_node(device_id); |
| 164 |
if (!device_path) { |
| 165 |
log_error("Failed to find device %s:%u, ignoring.", device_id, integritytab_line); |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
r = create_disk(name, device_path, empty_or_dash_to_null(key_file), empty_or_dash_to_null(options)); |
| 170 |
if (r < 0) |
| 171 |
return r; |
| 172 |
} |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 178 |
assert_se(arg_dest = dest); |
| 179 |
|
| 180 |
arg_integritytab = getenv("SYSTEMD_INTEGRITYTAB") ?: "/etc/integritytab"; |
| 181 |
|
| 182 |
return add_integritytab_devices(); |
| 183 |
} |
| 184 |
|
| 185 |
DEFINE_MAIN_GENERATOR_FUNCTION(run); |