X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=hostname-setup.c;h=3b988d4c8b68dfdad2c25c545d736e0adb4b470a;hb=136337ff74f05be3d42a769d9f0cb99716c5c40f;hp=4d7f32d0a79b562432d46619e2df06b6d6ba3ba0;hpb=ea6145dabe707ae721d036c6764c34a493cff401;p=elogind.git diff --git a/hostname-setup.c b/hostname-setup.c index 4d7f32d0a..3b988d4c8 100644 --- a/hostname-setup.c +++ b/hostname-setup.c @@ -34,17 +34,35 @@ #if defined(TARGET_FEDORA) #define FILENAME "/etc/sysconfig/network" -#elif defined(TARGET_SUSE) +#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) #define FILENAME "/etc/HOSTNAME" #elif defined(TARGET_DEBIAN) #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 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) +#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) int r; FILE *f; @@ -67,7 +85,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 +93,14 @@ static int read_hostname(char **hn) { goto finish; } + strip_bad_chars(k); + + if (k[0] == 0) { + free(k); + r = -ENOENT; + goto finish; + } + *hn = k; break; } @@ -85,7 +111,7 @@ finish: fclose(f); return r; -#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) +#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE) int r; char *s, *k; @@ -100,6 +126,13 @@ finish: if (!k) return -ENOMEM; + strip_bad_chars(k); + + if (k[0] == 0) { + free(k); + return -ENOENT; + } + *hn = k; #else