fw.py 19 KB
Newer Older
Őry Máté committed
1
from django.contrib import auth
Őry Máté committed
2
from firewall import models
Őry Máté committed
3
import os
Őry Máté committed
4
from cloud.settings import firewall_settings as settings
Őry Máté committed
5 6 7

import subprocess
import re
django committed
8
import json
Őry Máté committed
9 10 11


class firewall:
x committed
12
    IPV6=False
Dudás Ádám committed
13 14
    RULES = None
    RULES_NAT = []
x committed
15 16 17 18 19 20 21
    vlans = None
    dmz = None
    pub = None
    hosts = None
    fw = None

    def dportsport(self, rule, repl=True):
Dudás Ádám committed
22 23 24 25 26 27 28 29 30 31 32
        retval = ' '
        if rule.proto == 'tcp' or rule.proto == 'udp':
            retval = '-p %s ' % rule.proto
            if rule.sport:
                retval += ' --sport %s ' % rule.sport
            if rule.dport:
                retval += ' --dport %s ' % (rule.nat_dport
                        if (repl and rule.nat and rule.direction == '1')
                        else rule.dport)
        elif rule.proto == 'icmp':
            retval = '-p %s ' % rule.proto
x committed
33 34 35 36
        return retval


    def iptables(self, s):
Dudás Ádám committed
37 38
        """Append rule."""
        self.RULES.append(s)
x committed
39 40

    def iptablesnat(self, s):
Dudás Ádám committed
41
        self.RULES_NAT.append(s)
x committed
42 43

    def host2vlan(self, host, rule):
Bach Dániel committed
44 45 46
        if rule.foreign_network is None:
            return

Dudás Ádám committed
47 48
        if self.IPV6 and host.ipv6:
            ipaddr = host.ipv6 + '/112'
x committed
49 50 51 52 53
        else:
            ipaddr = host.ipv4

        dport_sport = self.dportsport(rule)

Bach Dániel committed
54
        for vlan in rule.foreign_network.vlans.all():
Dudás Ádám committed
55 56 57 58 59 60
            if rule.accept:
                if rule.direction == '0' and vlan.name == 'PUB':
                    if rule.dport == 25:
                        self.iptables('-A PUB_OUT -s %s %s -p tcp '
                                '--dport 25 -j LOG_ACC' %
                                (ipaddr, rule.extra))
x committed
61
                        break
Dudás Ádám committed
62
                    action = 'PUB_OUT'
x committed
63
                else:
Dudás Ádám committed
64
                    action = 'LOG_ACC'
x committed
65
            else:
Dudás Ádám committed
66
                action = 'LOG_DROP'
x committed
67

Dudás Ádám committed
68 69 70
            if rule.direction == '1': # going TO host
                self.iptables('-A %s_%s -d %s %s %s -g %s' % (vlan,
                    host.vlan, ipaddr, dport_sport, rule.extra, action))
x committed
71
            else:
Dudás Ádám committed
72 73
                self.iptables('-A %s_%s -s %s %s %s -g %s' % (host.vlan,
                    vlan, ipaddr, dport_sport, rule.extra, action))
x committed
74 75 76


    def fw2vlan(self, rule):
Bach Dániel committed
77 78 79
        if rule.foreign_network is None:
            return

x committed
80 81
        dport_sport = self.dportsport(rule)

Bach Dániel committed
82
        for vlan in rule.foreign_network.vlans.all():
Dudás Ádám committed
83 84 85 86
            if rule.direction == '1': # going TO host
                self.iptables('-A INPUT -i %s %s %s -g %s' %
                    (vlan.interface, dport_sport, rule.extra,
                        'LOG_ACC' if rule.accept else 'LOG_DROP'))
x committed
87
            else:
Dudás Ádám committed
88 89 90
                self.iptables('-A OUTPUT -o %s %s %s -g %s' %
                    (vlan.interface, dport_sport, rule.extra,
                        'LOG_ACC' if rule.accept else 'LOG_DROP'))
