3 * $Id: fw.c,v 1.6 1999/12/22 15:44:10 mdw Exp $
5 * Port forwarding thingy
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.6 1999/12/22 15:44:10 mdw
33 * Make syslog a separate option, and do it better.
35 * Revision 1.5 1999/10/22 22:47:50 mdw
36 * Grammar changes. Also, don't enable SIGINT if it's currently ignored.
38 * Revision 1.4 1999/10/10 16:46:12 mdw
39 * New resolver to initialize. Also, include options for grammar and
42 * Revision 1.3 1999/07/26 23:30:42 mdw
43 * Major reconstruction work for new design.
45 * Revision 1.2 1999/07/03 13:55:17 mdw
46 * Various changes. Add configuration grammar to help text. Change to
47 * root directory and open syslog when forking into background.
49 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
54 /*----- Header files ------------------------------------------------------*/
70 #include <mLib/bres.h>
71 #include <mLib/dstr.h>
72 #include <mLib/mdwopt.h>
73 #include <mLib/quis.h>
74 #include <mLib/report.h>
87 /*----- Global variables --------------------------------------------------*/
89 sel_state
*sel
; /* Multiplexor for nonblocking I/O */
91 /*----- Static variables --------------------------------------------------*/
93 static unsigned flags
= 0; /* Global state flags */
94 static unsigned active
= 0; /* Number of active things */
100 /*----- Main code ---------------------------------------------------------*/
102 /* --- @fw_log@ --- *
104 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
105 * @const char *fmt@ = format string to fill in
106 * @...@ = other arguments
110 * Use: Logs a connection.
113 void fw_log(time_t t
, const char *fmt
, ...)
119 if (flags
& FW_QUIET
)
126 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S ", tm
);
128 dstr_vputf(&d
, fmt
, ap
);
130 if (flags
& FW_SYSLOG
)
131 syslog(LOG_NOTICE
, "%s", d
.buf
);
134 dstr_write(&d
, stderr
);
139 /* --- @fw_inc@, @fw_dec@ --- *
145 * Use: Increments or decrements the active thing count. `fw' won't
146 * quit while there are active things.
149 void fw_inc(void) { flags
|= FW_SET
; active
++; }
150 void fw_dec(void) { if (active
) active
--; }
152 /* --- @fw_exit@ --- *
158 * Use: Exits when appropriate.
161 static void fw_exit(void)
167 /* --- @fw_tidy@ --- *
169 * Arguments: @int n@ = signal number
170 * @void *p@ = an uninteresting argument
174 * Use: Handles various signals and causes a clean tidy-up.
177 static void fw_tidy(int n
, void *p
)
179 const char *sn
= "unexpected signal (bug!)";
182 else if (n
== SIGINT
)
185 fw_log(-1, "closing down on %s", sn
);
189 /* --- Standard GNU help options --- */
191 static void version(FILE *fp
)
193 pquis(fp
, "$ version " VERSION
"\n");
196 static void usage(FILE *fp
)
198 pquis(fp
, "Usage: $ [-dql] [-f file] [config statements...]\n");
201 static void help(FILE *fp
)
207 An excessively full-featured port-forwarder, which subsumes large chunks\n\
208 of the functionality of inetd, netcat, and normal cat. Options available\n\
211 -h, --help Display this help message.\n\
212 -v, --version Display the program's version number.\n\
213 -u, --usage Display a terse usage summary.\n\
215 -g, --grammar Show a summary of the configuration language.\n\
216 -o, --options Show a summary of the source and target options.\n\
218 -f, --file=FILE Read configuration from a file.\n\
219 -q, --quiet Don't emit any logging information.\n\
220 -d, --daemon Fork into background after initializing.\n\
221 -l, --syslog Send log output to the system logger.\n\
223 Configuration may be supplied in one or more configuration files, or on\n\
224 the command line (or both). If no `-f' option is present, and no\n\
225 configuration is given on the command line, the standard input stream is\n\
228 Configuration is free-form. Comments begin with a `#' character and\n\
229 continue to the end of the line. Each command line argument is considered\n\
230 to be a separate line.\n\
232 The grammar is fairly complicated. For a summary, run `$ --grammar'.\n\
233 For a summary of the various options, run `$ --options'.\n\
237 /* --- Other helpful options --- */
239 static void grammar(FILE *fp
)
246 file ::= empty | file stmt [`;']\n\
247 stmt ::= option-stmt | fw-stmt\n\
248 fw-stmt ::= `fw' source options [`to'|`->'] target options\n\
249 options ::= `{' option-seq `}'\n\
250 option-seq ::= empty | option-stmt [`;'] option-seq\n\
253 option-stmt ::= q-option\n\
254 q-option ::= option\n\
255 | prefix `.' q-option\n\
256 | prefix `{' option-seq `}'\n\
259 File source and target\n\
262 file ::= `file' [`.'] fspec [`,' fspec]\n\
263 fspec ::= fd-spec | name-spec | null-spec\n\
264 fd-spec ::= [[`:']`fd'[`:']] number|`stdin'|`stdout'\n\
265 name-spec ::= [[`:']`file'[`:']] file-name\n\
266 file-name ::= path-seq | [ path-seq ]\n\
267 path-seq ::= path-elt | path-seq path-elt\n\
268 path-elt ::= `/' | word\n\
269 null-spec ::= [`:']`null'[`:']\n\
271 Exec source and target\n\
274 exec ::= `exec' [`.'] cmd-spec\n\
275 cmd-spec ::= shell-cmd | [prog-name] `[' argv0 arg-seq `]'\n\
276 arg-seq ::= word | arg-seq word\n\
277 shell-cmd ::= word\n\
280 Socket source and target\n\
281 source ::= socket-source\n\
282 target ::= socket-target\n\
283 socket-source ::= [`socket'[`.']] [[`:']addr-type[`:']] source-addr\n\
284 socket-target ::= [`socket'[`.']] [[`:']addr-type[`:']] target-addr\n\
286 inet-source-addr ::= [port] port\n\
287 inet-target-addr ::= address [`:'] port\n\
288 address ::= addr-elt | address addr-elt\n\
289 addr-elt ::= `.' | word\n\
291 unix-source-addr ::= file-name\n\
292 unix-target-addr ::= file-name\n\
296 static void options(FILE *fp
)
302 File attributes (`fattr')\n\
303 prefix.fattr.mode [=] mode\n\
304 prefix.fattr.owner [=] user\n\
305 prefix.fattr.group [=] group\n\
308 file.create [=] yes|no\n\
309 file.open [=] no|truncate|append\n\
313 exec.logging [=] yes|no\n\
314 exec.dir [=] file-name\n\
315 exec.root [=] file-name\n\
316 exec.user [=] user\n\
317 exec.group [=] group\n\
318 exec.rlimit.limit[.hard|.soft] [=] value\n\
320 exec.env.unset var\n\
321 exec.env.[set] var [=] value\n\
324 socket.conn [=] number|unlimited|one-shot\n\
325 socket.logging [=] yes|no\n\
326 socket.inet.[allow|deny] [from] address [/ address]\n\
327 socket.unix.fattr.*\n\
333 * Arguments: @int argc@ = number of command line arguments
334 * @char *argv[]@ = vector of argument strings
338 * Use: Simple port-forwarding server.
341 int main(int argc
, char *argv
[])
355 /* --- Initialize things --- */
365 fattr_init(&fattr_global
);
368 /* --- Set up some signal handlers --- *
370 * Don't enable @SIGINT@ if the caller already disabled it.
376 sig_add(&s_term
, SIGTERM
, fw_tidy
, 0);
377 sigaction(SIGINT
, 0, &sa
);
378 if (sa
.sa_handler
!= SIG_IGN
)
379 sig_add(&s_int
, SIGINT
, fw_tidy
, 0);
384 /* --- Parse command line options --- */
387 static struct option opts
[] = {
389 /* --- Standard GNU help options --- */
391 { "help", 0, 0, 'h' },
392 { "version", 0, 0, 'v' },
393 { "usage", 0, 0, 'u' },
395 /* --- Other help options --- */
397 { "grammar", 0, 0, 'g' },
398 { "options", 0, 0, 'o' },
400 /* --- Other useful arguments --- */
402 { "file", OPTF_ARGREQ
, 0, 'f' },
403 { "fork", 0, 0, 'd' },
404 { "daemon", 0, 0, 'd' },
405 { "syslog", 0, 0, 'l' },
406 { "log", 0, 0, 'l' },
407 { "quiet", 0, 0, 'q' },
409 /* --- Magic terminator --- */
413 int i
= mdwopt(argc
, argv
, "+hvu go f:dl", opts
, 0, 0, 0);
439 if (strcmp(optarg
, "-") == 0)
440 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
443 if ((fp
= fopen(optarg
, "r")) == 0)
444 die(1, "couldn't open file `%s': %s", optarg
, strerror(errno
));
445 scan_add(&sc
, scan_file(fp
, optarg
, 0));
469 /* --- Deal with the remaining arguments --- */
472 scan_add(&sc
, scan_argv(argv
+ optind
));
475 else if (!isatty(STDIN_FILENO
))
476 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
478 moan("no configuration given and stdin is a terminal.");
479 moan("type `%s --help' for usage information.", QUIS
);
483 /* --- Parse the configuration now gathered --- */
487 /* --- Fork into the background --- */
494 die(1, "couldn't fork: %s", strerror(errno
));
498 close(0); close(1); close(2);
509 openlog(QUIS
, 0, LOG_DAEMON
);
512 /* --- Let rip --- */
514 if (!(flags
& FW_SET
))
515 moan("nothing to do!");
516 signal(SIGPIPE
, SIG_IGN
);
522 /*----- That's all, folks -------------------------------------------------*/