1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | import os |
---|
4 | import shutil |
---|
5 | import json |
---|
6 | |
---|
7 | import sqlite3 |
---|
8 | |
---|
9 | class PasswordManager: |
---|
10 | |
---|
11 | PASSWORD_FILE="/net/server-sync/var/lib/n4d/n4d.json" |
---|
12 | LOG_PATH="/net/server-sync/var/lib/n4d" |
---|
13 | |
---|
14 | def __init__(self): |
---|
15 | |
---|
16 | self.users={} |
---|
17 | |
---|
18 | if not os.path.exists("/lib/systemd/system/net-server\\x2dsync.mount"): |
---|
19 | |
---|
20 | if os.path.exists("/net/server-sync/var/lib/n4d/n4d.sqlite"): |
---|
21 | ret=self.sqlite_to_json(True) |
---|
22 | if ret: |
---|
23 | os.remove("/net/server-sync/var/lib/n4d/n4d.sqlite") |
---|
24 | else: |
---|
25 | self.load_password_file() |
---|
26 | |
---|
27 | #def init |
---|
28 | |
---|
29 | |
---|
30 | def sqlite_to_json(self,force_write=False): |
---|
31 | |
---|
32 | try: |
---|
33 | conn = sqlite3.connect("/net/server-sync/var/lib/n4d/n4d.sqlite") |
---|
34 | cursor = conn.cursor() |
---|
35 | cursor.execute('select cn,sn,uid,passwd from password') |
---|
36 | result = cursor.fetchall() |
---|
37 | conn.close() |
---|
38 | |
---|
39 | self.users={} |
---|
40 | for user in result : |
---|
41 | self.users[user[2]]={} |
---|
42 | self.users[user[2]]["cn"] = user[0].encode('utf-8') |
---|
43 | self.users[user[2]]["sn"] = user[1].encode('utf-8') |
---|
44 | self.users[user[2]]["passwd"] = user[3] |
---|
45 | |
---|
46 | if force_write: |
---|
47 | self.write_file() |
---|
48 | |
---|
49 | return True |
---|
50 | except Exception as e: |
---|
51 | print(e) |
---|
52 | |
---|
53 | return False |
---|
54 | |
---|
55 | #def sqlite_to_json |
---|
56 | |
---|
57 | |
---|
58 | def load_password_file(self, f=None): |
---|
59 | |
---|
60 | self.users={} |
---|
61 | if f==None: |
---|
62 | f=PasswordManager.PASSWORD_FILE |
---|
63 | try: |
---|
64 | pfile=open(f,"r") |
---|
65 | self.users=json.load(pfile) |
---|
66 | pfile.close() |
---|
67 | |
---|
68 | except Exception as e: |
---|
69 | print("[PasswordManager] Error reading file: %s"%e) |
---|
70 | |
---|
71 | #def load_json |
---|
72 | |
---|
73 | |
---|
74 | def write_file(self,f=None): |
---|
75 | |
---|
76 | if f==None: |
---|
77 | f=PasswordManager.PASSWORD_FILE |
---|
78 | |
---|
79 | set_perms=False |
---|
80 | if not os.path.exists(f): |
---|
81 | set_perms=True |
---|
82 | |
---|
83 | data=unicode(json.dumps(self.users,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
84 | output_file=open(f,"w") |
---|
85 | output_file.write(data) |
---|
86 | output_file.close() |
---|
87 | |
---|
88 | if set_perms: |
---|
89 | prevmask=os.umask(0) |
---|
90 | os.chmod(f,0640) |
---|
91 | os.umask(prevmask) |
---|
92 | |
---|
93 | #def write_file |
---|
94 | |
---|
95 | |
---|
96 | def add_password(self,user_name,cn,sn,password): |
---|
97 | |
---|
98 | if user_name not in self.users: |
---|
99 | self.users[user_name]={} |
---|
100 | |
---|
101 | self.users[user_name]["cn"]=cn |
---|
102 | self.users[user_name]["sn"]=sn |
---|
103 | self.users[user_name]["passwd"]=password |
---|
104 | |
---|
105 | self.write_file() |
---|
106 | |
---|
107 | #def add_password |
---|
108 | |
---|
109 | |
---|
110 | def remove_password(self,user_name): |
---|
111 | |
---|
112 | if user_name in self.users: |
---|
113 | self.users.pop(user_name) |
---|
114 | |
---|
115 | return True |
---|
116 | |
---|
117 | #def remove_password |
---|
118 | |
---|
119 | |
---|
120 | def get_passwords(self): |
---|
121 | |
---|
122 | pwd_list = [] |
---|
123 | for user in self.users : |
---|
124 | a = {} |
---|
125 | a['cn'] = self.users[user]["cn"] |
---|
126 | a['sn'] = self.users[user]["sn"] |
---|
127 | a['uid'] = user |
---|
128 | a['passwd'] = self.users[user]["passwd"] |
---|
129 | pwd_list.append(a) |
---|
130 | |
---|
131 | return pwd_list |
---|
132 | |
---|
133 | #def get_passwords |
---|
134 | |
---|
135 | |
---|
136 | def is_user_in_database(self,uid): |
---|
137 | |
---|
138 | for user in self.users: |
---|
139 | return True |
---|
140 | |
---|
141 | return False |
---|
142 | |
---|
143 | #def is_user_in_database |
---|
144 | |
---|
145 | |
---|
146 | def set_externally_modified(self,uid): |
---|
147 | |
---|
148 | if self.is_user_in_database(uid): |
---|
149 | self.add_password(uid,self.users[uid]["cn"],self.users[uid]["sn"],"#! CHANGED MANUALLY !#") |
---|
150 | |
---|
151 | #def set_externally_modified |
---|
152 | |
---|
153 | |
---|
154 | #class PasswordManager |
---|
155 | |
---|
156 | if __name__=="__main__": |
---|
157 | |
---|
158 | pm=PasswordManager() |
---|
159 | |
---|
160 | |
---|