morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base.git
Log | Files | Refs

id.c (2350B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/types.h>
      3 
      4 #include <ctype.h>
      5 #include <errno.h>
      6 #include <grp.h>
      7 #include <limits.h>
      8 #include <pwd.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <unistd.h>
     12 
     13 #include "util.h"
     14 
     15 static void groupid(struct passwd *pw);
     16 static void user(struct passwd *pw);
     17 static void userid(uid_t id);
     18 static void usernam(const char *nam);
     19 
     20 static void
     21 usage(void)
     22 {
     23 	eprintf("usage: %s [-g] [-u] [-G] [user | uid]\n", argv0);
     24 }
     25 
     26 static int Gflag = 0;
     27 
     28 int
     29 main(int argc, char *argv[])
     30 {
     31 	ARGBEGIN {
     32 	case 'g':
     33 		printf("%d\n", getegid());
     34 		return 0;
     35 	case 'u':
     36 		printf("%d\n", geteuid());
     37 		return 0;
     38 	case 'G':
     39 		Gflag = 1;
     40 		break;
     41 	default:
     42 		usage();
     43 	} ARGEND;
     44 
     45 	switch (argc) {
     46 	case 0:
     47 		userid(getuid());
     48 		break;
     49 	case 1:
     50 		/* user names can't begin [0-9] */
     51 		if (isdigit(argv[0][0]))
     52 			userid(estrtol(argv[0], 0));
     53 		else
     54 			usernam(argv[0]);
     55 		break;
     56 	default:
     57 		usage();
     58 	}
     59 
     60 	return 0;
     61 }
     62 
     63 static void
     64 groupid(struct passwd *pw)
     65 {
     66 	gid_t gid, groups[NGROUPS_MAX];
     67 	int ngroups;
     68 	int i;
     69 
     70 	ngroups = NGROUPS_MAX;
     71 	getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
     72 	for (i = 0; i < ngroups; i++) {
     73 		gid = groups[i];
     74 		printf("%u", gid);
     75 		if (i < ngroups - 1)
     76 			putchar(' ');
     77 	}
     78 	putchar('\n');
     79 }
     80 
     81 static void
     82 usernam(const char *nam)
     83 {
     84 	struct passwd *pw;
     85 
     86 	errno = 0;
     87 	pw = getpwnam(nam);
     88 	if (!pw) {
     89 		if (errno)
     90 			eprintf("getpwnam %s:", nam);
     91 		else
     92 			eprintf("getpwnam %s: no such user\n", nam);
     93 	}
     94 	if (Gflag)
     95 		groupid(pw);
     96 	else
     97 		user(pw);
     98 }
     99 
    100 static void
    101 userid(uid_t id)
    102 {
    103 	struct passwd *pw;
    104 
    105 	errno = 0;
    106 	pw = getpwuid(id);
    107 	if (!pw) {
    108 		if (errno)
    109 			eprintf("getpwuid %d:", id);
    110 		else
    111 			eprintf("getpwuid %d: no such user\n", id);
    112 	}
    113 	if (Gflag)
    114 		groupid(pw);
    115 	else
    116 		user(pw);
    117 }
    118 
    119 static void
    120 user(struct passwd *pw)
    121 {
    122 	struct group *gr;
    123 	gid_t gid, groups[NGROUPS_MAX];
    124 	int ngroups;
    125 	int i;
    126 
    127 	printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
    128 	printf(" gid=%u", pw->pw_gid);
    129 	if (!(gr = getgrgid(pw->pw_gid)))
    130 		eprintf("getgrgid:");
    131 	printf("(%s)", gr->gr_name);
    132 
    133 	ngroups = NGROUPS_MAX;
    134 	getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    135 	for (i = 0; i < ngroups; i++) {
    136 		gid = groups[i];
    137 		printf("%s%u", !i ? " groups=" : ",", gid);
    138 		if (!(gr = getgrgid(gid)))
    139 			eprintf("getgrgid:");
    140 		printf("(%s)", gr->gr_name);
    141 	}
    142 	putchar('\n');
    143 }