From ed73eddb895909f8c80fedab0d8753af15e6f394 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 10 Dec 2013 19:13:49 +0100 Subject: main: make sure the non-privileged user has proper access to the config dir --- src/main.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 1a04aff..15cb5a1 100644 --- a/src/main.c +++ b/src/main.c @@ -535,12 +535,26 @@ int main(int argc, char *argv[]) #ifdef HAVE_LIBIMOBILEDEVICE const char* userprefdir = userpref_get_config_dir(); - struct stat fst; - int userprefdir_created = 0; + memset(&fst, '\0', sizeof(struct stat)); if (stat(userprefdir, &fst) < 0) { - mkdir(userprefdir, 0775); - userprefdir_created = 1; + if (mkdir(userprefdir, 0775) < 0) { + usbmuxd_log(LL_FATAL, "Failed to create required directory '%s': %s\n", userprefdir, strerror(errno)); + res = -1; + goto terminate; + } + if (stat(userprefdir, &fst) < 0) { + usbmuxd_log(LL_FATAL, "stat() failed after creating directory '%s': %s\n", userprefdir, strerror(errno)); + res = -1; + goto terminate; + } + } + + // make sure permission bits are set correctly + if (fst.st_mode != 02775) { + if (chmod(userprefdir, 02775) < 0) { + usbmuxd_log(LL_WARNING, "chmod(%s, 02775) failed: %s", userprefdir, strerror(errno)); + } } #endif @@ -562,12 +576,10 @@ int main(int argc, char *argv[]) usbmuxd_log(LL_INFO, "Not dropping privileges to root"); } else { #ifdef HAVE_LIBIMOBILEDEVICE - if (userprefdir_created) { + /* make sure the non-privileged user has proper access to the config directory */ + if ((fst.st_uid != pw->pw_uid) || (fst.st_gid != pw->pw_gid)) { if (chown(userprefdir, pw->pw_uid, pw->pw_gid) < 0) { - usbmuxd_log(LL_WARNING, "chown(%s, %d, %d) failed", userprefdir, pw->pw_uid, pw->pw_gid); - } - if (chmod(userprefdir, 02775) < 0) { - usbmuxd_log(LL_WARNING, "chmod %s failed", userprefdir); + usbmuxd_log(LL_WARNING, "chown(%s, %d, %d) failed: %s", userprefdir, pw->pw_uid, pw->pw_gid, strerror(errno)); } } #endif -- cgit v1.1-32-gdbae