|
|
@ -1,12 +1,13 @@
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
import configparser
|
|
|
|
import configparser
|
|
|
|
import lib.uis.config_ui # only follow -c flag
|
|
|
|
import sqlite3
|
|
|
|
import lib.Validator
|
|
|
|
|
|
|
|
import lib.sqlitedb
|
|
|
|
|
|
|
|
import lib.System
|
|
|
|
import lib.System
|
|
|
|
import lib.UserExceptions
|
|
|
|
import lib.UserExceptions
|
|
|
|
import sqlite3
|
|
|
|
import lib.Validator
|
|
|
|
|
|
|
|
import lib.sqlitedb
|
|
|
|
|
|
|
|
import lib.uis.config_ui # only follow -c flag
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
lib.uis.config_ui.argparser.description += " - Edit Tilde Users"
|
|
|
|
lib.uis.config_ui.argparser.description += " - Edit Tilde Users"
|
|
|
@ -35,115 +36,112 @@ if __name__ == "__main__":
|
|
|
|
args = ArgParser.parse_args()
|
|
|
|
args = ArgParser.parse_args()
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(args.config)
|
|
|
|
config.read(args.config)
|
|
|
|
try:
|
|
|
|
db = config['DEFAULT']['applications_db']
|
|
|
|
db = config['DEFAULT']['applications_db']
|
|
|
|
if not args.sshpubkey and not args.name and not args.username and not args.email and args.status is None \
|
|
|
|
if not args.sshpubkey and not args.name and not args.username and not args.email and args.status is None \
|
|
|
|
and not args.remove:
|
|
|
|
and not args.remove:
|
|
|
|
print(f"Well, SOMETHING must be done with {args.user} ;-)")
|
|
|
|
print(f"Well, SOMETHING must be done with {args.user} ;-)")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
# --> --user
|
|
|
|
|
|
|
|
if not lib.Validator.checkUserInDB(args.user, db):
|
|
|
|
|
|
|
|
print(f"User {args.user} does not exist in the database.")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
DB = lib.sqlitedb.SQLiteDB(db)
|
|
|
|
|
|
|
|
sys_ctl = lib.System.System(args.user)
|
|
|
|
|
|
|
|
if not DB:
|
|
|
|
|
|
|
|
print("Could not establish connection to database")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username`=?", tuple([args.user]))[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --> --remove
|
|
|
|
|
|
|
|
if args.remove:
|
|
|
|
|
|
|
|
print(f"Removing {args.user} from the system and the database...")
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
DB.removeApplicantFromDBperUsername(args.user)
|
|
|
|
|
|
|
|
print(f"Purged from the DB")
|
|
|
|
|
|
|
|
if CurrentUser["status"] == 1:
|
|
|
|
|
|
|
|
sys_ctl.remove_user()
|
|
|
|
|
|
|
|
print(f"Purged from the system")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print(f"'{args.user}' was not approved before, therefore not deleting from system itself.")
|
|
|
|
|
|
|
|
except lib.UserExceptions.General as e:
|
|
|
|
|
|
|
|
print(f"{e}")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print(f"Successfully removed '{args.user}'.")
|
|
|
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --> --sshpubkey
|
|
|
|
|
|
|
|
if args.sshpubkey:
|
|
|
|
|
|
|
|
if not lib.Validator.checkSSHKey(args.sshpubkey):
|
|
|
|
|
|
|
|
print(f"Pubkey '{args.sshpubkey}' isn't valid.")
|
|
|
|
exit(1)
|
|
|
|
exit(1)
|
|
|
|
# --> --user
|
|
|
|
try:
|
|
|
|
if not lib.Validator.checkUserInDB(args.user, db):
|
|
|
|
DB.safequery("UPDATE `applications` SET `pubkey`=? WHERE `username`=?",
|
|
|
|
print(f"User {args.user} does not exist in the database.")
|
|
|
|
tuple([args.sshpubkey, args.user]))
|
|
|
|
|
|
|
|
CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username` = ? ", tuple([args.user]))[0]
|
|
|
|
|
|
|
|
if int(CurrentUser["status"]) == 1:
|
|
|
|
|
|
|
|
sys_ctl.make_ssh_usable(args.sshpubkey)
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Something unexpected happened! {e}")
|
|
|
|
exit(1)
|
|
|
|
exit(1)
|
|
|
|
DB = lib.sqlitedb.SQLiteDB(db)
|
|
|
|
except lib.UserExceptions.ModifyFilesystem as e:
|
|
|
|
sys_ctl = lib.System.System(args.user)
|
|
|
|
print(f"One action failed during writing the ssh key back to the authorization file. {e}")
|
|
|
|
if not DB:
|
|
|
|
print(f"'{args.user}'s SSH-Key updated successfully.")
|
|
|
|
print("Could not establish connection to database")
|
|
|
|
|
|
|
|
|
|
|
|
# --> --name
|
|
|
|
|
|
|
|
if args.name:
|
|
|
|
|
|
|
|
if not lib.Validator.checkName(args.name):
|
|
|
|
|
|
|
|
print(f"'{args.name}' is not a valid Name.")
|
|
|
|
exit(1)
|
|
|
|
exit(1)
|
|
|
|
CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username`=?", tuple([args.user]))[0]
|
|
|
|
try:
|
|
|
|
|
|
|
|
DB.safequery("UPDATE `applications` SET `name` =? WHERE `username` =?", tuple([args.name, args.user]))
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not write '{args.name}' to database: {e}")
|
|
|
|
|
|
|
|
print(f"'{args.user}'s Name changed to '{args.name}'.")
|
|
|
|
|
|
|
|
|
|
|
|
# --> --remove
|
|
|
|
# --> --email
|
|
|
|
if args.remove:
|
|
|
|
if args.email:
|
|
|
|
print(f"Removing {args.user} from the system and the database...")
|
|
|
|
if not lib.Validator.checkEmail(args.email):
|
|
|
|
try:
|
|
|
|
print(f"'{args.email}' is not a valid Mail address!")
|
|
|
|
DB.removeApplicantFromDBperUsername(args.user)
|
|
|
|
exit(1)
|
|
|
|
print(f"Purged from the DB")
|
|
|
|
try:
|
|
|
|
if CurrentUser["status"] == 1:
|
|
|
|
DB.safequery("UPDATE `applications` SET `email` =? WHERE `username` =?", tuple([args.email]))
|
|
|
|
sys_ctl.remove_user()
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print(f"Purged from the system")
|
|
|
|
print(f"Could not write '{args.email}' to the database. {e}")
|
|
|
|
else:
|
|
|
|
print(f"'{args.user}' Mail changed to '{args.email}'.")
|
|
|
|
print(f"'{args.user}' was not approved before, therefore not deleting from system itself.")
|
|
|
|
|
|
|
|
except lib.UserExceptions.General as e:
|
|
|
|
# --> --status
|
|
|
|
print(f"{e}")
|
|
|
|
if args.status is not None:
|
|
|
|
exit(1)
|
|
|
|
if args.status != 0 and args.status != 1:
|
|
|
|
print(f"Successfully removed '{args.user}'.")
|
|
|
|
print("Only 0 and 1 are valid status, where 1 is activated and 0 is unapproved.")
|
|
|
|
exit(0)
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
# --> --sshpubkey
|
|
|
|
# just takes first result out of the dict
|
|
|
|
if args.sshpubkey:
|
|
|
|
if args.status == int(CurrentUser["status"]):
|
|
|
|
if not lib.Validator.checkSSHKey(args.sshpubkey):
|
|
|
|
print(f"New and old status are the same.")
|
|
|
|
print(f"Pubkey '{args.sshpubkey}' isn't valid.")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
if args.status == 0 and int(CurrentUser["status"]) == 1:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
DB.safequery("UPDATE `applications` SET `pubkey`=? WHERE `username`=?",
|
|
|
|
DB.safequery("UPDATE `applications` SET `status` =? WHERE `id`=?",
|
|
|
|
tuple([args.sshpubkey, args.user]))
|
|
|
|
tuple([args.status, CurrentUser["id"]]))
|
|
|
|
CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username` = ? ", tuple([args.user]))[0]
|
|
|
|
sys_ctl.remove_user()
|
|
|
|
if int(CurrentUser["status"]) == 1:
|
|
|
|
|
|
|
|
sys_ctl.make_ssh_usable(args.sshpubkey)
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print(f"Something unexpected happened! {e}")
|
|
|
|
print(f"Could not update database entry for '{args.user}', did not touch the system")
|
|
|
|
exit(1)
|
|
|
|
exit(1)
|
|
|
|
except lib.UserExceptions.ModifyFilesystem as e:
|
|
|
|
except lib.UserExceptions.UnknownReturnCode as e:
|
|
|
|
print(f"One action failed during writing the ssh key back to the authorization file. {e}")
|
|
|
|
print(f"Could not remove '{args.user}' from the system, unknown return code: {e}. DB is modified.")
|
|
|
|
print(f"'{args.user}'s SSH-Key updated successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --> --name
|
|
|
|
|
|
|
|
if args.name:
|
|
|
|
|
|
|
|
if not lib.Validator.checkName(args.name):
|
|
|
|
|
|
|
|
print(f"'{args.name}' is not a valid Name.")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
exit(1)
|
|
|
|
try:
|
|
|
|
print(f"Successfully changed '{args.user}'s status to 0 and cleared from the system.")
|
|
|
|
DB.safequery("UPDATE `applications` SET `name` =? WHERE `username` =?", tuple([args.name, args.user]))
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not write '{args.name}' to database: {e}")
|
|
|
|
|
|
|
|
print(f"'{args.user}'s Name changed to '{args.name}'.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --> --email
|
|
|
|
if args.status == 1 and int(CurrentUser["status"]) == 0:
|
|
|
|
if args.email:
|
|
|
|
|
|
|
|
if not lib.Validator.checkEmail(args.email):
|
|
|
|
|
|
|
|
print(f"'{args.email}' is not a valid Mail address!")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
DB.safequery("UPDATE `applications` SET `email` =? WHERE `username` =?", tuple([args.email]))
|
|
|
|
DB.safequery("UPDATE `applications` SET `status`=? WHERE `username`=?",
|
|
|
|
|
|
|
|
tuple([args.status, args.user]))
|
|
|
|
|
|
|
|
sys_ctl.aio_approve(CurrentUser["pubkey"])
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print(f"Could not write '{args.email}' to the database. {e}")
|
|
|
|
print(f"Could not update Users status in database")
|
|
|
|
print(f"'{args.user}' Mail changed to '{args.email}'.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
except lib.UserExceptions.General as ChangeUser:
|
|
|
|
# --> --status
|
|
|
|
print(f"Some chain in the cattle just slipped away, my lord! {ChangeUser}")
|
|
|
|
if args.status is not None:
|
|
|
|
exit(1)
|
|
|
|
if args.status != 0 and args.status != 1:
|
|
|
|
print(f"Successfully changed '{args.user}'s status to 1 and created on the system.")
|
|
|
|
print("Only 0 and 1 are valid status, where 1 is activated and 0 is unapproved.")
|
|
|
|
exit(0)
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# just takes first result out of the dict
|
|
|
|
|
|
|
|
if args.status == int(CurrentUser["status"]):
|
|
|
|
|
|
|
|
print(f"New and old status are the same.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.status == 0 and int(CurrentUser["status"]) == 1:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
DB.safequery("UPDATE `applications` SET `status` =? WHERE `id`=?",
|
|
|
|
|
|
|
|
tuple([args.status, CurrentUser["id"]]))
|
|
|
|
|
|
|
|
sys_ctl.remove_user()
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not update database entry for '{args.user}', did not touch the system")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
except lib.UserExceptions.UnknownReturnCode as e:
|
|
|
|
|
|
|
|
print(f"Could not remove '{args.user}' from the system, unknown return code: {e}. DB is modified.")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print(f"Successfully changed '{args.user}'s status to 0 and cleared from the system.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.status == 1 and int(CurrentUser["status"]) == 0:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
DB.safequery("UPDATE `applications` SET `status`=? WHERE `username`=?",
|
|
|
|
|
|
|
|
tuple([args.status, args.user]))
|
|
|
|
|
|
|
|
sys_ctl.aio_approve(CurrentUser["pubkey"])
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not update Users status in database")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
except lib.UserExceptions.General as ChangeUser:
|
|
|
|
|
|
|
|
print(f"Some chain in the cattle just slipped away, my lord! {ChangeUser}")
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print(f"Successfully changed '{args.user}'s status to 1 and created on the system.")
|
|
|
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
except KeyboardInterrupt as e:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|