1*45916cd2Sjpk /*
2*45916cd2Sjpk * CDDL HEADER START
3*45916cd2Sjpk *
4*45916cd2Sjpk * The contents of this file are subject to the terms of the
5*45916cd2Sjpk * Common Development and Distribution License (the "License").
6*45916cd2Sjpk * You may not use this file except in compliance with the License.
7*45916cd2Sjpk *
8*45916cd2Sjpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*45916cd2Sjpk * or http://www.opensolaris.org/os/licensing.
10*45916cd2Sjpk * See the License for the specific language governing permissions
11*45916cd2Sjpk * and limitations under the License.
12*45916cd2Sjpk *
13*45916cd2Sjpk * When distributing Covered Code, include this CDDL HEADER in each
14*45916cd2Sjpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*45916cd2Sjpk * If applicable, add the following below this CDDL HEADER, with the
16*45916cd2Sjpk * fields enclosed by brackets "[]" replaced with your own identifying
17*45916cd2Sjpk * information: Portions Copyright [yyyy] [name of copyright owner]
18*45916cd2Sjpk *
19*45916cd2Sjpk * CDDL HEADER END
20*45916cd2Sjpk */
21*45916cd2Sjpk /*
22*45916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*45916cd2Sjpk * Use is subject to license terms.
24*45916cd2Sjpk */
25*45916cd2Sjpk
26*45916cd2Sjpk #include <errno.h>
27*45916cd2Sjpk #include <priv.h>
28*45916cd2Sjpk #include <sys/tsol/priv.h>
29*45916cd2Sjpk #include <sys/varargs.h>
30*45916cd2Sjpk
31*45916cd2Sjpk /*
32*45916cd2Sjpk * set_effective_priv(op, num_priv, priv_id1, priv_id2, ... )
33*45916cd2Sjpk *
34*45916cd2Sjpk * Library routine to enable a user process to set its effective
35*45916cd2Sjpk * privilege set appropriately using a single call. User is
36*45916cd2Sjpk * required to specify the number of privilege ids that follow as
37*45916cd2Sjpk * arguments, rather than depending on the compiler to terminate
38*45916cd2Sjpk * the argument list with a NULL, which may be compiler-dependent.
39*45916cd2Sjpk */
40*45916cd2Sjpk int
set_effective_priv(priv_op_t op,int num_priv,...)41*45916cd2Sjpk set_effective_priv(priv_op_t op, int num_priv, ...)
42*45916cd2Sjpk {
43*45916cd2Sjpk priv_set_t *priv_set;
44*45916cd2Sjpk priv_t priv_id;
45*45916cd2Sjpk va_list ap;
46*45916cd2Sjpk int status;
47*45916cd2Sjpk
48*45916cd2Sjpk priv_set = priv_allocset();
49*45916cd2Sjpk PRIV_EMPTY(priv_set);
50*45916cd2Sjpk
51*45916cd2Sjpk va_start(ap, num_priv);
52*45916cd2Sjpk while (num_priv--) {
53*45916cd2Sjpk char *priv_name;
54*45916cd2Sjpk /*
55*45916cd2Sjpk * Do sanity checking on priv_id's here to assure
56*45916cd2Sjpk * valid inputs to privilege macros. This checks
57*45916cd2Sjpk * num_priv argument as well.
58*45916cd2Sjpk */
59*45916cd2Sjpk priv_id = va_arg(ap, priv_t);
60*45916cd2Sjpk priv_name = (char *)priv_getbynum((int)(uintptr_t)priv_id);
61*45916cd2Sjpk if (priv_name == NULL) {
62*45916cd2Sjpk errno = EINVAL;
63*45916cd2Sjpk priv_freeset(priv_set);
64*45916cd2Sjpk return (-1);
65*45916cd2Sjpk }
66*45916cd2Sjpk (void) priv_addset(priv_set, priv_name);
67*45916cd2Sjpk }
68*45916cd2Sjpk va_end(ap);
69*45916cd2Sjpk
70*45916cd2Sjpk /*
71*45916cd2Sjpk * Depend on system call to do sanity checking on "op"
72*45916cd2Sjpk */
73*45916cd2Sjpk status = setppriv(op, PRIV_EFFECTIVE, priv_set);
74*45916cd2Sjpk priv_freeset(priv_set);
75*45916cd2Sjpk return (status);
76*45916cd2Sjpk
77*45916cd2Sjpk } /* set_effective_priv() */
78*45916cd2Sjpk
79*45916cd2Sjpk
80*45916cd2Sjpk
81*45916cd2Sjpk
82*45916cd2Sjpk /*
83*45916cd2Sjpk * set_inheritable_priv(op, num_priv, priv_id1, priv_id2, ... )
84*45916cd2Sjpk *
85*45916cd2Sjpk * Library routine to enable a user process to set its inheritable
86*45916cd2Sjpk * privilege set appropriately using a single call. User is
87*45916cd2Sjpk * required to specify the number of privilege ids that follow as
88*45916cd2Sjpk * arguments, rather than depending on the compiler to terminate
89*45916cd2Sjpk * the argument list with a NULL, which may be compiler-dependent.
90*45916cd2Sjpk */
91*45916cd2Sjpk int
set_inheritable_priv(priv_op_t op,int num_priv,...)92*45916cd2Sjpk set_inheritable_priv(priv_op_t op, int num_priv, ...)
93*45916cd2Sjpk {
94*45916cd2Sjpk priv_set_t *priv_set;
95*45916cd2Sjpk priv_t priv_id;
96*45916cd2Sjpk va_list ap;
97*45916cd2Sjpk int status;
98*45916cd2Sjpk
99*45916cd2Sjpk priv_set = priv_allocset();
100*45916cd2Sjpk
101*45916cd2Sjpk PRIV_EMPTY(priv_set);
102*45916cd2Sjpk
103*45916cd2Sjpk va_start(ap, num_priv);
104*45916cd2Sjpk while (num_priv--) {
105*45916cd2Sjpk /*
106*45916cd2Sjpk * Do sanity checking on priv_id's here to assure
107*45916cd2Sjpk * valid inputs to privilege macros. This checks
108*45916cd2Sjpk * num_priv argument as well.
109*45916cd2Sjpk */
110*45916cd2Sjpk priv_id = va_arg(ap, priv_t);
111*45916cd2Sjpk if ((char *)priv_getbynum((int)(uintptr_t)priv_id) == NULL) {
112*45916cd2Sjpk errno = EINVAL;
113*45916cd2Sjpk priv_freeset(priv_set);
114*45916cd2Sjpk return (-1);
115*45916cd2Sjpk }
116*45916cd2Sjpk (void) PRIV_ASSERT(priv_set, priv_id);
117*45916cd2Sjpk }
118*45916cd2Sjpk va_end(ap);
119*45916cd2Sjpk
120*45916cd2Sjpk /*
121*45916cd2Sjpk * Depend on system call to do sanity checking on "op"
122*45916cd2Sjpk */
123*45916cd2Sjpk status = setppriv(op, PRIV_INHERITABLE, priv_set);
124*45916cd2Sjpk priv_freeset(priv_set);
125*45916cd2Sjpk return (status);
126*45916cd2Sjpk
127*45916cd2Sjpk } /* set_inheritable_priv() */
128*45916cd2Sjpk
129*45916cd2Sjpk
130*45916cd2Sjpk
131*45916cd2Sjpk
132*45916cd2Sjpk /*
133*45916cd2Sjpk * set_permitted_priv(op, num_priv, priv_id1, priv_id2, ... )
134*45916cd2Sjpk *
135*45916cd2Sjpk * Library routine to enable a user process to set its permitted
136*45916cd2Sjpk * privilege set appropriately using a single call. User is
137*45916cd2Sjpk * required to specify the number of privilege ids that follow as
138*45916cd2Sjpk * arguments, rather than depending on the compiler to terminate
139*45916cd2Sjpk * the argument list with a NULL, which may be compiler-dependent.
140*45916cd2Sjpk */
141*45916cd2Sjpk int
set_permitted_priv(priv_op_t op,int num_priv,...)142*45916cd2Sjpk set_permitted_priv(priv_op_t op, int num_priv, ...)
143*45916cd2Sjpk {
144*45916cd2Sjpk priv_set_t *priv_set;
145*45916cd2Sjpk priv_t priv_id;
146*45916cd2Sjpk va_list ap;
147*45916cd2Sjpk int status;
148*45916cd2Sjpk
149*45916cd2Sjpk priv_set = priv_allocset();
150*45916cd2Sjpk
151*45916cd2Sjpk PRIV_EMPTY(priv_set);
152*45916cd2Sjpk
153*45916cd2Sjpk va_start(ap, num_priv);
154*45916cd2Sjpk while (num_priv--) {
155*45916cd2Sjpk /*
156*45916cd2Sjpk * Do sanity checking on priv_id's here to assure
157*45916cd2Sjpk * valid inputs to privilege macros. This checks
158*45916cd2Sjpk * num_priv argument as well.
159*45916cd2Sjpk */
160*45916cd2Sjpk priv_id = va_arg(ap, priv_t);
161*45916cd2Sjpk if ((char *)priv_getbynum((int)(uintptr_t)priv_id) == NULL) {
162*45916cd2Sjpk errno = EINVAL;
163*45916cd2Sjpk priv_freeset(priv_set);
164*45916cd2Sjpk return (-1);
165*45916cd2Sjpk }
166*45916cd2Sjpk (void) PRIV_ASSERT(priv_set, priv_id);
167*45916cd2Sjpk }
168*45916cd2Sjpk va_end(ap);
169*45916cd2Sjpk
170*45916cd2Sjpk /*
171*45916cd2Sjpk * Depend on system call to do sanity checking on "op"
172*45916cd2Sjpk */
173*45916cd2Sjpk status = setppriv(op, PRIV_PERMITTED, priv_set);
174*45916cd2Sjpk priv_freeset(priv_set);
175*45916cd2Sjpk return (status);
176*45916cd2Sjpk
177*45916cd2Sjpk } /* set_permitted_priv() */
178