some slight cleanup in setuid code
git-svn-id: https://svn.musicpd.org/mpd/trunk@2078 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
10
src/main.c
10
src/main.c
@@ -229,18 +229,14 @@ void establishListen(Options * options) {
|
|||||||
|
|
||||||
void changeToUser(Options * options) {
|
void changeToUser(Options * options) {
|
||||||
if (options->usr && strlen(options->usr)) {
|
if (options->usr && strlen(options->usr)) {
|
||||||
int uid, gid;
|
|
||||||
|
|
||||||
/* get uid */
|
/* get uid */
|
||||||
struct passwd * userpwd;
|
struct passwd * userpwd;
|
||||||
if ((userpwd = getpwnam(options->usr)) == NULL) {
|
if ((userpwd = getpwnam(options->usr)) == NULL) {
|
||||||
ERROR("no such user: %s\n", options->usr);
|
ERROR("no such user: %s\n", options->usr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
uid = userpwd->pw_uid;
|
|
||||||
gid = userpwd->pw_gid;
|
|
||||||
|
|
||||||
if(setgid(gid) == -1) {
|
if(setgid(userpwd->pw_gid) == -1) {
|
||||||
ERROR("cannot setgid of user %s: %s\n", options->usr,
|
ERROR("cannot setgid of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -250,7 +246,7 @@ void changeToUser(Options * options) {
|
|||||||
/* init suplementary groups
|
/* init suplementary groups
|
||||||
* (must be done before we change our uid)
|
* (must be done before we change our uid)
|
||||||
*/
|
*/
|
||||||
if (initgroups(options->usr, gid) == -1) {
|
if (initgroups(options->usr, userpwd->pw_gid) == -1) {
|
||||||
WARNING("cannot init suplementary groups "
|
WARNING("cannot init suplementary groups "
|
||||||
"of user %s: %s\n", options->usr,
|
"of user %s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
@@ -258,7 +254,7 @@ void changeToUser(Options * options) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* set uid */
|
/* set uid */
|
||||||
if (setuid(uid) == -1) {
|
if (setuid(userpwd->pw_uid) == -1) {
|
||||||
ERROR("cannot change to uid of user "
|
ERROR("cannot change to uid of user "
|
||||||
"%s: %s\n", options->usr,
|
"%s: %s\n", options->usr,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
Reference in New Issue
Block a user