1 /* $OpenBSD: unveil-chdir.c,v 1.1 2019/08/04 09:00:17 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 24 int 25 main(int argc, char *argv[]) 26 { 27 char *cwd, *dir, *file, *path = NULL; 28 29 if (argc != 3 && argc != 4) 30 errx(2, "usage: unveil-chdir cwd dir [file]"); 31 32 cwd = 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 (chdir(cwd) == -1) 41 err(1, "chdir %s", cwd); 42 43 if (unveil(dir, "") == -1) 44 err(1, "unveil %s", dir); 45 if (file != NULL) { 46 if (open(path, O_RDONLY) != -1) 47 errx(1, "open %s succeeded", path); 48 if (errno != ENOENT) 49 err(1, "open %s error", path); 50 } 51 52 return 0; 53 } 54