1/*	$OpenBSD: unveil-perm.c,v 1.1.1.1 2019/08/01 15:20:51 bluhm Exp $	*/
2/*
3 * Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <err.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <unistd.h>
23
24int
25main(int argc, char *argv[])
26{
27	char *perm, *dir, *file, *path = NULL;
28
29	if (argc != 3 && argc != 4)
30		errx(2, "usage: unveil-perm perm dir [file]");
31
32	perm = argv[1];
33	dir = argv[2];
34	file = argv[3];
35	if (file != NULL) {
36		if (asprintf(&path, "%s/%s", dir, file) == -1)
37			err(1, "asprintf");
38	}
39
40	if (unveil(dir, perm) == -1)
41		err(1, "unveil %s %s", dir, perm);
42	if (file != NULL) {
43		if (open(path, O_RDONLY) != -1)
44			errx(1, "open %s succeeded", path);
45		if (perm == NULL) {
46			if (errno != ENOENT)
47				err(1, "open %s error", path);
48		}
49	}
50
51	return 0;
52}
53