pull-common.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
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 "alloc-util.h"
#include "btrfs-util.h"
#include "capability-util.h"
#include "copy.h"
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
#include "io-util.h"
#include "path-util.h"
#include "process-util.h"
#include "pull-common.h"
#include "pull-job.h"
#include "rm-rf.h"
#include "signal-util.h"
#include "siphash24.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
#include "web-util.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;
r = cunescape_length(a, b - a, 0, &u);
if (r < 0)
return r;
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)
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;
}
uint64_t h;
return -ENOMEM;
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;
}
if (!path)
return -ENOMEM;
/* URLs might make the path longer than the maximum allowed length for a file name.
* When that happens, a URL hash is used instead. Paths returned by this function
* can be later used with tempfn_random() which adds 16 bytes to the resulting name. */
int r;
if (r < 0)
return r;
if (!path)
return -ENOMEM;
}
return 0;
}
const char *url,
void *userdata) {
const char *q;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
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;
}
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("DOWNLOAD INVALID: Checksum did not check out, payload has been tampered with.");
return -EBADMSG;
}
if (settings_job &&
settings_job->error == 0 &&
!settings_job->etag_exists) {
if (r < 0)
return log_oom();
if (!filename_is_valid(settings_fn)) {
log_error("Cannot verify checksum, could not determine server-side settings file name.");
return -EBADMSG;
}
line,
log_error("DOWNLOAD INVALID: Checksum of settings file did not checkout, settings file has been tampered 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 */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
}
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("DOWNLOAD INVALID: Signature verification failed.");
r = -EBADMSG;
} else {
log_info("Signature verification succeeded.");
r = 0;
}
if (sig_file >= 0)
(void) unlink(sig_file_path);
if (gpg_home_created)
return r;
}