]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
importd: validate local image names with the right helper
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Feb 2024 11:31:03 +0000 (12:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 1 Mar 2024 21:25:42 +0000 (22:25 +0100)
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
src/import/import-common.c
src/import/import-fs.c
src/import/import.c
src/import/importctl.c
src/import/importd.c
src/import/pull-common.c
src/import/pull.c

index ac81b2fbabcc6a58b649b0145c4fecf0f9cc763d..cdb1d6246a3a890f23aeaa80e4df43405dc8eef4 100644 (file)
@@ -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
index 9d1448545ebaf73d057750b56fdb68fbdb556579..09faf16f9c745c865b3c4981aacfeedcd7878327 100644 (file)
@@ -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) {
index e37fd7b41d2d2a6a949328089abd8dce5ab00b6f..b1619ef9eac0d17c0b48e004f69e4d5f418c2c93 100644 (file)
@@ -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
index 22ce6f5aaa7f793f88f4953cedc8d0ef46f3e752..37dd0b2078f2f9943a0d804acaf05fe81711df05 100644 (file)
@@ -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);
index 3028f7d4bd39ebe46f54b2721876ed106c4d415f..1fc13e1326ad6005d793e3627eac299b8d5d2fde 100644 (file)
@@ -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);
         }
 
index 0f6bf71419ed8de5ee5e8995686e3560bf1548b5..41c329b0f28e7cabd42e5a6936a223ed99c49940 100644 (file)
@@ -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;
index 4fca08437c21f1616b92c00ea40ef9b9a0781a35..9a2ced002b4511e88274e35be71b7c5c2de28852 100644 (file)
@@ -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) {
index f281ab04fddd0e9084e2b9dfdbb5de32025a75af..19cfbe0d8d2638f4a6b9f83b28c60ee768de7bbd 100644 (file)
@@ -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);