summaryrefslogtreecommitdiffstats
path: root/sbin/devfs/rule.c
blob: f1fd908753e3754fecb6e6af62bd9cfa4e6e1b42 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*-
 * Copyright (c) 2002 Dima Dorfman.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Rule subsystem manipulation.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/ioctl.h>

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "extern.h"

static void rulespec_infp(FILE *fp, unsigned long reqest, devfs_rsnum rsnum);
static void rulespec_instr(struct devfs_rule *dr, const char *str,
    devfs_rsnum rsnum);
static void rulespec_intok(struct devfs_rule *dr, int ac, char **av,
    devfs_rsnum rsnum);
static void rulespec_outfp(FILE *fp, struct devfs_rule *dr);

static command_t rule_add, rule_apply, rule_applyset;
static command_t rule_del, rule_delset, rule_show, rule_showsets;

static ctbl_t ctbl_rule = {
	{ "add",		rule_add },
	{ "apply",		rule_apply },
	{ "applyset",		rule_applyset },
	{ "del",		rule_del },
	{ "delset",		rule_delset },
	{ "show",		rule_show },
	{ "showsets",		rule_showsets },
	{ NULL,			NULL }
};

static struct intstr ist_type[] = {
	{ "disk",		D_DISK },
	{ "mem",		D_MEM },
	{ "tape",		D_TAPE },
	{ "tty",		D_TTY },
	{ NULL,			-1 }
};

devfs_rsnum in_rsnum;

int
rule_main(int ac, char **av)
{
	struct cmd *c;
	char ch;

	setprogname("devfs rule");
	optreset = optind = 1;
	while ((ch = getopt(ac, av, "s:")) != -1)
		switch (ch) {
		case 's':
			in_rsnum = eatonum(optarg);
			break;
		default:
			usage();
		}
	ac -= optind;
	av += optind;
	if (ac < 1)
		usage();

	for (c = ctbl_rule; c->name != NULL; ++c)
		if (strcmp(c->name, av[0]) == 0)
			exit((*c->handler)(ac, av));
	errx(1, "unknown command: %s", av[0]);
}

static int
rule_add(int ac, char **av)
{
	struct devfs_rule dr;
	int rv;

	if (ac < 2)
		usage();
	if (strcmp(av[1], "-") == 0)
		rulespec_infp(stdin, DEVFSIO_RADD, in_rsnum);
	else {
		rulespec_intok(&dr, ac - 1, av + 1, in_rsnum);
		rv = ioctl(mpfd, DEVFSIO_RADD, &dr);
		if (rv == -1)
			err(1, "ioctl DEVFSIO_RADD");
	}
	return (0);
}

static int
rule_apply(int ac __unused, char **av __unused)
{
	struct devfs_rule dr;
	devfs_rnum rnum;
	devfs_rid rid;
	int rv;

	if (ac < 2)
		usage();
	if (!atonum(av[1], &rnum)) {
		if (strcmp(av[1], "-") == 0)
			rulespec_infp(stdin, DEVFSIO_RAPPLY, in_rsnum);
		else {
			rulespec_intok(&dr, ac - 1, av + 1, in_rsnum);
			rv = ioctl(mpfd, DEVFSIO_RAPPLY, &dr);
			if (rv == -1)
				err(1, "ioctl DEVFSIO_RAPPLY");
		}
	} else {
		rid = mkrid(in_rsnum, rnum);
		rv = ioctl(mpfd, DEVFSIO_RAPPLYID, &rid);
		if (rv == -1)
			err(1, "ioctl DEVFSIO_RAPPLYID");
	}
	return (0);
}

static int
rule_applyset(int ac, char **av __unused)
{
	int rv;

	if (ac != 1)
		usage();
	rv = ioctl(mpfd, DEVFSIO_SAPPLY, &in_rsnum);
	if (rv == -1)
		err(1, "ioctl DEVFSIO_SAPPLY");
	return (0);
}

static int
rule_del(int ac __unused, char **av)
{
	devfs_rid rid;
	int rv;

	if (av[1] == NULL)
		usage();
	rid = mkrid(in_rsnum, eatoi(av[1]));
	rv = ioctl(mpfd, DEVFSIO_RDEL, &rid);
	if (rv == -1)
		err(1, "ioctl DEVFSIO_RDEL");
	return (0);
}

