| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "creds-util.h" |
| 7 |
#include "discover-image.h" |
| 8 |
#include "efivars.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "extract-word.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "generator.h" |
| 14 |
#include "import-util.h" |
| 15 |
#include "initrd-util.h" |
| 16 |
#include "json-util.h" |
| 17 |
#include "parse-util.h" |
| 18 |
#include "path-util.h" |
| 19 |
#include "proc-cmdline.h" |
| 20 |
#include "runtime-scope.h" |
| 21 |
#include "specifier.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "unit-name.h" |
| 24 |
#include "web-util.h" |
| 25 |
|
| 26 |
typedef struct Transfer { |
| 27 |
ImageClass class; |
| 28 |
ImportType type; |
| 29 |
char *local; |
| 30 |
char *remote; |
| 31 |
const char *image_root; |
| 32 |
bool blockdev; |
| 33 |
sd_json_variant *json; |
| 34 |
} Transfer; |
| 35 |
|
| 36 |
static const char *arg_dest = NULL; |
| 37 |
static char *arg_success_action = NULL; |
| 38 |
static char *arg_failure_action = NULL; |
| 39 |
static Transfer *arg_transfers = NULL; |
| 40 |
static size_t arg_n_transfers = 0; |
| 41 |
|
| 42 |
static void transfer_destroy_many(Transfer *transfers, size_t n) { |
| 43 |
FOREACH_ARRAY(t, transfers, n) { |
| 44 |
free(t->local); |
| 45 |
free(t->remote); |
| 46 |
sd_json_variant_unref(t->json); |
| 47 |
} |
| 48 |
|
| 49 |
free(transfers); |
| 50 |
} |
| 51 |
|
| 52 |
STATIC_DESTRUCTOR_REGISTER(arg_success_action, freep); |
| 53 |
STATIC_DESTRUCTOR_REGISTER(arg_failure_action, freep); |
| 54 |
STATIC_ARRAY_DESTRUCTOR_REGISTER(arg_transfers, arg_n_transfers, transfer_destroy_many); |
| 55 |
|
| 56 |
static int parse_pull_expression(const char *v) { |
| 57 |
const char *p = v; |
| 58 |
int r; |
| 59 |
|
| 60 |
assert(v); |
| 61 |
|
| 62 |
_cleanup_free_ char *options = NULL; |
| 63 |
r = extract_first_word(&p, &options, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 64 |
if (r < 0) |
| 65 |
return log_error_errno(r, "Failed to extract option string from pull expression '%s': %m", v); |
| 66 |
if (r == 0) |
| 67 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No option string in pull expression '%s'.", v); |
| 68 |
|
| 69 |
_cleanup_free_ char *local = NULL; |
| 70 |
r = extract_first_word(&p, &local, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 71 |
if (r < 0) |
| 72 |
return log_error_errno(r, "Failed to extract local name from pull expression '%s': %m", v); |
| 73 |
if (r == 0) |
| 74 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No local string in pull expression '%s'.", v); |
| 75 |
if (isempty(p)) |
| 76 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No remote string in pull expression '%s'.", v); |
| 77 |
|
| 78 |
_cleanup_free_ char *remote = strdup(p); |
| 79 |
if (!remote) |
| 80 |
return log_oom(); |
| 81 |
|
| 82 |
if (isempty(local)) |
| 83 |
local = mfree(local); |
| 84 |
else if (!image_name_is_valid(local)) |
| 85 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid image name, refusing: %s", local); |
| 86 |
|
| 87 |
ImportType type = _IMPORT_TYPE_INVALID; |
| 88 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 89 |
ImportVerify verify = IMPORT_VERIFY_SIGNATURE; |
| 90 |
bool ro = false, blockdev = false, bootorigin = false, runtime = in_initrd(); |
| 91 |
|
| 92 |
const char *o = options; |
| 93 |
for (;;) { |
| 94 |
_cleanup_free_ char *opt = NULL; |
| 95 |
|
| 96 |
r = extract_first_word(&o, &opt, ",", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 97 |
if (r < 0) |
| 98 |
return log_error_errno(r, "Failed to extract option from pull option expression '%s': %m", options); |
| 99 |
if (r == 0) |
| 100 |
break; |
| 101 |
|
| 102 |
const char *suffix; |
| 103 |
|
| 104 |
if (streq(opt, "ro")) |
| 105 |
ro = true; |
| 106 |
else if (streq(opt, "rw")) |
| 107 |
ro = false; |
| 108 |
else if (streq(opt, "blockdev")) |
| 109 |
blockdev = true; |
| 110 |
else if (streq(opt, "bootorigin")) |
| 111 |
bootorigin = true; |
| 112 |
else if ((suffix = startswith(opt, "runtime="))) { |
| 113 |
r = parse_boolean(suffix); |
| 114 |
if (r < 0) |
| 115 |
log_warning_errno(r, "Unknown runtime= parameter, ignoring: %s", suffix); |
| 116 |
else |
| 117 |
runtime = r; |
| 118 |
} else if ((suffix = startswith(opt, "verify="))) { |
| 119 |
ImportVerify w = import_verify_from_string(suffix); |
| 120 |
|
| 121 |
if (w < 0) |
| 122 |
log_warning_errno(w, "Unknown verification mode, ignoring: %s", suffix); |
| 123 |
else |
| 124 |
verify = w; |
| 125 |
} else { |
| 126 |
ImageClass c; |
| 127 |
|
| 128 |
c = image_class_from_string(opt); |
| 129 |
if (c < 0) { |
| 130 |
ImportType t; |
| 131 |
|
| 132 |
t = import_type_from_string(opt); |
| 133 |
if (t < 0) |
| 134 |
log_warning_errno(c, "Unknown pull option, ignoring: %s", opt); |
| 135 |
else |
| 136 |
type = t; |
| 137 |
} else |
| 138 |
class = c; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (bootorigin) { |
| 143 |
_cleanup_free_ char *stub_url = NULL; |
| 144 |
|
| 145 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("StubDeviceURL"), &stub_url); |
| 146 |
if (r == -ENOENT) { |
| 147 |
log_debug("Option 'bootorigin' specified, but StubDeviceURL EFI variable not set, not scheduling import job for '%s'.", remote); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Failed to read 'StubDeviceURL' EFI variable: %m"); |
| 152 |
|
| 153 |
if (!http_url_is_valid(stub_url)) |
| 154 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Value of 'StubDeviceURL' is not a valid URL, refusing: %s", stub_url); |
| 155 |
|
| 156 |
_cleanup_free_ char *result = NULL; |
| 157 |
r = import_url_change_last_component(stub_url, remote, &result); |
| 158 |
if (r < 0) |
| 159 |
return log_error_errno(r, "Failed to replace last component of URL '%s': %m", stub_url); |
| 160 |
|
| 161 |
log_info("URL reported by StubDeviceURL is '%s', derived download URL '%s' from it.", stub_url, result); |
| 162 |
free_and_replace(remote, result); |
| 163 |
} |
| 164 |
|
| 165 |
if (!http_url_is_valid(remote) && !file_url_is_valid(remote)) |
| 166 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid URL, refusing: %s", remote); |
| 167 |
|
| 168 |
if (type < 0) |
| 169 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No image type (raw, tar) specified in pull expression, refusing: %s", v); |
| 170 |
if (class < 0) |
| 171 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No image class (machine, portable, sysext, confext) specified in pull expression, refusing: %s", v); |
| 172 |
|
| 173 |
if (!local) { |
| 174 |
_cleanup_free_ char *c = NULL; |
| 175 |
r = import_url_last_component(remote, &c); |
| 176 |
if (r < 0) |
| 177 |
return log_error_errno(r, "Failed to generate local name from URL '%s': %m", remote); |
| 178 |
|
| 179 |
switch (type) { |
| 180 |
|
| 181 |
case IMPORT_RAW: |
| 182 |
r = raw_strip_suffixes(c, &local); |
| 183 |
break; |
| 184 |
|
| 185 |
case IMPORT_TAR: |
| 186 |
r = tar_strip_suffixes(c, &local); |
| 187 |
break; |
| 188 |
|
| 189 |
default: |
| 190 |
assert_not_reached(); |
| 191 |
} |
| 192 |
if (r < 0) |
| 193 |
return log_error_errno(r, "Failed to strip suffix from URL '%s': %m", remote); |
| 194 |
|
| 195 |
log_info("Saving downloaded file under local name '%s'.", local); |
| 196 |
} |
| 197 |
|
| 198 |
if (blockdev && type != IMPORT_RAW) |
| 199 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option 'blockdev' only available for raw images, refusing: %s", v); |
| 200 |
|
| 201 |
if (!GREEDY_REALLOC(arg_transfers, arg_n_transfers + 1)) |
| 202 |
return log_oom(); |
| 203 |
|
| 204 |
_cleanup_free_ char *image_root = NULL; |
| 205 |
r = image_root_pick(RUNTIME_SCOPE_SYSTEM, class, runtime, &image_root); |
| 206 |
if (r < 0) |
| 207 |
return log_error_errno(r, "Failed to pick image root: %m"); |
| 208 |
|
| 209 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 210 |
r = sd_json_buildo( |
| 211 |
&j, |
| 212 |
SD_JSON_BUILD_PAIR_STRING("remote", remote), |
| 213 |
SD_JSON_BUILD_PAIR_STRING("local", local), |
| 214 |
JSON_BUILD_PAIR_ENUM("class", image_class_to_string(class)), |
| 215 |
JSON_BUILD_PAIR_ENUM("type", import_type_to_string(type)), |
| 216 |
SD_JSON_BUILD_PAIR_BOOLEAN("readOnly", ro), |
| 217 |
JSON_BUILD_PAIR_ENUM("verify", import_verify_to_string(verify)), |
| 218 |
SD_JSON_BUILD_PAIR_STRING("imageRoot", image_root)); |
| 219 |
if (r < 0) |
| 220 |
return log_error_errno(r, "Failed to build import JSON object: %m"); |
| 221 |
|
| 222 |
arg_transfers[arg_n_transfers++] = (Transfer) { |
| 223 |
.class = class, |
| 224 |
.type = type, |
| 225 |
.local = TAKE_PTR(local), |
| 226 |
.remote = TAKE_PTR(remote), |
| 227 |
.image_root = TAKE_PTR(image_root), |
| 228 |
.json = TAKE_PTR(j), |
| 229 |
.blockdev = blockdev, |
| 230 |
}; |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 236 |
int r; |
| 237 |
|
| 238 |
if (proc_cmdline_key_streq(key, "systemd.pull")) { |
| 239 |
|
| 240 |
if (proc_cmdline_value_missing(key, value)) |
| 241 |
return 0; |
| 242 |
|
| 243 |
r = parse_pull_expression(value); |
| 244 |
if (r < 0) |
| 245 |
log_warning_errno(r, "Failed to parse %s expression, ignoring: %s", key, value); |
| 246 |
|
| 247 |
} else if (proc_cmdline_key_streq(key, "systemd.pull.success_action")) { |
| 248 |
|
| 249 |
if (proc_cmdline_value_missing(key, value)) |
| 250 |
return 0; |
| 251 |
|
| 252 |
return free_and_strdup_warn(&arg_success_action, value); |
| 253 |
|
| 254 |
} else if (proc_cmdline_key_streq(key, "systemd.pull.failure_action")) { |
| 255 |
|
| 256 |
if (proc_cmdline_value_missing(key, value)) |
| 257 |
return 0; |
| 258 |
|
| 259 |
return free_and_strdup_warn(&arg_failure_action, value); |
| 260 |
} |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
static int parse_credentials(void) { |
| 266 |
_cleanup_free_ char *b = NULL; |
| 267 |
size_t sz = 0; |
| 268 |
int r; |
| 269 |
|
| 270 |
r = read_credential_with_decryption("import.pull", (void**) &b, &sz); |
| 271 |
if (r <= 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
_cleanup_fclose_ FILE *f = NULL; |
| 275 |
f = fmemopen_unlocked(b, sz, "r"); |
| 276 |
if (!f) |
| 277 |
return log_oom(); |
| 278 |
|
| 279 |
for (;;) { |
| 280 |
_cleanup_free_ char *item = NULL; |
| 281 |
|
| 282 |
r = read_stripped_line(f, LINE_MAX, &item); |
| 283 |
if (r == 0) |
| 284 |
break; |
| 285 |
if (r < 0) { |
| 286 |
log_error_errno(r, "Failed to parse credential 'import.pull': %m"); |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
if (startswith(item, "#")) |
| 291 |
continue; |
| 292 |
|
| 293 |
r = parse_pull_expression(item); |
| 294 |
if (r < 0) |
| 295 |
log_warning_errno(r, "Failed to parse expression, ignoring: %s", item); |
| 296 |
} |
| 297 |
|
| 298 |
return 0; |
| 299 |
} |
| 300 |
|
| 301 |
static char *transfer_get_local_path(const Transfer *t) { |
| 302 |
assert(t); |
| 303 |
assert(t->image_root); |
| 304 |
assert(t->local); |
| 305 |
|
| 306 |
switch (t->type) { |
| 307 |
case IMPORT_RAW: |
| 308 |
return strjoin(t->image_root, "/", t->local, ".raw"); |
| 309 |
|
| 310 |
case IMPORT_TAR: |
| 311 |
return path_join(t->image_root, t->local); |
| 312 |
|
| 313 |
default: |
| 314 |
assert_not_reached(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
static int transfer_generate(const Transfer *t) { |
| 319 |
int r; |
| 320 |
|
| 321 |
assert(t); |
| 322 |
|
| 323 |
_cleanup_free_ char *local_path = transfer_get_local_path(t); |
| 324 |
if (!local_path) |
| 325 |
return log_oom(); |
| 326 |
|
| 327 |
/* Give this unit a clear name derived from the file system object we are installed into the OS, so |
| 328 |
* that other components can nicely have dependencies on this. */ |
| 329 |
_cleanup_free_ char *service = NULL; |
| 330 |
r = unit_name_from_path_instance("systemd-import", local_path, ".service", &service); |
| 331 |
if (r < 0) |
| 332 |
return log_error_errno(r, "Failed to build import unit name from '%s': %m", local_path); |
| 333 |
|
| 334 |
_cleanup_fclose_ FILE *f = NULL; |
| 335 |
r = generator_open_unit_file(arg_dest, /* source= */ NULL, service, &f); |
| 336 |
if (r < 0) |
| 337 |
return r; |
| 338 |
|
| 339 |
fprintf(f, |
| 340 |
"[Unit]\n" |
| 341 |
"Description=Download of %s\n" |
| 342 |
"Documentation=man:systemd-import-generator(8)\n" |
| 343 |
"SourcePath=/proc/cmdline\n" |
| 344 |
"Requires=systemd-importd.socket\n" |
| 345 |
"After=imports-pre.target systemd-importd.socket\n" |
| 346 |
"Conflicts=shutdown.target\n" |
| 347 |
"Before=imports.target shutdown.target\n" |
| 348 |
"DefaultDependencies=no\n", |
| 349 |
t->remote); |
| 350 |
|
| 351 |
if (arg_success_action) |
| 352 |
fprintf(f, "SuccessAction=%s\n", |
| 353 |
arg_success_action); |
| 354 |
|
| 355 |
if (arg_failure_action) |
| 356 |
fprintf(f, "FailureAction=%s\n", |
| 357 |
arg_failure_action); |
| 358 |
|
| 359 |
if (t->class == IMAGE_SYSEXT) |
| 360 |
fprintf(f, "Before=systemd-sysext%s.service\n", |
| 361 |
in_initrd() ? "-initrd" : ""); |
| 362 |
else if (t->class == IMAGE_CONFEXT) |
| 363 |
fprintf(f, "Before=systemd-confext%s.service\n", |
| 364 |
in_initrd() ? "-initrd" : ""); |
| 365 |
|
| 366 |
/* Assume network resource unless URL is file:// */ |
| 367 |
if (!file_url_is_valid(t->remote)) |
| 368 |
fputs("Wants=network-online.target\n" |
| 369 |
"After=network-online.target\n", f); |
| 370 |
|
| 371 |
_cleanup_free_ char *loop_service = NULL; |
| 372 |
if (t->blockdev) { |
| 373 |
assert(t->type == IMPORT_RAW); |
| 374 |
|
| 375 |
r = unit_name_from_path_instance("systemd-loop", local_path, ".service", &loop_service); |
| 376 |
if (r < 0) |
| 377 |
return log_error_errno(r, "Failed to build [email protected] instance name from path '%s': %m", local_path); |
| 378 |
|
| 379 |
/* Make sure download completes before the loopback service is activated */ |
| 380 |
fprintf(f, "Before=%s\n", loop_service); |
| 381 |
} |
| 382 |
|
| 383 |
fputs("\n" |
| 384 |
"[Service]\n" |
| 385 |
"Type=oneshot\n" |
| 386 |
"NotifyAccess=main\n", f); |
| 387 |
|
| 388 |
_cleanup_free_ char *formatted = NULL; |
| 389 |
r = sd_json_variant_format(t->json, /* flags= */ 0, &formatted); |
| 390 |
if (r < 0) |
| 391 |
return log_error_errno(r, "Failed to format import JSON data: %m"); |
| 392 |
|
| 393 |
_cleanup_free_ char *escaped = specifier_escape(formatted); |
| 394 |
if (!escaped) |
| 395 |
return log_oom(); |
| 396 |
|
| 397 |
fprintf(f, "ExecStart=:varlinkctl call -q --more --timeout=infinity /run/systemd/io.systemd.Import io.systemd.Import.Pull '%s'\n", |
| 398 |
escaped); |
| 399 |
|
| 400 |
r = fflush_and_check(f); |
| 401 |
if (r < 0) |
| 402 |
return log_error_errno(r, "Failed to write unit %s: %m", service); |
| 403 |
|
| 404 |
r = generator_add_symlink(arg_dest, "imports.target", "wants", service); |
| 405 |
if (r < 0) |
| 406 |
return r; |
| 407 |
|
| 408 |
if (loop_service) { |
| 409 |
r = generator_add_symlink(arg_dest, "imports.target", "wants", loop_service); |
| 410 |
if (r < 0) |
| 411 |
return r; |
| 412 |
} |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static int generate(void) { |
| 418 |
int r = 0; |
| 419 |
|
| 420 |
FOREACH_ARRAY(i, arg_transfers, arg_n_transfers) |
| 421 |
RET_GATHER(r, transfer_generate(i)); |
| 422 |
|
| 423 |
return r; |
| 424 |
} |
| 425 |
|
| 426 |
static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 427 |
int r; |
| 428 |
|
| 429 |
assert_se(arg_dest = dest); |
| 430 |
|
| 431 |
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_RD_STRICT|PROC_CMDLINE_STRIP_RD_PREFIX); |
| 432 |
if (r < 0) |
| 433 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 434 |
|
| 435 |
(void) parse_credentials(); |
| 436 |
|
| 437 |
return generate(); |
| 438 |
} |
| 439 |
|
| 440 |
DEFINE_MAIN_GENERATOR_FUNCTION(run); |