2018-03-13 18:26:12 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-03-18 18:25:06 +01:00
|
|
|
"""
|
|
|
|
Doorlockd -- Binary Kitchen's smart door opener
|
|
|
|
|
|
|
|
Copyright (c) Binary Kitchen e.V., 2018
|
|
|
|
|
|
|
|
Author:
|
|
|
|
Ralf Ramsauer <ralf@binary-kitchen.de>
|
|
|
|
|
|
|
|
This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
the LICENSE file in the top-level directory.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
"""
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
from random import sample
|
|
|
|
from serial import Serial
|
2018-03-18 22:03:48 +01:00
|
|
|
from subprocess import Popen
|
2018-03-18 15:48:59 +01:00
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-20 23:49:11 +01:00
|
|
|
from flask import abort, Flask, render_template, request
|
2018-03-13 18:26:12 +01:00
|
|
|
from flask_bootstrap import Bootstrap
|
|
|
|
from flask_socketio import SocketIO
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import PasswordField, StringField, SubmitField
|
|
|
|
from wtforms.validators import DataRequired, Length
|
|
|
|
|
|
|
|
__author__ = 'Ralf Ramsauer'
|
|
|
|
__copyright = 'Copyright (c) Ralf Ramsauer, 2018'
|
|
|
|
__license__ = 'GPLv2'
|
|
|
|
__email__ = 'ralf@binary-kitchen.de'
|
|
|
|
__status__ = 'Development'
|
|
|
|
__maintainer__ = 'Ralf Ramsauer'
|
|
|
|
__version__ = '0.01a'
|
|
|
|
|
|
|
|
log_level = logging.DEBUG
|
|
|
|
date_fmt = '%Y-%m-%d %H:%M:%S'
|
|
|
|
log_fmt = '%(asctime)-15s %(levelname)-8s %(message)s'
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
default_ldap_uri = 'ldaps://ldap1.binary.kitchen/ ' \
|
|
|
|
'ldaps://ldap2.binary.kitchen/ ' \
|
|
|
|
'ldaps://ldapm.binary.kitchen/'
|
|
|
|
default_binddn = 'cn=%s,ou=people,dc=binary-kitchen,dc=de'
|
|
|
|
|
|
|
|
html_title = 'Binary Kitchen Doorlock (%s - v%s)' % (__status__, __version__)
|
|
|
|
|
|
|
|
webapp = Flask(__name__)
|
2018-03-18 16:15:48 +01:00
|
|
|
webapp.config.from_pyfile('config.cfg')
|
2018-03-13 18:26:12 +01:00
|
|
|
socketio = SocketIO(webapp, async_mode=None)
|
|
|
|
Bootstrap(webapp)
|
2018-03-16 16:52:43 +01:00
|
|
|
serial_port = webapp.config.get('SERIAL_PORT')
|
|
|
|
simulate = webapp.config.get('SIMULATE')
|
2018-03-21 00:58:16 +01:00
|
|
|
run_hooks = webapp.config.get('RUN_HOOKS')
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
# copied from sudo
|
|
|
|
eperm_insults = {
|
|
|
|
'Wrong! You cheating scum!',
|
|
|
|
'And you call yourself a Rocket Scientist!',
|
|
|
|
'No soap, honkie-lips.',
|
|
|
|
'Where did you learn to type?',
|
|
|
|
'Are you on drugs?',
|
|
|
|
'My pet ferret can type better than you!',
|
|
|
|
'You type like i drive.',
|
|
|
|
'Do you think like you type?',
|
|
|
|
'Your mind just hasn\'t been the same since the electro-shock, has it?',
|
|
|
|
'Maybe if you used more than just two fingers...',
|
|
|
|
'BOB says: You seem to have forgotten your passwd, enter another!',
|
|
|
|
'stty: unknown mode: doofus',
|
|
|
|
'I can\'t hear you -- I\'m using the scrambler.',
|
|
|
|
'The more you drive -- the dumber you get.',
|
|
|
|
'Listen, broccoli brains, I don\'t have time to listen to this trash.',
|
|
|
|
'I\'ve seen penguins that can type better than that.',
|
|
|
|
'Have you considered trying to match wits with a rutabaga?',
|
|
|
|
'You speak an infinite deal of nothing',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def choose_insult():
|
|
|
|
return(sample(eperm_insults, 1)[0])
|
|
|
|
|
|
|
|
|
2018-03-18 22:03:48 +01:00
|
|
|
def start_hook(script):
|
2018-03-21 00:58:16 +01:00
|
|
|
if not run_hooks:
|
|
|
|
log.info('Hooks disabled: not starting %s' % script)
|
2018-03-18 22:03:48 +01:00
|
|
|
return
|
|
|
|
log.info('Starting hook %s' % script)
|
|
|
|
Popen(['nohup', script])
|
|
|
|
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
class AuthMethod(Enum):
|
|
|
|
LDAP_USER_PW = 1
|
|
|
|
|
|
|
|
|
|
|
|
class DoorState(Enum):
|
|
|
|
Close = 1
|
|
|
|
Open = 2
|
|
|
|
|
|
|
|
def to_img(self):
|
|
|
|
led = 'red'
|
|
|
|
if self == DoorState.Open:
|
|
|
|
led = 'green'
|
2018-03-18 17:22:08 +01:00
|
|
|
return 'static/led-%s.png' % led
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def to_html(self):
|
|
|
|
if self == DoorState.Open:
|
|
|
|
return 'Offen'
|
|
|
|
return 'Zu'
|
|
|
|
|
|
|
|
|
|
|
|
class LogicResponse(Enum):
|
|
|
|
Success = 0
|
|
|
|
Perm = 1
|
|
|
|
AlreadyLocked = 2
|
|
|
|
AlreadyOpen = 3
|
|
|
|
Inval = 4
|
|
|
|
LDAP = 5
|
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
EmergencyUnlock = 10,
|
|
|
|
ButtonLock = 11,
|
|
|
|
ButtonUnlock = 12,
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
def to_html(self):
|
|
|
|
if self == LogicResponse.Success:
|
|
|
|
return 'Yo, passt.'
|
|
|
|
elif self == LogicResponse.Perm:
|
|
|
|
return choose_insult()
|
|
|
|
elif self == LogicResponse.AlreadyLocked:
|
|
|
|
return 'Narf. Schon zu.'
|
|
|
|
elif self == LogicResponse.AlreadyOpen:
|
|
|
|
return 'Schon offen, treten Sie ein!'
|
|
|
|
elif self == LogicResponse.Inval:
|
|
|
|
return 'Das was du willst geht nicht.'
|
|
|
|
elif self == LogicResponse.LDAP:
|
|
|
|
return 'Moep! Geh LDAP fixen!'
|
2018-03-19 18:46:33 +01:00
|
|
|
elif self == LogicResponse.EmergencyUnlock:
|
|
|
|
return '!!! Emergency Unlock !!!'
|
|
|
|
elif self == LogicResponse.ButtonLock:
|
|
|
|
return 'Closed by lock button'
|
|
|
|
elif self == LogicResponse.ButtonUnlock:
|
|
|
|
return 'Temporary unlock by unlock button'
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
return 'Bitte spezifizieren Sie.'
|
|
|
|
|
|
|
|
|
|
|
|
class DoorHandler:
|
|
|
|
state = DoorState.Close
|
2018-03-20 00:18:46 +01:00
|
|
|
do_schnapper = False
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
CMD_UNLOCK = b'u'
|
|
|
|
CMD_LOCK = b'l'
|
2018-03-20 00:18:46 +01:00
|
|
|
CMD_SCHNAPPER = b's'
|
2018-03-19 18:46:33 +01:00
|
|
|
BUTTON_LOCK_PRESS = b'L'
|
|
|
|
BUTTON_UNLOCK_PRESS = b'U'
|
|
|
|
BUTTON_EMERGENCY_PRESS = b'E'
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
def __init__(self, device):
|
2018-03-16 16:52:43 +01:00
|
|
|
if simulate:
|
|
|
|
return
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
self.serial = Serial(device, baudrate=9600, bytesize=8, parity='N',
|
2018-03-19 18:46:33 +01:00
|
|
|
stopbits=1, timeout=0)
|
2018-03-13 18:26:12 +01:00
|
|
|
self.thread = Thread(target=self.thread_worker)
|
|
|
|
self.thread.start()
|
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
def handle_input(self, recv, expect=None):
|
|
|
|
if recv == DoorHandler.BUTTON_LOCK_PRESS:
|
|
|
|
self.state = DoorState.Close
|
|
|
|
logic.emit_status(LogicResponse.ButtonLock)
|
|
|
|
elif recv == DoorHandler.BUTTON_UNLOCK_PRESS:
|
|
|
|
logic.emit_status(LogicResponse.ButtonUnlock)
|
|
|
|
elif recv == DoorHandler.BUTTON_EMERGENCY_PRESS:
|
|
|
|
logic.emit_status(LogicResponse.EmergencyUnlock)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
if expect is None:
|
|
|
|
return True
|
|
|
|
return recv == expect
|
|
|
|
|
|
|
|
def send_command(self, cmd):
|
|
|
|
self.serial.write(cmd)
|
|
|
|
self.serial.flush()
|
|
|
|
sleep(0.1)
|
|
|
|
char = self.serial.read(1)
|
|
|
|
if not self.handle_input(char, cmd):
|
|
|
|
log.warning('Sent serial command, got wrong response')
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def thread_worker(self):
|
|
|
|
while True:
|
2018-03-19 18:46:33 +01:00
|
|
|
sleep(0.4)
|
|
|
|
while True:
|
|
|
|
char = self.serial.read(1)
|
|
|
|
if len(char) == 0:
|
|
|
|
break
|
|
|
|
self.handle_input(char)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
if self.state == DoorState.Open:
|
2018-03-19 18:46:33 +01:00
|
|
|
self.send_command(DoorHandler.CMD_UNLOCK)
|
2018-03-20 00:18:46 +01:00
|
|
|
if self.do_schnapper:
|
|
|
|
self.send_command(DoorHandler.CMD_SCHNAPPER)
|
|
|
|
self.do_schnapper = False
|
2018-03-13 18:26:12 +01:00
|
|
|
elif self.state == DoorState.Close:
|
2018-03-19 18:46:33 +01:00
|
|
|
self.send_command(DoorHandler.CMD_LOCK)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def open(self):
|
2018-03-20 00:18:46 +01:00
|
|
|
self.do_schnapper = True
|
2018-03-13 18:26:12 +01:00
|
|
|
if self.state == DoorState.Open:
|
|
|
|
return LogicResponse.AlreadyOpen
|
|
|
|
|
|
|
|
self.state = DoorState.Open
|
2018-03-19 18:54:18 +01:00
|
|
|
start_hook('./scripts/post_unlock')
|
2018-03-13 18:26:12 +01:00
|
|
|
return LogicResponse.Success
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.state == DoorState.Close:
|
|
|
|
return LogicResponse.AlreadyLocked
|
|
|
|
|
|
|
|
self.state = DoorState.Close
|
2018-03-19 18:54:18 +01:00
|
|
|
start_hook('./scripts/post_lock')
|
2018-03-13 18:26:12 +01:00
|
|
|
return LogicResponse.Success
|
|
|
|
|
|
|
|
def request(self, state):
|
|
|
|
if state == DoorState.Close:
|
|
|
|
return self.close()
|
|
|
|
elif state == DoorState.Open:
|
|
|
|
return self.open()
|
|
|
|
|
|
|
|
|
|
|
|
class Logic:
|
2018-03-18 16:35:09 +01:00
|
|
|
def __init__(self):
|
2018-03-16 16:52:43 +01:00
|
|
|
self.door_handler = DoorHandler(serial_port)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def _try_auth_ldap(self, user, password):
|
2018-03-16 16:52:43 +01:00
|
|
|
if simulate:
|
|
|
|
log.info('SIMULATION MODE! ACCEPTING ANYTHING!')
|
|
|
|
return LogicResponse.Success
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
log.info('Trying to LDAP auth (user, password) as user %s', user)
|
|
|
|
return LogicResponse.LDAP
|
|
|
|
|
|
|
|
def try_auth(self, credentials):
|
|
|
|
method = credentials[0]
|
|
|
|
if method == AuthMethod.LDAP_USER_PW:
|
|
|
|
return self._try_auth_ldap(credentials[1], credentials[2])
|
|
|
|
|
|
|
|
return LogicResponse.Inval
|
|
|
|
|
|
|
|
def _request(self, state, credentials):
|
|
|
|
err = self.try_auth(credentials)
|
|
|
|
if err != LogicResponse.Success:
|
|
|
|
return err
|
|
|
|
return self.door_handler.request(state)
|
|
|
|
|
|
|
|
def request(self, state, credentials):
|
|
|
|
err = self._request(state, credentials)
|
2018-03-18 17:47:50 +01:00
|
|
|
self.emit_status(err)
|
2018-03-13 18:26:12 +01:00
|
|
|
return err
|
|
|
|
|
2018-03-18 17:47:50 +01:00
|
|
|
def emit_status(self, message=None):
|
|
|
|
led = self.state.to_img()
|
|
|
|
if message is None:
|
|
|
|
message = self.state.to_html()
|
|
|
|
else:
|
|
|
|
message = message.to_html()
|
|
|
|
|
|
|
|
socketio.emit('status', {'led': led, 'message': message})
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
return self.door_handler.state
|
|
|
|
|
|
|
|
|
|
|
|
class AuthenticationForm(FlaskForm):
|
|
|
|
username = StringField('Username', [Length(min=4, max=25)])
|
|
|
|
password = PasswordField('Password', [DataRequired()])
|
|
|
|
open = SubmitField('Open')
|
|
|
|
close = SubmitField('Close')
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
FlaskForm.__init__(self, *args, **kwargs)
|
|
|
|
self.desired_state = DoorState.Close
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
if not FlaskForm.validate(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.open.data:
|
|
|
|
self.desired_state = DoorState.Open
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-03-18 18:21:07 +01:00
|
|
|
@socketio.on('request_status')
|
2018-03-13 18:26:12 +01:00
|
|
|
@socketio.on('connect')
|
|
|
|
def on_connect():
|
2018-03-18 17:47:50 +01:00
|
|
|
logic.emit_status()
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
@webapp.route('/display')
|
|
|
|
def display():
|
2018-03-18 15:48:00 +01:00
|
|
|
if request.remote_addr != '127.0.0.1':
|
|
|
|
abort(403)
|
2018-03-13 18:26:12 +01:00
|
|
|
return render_template('display.html')
|
|
|
|
|
|
|
|
|
|
|
|
@webapp.route('/', methods=['GET', 'POST'])
|
|
|
|
def home():
|
|
|
|
authentication_form = AuthenticationForm()
|
|
|
|
response = None
|
|
|
|
|
2018-03-21 00:29:13 +01:00
|
|
|
# detect old API if the 'api' POST variable is set.
|
|
|
|
# NOTE: THESE BITS WILL BE REMOVED ONCE EVERYONE UPDATED THEIR APP
|
|
|
|
if request.method == 'POST' and request.form.get('api'):
|
|
|
|
log.info('Deprecated API usage detected')
|
|
|
|
user = request.form.get('user')
|
|
|
|
password = request.form.get('pass')
|
|
|
|
command = request.form.get('command')
|
|
|
|
|
|
|
|
if any(v is None for v in [user, password, command]):
|
|
|
|
log.warning('Incomplete deprecated API request')
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
desired_state = DoorState.Close
|
|
|
|
if command == 'unlock':
|
|
|
|
desired_state = DoorState.Open
|
|
|
|
credentials = AuthMethod.LDAP_USER_PW, user, password
|
|
|
|
|
|
|
|
log.info('Incoming request from %s' % user.encode('utf-8'))
|
|
|
|
log.info(' desired state: %s' % desired_state)
|
|
|
|
log.info(' current state: %s' % logic.state)
|
|
|
|
log.info(' -> Knock knock, %s, please update your app!' % user)
|
|
|
|
|
|
|
|
response = logic.request(desired_state, credentials)
|
|
|
|
if response == LogicResponse.Success:
|
|
|
|
return '0'
|
|
|
|
elif response == LogicResponse.Perm:
|
|
|
|
return '7'
|
|
|
|
elif response == LogicResponse.AlreadyLocked:
|
|
|
|
return '3'
|
|
|
|
elif response == LogicResponse.AlreadyOpen:
|
|
|
|
return '2'
|
|
|
|
elif response == LogicResponse.LDAP:
|
|
|
|
return '10'
|
|
|
|
else:
|
|
|
|
return '1' # Fail-mode
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
if request.method == 'POST' and authentication_form.validate():
|
|
|
|
user = authentication_form.username.data
|
|
|
|
password = authentication_form.password.data
|
|
|
|
credentials = AuthMethod.LDAP_USER_PW, user, password
|
|
|
|
|
2018-03-20 00:54:52 +01:00
|
|
|
log.info('Incoming request from %s' % user.encode('utf-8'))
|
2018-03-13 18:26:12 +01:00
|
|
|
desired_state = authentication_form.desired_state
|
|
|
|
log.info(' desired state: %s' % desired_state)
|
|
|
|
log.info(' current state: %s' % logic.state)
|
|
|
|
response = logic.request(desired_state, credentials)
|
|
|
|
log.info(' response: %s' % response)
|
|
|
|
|
2018-03-21 00:29:13 +01:00
|
|
|
# Don't trust python, zero credentials
|
|
|
|
user = password = credentials = None
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
return render_template('index.html',
|
|
|
|
authentication_form=authentication_form,
|
|
|
|
response=response,
|
|
|
|
state_text=logic.state.to_html(),
|
2018-03-18 17:22:08 +01:00
|
|
|
led=logic.state.to_img(),
|
2018-03-13 18:26:12 +01:00
|
|
|
title=html_title)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=log_level, stream=sys.stdout,
|
|
|
|
format=log_fmt, datefmt=date_fmt)
|
|
|
|
log.info('Starting doorlockd')
|
2018-03-18 16:35:09 +01:00
|
|
|
log.info('Using serial port: %s' % webapp.config.get('SERIAL_PORT'))
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-18 16:35:09 +01:00
|
|
|
logic = Logic()
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-18 17:22:08 +01:00
|
|
|
socketio.run(webapp, host='0.0.0.0', port=8080)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
sys.exit(0)
|