hostname-util: flagsify hostname_is_valid(), drop machine_name_is_valid()

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.
This commit is contained in:
Lennart Poettering 2020-12-11 16:40:45 +01:00
parent 9e815cf2c2
commit 52ef5dd798
30 changed files with 107 additions and 108 deletions

View File

@ -98,28 +98,24 @@ bool valid_ldh_char(char c) {
c == '-'; c == '-';
} }
/** bool hostname_is_valid(const char *s, ValidHostnameFlags flags) {
* 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) {
unsigned n_dots = 0; unsigned n_dots = 0;
const char *p; const char *p;
bool dot, hyphen; 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)) if (isempty(s))
return false; return false;
/* Doesn't accept empty hostnames, hostnames with if (streq(s, ".host")) /* Used by the container logic to denote the "root container" */
* leading dots, and hostnames with multiple dots in a return FLAGS_SET(flags, VALID_HOSTNAME_DOT_HOST);
* sequence. Also ensures that the length stays below
* HOST_NAME_MAX. */
for (p = s, dot = hyphen = true; *p; p++) for (p = s, dot = hyphen = true; *p; p++)
if (*p == '.') { if (*p == '.') {
@ -145,14 +141,13 @@ bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
hyphen = false; hyphen = false;
} }
if (dot && (n_dots < 2 || !allow_trailing_dot)) if (dot && (n_dots < 2 || !FLAGS_SET(flags, VALID_HOSTNAME_TRAILING_DOT)))
return false; return false;
if (hyphen) if (hyphen)
return false; return false;
if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on Linux, but DNS allows domain names up to
* Linux, but DNS allows domain names * 255 characters */
* up to 255 characters */
return false; return false;
return true; return true;
@ -243,7 +238,7 @@ int shorten_overlong(const char *s, char **ret) {
if (!h) if (!h)
return -ENOMEM; return -ENOMEM;
if (hostname_is_valid(h, false)) { if (hostname_is_valid(h, 0)) {
*ret = h; *ret = h;
return 0; return 0;
} }
@ -254,7 +249,7 @@ int shorten_overlong(const char *s, char **ret) {
strshorten(h, HOST_NAME_MAX); strshorten(h, HOST_NAME_MAX);
if (!hostname_is_valid(h, false)) { if (!hostname_is_valid(h, 0)) {
free(h); free(h);
return -EDOM; return -EDOM;
} }
@ -287,7 +282,7 @@ int read_etc_hostname_stream(FILE *f, char **ret) {
hostname_cleanup(p); /* normalize the hostname */ 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; return -EBADMSG;
copy = strdup(p); copy = strdup(p);

View File

@ -14,10 +14,14 @@ char* gethostname_short_malloc(void);
int gethostname_strict(char **ret); int gethostname_strict(char **ret);
bool valid_ldh_char(char c) _const_; 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); bool is_localhost(const char *hostname);

View File

@ -165,7 +165,7 @@ int container_get_leader(const char *machine, pid_t *pid) {
return 0; return 0;
} }
if (!machine_name_is_valid(machine)) if (!hostname_is_valid(machine, 0))
return -EINVAL; return -EINVAL;
p = strjoina("/run/systemd/machines/", machine); p = strjoina("/run/systemd/machines/", machine);

View File

@ -24,7 +24,7 @@ int hostname_setup(void) {
if (r < 0) if (r < 0)
log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m"); log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m");
else if (r > 0) { else if (r > 0) {
if (hostname_is_valid(b, true)) if (hostname_is_valid(b, VALID_HOSTNAME_TRAILING_DOT))
hn = b; hn = b;
else { else {
log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b); log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b);

View File

@ -493,7 +493,7 @@ static int prompt_hostname(void) {
break; break;
} }
if (!hostname_is_valid(h, true)) { if (!hostname_is_valid(h, VALID_HOSTNAME_TRAILING_DOT)) {
log_error("Specified hostname invalid."); log_error("Specified hostname invalid.");
continue; continue;
} }
@ -1135,7 +1135,7 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case ARG_HOSTNAME: 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), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Host name %s is not valid.", optarg); "Host name %s is not valid.", optarg);

View 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 /* 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 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
* hostname. */ * 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 */ p = ""; /* No pretty hostname (as it is redundant), just a static one */
else else
p = hostname; /* Use the passed name as pretty hostname */ p = hostname; /* Use the passed name as pretty hostname */

View File

@ -596,7 +596,7 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *
if (isempty(name)) if (isempty(name))
name = FALLBACK_HOSTNAME; 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); return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
assert_se(uname(&u) >= 0); 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])) if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
return sd_bus_reply_method_return(m, NULL); 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); return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
r = bus_verify_polkit_async( r = bus_verify_polkit_async(

View File

@ -65,7 +65,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
_cleanup_close_ int open_fd = -1; _cleanup_close_ int open_fd = -1;
int r, fd; 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); r = image_find(IMAGE_MACHINE, argv[1], &image);
if (r == -ENOENT) if (r == -ENOENT)
return log_error_errno(r, "Machine image %s not found.", argv[1]); 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; _cleanup_close_ int open_fd = -1;
int r, fd; 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); r = image_find(IMAGE_MACHINE, argv[1], &image);
if (r == -ENOENT) if (r == -ENOENT)
return log_error_errno(r, "Machine image %s not found.", argv[1]); return log_error_errno(r, "Machine image %s not found.", argv[1]);

