1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989, 1991 Free Software Foundation.
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. */
23 #include <sys/types.h>
30 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
31 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
32 #endif /* !NGROUPS_MAX */
34 /* Even though SunOS 4, Ultrix 4, and 386BSD are mostly POSIX.1 compliant,
35 their getgroups system call (except in the `System V' environment, which
36 is troublesome in other ways) fills in an array of int, not gid_t
37 (which is `short' on those systems). Kludge, kludge. */
39 #if !defined(sun) && !defined(ultrix) && !defined(__386BSD__)
40 #define GETGROUPS_T gid_t
41 #else /* sun or ultrix or 386BSD */
42 #define GETGROUPS_T int
43 #endif /* sun or ultrix or 386BSD */
44 #else /* not _POSIX_VERSION */
45 struct passwd *getpwuid ();
46 struct group *getgrgid ();
51 #include <sys/param.h>
52 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
53 #define NGROUPS_MAX NGROUPS
54 #endif /* not NGROUPS_MAX and NGROUPS */
55 #define GETGROUPS_T int
56 #endif /* not _POSIX_VERSION */
62 static void print_user ();
63 static void print_group ();
64 static void print_group_list ();
65 static void print_full_info ();
68 /* The name this program was run with. */
71 /* If nonzero, output only the group ID(s). -g */
72 static int just_group = 0;
74 /* If nonzero, output user/group name instead of ID number. -n */
75 static int use_name = 0;
77 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
78 static int use_real = 0;
80 /* If nonzero, output only the user ID(s). -u */
81 static int just_user = 0;
83 /* If nonzero, output only the supplementary groups. -G */
84 static int just_group_list = 0;
86 /* The real and effective IDs of the user to print. */
87 static uid_t ruid, euid;
88 static gid_t rgid, egid;
90 /* The number of errors encountered so far. */
91 static int problems = 0;
93 static struct option const longopts[] =
95 {"group", no_argument, NULL, 'g'},
96 {"name", no_argument, NULL, 'n'},
97 {"real", no_argument, NULL, 'r'},
98 {"user", no_argument, NULL, 'u'},
99 {"groups", no_argument, NULL, 'G'},
110 program_name = argv[0];
112 while ((optc = getopt_long (argc, argv, "gnruG", longopts, (int *) 0))
137 if (just_user + just_group + just_group_list > 1)
138 error (1, 0, "cannot print only user and only group");
140 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
141 error (1, 0, "cannot print only names or real IDs in default format");
143 if (argc - optind > 1)
146 if (argc - optind == 1)
148 struct passwd *pwd = getpwnam (argv[optind]);
150 error (1, 0, "%s: No such user", argv[optind]);
151 ruid = euid = pwd->pw_uid;
152 rgid = egid = pwd->pw_gid;
163 print_user (use_real ? ruid : euid);
165 print_group (use_real ? rgid : egid);
166 else if (just_group_list)
167 print_group_list (argv[optind]);
169 print_full_info (argv[optind]);
172 exit (problems != 0);
175 /* Print the name or value of user ID UID. */
181 struct passwd *pwd = NULL;
185 pwd = getpwuid (uid);
191 printf ("%u", (unsigned) uid);
193 printf ("%s", pwd->pw_name);
196 /* Print the name or value of group ID GID. */
202 struct group *grp = NULL;
206 grp = getgrgid (gid);
212 printf ("%u", (unsigned) gid);
214 printf ("%s", grp->gr_name);
217 /* Print all of the distinct groups the user is in . */
220 print_group_list (username)
236 groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
238 ngroups = getgroups (NGROUPS_MAX, groups);
240 ngroups = getugroups (NGROUPS_MAX, groups, username);
243 error (0, errno, "cannot get supplemental group list");
249 for (i = 0; i < ngroups; i++)
250 if (groups[i] != rgid && groups[i] != egid)
253 print_group (groups[i]);
260 /* Print all of the info about the user's user and group IDs. */
263 print_full_info (username)
269 printf ("uid=%u", (unsigned) ruid);
270 pwd = getpwuid (ruid);
274 printf ("(%s)", pwd->pw_name);
276 printf (" gid=%u", (unsigned) rgid);
277 grp = getgrgid (rgid);
281 printf ("(%s)", grp->gr_name);
285 printf (" euid=%u", (unsigned) euid);
286 pwd = getpwuid (euid);
290 printf ("(%s)", pwd->pw_name);
295 printf (" egid=%u", (unsigned) egid);
296 grp = getgrgid (egid);
300 printf ("(%s)", grp->gr_name);
309 groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
311 ngroups = getgroups (NGROUPS_MAX, groups);
313 ngroups = getugroups (NGROUPS_MAX, groups, username);
316 error (0, errno, "cannot get supplemental group list");
323 fputs (" groups=", stdout);
324 for (i = 0; i < ngroups; i++)
328 printf ("%u", (unsigned) groups[i]);
329 grp = getgrgid (groups[i]);
333 printf ("(%s)", grp->gr_name);
344 Usage: %s [-gnruG] [--group] [--name] [--real] [--user] [--groups] [username]\n",