1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
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 N_ ("Arnold Robbins and David MacKenzie")
38 #ifndef _POSIX_VERSION
39 struct passwd
*getpwuid ();
40 struct group
*getgrgid ();
45 #endif /* not _POSIX_VERSION */
49 static void print_user (uid_t uid
);
50 static void print_group (gid_t gid
);
51 static void print_group_list (const char *username
);
52 static void print_full_info (const char *username
);
54 /* The name this program was run with. */
57 /* If nonzero, output user/group name instead of ID number. -n */
58 static int use_name
= 0;
60 /* The real and effective IDs of the user to print. */
61 static uid_t ruid
, euid
;
62 static gid_t rgid
, egid
;
64 /* The number of errors encountered so far. */
65 static int problems
= 0;
67 static struct option
const longopts
[] =
69 {"group", no_argument
, NULL
, 'g'},
70 {"groups", no_argument
, NULL
, 'G'},
71 {"name", no_argument
, NULL
, 'n'},
72 {"real", no_argument
, NULL
, 'r'},
73 {"user", no_argument
, NULL
, 'u'},
74 {GETOPT_HELP_OPTION_DECL
},
75 {GETOPT_VERSION_OPTION_DECL
},
83 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
87 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name
);
89 Print information for USERNAME, or the current user.\n\
91 -a ignore, for compatibility with other versions\n\
92 -g, --group print only the effective group ID\n\
93 -G, --groups print all group IDs\n\
94 -n, --name print a name instead of a number, for -ugG\n\
95 -r, --real print the real ID instead of the effective ID, with -ugG\n\
96 -u, --user print only the effective user ID\n\
98 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
99 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
102 Without any OPTION, print some useful set of identified information.\n\
104 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
110 main (int argc
, char **argv
)
114 /* If nonzero, output the list of all group IDs. -G */
115 int just_group_list
= 0;
116 /* If nonzero, output only the group ID(s). -g */
118 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
120 /* If nonzero, output only the user ID(s). -u */
123 program_name
= argv
[0];
124 setlocale (LC_ALL
, "");
125 bindtextdomain (PACKAGE
, LOCALEDIR
);
126 textdomain (PACKAGE
);
128 atexit (close_stdout
);
130 while ((optc
= getopt_long (argc
, argv
, "agnruG", longopts
, NULL
)) != -1)
137 /* Ignore -a, for compatibility with SVR4. */
154 case_GETOPT_HELP_CHAR
;
155 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
157 usage (EXIT_FAILURE
);
161 if (just_user
+ just_group
+ just_group_list
> 1)
162 error (EXIT_FAILURE
, 0, _("cannot print only user and only group"));
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"));
168 if (argc
- optind
> 1)
169 usage (EXIT_FAILURE
);
171 if (argc
- optind
== 1)
173 struct passwd
*pwd
= getpwnam (argv
[optind
]);
175 error (EXIT_FAILURE
, 0, _("%s: No such user"), argv
[optind
]);
176 ruid
= euid
= pwd
->pw_uid
;
177 rgid
= egid
= pwd
->pw_gid
;
188 print_user (use_real
? ruid
: euid
);
190 print_group (use_real
? rgid
: egid
);
191 else if (just_group_list
)
192 print_group_list (argv
[optind
]);
194 print_full_info (argv
[optind
]);
197 exit (problems
!= 0);
200 /* Print the name or value of user ID UID. */
203 print_user (uid_t uid
)
205 struct passwd
*pwd
= NULL
;
209 pwd
= getpwuid (uid
);
212 error (0, 0, _("cannot find name for user ID %u"), uid
);
218 printf ("%u", (unsigned) uid
);
220 printf ("%s", pwd
->pw_name
);
223 /* Print the name or value of group ID GID. */
226 print_group (gid_t gid
)
228 struct group
*grp
= NULL
;
232 grp
= getgrgid (gid
);
235 error (0, 0, _("cannot find name for group ID %u"), gid
);
241 printf ("%u", (unsigned) gid
);
243 printf ("%s", grp
->gr_name
);
248 /* FIXME: document */
251 xgetgroups (const char *username
, gid_t gid
, int *n_groups
,
252 GETGROUPS_T
**groups
)
260 max_n_groups
= getgroups (0, NULL
);
262 max_n_groups
= getugroups (0, NULL
, username
, gid
);
264 /* Add 1 just in case max_n_groups is zero. */
265 g
= (GETGROUPS_T
*) xmalloc (max_n_groups
* sizeof (GETGROUPS_T
) + 1);
267 ng
= getgroups (max_n_groups
, g
);
269 ng
= getugroups (max_n_groups
, g
, username
, gid
);
273 error (0, errno
, _("cannot get supplemental group list"));
285 #endif /* HAVE_GETGROUPS */
287 /* Print all of the distinct groups the user is in. */
290 print_group_list (const char *username
)
294 pwd
= getpwuid (ruid
);
311 if (xgetgroups (username
, (pwd
? pwd
->pw_gid
: (gid_t
) -1),
318 for (i
= 0; i
< n_groups
; i
++)
319 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
322 print_group (groups
[i
]);
326 #endif /* HAVE_GETGROUPS */
329 /* Print all of the info about the user's user and group IDs. */
332 print_full_info (const char *username
)
337 printf ("uid=%u", (unsigned) ruid
);
338 pwd
= getpwuid (ruid
);
342 printf ("(%s)", pwd
->pw_name
);
344 printf (" gid=%u", (unsigned) rgid
);
345 grp
= getgrgid (rgid
);
349 printf ("(%s)", grp
->gr_name
);
353 printf (" euid=%u", (unsigned) euid
);
354 pwd
= getpwuid (euid
);
358 printf ("(%s)", pwd
->pw_name
);
363 printf (" egid=%u", (unsigned) egid
);
364 grp
= getgrgid (egid
);
368 printf ("(%s)", grp
->gr_name
);
377 if (xgetgroups (username
, (pwd
? pwd
->pw_gid
: (gid_t
) -1),
385 fputs (_(" groups="), stdout
);
386 for (i
= 0; i
< n_groups
; i
++)
390 printf ("%u", (unsigned) groups
[i
]);
391 grp
= getgrgid (groups
[i
]);
395 printf ("(%s)", grp
->gr_name
);
399 #endif /* HAVE_GETGROUPS */