/*
+ * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
* Copyright (c) 2008 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
- * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
+ * by Simon Schubert <2@0x2c.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
const char *
hostname(void)
{
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 255
+#endif
static char name[HOST_NAME_MAX+1];
static int initialized = 0;
+ char *s;
if (initialized)
return (name);
* 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;
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);
}
void
setlogident(const char *fmt, ...)
{
- char tag[50];
+ static char tag[50];
snprintf(tag, sizeof(tag), "%s", logident_base);
if (fmt != NULL) {
va_end(ap);
}
+ errno = oerrno;
if (*outs != 0) {
syslog(LOG_ERR, "%s: %m", outs);
fprintf(stderr, "%s: %s: %s\n", getprogname(), outs, strerror(oerrno));
set_username(void)
{
struct passwd *pwd;
- uid_t uid;
- uid = getuid();
- if (check_username(getlogin(), uid))
+ useruid = getuid();
+ if (check_username(getlogin(), useruid))
return;
- if (check_username(getenv("LOGNAME"), uid))
+ if (check_username(getenv("LOGNAME"), useruid))
return;
- if (check_username(getenv("USER"), uid))
+ if (check_username(getenv("USER"), useruid))
return;
- pwd = getpwuid(uid);
+ pwd = getpwuid(useruid);
if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0] != '\0') {
- if (check_username(pwd->pw_name, uid))
+ if (check_username(pwd->pw_name, useruid))
return;
}
- snprintf(username, sizeof(username), "uid=%ld", (long)uid);
+ snprintf(username, sizeof(username), "uid=%ld", (long)useruid);
}
void