import-raw.c revision 3576d6315f3b6b686cdcf9f280d5e829e3d3daa0
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <gcrypt.h>
#include "utf8.h"
#include "strv.h"
#include "copy.h"
#include "btrfs-util.h"
#include "util.h"
#include "macro.h"
#include "mkdir.h"
#include "curl-util.h"
#include "qcow2-util.h"
#include "import-job.h"
#include "import-util.h"
#include "import-raw.h"
typedef struct RawImportFile RawImportFile;
struct RawImport {
char *image_root;
void *userdata;
char *local;
bool force_local;
char *temp_path;
char *final_path;
};
if (!i)
return NULL;
import_job_unref(i->raw_job);
curl_glue_unref(i->glue);
sd_event_unref(i->event);
if (i->temp_path) {
}
free(i->final_path);
free(i->image_root);
free(i);
return NULL;
}
int raw_import_new(RawImport **ret, sd_event *event, const char *image_root, RawImportFinished on_finished, void *userdata) {
int r;
if (!i)
return -ENOMEM;
i->on_finished = on_finished;
if (!i->image_root)
return -ENOMEM;
if (event)
else {
r = sd_event_default(&i->event);
if (r < 0)
return r;
}
if (r < 0)
return r;
*ret = i;
i = NULL;
return 0;
}
static int raw_import_maybe_convert_qcow2(RawImport *i) {
_cleanup_free_ char *t = NULL;
int r;
assert(i);
if (r < 0)
return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
if (r == 0)
return 0;
/* This is a QCOW2 image, let's convert it */
r = tempfn_random(i->final_path, &t);
if (r < 0)
return log_oom();
if (converted_fd < 0)
if (r < 0)
log_info("Unpacking QCOW2 file.");
if (r < 0) {
unlink(t);
return log_error_errno(r, "Failed to convert qcow2 image: %m");
}
i->temp_path = t;
t = NULL;
converted_fd = -1;
return 1;
}
static int raw_import_make_local_copy(RawImport *i) {
const char *p;
int r;
assert(i);
if (!i->local)
return 0;
if (i->raw_job->etag_exists) {
/* We have downloaded this one previously, reopen it */
if (!i->final_path) {
r = import_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", ".raw", &i->final_path);
if (r < 0)
return log_oom();
}
} else {
/* We freshly downloaded the image, use it */
}
if (i->force_local) {
(void) btrfs_subvol_remove(p);
(void) rm_rf_dangerous(p, false, true, false);
}
r = tempfn_random(p, &tp);
if (r < 0)
return log_oom();
if (dfd < 0)
/* Turn off COW writing. This should greatly improve
* performance on COW file systems like btrfs, since it
* reduces fragmentation caused by not allowing in-place
* writes. */
if (r < 0)
if (r < 0) {
return log_error_errno(r, "Failed to make writable copy of image: %m");
}
if (r < 0) {
}
return 0;
}
static int raw_import_verify_sha256sum(RawImport *i) {
const char *p, *line;
char sig_file_path[] = "/tmp/sigXXXXXX";
int r;
assert(i);
if (!i->sha256sums_job)
return 0;
if (r < 0)
return log_oom();
if (!filename_is_valid(fn)) {
log_error("Cannot verify checksum, could not determine valid server-side file name.");
return -EBADMSG;
}
line,
log_error("Checksum did not check out, payload has been tempered with.");
return -EBADMSG;
}
if (!i->signature_job)
return 0;
if (r < 0)
if (sig_file < 0)
if (r < 0) {
log_error_errno(r, "Failed to write to temporary file: %m");
goto finish;
}
if (pid < 0)
if (pid == 0) {
const char *cmd[] = {
"gpg",
"--no-options",
"--no-default-keyring",
"--no-auto-key-locate",
"--no-auto-check-trustdb",
"--batch",
"--trust-model=always",
"--keyring=" VENDOR_KEYRING_PATH,
NULL, /* maybe user keyring */
NULL, /* --verify */
NULL, /* signature file */
NULL, /* dash */
NULL /* trailing NULL */
};
int null_fd;
/* Child */
}
if (gpg_pipe[0] != STDIN_FILENO)
if (null_fd < 0) {
}
}
if (null_fd != STDOUT_FILENO)
/* We add the user keyring only to the command line
* arguments, if it's around since gpg fails
* otherwise. */
cmd[k++] = "--verify";
cmd[k++] = sig_file_path;
cmd[k++] = "-";
}
if (r < 0) {
log_error_errno(r, "Failed to write to pipe: %m");
goto finish;
}
pid = 0;
if (r < 0)
goto finish;
if (r > 0) {
log_error("Signature verification failed.");
r = -EBADMSG;
} else {
log_info("Signature verification succeeded.");
r = 0;
}
if (sig_file >= 0)
return r;
}
static void raw_import_job_on_finished(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
if (j->error != 0) {
if (j == i->sha256sums_job)
else if (j == i->signature_job)
else
r = j->error;
goto finish;
}
/* This is invoked if either the download completed
* successfully, or the download was skipped because we
* already have the etag. In this case ->etag_exists is
* true.
*
* We only do something when we got all three files */
if (!IMPORT_JOB_STATE_IS_COMPLETE(i->raw_job))
return;
return;
return;
if (!i->raw_job->etag_exists) {
r = raw_import_verify_sha256sum(i);
if (r < 0)
goto finish;
r = raw_import_maybe_convert_qcow2(i);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0) {
goto finish;
}
}
r = raw_import_make_local_copy(i);
if (r < 0)
goto finish;
r = 0;
if (i->on_finished)
i->on_finished(i, r, i->userdata);
else
sd_event_exit(i->event, r);
}
static int raw_import_job_on_open_disk(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
if (r < 0)
return log_oom();
if (r <0)
return log_oom();
if (j->disk_fd < 0)
if (r < 0)
return 0;
}
int raw_import_pull(RawImport *i, const char *url, const char *local, bool force_local, ImportVerify verify) {
int r;
assert(i);
if (i->raw_job)
return -EBUSY;
if (!http_url_is_valid(url))
return -EINVAL;
return -EINVAL;
if (r < 0)
return r;
i->force_local = force_local;
/* Queue job for the image itself */
if (r < 0)
return r;
if (r < 0)
return r;
if (verify != IMPORT_VERIFY_NO) {
/* Queue job for the SHA256SUMS file for the image */
if (r < 0)
return r;
if (r < 0)
return r;
}
if (verify == IMPORT_VERIFY_SIGNATURE) {
/* Queue job for the SHA256SUMS.gpg file for the image. */
if (r < 0)
return r;
if (r < 0)
return r;
}
r = import_job_begin(i->raw_job);
if (r < 0)
return r;
if (i->sha256sums_job) {
r = import_job_begin(i->sha256sums_job);
if (r < 0)
return r;
}
if (i->signature_job) {
r = import_job_begin(i->signature_job);
if (r < 0)
return r;
}
return 0;
}