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 35static 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 42static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94"; 43#endif 44static const char rcsid[] =
| 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 35static 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 42static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94"; 43#endif 44static const char rcsid[] =
|
45 "$Id: id.c,v 1.6 1998/02/18 17:35:16 steve Exp $";
| 45 "$Id: id.c,v 1.7 1998/08/21 06:47:58 obrien 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 58void current __P((void)); 59void pline __P((struct passwd *)); 60void pretty __P((struct passwd *)); 61void group __P((struct passwd *, int)); 62void usage __P((void)); 63void user __P((struct passwd *)); 64struct passwd * 65 who __P((char *)); 66 67int 68main(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 160void 161pretty(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
| 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 58void current __P((void)); 59void pline __P((struct passwd *)); 60void pretty __P((struct passwd *)); 61void group __P((struct passwd *, int)); 62void usage __P((void)); 63void user __P((struct passwd *)); 64struct passwd * 65 who __P((char *)); 66 67int 68main(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 160void 161pretty(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)
| 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);
| 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()))
| 189 } 190 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);
| 191 if ((gr = getgrgid(rid))) 192 (void)printf("rgid\t%s\n", gr->gr_name); 193 else 194 (void)printf("rgid\t%u\n", rid);
|
| 195 }
|
194 (void)printf("groups\t"); 195 group(NULL, 1); 196 } 197} 198 199void 200current() 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 240void 241user(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 268void 269group(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 305struct passwd * 306who(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 326void 327pline(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 346void 347usage() 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}
| 196 (void)printf("groups\t"); 197 group(NULL, 1); 198 } 199} 200 201void 202current() 203{ 204 struct group *gr; 205 struct passwd *pw; 206 int cnt, id, eid, lastid, ngroups; 207 gid_t groups[NGROUPS]; 208 char *fmt; 209 210 id = getuid(); 211 (void)printf("uid=%u", id); 212 if ((pw = getpwuid(id))) 213 (void)printf("(%s)", pw->pw_name); 214 if ((eid = geteuid()) != id) { 215 (void)printf(" euid=%u", eid); 216 if ((pw = getpwuid(eid))) 217 (void)printf("(%s)", pw->pw_name); 218 } 219 id = getgid(); 220 (void)printf(" gid=%u", id); 221 if ((gr = getgrgid(id))) 222 (void)printf("(%s)", gr->gr_name); 223 if ((eid = getegid()) != id) { 224 (void)printf(" egid=%u", eid); 225 if ((gr = getgrgid(eid))) 226 (void)printf("(%s)", gr->gr_name); 227 } 228 if ((ngroups = getgroups(NGROUPS, groups))) { 229 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups; 230 fmt = ", %u", lastid = id) { 231 id = groups[cnt++]; 232 if (lastid == id) 233 continue; 234 (void)printf(fmt, id); 235 if ((gr = getgrgid(id))) 236 (void)printf("(%s)", gr->gr_name); 237 } 238 } 239 (void)printf("\n"); 240} 241 242void 243user(pw) 244 register struct passwd *pw; 245{ 246 register struct group *gr; 247 register char *fmt; 248 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1]; 249 250 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name); 251 gid = pw->pw_gid; 252 (void)printf(" gid=%u", gid); 253 if ((gr = getgrgid(gid))) 254 (void)printf("(%s)", gr->gr_name); 255 ngroups = NGROUPS + 1; 256 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups); 257 fmt = " groups=%u"; 258 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { 259 if (lastgid == (gid = groups[cnt])) 260 continue; 261 (void)printf(fmt, gid); 262 fmt = " %u"; 263 if ((gr = getgrgid(gid))) 264 (void)printf("(%s)", gr->gr_name); 265 lastgid = gid; 266 } 267 (void)printf("\n"); 268} 269 270void 271group(pw, nflag) 272 struct passwd *pw; 273 int nflag; 274{ 275 struct group *gr; 276 int cnt, id, lastid, ngroups; 277 gid_t groups[NGROUPS + 1]; 278 char *fmt; 279 280 if (pw) { 281 ngroups = NGROUPS + 1; 282 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); 283 } else { 284 groups[0] = getgid(); 285 ngroups = getgroups(NGROUPS, groups + 1) + 1; 286 } 287 fmt = nflag ? "%s" : "%u"; 288 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { 289 if (lastid == (id = groups[cnt])) 290 continue; 291 if (nflag) { 292 if ((gr = getgrgid(id))) 293 (void)printf(fmt, gr->gr_name); 294 else 295 (void)printf(*fmt == ' ' ? " %u" : "%u", 296 id); 297 fmt = " %s"; 298 } else { 299 (void)printf(fmt, id); 300 fmt = " %u"; 301 } 302 lastid = id; 303 } 304 (void)printf("\n"); 305} 306 307struct passwd * 308who(u) 309 char *u; 310{ 311 struct passwd *pw; 312 long id; 313 char *ep; 314 315 /* 316 * Translate user argument into a pw pointer. First, try to 317 * get it as specified. If that fails, try it as a number. 318 */ 319 if ((pw = getpwnam(u))) 320 return(pw); 321 id = strtol(u, &ep, 10); 322 if (*u && !*ep && (pw = getpwuid(id))) 323 return(pw); 324 errx(1, "%s: no such user", u); 325 /* NOTREACHED */ 326} 327 328void 329pline(pw) 330 struct passwd *pw; 331{ 332 struct group *gr; 333 u_int eid, rid; 334 char *login; 335 336 if (!pw) { 337 if ((pw = getpwuid(rid = getuid())) == NULL) 338 err(1, "getpwuid"); 339 } 340 341 (void)printf("%s:%s:%d:%d:%s:%d:%d:%s:%s:%s\n", pw->pw_name, 342 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 343 pw->pw_change, pw->pw_expire, pw->pw_gecos, 344 pw->pw_dir, pw->pw_shell); 345} 346 347 348void 349usage() 350{ 351 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 352 "usage: id [user]", 353 " id -G [-n] [user]", 354 " id -g [-nr] [user]", 355 " id -u [-nr] [user]"); 356 exit(1); 357}
|