From 8fb494435889dcb9e1c09b8c7220e47bab717bf9 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:15:26 -0400 Subject: hostname-util: add relax parameter to hostname_is_valid Tests are modified to check behaviour with relax and without relax. New tests are added for hostname_cleanup(). Tests are moved a new file (test-hostname-util) because there's now a bunch of them. New parameter is not used anywhere, except in tests, so there should be no observable change. --- src/basic/hostname-util.c | 15 ++- src/basic/hostname-util.h | 2 +- src/basic/util.c | 2 +- src/basic/util.h | 5 + src/firstboot/firstboot.c | 8 +- src/hostname/hostnamed.c | 4 +- src/import/pull-dkr.c | 2 +- src/libsystemd-network/sd-dhcp-lease.c | 5 +- src/locale/localed.c | 5 - src/network/networkd-network.c | 2 +- src/test/test-hostname-util.c | 163 +++++++++++++++++++++++++++++++++ src/test/test-util.c | 69 -------------- 12 files changed, 191 insertions(+), 91 deletions(-) create mode 100644 src/test/test-hostname-util.c (limited to 'src') diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index e336f269fa..21fae89fd0 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -61,14 +61,22 @@ static bool hostname_valid_char(char c) { c == '.'; } -bool hostname_is_valid(const char *s) { +/** + * Check if s looks like a valid host name 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 + * relax is true and at least two components are present in the name. + */ +bool hostname_is_valid(const char *s, bool relax) { const char *p; bool dot; + unsigned dots = 0; if (isempty(s)) return false; - /* Doesn't accept empty hostnames, hostnames with trailing or + /* 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. */ @@ -79,6 +87,7 @@ bool hostname_is_valid(const char *s) { return false; dot = true; + dots ++; } else { if (!hostname_valid_char(*p)) return false; @@ -87,7 +96,7 @@ bool hostname_is_valid(const char *s) { } } - if (dot) + if (dot && (dots < 2 || !relax)) return false; if (p-s > HOST_NAME_MAX) diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index 0c4763cf5a..ecae3a267c 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -29,7 +29,7 @@ bool hostname_is_set(void); char* gethostname_malloc(void); -bool hostname_is_valid(const char *s) _pure_; +bool hostname_is_valid(const char *s, bool relax) _pure_; char* hostname_cleanup(char *s, bool lowercase); bool is_localhost(const char *hostname); diff --git a/src/basic/util.c b/src/basic/util.c index 7896be8788..f81bf4e31e 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -2997,7 +2997,7 @@ char* strshorten(char *s, size_t l) { bool machine_name_is_valid(const char *s) { - if (!hostname_is_valid(s)) + if (!hostname_is_valid(s, false)) return false; /* Machine names should be useful hostnames, but also be diff --git a/src/basic/util.h b/src/basic/util.h index c2e5cc610b..5c0130d265 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -858,6 +858,11 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags); int unquote_first_word_and_warn(const char **p, char **ret, UnquoteFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue); int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_; +static inline void free_and_replace(char **s, char *v) { + free(*s); + *s = v; +} + int free_and_strdup(char **p, const char *s); #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 3805b29437..43669d28bf 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -386,7 +386,7 @@ static int prompt_hostname(void) { break; } - if (!hostname_is_valid(h)) { + if (!hostname_is_valid(h, false)) { log_error("Specified hostname invalid."); continue; } @@ -780,14 +780,12 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_HOSTNAME: - if (!hostname_is_valid(optarg)) { + if (!hostname_is_valid(optarg, false)) { log_error("Host name %s is not valid.", optarg); return -EINVAL; } - free(arg_hostname); - arg_hostname = strdup(optarg); - if (!arg_hostname) + if (free_and_strdup(&arg_hostname, optarg) < 0) return log_oom(); break; diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index e52b872a8c..a78516c8b5 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -424,7 +424,7 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error * if (isempty(name)) name = "localhost"; - if (!hostname_is_valid(name)) + if (!hostname_is_valid(name, false)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name); if (streq_ptr(name, c->data[PROP_HOSTNAME])) @@ -501,7 +501,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_ } else { char *h; - if (!hostname_is_valid(name)) + if (!hostname_is_valid(name, false)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name); h = strdup(name); diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c index 67ca1ce8e4..53d5174f33 100644 --- a/src/import/pull-dkr.c +++ b/src/import/pull-dkr.c @@ -721,7 +721,7 @@ static int dkr_pull_job_on_header(PullJob *j, const char *header, size_t sz) { return log_oom(); STRV_FOREACH(k, l) { - if (!hostname_is_valid(*k)) { + if (!hostname_is_valid(*k, false)) { log_error("Registry hostname is not valid."); strv_free(l); return -EBADMSG; diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 54417b3af3..43f7566f65 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -553,11 +553,10 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option, if (e) *e = 0; - if (!hostname_is_valid(hostname) || is_localhost(hostname)) + if (!hostname_is_valid(hostname, false) || is_localhost(hostname)) break; - free(lease->hostname); - lease->hostname = hostname; + free_and_replace(&lease->hostname, hostname); hostname = NULL; break; diff --git a/src/locale/localed.c b/src/locale/localed.c index 88756542fd..3b511bfaf4 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -99,11 +99,6 @@ static const char* nonempty(const char *s) { return isempty(s) ? NULL : s; } -static void free_and_replace(char **s, char *v) { - free(*s); - *s = v; -} - static bool startswith_comma(const char *s, const char *prefix) { const char *t; diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index d8f42621af..e3ac70ff33 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -838,7 +838,7 @@ int config_parse_hostname(const char *unit, if (r < 0) return r; - if (!hostname_is_valid(hn)) { + if (!hostname_is_valid(hn, false)) { log_syntax(unit, LOG_ERR, filename, line, EINVAL, "hostname is not valid, ignoring assignment: %s", rvalue); free(hn); diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c new file mode 100644 index 0000000000..2357ab5758 --- /dev/null +++ b/src/test/test-hostname-util.c @@ -0,0 +1,163 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + Copyright 2013 Thomas H.P. Andersen + Copyright 2015 Zbigniew Jędrzejewski-Szmek + + 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 "util.h" +#include "fileio.h" +#include "hostname-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("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)); +} + +static void test_hostname_cleanup(void) { + char *s; + + s = strdupa("foobar"); + assert_se(streq(hostname_cleanup(s, false), "foobar")); + s = strdupa("foobar.com"); + assert_se(streq(hostname_cleanup(s, false), "foobar.com")); + s = strdupa("foobar.com."); + assert_se(streq(hostname_cleanup(s, false), "foobar.com")); + s = strdupa("fooBAR"); + assert_se(streq(hostname_cleanup(s, false), "fooBAR")); + s = strdupa("fooBAR.com"); + assert_se(streq(hostname_cleanup(s, false), "fooBAR.com")); + s = strdupa("fooBAR."); + assert_se(streq(hostname_cleanup(s, false), "fooBAR")); + s = strdupa("fooBAR.com."); + assert_se(streq(hostname_cleanup(s, false), "fooBAR.com")); + s = strdupa("fööbar"); + assert_se(streq(hostname_cleanup(s, false), "fbar")); + s = strdupa(""); + assert_se(isempty(hostname_cleanup(s, false))); + s = strdupa("."); + assert_se(isempty(hostname_cleanup(s, false))); + s = strdupa(".."); + assert_se(isempty(hostname_cleanup(s, false))); + s = strdupa("foobar."); + assert_se(streq(hostname_cleanup(s, false), "foobar")); + s = strdupa(".foobar"); + assert_se(streq(hostname_cleanup(s, false), "foobar")); + s = strdupa("foo..bar"); + assert_se(streq(hostname_cleanup(s, false), "foo.bar")); + s = strdupa("foo.bar.."); + assert_se(streq(hostname_cleanup(s, false), "foo.bar")); + s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + assert_se(streq(hostname_cleanup(s, false), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); +} + +static void test_read_hostname_config(void) { + char path[] = "/tmp/hostname.XXXXXX"; + char *hostname; + int fd; + + fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC); + assert(fd > 0); + close(fd); + + /* simple hostname */ + write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); + assert_se(read_hostname_config(path, &hostname) == 0); + assert_se(streq(hostname, "foo")); + free(hostname); + hostname = NULL; + + /* with comment */ + write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); + assert_se(read_hostname_config(path, &hostname) == 0); + assert_se(hostname); + assert_se(streq(hostname, "foo")); + free(hostname); + hostname = NULL; + + /* with comment and extra whitespace */ + write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); + assert_se(read_hostname_config(path, &hostname) == 0); + assert_se(hostname); + assert_se(streq(hostname, "foo")); + free(hostname); + hostname = NULL; + + /* cleans up name */ + write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); + assert_se(read_hostname_config(path, &hostname) == 0); + assert_se(hostname); + assert_se(streq(hostname, "foobar.com")); + free(hostname); + hostname = NULL; + + /* no value set */ + hostname = (char*) 0x1234; + write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE); + assert_se(read_hostname_config(path, &hostname) == -ENOENT); + assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ + + /* nonexisting file */ + assert_se(read_hostname_config("/non/existing", &hostname) == -ENOENT); + assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ + + unlink(path); +} + +int main(int argc, char *argv[]) { + log_parse_environment(); + log_open(); + + test_hostname_is_valid(); + test_hostname_cleanup(); + test_read_hostname_config(); + + return 0; +} diff --git a/src/test/test-util.c b/src/test/test-util.c index f43433baa1..9ad912d20f 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -38,7 +38,6 @@ #include "conf-parser.h" #include "virt.h" #include "process-util.h" -#include "hostname-util.h" #include "signal-util.h" static void test_streq_ptr(void) { @@ -833,72 +832,6 @@ static void test_memdup_multiply(void) { free(dup); } -static void test_hostname_is_valid(void) { - assert_se(hostname_is_valid("foobar")); - assert_se(hostname_is_valid("foobar.com")); - assert_se(!hostname_is_valid("fööbar")); - assert_se(!hostname_is_valid("")); - assert_se(!hostname_is_valid(".")); - assert_se(!hostname_is_valid("..")); - assert_se(!hostname_is_valid("foobar.")); - assert_se(!hostname_is_valid(".foobar")); - assert_se(!hostname_is_valid("foo..bar")); - assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); -} - -static void test_read_hostname_config(void) { - char path[] = "/tmp/hostname.XXXXXX"; - char *hostname; - int fd; - - fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC); - assert(fd > 0); - close(fd); - - /* simple hostname */ - write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); - assert_se(read_hostname_config(path, &hostname) == 0); - assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; - - /* with comment */ - write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); - assert_se(read_hostname_config(path, &hostname) == 0); - assert_se(hostname); - assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; - - /* with comment and extra whitespace */ - write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); - assert_se(read_hostname_config(path, &hostname) == 0); - assert_se(hostname); - assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; - - /* cleans up name */ - write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); - assert_se(read_hostname_config(path, &hostname) == 0); - assert_se(hostname); - assert_se(streq(hostname, "foobar.com")); - free(hostname); - hostname = NULL; - - /* no value set */ - hostname = (char*) 0x1234; - write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE); - assert_se(read_hostname_config(path, &hostname) == -ENOENT); - assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ - - /* nonexisting file */ - assert_se(read_hostname_config("/non/existing", &hostname) == -ENOENT); - assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ - - unlink(path); -} - static void test_u64log2(void) { assert_se(u64log2(0) == 0); assert_se(u64log2(8) == 3); @@ -2124,8 +2057,6 @@ int main(int argc, char *argv[]) { test_foreach_word(); test_foreach_word_quoted(); test_memdup_multiply(); - test_hostname_is_valid(); - test_read_hostname_config(); test_u64log2(); test_protect_errno(); test_parse_size(); -- cgit v1.2.3-54-g00ecf From 34ad609010244513e0c31fdde59ce1cf84d4c3f6 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:20:46 -0400 Subject: firstboot: allow a trailing dot on fqdn --- src/firstboot/firstboot.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 43669d28bf..df98212da8 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -386,12 +386,13 @@ static int prompt_hostname(void) { break; } - if (!hostname_is_valid(h, false)) { + if (!hostname_is_valid(h, true)) { log_error("Specified hostname invalid."); continue; } - arg_hostname = h; + /* Get rid of the trailing dot that we allow, but don't want to see */ + arg_hostname = hostname_cleanup(h, false); h = NULL; break; } @@ -780,11 +781,12 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_HOSTNAME: - if (!hostname_is_valid(optarg, false)) { + if (!hostname_is_valid(optarg, true)) { log_error("Host name %s is not valid.", optarg); return -EINVAL; } + hostname_cleanup(optarg, false); if (free_and_strdup(&arg_hostname, optarg) < 0) return log_oom(); -- cgit v1.2.3-54-g00ecf From 79f17ea6f8d885da063f50d966891adaee236b26 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:21:07 -0400 Subject: networkd: allow trailing dot on fqdn in config file --- src/network/networkd-network.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index e3ac70ff33..9a19109b9f 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -838,14 +838,14 @@ int config_parse_hostname(const char *unit, if (r < 0) return r; - if (!hostname_is_valid(hn, false)) { + if (!hostname_is_valid(hn, true)) { log_syntax(unit, LOG_ERR, filename, line, EINVAL, "hostname is not valid, ignoring assignment: %s", rvalue); free(hn); return 0; } - *hostname = hn; + *hostname = hostname_cleanup(hn, false); return 0; } -- cgit v1.2.3-54-g00ecf From 17eb9a9ddba3f03fcba33445c1c1eedeb948da04 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:30:06 -0400 Subject: hostnamectl: allow trailing dot on fqdn When the user requests to set hostname, and we are setting both pretty and static hostnames, and the name is a valid FQDN, we use it as the static hostname, and unset the pretty hostname. The change is that a FQDN with a trailing dot is accepted and ignored. https://bugzilla.redhat.com/show_bug.cgi?id=1238246 Lowercasing of the static name is not done anymore. $ hostnamectl set-hostname Foobar. => static is "Foobar", pretty is "Foobar." $ hostnamectl set-hostname Foobar.org. => static is "Foobar.org", pretty is unset $ hostnamectl set-hostname Foobar.org.. => static is "Foobar.org", pretty is "Foobar.org.." --- src/hostname/hostnamectl.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index c996fc04a0..d194e9b629 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -252,7 +252,7 @@ static int set_simple_string(sd_bus *bus, const char *method, const char *value) static int set_hostname(sd_bus *bus, char **args, unsigned n) { _cleanup_free_ char *h = NULL; - const char *hostname = args[1]; + char *hostname = args[1]; int r; assert(args); @@ -270,17 +270,16 @@ static int set_hostname(sd_bus *bus, char **args, unsigned n) { * just set the passed hostname as static/dynamic * hostname. */ - h = strdup(hostname); - if (!h) - return log_oom(); - - hostname_cleanup(h, true); - - if (arg_static && streq(h, hostname)) + if (arg_static && hostname_is_valid(hostname, true)) { p = ""; - else { - p = hostname; - hostname = h; + /* maybe get rid of trailing dot */ + hostname = hostname_cleanup(hostname, false); + } else { + p = h = strdup(hostname); + if (!p) + return log_oom(); + + hostname_cleanup(hostname, false); } r = set_simple_string(bus, "SetPrettyHostname", p); -- cgit v1.2.3-54-g00ecf From ae691c1d9382995ea7e28317f5c37023229c27ee Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:36:36 -0400 Subject: hostname-util: get rid of unused parameter of hostname_cleanup() All users are now setting lowercase=false. --- src/basic/hostname-util.c | 6 +++--- src/basic/hostname-util.h | 2 +- src/firstboot/firstboot.c | 4 ++-- src/hostname/hostnamectl.c | 4 ++-- src/journal-remote/journal-gatewayd.c | 2 +- src/journal/journalctl.c | 2 +- src/network/networkd-network.c | 2 +- src/nspawn/nspawn.c | 2 +- src/test/test-hostname-util.c | 32 ++++++++++++++++---------------- 9 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 21fae89fd0..95d9c3dd83 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -105,7 +105,7 @@ bool hostname_is_valid(const char *s, bool relax) { return true; } -char* hostname_cleanup(char *s, bool lowercase) { +char* hostname_cleanup(char *s) { char *p, *d; bool dot; @@ -119,7 +119,7 @@ char* hostname_cleanup(char *s, bool lowercase) { *(d++) = '.'; dot = true; } else if (hostname_valid_char(*p)) { - *(d++) = lowercase ? tolower(*p) : *p; + *(d++) = *p; dot = false; } @@ -185,7 +185,7 @@ int read_hostname_config(const char *path, char **hostname) { truncate_nl(l); if (l[0] != '\0' && l[0] != '#') { /* found line with value */ - name = hostname_cleanup(l, false); + name = hostname_cleanup(l); name = strdup(name); if (!name) return -ENOMEM; diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index ecae3a267c..6f2b5b66ff 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -30,7 +30,7 @@ bool hostname_is_set(void); char* gethostname_malloc(void); bool hostname_is_valid(const char *s, bool relax) _pure_; -char* hostname_cleanup(char *s, bool lowercase); +char* hostname_cleanup(char *s); bool is_localhost(const char *hostname); diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index df98212da8..01a3d38746 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -392,7 +392,7 @@ static int prompt_hostname(void) { } /* Get rid of the trailing dot that we allow, but don't want to see */ - arg_hostname = hostname_cleanup(h, false); + arg_hostname = hostname_cleanup(h); h = NULL; break; } @@ -786,7 +786,7 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - hostname_cleanup(optarg, false); + hostname_cleanup(optarg); if (free_and_strdup(&arg_hostname, optarg) < 0) return log_oom(); diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index d194e9b629..dcbad99ae9 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -273,13 +273,13 @@ static int set_hostname(sd_bus *bus, char **args, unsigned n) { if (arg_static && hostname_is_valid(hostname, true)) { p = ""; /* maybe get rid of trailing dot */ - hostname = hostname_cleanup(hostname, false); + hostname = hostname_cleanup(hostname); } else { p = h = strdup(hostname); if (!p) return log_oom(); - hostname_cleanup(hostname, false); + hostname_cleanup(hostname); } r = set_simple_string(bus, "SetPrettyHostname", p); diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 9a09f401e0..1eb07a1681 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -797,7 +797,7 @@ static int request_handler_machine( "\"cutoff_to_realtime\" : \"%"PRIu64"\" }\n", SD_ID128_FORMAT_VAL(mid), SD_ID128_FORMAT_VAL(bid), - hostname_cleanup(hostname, false), + hostname_cleanup(hostname), os_name ? os_name : "Linux", v ? v : "bare", usage, diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 073cc77711..d2c0a03ccb 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1527,7 +1527,7 @@ static int setup_keys(void) { hn = gethostname_malloc(); if (hn) { - hostname_cleanup(hn, false); + hostname_cleanup(hn); fprintf(stderr, "\nThe keys have been generated for host %s/" SD_ID128_FORMAT_STR ".\n", hn, SD_ID128_FORMAT_VAL(machine)); } else fprintf(stderr, "\nThe keys have been generated for host " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(machine)); diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 9a19109b9f..6678b2c77a 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -845,7 +845,7 @@ int config_parse_hostname(const char *unit, return 0; } - *hostname = hostname_cleanup(hn, false); + *hostname = hostname_cleanup(hn); return 0; } diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 65b9a5071b..cf77cfd220 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4005,7 +4005,7 @@ static int determine_names(void) { if (!arg_machine) return log_oom(); - hostname_cleanup(arg_machine, false); + hostname_cleanup(arg_machine); if (!machine_name_is_valid(arg_machine)) { log_error("Failed to determine machine name automatically, please use -M."); return -EINVAL; diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c index 2357ab5758..08be3f7bf2 100644 --- a/src/test/test-hostname-util.c +++ b/src/test/test-hostname-util.c @@ -65,37 +65,37 @@ static void test_hostname_cleanup(void) { char *s; s = strdupa("foobar"); - assert_se(streq(hostname_cleanup(s, false), "foobar")); + assert_se(streq(hostname_cleanup(s), "foobar")); s = strdupa("foobar.com"); - assert_se(streq(hostname_cleanup(s, false), "foobar.com")); + assert_se(streq(hostname_cleanup(s), "foobar.com")); s = strdupa("foobar.com."); - assert_se(streq(hostname_cleanup(s, false), "foobar.com")); + assert_se(streq(hostname_cleanup(s), "foobar.com")); s = strdupa("fooBAR"); - assert_se(streq(hostname_cleanup(s, false), "fooBAR")); + assert_se(streq(hostname_cleanup(s), "fooBAR")); s = strdupa("fooBAR.com"); - assert_se(streq(hostname_cleanup(s, false), "fooBAR.com")); + assert_se(streq(hostname_cleanup(s), "fooBAR.com")); s = strdupa("fooBAR."); - assert_se(streq(hostname_cleanup(s, false), "fooBAR")); + assert_se(streq(hostname_cleanup(s), "fooBAR")); s = strdupa("fooBAR.com."); - assert_se(streq(hostname_cleanup(s, false), "fooBAR.com")); + assert_se(streq(hostname_cleanup(s), "fooBAR.com")); s = strdupa("fööbar"); - assert_se(streq(hostname_cleanup(s, false), "fbar")); + assert_se(streq(hostname_cleanup(s), "fbar")); s = strdupa(""); - assert_se(isempty(hostname_cleanup(s, false))); + assert_se(isempty(hostname_cleanup(s))); s = strdupa("."); - assert_se(isempty(hostname_cleanup(s, false))); + assert_se(isempty(hostname_cleanup(s))); s = strdupa(".."); - assert_se(isempty(hostname_cleanup(s, false))); + assert_se(isempty(hostname_cleanup(s))); s = strdupa("foobar."); - assert_se(streq(hostname_cleanup(s, false), "foobar")); + assert_se(streq(hostname_cleanup(s), "foobar")); s = strdupa(".foobar"); - assert_se(streq(hostname_cleanup(s, false), "foobar")); + assert_se(streq(hostname_cleanup(s), "foobar")); s = strdupa("foo..bar"); - assert_se(streq(hostname_cleanup(s, false), "foo.bar")); + assert_se(streq(hostname_cleanup(s), "foo.bar")); s = strdupa("foo.bar.."); - assert_se(streq(hostname_cleanup(s, false), "foo.bar")); + assert_se(streq(hostname_cleanup(s), "foo.bar")); s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); - assert_se(streq(hostname_cleanup(s, false), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); + assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); } static void test_read_hostname_config(void) { -- cgit v1.2.3-54-g00ecf From 90365b043ab6a0d8100e0c37dea4ca9d6fdb4695 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Jul 2015 22:37:52 -0400 Subject: hostname-util: ignore case when checking if hostname is localhost --- src/basic/hostname-util.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 95d9c3dd83..d901a5e82b 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -141,14 +141,14 @@ bool is_localhost(const char *hostname) { /* This tries to identify local host and domain names * described in RFC6761 plus the redhatism of .localdomain */ - return streq(hostname, "localhost") || - streq(hostname, "localhost.") || - streq(hostname, "localdomain.") || - streq(hostname, "localdomain") || - endswith(hostname, ".localhost") || - endswith(hostname, ".localhost.") || - endswith(hostname, ".localdomain") || - endswith(hostname, ".localdomain."); + return strcaseeq(hostname, "localhost") || + strcaseeq(hostname, "localhost.") || + strcaseeq(hostname, "localdomain.") || + strcaseeq(hostname, "localdomain") || + endswith_no_case(hostname, ".localhost") || + endswith_no_case(hostname, ".localhost.") || + endswith_no_case(hostname, ".localdomain") || + endswith_no_case(hostname, ".localdomain."); } int sethostname_idempotent(const char *s) { -- cgit v1.2.3-54-g00ecf