'sort' race condition fixes.
[coreutils.git] / src / id.c
blobb0613ae6e80d19daf9ea70fb61eb3b8221f345bc
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <getopt.h>
29 #include "system.h"
30 #include "error.h"
31 #include "closeout.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "id"
36 #define AUTHORS "Arnold Robbins and David MacKenzie"
38 #ifndef _POSIX_VERSION
39 struct passwd *getpwuid ();
40 struct group *getgrgid ();
41 uid_t getuid ();
42 gid_t getgid ();
43 uid_t geteuid ();
44 gid_t getegid ();
45 #endif /* not _POSIX_VERSION */
47 int getugroups ();
49 static void print_user PARAMS ((uid_t uid));
50 static void print_group PARAMS ((gid_t gid));
51 static void print_group_list PARAMS ((const char *username));
52 static void print_full_info PARAMS ((const char *username));
54 /* The name this program was run with. */
55 char *program_name;
57 /* If nonzero, output user/group name instead of ID number. -n */
58 static int use_name = 0;
60 /* The real and effective IDs of the user to print. */
61 static uid_t ruid, euid;
62 static gid_t rgid, egid;
64 /* The number of errors encountered so far. */
65 static int problems = 0;
67 static struct option const longopts[] =
69 {"group", no_argument, NULL, 'g'},
70 {"groups", no_argument, NULL, 'G'},
71 {"name", no_argument, NULL, 'n'},
72 {"real", no_argument, NULL, 'r'},
73 {"user", no_argument, NULL, 'u'},
74 {GETOPT_HELP_OPTION_DECL},
75 {GETOPT_VERSION_OPTION_DECL},
76 {NULL, 0, NULL, 0}
79 void
80 usage (int status)
82 if (status != 0)
83 fprintf (stderr, _("Try `%s --help' for more information.\n"),
84 program_name);
85 else
87 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
88 printf (_("\
89 Print information for USERNAME, or the current user.\n\
90 \n\
91 -a ignore, for compatibility with other versions\n\
92 -g, --group print only the effective group ID\n\
93 -G, --groups print all group IDs\n\
94 -n, --name print a name instead of a number, for -ugG\n\
95 -r, --real print the real ID instead of the effective ID, with -ugG\n\
96 -u, --user print only the effective user ID\n\
97 --help display this help and exit\n\
98 --version output version information and exit\n\
99 \n\
100 Without any OPTION, print some useful set of identified information.\n\
101 "));
102 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
104 exit (status);
108 main (int argc, char **argv)
110 int optc;
112 /* If nonzero, output the list of all group IDs. -G */
113 int just_group_list = 0;
114 /* If nonzero, output only the group ID(s). -g */
115 int just_group = 0;
116 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
117 int use_real = 0;
118 /* If nonzero, output only the user ID(s). -u */
119 int just_user = 0;
121 program_name = argv[0];
122 setlocale (LC_ALL, "");
123 bindtextdomain (PACKAGE, LOCALEDIR);
124 textdomain (PACKAGE);
126 atexit (close_stdout);
128 while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
130 switch (optc)
132 case 0:
133 break;
134 case 'a':
135 /* Ignore -a, for compatibility with SVR4. */
136 break;
137 case 'g':
138 just_group = 1;
139 break;
140 case 'n':
141 use_name = 1;
142 break;
143 case 'r':
144 use_real = 1;
145 break;
146 case 'u':
147 just_user = 1;
148 break;
149 case 'G':
150 just_group_list = 1;
151 break;
152 case_GETOPT_HELP_CHAR;
153 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
154 default:
155 usage (1);
159 if (just_user + just_group + just_group_list > 1)
160 error (1, 0, _("cannot print only user and only group"));
162 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
163 error (1, 0, _("cannot print only names or real IDs in default format"));
165 if (argc - optind > 1)
166 usage (1);
168 if (argc - optind == 1)
170 struct passwd *pwd = getpwnam (argv[optind]);
171 if (pwd == NULL)
172 error (1, 0, _("%s: No such user"), argv[optind]);
173 ruid = euid = pwd->pw_uid;
174 rgid = egid = pwd->pw_gid;
176 else
178 euid = geteuid ();
179 ruid = getuid ();
180 egid = getegid ();
181 rgid = getgid ();
184 if (just_user)
185 print_user (use_real ? ruid : euid);
186 else if (just_group)
187 print_group (use_real ? rgid : egid);
188 else if (just_group_list)
189 print_group_list (argv[optind]);
190 else
191 print_full_info (argv[optind]);
192 putchar ('\n');
194 exit (problems != 0);
197 /* Print the name or value of user ID UID. */
199 static void
200 print_user (uid_t uid)
202 struct passwd *pwd = NULL;
204 if (use_name)
206 pwd = getpwuid (uid);
207 if (pwd == NULL)
209 error (0, 0, _("cannot find name for user ID %u"), uid);
210 problems++;
214 if (pwd == NULL)
215 printf ("%u", (unsigned) uid);
216 else
217 printf ("%s", pwd->pw_name);
220 /* Print the name or value of group ID GID. */
222 static void
223 print_group (gid_t gid)
225 struct group *grp = NULL;
227 if (use_name)
229 grp = getgrgid (gid);
230 if (grp == NULL)
232 error (0, 0, _("cannot find name for group ID %u"), gid);
233 problems++;
237 if (grp == NULL)
238 printf ("%u", (unsigned) gid);
239 else
240 printf ("%s", grp->gr_name);
243 #if HAVE_GETGROUPS
245 /* FIXME: document */
247 static int
248 xgetgroups (const char *username, gid_t gid, int *n_groups,
249 GETGROUPS_T **groups)
251 int max_n_groups;
252 int ng;
253 GETGROUPS_T *g;
254 int fail = 0;
256 if (username == 0)
257 max_n_groups = getgroups (0, NULL);
258 else
259 max_n_groups = getugroups (0, NULL, username, gid);
261 /* Add 1 just in case max_n_groups is zero. */
262 g = (GETGROUPS_T *) xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
263 if (username == 0)
264 ng = getgroups (max_n_groups, g);
265 else
266 ng = getugroups (max_n_groups, g, username, gid);
268 if (ng < 0)
270 error (0, errno, _("cannot get supplemental group list"));
271 ++fail;
272 free (groups);
274 if (!fail)
276 *n_groups = ng;
277 *groups = g;
279 return fail;
282 #endif /* HAVE_GETGROUPS */
284 /* Print all of the distinct groups the user is in. */
286 static void
287 print_group_list (const char *username)
289 struct passwd *pwd;
291 pwd = getpwuid (ruid);
292 if (pwd == NULL)
293 problems++;
295 print_group (rgid);
296 if (egid != rgid)
298 putchar (' ');
299 print_group (egid);
302 #if HAVE_GETGROUPS
304 int n_groups;
305 GETGROUPS_T *groups;
306 register int i;
308 if (xgetgroups (username, pwd ? pwd->pw_gid : -1, &n_groups, &groups))
310 ++problems;
311 return;
314 for (i = 0; i < n_groups; i++)
315 if (groups[i] != rgid && groups[i] != egid)
317 putchar (' ');
318 print_group (groups[i]);
320 free (groups);
322 #endif /* HAVE_GETGROUPS */
325 /* Print all of the info about the user's user and group IDs. */
327 static void
328 print_full_info (const char *username)
330 struct passwd *pwd;
331 struct group *grp;
333 printf ("uid=%u", (unsigned) ruid);
334 pwd = getpwuid (ruid);
335 if (pwd == NULL)
336 problems++;
337 else
338 printf ("(%s)", pwd->pw_name);
340 printf (" gid=%u", (unsigned) rgid);
341 grp = getgrgid (rgid);
342 if (grp == NULL)
343 problems++;
344 else
345 printf ("(%s)", grp->gr_name);
347 if (euid != ruid)
349 printf (" euid=%u", (unsigned) euid);
350 pwd = getpwuid (euid);
351 if (pwd == NULL)
352 problems++;
353 else
354 printf ("(%s)", pwd->pw_name);
357 if (egid != rgid)
359 printf (" egid=%u", (unsigned) egid);
360 grp = getgrgid (egid);
361 if (grp == NULL)
362 problems++;
363 else
364 printf ("(%s)", grp->gr_name);
367 #if HAVE_GETGROUPS
369 int n_groups;
370 GETGROUPS_T *groups;
371 register int i;
373 if (xgetgroups (username, pwd ? pwd->pw_gid : -1, &n_groups, &groups))
375 ++problems;
376 return;
379 if (n_groups > 0)
380 fputs (_(" groups="), stdout);
381 for (i = 0; i < n_groups; i++)
383 if (i > 0)
384 putchar (',');
385 printf ("%u", (unsigned) groups[i]);
386 grp = getgrgid (groups[i]);
387 if (grp == NULL)
388 problems++;
389 else
390 printf ("(%s)", grp->gr_name);
392 free (groups);
394 #endif /* HAVE_GETGROUPS */