annotate toys/posix/id.c @ 1661:d4bc084916fd draft

id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
author Rob Landley <rob@landley.net>
date Fri, 16 Jan 2015 13:43:09 -0600
parents b84024067049
children 57f2a26fa92c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* id.c - print real and effective user and group IDs
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Sony Network Entertainment, Inc.
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * by Tim Bird <tim.bird@am.sony.com>
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
9 USE_ID(NEWTOY(id, ">1"USE_ID_SELINUX("Z")"nGgru[!"USE_ID_SELINUX("Z")"Ggu]", TOYFLAG_BIN))
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
10 USE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN))
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
11 USE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_BIN))
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
12 USE_WHOAMI(OLDTOY(whoami, logname, TOYFLAG_BIN))
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
13
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 config ID
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 bool "id"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
17 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 usage: id [-nGgru]
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
20 Print user and group ID.
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
22 -n print names instead of numeric IDs (to be used with -Ggu)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
23 -G Show only the group IDs
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
24 -g Show only the effective group ID
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
25 -r Show real ID instead of effective ID
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
26 -u Show only the effective user ID
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
27
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
28 config ID_SELINUX
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
29 bool
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
30 default y
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
31 depends on ID && TOYBOX_SELINUX
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
32 help
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
33 usage: id [-Z]
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
34
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
35 -Z Show only SELinux context
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
36
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
37 config GROUPS
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
38 bool "groups"
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
39 default y
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
40 help
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
41 usage: groups [user]
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
42
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
43 Print the groups a user is in.
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
44
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
45 config LOGNAME
1315
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
46 bool "logname"
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
47 default y
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
48 help
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
49 usage: logname
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
50
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
51 Print the current user name.
0f42d83199a9 logname and whoami are the same as id -un, so merge them.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1130
diff changeset
52
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
53 config WHOAMI
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
54 bool "whoami"
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
55 default y
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
56 help
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
57 usage: whoami
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
58
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
59 Print the current user name.
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 */
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
62 #define FOR_id
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 #include "toys.h"
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
64
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
65 GLOBALS(
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
66 int do_u, do_n, do_G, do_Z, is_groups;
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
67 )
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
68
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 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
70 {
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
71 if (TT.do_n) printf("%s", s);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
72 else printf("%u", u);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
73 if (done) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
74 xputc('\n');
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
75 exit(0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
76 }
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 }
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
78
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
79 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
80 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
81 printf("%s%u(%s)", header, u, s);
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
82 }
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
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
84 void do_id(char *username)
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 {
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
86 int flags, i, ngroups;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
87 struct passwd *pw;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
88 struct group *grp;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
89 uid_t uid = getuid(), euid = geteuid();
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
90 gid_t gid = getgid(), egid = getegid(), *groups;
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
91
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
92 flags = toys.optflags;
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
93
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
94 // check if a username is given
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
95 if (username) {
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
96 pw = xgetpwnam(username);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
97 uid = euid = pw->pw_uid;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
98 gid = egid = pw->pw_gid;
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
99 if (TT.is_groups) printf("%s : ", pw->pw_name);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
100 }
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
101
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
102 i = flags & FLAG_r;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
103 pw = xgetpwuid(i ? uid : euid);
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
104 if (TT.do_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
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
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
106 grp = xgetgrgid(i ? gid : egid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
107 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
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
108
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
109 if (!TT.do_G && !TT.do_Z) {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
110 showid("uid=", pw->pw_uid, pw->pw_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
111 showid(" gid=", grp->gr_gid, grp->gr_name);
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
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
113 if (!i) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
114 if (uid != euid) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
115 pw = xgetpwuid(euid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
116 showid(" euid=", pw->pw_uid, pw->pw_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
117 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
118 if (gid != egid) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
119 grp = xgetgrgid(egid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
120 showid(" egid=", grp->gr_gid, grp->gr_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
121 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
122 }
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
123
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
124 showid(" groups=", grp->gr_gid, grp->gr_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
125 }
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
126
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
127 if (!TT.do_Z) {
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
128 groups = (gid_t *)toybuf;
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
129 i = sizeof(toybuf)/sizeof(gid_t);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
130 ngroups = username ? getgrouplist(username, gid, groups, &i)
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
131 : getgroups(i, groups);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
132 if (ngroups<0) perror_exit(0);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
133
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
134 int show_separator = !TT.do_G;
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
135 for (i = 0; i<ngroups; i++) {
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
136 if (show_separator) xputc(TT.do_G ? ' ' : ',');
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
137 show_separator = 1;
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
138 if (!(grp = getgrgid(groups[i]))) perror_msg(0);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
139 else if (TT.do_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
140 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
141 else show_separator = 0; // Because we didn't show anything this time.
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
142 }
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
143 if (TT.do_G) {
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
144 xputc('\n');
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
145 exit(0);
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
146 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
147 }
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
148
1661
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
149 if (CFG_TOYBOX_SELINUX) {
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
150 char *context = NULL;
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
151
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
152 if (is_selinux_enabled() < 1) {
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
153 if (TT.do_Z)
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
154 error_exit("SELinux disabled");
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
155 } else if (getcon(&context) == 0) {
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
156 if (!TT.do_Z) xputc(' ');
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
157 printf("context=%s", context);
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
158 }
d4bc084916fd id.c #ifdefectomy. (ifdefs belong in headers, not in C code.)
Rob Landley <rob@landley.net>
parents: 1660
diff changeset
159 if (CFG_TOYBOX_FREE) free(context);
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
160 }
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
161
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
162 xputc('\n');
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 }
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
164
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
165 void id_main(void)
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
166 {
1518
4bfbd8b96f66 Various bugfixes (mostly resource leaks) from Ashwini Sharma's static analysis, plus occasional tweak by me while reviewing them.
Rob Landley <rob@landley.net>
parents: 1503
diff changeset
167 // FLAG macros can be 0 if "id" command not enabled, so snapshot them here.
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
168 if (FLAG_u) TT.do_u |= toys.optflags & FLAG_u;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
169 if (FLAG_n) TT.do_n |= toys.optflags & FLAG_n;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
170 if (FLAG_G) TT.do_G |= toys.optflags & FLAG_G;
1660
b84024067049 This patch adds a TOYBOX_SELINUX configuration option to control both
Elliott Hughes <enh@google.com>
parents: 1634
diff changeset
171 if (FLAG_Z) TT.do_Z |= toys.optflags & FLAG_Z;
1503
96bdf979d9e6 Untangle id/groups/logname so single.sh can build each one standalone.
Rob Landley <rob@landley.net>
parents: 1417
diff changeset
172
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
173 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
174 else do_id(NULL);
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
175 }
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
176
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
177 void groups_main(void)
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
178 {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
179 TT.is_groups = 1;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
180 TT.do_G = TT.do_n = 1;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
181 id_main();
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
182 }
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
183
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
184 void logname_main(void)
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
185 {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
186 TT.do_u = TT.do_n = 1;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
187 id_main();
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
188 }