3 * $Id: fw.c,v 1.4 1999/10/10 16:46:12 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.4 1999/10/10 16:46:12 mdw
33 * New resolver to initialize. Also, include options for grammar and
36 * Revision 1.3 1999/07/26 23:30:42 mdw
37 * Major reconstruction work for new design.
39 * Revision 1.2 1999/07/03 13:55:17 mdw
40 * Various changes. Add configuration grammar to help text. Change to
41 * root directory and open syslog when forking into background.
43 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
48 /*----- Header files ------------------------------------------------------*/
64 #include <mLib/bres.h>
65 #include <mLib/dstr.h>
66 #include <mLib/mdwopt.h>
67 #include <mLib/quis.h>
68 #include <mLib/report.h>
81 /*----- Global variables --------------------------------------------------*/
83 sel_state
*sel
; /* Multiplexor for nonblocking I/O */
85 /*----- Static variables --------------------------------------------------*/
87 static unsigned flags
= 0; /* Global state flags */
88 static unsigned active
= 0; /* Number of active things */
94 /*----- Main code ---------------------------------------------------------*/
98 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
99 * @const char *fmt@ = format string to fill in
100 * @...@ = other arguments
104 * Use: Logs a connection.
107 void fw_log(time_t t
, const char *fmt
, ...)
113 if (flags
& FW_QUIET
)
120 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S", tm
);
123 dstr_vputf(&d
, fmt
, ap
);
125 if (flags
& FW_SYSLOG
)
126 syslog(LOG_NOTICE
, "%s", d
.buf
);
129 dstr_write(&d
, stderr
);
134 /* --- @fw_inc@, @fw_dec@ --- *
140 * Use: Increments or decrements the active thing count. `fw' won't
141 * quit while there are active things.
144 void fw_inc(void) { flags
|= FW_SET
; active
++; }
145 void fw_dec(void) { if (active
) active
--; }
147 /* --- @fw_exit@ --- *
153 * Use: Exits when appropriate.
156 static void fw_exit(void)
162 /* --- @fw_tidy@ --- *
164 * Arguments: @int n@ = signal number
165 * @void *p@ = an uninteresting argument
169 * Use: Handles various signals and causes a clean tidy-up.
172 static void fw_tidy(int n
, void *p
)
174 const char *sn
= "unexpected signal (bug!)";
177 else if (n
== SIGINT
)
180 fw_log(-1, "closing down on %s", sn
);
184 /* --- Standard GNU help options --- */
186 static void version(FILE *fp
)
188 pquis(fp
, "$ version " VERSION
"\n");
191 static void usage(FILE *fp
)
193 pquis(fp
, "Usage: $ [-db] [-f file] [config statements...]\n");
196 static void help(FILE *fp
)
202 An excessively full-featured port-forwarder, which subsumes large chunks\n\
203 of the functionality of inetd, netcat, and normal cat. Options available\n\
206 -h, --help Display this help message.\n\
207 -v, --version Display the program's version number.\n\
208 -u, --usage Display a terse usage summary.\n\
210 -g, --grammar Show a summary of the configuration language.\n\
211 -o, --options Show a summary of the source and target options.\n\
213 -f, --file=FILE Read configuration from a file.\n\
214 -d, --daemon Fork into background after initializing.\n\
216 Configuration may be supplied in one or more configuration files, or on\n\
217 the command line (or both). If no `-f' option is present, and no\n\
218 configuration is given on the command line, the standard input stream is\n\
221 Configuration is free-form. Comments begin with a `#' character and\n\
222 continue to the end of the line. Each command line argument is considered\n\
223 to be a separate line.\n\
225 The grammar is fairly complicated. For a summary, run `$ --grammar'.\n\
226 For a summary of the various options, run `$ --options'.\n\
230 /* --- Other helpful options --- */
232 static void grammar(FILE *fp
)
239 file ::= empty | file stmt [`;']\n\
240 stmt ::= option-stmt | fw-stmt\n\
241 fw-stmt ::= `fw' source options [`to'|`->'] target options\n\
242 options ::= `{' option-seq `}'\n\
243 option-seq ::= empty | option-stmt [`;'] option-seq\n\
246 option-stmt ::= q-option\n\
247 q-option ::= option\n\
248 | prefix `.' q-option\n\
249 | prefix `{' option-seq `}'\n\
252 File source and target\n\
255 file ::= `file' [`.'] fspec [`,' fspec]\n\
256 fspec ::= fd-spec | name-spec | null-spec\n\
257 fd-spec ::= [[`:']`fd'[`:']] number|`stdin'|`stdout'\n\
258 name-spec ::= [[`:']`file'[`:']] file-name\n\
259 file-name ::= path-seq | [ path-seq ]\n\
260 path-seq ::= path-elt | path-seq path-elt\n\
261 path-elt ::= `/' | word\n\
262 null-spec ::= [`:']`null'[`:']\n\
264 Exec source and target\n\
267 exec ::= `exec' [`.'] cmd-spec\n\
268 cmd-spec ::= shell-cmd | [prog-name] `[' argv0 arg-seq `]'\n\
269 arg-seq ::= word | arg-seq word\n\
270 shell-cmd ::= word\n\
273 Socket source and target\n\
274 source ::= socket-source\n\
275 target ::= socket-target\n\
276 socket-source ::= [`socket'[`.']] [[`:']addr-type[`:']] source-addr\n\
277 socket-target ::= [`socket'[`.']] [[`:']addr-type[`:']] target-addr\n\
279 inet-source-addr ::= [port] port\n\
280 inet-target-addr ::= address [`:'] port\n\
281 address ::= addr-elt | address addr-elt\n\
282 addr-elt ::= `.' | word\n\
284 unix-source-addr ::= file-name\n\
285 unix-target-addr ::= file-name\n\
289 static void options(FILE *fp
)
295 File attributes (`fattr')\n\
296 prefix.fattr.mode [=] mode\n\
297 prefix.fattr.owner [=] user\n\
298 prefix.fattr.group [=] group\n\
301 file.create [=] yes|no\n\
302 file.open [=] no|truncate|append\n\
306 exec.logging [=] yes|no\n\
307 exec.dir [=] file-name\n\
308 exec.root [=] file-name\n\
309 exec.user [=] user\n\
310 exec.group [=] group\n\
311 exec.rlimit.limit[.hard|.soft] [=] value\n\
313 exec.env.unset var\n\
314 exec.env.[set] var [=] value\n\
317 socket.conn [=] number\n\
318 socket.logging [=] yes|no\n\
319 socket.inet.[allow|deny] [from] address [/ address]\n\
320 socket.unix.fattr.*\n\
326 * Arguments: @int argc@ = number of command line arguments
327 * @char *argv[]@ = vector of argument strings
331 * Use: Simple port-forwarding server.
334 int main(int argc
, char *argv
[])
347 /* --- Initialize things --- */
357 fattr_init(&fattr_global
);
360 /* --- Set up some signal handlers --- */
362 sig_add(&s_term
, SIGTERM
, fw_tidy
, 0);
363 sig_add(&s_int
, SIGINT
, fw_tidy
, 0);
366 /* --- Parse command line options --- */
369 static struct option opts
[] = {
371 /* --- Standard GNU help options --- */
373 { "help", 0, 0, 'h' },
374 { "version", 0, 0, 'v' },
375 { "usage", 0, 0, 'u' },
377 /* --- Other help options --- */
379 { "grammar", 0, 0, 'g' },
380 { "options", 0, 0, 'o' },
382 /* --- Other useful arguments --- */
384 { "file", OPTF_ARGREQ
, 0, 'f' },
385 { "fork", 0, 0, 'd' },
386 { "daemon", 0, 0, 'd' },
387 { "quiet", 0, 0, 'q' },
389 /* --- Magic terminator --- */
393 int i
= mdwopt(argc
, argv
, "+hvu go f:d", opts
, 0, 0, 0);
419 if (strcmp(optarg
, "-") == 0)
420 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
423 if ((fp
= fopen(optarg
, "r")) == 0)
424 die(1, "couldn't open file `%s': %s", optarg
, strerror(errno
));
425 scan_add(&sc
, scan_file(fp
, optarg
, 0));
446 /* --- Deal with the remaining arguments --- */
449 scan_add(&sc
, scan_argv(argv
+ optind
));
452 else if (!isatty(STDIN_FILENO
))
453 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
455 moan("no configuration given and stdin is a terminal.");
456 moan("type `%s --help' for usage information.", QUIS
);
460 /* --- Parse the configuration now gathered --- */
464 /* --- Fork into the background --- */
471 die(1, "couldn't fork: %s", strerror(errno
));
475 close(0); close(1); close(2);
484 openlog(QUIS
, 0, LOG_DAEMON
);
487 /* --- Let rip --- */
489 if (!(flags
& FW_SET
))
490 moan("nothing to do!");
491 signal(SIGPIPE
, SIG_IGN
);
497 /*----- That's all, folks -------------------------------------------------*/