]> 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 44fad4aaf8b06b02f493f0af8d529a55427d47e0..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 <pulsecore/winsock.h>
-#include <pulsecore/core-error.h>
-#include <pulsecore/log.h>
+#include <pulse/timeval.h>
+
+#include <pulsecore/socket.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/usergroup.h>
 
 #include "util.h"
 
 char *pa_get_user_name(char *s, size_t l) {
     const char *p;
+    char *name = NULL;
+#ifdef OS_IS_WIN32
     char buf[1024];
+#endif
 
 #ifdef HAVE_PWD_H
-    struct passwd pw, *r;
+    struct passwd *r;
 #endif
 
     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
 
-#ifdef HAVE_GETPWUID_R
-        if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
-#else
-        /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
-            * that do not support getpwuid_r. */
-        if ((r = getpwuid(getuid())) == NULL) {
-#endif
+        if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
             pa_snprintf(s, l, "%lu", (unsigned long) getuid());
             return s;
         }
 
-        p = r->pw_name;
+        name = pa_strlcpy(s, r->pw_name, l);
+        pa_getpwuid_free(r);
 
 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
         DWORD size = sizeof(buf);
@@ -100,7 +107,7 @@ char *pa_get_user_name(char *s, size_t l) {
             return NULL;
         }
 
-        p = buf;
+        name = pa_strlcpy(s, buf, l);
 
 #else /* HAVE_PWD_H */
 
@@ -108,7 +115,7 @@ char *pa_get_user_name(char *s, size_t l) {
 #endif /* HAVE_PWD_H */
     }
 
-    return pa_strlcpy(s, p, l);
+    return name;
 }
 
 char *pa_get_host_name(char *s, size_t l) {
@@ -125,43 +132,51 @@ char *pa_get_host_name(char *s, size_t l) {
 
 char *pa_get_home_dir(char *s, size_t l) {
     char *e;
-
+    char *dir;
 #ifdef HAVE_PWD_H
-    char buf[1024];
-    struct passwd pw, *r;
+    struct passwd *r;
 #endif
 
     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;
-#ifdef HAVE_GETPWUID_R
-    if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
-#else
-    /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
-        * that do not support getpwuid_r. */
-    if ((r = getpwuid(getuid())) == NULL) {
-#endif
+    if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
         if (!errno)
             errno = ENOENT;
 
         return NULL;
     }
 
-    return pa_strlcpy(s, r->pw_dir, l);
-#else /* HAVE_PWD_H */
+    dir = pa_strlcpy(s, r->pw_dir, l);
 
-    errno = ENOENT;
-    return NULL;
-#endif
+    pa_getpwuid_free(r);
+#endif /* HAVE_PWD_H */
+
+finish:
+    if (!dir) {
+        errno = ENOENT;
+        return NULL;
+    }
+
+    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) {
@@ -189,7 +204,18 @@ char *pa_get_binary_name(char *s, size_t l) {
             return s;
         }
     }
+#endif
 
+#ifdef __FreeBSD__
+    {
+        char *rp;
+
+        if ((rp = pa_readlink("/proc/curproc/file"))) {
+            pa_strlcpy(s, pa_path_get_filename(rp), l);
+            pa_xfree(rp);
+            return s;
+        }
+    }
 #endif
 
 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
@@ -210,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;
 }
@@ -217,7 +264,8 @@ char *pa_get_binary_name(char *s, size_t l) {
 char *pa_path_get_filename(const char *p) {
     char *fn;
 
-    pa_assert(p);
+    if (!p)
+        return NULL;
 
     if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
         return fn+1;
@@ -260,8 +308,8 @@ int pa_msleep(unsigned long t) {
 #elif defined(HAVE_NANOSLEEP)
     struct timespec ts;
 
-    ts.tv_sec = t/1000UL;
-    ts.tv_nsec = (t % 1000UL) * 1000000UL;
+    ts.tv_sec = (time_t) (t / PA_MSEC_PER_SEC);
+    ts.tv_nsec = (long) ((t % PA_MSEC_PER_SEC) * PA_NSEC_PER_MSEC);
 
     return nanosleep(&ts, NULL);
 #else