1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989, 1991 Free Software Foundation.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
22 #if defined (CONFIG_BROKETS)
23 /* We use <config.h> instead of "config.h" so that a compilation
24 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
25 (which it would do because it found this file in $srcdir). */
34 #include <sys/types.h>
44 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
45 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
46 #endif /* !NGROUPS_MAX */
48 #else /* not _POSIX_VERSION */
49 struct passwd
*getpwuid ();
50 struct group
*getgrgid ();
55 #include <sys/param.h>
56 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
57 #define NGROUPS_MAX NGROUPS
58 #endif /* not NGROUPS_MAX and NGROUPS */
59 #endif /* not _POSIX_VERSION */
65 static void print_user ();
66 static void print_group ();
67 static void print_group_list ();
68 static void print_full_info ();
71 /* The name this program was run with. */
74 /* If nonzero, output only the group ID(s). -g */
75 static int just_group
= 0;
77 /* If nonzero, output user/group name instead of ID number. -n */
78 static int use_name
= 0;
80 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
81 static int use_real
= 0;
83 /* If nonzero, output only the user ID(s). -u */
84 static int just_user
= 0;
86 /* If nonzero, output only the supplementary groups. -G */
87 static int just_group_list
= 0;
89 /* The real and effective IDs of the user to print. */
90 static uid_t ruid
, euid
;
91 static gid_t rgid
, egid
;
93 /* The number of errors encountered so far. */
94 static int problems
= 0;
96 /* If non-zero, display usage information and exit. */
99 /* If non-zero, print the version on standard output and exit. */
100 static int show_version
;
102 static struct option
const longopts
[] =
104 {"group", no_argument
, NULL
, 'g'},
105 {"groups", no_argument
, NULL
, 'G'},
106 {"help", no_argument
, &show_help
, 1},
107 {"name", no_argument
, NULL
, 'n'},
108 {"real", no_argument
, NULL
, 'r'},
109 {"user", no_argument
, NULL
, 'u'},
110 {"version", no_argument
, &show_version
, 1},
121 program_name
= argv
[0];
123 while ((optc
= getopt_long (argc
, argv
, "gnruG", longopts
, (int *) 0))
152 printf ("%s\n", version_string
);
159 if (just_user
+ just_group
+ just_group_list
> 1)
160 error (1, 0, "cannot print only user and only group");
162 if (just_user
+ just_group
+ just_group_list
== 0 && (use_real
|| use_name
))
163 error (1, 0, "cannot print only names or real IDs in default format");
165 if (argc
- optind
> 1)
168 if (argc
- optind
== 1)
170 struct passwd
*pwd
= getpwnam (argv
[optind
]);
172 error (1, 0, "%s: No such user", argv
[optind
]);
173 ruid
= euid
= pwd
->pw_uid
;
174 rgid
= egid
= pwd
->pw_gid
;
185 print_user (use_real
? ruid
: euid
);
187 print_group (use_real
? rgid
: egid
);
188 else if (just_group_list
)
189 print_group_list (argv
[optind
]);
191 print_full_info (argv
[optind
]);
194 exit (problems
!= 0);
197 /* Print the name or value of user ID UID. */
203 struct passwd
*pwd
= NULL
;
207 pwd
= getpwuid (uid
);
213 printf ("%u", (unsigned) uid
);
215 printf ("%s", pwd
->pw_name
);
218 /* Print the name or value of group ID GID. */
224 struct group
*grp
= NULL
;
228 grp
= getgrgid (gid
);
234 printf ("%u", (unsigned) gid
);
236 printf ("%s", grp
->gr_name
);
239 /* Print all of the distinct groups the user is in . */
242 print_group_list (username
)
258 groups
= (GETGROUPS_T
*) xmalloc (NGROUPS_MAX
* sizeof (GETGROUPS_T
));
260 ngroups
= getgroups (NGROUPS_MAX
, groups
);
262 ngroups
= getugroups (NGROUPS_MAX
, groups
, username
);
265 error (0, errno
, "cannot get supplemental group list");
271 for (i
= 0; i
< ngroups
; i
++)
272 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
275 print_group (groups
[i
]);
282 /* Print all of the info about the user's user and group IDs. */
285 print_full_info (username
)
291 printf ("uid=%u", (unsigned) ruid
);
292 pwd
= getpwuid (ruid
);
296 printf ("(%s)", pwd
->pw_name
);
298 printf (" gid=%u", (unsigned) rgid
);
299 grp
= getgrgid (rgid
);
303 printf ("(%s)", grp
->gr_name
);
307 printf (" euid=%u", (unsigned) euid
);
308 pwd
= getpwuid (euid
);
312 printf ("(%s)", pwd
->pw_name
);
317 printf (" egid=%u", (unsigned) egid
);
318 grp
= getgrgid (egid
);
322 printf ("(%s)", grp
->gr_name
);
331 groups
= (GETGROUPS_T
*) xmalloc (NGROUPS_MAX
* sizeof (GETGROUPS_T
));
333 ngroups
= getgroups (NGROUPS_MAX
, groups
);
335 ngroups
= getugroups (NGROUPS_MAX
, groups
, username
);
338 error (0, errno
, "cannot get supplemental group list");
345 fputs (" groups=", stdout
);
346 for (i
= 0; i
< ngroups
; i
++)
350 printf ("%u", (unsigned) groups
[i
]);
351 grp
= getgrgid (groups
[i
]);
355 printf ("(%s)", grp
->gr_name
);
366 Usage: %s [-gnruG] [--group] [--name] [--real] [--user] [--groups] [username]\n",