View File

@ -126,7 +126,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
local = empty_or_dash_to_null(local); local = empty_or_dash_to_null(local);
if (local) { if (local) {
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", "Local image name '%s' is not valid.",
local); local);

View File

@ -393,7 +393,7 @@ int raw_import_start(RawImport *i, int fd, const char *local, bool force_local,
assert(fd >= 0); assert(fd >= 0);
assert(local); assert(local);
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return -EINVAL; return -EINVAL;
if (i->input_fd >= 0) if (i->input_fd >= 0)

View File

@ -329,7 +329,7 @@ int tar_import_start(TarImport *i, int fd, const char *local, bool force_local,
assert(fd >= 0); assert(fd >= 0);
assert(local); assert(local);
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return -EINVAL; return -EINVAL;
if (i->input_fd >= 0) if (i->input_fd >= 0)

View File

@ -64,7 +64,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", "Local image name '%s' is not valid.",
local); local);
@ -159,7 +159,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", "Local image name '%s' is not valid.",
local); local);

View 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)) if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode))
return -EINVAL; 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, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local); "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) if (r < 0)
return r; 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, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local); "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) if (r < 0)
return r; 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, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local); "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)) if (isempty(local))
local = NULL; 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, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local); "Local name %s is invalid", local);

View File

@ -651,7 +651,7 @@ int raw_pull_start(
if (!http_url_is_valid(url)) if (!http_url_is_valid(url))
return -EINVAL; return -EINVAL;
if (local && !machine_name_is_valid(local)) if (local && !hostname_is_valid(local, 0))
return -EINVAL; return -EINVAL;
if (i->raw_job) if (i->raw_job)

View File

@ -481,7 +481,7 @@ int tar_pull_start(
if (!http_url_is_valid(url)) if (!http_url_is_valid(url))
return -EINVAL; return -EINVAL;
if (local && !machine_name_is_valid(local)) if (local && !hostname_is_valid(local, 0))
return -EINVAL; return -EINVAL;
if (i->tar_job) if (i->tar_job)

View File

@ -72,7 +72,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", "Local image name '%s' is not valid.",
local); local);
@ -158,7 +158,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", "Local image name '%s' is not valid.",
local); local);

View File

