git ssb

0+

cel / sslh



Tree: dd900ebf3ec86a097f9cca945d6e95b62fa16a48

Files: dd900ebf3ec86a097f9cca945d6e95b62fa16a48 / sslh-main.c

20066 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 (warning: no space between -F and file name!)\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 } else {
337 fprintf(stderr, "line %d: Illegal protocol description (missing name, host or port)\n", config_setting_source_line(prot));
338 exit(1);
339 }
340 }
341 }
342
343 return 0;
344}
345#endif
346
347/* Parses a config file
348 * in: *filename
349 * out: *listen, a newly-allocated linked list of listen addrinfo
350 * *prots, a newly-allocated linked list of protocols
351 * 1 on error, 0 on success
352 */
353#ifdef LIBCONFIG
354static int config_parse(char *filename, struct addrinfo **listen, struct proto **prots)
355{
356 config_t config;
357 int timeout;
358 const char* str;
359
360 config_init(&config);
361 if (config_read_file(&config, filename) == CONFIG_FALSE) {
362 /* If it's a parse error then there will be a line number for the failure
363 * an I/O error (such as non-existent file) will have the error line as 0
364 */
365 if (config_error_line(&config) != 0) {
366 fprintf(stderr, "%s:%d:%s\n",
367 filename,
368 config_error_line(&config),
369 config_error_text(&config));
370 exit(1);
371 }
372 fprintf(stderr, "%s:%s\n",
373 filename,
374 config_error_text(&config));
375 return 1;
376 }
377
378 config_lookup_bool(&config, "verbose", &verbose);
379 config_lookup_bool(&config, "inetd", &inetd);
380 config_lookup_bool(&config, "foreground", &foreground);
381 config_lookup_bool(&config, "numeric", &numeric);
382 config_lookup_bool(&config, "transparent", &transparent);
383
384 if (config_lookup_int(&config, "timeout", (int *)&timeout) == CONFIG_TRUE) {
385 probing_timeout = timeout;
386 }
387
388 if (config_lookup_string(&config, "on-timeout", &str)) {
389 set_ontimeout(str);
390 }
391
392 config_lookup_string(&config, "user", &user_name);
393 config_lookup_string(&config, "pidfile", &pid_file);
394
395 config_listen(&config, listen);
396 config_protocols(&config, prots);
397
398 return 0;
399}
400#endif
401
402/* Adds protocols to the list of options, so command-line parsing uses the
403 * protocol definition array
404 * options: array of options to add to; must be big enough
405 * n_opts: number of options in *options before calling (i.e. where to append)
406 * prot: array of protocols
407 * n_prots: number of protocols in *prot
408 * */
409static void append_protocols(struct option *options, int n_opts, struct proto *prot , int n_prots)
410{
411 int o, p;
412
413 for (o = n_opts, p = 0; p < n_prots; o++, p++) {
414 options[o].name = prot[p].description;
415 options[o].has_arg = required_argument;
416 options[o].flag = 0;
417 options[o].val = p + PROT_SHIFT;
418 }
419}
420
421static void make_alloptions(void)
422{
423 builtins = get_builtins();
424
425 /* Create all_options, composed of const_options followed by one option per
426 * known protocol */
427 all_options = calloc(ARRAY_SIZE(const_options) + get_num_builtins(), sizeof(struct option));
428 memcpy(all_options, const_options, sizeof(const_options));
429 append_protocols(all_options, ARRAY_SIZE(const_options) - 1, builtins, get_num_builtins());
430}
431
432/* Performs a first scan of command line options to see if a configuration file
433 * is specified. If there is one, parse it now before all other options (so
434 * configuration file settings can be overridden from the command line).
435 *
436 * prots: newly-allocated list of configured protocols, if any.
437 */
438static void cmdline_config(int argc, char* argv[], struct proto** prots)
439{
440#ifdef LIBCONFIG
441 int c, res;
442 char *config_filename;
443#endif
444
445 make_alloptions();
446
447#ifdef LIBCONFIG
448 optind = 1;
449 opterr = 0; /* we're missing protocol options at this stage so don't output errors */
450 while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
451 if (c == 'v') {
452 verbose++;
453 }
454 if (c == 'F') {
455 config_filename = optarg;
456 if (config_filename) {
457 res = config_parse(config_filename, &addr_listen, prots);
458 } else {
459 /* No configuration file specified -- try default file locations */
460 res = config_parse("/etc/sslh/sslh.cfg", &addr_listen, prots);
461 if (!res && verbose) fprintf(stderr, "Using /etc/sslh/sslh.cfg\n");
462 if (res) {
463 res = config_parse("/etc/sslh.cfg", &addr_listen, prots);
464 if (!res && verbose) fprintf(stderr, "Using /etc/sslh.cfg\n");
465 }
466 }
467 if (res)
468 exit(4);
469 break;
470 }
471 }
472#endif
473}
474
475
476/* Parse command-line options. prots points to a list of configured protocols,
477 * potentially non-allocated */
478static void parse_cmdline(int argc, char* argv[], struct proto* prots)
479{
480 int c;
481 struct addrinfo **a;
482 struct proto *p;
483
484 optind = 1;
485 opterr = 1;
486next_arg:
487 while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
488 if (c == 0) continue;
489
490 if (c >= PROT_SHIFT) {
491 if (prots)
492 for (p = prots; p && p->next; p = p->next) {
493 /* override if protocol was already defined by config file
494 * (note it only overrides address and use builtin probe) */
495 if (!strcmp(p->description, builtins[c-PROT_SHIFT].description)) {
496 resolve_name(&(p->saddr), optarg);
497 p->probe = builtins[c-PROT_SHIFT].probe;
498 goto next_arg;
499 }
500 }
501 /* At this stage, it's a new protocol: add it to the end of the
502 * list */
503 if (!prots) {
504 /* No protocols yet -- create the list */
505 p = prots = calloc(1, sizeof(*p));
506 } else {
507 p->next = calloc(1, sizeof(*p));
508 p = p->next;
509 }
510 memcpy(p, &builtins[c-PROT_SHIFT], sizeof(*p));
511 resolve_name(&(p->saddr), optarg);
512 continue;
513 }
514
515 switch (c) {
516
517 case 'F':
518 /* Legal option, but do nothing, it was already processed in
519 * cmdline_config() */
520#ifndef LIBCONFIG
521 fprintf(stderr, "Built without libconfig support: configuration file not available.\n");
522 exit(1);
523#endif
524 break;
525
526 case 't':
527 probing_timeout = atoi(optarg);
528 break;
529
530 case OPT_ONTIMEOUT:
531 set_ontimeout(optarg);
532 break;
533
534 case 'p':
535 /* find the end of the listen list */
536 for (a = &addr_listen; *a; a = &((*a)->ai_next));
537 /* append the specified addresses */
538 resolve_name(a, optarg);
539
540 break;
541
542 case 'V':
543 printf("%s %s\n", server_type, VERSION);
544 exit(0);
545
546 case 'u':
547 user_name = optarg;
548 break;
549
550 case 'P':
551 pid_file = optarg;
552 break;
553
554 case 'v':
555 verbose++;
556 break;
557
558 default:
559 print_usage();
560 exit(2);
561 }
562 }
563
564 if (!prots) {
565 fprintf(stderr, "At least one target protocol must be specified.\n");
566 exit(2);
567 }
568
569 set_protocol_list(prots);
570
571/* If compiling with systemd socket support no need to require listen address */
572#ifndef SYSTEMD
573 if (!addr_listen && !inetd) {
574 fprintf(stderr, "No listening address specified; use at least one -p option\n");
575 exit(1);
576 }
577#endif
578
579 /* Did command-line override foreground setting? */
580 if (background)
581 foreground = 0;
582
583}
584
585int main(int argc, char *argv[])
586{
587
588 extern char *optarg;
589 extern int optind;
590 int res, num_addr_listen;
591 struct proto* protocols = NULL;
592
593 int *listen_sockets;
594
595 /* Init defaults */
596 pid_file = NULL;
597 user_name = NULL;
598
599 cmdline_config(argc, argv, &protocols);
600 parse_cmdline(argc, argv, protocols);
601
602 if (inetd)
603 {
604 verbose = 0;
605 start_shoveler(0);
606 exit(0);
607 }
608
609 if (verbose)
610 printsettings();
611
612 num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
613
614#ifdef SYSTEMD
615 if (num_addr_listen < 1) {
616 fprintf(stderr, "No listening sockets found, restart sockets or specify addresses in config\n");
617 exit(1);
618 }
619#endif
620
621 if (!foreground) {
622 if (fork() > 0) exit(0); /* Detach */
623
624 /* New session -- become group leader */
625 if (getuid() == 0) {
626 res = setsid();
627 CHECK_RES_DIE(res, "setsid: already process leader");
628 }
629 }
630
631 setup_signals();
632
633 if (pid_file)
634 write_pid_file(pid_file);
635
636 if (user_name)
637 drop_privileges(user_name);
638
639
640 /* Open syslog connection */
641 setup_syslog(argv[0]);
642
643 if (verbose)
644 printcaps();
645
646 main_loop(listen_sockets, num_addr_listen);
647
648 return 0;
649}
650

Built with git-ssb-web