blob: 6252c0b0598f3b9c64a1100676a950ede11e4549 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to save the iptables state, in human readable-form. */
Harald Welte10a907f2002-08-07 09:07:41 +00002/* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This code is distributed under the terms of GNU GPL v2
6 *
Harald Welteae1ff9f2000-12-01 14:26:20 +00007 */
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000011#include <fcntl.h>
12#include <stdlib.h>
13#include <string.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000014#include <time.h>
Harald Welted8ac9672004-04-15 10:10:19 +000015#include <netdb.h>
Rusty Russellb1f69be2000-08-27 07:42:54 +000016#include "libiptc/libiptc.h"
17#include "iptables.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010018#include "iptables-multi.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000019
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000020#ifndef NO_SHARED_LIBS
21#include <dlfcn.h>
22#endif
23
Jan Engelhardtdbb77542008-02-11 00:33:30 +010024static int show_binary = 0, show_counters = 0;
Rusty Russella8f033e2000-07-30 01:43:01 +000025
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010026static const struct option options[] = {
27 {.name = "binary", .has_arg = false, .val = 'b'},
28 {.name = "counters", .has_arg = false, .val = 'c'},
29 {.name = "dump", .has_arg = false, .val = 'd'},
30 {.name = "table", .has_arg = true, .val = 't'},
31 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000032};
33
Marc Bouchere6869a82000-03-20 06:03:29 +000034/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +000035static int for_each_table(int (*func)(const char *tablename))
36{
Max Kellermann5b76f682008-01-29 13:42:48 +000037 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +000038 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +000039 char tablename[IPT_TABLE_MAXNAMELEN+1];
40
Harald Welteae1ff9f2000-12-01 14:26:20 +000041 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +000042 if (!procfile)
Victor Stinner65334ad2007-10-18 14:27:03 +000043 exit_error(OTHER_PROBLEM,
44 "Unable to open /proc/net/ip_tables_names: %s\n",
45 strerror(errno));
Rusty Russella8f033e2000-07-30 01:43:01 +000046
47 while (fgets(tablename, sizeof(tablename), procfile)) {
48 if (tablename[strlen(tablename) - 1] != '\n')
49 exit_error(OTHER_PROBLEM,
50 "Badly formed tablename `%s'\n",
51 tablename);
52 tablename[strlen(tablename) - 1] = '\0';
53 ret &= func(tablename);
54 }
55
56 return ret;
57}
Max Kellermann5b76f682008-01-29 13:42:48 +000058
Rusty Russella8f033e2000-07-30 01:43:01 +000059
Rusty Russella8f033e2000-07-30 01:43:01 +000060static int do_output(const char *tablename)
61{
Jan Engelhardtfd187312008-11-10 16:59:27 +010062 struct iptc_handle *h;
Rusty Russella8f033e2000-07-30 01:43:01 +000063 const char *chain = NULL;
64
65 if (!tablename)
66 return for_each_table(&do_output);
67
68 h = iptc_init(tablename);
69 if (!h)
Max Kellermann5b76f682008-01-29 13:42:48 +000070 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
Rusty Russella8f033e2000-07-30 01:43:01 +000071 iptc_strerror(errno));
72
Jan Engelhardtdbb77542008-02-11 00:33:30 +010073 if (!show_binary) {
Rusty Russella8f033e2000-07-30 01:43:01 +000074 time_t now = time(NULL);
75
76 printf("# Generated by iptables-save v%s on %s",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020077 XTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +000078 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +000079
Max Kellermann5b76f682008-01-29 13:42:48 +000080 /* Dump out chain names first,
Harald Welte9f7fa492001-03-15 15:12:02 +000081 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010082 for (chain = iptc_first_chain(h);
Rusty Russella8f033e2000-07-30 01:43:01 +000083 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010084 chain = iptc_next_chain(h)) {
Max Kellermann5b76f682008-01-29 13:42:48 +000085
Rusty Russella8f033e2000-07-30 01:43:01 +000086 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +000087 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +000088 struct ipt_counters count;
89 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010090 iptc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000091 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +000092 } else {
Harald Welted8e65632001-01-05 15:20:07 +000093 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +000094 }
Harald Welte9f7fa492001-03-15 15:12:02 +000095 }
Max Kellermann5b76f682008-01-29 13:42:48 +000096
Harald Welte9f7fa492001-03-15 15:12:02 +000097
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010098 for (chain = iptc_first_chain(h);
Harald Welte9f7fa492001-03-15 15:12:02 +000099 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100100 chain = iptc_next_chain(h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000101 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000102
Harald Welteae1ff9f2000-12-01 14:26:20 +0000103 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100104 e = iptc_first_rule(chain, h);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000105 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100106 print_rule(e, h, chain, show_counters);
107 e = iptc_next_rule(e, h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000108 }
109 }
110
111 now = time(NULL);
112 printf("COMMIT\n");
113 printf("# Completed on %s", ctime(&now));
114 } else {
115 /* Binary, huh? OK. */
116 exit_error(OTHER_PROBLEM, "Binary NYI\n");
117 }
118
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100119 iptc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000120
Rusty Russella8f033e2000-07-30 01:43:01 +0000121 return 1;
122}
123
Marc Bouchere6869a82000-03-20 06:03:29 +0000124/* Format:
125 * :Chain name POLICY packets bytes
126 * rule
127 */
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000128#ifdef IPTABLES_MULTI
129int
130iptables_save_main(int argc, char *argv[])
131#else
132int
133main(int argc, char *argv[])
134#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000135{
Rusty Russella8f033e2000-07-30 01:43:01 +0000136 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000137 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000138
139 program_name = "iptables-save";
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200140 program_version = XTABLES_VERSION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000141
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100142 lib_dir = getenv("XTABLES_LIBDIR");
143 if (lib_dir == NULL) {
144 lib_dir = getenv("IPTABLES_LIB_DIR");
145 if (lib_dir != NULL)
146 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated\n");
147 }
148 if (lib_dir == NULL)
149 lib_dir = XTABLES_LIBDIR;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000150
Harald Welte3efb6ea2001-08-06 18:50:21 +0000151#ifdef NO_SHARED_LIBS
152 init_extensions();
153#endif
154
Marc Boucher163ad782001-12-06 15:05:48 +0000155 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000156 switch (c) {
157 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100158 show_binary = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 break;
160
161 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100162 show_counters = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 break;
164
Rusty Russella8f033e2000-07-30 01:43:01 +0000165 case 't':
166 /* Select specific table. */
167 tablename = optarg;
168 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000170 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000171 exit(0);
172 }
173 }
174
175 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000176 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000177 exit(1);
178 }
179
Rusty Russella8f033e2000-07-30 01:43:01 +0000180 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000181}