git ssb

0+

cel / sslh



Tree: b108809a78dbedce34bf8dee060fd4a2e72480c8

Files: b108809a78dbedce34bf8dee060fd4a2e72480c8 / sslh-main.c

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

Built with git-ssb-web