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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
/* id -- print real and effective UIDs and GIDs
Copyright (C) 1989-2007 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Written by Arnold Robbins.
Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <getopt.h>
#include <selinux/selinux.h>
#include "system.h"
#include "error.h"
#include "getugroups.h"
#include "quote.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "id"
#define AUTHORS "Arnold Robbins", "David MacKenzie"
/* If nonzero, output only the SELinux context. -Z */
static int just_context = 0;
static void print_user (uid_t uid);
static void print_group (gid_t gid);
static void print_group_list (const char *username);
static void print_full_info (const char *username);
/* The name this program was run with. */
char *program_name;
/* If true, output user/group name instead of ID number. -n */
static bool use_name = false;
/* The real and effective IDs of the user to print. */
static uid_t ruid, euid;
static gid_t rgid, egid;
/* True unless errors have been encountered. */
static bool ok = true;
/* The SELinux context. Start with a known invalid value so print_full_info
knows when `context' has not been set to a meaningful value. */
static security_context_t context = NULL;
static struct option const longopts[] =
{
{"context", no_argument, NULL, 'Z'},
{"group", no_argument, NULL, 'g'},
{"groups", no_argument, NULL, 'G'},
{"name", no_argument, NULL, 'n'},
{"real", no_argument, NULL, 'r'},
{"user", no_argument, NULL, 'u'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
fputs (_("\
Print information for USERNAME, or the current user.\n\
\n\
-a ignore, for compatibility with other versions\n\
-Z, --context print only the security context of the current user\n\
-g, --group print only the effective group ID\n\
-G, --groups print all group IDs\n\
-n, --name print a name instead of a number, for -ugG\n\
-r, --real print the real ID instead of the effective ID, with -ugG\n\
-u, --user print only the effective user ID\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
Without any OPTION, print some useful set of identified information.\n\
"), stdout);
emit_bug_reporting_address ();
}
exit (status);
}
int
main (int argc, char **argv)
{
int optc;
int selinux_enabled = (is_selinux_enabled () > 0);
/* If true, output the list of all group IDs. -G */
bool just_group_list = false;
/* If true, output only the group ID(s). -g */
bool just_group = false;
/* If true, output real UID/GID instead of default effective UID/GID. -r */
bool use_real = false;
/* If true, output only the user ID(s). -u */
bool just_user = false;
initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
while ((optc = getopt_long (argc, argv, "agnruGZ", longopts, NULL)) != -1)
{
switch (optc)
{
case 'a':
/* Ignore -a, for compatibility with SVR4. */
break;
case 'Z':
/* politely decline if we're not on a selinux-enabled kernel. */
if (!selinux_enabled)
error (EXIT_FAILURE, 0,
_("--context (-Z) works only on an SELinux-enabled kernel"));
just_context = 1;
break;
case 'g':
just_group = true;
break;
case 'n':
use_name = true;
break;
case 'r':
use_real = true;
break;
case 'u':
just_user = true;
break;
case 'G':
just_group_list = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (1 < argc - optind)
{
error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
usage (EXIT_FAILURE);
}
if (argc - optind == 1 && just_context)
error (EXIT_FAILURE, 0,
_("cannot print security context when user specified"));
if (just_context && !selinux_enabled)
error (EXIT_FAILURE, 0, _("\
cannot display context when selinux not enabled or when displaying the id\n\
of a different user"));
/* If we are on a selinux-enabled kernel, get our context.
Otherwise, leave the context variable alone - it has
been initialized known invalid value; if we see this invalid
value later, we will know we are on a non-selinux kernel. */
if (selinux_enabled)
{
if (getcon (&context) && just_context)
error (EXIT_FAILURE, 0, _("can't get process context"));
}
if (just_user + just_group + just_group_list + just_context > 1)
error (EXIT_FAILURE, 0, _("cannot print \"only\" of more than one choice"));
if (just_user + just_group + just_group_list == 0 && (use_real | use_name))
error (EXIT_FAILURE, 0,
_("cannot print only names or real IDs in default format"));
if (argc - optind == 1)
{
struct passwd *pwd = getpwnam (argv[optind]);
if (pwd == NULL)
error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
ruid = euid = pwd->pw_uid;
rgid = egid = pwd->pw_gid;
}
else
{
euid = geteuid ();
ruid = getuid ();
egid = getegid ();
rgid = getgid ();
}
if (just_user)
print_user (use_real ? ruid : euid);
else if (just_group)
print_group (use_real ? rgid : egid);
else if (just_group_list)
print_group_list (argv[optind]);
else if (just_context)
fputs (context, stdout);
else
print_full_info (argv[optind]);
putchar ('\n');
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* Print the name or value of user ID UID. */
static void
print_user (uid_t uid)
{
struct passwd *pwd = NULL;
if (use_name)
{
pwd = getpwuid (uid);
if (pwd == NULL)
{
error (0, 0, _("cannot find name for user ID %lu"),
(unsigned long int) uid);
ok = false;
}
}
if (pwd == NULL)
printf ("%lu", (unsigned long int) uid);
else
printf ("%s", pwd->pw_name);
}
/* Print the name or value of group ID GID. */
static void
print_group (gid_t gid)
{
struct group *grp = NULL;
if (use_name)
{
grp = getgrgid (gid);
if (grp == NULL)
{
error (0, 0, _("cannot find name for group ID %lu"),
(unsigned long int) gid);
ok = false;
}
}
if (grp == NULL)
printf ("%lu", (unsigned long int) gid);
else
printf ("%s", grp->gr_name);
}
#if HAVE_GETGROUPS
/* FIXME: document */
static bool
xgetgroups (const char *username, gid_t gid, int *n_groups,
GETGROUPS_T **groups)
{
int max_n_groups;
int ng;
GETGROUPS_T *g = NULL;
if (!username)
max_n_groups = getgroups (0, NULL);
else
max_n_groups = getugroups (0, NULL, username, gid);
if (max_n_groups < 0)
ng = -1;
else
{
g = xnmalloc (max_n_groups, sizeof *g);
if (!username)
ng = getgroups (max_n_groups, g);
else
ng = getugroups (max_n_groups, g, username, gid);
}
if (ng < 0)
{
error (0, errno, _("cannot get supplemental group list"));
free (g);
return false;
}
else
{
*n_groups = ng;
*groups = g;
return true;
}
}
#endif /* HAVE_GETGROUPS */
/* Print all of the distinct groups the user is in. */
static void
print_group_list (const char *username)
{
struct passwd *pwd;
pwd = getpwuid (ruid);
if (pwd == NULL)
ok = false;
print_group (rgid);
if (egid != rgid)
{
putchar (' ');
print_group (egid);
}
#if HAVE_GETGROUPS
{
int n_groups;
GETGROUPS_T *groups;
int i;
if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
&n_groups, &groups))
{
ok = false;
return;
}
for (i = 0; i < n_groups; i++)
if (groups[i] != rgid && groups[i] != egid)
{
putchar (' ');
print_group (groups[i]);
}
free (groups);
}
#endif /* HAVE_GETGROUPS */
}
/* Print all of the info about the user's user and group IDs. */
static void
print_full_info (const char *username)
{
struct passwd *pwd;
struct group *grp;
printf ("uid=%lu", (unsigned long int) ruid);
pwd = getpwuid (ruid);
if (pwd)
printf ("(%s)", pwd->pw_name);
printf (" gid=%lu", (unsigned long int) rgid);
grp = getgrgid (rgid);
if (grp)
printf ("(%s)", grp->gr_name);
if (euid != ruid)
{
printf (" euid=%lu", (unsigned long int) euid);
pwd = getpwuid (euid);
if (pwd)
printf ("(%s)", pwd->pw_name);
}
if (egid != rgid)
{
printf (" egid=%lu", (unsigned long int) egid);
grp = getgrgid (egid);
if (grp)
printf ("(%s)", grp->gr_name);
}
#if HAVE_GETGROUPS
{
int n_groups;
GETGROUPS_T *groups;
int i;
if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
&n_groups, &groups))
{
ok = false;
return;
}
if (n_groups > 0)
fputs (_(" groups="), stdout);
for (i = 0; i < n_groups; i++)
{
if (i > 0)
putchar (',');
printf ("%lu", (unsigned long int) groups[i]);
grp = getgrgid (groups[i]);
if (grp)
printf ("(%s)", grp->gr_name);
}
free (groups);
}
#endif /* HAVE_GETGROUPS */
if (context != NULL)
printf (" context=%s", context);
}
|