x committed
91 92

    def vlan2vlan(self, l_vlan, rule):
Bach Dániel committed
93 94 95
        if rule.foreign_network is None:
            return

x committed
96 97
        dport_sport = self.dportsport(rule)

Bach Dániel committed
98
        for vlan in rule.foreign_network.vlans.all():
Dudás Ádám committed
99 100 101
            if rule.accept:
                if rule.direction == '0' and vlan.name == 'PUB':
                    action = 'PUB_OUT'
x committed
102
                else:
Dudás Ádám committed
103
                    action = 'LOG_ACC'
x committed
104
            else:
Dudás Ádám committed
105
                action = 'LOG_DROP'
x committed
106

Dudás Ádám committed
107 108 109
            if rule.direction == '1': # going TO host
                self.iptables('-A %s_%s %s %s -g %s' % (vlan, l_vlan,
                    dport_sport, rule.extra, action))
x committed
110
            else:
Dudás Ádám committed
111 112
                self.iptables('-A %s_%s %s %s -g %s' % (l_vlan, vlan,
                    dport_sport, rule.extra, action))
x committed
113 114 115


    def prerun(self):
Dudás Ádám committed
116 117 118 119
        self.iptables('*filter')
        self.iptables(':INPUT DROP [88:6448]')
        self.iptables(':FORWARD DROP [0:0]')
        self.iptables(':OUTPUT DROP [50:6936]')
x committed
120

121
        # initialize logging
Dudás Ádám committed
122
        self.iptables('-N LOG_DROP')
x committed
123
        # windows port scan are silently dropped
Dudás Ádám committed
124 125
        self.iptables('-A LOG_DROP -p tcp --dport 445 -j DROP')
        self.iptables('-A LOG_DROP -p udp --dport 137 -j DROP')
Őry Máté committed
126
        self.iptables('-A LOG_DROP -j LOG --log-level 7 '
Dudás Ádám committed
127 128 129
                '--log-prefix "[ipt][drop]"')
        self.iptables('-A LOG_DROP -j DROP')
        self.iptables('-N LOG_ACC')
Őry Máté committed
130
        self.iptables('-A LOG_ACC -j LOG --log-level 7 '
Dudás Ádám committed
131 132
                '--log-prefix "[ipt][isok]"')
        self.iptables('-A LOG_ACC -j ACCEPT')
x committed
133 134 135 136

        if not self.IPV6:
            # The chain which test is a packet has a valid public destination IP
            # (RFC-3330) packages passing this chain has valid destination IP addressed
Dudás Ádám committed
137 138 139 140 141 142 143 144
            self.iptables('-N r_pub_dIP')
            self.iptables('-A r_pub_dIP -d 0.0.0.0/8 -g LOG_DROP')
            self.iptables('-A r_pub_dIP -d 169.254.0.0/16 -g LOG_DROP')
            self.iptables('-A r_pub_dIP -d 172.16.0.0/12 -g LOG_DROP')
            self.iptables('-A r_pub_dIP -d 192.0.2.0/24 -g LOG_DROP')
            self.iptables('-A r_pub_dIP -d 192.168.0.0/16 -g LOG_DROP')
            self.iptables('-A r_pub_dIP -d 127.0.0.0/8 -g LOG_DROP')
            # self.iptables('-A r_pub_dIP -d 10.0.0.0/8 -g LOG_DROP')
x committed
145 146 147

            # The chain which test is a packet has a valid public source IP
            # (RFC-3330) packages passing this chain has valid destination IP addressed
