~sircmpwn/deacme unlisted

ref: refs/heads/master deacme/util-portable.c -rw-r--r-- 4.0 KiB
9e8c8d64Drew DeVault Add better logging in dnsproc write* fail 2 years ago
                                                                                
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*	$Id$ */
/*
 * Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "extern.h"
#include "util-portable.h"

// Whose dumb fucking idea was it to remove chroot from POSIX
int chroot(const char *path);

static uid_t uid;
static gid_t gid;

int dropfs(const char *path) {
	if (-1 == chroot(path)) {
		warn("%s: chroot", path);
	} else if (-1 == chdir("/")) {
		warn("/: chdir");
	} else {
		return 1;
	}
	return 0;
}

int checkprivs(void) {
	struct passwd *passent;

	/* We need root for our chroots. */
	if (0 != getuid()) {
		return 0;
	}

	/* We need this for our privdropping. */
	passent = getpwnam(NOBODY_USER);
	if (NULL == passent) {
		warnx("%s: unknown user", NOBODY_USER);
		return 0;
	}

	uid = passent->pw_uid;
	gid = passent->pw_gid;
	return 1;
}

int dropprivs(void) {
	/*
	 * Safely drop privileges into the given credentials.
	 */
	if (setregid(gid, gid) || setreuid(uid, uid)) {
		warnx("drop privileges");
		return 0;
	}

	if (getgid() != gid || getegid() != gid) {
		warnx("failed to drop gid");
		return 0;
	}
	if (getuid() != uid || geteuid() != uid) {
		warnx("failed to drop uid");
		return 0;
	}

	return 1;
}

#ifdef __linux__
#include <sys/random.h>
void arc4random_buf(void *buf, size_t nbytes) {
	getrandom(buf, nbytes, 0);
}
#endif

/* Some of these are adapted from musl libc. */

int vasprintf(char **strp, const char *fmt, va_list ap) {
	va_list ap2;
	va_copy(ap2, ap);
	int n = vsnprintf(NULL, 0, fmt, ap2);
	if (n < 0 || !(*strp = malloc(n + 1))) {
		return -1;
	}
	return vsnprintf(*strp, n, fmt, ap);
}

int asprintf(char **strp, const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	int n = vasprintf(strp, fmt, ap);
	va_end(ap);
	return n;
}

size_t strlcat(char *d, const char *s, size_t n) {
	size_t l = strnlen(d, n);
	if (l == n) {
		return l + strlen(s);
	}
	return l + strlcpy(d + l, s, n - l);
}

size_t strlcpy(char *d, const char *s, size_t n) {
	char *d0 = d;

	if (!n--) {
		goto finish;
	}
	for (; n && (*d = *s); n--, s++, d++);
	*d = 0;
finish:
	return d - d0 + strlen(s);
}

char *strsep(char **str, const char *sep) {
	char *s = *str, *end;
	if (!s) {
		return NULL;
	}
	end = s + strcspn(s, sep);
	if (*end) {
		*end++ = 0;
	} else {
		end = 0;
	}
	*str = end;
	return s;
}


void vwarn(const char *fmt, va_list ap) {
	if (fmt) {
		vfprintf(stderr, fmt, ap);
		fputs (": ", stderr);
	}
	fprintf(stderr, "%s\n", strerror(errno));
}

void vwarnx(const char *fmt, va_list ap) {
	if (fmt) {
		vfprintf(stderr, fmt, ap);
	}
	putc('\n', stderr);
}

void verr(int status, const char *fmt, va_list ap) {
	vwarn(fmt, ap);
	exit(status);
}

void verrx(int status, const char *fmt, va_list ap) {
	vwarnx(fmt, ap);
	exit(status);
}

void warn(const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	vwarn(fmt, ap);
	va_end(ap);
}

void warnx(const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	vwarnx(fmt, ap);
	va_end(ap);
}

void err(int status, const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	verr(status, fmt, ap);
	va_end(ap);
}

void errx(int status, const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	verrx(status, fmt, ap);
	va_end(ap);
}