1 | /* id - return uid and gid Author: John J. Marco */
|
---|
2 |
|
---|
3 | /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
---|
4 | /* ----- id.c ----- */
|
---|
5 | /* Id - get real and effective user id and group id */
|
---|
6 | /* Author: John J. Marco */
|
---|
7 | /* pa1343@sdcc15.ucsd.edu */
|
---|
8 | /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
|
---|
9 |
|
---|
10 | #include <sys/types.h>
|
---|
11 | #include <pwd.h>
|
---|
12 | #include <grp.h>
|
---|
13 | #include <unistd.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <limits.h>
|
---|
16 |
|
---|
17 | int main(int argc, char *argv[])
|
---|
18 | {
|
---|
19 | struct passwd *pwd;
|
---|
20 | struct group *grp;
|
---|
21 | uid_t ruid, euid;
|
---|
22 | gid_t rgid, egid;
|
---|
23 | #if __minix_vmd
|
---|
24 | uid_t suid;
|
---|
25 | gid_t sgid;
|
---|
26 | #else
|
---|
27 | # define suid ruid
|
---|
28 | # define sgid rgid
|
---|
29 | #endif
|
---|
30 | #if NGROUPS_MAX > 0
|
---|
31 | gid_t groups[NGROUPS_MAX];
|
---|
32 | int ngroups;
|
---|
33 | #else
|
---|
34 | # define groups (&rgid)
|
---|
35 | # define ngroups 0
|
---|
36 | #endif
|
---|
37 | int g;
|
---|
38 | int isug;
|
---|
39 | int c, uopt = 0;
|
---|
40 |
|
---|
41 | #if __minix_vmd
|
---|
42 | get6id(&ruid, &euid, &suid, &rgid, &egid, &sgid);
|
---|
43 | isug = issetugid();
|
---|
44 | #else
|
---|
45 | ruid = getuid();
|
---|
46 | euid = geteuid();
|
---|
47 | rgid = getgid();
|
---|
48 | egid = getegid();
|
---|
49 | isug = 0;
|
---|
50 | #endif
|
---|
51 | #if NGROUPS_MAX > 0
|
---|
52 | ngroups = getgroups(NGROUPS_MAX, groups);
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | while((c = getopt(argc, argv, "u")) != EOF) {
|
---|
56 | switch(c) {
|
---|
57 | case 'u':
|
---|
58 | uopt = 1;
|
---|
59 | break;
|
---|
60 | default:
|
---|
61 | fprintf(stderr, "%s: unrecognized option\n", argv[0]);
|
---|
62 | return(1);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | if(uopt) {
|
---|
67 | printf("%d\n", euid);
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if ((pwd = getpwuid(ruid)) == NULL)
|
---|
72 | printf("uid=%d", ruid);
|
---|
73 | else
|
---|
74 | printf("uid=%d(%s)", ruid, pwd->pw_name);
|
---|
75 |
|
---|
76 | if ((grp = getgrgid(rgid)) == NULL)
|
---|
77 | printf(" gid=%d", rgid);
|
---|
78 | else
|
---|
79 | printf(" gid=%d(%s)", rgid, grp->gr_name);
|
---|
80 |
|
---|
81 | if (euid != ruid)
|
---|
82 | if ((pwd = getpwuid(euid)) != NULL)
|
---|
83 | printf(" euid=%d(%s)", euid, pwd->pw_name);
|
---|
84 | else
|
---|
85 | printf(" euid=%d", euid);
|
---|
86 |
|
---|
87 | if (egid != rgid)
|
---|
88 | if ((grp = getgrgid(egid)) != NULL)
|
---|
89 | printf(" egid=%d(%s)", egid, grp->gr_name);
|
---|
90 | else
|
---|
91 | printf(" egid=%d", egid);
|
---|
92 |
|
---|
93 | if (suid != euid)
|
---|
94 | if ((pwd = getpwuid(suid)) != NULL)
|
---|
95 | printf(" suid=%d(%s)", suid, pwd->pw_name);
|
---|
96 | else
|
---|
97 | printf(" suid=%d", suid);
|
---|
98 |
|
---|
99 | if (sgid != egid)
|
---|
100 | if ((grp = getgrgid(sgid)) != NULL)
|
---|
101 | printf(" sgid=%d(%s)", sgid, grp->gr_name);
|
---|
102 | else
|
---|
103 | printf(" sgid=%d", sgid);
|
---|
104 |
|
---|
105 | if (isug) {
|
---|
106 | printf(" issetugid");
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (ngroups > 0) {
|
---|
110 | printf(" groups=");
|
---|
111 | for (g = 0; g < ngroups; g++) {
|
---|
112 | if (g > 0) fputc(',', stdout);
|
---|
113 | if ((grp = getgrgid(groups[g])) == NULL)
|
---|
114 | printf("%d", groups[g]);
|
---|
115 | else
|
---|
116 | printf("%d(%s)", groups[g], grp->gr_name);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | printf("\n");
|
---|
121 | return(0);
|
---|
122 | }
|
---|