Dudás Ádám committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            self.iptables('-N r_pub_sIP')
            self.iptables('-A r_pub_sIP -s 0.0.0.0/8 -g LOG_DROP')
            self.iptables('-A r_pub_sIP -s 169.254.0.0/16 -g LOG_DROP')
            self.iptables('-A r_pub_sIP -s 172.16.0.0/12 -g LOG_DROP')
            self.iptables('-A r_pub_sIP -s 192.0.2.0/24 -g LOG_DROP')
            self.iptables('-A r_pub_sIP -s 192.168.0.0/16 -g LOG_DROP')
            self.iptables('-A r_pub_sIP -s 127.0.0.0/8 -g LOG_DROP')
            # self.iptables('-A r_pub_sIP -s 10.0.0.0/8 -g LOG_DROP')

            # Chain which tests whether the destination specified by the
            # DMZ host is valid
            self.iptables('-N r_DMZ_dIP')
            self.iptables('-A r_DMZ_dIP -d 10.2.0.0/16 -j RETURN')
            self.iptables('-A r_DMZ_dIP -j r_pub_dIP')

        self.iptables('-N PUB_OUT')
x committed
164
        if not self.IPV6:
Dudás Ádám committed
165
            self.iptables('-A PUB_OUT -j r_pub_dIP')
x committed
166

Dudás Ádám committed
167
        self.iptables('-A FORWARD -m state --state INVALID -g LOG_DROP')
168
        self.iptables('-A FORWARD -m state --state ESTABLISHED,RELATED '
Dudás Ádám committed
169
                '-j ACCEPT')
170
        self.iptables('-A FORWARD -p icmp --icmp-type echo-request '
Dudás Ádám committed
171
                '-g LOG_ACC')
x committed
172
        if not self.IPV6:
Dudás Ádám committed
173 174 175
            self.iptables('-A FORWARD -j r_pub_sIP -o pub')
        self.iptables('-A INPUT -m state --state INVALID -g LOG_DROP')
        self.iptables('-A INPUT -i lo -j ACCEPT')
x committed
176
        if not self.IPV6:
Dudás Ádám committed
177
            self.iptables('-A INPUT -j r_pub_sIP')
178
        self.iptables('-A INPUT -m state --state ESTABLISHED,RELATED '
Dudás Ádám committed
179
                '-j ACCEPT')
x committed
180

Dudás Ádám committed
181 182
        self.iptables('-A OUTPUT -m state --state INVALID -g LOG_DROP')
        self.iptables('-A OUTPUT -o lo -j ACCEPT')
183
        self.iptables('-A OUTPUT -m state --state ESTABLISHED,RELATED '
Dudás Ádám committed
184
                '-j ACCEPT')
x committed
185 186 187


    def postrun(self):
188
        self.iptables('-A PUB_OUT -s 152.66.243.160/27 -p tcp --dport 25 '
Dudás Ádám committed
189
                '-j LOG_ACC')
190
        self.iptables('-A PUB_OUT -s 152.66.243.160/27 -p tcp --dport 445 '
Dudás Ádám committed
191 192 193 194
                '-j LOG_ACC')
        self.iptables('-A PUB_OUT -p tcp --dport 25 -j LOG_DROP')
        self.iptables('-A PUB_OUT -p tcp --dport 445 -j LOG_DROP')
        self.iptables('-A PUB_OUT -p udp --dport 445 -j LOG_DROP')
x committed
195

Dudás Ádám committed
196 197 198 199 200
        self.iptables('-A PUB_OUT -g LOG_ACC')
        self.iptables('-A FORWARD -g LOG_DROP')
        self.iptables('-A INPUT -g LOG_DROP')
        self.iptables('-A OUTPUT -g LOG_DROP')
        self.iptables('COMMIT')
x committed
201 202 203 204 205




    def ipt_nat(self):
Dudás Ádám committed
206 207 208 209 210
        self.iptablesnat('*nat')
        self.iptablesnat(':PREROUTING ACCEPT [0:0]')
        self.iptablesnat(':INPUT ACCEPT [0:0]')
        self.iptablesnat(':OUTPUT ACCEPT [1:708]')
        self.iptablesnat(':POSTROUTING ACCEPT [1:708]')
