Browse Source

delete_user was leaving the created by comment behind

Andy Teijelo Pérez 6 years ago
parent
commit
932c6929c4
2 changed files with 38 additions and 29 deletions
  1. 38 27
      main.py
  2. 0 2
      users.dpto2

+ 38 - 27
main.py

@@ -52,6 +52,16 @@ def reload_freeradius():
 def reload_dnsmasq():
     call("./sendhup dnsmasq".split())
 
+def nthash(password):
+    return hashlib.new('md4',password.encode('utf-16le')).hexdigest().upper()
+
+def create_user(username, password, creator):
+    with exclusive_lock:
+        f = open(users_file,"a")
+        f.write('# created by {}\n{} NT-Password := "{}"\n'.format(creator, username, nthash(password)))
+        f.close()
+    reload_freeradius()
+
 def delete_user(deluser):
     f = open(users_file)
     lines = f.readlines()
@@ -59,23 +69,41 @@ def delete_user(deluser):
     timestamp = time.time()
     tempfile = "{}.{}".format(users_file, timestamp)
     f = open(tempfile,"w")
+    createdby = None
     for line in lines:
+        if re.search("#\s+created by", line):
+            createdby = line
+            continue
         if line.startswith(deluser):
+            createdby = None
             continue
-        f.write(line)
+        if line.startswith("#"):
+            f.write(line)
+        else:
+            if createdby:
+                f.write(createdby)
+            f.write(line)
+            createdby = None
     f.close()
     rename(tempfile, users_file)
     reload_freeradius()
 
-def nthash(password):
-    return hashlib.new('md4',password.encode('utf-16le')).hexdigest().upper()
-
-def create_user(username, password, creator):
-    with exclusive_lock:
-        f = open(users_file,"a")
-        f.write('# created by {}\n{} NT-Password := "{}"\n'.format(creator, username, nthash(password)))
-        f.close()
-    reload_freeradius()
+def load_users():
+    users = []
+    creator = None
+    with open(users_file) as f:
+        for l in f:
+            l = l.strip()
+            if l.startswith("#"):
+                m = re.match("#\s+created by\s+(\S+)", l)
+                if m:
+                    creator = m.group(1)
+                continue
+            m = re.match("(^\S+).*-Password\s+:=\s+\"(\S+)\"", l)
+            if m:
+                users.append((m.group(1), m.group(2), creator))
+                creator = None
+    return users
 
 def login_required(f):
     def g(*args, **kwargs):
@@ -123,23 +151,6 @@ def index():
 
     return render_template("index.html", guestpass=guestpass)
 
-def load_users():
-    users = []
-    creator = None
-    with open(users_file) as f:
-        for l in f:
-            l = l.strip()
-            if l.startswith("#"):
-                m = re.match("#\s+created by\s+(\S+)", l)
-                if m:
-                    creator = m.group(1)
-                continue
-            m = re.match("(^\S+).*-Password\s+:=\s+\"(\S+)\"", l)
-            if m:
-                users.append((m.group(1), m.group(2), creator))
-                creator = None
-    return users
-
 def check_login(username, password):
     for u,p,c in load_users():
         if u == username and p == nthash(password):

+ 0 - 2
users.dpto2

@@ -24,5 +24,3 @@ oscarluis NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C"
 luilver NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C"
 # created by ateijelo
 perico NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C"
-# created by ateijelo
-qwer NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C"