(EXTRA_DIST): Distribute $(TESTS).
[coreutils.git] / src / id.c
blobe5d8e62138417a8e7a376e58f9aca33ecef33527
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 89, 90, 91, 92, 93, 94, 95, 1996 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"
32 #ifdef _POSIX_VERSION
33 #include <limits.h>
35 #else /* not _POSIX_VERSION */
36 struct passwd *getpwuid ();
37 struct group *getgrgid ();
38 uid_t getuid ();
39 gid_t getgid ();
40 uid_t geteuid ();
41 gid_t getegid ();
42 #include <sys/param.h>
43 #endif /* not _POSIX_VERSION */
45 char *xmalloc ();
46 int getugroups ();
48 static void print_user __P ((int uid));
49 static void print_group __P ((int gid));
50 static void print_group_list __P ((char *username));
51 static void print_full_info __P ((char *username));
52 static void usage __P ((int status));
54 /* The name this program was run with. */
55 char *program_name;
57 /* If nonzero, output only the group ID(s). -g */
58 static int just_group = 0;
60 /* If nonzero, output user/group name instead of ID number. -n */
61 static int use_name = 0;
63 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
64 static int use_real = 0;
66 /* If nonzero, output only the user ID(s). -u */
67 static int just_user = 0;
69 /* If nonzero, output only the supplementary groups. -G */
70 static int just_group_list = 0;
72 /* The real and effective IDs of the user to print. */
73 static uid_t ruid, euid;
74 static gid_t rgid, egid;
76 /* The number of errors encountered so far. */
77 static int problems = 0;
79 /* If nonzero, display usage information and exit. */
80 static int show_help;
82 /* If nonzero, print the version on standard output and exit. */
83 static int show_version;
85 static struct option const longopts[] =
87 {"group", no_argument, NULL, 'g'},
88 {"groups", no_argument, NULL, 'G'},
89 {"help", no_argument, &show_help, 1},
90 {"name", no_argument, NULL, 'n'},
91 {"real", no_argument, NULL, 'r'},
92 {"user", no_argument, NULL, 'u'},
93 {"version", no_argument, &show_version, 1},
94 {NULL, 0, NULL, 0}
97 int
98 main (int argc, char **argv)
100 int optc;
102 program_name = argv[0];
103 setlocale (LC_ALL, "");
104 bindtextdomain (PACKAGE, LOCALEDIR);
105 textdomain (PACKAGE);
107 while ((optc = getopt_long (argc, argv, "gnruG", longopts, (int *) 0))
108 != EOF)
110 switch (optc)
112 case 0:
113 break;
114 case 'g':
115 just_group = 1;
116 break;
117 case 'n':
118 use_name = 1;
119 break;
120 case 'r':
121 use_real = 1;
122 break;
123 case 'u':
124 just_user = 1;
125 break;
126 case 'G':
127 just_group_list = 1;
128 break;
129 default:
130 usage (1);
134 if (show_version)
136 printf ("id - %s\n", PACKAGE_VERSION);
137 exit (0);
140 if (show_help)
141 usage (0);
143 if (just_user + just_group + just_group_list > 1)
144 error (1, 0, _("cannot print only user and only group"));
146 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
147 error (1, 0, _("cannot print only names or real IDs in default format"));
149 if (argc - optind > 1)
150 usage (1);
152 if (argc - optind == 1)
154 struct passwd *pwd = getpwnam (argv[optind]);
155 if (pwd == NULL)
156 error (1, 0, _("%s: No such user"), argv[optind]);
157 ruid = euid = pwd->pw_uid;
158 rgid = egid = pwd->pw_gid;
160 else
162 euid = geteuid ();
163 ruid = getuid ();
164 egid = getegid ();
165 rgid = getgid ();
168 if (just_user)
169 print_user (use_real ? ruid : euid);
170 else if (just_group)
171 print_group (use_real ? rgid : egid);
172 else if (just_group_list)
173 print_group_list (argv[optind]);
174 else
175 print_full_info (argv[optind]);
176 putchar ('\n');
178 exit (problems != 0);
181 /* Print the name or value of user ID UID. */
183 static void
184 print_user (int uid)
186 struct passwd *pwd = NULL;
188 if (use_name)
190 pwd = getpwuid (uid);
191 if (pwd == NULL)
192 problems++;
195 if (pwd == NULL)
196 printf ("%u", (unsigned) uid);
197 else
198 printf ("%s", pwd->pw_name);
201 /* Print the name or value of group ID GID. */
203 static void
204 print_group (int gid)
206 struct group *grp = NULL;
208 if (use_name)
210 grp = getgrgid (gid);
211 if (grp == NULL)
212 problems++;
215 if (grp == NULL)
216 printf ("%u", (unsigned) gid);
217 else
218 printf ("%s", grp->gr_name);
221 static int
222 xgetgroups (const char *username, int *n_groups, GETGROUPS_T **groups)
224 int max_n_groups;
225 int ng;
226 GETGROUPS_T *g;
227 int fail = 0;
229 if (username == 0)
230 max_n_groups = getgroups (0, NULL);
231 else
232 max_n_groups = getugroups (0, NULL, username);
234 /* Add 1 just in case max_n_groups is zero. */
235 g = (GETGROUPS_T *) xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
236 if (username == 0)
237 ng = getgroups (max_n_groups, g);
238 else
239 ng = getugroups (max_n_groups, g, username);
241 if (ng < 0)
243 error (0, errno, _("cannot get supplemental group list"));
244 ++fail;
245 free (groups);
247 if (!fail)
249 *n_groups = ng;
250 *groups = g;
252 return fail;
255 /* Print all of the distinct groups the user is in. */
257 static void
258 print_group_list (char *username)
260 print_group (rgid);
261 if (egid != rgid)
263 putchar (' ');
264 print_group (egid);
267 #if HAVE_GETGROUPS
269 int n_groups;
270 GETGROUPS_T *groups;
271 register int i;
273 if (xgetgroups (username, &n_groups, &groups))
275 ++problems;
276 return;
279 for (i = 0; i < n_groups; i++)
280 if (groups[i] != rgid && groups[i] != egid)
282 putchar (' ');
283 print_group (groups[i]);
285 free (groups);
287 #endif
290 /* Print all of the info about the user's user and group IDs. */
292 static void
293 print_full_info (char *username)
295 struct passwd *pwd;
296 struct group *grp;
298 printf ("uid=%u", (unsigned) ruid);
299 pwd = getpwuid (ruid);
300 if (pwd == NULL)
301 problems++;
302 else
303 printf ("(%s)", pwd->pw_name);
305 printf (" gid=%u", (unsigned) rgid);
306 grp = getgrgid (rgid);
307 if (grp == NULL)
308 problems++;
309 else
310 printf ("(%s)", grp->gr_name);
312 if (euid != ruid)
314 printf (" euid=%u", (unsigned) euid);
315 pwd = getpwuid (euid);
316 if (pwd == NULL)
317 problems++;
318 else
319 printf ("(%s)", pwd->pw_name);
322 if (egid != rgid)
324 printf (" egid=%u", (unsigned) egid);
325 grp = getgrgid (egid);
326 if (grp == NULL)
327 problems++;
328 else
329 printf ("(%s)", grp->gr_name);
332 #if HAVE_GETGROUPS
334 int n_groups;
335 GETGROUPS_T *groups;
336 register int i;
338 if (xgetgroups (username, &n_groups, &groups))
340 ++problems;
341 return;
344 if (n_groups > 0)
345 fputs (_(" groups="), stdout);
346 for (i = 0; i < n_groups; i++)
348 if (i > 0)
349 putchar (',');
350 printf ("%u", (unsigned) groups[i]);
351 grp = getgrgid (groups[i]);
352 if (grp == NULL)
353 problems++;
354 else
355 printf ("(%s)", grp->gr_name);
357 free (groups);
359 #endif
362 static void
363 usage (int status)
365 if (status != 0)
366 fprintf (stderr, _("Try `%s --help' for more information.\n"),
367 program_name);
368 else
370 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
371 printf (_("\
372 Print information for USERNAME, or the current user.\n\
374 -g, --group print only the group ID\n\
375 -G, --groups print only the supplementary groups\n\
376 -n, --name print a name instead of a number, for -ugG\n\
377 -r, --real print the real ID instead of effective ID, for -ugG\n\
378 -u, --user print only the user ID\n\
379 --help display this help and exit\n\
380 --version output version information and exit\n\
382 Without any OPTION, print some useful set of identified information.\n\
383 "));
384 puts (_("\nReport bugs to bug-gnu-utils@gnu.ai.mit.edu"));
386 exit (status);