*** empty log message ***
[coreutils.git] / src / id.c
blobb9a404cd3d0ee6e06e874019c8e2090cac30f66c
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-1999 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 #ifndef _POSIX_VERSION
33 struct passwd *getpwuid ();
34 struct group *getgrgid ();
35 uid_t getuid ();
36 gid_t getgid ();
37 uid_t geteuid ();
38 gid_t getegid ();
39 #endif /* not _POSIX_VERSION */
41 int getugroups ();
43 static void print_user PARAMS ((int uid));
44 static void print_group PARAMS ((int gid));
45 static void print_group_list PARAMS ((const char *username));
46 static void print_full_info PARAMS ((const char *username));
48 /* The name this program was run with. */
49 char *program_name;
51 /* If nonzero, output only the group ID(s). -g */
52 static int just_group = 0;
54 /* If nonzero, output user/group name instead of ID number. -n */
55 static int use_name = 0;
57 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
58 static int use_real = 0;
60 /* If nonzero, output only the user ID(s). -u */
61 static int just_user = 0;
63 /* If nonzero, output only the supplementary groups. -G */
64 static int just_group_list = 0;
66 /* The real and effective IDs of the user to print. */
67 static uid_t ruid, euid;
68 static gid_t rgid, egid;
70 /* The number of errors encountered so far. */
71 static int problems = 0;
73 /* If nonzero, display usage information and exit. */
74 static int show_help;
76 /* If nonzero, print the version on standard output and exit. */
77 static int show_version;
79 static struct option const longopts[] =
81 {"group", no_argument, NULL, 'g'},
82 {"groups", no_argument, NULL, 'G'},
83 {"help", no_argument, &show_help, 1},
84 {"name", no_argument, NULL, 'n'},
85 {"real", no_argument, NULL, 'r'},
86 {"user", no_argument, NULL, 'u'},
87 {"version", no_argument, &show_version, 1},
88 {NULL, 0, NULL, 0}
91 void
92 usage (int status)
94 if (status != 0)
95 fprintf (stderr, _("Try `%s --help' for more information.\n"),
96 program_name);
97 else
99 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
100 printf (_("\
101 Print information for USERNAME, or the current user.\n\
103 -a ignore, for compatibility with other versions\n\
104 -g, --group print only the group ID\n\
105 -G, --groups print only the supplementary groups\n\
106 -n, --name print a name instead of a number, for -ugG\n\
107 -r, --real print the real ID instead of effective ID, for -ugG\n\
108 -u, --user print only the user ID\n\
109 --help display this help and exit\n\
110 --version output version information and exit\n\
112 Without any OPTION, print some useful set of identified information.\n\
113 "));
114 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
116 exit (status);
120 main (int argc, char **argv)
122 int optc;
124 program_name = argv[0];
125 setlocale (LC_ALL, "");
126 bindtextdomain (PACKAGE, LOCALEDIR);
127 textdomain (PACKAGE);
129 while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
131 switch (optc)
133 case 0:
134 break;
135 case 'a':
136 /* Ignore -a, for compatibility with SVR4. */
137 break;
138 case 'g':
139 just_group = 1;
140 break;
141 case 'n':
142 use_name = 1;
143 break;
144 case 'r':
145 use_real = 1;
146 break;
147 case 'u':
148 just_user = 1;
149 break;
150 case 'G':
151 just_group_list = 1;
152 break;
153 default:
154 usage (1);
158 if (show_version)
160 printf ("id (%s) %s\n", GNU_PACKAGE, VERSION);
161 exit (0);
164 if (show_help)
165 usage (0);
167 if (just_user + just_group + just_group_list > 1)
168 error (1, 0, _("cannot print only user and only group"));
170 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
171 error (1, 0, _("cannot print only names or real IDs in default format"));
173 if (argc - optind > 1)
174 usage (1);
176 if (argc - optind == 1)
178 struct passwd *pwd = getpwnam (argv[optind]);
179 if (pwd == NULL)
180 error (1, 0, _("%s: No such user"), argv[optind]);
181 ruid = euid = pwd->pw_uid;
182 rgid = egid = pwd->pw_gid;
184 else
186 euid = geteuid ();
187 ruid = getuid ();
188 egid = getegid ();
189 rgid = getgid ();
192 if (just_user)
193 print_user (use_real ? ruid : euid);
194 else if (just_group)
195 print_group (use_real ? rgid : egid);
196 else if (just_group_list)
197 print_group_list (argv[optind]);
198 else
199 print_full_info (argv[optind]);
200 putchar ('\n');
202 exit (problems != 0);
205 /* Print the name or value of user ID UID. */
207 static void
208 print_user (int uid)
210 struct passwd *pwd = NULL;
212 if (use_name)
214 pwd = getpwuid (uid);
215 if (pwd == NULL)
216 problems++;
219 if (pwd == NULL)
220 printf ("%u", (unsigned) uid);
221 else
222 printf ("%s", pwd->pw_name);
225 /* Print the name or value of group ID GID. */
227 static void
228 print_group (int gid)
230 struct group *grp = NULL;
232 if (use_name)
234 grp = getgrgid (gid);
235 if (grp == NULL)
236 problems++;
239 if (grp == NULL)
240 printf ("%u", (unsigned) gid);
241 else
242 printf ("%s", grp->gr_name);
245 #if HAVE_GETGROUPS
247 static int
248 xgetgroups (const char *username, int *n_groups, GETGROUPS_T **groups)
250 int max_n_groups;
251 int ng;
252 GETGROUPS_T *g;
253 int fail = 0;
255 if (username == 0)
256 max_n_groups = getgroups (0, NULL);
257 else
258 max_n_groups = getugroups (0, NULL, username);
260 /* Add 1 just in case max_n_groups is zero. */
261 g = (GETGROUPS_T *) xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
262 if (username == 0)
263 ng = getgroups (max_n_groups, g);
264 else
265 ng = getugroups (max_n_groups, g, username);
267 if (ng < 0)
269 error (0, errno, _("cannot get supplemental group list"));
270 ++fail;
271 free (groups);
273 if (!fail)
275 *n_groups = ng;
276 *groups = g;
278 return fail;
281 #endif /* HAVE_GETGROUPS */
283 /* Print all of the distinct groups the user is in. */
285 static void
286 print_group_list (const char *username)
288 print_group (rgid);
289 if (egid != rgid)
291 putchar (' ');
292 print_group (egid);
295 #if HAVE_GETGROUPS
297 int n_groups;
298 GETGROUPS_T *groups;
299 register int i;
301 if (xgetgroups (username, &n_groups, &groups))
303 ++problems;
304 return;
307 for (i = 0; i < n_groups; i++)
308 if (groups[i] != rgid && groups[i] != egid)
310 putchar (' ');
311 print_group (groups[i]);
313 free (groups);
315 #endif /* HAVE_GETGROUPS */
318 /* Print all of the info about the user's user and group IDs. */
320 static void
321 print_full_info (const char *username)
323 struct passwd *pwd;
324 struct group *grp;
326 printf ("uid=%u", (unsigned) ruid);
327 pwd = getpwuid (ruid);
328 if (pwd == NULL)
329 problems++;
330 else
331 printf ("(%s)", pwd->pw_name);
333 printf (" gid=%u", (unsigned) rgid);
334 grp = getgrgid (rgid);
335 if (grp == NULL)
336 problems++;
337 else
338 printf ("(%s)", grp->gr_name);
340 if (euid != ruid)
342 printf (" euid=%u", (unsigned) euid);
343 pwd = getpwuid (euid);
344 if (pwd == NULL)
345 problems++;
346 else
347 printf ("(%s)", pwd->pw_name);
350 if (egid != rgid)
352 printf (" egid=%u", (unsigned) egid);
353 grp = getgrgid (egid);
354 if (grp == NULL)
355 problems++;
356 else
357 printf ("(%s)", grp->gr_name);
360 #if HAVE_GETGROUPS
362 int n_groups;
363 GETGROUPS_T *groups;
364 register int i;
366 if (xgetgroups (username, &n_groups, &groups))
368 ++problems;
369 return;
372 if (n_groups > 0)
373 fputs (_(" groups="), stdout);
374 for (i = 0; i < n_groups; i++)
376 if (i > 0)
377 putchar (',');
378 printf ("%u", (unsigned) groups[i]);
379 grp = getgrgid (groups[i]);
380 if (grp == NULL)
381 problems++;
382 else
383 printf ("(%s)", grp->gr_name);
385 free (groups);
387 #endif /* HAVE_GETGROUPS */