void xsetuid(uid_t uid);
struct passwd *xgetpwuid(uid_t uid);
struct group *xgetgrgid(gid_t gid);
+struct passwd *xgetpwnam(char *name);
char *xreadlink(char *name);
long xparsetime(char *arg, long units, long *fraction);
void xpidfile(char *name);
struct passwd *xgetpwuid(uid_t uid)
{
struct passwd *pwd = getpwuid(uid);
- if (!pwd) error_exit(NULL);
+ if (!pwd) error_exit("bad uid %ld", (long)uid);
return pwd;
}
struct group *xgetgrgid(gid_t gid)
{
struct group *group = getgrgid(gid);
- if (!group) error_exit(NULL);
+ if (!group) error_exit("bad gid %ld", (long)gid);
return group;
}
+struct passwd *xgetpwnam(char *name)
+{
+ struct passwd *up = getpwnam(name);
+ if (!up) error_exit("bad user '%s'", name);
+ return up;
+}
+
// This can return null (meaning file not found). It just won't return null
// for memory allocation reasons.
char *xreadlink(char *name)
int ret = -1;
myuid = getuid();
- if ((myuid) && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d)))
- error_exit("You need to be root to do these actions\n");
+ if (myuid && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d)))
+ error_exit("Not root");
- pw = getpwuid(myuid);
- if (!pw) error_exit("Unknown uid '%u'",myuid);
+ pw = xgetpwuid(myuid);
- if (toys.optargs[0]) name = toys.optargs[0];
+ if (*toys.optargs) name = toys.optargs[0];
else name = xstrdup(pw->pw_name);
- pw = getpwnam(name);
- if (!pw) error_exit("Unknown user '%s'",name);
+ pw = xgetpwnam(name);
if (myuid && (myuid != pw->pw_uid))
error_exit("You need to be root to change '%s' password\n", name);
if (toys.optc == 2) { //add user to group
//toys.optargs[0]- user, toys.optargs[1] - group
- if (!getpwnam(toys.optargs[0]))
- error_exit("user '%s' does not exist", toys.optargs[0]);
+ xgetpwnam(*toys.optargs);
if (!(grp = getgrnam(toys.optargs[1])))
error_exit("group '%s' does not exist", toys.optargs[1]);
if (!grp->gr_mem) entry = xmsprintf("%s", *toys.optargs);
error_exit(NULL);
}
- if (toys.optflags & FLAG_u) {
- struct passwd *pw = getpwnam(TT.user);
- if (!pw) error_exit("unknown user %s", TT.user);
- TT.pw = pw;
- }
+ if (TT.user) TT.pw = xgetpwnam(TT.user);
if (*toys.optargs) {
if (chroot(*toys.optargs))
perror_exit("can't change root directory to '%s'", *toys.optargs);
// check if a username is given
if (username) {
- if (!(pw = getpwnam(username)))
- error_exit("no such user '%s'", username);
+ pw = xgetpwnam(username);
uid = euid = pw->pw_uid;
gid = egid = pw->pw_gid;
- if (cmd_groups)
- printf("%s : ", pw->pw_name);
+ if (cmd_groups) printf("%s : ", pw->pw_name);
}
i = flags & FLAG_r;