diff options
author | vuko <vuko@hackerspace.pl> | 2021-11-11 21:22:19 +0100 |
---|---|---|
committer | vuko <vuko@hackerspace.pl> | 2021-11-11 22:00:55 +0100 |
commit | 877ab90cde8c9c5fb4ae586b68ca8418da79a01e (patch) | |
tree | bd0d994e3708fc0bc10844fc4eaa0e332b2c7905 | |
parent | 7555b62a1e607a90cc6ea2ef29dd06dc9cbb4074 (diff) | |
download | doorman-877ab90cde8c9c5fb4ae586b68ca8418da79a01e.tar.gz doorman-877ab90cde8c9c5fb4ae586b68ca8418da79a01e.tar.bz2 doorman-877ab90cde8c9c5fb4ae586b68ca8418da79a01e.tar.xz doorman-877ab90cde8c9c5fb4ae586b68ca8418da79a01e.zip |
port to python3, revert to serial connection
-rwxr-xr-x | admin/bin/doorman_add | 22 | ||||
-rwxr-xr-x | admin/bin/doorman_dump | 16 | ||||
-rwxr-xr-x | admin/bin/doorman_ldap_sync | 36 | ||||
-rwxr-xr-x | admin/bin/doorman_revoke | 10 | ||||
-rwxr-xr-x | admin/bin/doorman_revoke_card | 20 | ||||
-rwxr-xr-x | admin/bin/doorman_revoke_user | 22 | ||||
-rwxr-xr-x | admin/bin/doorman_scan | 18 | ||||
-rwxr-xr-x | admin/bin/doorman_show_cards | 8 | ||||
-rw-r--r-- | admin/doorman/actions.py | 6 | ||||
-rw-r--r-- | admin/doorman/command.py | 10 | ||||
-rw-r--r-- | admin/doorman/options.py | 2 | ||||
-rw-r--r-- | admin/doorman/password.py | 2 | ||||
-rw-r--r-- | admin/doorman/proto.py | 34 | ||||
-rw-r--r-- | admin/doorman/storage/__init__.py | 4 | ||||
-rw-r--r-- | admin/doorman/storage/classes.py | 8 | ||||
-rw-r--r-- | admin/doorman/storage/ops.py | 13 | ||||
-rw-r--r-- | admin/doorman/storage/storage_encapsulation.py | 12 |
17 files changed, 115 insertions, 128 deletions
diff --git a/admin/bin/doorman_add b/admin/bin/doorman_add index ac9cfbc..87d483c 100755 --- a/admin/bin/doorman_add +++ b/admin/bin/doorman_add @@ -1,14 +1,14 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import argparse from sys import stderr, argv -from lib.proto import Proto -from lib.actions import * -from lib.storage import add_user -from lib.password import get_token, get_pin +from doorman.proto import Proto +from doorman.actions import * +from doorman.storage import add_user +from doorman.password import get_token, get_pin -import options +import doorman.options as options if __name__ == '__main__': parser = argparse.ArgumentParser( description='Add a card') @@ -27,25 +27,25 @@ if __name__ == '__main__': card = args.card uname = args.name if args.local and not (args.card and args.uid): - print 'Local update requires a card and uid to be specified!' + print('Local update requires a card and uid to be specified!') exit(2) if not args.local: token = get_token() proto = Proto(args.url) if not args.card: - print 'Please swipe token' + print('Please swipe token') frame = scan(token=token, proto=proto) assert(frame.command.upper() == 'S') card = frame.hash uid = frame.uid if frame.uid: - print >> stderr, 'E: Token already in use (user %d)' % frame.uid + print('E: Token already in use (user %d)' % frame.uid, file=stderr) exit(1) else: card = args.card status = add(token, card, proto=proto) if status.command.upper() != 'C': - print >> stderr, 'Unknown error:', str(status) + print('Unknown error:', str(status), file=stderr) exit(1) add_user(uname, card, uid) - print 'User %s added successfully (uid=%d)' % (uname, uid) + print('User %s added successfully (uid=%d)' % (uname, uid)) diff --git a/admin/bin/doorman_dump b/admin/bin/doorman_dump index f1b0a7c..29914bb 100755 --- a/admin/bin/doorman_dump +++ b/admin/bin/doorman_dump @@ -1,13 +1,13 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 from sys import argv -from lib.actions import scan -from lib.proto import Proto -from lib.storage import get_card -from lib.password import get_token -from lib.command import signed_command -import options +from doorman.actions import scan +from doorman.proto import Proto +from doorman.storage import get_card +from doorman.password import get_token +from doorman.command import signed_command +import doorman.options as options if __name__ == '__main__': url = argv[1] if len(argv) > 1 else options.url @@ -15,4 +15,4 @@ if __name__ == '__main__': proto = Proto(url) proto.send(signed_command(command='P', hash=options.empty_hash, uid=0, token=token)) while True: - print proto.fd.readline(), + print(proto.fd.readline(), end=' ') diff --git a/admin/bin/doorman_ldap_sync b/admin/bin/doorman_ldap_sync index 9ff3c74..7fb56f3 100755 --- a/admin/bin/doorman_ldap_sync +++ b/admin/bin/doorman_ldap_sync @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import ldap import getpass @@ -7,12 +7,12 @@ import requests from sys import argv -import options -from lib.actions import revoke_hash, add -from lib.proto import Proto -from lib.storage import get_card -from lib.password import get_token -from lib.command import signed_command +import doorman.options as options +from doorman.actions import revoke_hash, add +from doorman.proto import Proto +from doorman.storage import get_card +from doorman.password import get_token +from doorman.command import signed_command MEMBER_FILTER = ('(|' '(memberOf=cn=starving,ou=Group,dc=hackerspace,dc=pl)' @@ -40,7 +40,7 @@ def get_current_cards(token, proto): proto.send(signed_command(command='P', hash=options.empty_hash, uid=0, token=token)) while True: - l = proto.fd.readline().strip() + l = proto.fd.readline().strip().decode() if not l.startswith('REC,'): continue @@ -58,7 +58,7 @@ def get_target_cards(c): cards = set() for user, attrs in c.search_s('ou=People,dc=hackerspace,dc=pl',ldap.SCOPE_SUBTREE,'(&(mifareIDHash=*)%s)' % MEMBER_FILTER, ['mifareIDHash', 'uid']): for h in attrs['mifareIDHash']: - cards.add(shorthash((h, user))) + cards.add(shorthash((h.decode('ascii'), user))) return cards if __name__ == "__main__": @@ -74,28 +74,28 @@ if __name__ == "__main__": to_remove = cur - target to_add = target - cur - print 'current:', len(cur) - print 'target:', len(target) + print('current:', len(cur)) + print('target:', len(target)) pprint.pprint(target) - print 'to add:', len(to_add) + print('to add:', len(to_add)) pprint.pprint(to_add) - print 'to remove:', len(to_remove) + print('to remove:', len(to_remove)) pprint.pprint(to_remove) max_cards = 140 - print 'Memory utilization: %d / %d (%.2f%%)' % ( + print('Memory utilization: %d / %d (%.2f%%)' % ( len(cur), max_cards, 100.0 * len(cur) / max_cards - ) + )) print('Press y to confirm removal') - if raw_input().lower().strip() == 'y': + if input().lower().strip() == 'y': for h, u in to_remove: - print('Removing %s' % h) + print(('Removing %s' % h)) revoke_hash(token, h, proto=proto) for h, u in to_add: - print('Adding %s' % u) + print(('Adding %s' % u)) add(token, h, proto=proto) diff --git a/admin/bin/doorman_revoke b/admin/bin/doorman_revoke index b515f52..7a44045 100755 --- a/admin/bin/doorman_revoke +++ b/admin/bin/doorman_revoke @@ -3,9 +3,9 @@ import argparse from sys import stderr, argv -from lib.actions import * -from lib.storage import del_uid -from lib.password import get_token +from doorman.actions import * +from doorman.storage import del_uid +from doorman.password import get_token if __name__ == '__main__': url = argv[2] if len(argv) > 2 else None @@ -14,7 +14,7 @@ if __name__ == '__main__': status = revoke_uid(token, uid) if status.command == 'K': del_uid(uid) - print 'User %d revoked' % uid + print('User %d revoked' % uid) else: - print >> stderr, 'Unknown error:', status + print('Unknown error:', status, file=stderr) exit(1) diff --git a/admin/bin/doorman_revoke_card b/admin/bin/doorman_revoke_card index e5487a0..0e64a10 100755 --- a/admin/bin/doorman_revoke_card +++ b/admin/bin/doorman_revoke_card @@ -1,13 +1,13 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import argparse -import options +import doorman.options as options from sys import stderr, argv -from lib.actions import * -from lib.proto import Proto -from lib.storage import del_card -from lib.password import get_token +from doorman.actions import * +from doorman.proto import Proto +from doorman.storage import del_card +from doorman.password import get_token if __name__ == '__main__': parser = argparse.ArgumentParser( @@ -21,20 +21,20 @@ if __name__ == '__main__': parser.add_argument('-c', '--card', help='Card+PIN hash (will not scan), only required with -l') args = parser.parse_args() if args.local and not args.card: - print 'Local update requires a card to be specified!' + print('Local update requires a card to be specified!') exit(2) card = args.card if not args.local: token = get_token() proto = Proto(args.url) if not args.card: - print 'Please swipe token' + print('Please swipe token') frame = scan(token, proto=proto) assert(frame.command == 'S') card = frame.hash status = revoke_hash(token, card, proto=proto) if status.command != 'K': - print >> stderr, 'Unknown error:', str(status) + print('Unknown error:', str(status), file=stderr) exit(1) (uid, name) = del_card(card) - print 'Card %s (user %s, uid %d) revoked' % (card, name, uid) + print('Card %s (user %s, uid %d) revoked' % (card, name, uid)) diff --git a/admin/bin/doorman_revoke_user b/admin/bin/doorman_revoke_user index 6283066..c995ffd 100755 --- a/admin/bin/doorman_revoke_user +++ b/admin/bin/doorman_revoke_user @@ -1,13 +1,13 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import argparse -import options +import doorman.options as options from sys import stderr, argv -from lib.actions import * -from lib.proto import Proto -from lib.storage import cards_for_user, del_card -from lib.password import get_token +from doorman.actions import * +from doorman.proto import Proto +from doorman.storage import cards_for_user, del_card +from doorman.password import get_token if __name__ == '__main__': parser = argparse.ArgumentParser( @@ -26,18 +26,18 @@ if __name__ == '__main__': if args.local: for h in cards: del_card(h) - print 'Card %s revoked' % h + print('Card %s revoked' % h) else: token = get_token() proto = Proto(url) user = argv[1] for h in cards: - print 'Revoking card %s' % h + print('Revoking card %s' % h) status = revoke_hash(token, h, proto=proto) if status.command == 'K': del_card(h) - print 'Card %s revoked' % h + print('Card %s revoked' % h) else: - print >> stderr, 'Unknown error:', status + print('Unknown error:', status, file=stderr) exit(1) - print 'User %s\'s cards revoked' % user + print('User %s\'s cards revoked' % user) diff --git a/admin/bin/doorman_scan b/admin/bin/doorman_scan index cc352c8..0fb0fcc 100755 --- a/admin/bin/doorman_scan +++ b/admin/bin/doorman_scan @@ -1,19 +1,19 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 from sys import argv -from lib.actions import scan -from lib.proto import Proto -from lib.storage import get_card -from lib.password import get_token -import options +from doorman.actions import scan +from doorman.proto import Proto +from doorman.storage import get_card +from doorman.password import get_token +import doorman.options as options if __name__ == '__main__': url = argv[1] if len(argv) > 1 else options.url token = get_token() proto = Proto(url) - print 'Please swipe token' + print('Please swipe token') c = scan(token, proto=proto) assert(c.command.upper() == 'S') - print 'Hash: %s, UID: %x, Username: %s' % \ - (c.hash, c.uid, get_card(c.hash)[1]) + print('Hash: %s, UID: %x, Username: %s' % \ + (c.hash, c.uid, get_card(c.hash)[1])) diff --git a/admin/bin/doorman_show_cards b/admin/bin/doorman_show_cards index e6fa270..85e2660 100755 --- a/admin/bin/doorman_show_cards +++ b/admin/bin/doorman_show_cards @@ -1,7 +1,7 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 -from lib.storage import storage +from doorman.storage import storage if __name__ == '__main__': - for h, (u, n) in storage.iteritems(): - print 'hash=%s, uid=%s, nick=%s' % (h, u, n) + for h, (u, n) in storage.items(): + print('hash=%s, uid=%s, nick=%s' % (h, u, n)) diff --git a/admin/doorman/actions.py b/admin/doorman/actions.py index 229999e..621a6ce 100644 --- a/admin/doorman/actions.py +++ b/admin/doorman/actions.py @@ -1,6 +1,6 @@ -from proto import Proto -from options import * -from command import signed_command +from .proto import Proto +from .options import * +from .command import signed_command def scan(token, url=None, proto=None): proto = proto or Proto(url) diff --git a/admin/doorman/command.py b/admin/doorman/command.py index 88f7dae..6cb4bb0 100644 --- a/admin/doorman/command.py +++ b/admin/doorman/command.py @@ -2,7 +2,7 @@ from collections import namedtuple import hmac import hashlib -from options import * +from .options import * Property = namedtuple('Property', ['name', 'to_str', 'from_str', 'length', 'default']) @@ -48,7 +48,7 @@ def frame(name, fields): kw[f.name] = fv return cls(**kw) def __init__(self, **kw): - for n, v in kw.iteritems(): + for n, v in kw.items(): setattr(self, n, v) def __str__(self): s = '' @@ -76,7 +76,7 @@ def signed_command(command='P', hash='00'*32, uid=0, token=''): """Returns a MACd Command instance.""" data = str(Command(command=command, hash=hash, uid=uid, mac="aa"*32)) data = ','.join(data.split(',')[:3]) - print data - mac = hmac.HMAC(token, digestmod=hashlib.sha256) - mac.update(data) + print(data) + mac = hmac.HMAC(token.encode('ascii'), digestmod=hashlib.sha256) + mac.update(data.encode('ascii')) return Command(command=command, hash=hash, uid=uid, mac=mac.hexdigest()) diff --git a/admin/doorman/options.py b/admin/doorman/options.py index b8c7025..cd09ccf 100644 --- a/admin/doorman/options.py +++ b/admin/doorman/options.py @@ -7,7 +7,7 @@ shelf = './base' json = './cards.json' csv = './cards.csv' -storage = 'csv' +storage = "none" #whether the storage method should be encrypted storage_encrypt = False diff --git a/admin/doorman/password.py b/admin/doorman/password.py index 5971383..cd70f8f 100644 --- a/admin/doorman/password.py +++ b/admin/doorman/password.py @@ -1,7 +1,7 @@ import hashlib import getpass -import options +from . import options def get_des_storage_key(filename): password = getpass.getpass("DES Storage key (%s):" % filename) diff --git a/admin/doorman/proto.py b/admin/doorman/proto.py index 8ceb8d8..3c9efae 100644 --- a/admin/doorman/proto.py +++ b/admin/doorman/proto.py @@ -2,11 +2,9 @@ from time import sleep from sys import stderr import serial -import socket -import ssl -from command import Command -import options +from .command import Command +from . import options class RemoteException(Exception): pass @@ -16,30 +14,20 @@ class Proto(object): kwa.update(options.serial) kwa.update(kwargs) url = url or options.url - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(20) - print ('wrapping..') - ctx = ssl.create_default_context() - self.sock = ssl.wrap_socket(sock) - print ('connecting') - self.sock.connect((url, 443)) - self.sock.settimeout(60) - print ('done') - - self.fd = self.sock.makefile() - #self.fd = serial.serial_for_url(url, **kwa) + self.fd = serial.serial_for_url(url, **kwa) sleep(options.init_sleep) - ##self.fd.flushInput() - #self.fd.flushOutput() - print >> stderr, 'Serial port ready' + self.fd.flushInput() + self.fd.flushOutput() + print('Serial port ready', file=stderr) def send(self, command): cmd = str(command) + '\n' - print cmd + print(cmd) for i in cmd: - self.sock.send(i) + sleep(0.02) + self.fd.write(i.encode('ascii')) def recv(self): - line = self.fd.readline() - print (line) + line = self.fd.readline().decode('ascii') + print(line) if line[0] != '$': return self.recv() cmd = Command.from_str(line) diff --git a/admin/doorman/storage/__init__.py b/admin/doorman/storage/__init__.py index d074522..485acb8 100644 --- a/admin/doorman/storage/__init__.py +++ b/admin/doorman/storage/__init__.py @@ -1,2 +1,2 @@ -from classes import CsvStorage, JsonStorage -from ops import * +from .classes import CsvStorage, JsonStorage +from .ops import * diff --git a/admin/doorman/storage/classes.py b/admin/doorman/storage/classes.py index e6a93fe..113b2e8 100644 --- a/admin/doorman/storage/classes.py +++ b/admin/doorman/storage/classes.py @@ -1,6 +1,6 @@ from sys import stderr from collections import MutableMapping -from StringIO import StringIO +from io import StringIO import csv, json @@ -33,13 +33,13 @@ class CsvStorage(Storage): stored = {x[0]: [x[1], x[2]] for x in csv.reader(StringIO(text))} except IOError as e: - print >>stderr, e + print(e, file=stderr) stored = {} return stored def encode(self, data): f = StringIO() csv.writer(f).writerows( - [c, u, name] for c, (u, name) in data.iteritems()) + [c, u, name] for c, (u, name) in data.items()) return f.getvalue() class JsonStorage(Storage): @@ -47,7 +47,7 @@ class JsonStorage(Storage): try: stored = json.loads(self.encapsulation.data) except IOError as e: - print >>stderr, e + print(e, file=stderr) stored = {} return stored def encode(self, data): diff --git a/admin/doorman/storage/ops.py b/admin/doorman/storage/ops.py index 411c810..47b42ac 100644 --- a/admin/doorman/storage/ops.py +++ b/admin/doorman/storage/ops.py @@ -1,5 +1,5 @@ -import options -import storage_encapsulation +import doorman.options as options +from . import storage_encapsulation from .classes import CsvStorage, JsonStorage if options.storage_encrypt == True: @@ -17,8 +17,7 @@ if options.storage == 'csv': nobody = (None, '-unknown-') get_card = lambda h: storage.get(h, nobody) -cards_for_user = lambda name: map(lambda (k,v): k, - filter(lambda (k,(u,n)): n == name, storage.iteritems())) +cards_for_user = lambda name: [k_v1[0] for k_v1 in [k_u_n for k_u_n in iter(storage.items()) if k_u_n[1][1] == name]] def add_user(username, hash, uid): storage[hash] = (uid, username) @@ -27,11 +26,11 @@ def del_card(hash): return storage.pop(hash, nobody) def del_filter(f): - cards = map(lambda (k,v): k, filter(f, storage.iteritems())) + cards = [k_v[0] for k_v in list(filter(f, iter(storage.items())))] r = [] for c in cards: r.append(storage.pop(c, nobody)) return r -del_uid = lambda uid: del_filter(lambda (k, (u,n)): u == uid) -del_username = lambda name: del_filter(lambda (k, (u,n)): n == name) +del_uid = lambda uid: del_filter(lambda k_u_n2: k_u_n2[1][0] == uid) +del_username = lambda name: del_filter(lambda k_u_n3: k_u_n3[1][1] == name) diff --git a/admin/doorman/storage/storage_encapsulation.py b/admin/doorman/storage/storage_encapsulation.py index 4db29fd..59c062e 100644 --- a/admin/doorman/storage/storage_encapsulation.py +++ b/admin/doorman/storage/storage_encapsulation.py @@ -1,4 +1,4 @@ -# because admin.lib.password uses admin.options -_- +# because admin.doorman.password uses admin.options -_- if __name__ == "__main__": import sys sys.path.append("d:\\Development\\Projects\\doorman\\admin") @@ -7,7 +7,7 @@ import hashlib import os import tempfile -import lib.password as password +import doorman.password as password class RawFileEncapsulation(object): """ @@ -92,7 +92,7 @@ class DESFileEncapsulation(RawFileEncapsulation): def _decode_data(self, data): if data == "": - print "Input file empty. Assuming actually empty file." + print("Input file empty. Assuming actually empty file.") return "" from Crypto.Cipher import DES @@ -127,10 +127,10 @@ if __name__ == "__main__": except: n = 0 - print "ass! %i" % n + print("ass! %i" % n) r.data = str(n + 1) - print "try to modify the assfile, see it fail!" - raw_input() + print("try to modify the assfile, see it fail!") + input() r.end_transaction() |