diff options
author | Nikias Bassen | 2013-12-10 19:13:49 +0100 |
---|---|---|
committer | Nikias Bassen | 2013-12-10 19:13:49 +0100 |
commit | ed73eddb895909f8c80fedab0d8753af15e6f394 (patch) | |
tree | f96d8a5dfbd22cb775b10aef5699c4682f8d0444 /src/main.c | |
parent | a648e0b133a4e372544c1ddfe2e45084e2b2c50a (diff) | |
download | usbmuxd-ed73eddb895909f8c80fedab0d8753af15e6f394.tar.gz usbmuxd-ed73eddb895909f8c80fedab0d8753af15e6f394.tar.bz2 |
main: make sure the non-privileged user has proper access to the config dir
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 30 |
1 files changed, 21 insertions, 9 deletions
@@ -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 |