Mercurial > hg > toybox
annotate toys/id.c @ 587:82ffae226c40
Convert another realpath use to xrealpath().
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 01 Jun 2012 17:59:11 -0500 |
parents | 60b97ba66a70 |
children |
rev | line source |
---|---|
413 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * id.c - print real and effective user and group IDs | |
4 * | |
5 * Copyright 2012 Sony Network Entertainment, Inc. | |
6 * | |
7 * by Tim Bird <tim.bird@am.sony.com> | |
8 * | |
9 * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html | |
10 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
11 USE_ID(NEWTOY(id, "nGgru", TOYFLAG_BIN)) |
413 | 12 |
13 config ID | |
14 bool "id" | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
15 default y |
413 | 16 help |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
17 usage: id [-nGgru] |
413 | 18 |
19 Print user and group ID. | |
20 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
21 -n print names instead of numeric IDs (to be used with -Ggu) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
22 -G Show only the group IDs |
413 | 23 -g Show only the effective group ID |
24 -r Show real ID instead of effective ID | |
25 -u Show only the effective user ID | |
26 */ | |
27 | |
28 #include "toys.h" | |
29 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
30 #define FLAG_n (1<<4) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
31 #define FLAG_G (1<<3) |
413 | 32 #define FLAG_g (1<<2) |
33 #define FLAG_r (1<<1) | |
34 #define FLAG_u 1 | |
35 | |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
36 static void s_or_u(char *s, unsigned u, int done) |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
37 { |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
38 if (toys.optflags & FLAG_n) printf("%s", s); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
39 else printf("%u", u); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
40 if (done) { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
41 xputc('\n'); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
42 exit(0); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
43 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
44 } |
542
06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
45 |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
46 static void showid(char *header, unsigned u, char *s) |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
47 { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
48 printf("%s%u(%s)", header, u, s); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
49 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
50 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
51 struct passwd *xgetpwuid(uid_t uid) |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
52 { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
53 struct passwd *pwd = getpwuid(uid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
54 if (!pwd) error_exit(NULL); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
55 return pwd; |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
56 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
57 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
58 struct group *xgetgrgid(gid_t gid) |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
59 { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
60 struct group *group = getgrgid(gid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
61 if (!group) error_exit(NULL); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
62 return group; |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
63 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
64 |
413 | 65 void id_main(void) |
66 { | |
542
06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
67 int flags = toys.optflags, i, ngroups; |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
68 struct passwd *pw; |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
69 struct group *grp; |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
70 uid_t uid = getuid(), euid = geteuid(); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
71 gid_t gid = getgid(), egid = getegid(), *groups; |
413 | 72 |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
73 /* check if a username is given */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
74 if (*toys.optargs) { |
542
06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
75 if (!(pw = getpwnam(*toys.optargs))) |
06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
76 error_exit("no such user '%s'", *toys.optargs); |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
77 uid = euid = pw->pw_uid; |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
78 gid = egid = pw->pw_gid; |
413 | 79 } |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
80 |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
81 i = toys.optflags & FLAG_r; |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
82 pw = xgetpwuid(i ? uid : euid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
83 if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
84 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
85 grp = xgetgrgid(i ? gid : egid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
86 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
87 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
88 if (!(flags & FLAG_G)) { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
89 showid("uid=", pw->pw_uid, pw->pw_name); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
90 showid(" gid=", grp->gr_gid, grp->gr_name); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
91 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
92 if (!i) { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
93 if (uid != euid) { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
94 pw = xgetpwuid(euid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
95 showid(" euid=", pw->pw_uid, pw->pw_name); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
96 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
97 if (gid != egid) { |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
98 grp = xgetgrgid(egid); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
99 showid(" egid=", grp->gr_gid, grp->gr_name); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
100 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
101 } |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
102 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
103 showid(" groups=", grp->gr_gid, grp->gr_name); |
413 | 104 } |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
105 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
106 |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
107 groups = (gid_t *)toybuf; |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
108 if (0 >= (ngroups = getgroups(sizeof(toybuf)/sizeof(gid_t), groups))) |
542
06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
109 perror_exit(0); |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
110 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
111 for (i = 0; i < ngroups; i++) { |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
112 xputc(' '); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
113 if (!(grp = getgrgid(groups[i]))) perror_msg(0); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
114 else if (flags & FLAG_G) |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
115 s_or_u(grp->gr_name, grp->gr_gid, 0); |
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
116 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name); |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
117 } |
543
60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents:
542
diff
changeset
|
118 xputc('\n'); |
413 | 119 } |