summaryrefslogtreecommitdiffstats
path: root/func/minion/modules/iptables/__init__.py
blob: db11a235cb7f4f95d925838686b3b3e03330bce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#
# Copyright 2008
# Krzysztof A. Adamski <krzysztofa@gmail.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# our modules
from func.minion.modules import func_module
from func.minion.modules.iptables.common import *

IPTABLES_SAVE_FILE = "/etc/sysconfig/iptables"

class Iptables(func_module.FuncModule):

    # Update these if need be.
    version = "0.0.1"
    api_version = "0.0.1"
    description = "iptables module"

    def run(self, args):
        """
        Run 'iptables' command with arguments given. For example:
          > func '*' call iptables run "-L INPUT"
        """
        return run_iptables(args)

    def policy(self, chain="INPUT", policy=None):
        """
        Check/set default policy for the chain. Examples:
          * Check default policy for INPUT chain:
            > func '*' call iptables policy
            or
            > func '*' call iptables policy INPUT
          * Set default policy for OUTPUT:
            > func '*' call iptables policy OUTPUT DROP
        """
        if policy==None:
            return check_policy(chain)
        else:
            return set_policy(chain, policy)

    def flush(self, chain="INPUT"):
        """
        Flush the selected chain (or INPUT if none given).
        """
        return call_iptables("-F %s" % chain)

    def zero(self, chain="INPUT"):
        """
        Zero counters in selected chain (or INPUT if none given).
        """
        return call_iptables("-Z %s" % chain)

    def drop_from(self, ip):
        """
        Drop all incomming traffic from IP. Example:
          > func '*' call iptables drop_from 192.168.0.10
        """
        clear_all("-D INPUT -s %s -j ACCEPT" % ip)
        clear_all("-D INPUT -s %s -j REJECT" % ip)
        return call_if_policy("INPUT", "ACCEPT", "-I INPUT -s %s -j DROP" % ip)

    def reject_from(self, ip):
        """
        Reject all incoming traffic from IP. Example:
          > func '*' call iptables reject_from 192.168.0.10
        """
        clear_all("-D INPUT -s %s -j ACCEPT" % ip)
        clear_all("-D INPUT -s %s -j DROP" % ip)
        return call_iptables("-I INPUT -s %s -j REJECT" % ip)

    def accept_from(self, ip):
        """
        Accept all incoming traffic from IP. Example:
          > func '*' call iptables accept_from 192.168.0.10
        """
        clear_all("-D INPUT -s %s -j DROP" % ip)
        clear_all("-D INPUT -s %s -j REJECT" % ip)
        return call_if_policy("INPUT", "DROP", "-I INPUT -s %s -j ACCEPT" % ip)

    def drop_to(self, ip):
        """
        Drop all outgoing traffic to IP. Example:
          > func '*' call iptables drop_to 192.168.0.10
        """
        clear_all("-D OUTPUT -d %s -j ACCEPT" % ip)
        clear_all("-D OUTPUT -d %s -j REJECT" % ip)
        return call_if_policy("INPUT", "ACCEPT", "-I OUTPUT -d %s -j DROP" % ip)

    def reject_to(self, ip):
        """
        Reject all outgoing traffic to IP. Example:
          > func '*' call iptables reject_to 192.168.0.10
        """
        clear_all("-D OUTPUT -d %s -j ACCEPT" % ip)
        clear_all("-D OUTPUT -d %s -j DROP" % ip)
        return call_iptables("-I OUTPUT -d %s -j REJECT" % ip)

    def accept_to(self, ip):
        """
        Accept all outgoing traffic to IP. Example:
          > func '*' call iptables accept_to 192.168.0.10
        """
        clear_all("-D OUTPUT -d %s -j DROP" % ip)
        clear_all("-D OUTPUT -d %s -j REJECT" % ip)
        return call_if_policy("INPUT", "DROP", "-I OUTPUT -d %s -j ACCEPT" % ip)

    def inventory(self):
        return self.dump()

    def dump(self, counters=False):
        """
        Dump iptables configuration in iptables-save format.
        """
        args = []
        if counters:
            args.append("-c")

        cmd = sub_process.Popen(["/sbin/iptables-save"] + args,
                                executable="/sbin/iptables-save",
                                stdout=sub_process.PIPE,
                                stderr=sub_process.PIPE,
                                shell=False)

        data, error = cmd.communicate()

        return data

    def save(self, counters=False):
        """
        Save iptables state using '/sbin/iptables-save'. If counters=True,
        save counters too.
        TODO: maybe some locking?
        """
        f=open(IPTABLES_SAVE_FILE, 'w')
        f.write(self.dump(counters))
        f.close
        return True

    def panic(self):
        self.flush("")
        self.policy("INPUT", "DROP")
        self.policy("OUTPUT", "DROP")
        self.policy("FORWARD", "DROP")

    def register_method_args(self):
        """
        Implmenting the export arguments
        """
        ip={
                'type':'string',
                'optional':False,
                'default':'0.0.0.0'
                }
        chain={
                'type':'string',
                'optional':False,
                'default':'INPUT',
                'options':['INPUT','OUTPUT','FORWARD'],
                'description':"The chain to apply policy"
                }

        return {
                'run':{
                    'args':{
                        'args':{
                            'type':'string',
                            'optional':False,
                            'default':"-L",
                            'description':"The iptables command to send"
                            }
                        },
                    'description':"Runs a iptables command"
                    },
                'policy':{
                    'args':{
                        'chain':chain,
                        'policy':{
                            'type':'string',
                            'optional':True,
                            'description':"The policy to apply (optional)"
                            }

                        },
                    'description':"Change/set the policy of the given chain,if no policy is given it checks the chain status"
                    },
                'flush':{
                    'args':{
                        'chain':chain
                        },
                    'description':"Flush the given chain"
                    },
                'zero':{
                    'args':{
                        'chain':chain
                        },
                    'description':"Zero counters in selected chain (or INPUT if none given)"
                    },
                'drop_from':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Drop all incomming traffic from IP"
                    },
                'reject_from':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Reject all incoming traffic from IP"
                    },
                'accept_from':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Accept all incoming traffic from IP"
                    },
                'drop_to':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Drop all outgoing traffic to IP."
                    },
                'reject_to':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Reject all outgoing traffic to IP"
                    },
                'accept_to':{
                    'args':{
                        'ip':ip
                        },
                    'description':"Accept all outgoing traffic to IP."
                    },
                'inventory':{
                    'args':{},
                    'description':"The inventory part for that module"
                    },
                'dump':{
                    'args':{
                        'counters':{
                            'type':'boolean',
                            'optional':True,
                            'default':False,
                            'description':"Dump also the counters?"
                            }
                        },
                    'description':"Dump iptables configuration in iptables-save format"
                    },
                'save':{
                    'args':{
                        'counters':{
                            'type':'boolean',
                            'optional':True,
                            'default':False,
                            'description':"Save also the counters?"
                            }
                        },
                    'description':"Save iptables state using '/sbin/iptables-save'. If counters=True, save counters too."
                        },
                'panic':{
                    'args':{},
                    'description':"Drop all traffic (similar to 'service iptables panic')."
                        }
                }