forked from dlundquist/sniproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniproxy.c
290 lines (238 loc) · 7.69 KB
/
sniproxy.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (c) 2011-2014, Dustin Lundquist <dustin@null-ptr.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
#include <errno.h>
#include <ev.h>
#include "binder.h"
#include "config.h"
#include "connection.h"
#include "listener.h"
#include "resolv.h"
#include "logger.h"
static void usage();
static void daemonize(void);
static void write_pidfile(const char *, pid_t);
static void set_limits(rlim_t);
static void drop_perms(const char* username, const char* groupname);
static void perror_exit(const char *);
static void signal_cb(struct ev_loop *, struct ev_signal *, int revents);
static const char *sniproxy_version = PACKAGE_VERSION;
static const char *default_username = "daemon";
static struct Config *config;
static struct ev_signal sighup_watcher;
static struct ev_signal sigusr1_watcher;
static struct ev_signal sigint_watcher;
static struct ev_signal sigterm_watcher;
int
main(int argc, char **argv) {
const char *config_file = "/etc/sniproxy.conf";
int background_flag = 1;
rlim_t max_nofiles = 65536;
int opt;
while ((opt = getopt(argc, argv, "fc:n:V")) != -1) {
switch (opt) {
case 'c':
config_file = optarg;
break;
case 'f': /* foreground */
background_flag = 0;
break;
case 'n':
max_nofiles = strtoul(optarg, NULL, 10);
break;
case 'V':
printf("sniproxy %s\n", sniproxy_version);
return EXIT_SUCCESS;
default:
usage();
return EXIT_FAILURE;
}
}
config = init_config(config_file, EV_DEFAULT);
if (config == NULL) {
fprintf(stderr, "Unable to load %s\n", config_file);
usage();
return EXIT_FAILURE;
}
/* ignore SIGPIPE, or it will kill us */
signal(SIGPIPE, SIG_IGN);
if (background_flag) {
if (config->pidfile != NULL)
remove(config->pidfile);
daemonize();
if (config->pidfile != NULL)
write_pidfile(config->pidfile, getpid());
}
start_binder();
set_limits(max_nofiles);
init_listeners(&config->listeners, &config->tables, EV_DEFAULT);
/* Drop permissions only when we can */
drop_perms(config->user ? config->user : default_username, config->group);
ev_signal_init(&sighup_watcher, signal_cb, SIGHUP);
ev_signal_init(&sigusr1_watcher, signal_cb, SIGUSR1);
ev_signal_init(&sigint_watcher, signal_cb, SIGINT);
ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM);
ev_signal_start(EV_DEFAULT, &sighup_watcher);
ev_signal_start(EV_DEFAULT, &sigusr1_watcher);
ev_signal_start(EV_DEFAULT, &sigint_watcher);
ev_signal_start(EV_DEFAULT, &sigterm_watcher);
resolv_init(EV_DEFAULT, config->resolver.nameservers,
config->resolver.search, config->resolver.mode);
init_connections();
ev_run(EV_DEFAULT, 0);
free_connections(EV_DEFAULT);
resolv_shutdown(EV_DEFAULT);
free_config(config, EV_DEFAULT);
stop_binder();
return 0;
}
static void
daemonize(void) {
#ifdef HAVE_DAEMON
if (daemon(0,0) < 0)
perror_exit("daemon()");
#else
pid_t pid;
/* daemon(0,0) part */
pid = fork();
if (pid < 0)
perror_exit("fork()");
else if (pid != 0)
exit(EXIT_SUCCESS);
if (setsid() < 0)
perror_exit("setsid()");
if (chdir("/") < 0)
perror_exit("chdir()");
if (freopen("/dev/null", "r", stdin) == NULL)
perror_exit("freopen(stdin)");
if (freopen("/dev/null", "a", stdout) == NULL)
perror_exit("freopen(stdout)");
if (freopen("/dev/null", "a", stderr) == NULL)
perror_exit("freopen(stderr)");
pid = fork();
if (pid < 0)
perror_exit("fork()");
else if (pid != 0)
exit(EXIT_SUCCESS);
#endif
/* local part */
umask(022);
signal(SIGHUP, SIG_IGN);
ev_default_fork();
return;
}
/**
* Raise file handle limit to reasonable level
* At some point we should make this a config parameter
*/
static void
set_limits(rlim_t max_nofiles) {
struct rlimit fd_limit = {
.rlim_cur = max_nofiles,
.rlim_max = max_nofiles,
};
int result = setrlimit(RLIMIT_NOFILE, &fd_limit);
if (result < 0)
warn("Failed to set file handle limit: %s", strerror(errno));
}
static void
drop_perms(const char *username, const char *groupname) {
/* check if we are already an unprivileged user */
if (getuid() != 0)
return;
errno = 0;
struct passwd *user = getpwnam(username);
if (errno)
fatal("getpwnam(): %s", strerror(errno));
else if (user == NULL)
fatal("getpwnam(): user %s does not exist", username);
gid_t gid = user->pw_gid;
if (groupname != NULL) {
errno = 0;
struct group *group = getgrnam(groupname);
if (errno)
fatal("getgrnam(): %s", strerror(errno));
else if (group == NULL)
fatal("getgrnam(): group %s does not exist", groupname);
gid = group->gr_gid;
}
/* drop any supplementary groups */
if (setgroups(1, &gid) < 0)
fatal("setgroups(): %s", strerror(errno));
/* set the main gid */
if (setgid(gid) < 0)
fatal("setgid(): %s", strerror(errno));
if (setuid(user->pw_uid) < 0)
fatal("setuid(): %s", strerror(errno));
}
static void
perror_exit(const char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
static void
usage() {
fprintf(stderr, "Usage: sniproxy [-c <config>] [-f] [-n <max file descriptor limit>] [-V]\n");
}
static void
write_pidfile(const char *path, pid_t pid) {
FILE *fp = fopen(path, "w");
if (fp == NULL) {
perror("fopen");
return;
}
fprintf(fp, "%d\n", pid);
fclose(fp);
}
static void
signal_cb(struct ev_loop *loop, struct ev_signal *w, int revents) {
if (revents & EV_SIGNAL) {
switch (w->signum) {
case SIGHUP:
reopen_loggers();
reload_config(config, loop);
break;
case SIGUSR1:
print_connections();
break;
case SIGINT:
case SIGTERM:
ev_unloop(loop, EVUNLOOP_ALL);
}
}
}