xref: /onnv-gate/usr/src/lib/libc/port/gen/privlib.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
51059Scasper  * Common Development and Distribution License (the "License").
61059Scasper  * 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  */
213864Sraf 
220Sstevel@tonic-gate /*
233864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" /* TSOL 8 */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma weak getprivimplinfo	= _getprivimplinfo
300Sstevel@tonic-gate #pragma weak priv_addset	= _priv_addset
310Sstevel@tonic-gate #pragma weak priv_allocset	= _priv_allocset
320Sstevel@tonic-gate #pragma weak priv_copyset	= _priv_copyset
330Sstevel@tonic-gate #pragma weak priv_delset	= _priv_delset
340Sstevel@tonic-gate #pragma weak priv_emptyset	= _priv_emptyset
350Sstevel@tonic-gate #pragma weak priv_fillset	= _priv_fillset
360Sstevel@tonic-gate #pragma weak priv_freeset	= _priv_freeset
370Sstevel@tonic-gate #pragma weak priv_getbyname	= _priv_getbyname
380Sstevel@tonic-gate #pragma weak priv_getbynum	= _priv_getbynum
390Sstevel@tonic-gate #pragma weak priv_getsetbyname	= _priv_getsetbyname
400Sstevel@tonic-gate #pragma weak priv_getsetbynum	= _priv_getsetbynum
410Sstevel@tonic-gate #pragma weak priv_ineffect	= _priv_ineffect
420Sstevel@tonic-gate #pragma weak priv_intersect	= _priv_intersect
430Sstevel@tonic-gate #pragma weak priv_inverse	= _priv_inverse
440Sstevel@tonic-gate #pragma weak priv_isemptyset	= _priv_isemptyset
450Sstevel@tonic-gate #pragma weak priv_isequalset	= _priv_isequalset
460Sstevel@tonic-gate #pragma weak priv_isfullset	= _priv_isfullset
470Sstevel@tonic-gate #pragma weak priv_ismember	= _priv_ismember
480Sstevel@tonic-gate #pragma weak priv_issubset	= _priv_issubset
490Sstevel@tonic-gate #pragma weak priv_set		= _priv_set
500Sstevel@tonic-gate #pragma weak priv_union		= _priv_union
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include "synonyms.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	_STRUCTURED_PROC	1
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "priv_private.h"
570Sstevel@tonic-gate #include "mtlib.h"
580Sstevel@tonic-gate #include "libc.h"
590Sstevel@tonic-gate #include <errno.h>
600Sstevel@tonic-gate #include <stdarg.h>
610Sstevel@tonic-gate #include <stdlib.h>
620Sstevel@tonic-gate #include <unistd.h>
630Sstevel@tonic-gate #include <strings.h>
640Sstevel@tonic-gate #include <synch.h>
650Sstevel@tonic-gate #include <alloca.h>
663864Sraf #include <atomic.h>
670Sstevel@tonic-gate #include <sys/ucred.h>
680Sstevel@tonic-gate #include <sys/procfs.h>
690Sstevel@tonic-gate #include <sys/param.h>
700Sstevel@tonic-gate #include <sys/corectl.h>
710Sstevel@tonic-gate #include <priv_utils.h>
720Sstevel@tonic-gate #include <zone.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* Include each string only once - until the compiler/linker are fixed */
750Sstevel@tonic-gate static const char *permitted	= PRIV_PERMITTED;
760Sstevel@tonic-gate static const char *effective	= PRIV_EFFECTIVE;
770Sstevel@tonic-gate static const char *limit	= PRIV_LIMIT;
780Sstevel@tonic-gate static const char *inheritable	= PRIV_INHERITABLE;
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Data independent privilege set operations.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * Only a few functions are provided that do not default to
830Sstevel@tonic-gate  * the system implementation of privileges.  A limited set of
840Sstevel@tonic-gate  * interfaces is provided that accepts a priv_data_t *
850Sstevel@tonic-gate  * argument; this set of interfaces is a private interface between libc
860Sstevel@tonic-gate  * and libproc.  It is delivered in order to interpret privilege sets
870Sstevel@tonic-gate  * in debuggers in a implementation independent way.  As such, we
880Sstevel@tonic-gate  * don't need to provide the bulk of the interfaces, only a few
890Sstevel@tonic-gate  * boolean tests (isfull, isempty) the name<->num mappings and
900Sstevel@tonic-gate  * set pretty print functions.   The boolean tests are only needed for
910Sstevel@tonic-gate  * the latter, so those aren't provided externally.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * Additionally, we provide the function that maps the kernel implementation
940Sstevel@tonic-gate  * structure into a libc private data structure.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate priv_data_t *privdata;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static mutex_t pd_lock = DEFAULTMUTEX;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static int
1020Sstevel@tonic-gate parseninfo(priv_info_names_t *na, char ***buf, int *cp)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	char *q;
1050Sstevel@tonic-gate 	int i;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	*buf = libc_malloc(sizeof (char *) * na->cnt);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (*buf == NULL)
1100Sstevel@tonic-gate 		return (-1);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	q = na->names;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	for (i = 0; i < na->cnt; i++) {
1150Sstevel@tonic-gate 		int l = strlen(q);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		(*buf)[i] = q;
1180Sstevel@tonic-gate 		q += l + 1;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	*cp = na->cnt;
1210Sstevel@tonic-gate 	return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate struct strint {
1250Sstevel@tonic-gate 	char *name;
1260Sstevel@tonic-gate 	int rank;
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int
1300Sstevel@tonic-gate strintcmp(const void *a, const void *b)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	const struct strint *ap = a;
1330Sstevel@tonic-gate 	const struct strint *bp = b;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	return (strcasecmp(ap->name, bp->name));
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate priv_data_t *
1390Sstevel@tonic-gate __priv_parse_info(priv_impl_info_t *ip)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	priv_data_t *tmp;
1420Sstevel@tonic-gate 	char *x;
1430Sstevel@tonic-gate 	size_t size = PRIV_IMPL_INFO_SIZE(ip);
1440Sstevel@tonic-gate 	int i;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	tmp = libc_malloc(sizeof (*tmp));
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (tmp == NULL)
1490Sstevel@tonic-gate 		return (NULL);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	(void) memset(tmp, 0, sizeof (*tmp));
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	tmp->pd_pinfo = ip;
1540Sstevel@tonic-gate 	tmp->pd_setsize = sizeof (priv_chunk_t) * ip->priv_setsize;
1550Sstevel@tonic-gate 	tmp->pd_ucredsize = UCRED_SIZE(ip);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	x = (char *)ip;
1580Sstevel@tonic-gate 	x += ip->priv_headersize;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	while (x < ((char *)ip) + size) {
1610Sstevel@tonic-gate 		/* LINTED: alignment */
1620Sstevel@tonic-gate 		priv_info_names_t *na = (priv_info_names_t *)x;
1630Sstevel@tonic-gate 		/* LINTED: alignment */
1640Sstevel@tonic-gate 		priv_info_set_t *st = (priv_info_set_t *)x;
1650Sstevel@tonic-gate 		struct strint *tmparr;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		switch (na->info.priv_info_type) {
1680Sstevel@tonic-gate 		case PRIV_INFO_SETNAMES:
1690Sstevel@tonic-gate 			if (parseninfo(na, &tmp->pd_setnames, &tmp->pd_nsets))
1700Sstevel@tonic-gate 				goto out;
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 		case PRIV_INFO_PRIVNAMES:
1730Sstevel@tonic-gate 			if (parseninfo(na, &tmp->pd_privnames, &tmp->pd_nprivs))
1740Sstevel@tonic-gate 				goto out;
1750Sstevel@tonic-gate 			/*
1760Sstevel@tonic-gate 			 * We compute a sorted index which allows us
1770Sstevel@tonic-gate 			 * to present a sorted list of privileges
1780Sstevel@tonic-gate 			 * without actually having to sort it each time.
1790Sstevel@tonic-gate 			 */
1800Sstevel@tonic-gate 			tmp->pd_setsort = libc_malloc(tmp->pd_nprivs *
1810Sstevel@tonic-gate 			    sizeof (int));
1820Sstevel@tonic-gate 			if (tmp->pd_setsort == NULL)
1830Sstevel@tonic-gate 				goto out;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 			tmparr = libc_malloc(tmp->pd_nprivs *
1860Sstevel@tonic-gate 			    sizeof (struct strint));
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 			if (tmparr == NULL)
1890Sstevel@tonic-gate 				goto out;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 			for (i = 0; i < tmp->pd_nprivs; i++) {
1920Sstevel@tonic-gate 				tmparr[i].rank = i;
1930Sstevel@tonic-gate 				tmparr[i].name = tmp->pd_privnames[i];
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 			qsort(tmparr, tmp->pd_nprivs, sizeof (struct strint),
1960Sstevel@tonic-gate 				strintcmp);
1970Sstevel@tonic-gate 			for (i = 0; i < tmp->pd_nprivs; i++)
1980Sstevel@tonic-gate 				tmp->pd_setsort[i] = tmparr[i].rank;
1990Sstevel@tonic-gate 			libc_free(tmparr);
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 		case PRIV_INFO_BASICPRIVS:
2020Sstevel@tonic-gate 			tmp->pd_basicset = (priv_set_t *)&st->set[0];
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate 		default:
2050Sstevel@tonic-gate 			/* unknown, ignore */
2060Sstevel@tonic-gate 			break;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		x += na->info.priv_info_size;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	return (tmp);
2110Sstevel@tonic-gate out:
2120Sstevel@tonic-gate 	libc_free(tmp->pd_setnames);
2130Sstevel@tonic-gate 	libc_free(tmp->pd_privnames);
2140Sstevel@tonic-gate 	libc_free(tmp->pd_setsort);
2150Sstevel@tonic-gate 	libc_free(tmp);
2160Sstevel@tonic-gate 	return (NULL);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * Caller must have allocated d->pd_pinfo and should free it,
2210Sstevel@tonic-gate  * if necessary.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate void
2240Sstevel@tonic-gate __priv_free_info(priv_data_t *d)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	libc_free(d->pd_setnames);
2270Sstevel@tonic-gate 	libc_free(d->pd_privnames);
2280Sstevel@tonic-gate 	libc_free(d->pd_setsort);
2290Sstevel@tonic-gate 	libc_free(d);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2331059Scasper  * Return with the pd_lock held and data loaded or indicate failure.
2340Sstevel@tonic-gate  */
2351059Scasper int
2360Sstevel@tonic-gate lock_data(void)
2370Sstevel@tonic-gate {
2383864Sraf 	if (__priv_getdata() == NULL)
2391059Scasper 		return (-1);
2401059Scasper 
2410Sstevel@tonic-gate 	lmutex_lock(&pd_lock);
2421059Scasper 	return (0);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate boolean_t
2460Sstevel@tonic-gate refresh_data(void)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	priv_impl_info_t *ip, ii;
2490Sstevel@tonic-gate 	priv_data_t *tmp;
2500Sstevel@tonic-gate 	char *p0, *q0;
2510Sstevel@tonic-gate 	int oldn, newn;
2520Sstevel@tonic-gate 	int i;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (getprivinfo(&ii, sizeof (ii)) != 0 ||
2550Sstevel@tonic-gate 	    ii.priv_max == privdata->pd_nprivs)
2560Sstevel@tonic-gate 		return (B_FALSE);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	ip = alloca(PRIV_IMPL_INFO_SIZE(&ii));
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	(void) getprivinfo(ip, PRIV_IMPL_INFO_SIZE(&ii));
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* Parse the info; then copy the additional bits */
2630Sstevel@tonic-gate 	tmp = __priv_parse_info(ip);
2640Sstevel@tonic-gate 	if (tmp == NULL)
2650Sstevel@tonic-gate 		return (B_FALSE);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	oldn = privdata->pd_nprivs;
2680Sstevel@tonic-gate 	p0 = privdata->pd_privnames[0];
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	newn = tmp->pd_nprivs;
2710Sstevel@tonic-gate 	q0 = tmp->pd_privnames[0];
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/* copy the extra information to the old datastructure */
2740Sstevel@tonic-gate 	(void) memcpy((char *)privdata->pd_pinfo + sizeof (priv_impl_info_t),
2750Sstevel@tonic-gate 		(char *)ip + sizeof (priv_impl_info_t),
2760Sstevel@tonic-gate 		PRIV_IMPL_INFO_SIZE(ip) - sizeof (priv_impl_info_t));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* Copy the first oldn pointers */
2790Sstevel@tonic-gate 	(void) memcpy(tmp->pd_privnames, privdata->pd_privnames,
2800Sstevel@tonic-gate 	    oldn * sizeof (char *));
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* Adjust the rest */
2830Sstevel@tonic-gate 	for (i = oldn; i < newn; i++)
2840Sstevel@tonic-gate 		tmp->pd_privnames[i] += p0 - q0;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/* Install the larger arrays */
2870Sstevel@tonic-gate 	libc_free(privdata->pd_privnames);
2880Sstevel@tonic-gate 	privdata->pd_privnames = tmp->pd_privnames;
2890Sstevel@tonic-gate 	tmp->pd_privnames = NULL;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	libc_free(privdata->pd_setsort);
2920Sstevel@tonic-gate 	privdata->pd_setsort = tmp->pd_setsort;
2930Sstevel@tonic-gate 	tmp->pd_setsort = NULL;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* Copy the rest of the data */
2960Sstevel@tonic-gate 	*privdata->pd_pinfo = *ip;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	privdata->pd_nprivs = newn;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	__priv_free_info(tmp);
3010Sstevel@tonic-gate 	return (B_TRUE);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate void
3050Sstevel@tonic-gate unlock_data(void)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	lmutex_unlock(&pd_lock);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static priv_set_t *__priv_allocset(priv_data_t *);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate priv_data_t *
3130Sstevel@tonic-gate __priv_getdata(void)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	if (privdata == NULL) {
3163864Sraf 		lmutex_lock(&pd_lock);
3173864Sraf 		if (privdata == NULL) {
3183864Sraf 			priv_data_t *tmp;
3193864Sraf 			priv_impl_info_t *ip;
3203864Sraf 			size_t size = sizeof (priv_impl_info_t) + 2048;
3213864Sraf 			size_t realsize;
3223864Sraf 			priv_impl_info_t *aip = alloca(size);
3230Sstevel@tonic-gate 
3243864Sraf 			if (getprivinfo(aip, size) != 0)
3253864Sraf 				goto out;
3263864Sraf 
3273864Sraf 			realsize = PRIV_IMPL_INFO_SIZE(aip);
3283864Sraf 
3293864Sraf 			ip = libc_malloc(realsize);
3303864Sraf 
3313864Sraf 			if (ip == NULL)
3323864Sraf 				goto out;
3330Sstevel@tonic-gate 
3343864Sraf 			if (realsize <= size) {
3353864Sraf 				(void) memcpy(ip, aip, realsize);
3363864Sraf 			} else if (getprivinfo(ip, realsize) != 0) {
3373864Sraf 				libc_free(ip);
3383864Sraf 				goto out;
3393864Sraf 			}
3400Sstevel@tonic-gate 
3413864Sraf 			if ((tmp = __priv_parse_info(ip)) == NULL) {
3423864Sraf 				libc_free(ip);
3433864Sraf 				goto out;
3443864Sraf 			}
3450Sstevel@tonic-gate 
3463864Sraf 			/* Allocate the zoneset just once, here */
3473864Sraf 			tmp->pd_zoneset = __priv_allocset(tmp);
3483864Sraf 			if (tmp->pd_zoneset == NULL)
3493864Sraf 				goto clean;
3500Sstevel@tonic-gate 
3513864Sraf 			if (zone_getattr(getzoneid(), ZONE_ATTR_PRIVSET,
3523864Sraf 			    tmp->pd_zoneset, tmp->pd_setsize)
3533864Sraf 			    == tmp->pd_setsize) {
3543864Sraf 				membar_producer();
3553864Sraf 				privdata = tmp;
3563864Sraf 				goto out;
3573864Sraf 			}
3583864Sraf 
3593864Sraf 			priv_freeset(tmp->pd_zoneset);
3603864Sraf clean:
3613864Sraf 			__priv_free_info(tmp);
3620Sstevel@tonic-gate 			libc_free(ip);
3630Sstevel@tonic-gate 		}
3643864Sraf out:
3653864Sraf 		lmutex_unlock(&pd_lock);
3660Sstevel@tonic-gate 	}
3673864Sraf 	membar_consumer();
3680Sstevel@tonic-gate 	return (privdata);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate const priv_impl_info_t *
3720Sstevel@tonic-gate _getprivimplinfo(void)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	priv_data_t *d;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	LOADPRIVDATA(d);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	return (d->pd_pinfo);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate static priv_set_t *
3820Sstevel@tonic-gate priv_vlist(va_list ap)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	priv_set_t *pset = priv_allocset();
3850Sstevel@tonic-gate 	const char *priv;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (pset == NULL)
3880Sstevel@tonic-gate 		return (NULL);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	priv_emptyset(pset);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	while ((priv = va_arg(ap, const char *)) != NULL) {
3930Sstevel@tonic-gate 		if (priv_addset(pset, priv) < 0) {
3940Sstevel@tonic-gate 			priv_freeset(pset);
3950Sstevel@tonic-gate 			return (NULL);
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	return (pset);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * priv_set(op, set, priv_id1, priv_id2, ..., NULL)
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * Library routine to enable a user process to set a specific
4050Sstevel@tonic-gate  * privilege set appropriately using a single call.  User is
4060Sstevel@tonic-gate  * required to terminate the list of privileges with NULL.
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate int
4090Sstevel@tonic-gate priv_set(priv_op_t op, priv_ptype_t setname, ...)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	va_list ap;
4120Sstevel@tonic-gate 	priv_set_t *pset;
4130Sstevel@tonic-gate 	int ret;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	va_start(ap, setname);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	pset = priv_vlist(ap);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	va_end(ap);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (pset == NULL)
4220Sstevel@tonic-gate 		return (-1);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/* All sets */
4250Sstevel@tonic-gate 	if (setname == NULL) {
4260Sstevel@tonic-gate 		priv_data_t *d;
4270Sstevel@tonic-gate 		int set;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		LOADPRIVDATA(d);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		for (set = 0; set < d->pd_nsets; set++)
4320Sstevel@tonic-gate 			if ((ret = syscall(SYS_privsys, PRIVSYS_SETPPRIV, op,
4330Sstevel@tonic-gate 					set, (void *)pset, d->pd_setsize)) != 0)
4340Sstevel@tonic-gate 				break;
4350Sstevel@tonic-gate 	} else {
4360Sstevel@tonic-gate 		ret = setppriv(op, setname, pset);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	priv_freeset(pset);
4400Sstevel@tonic-gate 	return (ret);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * priv_ineffect(privilege).
4450Sstevel@tonic-gate  * tests the existance of a privilege against the effective set.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate boolean_t
4480Sstevel@tonic-gate priv_ineffect(const char *priv)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	priv_set_t *curset;
4510Sstevel@tonic-gate 	boolean_t res;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	curset = priv_allocset();
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (curset == NULL)
4560Sstevel@tonic-gate 		return (B_FALSE);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (getppriv(effective, curset) != 0 ||
4590Sstevel@tonic-gate 	    !priv_ismember(curset, priv))
4600Sstevel@tonic-gate 		res = B_FALSE;
4610Sstevel@tonic-gate 	else
4620Sstevel@tonic-gate 		res = B_TRUE;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	priv_freeset(curset);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	return (res);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate  * The routine __init_daemon_priv() is private to Solaris and is
4710Sstevel@tonic-gate  * used by daemons to limit the privileges they can use and
4720Sstevel@tonic-gate  * to set the uid they run under.
4730Sstevel@tonic-gate  */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate static const char root_cp[] = "/core.%f.%t";
4760Sstevel@tonic-gate static const char daemon_cp[] = "/var/tmp/core.%f.%t";
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate int
4790Sstevel@tonic-gate __init_daemon_priv(int flags, uid_t uid, gid_t gid, ...)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	priv_set_t *nset;
4820Sstevel@tonic-gate 	priv_set_t *perm = NULL;
4830Sstevel@tonic-gate 	va_list pa;
4840Sstevel@tonic-gate 	priv_data_t *d;
4850Sstevel@tonic-gate 	int ret = -1;
4860Sstevel@tonic-gate 	char buf[1024];
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	LOADPRIVDATA(d);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	va_start(pa, gid);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	nset = priv_vlist(pa);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	va_end(pa);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (nset == NULL)
4970Sstevel@tonic-gate 		return (-1);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/* Always add the basic set */
5000Sstevel@tonic-gate 	if (d->pd_basicset != NULL)
5010Sstevel@tonic-gate 		priv_union(d->pd_basicset, nset);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * This is not a significant failure: it allows us to start programs
5050Sstevel@tonic-gate 	 * with sufficient privileges and with the proper uid.   We don't
5060Sstevel@tonic-gate 	 * care enough about the extra groups in that case.
5070Sstevel@tonic-gate 	 */
5080Sstevel@tonic-gate 	if (flags & PU_RESETGROUPS)
5090Sstevel@tonic-gate 		(void) setgroups(0, NULL);
5100Sstevel@tonic-gate 
511*4321Scasper 	if (gid != (gid_t)-1 && setgid(gid) != 0)
5120Sstevel@tonic-gate 		goto end;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	perm = priv_allocset();
5150Sstevel@tonic-gate 	if (perm == NULL)
5160Sstevel@tonic-gate 		goto end;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	/* E = P */
5190Sstevel@tonic-gate 	(void) getppriv(permitted, perm);
5200Sstevel@tonic-gate 	(void) setppriv(PRIV_SET, effective, perm);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/* Now reset suid and euid */
523*4321Scasper 	if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
5240Sstevel@tonic-gate 		goto end;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/* Check for the limit privs */
5270Sstevel@tonic-gate 	if ((flags & PU_LIMITPRIVS) &&
5280Sstevel@tonic-gate 	    setppriv(PRIV_SET, limit, nset) != 0)
5290Sstevel@tonic-gate 		goto end;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (flags & PU_CLEARLIMITSET) {
5320Sstevel@tonic-gate 		priv_emptyset(perm);
5330Sstevel@tonic-gate 		if (setppriv(PRIV_SET, limit, perm) != 0)
5340Sstevel@tonic-gate 			goto end;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/* Remove the privileges from all the other sets */
5380Sstevel@tonic-gate 	if (setppriv(PRIV_SET, permitted, nset) != 0)
5390Sstevel@tonic-gate 		goto end;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (!(flags & PU_INHERITPRIVS))
5420Sstevel@tonic-gate 		priv_emptyset(nset);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	ret = setppriv(PRIV_SET, inheritable, nset);
5450Sstevel@tonic-gate end:
5460Sstevel@tonic-gate 	priv_freeset(nset);
5470Sstevel@tonic-gate 	priv_freeset(perm);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
5500Sstevel@tonic-gate 	    strcmp(buf, "core") == 0) {
5510Sstevel@tonic-gate 
552*4321Scasper 		if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
5530Sstevel@tonic-gate 			(void) core_set_process_path(root_cp, sizeof (root_cp),
5540Sstevel@tonic-gate 			    getpid());
5550Sstevel@tonic-gate 		} else {
5560Sstevel@tonic-gate 			(void) core_set_process_path(daemon_cp,
5570Sstevel@tonic-gate 			    sizeof (daemon_cp), getpid());
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 	(void) setpflags(__PROC_PROTECT, 0);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	return (ret);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * The routine __fini_daemon_priv() is private to Solaris and is
5670Sstevel@tonic-gate  * used by daemons to clear remaining unwanted privileges and
5680Sstevel@tonic-gate  * reenable core dumps.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate void
5710Sstevel@tonic-gate __fini_daemon_priv(const char *priv, ...)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	priv_set_t *nset;
5740Sstevel@tonic-gate 	va_list pa;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	va_start(pa, priv);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (priv != NULL) {
5790Sstevel@tonic-gate 		nset = priv_vlist(pa);
5800Sstevel@tonic-gate 		if (nset == NULL)
5810Sstevel@tonic-gate 			return;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		(void) priv_addset(nset, priv);
5840Sstevel@tonic-gate 		(void) setppriv(PRIV_OFF, permitted, nset);
5850Sstevel@tonic-gate 		priv_freeset(nset);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	va_end(pa);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	(void) setpflags(__PROC_PROTECT, 0);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * The routine __init_suid_priv() is private to Solaris and is
5950Sstevel@tonic-gate  * used by set-uid root programs to limit the privileges acquired
5960Sstevel@tonic-gate  * to those actually needed.
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate static priv_set_t *bracketpriv;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate int
6020Sstevel@tonic-gate __init_suid_priv(int flags, ...)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	priv_set_t *nset = NULL;
6050Sstevel@tonic-gate 	priv_set_t *tmpset = NULL;
6060Sstevel@tonic-gate 	va_list pa;
6070Sstevel@tonic-gate 	int r = -1;
6080Sstevel@tonic-gate 	uid_t ruid, euid;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	euid = geteuid();
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/* If we're not set-uid root, don't reset the uid */
6130Sstevel@tonic-gate 	if (euid == 0) {
6140Sstevel@tonic-gate 		ruid = getuid();
6150Sstevel@tonic-gate 		/* If we're running as root, keep everything */
6160Sstevel@tonic-gate 		if (ruid == 0)
6170Sstevel@tonic-gate 			return (0);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/* Can call this only once */
6210Sstevel@tonic-gate 	if (bracketpriv != NULL)
6220Sstevel@tonic-gate 		return (-1);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	va_start(pa, flags);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	nset = priv_vlist(pa);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	va_end(pa);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (nset == NULL)
6310Sstevel@tonic-gate 		goto end;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	tmpset = priv_allocset();
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (tmpset == NULL)
6360Sstevel@tonic-gate 		goto end;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* We cannot grow our privileges beyond P, so start there */
6390Sstevel@tonic-gate 	(void) getppriv(permitted, tmpset);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/* Is the privilege we need even in P? */
6420Sstevel@tonic-gate 	if (!priv_issubset(nset, tmpset))
6430Sstevel@tonic-gate 		goto end;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	bracketpriv = priv_allocset();
6460Sstevel@tonic-gate 	if (bracketpriv == NULL)
6470Sstevel@tonic-gate 		goto end;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	priv_copyset(nset, bracketpriv);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/* Always add the basic set */
6520Sstevel@tonic-gate 	priv_union(priv_basic(), nset);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/* But don't add what we don't have */
6550Sstevel@tonic-gate 	priv_intersect(tmpset, nset);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	(void) getppriv(inheritable, tmpset);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* And stir in the inheritable privileges */
6600Sstevel@tonic-gate 	priv_union(tmpset, nset);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if ((r = setppriv(PRIV_SET, effective, tmpset)) != 0)
6630Sstevel@tonic-gate 		goto end;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if ((r = setppriv(PRIV_SET, permitted, nset)) != 0)
6660Sstevel@tonic-gate 		goto end;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (flags & PU_CLEARLIMITSET)
6690Sstevel@tonic-gate 		priv_emptyset(nset);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if ((flags & (PU_LIMITPRIVS|PU_CLEARLIMITSET)) != 0 &&
6720Sstevel@tonic-gate 	    (r = setppriv(PRIV_SET, limit, nset)) != 0)
6730Sstevel@tonic-gate 		goto end;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (euid == 0)
6760Sstevel@tonic-gate 		r = setreuid(ruid, ruid);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate end:
6790Sstevel@tonic-gate 	priv_freeset(tmpset);
6800Sstevel@tonic-gate 	priv_freeset(nset);
6810Sstevel@tonic-gate 	if (r != 0) {
6820Sstevel@tonic-gate 		/* Fail without leaving uid 0 around */
6830Sstevel@tonic-gate 		if (euid == 0)
6840Sstevel@tonic-gate 			(void) setreuid(ruid, ruid);
6850Sstevel@tonic-gate 		priv_freeset(bracketpriv);
6860Sstevel@tonic-gate 		bracketpriv = NULL;
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	return (r);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate  * Toggle privileges on/off in the effective set.
6940Sstevel@tonic-gate  */
6950Sstevel@tonic-gate int
6960Sstevel@tonic-gate __priv_bracket(priv_op_t op)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate 	/* We're running fully privileged or didn't check errors first time */
6990Sstevel@tonic-gate 	if (bracketpriv == NULL)
7000Sstevel@tonic-gate 		return (0);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* Only PRIV_ON and PRIV_OFF are valid */
7030Sstevel@tonic-gate 	if (op == PRIV_SET)
7040Sstevel@tonic-gate 		return (-1);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (setppriv(op, effective, bracketpriv));
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate  * Remove privileges from E & P.
7110Sstevel@tonic-gate  */
7120Sstevel@tonic-gate void
7130Sstevel@tonic-gate __priv_relinquish(void)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	if (bracketpriv != NULL) {
7160Sstevel@tonic-gate 		(void) setppriv(PRIV_OFF, permitted, bracketpriv);
7170Sstevel@tonic-gate 		priv_freeset(bracketpriv);
7180Sstevel@tonic-gate 		bracketpriv = NULL;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate  * Use binary search on the ordered list.
7240Sstevel@tonic-gate  */
7250Sstevel@tonic-gate int
7260Sstevel@tonic-gate __priv_getbyname(const priv_data_t *d, const char *name)
7270Sstevel@tonic-gate {
7281059Scasper 	char *const *list;
7291059Scasper 	const int *order;
7300Sstevel@tonic-gate 	int lo = 0;
7311059Scasper 	int hi;
7321059Scasper 
7331059Scasper 	if (d == NULL)
7341059Scasper 		return (-1);
7351059Scasper 
7361059Scasper 	list = d->pd_privnames;
7371059Scasper 	order = d->pd_setsort;
7381059Scasper 	hi = d->pd_nprivs - 1;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (strncasecmp(name, "priv_", 5) == 0)
7410Sstevel@tonic-gate 		name += 5;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	do {
7440Sstevel@tonic-gate 		int mid = (lo + hi) / 2;
7450Sstevel@tonic-gate 		int res = strcasecmp(name, list[order[mid]]);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		if (res == 0)
7480Sstevel@tonic-gate 			return (order[mid]);
7490Sstevel@tonic-gate 		else if (res < 0)
7500Sstevel@tonic-gate 			hi = mid - 1;
7510Sstevel@tonic-gate 		else
7520Sstevel@tonic-gate 			lo = mid + 1;
7530Sstevel@tonic-gate 	} while (lo <= hi);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	errno = EINVAL;
7560Sstevel@tonic-gate 	return (-1);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate int
7600Sstevel@tonic-gate priv_getbyname(const char *name)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	WITHPRIVLOCKED(int, -1, __priv_getbyname(GETPRIVDATA(), name));
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate int
7660Sstevel@tonic-gate __priv_getsetbyname(const priv_data_t *d, const char *name)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	int i;
7690Sstevel@tonic-gate 	int n = d->pd_nsets;
7700Sstevel@tonic-gate 	char *const *list = d->pd_setnames;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (strncasecmp(name, "priv_", 5) == 0)
7730Sstevel@tonic-gate 		name += 5;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
7760Sstevel@tonic-gate 		if (strcasecmp(list[i], name) == 0)
7770Sstevel@tonic-gate 			return (i);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	errno = EINVAL;
7810Sstevel@tonic-gate 	return (-1);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate int
7850Sstevel@tonic-gate priv_getsetbyname(const char *name)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	/* Not locked: sets don't change */
7880Sstevel@tonic-gate 	return (__priv_getsetbyname(GETPRIVDATA(), name));
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate static const char *
7920Sstevel@tonic-gate priv_bynum(int i, int n, char **list)
7930Sstevel@tonic-gate {
7940Sstevel@tonic-gate 	if (i < 0 || i >= n)
7950Sstevel@tonic-gate 		return (NULL);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	return (list[i]);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate const char *
8010Sstevel@tonic-gate __priv_getbynum(const priv_data_t *d, int num)
8020Sstevel@tonic-gate {
8031059Scasper 	if (d == NULL)
8041059Scasper 		return (NULL);
8050Sstevel@tonic-gate 	return (priv_bynum(num, d->pd_nprivs, d->pd_privnames));
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate const char *
8090Sstevel@tonic-gate priv_getbynum(int num)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	WITHPRIVLOCKED(const char *, NULL, __priv_getbynum(GETPRIVDATA(), num));
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate const char *
8150Sstevel@tonic-gate __priv_getsetbynum(const priv_data_t *d, int num)
8160Sstevel@tonic-gate {
8171059Scasper 	if (d == NULL)
8181059Scasper 		return (NULL);
8190Sstevel@tonic-gate 	return (priv_bynum(num, d->pd_nsets, d->pd_setnames));
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate const char *
8230Sstevel@tonic-gate priv_getsetbynum(int num)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	return (__priv_getsetbynum(GETPRIVDATA(), num));
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * Privilege manipulation functions
8310Sstevel@tonic-gate  *
8320Sstevel@tonic-gate  * Without knowing the details of the privilege set implementation,
8330Sstevel@tonic-gate  * opaque pointers can be used to manipulate sets at will.
8340Sstevel@tonic-gate  */
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate static priv_set_t *
8370Sstevel@tonic-gate __priv_allocset(priv_data_t *d)
8380Sstevel@tonic-gate {
8391059Scasper 	if (d == NULL)
8401059Scasper 		return (NULL);
8411059Scasper 
8420Sstevel@tonic-gate 	return (libc_malloc(d->pd_setsize));
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate priv_set_t *
8460Sstevel@tonic-gate priv_allocset(void)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	return (__priv_allocset(GETPRIVDATA()));
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate void
8520Sstevel@tonic-gate priv_freeset(priv_set_t *p)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	int er = errno;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	libc_free(p);
8570Sstevel@tonic-gate 	errno = er;
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate void
8610Sstevel@tonic-gate __priv_emptyset(priv_data_t *d, priv_set_t *set)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	(void) memset(set, 0, d->pd_setsize);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate void
8670Sstevel@tonic-gate priv_emptyset(priv_set_t *set)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	__priv_emptyset(GETPRIVDATA(), set);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate void
8730Sstevel@tonic-gate __priv_fillset(priv_data_t *d, priv_set_t *set)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	(void) memset(set, ~0, d->pd_setsize);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate void
8790Sstevel@tonic-gate priv_fillset(priv_set_t *set)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 	__priv_fillset(GETPRIVDATA(), set);
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate #define	PRIV_TEST_BODY_D(d, test) \
8860Sstevel@tonic-gate 	int i; \
8870Sstevel@tonic-gate \
8880Sstevel@tonic-gate 	for (i = d->pd_pinfo->priv_setsize; i-- > 0; ) \
8890Sstevel@tonic-gate 		if (!(test)) \
8900Sstevel@tonic-gate 			return (B_FALSE); \
8910Sstevel@tonic-gate \
8920Sstevel@tonic-gate 	return (B_TRUE)
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate boolean_t
8950Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	priv_data_t *d;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	LOADPRIVDATA(d);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	return ((boolean_t)(memcmp(a, b, d->pd_setsize) == 0));
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate boolean_t
9050Sstevel@tonic-gate __priv_isemptyset(priv_data_t *d, const priv_set_t *set)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == 0);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate boolean_t
9110Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	return (__priv_isemptyset(GETPRIVDATA(), set));
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate boolean_t
9170Sstevel@tonic-gate __priv_isfullset(priv_data_t *d, const priv_set_t *set)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == ~(priv_chunk_t)0);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate boolean_t
9230Sstevel@tonic-gate priv_isfullset(const priv_set_t *set)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	return (__priv_isfullset(GETPRIVDATA(), set));
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate  * Return true if a is a subset of b
9300Sstevel@tonic-gate  */
9310Sstevel@tonic-gate boolean_t
9320Sstevel@tonic-gate __priv_issubset(priv_data_t *d, const priv_set_t *a, const priv_set_t *b)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate 	PRIV_TEST_BODY_D(d, (((priv_chunk_t *)a)[i] | ((priv_chunk_t *)b)[i]) ==
9350Sstevel@tonic-gate 		((priv_chunk_t *)b)[i]);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate boolean_t
9390Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	return (__priv_issubset(GETPRIVDATA(), a, b));
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate #define	PRIV_CHANGE_BODY(a, op, b) \
9450Sstevel@tonic-gate 	int i; \
9460Sstevel@tonic-gate 	priv_data_t *d; \
9470Sstevel@tonic-gate \
9480Sstevel@tonic-gate 	LOADPRIVDATA(d); \
9490Sstevel@tonic-gate \
9500Sstevel@tonic-gate 	for (i = 0; i < d->pd_pinfo->priv_setsize; i++) \
9510Sstevel@tonic-gate 		((priv_chunk_t *)a)[i] op \
9520Sstevel@tonic-gate 			((priv_chunk_t *)b)[i]
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /* B = A ^ B */
9550Sstevel@tonic-gate void
9560Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	/* CSTYLED */
9590Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, &=, a);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate /* B = A */
9630Sstevel@tonic-gate void
9640Sstevel@tonic-gate priv_copyset(const priv_set_t *a, priv_set_t *b)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	/* CSTYLED */
9670Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, =, a);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /* B = A v B */
9710Sstevel@tonic-gate void
9720Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate 	/* CSTYLED */
9750Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, |=, a);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate /* A = ! A */
9790Sstevel@tonic-gate void
9800Sstevel@tonic-gate priv_inverse(priv_set_t *a)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	PRIV_CHANGE_BODY(a, = ~, a);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate /*
9860Sstevel@tonic-gate  * Manipulating single privileges.
9870Sstevel@tonic-gate  */
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate int
9900Sstevel@tonic-gate priv_addset(priv_set_t *a, const char *p)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	int priv = priv_getbyname(p);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (priv < 0)
9950Sstevel@tonic-gate 		return (-1);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	PRIV_ADDSET(a, priv);
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	return (0);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate int
10030Sstevel@tonic-gate priv_delset(priv_set_t *a, const char *p)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	int priv = priv_getbyname(p);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	if (priv < 0)
10080Sstevel@tonic-gate 		return (-1);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	PRIV_DELSET(a, priv);
10110Sstevel@tonic-gate 	return (0);
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate boolean_t
10150Sstevel@tonic-gate priv_ismember(const priv_set_t *a, const char *p)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	int priv = priv_getbyname(p);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if (priv < 0)
10200Sstevel@tonic-gate 		return (B_FALSE);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	return ((boolean_t)PRIV_ISMEMBER(a, priv));
10230Sstevel@tonic-gate }
1024