df: fix mount list processing with unstatable mount dirs
[coreutils.git] / src / id.c
blob3e7016f7b34235bbf5e744f7065b5640752befb7
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2013 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Arnold Robbins.
18 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <getopt.h>
26 #include <selinux/selinux.h>
28 #include "system.h"
29 #include "error.h"
30 #include "mgetgroups.h"
31 #include "quote.h"
32 #include "group-list.h"
33 #include "smack.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "id"
38 #define AUTHORS \
39 proper_name ("Arnold Robbins"), \
40 proper_name ("David MacKenzie")
42 /* If nonzero, output only the SELinux context. -Z */
43 static int just_context = 0;
45 static void print_user (uid_t uid);
46 static void print_full_info (const char *username);
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 /* The SELinux context. Start with a known invalid value so print_full_info
59 knows when 'context' has not been set to a meaningful value. */
60 static security_context_t context = NULL;
62 static struct option const longopts[] =
64 {"context", no_argument, NULL, 'Z'},
65 {"group", no_argument, NULL, 'g'},
66 {"groups", no_argument, NULL, 'G'},
67 {"name", no_argument, NULL, 'n'},
68 {"real", no_argument, NULL, 'r'},
69 {"user", no_argument, NULL, 'u'},
70 {GETOPT_HELP_OPTION_DECL},
71 {GETOPT_VERSION_OPTION_DECL},
72 {NULL, 0, NULL, 0}
75 void
76 usage (int status)
78 if (status != EXIT_SUCCESS)
79 emit_try_help ();
80 else
82 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
83 fputs (_("\
84 Print user and group information for the specified USERNAME,\n\
85 or (when USERNAME omitted) for the current user.\n\
86 \n\
87 -a ignore, for compatibility with other versions\n\
88 -Z, --context print only the security context of the current user\n\
89 -g, --group print only the effective group ID\n\
90 -G, --groups print all group IDs\n\
91 -n, --name print a name instead of a number, for -ugG\n\
92 -r, --real print the real ID instead of the effective ID, with -ugG\n\
93 -u, --user print only the effective user ID\n\
94 "), stdout);
95 fputs (HELP_OPTION_DESCRIPTION, stdout);
96 fputs (VERSION_OPTION_DESCRIPTION, stdout);
97 fputs (_("\
98 \n\
99 Without any OPTION, print some useful set of identified information.\n\
100 "), stdout);
101 emit_ancillary_info ();
103 exit (status);
107 main (int argc, char **argv)
109 int optc;
110 int selinux_enabled = (is_selinux_enabled () > 0);
111 bool smack_enabled = is_smack_enabled ();
113 /* If true, output the list of all group IDs. -G */
114 bool just_group_list = false;
115 /* If true, output only the group ID(s). -g */
116 bool just_group = false;
117 /* If true, output real UID/GID instead of default effective UID/GID. -r */
118 bool use_real = false;
119 /* If true, output only the user ID(s). -u */
120 bool just_user = false;
122 initialize_main (&argc, &argv);
123 set_program_name (argv[0]);
124 setlocale (LC_ALL, "");
125 bindtextdomain (PACKAGE, LOCALEDIR);
126 textdomain (PACKAGE);
128 atexit (close_stdout);
130 while ((optc = getopt_long (argc, argv, "agnruGZ", longopts, NULL)) != -1)
132 switch (optc)
134 case 'a':
135 /* Ignore -a, for compatibility with SVR4. */
136 break;
138 case 'Z':
139 /* politely decline if we're not on a SELinux/SMACK-enabled kernel. */
140 #ifdef HAVE_SMACK
141 if (!selinux_enabled && !smack_enabled)
142 error (EXIT_FAILURE, 0,
143 _("--context (-Z) works only on "
144 "an SELinux/SMACK-enabled kernel"));
145 #else
146 if (!selinux_enabled)
147 error (EXIT_FAILURE, 0,
148 _("--context (-Z) works only on an SELinux-enabled kernel"));
149 #endif
150 just_context = 1;
151 break;
153 case 'g':
154 just_group = true;
155 break;
156 case 'n':
157 use_name = true;
158 break;
159 case 'r':
160 use_real = true;
161 break;
162 case 'u':
163 just_user = true;
164 break;
165 case 'G':
166 just_group_list = true;
167 break;
168 case_GETOPT_HELP_CHAR;
169 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
170 default:
171 usage (EXIT_FAILURE);
175 size_t n_ids = argc - optind;
176 if (1 < n_ids)
178 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
179 usage (EXIT_FAILURE);
182 if (n_ids && just_context)
183 error (EXIT_FAILURE, 0,
184 _("cannot print security context when user specified"));
186 if (just_user + just_group + just_group_list + just_context > 1)
187 error (EXIT_FAILURE, 0, _("cannot print \"only\" of more than one choice"));
189 bool default_format = (just_user + just_group + just_group_list
190 + just_context == 0);
192 if (default_format && (use_real || use_name))
193 error (EXIT_FAILURE, 0,
194 _("cannot print only names or real IDs in default format"));
196 /* If we are on a SELinux/SMACK-enabled kernel, no user is specified, and
197 either --context is specified or none of (-u,-g,-G) is specified,
198 and we're not in POSIXLY_CORRECT mode, get our context. Otherwise,
199 leave the context variable alone - it has been initialized to an
200 invalid value that will be not displayed in print_full_info(). */
201 if (n_ids == 0
202 && (just_context
203 || (default_format && ! getenv ("POSIXLY_CORRECT"))))
205 /* Report failure only if --context (-Z) was explicitly requested. */
206 if ((selinux_enabled && getcon (&context) && just_context)
207 || (smack_enabled
208 && smack_new_label_from_self ((char **) &context) < 0
209 && just_context))
210 error (EXIT_FAILURE, 0, _("can't get process context"));
213 if (n_ids == 1)
215 struct passwd *pwd = getpwnam (argv[optind]);
216 if (pwd == NULL)
217 error (EXIT_FAILURE, 0, _("%s: no such user"), argv[optind]);
218 ruid = euid = pwd->pw_uid;
219 rgid = egid = pwd->pw_gid;
221 else
223 /* POSIX says identification functions (getuid, getgid, and
224 others) cannot fail, but they can fail under GNU/Hurd and a
225 few other systems. Test for failure by checking errno. */
226 uid_t NO_UID = -1;
227 gid_t NO_GID = -1;
229 if (just_user ? !use_real
230 : !just_group && !just_group_list && !just_context)
232 errno = 0;
233 euid = geteuid ();
234 if (euid == NO_UID && errno)
235 error (EXIT_FAILURE, errno, _("cannot get effective UID"));
238 if (just_user ? use_real
239 : !just_group && (just_group_list || !just_context))
241 errno = 0;
242 ruid = getuid ();
243 if (ruid == NO_UID && errno)
244 error (EXIT_FAILURE, errno, _("cannot get real UID"));
247 if (!just_user && (just_group || just_group_list || !just_context))
249 errno = 0;
250 egid = getegid ();
251 if (egid == NO_GID && errno)
252 error (EXIT_FAILURE, errno, _("cannot get effective GID"));
254 errno = 0;
255 rgid = getgid ();
256 if (rgid == NO_GID && errno)
257 error (EXIT_FAILURE, errno, _("cannot get real GID"));
261 if (just_user)
263 print_user (use_real ? ruid : euid);
265 else if (just_group)
267 if (!print_group (use_real ? rgid : egid, use_name))
268 ok = false;
270 else if (just_group_list)
272 if (!print_group_list (argv[optind], ruid, rgid, egid, use_name))
273 ok = false;
275 else if (just_context)
277 fputs (context, stdout);
279 else
281 print_full_info (argv[optind]);
283 putchar ('\n');
285 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
288 /* Convert a gid_t to string. Do not use this function directly.
289 Instead, use it via the gidtostr macro.
290 Beware that it returns a pointer to static storage. */
291 static char *
292 gidtostr_ptr (gid_t const *gid)
294 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];
295 return umaxtostr (*gid, buf);
297 #define gidtostr(g) gidtostr_ptr (&(g))
299 /* Convert a uid_t to string. Do not use this function directly.
300 Instead, use it via the uidtostr macro.
301 Beware that it returns a pointer to static storage. */
302 static char *
303 uidtostr_ptr (uid_t const *uid)
305 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];
306 return umaxtostr (*uid, buf);
308 #define uidtostr(u) uidtostr_ptr (&(u))
310 /* Print the name or value of user ID UID. */
312 static void
313 print_user (uid_t uid)
315 struct passwd *pwd = NULL;
317 if (use_name)
319 pwd = getpwuid (uid);
320 if (pwd == NULL)
322 error (0, 0, _("cannot find name for user ID %s"),
323 uidtostr (uid));
324 ok = false;
328 char *s = pwd ? pwd->pw_name : uidtostr (uid);
329 fputs (s, stdout);
332 /* Print all of the info about the user's user and group IDs. */
334 static void
335 print_full_info (const char *username)
337 struct passwd *pwd;
338 struct group *grp;
340 printf (_("uid=%s"), uidtostr (ruid));
341 pwd = getpwuid (ruid);
342 if (pwd)
343 printf ("(%s)", pwd->pw_name);
345 printf (_(" gid=%s"), gidtostr (rgid));
346 grp = getgrgid (rgid);
347 if (grp)
348 printf ("(%s)", grp->gr_name);
350 if (euid != ruid)
352 printf (_(" euid=%s"), uidtostr (euid));
353 pwd = getpwuid (euid);
354 if (pwd)
355 printf ("(%s)", pwd->pw_name);
358 if (egid != rgid)
360 printf (_(" egid=%s"), gidtostr (egid));
361 grp = getgrgid (egid);
362 if (grp)
363 printf ("(%s)", grp->gr_name);
367 gid_t *groups;
368 int i;
370 int n_groups = xgetgroups (username, (pwd ? pwd->pw_gid : -1),
371 &groups);
372 if (n_groups < 0)
374 if (username)
376 error (0, errno, _("failed to get groups for user %s"),
377 quote (username));
379 else
381 error (0, errno, _("failed to get groups for the current process"));
383 ok = false;
384 return;
387 if (n_groups > 0)
388 fputs (_(" groups="), stdout);
389 for (i = 0; i < n_groups; i++)
391 if (i > 0)
392 putchar (',');
393 fputs (gidtostr (groups[i]), stdout);
394 grp = getgrgid (groups[i]);
395 if (grp)
396 printf ("(%s)", grp->gr_name);
398 free (groups);
401 /* POSIX mandates the precise output format, and that it not include
402 any context=... part, so skip that if POSIXLY_CORRECT is set. */
403 if (context)
404 printf (_(" context=%s"), context);