*** empty log message ***
[coreutils.git] / src / id.c
blobad070dc05c7af27cf47c417ba6dee16c819d3088
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 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "id"
35 #define AUTHORS "Arnold Robbins and David MacKenzie"
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct group *getgrgid ();
40 uid_t getuid ();
41 gid_t getgid ();
42 uid_t geteuid ();
43 gid_t getegid ();
44 #endif /* not _POSIX_VERSION */
46 int getugroups ();
48 static void print_user PARAMS ((uid_t uid));
49 static void print_group PARAMS ((gid_t gid));
50 static void print_group_list PARAMS ((const char *username));
51 static void print_full_info PARAMS ((const char *username));
53 /* The name this program was run with. */
54 char *program_name;
56 /* If nonzero, output only the group ID(s). -g */
57 static int just_group = 0;
59 /* If nonzero, output user/group name instead of ID number. -n */
60 static int use_name = 0;
62 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
63 static int use_real = 0;
65 /* If nonzero, output only the user ID(s). -u */
66 static int just_user = 0;
68 /* If nonzero, output only the supplementary groups. -G */
69 static int just_group_list = 0;
71 /* The real and effective IDs of the user to print. */
72 static uid_t ruid, euid;
73 static gid_t rgid, egid;
75 /* The number of errors encountered so far. */
76 static int problems = 0;
78 static struct option const longopts[] =
80 {"group", no_argument, NULL, 'g'},
81 {"groups", no_argument, NULL, 'G'},
82 {"name", no_argument, NULL, 'n'},
83 {"real", no_argument, NULL, 'r'},
84 {"user", no_argument, NULL, 'u'},
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
87 {NULL, 0, NULL, 0}
90 void
91 usage (int status)
93 if (status != 0)
94 fprintf (stderr, _("Try `%s --help' for more information.\n"),
95 program_name);
96 else
98 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
99 printf (_("\
100 Print information for USERNAME, or the current user.\n\
102 -a ignore, for compatibility with other versions\n\
103 -g, --group print only the group ID\n\
104 -G, --groups print only the supplementary groups\n\
105 -n, --name print a name instead of a number, for -ugG\n\
106 -r, --real print the real ID instead of effective ID, for -ugG\n\
107 -u, --user print only the user ID\n\
108 --help display this help and exit\n\
109 --version output version information and exit\n\
111 Without any OPTION, print some useful set of identified information.\n\
112 "));
113 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
115 exit (status);
119 main (int argc, char **argv)
121 int optc;
123 program_name = argv[0];
124 setlocale (LC_ALL, "");
125 bindtextdomain (PACKAGE, LOCALEDIR);
126 textdomain (PACKAGE);
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)
208 problems++;
211 if (pwd == NULL)
212 printf ("%u", (unsigned) uid);
213 else
214 printf ("%s", pwd->pw_name);
217 /* Print the name or value of group ID GID. */
219 static void
220 print_group (gid_t gid)
222 struct group *grp = NULL;
224 if (use_name)
226 grp = getgrgid (gid);
227 if (grp == NULL)
228 problems++;
231 if (grp == NULL)
232 printf ("%u", (unsigned) gid);
233 else
234 printf ("%s", grp->gr_name);
237 #if HAVE_GETGROUPS
239 /* FIXME: document */
241 static int
242 xgetgroups (const char *username, gid_t gid, int *n_groups,
243 GETGROUPS_T **groups)
245 int max_n_groups;
246 int ng;
247 GETGROUPS_T *g;
248 int fail = 0;
250 if (username == 0)
251 max_n_groups = getgroups (0, NULL);
252 else
253 max_n_groups = getugroups (0, NULL, username, gid);
255 /* Add 1 just in case max_n_groups is zero. */
256 g = (GETGROUPS_T *) xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
257 if (username == 0)
258 ng = getgroups (max_n_groups, g);
259 else
260 ng = getugroups (max_n_groups, g, username, gid);
262 if (ng < 0)
264 error (0, errno, _("cannot get supplemental group list"));
265 ++fail;
266 free (groups);
268 if (!fail)
270 *n_groups = ng;
271 *groups = g;
273 return fail;
276 #endif /* HAVE_GETGROUPS */
278 /* Print all of the distinct groups the user is in. */
280 static void
281 print_group_list (const char *username)
283 struct passwd *pwd;
285 pwd = getpwuid (ruid);
286 if (pwd == NULL)
287 problems++;
289 print_group (rgid);
290 if (egid != rgid)
292 putchar (' ');
293 print_group (egid);
296 #if HAVE_GETGROUPS
298 int n_groups;
299 GETGROUPS_T *groups;
300 register int i;
302 if (xgetgroups (username, pwd ? pwd->pw_gid : -1, &n_groups, &groups))
304 ++problems;
305 return;
308 for (i = 0; i < n_groups; i++)
309 if (groups[i] != rgid && groups[i] != egid)
311 putchar (' ');
312 print_group (groups[i]);
314 free (groups);
316 #endif /* HAVE_GETGROUPS */
319 /* Print all of the info about the user's user and group IDs. */
321 static void
322 print_full_info (const char *username)
324 struct passwd *pwd;
325 struct group *grp;
327 printf ("uid=%u", (unsigned) ruid);
328 pwd = getpwuid (ruid);
329 if (pwd == NULL)
330 problems++;
331 else
332 printf ("(%s)", pwd->pw_name);
334 printf (" gid=%u", (unsigned) rgid);
335 grp = getgrgid (rgid);
336 if (grp == NULL)
337 problems++;
338 else
339 printf ("(%s)", grp->gr_name);
341 if (euid != ruid)
343 printf (" euid=%u", (unsigned) euid);
344 pwd = getpwuid (euid);
345 if (pwd == NULL)
346 problems++;
347 else
348 printf ("(%s)", pwd->pw_name);
351 if (egid != rgid)
353 printf (" egid=%u", (unsigned) egid);
354 grp = getgrgid (egid);
355 if (grp == NULL)
356 problems++;
357 else
358 printf ("(%s)", grp->gr_name);
361 #if HAVE_GETGROUPS
363 int n_groups;
364 GETGROUPS_T *groups;
365 register int i;
367 if (xgetgroups (username, pwd ? pwd->pw_gid : -1, &n_groups, &groups))
369 ++problems;
370 return;
373 if (n_groups > 0)
374 fputs (_(" groups="), stdout);
375 for (i = 0; i < n_groups; i++)
377 if (i > 0)
378 putchar (',');
379 printf ("%u", (unsigned) groups[i]);
380 grp = getgrgid (groups[i]);
381 if (grp == NULL)
382 problems++;
383 else
384 printf ("(%s)", grp->gr_name);
386 free (groups);
388 #endif /* HAVE_GETGROUPS */