static int
rule_delset(int ac, char **av __unused)
{
	struct devfs_rule dr;
	int rv;

	if (ac != 1)
		usage();
	memset(&dr, '\0', sizeof(dr));
	dr.dr_magic = DEVFS_MAGIC;
	dr.dr_id = mkrid(in_rsnum, 0);
	while (ioctl(mpfd, DEVFSIO_RGETNEXT, &dr) != -1) {
		rv = ioctl(mpfd, DEVFSIO_RDEL, &dr.dr_id);
		if (rv == -1)
			err(1, "ioctl DEVFSIO_RDEL");
	}
	if (errno != ENOENT)
		err(1, "ioctl DEVFSIO_RGETNEXT");
	return (0);
}

static int
rule_show(int ac __unused, char **av)
{
	struct devfs_rule dr;
	devfs_rnum rnum;
	int rv;

	memset(&dr, '\0', sizeof(dr));
	dr.dr_magic = DEVFS_MAGIC;
	if (av[1] != NULL) {
		rnum = eatoi(av[1]);
		dr.dr_id = mkrid(in_rsnum, rnum - 1);
		rv = ioctl(mpfd, DEVFSIO_RGETNEXT, &dr);
		if (rv == -1)
			err(1, "ioctl DEVFSIO_RGETNEXT");
		if (rid2rn(dr.dr_id) == rnum)
			rulespec_outfp(stdout, &dr);
	} else {
		dr.dr_id = mkrid(in_rsnum, 0);
		while (ioctl(mpfd, DEVFSIO_RGETNEXT, &dr) != -1)
			rulespec_outfp(stdout, &dr);
		if (errno != ENOENT)
			err(1, "ioctl DEVFSIO_RGETNEXT");
	}
	return (0);
}

static int
rule_showsets(int ac, char **av __unused)
{
	devfs_rsnum rsnum;

	if (ac != 1)
		usage();
	rsnum = 0;
	while (ioctl(mpfd, DEVFSIO_SGETNEXT, &rsnum) != -1)
		printf("%d\n", rsnum);
	if (errno != ENOENT)
		err(1, "ioctl DEVFSIO_SGETNEXT");
	return (0);
}

int
ruleset_main(int ac, char **av)
{
	devfs_rsnum rsnum;
	int rv;

	setprogname("devfs ruleset");
	if (ac < 2)
		usage();
	rsnum = eatonum(av[1]);
	rv = ioctl(mpfd, DEVFSIO_SUSE, &rsnum);
	if (rv == -1)
		err(1, "ioctl DEVFSIO_SUSE");
	return (0);
}


/*
 * Input rules from a file (probably the standard input).  This
 * differs from the other rulespec_in*() routines in that it also
 * calls ioctl() for the rules, since it is impractical (and not very
 * useful) to return a list (or array) of rules, just so the caller
 * can call call ioctl() for each of them.
 */
static void
rulespec_infp(FILE *fp, unsigned long request, devfs_rsnum rsnum)
{
	struct devfs_rule dr;
	char *line;
	int rv;

	assert(fp == stdin);	/* XXX: De-hardcode "stdin" from error msg. */
	while (efgetln(fp, &line)) {
		rulespec_instr(&dr, line, rsnum);
		rv = ioctl(mpfd, request, &dr);
		if (rv == -1)
			err(1, "ioctl");
		free(line);	/* efgetln() always malloc()s. */
	}
	if (ferror(stdin))
		err(1, "stdin");
}

/*
 * Construct a /struct devfs_rule/ from a string.
 */
static void
rulespec_instr(struct devfs_rule *dr, const char *str, devfs_rsnum rsnum)
{
	char **av;
	int ac;

	tokenize(str, &ac, &av);
	if (ac == 0)
		errx(1, "unexpected end of rulespec");
	rulespec_intok(dr, ac, av, rsnum);
	free(av[0]);
	free(av);
}

/*
 * Construct a /struct devfs_rule/ from ac and av.
 */
