* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / id.c
blob7abb3e50ca7b295c645d16e5811fbd1005aa7a9a
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Arnold Robbins.
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"
31 #include "quote.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "id"
36 #define AUTHORS "Arnold Robbins", "David MacKenzie"
38 int getugroups ();
40 static void print_user (uid_t uid);
41 static void print_group (gid_t gid);
42 static void print_group_list (const char *username);
43 static void print_full_info (const char *username);
45 /* The name this program was run with. */
46 char *program_name;
48 /* If true, output user/group name instead of ID number. -n */
49 static bool use_name = false;
51 /* The real and effective IDs of the user to print. */
52 static uid_t ruid, euid;
53 static gid_t rgid, egid;
55 /* True unless errors have been encountered. */
56 static bool ok = true;
58 static struct option const longopts[] =
60 {"group", no_argument, NULL, 'g'},
61 {"groups", no_argument, NULL, 'G'},
62 {"name", no_argument, NULL, 'n'},
63 {"real", no_argument, NULL, 'r'},
64 {"user", no_argument, NULL, 'u'},
65 {GETOPT_HELP_OPTION_DECL},
66 {GETOPT_VERSION_OPTION_DECL},
67 {NULL, 0, NULL, 0}
70 void
71 usage (int status)
73 if (status != EXIT_SUCCESS)
74 fprintf (stderr, _("Try `%s --help' for more information.\n"),
75 program_name);
76 else
78 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
79 fputs (_("\
80 Print information for USERNAME, or the current user.\n\
81 \n\
82 -a ignore, for compatibility with other versions\n\
83 -g, --group print only the effective group ID\n\
84 -G, --groups print all group IDs\n\
85 -n, --name print a name instead of a number, for -ugG\n\
86 -r, --real print the real ID instead of the effective ID, with -ugG\n\
87 -u, --user print only the effective user ID\n\
88 "), stdout);
89 fputs (HELP_OPTION_DESCRIPTION, stdout);
90 fputs (VERSION_OPTION_DESCRIPTION, stdout);
91 fputs (_("\
92 \n\
93 Without any OPTION, print some useful set of identified information.\n\
94 "), stdout);
95 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
97 exit (status);
101 main (int argc, char **argv)
103 int optc;
105 /* If true, output the list of all group IDs. -G */
106 bool just_group_list = false;
107 /* If true, output only the group ID(s). -g */
108 bool just_group = false;
109 /* If true, output real UID/GID instead of default effective UID/GID. -r */
110 bool use_real = false;
111 /* If true, output only the user ID(s). -u */
112 bool just_user = false;
114 initialize_main (&argc, &argv);
115 program_name = argv[0];
116 setlocale (LC_ALL, "");
117 bindtextdomain (PACKAGE, LOCALEDIR);
118 textdomain (PACKAGE);
120 atexit (close_stdout);
122 while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
124 switch (optc)
126 case 'a':
127 /* Ignore -a, for compatibility with SVR4. */
128 break;
129 case 'g':
130 just_group = true;
131 break;
132 case 'n':
133 use_name = true;
134 break;
135 case 'r':
136 use_real = true;
137 break;
138 case 'u':
139 just_user = true;
140 break;
141 case 'G':
142 just_group_list = true;
143 break;
144 case_GETOPT_HELP_CHAR;
145 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
146 default:
147 usage (EXIT_FAILURE);
151 if (just_user + just_group + just_group_list > 1)
152 error (EXIT_FAILURE, 0, _("cannot print only user and only group"));
154 if (just_user + just_group + just_group_list == 0 && (use_real | use_name))
155 error (EXIT_FAILURE, 0,
156 _("cannot print only names or real IDs in default format"));
158 if (argc - optind > 1)
160 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
161 usage (EXIT_FAILURE);
164 if (argc - optind == 1)
166 struct passwd *pwd = getpwnam (argv[optind]);
167 if (pwd == NULL)
168 error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
169 ruid = euid = pwd->pw_uid;
170 rgid = egid = pwd->pw_gid;
172 else
174 euid = geteuid ();
175 ruid = getuid ();
176 egid = getegid ();
177 rgid = getgid ();
180 if (just_user)
181 print_user (use_real ? ruid : euid);
182 else if (just_group)
183 print_group (use_real ? rgid : egid);
184 else if (just_group_list)
185 print_group_list (argv[optind]);
186 else
187 print_full_info (argv[optind]);
188 putchar ('\n');
190 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
193 /* Print the name or value of user ID UID. */
195 static void
196 print_user (uid_t uid)
198 struct passwd *pwd = NULL;
200 if (use_name)
202 pwd = getpwuid (uid);
203 if (pwd == NULL)
205 error (0, 0, _("cannot find name for user ID %lu"),
206 (unsigned long int) uid);
207 ok = false;
211 if (pwd == NULL)
212 printf ("%lu", (unsigned long int) 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)
229 error (0, 0, _("cannot find name for group ID %lu"),
230 (unsigned long int) gid);
231 ok = false;
235 if (grp == NULL)
236 printf ("%lu", (unsigned long int) gid);
237 else
238 printf ("%s", grp->gr_name);
241 #if HAVE_GETGROUPS
243 /* FIXME: document */
245 static bool
246 xgetgroups (const char *username, gid_t gid, int *n_groups,
247 GETGROUPS_T **groups)
249 int max_n_groups;
250 int ng;
251 GETGROUPS_T *g = NULL;
253 if (!username)
254 max_n_groups = getgroups (0, NULL);
255 else
256 max_n_groups = getugroups (0, NULL, username, gid);
258 if (max_n_groups < 0)
259 ng = -1;
260 else
262 g = xnmalloc (max_n_groups, sizeof *g);
263 if (!username)
264 ng = getgroups (max_n_groups, g);
265 else
266 ng = getugroups (max_n_groups, g, username, gid);
269 if (ng < 0)
271 error (0, errno, _("cannot get supplemental group list"));
272 free (g);
273 return false;
275 else
277 *n_groups = ng;
278 *groups = g;
279 return true;
283 #endif /* HAVE_GETGROUPS */
285 /* Print all of the distinct groups the user is in. */
287 static void
288 print_group_list (const char *username)
290 struct passwd *pwd;
292 pwd = getpwuid (ruid);
293 if (pwd == NULL)
294 ok = false;
296 print_group (rgid);
297 if (egid != rgid)
299 putchar (' ');
300 print_group (egid);
303 #if HAVE_GETGROUPS
305 int n_groups;
306 GETGROUPS_T *groups;
307 int i;
309 if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
310 &n_groups, &groups))
312 ok = false;
313 return;
316 for (i = 0; i < n_groups; i++)
317 if (groups[i] != rgid && groups[i] != egid)
319 putchar (' ');
320 print_group (groups[i]);
322 free (groups);
324 #endif /* HAVE_GETGROUPS */
327 /* Print all of the info about the user's user and group IDs. */
329 static void
330 print_full_info (const char *username)
332 struct passwd *pwd;
333 struct group *grp;
335 printf ("uid=%lu", (unsigned long int) ruid);
336 pwd = getpwuid (ruid);
337 if (pwd)
338 printf ("(%s)", pwd->pw_name);
340 printf (" gid=%lu", (unsigned long int) rgid);
341 grp = getgrgid (rgid);
342 if (grp)
343 printf ("(%s)", grp->gr_name);
345 if (euid != ruid)
347 printf (" euid=%lu", (unsigned long int) euid);
348 pwd = getpwuid (euid);
349 if (pwd)
350 printf ("(%s)", pwd->pw_name);
353 if (egid != rgid)
355 printf (" egid=%lu", (unsigned long int) egid);
356 grp = getgrgid (egid);
357 if (grp)
358 printf ("(%s)", grp->gr_name);
361 #if HAVE_GETGROUPS
363 int n_groups;
364 GETGROUPS_T *groups;
365 int i;
367 if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
368 &n_groups, &groups))
370 ok = false;
371 return;
374 if (n_groups > 0)
375 fputs (_(" groups="), stdout);
376 for (i = 0; i < n_groups; i++)
378 if (i > 0)
379 putchar (',');
380 printf ("%lu", (unsigned long int) groups[i]);
381 grp = getgrgid (groups[i]);
382 if (grp)
383 printf ("(%s)", grp->gr_name);
385 free (groups);
387 #endif /* HAVE_GETGROUPS */