summaryrefslogtreecommitdiff
path: root/usr/src/cmd/devfsadm/devpolicy.c
blob: 068567d1df41a795f0daeece453d9c3a99968eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <priv.h>
#include <string.h>
#include <libgen.h>
#include <errno.h>
#include <libintl.h>
#include <sys/devpolicy.h>
#include <sys/modctl.h>
#include "message.h"
#include "plcysubr.h"

/* Cannot include devfsadm_impl.h because of static definitions */
#define	err_print	devfsadm_errprint
extern void err_print(char *, ...);

#define	PLCY_CHUNK	128

/*
 * devpolicy sort order sorts on three items to help the kernel;
 * the kernel will verify but not sort.
 *
 *	1) major number - but default major will be first in sorted output
 *	2) wildcard or not - non wildcard entries are sorted first.
 *		2a) Expanded minor numbers first (empty name sorts first).
 *		2b) Named minors.
 *	3) length of wildcard entry - longest pattern first
 *
 * The last rule allows patterns such as *ctl and * to be used both
 * unambiguously instead of current bogosities as found in /etc/minor_perm:
 *	rtvc:ctl 0644 root sys
 *	rtvc:rtvcctl* 0644 root sys
 *	rtvc:rtvc[!ctl]* 0666 root sys
 *
 * The last pattern only works by accident.
 *
 * This would simply become (in sorted order):
 *	rtvc:ctl
 *	rtvc:rtvcctl*
 *	rtvc:*
 */

static int
qcmp(const void *a, const void *b)
{
	const devplcysys_t *pa = a;
	const devplcysys_t *pb = b;
	int wilda, wildb;

	/* sort on major number, default major first in sort output */
	if (pa->dps_maj == DEVPOLICY_DFLT_MAJ)
		return (-1);
	if (pb->dps_maj == DEVPOLICY_DFLT_MAJ)
		return (1);

	if (pa->dps_maj > pb->dps_maj)
		return (1);
	else if (pa->dps_maj < pb->dps_maj)
		return (-1);

	wilda = strchr(pa->dps_minornm, '*') != NULL;
	wildb = strchr(pb->dps_minornm, '*') != NULL;

	/* sort the entry with the wildcard last */
	if (wilda != wildb)
		return (wilda - wildb);

	/* entries without wildcards compare with strcmp() */
	if (wilda == 0)
		return (strcmp(pa->dps_minornm, pb->dps_minornm));

	/* shortest wildcard last */
	return ((int)(strlen(pb->dps_minornm) - strlen(pa->dps_minornm)));
}

static int
loadprivs(const char *infile)
{
	char *line, *col;
	FILE *in;
	struct fileentry *fep;
	int res = 0;

	in = fopen(infile, "r");

	if (in == NULL)
		return (0);

	while ((fep = fgetline(in)) != NULL && fep->entry != NULL) {
		line = fep->entry;

		if (*line == '\0')
			continue;

		line[strlen(line)-1] = '\0';

		col = strchr(line, ':');

		if (col != NULL) {
			major_t maj;
			*col = '\0';

			if (modctl(MODGETMAJBIND, line, col - line + 1, &maj)
			    != 0)
				continue;

			line = col + 1;
		}

		if (modctl(MODALLOCPRIV, line) != 0) {
			(void) err_print("modctl(MODALLOCPRIV, %s): %s\n",
				line, strerror(errno));
			res = -1;
		}
	}
	return (res);
}

static int
loadpolicy(const char *infile)
{
	char *line;
	int nalloc = 0, cnt = 0;
	char *mem = NULL;
	devplcysys_t *dp, *dflt = NULL;
	FILE *in;
	struct fileentry *fep;
	int res;

	char *maj;
	char *tok;
	char *min;

	in = fopen(infile, "r");

	if (in == NULL) {
		err_print(OPEN_FAILED, infile, strerror(errno));
		return (-1);
	}

	while ((fep = fgetline(in)) != NULL && fep->entry != NULL) {
		line = fep->entry;
		if (cnt >= nalloc) {
			nalloc += PLCY_CHUNK;
			mem = realloc(mem, nalloc * devplcysys_sz);
			if (mem == NULL) {
				err_print(MALLOC_FAILED,
					nalloc * devplcysys_sz);
				return (-1);
			}

			/* Readjust pointer to dflt after realloc */
			if (dflt != NULL)
				/* LINTED: alignment */
				dflt = (devplcysys_t *)mem;
		}
		maj = strtok(line, "\n\t ");

		if (maj == NULL)
			continue;

		/* LINTED: alignment */
		dp = (devplcysys_t *)(mem + devplcysys_sz * cnt);

		if (strcmp(maj, "*") == 0) {
			if (dflt != NULL) {
				err_print(DPLCY_ONE_DFLT, infile);
				return (-1);
			}
			(void) memset(dp, 0, devplcysys_sz);
			dp->dps_maj = DEVPOLICY_DFLT_MAJ;
			dflt = dp;
		} else {
			if (dflt == NULL) {
				err_print(DPLCY_FIRST, infile);
				return (-1);
			}

			(void) memcpy(dp, dflt, devplcysys_sz);

			min = strchr(maj, ':');

			if (min != NULL) {
				*min++ = '\0';
				if (strchr(min, ':') != NULL) {
					(void) fprintf(stderr,
					    "Too many ``:'' in entry\n");
					return (-1);
				}
			} else
				min = "*";

			/* Silently ignore unknown devices. */
			if (modctl(MODGETMAJBIND, maj, strlen(maj) + 1,
			    &dp->dps_maj) != 0)
				continue;

			if (*min == '(') {
				/* Numeric minor range */
				char type;

				if (parse_minor_range(min, &dp->dps_lomin,
				    &dp->dps_himin, &type) == -1) {
					err_print(INVALID_MINOR, min);
					return (-1);
				}
				dp->dps_isblock = type == 'b';
			} else {
				if (strlen(min) >= sizeof (dp->dps_minornm)) {
					err_print(MINOR_TOO_LONG, maj, min);
					return (-1);
				}
				(void) strcpy(dp->dps_minornm, min);
			}
		}

		while (tok = strtok(NULL, "\n\t ")) {
			if (parse_plcy_token(tok, dp)) {
				err_print(BAD_ENTRY, fep->startline,
					fep->orgentry);
				return (-1);
			}
		}
		cnt++;
	}
	if (fep == NULL) {
		if (feof(in))
			err_print(UNEXPECTED_EOF, infile);
		else
			err_print(NO_MEMORY);
		return (-1);
	}
	qsort(mem, cnt, devplcysys_sz, qcmp);

	if ((res = modctl(MODSETDEVPOLICY, cnt, devplcysys_sz, mem)) != 0)
		err_print("modctl(MODSETDEVPOLICY): %s\n", strerror(errno));

	return (res);
}

int
load_devpolicy(void)
{
	int res;

	devplcy_init();

	res = loadprivs(EXTRA_PRIVS);
	res += loadpolicy(DEV_POLICY);

	return (res);
}