00001 #include <errno.h>
00002 #include <sys/types.h>
00003 #include <grp.h>
00004 #include <pwd.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008
00009 #include <iostream>
00010
00011 #include "setugid.h"
00012
00013 using namespace std;
00014
00015 void setugid(const char *uname, const char *gname)
00016 {
00017 struct passwd *pwd;
00018 struct group *grp;
00019
00020 if (gname && *gname) {
00021 if ((grp = getgrnam(gname)) == NULL) {
00022 cerr << "Cannot get grent for " << gname
00023 <<
00024 "\nPlease set the Groupname in newscache.conf\n";
00025 exit(1);
00026 }
00027 if (setregid(grp->gr_gid, grp->gr_gid) < 0) {
00028 cerr << "Cannot set effective groupid to " << grp->
00029 gr_gid << ": " << strerror(errno) << endl;
00030 exit(1);
00031 }
00032 }
00033 if (uname && *uname) {
00034 if ((pwd = getpwnam(uname)) == NULL) {
00035 cerr << "Cannot get pwent for " << uname
00036 <<
00037 "\nPlease set the Username in newscache.conf\n";
00038 exit(1);
00039 }
00040 if (setreuid(pwd->pw_uid, pwd->pw_uid) < 0) {
00041 cerr << "Cannot set effective userid to " << pwd->
00042 pw_uid << ": " << strerror(errno) << endl;
00043 exit(1);
00044 }
00045 }
00046 }