-
Notifications
You must be signed in to change notification settings - Fork 95
/
ankiserverctl.py
executable file
·169 lines (133 loc) · 4.51 KB
/
ankiserverctl.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
#!/usr/bin/env python
import os
import sys
import signal
import subprocess
import binascii
import getpass
import hashlib
import sqlite3
SERVERCONFIG = "production.ini"
AUTHDBPATH = "auth.db"
PIDPATH = "/tmp/ankiserver.pid"
COLLECTIONPATH = "collections/"
def usage():
print "usage: "+sys.argv[0]+" <command> [<args>]"
print
print "Commands:"
print " start [configfile] - start the server"
print " debug [configfile] - start the server in debug mode"
print " stop - stop the server"
print " adduser <username> - add a new user"
print " deluser <username> - delete a user"
print " lsuser - list users"
print " passwd <username> - change password of a user"
def startsrv(configpath, debug):
if not configpath:
configpath = SERVERCONFIG
# We change to the directory containing the config file
# so that all the paths will be relative to it.
configdir = os.path.dirname(configpath)
if configdir != '':
os.chdir(configdir)
configpath = os.path.basename(configpath)
if debug:
# Start it in the foreground and wait for it to complete.
subprocess.call( ["paster", "serve", configpath], shell=False)
return
devnull = open(os.devnull, "w")
pid = subprocess.Popen( ["paster", "serve", configpath],
stdout=devnull,
stderr=devnull).pid
with open(PIDPATH, "w") as pidfile:
pidfile.write(str(pid))
def stopsrv():
if os.path.isfile(PIDPATH):
try:
with open(PIDPATH) as pidfile:
pid = int(pidfile.read())
os.kill(pid, signal.SIGKILL)
os.remove(PIDPATH)
except Exception, error:
print >>sys.stderr, sys.argv[0]+": Failed to stop server: "+error.message
else:
print >>sys.stderr, sys.argv[0]+": The server is not running"
def adduser(username):
if username:
print "Enter password for "+username+": "
password = getpass.getpass()
salt = binascii.b2a_hex(os.urandom(8))
hash = hashlib.sha256(username+password+salt).hexdigest()+salt
conn = sqlite3.connect(AUTHDBPATH)
cursor = conn.cursor()
cursor.execute( "CREATE TABLE IF NOT EXISTS auth "
"(user VARCHAR PRIMARY KEY, hash VARCHAR)")
cursor.execute("INSERT INTO auth VALUES (?, ?)", (username, hash))
if not os.path.isdir(COLLECTIONPATH+username):
os.makedirs(COLLECTIONPATH+username)
conn.commit()
conn.close()
else:
usage()
def deluser(username):
if username and os.path.isfile(AUTHDBPATH):
conn = sqlite3.connect(AUTHDBPATH)
cursor = conn.cursor()
cursor.execute("DELETE FROM auth WHERE user=?", (username,))
conn.commit()
conn.close()
elif not username:
usage()
else:
print >>sys.stderr, sys.argv[0]+": Database file does not exist"
def lsuser():
conn = sqlite3.connect(AUTHDBPATH)
cursor = conn.cursor()
cursor.execute("SELECT user FROM auth")
row = cursor.fetchone()
while row is not None:
print row[0]
row = cursor.fetchone()
conn.close()
def passwd(username):
if os.path.isfile(AUTHDBPATH):
print "Enter password for "+username+": "
password = getpass.getpass()
salt = binascii.b2a_hex(os.urandom(8))
hash = hashlib.sha256(username+password+salt).hexdigest()+salt
conn = sqlite3.connect(AUTHDBPATH)
cursor = conn.cursor()
cursor.execute("UPDATE auth SET hash=? WHERE user=?", (hash, username))
conn.commit()
conn.close()
else:
print >>sys.stderr, sys.argv[0]+": Database file does not exist"
def main():
argc = len(sys.argv)
exitcode = 0
if argc < 2:
usage()
exitcode = 1
else:
if argc < 3:
sys.argv.append(None)
if sys.argv[1] == "start":
startsrv(sys.argv[2], False)
elif sys.argv[1] == "debug":
startsrv(sys.argv[2], True)
elif sys.argv[1] == "stop":
stopsrv()
elif sys.argv[1] == "adduser":
adduser(sys.argv[2])
elif sys.argv[1] == "deluser":
deluser(sys.argv[2])
elif sys.argv[1] == "lsuser":
lsuser()
elif sys.argv[1] == "passwd":
passwd(sys.argv[2])
else:
usage()
exitcode = 1
sys.exit(exitcode)
if __name__ == "__main__":
main()