-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdoorctl.py
executable file
·179 lines (138 loc) · 4.53 KB
/
doorctl.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
174
175
176
177
178
#!/usr/bin/env python
import collections
import re
import socket
import sqlite3
import sys
import os
import userdb
try:
from rfidconv import valid_rfid, encode_rfid, decode_rfid
except ImportError:
def valid_rfid(rfid):
return True
def encode_rfid(rfid):
return rfid
def decode_rfid(rfid):
return rfid
def path_relative(name):
return os.path.join(os.path.dirname(__file__), name)
dbfile = path_relative("db/user.db")
host, port = '::1', 4242
conn = None
def get_conn():
global conn
if conn == None:
conn = sqlite3.connect(dbfile)
return conn
def list_users():
users = userdb.get_users(get_conn())
users.sort(key=lambda x:x['rfid'])
for row in users:
row['rfid'] = decode_rfid(row['rfid'])
row['authorised'] = ('disabled','enabled')[int(row['authorised']==True)]
sys.stdout.write('{rfid} {authorised}\n'.format(**row))
def export_users():
for row in userdb.get_users(get_conn()):
row['rfid'] = decode_rfid(row['rfid'])
sys.stdout.write('{rfid} {authorised} {hash}\n'.format(**row))
def change_pin(rfid, pin):
dbrfid = encode_rfid(rfid)
if not re.match("^[0-9]*$", pin):
raise ValueError("bad pin")
if not userdb.update_pin(get_conn(), dbrfid, pin):
raise ValueError("fob {0} does not exists".format(rfid))
def import_user(rfid, authorised, pin, plain=False):
dbrfid = encode_rfid(rfid)
if authorised not in '01':
raise ValueError("bad authorised field")
if plain:
if not re.match("^[0-9]*$", pin):
raise ValueError("bad pin")
func = userdb.add_user
else:
if not re.match("^[0-9a-fA-F]*$", pin):
raise ValueError("bad pin hash")
func = userdb.import_user
if not func(get_conn(), dbrfid, pin, int(authorised)):
raise ValueError("fob {0} already exists".format(rfid))
def import_users(plain=False):
for line in sys.stdin:
line = line.rstrip('\n\r\0')
fields = line.split(' ')
try:
if len(fields) != 3:
raise ValueError("bad input")
rfid, authorised, pin = fields
import_user(rfid, authorised, pin)
print "fob {0} imported".format(rfid)
except ValueError as e:
print str(e)
def import_plain():
import_users(plain=True)
def delete(rfid):
if valid_rfid(rfid):
if userdb.del_user(get_conn(), encode_rfid(rfid)):
print "fob deleted"
else:
print "fob not in the system"
else:
print "bad fob code {0}".format(rfid)
def enable(rfid):
if valid_rfid(rfid):
if userdb.enable(get_conn(), encode_rfid(rfid)):
print "fob enabled"
else:
print "fob not in the system"
else:
print "bad fob code {0}".format(rfid)
def disable(rfid):
if valid_rfid(rfid):
if userdb.disable(get_conn(), encode_rfid(rfid)):
print "fob disabled"
else:
print "fob not in the system"
else:
print "bad fob code {0}".format(rfid)
sock_commands = ('addkey', 'openmode', 'authmode', 'resetpin', 'rfidlisten', 'shutdown', 'restart')
db_commands = collections.OrderedDict([
( 'initdb' , (userdb.init_db, 0, '') ),
( 'list' , (list_users, 0, '') ),
( 'export' , (export_users, 0, '') ),
( 'import' , (import_users, 0, ' < file') ),
( 'import-plain' , (import_plain, 0, ' < file') ),
( 'delete' , (delete, 1, ' <key-id>') ),
( 'enable' , (enable, 1, ' <key-id>') ),
( 'disable' , (disable, 1, ' <key-id>') ),
])
def usage():
subcmds = sock_commands+tuple(k+v[2] for k,v in db_commands.iteritems())
pre = "Usage: "+sys.argv[0]
for cmd in subcmds:
print pre+' '+cmd
pre = " "+sys.argv[0]
sys.exit(1)
def socket_command(command, close=True):
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.connect( (host, port) )
s.send(command+'\n')
if close:
s.close()
else:
return s
def doorctl(cmd, *args):
if cmd in sock_commands:
if cmd == 'rfidlisten':
for line in socket_command(cmd, close = False).makefile():
print decode_rfid(line.rstrip('\n\r\0'))
else:
socket_command(cmd)
elif cmd in db_commands:
func, n_args, _ = db_commands[cmd]
if len(args) != n_args:
usage()
func(*args)
if __name__ == '__main__':
if len(sys.argv) < 2:
usage()
doorctl(*sys.argv[1:])