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