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