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