Deleted Added
full compact
main.c (190633) main.c (204591)
1/*
1/*
2 * Copyright (c) 2002-2003 Luigi Rizzo
2 * Copyright (c) 2002-2003,2010 Luigi Rizzo
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
5 *
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
8 *
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
11 *
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
15 *
16 * This software is provided ``AS IS'' without any warranties of any kind.
17 *
18 * Command line interface for IP firewall facility
19 *
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
5 *
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
8 *
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
11 *
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
15 *
16 * This software is provided ``AS IS'' without any warranties of any kind.
17 *
18 * Command line interface for IP firewall facility
19 *
20 * $FreeBSD: head/sbin/ipfw/main.c 190633 2009-04-01 20:23:47Z piso $
20 * $FreeBSD: head/sbin/ipfw/main.c 204591 2010-03-02 17:40:48Z luigi $
21 */
22
23#include <sys/wait.h>
24#include <ctype.h>
25#include <err.h>
26#include <errno.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sysexits.h>
32#include <unistd.h>
33
34#include "ipfw2.h"
35
36static void
37help(void)
38{
39 fprintf(stderr,
40"ipfw syntax summary (but please do read the ipfw(8) manpage):\n\n"
41"\tipfw [-abcdefhnNqStTv] <command>\n\n"
42"where <command> is one of the following:\n\n"
43"add [num] [set N] [prob x] RULE-BODY\n"
44"{pipe|queue} N config PIPE-BODY\n"
45"[pipe|queue] {zero|delete|show} [N{,N}]\n"
46"nat N config {ip IPADDR|if IFNAME|log|deny_in|same_ports|unreg_only|reset|\n"
47" reverse|proxy_only|redirect_addr linkspec|\n"
48" redirect_port linkspec|redirect_proto linkspec}\n"
49"set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n"
50"set N {show|list|zero|resetlog|delete} [N{,N}] | flush\n"
51"table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n"
52"table all {flush | list}\n"
53"\n"
54"RULE-BODY: check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n"
55"ACTION: check-state | allow | count | deny | unreach{,6} CODE |\n"
56" skipto N | {divert|tee} PORT | forward ADDR |\n"
57" pipe N | queue N | nat N | setfib FIB | reass\n"
58"PARAMS: [log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n"
59"ADDR: [ MAC dst src ether_type ] \n"
60" [ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n"
61" [ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n"
62"IPADDR: [not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n"
63"IP6ADDR: [not] { any | me | me6 | ip6/bits | IP6LIST }\n"
64"IP6LIST: { ip6 | ip6/bits }[,IP6LIST]\n"
65"IPLIST: { ip | ip/bits | ip:mask }[,IPLIST]\n"
66"OPTION_LIST: OPTION [OPTION_LIST]\n"
67"OPTION: bridged | diverted | diverted-loopback | diverted-output |\n"
68" {dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n"
69" {dst-port|src-port} LIST |\n"
70" estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n"
71" iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n"
72" ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n"
73" icmp6types LIST | ext6hdr LIST | flow-id N[,N] | fib FIB |\n"
74" mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n"
75" setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n"
76" tcpdatalen LIST | verrevpath | versrcreach | antispoof\n"
77);
78
79 exit(0);
80}
81
82/*
21 */
22
23#include <sys/wait.h>
24#include <ctype.h>
25#include <err.h>
26#include <errno.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sysexits.h>
32#include <unistd.h>
33
34#include "ipfw2.h"
35
36static void
37help(void)
38{
39 fprintf(stderr,
40"ipfw syntax summary (but please do read the ipfw(8) manpage):\n\n"
41"\tipfw [-abcdefhnNqStTv] <command>\n\n"
42"where <command> is one of the following:\n\n"
43"add [num] [set N] [prob x] RULE-BODY\n"
44"{pipe|queue} N config PIPE-BODY\n"
45"[pipe|queue] {zero|delete|show} [N{,N}]\n"
46"nat N config {ip IPADDR|if IFNAME|log|deny_in|same_ports|unreg_only|reset|\n"
47" reverse|proxy_only|redirect_addr linkspec|\n"
48" redirect_port linkspec|redirect_proto linkspec}\n"
49"set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n"
50"set N {show|list|zero|resetlog|delete} [N{,N}] | flush\n"
51"table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n"
52"table all {flush | list}\n"
53"\n"
54"RULE-BODY: check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n"
55"ACTION: check-state | allow | count | deny | unreach{,6} CODE |\n"
56" skipto N | {divert|tee} PORT | forward ADDR |\n"
57" pipe N | queue N | nat N | setfib FIB | reass\n"
58"PARAMS: [log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n"
59"ADDR: [ MAC dst src ether_type ] \n"
60" [ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n"
61" [ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n"
62"IPADDR: [not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n"
63"IP6ADDR: [not] { any | me | me6 | ip6/bits | IP6LIST }\n"
64"IP6LIST: { ip6 | ip6/bits }[,IP6LIST]\n"
65"IPLIST: { ip | ip/bits | ip:mask }[,IPLIST]\n"
66"OPTION_LIST: OPTION [OPTION_LIST]\n"
67"OPTION: bridged | diverted | diverted-loopback | diverted-output |\n"
68" {dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n"
69" {dst-port|src-port} LIST |\n"
70" estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n"
71" iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n"
72" ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n"
73" icmp6types LIST | ext6hdr LIST | flow-id N[,N] | fib FIB |\n"
74" mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n"
75" setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n"
76" tcpdatalen LIST | verrevpath | versrcreach | antispoof\n"
77);
78
79 exit(0);
80}
81
82/*
83 * Free a the (locally allocated) copy of command line arguments.
84 */
85static void
86free_args(int ac, char **av)
87{
88 int i;
89
90 for (i=0; i < ac; i++)
91 free(av[i]);
92 free(av);
93}
94
95/*
96 * Called with the arguments, including program name because getopt
97 * wants it to be present.
98 * Returns 0 if successful, 1 if empty command, errx() in case of errors.
83 * Called with the arguments, including program name because getopt
84 * wants it to be present.
85 * Returns 0 if successful, 1 if empty command, errx() in case of errors.
86 * First thing we do is process parameters creating an argv[] array
87 * which includes the program name and a NULL entry at the end.
88 * If we are called with a single string, we split it on whitespace.
89 * Also, arguments with a trailing ',' are joined to the next one.
90 * The pointers (av[]) and data are in a a single chunk of memory.
91 * av[0] points to the original program name, all other entries
92 * point into the allocated chunk.
99 */
100static int
101ipfw_main(int oldac, char **oldav)
102{
93 */
94static int
95ipfw_main(int oldac, char **oldav)
96{
103 int ch, ac, save_ac;
97 int ch, ac;
104 const char *errstr;
105 char **av, **save_av;
106 int do_acct = 0; /* Show packet/byte count */
107 int try_next = 0; /* set if pipe cmd not found */
98 const char *errstr;
99 char **av, **save_av;
100 int do_acct = 0; /* Show packet/byte count */
101 int try_next = 0; /* set if pipe cmd not found */
102 int av_size; /* compute the av size */
103 char *av_p; /* used to build the av list */
108
109#define WHITESP " \t\f\v\n\r"
110 if (oldac < 2)
111 return 1; /* need at least one argument */
112
113 if (oldac == 2) {
114 /*
104
105#define WHITESP " \t\f\v\n\r"
106 if (oldac < 2)
107 return 1; /* need at least one argument */
108
109 if (oldac == 2) {
110 /*
115 * If we are called with a single string, try to split it into
116 * arguments for subsequent parsing.
117 * But first, remove spaces after a ',', by copying the string
118 * in-place.
111 * If we are called with one argument, try to split it into
112 * words for subsequent parsing. Spaces after a ',' are
113 * removed by copying the string in-place.
119 */
120 char *arg = oldav[1]; /* The string is the first arg. */
121 int l = strlen(arg);
122 int copy = 0; /* 1 if we need to copy, 0 otherwise */
123 int i, j;
124
125 for (i = j = 0; i < l; i++) {
126 if (arg[i] == '#') /* comment marker */
127 break;
128 if (copy) {
129 arg[j++] = arg[i];
130 copy = !index("," WHITESP, arg[i]);
131 } else {
132 copy = !index(WHITESP, arg[i]);
133 if (copy)
134 arg[j++] = arg[i];
135 }
136 }
137 if (!copy && j > 0) /* last char was a 'blank', remove it */
138 j--;
139 l = j; /* the new argument length */
140 arg[j++] = '\0';
141 if (l == 0) /* empty string! */
142 return 1;
143
144 /*
145 * First, count number of arguments. Because of the previous
146 * processing, this is just the number of blanks plus 1.
147 */
148 for (i = 0, ac = 1; i < l; i++)
149 if (index(WHITESP, arg[i]) != NULL)
150 ac++;
151
152 /*
114 */
115 char *arg = oldav[1]; /* The string is the first arg. */
116 int l = strlen(arg);
117 int copy = 0; /* 1 if we need to copy, 0 otherwise */
118 int i, j;
119
120 for (i = j = 0; i < l; i++) {
121 if (arg[i] == '#') /* comment marker */
122 break;
123 if (copy) {
124 arg[j++] = arg[i];
125 copy = !index("," WHITESP, arg[i]);
126 } else {
127 copy = !index(WHITESP, arg[i]);
128 if (copy)
129 arg[j++] = arg[i];
130 }
131 }
132 if (!copy && j > 0) /* last char was a 'blank', remove it */
133 j--;
134 l = j; /* the new argument length */
135 arg[j++] = '\0';
136 if (l == 0) /* empty string! */
137 return 1;
138
139 /*
140 * First, count number of arguments. Because of the previous
141 * processing, this is just the number of blanks plus 1.
142 */
143 for (i = 0, ac = 1; i < l; i++)
144 if (index(WHITESP, arg[i]) != NULL)
145 ac++;
146
147 /*
153 * Allocate the argument list, including one entry for
154 * the program name because getopt expects it.
148 * Allocate the argument list structure as a single block
149 * of memory, containing pointers and the argument
150 * strings. We include one entry for the program name
151 * because getopt expects it, and a NULL at the end
152 * to simplify further parsing.
155 */
153 */
156 av = safe_calloc(ac + 1, sizeof(char *));
154 ac++; /* add 1 for the program name */
155 av_size = (ac+1) * sizeof(char *) + l + 1;
156 av = safe_calloc(av_size, 1);
157
158 /*
157
158 /*
159 * Second, copy arguments from arg[] to av[]. For each one,
159 * Init the argument pointer to the end of the array
160 * and copy arguments from arg[] to av[]. For each one,
160 * j is the initial character, i is the one past the end.
161 */
161 * j is the initial character, i is the one past the end.
162 */
162 for (ac = 1, i = j = 0; i < l; i++)
163 av_p = (char *)&av[ac+1];
164 for (ac = 1, i = j = 0; i < l; i++) {
163 if (index(WHITESP, arg[i]) != NULL || i == l-1) {
164 if (i == l-1)
165 i++;
165 if (index(WHITESP, arg[i]) != NULL || i == l-1) {
166 if (i == l-1)
167 i++;
166 av[ac] = safe_calloc(i-j+1, 1);
167 bcopy(arg+j, av[ac], i-j);
168 bcopy(arg+j, av_p, i-j);
169 av[ac] = av_p;
170 av_p += i-j; /* the lenght of the string */
171 *av_p++ = '\0';
168 ac++;
169 j = i + 1;
170 }
172 ac++;
173 j = i + 1;
174 }
175 }
171 } else {
172 /*
173 * If an argument ends with ',' join with the next one.
174 */
176 } else {
177 /*
178 * If an argument ends with ',' join with the next one.
179 */
175 int first, i, l;
180 int first, i, l=0;
176
181
177 av = safe_calloc(oldac, sizeof(char *));
182 /*
183 * Allocate the argument list structure as a single block
184 * of memory, containing both pointers and the argument
185 * strings. We include some space for the program name
186 * because getopt expects it.
187 * We add an extra pointer to the end of the array,
188 * to make simpler further parsing.
189 */
190 for (i=0; i<oldac; i++)
191 l += strlen(oldav[i]);
192
193 av_size = (oldac+1) * sizeof(char *) + l + oldac;
194 av = safe_calloc(av_size, 1);
195
196 /*
197 * Init the argument pointer to the end of the array
198 * and copy arguments from arg[] to av[]
199 */
200 av_p = (char *)&av[oldac+1];
178 for (first = i = ac = 1, l = 0; i < oldac; i++) {
179 char *arg = oldav[i];
180 int k = strlen(arg);
181
182 l += k;
183 if (arg[k-1] != ',' || i == oldac-1) {
184 /* Time to copy. */
201 for (first = i = ac = 1, l = 0; i < oldac; i++) {
202 char *arg = oldav[i];
203 int k = strlen(arg);
204
205 l += k;
206 if (arg[k-1] != ',' || i == oldac-1) {
207 /* Time to copy. */
185 av[ac] = safe_calloc(l+1, 1);
208 av[ac] = av_p;
186 for (l=0; first <= i; first++) {
209 for (l=0; first <= i; first++) {
187 strcat(av[ac]+l, oldav[first]);
188 l += strlen(oldav[first]);
210 strcat(av_p, oldav[first]);
211 av_p += strlen(oldav[first]);
189 }
212 }
213 *av_p++ = '\0';
190 ac++;
191 l = 0;
192 first = i+1;
193 }
194 }
195 }
196
214 ac++;
215 l = 0;
216 first = i+1;
217 }
218 }
219 }
220
197 av[0] = strdup(oldav[0]); /* copy progname from the caller */
221 /*
222 * set the progname pointer to the original string
223 * and terminate the array with null
224 */
225 av[0] = oldav[0];
226 av[ac] = NULL;
227
198 /* Set the force flag for non-interactive processes */
199 if (!co.do_force)
200 co.do_force = !isatty(STDIN_FILENO);
201
228 /* Set the force flag for non-interactive processes */
229 if (!co.do_force)
230 co.do_force = !isatty(STDIN_FILENO);
231
232#ifdef EMULATE_SYSCTL /* sysctl emulation */
233 if ( ac >= 2 && !strcmp(av[1], "sysctl")) {
234 char *s;
235 int i;
236
237 if (ac != 3) {
238 printf( "sysctl emulation usage:\n"
239 " ipfw sysctl name[=value]\n"
240 " ipfw sysctl -a\n");
241 return 0;
242 }
243 s = index(av[2], '=');
244 if (s == NULL) {
245 s = !strcmp(av[2], "-a") ? NULL : av[2];
246 sysctlbyname(s, NULL, NULL, NULL, 0);
247 } else { /* ipfw sysctl x.y.z=value */
248 /* assume an INT value, will extend later */
249 if (s[1] == '\0') {
250 printf("ipfw sysctl: missing value\n\n");
251 return 0;
252 }
253 *s = '\0';
254 i = strtol(s+1, NULL, 0);
255 sysctlbyname(av[2], NULL, NULL, &i, sizeof(int));
256 }
257 return 0;
258 }
259#endif
260
202 /* Save arguments for final freeing of memory. */
261 /* Save arguments for final freeing of memory. */
203 save_ac = ac;
204 save_av = av;
205
206 optind = optreset = 1; /* restart getopt() */
207 while ((ch = getopt(ac, av, "abcdefhinNqs:STtv")) != -1)
208 switch (ch) {
209 case 'a':
210 do_acct = 1;
211 break;
212
213 case 'b':
214 co.comment_only = 1;
215 co.do_compact = 1;
216 break;
217
218 case 'c':
219 co.do_compact = 1;
220 break;
221
222 case 'd':
223 co.do_dynamic = 1;
224 break;
225
226 case 'e':
227 co.do_expired = 1;
228 break;
229
230 case 'f':
231 co.do_force = 1;
232 break;
233
234 case 'h': /* help */
262 save_av = av;
263
264 optind = optreset = 1; /* restart getopt() */
265 while ((ch = getopt(ac, av, "abcdefhinNqs:STtv")) != -1)
266 switch (ch) {
267 case 'a':
268 do_acct = 1;
269 break;
270
271 case 'b':
272 co.comment_only = 1;
273 co.do_compact = 1;
274 break;
275
276 case 'c':
277 co.do_compact = 1;
278 break;
279
280 case 'd':
281 co.do_dynamic = 1;
282 break;
283
284 case 'e':
285 co.do_expired = 1;
286 break;
287
288 case 'f':
289 co.do_force = 1;
290 break;
291
292 case 'h': /* help */
235 free_args(save_ac, save_av);
293 free(save_av);
236 help();
237 break; /* NOTREACHED */
238
239 case 'i':
240 co.do_value_as_ip = 1;
241 break;
242
243 case 'n':
244 co.test_only = 1;
245 break;
246
247 case 'N':
248 co.do_resolv = 1;
249 break;
250
251 case 'q':
252 co.do_quiet = 1;
253 break;
254
255 case 's': /* sort */
256 co.do_sort = atoi(optarg);
257 break;
258
259 case 'S':
260 co.show_sets = 1;
261 break;
262
263 case 't':
264 co.do_time = 1;
265 break;
266
267 case 'T':
268 co.do_time = 2; /* numeric timestamp */
269 break;
270
271 case 'v': /* verbose */
272 co.verbose = 1;
273 break;
274
275 default:
294 help();
295 break; /* NOTREACHED */
296
297 case 'i':
298 co.do_value_as_ip = 1;
299 break;
300
301 case 'n':
302 co.test_only = 1;
303 break;
304
305 case 'N':
306 co.do_resolv = 1;
307 break;
308
309 case 'q':
310 co.do_quiet = 1;
311 break;
312
313 case 's': /* sort */
314 co.do_sort = atoi(optarg);
315 break;
316
317 case 'S':
318 co.show_sets = 1;
319 break;
320
321 case 't':
322 co.do_time = 1;
323 break;
324
325 case 'T':
326 co.do_time = 2; /* numeric timestamp */
327 break;
328
329 case 'v': /* verbose */
330 co.verbose = 1;
331 break;
332
333 default:
276 free_args(save_ac, save_av);
334 free(save_av);
277 return 1;
278 }
279
280 ac -= optind;
281 av += optind;
282 NEED1("bad arguments, for usage summary ``ipfw''");
283
284 /*
285 * An undocumented behaviour of ipfw1 was to allow rule numbers first,
286 * e.g. "100 add allow ..." instead of "add 100 allow ...".
287 * In case, swap first and second argument to get the normal form.
288 */
289 if (ac > 1 && isdigit(*av[0])) {
290 char *p = av[0];
291
292 av[0] = av[1];
293 av[1] = p;
294 }
295
296 /*
297 * Optional: pipe, queue or nat.
298 */
299 co.do_nat = 0;
300 co.do_pipe = 0;
301 if (!strncmp(*av, "nat", strlen(*av)))
302 co.do_nat = 1;
303 else if (!strncmp(*av, "pipe", strlen(*av)))
304 co.do_pipe = 1;
305 else if (_substrcmp(*av, "queue") == 0)
306 co.do_pipe = 2;
335 return 1;
336 }
337
338 ac -= optind;
339 av += optind;
340 NEED1("bad arguments, for usage summary ``ipfw''");
341
342 /*
343 * An undocumented behaviour of ipfw1 was to allow rule numbers first,
344 * e.g. "100 add allow ..." instead of "add 100 allow ...".
345 * In case, swap first and second argument to get the normal form.
346 */
347 if (ac > 1 && isdigit(*av[0])) {
348 char *p = av[0];
349
350 av[0] = av[1];
351 av[1] = p;
352 }
353
354 /*
355 * Optional: pipe, queue or nat.
356 */
357 co.do_nat = 0;
358 co.do_pipe = 0;
359 if (!strncmp(*av, "nat", strlen(*av)))
360 co.do_nat = 1;
361 else if (!strncmp(*av, "pipe", strlen(*av)))
362 co.do_pipe = 1;
363 else if (_substrcmp(*av, "queue") == 0)
364 co.do_pipe = 2;
365 else if (_substrcmp(*av, "flowset") == 0)
366 co.do_pipe = 2;
367 else if (_substrcmp(*av, "sched") == 0)
368 co.do_pipe = 3;
307 else if (!strncmp(*av, "set", strlen(*av))) {
308 if (ac > 1 && isdigit(av[1][0])) {
309 co.use_set = strtonum(av[1], 0, resvd_set_number,
310 &errstr);
311 if (errstr)
312 errx(EX_DATAERR,
313 "invalid set number %s\n", av[1]);
314 ac -= 2; av += 2; co.use_set++;
315 }
316 }
317
318 if (co.do_pipe || co.do_nat) {
319 ac--;
320 av++;
321 }
322 NEED1("missing command");
323
324 /*
325 * For pipes, queues and nats we normally say 'nat|pipe NN config'
326 * but the code is easier to parse as 'nat|pipe config NN'
327 * so we swap the two arguments.
328 */
329 if ((co.do_pipe || co.do_nat) && ac > 1 && isdigit(*av[0])) {
330 char *p = av[0];
331
332 av[0] = av[1];
333 av[1] = p;
334 }
335
336 if (co.use_set == 0) {
337 if (_substrcmp(*av, "add") == 0)
369 else if (!strncmp(*av, "set", strlen(*av))) {
370 if (ac > 1 && isdigit(av[1][0])) {
371 co.use_set = strtonum(av[1], 0, resvd_set_number,
372 &errstr);
373 if (errstr)
374 errx(EX_DATAERR,
375 "invalid set number %s\n", av[1]);
376 ac -= 2; av += 2; co.use_set++;
377 }
378 }
379
380 if (co.do_pipe || co.do_nat) {
381 ac--;
382 av++;
383 }
384 NEED1("missing command");
385
386 /*
387 * For pipes, queues and nats we normally say 'nat|pipe NN config'
388 * but the code is easier to parse as 'nat|pipe config NN'
389 * so we swap the two arguments.
390 */
391 if ((co.do_pipe || co.do_nat) && ac > 1 && isdigit(*av[0])) {
392 char *p = av[0];
393
394 av[0] = av[1];
395 av[1] = p;
396 }
397
398 if (co.use_set == 0) {
399 if (_substrcmp(*av, "add") == 0)
338 ipfw_add(ac, av);
400 ipfw_add(av);
339 else if (co.do_nat && _substrcmp(*av, "show") == 0)
340 ipfw_show_nat(ac, av);
341 else if (co.do_pipe && _substrcmp(*av, "config") == 0)
342 ipfw_config_pipe(ac, av);
343 else if (co.do_nat && _substrcmp(*av, "config") == 0)
344 ipfw_config_nat(ac, av);
345 else if (_substrcmp(*av, "set") == 0)
401 else if (co.do_nat && _substrcmp(*av, "show") == 0)
402 ipfw_show_nat(ac, av);
403 else if (co.do_pipe && _substrcmp(*av, "config") == 0)
404 ipfw_config_pipe(ac, av);
405 else if (co.do_nat && _substrcmp(*av, "config") == 0)
406 ipfw_config_nat(ac, av);
407 else if (_substrcmp(*av, "set") == 0)
346 ipfw_sets_handler(ac, av);
408 ipfw_sets_handler(av);
347 else if (_substrcmp(*av, "table") == 0)
348 ipfw_table_handler(ac, av);
349 else if (_substrcmp(*av, "enable") == 0)
409 else if (_substrcmp(*av, "table") == 0)
410 ipfw_table_handler(ac, av);
411 else if (_substrcmp(*av, "enable") == 0)
350 ipfw_sysctl_handler(ac, av, 1);
412 ipfw_sysctl_handler(av, 1);
351 else if (_substrcmp(*av, "disable") == 0)
413 else if (_substrcmp(*av, "disable") == 0)
352 ipfw_sysctl_handler(ac, av, 0);
414 ipfw_sysctl_handler(av, 0);
353 else
354 try_next = 1;
355 }
356
357 if (co.use_set || try_next) {
358 if (_substrcmp(*av, "delete") == 0)
415 else
416 try_next = 1;
417 }
418
419 if (co.use_set || try_next) {
420 if (_substrcmp(*av, "delete") == 0)
359 ipfw_delete(ac, av);
421 ipfw_delete(av);
360 else if (_substrcmp(*av, "flush") == 0)
361 ipfw_flush(co.do_force);
362 else if (_substrcmp(*av, "zero") == 0)
363 ipfw_zero(ac, av, 0 /* IP_FW_ZERO */);
364 else if (_substrcmp(*av, "resetlog") == 0)
365 ipfw_zero(ac, av, 1 /* IP_FW_RESETLOG */);
366 else if (_substrcmp(*av, "print") == 0 ||
367 _substrcmp(*av, "list") == 0)
368 ipfw_list(ac, av, do_acct);
369 else if (_substrcmp(*av, "show") == 0)
370 ipfw_list(ac, av, 1 /* show counters */);
371 else
372 errx(EX_USAGE, "bad command `%s'", *av);
373 }
374
375 /* Free memory allocated in the argument parsing. */
422 else if (_substrcmp(*av, "flush") == 0)
423 ipfw_flush(co.do_force);
424 else if (_substrcmp(*av, "zero") == 0)
425 ipfw_zero(ac, av, 0 /* IP_FW_ZERO */);
426 else if (_substrcmp(*av, "resetlog") == 0)
427 ipfw_zero(ac, av, 1 /* IP_FW_RESETLOG */);
428 else if (_substrcmp(*av, "print") == 0 ||
429 _substrcmp(*av, "list") == 0)
430 ipfw_list(ac, av, do_acct);
431 else if (_substrcmp(*av, "show") == 0)
432 ipfw_list(ac, av, 1 /* show counters */);
433 else
434 errx(EX_USAGE, "bad command `%s'", *av);
435 }
436
437 /* Free memory allocated in the argument parsing. */
376 free_args(save_ac, save_av);
438 free(save_av);
377 return 0;
378}
379
380
381static void
382ipfw_readfile(int ac, char *av[])
383{
384#define MAX_ARGS 32
385 char buf[BUFSIZ];
386 char *progname = av[0]; /* original program name */
387 const char *cmd = NULL; /* preprocessor name, if any */
388 const char *filename = av[ac-1]; /* file to read */
389 int c, lineno=0;
390 FILE *f = NULL;
391 pid_t preproc = 0;
392
393 while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
394 switch(c) {
395 case 'c':
396 co.do_compact = 1;
397 break;
398
399 case 'f':
400 co.do_force = 1;
401 break;
402
403 case 'N':
404 co.do_resolv = 1;
405 break;
406
407 case 'n':
408 co.test_only = 1;
409 break;
410
411 case 'p':
412 /*
413 * ipfw -p cmd [args] filename
414 *
415 * We are done with getopt(). All arguments
416 * except the filename go to the preprocessor,
417 * so we need to do the following:
418 * - check that a filename is actually present;
419 * - advance av by optind-1 to skip arguments
420 * already processed;
421 * - decrease ac by optind, to remove the args
422 * already processed and the final filename;
423 * - set the last entry in av[] to NULL so
424 * popen() can detect the end of the array;
425 * - set optind=ac to let getopt() terminate.
426 */
427 if (optind == ac)
428 errx(EX_USAGE, "no filename argument");
429 cmd = optarg;
430 av[ac-1] = NULL;
431 av += optind - 1;
432 ac -= optind;
433 optind = ac;
434 break;
435
436 case 'q':
437 co.do_quiet = 1;
438 break;
439
440 case 'S':
441 co.show_sets = 1;
442 break;
443
444 default:
445 errx(EX_USAGE, "bad arguments, for usage"
446 " summary ``ipfw''");
447 }
448
449 }
450
451 if (cmd == NULL && ac != optind + 1)
452 errx(EX_USAGE, "extraneous filename arguments %s", av[ac-1]);
453
454 if ((f = fopen(filename, "r")) == NULL)
455 err(EX_UNAVAILABLE, "fopen: %s", filename);
456
457 if (cmd != NULL) { /* pipe through preprocessor */
458 int pipedes[2];
459
460 if (pipe(pipedes) == -1)
461 err(EX_OSERR, "cannot create pipe");
462
463 preproc = fork();
464 if (preproc == -1)
465 err(EX_OSERR, "cannot fork");
466
467 if (preproc == 0) {
468 /*
469 * Child, will run the preprocessor with the
470 * file on stdin and the pipe on stdout.
471 */
472 if (dup2(fileno(f), 0) == -1
473 || dup2(pipedes[1], 1) == -1)
474 err(EX_OSERR, "dup2()");
475 fclose(f);
476 close(pipedes[1]);
477 close(pipedes[0]);
478 execvp(cmd, av);
479 err(EX_OSERR, "execvp(%s) failed", cmd);
480 } else { /* parent, will reopen f as the pipe */
481 fclose(f);
482 close(pipedes[1]);
483 if ((f = fdopen(pipedes[0], "r")) == NULL) {
484 int savederrno = errno;
485
486 (void)kill(preproc, SIGTERM);
487 errno = savederrno;
488 err(EX_OSERR, "fdopen()");
489 }
490 }
491 }
492
493 while (fgets(buf, BUFSIZ, f)) { /* read commands */
494 char linename[10];
495 char *args[2];
496
497 lineno++;
498 sprintf(linename, "Line %d", lineno);
499 setprogname(linename); /* XXX */
500 args[0] = progname;
501 args[1] = buf;
502 ipfw_main(2, args);
503 }
504 fclose(f);
505 if (cmd != NULL) {
506 int status;
507
508 if (waitpid(preproc, &status, 0) == -1)
509 errx(EX_OSERR, "waitpid()");
510 if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
511 errx(EX_UNAVAILABLE,
512 "preprocessor exited with status %d",
513 WEXITSTATUS(status));
514 else if (WIFSIGNALED(status))
515 errx(EX_UNAVAILABLE,
516 "preprocessor exited with signal %d",
517 WTERMSIG(status));
518 }
519}
520
521int
522main(int ac, char *av[])
523{
524 /*
525 * If the last argument is an absolute pathname, interpret it
526 * as a file to be preprocessed.
527 */
528
529 if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0)
530 ipfw_readfile(ac, av);
531 else {
532 if (ipfw_main(ac, av)) {
533 errx(EX_USAGE,
534 "usage: ipfw [options]\n"
535 "do \"ipfw -h\" or \"man ipfw\" for details");
536 }
537 }
538 return EX_OK;
539}
439 return 0;
440}
441
442
443static void
444ipfw_readfile(int ac, char *av[])
445{
446#define MAX_ARGS 32
447 char buf[BUFSIZ];
448 char *progname = av[0]; /* original program name */
449 const char *cmd = NULL; /* preprocessor name, if any */
450 const char *filename = av[ac-1]; /* file to read */
451 int c, lineno=0;
452 FILE *f = NULL;
453 pid_t preproc = 0;
454
455 while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
456 switch(c) {
457 case 'c':
458 co.do_compact = 1;
459 break;
460
461 case 'f':
462 co.do_force = 1;
463 break;
464
465 case 'N':
466 co.do_resolv = 1;
467 break;
468
469 case 'n':
470 co.test_only = 1;
471 break;
472
473 case 'p':
474 /*
475 * ipfw -p cmd [args] filename
476 *
477 * We are done with getopt(). All arguments
478 * except the filename go to the preprocessor,
479 * so we need to do the following:
480 * - check that a filename is actually present;
481 * - advance av by optind-1 to skip arguments
482 * already processed;
483 * - decrease ac by optind, to remove the args
484 * already processed and the final filename;
485 * - set the last entry in av[] to NULL so
486 * popen() can detect the end of the array;
487 * - set optind=ac to let getopt() terminate.
488 */
489 if (optind == ac)
490 errx(EX_USAGE, "no filename argument");
491 cmd = optarg;
492 av[ac-1] = NULL;
493 av += optind - 1;
494 ac -= optind;
495 optind = ac;
496 break;
497
498 case 'q':
499 co.do_quiet = 1;
500 break;
501
502 case 'S':
503 co.show_sets = 1;
504 break;
505
506 default:
507 errx(EX_USAGE, "bad arguments, for usage"
508 " summary ``ipfw''");
509 }
510
511 }
512
513 if (cmd == NULL && ac != optind + 1)
514 errx(EX_USAGE, "extraneous filename arguments %s", av[ac-1]);
515
516 if ((f = fopen(filename, "r")) == NULL)
517 err(EX_UNAVAILABLE, "fopen: %s", filename);
518
519 if (cmd != NULL) { /* pipe through preprocessor */
520 int pipedes[2];
521
522 if (pipe(pipedes) == -1)
523 err(EX_OSERR, "cannot create pipe");
524
525 preproc = fork();
526 if (preproc == -1)
527 err(EX_OSERR, "cannot fork");
528
529 if (preproc == 0) {
530 /*
531 * Child, will run the preprocessor with the
532 * file on stdin and the pipe on stdout.
533 */
534 if (dup2(fileno(f), 0) == -1
535 || dup2(pipedes[1], 1) == -1)
536 err(EX_OSERR, "dup2()");
537 fclose(f);
538 close(pipedes[1]);
539 close(pipedes[0]);
540 execvp(cmd, av);
541 err(EX_OSERR, "execvp(%s) failed", cmd);
542 } else { /* parent, will reopen f as the pipe */
543 fclose(f);
544 close(pipedes[1]);
545 if ((f = fdopen(pipedes[0], "r")) == NULL) {
546 int savederrno = errno;
547
548 (void)kill(preproc, SIGTERM);
549 errno = savederrno;
550 err(EX_OSERR, "fdopen()");
551 }
552 }
553 }
554
555 while (fgets(buf, BUFSIZ, f)) { /* read commands */
556 char linename[10];
557 char *args[2];
558
559 lineno++;
560 sprintf(linename, "Line %d", lineno);
561 setprogname(linename); /* XXX */
562 args[0] = progname;
563 args[1] = buf;
564 ipfw_main(2, args);
565 }
566 fclose(f);
567 if (cmd != NULL) {
568 int status;
569
570 if (waitpid(preproc, &status, 0) == -1)
571 errx(EX_OSERR, "waitpid()");
572 if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
573 errx(EX_UNAVAILABLE,
574 "preprocessor exited with status %d",
575 WEXITSTATUS(status));
576 else if (WIFSIGNALED(status))
577 errx(EX_UNAVAILABLE,
578 "preprocessor exited with signal %d",
579 WTERMSIG(status));
580 }
581}
582
583int
584main(int ac, char *av[])
585{
586 /*
587 * If the last argument is an absolute pathname, interpret it
588 * as a file to be preprocessed.
589 */
590
591 if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0)
592 ipfw_readfile(ac, av);
593 else {
594 if (ipfw_main(ac, av)) {
595 errx(EX_USAGE,
596 "usage: ipfw [options]\n"
597 "do \"ipfw -h\" or \"man ipfw\" for details");
598 }
599 }
600 return EX_OK;
601}