]> code.delx.au - pulseaudio/blobdiff - src/pulse/util.c
util: Check that the home dir is an absolute path
[pulseaudio] / src / pulse / util.c
index ca766dab75dfc7966ba987d11fe411fda930083c..50f90b85bda03a01d7332f801ee4bbc945fbe298 100644 (file)
 #include <pwd.h>
 #endif
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
 #include <sys/prctl.h>
 #endif
 
+#ifdef OS_IS_DARWIN
+#include <libgen.h>
+#include <sys/sysctl.h>
+#endif
+
 #include <pulse/xmalloc.h>
 #include <pulse/timeval.h>
 
-#include <pulsecore/winsock.h>
-#include <pulsecore/core-error.h>
-#include <pulsecore/log.h>
+#include <pulsecore/socket.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/usergroup.h>
@@ -79,11 +78,15 @@ char *pa_get_user_name(char *s, size_t l) {
     pa_assert(s);
     pa_assert(l > 0);
 
-    if ((p = (getuid() == 0 ? "root" : NULL)) ||
-        (p = getenv("USER")) ||
-        (p = getenv("LOGNAME")) ||
-        (p = getenv("USERNAME")))
-    {
+    p = NULL;
+#ifdef HAVE_GETUID
+    p = getuid() == 0 ? "root" : NULL;
+#endif
+    if (!p) p = getenv("USER");
+    if (!p) p = getenv("LOGNAME");
+    if (!p) p = getenv("USERNAME");
+
+    if (p) {
         name = pa_strlcpy(s, p, l);
     } else {
 #ifdef HAVE_PWD_H
@@ -128,8 +131,8 @@ char *pa_get_host_name(char *s, size_t l) {
 }
 
 char *pa_get_home_dir(char *s, size_t l) {
-    char *e, *dir;
-
+    char *e;
+    char *dir;
 #ifdef HAVE_PWD_H
     struct passwd *r;
 #endif
@@ -137,11 +140,15 @@ char *pa_get_home_dir(char *s, size_t l) {
     pa_assert(s);
     pa_assert(l > 0);
 
-    if ((e = getenv("HOME")))
-        return pa_strlcpy(s, e, l);
+    if ((e = getenv("HOME"))) {
+        dir = pa_strlcpy(s, e, l);
+        goto finish;
+    }
 
-    if ((e = getenv("USERPROFILE")))
-        return pa_strlcpy(s, e, l);
+    if ((e = getenv("USERPROFILE"))) {
+        dir = pa_strlcpy(s, e, l);
+        goto finish;
+    }
 
 #ifdef HAVE_PWD_H
     errno = 0;
@@ -155,13 +162,21 @@ char *pa_get_home_dir(char *s, size_t l) {
     dir = pa_strlcpy(s, r->pw_dir, l);
 
     pa_getpwuid_free(r);
+#endif /* HAVE_PWD_H */
 
-    return dir;
-#else /* HAVE_PWD_H */
+finish:
+    if (!dir) {
+        errno = ENOENT;
+        return NULL;
+    }
 
-    errno = ENOENT;
-    return NULL;
-#endif
+    if (!pa_is_path_absolute(dir)) {
+        pa_log("Failed to get the home directory, not an absolute path: %s", dir);
+        errno = ENOENT;
+        return NULL;
+    }
+
+    return dir;
 }
 
 char *pa_get_binary_name(char *s, size_t l) {
@@ -195,11 +210,11 @@ char *pa_get_binary_name(char *s, size_t l) {
     {
         char *rp;
 
-       if ((rp = pa_readlink("/proc/curproc/file"))) {
-           pa_strlcpy(s, pa_path_get_filename(rp), l);
-           pa_xfree(rp);
-           return s;
-       }
+        if ((rp = pa_readlink("/proc/curproc/file"))) {
+            pa_strlcpy(s, pa_path_get_filename(rp), l);
+            pa_xfree(rp);
+            return s;
+        }
     }
 #endif
 
@@ -221,6 +236,27 @@ char *pa_get_binary_name(char *s, size_t l) {
     }
 #endif
 
+#ifdef OS_IS_DARWIN
+    {
+        int mib[] = { CTL_KERN, KERN_PROCARGS, getpid(), 0 };
+        size_t len, nmib = (sizeof(mib) / sizeof(mib[0])) - 1;
+        char *buf;
+
+        sysctl(mib, nmib, NULL, &len, NULL, 0);
+        buf = (char *) pa_xmalloc(len);
+
+        if (sysctl(mib, nmib, buf, &len, NULL, 0) == 0) {
+            pa_strlcpy(s, basename(buf), l);
+            pa_xfree(buf);
+            return s;
+        }
+
+        pa_xfree(buf);
+
+        /* fall thru */
+    }
+#endif /* OS_IS_DARWIN */
+
     errno = ENOENT;
     return NULL;
 }