1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)id.c 5.6 (Berkeley) 11/12/91"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <pwd.h> 20 #include <grp.h> 21 #include <errno.h> 22 #include <unistd.h> 23 #include <stdlib.h> 24 #include <stdio.h> 25 #include <string.h> 26 27 void current __P((void)); 28 void err __P((const char *, ...)); 29 void pretty __P((struct passwd *)); 30 void group __P((struct passwd *, int)); 31 void usage __P((void)); 32 void user __P((struct passwd *)); 33 struct passwd * 34 who __P((char *)); 35 36 int 37 main(argc, argv) 38 int argc; 39 char *argv[]; 40 { 41 struct group *gr; 42 struct passwd *pw; 43 int Gflag, ch, gflag, id, nflag, pflag, rflag, uflag; 44 45 Gflag = gflag = nflag = pflag = rflag = uflag = 0; 46 while ((ch = getopt(argc, argv, "Ggnpru")) != EOF) 47 switch(ch) { 48 case 'G': 49 Gflag = 1; 50 break; 51 case 'g': 52 gflag = 1; 53 break; 54 case 'n': 55 nflag = 1; 56 break; 57 case 'p': 58 pflag = 1; 59 break; 60 case 'r': 61 rflag = 1; 62 break; 63 case 'u': 64 uflag = 1; 65 break; 66 case '?': 67 default: 68 usage(); 69 } 70 argc -= optind; 71 argv += optind; 72 73 switch(Gflag + gflag + pflag + uflag) { 74 case 1: 75 break; 76 case 0: 77 if (!nflag && !rflag) 78 break; 79 /* FALLTHROUGH */ 80 default: 81 usage(); 82 } 83 84 pw = *argv ? who(*argv) : NULL; 85 86 if (gflag) { 87 id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); 88 if (nflag && (gr = getgrgid(id))) 89 (void)printf("%s\n", gr->gr_name); 90 else 91 (void)printf("%u\n", id); 92 exit(0); 93 } 94 95 if (uflag) { 96 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); 97 if (nflag && (pw = getpwuid(id))) 98 (void)printf("%s\n", pw->pw_name); 99 else 100 (void)printf("%u\n", id); 101 exit(0); 102 } 103 104 if (Gflag) { 105 group(pw, nflag); 106 exit(0); 107 } 108 109 if (pflag) { 110 pretty(pw); 111 exit(0); 112 } 113 114 if (pw) 115 user(pw); 116 else 117 current(); 118 exit(0); 119 } 120 121 void 122 pretty(pw) 123 struct passwd *pw; 124 { 125 struct group *gr; 126 u_int eid, rid; 127 char *login; 128 129 if (pw) { 130 (void)printf("uid\t%s\n", pw->pw_name); 131 (void)printf("groups\t"); 132 group(pw, 1); 133 } else { 134 if ((login = getlogin()) == NULL) 135 err("getlogin: %s", strerror(errno)); 136 137 pw = getpwuid(rid = getuid()); 138 if (pw == NULL || strcmp(login, pw->pw_name)) 139 (void)printf("login\t%s\n", login); 140 if (pw) 141 (void)printf("uid\t%s\n", pw->pw_name); 142 else 143 (void)printf("uid\t%u\n", rid); 144 145 if ((eid = geteuid()) != rid) 146 if (pw = getpwuid(eid)) 147 (void)printf("euid\t%s", pw->pw_name); 148 else 149 (void)printf("euid\t%u", eid); 150 if ((rid = getgid()) != (eid = getegid())) 151 if (gr = getgrgid(rid)) 152 (void)printf("rgid\t%s\n", gr->gr_name); 153 else 154 (void)printf("rgid\t%u\n", rid); 155 (void)printf("groups\t"); 156 group(NULL, 1); 157 } 158 } 159 160 void 161 current() 162 { 163 struct group *gr; 164 struct passwd *pw; 165 int cnt, id, eid, lastid, ngroups, groups[NGROUPS]; 166 char *fmt; 167 168 id = getuid(); 169 (void)printf("uid=%u", id); 170 if (pw = getpwuid(id)) 171 (void)printf("(%s)", pw->pw_name); 172 if ((eid = geteuid()) != id) { 173 (void)printf(" euid=%u", eid); 174 if (pw = getpwuid(eid)) 175 (void)printf("(%s)", pw->pw_name); 176 } 177 id = getgid(); 178 (void)printf(" gid=%u", id); 179 if (gr = getgrgid(id)) 180 (void)printf("(%s)", gr->gr_name); 181 if ((eid = getegid()) != id) { 182 (void)printf(" egid=%u", eid); 183 if (gr = getgrgid(eid)) 184 (void)printf("(%s)", gr->gr_name); 185 } 186 if (ngroups = getgroups(NGROUPS, groups)) { 187 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups; 188 fmt = ", %u", lastid = id) { 189 id = groups[cnt++]; 190 if (lastid == id) 191 continue; 192 (void)printf(fmt, id); 193 if (gr = getgrgid(id)) 194 (void)printf("(%s)", gr->gr_name); 195 } 196 } 197 (void)printf("\n"); 198 } 199 200 void 201 user(pw) 202 register struct passwd *pw; 203 { 204 register struct group *gr; 205 register char *fmt, **p; 206 int cnt, id, lastid, ngroups, groups[NGROUPS + 1]; 207 208 id = pw->pw_uid; 209 (void)printf("uid=%u(%s)", id, pw->pw_name); 210 (void)printf(" gid=%u", pw->pw_gid); 211 if (gr = getgrgid(id)) 212 (void)printf("(%s)", gr->gr_name); 213 ngroups = NGROUPS + 1; 214 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); 215 fmt = " groups=%u"; 216 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { 217 if (lastid == (id = groups[cnt])) 218 continue; 219 (void)printf(fmt, id); 220 fmt = " %u"; 221 if (gr = getgrgid(id)) 222 (void)printf("(%s)", gr->gr_name); 223 lastid = id; 224 } 225 (void)printf("\n"); 226 } 227 228 void 229 group(pw, nflag) 230 struct passwd *pw; 231 int nflag; 232 { 233 struct group *gr; 234 int cnt, id, lastid, ngroups, groups[NGROUPS + 1]; 235 char *fmt; 236 237 if (pw) { 238 ngroups = NGROUPS + 1; 239 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); 240 } else { 241 groups[0] = getgid(); 242 ngroups = getgroups(NGROUPS, groups + 1) + 1; 243 } 244 fmt = nflag ? "%s" : "%u"; 245 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { 246 if (lastid == (id = groups[cnt])) 247 continue; 248 if (nflag) { 249 if (gr = getgrgid(id)) 250 (void)printf(fmt, gr->gr_name); 251 else 252 (void)printf(*fmt == ' ' ? " %u" : "%u", 253 id); 254 fmt = " %s"; 255 } else { 256 (void)printf(fmt, id); 257 fmt = " %u"; 258 } 259 lastid = id; 260 } 261 (void)printf("\n"); 262 } 263 264 struct passwd * 265 who(u) 266 char *u; 267 { 268 struct passwd *pw; 269 long id; 270 char *ep; 271 272 /* 273 * Translate user argument into a pw pointer. First, try to 274 * get it as specified. If that fails, try it as a number. 275 */ 276 if (pw = getpwnam(u)) 277 return(pw); 278 id = strtol(u, &ep, 10); 279 if (*u && !*ep && (pw = getpwuid(id))) 280 return(pw); 281 err("%s: No such user", u); 282 /* NOTREACHED */ 283 } 284 285 #if __STDC__ 286 #include <stdarg.h> 287 #else 288 #include <varargs.h> 289 #endif 290 291 void 292 #if __STDC__ 293 err(const char *fmt, ...) 294 #else 295 err(fmt, va_alist) 296 char *fmt; 297 va_dcl 298 #endif 299 { 300 va_list ap; 301 #if __STDC__ 302 va_start(ap, fmt); 303 #else 304 va_start(ap); 305 #endif 306 (void)fprintf(stderr, "id: "); 307 (void)vfprintf(stderr, fmt, ap); 308 va_end(ap); 309 (void)fprintf(stderr, "\n"); 310 exit(1); 311 /* NOTREACHED */ 312 } 313 314 void 315 usage() 316 { 317 (void)fprintf(stderr, "usage: id [user]\n"); 318 (void)fprintf(stderr, " id -G [-n] [user]\n"); 319 (void)fprintf(stderr, " id -g [-nr] [user]\n"); 320 (void)fprintf(stderr, " id -u [-nr] [user]\n"); 321 exit(1); 322 } 323