x committed
211 212 213 214 215 216

        # portforward
        for host in self.hosts.exclude(pub_ipv4=None):
            for rule in host.rules.filter(nat=True, direction='1'):
                dport_sport = self.dportsport(rule, False)
                if host.vlan.snat_ip:
217
                    self.iptablesnat('-A PREROUTING -d %s %s %s -j DNAT '
Dudás Ádám committed
218 219 220
                            '--to-destination %s:%s' % (host.pub_ipv4,
                                dport_sport, rule.extra, host.ipv4,
                                rule.nat_dport))
x committed
221

Dudás Ádám committed
222
        # rules for machines with dedicated public IP
x committed
223
        for host in self.hosts.exclude(shared_ip=True):
Dudás Ádám committed
224
            if host.pub_ipv4:
225
                self.iptablesnat('-A PREROUTING -d %s -j DNAT '
Dudás Ádám committed
226
                        '--to-destination %s' % (host.pub_ipv4, host.ipv4))
227
                self.iptablesnat('-A POSTROUTING -s %s -j SNAT '
Dudás Ádám committed
228
                        '--to-source %s' % (host.ipv4, host.pub_ipv4))
x committed
229

Dudás Ádám committed
230
        # default NAT rules for VLANs
x committed
231
        for s_vlan in self.vlans:
Dudás Ádám committed
232
            if s_vlan.snat_ip:
x committed
233
                for d_vlan in s_vlan.snat_to.all():
234
                    self.iptablesnat('-A POSTROUTING -s %s -o %s -j SNAT '
Dudás Ádám committed
235 236
                            '--to-source %s' % (s_vlan.net_ipv4(),
                                d_vlan.interface, s_vlan.snat_ip))
x committed
237 238


Dudás Ádám committed
239
        # hard-wired rules
240
        self.iptablesnat('-A POSTROUTING -s 10.5.0.0/16 -o vlan0003 -j SNAT '
Dudás Ádám committed
241
                '--to-source 10.3.255.254') # man elerheto legyen
242
        self.iptablesnat('-A POSTROUTING -s 10.5.0.0/16 -o vlan0008 -j SNAT '
Dudás Ádám committed
243
                '--to-source 10.0.0.247') # wolf network for printing
244
        self.iptablesnat('-A POSTROUTING -s 10.3.0.0/16 -o vlan0002 -j SNAT '
Dudás Ádám committed
245
                '--to-source %s' % self.pub.ipv4) # kulonben nemmegy a du
x committed
246

Dudás Ádám committed
247
        self.iptablesnat('COMMIT')
x committed
248 249

    def ipt_filter(self):
Dudás Ádám committed
250
        ipv4_re = re.compile('([0-9]{1,3}\.){3}[0-9]{1,3}')
x committed
251

Dudás Ádám committed
252
        # pre-run stuff
x committed
253 254
        self.prerun()

Dudás Ádám committed
255
        # firewall's own rules
x committed
256 257 258 259 260 261 262
        for f in self.fw:
            for rule in f.rules.all():
                self.fw2vlan(rule)

        # zonak kozotti lancokra ugras
        for s_vlan in self.vlans:
            for d_vlan in self.vlans:
Dudás Ádám committed
263
                self.iptables('-N %s_%s' % (s_vlan, d_vlan))
264 265
                self.iptables('-A FORWARD -i %s -o %s -g %s_%s' %
                    (s_vlan.interface, d_vlan.interface, s_vlan, d_vlan))
x committed
266

Dudás Ádám committed
267
        # hosts' rules
x committed
268 269 270 271 272 273 274 275
        for i_vlan in self.vlans:
            for i_host in i_vlan.host_set.all():
                for group in i_host.groups.all():
                    for rule in group.rules.all():
                        self.host2vlan(i_host, rule)
                for rule in i_host.rules.all():
                    self.host2vlan(i_host, rule)

Dudás Ádám committed
276
        # enable communication between VLANs
