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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* UNG's Not GNU
*
* Copyright (c) 2011-2019, Jakob Kaivo <jkk@ung.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#include <grp.h>
#include <pwd.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
enum type { USER, GROUP };
enum mode { DEFAULT, UID, GID, ALL_GID };
enum display { FULL, NAMES, NUMBERS };
static char *get_name(enum type type, id_t id)
{
if (type == GROUP) {
struct group *grp = getgrgid(id);
if (grp) {
return grp->gr_name;
}
} else {
struct passwd *pwd = getpwuid(id);
if (pwd) {
return pwd->pw_name;
}
}
return NULL;
}
static void print_id(const char *prefix, const char *name, id_t id, enum display mode)
{
printf("%s", prefix);
if (mode == FULL || mode == NUMBERS || name == NULL) {
printf("%ju", (uintmax_t)id);
}
if (name != NULL) {
if (mode == FULL) {
printf("(%s)", name);
} else if (mode == NAMES) {
printf("%s", name);
}
}
}
static void print_groups(uid_t uid, gid_t rgid, gid_t egid, enum display mode)
{
char *name = get_name(USER, uid);
if (name == NULL) {
return;
}
char *prefix = "";
if (mode == FULL) {
prefix = " groups=";
}
print_id(prefix, get_name(GROUP, rgid), rgid, mode);
if (mode == FULL) {
prefix = ",";
} else {
prefix = " ";
}
if (rgid != egid) {
print_id(prefix, get_name(GROUP, egid), egid, mode);
}
struct group *grp = NULL;
setgrent();
while ((grp = getgrent()) != NULL) {
if (grp->gr_gid == rgid || grp->gr_gid == egid) {
continue;
}
for (int i = 0; grp->gr_mem[i] != NULL; i++) {
if (!strcmp(name, grp->gr_mem[i])) {
print_id(prefix, grp->gr_name, grp->gr_gid, mode);
}
}
}
endgrent();
}
int main(int argc, char *argv[])
{
enum display dmode = NUMBERS;
enum mode mode = DEFAULT;
uid_t ruid = getuid();
uid_t euid = geteuid();
gid_t rgid = getgid();
gid_t egid = getegid();
uid_t uid = euid;
gid_t gid = egid;
setlocale(LC_ALL, "");
int c;
while ((c = getopt(argc, argv, "Ggunr")) != -1) {
switch (c) {
case 'G':
mode = ALL_GID;
break;
case 'g':
mode = GID;
break;
case 'u':
mode = UID;
break;
case 'n':
dmode = NAMES;
break;
case 'r':
uid = ruid;
gid = rgid;
break;
default:
return 1;
}
}
/* TODO: validate arguments together */
if (optind < argc - 1) {
fprintf(stderr, "id: too many operands\n");
return 1;
}
if (optind == argc - 1) {
struct passwd *pwd = getpwnam(argv[optind]);
if (pwd == NULL) {
fprintf(stderr, "id: user '%s' not found\n",
argv[optind]);
return 1;
}
uid = ruid = euid = pwd->pw_uid;
gid = rgid = egid = pwd->pw_gid;
}
if (mode == ALL_GID) {
print_groups(uid, rgid, egid, dmode);
} else if (mode == GID) {
print_id("", get_name(GROUP, gid), gid, dmode);
} else if (mode == UID) {
print_id("", get_name(USER, uid), uid, dmode);
} else {
print_id("uid=", get_name(USER, ruid), ruid, 0);
print_id(" gid=", get_name(GROUP, rgid), rgid, 0);
if (ruid != euid) {
print_id(" euid=", get_name(USER, euid), euid, 0);
}
if (rgid != egid) {
print_id(" egid=", get_name(GROUP, egid), egid, 0);
}
print_groups(ruid, rgid, egid, FULL);
}
printf("\n");
return 0;
}
|