3 * $Id: fw.c,v 1.5 1999/10/22 22:47:50 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.5 1999/10/22 22:47:50 mdw
33 * Grammar changes. Also, don't enable SIGINT if it's currently ignored.
35 * Revision 1.4 1999/10/10 16:46:12 mdw
36 * New resolver to initialize. Also, include options for grammar and
39 * Revision 1.3 1999/07/26 23:30:42 mdw
40 * Major reconstruction work for new design.
42 * Revision 1.2 1999/07/03 13:55:17 mdw
43 * Various changes. Add configuration grammar to help text. Change to
44 * root directory and open syslog when forking into background.
46 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
51 /*----- Header files ------------------------------------------------------*/
67 #include <mLib/bres.h>
68 #include <mLib/dstr.h>
69 #include <mLib/mdwopt.h>
70 #include <mLib/quis.h>
71 #include <mLib/report.h>
84 /*----- Global variables --------------------------------------------------*/
86 sel_state
*sel
; /* Multiplexor for nonblocking I/O */
88 /*----- Static variables --------------------------------------------------*/
90 static unsigned flags
= 0; /* Global state flags */
91 static unsigned active
= 0; /* Number of active things */
97 /*----- Main code ---------------------------------------------------------*/
101 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
102 * @const char *fmt@ = format string to fill in
103 * @...@ = other arguments
107 * Use: Logs a connection.
110 void fw_log(time_t t
, const char *fmt
, ...)
116 if (flags
& FW_QUIET
)
123 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S", tm
);
126 dstr_vputf(&d
, fmt
, ap
);
128 if (flags
& FW_SYSLOG
)
129 syslog(LOG_NOTICE
, "%s", d
.buf
);
132 dstr_write(&d
, stderr
);
137 /* --- @fw_inc@, @fw_dec@ --- *
143 * Use: Increments or decrements the active thing count. `fw' won't
144 * quit while there are active things.
147 void fw_inc(void) { flags
|= FW_SET
; active
++; }
148 void fw_dec(void) { if (active
) active
--; }
150 /* --- @fw_exit@ --- *
156 * Use: Exits when appropriate.
159 static void fw_exit(void)
165 /* --- @fw_tidy@ --- *
167 * Arguments: @int n@ = signal number
168 * @void *p@ = an uninteresting argument
172 * Use: Handles various signals and causes a clean tidy-up.
175 static void fw_tidy(int n
, void *p
)
177 const char *sn
= "unexpected signal (bug!)";
180 else if (n
== SIGINT
)
183 fw_log(-1, "closing down on %s", sn
);
187 /* --- Standard GNU help options --- */
189 static void version(FILE *fp
)
191 pquis(fp
, "$ version " VERSION
"\n");
194 static void usage(FILE *fp
)
196 pquis(fp
, "Usage: $ [-db] [-f file] [config statements...]\n");
199 static void help(FILE *fp
)
205 An excessively full-featured port-forwarder, which subsumes large chunks\n\
206 of the functionality of inetd, netcat, and normal cat. Options available\n\
209 -h, --help Display this help message.\n\
210 -v, --version Display the program's version number.\n\
211 -u, --usage Display a terse usage summary.\n\
213 -g, --grammar Show a summary of the configuration language.\n\
214 -o, --options Show a summary of the source and target options.\n\
216 -f, --file=FILE Read configuration from a file.\n\
217 -d, --daemon Fork into background after initializing.\n\
219 Configuration may be supplied in one or more configuration files, or on\n\
220 the command line (or both). If no `-f' option is present, and no\n\
221 configuration is given on the command line, the standard input stream is\n\
224 Configuration is free-form. Comments begin with a `#' character and\n\
225 continue to the end of the line. Each command line argument is considered\n\
226 to be a separate line.\n\
228 The grammar is fairly complicated. For a summary, run `$ --grammar'.\n\
229 For a summary of the various options, run `$ --options'.\n\
233 /* --- Other helpful options --- */
235 static void grammar(FILE *fp
)
242 file ::= empty | file stmt [`;']\n\
243 stmt ::= option-stmt | fw-stmt\n\
244 fw-stmt ::= `fw' source options [`to'|`->'] target options\n\
245 options ::= `{' option-seq `}'\n\
246 option-seq ::= empty | option-stmt [`;'] option-seq\n\
249 option-stmt ::= q-option\n\
250 q-option ::= option\n\
251 | prefix `.' q-option\n\
252 | prefix `{' option-seq `}'\n\
255 File source and target\n\
258 file ::= `file' [`.'] fspec [`,' fspec]\n\
259 fspec ::= fd-spec | name-spec | null-spec\n\
260 fd-spec ::= [[`:']`fd'[`:']] number|`stdin'|`stdout'\n\
261 name-spec ::= [[`:']`file'[`:']] file-name\n\
262 file-name ::= path-seq | [ path-seq ]\n\
263 path-seq ::= path-elt | path-seq path-elt\n\
264 path-elt ::= `/' | word\n\
265 null-spec ::= [`:']`null'[`:']\n\
267 Exec source and target\n\
270 exec ::= `exec' [`.'] cmd-spec\n\
271 cmd-spec ::= shell-cmd | [prog-name] `[' argv0 arg-seq `]'\n\
272 arg-seq ::= word | arg-seq word\n\
273 shell-cmd ::= word\n\
276 Socket source and target\n\
277 source ::= socket-source\n\
278 target ::= socket-target\n\
279 socket-source ::= [`socket'[`.']] [[`:']addr-type[`:']] source-addr\n\
280 socket-target ::= [`socket'[`.']] [[`:']addr-type[`:']] target-addr\n\
282 inet-source-addr ::= [port] port\n\
283 inet-target-addr ::= address [`:'] port\n\
284 address ::= addr-elt | address addr-elt\n\
285 addr-elt ::= `.' | word\n\
287 unix-source-addr ::= file-name\n\
288 unix-target-addr ::= file-name\n\
292 static void options(FILE *fp
)
298 File attributes (`fattr')\n\
299 prefix.fattr.mode [=] mode\n\
300 prefix.fattr.owner [=] user\n\
301 prefix.fattr.group [=] group\n\
304 file.create [=] yes|no\n\
305 file.open [=] no|truncate|append\n\
309 exec.logging [=] yes|no\n\
310 exec.dir [=] file-name\n\
311 exec.root [=] file-name\n\
312 exec.user [=] user\n\
313 exec.group [=] group\n\
314 exec.rlimit.limit[.hard|.soft] [=] value\n\
316 exec.env.unset var\n\
317 exec.env.[set] var [=] value\n\
320 socket.conn [=] number|unlimited|one-shot\n\
321 socket.logging [=] yes|no\n\
322 socket.inet.[allow|deny] [from] address [/ address]\n\
323 socket.unix.fattr.*\n\
329 * Arguments: @int argc@ = number of command line arguments
330 * @char *argv[]@ = vector of argument strings
334 * Use: Simple port-forwarding server.
337 int main(int argc
, char *argv
[])
350 /* --- Initialize things --- */
360 fattr_init(&fattr_global
);
363 /* --- Set up some signal handlers --- *
365 * Don't enable @SIGINT@ if the caller already disabled it.
371 sig_add(&s_term
, SIGTERM
, fw_tidy
, 0);
372 sigaction(SIGINT
, 0, &sa
);
373 if (sa
.sa_handler
!= SIG_IGN
)
374 sig_add(&s_int
, SIGINT
, fw_tidy
, 0);
379 /* --- Parse command line options --- */
382 static struct option opts
[] = {
384 /* --- Standard GNU help options --- */
386 { "help", 0, 0, 'h' },
387 { "version", 0, 0, 'v' },
388 { "usage", 0, 0, 'u' },
390 /* --- Other help options --- */
392 { "grammar", 0, 0, 'g' },
393 { "options", 0, 0, 'o' },
395 /* --- Other useful arguments --- */
397 { "file", OPTF_ARGREQ
, 0, 'f' },
398 { "fork", 0, 0, 'd' },
399 { "daemon", 0, 0, 'd' },
400 { "quiet", 0, 0, 'q' },
402 /* --- Magic terminator --- */
406 int i
= mdwopt(argc
, argv
, "+hvu go f:d", opts
, 0, 0, 0);
432 if (strcmp(optarg
, "-") == 0)
433 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
436 if ((fp
= fopen(optarg
, "r")) == 0)
437 die(1, "couldn't open file `%s': %s", optarg
, strerror(errno
));
438 scan_add(&sc
, scan_file(fp
, optarg
, 0));
459 /* --- Deal with the remaining arguments --- */
462 scan_add(&sc
, scan_argv(argv
+ optind
));
465 else if (!isatty(STDIN_FILENO
))
466 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
468 moan("no configuration given and stdin is a terminal.");
469 moan("type `%s --help' for usage information.", QUIS
);
473 /* --- Parse the configuration now gathered --- */
477 /* --- Fork into the background --- */
484 die(1, "couldn't fork: %s", strerror(errno
));
488 close(0); close(1); close(2);
497 openlog(QUIS
, 0, LOG_DAEMON
);
500 /* --- Let rip --- */
502 if (!(flags
& FW_SET
))
503 moan("nothing to do!");
504 signal(SIGPIPE
, SIG_IGN
);
510 /*----- That's all, folks -------------------------------------------------*/