#! /bin/env python3 # LibreVax # Copyright (C) 2024 Dr. Agnibho Mondal # This file is part of LibreVax. # LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. # LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with LibreVax. If not, see . import sqlite3, sys, bcrypt, base64 from getpass import getpass try: connection=sqlite3.connect("data/database.db") cur=connection.cursor() if sys.argv[1]=="list": record=cur.execute("SELECT * FROM users") print("User\tAuth") print("----\t----") for r in record.fetchall(): print(r[0]+"\t"+r[1]) elif sys.argv[1]=="create": try: print("Creating user: "+sys.argv[2]) auth=input("Authorisation (all/none):") pwd=getpass() pwd=base64.b64encode(bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt())) cur.execute("INSERT INTO users (user, auth, hash) VALUES (?, ?, ?)", (sys.argv[2], auth, pwd)) except IndexError: print("Username not provided") elif sys.argv[1]=="update": try: print("Updating user: "+sys.argv[2]) auth=input("Authorisation (all/none):") pwd=getpass() pwd=base64.b64encode(bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt())) cur.execute("UPDATE users SET auth=?, hash=? WHERE user=?", (auth, pwd, sys.argv[2])) if(cur.rowcount==0): print("No record to update") except IndexError: print("Username not provided") elif sys.argv[1]=="delete": try: print("Deleting user: "+sys.argv[2]) cur.execute("DELETE FROM users WHERE user=?", (sys.argv[2],)) if(cur.rowcount==0): print("No record to delete") except IndexError: print("Username not provided") connection.commit() connection.close() except IndexError: print("Allowed commands: list, create, update, delete")