From: John Hodge Date: Sun, 19 Feb 2017 10:45:09 +0000 (+0800) Subject: Move MIFARE database to dispense X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=8cda0f578277ed1c4ce70c712d2e54311dd79e70;p=uccvend-vendserver.git Move MIFARE database to dispense --- diff --git a/VendServer/OpenDispense.py b/VendServer/OpenDispense.py index 3790348..de8ff84 100644 --- a/VendServer/OpenDispense.py +++ b/VendServer/OpenDispense.py @@ -15,6 +15,9 @@ import pwd from subprocess import Popen, PIPE from LDAPConnector import get_uid,get_uname, set_card_id +DISPENSE_ENDPOINT = ("localhost", 11020) +DISPSRV_MIFARE = True + class OpenDispense(DispenseInterface): _username = "" _disabled = True @@ -67,27 +70,73 @@ class OpenDispense(DispenseInterface): return False def authMifareCard(self, cardId): - # Get the users ID - self._userid = get_uid(cardId) + if DISPSRV_MIFARE: + card_base64 = base64.b64encode(cardId) + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + sock.connect(DISPENSE_ENDPOINT) + sock.write("AUTHIDENT\n") + rsp = sock.readline() + assert "200" in rsp + sock.write("AUTHCARD %s\n" % (card_base64,)) + rsp = sock.readline() + if not "200" in rsp: + raise ValueError, "no UID found for card ID" + username = rsp.split('=')[1] + + # Check for thier username + try: + # Get info from the system (by username) + info = pwd.getpwnam(username) + except KeyError: + logging.info('getting pin for uid %d: user not in password file'%uid) + return False + else: + # Get the users ID + self._userid = get_uid(cardId) - # Check for thier username - try: - # Get info from - info = pwd.getpwuid(userId) - except KeyError: - logging.info('getting pin for uid %d: user not in password file'%uid) - return False + # Check for thier username + try: + # Get info from the system (by UID) + info = pwd.getpwuid(self._userid) + except KeyError: + logging.info('getting pin for uid %d: user not in password file'%uid) + return False # If we get this far all is good self._loggedIn = True self._disabled = False + self._userid = info.pw_uid self._username = info.pw_name return True def addCard(self, cardId): - if self.isLoggedIn(): - set_card_id(self._userId, cardId) - return True + if not self.isLoggedIn(): + return False + if DISPSRV_MIFARE: + card_base64 = base64.b64encode(cardId) + logging.info('Enrolling card %s to uid %s (%s)' % (cardId, self._userId, self._username)) + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + sock.connect(DISPENSE_ENDPOINT) + sock.write("AUTHIDENT\n") + rsp = sock.readline() + assert "200" in rsp + sock.write("SETEUSER %s\n", self._username) + rsp = sock.readline() + assert "200" in rsp + sock.write("CARD_ADD %s\n", card_base64) + rsp = sock.readline() + if "200" in rsp: + return True + else: + return False + else: + if get_uid(cardId) != None: + return False + else: + logging.info('Enrolling card %s to uid %s (%s)' % (cardId, self._userId, self._username)) + set_card_id(self._userId, cardId) + return True def isLoggedIn(self): return self._loggedIn diff --git a/VendServer/VendServer.py b/VendServer/VendServer.py index be8cea0..6dfbfe1 100755 --- a/VendServer/VendServer.py +++ b/VendServer/VendServer.py @@ -19,7 +19,6 @@ from Idler import GreetingIdler,TrainIdler,GrayIdler,StringIdler,ClockIdler,Fort from SnackConfig import get_snack#, get_snacks import socket from posix import geteuid -from LDAPConnector import get_uid,get_uname, set_card_id from OpenDispense import OpenDispense as Dispense CREDITS=""" @@ -815,15 +814,11 @@ class VendServer(): self._last_card_id = card_id - res = self.dispense.addCard(card_id) - - if get_uid(card_id) != None: + if not self.dispense.addCard(card_id): self.vstatus.mk.set_messages( [(self.center('ALREADY'), False, 0.5), (self.center('ENROLLED'), False, 0.5)]) else: - logging.info('Enrolling card %s to uid %s (%s)'%(card_id, self.vstatus.cur_user, self.vstatus.username)) - self.set_card_id(self.vstatus.cur_user, self.card_id) self.vstatus.mk.set_messages( [(self.center('CARD'), False, 0.5), (self.center('ENROLLED'), False, 0.5)])