git ssb

0+

cel / sslh



Tree: ca461ea077d748dde25eb6610e66514fa0151baf

Files: ca461ea077d748dde25eb6610e66514fa0151baf / sslh-main.c

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

Built with git-ssb-web