3 * $Id: fw.c,v 1.3 1999/07/26 23:30:42 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.3 1999/07/26 23:30:42 mdw
33 * Major reconstruction work for new design.
35 * Revision 1.2 1999/07/03 13:55:17 mdw
36 * Various changes. Add configuration grammar to help text. Change to
37 * root directory and open syslog when forking into background.
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
44 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/dstr.h>
61 #include <mLib/mdwopt.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
77 /*----- Global variables --------------------------------------------------*/
79 sel_state
*sel
; /* Multiplexor for nonblocking I/O */
81 /*----- Static variables --------------------------------------------------*/
83 static unsigned flags
= 0; /* Global state flags */
84 static unsigned active
= 0; /* Number of active things */
90 /*----- Main code ---------------------------------------------------------*/
94 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
95 * @const char *fmt@ = format string to fill in
96 * @...@ = other arguments
100 * Use: Logs a connection.
103 void fw_log(time_t t
, const char *fmt
, ...)
109 if (flags
& FW_QUIET
)
116 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S", tm
);
119 dstr_vputf(&d
, fmt
, ap
);
121 if (flags
& FW_SYSLOG
)
122 syslog(LOG_NOTICE
, "%s", d
.buf
);
125 dstr_write(&d
, stderr
);
130 /* --- @fw_inc@, @fw_dec@ --- *
136 * Use: Increments or decrements the active thing count. `fw' won't
137 * quit while there are active things.
140 void fw_inc(void) { flags
|= FW_SET
; active
++; }
141 void fw_dec(void) { if (active
) active
--; }
143 /* --- @fw_exit@ --- *
149 * Use: Exits when appropriate.
152 static void fw_exit(void)
158 /* --- @fw_tidy@ --- *
160 * Arguments: @int n@ = signal number
161 * @void *p@ = an uninteresting argument
165 * Use: Handles various signals and causes a clean tidy-up.
168 static void fw_tidy(int n
, void *p
)
170 const char *sn
= "unexpected signal (bug!)";
173 else if (n
== SIGINT
)
176 fw_log(-1, "closing down on %s", sn
);
180 /* --- Standard GNU help options --- */
182 static void version(FILE *fp
)
184 fprintf(fp
, "%s version " VERSION
"\n", QUIS
);
187 static void usage(FILE *fp
)
189 fprintf(stderr
, "Usage: %s [-db] [-f file] [config statements...]\n",
193 static void help(FILE *fp
)
199 An excessively full-featured port-forwarder, which subsumes large chunks\n\
200 of the functionality of inetd, netcat, and normal cat. Options available\n\
203 -h, --help Display this help message.\n\
204 -v, --version Display the program's version number.\n\
205 -u, --usage Display a terse usage summary.\n\
207 -f, --file=FILE Read configuration from a file.\n\
208 -d, --daemon Fork into background after initializing.\n\
210 Configuration may be supplied in one or more configuration files, or on\n\
211 the command line (or both). If no `-f' option is present, and no\n\
212 configuration is given on the command line, the standard input stream is\n\
215 Configuration is free-form. Comments begin with a `#' character and\n\
216 continue to the end of the line. Each command line argument is considered\n\
217 to be a separate line.\n\
219 The syntax is too complicated to describe here. Read the manual.\n\
225 * Arguments: @int argc@ = number of command line arguments
226 * @char *argv[]@ = vector of argument strings
230 * Use: Simple port-forwarding server.
233 int main(int argc
, char *argv
[])
246 /* --- Initialize things --- */
255 fattr_init(&fattr_global
);
258 /* --- Set up some signal handlers --- */
260 sig_add(&s_term
, SIGTERM
, fw_tidy
, 0);
261 sig_add(&s_int
, SIGINT
, fw_tidy
, 0);
264 /* --- Parse command line options --- */
267 static struct option opts
[] = {
269 /* --- Standard GNU help options --- */
271 { "help", 0, 0, 'h' },
272 { "version", 0, 0, 'v' },
273 { "usage", 0, 0, 'u' },
275 /* --- Other useful arguments --- */
277 { "file", OPTF_ARGREQ
, 0, 'f' },
278 { "fork", 0, 0, 'd' },
279 { "daemon", 0, 0, 'd' },
280 { "quiet", 0, 0, 'q' },
282 /* --- Magic terminator --- */
286 int i
= mdwopt(argc
, argv
, "+hvu f:d", opts
, 0, 0, 0);
304 if (strcmp(optarg
, "-") == 0)
305 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
308 if ((fp
= fopen(optarg
, "r")) == 0)
309 die(1, "couldn't open file `%s': %s", optarg
, strerror(errno
));
310 scan_add(&sc
, scan_file(fp
, optarg
, 0));
331 /* --- Deal with the remaining arguments --- */
334 scan_add(&sc
, scan_argv(argv
+ optind
));
337 else if (!isatty(STDIN_FILENO
))
338 scan_add(&sc
, scan_file(stdin
, "<stdin>", SCF_NOCLOSE
));
340 moan("no configuration given and stdin is a terminal.");
341 moan("type `%s --help' for usage information.", QUIS
);
345 /* --- Parse the configuration now gathered --- */
349 /* --- Fork into the background --- */
356 die(1, "couldn't fork: %s", strerror(errno
));
360 close(0); close(1); close(2);
369 openlog(QUIS
, 0, LOG_DAEMON
);
372 /* --- Let rip --- */
374 if (!(flags
& FW_SET
))
375 moan("nothing to do!");
376 signal(SIGPIPE
, SIG_IGN
);
382 /*----- That's all, folks -------------------------------------------------*/