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