04235d4d1c44ed34813e1f43296ce9858bfa7437
3 * $Id: fw.c,v 1.2 1999/07/03 13:55:17 mdw Exp $
5 * Port forwarding thingy
7 * (c) 1999 Mark Wooding
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.2 1999/07/03 13:55:17 mdw
33 * Various changes. Add configuration grammar to help text. Change to
34 * root directory and open syslog when forking into background.
36 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
41 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
67 /*----- Global variables --------------------------------------------------*/
69 sel_state
*sel
; /* Multiplexor for nonblocking I/O */
70 unsigned flags
= 0; /* Global state flags */
72 /*----- Main code ---------------------------------------------------------*/
74 /* --- Standard GNU help options --- */
76 static void version(FILE *fp
)
78 fprintf(fp
, "%s version " VERSION
"\n", QUIS
);
81 static void usage(FILE *fp
)
83 fprintf(stderr
, "Usage: %s [-db] [-f file] [config statements...]\n",
87 static void help(FILE *fp
)
93 A fairly full-featured port-forwarder. Options available are:\n\
95 -h, --help Display this help message.\n\
96 -v, --version Display the program's version number.\n\
97 -u, --usage Display a terse usage summary.\n\
99 -f, --file=FILE Read configuration from a file.\n\
100 -d, --dump Dump the configuration to standard output.\n\
101 -b, --background Fork into background after initializing.\n\
103 Configuration may be supplied in one or more configuration files, or on\n\
104 the command line (or both). If no `-f' option is present, and no\n\
105 configuration is given on the command line, the standard input stream is\n\
108 Configuration is free-form. Comments begin with a `#' character and\n\
109 continue to the end of the line. Each command line argument is considered\n\
110 to be a separate line. The syntax is as follows:\n\
113 stmt : fwd-stmt | acl-stmt\n\
114 fwd-stmt : `forward' [`port'] port [`to'] addr [`:'] port [fwd-attr] [`;']\n\
115 fwd-attr : `{' acl-stmt... `}'\n\
116 acl-stmt : (`allow' | `deny') [`from'] addr [`/' mask] [`;']\n\
122 * Arguments: @int argc@ = number of command line arguments
123 * @char *argv[]@ = vector of argument strings
127 * Use: Simple port-forwarding server.
130 int main(int argc
, char *argv
[])
142 /* --- Initialize things --- */
150 /* --- Parse command line options --- */
153 static struct option opts
[] = {
155 /* --- Standard GNU help options --- */
157 { "help", 0, 0, 'h' },
158 { "version", 0, 0, 'v' },
159 { "usage", 0, 0, 'u' },
161 /* --- Other useful arguments --- */
163 { "file", OPTF_ARGREQ
, 0, 'f' },
164 { "dump", 0, 0, 'd' },
165 { "fork", 0, 0, 'b' },
166 { "background", 0, 0, 'b' },
168 /* --- Magic terminator --- */
172 int i
= mdwopt(argc
, argv
, "hvu f:db", opts
, 0, 0, 0);
192 if ((fp
= fopen(optarg
, "r")) == 0)
193 die(1, "couldn't open file `%s': %s", optarg
, strerror(errno
));
194 scan_fileinit(&ctx
, fp
, optarg
);
216 /* --- Deal with the remaining arguments --- */
218 if (optind
== argc
) {
221 else if (isatty(STDIN_FILENO
)) {
222 moan("no configuration given and stdin is a terminal.");
223 moan("type `%s --help' for usage information.", QUIS
);
227 scan_fileinit(&ctx
, stdin
, "<stdin>");
232 scan_argvinit(&ctx
, argv
+ optind
);
236 /* --- Dump out the state --- */
240 fputs("global acl:\n", stdout
);
242 for (s
= sel
->files
; s
; s
= s
->next
)
243 listener_dump((listener
*)s
, stdout
);
247 /* --- Fork into the background --- */
254 die(1, "couldn't fork: %s", strerror(errno
));
258 close(0); close(1); close(2);
267 openlog(QUIS
, 0, LOG_DAEMON
);
270 /* --- Let rip --- */
277 /*----- That's all, folks -------------------------------------------------*/