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
|
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
#include <assert.h>
#include "options.h"
#include "utils.h"
#include "ptunnel.h"
#include "md5.h"
struct options opts;
enum option_type {
OPT_BOOL, OPT_DEC32, OPT_HEX32, OPT_STR
};
struct option_usage {
const char *short_help;
int required;
enum option_type otype;
union {
int32_t num;
uint32_t unum;
const char *str;
};
const char *long_help;
};
static const struct option_usage usage[] = {
{"magic", 0, OPT_HEX32, {.unum = 0xdeadc0de},
"Set ptunnel magic hexadecimal number. (32-bit unsigned)\n"
"This will be prefixed in all ICMP packets\n"
"and can be used to bypass Cisco IPS\n"
"This value has to be the same on the server and client!\n"
},
{"address:port", 1, OPT_STR, {.unum = 0},
"Set address of peer running packet forwarder. This causes\n"
"ptunnel to operate in forwarding mode - the absence of this\n"
"option causes ptunnel to operate in proxy mode.\n"
},
{"port", 1, OPT_DEC32, {.num = 1234},
"Set TCP listening port (only used when operating in forward mode)\n"
},
{"address:port", 1, OPT_STR, {.str = "127.0.0.1:22"},
"Set remote proxy destination address:port if client\n"
"Restrict to only this destination address:port if server\n"
},
{"connections", 0, OPT_DEC32, {.num = 4},
"Set maximum number of concurrent tunnels\n"
},
{"level", 0, OPT_DEC32, {.num = 1},
"Verbosity level (-1 to 4, where -1 is no output, and 4 is all output)\n"
},
{NULL, 0, OPT_BOOL, {.unum = 0},
"Enable libpcap on the given device.\n"
},
{"file", 0, OPT_STR, {.str = "/var/log/ptunnel.log"},
"Specify a file to log to, rather than printing to standard out.\n"
},
{NULL, 0, OPT_BOOL, {.unum = 0},
"Client only. Enables continuous output of statistics (packet loss, etc.)\n"
},
#ifndef WIN32
{NULL, 0, OPT_BOOL, {.unum = 0},
"Run in background, the PID will be written in the file supplied as argument\n"
},
{NULL, 0, OPT_BOOL, {.unum = 0},
"Output debug to syslog instead of standard out.\n"
},
#endif
{NULL, 0, OPT_BOOL, {.unum = 0},
"Toggle use of UDP instead of ICMP. Proxy will listen on port 53 (must be root).\n"
},
{"password", 0, OPT_STR, {.str = NULL},
"Set password (must be same on client and proxy)\n"
"If no password is set, you will be asked during runtime.\n"
},
{NULL, 0, OPT_BOOL, {.unum = 0},
"Run proxy in unprivileged mode. This causes the proxy to forward\n"
"packets using standard echo requests, instead of crafting custom echo replies.\n"
"Unprivileged mode will only work on some systems, and is in general less reliable\n"
"than running in privileged mode.\n"
},
#ifndef WIN32
{"user", 0, OPT_STR, {.str = "nobody"},
"When started in privileged mode, drop down to user's rights as soon as possible\n"
},
{"group", 0, OPT_STR, {.str = "nogroup"},
"When started in privileged mode, drop down to group's rights as soon as possible\n"
},
{"directory", 0, OPT_STR, {.str = "/var/lib/ptunnel"},
"When started in privileged mode, restrict file access to the specified directory\n"
},
#endif
#ifdef HAVE_SELINUX
{NULL, 0, OPT_BOOL, {.unum = 0},
"Set SELinux context when all there is left to do are network I/O operations\n"
"To combine with -chroot you will have to `mount --bind /proc /chrootdir/proc`\n"
},
#endif
{"help", 0, OPT_STR, {.str = NULL}, "this\n"},
{NULL,0,OPT_BOOL,{.unum=0},NULL}
};
static struct option long_options[] = {
{"magic", required_argument, 0, 'm'},
{"proxy", required_argument, 0, 'p'},
{"listen", required_argument, 0, 'l'},
{"remote", required_argument, 0, 'r'},
{"connections", required_argument, 0, 'c'},
{"verbosity", required_argument, 0, 'v'},
{"libpcap", required_argument, 0, 'a'},
{"logfile", required_argument, 0, 'o'},
{"statistics", no_argument, 0, 's'},
#ifndef WIN32
{"daemon", no_argument, 0, 'd'},
{"syslog", no_argument, 0, 'S'},
#endif
{"udp", no_argument, &opts.udp, 1 },
{"passwd", required_argument, 0, 'x'},
{"unprivileged", no_argument, &opts.unprivledged, 1 },
#ifndef WIN32
{"user", required_argument, 0, 'u'},
{"group", required_argument, 0, 'g'},
{"chroot", required_argument, 0, 't'},
#endif
#ifdef HAVE_SELINUX
{"setcon", no_argument, 0, 'e'},
#endif
{"help", no_argument, 0, 'h'},
{NULL,0,0,0}
};
static void print_multiline(const char *prefix, const char *multiline) {
const char sep[] = "\n";
const char *start, *end;
start = multiline;
do {
if (start) {
end = strstr(start, sep);
if (end) {
printf("%s%.*s\n", prefix, (int)(end-start), start);
start = end + strlen(sep);
}
}
} while (start && end);
}
static void print_long_help(unsigned index, int required_state) {
const char spaces[] = " ";
if (usage[index].required != required_state)
return;
if (!long_options[index].name)
return;
if (isalpha(long_options[index].val)) {
printf("%.*s-%c --%s\n", 4, spaces, long_options[index].val, long_options[index].name);
} else {
printf("%.*s--%s\n", 4, spaces, long_options[index].name);
}
if (usage[index].long_help) {
print_multiline(&spaces[4], usage[index].long_help);
}
switch (usage[index].otype) {
case OPT_BOOL:
break;
case OPT_DEC32:
printf("%s(default: %d)\n", spaces, usage[index].num);
break;
case OPT_HEX32:
printf("%s(default: 0x%X)\n", spaces, usage[index].unum);
break;
case OPT_STR:
if (usage[index].str)
printf("%s(default: %s)\n", spaces, usage[index].str);
break;
}
}
static void print_short_help(unsigned index, int required_state) {
const char *ob = (required_state == 0 ? "[" : "");
const char *cb = (required_state == 0 ? "]" : "");
if (usage[index].required != required_state)
return;
if (!long_options[index].name)
return;
if (!usage[index].short_help && isalpha(long_options[index].val)) {
printf(" %s-%c%s", ob, long_options[index].val, cb);
}
else if (!usage[index].short_help) {
printf(" %s--%s%s", ob, long_options[index].name, cb);
}
else if (isalpha(long_options[index].val)) {
printf(" %s-%c <%s>%s", ob, long_options[index].val, usage[index].short_help, cb);
}
else {
printf(" %s--%s <%s>%s", ob, long_options[index].name, usage[index].short_help, cb);
}
}
void print_usage(const char *arg0) {
unsigned i;
assert( ARRAY_SIZE(long_options) == ARRAY_SIZE(usage) );
printf("ptunnel-ng v%d.%.2d\n\nUsage: %s", kMajor_version, kMinor_version, arg0);
/* print (short)help argument line */
for (i = 0; i < ARRAY_SIZE(usage); ++i) {
print_short_help(i, 1);
}
for (i = 0; i < ARRAY_SIZE(usage); ++i) {
print_short_help(i, 0);
}
printf("%s", "\n\n");
/* print (long)help lines */
for (i = 0; i < ARRAY_SIZE(usage); ++i) {
print_long_help(i, 1);
}
for (i = 0; i < ARRAY_SIZE(usage); ++i) {
print_long_help(i, 0);
}
}
int parse_options(int argc, char **argv) {
int c = 0, optind = -1;
struct hostent *host_ent;
md5_state_t state;
#ifndef WIN32
struct passwd *pwnam;
struct group *grnam;
#endif
while (1) {
c = getopt_long(argc, argv, "m:p:l:r:c:v:a:o:sdSx:u:g:t:eh", &long_options[0], &optind);
if (c == -1) break;
switch (c) {
case 'm':
opts.magic = strtoul(optarg, NULL, 16);
break;
case 'p':
if (NULL == (host_ent = gethostbyname(optarg))) {
pt_log(kLog_error, "Failed to look up %s as proxy address\n", optarg);
return 1;
}
opts.given_proxy_ip = *(uint32_t*)host_ent->h_addr_list[0];
break;
case 'l':
opts.tcp_listen_port = strtoul(optarg, NULL, 10);
break;
case 'r':
if (NULL == (host_ent = gethostbyname(optarg))) {
pt_log(kLog_error, "Failed to look up %s as destination address\n", optarg);
return 1;
}
opts.given_dst_ip = *(uint32_t*)host_ent->h_addr_list[0];
break;
case 'c':
opts.max_tunnels = strtoul(optarg, NULL,10);
if (opts.max_tunnels > kMax_tunnels)
opts.max_tunnels = kMax_tunnels;
break;
case 'v':
opts.log_level = strtoul(optarg, NULL, 10);
break;
case 'a':
opts.pcap_device = strdup(optarg);
break;
case 'o':
opts.log_file = fopen(optarg, "a");
if (!opts.log_file) {
opts.log_file = stdout;
pt_log(kLog_error, "Failed to open log file: '%s'. Cause: % s\n", optarg, strerror(errno));
pt_log(kLog_error, "Reverting log to standard out.\n");
}
break;
case 's':
opts.print_stats = !opts.print_stats;
break;
case 'x':
opts.password_digest = (unsigned char *)calloc(MD5_LEN, sizeof(unsigned char));
pt_log(kLog_debug, "Password set - unauthenicated connections will be refused.\n");
// Compute the password digest
md5_init(&state);
md5_append(&state, (md5_byte_t*)optarg, strlen(optarg));
md5_finish(&state, opts.password_digest);
// Hide the password in process listing
memset(optarg, '*', strlen(optarg));
break;
#ifndef WIN32
case 'd':
opts.daemonize = true;
if (NULL == (opts.pid_file = fopen(optarg, "w")))
pt_log(kLog_error, "%s: %s\n", optarg, strerror(errno));
break;
case 'S':
opts.syslog = 1;
break;
case 'u':
errno = 0;
if (NULL == (pwnam = getpwnam(optarg))) {
pt_log(kLog_error, "%s: %s\n", optarg, errno ? strerror(errno) : "unknown user");
exit(1);
}
opts.uid = pwnam->pw_uid;
if (!opts.gid)
opts.gid = pwnam->pw_gid;
break;
case 'g':
errno = 0;
if (NULL == (grnam = getgrnam(optarg))) {
pt_log(kLog_error, "%s: %s\n", optarg, errno ? strerror(errno) : "unknown group");
exit(1);
}
opts.gid = grnam->gr_gid;
break;
case 't':
opts.root_dir = strdup(optarg);
break;
#else
case 'd':
case 'S':
case 'U':
case 'g':
case 't':
pt_log(kLog_error, "%s: feature not supported", optarg);
exit(1);
#endif
case 'e':
#ifdef HAVE_SELINUX
opts.selinux_context = strdup(optarg);
break;
#else
pt_log(kLog_error, "%s: feature not supported", optarg);
exit(1);
#endif
case 'h':
print_usage(argv[0]);
_exit(EXIT_SUCCESS);
default:
printf("Opt ERROR\n");
break;
}
}
return 0;
}
|