dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ptools / ppriv / ppriv.c
blob74b5485357da6a50c2242d0ac594b9fa541d6614
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright 2015, Joyent, Inc.
29 * Program to examine or set process privileges.
32 #include <stdio.h>
33 #include <stdio_ext.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <sys/types.h>
40 #include <libproc.h>
41 #include <priv.h>
42 #include <errno.h>
43 #include <ctype.h>
45 #include <locale.h>
46 #include <langinfo.h>
48 static int look(char *);
49 static void perr(char *);
50 static void usage(void);
51 static void loadprivinfo(void);
52 static int parsespec(const char *);
53 static void privupdate(prpriv_t *, const char *);
54 static void privupdate_self(void);
55 static int dumppriv(char **);
56 static void flags2str(uint_t);
58 static char *command;
59 static char *procname;
60 static boolean_t verb = B_FALSE;
61 static boolean_t set = B_FALSE;
62 static boolean_t exec = B_FALSE;
63 static boolean_t Don = B_FALSE;
64 static boolean_t Doff = B_FALSE;
65 static boolean_t list = B_FALSE;
66 static boolean_t pfexec = B_FALSE;
67 static boolean_t xpol = B_FALSE;
68 static int mode = PRIV_STR_PORT;
70 int
71 main(int argc, char **argv)
73 int rc = 0;
74 int opt;
75 struct rlimit rlim;
77 (void) setlocale(LC_ALL, "");
78 (void) textdomain(TEXT_DOMAIN);
80 if ((command = strrchr(argv[0], '/')) != NULL)
81 command++;
82 else
83 command = argv[0];
85 while ((opt = getopt(argc, argv, "lDNPevs:xS")) != EOF) {
86 switch (opt) {
87 case 'l':
88 list = B_TRUE;
89 break;
90 case 'D':
91 set = B_TRUE;
92 Don = B_TRUE;
93 break;
94 case 'N':
95 set = B_TRUE;
96 Doff = B_TRUE;
97 break;
98 case 'P':
99 set = B_TRUE;
100 pfexec = B_TRUE;
101 break;
102 case 'e':
103 exec = B_TRUE;
104 break;
105 case 'S':
106 mode = PRIV_STR_SHORT;
107 break;
108 case 'v':
109 verb = B_TRUE;
110 mode = PRIV_STR_LIT;
111 break;
112 case 's':
113 set = B_TRUE;
114 if ((rc = parsespec(optarg)) != 0)
115 return (rc);
116 break;
117 case 'x':
118 set = B_TRUE;
119 xpol = B_TRUE;
120 break;
121 default:
122 usage();
123 /*NOTREACHED*/
127 argc -= optind;
128 argv += optind;
130 if ((argc < 1 && !list) || Doff && Don || list && (set || exec))
131 usage();
134 * Make sure we'll have enough file descriptors to handle a target
135 * that has many many mappings.
137 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
138 rlim.rlim_cur = rlim.rlim_max;
139 (void) setrlimit(RLIMIT_NOFILE, &rlim);
140 (void) enable_extended_FILE_stdio(-1, -1);
143 if (exec) {
144 privupdate_self();
145 rc = execvp(argv[0], &argv[0]);
146 (void) fprintf(stderr, "%s: %s: %s\n", command, argv[0],
147 strerror(errno));
148 } else if (list) {
149 rc = dumppriv(argv);
150 } else {
151 while (argc-- > 0)
152 rc += look(*argv++);
155 return (rc);
158 static int
159 look(char *arg)
161 struct ps_prochandle *Pr;
162 int gcode;
163 size_t sz;
164 void *pdata;
165 char *x;
166 int i;
167 boolean_t nodata;
168 prpriv_t *ppriv;
170 procname = arg; /* for perr() */
172 if ((Pr = proc_arg_grab(arg, set ? PR_ARG_PIDS : PR_ARG_ANY,
173 PGRAB_RETAIN | PGRAB_FORCE | (set ? 0 : PGRAB_RDONLY) |
174 PGRAB_NOSTOP, &gcode)) == NULL) {
175 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
176 command, arg, Pgrab_error(gcode));
177 return (1);
180 if (Ppriv(Pr, &ppriv) == -1) {
181 perr(command);
182 Prelease(Pr, 0);
183 return (1);
185 sz = PRIV_PRPRIV_SIZE(ppriv);
188 * The ppriv fields are unsigned and may overflow, so check them
189 * separately. Size must be word aligned, so check that too.
190 * Make sure size is "smallish" too.
192 if ((sz & 3) || ppriv->pr_nsets == 0 ||
193 sz / ppriv->pr_nsets < ppriv->pr_setsize ||
194 ppriv->pr_infosize > sz || sz > 1024 * 1024) {
195 (void) fprintf(stderr,
196 "%s: %s: bad PRNOTES section, size = %lx\n",
197 command, arg, (long)sz);
198 Prelease(Pr, 0);
199 Ppriv_free(Pr, ppriv);
200 return (1);
203 if (set) {
204 privupdate(ppriv, arg);
205 if (Psetpriv(Pr, ppriv) != 0) {
206 perr(command);
207 Prelease(Pr, 0);
208 Ppriv_free(Pr, ppriv);
209 return (1);
211 Prelease(Pr, 0);
212 Ppriv_free(Pr, ppriv);
213 return (0);
216 if (Pstate(Pr) == PS_DEAD) {
217 (void) printf("core '%s' of %d:\t%.70s\n",
218 arg, (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
219 pdata = Pprivinfo(Pr);
220 nodata = Pstate(Pr) == PS_DEAD && pdata == NULL;
221 } else {
222 (void) printf("%d:\t%.70s\n",
223 (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
224 pdata = NULL;
225 nodata = B_FALSE;
228 x = (char *)ppriv + sz - ppriv->pr_infosize;
229 while (x < (char *)ppriv + sz) {
230 /* LINTED: alignment */
231 priv_info_t *pi = (priv_info_t *)x;
232 priv_info_uint_t *pii;
234 switch (pi->priv_info_type) {
235 case PRIV_INFO_FLAGS:
236 /* LINTED: alignment */
237 pii = (priv_info_uint_t *)x;
238 (void) printf("flags =");
239 flags2str(pii->val);
240 (void) putchar('\n');
241 break;
242 default:
243 (void) fprintf(stderr, "%s: unknown priv_info: %d\n",
244 arg, pi->priv_info_type);
245 break;
247 if (pi->priv_info_size > ppriv->pr_infosize ||
248 pi->priv_info_size <= sizeof (priv_info_t) ||
249 (pi->priv_info_size & 3) != 0) {
250 (void) fprintf(stderr, "%s: bad priv_info_size: %u\n",
251 arg, pi->priv_info_size);
252 break;
254 x += pi->priv_info_size;
257 for (i = 0; i < ppriv->pr_nsets; i++) {
258 extern const char *__priv_getsetbynum(const void *, int);
259 const char *setnm = pdata ? __priv_getsetbynum(pdata, i) :
260 priv_getsetbynum(i);
261 priv_chunk_t *pc =
262 (priv_chunk_t *)&ppriv->pr_sets[ppriv->pr_setsize * i];
265 (void) printf("\t%c: ", setnm && !nodata ? *setnm : '?');
266 if (!nodata) {
267 extern char *__priv_set_to_str(void *,
268 const priv_set_t *, char, int);
269 priv_set_t *pset = (priv_set_t *)pc;
271 char *s;
273 if (pdata)
274 s = __priv_set_to_str(pdata, pset, ',', mode);
275 else
276 s = priv_set_to_str(pset, ',', mode);
277 (void) puts(s);
278 free(s);
279 } else {
280 int j;
281 for (j = 0; j < ppriv->pr_setsize; j++)
282 (void) printf("%08x", pc[j]);
283 (void) putchar('\n');
286 Prelease(Pr, 0);
287 Ppriv_free(Pr, ppriv);
288 return (0);
291 static void
292 fatal(const char *s)
294 (void) fprintf(stderr, "%s: %s: %s\n", command, s, strerror(errno));
295 exit(3);
298 static void
299 perr(char *s)
301 int err = errno;
303 if (s != NULL)
304 (void) fprintf(stderr, "%s: ", procname);
305 else
306 s = procname;
308 errno = err;
309 perror(s);
312 static void
313 usage(void)
315 (void) fprintf(stderr,
316 "usage:\t%s [-v] [-S] [-D|-N] [-s spec] { pid | core } ...\n"
317 "\t%s -e [-D|-N] [-M] [-s spec] cmd [args ...]\n"
318 "\t%s -l [-v] [privilege ...]\n"
319 " (report, set or list process privileges)\n", command,
320 command, command);
321 exit(2);
322 /*NOTREACHED*/
326 * Parse the privilege bits to add and/or remove from
327 * a privilege set.
329 * [EPIL][+-=]priv,priv,priv
332 static int
333 strindex(char c, const char *str)
335 const char *s;
337 if (islower(c))
338 c = toupper(c);
340 s = strchr(str, c);
342 if (s == NULL)
343 return (-1);
344 else
345 return (s - str);
348 static void
349 badspec(const char *spec)
351 (void) fprintf(stderr, "%s: bad privilege specification: \"%s\"\n",
352 command, spec);
353 exit(3);
354 /*NOTREACHED*/
358 * For each set, you can set either add and/or
359 * remove or you can set assign.
361 static priv_set_t **rem, **add, **assign;
362 static const priv_impl_info_t *pri = NULL;
363 static char *sets;
365 static void
366 loadprivinfo(void)
368 int i;
370 if (pri != NULL)
371 return;
373 pri = getprivimplinfo();
375 if (pri == NULL)
376 fatal("getprivimplinfo");
378 sets = malloc(pri->priv_nsets + 1);
379 if (sets == NULL)
380 fatal("malloc");
382 for (i = 0; i < pri->priv_nsets; i++) {
383 sets[i] = *priv_getsetbynum(i);
384 if (islower(sets[i]))
385 sets[i] = toupper(sets[i]);
388 sets[pri->priv_nsets] = '\0';
390 rem = calloc(pri->priv_nsets, sizeof (priv_set_t *));
391 add = calloc(pri->priv_nsets, sizeof (priv_set_t *));
392 assign = calloc(pri->priv_nsets, sizeof (priv_set_t *));
393 if (rem == NULL || add == NULL || assign == NULL)
394 fatal("calloc");
397 static int
398 parsespec(const char *spec)
400 char *p;
401 const char *q;
402 int count;
403 priv_set_t ***toupd;
404 priv_set_t *upd;
405 int i;
406 boolean_t freeupd = B_TRUE;
408 if (pri == NULL)
409 loadprivinfo();
411 p = strpbrk(spec, "+-=");
413 if (p == NULL || p - spec > pri->priv_nsets)
414 badspec(spec);
416 if (p[1] == '\0' || (upd = priv_str_to_set(p + 1, ",", NULL)) == NULL)
417 badspec(p + 1);
419 count = p - spec;
420 switch (*p) {
421 case '+':
422 toupd = &add;
423 break;
424 case '-':
425 toupd = &rem;
426 priv_inverse(upd);
427 break;
428 case '=':
429 toupd = &assign;
430 break;
433 /* Update all sets? */
434 if (count == 0 || *spec == 'a' || *spec == 'A') {
435 count = pri->priv_nsets;
436 q = sets;
437 } else
438 q = spec;
440 for (i = 0; i < count; i++) {
441 int ind = strindex(q[i], sets);
443 if (ind == -1)
444 badspec(spec);
446 /* Assign is mutually exclusive with add/remove and itself */
447 if (((toupd == &rem || toupd == &add) && assign[ind] != NULL) ||
448 (toupd == &assign && (assign[ind] != NULL ||
449 rem[ind] != NULL || add[ind] != NULL))) {
450 (void) fprintf(stderr, "%s: conflicting spec: %s\n",
451 command, spec);
452 exit(1);
454 if ((*toupd)[ind] != NULL) {
455 if (*p == '-')
456 priv_intersect(upd, (*toupd)[ind]);
457 else
458 priv_union(upd, (*toupd)[ind]);
459 } else {
460 (*toupd)[ind] = upd;
461 freeupd = B_FALSE;
464 if (freeupd)
465 priv_freeset(upd);
466 return (0);
469 static void
470 privupdate(prpriv_t *pr, const char *arg)
472 int i;
474 if (sets != NULL) {
475 for (i = 0; i < pri->priv_nsets; i++) {
476 priv_set_t *target =
477 (priv_set_t *)&pr->pr_sets[pr->pr_setsize * i];
478 if (rem[i] != NULL)
479 priv_intersect(rem[i], target);
480 if (add[i] != NULL)
481 priv_union(add[i], target);
482 if (assign[i] != NULL)
483 priv_copyset(assign[i], target);
487 if (Doff || Don || pfexec || xpol) {
488 priv_info_uint_t *pii;
489 int sz = PRIV_PRPRIV_SIZE(pr);
490 char *x = (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr);
491 uint32_t fl = 0;
493 while (x < (char *)pr + sz) {
494 /* LINTED: alignment */
495 priv_info_t *pi = (priv_info_t *)x;
497 if (pi->priv_info_type == PRIV_INFO_FLAGS) {
498 /* LINTED: alignment */
499 pii = (priv_info_uint_t *)x;
500 fl = pii->val;
501 goto done;
503 if (pi->priv_info_size > pr->pr_infosize ||
504 pi->priv_info_size <= sizeof (priv_info_t) ||
505 (pi->priv_info_size & 3) != 0)
506 break;
507 x += pi->priv_info_size;
509 (void) fprintf(stderr,
510 "%s: cannot find privilege flags to set\n", arg);
511 pr->pr_infosize = 0;
512 return;
513 done:
515 pr->pr_infosize = sizeof (priv_info_uint_t);
516 /* LINTED: alignment */
517 pii = (priv_info_uint_t *)
518 ((char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
520 if (Don)
521 fl |= PRIV_DEBUG;
522 if (Doff)
523 fl &= ~PRIV_DEBUG;
524 if (pfexec)
525 fl |= PRIV_PFEXEC;
526 if (xpol)
527 fl |= PRIV_XPOLICY;
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;
537 static void
538 privupdate_self(void)
540 int set;
542 if (pfexec) {
543 if (setpflags(PRIV_PFEXEC, 1) != 0)
544 fatal("setpflags(PRIV_PFEXEC)");
547 if (sets != NULL) {
548 priv_set_t *target = priv_allocset();
550 if (target == NULL)
551 fatal("priv_allocet");
553 set = priv_getsetbyname(PRIV_INHERITABLE);
554 if (rem[set] != NULL || add[set] != NULL ||
555 assign[set] != NULL) {
556 (void) getppriv(PRIV_INHERITABLE, target);
557 if (rem[set] != NULL)
558 priv_intersect(rem[set], target);
559 if (add[set] != NULL)
560 priv_union(add[set], target);
561 if (assign[set] != NULL)
562 priv_copyset(assign[set], target);
563 if (setppriv(PRIV_SET, PRIV_INHERITABLE, target) != 0)
564 fatal("setppriv(Inheritable)");
566 set = priv_getsetbyname(PRIV_LIMIT);
567 if (rem[set] != NULL || add[set] != NULL ||
568 assign[set] != NULL) {
569 (void) getppriv(PRIV_LIMIT, target);
570 if (rem[set] != NULL)
571 priv_intersect(rem[set], target);
572 if (add[set] != NULL)
573 priv_union(add[set], target);
574 if (assign[set] != NULL)
575 priv_copyset(assign[set], target);
576 if (setppriv(PRIV_SET, PRIV_LIMIT, target) != 0)
577 fatal("setppriv(Limit)");
579 priv_freeset(target);
582 if (Doff || Don)
583 (void) setpflags(PRIV_DEBUG, Don ? 1 : 0);
584 if (xpol)
585 (void) setpflags(PRIV_XPOLICY, 1);
586 if (pfexec)
587 (void) setpflags(PRIV_PFEXEC, 1);
590 static int
591 dopriv(const char *p)
593 (void) puts(p);
594 if (verb) {
595 char *text = priv_gettext(p);
596 char *p, *q;
597 if (text == NULL)
598 return (1);
599 for (p = text; q = strchr(p, '\n'); p = q + 1) {
600 *q = '\0';
601 (void) printf("\t%s\n", p);
603 free(text);
605 return (0);
608 static int
609 dumppriv(char **argv)
611 int rc = 0;
612 const char *pname;
613 int i;
615 if (argv[0] == NULL) {
616 for (i = 0; ((pname = priv_getbynum(i++)) != NULL); )
617 rc += dopriv(pname);
618 } else {
619 for (; *argv; argv++) {
620 priv_set_t *pset = priv_str_to_set(*argv, ",", NULL);
622 if (pset == NULL) {
623 (void) fprintf(stderr, "%s: %s: bad privilege"
624 " list\n", command, *argv);
625 rc++;
626 continue;
628 for (i = 0; ((pname = priv_getbynum(i++)) != NULL); )
629 if (priv_ismember(pset, pname))
630 rc += dopriv(pname);
633 return (rc);
636 static struct {
637 int flag;
638 char *name;
639 } flags[] = {
640 { PRIV_DEBUG, "PRIV_DEBUG" },
641 { PRIV_AWARE, "PRIV_AWARE" },
642 { PRIV_AWARE_INHERIT, "PRIV_AWARE_INHERIT" },
643 { PRIV_AWARE_RESET, "PRIV_AWARE_RESET" },
644 { PRIV_XPOLICY, "PRIV_XPOLICY" },
645 { PRIV_PFEXEC, "PRIV_PFEXEC" },
649 * Print flags preceeded by a space.
651 static void
652 flags2str(uint_t pflags)
654 char c = ' ';
655 int i;
657 if (pflags == 0) {
658 (void) fputs(" <none>", stdout);
659 return;
661 for (i = 0; i < sizeof (flags)/sizeof (flags[0]) && pflags != 0; i++) {
662 if ((pflags & flags[i].flag) != 0) {
663 (void) printf("%c%s", c, flags[i].name);
664 pflags &= ~flags[i].flag;
665 c = '|';
668 if (pflags != 0)
669 (void) printf("%c<0x%x>", c, pflags);