xref: /onnv-gate/usr/src/cmd/ptools/pcred/pcred.c (revision 4321:a8930ec16e52)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
28*4321Scasper #include <errno.h>
290Sstevel@tonic-gate #include <stdio.h>
301914Scasper #include <stdio_ext.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <pwd.h>
380Sstevel@tonic-gate #include <grp.h>
390Sstevel@tonic-gate #include <libproc.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int look(char *);
440Sstevel@tonic-gate static int perr(char *);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void usage(void);
470Sstevel@tonic-gate static void initcred(void);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static char *command;
500Sstevel@tonic-gate static char *procname;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static char *user;
530Sstevel@tonic-gate static char *group;
540Sstevel@tonic-gate static char *grplst;
550Sstevel@tonic-gate static char *login;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static boolean_t all = B_FALSE;
580Sstevel@tonic-gate static boolean_t doset = B_FALSE;
590Sstevel@tonic-gate static int ngrp = -1;
600Sstevel@tonic-gate static gid_t *groups;
610Sstevel@tonic-gate static long ngroups_max;
620Sstevel@tonic-gate 
63*4321Scasper static uid_t uid = (uid_t)-1;
64*4321Scasper static gid_t gid = (gid_t)-1;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate int
main(int argc,char ** argv)670Sstevel@tonic-gate main(int argc, char **argv)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	int rc = 0;
700Sstevel@tonic-gate 	int c;
710Sstevel@tonic-gate 	struct rlimit rlim;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
740Sstevel@tonic-gate 		command++;
750Sstevel@tonic-gate 	else
760Sstevel@tonic-gate 		command = argv[0];
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) < 0)
790Sstevel@tonic-gate 		return (perr("sysconf(_SC_NGROUPS_MAX)"));
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	opterr = 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "au:g:l:G:")) != EOF) {
840Sstevel@tonic-gate 		switch (c) {
850Sstevel@tonic-gate 		case 'a':
860Sstevel@tonic-gate 			all = B_TRUE;
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 		case 'u':
890Sstevel@tonic-gate 			user = optarg;
900Sstevel@tonic-gate 			doset = B_TRUE;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		case 'g':
930Sstevel@tonic-gate 			group = optarg;
940Sstevel@tonic-gate 			doset = B_TRUE;
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		case 'G':
970Sstevel@tonic-gate 			grplst = optarg;
980Sstevel@tonic-gate 			doset = B_TRUE;
990Sstevel@tonic-gate 			break;
1000Sstevel@tonic-gate 		case 'l':
1010Sstevel@tonic-gate 			login = optarg;
1020Sstevel@tonic-gate 			doset = B_TRUE;
1030Sstevel@tonic-gate 			break;
1040Sstevel@tonic-gate 		default:
1050Sstevel@tonic-gate 			usage();
1060Sstevel@tonic-gate 			/*NOTREACHED*/
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	if (login != NULL && (user != NULL || group != NULL || grplst != NULL))
1100Sstevel@tonic-gate 		usage();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (all && doset)
1130Sstevel@tonic-gate 		usage();
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	argc -= optind;
1160Sstevel@tonic-gate 	argv += optind;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (argc == 0)
1190Sstevel@tonic-gate 		usage();
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (doset)
1220Sstevel@tonic-gate 		initcred();
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
1260Sstevel@tonic-gate 	 * that has many many mappings.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1290Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
1300Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
1311914Scasper 		(void) enable_extended_FILE_stdio(-1, -1);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	while (argc-- > 0)
1350Sstevel@tonic-gate 		rc += look(*argv++);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (rc > 255 ? 255 : rc);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static void
credupdate(prcred_t * pcr)1410Sstevel@tonic-gate credupdate(prcred_t *pcr)
1420Sstevel@tonic-gate {
143*4321Scasper 	if (uid != (uid_t)-1)
1440Sstevel@tonic-gate 		pcr->pr_euid = pcr->pr_ruid = pcr->pr_suid = uid;
145*4321Scasper 	if (gid != (gid_t)-1)
1460Sstevel@tonic-gate 		pcr->pr_egid = pcr->pr_rgid = pcr->pr_sgid = gid;
1470Sstevel@tonic-gate 	if (ngrp >= 0) {
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		pcr->pr_ngroups = ngrp;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		(void) memcpy(pcr->pr_groups, groups, ngrp * sizeof (gid_t));
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static int
look(char * arg)1560Sstevel@tonic-gate look(char *arg)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct ps_prochandle *Pr;
1590Sstevel@tonic-gate 	static prcred_t *prcred = NULL;
1600Sstevel@tonic-gate 	int gcode;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	procname = arg;		/* for perr() */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (prcred == NULL) {
1650Sstevel@tonic-gate 		prcred = malloc(sizeof (prcred_t) +
1660Sstevel@tonic-gate 			(ngroups_max - 1) * sizeof (gid_t));
1670Sstevel@tonic-gate 		if (prcred == NULL) {
1680Sstevel@tonic-gate 			(void) perr("malloc");
1690Sstevel@tonic-gate 			exit(1);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if ((Pr = proc_arg_grab(arg, doset ? PR_ARG_PIDS : PR_ARG_ANY,
1740Sstevel@tonic-gate 	    PGRAB_RETAIN | PGRAB_FORCE | (doset ? 0 : PGRAB_RDONLY) |
1750Sstevel@tonic-gate 	    PGRAB_NOSTOP, &gcode)) == NULL) {
1760Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1770Sstevel@tonic-gate 		    command, arg, Pgrab_error(gcode));
1780Sstevel@tonic-gate 		return (1);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (Pcred(Pr, prcred, ngroups_max) == -1) {
1820Sstevel@tonic-gate 		(void) perr("getcred");
1830Sstevel@tonic-gate 		Prelease(Pr, 0);
1840Sstevel@tonic-gate 		return (1);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (doset) {
1880Sstevel@tonic-gate 		credupdate(prcred);
1890Sstevel@tonic-gate 		if (Psetcred(Pr, prcred) != 0) {
1900Sstevel@tonic-gate 			(void) perr("setcred");
1910Sstevel@tonic-gate 			Prelease(Pr, 0);
1920Sstevel@tonic-gate 			return (1);
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		Prelease(Pr, 0);
1950Sstevel@tonic-gate 		return (0);
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (Pstate(Pr) == PS_DEAD)
1990Sstevel@tonic-gate 		(void) printf("core of %d:\t", (int)Pstatus(Pr)->pr_pid);
2000Sstevel@tonic-gate 	else
2010Sstevel@tonic-gate 		(void) printf("%d:\t", (int)Pstatus(Pr)->pr_pid);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (!all &&
2040Sstevel@tonic-gate 	    prcred->pr_euid == prcred->pr_ruid &&
2050Sstevel@tonic-gate 	    prcred->pr_ruid == prcred->pr_suid)
206*4321Scasper 		(void) printf("e/r/suid=%u  ", prcred->pr_euid);
2070Sstevel@tonic-gate 	else
208*4321Scasper 		(void) printf("euid=%u ruid=%u suid=%u  ",
209*4321Scasper 			prcred->pr_euid, prcred->pr_ruid, prcred->pr_suid);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (!all &&
2120Sstevel@tonic-gate 	    prcred->pr_egid == prcred->pr_rgid &&
2130Sstevel@tonic-gate 	    prcred->pr_rgid == prcred->pr_sgid)
214*4321Scasper 		(void) printf("e/r/sgid=%u\n", prcred->pr_egid);
2150Sstevel@tonic-gate 	else
216*4321Scasper 		(void) printf("egid=%u rgid=%u sgid=%u\n",
217*4321Scasper 			prcred->pr_egid, prcred->pr_rgid, prcred->pr_sgid);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (prcred->pr_ngroups != 0 &&
2200Sstevel@tonic-gate 	    (all || prcred->pr_ngroups != 1 ||
2210Sstevel@tonic-gate 	    prcred->pr_groups[0] != prcred->pr_rgid)) {
2220Sstevel@tonic-gate 		int i;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		(void) printf("\tgroups:");
2250Sstevel@tonic-gate 		for (i = 0; i < prcred->pr_ngroups; i++)
226*4321Scasper 			(void) printf(" %u", prcred->pr_groups[i]);
2270Sstevel@tonic-gate 		(void) printf("\n");
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	Prelease(Pr, 0);
2310Sstevel@tonic-gate 	return (0);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static int
perr(char * s)2350Sstevel@tonic-gate perr(char *s)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	if (s)
2380Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", procname);
2390Sstevel@tonic-gate 	else
2400Sstevel@tonic-gate 		s = procname;
2410Sstevel@tonic-gate 	perror(s);
2420Sstevel@tonic-gate 	return (1);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static void
usage(void)2460Sstevel@tonic-gate usage(void)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	(void) fprintf(stderr, "usage:\t%s [-a] { pid | core } ...\n"
2490Sstevel@tonic-gate 	    "\t%s [-u user] [-g group] [-G groups] pid ...\n"
2500Sstevel@tonic-gate 	    "\t%s -l login pid ...\n"
2510Sstevel@tonic-gate 	    "  (report or modify process credentials)\n",
2520Sstevel@tonic-gate 	    command, command, command);
2530Sstevel@tonic-gate 	exit(2);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 
257*4321Scasper static uint32_t
str2id(const char * str)2580Sstevel@tonic-gate str2id(const char *str)
2590Sstevel@tonic-gate {
260*4321Scasper 	unsigned long res;
2610Sstevel@tonic-gate 	char *p;
2620Sstevel@tonic-gate 
263*4321Scasper 	errno = 0;
264*4321Scasper 	res = strtoul(str, &p, 0);
265*4321Scasper 	if (p == str || *p != '\0' || errno != 0)
266*4321Scasper 		return ((uint32_t)-1);
2670Sstevel@tonic-gate 	else
268*4321Scasper 		return ((uint32_t)res);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static gid_t
str2gid(const char * grnam)2720Sstevel@tonic-gate str2gid(const char *grnam)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	struct group *grp = getgrnam(grnam);
2750Sstevel@tonic-gate 	gid_t res;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (grp == NULL) {
278*4321Scasper 		res = (gid_t)str2id(grnam);
279*4321Scasper 		if (res == (gid_t)-1) {
2800Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: unknown group"
2810Sstevel@tonic-gate 			    " or bad gid\n",
2820Sstevel@tonic-gate 			    command, grnam);
2830Sstevel@tonic-gate 			exit(1);
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 	} else {
2860Sstevel@tonic-gate 		res = grp->gr_gid;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	return (res);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate static void
initcred(void)2920Sstevel@tonic-gate initcred(void)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	struct passwd *pwd;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if ((groups = malloc(ngroups_max * sizeof (gid_t))) == NULL) {
2970Sstevel@tonic-gate 		(void) perr("malloc");
2980Sstevel@tonic-gate 		exit(1);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (login != NULL) {
3020Sstevel@tonic-gate 		pwd = getpwnam(login);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		if (pwd == NULL) {
3050Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: unknown user\n",
3060Sstevel@tonic-gate 			    command, login);
3070Sstevel@tonic-gate 			exit(1);
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 		uid = pwd->pw_uid;
3100Sstevel@tonic-gate 		gid = pwd->pw_gid;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		groups[0] = gid;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		ngrp = _getgroupsbymember(login, groups, (int)ngroups_max, 1);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (user != NULL) {
3180Sstevel@tonic-gate 		pwd = getpwnam(user);
3190Sstevel@tonic-gate 		if (pwd == NULL) {
320*4321Scasper 			uid = (uid_t)str2id(user);
321*4321Scasper 			if (uid == (uid_t)-1) {
3220Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: %s: unknown user"
3230Sstevel@tonic-gate 				    " or bad uid\n",
3240Sstevel@tonic-gate 				    command, user);
3250Sstevel@tonic-gate 				exit(1);
3260Sstevel@tonic-gate 			}
3270Sstevel@tonic-gate 		} else {
3280Sstevel@tonic-gate 			uid = pwd->pw_uid;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (group != NULL)
3330Sstevel@tonic-gate 		gid = str2gid(group);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (grplst != NULL) {
3360Sstevel@tonic-gate 		char *cgrp;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		ngrp = 0;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		while ((cgrp = strtok(grplst, ",")) != NULL) {
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 			if (ngrp >= ngroups_max) {
3430Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: Too many groups\n",
3440Sstevel@tonic-gate 				    command);
3450Sstevel@tonic-gate 				exit(1);
3460Sstevel@tonic-gate 			}
3470Sstevel@tonic-gate 			groups[ngrp++] = str2gid(cgrp);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 			/* For iterations of strtok */
3500Sstevel@tonic-gate 			grplst = NULL;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate }
354