Deleted Added
sdiff udiff text old ( 190633 ) new ( 204591 )
full compact
1/*
2 * Copyright (c) 2002-2003 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 *
20 * $FreeBSD: head/sbin/ipfw/main.c 190633 2009-04-01 20:23:47Z piso $
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>

--- 46 unchanged lines hidden (view full) ---

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.
99 */
100static int
101ipfw_main(int oldac, char **oldav)
102{
103 int ch, ac, save_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 */
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 /*
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.
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 */

--- 18 unchanged lines hidden (view full) ---

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 /*
153 * Allocate the argument list, including one entry for
154 * the program name because getopt expects it.
155 */
156 av = safe_calloc(ac + 1, sizeof(char *));
157
158 /*
159 * Second, copy arguments from arg[] to av[]. For each one,
160 * j is the initial character, i is the one past the end.
161 */
162 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++;
166 av[ac] = safe_calloc(i-j+1, 1);
167 bcopy(arg+j, av[ac], i-j);
168 ac++;
169 j = i + 1;
170 }
171 } else {
172 /*
173 * If an argument ends with ',' join with the next one.
174 */
175 int first, i, l;
176
177 av = safe_calloc(oldac, sizeof(char *));
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. */
185 av[ac] = safe_calloc(l+1, 1);
186 for (l=0; first <= i; first++) {
187 strcat(av[ac]+l, oldav[first]);
188 l += strlen(oldav[first]);
189 }
190 ac++;
191 l = 0;
192 first = i+1;
193 }
194 }
195 }
196
197 av[0] = strdup(oldav[0]); /* copy progname from the caller */
198 /* Set the force flag for non-interactive processes */
199 if (!co.do_force)
200 co.do_force = !isatty(STDIN_FILENO);
201
202 /* 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;

--- 15 unchanged lines hidden (view full) ---

227 co.do_expired = 1;
228 break;
229
230 case 'f':
231 co.do_force = 1;
232 break;
233
234 case 'h': /* help */
235 free_args(save_ac, save_av);
236 help();
237 break; /* NOTREACHED */
238
239 case 'i':
240 co.do_value_as_ip = 1;
241 break;
242
243 case 'n':

--- 24 unchanged lines hidden (view full) ---

268 co.do_time = 2; /* numeric timestamp */
269 break;
270
271 case 'v': /* verbose */
272 co.verbose = 1;
273 break;
274
275 default:
276 free_args(save_ac, save_av);
277 return 1;
278 }
279
280 ac -= optind;
281 av += optind;
282 NEED1("bad arguments, for usage summary ``ipfw''");
283
284 /*

--- 14 unchanged lines hidden (view full) ---

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;
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++;

--- 15 unchanged lines hidden (view full) ---

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)
338 ipfw_add(ac, 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)
346 ipfw_sets_handler(ac, av);
347 else if (_substrcmp(*av, "table") == 0)
348 ipfw_table_handler(ac, av);
349 else if (_substrcmp(*av, "enable") == 0)
350 ipfw_sysctl_handler(ac, av, 1);
351 else if (_substrcmp(*av, "disable") == 0)
352 ipfw_sysctl_handler(ac, av, 0);
353 else
354 try_next = 1;
355 }
356
357 if (co.use_set || try_next) {
358 if (_substrcmp(*av, "delete") == 0)
359 ipfw_delete(ac, 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. */
376 free_args(save_ac, save_av);
377 return 0;
378}
379
380
381static void
382ipfw_readfile(int ac, char *av[])
383{
384#define MAX_ARGS 32

--- 155 unchanged lines hidden ---