git ssb

0+

cel / sslh



Tree: 63a83cf0416ab89720b15a4a649786812b630b79

Files: 63a83cf0416ab89720b15a4a649786812b630b79 / sslh-main.c

19836 bytesRaw
1/*
2# main: processing of config file, command line options and start the main
3# loop.
4#
5# Copyright (C) 2007-2016 Yves Rutschle
6#
7# This program is free software; you can redistribute it
8# and/or modify it under the terms of the GNU General Public
9# License as published by the Free Software Foundation; either
10# version 2 of the License, or (at your option) any later
11# version.
12#
13# This program is distributed in the hope that it will be
14# useful, but WITHOUT ANY WARRANTY; without even the implied
15# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16# PURPOSE. See the GNU General Public License for more
17# details.
18#
19# The full text for the General Public License is here:
20# http://www.gnu.org/licenses/gpl.html
21
22*/
23
24#define _GNU_SOURCE
25#ifdef LIBCONFIG
26#include <libconfig.h>
27#endif
28#ifdef ENABLE_REGEX
29#ifdef LIBPCRE
30#include <pcreposix.h>
31#else
32#include <regex.h>
33#endif
34#endif
35
36#include "common.h"
37#include "probe.h"
38
39const char* USAGE_STRING =
40"sslh " VERSION "\n" \
41"usage:\n" \
42"\tsslh [-v] [-i] [-V] [-f] [-n] [--transparent] [-F <file>]\n"
43"\t[-t <timeout>] [-P <pidfile>] -u <username> -p <add> [-p <addr> ...] \n" \
44"%s\n\n" /* Dynamically built list of builtin protocols */ \
45"\t[--on-timeout <addr>]\n" \
46"-v: verbose\n" \
47"-V: version\n" \
48"-f: foreground\n" \
49"-n: numeric output\n" \
50"-u: specify under which user to run\n" \
51"--transparent: behave as a transparent proxy\n" \
52"-F: use configuration file\n" \
53"--on-timeout: connect to specified address upon timeout (default: ssh address)\n" \
54"-t: seconds to wait before connecting to --on-timeout address.\n" \
55"-p: address and port to listen on.\n Can be used several times to bind to several addresses.\n" \
56"--[ssh,ssl,...]: where to connect connections from corresponding protocol.\n" \
57"-P: PID file.\n" \
58"-i: Run as a inetd service.\n" \
59"";
60
61/* Constants for options that have no one-character shorthand */
62#define OPT_ONTIMEOUT 257
63
64static struct option const_options[] = {
65 { "inetd", no_argument, &inetd, 1 },
66 { "foreground", no_argument, &foreground, 1 },
67 { "background", no_argument, &background, 1 },
68 { "transparent", no_argument, &transparent, 1 },
69 { "numeric", no_argument, &numeric, 1 },
70 { "verbose", no_argument, &verbose, 1 },
71 { "user", required_argument, 0, 'u' },
72 { "config", optional_argument, 0, 'F' },
73 { "pidfile", required_argument, 0, 'P' },
74 { "timeout", required_argument, 0, 't' },
75 { "on-timeout", required_argument, 0, OPT_ONTIMEOUT },
76 { "listen", required_argument, 0, 'p' },
77 {}
78};
79static struct option* all_options;
80static struct proto* builtins;
81static const char *optstr = "vt:T:p:VP:F::";
82
83
84
85static void print_usage(void)
86{
87 struct proto *p;
88 int i;
89 int res;
90 char *prots = "";
91
92 p = get_builtins();
93 for (i = 0; i < get_num_builtins(); i++) {
94 res = asprintf(&prots, "%s\t[--%s <addr>]\n", prots, p[i].description);
95 CHECK_RES_DIE(res, "asprintf");
96 }
97
98 fprintf(stderr, USAGE_STRING, prots);
99}
100
101static void printcaps(void) {
102#ifdef LIBCAP
103 cap_t caps;
104 char* desc;
105 ssize_t len;
106
107 caps = cap_get_proc();
108
109 desc = cap_to_text(caps, &len);
110
111 fprintf(stderr, "capabilities: %s\n", desc);
112
113 cap_free(caps);
114 cap_free(desc);
115#endif
116}
117
118static void printsettings(void)
119{
120 char buf[NI_MAXHOST];
121 struct addrinfo *a;
122 struct proto *p;
123
124 for (p = get_first_protocol(); p; p = p->next) {
125 fprintf(stderr,
126 "%s addr: %s. libwrap service: %s log_level: %d family %d %d [%s]\n",
127 p->description,
128 sprintaddr(buf, sizeof(buf), p->saddr),
129 p->service,
130 p->log_level,
131 p->saddr->ai_family,
132 p->saddr->ai_addr->sa_family,
133 p->keepalive ? "keepalive" : "");
134 }
135 fprintf(stderr, "listening on:\n");
136 for (a = addr_listen; a; a = a->ai_next) {
137 fprintf(stderr,
138 "\t%s\t[%s]\n",
139 sprintaddr(buf, sizeof(buf), a),
140 a->ai_flags & SO_KEEPALIVE ? "keepalive" : "");
141 }
142 fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout,
143 timeout_protocol()->description);
144}
145
146
147/* Extract configuration on addresses and ports on which to listen.
148 * out: newly allocated list of addrinfo to listen to
149 */
150#ifdef LIBCONFIG
151static int config_listen(config_t *config, struct addrinfo **listen)
152{
153 config_setting_t *setting, *addr;
154 int len, i, keepalive;
155 const char *hostname, *port;
156
157 setting = config_lookup(config, "listen");
158 if (setting) {
159 len = config_setting_length(setting);
160 for (i = 0; i < len; i++) {
161 addr = config_setting_get_elem(setting, i);
162 if (! (config_setting_lookup_string(addr, "host", &hostname) &&
163 config_setting_lookup_string(addr, "port", &port))) {
164 fprintf(stderr,
165 "line %d:Incomplete specification (hostname and port required)\n",
166 config_setting_source_line(addr));
167 return -1;
168 }
169
170 keepalive = 0;
171 config_setting_lookup_bool(addr, "keepalive", &keepalive);
172
173 resolve_split_name(listen, hostname, port);
174
175 /* getaddrinfo returned a list of addresses corresponding to the
176 * specification; move the pointer to the end of that list before
177 * processing the next specification, while setting flags for
178 * start_listen_sockets() through ai_flags (which is not meant for
179 * that, but is only used as hint in getaddrinfo, so it's OK) */
180 for (; *listen; listen = &((*listen)->ai_next)) {
181 if (keepalive)
182 (*listen)->ai_flags = SO_KEEPALIVE;
183 }
184 }
185 }
186
187 return 0;
188}
189#endif
190
191
192
193#ifdef LIBCONFIG
194static void setup_regex_probe(struct proto *p, config_setting_t* probes)
195{
196#ifdef ENABLE_REGEX
197 int num_probes, errsize, i, res;
198 char *err;
199 const char * expr;
200 regex_t** probe_list;
201
202 num_probes = config_setting_length(probes);
203 if (!num_probes) {
204 fprintf(stderr, "%s: no probes specified\n", p->description);
205 exit(1);
206 }
207
208 p->probe = get_probe("regex");
209 probe_list = calloc(num_probes + 1, sizeof(*probe_list));
210 p->data = (void*)probe_list;
211
212 for (i = 0; i < num_probes; i++) {
213 probe_list[i] = malloc(sizeof(*(probe_list[i])));
214 expr = config_setting_get_string_elem(probes, i);
215 res = regcomp(probe_list[i], expr, 0);
216 if (res) {
217 err = malloc(errsize = regerror(res, probe_list[i], NULL, 0));
218 regerror(res, probe_list[i], err, errsize);
219 fprintf(stderr, "%s:%s\n", expr, err);
220 free(err);
221 exit(1);
222 }
223 }
224#else
225 fprintf(stderr, "line %d: regex probe specified but not compiled in\n", config_setting_source_line(probes));
226 exit(5);
227#endif
228}
229#endif
230
231#ifdef LIBCONFIG
232static void setup_sni_alpn_list(struct proto *p, config_setting_t* config_items, const char* name, int alpn)
233{
234 int num_probes, i, max_server_name_len, server_name_len;
235 const char * config_item;
236 char** sni_hostname_list;
237
238 num_probes = config_setting_length(config_items);
239 if (!num_probes) {
240 fprintf(stderr, "%s: no %s specified\n", p->description, name);
241 return;
242 }
243
244 max_server_name_len = 0;
245 for (i = 0; i < num_probes; i++) {
246 server_name_len = strlen(config_setting_get_string_elem(config_items, i));
247 if(server_name_len > max_server_name_len)
248 max_server_name_len = server_name_len;
249 }
250
251 sni_hostname_list = calloc(num_probes + 1, ++max_server_name_len);
252
253 for (i = 0; i < num_probes; i++) {
254 config_item = config_setting_get_string_elem(config_items, i);
255 sni_hostname_list[i] = malloc(max_server_name_len);
256 strcpy (sni_hostname_list[i], config_item);
257 if(verbose) fprintf(stderr, "%s: %s[%d]: %s\n", p->description, name, i, sni_hostname_list[i]);
258 }
259
260 p->data = (void*)tls_data_set_list(p->data, alpn, sni_hostname_list);
261}
262
263static void setup_sni_alpn(struct proto *p, config_setting_t* prot)
264{
265 config_setting_t *sni_hostnames, *alpn_protocols;
266
267 p->data = (void*)new_tls_data();
268 sni_hostnames = config_setting_get_member(prot, "sni_hostnames");
269 alpn_protocols = config_setting_get_member(prot, "alpn_protocols");
270
271 if(sni_hostnames && config_setting_is_array(sni_hostnames)) {
272 p->probe = get_probe("sni_alpn");
273 setup_sni_alpn_list(p, sni_hostnames, "sni_hostnames", 0);
274 }
275 if(alpn_protocols && config_setting_is_array(alpn_protocols)) {
276 p->probe = get_probe("sni_alpn");
277 setup_sni_alpn_list(p, alpn_protocols, "alpn_protocols", 1);
278 }
279}
280#endif
281
282/* Extract configuration for protocols to connect to.
283 * out: newly-allocated list of protocols
284 */
285#ifdef LIBCONFIG
286static int config_protocols(config_t *config, struct proto **prots)
287{
288 config_setting_t *setting, *prot, *patterns;
289 const char *hostname, *port, *name;
290 int i, num_prots;
291 struct proto *p, *prev = NULL;
292
293 setting = config_lookup(config, "protocols");
294 if (setting) {
295 num_prots = config_setting_length(setting);
296 for (i = 0; i < num_prots; i++) {
297 p = calloc(1, sizeof(*p));
298 if (i == 0) *prots = p;
299 if (prev) prev->next = p;
300 prev = p;
301
302 prot = config_setting_get_elem(setting, i);
303 if ((config_setting_lookup_string(prot, "name", &name) &&
304 config_setting_lookup_string(prot, "host", &hostname) &&
305 config_setting_lookup_string(prot, "port", &port)
306 )) {
307 p->description = name;
308 config_setting_lookup_string(prot, "service", &(p->service));
309 config_setting_lookup_bool(prot, "keepalive", &p->keepalive);
310
311 if (config_setting_lookup_int(prot, "log_level", &p->log_level) == CONFIG_FALSE) {
312 p->log_level = 1;
313 }
314
315 resolve_split_name(&(p->saddr), hostname, port);
316
317 p->probe = get_probe(name);
318 if (!p->probe || !strcmp(name, "sni_alpn")) {
319 fprintf(stderr, "line %d: %s: probe unknown\n", config_setting_source_line(prot), name);
320 exit(1);
321 }
322
323 /* Probe-specific options: regex patterns */
324 if (!strcmp(name, "regex")) {
325 patterns = config_setting_get_member(prot, "regex_patterns");
326 if (patterns && config_setting_is_array(patterns)) {
327 setup_regex_probe(p, patterns);
328 }
329 }
330
331 /* Probe-specific options: SNI/ALPN */
332 if (!strcmp(name, "tls")) {
333 setup_sni_alpn(p, prot);
334 }
335
336 }
337 }
338 }
339
340 return 0;
341}
342#endif
343
344/* Parses a config file
345 * in: *filename
346 * out: *listen, a newly-allocated linked list of listen addrinfo
347 * *prots, a newly-allocated linked list of protocols
348 * 1 on error, 0 on success
349 */
350#ifdef LIBCONFIG
351static int config_parse(char *filename, struct addrinfo **listen, struct proto **prots)
352{
353 config_t config;
354 int timeout;
355 const char* str;
356
357 config_init(&config);
358 if (config_read_file(&config, filename) == CONFIG_FALSE) {
359 /* If it's a parse error then there will be a line number for the failure
360 * an I/O error (such as non-existent file) will have the error line as 0
361 */
362 if (config_error_line(&config) != 0) {
363 fprintf(stderr, "%s:%d:%s\n",
364 filename,
365 config_error_line(&config),
366 config_error_text(&config));
367 exit(1);
368 }
369 fprintf(stderr, "%s:%s\n",
370 filename,
371 config_error_text(&config));
372 return 1;
373 }
374
375 config_lookup_bool(&config, "verbose", &verbose);
376 config_lookup_bool(&config, "inetd", &inetd);
377 config_lookup_bool(&config, "foreground", &foreground);
378 config_lookup_bool(&config, "numeric", &numeric);
379 config_lookup_bool(&config, "transparent", &transparent);
380
381 if (config_lookup_int(&config, "timeout", (int *)&timeout) == CONFIG_TRUE) {
382 probing_timeout = timeout;
383 }
384
385 if (config_lookup_string(&config, "on-timeout", &str)) {
386 set_ontimeout(str);
387 }
388
389 config_lookup_string(&config, "user", &user_name);
390 config_lookup_string(&config, "pidfile", &pid_file);
391
392 config_listen(&config, listen);
393 config_protocols(&config, prots);
394
395 return 0;
396}
397#endif
398
399/* Adds protocols to the list of options, so command-line parsing uses the
400 * protocol definition array
401 * options: array of options to add to; must be big enough
402 * n_opts: number of options in *options before calling (i.e. where to append)
403 * prot: array of protocols
404 * n_prots: number of protocols in *prot
405 * */
406static void append_protocols(struct option *options, int n_opts, struct proto *prot , int n_prots)
407{
408 int o, p;
409
410 for (o = n_opts, p = 0; p < n_prots; o++, p++) {
411 options[o].name = prot[p].description;
412 options[o].has_arg = required_argument;
413 options[o].flag = 0;
414 options[o].val = p + PROT_SHIFT;
415 }
416}
417
418static void make_alloptions(void)
419{
420 builtins = get_builtins();
421
422 /* Create all_options, composed of const_options followed by one option per
423 * known protocol */
424 all_options = calloc(ARRAY_SIZE(const_options) + get_num_builtins(), sizeof(struct option));
425 memcpy(all_options, const_options, sizeof(const_options));
426 append_protocols(all_options, ARRAY_SIZE(const_options) - 1, builtins, get_num_builtins());
427}
428
429/* Performs a first scan of command line options to see if a configuration file
430 * is specified. If there is one, parse it now before all other options (so
431 * configuration file settings can be overridden from the command line).
432 *
433 * prots: newly-allocated list of configured protocols, if any.
434 */
435static void cmdline_config(int argc, char* argv[], struct proto** prots)
436{
437#ifdef LIBCONFIG
438 int c, res;
439 char *config_filename;
440#endif
441
442 make_alloptions();
443
444#ifdef LIBCONFIG
445 optind = 1;
446 opterr = 0; /* we're missing protocol options at this stage so don't output errors */
447 while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
448 if (c == 'v') {
449 verbose++;
450 }
451 if (c == 'F') {
452 config_filename = optarg;
453 if (config_filename) {
454 res = config_parse(config_filename, &addr_listen, prots);
455 } else {
456 /* No configuration file specified -- try default file locations */
457 res = config_parse("/etc/sslh/sslh.cfg", &addr_listen, prots);
458 if (!res && verbose) fprintf(stderr, "Using /etc/sslh/sslh.cfg\n");
459 if (res) {
460 res = config_parse("/etc/sslh.cfg", &addr_listen, prots);
461 if (!res && verbose) fprintf(stderr, "Using /etc/sslh.cfg\n");
462 }
463 }
464 if (res)
465 exit(4);
466 break;
467 }
468 }
469#endif
470}
471
472
473/* Parse command-line options. prots points to a list of configured protocols,
474 * potentially non-allocated */
475static void parse_cmdline(int argc, char* argv[], struct proto* prots)
476{
477 int c;
478 struct addrinfo **a;
479 struct proto *p;
480
481 optind = 1;
482 opterr = 1;
483next_arg:
484 while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
485 if (c == 0) continue;
486
487 if (c >= PROT_SHIFT) {
488 if (prots)
489 for (p = prots; p && p->next; p = p->next) {
490 /* override if protocol was already defined by config file
491 * (note it only overrides address and use builtin probe) */
492 if (!strcmp(p->description, builtins[c-PROT_SHIFT].description)) {
493 resolve_name(&(p->saddr), optarg);
494 p->probe = builtins[c-PROT_SHIFT].probe;
495 goto next_arg;
496 }
497 }
498 /* At this stage, it's a new protocol: add it to the end of the
499 * list */
500 if (!prots) {
501 /* No protocols yet -- create the list */
502 p = prots = calloc(1, sizeof(*p));
503 } else {
504 p->next = calloc(1, sizeof(*p));
505 p = p->next;
506 }
507 memcpy(p, &builtins[c-PROT_SHIFT], sizeof(*p));
508 resolve_name(&(p->saddr), optarg);
509 continue;
510 }
511
512 switch (c) {
513
514 case 'F':
515 /* Legal option, but do nothing, it was already processed in
516 * cmdline_config() */
517#ifndef LIBCONFIG
518 fprintf(stderr, "Built without libconfig support: configuration file not available.\n");
519 exit(1);
520#endif
521 break;
522
523 case 't':
524 probing_timeout = atoi(optarg);
525 break;
526
527 case OPT_ONTIMEOUT:
528 set_ontimeout(optarg);
529 break;
530
531 case 'p':
532 /* find the end of the listen list */
533 for (a = &addr_listen; *a; a = &((*a)->ai_next));
534 /* append the specified addresses */
535 resolve_name(a, optarg);
536
537 break;
538
539 case 'V':
540 printf("%s %s\n", server_type, VERSION);
541 exit(0);
542
543 case 'u':
544 user_name = optarg;
545 break;
546
547 case 'P':
548 pid_file = optarg;
549 break;
550
551 case 'v':
552 verbose++;
553 break;
554
555 default:
556 print_usage();
557 exit(2);
558 }
559 }
560
561 if (!prots) {
562 fprintf(stderr, "At least one target protocol must be specified.\n");
563 exit(2);
564 }
565
566 set_protocol_list(prots);
567
568/* If compiling with systemd socket support no need to require listen address */
569#ifndef SYSTEMD
570 if (!addr_listen && !inetd) {
571 fprintf(stderr, "No listening address specified; use at least one -p option\n");
572 exit(1);
573 }
574#endif
575
576 /* Did command-line override foreground setting? */
577 if (background)
578 foreground = 0;
579
580}
581
582int main(int argc, char *argv[])
583{
584
585 extern char *optarg;
586 extern int optind;
587 int res, num_addr_listen;
588 struct proto* protocols = NULL;
589
590 int *listen_sockets;
591
592 /* Init defaults */
593 pid_file = NULL;
594 user_name = NULL;
595
596 cmdline_config(argc, argv, &protocols);
597 parse_cmdline(argc, argv, protocols);
598
599 if (inetd)
600 {
601 verbose = 0;
602 start_shoveler(0);
603 exit(0);
604 }
605
606 if (verbose)
607 printsettings();
608
609 num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
610
611#ifdef SYSTEMD
612 if (num_addr_listen < 1) {
613 fprintf(stderr, "No listening sockets found, restart sockets or specify addresses in config\n");
614 exit(1);
615 }
616#endif
617
618 if (!foreground) {
619 if (fork() > 0) exit(0); /* Detach */
620
621 /* New session -- become group leader */
622 if (getuid() == 0) {
623 res = setsid();
624 CHECK_RES_DIE(res, "setsid: already process leader");
625 }
626 }
627
628 setup_signals();
629
630 if (pid_file)
631 write_pid_file(pid_file);
632
633 if (user_name)
634 drop_privileges(user_name);
635
636
637 /* Open syslog connection */
638 setup_syslog(argv[0]);
639
640 if (verbose)
641 printcaps();
642
643 main_loop(listen_sockets, num_addr_listen);
644
645 return 0;
646}
647

Built with git-ssb-web