From: Simon Schubert <2@0x2c.org> Date: Fri, 29 Oct 2010 01:19:25 +0000 (+0200) Subject: sanitize hostname a bit more X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c3fb272bd3d3ac42f45a277bac17848312082b8b;p=people%2Fms%2Fdma.git sanitize hostname a bit more --- diff --git a/util.c b/util.c index 83002f4..46619d7 100644 --- a/util.c +++ b/util.c @@ -53,6 +53,7 @@ hostname(void) { static char name[HOST_NAME_MAX+1]; static int initialized = 0; + char *s; if (initialized) return (name); @@ -66,20 +67,19 @@ hostname(void) * treat it as a file. */ FILE *fp; - char *res; fp = fopen(config.mailname, "r"); if (fp == NULL) goto local; - res = fgets(name, sizeof(name), fp); + s = fgets(name, sizeof(name), fp); fclose(fp); - if (res == NULL) + if (s == NULL) goto local; - while (*res != 0 && !isspace(*res)) - ++res; - *res = 0; + for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s) + /* NOTHING */; + *s = 0; if (!*name) goto local; @@ -94,12 +94,20 @@ hostname(void) local: if (gethostname(name, sizeof(name)) != 0) - strcpy(name, "(unknown hostname)"); + *name = 0; /* * gethostname() is allowed to truncate name without NUL-termination * and at the same time not return an error. */ name[sizeof(name) - 1] = 0; + + for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s) + /* NOTHING */; + *s = 0; + + if (!*name) + snprintf(name, sizeof(name), "unknown-hostname"); + initialized = 1; return (name); }