Add basic pledge/unveil sandbox on OpenBSD
[tinc] / src / bsd / openbsd / sandbox.c
1 #include "../../system.h"
2
3 #include "sandbox.h"
4 #include "../../logger.h"
5
6 void allow_path(const char *path, const char *priv) {
7         if(path) {
8                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Allowing path %s with %s", path, priv);
9
10                 if(unveil(path, priv)) {
11                         logger(DEBUG_ALWAYS, LOG_ERR, "unveil(%s, %s) failed: %s", path, priv, strerror(errno));
12                 }
13         }
14 }
15
16 void allow_paths(const unveil_path_t paths[]) {
17         // Since some path variables may contain NULL, we check priv here.
18         // If a NULL path is seen, just skip it.
19         for(const unveil_path_t *p = paths; p->priv; ++p) {
20                 allow_path(p->path, p->priv);
21         }
22 }
23
24 bool restrict_privs(const char *promises, const char *execpromises) {
25         if(pledge(promises, execpromises)) {
26                 logger(DEBUG_ALWAYS, LOG_ERR, "pledge(%s, %s) failed: %s", promises, execpromises, strerror(errno));
27                 return false;
28         } else {
29                 return true;
30         }
31 }