forked from hugsy/proxenet
/
main.c
365 lines (304 loc) · 8.35 KB
/
main.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
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include "core.h"
#include "utils.h"
#include "main.h"
#include "socket.h"
/**
*
*/
void version(bool end)
{
printf("%s v%s\n", PROGNAME, VERSION);
if (end) {
proxenet_free_config();
exit(0);
}
}
/**
*
*/
void usage(int retcode)
{
FILE* fd;
fd = (retcode == 0) ? stdout : stderr;
fprintf(fd,
"\n"RED"SYNTAX:"NOCOLOR"\n"
"\t%s "GREEN"[OPTIONS+]"NOCOLOR"\n"
"\n"RED"OPTIONS:"NOCOLOR"\n"
"\t-t, --nb-threads=N\t\t\tNumber of threads (default: %d)\n"
"\t-b, --lbind=bindaddr\t\t\tBind local address (default: %s)\n"
"\t-p, --lport=N\t\t\t\tBind local port file (default: %s)\n"
"\t-l, --logfile=/path/to/logfile\t\tLog actions in file\n"
"\t-x, --plugins=/path/to/plugins/dir\tSpecify plugins directory (default: %s)\n"
"\t-X, --proxy-host=proxyhost\t\tForward to proxy\n"
"\t-P --proxy-port=proxyport\t\tSpecify port for proxy (default: %s)\n"
"\t-k, --key=/path/to/ssl.key\t\tSpecify SSL key to use (default: %s)\n"
"\t-c, --cert=/path/to/ssl.crt\t\tSpecify SSL cert to use (default: %s)\n"
"\t-v, --verbose\t\t\t\tIncrease verbosity (default: 0)\n"
"\t-n, --no-color\t\t\t\tDisable colored output\n"
"\t-4, \t\t\t\t\tIPv4 only (default)\n"
"\t-6, \t\t\t\t\tIPv6 only (default: IPv4)\n"
"\t-h, --help\t\t\t\tShow help\n"
"\t-V, --version\t\t\t\tShow version\n"
,
PROGNAME,
CFG_DEFAULT_NB_THREAD,
CFG_DEFAULT_BIND_ADDR,
CFG_DEFAULT_PORT,
CFG_DEFAULT_PLUGINS_PATH,
CFG_DEFAULT_PROXY_PORT,
CFG_DEFAULT_SSL_KEYFILE,
CFG_DEFAULT_SSL_CERTFILE);
exit(retcode);
}
/**
*
*/
void help(char* argv0)
{
const char* plugin_name;
const char* plugin_ext;
int i;
version(false);
printf("Written by %s\n"
"Released under: %s\n\n"
"Compiled with support for :\n",
AUTHOR, LICENSE);
i = 0;
while (true) {
plugin_name = supported_plugins_str[i];
plugin_ext = plugins_extensions_str[i];
if (!plugin_name || !plugin_ext)
break;
printf("\t[+] 0x%.2x "GREEN"%-10s"NOCOLOR" (%s)\n", i, plugin_name, plugin_ext);
i++;
}
if (i==0)
printf("\t[-] "RED"No support for plugin"NOCOLOR"\n");
#ifdef DEBUG
printf("\t[+] "BLUE"Proxenet DEBUG symbols"NOCOLOR"\n");
#endif
#ifdef DEBUG_SSL
printf("\t[+] "BLUE"PolarSSL DEBUG symbols"NOCOLOR"\n");
#endif
usage(EXIT_SUCCESS);
}
/**
* Checks command line arguments once and for all. Handles (de)allocation for config_t structure
*
* @return 0 if successfully parsed and allocated
* @return -1
*/
int parse_options (int argc, char** argv)
{
int curopt, curopt_idx;
char *logfile, *path, *keyfile, *certfile;
char *proxy_host, *proxy_port;
proxy_host = proxy_port = NULL;
const struct option long_opts[] = {
{ "help", 0, 0, 'h' },
{ "verbose", 0, 0, 'v' },
{ "nb-threads", 1, 0, 't' },
{ "lbind", 1, 0, 'b' },
{ "lport", 1, 0, 'p' },
{ "logfile", 1, 0, 'l' },
{ "certfile", 1, 0, 'c' },
{ "keyfile", 1, 0, 'k' },
{ "plugins", 1, 0, 'x' },
{ "proxy-host", 1, 0, 'X' },
{ "proxy-port", 1, 0, 'P' },
{ "no-color", 0, 0, 'n' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
};
cfg->iface = CFG_DEFAULT_BIND_ADDR;
cfg->port = CFG_DEFAULT_PORT;
cfg->logfile_fd = CFG_DEFAULT_OUTPUT;
cfg->nb_threads = CFG_DEFAULT_NB_THREAD;
cfg->use_color = true;
cfg->ip_version = CFG_DEFAULT_IP_VERSION;
cfg->try_exit = 0;
cfg->try_exit_max = CFG_DEFAULT_TRY_EXIT_MAX;
path = CFG_DEFAULT_PLUGINS_PATH;
keyfile = CFG_DEFAULT_SSL_KEYFILE;
certfile = CFG_DEFAULT_SSL_CERTFILE;
logfile = NULL;
/* parse command line arguments */
while (1) {
curopt = -1;
curopt_idx = 0;
curopt = getopt_long (argc,argv,"n46Vhvb:p:l:t:c:k:x:X:P:",long_opts, &curopt_idx);
if (curopt == -1) break;
switch (curopt) {
case 'v': cfg->verbose++; break;
case 'b': cfg->iface = optarg; break;
case 'p': cfg->port = optarg; break;
case 'l': logfile = optarg; break;
case 't': cfg->nb_threads = (unsigned short)atoi(optarg); break;
case 'X':
proxy_host = optarg;
proxy_port = CFG_DEFAULT_PROXY_PORT;
break;
case 'P': proxy_port = optarg; break;
case 'c': certfile = optarg; break;
case 'k': keyfile = optarg; break;
case 'h': help(argv[0]); break;
case 'V': version(true); break;
case 'n': cfg->use_color = false; break;
case '4': cfg->ip_version = AF_INET; break;
case '6': cfg->ip_version = AF_INET6; break;
case 'x': path = optarg; break;
case '?':
default:
usage (EXIT_FAILURE);
}
curopt_idx = 0;
}
/* validate command line arguments */
/* logfile validation : if a logfile is given, use its FILE* for output */
if (logfile) {
cfg->logfile = realpath(logfile, NULL);
if (cfg->logfile == NULL){
xlog(LOG_CRITICAL, "realpath(logfile) failed: %s\n", strerror(errno));
return -1;
}
if(is_readable_file(cfg->logfile)) {
cfg->logfile_fd = fopen(cfg->logfile, "w");
if(!cfg->logfile_fd) {
cfg->logfile_fd = stderr;
xlog(LOG_CRITICAL, "[-] Failed to open '%s': %s\n", cfg->logfile, strerror(errno));
return -1;
}
}
}
/* check if nb of threads is in boundaries */
if (cfg->nb_threads > MAX_THREADS) {
fprintf(stderr, "Too many threads. Setting to default.\n");
cfg->nb_threads = CFG_DEFAULT_NB_THREAD;
}
/* check plugins path */
cfg->plugins_path = realpath(path, NULL);
if (cfg->plugins_path == NULL){
xlog(LOG_CRITICAL, "realpath(plugins_path) failed: %s\n", strerror(errno));
return -1;
}
if (!is_valid_path(cfg->plugins_path)) {
xlog(LOG_CRITICAL, "%s\n", "Invalid plugins path provided");
return -1;
}
/* check ssl certificate */
cfg->certfile = realpath(certfile, NULL);
if (cfg->certfile == NULL){
xlog(LOG_CRITICAL, "realpath(certfile) failed: %s\n", strerror(errno));
return -1;
}
if ( !is_readable_file(cfg->certfile) ) {
xlog(LOG_CRITICAL, "Failed to read certificate '%s'\n", cfg->certfile);
return -1;
}
/* check ssl key */
cfg->keyfile = realpath(keyfile, NULL);
if (cfg->certfile == NULL){
xlog(LOG_CRITICAL, "realpath(certfile) failed: %s\n", strerror(errno));
return -1;
}
if ( !is_readable_file(cfg->keyfile) ){
xlog(LOG_CRITICAL, "Failed to read private key '%s'\n", cfg->keyfile);
return -1;
}
/* check proxy values (if any) */
if ( proxy_port && !proxy_host) {
xlog(LOG_CRITICAL, "%s\n", "Cannot use proxy-port without proxy-host");
return -1;
}
if (proxy_host) {
cfg->proxy.host = strdup(proxy_host);
if (!cfg->proxy.host) {
xlog(LOG_CRITICAL, "proxy %s\n", strerror(errno));
return -1;
}
cfg->proxy.port = strdup(proxy_port);
if (!cfg->proxy.port) {
xlog(LOG_CRITICAL, "proxy_port %s\n", strerror(errno));
return -1;
}
}
return 0;
}
/**
* initialize config and allocate buffers
*
* @return -1 if an error occured
* @return 0 on success
*/
int proxenet_init_config(int argc, char** argv)
{
cfg = ¤t_config;
proxenet_xzero(cfg, sizeof(conf_t));
if (parse_options(argc, argv) < 0) {
xlog(LOG_CRITICAL, "%s\n", "Failed to parse arguments");
proxenet_free_config();
proxenet_xzero(cfg, sizeof(conf_t));
return -1;
}
return 0;
}
/**
* deallocate all data related to cfg
*/
void proxenet_free_config()
{
/* those calls should be safe */
if (cfg->logfile)
proxenet_xfree(cfg->logfile);
if (cfg->plugins_path)
proxenet_xfree(cfg->plugins_path);
if (cfg->certfile)
proxenet_xfree(cfg->certfile);
if (cfg->keyfile)
proxenet_xfree(cfg->keyfile);
if(cfg->logfile_fd)
fclose(cfg->logfile_fd);
if (cfg->proxy.host) {
proxenet_xfree(cfg->proxy.host);
proxenet_xfree(cfg->proxy.port);
}
}
/**
*
*/
int main (int argc, char **argv, char** envp)
{
int retcode = -1;
/* init semaphore for unified display */
sem_init(&tty_semaphore, 0, 1);
/* get configuration */
retcode = proxenet_init_config(argc, argv);
if (retcode<0)
return EXIT_FAILURE;
#ifdef _PERL_PLUGIN
/* perform plugin pre-initialisation -- currently done only for Perl */
proxenet_init_once_plugins(argc, argv, envp);
#endif
/* proxenet starts here */
retcode = proxenet_start();
#ifdef _PERL_PLUGIN
/* perform plugin post-deletion */
proxenet_delete_once_plugins();
#endif
/* proxenet ends here */
proxenet_free_config();
return (retcode == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}