X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=hostname-setup.c;h=71a3f75bd98fed18872dbb552fd1f0992e0613ff;hp=5f3ee77ac01a61d3ffc94745d5609f967f47ba46;hb=bab45044482dc012331c768c08d78a2d006485ad;hpb=206bf5c294d300cf05ee7b17c5bf42a21f6a52c0 diff --git a/hostname-setup.c b/hostname-setup.c index 5f3ee77ac..71a3f75bd 100644 --- a/hostname-setup.c +++ b/hostname-setup.c @@ -32,15 +32,43 @@ #define LINE_MAX 4096 +#if defined(TARGET_FEDORA) +#define FILENAME "/etc/sysconfig/network" +#elif defined(TARGET_SUSE) +#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) +#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) int r; FILE *f; assert(hn); - if (!(f = fopen("/etc/sysconfig/network", "re"))) + if (!(f = fopen(FILENAME, "re"))) return -errno; for (;;) { @@ -57,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))) { @@ -65,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; } @@ -75,13 +111,13 @@ finish: fclose(f); return r; -#elif defined(TARGET_SUSE) +#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) int r; char *s, *k; assert(hn); - if ((r = read_one_line_file("/etc/HOSTNAME", &s)) < 0) + if ((r = read_one_line_file(FILENAME, &s)) < 0) return r; k = strdup(strstrip(s)); @@ -90,24 +126,15 @@ finish: if (!k) return -ENOMEM; - *hn = k; - -#elif defined(TARGET_DEBIAN) - int r; - char *s, *k; - - assert(hn); - - if ((r = read_one_line_file("/etc/hostname", &s)) < 0) - return r; - - k = strdup(strstrip(s)); - free(s); + strip_bad_chars(k); - if (!k) - return -ENOMEM; + if (k[0] == 0) { + free(k); + return -ENOENT; + } *hn = k; + #else #warning "Don't know how to read the hostname"