-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
freebsd_priv.c
49 lines (41 loc) · 1.35 KB
/
freebsd_priv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <sys/capsicum.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "memcached.h"
/*
* dropping privileges is entering in capability mode
* in FreeBSD vocabulary.
*/
void drop_privileges() {
cap_rights_t wd, rd;
if (cap_rights_init(&wd, CAP_WRITE, CAP_READ) == NULL) {
fprintf(stderr, "cap_rights_init write protection failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (cap_rights_init(&rd, CAP_FCNTL, CAP_READ, CAP_EVENT) == NULL) {
fprintf(stderr, "cap_rights_init read protection failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (cap_rights_limit(STDIN_FILENO, &rd) != 0) {
fprintf(stderr, "cap_rights_limit stdin failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (cap_rights_limit(STDOUT_FILENO, &wd) != 0) {
fprintf(stderr, "cap_rights_limit stdout failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (cap_rights_limit(STDERR_FILENO, &wd) != 0) {
fprintf(stderr, "cap_rights_limit stderr failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (cap_enter() != 0) {
fprintf(stderr, "cap_enter failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
void setup_privilege_violations_handler(void) {
// not needed
}