xref: /titanic_50/usr/src/cmd/ptools/ppriv/ppriv.c (revision 35fe197b91640f2efc8c0b3849eee882e373c729)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Program to examine or set process privileges.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include <libproc.h>
38 #include <priv.h>
39 #include <errno.h>
40 #include <ctype.h>
41 
42 #include <locale.h>
43 #include <langinfo.h>
44 
45 static int	look(char *);
46 static void	perr(char *);
47 static void	usage(void);
48 static void	loadprivinfo(void);
49 static int	parsespec(const char *);
50 static void	privupdate(prpriv_t *, const char *);
51 static void	privupdate_self(void);
52 static int	dumppriv(char **);
53 static void	flags2str(uint_t);
54 
55 static char		*command;
56 static char		*procname;
57 static boolean_t	verb = B_FALSE;
58 static boolean_t	set = B_FALSE;
59 static boolean_t	exec = B_FALSE;
60 static boolean_t	Don = B_FALSE;
61 static boolean_t	Doff = B_FALSE;
62 static boolean_t	list = B_FALSE;
63 static boolean_t	mac_aware = B_FALSE;
64 static int		mode = PRIV_STR_PORT;
65 
66 int
67 main(int argc, char **argv)
68 {
69 	int rc = 0;
70 	int opt;
71 	struct rlimit rlim;
72 
73 	(void) setlocale(LC_ALL, "");
74 	(void) textdomain(TEXT_DOMAIN);
75 
76 	if ((command = strrchr(argv[0], '/')) != NULL)
77 		command++;
78 	else
79 		command = argv[0];
80 
81 	while ((opt = getopt(argc, argv, "lDMNevs:S")) != EOF) {
82 		switch (opt) {
83 		case 'l':
84 			list = B_TRUE;
85 			break;
86 		case 'D':
87 			set = B_TRUE;
88 			Don = B_TRUE;
89 			break;
90 		case 'M':
91 			mac_aware = B_TRUE;
92 			break;
93 		case 'N':
94 			set = B_TRUE;
95 			Doff = B_TRUE;
96 			break;
97 		case 'e':
98 			exec = B_TRUE;
99 			break;
100 		case 'S':
101 			mode = PRIV_STR_SHORT;
102 			break;
103 		case 'v':
104 			verb = B_TRUE;
105 			mode = PRIV_STR_LIT;
106 			break;
107 		case 's':
108 			set = B_TRUE;
109 			if ((rc = parsespec(optarg)) != 0)
110 				return (rc);
111 			break;
112 		default:
113 			usage();
114 			/*NOTREACHED*/
115 		}
116 	}
117 
118 	argc -= optind;
119 	argv += optind;
120 
121 	if ((argc < 1 && !list) || Doff && Don || list && (set || exec) ||
122 	    (mac_aware && !exec))
123 		usage();
124 
125 	/*
126 	 * Make sure we'll have enough file descriptors to handle a target
127 	 * that has many many mappings.
128 	 */
129 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
130 		rlim.rlim_cur = rlim.rlim_max;
131 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
132 	}
133 
134 	if (exec) {
135 		privupdate_self();
136 		rc = execvp(argv[0], &argv[0]);
137 		(void) fprintf(stderr, "%s: %s: %s\n", command, argv[0],
138 			strerror(errno));
139 	} else if (list) {
140 		rc = dumppriv(argv);
141 	} else {
142 		while (argc-- > 0)
143 			rc += look(*argv++);
144 	}
145 
146 	return (rc);
147 }
148 
149 static int
150 look(char *arg)
151 {
152 	static size_t pprivsz = sizeof (prpriv_t);
153 	static prpriv_t *ppriv;
154 
155 	struct ps_prochandle *Pr;
156 	int gcode;
157 	size_t sz;
158 	void *pdata;
159 	char *x;
160 	int i;
161 	boolean_t nodata;
162 
163 	procname = arg;		/* for perr() */
164 
165 	if ((Pr = proc_arg_grab(arg, set ? PR_ARG_PIDS : PR_ARG_ANY,
166 	    PGRAB_RETAIN | PGRAB_FORCE | (set ? 0 : PGRAB_RDONLY) |
167 	    PGRAB_NOSTOP, &gcode)) == NULL) {
168 		(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
169 		    command, arg, Pgrab_error(gcode));
170 		return (1);
171 	}
172 
173 	if (ppriv == NULL)
174 		ppriv = malloc(pprivsz);
175 
176 	if (Ppriv(Pr, ppriv, pprivsz) == -1) {
177 		perr(command);
178 		Prelease(Pr, 0);
179 		return (1);
180 	}
181 
182 	sz = PRIV_PRPRIV_SIZE(ppriv);
183 
184 	/*
185 	 * The ppriv fields are unsigned and may overflow, so check them
186 	 * separately.  Size must be word aligned, so check that too.
187 	 * Make sure size is "smallish" too.
188 	 */
189 	if ((sz & 3) || ppriv->pr_nsets == 0 ||
190 	    sz / ppriv->pr_nsets < ppriv->pr_setsize ||
191 	    ppriv->pr_infosize > sz || sz > 1024 * 1024) {
192 		(void) fprintf(stderr,
193 			"%s: %s: bad PRNOTES section, size = %lx\n",
194 			command, arg, (long)sz);
195 		Prelease(Pr, 0);
196 		return (1);
197 	}
198 
199 	if (sz > pprivsz) {
200 		ppriv = realloc(ppriv, sz);
201 
202 		if (ppriv == NULL || Ppriv(Pr, ppriv, sz) != sz) {
203 			perr(command);
204 			Prelease(Pr, 0);
205 			return (1);
206 		}
207 		pprivsz = sz;
208 	}
209 
210 	if (set) {
211 		privupdate(ppriv, arg);
212 		if (Psetpriv(Pr, ppriv) != 0) {
213 			perr(command);
214 			Prelease(Pr, 0);
215 			return (1);
216 		}
217 		Prelease(Pr, 0);
218 		return (0);
219 	}
220 
221 	if (Pstate(Pr) == PS_DEAD) {
222 		(void) printf("core '%s' of %d:\t%.70s\n",
223 		    arg, (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
224 		pdata = Pprivinfo(Pr);
225 		nodata = Pstate(Pr) == PS_DEAD && pdata == NULL;
226 	} else {
227 		(void) printf("%d:\t%.70s\n",
228 		    (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
229 		pdata = NULL;
230 		nodata = B_FALSE;
231 	}
232 
233 	x = (char *)ppriv + sz - ppriv->pr_infosize;
234 	while (x < (char *)ppriv + sz) {
235 		/* LINTED: alignment */
236 		priv_info_t *pi = (priv_info_t *)x;
237 		priv_info_uint_t *pii;
238 
239 		switch (pi->priv_info_type) {
240 		case PRIV_INFO_FLAGS:
241 			/* LINTED: alignment */
242 			pii = (priv_info_uint_t *)x;
243 			(void) printf("flags =");
244 			flags2str(pii->val);
245 			(void) putchar('\n');
246 			break;
247 		default:
248 			(void) fprintf(stderr, "%s: unknown priv_info: %d\n",
249 				arg, pi->priv_info_type);
250 			break;
251 		}
252 		if (pi->priv_info_size > ppriv->pr_infosize ||
253 		    pi->priv_info_size <=  sizeof (priv_info_t) ||
254 		    (pi->priv_info_size & 3) != 0) {
255 			(void) fprintf(stderr, "%s: bad priv_info_size: %u\n",
256 				arg, pi->priv_info_size);
257 			break;
258 		}
259 		x += pi->priv_info_size;
260 	}
261 
262 	for (i = 0; i < ppriv->pr_nsets; i++) {
263 		extern const char *__priv_getsetbynum(const void *, int);
264 		const char *setnm = pdata ? __priv_getsetbynum(pdata, i)
265 					    : priv_getsetbynum(i);
266 		priv_chunk_t *pc = (priv_chunk_t *)
267 				    &ppriv->pr_sets[ppriv->pr_setsize * i];
268 
269 
270 		(void) printf("\t%c: ", setnm && !nodata ? *setnm : '?');
271 		if (!nodata) {
272 			extern char *__priv_set_to_str(void *,
273 					const priv_set_t *, char, int);
274 			priv_set_t *pset = (priv_set_t *)pc;
275 
276 			char *s;
277 
278 			if (pdata)
279 				s = __priv_set_to_str(pdata, pset, ',', mode);
280 			else
281 				s = priv_set_to_str(pset, ',', mode);
282 			(void) puts(s);
283 			free(s);
284 		} else {
285 			int j;
286 			for (j = 0; j < ppriv->pr_setsize; j++)
287 				(void) printf("%08x", pc[j]);
288 			(void) putchar('\n');
289 		}
290 	}
291 	Prelease(Pr, 0);
292 	return (0);
293 }
294 
295 static void
296 fatal(const char *s)
297 {
298 	(void) fprintf(stderr, "%s: %s: %s\n", command, s, strerror(errno));
299 	exit(3);
300 }
301 
302 static void
303 perr(char *s)
304 {
305 	int err = errno;
306 
307 	if (s != NULL)
308 		(void) fprintf(stderr, "%s: ", procname);
309 	else
310 		s = procname;
311 
312 	errno = err;
313 	perror(s);
314 }
315 
316 static void
317 usage(void)
318 {
319 	(void) fprintf(stderr,
320 	    "usage:\t%s [-v] [-S] [-D|-N] [-s spec] { pid | core } ...\n"
321 	    "\t%s -e [-D|-N] [-M] [-s spec] cmd [args ...]\n"
322 	    "\t%s -l [-v] [privilege ...]\n"
323 	    "  (report, set or list process privileges)\n", command,
324 	    command, command);
325 	exit(2);
326 	/*NOTREACHED*/
327 }
328 
329 /*
330  * Parse the privilege bits to add and/or remove from
331  * a privilege set.
332  *
333  * [EPIL][+-=]priv,priv,priv
334  */
335 
336 static int
337 strindex(char c, const char *str)
338 {
339 	const char *s;
340 
341 	if (islower(c))
342 		c = toupper(c);
343 
344 	s = strchr(str, c);
345 
346 	if (s == NULL)
347 		return (-1);
348 	else
349 		return (s - str);
350 }
351 
352 static void
353 badspec(const char *spec)
354 {
355 	(void) fprintf(stderr, "%s: bad privilege specification: \"%s\"\n",
356 				command, spec);
357 	exit(3);
358 	/*NOTREACHED*/
359 }
360 
361 /*
362  * For each set, you can set either add and/or
363  * remove or you can set assign.
364  */
365 static priv_set_t **rem, **add, **assign;
366 static const priv_impl_info_t *pri = NULL;
367 static char *sets;
368 
369 static void
370 loadprivinfo(void)
371 {
372 	int i;
373 
374 	if (pri != NULL)
375 		return;
376 
377 	pri = getprivimplinfo();
378 
379 	if (pri == NULL)
380 		fatal("getprivimplinfo");
381 
382 	sets = malloc(pri->priv_nsets + 1);
383 	if (sets == NULL)
384 		fatal("malloc");
385 
386 	for (i = 0; i < pri->priv_nsets; i++) {
387 		sets[i] = *priv_getsetbynum(i);
388 		if (islower(sets[i]))
389 			sets[i] = toupper(sets[i]);
390 	}
391 
392 	sets[pri->priv_nsets] = '\0';
393 
394 	rem = calloc(pri->priv_nsets, sizeof (priv_set_t *));
395 	add = calloc(pri->priv_nsets, sizeof (priv_set_t *));
396 	assign = calloc(pri->priv_nsets, sizeof (priv_set_t *));
397 	if (rem == NULL || add == NULL || assign == NULL)
398 		fatal("calloc");
399 }
400 
401 static int
402 parsespec(const char *spec)
403 {
404 	char *p;
405 	const char *q;
406 	int count;
407 	priv_set_t ***toupd;
408 	priv_set_t *upd;
409 	int i;
410 	boolean_t freeupd = B_TRUE;
411 
412 	if (pri == NULL)
413 		loadprivinfo();
414 
415 	p = strpbrk(spec, "+-=");
416 
417 	if (p == NULL || p - spec > pri->priv_nsets)
418 		badspec(spec);
419 
420 	if (p[1] == '\0' || (upd = priv_str_to_set(p + 1, ",", NULL)) == NULL)
421 		badspec(p + 1);
422 
423 	count = p - spec;
424 	switch (*p) {
425 	case '+':
426 		toupd = &add;
427 		break;
428 	case '-':
429 		toupd = &rem;
430 		priv_inverse(upd);
431 		break;
432 	case '=':
433 		toupd = &assign;
434 		break;
435 	}
436 
437 	/* Update all sets? */
438 	if (count == 0 || *spec == 'a' || *spec == 'A') {
439 		count = pri->priv_nsets;
440 		q = sets;
441 	} else
442 		q = spec;
443 
444 	for (i = 0; i < count; i++) {
445 		int ind = strindex(q[i], sets);
446 
447 		if (ind == -1)
448 			badspec(spec);
449 
450 		/* Assign is mutually exclusive with add/remove and itself */
451 		if (((toupd == &rem || toupd == &add) && assign[ind] != NULL) ||
452 		    (toupd == &assign && (assign[ind] != NULL ||
453 			    rem[ind] != NULL || add[ind] != NULL))) {
454 			(void) fprintf(stderr, "%s: conflicting spec: %s\n",
455 					command, spec);
456 			exit(1);
457 		}
458 		if ((*toupd)[ind] != NULL) {
459 			if (*p == '-')
460 				priv_intersect(upd, (*toupd)[ind]);
461 			else
462 				priv_union(upd, (*toupd)[ind]);
463 		} else {
464 			(*toupd)[ind] = upd;
465 			freeupd = B_FALSE;
466 		}
467 	}
468 	if (freeupd)
469 		priv_freeset(upd);
470 	return (0);
471 }
472 
473 static void
474 privupdate(prpriv_t *pr, const char *arg)
475 {
476 	int i;
477 
478 	if (sets != NULL) {
479 		for (i = 0; i < pri->priv_nsets; i++) {
480 			priv_set_t *target =
481 				(priv_set_t *)&pr->pr_sets[pr->pr_setsize * i];
482 			if (rem[i] != NULL)
483 				priv_intersect(rem[i], target);
484 			if (add[i] != NULL)
485 				priv_union(add[i], target);
486 			if (assign[i] != NULL)
487 				priv_copyset(assign[i], target);
488 		}
489 	}
490 
491 	if (Doff || Don) {
492 		priv_info_uint_t *pii;
493 		int sz = PRIV_PRPRIV_SIZE(pr);
494 		char *x = (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr);
495 		uint32_t fl = 0;
496 
497 		while (x < (char *)pr + sz) {
498 			/* LINTED: alignment */
499 			priv_info_t *pi = (priv_info_t *)x;
500 
501 			if (pi->priv_info_type == PRIV_INFO_FLAGS) {
502 				/* LINTED: alignment */
503 				pii = (priv_info_uint_t *)x;
504 				fl = pii->val;
505 				goto done;
506 			}
507 			if (pi->priv_info_size > pr->pr_infosize ||
508 			    pi->priv_info_size <=  sizeof (priv_info_t) ||
509 			    (pi->priv_info_size & 3) != 0)
510 				break;
511 			x += pi->priv_info_size;
512 		}
513 		(void) fprintf(stderr,
514 			"%s: cannot find privilege flags to set\n", arg);
515 		pr->pr_infosize = 0;
516 		return;
517 done:
518 
519 		pr->pr_infosize = sizeof (priv_info_uint_t);
520 		/* LINTED: alignment */
521 		pii = (priv_info_uint_t *)
522 			    ((char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
523 
524 		if (Don)
525 			fl |= PRIV_DEBUG;
526 		else
527 			fl &= ~PRIV_DEBUG;
528 
529 		pii->info.priv_info_size = sizeof (*pii);
530 		pii->info.priv_info_type = PRIV_INFO_FLAGS;
531 		pii->val = fl;
532 	} else {
533 		pr->pr_infosize = 0;
534 	}
535 }
536 
537 static void
538 privupdate_self(void)
539 {
540 	int set;
541 
542 	if (mac_aware) {
543 		if (setpflags(NET_MAC_AWARE, 1) != 0)
544 			fatal("setpflags(NET_MAC_AWARE)");
545 		if (setpflags(NET_MAC_AWARE_INHERIT, 1) != 0)
546 			fatal("setpflags(NET_MAC_AWARE_INHERIT)");
547 	}
548 
549 	if (sets != NULL) {
550 		priv_set_t *target = priv_allocset();
551 
552 		if (target == NULL)
553 			fatal("priv_allocet");
554 
555 		set = priv_getsetbyname(PRIV_INHERITABLE);
556 		if (rem[set] != NULL || add[set] != NULL ||
557 		    assign[set] != NULL) {
558 			(void) getppriv(PRIV_INHERITABLE, target);
559 			if (rem[set] != NULL)
560 				priv_intersect(rem[set], target);
561 			if (add[set] != NULL)
562 				priv_union(add[set], target);
563 			if (assign[set] != NULL)
564 				priv_copyset(assign[set], target);
565 			if (setppriv(PRIV_SET, PRIV_INHERITABLE, target) != 0)
566 				fatal("setppriv(Inheritable)");
567 		}
568 		set = priv_getsetbyname(PRIV_LIMIT);
569 		if (rem[set] != NULL || add[set] != NULL ||
570 		    assign[set] != NULL) {
571 			(void) getppriv(PRIV_LIMIT, target);
572 			if (rem[set] != NULL)
573 				priv_intersect(rem[set], target);
574 			if (add[set] != NULL)
575 				priv_union(add[set], target);
576 			if (assign[set] != NULL)
577 				priv_copyset(assign[set], target);
578 			if (setppriv(PRIV_SET, PRIV_LIMIT, target) != 0)
579 				fatal("setppriv(Limit)");
580 		}
581 		priv_freeset(target);
582 	}
583 
584 	if (Doff || Don)
585 		(void) setpflags(PRIV_DEBUG, Don ? 1 : 0);
586 }
587 
588 static int
589 dopriv(const char *p)
590 {
591 	(void) puts(p);
592 	if (verb) {
593 		char *text = priv_gettext(p);
594 		char *p, *q;
595 		if (text == NULL)
596 			return (1);
597 		for (p = text; q = strchr(p, '\n'); p = q + 1)
598 			(void) printf("\t%.*s", (int)(q - p + 1), p);
599 		free(text);
600 	}
601 	return (0);
602 }
603 
604 static int
605 dumppriv(char **argv)
606 {
607 	int rc = 0;
608 	const char *pname;
609 	int i;
610 
611 	if (argv[0] == NULL) {
612 		for (i = 0; ((pname = priv_getbynum(i++)) != NULL); )
613 			rc += dopriv(pname);
614 	} else {
615 		for (; *argv; argv++) {
616 			priv_set_t *pset = priv_str_to_set(*argv, ",", NULL);
617 
618 			if (pset == NULL) {
619 				(void) fprintf(stderr, "%s: %s: bad privilege"
620 				    " list\n", command, *argv);
621 				rc++;
622 				continue;
623 			}
624 			for (i = 0; ((pname = priv_getbynum(i++)) != NULL); )
625 				if (priv_ismember(pset, pname))
626 					rc += dopriv(pname);
627 		}
628 	}
629 	return (rc);
630 }
631 
632 static struct {
633 	int flag;
634 	char *name;
635 } flags[] = {
636 	{ PRIV_DEBUG, "PRIV_DEBUG" },
637 	{ PRIV_AWARE, "PRIV_AWARE" },
638 	{ PRIV_AWARE_INHERIT, "PRIV_AWARE_INHERIT" },
639 };
640 
641 /*
642  * Print flags preceeded by a space.
643  */
644 static void
645 flags2str(uint_t pflags)
646 {
647 	char c = ' ';
648 	int i;
649 
650 	if (pflags == 0) {
651 		(void) fputs(" <none>", stdout);
652 		return;
653 	}
654 	for (i = 0; i < sizeof (flags)/sizeof (flags[0]) && pflags != 0; i++) {
655 		if ((pflags & flags[i].flag) != 0) {
656 			(void) printf("%c%s", c, flags[i].name);
657 			pflags &= ~flags[i].flag;
658 			c = '|';
659 		}
660 	}
661 	if (pflags != 0)
662 		(void) printf("%c<0x%x>", c, pflags);
663 }
664