static void
rulespec_intok(struct devfs_rule *dr, int ac __unused, char **av,
    devfs_rsnum rsnum)
{
	struct intstr *is;
	struct passwd *pw;
	struct group *gr;
	devfs_rnum rnum;
	char *cp;
	long l;

	memset(dr, '\0', sizeof(*dr));

	/*
	 * We don't maintain ac hereinafter.
	 */
	if (av[0] == NULL)
		errx(1, "unexpected end of rulespec");

	/* If the first argument is an integer, treat it as a rule number. */
	if (!atonum(av[0], &rnum))
		rnum = 0;		/* auto-number */
	else
		++av;

	/*
	 * These aren't table-driven since that would result in more
	 * tiny functions than I care to deal with.
	 */
	for (;;) {
		if (av[0] == NULL)
			break;
		else if (strcmp(av[0], "type") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for type");
			for (is = ist_type; is->s != NULL; ++is)
				if (strcmp(av[1], is->s) == 0) {
					dr->dr_dswflags |= is->i;
					break;
				}
			if (is->s == NULL)
				errx(1, "unknown type: %s", av[1]);
			dr->dr_icond |= DRC_DSWFLAGS;
			av += 2;
		} else if (strcmp(av[0], "path") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for path");
			if (strlcpy(dr->dr_pathptrn, av[1], DEVFS_MAXPTRNLEN)
			    >= DEVFS_MAXPTRNLEN)
				warnx("pattern specified too long; truncated");
			dr->dr_icond |= DRC_PATHPTRN;
			av += 2;
		} else if (strcmp(av[0], "major") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for major");
			dr->dr_major = eatoi(av[1]);
			dr->dr_icond |= DRC_MAJOR;
			av += 2;
		} else
			break;
	}
	for (;;) {
		if (av[0] == NULL)
			break;
		else if (strcmp(av[0], "hide") == 0) {
			dr->dr_iacts |= DRA_BACTS;
			dr->dr_bacts |= DRB_HIDE;
			++av;
		} else if (strcmp(av[0], "unhide") == 0) {
			dr->dr_iacts |= DRA_BACTS;
			dr->dr_bacts |= DRB_UNHIDE;
			++av;
		} else if (strcmp(av[0], "user") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for user");
			dr->dr_iacts |= DRA_UID;
			pw = getpwnam(av[1]);
			if (pw != NULL)
				dr->dr_uid = pw->pw_uid;
			else
				dr->dr_uid = eatoi(av[1]); /* XXX overflow */
			av += 2;
		} else if (strcmp(av[0], "group") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for group");
			dr->dr_iacts |= DRA_GID;
			gr = getgrnam(av[1]);
			if (gr != NULL)
				dr->dr_gid = gr->gr_gid;
			else
				dr->dr_gid = eatoi(av[1]); /* XXX overflow */
			av += 2;
		} else if (strcmp(av[0], "mode") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for mode");
			dr->dr_iacts |= DRA_MODE;
			l = strtol(av[1], &cp, 8);
			if (l > (1 << (sizeof(dr->dr_mode) * 8)) - 1 ||
			    *cp != '\0')
				errx(1, "invalid mode: %s", av[1]);
			dr->dr_mode = l;
			av += 2;
		} else if (strcmp(av[0], "include") == 0) {
			if (av[1] == NULL)
				errx(1, "expecting argument for include");
			dr->dr_iacts |= DRA_INCSET;
			dr->dr_incset = eatonum(av[1]);
			av += 2;
		} else
			errx(1, "unknown argument: %s", av[0]);
	}

	dr->dr_id = mkrid(rsnum, rnum);
	dr->dr_magic = DEVFS_MAGIC;
}

/*
 * Write a human-readable (and machine-parsable, by rulespec_in*())
 * representation of dr to bufp.  *bufp should be free(3)'d when the
 * caller is finished with it.
 */
static void
rulespec_outfp(FILE *fp, struct devfs_rule *dr)
{
	struct intstr *is;
	struct passwd *pw;
	struct group *gr;

	fprintf(fp, "%d", rid2rn(dr->dr_id));

	if (dr->dr_icond & DRC_DSWFLAGS)
		for (is = ist_type; is->s != NULL; ++is)
			if (dr->dr_dswflags & is->i)
				fprintf(fp, " type %s", is->s);
	if (dr->dr_icond & DRC_PATHPTRN)
		fprintf(fp, " path %s", dr->dr_pathptrn);
	if (dr->dr_icond & DRC_MAJOR)
		fprintf(fp, " major %d", dr->dr_major);

	if (dr->dr_iacts & DRA_BACTS) {
		if (dr->dr_bacts & DRB_HIDE)
			fprintf(fp, " hide");
		if (dr->dr_bacts & DRB_UNHIDE)
			fprintf(fp, " unhide");
	}
	if (dr->dr_iacts & DRA_UID) {
		pw = getpwuid(dr->dr_uid);
		if (pw == NULL)
			fprintf(fp, " user %d", dr->dr_uid);
		else
			fprintf(fp, " user %s", pw->pw_name);
	}
	if (dr->dr_iacts & DRA_GID) {
		gr = getgrgid(dr->dr_gid);
		if (gr == NULL)
			fprintf(fp, " group %d", dr->dr_gid);
		else
			fprintf(fp, " group %s", gr->gr_name);
	}
	if (dr->dr_iacts & DRA_MODE)
		fprintf(fp, " mode %o", dr->dr_mode);
	if (dr->dr_iacts & DRA_INCSET)
		fprintf(fp, " include %d", dr->dr_incset);

	fprintf(fp, "\n");
}
OpenPOWER on IntegriCloud