x committed
277 278 279 280 281 282 283
        for s_vlan in self.vlans:
            for rule in s_vlan.rules.all():
                self.vlan2vlan(s_vlan, rule)

        # zonak kozotti lancokat zarja le
        for s_vlan in self.vlans:
            for d_vlan in self.vlans:
Dudás Ádám committed
284
                self.iptables('-A %s_%s -g LOG_DROP' % (s_vlan, d_vlan))
x committed
285

Dudás Ádám committed
286
        # post-run stuff
x committed
287 288 289
        self.postrun()

        if self.IPV6:
Dudás Ádám committed
290 291
            self.RULES = [x for x in self.RULES if not ipv4_re.search(x)]
            self.RULES = [x.replace('icmp', 'icmpv6') for x in self.RULES]
x committed
292 293

    def __init__(self, IPV6=False):
Dudás Ádám committed
294 295
        self.RULES=[]
        self.RULES_NAT=[]
x committed
296 297 298
        self.IPV6 = IPV6
        self.vlans = models.Vlan.objects.all()
        self.hosts = models.Host.objects.all()
Dudás Ádám committed
299 300
        self.dmz = models.Vlan.objects.get(name='DMZ')
        self.pub = models.Vlan.objects.get(name='PUB')
x committed
301 302 303 304 305 306 307
        self.fw = models.Firewall.objects.all()
        self.ipt_filter()
        if not self.IPV6:
            self.ipt_nat()

    def reload(self):
        if self.IPV6:
Dudás Ádám committed
308 309 310 311
            process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
                    '/usr/bin/sudo', '/sbin/ip6tables-restore', '-c'],
                shell=False, stdin=subprocess.PIPE)
            process.communicate('\n'.join(self.RULES) + '\n')
x committed
312
        else:
Dudás Ádám committed
313 314 315 316 317
            process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
                    '/usr/bin/sudo', '/sbin/iptables-restore', '-c'],
                shell=False, stdin=subprocess.PIPE)
            process.communicate('\n'.join(self.RULES) + '\n' +
                    '\n'.join(self.RULES_NAT) + '\n')
x committed
318

319 320
    def get(self):
        if self.IPV6:
321
            return { 'filter': self.RULES, }
322
        else:
323
            return { 'filter': self.RULES, 'nat':self.RULES_NAT }
324

x committed
325 326
    def show(self):
        if self.IPV6:
Dudás Ádám committed
327
            return '\n'.join(self.RULES) + '\n'
x committed
328
        else:
Dudás Ádám committed
329 330
            return ('\n'.join(self.RULES) + '\n' +
                '\n'.join(self.RULES_NAT) + '\n')
Őry Máté committed
331 332


Őry Máté committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346
def ipv6_to_octal(ipv6):
    while len(ipv6.split(':')) < 8:
        ipv6 = ipv6.replace('::', ':::')
    octets = []
    for part in ipv6.split(':'):
        if not part:
            octets.extend([0, 0])
        else:
            # Pad hex part to 4 digits.
            part = '%04x' % int(part, 16)
            octets.append(int(part[:2], 16))
            octets.append(int(part[2:], 16))
    return '\\' + '\\'.join(['%03o' % x for x in octets])

django committed
347 348
def ipv4_to_arpa(ipv4, cname=False):
    m2 = re.search(r'^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$', ipv4)
Dudás Ádám committed
349 350 351
    if cname:
        return ('%s.dns1.%s.%s.%s.in-addr.arpa' %
            (m2.group(4), m2.group(3), m2.group(2), m2.group(1)))
django committed
352
    else:
Dudás Ádám committed
353 354
        return ('%s.%s.%s.%s.in-addr.arpa' %
            (m2.group(4), m2.group(3), m2.group(2), m2.group(1)))
Őry Máté committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

