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