-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
173 lines (142 loc) · 5.06 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import string
import random
import hashlib
import sys
from utils.dbconfig import dbconfig
from rich import print as printc
from rich.console import Console
from getpass import getpass
console = Console()
def generateDeviceSecret(length=10):
return ''.join(random.choices(string.ascii_uppercase+string.digits, k =length))
def checkConfig():
db = dbconfig()
cursor = db.cursor()
query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'pm'"
cursor.execute(query)
results = cursor.fetchall()
db.close()
if len(results)!=0:
return True
return False
def config():
db = dbconfig()
cur = db.cursor()
while 1:
mp = getpass('Choose a MASTER PASSWORD')
if mp ==getpass('Re-type') and mp!="":
break
printc('[yellow][.]Please try again[/yellow]')
hashed_mp = hashlib.sha256(mp.encode()).hexdigest()
printc('[green][+[/green] Generated hash of MASTER PASSWORD]')
ds = generateDeviceSecret()
printc("[green][+][/green] Device Secret is Generated")
query = "INSERT INTO pm.secrets(masterkey_hash, device_secret) VALUES (%s,%s)"
val=(hashed_mp,ds)
cur.execute(query,val)
db.commit()
printc("[green][+][/green] Added to the database")
printc("[green][+]Configuration done[/green]")
db.close()
def db__init__():
try:
if not checkConfig():
db = dbconfig() # Assuming dbconfig() establishes the database connection
cur = db.cursor()
try:
cur.execute("CREATE DATABASE pm")
except Exception as e:
printc("[red][!] An error occured while creating a database")
console.print_exception(show_locals = True)
sys.exit(0)
printc("[green][+][/green] Database 'pm created")
query = "CREATE TABLE pm.secrets(masterkey_hash TEXT NOT NULL, device_secret TEXT NOT NULL)"
res = cur.execute(query)
printc("[green][+][/green]Tables secret created")
query = "CREATE TABLE pm.entries(sitename TEXT NOT NULL, siteurl TEXT NOT NULL,email TEXT, username TEXT, password TEXT NOT NULL )"
res = cur.execute(query)
printc('[green][+][/green] Tables entries created')
db.commit()
db.close()
printc('[green]Database initialized successfully[/green]')
else:
printc('[yellow]Database already initialized[/yellow]')
except Exception as e:
printc(f'[red]Error initializing database: {e}[/red]')
def del_pass(field):
try:
db = dbconfig() # Establish your database connection
cur = db.cursor()
# Construct the query dynamically
query = "DELETE FROM pm.entries WHERE "
conditions = []
for key, value in field.items():
if value:
conditions.append(f"{key} = '{value}'")
if conditions:
query += " AND ".join(conditions)
cur.execute(query) # Execute the query
db.commit()
db.close()
printc("[green][-][/green] Password deleted successfully")
else:
printc("/n[yellow]No conditions provided for deletion.[/yellow]")
except Exception as e:
print(f'Error deleting passwords: {e}')
def del_all():
try:
db = dbconfig()
cur = db.cursor()
query = 'DELETE FROM pm.entries'
cur.execute(query)
printc("[green][-][/green] All Passwords deleted successfully")
db.commit()
db.close()
except Exception as e:
print(f'Error deleting passwords: {e}')
def delete():
printc("[green][-][/green] Deleting config")
if not checkConfig():
printc("[yellow][-][/yellow] No configuration exists to delete!")
return
success = True
try:
db = dbconfig()
cursor = db.cursor()
query = "DROP DATABASE pm"
cursor.execute(query)
db.commit()
db.close()
except Exception as e:
print(f"Error deleting config: {e}")
success = False
if success:
printc("[green][+] Config deleted![/green]")
sys.exit()
def delete_rw():
printc("[green][-][/green] Deleting config")
if not checkConfig():
printc("[yellow][-][/yellow] No configuration exists to delete!")
return
success = True
try:
db = dbconfig()
print("Got the DB connection")
cursor = db.cursor()
print("Got the cursor")
query = "DROP DATABASE pm"
cursor.execute(query)
print("Executed the query")
db.commit()
print("Committed changes")
db.close()
print("Closed the connection")
except Exception as e:
print(f"Error deleting config: {e}")
success = False
if success:
printc("[green][+] Config deleted![/green]")
else:
print("Deletion failed!")