@ -1981,7 +1981,7 @@ _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, in
assert_return(machine, -EINVAL); assert_return(machine, -EINVAL);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -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); p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(NULL, p, r = parse_env_file(NULL, p,

View File

@ -540,7 +540,7 @@ int sd_dhcp_client_set_hostname(
/* Make sure hostnames qualify as DNS and as Linux hostnames */ /* Make sure hostnames qualify as DNS and as Linux hostnames */
if (hostname && 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 -EINVAL;
return free_and_strdup(&client->hostname, hostname); return free_and_strdup(&client->hostname, hostname);

View File

@ -407,7 +407,7 @@ int sd_dhcp6_client_set_fqdn(
/* Make sure FQDN qualifies as DNS and as Linux hostname */ /* Make sure FQDN qualifies as DNS and as Linux hostname */
if (fqdn && 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 -EINVAL;
return free_and_strdup(&client->fqdn, fqdn); return free_and_strdup(&client->fqdn, fqdn);

View File

@ -973,7 +973,7 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid)
return -EINVAL; return -EINVAL;
if (machine) { if (machine) {
if (!streq(machine, ".host") && !machine_name_is_valid(machine)) if (!hostname_is_valid(machine, VALID_HOSTNAME_DOT_HOST))
return -EINVAL; return -EINVAL;
free_and_replace(b->machine, machine); 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 (!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; return -EINVAL;
m = TAKE_PTR(p); 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: interpret_port_as_machine_old_syntax:
/* Let's make sure this is not a port of some kind, /* Let's make sure this is not a port of some kind,
* and is a valid machine name. */ * 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); 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(machine, -EINVAL);
assert_return(ret, -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); r = sd_bus_new(&b);
if (r < 0) if (r < 0)

View File

@ -847,7 +847,7 @@ _public_ int sd_get_machine_names(char ***machines) {
/* Filter out the unit: symlinks */ /* Filter out the unit: symlinks */
for (a = b = l; *a; a++) { 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); free(*a);
else { else {
*b = *a; *b = *a;
@ -877,7 +877,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
if (!c) if (!c)
return -ENOMEM; return -ENOMEM;
} else { } else {
if (!machine_name_is_valid(machine)) if (!hostname_is_valid(machine, 0))
return -EINVAL; return -EINVAL;
p = strjoina("/run/systemd/machines/", machine); 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; const char *p;
int r; int r;
assert_return(machine_name_is_valid(machine), -EINVAL); assert_return(hostname_is_valid(machine, 0), -EINVAL);
p = strjoina("/run/systemd/machines/", machine); p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(NULL, p, "NETIF", &netif_line); r = parse_env_file(NULL, p, "NETIF", &netif_line);

View File

@ -1561,7 +1561,7 @@ static int make_service_name(const char *name, char **ret) {
assert(name); assert(name);
assert(ret); assert(ret);
if (!machine_name_is_valid(name)) if (!hostname_is_valid(name, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid machine name %s.", name); "Invalid machine name %s.", name);
@ -1881,7 +1881,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.", "Local name %s is not a suitable machine name.",
local); local);
@ -1941,7 +1941,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.", "Local name %s is not a suitable machine name.",
local); local);
@ -1995,7 +1995,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Need either path or local name."); "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), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.", "Local name %s is not a suitable machine name.",
local); local);
@ -2048,7 +2048,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
local = argv[1]; local = argv[1];
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Machine name %s is not valid.", local); "Machine name %s is not valid.", local);
@ -2090,7 +2090,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
local = argv[1]; local = argv[1];
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Machine name %s is not valid.", local); "Machine name %s is not valid.", local);
@ -2155,7 +2155,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.", "Local name %s is not a suitable machine name.",
local); local);
@ -2211,7 +2211,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
local = ll; local = ll;
if (!machine_name_is_valid(local)) if (!hostname_is_valid(local, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.", "Local name %s is not a suitable machine name.",
local); local);

View 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); r = sd_bus_message_read(message, "s", &name);
if (r < 0) if (r < 0)
return r; 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"); return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
r = sd_bus_message_read_array(message, 'y', &v, &n); r = sd_bus_message_read_array(message, 'y', &v, &n);

View File

@ -166,7 +166,7 @@ static int manager_enumerate_machines(Manager *m) {
if (startswith(de->d_name, "unit:")) if (startswith(de->d_name, "unit:"))
continue; continue;
if (!machine_name_is_valid(de->d_name)) if (!hostname_is_valid(de->d_name, 0))
continue; continue;
k = manager_add_machine(m, de->d_name, &machine); k = manager_add_machine(m, de->d_name, &machine);

View File

@ -601,7 +601,7 @@ static int parse_cmdline_ip_address(Context *context, int family, const char *va
if (p != value) { if (p != value) {
hostname = strndupa(value, p - value); hostname = strndupa(value, p - value);
if (!hostname_is_valid(hostname, false)) if (!hostname_is_valid(hostname, 0))
return -EINVAL; return -EINVAL;
} }

View File

@ -922,7 +922,7 @@ int config_parse_hostname(
if (r < 0) if (r < 0)
return r; return r;
if (!hostname_is_valid(hn, false)) { if (!hostname_is_valid(hn, 0)) {
log_syntax(unit, LOG_WARNING, filename, line, 0, log_syntax(unit, LOG_WARNING, filename, line, 0,
"Hostname is not valid, ignoring assignment: %s", rvalue); "Hostname is not valid, ignoring assignment: %s", rvalue);
return 0; return 0;

View File

@ -462,7 +462,7 @@ static int oci_hostname(const char *name, JsonVariant *v, JsonDispatchFlags flag
assert_se(n = json_variant_string(v)); 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), return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
"Hostname string is not a valid hostname: %s", n); "Hostname string is not a valid hostname: %s", n);

View File

@ -701,7 +701,7 @@ int config_parse_hostname(
assert(rvalue); assert(rvalue);
assert(s); 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); log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid hostname, ignoring: %s", rvalue);
return 0; return 0;
} }

View File

@ -985,7 +985,7 @@ static int parse_argv(int argc, char *argv[]) {
if (isempty(optarg)) if (isempty(optarg))
arg_machine = mfree(arg_machine); arg_machine = mfree(arg_machine);
else { else {
if (!machine_name_is_valid(optarg)) if (!hostname_is_valid(optarg, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid machine name: %s", optarg); "Invalid machine name: %s", optarg);
@ -999,7 +999,7 @@ static int parse_argv(int argc, char *argv[]) {
if (isempty(optarg)) if (isempty(optarg))
arg_hostname = mfree(arg_hostname); arg_hostname = mfree(arg_hostname);
else { else {
if (!hostname_is_valid(optarg, false)) if (!hostname_is_valid(optarg, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid hostname: %s", optarg); "Invalid hostname: %s", optarg);
@ -2984,7 +2984,7 @@ static int determine_names(void) {
return log_oom(); return log_oom();
hostname_cleanup(arg_machine); 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."); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
if (arg_ephemeral) { if (arg_ephemeral) {

View File

@ -10,40 +10,40 @@
#include "util.h" #include "util.h"
static void test_hostname_is_valid(void) { static void test_hostname_is_valid(void) {
assert_se(hostname_is_valid("foobar", false)); assert_se(hostname_is_valid("foobar", 0));
assert_se(hostname_is_valid("foobar.com", false)); assert_se(hostname_is_valid("foobar.com", 0));
assert_se(!hostname_is_valid("foobar.com.", false)); assert_se(!hostname_is_valid("foobar.com.", 0));
assert_se(hostname_is_valid("fooBAR", false)); assert_se(hostname_is_valid("fooBAR", 0));
assert_se(hostname_is_valid("fooBAR.com", false)); assert_se(hostname_is_valid("fooBAR.com", 0));
assert_se(!hostname_is_valid("fooBAR.", false)); assert_se(!hostname_is_valid("fooBAR.", 0));
assert_se(!hostname_is_valid("fooBAR.com.", false)); assert_se(!hostname_is_valid("fooBAR.com.", 0));
assert_se(!hostname_is_valid("fööbar", false)); assert_se(!hostname_is_valid("fööbar", 0));
assert_se(!hostname_is_valid("", false)); assert_se(!hostname_is_valid("", 0));
assert_se(!hostname_is_valid(".", false)); assert_se(!hostname_is_valid(".", 0));
assert_se(!hostname_is_valid("..", false)); assert_se(!hostname_is_valid("..", 0));
assert_se(!hostname_is_valid("foobar.", false)); assert_se(!hostname_is_valid("foobar.", 0));
assert_se(!hostname_is_valid(".foobar", false)); assert_se(!hostname_is_valid(".foobar", 0));
assert_se(!hostname_is_valid("foo..bar", false)); assert_se(!hostname_is_valid("foo..bar", 0));
assert_se(!hostname_is_valid("foo.bar..", false)); assert_se(!hostname_is_valid("foo.bar..", 0));
assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false)); assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false)); assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
assert_se(hostname_is_valid("foobar", true)); assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("foobar.com", true)); assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("foobar.com.", true)); assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR", true)); assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR.com", true)); assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("fooBAR.", true)); assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR.com.", true)); assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("fööbar", true)); assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("", true)); assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid(".", true)); assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("..", true)); assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foobar.", true)); assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid(".foobar", true)); assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foo..bar", true)); assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foo.bar..", true)); assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true)); assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT));
} }
static void test_hostname_cleanup(void) { static void test_hostname_cleanup(void) {
@ -151,7 +151,7 @@ static void test_hostname_malloc(void) {
} }
static void test_fallback_hostname(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); log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }