[systemd-devel] [PATCH] network: allow domain names up to 255 characters

Nick Owens mischief at offblast.org
Fri Apr 10 13:03:19 PDT 2015


From: mischief <mischief at offblast.org>

The maximum domain name size is larger than the maximum host name size.
The smaller limit causes valid domains provided by DHCP or .network
files to be silently ignored.
---
 src/libsystemd-network/sd-dhcp-lease.c |  2 +-
 src/network/networkd-network.c         |  2 +-
 src/shared/util.c                      | 21 ++++++++++++++-------
 src/shared/util.h                      |  4 ++++
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 34aa36c..5ec0c20 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -511,7 +511,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
                 if (e)
                         *e = 0;
 
-                if (!hostname_is_valid(domainname) || is_localhost(domainname))
+                if (!domainname_is_valid(domainname) || is_localhost(domainname))
                         break;
 
                 free(lease->domainname);
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 0d1ec0b..3fe1600 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -456,7 +456,7 @@ int config_parse_domains(const char *unit,
         STRV_FOREACH(domain, *domains) {
                 if (is_localhost(*domain))
                         log_syntax(unit, LOG_ERR, filename, line, EINVAL, "'localhost' domain names may not be configured, ignoring assignment: %s", *domain);
-                else if (!hostname_is_valid(*domain)) {
+                else if (!domainname_is_valid(*domain)) {
                         if (!streq(*domain, "*"))
                                 log_syntax(unit, LOG_ERR, filename, line, EINVAL, "domain name is not valid, ignoring assignment: %s", *domain);
                 } else
diff --git a/src/shared/util.c b/src/shared/util.c
index ba035ca..705112b1 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4245,18 +4245,16 @@ static bool hostname_valid_char(char c) {
                 c == '.';
 }
 
-bool hostname_is_valid(const char *s) {
+/* Doesn't accept empty hostnames, hostnames with trailing or
+ * leading dots, and hostnames with multiple dots in a
+ * sequence. Also ensures that the length stays below n */
+static bool hostname_is_valid_len(const char *s, size_t n) {
         const char *p;
         bool dot;
 
         if (isempty(s))
                 return false;
 
-        /* Doesn't accept empty hostnames, hostnames with trailing or
-         * leading dots, and hostnames with multiple dots in a
-         * sequence. Also ensures that the length stays below
-         * HOST_NAME_MAX. */
-
         for (p = s, dot = true; *p; p++) {
                 if (*p == '.') {
                         if (dot)
@@ -4274,10 +4272,19 @@ bool hostname_is_valid(const char *s) {
         if (dot)
                 return false;
 
-        if (p-s > HOST_NAME_MAX)
+        if (p-s > n)
                 return false;
 
         return true;
+
+}
+
+bool hostname_is_valid(const char *s) {
+	return hostname_is_valid_len(s, HOST_NAME_MAX);
+}
+
+bool domainname_is_valid(const char *s) {
+	return hostname_is_valid_len(s, DOMAIN_NAME_MAX);
 }
 
 char* hostname_cleanup(char *s, bool lowercase) {
diff --git a/src/shared/util.h b/src/shared/util.h
index a83b588..1c8691d 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -89,6 +89,9 @@
 #include "missing.h"
 #include "time-util.h"
 
+/* Maximum length of a domain name. See RFC 1035 */
+#define DOMAIN_NAME_MAX 255
+
 /* What is interpreted as whitespace? */
 #define WHITESPACE " \t\n\r"
 #define NEWLINE    "\n\r"
@@ -553,6 +556,7 @@ bool nulstr_contains(const char*nulstr, const char *needle);
 bool plymouth_running(void);
 
 bool hostname_is_valid(const char *s) _pure_;
+bool domainname_is_valid(const char *s) _pure_;
 char* hostname_cleanup(char *s, bool lowercase);
 
 bool machine_name_is_valid(const char *s) _pure_;
-- 
2.0.5



More information about the systemd-devel mailing list