[systemd-devel] [PATCH] allow apostrophes in the pretty hostname

Nathaniel Chen nathaniel.chen at intel.com
Mon Mar 4 13:34:18 PST 2013


this addresses the bug at:
  https://bugs.freedesktop.org/show_bug.cgi?id=59311

hostnamectl is supposed to allow a range of special characters for
the 'pretty' hostname:
  $ hostnamectl set-hostname --pretty "Nathaniels Desktop !@#$%"
..however, it rejects apostrophes.
The manual for hostname suggests that this should be allowed.

It makes sense to reject \0, \n, \\, etc...but since the function
string_is_safe() is used in multiple places,
pretty_string_is_safe() is the same, but without the apostrophe check.
---
 src/hostname/hostnamed.c |  2 +-
 src/shared/util.c        | 15 +++++++++++++++
 src/shared/util.h        |  1 +
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 7ea891c..ed4ceea 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -553,7 +553,7 @@ static DBusHandlerResult hostname_message_handler(
                                  * safe than sorry */
                                 if (k == PROP_ICON_NAME && !filename_is_safe(name))
                                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
-                                if (k == PROP_PRETTY_HOSTNAME && !string_is_safe(name))
+                                if (k == PROP_PRETTY_HOSTNAME && !pretty_string_is_safe(name))
                                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
                                 if (k == PROP_CHASSIS && !valid_chassis(name))
                                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
diff --git a/src/shared/util.c b/src/shared/util.c
index e643cd3..b4e2e60 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5288,6 +5288,21 @@ bool string_is_safe(const char *p) {
         return true;
 }
 
+bool pretty_string_is_safe(const char *p) {
+	const char *t;
+
+	assert(p);
+
+	for (t = p; *t; t++) {
+		if (*t > 0 && *t < ' ')
+			return false;
+		if (strchr("\\\"", *t))
+			return false;
+	}
+
+	return true;
+}
+
 bool string_has_cc(const char *p) {
         const char *t;
 
diff --git a/src/shared/util.h b/src/shared/util.h
index 27b21f9..24b4a23 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -543,6 +543,7 @@ _malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b)
 bool filename_is_safe(const char *p);
 bool path_is_safe(const char *p);
 bool string_is_safe(const char *p);
+bool pretty_string_is_safe(const char *p);
 bool string_has_cc(const char *p);
 
 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
-- 
1.8.1.4



More information about the systemd-devel mailing list