2018-11-25 21:02:16 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
2018-11-25 15:05:26 +00:00
|
|
|
from django.http import HttpResponse
|
2019-04-21 12:13:40 +00:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
|
|
|
|
from django.contrib.auth.forms import AdminPasswordChangeForm
|
2018-11-25 21:02:16 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.utils import timezone
|
2018-11-28 21:35:57 +00:00
|
|
|
from django.core.files import File
|
|
|
|
from django.db.models.fields.files import FieldFile
|
2019-01-13 12:55:53 +00:00
|
|
|
from django.contrib.auth.models import User
|
2019-01-17 08:19:26 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, user_passes_test
|
2019-01-31 19:58:08 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.conf import settings
|
2019-02-02 11:57:15 +00:00
|
|
|
from django.contrib.auth.forms import PasswordResetForm
|
2019-02-15 10:52:56 +00:00
|
|
|
from django.urls import reverse
|
2018-12-25 23:25:53 +00:00
|
|
|
|
2019-01-31 19:58:08 +00:00
|
|
|
from .models import Device, Organization, Network, Model, Wifi, UserStatus
|
2019-01-18 12:50:41 +00:00
|
|
|
from .signals import *
|
2019-02-02 14:31:25 +00:00
|
|
|
from .forms import *
|
2019-02-02 08:18:57 +00:00
|
|
|
from .device import makewificonfig, heartbeathandler
|
2019-02-03 15:15:28 +00:00
|
|
|
from .tasks import mkfirmware
|
2018-11-25 15:05:26 +00:00
|
|
|
|
2018-11-28 21:35:57 +00:00
|
|
|
from distutils.dir_util import copy_tree
|
2019-02-07 20:52:59 +00:00
|
|
|
from celery.exceptions import TimeoutError
|
2019-04-21 12:13:40 +00:00
|
|
|
from subprocess import call as execute
|
2018-11-28 21:35:57 +00:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2018-11-26 19:55:09 +00:00
|
|
|
import os
|
|
|
|
import socket
|
2018-11-28 21:35:57 +00:00
|
|
|
import tempfile
|
|
|
|
import crypt
|
|
|
|
import tarfile
|
2019-04-21 12:13:40 +00:00
|
|
|
from datetime import datetime
|
2019-01-13 16:56:00 +00:00
|
|
|
import time
|
2019-02-02 11:57:15 +00:00
|
|
|
import uuid
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
def is_superuser(user):
|
|
|
|
return user.is_superuser
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
def is_staff(user):
|
|
|
|
return user.is_staff
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2018-11-25 15:05:26 +00:00
|
|
|
def index(request):
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("devices"))
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2018-11-25 21:02:16 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def heartbeat(request):
|
|
|
|
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
|
2018-12-01 17:56:29 +00:00
|
|
|
ip = request.POST.get("ip", "")
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2019-02-02 08:18:57 +00:00
|
|
|
return HttpResponse(heartbeathandler(device, ip))
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def wireless(request):
|
|
|
|
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
|
2019-02-02 08:18:57 +00:00
|
|
|
device.wireless = timezone.now()
|
|
|
|
device.save()
|
2018-12-28 09:43:57 +00:00
|
|
|
return HttpResponse(makewificonfig(device))
|
|
|
|
|
|
|
|
|
2018-11-26 19:55:09 +00:00
|
|
|
@csrf_exempt
|
2018-11-25 21:02:16 +00:00
|
|
|
def hosts(request):
|
|
|
|
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
|
2019-01-18 12:50:41 +00:00
|
|
|
sigRebootDevice(device.serial, None, False)
|
2018-11-28 15:38:35 +00:00
|
|
|
device.reboot = False
|
|
|
|
device.save()
|
2018-11-25 15:05:26 +00:00
|
|
|
return render(request, "manager/hosts", {"device": device})
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def update(request):
|
|
|
|
FWDIR = "/opt/vpnmanager/images/"
|
|
|
|
|
|
|
|
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2019-02-07 20:52:59 +00:00
|
|
|
try:
|
2019-04-21 12:13:40 +00:00
|
|
|
process = mkfirmware.apply_async((device, FWDIR), serializer='pickle')
|
|
|
|
if not process.get(timeout=300):
|
2019-02-07 20:52:59 +00:00
|
|
|
return HttpResponse(status=503)
|
|
|
|
except TimeoutError:
|
2018-12-25 23:25:53 +00:00
|
|
|
return HttpResponse(status=503)
|
|
|
|
|
2019-01-18 12:50:41 +00:00
|
|
|
sigUpdateDevice(device.serial, None, False)
|
2018-12-25 23:25:53 +00:00
|
|
|
device.update = False
|
|
|
|
device.save()
|
|
|
|
|
|
|
|
with open("%s/%s.bin" % (FWDIR, device.id), "rb") as download:
|
2019-04-21 12:13:40 +00:00
|
|
|
response = HttpResponse(
|
|
|
|
download.read(),
|
|
|
|
content_type="application/octet-stream"
|
|
|
|
)
|
|
|
|
|
|
|
|
response['Content-Disposition'] = (
|
|
|
|
'inline; '
|
|
|
|
'filename=%s.bin' % device.serial
|
|
|
|
)
|
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
return response
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2018-11-26 19:55:09 +00:00
|
|
|
def ping(request, device_id):
|
|
|
|
if request.user.is_authenticated:
|
2019-02-11 14:09:56 +00:00
|
|
|
try:
|
2019-04-21 12:13:40 +00:00
|
|
|
device = Device.objects.get(
|
|
|
|
id=device_id,
|
|
|
|
organization__in=request.user.organization_set.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception:
|
2019-02-11 14:09:56 +00:00
|
|
|
device = None
|
2018-11-28 15:38:35 +00:00
|
|
|
|
2019-02-11 14:09:56 +00:00
|
|
|
ajax = '{\n "status": '
|
2018-11-26 19:55:09 +00:00
|
|
|
|
|
|
|
if not device:
|
2019-04-21 12:13:40 +00:00
|
|
|
status = "-1"
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2018-11-28 15:38:35 +00:00
|
|
|
else:
|
2019-04-21 12:13:40 +00:00
|
|
|
tz = timezone.get_current_timezone()
|
|
|
|
if device.lasttime:
|
|
|
|
naive = timezone.make_naive(device.lasttime, tz)
|
|
|
|
tt = naive.timetuple()
|
|
|
|
lasttime = str(int(time.mktime(tt)) * 1000)
|
|
|
|
else:
|
|
|
|
lasttime = None
|
|
|
|
|
|
|
|
if device.lastbeat:
|
|
|
|
naive = timezone.make_naive(device.lastbeat, tz)
|
|
|
|
tt = naive.timetuple()
|
|
|
|
lastbeat = str(int(time.mktime(tt)) * 1000)
|
|
|
|
else:
|
|
|
|
lastbeat = None
|
|
|
|
|
|
|
|
status = None
|
|
|
|
|
2018-11-28 15:38:35 +00:00
|
|
|
try:
|
2019-02-11 14:09:56 +00:00
|
|
|
if device.curip:
|
2019-04-21 12:13:40 +00:00
|
|
|
try:
|
|
|
|
socket.inet_aton(device.curip)
|
|
|
|
except Exception:
|
|
|
|
status = -3
|
|
|
|
else:
|
|
|
|
if device.lasttime:
|
|
|
|
diff = timezone.now() - device.lasttime
|
|
|
|
secs = diff.total_seconds()
|
|
|
|
if secs < 300:
|
|
|
|
cmd = "ping -c1 -w1 %s >/dev/null 2>&1"
|
|
|
|
status = os.system(cmd % device.curip)
|
|
|
|
if not os.WEXITSTATUS(status):
|
|
|
|
status = 1
|
|
|
|
|
|
|
|
if not status:
|
|
|
|
if (not device.lasttime) or secs > 120:
|
|
|
|
diff = timezone.now() - device.lastbeat
|
|
|
|
secs = diff.total_seconds()
|
|
|
|
if secs < 60:
|
|
|
|
status = 2
|
|
|
|
|
|
|
|
if not status:
|
|
|
|
status = 0
|
|
|
|
|
2019-01-13 17:12:09 +00:00
|
|
|
except Exception as e:
|
2019-04-21 12:13:40 +00:00
|
|
|
status = str(e)
|
|
|
|
status += "-4"
|
|
|
|
finally:
|
|
|
|
ajax += str(status)
|
|
|
|
ajax += ',\n "serial": "%s"' % device.serial
|
|
|
|
ajax += ',\n "name": "%s"' % (device.name or "")
|
|
|
|
ajax += ',\n "ip": "%s"' % (device.curip or "")
|
|
|
|
ajax += ',\n "time": "%s"' % lasttime
|
|
|
|
ajax += ',\n "lastbeat": "%s"' % lastbeat
|
|
|
|
ajax += ',\n "reboot": %i' % (1 if device.reboot else 0)
|
|
|
|
ajax += ',\n "update": %i' % (1 if device.update else 0)
|
|
|
|
|
|
|
|
ajax += ',\n "network": {'
|
|
|
|
ajax += '\n "intip": "%s"' % device.network.intip
|
|
|
|
ajax += ',\n "extip": "%s"' % device.network.extip
|
|
|
|
ajax += ',\n "name": "%s"' % (device.network.name or "")
|
|
|
|
ajax += '\n }'
|
2018-11-26 19:55:09 +00:00
|
|
|
|
|
|
|
else:
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax += "-2"
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax += "\n}"
|
2018-12-15 11:35:00 +00:00
|
|
|
return HttpResponse(ajax, content_type="application/json")
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2019-04-18 13:59:06 +00:00
|
|
|
def devices(request, code=200, exception=None):
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/index.html",
|
|
|
|
{
|
|
|
|
"title": "Device Administration",
|
|
|
|
"exception": exception
|
|
|
|
},
|
|
|
|
status=code
|
|
|
|
)
|
|
|
|
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2018-11-25 21:02:16 +00:00
|
|
|
def editdevice(request, device_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
post = request.method == "POST"
|
|
|
|
|
|
|
|
device = get_object_or_404(
|
|
|
|
Device,
|
|
|
|
id=device_id,
|
|
|
|
organization__in=request.user.organization_set.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
subnets = Network.objects.filter(
|
|
|
|
organization=device.organization
|
|
|
|
)
|
|
|
|
|
|
|
|
wifis = Wifi.objects.filter(
|
|
|
|
organization=device.organization
|
|
|
|
)
|
|
|
|
|
|
|
|
form = DeviceForm(
|
|
|
|
request.POST if post else None,
|
|
|
|
instance=device
|
|
|
|
)
|
|
|
|
|
|
|
|
form.fields["mac"].disabled = True
|
|
|
|
form.fields["serial"].disabled = True
|
|
|
|
form.fields["secret"].disabled = True
|
|
|
|
form.fields["model"].disabled = True
|
|
|
|
form.fields["password"].disabled = True
|
|
|
|
|
|
|
|
if post and form.is_valid():
|
2019-03-16 17:06:27 +00:00
|
|
|
form.save()
|
|
|
|
return redirect(reverse("devices"))
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Edit Device",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-02-02 11:57:15 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def makeuser(request):
|
|
|
|
if request.POST.get("username", ""):
|
2019-04-21 12:13:40 +00:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username=request.POST.get("username", ""),
|
|
|
|
password=str(uuid.uuid4().hex),
|
|
|
|
first_name=request.POST.get("firstname", ""),
|
2019-02-15 10:52:56 +00:00
|
|
|
last_name=request.POST.get("lastname", ""),
|
2019-04-21 12:13:40 +00:00
|
|
|
is_staff=request.POST.get("staff", "0") == "True",
|
|
|
|
is_superuser=request.POST.get("superuser", "0") == "True",
|
2019-02-15 10:52:56 +00:00
|
|
|
email=request.POST.get("email", "")
|
2019-02-02 11:57:15 +00:00
|
|
|
)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
user.organization_set.set(set(request.POST.getlist("orga", [])))
|
2019-02-02 11:57:15 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
form = PasswordResetForm({"email": user.email})
|
2019-02-02 11:57:15 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
if form.is_valid():
|
|
|
|
form.save(
|
|
|
|
request=request,
|
|
|
|
use_https=True,
|
|
|
|
email_template_name='registration/add_user.html')
|
|
|
|
|
|
|
|
return redirect(reverse("users"))
|
2019-02-02 11:57:15 +00:00
|
|
|
|
|
|
|
else:
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(request, "manager/adduser.html", {"title": "Add User"})
|
|
|
|
|
2019-02-02 11:57:15 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2019-01-13 12:55:53 +00:00
|
|
|
def edituser(request, user_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
if request.user.is_staff or request.user.id == user_id:
|
|
|
|
orgas = request.user.organization_set.all()
|
|
|
|
user = User.objects.distinct().get(id=user_id, organization__in=orgas)
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
return redirect(reverse("users"))
|
|
|
|
|
|
|
|
if request.POST.get("form", ""):
|
|
|
|
newfirst = request.POST.get("firstname", "")
|
|
|
|
newlast = request.POST.get("lastname", "")
|
|
|
|
|
|
|
|
if newlast != user.last_name or newfirst != user.first_name:
|
|
|
|
sigRenameUser(
|
|
|
|
user.username,
|
|
|
|
request.user.username,
|
|
|
|
"%s %s" % (user.first_name, user.last_name),
|
|
|
|
"%s %s" % (newfirst, newlast)
|
|
|
|
)
|
|
|
|
user.first_name = request.POST.get("firstname", "")
|
|
|
|
user.last_name = request.POST.get("lastname", "")
|
|
|
|
|
|
|
|
if request.user.is_staff or request.user.is_superuser:
|
|
|
|
newstaff = request.POST.get("staff", "0") == "True"
|
|
|
|
if newstaff != user.is_staff:
|
|
|
|
sigStaffUser(
|
|
|
|
user.username,
|
|
|
|
request.user.username,
|
|
|
|
newstaff
|
|
|
|
)
|
|
|
|
user.is_staff = newstaff
|
|
|
|
|
|
|
|
if request.user.is_superuser:
|
|
|
|
newsuper = request.POST.get("superuser", "0") == "True"
|
|
|
|
if newsuper != user.is_superuser:
|
|
|
|
sigSuperUser(
|
|
|
|
user.username,
|
|
|
|
request.user.username,
|
|
|
|
newsuper)
|
|
|
|
user.is_superuser = newsuper
|
|
|
|
|
|
|
|
neworgas = set(request.POST.getlist("orga", []))
|
|
|
|
oldorgas = set(user.organization_set.all())
|
|
|
|
if neworgas != oldorgas:
|
|
|
|
sigOrgaUser(
|
|
|
|
user.username,
|
|
|
|
request.user.username,
|
|
|
|
oldorgas,
|
|
|
|
neworgas
|
|
|
|
)
|
|
|
|
user.organization_set.set(neworgas)
|
|
|
|
|
|
|
|
newmail = request.POST.get("email", "")
|
|
|
|
if newmail != user.email:
|
|
|
|
sigMailUser(
|
|
|
|
user.username,
|
|
|
|
request.user.username,
|
|
|
|
user.email,
|
|
|
|
newmail
|
|
|
|
)
|
|
|
|
user.email = newmail
|
|
|
|
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
return redirect(reverse("users"))
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/edituser.html",
|
|
|
|
{
|
2019-01-17 08:19:26 +00:00
|
|
|
"title": "Edit User",
|
2019-02-02 11:57:15 +00:00
|
|
|
"auser": user
|
2019-01-17 08:19:26 +00:00
|
|
|
}
|
|
|
|
)
|
2019-01-13 12:55:53 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return redirect(
|
|
|
|
reverse(
|
|
|
|
"two_factor:login",
|
|
|
|
kwargs={
|
|
|
|
'next': request.path
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-01-13 12:55:53 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2018-12-28 09:43:57 +00:00
|
|
|
def editwifi(request, wifi_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
userorgas = request.user.organization_set.all()
|
|
|
|
|
|
|
|
wifi = get_object_or_404(
|
|
|
|
Wifi,
|
|
|
|
id=wifi_id,
|
|
|
|
organization__in=userorgas
|
|
|
|
)
|
|
|
|
|
|
|
|
form = WifiForm(
|
|
|
|
request.POST if request.method == "POST" else None,
|
|
|
|
instance=wifi
|
|
|
|
)
|
|
|
|
|
|
|
|
form.fields["organization"].queryset = userorgas
|
|
|
|
|
2019-03-16 17:06:27 +00:00
|
|
|
if request.method == "POST" and form.is_valid():
|
|
|
|
form.save()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("wifi"))
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Edit WiFi",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_superuser)
|
2018-11-28 21:35:57 +00:00
|
|
|
def getconfig(request, device_id):
|
2018-12-25 23:25:53 +00:00
|
|
|
FWDIR = "/opt/vpnmanager/images/"
|
|
|
|
|
2018-11-28 21:35:57 +00:00
|
|
|
device = get_object_or_404(Device, id=device_id)
|
2019-02-24 13:08:32 +00:00
|
|
|
files = glob.glob("%s/%s.bin" % (FWDIR, device_id))
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
if not files:
|
|
|
|
mtime = os.path.getmtime(files[0])
|
|
|
|
tz = timezone.get_current_timezone()
|
|
|
|
|
|
|
|
if not datetime.fromtimestamp(mtime, tz) > device.model.firmware:
|
|
|
|
work = mkfirmware.apply_async((device, FWDIR), serializer='pickle')
|
|
|
|
if not work.get(timeout=300):
|
|
|
|
return HttpResponse(status=503)
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2019-01-18 12:50:41 +00:00
|
|
|
sigUpdateDevice(device.serial, None, False)
|
2018-12-25 23:25:53 +00:00
|
|
|
device.update = False
|
|
|
|
device.save()
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
with open("%s/%s.bin" % (FWDIR, device.id), "rb") as download:
|
2019-04-21 12:13:40 +00:00
|
|
|
response = HttpResponse(
|
|
|
|
download.read(),
|
|
|
|
content_type="application/octet-stream"
|
|
|
|
)
|
|
|
|
|
|
|
|
response['Content-Disposition'] = (
|
|
|
|
'inline; '
|
|
|
|
'filename=%s.bin' % device.serial
|
|
|
|
)
|
|
|
|
|
2018-12-01 17:56:29 +00:00
|
|
|
return response
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2018-11-28 21:35:57 +00:00
|
|
|
def rebootdevice(request, device_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
device = get_object_or_404(
|
|
|
|
Device,
|
|
|
|
id=device_id,
|
|
|
|
organization__in=request.user.organization_set.all()
|
|
|
|
)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-02-15 10:52:56 +00:00
|
|
|
if not device.reboot:
|
2019-04-21 12:13:40 +00:00
|
|
|
sigRebootDevice(device.serial, request.user.username, True)
|
|
|
|
device.reboot = True
|
|
|
|
device.save()
|
2018-12-25 23:25:53 +00:00
|
|
|
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("devices"))
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_staff)
|
2018-12-25 23:25:53 +00:00
|
|
|
def updatedevice(request, device_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
device = get_object_or_404(
|
|
|
|
Device,
|
|
|
|
id=device_id,
|
|
|
|
organization__in=request.user.organization_set.all()
|
|
|
|
)
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2019-02-15 10:52:56 +00:00
|
|
|
if not device.update:
|
|
|
|
sigUpdateDevice(device.serial, request.user.username, True)
|
|
|
|
device.update = True
|
|
|
|
device.save()
|
2018-12-25 23:25:53 +00:00
|
|
|
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("devices"))
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_superuser)
|
2018-11-28 21:35:57 +00:00
|
|
|
def deletedevice(request, device_id):
|
2019-01-17 08:19:26 +00:00
|
|
|
CADIR = "/etc/openvpn/ca/"
|
|
|
|
BEFORE = os.getcwd()
|
2018-12-15 11:35:00 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
device = get_object_or_404(Device, id=device_id)
|
2018-12-15 11:35:00 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
os.chdir(CADIR)
|
2018-12-15 11:35:00 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
execute(CADIR + "/revoke " + device.serial, shell=True)
|
2019-01-17 08:19:26 +00:00
|
|
|
os.system("rm " + CADIR + "/keys/" + device.serial + ".{crt,csr,key}")
|
2018-12-15 11:35:00 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
os.chdir(BEFORE)
|
2018-12-15 11:35:00 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
device.delete()
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("devices"))
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_staff)
|
2019-01-06 18:15:13 +00:00
|
|
|
def deletewifi(request, wifi_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
wifi = get_object_or_404(
|
|
|
|
Wifi,
|
|
|
|
id=wifi_id,
|
|
|
|
organization__in=request.user.organization_set.all()
|
|
|
|
)
|
2019-02-02 12:31:05 +00:00
|
|
|
wifi.delete()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("wifi"))
|
2019-01-06 18:15:13 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-02-02 12:31:05 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def deleteuser(request, user_id):
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
user.delete()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("users"))
|
2019-02-02 12:31:05 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-02-02 15:01:18 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def deletenetwork(request, network_id):
|
|
|
|
network = get_object_or_404(Network, id=network_id)
|
|
|
|
network.delete()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("networks"))
|
2019-02-02 15:01:18 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-02-03 09:51:02 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def deleteorga(request, orga_id):
|
|
|
|
orga = get_object_or_404(Organization, id=orga_id)
|
|
|
|
orga.delete()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("organizations"))
|
2019-02-03 09:51:02 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-02-21 09:46:25 +00:00
|
|
|
@login_required
|
2018-12-28 09:43:57 +00:00
|
|
|
def makewifi(request):
|
2019-04-21 12:13:40 +00:00
|
|
|
post = request.method == "POST"
|
|
|
|
postdata = request.POST if post else None
|
|
|
|
|
|
|
|
organization = request.user.userstatus.orga
|
|
|
|
initial = {"organization": organization} if not post else None
|
|
|
|
|
|
|
|
form = WifiForm(postdata, initial=initial)
|
2019-03-16 17:06:27 +00:00
|
|
|
form.fields["organization"].queryset = request.user.organization_set.all()
|
|
|
|
if request.method == "POST" and form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect(reverse("wifi"))
|
2019-04-21 12:13:40 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Add WiFi",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-02-02 14:31:25 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def makenetwork(request):
|
|
|
|
if request.method == "POST":
|
2019-03-16 13:25:37 +00:00
|
|
|
form = NetworkForm(request.POST)
|
2019-02-02 14:31:25 +00:00
|
|
|
if form.is_valid():
|
2019-03-16 13:25:37 +00:00
|
|
|
form.save()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("networks"))
|
2019-02-02 14:31:25 +00:00
|
|
|
|
|
|
|
else:
|
2019-03-16 13:25:37 +00:00
|
|
|
form = NetworkForm()
|
2019-02-02 14:31:25 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Add Network",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-02-02 14:31:25 +00:00
|
|
|
|
2019-03-16 13:25:37 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def editnetwork(request, network_id):
|
|
|
|
net = get_object_or_404(Network, id=network_id)
|
2019-04-21 12:13:40 +00:00
|
|
|
postdata = request.POST if request.method == "POST" else None
|
|
|
|
form = NetworkForm(postdata, instance=net)
|
2019-03-16 13:25:37 +00:00
|
|
|
form.fields["intip"].disabled = True
|
|
|
|
form.fields["extip"].disabled = True
|
|
|
|
|
|
|
|
if request.method == "POST" and form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect(reverse("networks"))
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Edit Network",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-03-16 13:25:37 +00:00
|
|
|
|
2019-02-19 14:42:13 +00:00
|
|
|
@login_required
|
|
|
|
def setactiveorga(request, orga_id):
|
2019-04-21 12:13:40 +00:00
|
|
|
request.user.userstatus.orga = get_object_or_404(
|
|
|
|
Organization,
|
|
|
|
id=orga_id,
|
|
|
|
users=request.user
|
|
|
|
)
|
|
|
|
|
2019-02-19 14:42:13 +00:00
|
|
|
request.user.userstatus.save()
|
|
|
|
return HttpResponse("")
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-02-03 09:51:02 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def makeorganization(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
form = OrgaForm(request.POST)
|
|
|
|
if form.is_valid():
|
2019-03-16 13:25:37 +00:00
|
|
|
orga = form.save()
|
2019-02-03 09:51:02 +00:00
|
|
|
request.user.organization_set.add(orga)
|
2019-02-19 13:53:03 +00:00
|
|
|
Network.objects.get(intip="No VPN").organization.add(orga)
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("organizations"))
|
2019-02-03 09:51:02 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
form = OrgaForm()
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Add Organization",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-02-03 09:51:02 +00:00
|
|
|
|
2019-02-03 10:35:52 +00:00
|
|
|
@user_passes_test(is_superuser)
|
|
|
|
def editorganization(request, orga_id):
|
|
|
|
orga = get_object_or_404(Organization, id=orga_id)
|
|
|
|
if request.method == "POST":
|
2019-03-16 13:25:37 +00:00
|
|
|
form = OrgaForm(request.POST, instance=orga)
|
2019-02-03 10:35:52 +00:00
|
|
|
if form.is_valid():
|
2019-03-16 13:25:37 +00:00
|
|
|
form.save()
|
2019-02-15 10:52:56 +00:00
|
|
|
return redirect(reverse("organizations"))
|
2019-02-03 10:35:52 +00:00
|
|
|
|
|
|
|
else:
|
2019-03-16 13:25:37 +00:00
|
|
|
form = OrgaForm(instance=orga)
|
2019-02-03 10:35:52 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"manager/form.html",
|
|
|
|
{
|
|
|
|
"title": "Change Organization",
|
|
|
|
"form": form
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-02-03 10:35:52 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_superuser)
|
2018-11-28 21:35:57 +00:00
|
|
|
def makedevice(request):
|
2019-04-21 12:13:40 +00:00
|
|
|
CADIR = "/etc/openvpn/ca/"
|
|
|
|
CONFIGDIR = "/etc/openvpn/client-configs/"
|
|
|
|
BEFORE = os.getcwd()
|
|
|
|
|
|
|
|
device_serial = request.POST.get("serial", "").replace(" ", "_")
|
|
|
|
device_name = request.POST.get("name", "")
|
|
|
|
device_organization = request.POST.get("organization", "")
|
|
|
|
device_model = request.POST.get("model", "")
|
|
|
|
device_ssid = request.POST.get("ssid", device_serial)
|
|
|
|
device_key = request.POST.get("key", "")
|
|
|
|
device_mac = request.POST.get("mac", "")
|
|
|
|
|
|
|
|
if not device_serial:
|
|
|
|
orga = Organization.objects.all()
|
|
|
|
models = Model.objects.all()
|
|
|
|
|
|
|
|
return render(request, "manager/add.html",
|
|
|
|
{
|
|
|
|
"title": "Add Device",
|
|
|
|
"organizations": orga,
|
|
|
|
"models": models
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if glob.glob(CADIR + "/keys/" + device_serial + "*"):
|
|
|
|
return HttpResponse("Key already exists.")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
os.chdir(CADIR)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
if execute(CADIR + "/generate-key " + device_serial, shell=True):
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
return HttpResponse("Error generating key.")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
if glob.glob(CONFIGDIR + "/files/" + device_serial + "*"):
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
return HttpResponse("Configuration file already exists.")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
os.chdir(CONFIGDIR)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
if execute(CONFIGDIR + "/make_config " + device_serial, shell=True):
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
return HttpResponse("Error generating config file.")
|
|
|
|
|
|
|
|
os.chdir(BEFORE)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
with open(CONFIGDIR + "/files/" + device_serial + ".ovpn") as vpnfile:
|
|
|
|
vpnconfig = vpnfile.read()
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
organization = Organization.objects.get(id=device_organization)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
device = Device.objects.create(
|
2018-11-28 21:35:57 +00:00
|
|
|
serial=device_serial,
|
|
|
|
name=device_name,
|
2019-03-08 15:16:37 +00:00
|
|
|
ssid=device_ssid,
|
|
|
|
key=device_key,
|
2018-12-15 11:35:00 +00:00
|
|
|
model=Model.objects.filter(id=device_model)[0],
|
2018-12-01 17:56:29 +00:00
|
|
|
network=Network.objects.filter(intip="No VPN")[0],
|
2019-04-21 12:13:40 +00:00
|
|
|
organization=organization,
|
|
|
|
vpnconfig=vpnconfig,
|
|
|
|
mac=device_mac or None,
|
|
|
|
changed=timezone.now()
|
2018-11-28 21:35:57 +00:00
|
|
|
)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
return redirect(reverse("devices"))
|
|
|
|
|
2019-01-31 19:58:08 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
|
|
|
|
def createUserStatus(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
UserStatus.objects.create(user=instance)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-01-31 19:58:08 +00:00
|
|
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
|
|
|
|
def saveUserStatus(sender, instance, **kwargs):
|
2019-02-01 08:27:34 +00:00
|
|
|
try:
|
|
|
|
instance.userstatus.save()
|
2019-04-21 12:13:40 +00:00
|
|
|
except Exception:
|
2019-02-01 08:27:34 +00:00
|
|
|
UserStatus.objects.create(user=instance)
|
2019-01-31 19:58:08 +00:00
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
def errorhandler(request, code, exception):
|
|
|
|
if code == 403:
|
2019-04-21 12:13:40 +00:00
|
|
|
exception.human = ("You have no permission "
|
|
|
|
"to access the requested page.")
|
2019-04-18 13:59:06 +00:00
|
|
|
elif code == 400:
|
2019-04-21 12:13:40 +00:00
|
|
|
exception.human = ("Something seems to be wrong with "
|
|
|
|
"the request you sent. Please try again.")
|
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
elif code == 404:
|
2019-04-21 12:13:40 +00:00
|
|
|
exception.human = ("Sorry, we were unable to find the page you "
|
|
|
|
"requested. Please check the address and try again."
|
|
|
|
)
|
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
elif code == 500:
|
2019-04-21 12:13:40 +00:00
|
|
|
exception.human = ("Sorry, something seems to be wrong on our end. "
|
|
|
|
"Please try again later or contact support if "
|
|
|
|
"the problem persists.")
|
2019-04-18 13:59:06 +00:00
|
|
|
|
|
|
|
return devices(request, code=code, exception=exception)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
def handler400(request, exception):
|
|
|
|
return errorhandler(request, 400, exception)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
def handler403(request, exception):
|
|
|
|
return errorhandler(request, 403, exception)
|
|
|
|
|
2019-04-21 12:13:40 +00:00
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
def handler404(request, exception):
|
2019-04-21 12:13:40 +00:00
|
|
|
return errorhandler(
|
|
|
|
request,
|
|
|
|
404,
|
|
|
|
exception
|
|
|
|
)
|
|
|
|
|
2019-04-18 13:59:06 +00:00
|
|
|
|
|
|
|
def handler500(request):
|
2019-04-21 12:13:40 +00:00
|
|
|
return errorhandler(
|
|
|
|
request,
|
|
|
|
500,
|
|
|
|
Exception("Please check server logs for additional information.")
|
|
|
|
)
|