pull-common.c revision d9e2daaf3d8649650cf9784b4fe9d9de4507da0c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2015 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 "util.h"
#include "strv.h"
#include "copy.h"
#include "rm-rf.h"
#include "btrfs-util.h"
#include "capability.h"
#include "pull-job.h"
#include "pull-common.h"
#define FILENAME_ESCAPE "/.#\"\'"
int pull_find_old_etags(const char *url, const char *image_root, int dt, const char *prefix, const char *suffix, char ***etags) {
_cleanup_strv_free_ char **l = NULL;
int r;
if (!image_root)
image_root = "/var/lib/machines";
if (!escaped_url)
return -ENOMEM;
d = opendir(image_root);
if (!d) {
return 0;
}
return -errno;
}
const char *a, *b;
char *u;
continue;
if (prefix) {
if (!a)
continue;
} else
a = startswith(a, escaped_url);
if (!a)
continue;
a = startswith(a, ".");
if (!a)
continue;
if (suffix) {
if (!b)
continue;
} else
if (a >= b)
continue;
u = cunescape_length(a, b - a);
if (!u)
return -ENOMEM;
if (!http_etag_is_valid(u)) {
free(u);
continue;
}
r = strv_consume(&l, u);
if (r < 0)
return r;
}
*etags = l;
l = NULL;
return 0;
}
int pull_make_local_copy(const char *final, const char *image_root, const char *local, bool force_local) {
const char *p;
int r;
if (!image_root)
image_root = "/var/lib/machines";
if (force_local)
r = btrfs_subvol_snapshot(final, p, false, false);
if (r == -ENOTTY) {
if (r < 0)
return log_error_errno(r, "Failed to copy image: %m");
} else if (r < 0)
return log_error_errno(r, "Failed to create local image: %m");
return 0;
}
int pull_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) {
char *path;
if (!image_root)
image_root = "/var/lib/machines";
if (!escaped_url)
return -ENOMEM;
if (etag) {
if (!escaped_etag)
return -ENOMEM;
path = strjoin(image_root, "/", strempty(prefix), escaped_url, ".", escaped_etag, strempty(suffix), NULL);
} else
if (!path)
return -ENOMEM;
return 0;
}
const char *url,
void *userdata) {
int 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;
}
return 0;
}
int pull_verify(
PullJob *signature_job) {
const char *p, *line;
bool gpg_home_created = false;
int r;
if (!checksum_job)
return 0;
log_error("Checksum is empty, cannot verify.");
return -EBADMSG;
}
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 (!signature_job)
return 0;
log_error("Signature is empty, cannot verify.");
return -EBADMSG;
}
if (r < 0)
if (sig_file < 0)
if (r < 0) {
log_error_errno(r, "Failed to write to temporary file: %m");
goto finish;
}
goto finish;
}
gpg_home_created = true;
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",
NULL, /* --homedir= */
NULL, /* --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. */
else
cmd[k++] = "--verify";
cmd[k++] = sig_file_path;
cmd[k++] = "-";
fd_cloexec(STDIN_FILENO, false);
fd_cloexec(STDOUT_FILENO, false);
fd_cloexec(STDERR_FILENO, false);
}
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)
if (gpg_home_created)
return r;
}