$OpenBSD$

Prevent crash after reload with ocsp-dir change:
https://github.com/varnish/hitch/pull/265

Pledge oscp and worker processes.
chroot(2) is not (yet) hoisted so pledge main process only iff not used.

Index: src/hitch.c
--- src/hitch.c.orig
+++ src/hitch.c
@@ -2979,6 +2979,11 @@ start_workers(int start_index, int count)
 				change_root();
 			if (CONFIG->UID >= 0 || CONFIG->GID >= 0)
 				drop_privileges();
+			/* drop wpath cpath flock getpw proc id */
+			if (pledge("stdio rpath inet dns", NULL) == -1) {
+				ERR("pledge\n");
+				_exit(1);
+			}
 			if (geteuid() == 0) {
 				ERR("{core} ERROR: "
 				    "Refusing to run workers as root.\n");
@@ -3004,6 +3009,11 @@ start_ocsp_proc(void)
 	} else if (ocsp_proc_pid == 0) {
 		if (CONFIG->UID >= 0 || CONFIG->GID >= 0)
 			drop_privileges();
+		/* drop flock getpw proc id */
+		if (pledge("stdio rpath wpath cpath inet dns", NULL) == -1) {
+			ERR("pledge\n");
+			_exit(1);
+		}
 		if (geteuid() == 0) {
 			ERR("{core} ERROR: "
 			    "Refusing to run workers as root.\n");
@@ -3680,7 +3690,8 @@ reconfigure(int argc, char **argv)
 	notify_workers(&wu);
 
 	if (CONFIG->OCSP_DIR != NULL) {
-		(void) kill(ocsp_proc_pid, SIGTERM);
+		if (ocsp_proc_pid != 0)
+			(void) kill(ocsp_proc_pid, SIGTERM);
 		/*
 		 * Restarting the OCSP process is taken
 		 * care of in do_wait
@@ -3735,6 +3746,35 @@ main(int argc, char **argv)
 		return (rv);
 	}
 
+	/* chroot(2) must be hoisted */
+	if (!CONFIG->CHROOT) {
+		/* needs wpath id */
+		if (CONFIG->UID >= 0 || CONFIG->GID >= 0) {
+			/* needs wpath cpath */
+			if (CONFIG->OCSP_DIR) {
+				if (pledge("stdio rpath wpath cpath inet flock"
+				    " dns getpw proc id", NULL) == -1)
+					fail("pledge\n");
+			} else {
+				if (pledge("stdio rpath wpath inet flock"
+				    " dns getpw proc id", NULL) == -1)
+					fail("pledge\n");
+			}
+		} else {
+			/* needs wpath cpath */
+			if (CONFIG->OCSP_DIR) {
+				if (pledge("stdio rpath wpath cpath inet flock"
+				    " dns getpw proc", NULL) == -1)
+					fail("pledge\n");
+			} else {
+				if (pledge("stdio rpath inet flock"
+				    " dns getpw proc", NULL) == -1)
+					fail("pledge\n");
+			}
+		
+		}
+	}
+
 	if (CONFIG->TEST) {
 		fprintf(stderr, "Trying to initialize SSL contexts with your"
 		    " certificates\n");
@@ -3821,6 +3861,10 @@ main(int argc, char **argv)
 			    CONFIG->PIDFILE, strerror(errno));
 			exit(1);
 		}
+		/* drop flock after VPF_Open() */
+		if (pledge("stdio rpath wpath cpath inet dns getpw proc id",
+		    NULL) == -1)
+			fail("pledge\n");
 
 		AZ(VPF_Write(pfh));
 		atexit(remove_pfh);
@@ -3830,6 +3874,11 @@ main(int argc, char **argv)
 
 	if (CONFIG->OCSP_DIR != NULL)
 		start_ocsp_proc();
+	else {
+		/* drop wpath cpath after VPF_Write() and start_ocsp_proc() */
+		if (pledge("stdio rpath inet dns getpw proc", NULL) == -1)
+			fail("pledge\n");
+	}
 
 #ifdef USE_SHARED_CACHE
 	if (CONFIG->SHCUPD_PORT) {
