chiark / gitweb /
util: generalize is_localhost() and use it everywhere where applicable
[elogind.git] / src / shared / util.c
index af6bde2c3dd280e8c3f72f090d035d8d88b32e17..ceafa019a83d60c90472e83edeed1a1ae7c13ec7 100644 (file)
@@ -282,11 +282,11 @@ int parse_uid(const char *s, uid_t* ret_uid) {
 
         /* Some libc APIs use (uid_t) -1 as special placeholder */
         if (uid == (uid_t) 0xFFFFFFFF)
-                return -EINVAL;
+                return -ENXIO;
 
         /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
         if (uid == (uid_t) 0xFFFF)
-                return -EINVAL;
+                return -ENXIO;
 
         *ret_uid = uid;
         return 0;
@@ -3482,6 +3482,17 @@ int wait_for_terminate(pid_t pid, siginfo_t *status) {
         }
 }
 
+/*
+ * Return values:
+ * < 0 : wait_for_terminate() failed to get the state of the
+ *       process, the process was terminated by a signal, or
+ *       failed for an unknown reason.
+ * >=0 : The process terminated normally, and its exit code is
+ *       returned.
+ *
+ * That is, success is indicated by a return value of zero, and an
+ * error is indicated by a non-zero value.
+ */
 int wait_for_terminate_and_warn(const char *name, pid_t pid) {
         int r;
         siginfo_t status;
@@ -5215,8 +5226,8 @@ int get_home_dir(char **_h) {
         assert(_h);
 
         /* Take the user specified one */
-        e = getenv("HOME");
-        if (e) {
+        e = secure_getenv("HOME");
+        if (e && path_is_absolute(e)) {
                 h = strdup(e);
                 if (!h)
                         return -ENOMEM;
@@ -6721,3 +6732,18 @@ char *tempfn_random(const char *p) {
 
         return t;
 }
+
+/* make sure the hostname is not "localhost" */
+bool is_localhost(const char *hostname) {
+        assert(hostname);
+
+        /* This tries to identify local hostnames described in RFC6761
+         * plus the redhatism of .localdomain */
+
+        return streq(hostname, "localhost") ||
+               streq(hostname, "localhost.") ||
+               endswith(hostname, ".localhost") ||
+               endswith(hostname, ".localhost.") ||
+               endswith(hostname, ".localdomain") ||
+               endswith(hostname, ".localdomain.");
+}