From 8f20b498bd072bc9a0e9640b8560685ed11262f8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Feb 2024 12:31:03 +0100 Subject: [PATCH] importd: validate local image names with the right helper A while back we introduced image_name_is_valid() for validating image file names. It's more liberal than hostname_is_valid() in many ways (and allows version suffixes and such). Since importd deals in offline images (as opposed to machined otherwise which deals in running machines), let's hence use the right helper to validate the identifiers. --- src/import/export.c | 18 ++++++++++-------- src/import/import-common.c | 2 +- src/import/import-fs.c | 2 +- src/import/import.c | 2 +- src/import/importctl.c | 28 ++++++++++++++-------------- src/import/importd.c | 16 ++++++++-------- src/import/pull-common.c | 3 ++- src/import/pull.c | 2 +- 8 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/import/export.c b/src/import/export.c index ac81b2fbab..cdb1d6246a 100644 --- a/src/import/export.c +++ b/src/import/export.c @@ -63,12 +63,13 @@ static int export_tar(int argc, char *argv[], void *userdata) { _cleanup_close_ int open_fd = -EBADF; int r, fd; - if (hostname_is_valid(argv[1], 0)) { - r = image_find(arg_class, argv[1], NULL, &image); + local = argv[1]; + if (image_name_is_valid(local)) { + r = image_find(arg_class, local, NULL, &image); if (r == -ENOENT) - return log_error_errno(r, "Machine image %s not found.", argv[1]); + return log_error_errno(r, "Image %s not found.", local); if (r < 0) - return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]); + return log_error_errno(r, "Failed to look for image %s: %m", local); local = image->path; } else @@ -135,12 +136,13 @@ static int export_raw(int argc, char *argv[], void *userdata) { _cleanup_close_ int open_fd = -EBADF; int r, fd; - if (hostname_is_valid(argv[1], 0)) { - r = image_find(arg_class, argv[1], NULL, &image); + local = argv[1]; + if (image_name_is_valid(local)) { + r = image_find(arg_class, local, NULL, &image); if (r == -ENOENT) - return log_error_errno(r, "Machine image %s not found.", argv[1]); + return log_error_errno(r, "Image %s not found.", local); if (r < 0) - return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]); + return log_error_errno(r, "Failed to look for image %s: %m", local); local = image->path; } else diff --git a/src/import/import-common.c b/src/import/import-common.c index 9d1448545e..09faf16f9c 100644 --- a/src/import/import-common.c +++ b/src/import/import-common.c @@ -276,7 +276,7 @@ bool import_validate_local(const char *name, ImportFlags flags) { if (FLAGS_SET(flags, IMPORT_DIRECT)) return path_is_valid(name); - return hostname_is_valid(name, 0); + return image_name_is_valid(name); } static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { diff --git a/src/import/import-fs.c b/src/import/import-fs.c index e37fd7b41d..b1619ef9ea 100644 --- a/src/import/import-fs.c +++ b/src/import/import-fs.c @@ -133,7 +133,7 @@ static int import_fs(int argc, char *argv[], void *userdata) { "Local path name '%s' is not valid.", final_path); } else { if (local) { - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Local image name '%s' is not valid.", local); } else diff --git a/src/import/import.c b/src/import/import.c index 22ce6f5aaa..37dd0b2078 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -54,7 +54,7 @@ static int normalize_local(const char *local, char **ret) { "Local path name '%s' is not valid.", local); } else { if (local) { - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Local image name '%s' is not valid.", local); diff --git a/src/import/importctl.c b/src/import/importctl.c index 3028f7d4bd..1fc13e1326 100644 --- a/src/import/importctl.c +++ b/src/import/importctl.c @@ -220,9 +220,9 @@ static int import_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Local name %s is not a suitable machine name.", + "Local name %s is not a suitable image name.", local); if (path) { @@ -299,9 +299,9 @@ static int import_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Local name %s is not a suitable machine name.", + "Local name %s is not a suitable image name.", local); if (path) { @@ -369,9 +369,9 @@ static int import_fs(int argc, char *argv[], void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Need either path or local name."); - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Local name %s is not a suitable machine name.", + "Local name %s is not a suitable image name.", local); if (path) { @@ -439,9 +439,9 @@ static int export_tar(int argc, char *argv[], void *userdata) { return r; local = argv[1]; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Machine name %s is not valid.", local); + "Image name %s is not valid.", local); if (argc >= 3) path = argv[2]; @@ -498,9 +498,9 @@ static int export_raw(int argc, char *argv[], void *userdata) { return r; local = argv[1]; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Machine name %s is not valid.", local); + "Image name %s is not valid.", local); if (argc >= 3) path = argv[2]; @@ -580,9 +580,9 @@ static int pull_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Local name %s is not a suitable machine name.", + "Local name %s is not a suitable image name.", local); } @@ -653,9 +653,9 @@ static int pull_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Local name %s is not a suitable machine name.", + "Local name %s is not a suitable image name.", local); } diff --git a/src/import/importd.c b/src/import/importd.c index 0f6bf71419..41c329b0f2 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -771,9 +771,9 @@ static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_ if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode)) return -EINVAL; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, - "Local name %s is invalid", local); + "Local image name %s is invalid", local); if (class == IMAGE_MACHINE) { r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota); @@ -870,9 +870,9 @@ static int method_import_fs(sd_bus_message *msg, void *userdata, sd_bus_error *e if (r < 0) return r; - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, - "Local name %s is invalid", local); + "Local image name %s is invalid", local); if (class == IMAGE_MACHINE) { r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota); @@ -955,9 +955,9 @@ static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_ flags = 0; } - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, - "Local name %s is invalid", local); + "Local image name %s is invalid", local); r = fd_verify_safe_flags(fd); if (r < 0) @@ -1063,9 +1063,9 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er if (isempty(local)) local = NULL; - else if (!hostname_is_valid(local, 0)) + else if (!image_name_is_valid(local)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, - "Local name %s is invalid", local); + "Local image name %s is invalid", local); if (isempty(verify)) v = IMPORT_VERIFY_SIGNATURE; diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 4fca08437c..9a2ced002b 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -7,6 +7,7 @@ #include "capability-util.h" #include "copy.h" #include "dirent-util.h" +#include "discover-image.h" #include "escape.h" #include "fd-util.h" #include "hostname-util.h" @@ -644,7 +645,7 @@ bool pull_validate_local(const char *name, ImportFlags flags) { if (FLAGS_SET(flags, IMPORT_DIRECT)) return path_is_valid(name); - return hostname_is_valid(name, 0); + return image_name_is_valid(name); } int pull_url_needs_checksum(const char *url) { diff --git a/src/import/pull.c b/src/import/pull.c index f281ab04fd..19cfbe0d8d 100644 --- a/src/import/pull.c +++ b/src/import/pull.c @@ -59,7 +59,7 @@ static int normalize_local(const char *local, const char *url, char **ret) { } else if (local) { - if (!hostname_is_valid(local, 0)) + if (!image_name_is_valid(local)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Local image name '%s' is not valid.", local);