]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/pull-common.c
Merge pull request #10976 from yuwata/typesafe-netlink-call
[thirdparty/systemd.git] / src / import / pull-common.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
56ebfaf1 2
98c38001
LP
3#include <sys/prctl.h>
4
b5efdb8a 5#include "alloc-util.h"
56ebfaf1 6#include "btrfs-util.h"
430f0182 7#include "capability-util.h"
4f5dd394 8#include "copy.h"
a0956174 9#include "dirent-util.h"
4f5dd394 10#include "escape.h"
3ffd4af2 11#include "fd-util.h"
c004493c 12#include "io-util.h"
bb15fafe 13#include "path-util.h"
0b452006 14#include "process-util.h"
3ffd4af2 15#include "pull-common.h"
4f5dd394
LP
16#include "pull-job.h"
17#include "rm-rf.h"
24882e06 18#include "signal-util.h"
9818005d 19#include "siphash24.h"
07630cea 20#include "string-util.h"
4f5dd394
LP
21#include "strv.h"
22#include "util.h"
49cf4170 23#include "web-util.h"
56ebfaf1
LP
24
25#define FILENAME_ESCAPE "/.#\"\'"
9818005d 26#define HASH_URL_THRESHOLD_LENGTH (_POSIX_PATH_MAX - 16)
56ebfaf1 27
9854730b
LP
28int pull_find_old_etags(
29 const char *url,
30 const char *image_root,
31 int dt,
32 const char *prefix,
33 const char *suffix,
34 char ***etags) {
35
56ebfaf1
LP
36 _cleanup_free_ char *escaped_url = NULL;
37 _cleanup_closedir_ DIR *d = NULL;
38 _cleanup_strv_free_ char **l = NULL;
39 struct dirent *de;
40 int r;
41
42 assert(url);
43 assert(etags);
44
45 if (!image_root)
46 image_root = "/var/lib/machines";
47
48 escaped_url = xescape(url, FILENAME_ESCAPE);
49 if (!escaped_url)
50 return -ENOMEM;
51
52 d = opendir(image_root);
53 if (!d) {
54 if (errno == ENOENT) {
55 *etags = NULL;
56 return 0;
57 }
58
59 return -errno;
60 }
61
62 FOREACH_DIRENT_ALL(de, d, return -errno) {
63 const char *a, *b;
64 char *u;
65
66 if (de->d_type != DT_UNKNOWN &&
67 de->d_type != dt)
68 continue;
69
70 if (prefix) {
71 a = startswith(de->d_name, prefix);
72 if (!a)
73 continue;
74 } else
75 a = de->d_name;
76
77 a = startswith(a, escaped_url);
78 if (!a)
79 continue;
80
81 a = startswith(a, ".");
82 if (!a)
83 continue;
84
85 if (suffix) {
86 b = endswith(de->d_name, suffix);
87 if (!b)
88 continue;
89 } else
90 b = strchr(de->d_name, 0);
91
92 if (a >= b)
93 continue;
94
527b7a42
LP
95 r = cunescape_length(a, b - a, 0, &u);
96 if (r < 0)
97 return r;
56ebfaf1
LP
98
99 if (!http_etag_is_valid(u)) {
100 free(u);
101 continue;
102 }
103
104 r = strv_consume(&l, u);
105 if (r < 0)
106 return r;
107 }
108
ae2a15bc 109 *etags = TAKE_PTR(l);
56ebfaf1
LP
110
111 return 0;
112}
113
dc2c282b 114int pull_make_local_copy(const char *final, const char *image_root, const char *local, bool force_local) {
56ebfaf1
LP
115 const char *p;
116 int r;
117
118 assert(final);
119 assert(local);
120
121 if (!image_root)
122 image_root = "/var/lib/machines";
123
63c372cb 124 p = strjoina(image_root, "/", local);
56ebfaf1 125
d9e2daaf
LP
126 if (force_local)
127 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
56ebfaf1 128
17cbb288
LP
129 r = btrfs_subvol_snapshot(final, p,
130 BTRFS_SNAPSHOT_QUOTA|
131 BTRFS_SNAPSHOT_FALLBACK_COPY|
132 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY|
133 BTRFS_SNAPSHOT_RECURSIVE);
134 if (r < 0)
56ebfaf1
LP
135 return log_error_errno(r, "Failed to create local image: %m");
136
137 log_info("Created new local image '%s'.", local);
138
139 return 0;
140}
141
9818005d
JS
142static int hash_url(const char *url, char **ret) {
143 uint64_t h;
144 static const sd_id128_t k = SD_ID128_ARRAY(df,89,16,87,01,cc,42,30,98,ab,4a,19,a6,a5,63,4f);
145
146 assert(url);
147
933f9cae 148 h = siphash24(url, strlen(url), k.bytes);
9818005d
JS
149 if (asprintf(ret, "%"PRIx64, h) < 0)
150 return -ENOMEM;
151
152 return 0;
153}
154
dc2c282b 155int pull_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) {
9818005d 156 _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL;
56ebfaf1
LP
157 char *path;
158
159 assert(url);
160 assert(ret);
161
162 if (!image_root)
163 image_root = "/var/lib/machines";
164
165 escaped_url = xescape(url, FILENAME_ESCAPE);
166 if (!escaped_url)
167 return -ENOMEM;
168
169 if (etag) {
56ebfaf1
LP
170 escaped_etag = xescape(etag, FILENAME_ESCAPE);
171 if (!escaped_etag)
172 return -ENOMEM;
9818005d 173 }
56ebfaf1 174
9818005d 175 path = strjoin(image_root, "/", strempty(prefix), escaped_url, escaped_etag ? "." : "",
4600a396 176 strempty(escaped_etag), strempty(suffix));
56ebfaf1
LP
177 if (!path)
178 return -ENOMEM;
179
9818005d
JS
180 /* URLs might make the path longer than the maximum allowed length for a file name.
181 * When that happens, a URL hash is used instead. Paths returned by this function
182 * can be later used with tempfn_random() which adds 16 bytes to the resulting name. */
183 if (strlen(path) >= HASH_URL_THRESHOLD_LENGTH) {
184 _cleanup_free_ char *hash = NULL;
185 int r;
186
187 free(path);
188
189 r = hash_url(url, &hash);
190 if (r < 0)
191 return r;
192
193 path = strjoin(image_root, "/", strempty(prefix), hash, escaped_etag ? "." : "",
4600a396 194 strempty(escaped_etag), strempty(suffix));
9818005d
JS
195 if (!path)
196 return -ENOMEM;
197 }
198
56ebfaf1
LP
199 *ret = path;
200 return 0;
201}
85dbc41d 202
91359193 203int pull_make_auxiliary_job(
9854730b
LP
204 PullJob **ret,
205 const char *url,
91359193
LP
206 int (*strip_suffixes)(const char *name, char **ret),
207 const char *suffix,
9854730b
LP
208 CurlGlue *glue,
209 PullJobFinished on_finished,
210 void *userdata) {
211
91359193 212 _cleanup_free_ char *last_component = NULL, *ll = NULL, *auxiliary_url = NULL;
9854730b
LP
213 _cleanup_(pull_job_unrefp) PullJob *job = NULL;
214 const char *q;
215 int r;
216
217 assert(ret);
218 assert(url);
91359193 219 assert(strip_suffixes);
9854730b
LP
220 assert(glue);
221
222 r = import_url_last_component(url, &last_component);
223 if (r < 0)
224 return r;
225
91359193 226 r = strip_suffixes(last_component, &ll);
9854730b
LP
227 if (r < 0)
228 return r;
229
91359193 230 q = strjoina(ll, suffix);
9854730b 231
91359193 232 r = import_url_change_last_component(url, q, &auxiliary_url);
9854730b
LP
233 if (r < 0)
234 return r;
235
91359193 236 r = pull_job_new(&job, auxiliary_url, glue, userdata);
9854730b
LP
237 if (r < 0)
238 return r;
239
240 job->on_finished = on_finished;
241 job->compressed_max = job->uncompressed_max = 1ULL * 1024ULL * 1024ULL;
242
1cc6c93a 243 *ret = TAKE_PTR(job);
9854730b
LP
244
245 return 0;
246}
247
dc2c282b
LP
248int pull_make_verification_jobs(
249 PullJob **ret_checksum_job,
250 PullJob **ret_signature_job,
98c38001
LP
251 ImportVerify verify,
252 const char *url,
253 CurlGlue *glue,
dc2c282b 254 PullJobFinished on_finished,
98c38001
LP
255 void *userdata) {
256
dc2c282b 257 _cleanup_(pull_job_unrefp) PullJob *checksum_job = NULL, *signature_job = NULL;
98c38001 258 int r;
697be0be 259 const char *chksums = NULL;
98c38001
LP
260
261 assert(ret_checksum_job);
262 assert(ret_signature_job);
263 assert(verify >= 0);
264 assert(verify < _IMPORT_VERIFY_MAX);
265 assert(url);
266 assert(glue);
267
268 if (verify != IMPORT_VERIFY_NO) {
697be0be 269 _cleanup_free_ char *checksum_url = NULL, *fn = NULL;
98c38001 270
697be0be
TB
271 /* Queue jobs for the checksum file for the image. */
272 r = import_url_last_component(url, &fn);
273 if (r < 0)
274 return r;
275
276 chksums = strjoina(fn, ".sha256");
277
278 r = import_url_change_last_component(url, chksums, &checksum_url);
98c38001
LP
279 if (r < 0)
280 return r;
281
dc2c282b 282 r = pull_job_new(&checksum_job, checksum_url, glue, userdata);
98c38001
LP
283 if (r < 0)
284 return r;
285
286 checksum_job->on_finished = on_finished;
287 checksum_job->uncompressed_max = checksum_job->compressed_max = 1ULL * 1024ULL * 1024ULL;
288 }
289
290 if (verify == IMPORT_VERIFY_SIGNATURE) {
291 _cleanup_free_ char *signature_url = NULL;
292
293 /* Queue job for the SHA256SUMS.gpg file for the image. */
294 r = import_url_change_last_component(url, "SHA256SUMS.gpg", &signature_url);
295 if (r < 0)
296 return r;
297
dc2c282b 298 r = pull_job_new(&signature_job, signature_url, glue, userdata);
98c38001
LP
299 if (r < 0)
300 return r;
301
302 signature_job->on_finished = on_finished;
303 signature_job->uncompressed_max = signature_job->compressed_max = 1ULL * 1024ULL * 1024ULL;
304 }
305
306 *ret_checksum_job = checksum_job;
307 *ret_signature_job = signature_job;
308
309 checksum_job = signature_job = NULL;
310
311 return 0;
312}
313
91359193 314static int verify_one(PullJob *checksum_job, PullJob *job) {
98c38001 315 _cleanup_free_ char *fn = NULL;
91359193 316 const char *line, *p;
98c38001
LP
317 int r;
318
91359193 319 assert(checksum_job);
98c38001 320
91359193 321 if (!job)
98c38001
LP
322 return 0;
323
91359193 324 assert(IN_SET(job->state, PULL_JOB_DONE, PULL_JOB_FAILED));
98c38001 325
91359193
LP
326 /* Don't verify the checksum if we didn't actually successfully download something new */
327 if (job->state != PULL_JOB_DONE)
328 return 0;
329 if (job->error != 0)
330 return 0;
331 if (job->etag_exists)
332 return 0;
98c38001 333
91359193
LP
334 assert(job->calc_checksum);
335 assert(job->checksum);
336
337 r = import_url_last_component(job->url, &fn);
98c38001
LP
338 if (r < 0)
339 return log_oom();
340
baaa35ad
ZJS
341 if (!filename_is_valid(fn))
342 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
343 "Cannot verify checksum, could not determine server-side file name.");
98c38001 344
91359193 345 line = strjoina(job->checksum, " *", fn, "\n");
98c38001
LP
346
347 p = memmem(checksum_job->payload,
348 checksum_job->payload_size,
349 line,
350 strlen(line));
351
697be0be
TB
352 if (!p) {
353 line = strjoina(job->checksum, " ", fn, "\n");
354
355 p = memmem(checksum_job->payload,
356 checksum_job->payload_size,
357 line,
358 strlen(line));
359 }
360
baaa35ad
ZJS
361 if (!p || (p != (char*) checksum_job->payload && p[-1] != '\n'))
362 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
363 "DOWNLOAD INVALID: Checksum of %s file did not checkout, file has been tampered with.", fn);
98c38001 364
91359193
LP
365 log_info("SHA256 checksum of %s is valid.", job->url);
366 return 1;
367}
98c38001 368
91359193
LP
369int pull_verify(PullJob *main_job,
370 PullJob *roothash_job,
371 PullJob *settings_job,
372 PullJob *checksum_job,
373 PullJob *signature_job) {
9854730b 374
91359193 375 _cleanup_close_pair_ int gpg_pipe[2] = { -1, -1 };
91359193
LP
376 _cleanup_close_ int sig_file = -1;
377 char sig_file_path[] = "/tmp/sigXXXXXX", gpg_home[] = "/tmp/gpghomeXXXXXX";
378 _cleanup_(sigkill_waitp) pid_t pid = 0;
379 bool gpg_home_created = false;
380 int r;
9854730b 381
91359193
LP
382 assert(main_job);
383 assert(main_job->state == PULL_JOB_DONE);
9854730b 384
91359193
LP
385 if (!checksum_job)
386 return 0;
9854730b 387
91359193
LP
388 assert(main_job->calc_checksum);
389 assert(main_job->checksum);
9854730b 390
91359193 391 assert(checksum_job->state == PULL_JOB_DONE);
9854730b 392
91359193
LP
393 if (!checksum_job->payload || checksum_job->payload_size <= 0) {
394 log_error("Checksum is empty, cannot verify.");
395 return -EBADMSG;
396 }
9854730b 397
91359193
LP
398 r = verify_one(checksum_job, main_job);
399 if (r < 0)
400 return r;
9854730b 401
91359193
LP
402 r = verify_one(checksum_job, roothash_job);
403 if (r < 0)
404 return r;
9854730b 405
91359193
LP
406 r = verify_one(checksum_job, settings_job);
407 if (r < 0)
408 return r;
9854730b 409
98c38001
LP
410 if (!signature_job)
411 return 0;
412
697be0be
TB
413 if (checksum_job->style == VERIFICATION_PER_FILE)
414 signature_job = checksum_job;
415
dc2c282b 416 assert(signature_job->state == PULL_JOB_DONE);
98c38001
LP
417
418 if (!signature_job->payload || signature_job->payload_size <= 0) {
419 log_error("Signature is empty, cannot verify.");
420 return -EBADMSG;
421 }
422
423 r = pipe2(gpg_pipe, O_CLOEXEC);
424 if (r < 0)
0100b6e1 425 return log_error_errno(errno, "Failed to create pipe for gpg: %m");
98c38001
LP
426
427 sig_file = mkostemp(sig_file_path, O_RDWR);
428 if (sig_file < 0)
429 return log_error_errno(errno, "Failed to create temporary file: %m");
430
431 r = loop_write(sig_file, signature_job->payload, signature_job->payload_size, false);
432 if (r < 0) {
433 log_error_errno(r, "Failed to write to temporary file: %m");
434 goto finish;
435 }
436
0acfdffe
LP
437 if (!mkdtemp(gpg_home)) {
438 r = log_error_errno(errno, "Failed to create tempory home for gpg: %m");
439 goto finish;
440 }
441
442 gpg_home_created = true;
443
b6e1fff1 444 r = safe_fork("(gpg)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
4c253ed1 445 if (r < 0)
b6e1fff1 446 return r;
4c253ed1 447 if (r == 0) {
98c38001
LP
448 const char *cmd[] = {
449 "gpg",
450 "--no-options",
451 "--no-default-keyring",
452 "--no-auto-key-locate",
453 "--no-auto-check-trustdb",
454 "--batch",
455 "--trust-model=always",
0acfdffe
LP
456 NULL, /* --homedir= */
457 NULL, /* --keyring= */
98c38001
LP
458 NULL, /* --verify */
459 NULL, /* signature file */
460 NULL, /* dash */
461 NULL /* trailing NULL */
462 };
0acfdffe 463 unsigned k = ELEMENTSOF(cmd) - 6;
98c38001
LP
464
465 /* Child */
466
98c38001
LP
467 gpg_pipe[1] = safe_close(gpg_pipe[1]);
468
2b33ab09 469 r = rearrange_stdio(gpg_pipe[0], -1, STDERR_FILENO);
046a82c1 470 if (r < 0) {
2b33ab09 471 log_error_errno(r, "Failed to rearrange stdin/stdout: %m");
98c38001
LP
472 _exit(EXIT_FAILURE);
473 }
474
0acfdffe
LP
475 cmd[k++] = strjoina("--homedir=", gpg_home);
476
98c38001
LP
477 /* We add the user keyring only to the command line
478 * arguments, if it's around since gpg fails
479 * otherwise. */
480 if (access(USER_KEYRING_PATH, F_OK) >= 0)
481 cmd[k++] = "--keyring=" USER_KEYRING_PATH;
1c49d1ba
LP
482 else
483 cmd[k++] = "--keyring=" VENDOR_KEYRING_PATH;
98c38001
LP
484
485 cmd[k++] = "--verify";
697be0be
TB
486 if (checksum_job->style == VERIFICATION_PER_DIRECTORY) {
487 cmd[k++] = sig_file_path;
488 cmd[k++] = "-";
489 cmd[k++] = NULL;
490 }
98c38001 491
0acfdffe 492 execvp("gpg2", (char * const *) cmd);
98c38001
LP
493 execvp("gpg", (char * const *) cmd);
494 log_error_errno(errno, "Failed to execute gpg: %m");
495 _exit(EXIT_FAILURE);
496 }
497
498 gpg_pipe[0] = safe_close(gpg_pipe[0]);
499
500 r = loop_write(gpg_pipe[1], checksum_job->payload, checksum_job->payload_size, false);
501 if (r < 0) {
502 log_error_errno(r, "Failed to write to pipe: %m");
503 goto finish;
504 }
505
506 gpg_pipe[1] = safe_close(gpg_pipe[1]);
507
7d4904fe 508 r = wait_for_terminate_and_check("gpg", pid, WAIT_LOG_ABNORMAL);
98c38001
LP
509 pid = 0;
510 if (r < 0)
511 goto finish;
b4a34311 512 if (r != EXIT_SUCCESS) {
9854730b 513 log_error("DOWNLOAD INVALID: Signature verification failed.");
98c38001
LP
514 r = -EBADMSG;
515 } else {
516 log_info("Signature verification succeeded.");
517 r = 0;
518 }
519
520finish:
d61b34f1 521 (void) unlink(sig_file_path);
98c38001 522
0acfdffe 523 if (gpg_home_created)
c6878637 524 (void) rm_rf(gpg_home, REMOVE_ROOT|REMOVE_PHYSICAL);
0acfdffe 525
98c38001
LP
526 return r;
527}