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-01-13 12:55:53 +00:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm, 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
|
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 *
|
2018-11-25 15:05:26 +00:00
|
|
|
|
2018-11-28 21:35:57 +00:00
|
|
|
from distutils.dir_util import copy_tree
|
|
|
|
|
|
|
|
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
|
2018-12-25 23:25:53 +00:00
|
|
|
import datetime
|
2019-01-13 16:56:00 +00:00
|
|
|
import time
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
def is_superuser(user):
|
|
|
|
return user.is_superuser
|
|
|
|
|
|
|
|
def is_staff(user):
|
|
|
|
return user.is_staff
|
|
|
|
|
|
|
|
@login_required
|
2018-11-25 15:05:26 +00:00
|
|
|
def index(request):
|
2019-01-17 08:19:26 +00:00
|
|
|
return redirect("/devices")
|
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
|
|
|
|
2018-12-26 01:24:36 +00:00
|
|
|
if device.update:
|
2019-01-18 12:50:41 +00:00
|
|
|
sigRebootDevice(device.serial, None, False)
|
2018-12-28 09:43:57 +00:00
|
|
|
device.reboot = False
|
|
|
|
code = """
|
2018-12-26 01:24:36 +00:00
|
|
|
. /etc/vpnsecret
|
|
|
|
if /usr/bin/wget -O/tmp/update.bin https://admin360.kumi.host/update --post-data "secret=$SECRET" --no-check-certificate 2>/var/log/wget;
|
|
|
|
then
|
|
|
|
/sbin/sysupgrade -F -n /tmp/update.bin
|
|
|
|
fi
|
2018-12-28 09:52:28 +00:00
|
|
|
"""
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-01-31 07:39:54 +00:00
|
|
|
elif device.model.wwan and device.lastbeat < device.changed:
|
2018-12-28 09:43:57 +00:00
|
|
|
code = """
|
|
|
|
. /etc/vpnsecret
|
|
|
|
if /usr/bin/wget -O/tmp/wireless.in https://admin360.kumi.host/wireless --post-data "secret=$SECRET" --no-check-certificate 2>/var/log/wget;
|
|
|
|
then
|
|
|
|
while read p
|
|
|
|
do
|
2019-01-03 17:29:49 +00:00
|
|
|
echo $p >/tmp/wireless.line
|
|
|
|
if /bin/grep wifi-iface /tmp/wireless.line && ! /bin/grep radio /tmp/wireless.line;
|
2018-12-28 09:43:57 +00:00
|
|
|
then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
echo $p >>/tmp/wireless.og
|
|
|
|
done </etc/config/wireless
|
|
|
|
/bin/cat /tmp/wireless.in >>/tmp/wireless.og
|
2019-01-03 17:29:49 +00:00
|
|
|
/bin/cp /tmp/wireless.og /etc/config/wireless
|
2018-12-28 09:43:57 +00:00
|
|
|
/sbin/uci commit /etc/config/wireless
|
|
|
|
/bin/rm /tmp/wireless.*
|
|
|
|
fi
|
2018-12-28 09:52:57 +00:00
|
|
|
"""
|
2019-01-03 17:29:49 +00:00
|
|
|
device.lastbeat = timezone.now()
|
2018-12-26 01:24:36 +00:00
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
elif device.reboot:
|
|
|
|
code = "/sbin/reboot"
|
|
|
|
|
|
|
|
else:
|
|
|
|
code = ""
|
|
|
|
device.lastbeat = timezone.now()
|
|
|
|
if ip:
|
|
|
|
device.lasttime = timezone.now()
|
|
|
|
device.curip = ip
|
|
|
|
|
|
|
|
device.save()
|
2018-12-26 01:24:36 +00:00
|
|
|
|
|
|
|
return HttpResponse(code)
|
2018-11-25 15:05:26 +00:00
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
def makewificonfig(device):
|
2019-01-03 17:29:49 +00:00
|
|
|
output = ""
|
|
|
|
i = 1
|
2018-12-28 10:08:30 +00:00
|
|
|
for wifi in device.wifi.all():
|
2019-01-03 17:29:49 +00:00
|
|
|
output += """config wifi-iface
|
|
|
|
option network 'wwan%i'
|
2018-12-28 09:43:57 +00:00
|
|
|
option ssid '%s'
|
|
|
|
option encryption 'psk2'
|
|
|
|
option device 'radio1'
|
|
|
|
option mode 'sta'
|
|
|
|
option key '%s'
|
2019-01-03 17:29:49 +00:00
|
|
|
""" % (i, wifi.ssid, wifi.key)
|
|
|
|
i += 1
|
2018-12-28 09:43:57 +00:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def wireless(request):
|
|
|
|
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
|
|
|
|
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
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
def mkfirmware(device, path):
|
2018-12-28 10:06:06 +00:00
|
|
|
if device.firmware and device.firmware > device.model.firmware and device.firmware > device.changed and glob.glob("%s/%s.bin" % (path, device.id)):
|
2018-12-25 23:25:53 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
BEFORE = os.getcwd()
|
|
|
|
DEVICEDIR = "/opt/vpnmanager/device-config/%i/" % device.model.id
|
|
|
|
SRCDIR = "/opt/vpnmanager/imagebuilder/%i/" % device.model.id
|
|
|
|
|
|
|
|
if glob.glob(SRCDIR + "/.kumilock"):
|
|
|
|
return False
|
|
|
|
|
|
|
|
with open(SRCDIR + "/.kumilock", "w") as lock:
|
|
|
|
lock.write("")
|
|
|
|
|
|
|
|
tempdir = tempfile.TemporaryDirectory()
|
|
|
|
|
|
|
|
copy_tree(DEVICEDIR, tempdir.name)
|
|
|
|
|
|
|
|
# Write OpenVPN config
|
|
|
|
|
|
|
|
with open(tempdir.name + "/etc/openvpn/client.conf", "w") as vpnconf:
|
|
|
|
vpnconf.write(device.vpnconfig)
|
|
|
|
|
|
|
|
# Write secret
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
with open(tempdir.name + "/etc/vpnsecret", "w") as secret:
|
|
|
|
secret.write('SECRET="%s"' % device.secret)
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
# Write password
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
with open(tempdir.name + "/etc/shadow", "r") as shadow:
|
|
|
|
password = crypt.crypt(device.password, crypt.mksalt(crypt.METHOD_MD5))
|
|
|
|
shadowin = shadow.read()
|
2019-01-17 08:19:26 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
with open(tempdir.name + "/etc/shadow", "w") as shadowout:
|
|
|
|
shadowout.write(shadowin.replace("$PASSWORD", password))
|
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
# Write Wireless config
|
2018-12-25 23:25:53 +00:00
|
|
|
|
|
|
|
with open(tempdir.name + "/etc/config/wireless", "r") as wireless:
|
|
|
|
wirein = wireless.read()
|
|
|
|
|
|
|
|
with open(tempdir.name + "/etc/config/wireless", "w") as wireout:
|
|
|
|
wire = wirein.replace("$SSID", device.serial)
|
2018-12-28 09:43:57 +00:00
|
|
|
wireout.write(wire + "\n" + makewificonfig(device))
|
2018-12-25 23:25:53 +00:00
|
|
|
|
|
|
|
# Create compilation environment
|
|
|
|
|
|
|
|
os.system("rm -rf " + SRCDIR + "/files/")
|
|
|
|
os.mkdir(SRCDIR + "/files/")
|
|
|
|
os.system("cp -r " + tempdir.name + "/* " + SRCDIR + "/files/")
|
|
|
|
|
|
|
|
tempdir.cleanup()
|
|
|
|
|
|
|
|
os.system("rm " + SRCDIR + "/bin/targets/ar71xx/generic/*")
|
|
|
|
|
|
|
|
# Build image
|
|
|
|
|
|
|
|
os.chdir(SRCDIR)
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.call(["/usr/bin/make"])
|
|
|
|
except:
|
|
|
|
os.remove(SRCDIR + "/.kumilock")
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
return False
|
|
|
|
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
|
|
|
|
os.rename(glob.glob(SRCDIR + "/bin/targets/ar71xx/generic/*squashfs-sysupgrade.bin")[0], "%s/%s.bin" % (path, device.id))
|
|
|
|
os.remove(SRCDIR + "/.kumilock")
|
|
|
|
os.system("rm -rf " + SRCDIR + "/files/")
|
|
|
|
os.system("rm " + SRCDIR + "/bin/targets/ar71xx/generic/*")
|
|
|
|
device.firmware = datetime.datetime.now()
|
|
|
|
device.save()
|
|
|
|
return True
|
|
|
|
|
|
|
|
@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
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
if not mkfirmware(device, FWDIR):
|
|
|
|
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:
|
|
|
|
response = HttpResponse(download.read(), content_type="application/octet-stream")
|
|
|
|
response['Content-Disposition'] = 'inline; filename=%s.bin' % device.serial
|
|
|
|
return response
|
|
|
|
|
2018-11-26 19:55:09 +00:00
|
|
|
def ping(request, device_id):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
device = None
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax = '{\n "status": '
|
|
|
|
|
2018-11-26 19:55:09 +00:00
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
device = device or Device.objects.filter(id=device_id, organization=organization)
|
|
|
|
|
|
|
|
if not device:
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax += "-1"
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2018-11-28 15:38:35 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
socket.inet_aton(device[0].curip)
|
2019-01-13 17:12:09 +00:00
|
|
|
ajax += str(1 if not os.WEXITSTATUS(os.system("ping -c1 -w1 " + device[0].curip + " > /dev/null 2>&1")) else 2 if (timezone.now() - device[0].lasttime).total_seconds() > 120 and (timezone.now() - device[0].lastbeat).total_seconds() < 60 else 0)
|
2018-12-15 11:35:00 +00:00
|
|
|
ajax += ',\n "serial": "%s"' % device[0].serial
|
2018-12-28 09:43:57 +00:00
|
|
|
ajax += ',\n "name": "%s"' % device[0].name if device[0].name else ""
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax += ',\n "ip": "%s"' % device[0].curip
|
2019-01-13 16:56:00 +00:00
|
|
|
ajax += ',\n "time": "%i"' % (int(time.mktime(timezone.make_naive(device[0].lasttime, timezone.get_current_timezone()).timetuple())) * 1000)
|
|
|
|
ajax += ',\n "lastbeat": "%s"' % (int(time.mktime(timezone.make_naive(device[0].lastbeat, timezone.get_current_timezone()).timetuple())) * 1000)
|
2018-12-15 11:35:00 +00:00
|
|
|
ajax += ',\n "reboot": %i' % (1 if device[0].reboot else 0)
|
2018-12-26 01:24:36 +00:00
|
|
|
ajax += ',\n "update": %i' % (1 if device[0].update else 0)
|
2018-12-15 11:35:00 +00:00
|
|
|
|
|
|
|
ajax += ',\n "network": {'
|
|
|
|
ajax += '\n "intip": "%s"' % device[0].network.intip
|
2019-01-03 18:20:13 +00:00
|
|
|
ajax += ',\n "extip": "%s"' % device[0].network.extip
|
2019-01-17 08:47:41 +00:00
|
|
|
ajax += ',\n "name": "%s"' % (device[0].network.name if device[0].network.name else "")
|
2018-12-15 11:35:00 +00:00
|
|
|
ajax += '\n }'
|
2018-11-26 19:55:09 +00:00
|
|
|
|
2019-01-13 17:12:09 +00:00
|
|
|
except Exception as e:
|
2018-11-28 15:38:35 +00:00
|
|
|
ajax += "-3"
|
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
|
|
|
|
|
|
|
@login_required
|
2018-11-25 21:02:16 +00:00
|
|
|
def devices(request):
|
2019-02-01 14:38:37 +00:00
|
|
|
return render(request, "manager/index.html", {"title": "Device Administration"})
|
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-01-17 08:19:26 +00:00
|
|
|
device = None
|
|
|
|
subnets = set()
|
|
|
|
wifis = set()
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
device = device or Device.objects.filter(id=device_id, organization=organization)
|
|
|
|
if not device:
|
|
|
|
return redirect("/")
|
2019-01-05 15:08:19 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
for subnet in Network.objects.filter(organizations=device[0].organization):
|
|
|
|
subnets.add(subnet)
|
2019-01-05 15:08:19 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
for wifi in Wifi.objects.filter(organization=device[0].organization):
|
|
|
|
wifis.add(wifi)
|
2019-01-05 15:08:19 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
if request.POST.get("subnet", ""):
|
|
|
|
subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip if device[0].network else "No VPN"))
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
if subnet[0] in subnets:
|
2019-01-18 12:50:41 +00:00
|
|
|
newname = request.POST.get("name", "")
|
|
|
|
if newname != device[0].name:
|
|
|
|
sigRenameDevice(device[0].serial, request.user.username, device[0].name, newname)
|
|
|
|
device[0].name = newname
|
|
|
|
|
|
|
|
if subnet[0] != device[0].network:
|
|
|
|
sigNetDevice(device[0].serial, request.user.username, str(device[0].network), str(subnet[0]))
|
|
|
|
device[0].network = subnet[0]
|
|
|
|
|
|
|
|
newreboot = True if request.POST.get("reboot", "0") == "True" else False
|
|
|
|
if newreboot != device[0].reboot:
|
|
|
|
sigRebootDevice(device[0].serial, request.user.username, newreboot)
|
|
|
|
device[0].reboot = newreboot
|
|
|
|
|
|
|
|
newupdate = True if request.POST.get("update", "0") == "True" else False
|
|
|
|
if newupdate != device[0].update:
|
|
|
|
sigUpdateDevice(device[0].serial, request.user.username, newupdate)
|
|
|
|
device[0].update = newupdate
|
|
|
|
|
|
|
|
newwifis = set(request.POST.getlist("wifi", []))
|
|
|
|
oldwifis = set(device[0].wifi.all())
|
|
|
|
if newwifis != oldwifis:
|
|
|
|
sigWifiDevice(device[0].serial, request.user.username, oldwifis, newwifis)
|
|
|
|
device[0].wifi.set(newwifis)
|
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
device[0].changed = timezone.now()
|
|
|
|
device[0].save()
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
return redirect("/")
|
2018-11-25 21:02:16 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
return render(request, "manager/edit.html",
|
|
|
|
{
|
|
|
|
"title": "Edit Device",
|
|
|
|
"device": device[0],
|
|
|
|
"subnets": subnets,
|
|
|
|
"wifis": wifis,
|
|
|
|
"curfis": Wifi.objects.filter(device=device[0])
|
|
|
|
}
|
|
|
|
)
|
2018-11-28 21:35:57 +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-01-17 08:19:26 +00:00
|
|
|
if request.user.is_staff or request.user.id == user_id:
|
2019-01-13 12:55:53 +00:00
|
|
|
user = None
|
|
|
|
orgas = Organization.objects.filter(users=request.user)
|
|
|
|
|
|
|
|
for organization in orgas:
|
2019-01-17 08:19:26 +00:00
|
|
|
user = user or User.objects.filter(id=user_id, organization=organization)
|
2019-01-13 12:55:53 +00:00
|
|
|
if not user:
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
if request.POST.get("form", ""):
|
2019-01-19 09:51:17 +00:00
|
|
|
newfirst = request.POST.get("firstname", "")
|
|
|
|
newlast = request.POST.get("lastname", "")
|
|
|
|
if newlast != user[0].last_name or newfirst != user[0].first_name:
|
|
|
|
sigRenameUser(user[0].username, request.user.username, "%s %s" % (user[0].first_name, user[0].last_name), "%s %s" % (newfirst, newlast))
|
|
|
|
user[0].first_name = request.POST.get("firstname", "")
|
|
|
|
user[0].last_name = request.POST.get("lastname", "")
|
2019-01-17 08:52:38 +00:00
|
|
|
|
|
|
|
if request.user.is_staff or request.user.is_superuser:
|
2019-01-19 09:51:17 +00:00
|
|
|
newstaff = request.POST.get("staff", "0") == "True"
|
|
|
|
if newstaff != user[0].is_staff:
|
|
|
|
sigStaffUser(user[0].username, request.user.username, newstaff)
|
|
|
|
user[0].is_staff = newstaff
|
2019-01-17 08:52:38 +00:00
|
|
|
|
|
|
|
if request.user.is_superuser:
|
2019-01-19 09:51:17 +00:00
|
|
|
newsuper = request.POST.get("superuser", "0") == "True"
|
|
|
|
if newsuper != user[0].is_superuser:
|
|
|
|
sigSuperUser(user[0].username, request.user.username, newsuper)
|
|
|
|
user[0].is_superuser = newsuper
|
|
|
|
|
|
|
|
newmail = request.POST.get("email", "")
|
|
|
|
if newmail != user[0].email:
|
|
|
|
sigMailUser(user[0].username, request.user.username, user[0].email, newmail)
|
|
|
|
user[0].email = newmail
|
2019-01-17 08:52:38 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
user[0].save()
|
2019-01-13 12:55:53 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
return redirect("/")
|
2019-01-13 12:55:53 +00:00
|
|
|
|
|
|
|
return render(request, "manager/edituser.html",
|
2019-01-17 08:19:26 +00:00
|
|
|
{
|
|
|
|
"title": "Edit User",
|
|
|
|
"auser": user[0]
|
|
|
|
}
|
|
|
|
)
|
2019-01-13 12:55:53 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
else:
|
2019-01-17 16:47:33 +00:00
|
|
|
return redirect('/account/login/?next=%s' % 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):
|
|
|
|
wifi = None
|
|
|
|
|
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
wifi = wifi or Wifi.objects.filter(id=wifi_id, organization=organization)
|
|
|
|
|
|
|
|
if not wifi:
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
if request.POST.get("serial", ""):
|
2019-01-19 09:51:17 +00:00
|
|
|
newserial = request.POST.get("serial", "")
|
|
|
|
if newserial != wifi[0].serial:
|
|
|
|
sigRenameWifi(wifi[0].serial, request.user.username, wifi[0].serial, newserial)
|
|
|
|
wifi[0].serial = newserial
|
|
|
|
|
|
|
|
newssid = request.POST.get("ssid", "")
|
|
|
|
if newssid != wifi[0].ssid:
|
|
|
|
sigSSIDWifi(wifi[0].serial, request.user.username, wifi[0].ssid, newssid)
|
|
|
|
wifi[0].ssid = newssid
|
|
|
|
|
|
|
|
newkey = request.POST.get("key", "")
|
|
|
|
if newkey != wifi[0].key:
|
|
|
|
sigKeyWifi(wifi[0].serial, request.user.username, wifi[0].key, newkey)
|
|
|
|
wifi[0].key = newkey
|
|
|
|
|
2018-12-28 09:43:57 +00:00
|
|
|
wifi[0].save()
|
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
return render(request, "manager/editwifi.html",
|
|
|
|
{
|
|
|
|
"title": "Edit WiFi",
|
|
|
|
"wifi": wifi[0]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
if not mkfirmware(device, FWDIR):
|
|
|
|
return HttpResponse("Something went wrong generating the firmware image. The server may be busy, please try again later.")
|
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:
|
2018-12-01 17:56:29 +00:00
|
|
|
response = HttpResponse(download.read(), content_type="application/octet-stream")
|
2018-12-25 23:25:53 +00:00
|
|
|
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-01-17 08:19:26 +00:00
|
|
|
@login_required
|
2018-11-28 21:35:57 +00:00
|
|
|
def rebootdevice(request, device_id):
|
2019-01-17 08:19:26 +00:00
|
|
|
device = None
|
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
device = device or Device.objects.filter(id=device_id, organization=organization)
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2019-01-18 12:50:41 +00:00
|
|
|
if device and not device[0].reboot:
|
|
|
|
sigRebootDevice(device[0].serial, request.user.username, True)
|
2019-01-17 08:19:26 +00:00
|
|
|
device[0].reboot = True
|
|
|
|
device[0].save()
|
2018-12-25 23:25:53 +00:00
|
|
|
|
|
|
|
return redirect("/")
|
2018-11-28 21:35:57 +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-01-17 08:19:26 +00:00
|
|
|
device = None
|
|
|
|
|
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
device = device or Device.objects.filter(id=device_id, organization=organization)
|
2018-12-25 23:25:53 +00:00
|
|
|
|
2019-01-18 12:50:41 +00:00
|
|
|
if device and not device[0].update:
|
|
|
|
sigUpdateDevice(device[0].serial, request.user.username, True)
|
2019-01-17 08:19:26 +00:00
|
|
|
device[0].update = True
|
|
|
|
device[0].save()
|
2018-11-28 21:35:57 +00:00
|
|
|
|
2018-12-25 23:25:53 +00:00
|
|
|
return redirect("/")
|
2018-11-28 21:35: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 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-01-17 08:19:26 +00:00
|
|
|
subprocess.call(CADIR + "/revoke " + device.serial, shell=True)
|
|
|
|
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
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
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-01-17 08:19:26 +00:00
|
|
|
wifi = get_object_or_404(Wifi, id=wifi_id)
|
2019-01-06 18:15:13 +00:00
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
for organization in Organization.objects.filter(users=request.user):
|
|
|
|
if organization == wifi.organization:
|
|
|
|
wifi.delete()
|
|
|
|
break
|
2019-01-06 18:15:13 +00:00
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
2019-01-17 08:19:26 +00:00
|
|
|
@user_passes_test(is_staff)
|
2018-12-28 09:43:57 +00:00
|
|
|
def makewifi(request):
|
|
|
|
wifi_serial = request.POST.get("serial", "")
|
|
|
|
wifi_ssid = request.POST.get("ssid", "")
|
|
|
|
wifi_key = request.POST.get("key", "")
|
|
|
|
wifi_organization = request.POST.get("organization", "")
|
|
|
|
|
|
|
|
if not wifi_serial:
|
|
|
|
orga = Organization.objects.filter(users=request.user)
|
|
|
|
|
|
|
|
return render(request, "manager/addwifi.html",
|
|
|
|
{
|
|
|
|
"title": "Add WiFi",
|
|
|
|
"organizations": orga
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-02-01 14:28:56 +00:00
|
|
|
wifi = Wifi.objects.create(
|
2019-01-17 08:19:26 +00:00
|
|
|
serial = wifi_serial,
|
|
|
|
ssid = wifi_ssid,
|
|
|
|
key = wifi_key,
|
|
|
|
organization = Organization.objects.filter(id=wifi_organization)[0]
|
|
|
|
)
|
2018-12-28 09:43:57 +00:00
|
|
|
|
2019-02-01 14:28:56 +00:00
|
|
|
return redirect("/")
|
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 makedevice(request):
|
|
|
|
CADIR = "/etc/openvpn/ca/"
|
|
|
|
CONFIGDIR = "/etc/openvpn/client-configs/"
|
|
|
|
BEFORE = os.getcwd()
|
|
|
|
|
|
|
|
device_serial = request.POST.get("serial", "")
|
|
|
|
device_name = request.POST.get("name", "")
|
|
|
|
device_organization = request.POST.get("organization", "")
|
2018-12-15 11:35:00 +00:00
|
|
|
device_model = request.POST.get("model", "")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
|
|
|
if not request.user.is_superuser:
|
2019-01-17 08:19:26 +00:00
|
|
|
return redirect("/")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
|
|
|
if not device_serial:
|
2019-01-17 08:19:26 +00:00
|
|
|
orga = Organization.objects.all()
|
|
|
|
models = Model.objects.all()
|
|
|
|
|
|
|
|
return render(request, "manager/add.html",
|
2018-11-28 21:35:57 +00:00
|
|
|
{
|
|
|
|
"title": "Add Device",
|
|
|
|
"organizations": orga,
|
2018-12-15 11:35:00 +00:00
|
|
|
"models": models
|
2018-11-28 21:35:57 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-02-01 14:30:46 +00:00
|
|
|
if glob.glob(CADIR + "/keys/" + device_serial + "*"):
|
|
|
|
return HttpResponse("This key already exists.")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
|
|
|
os.chdir(CADIR)
|
|
|
|
|
|
|
|
if subprocess.call(CADIR + "/generate-key " + device_serial, shell=True):
|
2019-01-17 08:19:26 +00:00
|
|
|
os.chdir(BEFORE)
|
|
|
|
return HttpResponse("Something went wrong trying to generate the key.")
|
2018-11-28 21:35:57 +00:00
|
|
|
|
|
|
|
if glob.glob(CONFIGDIR + "/files/" + device_serial + "*"):
|
2018-12-15 11:35:00 +00:00
|
|
|
os.chdir(BEFORE)
|
2018-11-28 21:35:57 +00:00
|
|
|
return HttpResponse("This configuration file already exists.")
|
|
|
|
|
|
|
|
os.chdir(CONFIGDIR)
|
|
|
|
|
|
|
|
if subprocess.call(CONFIGDIR + "/make_config " + device_serial, shell=True):
|
2018-12-15 11:35:00 +00:00
|
|
|
os.chdir(BEFORE)
|
2018-11-28 21:35:57 +00:00
|
|
|
return HttpResponse("Something went wrong trying to generate the config file.")
|
|
|
|
|
|
|
|
os.chdir(BEFORE)
|
|
|
|
|
|
|
|
device = Device.objects.create(
|
|
|
|
serial=device_serial,
|
|
|
|
name=device_name,
|
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],
|
2018-11-28 21:35:57 +00:00
|
|
|
organization=Organization.objects.filter(id=device_organization)[0],
|
|
|
|
vpnconfig = open(CONFIGDIR + "/files/" + device_serial + ".ovpn").read()
|
|
|
|
)
|
|
|
|
|
|
|
|
return redirect("/")
|
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)
|
|
|
|
|
|
|
|
@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()
|
|
|
|
except:
|
|
|
|
UserStatus.objects.create(user=instance)
|
2019-01-31 19:58:08 +00:00
|
|
|
|