|
|
@ -1,9 +1,19 @@
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import pwd
|
|
|
|
import pwd
|
|
|
|
import lib.sqlitedb
|
|
|
|
import lib.sqlitedb
|
|
|
|
|
|
|
|
import csv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkUsernameCharacters(username: str) -> bool:
|
|
|
|
def checkUsernameCharacters(username: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks the Username for invalid characters. Allow only alphanumerical characters, a lower alpha one first,
|
|
|
|
|
|
|
|
Followed by any sequence of digits and characters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param username: String to check for validity
|
|
|
|
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:return: True when valid, False when not
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
if " " not in username and "_" not in username and username.isascii() and username[:1].islower() and \
|
|
|
|
if " " not in username and "_" not in username and username.isascii() and username[:1].islower() and \
|
|
|
|
not username[0].isnumeric():
|
|
|
|
not username[0].isnumeric():
|
|
|
|
if not re.search(r"\W+", username):
|
|
|
|
if not re.search(r"\W+", username):
|
|
|
@ -12,15 +22,35 @@ def checkUsernameCharacters(username: str) -> bool:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkUsernameLength(username: str) -> bool:
|
|
|
|
def checkUsernameLength(username: str, upper_limit: int = 16, lower_limit: int = 3) -> bool:
|
|
|
|
if len(username) > 16:
|
|
|
|
""" Checks username for an upper and lower bounds limit character count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param username: Username to check
|
|
|
|
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:param upper_limit: Upper limit bounds to check for(default is 16)
|
|
|
|
|
|
|
|
:type upper_limit: int
|
|
|
|
|
|
|
|
:param lower_limit: Lower limit bounds to check for(default is 3)
|
|
|
|
|
|
|
|
:return: True, when all bounds are in, False when one or both aren't.
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(username) > upper_limit:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
if len(username) < 3:
|
|
|
|
if len(username) < lower_limit:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkUserExists(username: str) -> bool:
|
|
|
|
def checkUserExists(username: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks if the User exists on the **SYSTEM** by calling PWD on it.
|
|
|
|
|
|
|
|
**Note**: You might want to use this in conjunction with checkUserInDB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param username:
|
|
|
|
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:return: True when exists, False when not
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
pwd.getpwnam(username)
|
|
|
|
pwd.getpwnam(username)
|
|
|
|
except KeyError:
|
|
|
|
except KeyError:
|
|
|
@ -30,6 +60,16 @@ def checkUserExists(username: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkUserInDB(username: str, db: str) -> bool:
|
|
|
|
def checkUserInDB(username: str, db: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks users existence in the **DATABASE**.
|
|
|
|
|
|
|
|
:Note: You might want to use this in conjunction with `checkUserExists`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param username: Username to check existence in database
|
|
|
|
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:param db: Path to database to check in
|
|
|
|
|
|
|
|
:type db: str
|
|
|
|
|
|
|
|
:return: True, when User exists, False when not
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
ldb = lib.sqlitedb.SQLitedb(db)
|
|
|
|
ldb = lib.sqlitedb.SQLitedb(db)
|
|
|
|
fetched = ldb.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username]))
|
|
|
|
fetched = ldb.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username]))
|
|
|
@ -41,6 +81,16 @@ def checkUserInDB(username: str, db: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkSSHKey(key: str) -> bool:
|
|
|
|
def checkSSHKey(key: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks SSH Key for meta-data that we accept.
|
|
|
|
|
|
|
|
:Note: We currently only allow ssh keys without options but with a mail address at the end in b64 encoded.
|
|
|
|
|
|
|
|
The currently supported algorithms are: ecdfsa-sha2-nistp256, 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521',
|
|
|
|
|
|
|
|
'ssh-rsa', 'ssh-dss' and 'ssh-ed25519'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param key: Key to check
|
|
|
|
|
|
|
|
:return: True, when Key is valid, False when not
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# taken from https://github.com/hashbang/provisor/blob/master/provisor/utils.py, all belongs to them! ;)
|
|
|
|
# taken from https://github.com/hashbang/provisor/blob/master/provisor/utils.py, all belongs to them! ;)
|
|
|
|
import base64
|
|
|
|
import base64
|
|
|
|
if len(key) > 8192 or len(key) < 80:
|
|
|
|
if len(key) > 8192 or len(key) < 80:
|
|
|
@ -60,66 +110,110 @@ def checkSSHKey(key: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkEmail(mail: str) -> bool:
|
|
|
|
def checkEmail(mail: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks Mail against a relatively simple REgex Pattern.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param mail: Mail to check
|
|
|
|
|
|
|
|
:type mail: str
|
|
|
|
|
|
|
|
:return: False, when the Mail is invalid, True when valid.
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
if not re.match("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", mail):
|
|
|
|
if not re.match("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", mail):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkDatetimeFormat(form: str) -> bool :
|
|
|
|
def checkDatetimeFormat(datetime_str: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks a Strings format on date time.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param datetime_str: String to check
|
|
|
|
|
|
|
|
:type datetime_str: str
|
|
|
|
|
|
|
|
:return: True when valid, False when not.
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import datetime
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
datetime.datetime.strptime(form, "%Y-%m-%d %H:%M:%S")
|
|
|
|
datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
|
|
|
|
except ValueError:
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkName(name: str) -> bool:
|
|
|
|
def checkName(name: str) -> bool:
|
|
|
|
|
|
|
|
""" Checks a users (real) Name against a real simple REgex Pattern.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param name: Name/String to check
|
|
|
|
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:return: True when valid, False when not.
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
if not re.match("\w+\s*\w", name):
|
|
|
|
if not re.match("\w+\s*\w", name):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkImportFile(fname: str, db: str) -> bool:
|
|
|
|
def checkImportFile(path: str, db: str):
|
|
|
|
error_list = str()
|
|
|
|
""" Checks an CSV file against most of the validators and prints an Error message with the line number corresponding
|
|
|
|
|
|
|
|
to said failure.. Those includes: checkName, checkUsernameCharacters,
|
|
|
|
|
|
|
|
ckeckUsernameLength, duplicate usernames(in the CSV), checkSSHKey, checkEmail, checkUserExists, checkUserInDB,
|
|
|
|
|
|
|
|
checkDatetimeformat and if the status is 1 or 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param path: Path to file to check
|
|
|
|
|
|
|
|
:type path: str
|
|
|
|
|
|
|
|
:param db: Path to database file(SQLite)
|
|
|
|
|
|
|
|
:type db: str
|
|
|
|
|
|
|
|
:return: Str when Failure, True when success(All tests passed)
|
|
|
|
|
|
|
|
:rtype: Str or None
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
errstr = ""
|
|
|
|
valid = True
|
|
|
|
valid = True
|
|
|
|
ln = 1 # line number
|
|
|
|
ln = 1 # line number
|
|
|
|
with open(fname, 'r', newline='') as f:
|
|
|
|
valid_names_list = []
|
|
|
|
import csv
|
|
|
|
with open(path, 'r', newline='') as f:
|
|
|
|
reador = csv.DictReader(f)
|
|
|
|
reader = csv.DictReader(f)
|
|
|
|
for row in reador:
|
|
|
|
for row in reader:
|
|
|
|
# if any of this fails move on to the next user, just print a relatively helpful message lel
|
|
|
|
# if any of this fails move on to the next user, just print a relatively helpful message lel
|
|
|
|
if not lib.validator.checkName(row["name"]):
|
|
|
|
if not lib.validator.checkName(row["name"]):
|
|
|
|
error_list += f"Line{ln}: {row['name']} seems not legit. Character followed by character should be " \
|
|
|
|
errstr += f"Line {ln}: Name: '{row['name']}' seems not legit. Character followed by character should" \
|
|
|
|
f"correct.\n"
|
|
|
|
f" be correct.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if not lib.validator.checkUsernameCharacters(row["username"]):
|
|
|
|
if not lib.validator.checkUsernameCharacters(row["username"]):
|
|
|
|
error_list += (f"Line {ln}: Username contains unsupported characters or starts with a number: '"
|
|
|
|
errstr += (f"Line {ln}: Username contains unsupported characters or starts with a number: '"
|
|
|
|
f"{row['username']}'.\n")
|
|
|
|
f"{row['username']}'.\n")
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if not lib.validator.checkUsernameLength(row["username"]):
|
|
|
|
if not lib.validator.checkUsernameLength(row["username"]):
|
|
|
|
error_list += f"Line {ln}: Username '{row['username']}' is either too long(>16) or short(<3)\n"
|
|
|
|
errstr += f"Line {ln}: Username '{row['username']}' is either too long(>16) or short(<3)\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
|
|
|
|
# dup checking
|
|
|
|
|
|
|
|
if row["username"] in valid_names_list:
|
|
|
|
|
|
|
|
errstr += f"Line {ln}: Duplicate Username {row['username']}!\n"
|
|
|
|
|
|
|
|
valid = False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
valid_names_list.append(row["username"])
|
|
|
|
|
|
|
|
# dup end
|
|
|
|
if not lib.validator.checkSSHKey(row["pubkey"]):
|
|
|
|
if not lib.validator.checkSSHKey(row["pubkey"]):
|
|
|
|
error_list += f"Line {ln}: Following SSH-Key of user '{row['username']}' isn't valid: '{row['pubkey']}'."\
|
|
|
|
errstr += f"Line {ln}: Following SSH-Key of user '{row['username']}' isn't valid: " \
|
|
|
|
f"\n"
|
|
|
|
f"'{row['pubkey']}'.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if not lib.validator.checkEmail(row["email"]):
|
|
|
|
if not lib.validator.checkEmail(row["email"]):
|
|
|
|
error_list += f"Line {ln}: E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n"
|
|
|
|
errstr += f"Line {ln}: E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if not lib.validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db):
|
|
|
|
if not lib.validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db):
|
|
|
|
error_list += f"Line {ln}: User '{row['username']}' already exists.\n"
|
|
|
|
errstr += f"Line {ln}: User '{row['username']}' already exists.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if not lib.validator.checkDatetimeFormat(row["timestamp"]):
|
|
|
|
if not lib.validator.checkDatetimeFormat(row["timestamp"]):
|
|
|
|
error_list += f"Line {ln}: Timestamp '{row['timestamp']}' from user '{row['username']}' is invalid.\n"
|
|
|
|
errstr += f"Line {ln}: Timestamp '{row['timestamp']}' from user '{row['username']}' is invalid.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
if int(row["status"]) > 1 or int(row["status"]) < 0:
|
|
|
|
if int(row["status"]) > 1 or int(row["status"]) < 0:
|
|
|
|
error_list += f"Line {ln}: Status '{row['status']}' MUST be either 0 or 1.\n"
|
|
|
|
errstr += f"Line {ln}: Status '{row['status']}' MUST be either 0 or 1.\n"
|
|
|
|
valid = False
|
|
|
|
valid = False
|
|
|
|
ln += 1
|
|
|
|
ln += 1
|
|
|
|
if valid:
|
|
|
|
if valid:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return error_list
|
|
|
|
return errstr
|
|
|
|