diff options
author | Lennart Poettering <lennart@poettering.net> | 2010-05-09 19:12:06 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2010-05-09 19:12:39 +0200 |
commit | a06b0b562bc11e5ca2ea88074fb3b38f2503ed6b (patch) | |
tree | 18a2746e0d25fe6e4b94a213609b22a4db602ead | |
parent | 3177a7fa12247d30b854fcb7697cd578b9086bf5 (diff) |
hostname: drop invalid chars when reading hostname from disk
-rw-r--r-- | hostname-setup.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/hostname-setup.c b/hostname-setup.c index b8551d9802..9f61474625 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 |