2 * id - POSIX.2 user identity
4 * (INCOMPLETE -- supplementary groups for other users not yet done)
6 * usage: id [-Ggu] [-nr] [user]
8 * The default output format looks something like:
9 * uid=xxx(chet) gid=xx groups=aa(aname), bb(bname), cc(cname)
13 Copyright (C) 1999-2009 Free Software Foundation, Inc.
15 This file is part of GNU Bash.
16 Bash is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
21 Bash is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Bash. If not, see <http://www.gnu.org/licenses/>.
32 #include "bashtypes.h"
40 # include <sys/param.h>
43 #if !defined (HAVE_GETPW_DECLS)
44 extern struct passwd *getpwuid ();
46 extern struct group *getgrgid ();
52 #include "bashgetopt.h"
54 #define ID_ALLGROUPS 0x001 /* -G */
55 #define ID_GIDONLY 0x002 /* -g */
56 #define ID_USENAME 0x004 /* -n */
57 #define ID_USEREAL 0x008 /* -r */
58 #define ID_USERONLY 0x010 /* -u */
60 #define ID_FLAGSET(s) ((id_flags & (s)) != 0)
64 static uid_t ruid, euid;
65 static gid_t rgid, egid;
69 static int inituser ();
71 static int id_pruser ();
72 static int id_prgrp ();
73 static int id_prgroups ();
74 static int id_prall ();
84 reset_internal_getopt ();
85 while ((opt = internal_getopt (list, "Ggnru")) != -1)
89 case 'G': id_flags |= ID_ALLGROUPS; break;
90 case 'g': id_flags |= ID_GIDONLY; break;
91 case 'n': id_flags |= ID_USENAME; break;
92 case 'r': id_flags |= ID_USEREAL; break;
93 case 'u': id_flags |= ID_USERONLY; break;
101 user = list ? list->word->word : (char *)NULL;
103 /* Check for some invalid option combinations */
104 opt = ID_FLAGSET (ID_ALLGROUPS) + ID_FLAGSET (ID_GIDONLY) + ID_FLAGSET (ID_USERONLY);
105 if (opt > 1 || (opt == 0 && ((id_flags & (ID_USEREAL|ID_USENAME)) != 0)))
111 if (list && list->next)
117 if (inituser (user) < 0)
118 return (EXECUTION_FAILURE);
121 if (id_flags & ID_USERONLY)
122 opt += id_pruser ((id_flags & ID_USEREAL) ? ruid : euid);
123 else if (id_flags & ID_GIDONLY)
124 opt += id_prgrp ((id_flags & ID_USEREAL) ? rgid : egid);
125 else if (id_flags & ID_ALLGROUPS)
126 opt += id_prgroups (user);
128 opt += id_prall (user);
132 return (opt == 0 ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
143 pwd = getpwnam (uname);
146 builtin_error ("%s: no such user", uname);
149 ruid = euid = pwd->pw_uid;
150 rgid = egid = pwd->pw_gid;
154 ruid = current_user.uid;
155 euid = current_user.euid;
156 rgid = current_user.gid;
157 egid = current_user.egid;
162 /* Print the name or value of user ID UID. */
167 struct passwd *pwd = NULL;
171 if (id_flags & ID_USENAME)
173 pwd = getpwuid (uid);
178 printf ("%s", pwd->pw_name);
180 printf ("%u", (unsigned) uid);
185 /* Print the name or value of group ID GID. */
191 struct group *grp = NULL;
195 if (id_flags & ID_USENAME)
197 grp = getgrgid (gid);
203 printf ("%s", grp->gr_name);
205 printf ("%u", (unsigned) gid);
214 int *glist, ng, i, r;
226 builtin_error ("supplementary groups for other users not yet implemented");
232 glist = get_group_array (&ng);
234 for (i = 0; i < ng; i++)
235 if (glist[i] != rgid && glist[i] != egid)
248 int r, i, ng, *glist;
253 printf ("uid=%u", (unsigned) ruid);
254 pwd = getpwuid (ruid);
258 printf ("(%s)", pwd->pw_name);
260 printf (" gid=%u", (unsigned) rgid);
261 grp = getgrgid (rgid);
265 printf ("(%s)", grp->gr_name);
269 printf (" euid=%u", (unsigned) euid);
270 pwd = getpwuid (euid);
274 printf ("(%s)", pwd->pw_name);
279 printf (" egid=%u", (unsigned) egid);
280 grp = getgrgid (egid);
284 printf ("(%s)", grp->gr_name);
289 builtin_error ("supplementary groups for other users not yet implemented");
295 glist = get_group_array (&ng);
299 for (i = 0; i < ng; i++)
303 printf ("%u", (unsigned) glist[i]);
304 grp = getgrgid (glist[i]);
308 printf ("(%s)", grp->gr_name);
315 "Display information about user."
317 "Return information about user identity",
321 struct builtin id_struct = {
326 "id [user]\n\tid -G [-n] [user]\n\tid -g [-nr] [user]\n\tid -u [-nr] [user]",