[systemd-commits] 2 commits - hostname-setup.c util.c util.h

Lennart Poettering lennart at kemper.freedesktop.org
Sun May 9 10:13:22 PDT 2010


 hostname-setup.c |   37 +++++++++++++++++++++++++++++++++++--
 util.c           |   25 +++++++++++++++++++++++++
 util.h           |    1 +
 3 files changed, 61 insertions(+), 2 deletions(-)

New commits:
commit a06b0b562bc11e5ca2ea88074fb3b38f2503ed6b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun May 9 19:12:06 2010 +0200

    hostname: drop invalid chars when reading hostname from disk

diff --git a/hostname-setup.c b/hostname-setup.c
index b8551d9..9f61474 100644
--- a/hostname-setup.c
+++ b/hostname-setup.c
@@ -44,6 +44,22 @@
 #define FILENAME "/etc/conf.d/hostname"
 #endif
 
+static char* strip_bad_chars(char *s) {
+        char *p, *d;
+
+        for (p = s, d = s; *p; p++)
+                if ((*p >= 'a' && *p <= 'z') ||
+                    (*p >= 'A' && *p <= 'Z') ||
+                    (*p >= '0' && *p <= '9') ||
+                    *p == '-' ||
+                    *p == '_')
+                        *(d++) = *p;
+
+        *d = 0;
+
+        return s;
+}
+
 static int read_hostname(char **hn) {
 
 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
@@ -77,8 +93,11 @@ static int read_hostname(char **hn) {
                         goto finish;
                 }
 
-                if (!(k = delete_chars(k, "\"\'"))) {
-                        r = -ENOMEM;
+                strip_bad_chars(k);
+
+                if (k[0] == 0) {
+                        free(k);
+                        r = -ENOENT;
                         goto finish;
                 }
 
@@ -107,6 +126,13 @@ finish:
         if (!k)
                 return -ENOMEM;
 
+        strip_bad_chars(k);
+
+        if (k[0] == 0) {
+                free(k);
+                return -NOENT;
+        }
+
         *hn = k;
 
 #else
commit 3177a7fa12247d30b854fcb7697cd578b9086bf5
Author: Marc-Antoine Perennou <Marc-Antoine at Perennou.com>
Date:   Sun May 9 18:13:02 2010 +0200

    hostname: read hostname for Gentoo

diff --git a/hostname-setup.c b/hostname-setup.c
index 4d7f32d..b8551d9 100644
--- a/hostname-setup.c
+++ b/hostname-setup.c
@@ -40,11 +40,13 @@
 #define FILENAME "/etc/hostname"
 #elif defined(TARGET_ARCH)
 #define FILENAME "/etc/rc.conf"
+#elif defined(TARGET_GENTOO)
+#define FILENAME "/etc/conf.d/hostname"
 #endif
 
 static int read_hostname(char **hn) {
 
-#if defined(TARGET_FEDORA) || defined(TARGET_ARCH)
+#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
         int r;
         FILE *f;
 
@@ -67,7 +69,7 @@ static int read_hostname(char **hn) {
 
                 s = strstrip(line);
 
-                if (!startswith(s, "HOSTNAME="))
+                if (!startswith_no_case(s, "HOSTNAME="))
                         continue;
 
                 if (!(k = strdup(s+9))) {
@@ -75,6 +77,11 @@ static int read_hostname(char **hn) {
                         goto finish;
                 }
 
+                if (!(k = delete_chars(k, "\"\'"))) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
                 *hn = k;
                 break;
         }
diff --git a/util.c b/util.c
index eed9aa7..8042214 100644
--- a/util.c
+++ b/util.c
@@ -42,6 +42,7 @@
 #include <sys/inotify.h>
 #include <sys/poll.h>
 #include <libgen.h>
+#include <ctype.h>
 
 #include "macro.h"
 #include "util.h"
@@ -141,6 +142,30 @@ bool startswith(const char *s, const char *prefix) {
         return memcmp(s, prefix, pl) == 0;
 }
 
+bool startswith_no_case(const char *s, const char *prefix) {
+        size_t sl, pl;
+        unsigned i;
+
+        assert(s);
+        assert(prefix);
+
+        sl = strlen(s);
+        pl = strlen(prefix);
+
+        if (pl == 0)
+                return true;
+
+        if (sl < pl)
+                return false;
+
+        for(i = 0; i < pl; ++i) {
+                if (tolower(s[i]) != tolower(prefix[i]))
+                        return false;
+        }
+
+        return true;
+}
+
 bool first_word(const char *s, const char *word) {
         size_t sl, wl;
 
diff --git a/util.h b/util.h
index ecf3b15..b9eb360 100644
--- a/util.h
+++ b/util.h
@@ -89,6 +89,7 @@ static inline bool is_path_absolute(const char *p) {
 
 bool endswith(const char *s, const char *postfix);
 bool startswith(const char *s, const char *prefix);
+bool startswith_no_case(const char *s, const char *prefix);
 
 bool first_word(const char *s, const char *word);
 


More information about the systemd-commits mailing list