xref: /onnv-gate/usr/src/cmd/devfsadm/devpolicy.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <ctype.h>
33*0Sstevel@tonic-gate #include <priv.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <libgen.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <libintl.h>
38*0Sstevel@tonic-gate #include <sys/devpolicy.h>
39*0Sstevel@tonic-gate #include <sys/modctl.h>
40*0Sstevel@tonic-gate #include "message.h"
41*0Sstevel@tonic-gate #include "plcysubr.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /* Cannot include devfsadm_impl.h because of static definitions */
44*0Sstevel@tonic-gate #define	err_print	devfsadm_errprint
45*0Sstevel@tonic-gate extern void err_print(char *, ...);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	PLCY_CHUNK	128
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * devpolicy sort order sorts on three items to help the kernel;
51*0Sstevel@tonic-gate  * the kernel will verify but not sort.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  *	1) major number - but default major will be first in sorted output
54*0Sstevel@tonic-gate  *	2) wildcard or not - non wildcard entries are sorted first.
55*0Sstevel@tonic-gate  *		2a) Expanded minor numbers first (empty name sorts first).
56*0Sstevel@tonic-gate  *		2b) Named minors.
57*0Sstevel@tonic-gate  *	3) length of wildcard entry - longest pattern first
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * The last rule allows patterns such as *ctl and * to be used both
60*0Sstevel@tonic-gate  * unambiguously instead of current bogosities as found in /etc/minor_perm:
61*0Sstevel@tonic-gate  *	rtvc:ctl 0644 root sys
62*0Sstevel@tonic-gate  *	rtvc:rtvcctl* 0644 root sys
63*0Sstevel@tonic-gate  *	rtvc:rtvc[!ctl]* 0666 root sys
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * The last pattern only works by accident.
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * This would simply become (in sorted order):
68*0Sstevel@tonic-gate  *	rtvc:ctl
69*0Sstevel@tonic-gate  *	rtvc:rtvcctl*
70*0Sstevel@tonic-gate  *	rtvc:*
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static int
qcmp(const void * a,const void * b)74*0Sstevel@tonic-gate qcmp(const void *a, const void *b)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	const devplcysys_t *pa = a;
77*0Sstevel@tonic-gate 	const devplcysys_t *pb = b;
78*0Sstevel@tonic-gate 	int wilda, wildb;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/* sort on major number, default major first in sort output */
81*0Sstevel@tonic-gate 	if (pa->dps_maj == DEVPOLICY_DFLT_MAJ)
82*0Sstevel@tonic-gate 		return (-1);
83*0Sstevel@tonic-gate 	if (pb->dps_maj == DEVPOLICY_DFLT_MAJ)
84*0Sstevel@tonic-gate 		return (1);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (pa->dps_maj > pb->dps_maj)
87*0Sstevel@tonic-gate 		return (1);
88*0Sstevel@tonic-gate 	else if (pa->dps_maj < pb->dps_maj)
89*0Sstevel@tonic-gate 		return (-1);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	wilda = strchr(pa->dps_minornm, '*') != NULL;
92*0Sstevel@tonic-gate 	wildb = strchr(pb->dps_minornm, '*') != NULL;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	/* sort the entry with the wildcard last */
95*0Sstevel@tonic-gate 	if (wilda != wildb)
96*0Sstevel@tonic-gate 		return (wilda - wildb);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/* entries without wildcards compare with strcmp() */
99*0Sstevel@tonic-gate 	if (wilda == 0)
100*0Sstevel@tonic-gate 		return (strcmp(pa->dps_minornm, pb->dps_minornm));
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	/* shortest wildcard last */
103*0Sstevel@tonic-gate 	return ((int)(strlen(pb->dps_minornm) - strlen(pa->dps_minornm)));
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static int
loadprivs(const char * infile)107*0Sstevel@tonic-gate loadprivs(const char *infile)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	char *line, *col;
110*0Sstevel@tonic-gate 	FILE *in;
111*0Sstevel@tonic-gate 	struct fileentry *fep;
112*0Sstevel@tonic-gate 	int res = 0;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	in = fopen(infile, "r");
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if (in == NULL)
117*0Sstevel@tonic-gate 		return (0);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	while ((fep = fgetline(in)) != NULL && fep->entry != NULL) {
120*0Sstevel@tonic-gate 		line = fep->entry;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		if (*line == '\0')
123*0Sstevel@tonic-gate 			continue;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		line[strlen(line)-1] = '\0';
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		col = strchr(line, ':');
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		if (col != NULL) {
130*0Sstevel@tonic-gate 			major_t maj;
131*0Sstevel@tonic-gate 			*col = '\0';
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 			if (modctl(MODGETMAJBIND, line, col - line + 1, &maj)
134*0Sstevel@tonic-gate 			    != 0)
135*0Sstevel@tonic-gate 				continue;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 			line = col + 1;
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		if (modctl(MODALLOCPRIV, line) != 0) {
141*0Sstevel@tonic-gate 			(void) err_print("modctl(MODALLOCPRIV, %s): %s\n",
142*0Sstevel@tonic-gate 				line, strerror(errno));
143*0Sstevel@tonic-gate 			res = -1;
144*0Sstevel@tonic-gate 		}
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 	return (res);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static int
loadpolicy(const char * infile)150*0Sstevel@tonic-gate loadpolicy(const char *infile)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	char *line;
153*0Sstevel@tonic-gate 	int nalloc = 0, cnt = 0;
154*0Sstevel@tonic-gate 	char *mem = NULL;
155*0Sstevel@tonic-gate 	devplcysys_t *dp, *dflt = NULL;
156*0Sstevel@tonic-gate 	FILE *in;
157*0Sstevel@tonic-gate 	struct fileentry *fep;
158*0Sstevel@tonic-gate 	int res;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	char *maj;
161*0Sstevel@tonic-gate 	char *tok;
162*0Sstevel@tonic-gate 	char *min;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	in = fopen(infile, "r");
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (in == NULL) {
167*0Sstevel@tonic-gate 		err_print(OPEN_FAILED, infile, strerror(errno));
168*0Sstevel@tonic-gate 		return (-1);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	while ((fep = fgetline(in)) != NULL && fep->entry != NULL) {
172*0Sstevel@tonic-gate 		line = fep->entry;
173*0Sstevel@tonic-gate 		if (cnt >= nalloc) {
174*0Sstevel@tonic-gate 			nalloc += PLCY_CHUNK;
175*0Sstevel@tonic-gate 			mem = realloc(mem, nalloc * devplcysys_sz);
176*0Sstevel@tonic-gate 			if (mem == NULL) {
177*0Sstevel@tonic-gate 				err_print(MALLOC_FAILED,
178*0Sstevel@tonic-gate 					nalloc * devplcysys_sz);
179*0Sstevel@tonic-gate 				return (-1);
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 			/* Readjust pointer to dflt after realloc */
183*0Sstevel@tonic-gate 			if (dflt != NULL)
184*0Sstevel@tonic-gate 				/* LINTED: alignment */
185*0Sstevel@tonic-gate 				dflt = (devplcysys_t *)mem;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 		maj = strtok(line, "\n\t ");
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		if (maj == NULL)
190*0Sstevel@tonic-gate 			continue;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		/* LINTED: alignment */
193*0Sstevel@tonic-gate 		dp = (devplcysys_t *)(mem + devplcysys_sz * cnt);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		if (strcmp(maj, "*") == 0) {
196*0Sstevel@tonic-gate 			if (dflt != NULL) {
197*0Sstevel@tonic-gate 				err_print(DPLCY_ONE_DFLT, infile);
198*0Sstevel@tonic-gate 				return (-1);
199*0Sstevel@tonic-gate 			}
200*0Sstevel@tonic-gate 			(void) memset(dp, 0, devplcysys_sz);
201*0Sstevel@tonic-gate 			dp->dps_maj = DEVPOLICY_DFLT_MAJ;
202*0Sstevel@tonic-gate 			dflt = dp;
203*0Sstevel@tonic-gate 		} else {
204*0Sstevel@tonic-gate 			if (dflt == NULL) {
205*0Sstevel@tonic-gate 				err_print(DPLCY_FIRST, infile);
206*0Sstevel@tonic-gate 				return (-1);
207*0Sstevel@tonic-gate 			}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 			(void) memcpy(dp, dflt, devplcysys_sz);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 			min = strchr(maj, ':');
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 			if (min != NULL) {
214*0Sstevel@tonic-gate 				*min++ = '\0';
215*0Sstevel@tonic-gate 				if (strchr(min, ':') != NULL) {
216*0Sstevel@tonic-gate 					(void) fprintf(stderr,
217*0Sstevel@tonic-gate 					    "Too many ``:'' in entry\n");
218*0Sstevel@tonic-gate 					return (-1);
219*0Sstevel@tonic-gate 				}
220*0Sstevel@tonic-gate 			} else
221*0Sstevel@tonic-gate 				min = "*";
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 			/* Silently ignore unknown devices. */
224*0Sstevel@tonic-gate 			if (modctl(MODGETMAJBIND, maj, strlen(maj) + 1,
225*0Sstevel@tonic-gate 			    &dp->dps_maj) != 0)
226*0Sstevel@tonic-gate 				continue;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 			if (*min == '(') {
229*0Sstevel@tonic-gate 				/* Numeric minor range */
230*0Sstevel@tonic-gate 				char type;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 				if (parse_minor_range(min, &dp->dps_lomin,
233*0Sstevel@tonic-gate 				    &dp->dps_himin, &type) == -1) {
234*0Sstevel@tonic-gate 					err_print(INVALID_MINOR, min);
235*0Sstevel@tonic-gate 					return (-1);
236*0Sstevel@tonic-gate 				}
237*0Sstevel@tonic-gate 				dp->dps_isblock = type == 'b';
238*0Sstevel@tonic-gate 			} else {
239*0Sstevel@tonic-gate 				if (strlen(min) >= sizeof (dp->dps_minornm)) {
240*0Sstevel@tonic-gate 					err_print(MINOR_TOO_LONG, maj, min);
241*0Sstevel@tonic-gate 					return (-1);
242*0Sstevel@tonic-gate 				}
243*0Sstevel@tonic-gate 				(void) strcpy(dp->dps_minornm, min);
244*0Sstevel@tonic-gate 			}
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		while (tok = strtok(NULL, "\n\t ")) {
248*0Sstevel@tonic-gate 			if (parse_plcy_token(tok, dp)) {
249*0Sstevel@tonic-gate 				err_print(BAD_ENTRY, fep->startline,
250*0Sstevel@tonic-gate 					fep->orgentry);
251*0Sstevel@tonic-gate 				return (-1);
252*0Sstevel@tonic-gate 			}
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 		cnt++;
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 	if (fep == NULL) {
257*0Sstevel@tonic-gate 		if (feof(in))
258*0Sstevel@tonic-gate 			err_print(UNEXPECTED_EOF, infile);
259*0Sstevel@tonic-gate 		else
260*0Sstevel@tonic-gate 			err_print(NO_MEMORY);
261*0Sstevel@tonic-gate 		return (-1);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 	qsort(mem, cnt, devplcysys_sz, qcmp);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if ((res = modctl(MODSETDEVPOLICY, cnt, devplcysys_sz, mem)) != 0)
266*0Sstevel@tonic-gate 		err_print("modctl(MODSETDEVPOLICY): %s\n", strerror(errno));
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	return (res);
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate int
load_devpolicy(void)272*0Sstevel@tonic-gate load_devpolicy(void)
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate 	int res;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	devplcy_init();
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	res = loadprivs(EXTRA_PRIVS);
279*0Sstevel@tonic-gate 	res += loadpolicy(DEV_POLICY);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	return (res);
282*0Sstevel@tonic-gate }
283