id: adjust/restrict smack support to newer versions of libsmack
[coreutils.git] / src / id.c
blobc91dbcdbc6edd05e5191271848a70f8d997c9a56
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>
27 #ifdef HAVE_SMACK
28 # include <sys/smack.h>
29 #endif
31 #include "system.h"
32 #include "error.h"
33 #include "mgetgroups.h"
34 #include "quote.h"
35 #include "group-list.h"
37 /* The official name of this program (e.g., no 'g' prefix). */
38 #define PROGRAM_NAME "id"
40 #define AUTHORS \
41 proper_name ("Arnold Robbins"), \
42 proper_name ("David MacKenzie")
44 /* If nonzero, output only the SELinux context. -Z */
45 static int just_context = 0;
47 static void print_user (uid_t uid);
48 static void print_full_info (const char *username);
50 /* If true, output user/group name instead of ID number. -n */
51 static bool use_name = false;
53 /* The real and effective IDs of the user to print. */
54 static uid_t ruid, euid;
55 static gid_t rgid, egid;
57 /* True unless errors have been encountered. */
58 static bool ok = true;
60 /* The SELinux context. Start with a known invalid value so print_full_info
61 knows when 'context' has not been set to a meaningful value. */
62 static security_context_t context = NULL;
64 static struct option const longopts[] =
66 {"context", no_argument, NULL, 'Z'},
67 {"group", no_argument, NULL, 'g'},
68 {"groups", no_argument, NULL, 'G'},
69 {"name", no_argument, NULL, 'n'},
70 {"real", no_argument, NULL, 'r'},
71 {"user", no_argument, NULL, 'u'},
72 {GETOPT_HELP_OPTION_DECL},
73 {GETOPT_VERSION_OPTION_DECL},
74 {NULL, 0, NULL, 0}
77 void
78 usage (int status)
80 if (status != EXIT_SUCCESS)
81 emit_try_help ();
82 else
84 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
85 fputs (_("\
86 Print user and group information for the specified USERNAME,\n\
87 or (when USERNAME omitted) for the current user.\n\
88 \n\
89 -a ignore, for compatibility with other versions\n\
90 -Z, --context print only the security context of the current user\n\
91 -g, --group print only the effective group ID\n\
92 -G, --groups print all group IDs\n\
93 -n, --name print a name instead of a number, for -ugG\n\
94 -r, --real print the real ID instead of the effective ID, with -ugG\n\
95 -u, --user print only the effective user ID\n\
96 "), stdout);
97 fputs (HELP_OPTION_DESCRIPTION, stdout);
98 fputs (VERSION_OPTION_DESCRIPTION, stdout);
99 fputs (_("\
101 Without any OPTION, print some useful set of identified information.\n\
102 "), stdout);
103 emit_ancillary_info ();
105 exit (status);
109 main (int argc, char **argv)
111 int optc;
112 int selinux_enabled = (is_selinux_enabled () > 0);
113 #ifdef HAVE_SMACK
114 int smack_enabled = (smack_smackfs_path () != NULL);
115 #endif
117 /* If true, output the list of all group IDs. -G */
118 bool just_group_list = false;
119 /* If true, output only the group ID(s). -g */
120 bool just_group = false;
121 /* If true, output real UID/GID instead of default effective UID/GID. -r */
122 bool use_real = false;
123 /* If true, output only the user ID(s). -u */
124 bool just_user = false;
126 initialize_main (&argc, &argv);
127 set_program_name (argv[0]);
128 setlocale (LC_ALL, "");
129 bindtextdomain (PACKAGE, LOCALEDIR);
130 textdomain (PACKAGE);
132 atexit (close_stdout);
134 while ((optc = getopt_long (argc, argv, "agnruGZ", longopts, NULL)) != -1)
136 switch (optc)
138 case 'a':
139 /* Ignore -a, for compatibility with SVR4. */
140 break;
142 case 'Z':
143 /* politely decline if we're not on a SELinux/SMACK-enabled kernel. */
144 #ifdef HAVE_SMACK
145 if (!selinux_enabled && !smack_enabled)
146 error (EXIT_FAILURE, 0,
147 _("--context (-Z) works only on "
148 "an SELinux/SMACK-enabled kernel"));
149 #else
150 if (!selinux_enabled)
151 error (EXIT_FAILURE, 0,
152 _("--context (-Z) works only on an SELinux-enabled kernel"));
153 #endif
154 just_context = 1;
155 break;
157 case 'g':
158 just_group = true;
159 break;
160 case 'n':
161 use_name = true;
162 break;
163 case 'r':
164 use_real = true;
165 break;
166 case 'u':
167 just_user = true;
168 break;
169 case 'G':
170 just_group_list = true;
171 break;
172 case_GETOPT_HELP_CHAR;
173 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
174 default:
175 usage (EXIT_FAILURE);
179 size_t n_ids = argc - optind;
180 if (1 < n_ids)
182 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
183 usage (EXIT_FAILURE);
186 if (n_ids && just_context)
187 error (EXIT_FAILURE, 0,
188 _("cannot print security context when user specified"));
190 if (just_user + just_group + just_group_list + just_context > 1)
191 error (EXIT_FAILURE, 0, _("cannot print \"only\" of more than one choice"));
193 bool default_format = (just_user + just_group + just_group_list
194 + just_context == 0);
196 if (default_format && (use_real || use_name))
197 error (EXIT_FAILURE, 0,
198 _("cannot print only names or real IDs in default format"));
200 /* If we are on a selinux-enabled kernel, no user is specified, and
201 either --context is specified or none of (-u,-g,-G) is specified,
202 and we're not in POSIXLY_CORRECT mode, get our context. Otherwise,
203 leave the context variable alone - it has been initialized to an
204 invalid value that will be not displayed in print_full_info(). */
205 if (n_ids == 0
206 && (just_context
207 || (default_format && ! getenv ("POSIXLY_CORRECT"))))
209 /* Report failure only if --context (-Z) was explicitly requested. */
210 if (selinux_enabled && getcon (&context) && just_context)
211 error (EXIT_FAILURE, 0, _("can't get process context"));
212 #ifdef HAVE_SMACK
213 else if (smack_enabled
214 && smack_new_label_from_self ((char **) &context) < 0)
215 error (EXIT_FAILURE, 0, _("can't get process context"));
216 #endif
219 if (n_ids == 1)
221 struct passwd *pwd = getpwnam (argv[optind]);
222 if (pwd == NULL)
223 error (EXIT_FAILURE, 0, _("%s: no such user"), argv[optind]);
224 ruid = euid = pwd->pw_uid;
225 rgid = egid = pwd->pw_gid;
227 else
229 /* POSIX says identification functions (getuid, getgid, and
230 others) cannot fail, but they can fail under GNU/Hurd and a
231 few other systems. Test for failure by checking errno. */
232 uid_t NO_UID = -1;
233 gid_t NO_GID = -1;
235 if (just_user ? !use_real
236 : !just_group && !just_group_list && !just_context)
238 errno = 0;
239 euid = geteuid ();
240 if (euid == NO_UID && errno)
241 error (EXIT_FAILURE, errno, _("cannot get effective UID"));
244 if (just_user ? use_real
245 : !just_group && (just_group_list || !just_context))
247 errno = 0;
248 ruid = getuid ();
249 if (ruid == NO_UID && errno)
250 error (EXIT_FAILURE, errno, _("cannot get real UID"));
253 if (!just_user && (just_group || just_group_list || !just_context))
255 errno = 0;
256 egid = getegid ();
257 if (egid == NO_GID && errno)
258 error (EXIT_FAILURE, errno, _("cannot get effective GID"));
260 errno = 0;
261 rgid = getgid ();
262 if (rgid == NO_GID && errno)
263 error (EXIT_FAILURE, errno, _("cannot get real GID"));
267 if (just_user)
269 print_user (use_real ? ruid : euid);
271 else if (just_group)
273 if (!print_group (use_real ? rgid : egid, use_name))
274 ok = false;
276 else if (just_group_list)
278 if (!print_group_list (argv[optind], ruid, rgid, egid, use_name))
279 ok = false;
281 else if (just_context)
283 fputs (context, stdout);
285 else
287 print_full_info (argv[optind]);
289 putchar ('\n');
291 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
294 /* Convert a gid_t to string. Do not use this function directly.
295 Instead, use it via the gidtostr macro.
296 Beware that it returns a pointer to static storage. */
297 static char *
298 gidtostr_ptr (gid_t const *gid)
300 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];
301 return umaxtostr (*gid, buf);
303 #define gidtostr(g) gidtostr_ptr (&(g))
305 /* Convert a uid_t to string. Do not use this function directly.
306 Instead, use it via the uidtostr macro.
307 Beware that it returns a pointer to static storage. */
308 static char *
309 uidtostr_ptr (uid_t const *uid)
311 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];
312 return umaxtostr (*uid, buf);
314 #define uidtostr(u) uidtostr_ptr (&(u))
316 /* Print the name or value of user ID UID. */
318 static void
319 print_user (uid_t uid)
321 struct passwd *pwd = NULL;
323 if (use_name)
325 pwd = getpwuid (uid);
326 if (pwd == NULL)
328 error (0, 0, _("cannot find name for user ID %s"),
329 uidtostr (uid));
330 ok = false;
334 char *s = pwd ? pwd->pw_name : uidtostr (uid);
335 fputs (s, stdout);
338 /* Print all of the info about the user's user and group IDs. */
340 static void
341 print_full_info (const char *username)
343 struct passwd *pwd;
344 struct group *grp;
346 printf (_("uid=%s"), uidtostr (ruid));
347 pwd = getpwuid (ruid);
348 if (pwd)
349 printf ("(%s)", pwd->pw_name);
351 printf (_(" gid=%s"), gidtostr (rgid));
352 grp = getgrgid (rgid);
353 if (grp)
354 printf ("(%s)", grp->gr_name);
356 if (euid != ruid)
358 printf (_(" euid=%s"), uidtostr (euid));
359 pwd = getpwuid (euid);
360 if (pwd)
361 printf ("(%s)", pwd->pw_name);
364 if (egid != rgid)
366 printf (_(" egid=%s"), gidtostr (egid));
367 grp = getgrgid (egid);
368 if (grp)
369 printf ("(%s)", grp->gr_name);
373 gid_t *groups;
374 int i;
376 int n_groups = xgetgroups (username, (pwd ? pwd->pw_gid : -1),
377 &groups);
378 if (n_groups < 0)
380 if (username)
382 error (0, errno, _("failed to get groups for user %s"),
383 quote (username));
385 else
387 error (0, errno, _("failed to get groups for the current process"));
389 ok = false;
390 return;
393 if (n_groups > 0)
394 fputs (_(" groups="), stdout);
395 for (i = 0; i < n_groups; i++)
397 if (i > 0)
398 putchar (',');
399 fputs (gidtostr (groups[i]), stdout);
400 grp = getgrgid (groups[i]);
401 if (grp)
402 printf ("(%s)", grp->gr_name);
404 free (groups);
407 /* POSIX mandates the precise output format, and that it not include
408 any context=... part, so skip that if POSIXLY_CORRECT is set. */
409 if (context)
410 printf (_(" context=%s"), context);