Files: 5886bd2d43db3ee6a9ae58e3e47c34e07cf80f00 / sslh-main.c
16897 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 | /* Extract configuration for protocols to connect to. |
215 | * out: newly-allocated list of protocols |
216 | */ |
217 | |
218 | static int config_protocols(config_t *config, struct proto **prots) |
219 | { |
220 | config_setting_t *setting, *prot, *probes; |
221 | const char *hostname, *port, *name; |
222 | int i, num_prots; |
223 | struct proto *p, *prev = NULL; |
224 | |
225 | setting = config_lookup(config, "protocols"); |
226 | if (setting) { |
227 | num_prots = config_setting_length(setting); |
228 | for (i = 0; i < num_prots; i++) { |
229 | p = calloc(1, sizeof(*p)); |
230 | if (i == 0) *prots = p; |
231 | if (prev) prev->next = p; |
232 | prev = p; |
233 | |
234 | prot = config_setting_get_elem(setting, i); |
235 | if ((config_setting_lookup_string(prot, "name", &name) && |
236 | config_setting_lookup_string(prot, "host", &hostname) && |
237 | config_setting_lookup_string(prot, "port", &port) |
238 | )) { |
239 | p->description = name; |
240 | config_setting_lookup_string(prot, "service", &(p->service)); |
241 | |
242 | resolve_split_name(&(p->saddr), hostname, port); |
243 | |
244 | |
245 | probes = config_setting_get_member(prot, "probe"); |
246 | if (probes) { |
247 | if (config_setting_is_array(probes)) { |
248 | /* If 'probe' is an array, setup a regex probe using the |
249 | * array of strings as pattern */ |
250 | |
251 | setup_regex_probe(p, probes); |
252 | |
253 | } else { |
254 | /* if 'probe' is 'builtin', set the probe to the |
255 | * appropriate builtin protocol */ |
256 | if (!strcmp(config_setting_get_string(probes), "builtin")) { |
257 | p->probe = get_probe(name); |
258 | if (!p->probe) { |
259 | fprintf(stderr, "%s: no builtin probe for this protocol\n", name); |
260 | exit(1); |
261 | } |
262 | } else { |
263 | fprintf(stderr, "%s: illegal probe name\n", name); |
264 | exit(1); |
265 | } |
266 | } |
267 | } |
268 | } |
269 | } |
270 | } |
271 | |
272 | return 0; |
273 | } |
274 | |
275 | |
276 | /* Parses a config file |
277 | * in: *filename |
278 | * out: *listen, a newly-allocated linked list of listen addrinfo |
279 | * *prots, a newly-allocated linked list of protocols |
280 | */ |
281 | |
282 | static int config_parse(char *filename, struct addrinfo **listen, struct proto **prots) |
283 | { |
284 | config_t config; |
285 | int timeout; |
286 | const char* str; |
287 | |
288 | config_init(&config); |
289 | if (config_read_file(&config, filename) == CONFIG_FALSE) { |
290 | /* If it's a parse error then there will be a line number for the failure |
291 | * an I/O error (such as non-existent file) will have the error line as 0 |
292 | */ |
293 | fprintf(stderr, "%s:%d:%s\n", |
294 | filename, |
295 | config_error_line(&config), |
296 | config_error_text(&config)); |
297 | exit(1); |
298 | } |
299 | |
300 | config_lookup_bool(&config, "verbose", &verbose); |
301 | config_lookup_bool(&config, "inetd", &inetd); |
302 | config_lookup_bool(&config, "foreground", &foreground); |
303 | config_lookup_bool(&config, "numeric", &numeric); |
304 | config_lookup_bool(&config, "transparent", &transparent); |
305 | |
306 | if (config_lookup_int(&config, "timeout", (int *)&timeout) == CONFIG_TRUE) { |
307 | probing_timeout = timeout; |
308 | } |
309 | |
310 | if (config_lookup_string(&config, "on-timeout", &str)) { |
311 | set_ontimeout(str); |
312 | } |
313 | |
314 | config_lookup_string(&config, "user", &user_name); |
315 | config_lookup_string(&config, "pidfile", &pid_file); |
316 | |
317 | config_listen(&config, listen); |
318 | config_protocols(&config, prots); |
319 | |
320 | return 0; |
321 | } |
322 | |
323 | |
324 | /* Adds protocols to the list of options, so command-line parsing uses the |
325 | * protocol definition array |
326 | * options: array of options to add to; must be big enough |
327 | * n_opts: number of options in *options before calling (i.e. where to append) |
328 | * prot: array of protocols |
329 | * n_prots: number of protocols in *prot |
330 | * */ |
331 | static void append_protocols(struct option *options, int n_opts, struct proto *prot , int n_prots) |
332 | { |
333 | int o, p; |
334 | |
335 | for (o = n_opts, p = 0; p < n_prots; o++, p++) { |
336 | options[o].name = prot[p].description; |
337 | options[o].has_arg = required_argument; |
338 | options[o].flag = 0; |
339 | options[o].val = p + PROT_SHIFT; |
340 | } |
341 | } |
342 | |
343 | static void make_alloptions(void) |
344 | { |
345 | builtins = get_builtins(); |
346 | |
347 | /* Create all_options, composed of const_options followed by one option per |
348 | * known protocol */ |
349 | all_options = calloc(ARRAY_SIZE(const_options) + get_num_builtins(), sizeof(struct option)); |
350 | memcpy(all_options, const_options, sizeof(const_options)); |
351 | append_protocols(all_options, ARRAY_SIZE(const_options) - 1, builtins, get_num_builtins()); |
352 | } |
353 | |
354 | /* Performs a first scan of command line options to see if a configuration file |
355 | * is specified. If there is one, parse it now before all other options (so |
356 | * configuration file settings can be overridden from the command line). |
357 | * |
358 | * prots: newly-allocated list of configured protocols, if any. |
359 | */ |
360 | static void cmdline_config(int argc, char* argv[], struct proto** prots) |
361 | { |
362 | |
363 | int c, res; |
364 | char *config_filename; |
365 | |
366 | |
367 | make_alloptions(); |
368 | |
369 | |
370 | optind = 1; |
371 | opterr = 0; /* we're missing protocol options at this stage so don't output errors */ |
372 | while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) { |
373 | if (c == 'F') { |
374 | config_filename = optarg; |
375 | if (config_filename) { |
376 | fprintf(stderr, "config: %s\n", config_filename); |
377 | res = config_parse(config_filename, &addr_listen, prots); |
378 | } else { |
379 | /* No configuration file specified -- try default file locations */ |
380 | res = config_parse("/etc/sslh/sslh.cfg", &addr_listen, prots); |
381 | if (!res && verbose) fprintf(stderr, "Using /etc/sslh/sslh.cfg\n"); |
382 | if (res) { |
383 | res = config_parse("/etc/sslh.cfg", &addr_listen, prots); |
384 | if (!res && verbose) fprintf(stderr, "Using /etc/sslh.cfg\n"); |
385 | } |
386 | } |
387 | if (res) |
388 | exit(4); |
389 | break; |
390 | } |
391 | } |
392 | |
393 | } |
394 | |
395 | |
396 | /* Parse command-line options. prots points to a list of configured protocols, |
397 | * potentially non-allocated */ |
398 | static void parse_cmdline(int argc, char* argv[], struct proto* prots) |
399 | { |
400 | int c; |
401 | struct addrinfo **a; |
402 | struct proto *p; |
403 | |
404 | optind = 1; |
405 | opterr = 1; |
406 | next_arg: |
407 | while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) { |
408 | if (c == 0) continue; |
409 | |
410 | if (c >= PROT_SHIFT) { |
411 | if (prots) |
412 | for (p = prots; p && p->next; p = p->next) { |
413 | /* override if protocol was already defined by config file |
414 | * (note it only overrides address and use builtin probe) */ |
415 | if (!strcmp(p->description, builtins[c-PROT_SHIFT].description)) { |
416 | resolve_name(&(p->saddr), optarg); |
417 | p->probe = builtins[c-PROT_SHIFT].probe; |
418 | goto next_arg; |
419 | } |
420 | } |
421 | /* At this stage, it's a new protocol: add it to the end of the |
422 | * list */ |
423 | if (!prots) { |
424 | /* No protocols yet -- create the list */ |
425 | p = prots = calloc(1, sizeof(*p)); |
426 | } else { |
427 | p->next = calloc(1, sizeof(*p)); |
428 | p = p->next; |
429 | } |
430 | memcpy(p, &builtins[c-PROT_SHIFT], sizeof(*p)); |
431 | resolve_name(&(p->saddr), optarg); |
432 | continue; |
433 | } |
434 | |
435 | switch (c) { |
436 | |
437 | case 'F': |
438 | /* Legal option, but do nothing, it was already processed in |
439 | * cmdline_config() */ |
440 | |
441 | fprintf(stderr, "Built without libconfig support: configuration file not available.\n"); |
442 | exit(1); |
443 | |
444 | break; |
445 | |
446 | case 't': |
447 | probing_timeout = atoi(optarg); |
448 | break; |
449 | |
450 | case OPT_ONTIMEOUT: |
451 | set_ontimeout(optarg); |
452 | break; |
453 | |
454 | case 'p': |
455 | /* find the end of the listen list */ |
456 | for (a = &addr_listen; *a; a = &((*a)->ai_next)); |
457 | /* append the specified addresses */ |
458 | resolve_name(a, optarg); |
459 | |
460 | break; |
461 | |
462 | case 'V': |
463 | printf("%s %s\n", server_type, VERSION); |
464 | exit(0); |
465 | |
466 | case 'u': |
467 | user_name = optarg; |
468 | break; |
469 | |
470 | case 'P': |
471 | pid_file = optarg; |
472 | break; |
473 | |
474 | case 'v': |
475 | verbose++; |
476 | break; |
477 | |
478 | default: |
479 | print_usage(); |
480 | exit(2); |
481 | } |
482 | } |
483 | |
484 | if (!prots) { |
485 | fprintf(stderr, "At least one target protocol must be specified.\n"); |
486 | exit(2); |
487 | } |
488 | |
489 | set_protocol_list(prots); |
490 | |
491 | if (!addr_listen && !inetd) { |
492 | fprintf(stderr, "No listening address specified; use at least one -p option\n"); |
493 | exit(1); |
494 | } |
495 | |
496 | /* Did command-line override foreground setting? */ |
497 | if (background) |
498 | foreground = 0; |
499 | |
500 | } |
501 | |
502 | int main(int argc, char *argv[]) |
503 | { |
504 | |
505 | extern char *optarg; |
506 | extern int optind; |
507 | int res, num_addr_listen; |
508 | struct proto* protocols = NULL; |
509 | |
510 | int *listen_sockets; |
511 | |
512 | /* Init defaults */ |
513 | pid_file = NULL; |
514 | user_name = NULL; |
515 | |
516 | cmdline_config(argc, argv, &protocols); |
517 | parse_cmdline(argc, argv, protocols); |
518 | |
519 | if (inetd) |
520 | { |
521 | verbose = 0; |
522 | start_shoveler(0); |
523 | exit(0); |
524 | } |
525 | |
526 | if (verbose) |
527 | printsettings(); |
528 | |
529 | num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen); |
530 | |
531 | if (!foreground) { |
532 | if (fork() > 0) exit(0); /* Detach */ |
533 | |
534 | /* New session -- become group leader */ |
535 | if (getuid() == 0) { |
536 | res = setsid(); |
537 | CHECK_RES_DIE(res, "setsid: already process leader"); |
538 | } |
539 | } |
540 | |
541 | setup_signals(); |
542 | |
543 | if (pid_file) |
544 | write_pid_file(pid_file); |
545 | |
546 | if (user_name) |
547 | drop_privileges(user_name); |
548 | |
549 | |
550 | /* Open syslog connection */ |
551 | setup_syslog(argv[0]); |
552 | |
553 | if (verbose) |
554 | printcaps(); |
555 | |
556 | main_loop(listen_sockets, num_addr_listen); |
557 | |
558 | return 0; |
559 | } |
560 |
Built with git-ssb-web