summaryrefslogtreecommitdiff
path: root/toys/id.c
blob: 6ae3efe5ca92aea774652272509202e2e135d6a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* vi: set sw=4 ts=4:
 *
 * id.c - print real and effective user and group IDs
 *
 * Copyright 2012 Sony Network Entertainment, Inc.
 *
 * by Tim Bird <tim.bird@am.sony.com>
 *
 * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html

USE_ID(NEWTOY(id, "nGgru", TOYFLAG_BIN))

config ID
	bool "id"
	default y
	help
	  usage: id [-nGgru]

	  Print user and group ID.

	  -n	print names instead of numeric IDs (to be used with -Ggu)
	  -G	Show only the group IDs
	  -g    Show only the effective group ID
	  -r	Show real ID instead of effective ID
	  -u    Show only the effective user ID
*/

#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include "toys.h"

#define FLAG_n (1<<4)
#define FLAG_G (1<<3)
#define FLAG_g (1<<2)
#define FLAG_r (1<<1)
#define FLAG_u 1

void
pretty_print(struct passwd *pw, struct group *grp, struct group **grps, int n)
{
	int i;
	printf("uid= %d(%s) gid= %d(%s)", pw->pw_uid, pw->pw_name,
									  grp->gr_gid, grp->gr_name);
	if (n) {
		printf(" groups= ");
	}
	for (i = 0; i < n; i++) {
		printf("%d(%s)%s", grps[i]->gr_gid, grps[i]->gr_name,
						   (i < n-1) ? ",": "");
	}
	printf("\n");
}

void id_main(void)
{
	int flags = toys.optflags;

	struct passwd *pw;
	struct group *grp;
	struct group **grps;
	uid_t uid;
	gid_t gid;
	gid_t *groups;
	int i;
	int ngroups;
	char *username;

	/* check if a username is given */
	if (*toys.optargs) {
		username = *(toys.optargs);
		pw = getpwnam(username);
		if (!pw) {
			printf("id: %s: no such user\n", username);
			toys.exitval = 1;
			return;
		}
		uid = pw->pw_uid;
		gid = pw->pw_gid;
	} else {
		/* show effective, unless user specifies real */
		if (flags & FLAG_r) {
			uid = getuid();
			gid = getgid();
		} else {
			uid = geteuid();
			gid = getegid();
		}
	}

	pw = getpwuid(uid);
	if (!pw) {
		perror("id");
		toys.exitval = 1;
		return;
	}
	
	grp = getgrgid(pw->pw_gid);
	if (!grp) {
		perror("id");
		toys.exitval = 1;
		return;
	}
	
	if (flags & FLAG_u) {
		if (flags & FLAG_n)
		    printf("%s\n", pw->pw_name);
		else
		    printf("%d\n", pw->pw_uid);
		return;
	}
	if (flags & FLAG_g) {
		if (flags & FLAG_n)
			printf("%s\n", grp->gr_name);
		else
			printf("%d\n", grp->gr_gid);
		return;
	}
	

	if (flags & FLAG_r) {
		printf("-r can only be used in combination with -u or -g\n");
		toys.exitval = 1;
		return;
	}
	ngroups = sysconf(_SC_NGROUPS_MAX);
	/* fallback for number of groups to 32 */
	if (ngroups < 0)
		ngroups = 32;
	groups = malloc(ngroups * sizeof(gid_t));
	if (!groups) {
		perror("id");
		toys.exitval = 1;
		return;
	}
	if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) < 0) {
		perror("id");
		toys.exitval = 1;
		return;
	}
	grps = malloc(ngroups * sizeof(struct group *));
	for (i = 0; i < ngroups; i++) {
		struct group *tmp;
		grps[i] = malloc(sizeof(struct group));
		size_t f = sysconf(_SC_GETGR_R_SIZE_MAX);
		char *buf = malloc(f);
		if (getgrgid_r(groups[i], grps[i], buf, f, &tmp) < 0) {
			perror("id");
			continue;
		}
		if (tmp == NULL) {
			perror("id");
			continue;
		}
	}
	
	if (flags & FLAG_G) {
		for (i = 0; i < ngroups; i++) {
			if (flags & FLAG_n)
				printf("%s%s", !i ? "" : " ", grps[i]->gr_name);
			else
				printf("%s%d", !i ? "" : " ", grps[i]->gr_gid);
		}
		printf("\n");
		return;
	}

	pretty_print(pw, grp, grps, ngroups);
	for (i=0; i < ngroups; i++)
		free(grps[i]);

}