2014-02-27 14:27:28 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/types.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2014-02-27 14:27:28 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
2014-02-27 14:27:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-02-27 14:27:28 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void groupid(struct passwd *pw);
|
|
|
|
static void user(struct passwd *pw);
|
|
|
|
static void userid(uid_t id);
|
|
|
|
static void usernam(const char *nam);
|
|
|
|
|
|
|
|
static int Gflag = 0;
|
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
static void
|
|
|
|
groupid(struct passwd *pw)
|
2014-02-27 14:27:28 +00:00
|
|
|
{
|
2015-02-06 14:19:33 +00:00
|
|
|
gid_t gid, groups[NGROUPS_MAX];
|
|
|
|
int ngroups;
|
|
|
|
int i;
|
2014-02-27 14:27:28 +00:00
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
ngroups = NGROUPS_MAX;
|
|
|
|
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
|
|
|
|
for (i = 0; i < ngroups; i++) {
|
|
|
|
gid = groups[i];
|
|
|
|
printf("%u", gid);
|
|
|
|
if (i < ngroups - 1)
|
|
|
|
putchar(' ');
|
2014-02-27 14:27:28 +00:00
|
|
|
}
|
2015-02-06 14:19:33 +00:00
|
|
|
putchar('\n');
|
2014-02-27 14:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-06 14:19:33 +00:00
|
|
|
user(struct passwd *pw)
|
2014-02-27 14:27:28 +00:00
|
|
|
{
|
2015-02-06 14:19:33 +00:00
|
|
|
struct group *gr;
|
2014-02-27 14:27:28 +00:00
|
|
|
gid_t gid, groups[NGROUPS_MAX];
|
|
|
|
int ngroups;
|
|
|
|
int i;
|
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
|
|
|
|
printf(" gid=%u", pw->pw_gid);
|
|
|
|
if (!(gr = getgrgid(pw->pw_gid)))
|
|
|
|
eprintf("getgrgid:");
|
|
|
|
printf("(%s)", gr->gr_name);
|
|
|
|
|
2014-02-27 14:27:28 +00:00
|
|
|
ngroups = NGROUPS_MAX;
|
|
|
|
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
|
|
|
|
for (i = 0; i < ngroups; i++) {
|
|
|
|
gid = groups[i];
|
2015-02-06 14:19:33 +00:00
|
|
|
printf("%s%u", !i ? " groups=" : ",", gid);
|
|
|
|
if (!(gr = getgrgid(gid)))
|
|
|
|
eprintf("getgrgid:");
|
|
|
|
printf("(%s)", gr->gr_name);
|
2014-02-27 14:27:28 +00:00
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usernam(const char *nam)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwnam(nam);
|
2014-07-09 15:39:32 +00:00
|
|
|
if (!pw) {
|
2014-07-09 14:56:02 +00:00
|
|
|
if (errno)
|
|
|
|
eprintf("getpwnam %s:", nam);
|
|
|
|
else
|
|
|
|
eprintf("getpwnam %s: no such user\n", nam);
|
|
|
|
}
|
2014-02-27 14:27:28 +00:00
|
|
|
if (Gflag)
|
|
|
|
groupid(pw);
|
|
|
|
else
|
|
|
|
user(pw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
userid(uid_t id)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwuid(id);
|
2014-07-09 15:39:32 +00:00
|
|
|
if (!pw) {
|
2014-07-09 14:56:02 +00:00
|
|
|
if (errno)
|
|
|
|
eprintf("getpwuid %d:", id);
|
|
|
|
else
|
|
|
|
eprintf("getpwuid %d: no such user\n", id);
|
|
|
|
}
|
2014-02-27 14:27:28 +00:00
|
|
|
if (Gflag)
|
|
|
|
groupid(pw);
|
|
|
|
else
|
|
|
|
user(pw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-06 14:19:33 +00:00
|
|
|
usage(void)
|
2014-02-27 14:27:28 +00:00
|
|
|
{
|
2015-02-06 14:19:33 +00:00
|
|
|
eprintf("usage: %s [-g] [-u] [-G] [user | uid]\n", argv0);
|
|
|
|
}
|
2014-02-27 14:27:28 +00:00
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'g':
|
|
|
|
printf("%d\n", getegid());
|
|
|
|
return 0;
|
|
|
|
case 'u':
|
|
|
|
printf("%d\n", geteuid());
|
|
|
|
return 0;
|
|
|
|
case 'G':
|
|
|
|
Gflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2014-02-27 14:27:28 +00:00
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
userid(getuid());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* user names can't begin [0-9] */
|
|
|
|
if (isdigit(argv[0][0]))
|
|
|
|
userid(estrtol(argv[0], 0));
|
|
|
|
else
|
|
|
|
usernam(argv[0]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2014-02-27 14:27:28 +00:00
|
|
|
}
|
2015-02-06 14:19:33 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-02-27 14:27:28 +00:00
|
|
|
}
|