def ipv6_to_arpa(ipv6):
    while len(ipv6.split(':')) < 8:
        ipv6 = ipv6.replace('::', ':::')
    octets = []
    for part in ipv6.split(':'):
        if not part:
            octets.extend([0, 0, 0, 0])
        else:
            # Pad hex part to 4 digits.
            part = '%04x' % int(part, 16)
            octets.insert(0, int(part[0], 16))
            octets.insert(0, int(part[1], 16))
            octets.insert(0, int(part[2], 16))
            octets.insert(0, int(part[3], 16))
    return '.'.join(['%1x' % x for x in octets]) + '.ip6.arpa'


django committed
373 374 375 376 377 378 379 380
# =fqdn:ip:ttl          A, PTR
# &fqdn:ip:x:ttl        NS
# ZfqdnSOA
# +fqdn:ip:ttl          A
# ^                     PTR
# C                     CNAME
# :                     generic

Őry Máté committed
381
def dns():
x committed
382 383 384 385 386 387
    vlans = models.Vlan.objects.all()
    regex = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$')
    DNS = []

    for i_vlan in vlans:
        m = regex.search(i_vlan.net4)
388 389
        rev = i_vlan.reverse_domain

x committed
390
        for i_host in i_vlan.host_set.all():
391 392
            ipv4 = (i_host.pub_ipv4 if i_host.pub_ipv4 and
                    not i_host.shared_ip else i_host.ipv4)
393
            i = ipv4.split('.', 4)
394 395
            reverse = (i_host.reverse if i_host.reverse and
                    len(i_host.reverse) else i_host.get_fqdn())
django committed
396

x committed
397
            # ipv4
django committed
398
            if i_host.ipv4:
399 400 401
                DNS.append("^%s:%s:%s" % (
                    (rev % { 'a': int(i[0]), 'b': int(i[1]), 'c': int(i[2]),
                             'd': int(i[3]) }),
Dudás Ádám committed
402
                    reverse, models.settings['dns_ttl']))
django committed
403

x committed
404
            # ipv6
405
            if i_host.ipv6:
Dudás Ádám committed
406
                DNS.append("^%s:%s:%s" % (ipv6_to_arpa(i_host.ipv6),
407
                    reverse, models.settings['dns_ttl']))
django committed
408

409
    for domain in models.Domain.objects.all():
410 411
        DNS.append("Z%s:%s:support.ik.bme.hu::::::%s" % (domain.name,
            settings['dns_hostname'], models.settings['dns_ttl']))
412

413 414 415 416 417
    for r in models.Record.objects.all():
        d = r.get_data()
        if d['type'] == 'A':
            DNS.append("+%s:%s:%s" % (d['name'], d['address'], d['ttl']))
        elif d['type'] == 'AAAA':
418 419
            DNS.append(":%s:28:%s:%s" % (d['name'],
                ipv6_to_octal(d['address']), d['ttl']))
420 421 422 423
        elif d['type'] == 'NS':
            DNS.append("&%s::%s:%s" % (d['name'], d['address'], d['ttl']))
        elif d['type'] == 'CNAME':
            DNS.append("C%s:%s:%s" % (d['name'], d['address'], d['ttl']))
x committed
424 425
        elif d['type'] == 'MX':
            mx = d['address'].split(':', 2)
426 427 428
            DNS.append("@%(fqdn)s::%(mx)s:%(dist)s:%(ttl)s" %
                    {'fqdn': d['name'], 'mx': mx[1], 'dist': mx[0],
                     'ttl': d['ttl']})
x committed
429

430
    return DNS
Dudás Ádám committed
431
    process = subprocess.Popen(['/usr/bin/ssh', 'tinydns@%s' %
432
        settings['dns_hostname']], shell=False, stdin=subprocess.PIPE)
x committed
433
    process.communicate("\n".join(DNS)+"\n")
django committed
434
    # print "\n".join(DNS)+"\n"
Őry Máté committed
435

Őry Máté committed
436

437
def prefix_to_mask(prefix):
x committed
438 439
    t = [0, 0, 0, 0]
    for i in range(0, 4):
Dudás Ádám committed
440
        if prefix > i * 8 + 7:
x committed
441
            t[i] = 255
