1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 89, 90, 91, 92, 93, 94, 1995 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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
24 #include <sys/types.h>
35 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
37 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
38 #endif /* !NGROUPS_MAX */
40 #else /* not _POSIX_VERSION */
41 struct passwd *getpwuid ();
42 struct group *getgrgid ();
47 #include <sys/param.h>
48 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
49 #define NGROUPS_MAX NGROUPS
50 #endif /* not NGROUPS_MAX and NGROUPS */
51 #endif /* not _POSIX_VERSION */
56 static void print_user ();
57 static void print_group ();
58 static void print_group_list ();
59 static void print_full_info ();
62 /* The name this program was run with. */
65 /* If nonzero, output only the group ID(s). -g */
66 static int just_group = 0;
68 /* If nonzero, output user/group name instead of ID number. -n */
69 static int use_name = 0;
71 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
72 static int use_real = 0;
74 /* If nonzero, output only the user ID(s). -u */
75 static int just_user = 0;
77 /* If nonzero, output only the supplementary groups. -G */
78 static int just_group_list = 0;
80 /* The real and effective IDs of the user to print. */
81 static uid_t ruid, euid;
82 static gid_t rgid, egid;
84 /* The number of errors encountered so far. */
85 static int problems = 0;
87 /* If non-zero, display usage information and exit. */
90 /* If non-zero, print the version on standard output and exit. */
91 static int show_version;
93 static struct option const longopts[] =
95 {"group", no_argument, NULL, 'g'},
96 {"groups", no_argument, NULL, 'G'},
97 {"help", no_argument, &show_help, 1},
98 {"name", no_argument, NULL, 'n'},
99 {"real", no_argument, NULL, 'r'},
100 {"user", no_argument, NULL, 'u'},
101 {"version", no_argument, &show_version, 1},
112 program_name = argv[0];
114 while ((optc = getopt_long (argc, argv, "gnruG", longopts, (int *) 0))
143 printf ("id - %s\n", version_string);
150 if (just_user + just_group + just_group_list > 1)
151 error (1, 0, "cannot print only user and only group");
153 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
154 error (1, 0, "cannot print only names or real IDs in default format");
156 if (argc - optind > 1)
159 if (argc - optind == 1)
161 struct passwd *pwd = getpwnam (argv[optind]);
163 error (1, 0, "%s: No such user", argv[optind]);
164 ruid = euid = pwd->pw_uid;
165 rgid = egid = pwd->pw_gid;
176 print_user (use_real ? ruid : euid);
178 print_group (use_real ? rgid : egid);
179 else if (just_group_list)
180 print_group_list (argv[optind]);
182 print_full_info (argv[optind]);
185 exit (problems != 0);
188 /* Print the name or value of user ID UID. */
194 struct passwd *pwd = NULL;
198 pwd = getpwuid (uid);
204 printf ("%u", (unsigned) uid);
206 printf ("%s", pwd->pw_name);
209 /* Print the name or value of group ID GID. */
215 struct group *grp = NULL;
219 grp = getgrgid (gid);
225 printf ("%u", (unsigned) gid);
227 printf ("%s", grp->gr_name);
230 /* Print all of the distinct groups the user is in . */
233 print_group_list (username)
243 #if defined(NGROUPS_MAX) && defined(HAVE_GETGROUPS)
249 groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
251 ngroups = getgroups (NGROUPS_MAX, groups);
253 ngroups = getugroups (NGROUPS_MAX, groups, username);
256 error (0, errno, "cannot get supplemental group list");
262 for (i = 0; i < ngroups; i++)
263 if (groups[i] != rgid && groups[i] != egid)
266 print_group (groups[i]);
273 /* Print all of the info about the user's user and group IDs. */
276 print_full_info (username)
282 printf ("uid=%u", (unsigned) ruid);
283 pwd = getpwuid (ruid);
287 printf ("(%s)", pwd->pw_name);
289 printf (" gid=%u", (unsigned) rgid);
290 grp = getgrgid (rgid);
294 printf ("(%s)", grp->gr_name);
298 printf (" euid=%u", (unsigned) euid);
299 pwd = getpwuid (euid);
303 printf ("(%s)", pwd->pw_name);
308 printf (" egid=%u", (unsigned) egid);
309 grp = getgrgid (egid);
313 printf ("(%s)", grp->gr_name);
316 #if defined(NGROUPS_MAX) && defined(HAVE_GETGROUPS)
322 groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
324 ngroups = getgroups (NGROUPS_MAX, groups);
326 ngroups = getugroups (NGROUPS_MAX, groups, username);
329 error (0, errno, "cannot get supplemental group list");
336 fputs (" groups=", stdout);
337 for (i = 0; i < ngroups; i++)
341 printf ("%u", (unsigned) groups[i]);
342 grp = getgrgid (groups[i]);
346 printf ("(%s)", grp->gr_name);
358 fprintf (stderr, "Try `%s --help' for more information.\n",
362 printf ("Usage: %s [OPTION]... [USERNAME]\n", program_name);
365 -g, --group print only the group ID\n\
366 -G, --groups print only the supplementary groups\n\
367 -n, --name print a name instead of a number, for -ugG\n\
368 -r, --real print the real ID instead of effective ID, for -ugG\n\
369 -u, --user print only the user ID\n\
370 --help display this help and exit\n\
371 --version output version information and exit\n\
373 Without any OPTION, print some useful set of identified information.\n\