]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/getipstat.c
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / src / misc-progs / getipstat.c
1 /* IPFire helper program - IPStat
2 *
3 * Get the list from IPTABLES -L
4 *
5 * Optional commandline parameters:
6 * -x
7 * instruct iptables to expand numbers
8 * -f
9 * display filter table
10 * -n
11 * display nat table
12 * -m
13 * display mangle table
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include "setuid.h"
22
23 int main(int argc, char** argv)
24 {
25 // Set defaults
26 // first argument has to be "iptables" since execve executes the program pointed to by filename
27 // but /sbin/iptables is actually a symlink to /sbin/xtables-legacy-multi hence that program is executed
28 // however without the notion that it was called as "iptables". So we have to pass "iptables" as first
29 // argument.
30 char *args[] = {"--list", "--verbose", "--numeric", "--wait", "5", NULL, NULL, NULL, NULL};
31 char *usage = "getipstat [-x][-f|-n|-m]";
32 unsigned int pcount = 5;
33 unsigned int table_set = 0;
34
35 int opt;
36
37 if (!(initsetuid()))
38 exit(1);
39
40 // Parse command line arguments
41 if (argc > 1) {
42 while ((opt = getopt(argc, argv, "xfnm")) != -1) {
43 switch(opt) {
44 case 'x':
45 args[pcount++] = "--exact";
46 break;
47 case 'f':
48 table_set++;
49 break;
50 case 'n':
51 if (table_set == 0) {
52 args[pcount++] = "--table";
53 args[pcount++] = "nat";
54 }
55 table_set++;
56 break;
57 case 'm':
58 if (table_set == 0) {
59 args[pcount++] = "--table";
60 args[pcount++] = "mangle";
61 }
62 table_set++;
63 break;
64 default:
65 fprintf(stderr, "\nBad argument given.\n\n%s\n", usage);
66 exit(1);
67 }
68 }
69 if (table_set > 1) {
70 fprintf(stderr, "\nArguments -f/-n/-m are mutualy exclusive.\n\n%s\n", usage);
71 exit(1);
72 }
73 }
74
75 return run("/sbin/iptables", args);
76 }
77