1 /* $OpenBSD: unveil-unlink.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 <fcntl.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 int
main(int argc,char * argv[])24 main(int argc, char *argv[])
25 {
26 char *dir, *file, *path;
27 int fd;
28
29 if (argc != 3)
30 errx(2, "usage: unveil-unlink dir file");
31
32 dir = argv[1];
33 file = argv[2];
34 if (asprintf(&path, "%s/%s", dir, file) == -1)
35 err(1, "asprintf");
36
37 fd = open(path, O_WRONLY|O_CREAT, 0755);
38 if (fd == -1)
39 err(1, "open %s", dir);
40 close(fd);
41
42 if (unveil(dir, "r") == -1)
43 err(1, "unveil %s", dir);
44 if (unveil(NULL, NULL) == -1)
45 err(1, "unveil NULL");
46 if (unlink(path) == 0)
47 errx(1, "unlink %s succeeded", path);
48
49 return 0;
50 }
51