diff options
| author | 2009-04-17 17:43:59 +0200 | |
|---|---|---|
| committer | 2009-04-17 17:43:59 +0200 | |
| commit | d54c5f4f0d87f76cf2a11fd88ec18cdbd67edaa2 (patch) | |
| tree | d66cdea31d3b020cfe2d859f8e21941095459973 /main.c | |
| parent | 8743112105e2f5d1539a266f9e174c9895c7c2c5 (diff) | |
| download | usbmuxd-d54c5f4f0d87f76cf2a11fd88ec18cdbd67edaa2.tar.gz usbmuxd-d54c5f4f0d87f76cf2a11fd88ec18cdbd67edaa2.tar.bz2 | |
Drop privileges after startup
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 25 |
1 files changed, 23 insertions, 2 deletions
| @@ -37,6 +37,7 @@ | |||
| 37 | #include <pthread.h> | 37 | #include <pthread.h> |
| 38 | #include <stdint.h> | 38 | #include <stdint.h> |
| 39 | #include <usb.h> | 39 | #include <usb.h> |
| 40 | #include <pwd.h> | ||
| 40 | 41 | ||
| 41 | #include "usbmuxd-proto.h" | 42 | #include "usbmuxd-proto.h" |
| 42 | #include "sock_stuff.h" | 43 | #include "sock_stuff.h" |
| @@ -1018,7 +1019,27 @@ int main(int argc, char **argv) | |||
| 1018 | lock.l_whence = SEEK_SET; | 1019 | lock.l_whence = SEEK_SET; |
| 1019 | lock.l_start = 0; | 1020 | lock.l_start = 0; |
| 1020 | lock.l_len = 0; | 1021 | lock.l_len = 0; |
| 1021 | fcntl(fileno(lfd), F_SETLK, &lock); | 1022 | if (fcntl(fileno(lfd), F_SETLK, &lock) == -1) { |
| 1023 | logmsg(LOG_ERR, "ERROR: lockfile locking failed!"); | ||
| 1024 | } | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | // drop elevated privileges | ||
| 1028 | if (getuid() == 0 || geteuid() == 0) { | ||
| 1029 | struct passwd *pw = getpwnam("nobody"); | ||
| 1030 | if (pw) { | ||
| 1031 | setuid(pw->pw_uid); | ||
| 1032 | } else { | ||
| 1033 | logmsg(LOG_ERR, "ERROR: Dropping privileges failed, check if user 'nobody' exists! Will now terminate."); | ||
| 1034 | exit(EXIT_FAILURE); | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | // security check | ||
| 1038 | if (setuid(0) != -1) { | ||
| 1039 | logmsg(LOG_ERR, "ERROR: Failed to drop privileges properly!"); | ||
| 1040 | exit(EXIT_FAILURE); | ||
| 1041 | } | ||
| 1042 | if (verbose >= 2) logmsg(LOG_NOTICE, "Successfully dropped privileges"); | ||
| 1022 | } | 1043 | } |
| 1023 | 1044 | ||
| 1024 | // Reserve space for 10 clients which should be enough. If not, the | 1045 | // Reserve space for 10 clients which should be enough. If not, the |
| @@ -1137,7 +1158,7 @@ int main(int argc, char **argv) | |||
| 1137 | // unlock lock file and close it. | 1158 | // unlock lock file and close it. |
| 1138 | if (lfd) { | 1159 | if (lfd) { |
| 1139 | lock.l_type = F_UNLCK; | 1160 | lock.l_type = F_UNLCK; |
| 1140 | fcntl(fileno(lfd), F_SETLK, lock); | 1161 | fcntl(fileno(lfd), F_SETLK, &lock); |
| 1141 | fclose(lfd); | 1162 | fclose(lfd); |
| 1142 | } | 1163 | } |
| 1143 | 1164 | ||
