1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2005 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Arnold Robbins.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
24 #include <sys/types.h>
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "id"
36 #define AUTHORS "Arnold Robbins", "David MacKenzie"
40 static void print_user (uid_t uid
);
41 static void print_group (gid_t gid
);
42 static void print_group_list (const char *username
);
43 static void print_full_info (const char *username
);
45 /* The name this program was run with. */
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 static struct option
const longopts
[] =
60 {"group", no_argument
, NULL
, 'g'},
61 {"groups", no_argument
, NULL
, 'G'},
62 {"name", no_argument
, NULL
, 'n'},
63 {"real", no_argument
, NULL
, 'r'},
64 {"user", no_argument
, NULL
, 'u'},
65 {GETOPT_HELP_OPTION_DECL
},
66 {GETOPT_VERSION_OPTION_DECL
},
73 if (status
!= EXIT_SUCCESS
)
74 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
78 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name
);
80 Print information for USERNAME, or the current user.\n\
82 -a ignore, for compatibility with other versions\n\
83 -g, --group print only the effective group ID\n\
84 -G, --groups print all group IDs\n\
85 -n, --name print a name instead of a number, for -ugG\n\
86 -r, --real print the real ID instead of the effective ID, with -ugG\n\
87 -u, --user print only the effective user ID\n\
89 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
90 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
93 Without any OPTION, print some useful set of identified information.\n\
95 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
101 main (int argc
, char **argv
)
105 /* If true, output the list of all group IDs. -G */
106 bool just_group_list
= false;
107 /* If true, output only the group ID(s). -g */
108 bool just_group
= false;
109 /* If true, output real UID/GID instead of default effective UID/GID. -r */
110 bool use_real
= false;
111 /* If true, output only the user ID(s). -u */
112 bool just_user
= false;
114 initialize_main (&argc
, &argv
);
115 program_name
= argv
[0];
116 setlocale (LC_ALL
, "");
117 bindtextdomain (PACKAGE
, LOCALEDIR
);
118 textdomain (PACKAGE
);
120 atexit (close_stdout
);
122 while ((optc
= getopt_long (argc
, argv
, "agnruG", longopts
, NULL
)) != -1)
127 /* Ignore -a, for compatibility with SVR4. */
142 just_group_list
= true;
144 case_GETOPT_HELP_CHAR
;
145 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
147 usage (EXIT_FAILURE
);
151 if (just_user
+ just_group
+ just_group_list
> 1)
152 error (EXIT_FAILURE
, 0, _("cannot print only user and only group"));
154 if (just_user
+ just_group
+ just_group_list
== 0 && (use_real
| use_name
))
155 error (EXIT_FAILURE
, 0,
156 _("cannot print only names or real IDs in default format"));
158 if (argc
- optind
> 1)
160 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
161 usage (EXIT_FAILURE
);
164 if (argc
- optind
== 1)
166 struct passwd
*pwd
= getpwnam (argv
[optind
]);
168 error (EXIT_FAILURE
, 0, _("%s: No such user"), argv
[optind
]);
169 ruid
= euid
= pwd
->pw_uid
;
170 rgid
= egid
= pwd
->pw_gid
;
181 print_user (use_real
? ruid
: euid
);
183 print_group (use_real
? rgid
: egid
);
184 else if (just_group_list
)
185 print_group_list (argv
[optind
]);
187 print_full_info (argv
[optind
]);
190 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);
193 /* Print the name or value of user ID UID. */
196 print_user (uid_t uid
)
198 struct passwd
*pwd
= NULL
;
202 pwd
= getpwuid (uid
);
205 error (0, 0, _("cannot find name for user ID %lu"),
206 (unsigned long int) uid
);
212 printf ("%lu", (unsigned long int) uid
);
214 printf ("%s", pwd
->pw_name
);
217 /* Print the name or value of group ID GID. */
220 print_group (gid_t gid
)
222 struct group
*grp
= NULL
;
226 grp
= getgrgid (gid
);
229 error (0, 0, _("cannot find name for group ID %lu"),
230 (unsigned long int) gid
);
236 printf ("%lu", (unsigned long int) gid
);
238 printf ("%s", grp
->gr_name
);
243 /* FIXME: document */
246 xgetgroups (const char *username
, gid_t gid
, int *n_groups
,
247 GETGROUPS_T
**groups
)
251 GETGROUPS_T
*g
= NULL
;
254 max_n_groups
= getgroups (0, NULL
);
256 max_n_groups
= getugroups (0, NULL
, username
, gid
);
258 if (max_n_groups
< 0)
262 g
= xnmalloc (max_n_groups
, sizeof *g
);
264 ng
= getgroups (max_n_groups
, g
);
266 ng
= getugroups (max_n_groups
, g
, username
, gid
);
271 error (0, errno
, _("cannot get supplemental group list"));
283 #endif /* HAVE_GETGROUPS */
285 /* Print all of the distinct groups the user is in. */
288 print_group_list (const char *username
)
292 pwd
= getpwuid (ruid
);
309 if (! xgetgroups (username
, (pwd
? pwd
->pw_gid
: (gid_t
) -1),
316 for (i
= 0; i
< n_groups
; i
++)
317 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
320 print_group (groups
[i
]);
324 #endif /* HAVE_GETGROUPS */
327 /* Print all of the info about the user's user and group IDs. */
330 print_full_info (const char *username
)
335 printf ("uid=%lu", (unsigned long int) ruid
);
336 pwd
= getpwuid (ruid
);
338 printf ("(%s)", pwd
->pw_name
);
340 printf (" gid=%lu", (unsigned long int) rgid
);
341 grp
= getgrgid (rgid
);
343 printf ("(%s)", grp
->gr_name
);
347 printf (" euid=%lu", (unsigned long int) euid
);
348 pwd
= getpwuid (euid
);
350 printf ("(%s)", pwd
->pw_name
);
355 printf (" egid=%lu", (unsigned long int) egid
);
356 grp
= getgrgid (egid
);
358 printf ("(%s)", grp
->gr_name
);
367 if (! xgetgroups (username
, (pwd
? pwd
->pw_gid
: (gid_t
) -1),
375 fputs (_(" groups="), stdout
);
376 for (i
= 0; i
< n_groups
; i
++)
380 printf ("%lu", (unsigned long int) groups
[i
]);
381 grp
= getgrgid (groups
[i
]);
383 printf ("(%s)", grp
->gr_name
);
387 #endif /* HAVE_GETGROUPS */