Dudás Ádám committed
442 443
        elif i * 8 < prefix and prefix <= (i + 1) * 8:
            t[i] = 256 - (2 ** ((i + 1) * 8 - prefix))
x committed
444
    return ".".join([str(i) for i in t])
Őry Máté committed
445 446

def dhcp():
x committed
447
    vlans = models.Vlan.objects.all()
Dudás Ádám committed
448 449
    regex = re.compile(r'^([0-9]+)\.([0-9]+)\.[0-9]+\.[0-9]+\s+'
            r'([0-9]+)\.([0-9]+)\.[0-9]+\.[0-9]+$')
x committed
450 451 452 453 454 455 456 457 458
    DHCP = []

# /tools/dhcp3/dhcpd.conf.generated

    for i_vlan in vlans:
        if(i_vlan.dhcp_pool):
            m = regex.search(i_vlan.dhcp_pool)
            if(m or i_vlan.dhcp_pool == "manual"):
                DHCP.append ('''
Dudás Ádám committed
459 460 461 462 463 464 465 466 467 468 469 470
    # %(name)s - %(interface)s
    subnet %(net)s netmask %(netmask)s {
      %(extra)s;
      option domain-name "%(domain)s";
      option routers %(router)s;
      option domain-name-servers %(dnsserver)s;
      option ntp-servers %(ntp)s;
      next-server %(tftp)s;
      authoritative;
      filename \"pxelinux.0\";
      allow bootp; allow booting;
    }'''        % {
x committed
471 472 473 474 475
                    'net': i_vlan.net4,
                    'netmask': prefix_to_mask(i_vlan.prefix4),
                    'domain': i_vlan.domain,
                    'router': i_vlan.ipv4,
                    'ntp': i_vlan.ipv4,
Őry Máté committed
476
                    'dnsserver': settings['rdns_ip'],
Dudás Ádám committed
477
                    'extra': "range %s" % (i_vlan.dhcp_pool
478
                        if m else "deny unknown clients"),
x committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                    'interface': i_vlan.interface,
                    'name': i_vlan.name,
                    'tftp': i_vlan.ipv4
                })

                for i_host in i_vlan.host_set.all():
                    DHCP.append ('''
                    host %(hostname)s {
                      hardware ethernet %(mac)s;
                      fixed-address %(ipv4)s;
                    }''' % {
                        'hostname': i_host.hostname,
                        'mac': i_host.mac,
                        'ipv4': i_host.ipv4,
                    })

495
    return DHCP
Dudás Ádám committed
496 497 498 499
    process = subprocess.Popen(['/usr/bin/ssh', 'fw2',
        'cat > /tools/dhcp3/dhcpd.conf.generated;'
        'sudo /etc/init.d/isc-dhcp-server restart'], shell=False,
        stdin=subprocess.PIPE)
x committed
500 501
#   print "\n".join(DHCP)+"\n"
    process.communicate("\n".join(DHCP)+"\n")
Őry Máté committed
502 503 504


'''
x committed
505
i=2
Őry Máté committed
506
for mac, name, ipend in [("18:a9:05:64:19:aa", "mega6", 16), ("00:1e:0b:e9:79:1e", "blade1", 21), ("00:22:64:9c:fd:34", "blade2", 22), ("00:1e:0b:ec:65:46", "blade3", 23), ("b4:b5:2f:61:d2:5a", "cloud-man", 1)]:
x committed
507 508 509 510 511 512 513 514
    h1 = models.Host(hostname= name, vlan=models.Vlan.objects.get(vid=3), mac=mac, ipv4="10.3.1.%d" % ipend, ipv6="2001:738:2001:4031:3:1:%d:0" % ipend, owner=auth.models.User.objects.get(username="bd"))
    try:
        h1.save()
        h1.groups.add(models.Group.objects.get(name="netezhet manbol"))
        h1.save()
#       i = i + 1
    except:
        print "nemok %s" % name
Őry Máté committed
515
'''
x committed
516