Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / id.c
1 /* id -- print real and effective UIDs and GIDs
2    Copyright (C) 1989-2003 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Arnold Robbins.
19    Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
20
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>
28
29 #include "system.h"
30 #include "error.h"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "id"
34
35 #define AUTHORS "Arnold Robbins", "David MacKenzie"
36
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct group *getgrgid ();
40 uid_t getuid ();
41 gid_t getgid ();
42 uid_t geteuid ();
43 gid_t getegid ();
44 #endif /* not _POSIX_VERSION */
45
46 int getugroups ();
47
48 static void print_user (uid_t uid);
49 static void print_group (gid_t gid);
50 static void print_group_list (const char *username);
51 static void print_full_info (const char *username);
52
53 /* The name this program was run with. */
54 char *program_name;
55
56 /* If nonzero, output user/group name instead of ID number. -n */
57 static int use_name = 0;
58
59 /* The real and effective IDs of the user to print. */
60 static uid_t ruid, euid;
61 static gid_t rgid, egid;
62
63 /* The number of errors encountered so far. */
64 static int problems = 0;
65
66 static struct option const longopts[] =
67 {
68   {"group", no_argument, NULL, 'g'},
69   {"groups", no_argument, NULL, 'G'},
70   {"name", no_argument, NULL, 'n'},
71   {"real", no_argument, NULL, 'r'},
72   {"user", no_argument, NULL, 'u'},
73   {GETOPT_HELP_OPTION_DECL},
74   {GETOPT_VERSION_OPTION_DECL},
75   {NULL, 0, NULL, 0}
76 };
77
78 void
79 usage (int status)
80 {
81   if (status != 0)
82     fprintf (stderr, _("Try `%s --help' for more information.\n"),
83              program_name);
84   else
85     {
86       printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
87       fputs (_("\
88 Print information for USERNAME, or the current user.\n\
89 \n\
90   -a              ignore, for compatibility with other versions\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 (_("\
100 \n\
101 Without any OPTION, print some useful set of identified information.\n\
102 "), stdout);
103       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
104     }
105   exit (status);
106 }
107
108 int
109 main (int argc, char **argv)
110 {
111   int optc;
112
113   /* If nonzero, output the list of all group IDs. -G */
114   int just_group_list = 0;
115   /* If nonzero, output only the group ID(s). -g */
116   int just_group = 0;
117   /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
118   int use_real = 0;
119   /* If nonzero, output only the user ID(s). -u */
120   int just_user = 0;
121
122   initialize_main (&argc, &argv);
123   program_name = argv[0];
124   setlocale (LC_ALL, "");
125   bindtextdomain (PACKAGE, LOCALEDIR);
126   textdomain (PACKAGE);
127
128   atexit (close_stdout);
129
130   while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
131     {
132       switch (optc)
133         {
134         case 0:
135           break;
136         case 'a':
137           /* Ignore -a, for compatibility with SVR4.  */
138           break;
139         case 'g':
140           just_group = 1;
141           break;
142         case 'n':
143           use_name = 1;
144           break;
145         case 'r':
146           use_real = 1;
147           break;
148         case 'u':
149           just_user = 1;
150           break;
151         case 'G':
152           just_group_list = 1;
153           break;
154         case_GETOPT_HELP_CHAR;
155         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
156         default:
157           usage (EXIT_FAILURE);
158         }
159     }
160
161   if (just_user + just_group + just_group_list > 1)
162     error (EXIT_FAILURE, 0, _("cannot print only user and only group"));
163
164   if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
165     error (EXIT_FAILURE, 0,
166            _("cannot print only names or real IDs in default format"));
167
168   if (argc - optind > 1)
169     usage (EXIT_FAILURE);
170
171   if (argc - optind == 1)
172     {
173       struct passwd *pwd = getpwnam (argv[optind]);
174       if (pwd == NULL)
175         error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
176       ruid = euid = pwd->pw_uid;
177       rgid = egid = pwd->pw_gid;
178     }
179   else
180     {
181       euid = geteuid ();
182       ruid = getuid ();
183       egid = getegid ();
184       rgid = getgid ();
185     }
186
187   if (just_user)
188     print_user (use_real ? ruid : euid);
189   else if (just_group)
190     print_group (use_real ? rgid : egid);
191   else if (just_group_list)
192     print_group_list (argv[optind]);
193   else
194     print_full_info (argv[optind]);
195   putchar ('\n');
196
197   exit (problems != 0);
198 }
199
200 /* Print the name or value of user ID UID. */
201
202 static void
203 print_user (uid_t uid)
204 {
205   struct passwd *pwd = NULL;
206
207   if (use_name)
208     {
209       pwd = getpwuid (uid);
210       if (pwd == NULL)
211         {
212           error (0, 0, _("cannot find name for user ID %u"), uid);
213           problems++;
214         }
215     }
216
217   if (pwd == NULL)
218     printf ("%u", (unsigned) uid);
219   else
220     printf ("%s", pwd->pw_name);
221 }
222
223 /* Print the name or value of group ID GID. */
224
225 static void
226 print_group (gid_t gid)
227 {
228   struct group *grp = NULL;
229
230   if (use_name)
231     {
232       grp = getgrgid (gid);
233       if (grp == NULL)
234         {
235           error (0, 0, _("cannot find name for group ID %u"), gid);
236           problems++;
237         }
238     }
239
240   if (grp == NULL)
241     printf ("%u", (unsigned) gid);
242   else
243     printf ("%s", grp->gr_name);
244 }
245
246 #if HAVE_GETGROUPS
247
248 /* FIXME: document */
249
250 static int
251 xgetgroups (const char *username, gid_t gid, int *n_groups,
252             GETGROUPS_T **groups)
253 {
254   int max_n_groups;
255   int ng;
256   GETGROUPS_T *g;
257   int fail = 0;
258
259   if (username == 0)
260     max_n_groups = getgroups (0, NULL);
261   else
262     max_n_groups = getugroups (0, NULL, username, gid);
263
264   /* Add 1 just in case max_n_groups is zero.  */
265   g = xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
266   if (username == 0)
267     ng = getgroups (max_n_groups, g);
268   else
269     ng = getugroups (max_n_groups, g, username, gid);
270
271   if (ng < 0)
272     {
273       error (0, errno, _("cannot get supplemental group list"));
274       ++fail;
275       free (groups);
276     }
277   if (!fail)
278     {
279       *n_groups = ng;
280       *groups = g;
281     }
282   return fail;
283 }
284
285 #endif /* HAVE_GETGROUPS */
286
287 /* Print all of the distinct groups the user is in. */
288
289 static void
290 print_group_list (const char *username)
291 {
292   struct passwd *pwd;
293
294   pwd = getpwuid (ruid);
295   if (pwd == NULL)
296     problems++;
297
298   print_group (rgid);
299   if (egid != rgid)
300     {
301       putchar (' ');
302       print_group (egid);
303     }
304
305 #if HAVE_GETGROUPS
306   {
307     int n_groups;
308     GETGROUPS_T *groups;
309     register int i;
310
311     if (xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
312                     &n_groups, &groups))
313       {
314         ++problems;
315         return;
316       }
317
318     for (i = 0; i < n_groups; i++)
319       if (groups[i] != rgid && groups[i] != egid)
320         {
321           putchar (' ');
322           print_group (groups[i]);
323         }
324     free (groups);
325   }
326 #endif /* HAVE_GETGROUPS */
327 }
328
329 /* Print all of the info about the user's user and group IDs. */
330
331 static void
332 print_full_info (const char *username)
333 {
334   struct passwd *pwd;
335   struct group *grp;
336
337   printf ("uid=%u", (unsigned) ruid);
338   pwd = getpwuid (ruid);
339   if (pwd == NULL)
340     problems++;
341   else
342     printf ("(%s)", pwd->pw_name);
343
344   printf (" gid=%u", (unsigned) rgid);
345   grp = getgrgid (rgid);
346   if (grp == NULL)
347     problems++;
348   else
349     printf ("(%s)", grp->gr_name);
350
351   if (euid != ruid)
352     {
353       printf (" euid=%u", (unsigned) euid);
354       pwd = getpwuid (euid);
355       if (pwd == NULL)
356         problems++;
357       else
358         printf ("(%s)", pwd->pw_name);
359     }
360
361   if (egid != rgid)
362     {
363       printf (" egid=%u", (unsigned) egid);
364       grp = getgrgid (egid);
365       if (grp == NULL)
366         problems++;
367       else
368         printf ("(%s)", grp->gr_name);
369     }
370
371 #if HAVE_GETGROUPS
372   {
373     int n_groups;
374     GETGROUPS_T *groups;
375     register int i;
376
377     if (xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
378                     &n_groups, &groups))
379       {
380         ++problems;
381         return;
382       }
383
384     if (n_groups > 0)
385       fputs (_(" groups="), stdout);
386     for (i = 0; i < n_groups; i++)
387       {
388         if (i > 0)
389           putchar (',');
390         printf ("%u", (unsigned) groups[i]);
391         grp = getgrgid (groups[i]);
392         if (grp == NULL)
393           problems++;
394         else
395           printf ("(%s)", grp->gr_name);
396       }
397     free (groups);
398   }
399 #endif /* HAVE_GETGROUPS */
400 }