views.py 3.21 KB
Newer Older
Őry Máté committed
1 2 3
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.shortcuts import render_to_response
Őry Máté committed
4 5
from firewall.models import *
from firewall.fw import *
6
from django.views.decorators.csrf import csrf_exempt
7
from django.views.decorators.http import require_POST
8
from django.db import IntegrityError
9 10
from tasks import *
from celery.task.control import inspect
11
from django.utils.translation import ugettext_lazy as _
root committed
12

13
import re
root committed
14 15 16
import base64
import json
import sys
Őry Máté committed
17

18

Őry Máté committed
19
def reload_firewall(request):
x committed
20
    if request.user.is_authenticated():
21
        if request.user.is_superuser:
22
            html = ((_("Dear %s, you've signed in as administrator!") %
23
                    request.user.username) + "<br />" +
24
                    _("Reloading in 10 seconds..."))
x committed
25 26
            ReloadTask.delay()
        else:
27
            html = (_("Dear %s, you've signed in!")
28
                    % request.user.username)
x committed
29
    else:
30
        html = _("Dear anonymous, you've not signed in yet!")
x committed
31
    return HttpResponse(html)
root committed
32 33

@csrf_exempt
34
@require_POST
root committed
35
def firewall_api(request):
36 37 38 39 40
    try:
        data=json.loads(base64.b64decode(request.POST["data"]))
        command = request.POST["command"]
        if data["password"] != "bdmegintelrontottaanetet":
            raise Exception(_("Wrong password."))
41

42 43 44 45 46 47
        if command == "blacklist":
            obj, created = Blacklist.objects.get_or_create(ipv4=data["ip"])
            if created:
                obj.reason=data["reason"]
                obj.snort_message=data["snort_message"]
            obj.save()
Bach Dániel committed
48
            return HttpResponse(unicode(_("OK")));
49

50 51
        if not (data["vlan"] == "vm-net" or data["vlan"] == "war"):
            raise Exception(_("Only vm-net and war can be used."))
52

53
        data["hostname"] = re.sub(r' ','_', data["hostname"])
54

55 56 57 58 59 60 61 62 63 64 65
        if command == "create":
            data["owner"] = "opennebula"
            owner = auth.models.User.objects.get(username=data["owner"])
            host = models.Host(hostname=data["hostname"],
                    vlan=models.Vlan.objects.get(name=data["vlan"]),
                    mac=data["mac"], ipv4=data["ip"], owner=owner,
                    description=data["description"], pub_ipv4=models.
                        Vlan.objects.get(name=data["vlan"]).snat_ip,
                    shared_ip=True)
            host.full_clean()
            host.save()
66

67
            host.enable_net()
68

69 70 71 72
            for p in data["portforward"]:
                host.add_port(proto=p["proto"],
                        public=int(p["public_port"]),
                        private=int(p["private_port"]))
root committed
73

74 75 76 77 78 79
        elif command == "destroy":
            data["owner"] = "opennebula"
            print data["hostname"]
            owner = auth.models.User.objects.get(username=data["owner"])
            host = models.Host.objects.get(hostname=data["hostname"],
                    owner=owner)
root committed
80

81 82 83 84
            host.del_rules()
            host.delete()
        else:
            raise Exception(_("Unknown command."))
85

86 87 88 89
    except (ValidationError, IntegrityError, AttributeError, Exception) as e:
        return HttpResponse(_("Something went wrong!\n%s\n") % e);
    except:
        return HttpResponse(_("Something went wrong!\n"));
90
 
Bach Dániel committed
91
    return HttpResponse(unicode(_("OK")));