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=eca33a98533b35af7c15c015756954f9b93626c6;hb=bab45044482dc012331c768c08d78a2d006485ad;hpb=302e8c4c4c7c776531d33fddae9cc0cac90846c3 diff --git a/hostname-setup.c b/hostname-setup.c index eca33a985..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_DEBIAN) +#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,9 +126,17 @@ finish: if (!k) return -ENOMEM; + strip_bad_chars(k); + + if (k[0] == 0) { + free(k); + return -ENOENT; + } + *hn = k; + #else -#warn "Don't know how to read the hostname" +#warning "Don't know how to read the hostname" return -ENOENT; #endif