]> git.ipfire.org Git - ipfire-2.x.git/blame - src/misc-progs/getipstat.c
Merge branch 'next'
[ipfire-2.x.git] / src / misc-progs / getipstat.c
CommitLineData
a68fedca
MT
1/* IPFire helper program - IPStat
2 *
3 * Get the list from IPTABLES -L
4 *
8b68ed12
RR
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
a68fedca 14 */
77cdccf4 15
a68fedca
MT
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19#include <stdlib.h>
20#include <sys/types.h>
a68fedca
MT
21#include "setuid.h"
22
8b68ed12 23int main(int argc, char** argv)
a68fedca 24{
8b68ed12
RR
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[10] = {"iptables", "--list", "--verbose", "--numeric", "--wait", "5", NULL, NULL, NULL, NULL};
31 char *usage = "getipstat [-x][-f|-n|-m]";
32 unsigned int pcount = 6;
33 unsigned int table_set = 0;
34
35 int opt;
36
a68fedca
MT
37 if (!(initsetuid()))
38 exit(1);
77cdccf4 39
8b68ed12
RR
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);
a68fedca
MT
76}
77