diff options
author | lns <matzeton@googlemail.com> | 2018-04-26 15:31:31 +0200 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2018-04-26 15:31:31 +0200 |
commit | dc8d50bd8d382505ca7a34d501097ce7c181803b (patch) | |
tree | 718f995e2fc730e406fad2c616d4e4e8d41cc4f2 /src/utils.c | |
parent | d05358d2667d5b55a1bec36d051d95c06a3c7536 (diff) |
POTD skeleton #30.
Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index 996a8a7..ada3c13 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,6 +5,8 @@ #include <stdarg.h> #include <fcntl.h> #include <signal.h> +#include <pwd.h> +#include <grp.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> @@ -153,6 +155,8 @@ int redirect_devnull_to(int fds, ...) if (null_fd < 0) null_fd = open("/dev/null", O_RDWR); + if (null_fd < 0) + return -1; assert(null_fd >= 0); va_start(ap, fds); @@ -169,5 +173,27 @@ int redirect_devnull_to(int fds, ...) int change_user_group(const char *user, const char *group) { + struct passwd *pwd = NULL; + struct group *grp = NULL; + gid_t gid; + + pwd = getpwnam(user); + if (!pwd) + return 1; + + if (!group) { + gid = pwd->pw_gid; + } else { + grp = getgrnam(group); + if (!grp) + return 1; + gid = grp->gr_gid; + } + + if (setregid(gid, gid)) + return 1; + if (setreuid(pwd->pw_uid, pwd->pw_uid)) + return 1; + return 0; } |