]> git.ipfire.org Git - people/ms/dma.git/blobdiff - util.c
local: use space instead of tab in "From " separator
[people/ms/dma.git] / util.c
diff --git a/util.c b/util.c
index 83002f4492ec265c1f370302dfa466ea3375f41d..d528f313d091769b17582f8d46704165c10dcaea 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,8 +1,9 @@
 /*
+ * 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);
@@ -66,20 +71,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 +98,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);
 }
@@ -107,7 +119,7 @@ local:
 void
 setlogident(const char *fmt, ...)
 {
-       char tag[50];
+       static char tag[50];
 
        snprintf(tag, sizeof(tag), "%s", logident_base);
        if (fmt != NULL) {
@@ -137,6 +149,7 @@ errlog(int exitcode, const char *fmt, ...)
                va_end(ap);
        }
 
+       errno = oerrno;
        if (*outs != 0) {
                syslog(LOG_ERR, "%s: %m", outs);
                fprintf(stderr, "%s: %s: %s\n", getprogname(), outs, strerror(oerrno));
@@ -190,21 +203,20 @@ void
 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