1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1991, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94"; 43 #endif 44 static const char rcsid[] = 45 "$Id: id.c,v 1.6 1998/02/18 17:35:16 steve Exp $"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 50 #include <err.h> 51 #include <grp.h> 52 #include <pwd.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 void current __P((void)); 59 void pline __P((struct passwd *)); 60 void pretty __P((struct passwd *)); 61 void group __P((struct passwd *, int)); 62 void usage __P((void)); 63 void user __P((struct passwd *)); 64 struct passwd * 65 who __P((char *)); 66 67 int 68 main(argc, argv) 69 int argc; 70 char *argv[]; 71 { 72 struct group *gr; 73 struct passwd *pw; 74 int Gflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag; 75 76 Gflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0; 77 while ((ch = getopt(argc, argv, "PGgnpru")) != -1) 78 switch(ch) { 79 case 'G': 80 Gflag = 1; 81 break; 82 case 'P': 83 Pflag = 1; 84 break; 85 case 'g': 86 gflag = 1; 87 break; 88 case 'n': 89 nflag = 1; 90 break; 91 case 'p': 92 pflag = 1; 93 break; 94 case 'r': 95 rflag = 1; 96 break; 97 case 'u': 98 uflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 switch(Gflag + Pflag + gflag + pflag + uflag) { 108 case 1: 109 break; 110 case 0: 111 if (!nflag && !rflag) 112 break; 113 /* FALLTHROUGH */ 114 default: 115 usage(); 116 } 117 118 pw = *argv ? who(*argv) : NULL; 119 120 if (gflag) { 121 id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); 122 if (nflag && (gr = getgrgid(id))) 123 (void)printf("%s\n", gr->gr_name); 124 else 125 (void)printf("%u\n", id); 126 exit(0); 127 } 128 129 if (uflag) { 130 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); 131 if (nflag && (pw = getpwuid(id))) 132 (void)printf("%s\n", pw->pw_name); 133 else 134 (void)printf("%u\n", id); 135 exit(0); 136 } 137 138 if (Gflag) { 139 group(pw, nflag); 140 exit(0); 141 } 142 143 if (Pflag) { 144 pline(pw); 145 exit(0); 146 } 147 148 if (pflag) { 149 pretty(pw); 150 exit(0); 151 } 152 153 if (pw) 154 user(pw); 155 else 156 current(); 157 exit(0); 158 } 159 160 void 161 pretty(pw) 162 struct passwd *pw; 163 { 164 struct group *gr; 165 u_int eid, rid; 166 char *login; 167 168 if (pw) { 169 (void)printf("uid\t%s\n", pw->pw_name); 170 (void)printf("groups\t"); 171 group(pw, 1); 172 } else { 173 if ((login = getlogin()) == NULL) 174 err(1, "getlogin"); 175 176 pw = getpwuid(rid = getuid()); 177 if (pw == NULL || strcmp(login, pw->pw_name)) 178 (void)printf("login\t%s\n", login); 179 if (pw) 180 (void)printf("uid\t%s\n", pw->pw_name); 181 else 182 (void)printf("uid\t%u\n", rid); 183 184 if ((eid = geteuid()) != rid) 185 if ((pw = getpwuid(eid))) 186 (void)printf("euid\t%s\n", pw->pw_name); 187 else 188 (void)printf("euid\t%u\n", eid); 189 if ((rid = getgid()) != (eid = getegid())) 190 if ((gr = getgrgid(rid))) 191 (void)printf("rgid\t%s\n", gr->gr_name); 192 else 193 (void)printf("rgid\t%u\n", rid); 194 (void)printf("groups\t"); 195 group(NULL, 1); 196 } 197 } 198 199 void 200 current() 201 { 202 struct group *gr; 203 struct passwd *pw; 204 int cnt, id, eid, lastid, ngroups; 205 gid_t groups[NGROUPS]; 206 char *fmt; 207 208 id = getuid(); 209 (void)printf("uid=%u", id); 210 if ((pw = getpwuid(id))) 211 (void)printf("(%s)", pw->pw_name); 212 if ((eid = geteuid()) != id) { 213 (void)printf(" euid=%u", eid); 214 if ((pw = getpwuid(eid))) 215 (void)printf("(%s)", pw->pw_name); 216 } 217 id = getgid(); 218 (void)printf(" gid=%u", id); 219 if ((gr = getgrgid(id))) 220 (void)printf("(%s)", gr->gr_name); 221 if ((eid = getegid()) != id) { 222 (void)printf(" egid=%u", eid); 223 if ((gr = getgrgid(eid))) 224 (void)printf("(%s)", gr->gr_name); 225 } 226 if ((ngroups = getgroups(NGROUPS, groups))) { 227 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups; 228 fmt = ", %u", lastid = id) { 229 id = groups[cnt++]; 230 if (lastid == id) 231 continue; 232 (void)printf(fmt, id); 233 if ((gr = getgrgid(id))) 234 (void)printf("(%s)", gr->gr_name); 235 } 236 } 237 (void)printf("\n"); 238 } 239 240 void 241 user(pw) 242 register struct passwd *pw; 243 { 244 register struct group *gr; 245 register char *fmt; 246 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1]; 247 248 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name); 249 gid = pw->pw_gid; 250 (void)printf(" gid=%u", gid); 251 if ((gr = getgrgid(gid))) 252 (void)printf("(%s)", gr->gr_name); 253 ngroups = NGROUPS + 1; 254 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups); 255 fmt = " groups=%u"; 256 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { 257 if (lastgid == (gid = groups[cnt])) 258 continue; 259 (void)printf(fmt, gid); 260 fmt = " %u"; 261 if ((gr = getgrgid(gid))) 262 (void)printf("(%s)", gr->gr_name); 263 lastgid = gid; 264 } 265 (void)printf("\n"); 266 } 267 268 void 269 group(pw, nflag) 270 struct passwd *pw; 271 int nflag; 272 { 273 struct group *gr; 274 int cnt, id, lastid, ngroups; 275 gid_t groups[NGROUPS + 1]; 276 char *fmt; 277 278 if (pw) { 279 ngroups = NGROUPS + 1; 280 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); 281 } else { 282 groups[0] = getgid(); 283 ngroups = getgroups(NGROUPS, groups + 1) + 1; 284 } 285 fmt = nflag ? "%s" : "%u"; 286 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { 287 if (lastid == (id = groups[cnt])) 288 continue; 289 if (nflag) { 290 if ((gr = getgrgid(id))) 291 (void)printf(fmt, gr->gr_name); 292 else 293 (void)printf(*fmt == ' ' ? " %u" : "%u", 294 id); 295 fmt = " %s"; 296 } else { 297 (void)printf(fmt, id); 298 fmt = " %u"; 299 } 300 lastid = id; 301 } 302 (void)printf("\n"); 303 } 304 305 struct passwd * 306 who(u) 307 char *u; 308 { 309 struct passwd *pw; 310 long id; 311 char *ep; 312 313 /* 314 * Translate user argument into a pw pointer. First, try to 315 * get it as specified. If that fails, try it as a number. 316 */ 317 if ((pw = getpwnam(u))) 318 return(pw); 319 id = strtol(u, &ep, 10); 320 if (*u && !*ep && (pw = getpwuid(id))) 321 return(pw); 322 errx(1, "%s: no such user", u); 323 /* NOTREACHED */ 324 } 325 326 void 327 pline(pw) 328 struct passwd *pw; 329 { 330 struct group *gr; 331 u_int eid, rid; 332 char *login; 333 334 if (!pw) { 335 if ((pw = getpwuid(rid = getuid())) == NULL) 336 err(1, "getpwuid"); 337 } 338 339 (void)printf("%s:%s:%d:%d:%s:%d:%d:%s:%s:%s\n", pw->pw_name, 340 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 341 pw->pw_change, pw->pw_expire, pw->pw_gecos, 342 pw->pw_dir, pw->pw_shell); 343 } 344 345 346 void 347 usage() 348 { 349 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 350 "usage: id [user]", 351 " id -G [-n] [user]", 352 " id -g [-nr] [user]", 353 " id -u [-nr] [user]"); 354 exit(1); 355 } 356