annotate toys/id.c @ 542:06763d1a2f9d

Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
author Rob Landley <rob@landley.net>
date Mon, 12 Mar 2012 20:56:56 -0500
parents 31215cc6c9f2
children 60b97ba66a70
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
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 * id.c - print real and effective user and group IDs
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 * Copyright 2012 Sony Network Entertainment, Inc.
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * by Tim Bird <tim.bird@am.sony.com>
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 config ID
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 Print user and group ID.
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 -g Show only the effective group ID
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -r Show real ID instead of effective ID
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 -u Show only the effective user ID
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 */
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 #include "toys.h"
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #define FLAG_g (1<<2)
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #define FLAG_r (1<<1)
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #define FLAG_u 1
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
35
533
31215cc6c9f2 Consolidate headers.
Rob Landley <rob@landley.net>
parents: 493
diff changeset
36 void pretty_print(struct passwd *pw, struct group *grp, struct group **grps,
31215cc6c9f2 Consolidate headers.
Rob Landley <rob@landley.net>
parents: 493
diff changeset
37 int n)
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
38 {
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
39 int i = 0;
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
40
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
41 printf("uid=%u(%s) gid=%u(%s)", pw->pw_uid, pw->pw_name,
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
42 grp->gr_gid, grp->gr_name);
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
43 if (n) printf(" groups=");
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
44 while (i < n) {
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 printf("%d(%s)", grps[i]->gr_gid, grps[i]->gr_name);
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
46 if (++i<n) xputc(',');
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
47 }
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
48 xputc('\n');
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
49 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
50
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 void id_main(void)
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 {
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
53 int flags = toys.optflags, i, ngroups;
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
54 struct passwd *pw;
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
55 struct group *grp, **grps;
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 uid_t uid;
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
57 gid_t gid, *groups;
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
58
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
59 /* check if a username is given */
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
60 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
61 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
62 error_exit("no such user '%s'", *toys.optargs);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
63 uid = pw->pw_uid;
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
64 gid = pw->pw_gid;
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
65 } else {
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
66 /* show effective, unless user specifies real */
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
67 if (flags & FLAG_r) {
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
68 uid = getuid();
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
69 gid = getgid();
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
70 } else {
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
71 uid = geteuid();
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
72 gid = getegid();
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
73 }
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
75
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
76 if (!(pw = getpwuid(uid)) || !(grp = getgrgid(gid)))
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
77 perror_exit(0);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
78
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 if (flags & FLAG_u) {
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
80 if (flags & FLAG_n) xputs(pw->pw_name);
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
81 else printf("%d\n", pw->pw_uid);
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 return;
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 }
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if (flags & FLAG_g) {
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
85 if (flags & FLAG_n) xputs(grp->gr_name);
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
86 else printf("%d\n", grp->gr_gid);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
87 return;
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
88 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
89
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
90 ngroups = sysconf(_SC_NGROUPS_MAX);
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
91 if (ngroups<1) ngroups = 32;
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
92 groups = xmalloc(ngroups * sizeof(gid_t));
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
93 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) < 0)
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
94 perror_exit(0);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
95
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
96 grps = xmalloc(ngroups * sizeof(struct group *));
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
97 for (i = 0; i < ngroups; i++) {
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
98 struct group *tmp;
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
99 grps[i] = xmalloc(sizeof(struct group));
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
100 size_t f = sysconf(_SC_GETGR_R_SIZE_MAX);
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
101 char *buf = xmalloc(f);
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
102 if (getgrgid_r(groups[i], grps[i], buf, f, &tmp) < 0 || !tmp) {
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
103 perror_msg(0);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
104 continue;
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
105 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
106 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
107
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
108 if (flags & FLAG_G) {
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
109 for (i = 0; i < ngroups; i++) {
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
110 if (i) xputc(' ');
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
111 if (flags & FLAG_n) printf("%s", grps[i]->gr_name);
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
112 else printf("%d", grps[i]->gr_gid);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
113 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
114 printf("\n");
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
115 return;
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
116 }
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
117
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
118 pretty_print(pw, grp, grps, ngroups);
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
119 for (i=0; i < ngroups; i++)
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
120 free(grps[i]);
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
121
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 }