From 901992209e3c87a4cf06b530d7b26ae2d35680ef Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 24 Dec 2014 16:44:56 +0100 Subject: [PATCH] import: add a new "pull-gpt" verb for downloading GPT disk images from the internet --- Makefile.am | 2 + src/import/curl-util.c | 31 +++ src/import/curl-util.h | 1 + src/import/import-gpt.c | 504 ++++++++++++++++++++++++++++++++++++++++ src/import/import-gpt.h | 37 +++ src/import/import.c | 96 +++++++- 6 files changed, 669 insertions(+), 2 deletions(-) create mode 100644 src/import/import-gpt.c create mode 100644 src/import/import-gpt.h diff --git a/Makefile.am b/Makefile.am index 4173147ee..89f3af89f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5136,6 +5136,8 @@ bin_PROGRAMS += \ systemd_import_SOURCES = \ src/import/import.c \ + src/import/import-gpt.c \ + src/import/import-gpt.h \ src/import/import-dkr.c \ src/import/import-dkr.h \ src/import/curl-util.c \ diff --git a/src/import/curl-util.c b/src/import/curl-util.c index eaaebaef4..78a58a8a6 100644 --- a/src/import/curl-util.c +++ b/src/import/curl-util.c @@ -413,3 +413,34 @@ int curl_header_strdup(const void *contents, size_t sz, const char *field, char *value = s; return 1; } + +int curl_parse_http_time(const char *t, time_t *ret) { + struct tm tm; + time_t v; + + assert(t); + assert(ret); + + RUN_WITH_LOCALE(LC_TIME, "C") { + const char *e; + + /* RFC822 */ + e = strptime(t, "%a, %d %b %Y %H:%M:%S %Z", &tm); + if (!e || *e != 0) + /* RFC 850 */ + e = strptime(t, "%A, %d-%b-%y %H:%M:%S %Z", &tm); + if (!e || *e != 0) + /* ANSI C */ + e = strptime(t, "%a %b %d %H:%M:%S %Y", &tm); + if (!e || *e != 0) + return -EINVAL; + + v = timegm(&tm); + } + + if (v == (time_t) -1) + return -EINVAL; + + *ret = v; + return 0; +} diff --git a/src/import/curl-util.h b/src/import/curl-util.h index 5a7550deb..b4d75e895 100644 --- a/src/import/curl-util.h +++ b/src/import/curl-util.h @@ -51,6 +51,7 @@ void curl_glue_remove_and_free(CurlGlue *g, CURL *c); struct curl_slist *curl_slist_new(const char *first, ...) _sentinel_; int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value); +int curl_parse_http_time(const char *t, time_t *ret); DEFINE_TRIVIAL_CLEANUP_FUNC(CURL*, curl_easy_cleanup); DEFINE_TRIVIAL_CLEANUP_FUNC(struct curl_slist*, curl_slist_free_all); diff --git a/src/import/import-gpt.c b/src/import/import-gpt.c new file mode 100644 index 000000000..eda6dcacb --- /dev/null +++ b/src/import/import-gpt.c @@ -0,0 +1,504 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + 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 . +***/ + +#include +#include + +#include "hashmap.h" +#include "utf8.h" +#include "curl-util.h" +#include "import-gpt.h" + +typedef struct GptImportFile GptImportFile; + +struct GptImportFile { + GptImport *import; + + char *url; + char *local; + + CURL *curl; + struct curl_slist *request_header; + + char *temp_path; + char *final_path; + char *etag; + char *old_etag; + + uint64_t content_length; + uint64_t written; + + time_t mtime; + + bool force_local; + bool done; + + int disk_fd; +}; + +struct GptImport { + sd_event *event; + CurlGlue *glue; + + Hashmap *files; + + gpt_import_on_finished on_finished; + void *userdata; + + bool finished; +}; + +static GptImportFile *gpt_import_file_unref(GptImportFile *f) { + if (!f) + return NULL; + + if (f->import) + curl_glue_remove_and_free(f->import->glue, f->curl); + curl_slist_free_all(f->request_header); + + safe_close(f->disk_fd); + + free(f->final_path); + + if (f->temp_path) { + unlink(f->temp_path); + free(f->temp_path); + } + + free(f->url); + free(f->local); + free(f->etag); + free(f->old_etag); + free(f); + + return NULL; +} + +DEFINE_TRIVIAL_CLEANUP_FUNC(GptImportFile*, gpt_import_file_unref); + +static void gpt_import_finish(GptImport *import, int error) { + assert(import); + + if (import->finished) + return; + + import->finished = true; + + if (import->on_finished) + import->on_finished(import, error, import->userdata); + else + sd_event_exit(import->event, error); +} + +static void gpt_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) { + GptImportFile *f = NULL; + struct stat st; + CURLcode code; + long status; + int r; + + if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &f) != CURLE_OK) + return; + + if (!f) + return; + + f->done = true; + + if (result != CURLE_OK) { + log_error("Transfer failed: %s", curl_easy_strerror(result)); + r = -EIO; + goto fail; + } + + code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); + if (code != CURLE_OK) { + log_error("Failed to retrieve response code: %s", curl_easy_strerror(code)); + r = -EIO; + goto fail; + } else if (status == 304) { + log_info("File unmodified."); + r = 0; + goto fail; + } else if (status >= 300) { + log_error("HTTP request to %s failed with code %li.", f->url, status); + r = -EIO; + goto fail; + } else if (status < 200) { + log_error("HTTP request to %s finished with unexpected code %li.", f->url, status); + r = -EIO; + goto fail; + } + + if (f->disk_fd < 0) { + log_error("No data received."); + r = -EIO; + goto fail; + } + + if (f->content_length != (uint64_t) -1 && + f->content_length != f->written) { + log_error("Download truncated."); + r = -EIO; + goto fail; + } + + if (f->etag) + (void) fsetxattr(f->disk_fd, "user.etag", f->etag, strlen(f->etag), XATTR_CREATE); + + if (f->mtime != 0) { + struct timespec ut[2]; + + timespec_store(&ut[0], (usec_t) f->mtime * USEC_PER_SEC); + ut[1] = ut[0]; + + (void) futimens(f->disk_fd, ut); + } + + if (fstat(f->disk_fd, &st) < 0) { + r = log_error_errno(errno, "Failed to stat file: %m"); + goto fail; + } + + /* Mark read-only */ + (void) fchmod(f->disk_fd, st.st_mode & 07444); + + f->disk_fd = safe_close(f->disk_fd); + + assert(f->temp_path); + assert(f->final_path); + + r = rename(f->temp_path, f->final_path); + if (r < 0) { + r = log_error_errno(errno, "Failed to move GPT file into place: %m"); + goto fail; + } + + r = 0; + +fail: + gpt_import_finish(f->import, r); +} + +static int gpt_import_file_open_disk(GptImportFile *f) { + int r; + + assert(f); + + if (f->disk_fd >= 0) + return 0; + + assert(f->final_path); + + if (!f->temp_path) { + r = tempfn_random(f->final_path, &f->temp_path); + if (r < 0) + return log_oom(); + } + + f->disk_fd = open(f->temp_path, O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC|O_WRONLY, 0644); + if (f->disk_fd < 0) + return log_error_errno(errno, "Failed to create %s: %m", f->temp_path); + + return 0; +} + +static size_t gpt_import_file_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { + GptImportFile *f = userdata; + size_t sz = size * nmemb; + ssize_t n; + int r; + + assert(contents); + assert(f); + + r = gpt_import_file_open_disk(f); + if (r < 0) + goto fail; + + if (f->written + sz < f->written) { + log_error("File too large, overflow"); + r = -EOVERFLOW; + goto fail; + } + + if (f->content_length != (uint64_t) -1 && + f->written + sz > f->content_length) { + log_error("Content length incorrect."); + r = -EFBIG; + goto fail; + } + + n = write(f->disk_fd, contents, sz); + if (n < 0) { + log_error_errno(errno, "Failed to write file: %m"); + goto fail; + } + + if ((size_t) n < sz) { + log_error("Short write"); + r = -EIO; + goto fail; + } + + f->written += sz; + + return sz; + +fail: + gpt_import_finish(f->import, r); + return 0; +} + +static size_t gpt_import_file_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) { + GptImportFile *f = userdata; + size_t sz = size * nmemb; + _cleanup_free_ char *length = NULL, *last_modified = NULL; + char *etag; + int r; + + assert(contents); + assert(f); + + r = curl_header_strdup(contents, sz, "ETag:", &etag); + if (r < 0) { + log_oom(); + goto fail; + } + if (r > 0) { + free(f->etag); + f->etag = etag; + + if (streq_ptr(f->old_etag, f->etag)) { + log_info("Image already up to date. Finishing."); + gpt_import_finish(f->import, 0); + return sz; + } + + return sz; + } + + r = curl_header_strdup(contents, sz, "Content-Length:", &length); + if (r < 0) { + log_oom(); + goto fail; + } + if (r > 0) { + (void) safe_atou64(length, &f->content_length); + return sz; + } + + r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified); + if (r < 0) { + log_oom(); + goto fail; + } + if (r > 0) { + (void) curl_parse_http_time(last_modified, &f->mtime); + return sz; + } + + return sz; + +fail: + gpt_import_finish(f->import, r); + return 0; +} + +static int gpt_import_file_begin(GptImportFile *f) { + int r; + + assert(f); + assert(!f->curl); + + log_info("Getting %s.", f->url); + + r = curl_glue_make(&f->curl, f->url, f); + if (r < 0) + return r; + + if (f->old_etag) { + const char *hdr; + + hdr = strappenda("If-None-Match: ", f->old_etag); + + f->request_header = curl_slist_new(hdr, NULL); + if (!f->request_header) + return -ENOMEM; + + if (curl_easy_setopt(f->curl, CURLOPT_HTTPHEADER, f->request_header) != CURLE_OK) + return -EIO; + } + + if (curl_easy_setopt(f->curl, CURLOPT_WRITEFUNCTION, gpt_import_file_write_callback) != CURLE_OK) + return -EIO; + + if (curl_easy_setopt(f->curl, CURLOPT_WRITEDATA, f) != CURLE_OK) + return -EIO; + + if (curl_easy_setopt(f->curl, CURLOPT_HEADERFUNCTION, gpt_import_file_header_callback) != CURLE_OK) + return -EIO; + + if (curl_easy_setopt(f->curl, CURLOPT_HEADERDATA, f) != CURLE_OK) + return -EIO; + + r = curl_glue_add(f->import->glue, f->curl); + if (r < 0) + return r; + + return 0; +} + +int gpt_import_new(GptImport **import, sd_event *event, gpt_import_on_finished on_finished, void *userdata) { + _cleanup_(gpt_import_unrefp) GptImport *i = NULL; + int r; + + assert(import); + + i = new0(GptImport, 1); + if (!i) + return -ENOMEM; + + i->on_finished = on_finished; + i->userdata = userdata; + + if (event) + i->event = sd_event_ref(event); + else { + r = sd_event_default(&i->event); + if (r < 0) + return r; + } + + r = curl_glue_new(&i->glue, i->event); + if (r < 0) + return r; + + i->glue->on_finished = gpt_import_curl_on_finished; + i->glue->userdata = i; + + *import = i; + i = NULL; + + return 0; +} + +GptImport* gpt_import_unref(GptImport *import) { + GptImportFile *f; + + if (!import) + return NULL; + + while ((f = hashmap_steal_first(import->files))) + gpt_import_file_unref(f); + hashmap_free(import->files); + + curl_glue_unref(import->glue); + sd_event_unref(import->event); + + free(import); + + return NULL; +} + +int gpt_import_cancel(GptImport *import, const char *url) { + GptImportFile *f; + + assert(import); + assert(url); + + f = hashmap_remove(import->files, url); + if (!f) + return 0; + + gpt_import_file_unref(f); + return 1; +} + +int gpt_import_pull(GptImport *import, const char *url, const char *local, bool force_local) { + _cleanup_(gpt_import_file_unrefp) GptImportFile *f = NULL; + char etag[LINE_MAX]; + ssize_t n; + int r; + + assert(import); + assert(gpt_url_is_valid(url)); + assert(machine_name_is_valid(local)); + + if (hashmap_get(import->files, url)) + return -EEXIST; + + r = hashmap_ensure_allocated(&import->files, &string_hash_ops); + if (r < 0) + return r; + + f = new0(GptImportFile, 1); + if (!f) + return -ENOMEM; + + f->import = import; + f->disk_fd = -1; + f->content_length = (uint64_t) -1; + + f->url = strdup(url); + if (!f->url) + return -ENOMEM; + + f->local = strdup(local); + if (!f->local) + return -ENOMEM; + + f->final_path = strjoin("/var/lib/container/", local, ".gpt", NULL); + if (!f->final_path) + return -ENOMEM; + + n = getxattr(f->final_path, "user.etag", etag, sizeof(etag)); + if (n > 0) { + f->old_etag = strndup(etag, n); + if (!f->old_etag) + return -ENOMEM; + } + + r = hashmap_put(import->files, f->url, f); + if (r < 0) + return r; + + r = gpt_import_file_begin(f); + if (r < 0) { + gpt_import_cancel(import, f->url); + f = NULL; + return r; + } + + f = NULL; + return 0; +} + +bool gpt_url_is_valid(const char *url) { + if (isempty(url)) + return false; + + if (!startswith(url, "http://") && + !startswith(url, "https://")) + return false; + + return ascii_is_valid(url); +} diff --git a/src/import/import-gpt.h b/src/import/import-gpt.h new file mode 100644 index 000000000..e4c534c41 --- /dev/null +++ b/src/import/import-gpt.h @@ -0,0 +1,37 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + 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 . +***/ + +#include "sd-event.h" +#include "util.h" + +typedef struct GptImport GptImport; + +typedef void (*gpt_import_on_finished)(GptImport *import, int error, void *userdata); + +int gpt_import_new(GptImport **import, sd_event *event, gpt_import_on_finished on_finished, void *userdata); +GptImport* gpt_import_unref(GptImport *import); + +DEFINE_TRIVIAL_CLEANUP_FUNC(GptImport*, gpt_import_unref); + +int gpt_import_pull(GptImport *import, const char *url, const char *local, bool force_local); +int gpt_import_cancel(GptImport *import, const char *name); + +bool gpt_url_is_valid(const char *url); diff --git a/src/import/import.c b/src/import/import.c index 54f311363..a81200c9f 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -25,13 +25,103 @@ #include "event-util.h" #include "verbs.h" #include "build.h" +#include "import-gpt.h" #include "import-dkr.h" static bool arg_force = false; static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL; -static void on_finished(DkrImport *import, int error, void *userdata) { +static void on_gpt_finished(GptImport *import, int error, void *userdata) { + sd_event *event = userdata; + assert(import); + + if (error == 0) + log_info("Operation completed successfully."); + else + log_info_errno(error, "Operation failed: %m"); + + sd_event_exit(event, error); +} + +static int pull_gpt(int argc, char *argv[], void *userdata) { + _cleanup_(gpt_import_unrefp) GptImport *import = NULL; + _cleanup_event_unref_ sd_event *event = NULL; + _cleanup_free_ char *local_truncated = NULL, *local_generated = NULL; + const char *url, *local, *suffix; + int r; + + url = argv[1]; + local = argv[2]; + + if (!gpt_url_is_valid(url)) { + log_error("URL '%s' is not valid.", url); + return -EINVAL; + } + + if (isempty(local) || streq(local, "-")) + local = NULL; + + if (!local) { + const char *e, *p; + + e = url + strlen(url); + while (e > url && e[-1] == '/') + e--; + + p = e; + while (p > url && p[-1] != '/') + p--; + + local_generated = strndup(p, e - p); + if (!local_generated) + return log_oom(); + + local = local_generated; + } + + suffix = endswith(local, ".gpt"); + if (suffix) { + local_truncated = strndup(local, suffix - local); + if (!local_truncated) + return log_oom(); + + local = local_truncated; + } + + if (!machine_name_is_valid(local)) { + log_error("Local image name '%s' is not valid.", local); + return -EINVAL; + } + + log_info("Pulling '%s' as '%s'", url, local); + + r = sd_event_default(&event); + if (r < 0) + return log_error_errno(r, "Failed to allocate event loop: %m"); + + assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); + sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL); + sd_event_add_signal(event, NULL, SIGINT, NULL, NULL); + + r = gpt_import_new(&import, event, on_gpt_finished, event); + if (r < 0) + return log_error_errno(r, "Failed to allocate importer: %m"); + + r = gpt_import_pull(import, url, local, arg_force); + if (r < 0) + return log_error_errno(r, "Failed to pull image: %m"); + + r = sd_event_loop(event); + if (r < 0) + return log_error_errno(r, "Failed to run event loop: %m"); + + log_info("Exiting."); + + return 0; +} + +static void on_dkr_finished(DkrImport *import, int error, void *userdata) { sd_event *event = userdata; assert(import); @@ -141,7 +231,8 @@ static int help(int argc, char *argv[], void *userdata) { " --force Force creation of image\n" " --dkr-index-url=URL Specify index URL to use for downloads\n\n" "Commands:\n" - " pull-dkr REMOTE [NAME] Download an image\n", + " pull-dkr REMOTE [NAME] Download a DKR image\n" + " pull-gpt URL [NAME] Download a GPT image\n", program_invocation_short_name); return 0; @@ -208,6 +299,7 @@ static int import_main(int argc, char *argv[]) { static const Verb verbs[] = { { "help", VERB_ANY, VERB_ANY, 0, help }, { "pull-dkr", 2, 3, 0, pull_dkr }, + { "pull-gpt", 2, 3, 0, pull_gpt }, {} }; -- 2.30.2