]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: flagsify hostname_is_valid(), drop machine_name_is_valid()
authorLennart Poettering <lennart@poettering.net>
Fri, 11 Dec 2020 15:40:45 +0000 (16:40 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 15 Dec 2020 16:59:48 +0000 (17:59 +0100)
Let's clean up hostname_is_valid() a bit: let's turn the second boolean
argument into a more explanatory flags field, and add a flag that
accepts the special name ".host" as valid. This is useful for the
container logic, where the special hostname ".host" refers to the "root
container", i.e. the host system itself, and can be specified at various
places.

let's also get rid of machine_name_is_valid(). It was just an alias,
which is confusing and even more so now that we have the flags param.

30 files changed:
src/basic/hostname-util.c
src/basic/hostname-util.h
src/basic/util.c
src/core/hostname-setup.c
src/firstboot/firstboot.c
src/hostname/hostnamectl.c
src/hostname/hostnamed.c
src/import/export.c
src/import/import-fs.c
src/import/import-raw.c
src/import/import-tar.c
src/import/import.c
src/import/importd.c
src/import/pull-raw.c
src/import/pull-tar.c
src/import/pull.c
src/journal/sd-journal.c
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-login/sd-login.c
src/machine/machinectl.c
src/machine/machined-dbus.c
src/machine/machined.c
src/network/generator/network-generator.c
src/network/networkd-network.c
src/nspawn/nspawn-oci.c
src/nspawn/nspawn-settings.c
src/nspawn/nspawn.c
src/test/test-hostname-util.c

index 82cd37b15434eabca26ba89f1622c2ace98ef446..e1e3d1b06136756b341c3c1728ba1acff236851f 100644 (file)
@@ -98,28 +98,24 @@ bool valid_ldh_char(char c) {
                 c == '-';
 }
 
-/**
- * Check if s looks like a valid hostname or FQDN. This does not do
- * full DNS validation, but only checks if the name is composed of
- * allowed characters and the length is not above the maximum allowed
- * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
- * allow_trailing_dot is true and at least two components are present
- * in the name. Note that due to the restricted charset and length
- * this call is substantially more conservative than
- * dns_name_is_valid().
- */
-bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
+bool hostname_is_valid(const char *s, ValidHostnameFlags flags) {
         unsigned n_dots = 0;
         const char *p;
         bool dot, hyphen;
 
+        /* Check if s looks like a valid hostname or FQDN. This does not do full DNS validation, but only
+         * checks if the name is composed of allowed characters and the length is not above the maximum
+         * allowed by Linux (c.f. dns_name_is_valid()). A trailing dot is allowed if
+         * VALID_HOSTNAME_TRAILING_DOT flag is set and at least two components are present in the name. Note
+         * that due to the restricted charset and length this call is substantially more conservative than
+         * dns_name_is_valid(). Doesn't accept empty hostnames, hostnames with leading dots, and hostnames
+         * with multiple dots in a sequence. Doesn't allow hyphens at the beginning or end of label. */
+
         if (isempty(s))
                 return false;
 
-        /* Doesn't accept empty hostnames, hostnames with
-         * leading dots, and hostnames with multiple dots in a
-         * sequence. Also ensures that the length stays below
-         * HOST_NAME_MAX. */
+        if (streq(s, ".host")) /* Used by the container logic to denote the "root container" */
+                return FLAGS_SET(flags, VALID_HOSTNAME_DOT_HOST);
 
         for (p = s, dot = hyphen = true; *p; p++)
                 if (*p == '.') {
@@ -145,14 +141,13 @@ bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
                         hyphen = false;
                 }
 
-        if (dot && (n_dots < 2 || !allow_trailing_dot))
+        if (dot && (n_dots < 2 || !FLAGS_SET(flags, VALID_HOSTNAME_TRAILING_DOT)))
                 return false;
         if (hyphen)
                 return false;
 
-        if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
-                                  * Linux, but DNS allows domain names
-                                  * up to 255 characters */
+        if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on Linux, but DNS allows domain names up to
+                                  * 255 characters */
                 return false;
 
         return true;
@@ -243,7 +238,7 @@ int shorten_overlong(const char *s, char **ret) {
         if (!h)
                 return -ENOMEM;
 
-        if (hostname_is_valid(h, false)) {
+        if (hostname_is_valid(h, 0)) {
                 *ret = h;
                 return 0;
         }
@@ -254,7 +249,7 @@ int shorten_overlong(const char *s, char **ret) {
 
         strshorten(h, HOST_NAME_MAX);
 
-        if (!hostname_is_valid(h, false)) {
+        if (!hostname_is_valid(h, 0)) {
                 free(h);
                 return -EDOM;
         }
@@ -287,7 +282,7 @@ int read_etc_hostname_stream(FILE *f, char **ret) {
 
                         hostname_cleanup(p); /* normalize the hostname */
 
-                        if (!hostname_is_valid(p, true)) /* check that the hostname we return is valid */
+                        if (!hostname_is_valid(p, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */
                                 return -EBADMSG;
 
                         copy = strdup(p);
index 34819c2953b571904b76062c80398a3ff5b66d6b..fe417297eeaaf4b4e45d5c004f3e87c13a69fe09 100644 (file)
@@ -14,10 +14,14 @@ char* gethostname_short_malloc(void);
 int gethostname_strict(char **ret);
 
 bool valid_ldh_char(char c) _const_;
-bool hostname_is_valid(const char *s, bool allow_trailing_dot) _pure_;
-char* hostname_cleanup(char *s);
 
-#define machine_name_is_valid(s) hostname_is_valid(s, false)
+typedef enum ValidHostnameFlags {
+        VALID_HOSTNAME_TRAILING_DOT = 1 << 0,   /* Accept trailing dot on multi-label names */
+        VALID_HOSTNAME_DOT_HOST     = 1 << 1,   /* Accept ".host" as valid hostname */
+} ValidHostnameFlags;
+
+bool hostname_is_valid(const char *s, ValidHostnameFlags flags) _pure_;
+char* hostname_cleanup(char *s);
 
 bool is_localhost(const char *hostname);
 
index b080ce4e96b6fe2eb0647250c5dc9fd6da7eb0f6..7c708eb3be75a3b0fa871c10648c77c7ee18d025 100644 (file)
@@ -165,7 +165,7 @@ int container_get_leader(const char *machine, pid_t *pid) {
                 return 0;
         }
 
-        if (!machine_name_is_valid(machine))
+        if (!hostname_is_valid(machine, 0))
                 return -EINVAL;
 
         p = strjoina("/run/systemd/machines/", machine);
index 867ea199057ad631df164842209dfde7ddf7cab5..4e20e764a3c9d8215010de533c8e57be078d57f0 100644 (file)
@@ -24,7 +24,7 @@ int hostname_setup(void) {
         if (r < 0)
                 log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m");
         else if (r > 0) {
-                if (hostname_is_valid(b, true))
+                if (hostname_is_valid(b, VALID_HOSTNAME_TRAILING_DOT))
                         hn = b;
                 else  {
                         log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b);
index 742b43f9fca7743a174042efa9819b7d5a7a0b22..48baae3f89a133ea855bd07da3652c68dbafaafc 100644 (file)
@@ -493,7 +493,7 @@ static int prompt_hostname(void) {
                         break;
                 }
 
-                if (!hostname_is_valid(h, true)) {
+                if (!hostname_is_valid(h, VALID_HOSTNAME_TRAILING_DOT)) {
                         log_error("Specified hostname invalid.");
                         continue;
                 }
@@ -1135,7 +1135,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_HOSTNAME:
-                        if (!hostname_is_valid(optarg, true))
+                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT))
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                        "Host name %s is not valid.", optarg);
 
index 0d39e9176f351b08778abf824f5fa59e94cf4f0b..de6eb9ea2fd180d65198a83ddcdae39332ebf99a 100644 (file)
@@ -248,7 +248,7 @@ static int set_hostname(int argc, char **argv, void *userdata) {
                 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
                  * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
                  * hostname. */
-                if (arg_static && hostname_is_valid(hostname, true))
+                if (arg_static && hostname_is_valid(hostname, VALID_HOSTNAME_TRAILING_DOT))
                         p = ""; /* No pretty hostname (as it is redundant), just a static one */
                 else
                         p = hostname; /* Use the passed name as pretty hostname */
index a1794bdab1e36a9715dfec29b3b16063b1fc7c42..a0dc25c8347b174e5115a0643ce7b6ca5494d8fa 100644 (file)
@@ -596,7 +596,7 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *
         if (isempty(name))
                 name = FALLBACK_HOSTNAME;
 
-        if (!hostname_is_valid(name, false))
+        if (!hostname_is_valid(name, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
 
         assert_se(uname(&u) >= 0);
@@ -650,7 +650,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
         if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
                 return sd_bus_reply_method_return(m, NULL);
 
-        if (!isempty(name) && !hostname_is_valid(name, false))
+        if (!isempty(name) && !hostname_is_valid(name, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
 
         r = bus_verify_polkit_async(
index 83990df64c69fb08688472193ddebcf17fc6595b..b8507330ac0bd962ebd5665da0bf4dddb56dc1c7 100644 (file)
@@ -65,7 +65,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
         _cleanup_close_ int open_fd = -1;
         int r, fd;
 
-        if (machine_name_is_valid(argv[1])) {
+        if (hostname_is_valid(argv[1], 0)) {
                 r = image_find(IMAGE_MACHINE, argv[1], &image);
                 if (r == -ENOENT)
                         return log_error_errno(r, "Machine image %s not found.", argv[1]);
@@ -141,7 +141,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
         _cleanup_close_ int open_fd = -1;
         int r, fd;
 
-        if (machine_name_is_valid(argv[1])) {
+        if (hostname_is_valid(argv[1], 0)) {
                 r = image_find(IMAGE_MACHINE, argv[1], &image);
                 if (r == -ENOENT)
                         return log_error_errno(r, "Machine image %s not found.", argv[1]);
index 3b43ea112d9d5587ccb549dd349db72bbd71054b..a22eef8255824815c109aba5e9121ff24be55c08 100644 (file)
@@ -126,7 +126,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
         local = empty_or_dash_to_null(local);
 
         if (local) {
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local image name '%s' is not valid.",
                                                local);
index 9f5c13ba1634a84121803b3d0beaf17b931c2956..c0869ffcf96cf2b19c86523838f2f342fd195a69 100644 (file)
@@ -393,7 +393,7 @@ int raw_import_start(RawImport *i, int fd, const char *local, bool force_local,
         assert(fd >= 0);
         assert(local);
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return -EINVAL;
 
         if (i->input_fd >= 0)
index 9f68d45eacb615584633808f0a53b4e076979b3b..d68ce916130d382e22efdd3ad5c32d0cb468e04e 100644 (file)
@@ -329,7 +329,7 @@ int tar_import_start(TarImport *i, int fd, const char *local, bool force_local,
         assert(fd >= 0);
         assert(local);
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return -EINVAL;
 
         if (i->input_fd >= 0)
index eade0f0ec881f73a44a6539893ee0f02a025c1e6..9ea8e7f16dd9b806247de905f5f6bceca9a98be3 100644 (file)
@@ -64,7 +64,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local image name '%s' is not valid.",
                                                local);
@@ -159,7 +159,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local image name '%s' is not valid.",
                                                local);
index 63f80e0e38bdbba8f94742e84cc4955914086e14..b5cff4aca8676a887abdae956a5669a77ba5fa2a 100644 (file)
@@ -717,7 +717,7 @@ 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 (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                          "Local name %s is invalid", local);
 
@@ -787,7 +787,7 @@ static int method_import_fs(sd_bus_message *msg, void *userdata, sd_bus_error *e
         if (r < 0)
                 return r;
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                          "Local name %s is invalid", local);
 
@@ -852,7 +852,7 @@ static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
         if (r < 0)
                 return r;
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                          "Local name %s is invalid", local);
 
@@ -932,7 +932,7 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
 
         if (isempty(local))
                 local = NULL;
-        else if (!machine_name_is_valid(local))
+        else if (!hostname_is_valid(local, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                          "Local name %s is invalid", local);
 
index 7956ef0395bdd61aa21cff148cdd3cc38019bc6e..4a2bab3d0734433dcaa2efaaa598f107a680b754 100644 (file)
@@ -651,7 +651,7 @@ int raw_pull_start(
         if (!http_url_is_valid(url))
                 return -EINVAL;
 
-        if (local && !machine_name_is_valid(local))
+        if (local && !hostname_is_valid(local, 0))
                 return -EINVAL;
 
         if (i->raw_job)
index 72e5b8be2701a2ce318496a04b89518e2534e1ea..bbbd0f57a0ef8ee9950b07e182bed382b8a3c642 100644 (file)
@@ -481,7 +481,7 @@ int tar_pull_start(
         if (!http_url_is_valid(url))
                 return -EINVAL;
 
-        if (local && !machine_name_is_valid(local))
+        if (local && !hostname_is_valid(local, 0))
                 return -EINVAL;
 
         if (i->tar_job)
index 9aff377b93fad217efe13de40e78645eb37f0346..a4cec9448e2910a96f5b366b2ba3af7e717d4540 100644 (file)
@@ -72,7 +72,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local image name '%s' is not valid.",
                                                local);
@@ -158,7 +158,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local image name '%s' is not valid.",
                                                local);
index 946f74d53538648663a35cf1ad18b5d3e951bd90..e94fb4dafb3bec493b2fa19efa7d98ddb6e22099 100644 (file)
@@ -1981,7 +1981,7 @@ _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, in
         assert_return(machine, -EINVAL);
         assert_return(ret, -EINVAL);
         assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
-        assert_return(machine_name_is_valid(machine), -EINVAL);
+        assert_return(hostname_is_valid(machine, 0), -EINVAL);
 
         p = strjoina("/run/systemd/machines/", machine);
         r = parse_env_file(NULL, p,
index f47a542483444f6be3f653a6d949858dddb46561..252a01add64be93d9fbde6173858dbfb36ef6257 100644 (file)
@@ -540,7 +540,7 @@ int sd_dhcp_client_set_hostname(
 
         /* Make sure hostnames qualify as DNS and as Linux hostnames */
         if (hostname &&
-            !(hostname_is_valid(hostname, false) && dns_name_is_valid(hostname) > 0))
+            !(hostname_is_valid(hostname, 0) && dns_name_is_valid(hostname) > 0))
                 return -EINVAL;
 
         return free_and_strdup(&client->hostname, hostname);
index 97fd5fd4fb32591cc9107fdad6d529d9990daddb..e068e0dc912e5b4b2c5bf2755554cb66ea240a79 100644 (file)
@@ -407,7 +407,7 @@ int sd_dhcp6_client_set_fqdn(
 
         /* Make sure FQDN qualifies as DNS and as Linux hostname */
         if (fqdn &&
-            !(hostname_is_valid(fqdn, false) && dns_name_is_valid(fqdn) > 0))
+            !(hostname_is_valid(fqdn, 0) && dns_name_is_valid(fqdn) > 0))
                 return -EINVAL;
 
         return free_and_strdup(&client->fqdn, fqdn);
index b8d4dc8d959f631aedee917e3d13c2ddcd29512f..da7827015a447a4106a92a7c256884f850e1b0cf 100644 (file)
@@ -973,7 +973,7 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid)
                 return -EINVAL;
 
         if (machine) {
-                if (!streq(machine, ".host") && !machine_name_is_valid(machine))
+                if (!hostname_is_valid(machine, VALID_HOSTNAME_DOT_HOST))
                         return -EINVAL;
 
                 free_and_replace(b->machine, machine);
@@ -1448,7 +1448,7 @@ int bus_set_address_system_remote(sd_bus *b, const char *host) {
                 }
 
                 if (!in_charset(p, "0123456789") || *p == '\0') {
-                        if (!machine_name_is_valid(p) || got_forward_slash)
+                        if (!hostname_is_valid(p, 0) || got_forward_slash)
                                 return -EINVAL;
 
                         m = TAKE_PTR(p);
@@ -1463,7 +1463,7 @@ int bus_set_address_system_remote(sd_bus *b, const char *host) {
 interpret_port_as_machine_old_syntax:
                 /* Let's make sure this is not a port of some kind,
                  * and is a valid machine name. */
-                if (!in_charset(m, "0123456789") && machine_name_is_valid(m))
+                if (!in_charset(m, "0123456789") && hostname_is_valid(m, 0))
                         c = strjoina(",argv", p ? "7" : "5", "=--machine=", m);
         }
 
@@ -1538,7 +1538,7 @@ _public_ int sd_bus_open_system_machine(sd_bus **ret, const char *machine) {
 
         assert_return(machine, -EINVAL);
         assert_return(ret, -EINVAL);
-        assert_return(streq(machine, ".host") || machine_name_is_valid(machine), -EINVAL);
+        assert_return(hostname_is_valid(machine, VALID_HOSTNAME_DOT_HOST), -EINVAL);
 
         r = sd_bus_new(&b);
         if (r < 0)
index 1fc379512f0194a0ce88fda6adb923d4c0a8f9ca..d15dafbd95641b16c6a72fa9cc92961a895c232b 100644 (file)
@@ -847,7 +847,7 @@ _public_ int sd_get_machine_names(char ***machines) {
 
                 /* Filter out the unit: symlinks */
                 for (a = b = l; *a; a++) {
-                        if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
+                        if (startswith(*a, "unit:") || !hostname_is_valid(*a, 0))
                                 free(*a);
                         else {
                                 *b = *a;
@@ -877,7 +877,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
                 if (!c)
                         return -ENOMEM;
         } else {
-                if (!machine_name_is_valid(machine))
+                if (!hostname_is_valid(machine, 0))
                         return -EINVAL;
 
                 p = strjoina("/run/systemd/machines/", machine);
@@ -899,7 +899,7 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices)
         const char *p;
         int r;
 
-        assert_return(machine_name_is_valid(machine), -EINVAL);
+        assert_return(hostname_is_valid(machine, 0), -EINVAL);
 
         p = strjoina("/run/systemd/machines/", machine);
         r = parse_env_file(NULL, p, "NETIF", &netif_line);
index 4a3279d2640810f3ab3b1c48b109da0b907b3300..3c839455b699943c2d2ffec1cc561d0087f46c63 100644 (file)
@@ -1561,7 +1561,7 @@ static int make_service_name(const char *name, char **ret) {
         assert(name);
         assert(ret);
 
-        if (!machine_name_is_valid(name))
+        if (!hostname_is_valid(name, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Invalid machine name %s.", name);
 
@@ -1881,7 +1881,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
 
         local = ll;
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Local name %s is not a suitable machine name.",
                                        local);
@@ -1941,7 +1941,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
 
         local = ll;
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Local name %s is not a suitable machine name.",
                                        local);
@@ -1995,7 +1995,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Need either path or local name.");
 
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Local name %s is not a suitable machine name.",
                                        local);
@@ -2048,7 +2048,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
         assert(bus);
 
         local = argv[1];
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Machine name %s is not valid.", local);
 
@@ -2090,7 +2090,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
         assert(bus);
 
         local = argv[1];
-        if (!machine_name_is_valid(local))
+        if (!hostname_is_valid(local, 0))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Machine name %s is not valid.", local);
 
@@ -2155,7 +2155,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local name %s is not a suitable machine name.",
                                                local);
@@ -2211,7 +2211,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
 
                 local = ll;
 
-                if (!machine_name_is_valid(local))
+                if (!hostname_is_valid(local, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Local name %s is not a suitable machine name.",
                                                local);
index 494813e334813da904392174bd51419e5ebdbc89..501db643f8f9ec5c74aa09ef3f3b4e0177c88807 100644 (file)
@@ -239,7 +239,7 @@ static int method_create_or_register_machine(Manager *manager, sd_bus_message *m
         r = sd_bus_message_read(message, "s", &name);
         if (r < 0)
                 return r;
-        if (!machine_name_is_valid(name))
+        if (!hostname_is_valid(name, 0))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
 
         r = sd_bus_message_read_array(message, 'y', &v, &n);
index 4d8891c4fc4fc479ca3fc9c2ad122c374a678b1f..6a5bf391e6b24d477d302c215315e697210a3157 100644 (file)
@@ -166,7 +166,7 @@ static int manager_enumerate_machines(Manager *m) {
                 if (startswith(de->d_name, "unit:"))
                         continue;
 
-                if (!machine_name_is_valid(de->d_name))
+                if (!hostname_is_valid(de->d_name, 0))
                         continue;
 
                 k = manager_add_machine(m, de->d_name, &machine);
index 2fa21a067a4d3d7a142676a7c0e6e1025eca4943..f9b51d8b7bf05ecd64f49ef55f7b758a3d0b7ff3 100644 (file)
@@ -601,7 +601,7 @@ static int parse_cmdline_ip_address(Context *context, int family, const char *va
 
         if (p != value) {
                 hostname = strndupa(value, p - value);
-                if (!hostname_is_valid(hostname, false))
+                if (!hostname_is_valid(hostname, 0))
                         return -EINVAL;
         }
 
index 7ad8d087f41786ee7c77a338e7c37370b8a1f954..73c3788e278cee6eb7038f6c6c7a527db1352aa5 100644 (file)
@@ -922,7 +922,7 @@ int config_parse_hostname(
         if (r < 0)
                 return r;
 
-        if (!hostname_is_valid(hn, false)) {
+        if (!hostname_is_valid(hn, 0)) {
                 log_syntax(unit, LOG_WARNING, filename, line, 0,
                            "Hostname is not valid, ignoring assignment: %s", rvalue);
                 return 0;
index ca708be7556b66b90b8256876f2c68a39090ea30..ccbae616cac0ed552f10c55d75c59d9ad62177a1 100644 (file)
@@ -462,7 +462,7 @@ static int oci_hostname(const char *name, JsonVariant *v, JsonDispatchFlags flag
 
         assert_se(n = json_variant_string(v));
 
-        if (!hostname_is_valid(n, false))
+        if (!hostname_is_valid(n, 0))
                 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                 "Hostname string is not a valid hostname: %s", n);
 
index 92bb5120abe82e2056e98d52548264f949fd51f3..b43d61468ea0b16a85ebf6e69a9e72d33355a8ae 100644 (file)
@@ -701,7 +701,7 @@ int config_parse_hostname(
         assert(rvalue);
         assert(s);
 
-        if (!hostname_is_valid(rvalue, false)) {
+        if (!hostname_is_valid(rvalue, 0)) {
                 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid hostname, ignoring: %s", rvalue);
                 return 0;
         }
index 5e3d11be36cbc70433b19d826ebec6cbab8c5b27..cfbc8f11bf1fc23efa426bcb319313b535a31299 100644 (file)
@@ -985,7 +985,7 @@ static int parse_argv(int argc, char *argv[]) {
                         if (isempty(optarg))
                                 arg_machine = mfree(arg_machine);
                         else {
-                                if (!machine_name_is_valid(optarg))
+                                if (!hostname_is_valid(optarg, 0))
                                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                                "Invalid machine name: %s", optarg);
 
@@ -999,7 +999,7 @@ static int parse_argv(int argc, char *argv[]) {
                         if (isempty(optarg))
                                 arg_hostname = mfree(arg_hostname);
                         else {
-                                if (!hostname_is_valid(optarg, false))
+                                if (!hostname_is_valid(optarg, 0))
                                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                                "Invalid hostname: %s", optarg);
 
@@ -2984,7 +2984,7 @@ static int determine_names(void) {
                         return log_oom();
 
                 hostname_cleanup(arg_machine);
-                if (!machine_name_is_valid(arg_machine))
+                if (!hostname_is_valid(arg_machine, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
 
                 if (arg_ephemeral) {
index 73839b3115197bc2160ad4ad65d4334fa9d5e54e..c7a63bd047d6788cc4263351a9b57e09b5dea678 100644 (file)
 #include "util.h"
 
 static void test_hostname_is_valid(void) {
-        assert_se(hostname_is_valid("foobar", false));
-        assert_se(hostname_is_valid("foobar.com", false));
-        assert_se(!hostname_is_valid("foobar.com.", false));
-        assert_se(hostname_is_valid("fooBAR", false));
-        assert_se(hostname_is_valid("fooBAR.com", false));
-        assert_se(!hostname_is_valid("fooBAR.", false));
-        assert_se(!hostname_is_valid("fooBAR.com.", false));
-        assert_se(!hostname_is_valid("fööbar", false));
-        assert_se(!hostname_is_valid("", false));
-        assert_se(!hostname_is_valid(".", false));
-        assert_se(!hostname_is_valid("..", false));
-        assert_se(!hostname_is_valid("foobar.", false));
-        assert_se(!hostname_is_valid(".foobar", false));
-        assert_se(!hostname_is_valid("foo..bar", false));
-        assert_se(!hostname_is_valid("foo.bar..", false));
-        assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
-        assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
-
-        assert_se(hostname_is_valid("foobar", true));
-        assert_se(hostname_is_valid("foobar.com", true));
-        assert_se(hostname_is_valid("foobar.com.", true));
-        assert_se(hostname_is_valid("fooBAR", true));
-        assert_se(hostname_is_valid("fooBAR.com", true));
-        assert_se(!hostname_is_valid("fooBAR.", true));
-        assert_se(hostname_is_valid("fooBAR.com.", true));
-        assert_se(!hostname_is_valid("fööbar", true));
-        assert_se(!hostname_is_valid("", true));
-        assert_se(!hostname_is_valid(".", true));
-        assert_se(!hostname_is_valid("..", true));
-        assert_se(!hostname_is_valid("foobar.", true));
-        assert_se(!hostname_is_valid(".foobar", true));
-        assert_se(!hostname_is_valid("foo..bar", true));
-        assert_se(!hostname_is_valid("foo.bar..", true));
-        assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
+        assert_se(hostname_is_valid("foobar", 0));
+        assert_se(hostname_is_valid("foobar.com", 0));
+        assert_se(!hostname_is_valid("foobar.com.", 0));
+        assert_se(hostname_is_valid("fooBAR", 0));
+        assert_se(hostname_is_valid("fooBAR.com", 0));
+        assert_se(!hostname_is_valid("fooBAR.", 0));
+        assert_se(!hostname_is_valid("fooBAR.com.", 0));
+        assert_se(!hostname_is_valid("fööbar", 0));
+        assert_se(!hostname_is_valid("", 0));
+        assert_se(!hostname_is_valid(".", 0));
+        assert_se(!hostname_is_valid("..", 0));
+        assert_se(!hostname_is_valid("foobar.", 0));
+        assert_se(!hostname_is_valid(".foobar", 0));
+        assert_se(!hostname_is_valid("foo..bar", 0));
+        assert_se(!hostname_is_valid("foo.bar..", 0));
+        assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
+        assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
+
+        assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT));
+        assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT));
 }
 
 static void test_hostname_cleanup(void) {
@@ -151,7 +151,7 @@ static void test_hostname_malloc(void) {
 }
 
 static void test_fallback_hostname(void) {
-        if (!hostname_is_valid(FALLBACK_HOSTNAME, false)) {
+        if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
                 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
                 exit(EXIT_FAILURE);
         }