/
proxy_init.c
424 lines (388 loc) · 11.4 KB
/
proxy_init.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* UFTP - UDP based FTP with multicast
*
* Copyright (C) 2001-2011 Dennis A. Bush, Jr. bush@tcnj.edu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, the copyright holder
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#ifdef WINDOWS
#include <process.h>
#include <ws2tcpip.h>
#include <io.h>
#else // WINDOWS
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include "proxy.h"
#include "proxy_init.h"
#include "proxy_config.h"
#include "proxy_common.h"
static int parent; // is this the parent process that exits after a fork?
/**
* Cleanup routine set up by atexit
*/
void cleanup(void)
{
int i;
for (i = 0; i < MAXLIST; i++) {
if (group_list[i].group_id != 0) {
group_cleanup(i);
}
}
if (!parent) {
for (i = 0; i < pub_multi_count; i++) {
multicast_leave(listener, 0, &pub_multi[i], m_interface,
interface_count, server_fp, server_fp_count);
}
}
closesocket(listener);
for (i = 0; i < key_count; i++) {
free_RSA_key(privkey[i]);
}
crypto_cleanup();
#ifdef WINDOWS
WSACleanup();
#endif
fclose(stderr);
}
/**
* Generic signal handler, exits on signal
*/
void gotsig(int sig)
{
log(0, 0, "Exiting on signal %d", sig);
exit(0);
}
/**
* Signal handler for SIGPIPE
*/
void gotpipe(int sig)
{
log(0, 0, "Got SIGPIPE");
}
/**
* Do initial setup before parsing arguments, including getting interface list
*/
void pre_initialize()
{
#ifdef WINDOWS
struct WSAData data;
if (WSAStartup(2, &data)) {
fprintf(stderr, "Error in WSAStartup: %d\n", WSAGetLastError());
exit(1);
}
#endif
applog = stderr;
ifl_len = sizeof(ifl) / sizeof(struct iflist);
getiflist(ifl, &ifl_len);
}
/**
* Set up log file and run in the backgroud
*/
void daemonize()
{
#ifdef WINDOWS
if (!debug) {
int fd;
FILE *pidfh;
if ((fd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) {
perror("Can't open log file");
exit(1);
}
if (strcmp(pidfile, "")) {
// Write out the pid file, before we redirect STDERR to the log.
if ((pidfh = fopen(pidfile, "w")) == NULL) {
perror("Can't open pid file for writing");
exit(1);
}
fprintf(pidfh, "%d\n", GetCurrentProcessId());
fclose(pidfh);
}
dup2(fd, 2);
close(fd);
applog = stderr;
}
#else // WINDOWS
if (!debug) {
int pid, fd;
FILE *pidfh;
if ((fd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) {
perror("Can't open log file");
exit(1);
}
if ((pid = fork()) == -1) {
perror("Couldn't fork");
exit(1);
} else if (pid > 0) {
parent = 1;
exit(0);
}
if (strcmp(pidfile, "")) {
// Write out the pid file, before we redirect STDERR to the log.
if ((pidfh = fopen(pidfile, "w")) == NULL) {
perror("Can't open pid file for writing");
exit(1);
}
fprintf(pidfh, "%d\n", getpid());
fclose(pidfh);
}
setsid();
dup2(fd, 2);
close(fd);
for (fd = 0; fd < 30; fd++) {
if ((fd != 2) && (fd != listener)) {
close(fd);
}
}
#ifdef VMS
chdir("SYS$LOGIN");
#else
chdir("/");
#endif
umask(0);
applog = stderr;
}
nice(-20);
{
struct sigaction act;
sigfillset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
act.sa_handler = gotsig;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
act.sa_handler = gotpipe;
sigaction(SIGPIPE, &act, NULL);
act.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &act, NULL);
}
#endif // WINDOWS
}
/**
* Initialize crypto library, generate keys
*/
void key_init()
{
#ifndef NO_ENCRYPTION
int i;
crypto_init(sys_keys);
if ((keyfile_count == 0) || (newkeylen != 0)) {
privkey[0] = gen_RSA_key(newkeylen, RSA_EXP, keyfile[0]);
if (!privkey[0]) {
exit(1);
}
key_count = 1;
} else {
for (i = 0; i < keyfile_count; i++) {
privkey[key_count] = read_RSA_key(keyfile[i]);
if (!privkey[key_count]) {
exit(1);
}
key_count++;
}
}
#endif
}
/**
* Do all socket creation and initialization
*/
void create_sockets()
{
struct sockaddr_in sin;
int fdflag, found_if, i;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
if ((listener = socket(AF_INET,SOCK_DGRAM,0)) == INVALID_SOCKET) {
sockerror(0, 0, "Error creating socket for listener");
exit(1);
}
if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) {
sockerror(0, 0, "Error binding socket for listener");
exit(1);
}
#ifndef BLOCKING
#ifdef WINDOWS
fdflag = 1;
if (ioctlsocket(listener, FIONBIO, &fdflag) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting non-blocking option");
closesocket(listener);
exit(1);
}
#else
if ((fdflag = fcntl(listener, F_GETFL)) == SOCKET_ERROR) {
sockerror(0, 0, "Error getting socket descriptor flags");
closesocket(listener);
exit(1);
}
fdflag |= O_NONBLOCK;
if (fcntl(listener, F_SETFL, fdflag) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting non-blocking option");
closesocket(listener);
exit(1);
}
#endif
#endif // BLOCKING
if (setsockopt(listener, IPPROTO_IP, IP_TOS, (char *)&dscp,
sizeof(dscp)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting dscp");
closesocket(listener);
exit(1);
}
if (buffer) {
if (setsockopt(listener, SOL_SOCKET, SO_RCVBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting receive buffer size");
exit(1);
}
if (setsockopt(listener, SOL_SOCKET, SO_SNDBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting send buffer size");
exit(1);
}
} else {
buffer = DEF_RCVBUF;
if (setsockopt(listener, SOL_SOCKET, SO_RCVBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
buffer = DEF_BSD_RCVBUF;
if (setsockopt(listener, SOL_SOCKET, SO_RCVBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting receive buffer size");
exit(1);
}
}
buffer = DEF_RCVBUF;
if (setsockopt(listener, SOL_SOCKET, SO_SNDBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
buffer = DEF_BSD_RCVBUF;
if (setsockopt(listener, SOL_SOCKET, SO_SNDBUF,
(char *)&buffer, sizeof(buffer)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting send buffer size");
exit(1);
}
}
}
if (setsockopt(listener, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(ttl)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting ttl");
closesocket(listener);
exit(1);
}
if (out_addr.s_addr == INADDR_NONE) {
for (i = 0, found_if = 0; (i < ifl_len) && !found_if; i++) {
if (!ifl[i].isloopback) {
found_if = 1;
out_addr.s_addr = ifl[i].addr.s_addr;
}
}
if (!found_if) {
if (ifl_len > 0) {
out_addr.s_addr = ifl[0].addr.s_addr;
} else {
fprintf(stderr, "ERROR: no network interfaces found!\n");
exit(1);
}
}
}
if (setsockopt(listener, IPPROTO_IP, IP_MULTICAST_IF, (char *)&out_addr,
sizeof(out_addr)) == SOCKET_ERROR) {
sockerror(0, 0, "Error setting outgoing interface");
closesocket(listener);
exit(1);
}
for (i = 0; i < pub_multi_count; i++) {
if (!multicast_join(listener, 0, &pub_multi[i], m_interface,
interface_count, server_fp, server_fp_count)) {
exit(1);
}
}
}
/**
* Initialization based on command line args
*/
void initialize()
{
char hostname[256];
struct hostent *hp;
struct in_addr *addr;
int i;
parent = 0;
// Load list of multicast interfaces
if (interface_count == 0) {
for (i = 0; i < ifl_len; i++) {
if (!ifl[i].isloopback) {
m_interface[interface_count++] = ifl[i];
}
}
}
// No non-loopback interfaces, so just use the hostname's interface
if (interface_count == 0) {
gethostname(hostname, sizeof(hostname));
if ((hp = gethostbyname(hostname)) == NULL) {
fprintf(stderr, "Can't get host name\n");
exit(1);
} else {
addr = (struct in_addr *)hp->h_addr_list[0];
m_interface[interface_count].addr = *addr;
m_interface[interface_count++].ismulti = 1;
m_interface[interface_count++].isloopback = 0;
}
}
if (uid) {
m_interface[interface_count].addr.s_addr = uid;
m_interface[interface_count++].ismulti = 0;
m_interface[interface_count++].isloopback = 0;
}
if (proxy_type == CLIENT_PROXY) {
pub_multi_count = 0;
} else if (!pub_multi_count) {
pub_multi[0].s_addr = inet_addr(DEF_PUB_MULTI);
pub_multi_count = 1;
}
for (i = 0; i < MAXLIST; i++) {
group_list[i].group_id = 0;
}
next_hb_time.tv_sec = 0;
next_hb_time.tv_usec = 0;
last_key_req.tv_sec = 0;
last_key_req.tv_usec = 0;
atexit(cleanup);
key_init();
create_sockets();
daemonize();
showtime = 1;
}