Patch-ID: bash40-021
[bash.git] / examples / loadables / id.c
blob8773349413a451aad4d78402c5d2a0c6bea4ad0e
1 /*
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/>.
30 #include <config.h>
31 #include <stdio.h>
32 #include "bashtypes.h"
33 #include <pwd.h>
34 #include <grp.h>
35 #include "bashansi.h"
37 #ifdef HAVE_LIMITS_H
38 # include <limits.h>
39 #else
40 # include <sys/param.h>
41 #endif
43 #if !defined (HAVE_GETPW_DECLS)
44 extern struct passwd *getpwuid ();
45 #endif
46 extern struct group *getgrgid ();
48 #include "shell.h"
49 #include "builtins.h"
50 #include "stdc.h"
51 #include "common.h"
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)
62 static int id_flags;
64 static uid_t ruid, euid;
65 static gid_t rgid, egid;
67 static char *id_user;
69 static int inituser ();
71 static int id_pruser ();
72 static int id_prgrp ();
73 static int id_prgroups ();
74 static int id_prall ();
76 int
77 id_builtin (list)
78 WORD_LIST *list;
80 int opt;
81 char *user;
83 id_flags = 0;
84 reset_internal_getopt ();
85 while ((opt = internal_getopt (list, "Ggnru")) != -1)
87 switch (opt)
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;
94 default:
95 builtin_usage ();
96 return (EX_USAGE);
99 list = loptend;
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)))
107 builtin_usage ();
108 return (EX_USAGE);
111 if (list && list->next)
113 builtin_usage ();
114 return (EX_USAGE);
117 if (inituser (user) < 0)
118 return (EXECUTION_FAILURE);
120 opt = 0;
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);
127 else
128 opt += id_prall (user);
129 putchar ('\n');
130 fflush (stdout);
132 return (opt == 0 ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
135 static int
136 inituser (uname)
137 char *uname;
139 struct passwd *pwd;
141 if (uname)
143 pwd = getpwnam (uname);
144 if (pwd == 0)
146 builtin_error ("%s: no such user", uname);
147 return -1;
149 ruid = euid = pwd->pw_uid;
150 rgid = egid = pwd->pw_gid;
152 else
154 ruid = current_user.uid;
155 euid = current_user.euid;
156 rgid = current_user.gid;
157 egid = current_user.egid;
159 return 0;
162 /* Print the name or value of user ID UID. */
163 static int
164 id_pruser (uid)
165 int uid;
167 struct passwd *pwd = NULL;
168 int r;
170 r = 0;
171 if (id_flags & ID_USENAME)
173 pwd = getpwuid (uid);
174 if (pwd == NULL)
175 r = 1;
177 if (pwd)
178 printf ("%s", pwd->pw_name);
179 else
180 printf ("%u", (unsigned) uid);
182 return r;
185 /* Print the name or value of group ID GID. */
187 static int
188 id_prgrp (gid)
189 int gid;
191 struct group *grp = NULL;
192 int r;
194 r = 0;
195 if (id_flags & ID_USENAME)
197 grp = getgrgid (gid);
198 if (grp == NULL)
199 r = 1;
202 if (grp)
203 printf ("%s", grp->gr_name);
204 else
205 printf ("%u", (unsigned) gid);
207 return r;
210 static int
211 id_prgroups (uname)
212 char *uname;
214 int *glist, ng, i, r;
216 r = 0;
217 id_prgrp (rgid);
218 if (egid != rgid)
220 putchar (' ');
221 id_prgrp (egid);
224 if (uname)
226 builtin_error ("supplementary groups for other users not yet implemented");
227 glist = (int *)NULL;
228 ng = 0;
229 r = 1;
231 else
232 glist = get_group_array (&ng);
234 for (i = 0; i < ng; i++)
235 if (glist[i] != rgid && glist[i] != egid)
237 putchar (' ');
238 id_prgrp (glist[i]);
241 return r;
244 static int
245 id_prall (uname)
246 char *uname;
248 int r, i, ng, *glist;
249 struct passwd *pwd;
250 struct group *grp;
252 r = 0;
253 printf ("uid=%u", (unsigned) ruid);
254 pwd = getpwuid (ruid);
255 if (pwd == NULL)
256 r = 1;
257 else
258 printf ("(%s)", pwd->pw_name);
260 printf (" gid=%u", (unsigned) rgid);
261 grp = getgrgid (rgid);
262 if (grp == NULL)
263 r = 1;
264 else
265 printf ("(%s)", grp->gr_name);
267 if (euid != ruid)
269 printf (" euid=%u", (unsigned) euid);
270 pwd = getpwuid (euid);
271 if (pwd == NULL)
272 r = 1;
273 else
274 printf ("(%s)", pwd->pw_name);
277 if (egid != rgid)
279 printf (" egid=%u", (unsigned) egid);
280 grp = getgrgid (egid);
281 if (grp == NULL)
282 r = 1;
283 else
284 printf ("(%s)", grp->gr_name);
287 if (uname)
289 builtin_error ("supplementary groups for other users not yet implemented");
290 glist = (int *)NULL;
291 ng = 0;
292 r = 1;
294 else
295 glist = get_group_array (&ng);
297 if (ng > 0)
298 printf (" groups=");
299 for (i = 0; i < ng; i++)
301 if (i > 0)
302 printf (", ");
303 printf ("%u", (unsigned) glist[i]);
304 grp = getgrgid (glist[i]);
305 if (grp == NULL)
306 r = 1;
307 else
308 printf ("(%s)", grp->gr_name);
311 return r;
314 char *id_doc[] = {
315 "Display information about user."
317 "Return information about user identity",
318 (char *)NULL
321 struct builtin id_struct = {
322 "id",
323 id_builtin,
324 BUILTIN_ENABLED,
325 id_doc,
326 "id [user]\n\tid -G [-n] [user]\n\tid -g [-nr] [user]\n\tid -u [-nr] [user]",