4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
36 #include <sys/param.h>
43 #define PWNULL ((struct passwd *)0)
44 #define GRNULL ((struct group *)0)
47 UID
, EUID
, GID
, EGID
, SGID
51 CURR
, /* Print uid/gid only */
52 ALLGROUPS
, /* Print all groups */
53 GROUP
, /* Print only group */
54 USER
/* Print only uid */
56 static PRINT mode
= CURR
;
58 static int usage(void);
59 static void puid(uid_t
);
60 static void pgid(gid_t
);
61 static void prid(TYPE
, uid_t
);
62 static int getusergroups(int, gid_t
*, char *, gid_t
);
64 static int nflag
= 0; /* Output names, not numbers */
65 static int rflag
= 0; /* Output real, not effective IDs */
66 static char stdbuf
[BUFSIZ
];
69 main(int argc
, char *argv
[])
73 gid_t gid
, egid
, prgid
;
74 int c
, aflag
= 0, project_flag
= 0;
77 int groupmax
= sysconf(_SC_NGROUPS_MAX
);
78 gid_t
*groupids
= alloca(groupmax
* sizeof (gid_t
));
82 (void) setlocale(LC_ALL
, "");
84 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
85 #define TEXT_DOMAIN "SYS_TEST"
87 (void) textdomain(TEXT_DOMAIN
);
88 while ((c
= getopt(argc
, argv
, "Ggunarp")) != EOF
) {
130 setbuf(stdout
, stdbuf
);
134 /* -n and -r must be combined with one of -[Ggu] */
135 /* -r cannot be combined with -G */
136 /* -a and -p cannot be combined with -[Ggu] */
138 if ((mode
== CURR
&& (nflag
|| rflag
)) ||
139 (mode
== ALLGROUPS
&& rflag
) ||
140 (argc
!= 1 && argc
!= 2) ||
141 (mode
!= CURR
&& (project_flag
|| aflag
)))
144 if ((pwp
= getpwnam(argv
[1])) == PWNULL
) {
145 (void) fprintf(stderr
,
146 gettext("id: invalid user name: \"%s\"\n"),
151 uid
= euid
= pwp
->pw_uid
;
152 prgid
= gid
= egid
= pwp
->pw_gid
;
167 else if (mode
== GROUP
)
169 else if (mode
== ALLGROUPS
) {
172 i
= getusergroups(groupmax
, groupids
, user
,
175 i
= getgroups(groupmax
, groupids
);
179 for (j
= 0; j
< i
; ++j
) {
180 if ((gid
= groupids
[j
]) == egid
)
187 (void) putchar('\n');
198 i
= getusergroups(groupmax
, groupids
, user
,
201 i
= getgroups(groupmax
, groupids
);
205 (void) printf(" groups=");
206 for (idp
= groupids
; i
--; idp
++) {
207 (void) printf("%u", *idp
);
208 if (gr
= getgrgid(*idp
))
209 (void) printf("(%s)",
218 * POSIX requires us to show all supplementary groups
219 * groups other than the effective group already listed.
221 * This differs from -a above, because -a always shows
222 * all groups including the effective group in the group=
225 * It would be simpler if SunOS could just adopt this
226 * POSIX behavior, as it is so incredibly close to the
229 * Then the magic -a flag could just indicate whether or
230 * not we are suppressing the effective group id.
234 i
= getusergroups(groupmax
, groupids
, user
,
237 i
= getgroups(groupmax
, groupids
);
241 (void) printf(" groups=");
242 for (idp
= groupids
; i
--; idp
++) {
245 (void) printf("%u", *idp
);
246 if (gr
= getgrgid(*idp
))
247 (void) printf("(%s)",
258 projid_t curprojid
= getprojid();
260 if ((projbuf
= malloc(PROJECT_BUFSZ
)) == NULL
) {
261 (void) fprintf(stderr
, "unable to allocate "
267 if (getdefaultproj(user
, &proj
, projbuf
,
268 PROJECT_BUFSZ
) != NULL
)
269 (void) printf(" projid=%d(%s)",
270 (int)proj
.pj_projid
, proj
.pj_name
);
273 * This can only happen if project
274 * "default" has been removed from
275 * /etc/project file or the whole
276 * project database file was removed.
278 (void) printf(" projid=(NONE)");
280 if (getprojbyid(curprojid
, &proj
, projbuf
,
281 PROJECT_BUFSZ
) == NULL
)
282 (void) printf(" projid=%d",
285 (void) printf(" projid=%d(%s)",
286 (int)curprojid
, proj
.pj_name
);
290 (void) putchar('\n');
298 (void) fprintf(stderr
, gettext(
299 "Usage: id [-ap] [user]\n"
300 " id -G [-n] [user]\n"
301 " id -g [-nr] [user]\n"
302 " id -u [-nr] [user]\n"));
311 if (nflag
&& (pw
= getpwuid(uid
)) != PWNULL
)
312 (void) printf("%s", pw
->pw_name
);
314 (void) printf("%u", uid
);
322 if (nflag
&& (gr
= getgrgid(gid
)) != GRNULL
)
323 (void) printf("%s", gr
->gr_name
);
325 (void) printf("%u", gid
);
329 prid(TYPE how
, uid_t id
)
352 (void) printf("%s=", s
);
353 (void) printf("%u", id
);
360 if ((pwp
= getpwuid(id
)) != PWNULL
)
361 (void) printf("(%s)", pwp
->pw_name
);
370 if ((grp
= getgrgid(id
)) != GRNULL
)
371 (void) printf("(%s)", grp
->gr_name
);
378 * Get the supplementary group affiliation for the user
380 static int getusergroups(gidsetsize
, grouplist
, user
, prgid
)
391 while ((ngroups
< gidsetsize
) && ((group
= getgrent()) != NULL
))
392 for (gr_mem
= group
->gr_mem
; *gr_mem
; gr_mem
++)
393 if (strcmp(user
, *gr_mem
) == 0) {
395 grouplist
[ngroups
] = group
->gr_gid
;
399 if (gidsetsize
&& !ngroups
)
400 grouplist
[ngroups
++] = prgid
;