def change_status(self): """Manually change the status of the BITS system""" with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None: textstatus = Status.CLOSED else: textstatus = Status.OPEN if curstatus.value == Status.CLOSED else Status.CLOSED LOG.info('Change of BITS to status={}'.format(textstatus) + ' from web interface.') message = '' try: status = query.log_status(session, textstatus, 'web') broadcast(status.jsondict()) notifier.send_status(textstatus) message = "Ora la sede è {}.".format(textstatus) except IntegrityError: LOG.error("Status changed too quickly, not logged.") message = "Errore: modifica troppo veloce!" raise finally: self.render('templates/admin.html', page_message=message)
def change_status(self): """Manually change the status of the BITS system""" with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None: textstatus = Status.CLOSED else: textstatus = Status.OPEN if curstatus.value == Status.CLOSED else Status.CLOSED LOG.info('Change of BITS to status=%r from web interface.', textstatus) message = '' try: status = query.log_status(session, textstatus, 'web') broadcast(status.jsondict()) notifier.send_status(textstatus) message = "Ora la sede è {}.".format(textstatus) except IntegrityError: LOG.error("Status changed too quickly, not logged.") message = "Errore: modifica troppo veloce!" raise finally: self.render( 'templates/admin.html', page_message=message, roster=MACUpdateHandler.ROSTER )
def get(self): """Display and paginate log.""" wants_json = self.get_argument("format", "html") == "json" offset = self.get_integer_or_400("offset", 0) limit = self.get_integer_or_400("limit", self.LINES_PER_PAGE) with session_scope() as session: latest_statuses = query.get_latest_statuses( session, offset=offset, limit=limit ) # Handle limit = 1 case (result is not a list) if type(latest_statuses) == Status: latest_statuses = [latest_statuses] if wants_json: self.write(self.jsonize(latest_statuses)) self.finish() else: self.render('templates/log.html', latest_statuses=latest_statuses, # Used by the paginator offset=offset, limit=self.LINES_PER_PAGE, count=query.get_number_of_statuses(session), )
def open(self): """Register new handler with MessageNotifier.""" StatusHandler.CLIENTS.register(self) with session_scope() as session: latest = query.get_latest_data(session) self.write_message(latest) LOG.debug('Registered client')
def get(self): """Display and paginate log.""" wants_json = self.get_argument("format", "html") == "json" offset = self.get_integer_or_400("offset", 0) limit = self.get_integer_or_400("limit", self.LINES_PER_PAGE) with session_scope() as session: latest_statuses = query.get_latest_statuses(session, offset=offset, limit=limit) # Handle limit = 1 case (result is not a list) if type(latest_statuses) == Status: latest_statuses = [latest_statuses] if wants_json: self.write(self.jsonize(latest_statuses)) self.finish() else: self.render( 'templates/log.html', latest_statuses=latest_statuses, # Used by the paginator offset=offset, limit=self.LINES_PER_PAGE, count=query.get_number_of_statuses(session), )
def on_open(self, info): """Register new handler with MessageNotifier.""" StatusConnection.CLIENTS.register(self) with session_scope() as session: latest = query.get_latest_data(session) self.send(latest) LOG.debug('Registered client')
def get_samples(self): with session_scope() as session: samples = session \ .query(Status.timestamp, Status.value) \ .filter((Status.value == Status.OPEN) | (Status.value == Status.CLOSED)) \ .order_by(asc(Status.timestamp)) \ .limit(self.samples_count) offset = self.first_open_offset(samples) return samples[offset:]
def post(self): now = datetime.now() remote_ip = self.request.remote_ip with session_scope() as session: last = query.get_last_login_attempt(session, remote_ip) if last is None: last = LoginAttempt(None, remote_ip) persist(session, last) else: if (now - last.timestamp) < timedelta(seconds=options.mac_update_interval): LOG.warning("Too frequent attempts to update, remote IP address is %s", remote_ip) raise HTTPError(403, "Too frequent") else: last.timestamp = now persist(session, last) try: password = self.get_argument("password") macs = self.get_argument("macs") except MissingArgumentError: LOG.warning("MAC update received malformed parameters: %s", self.request.arguments) raise HTTPError(400, "Bad parameters list") if not secure_compare(password, options.mac_update_password): LOG.warning("Client provided wrong password for MAC update!") raise HTTPError(403, "Wrong password") LOG.info("Authorized request to update list of checked-in users from IP address %s", remote_ip) macs = json.loads(macs) with session_scope() as session: names = session.\ query(distinct(User.name)).\ filter(User.userid == MACToUser.userid).\ filter(MACToUser.mac_hash .in_ (macs)).\ all() MACUpdateHandler.ROSTER = [n[0] for n in names] LOG.debug("Updated list of checked in users: %s", MACUpdateHandler.ROSTER)
def handle_temperature_command(sensorid, value): """Receives and log data received from remote sensor.""" LOG.info('Received temperature: sensorid=%r, value=%r', sensorid, value) try: sensorid = int(sensorid) value = float(value) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return with session_scope() as session: temp = query.log_temperature(session, value, sensorid, 'BITS') broadcast(temp.jsondict())
def get(self, slug): with session_scope() as session: page = query.get_page(session, slug) if page is None: raise tornado.web.HTTPError(404) self.render('templates/mdpage.html', body=markdown.markdown( page.body, safe_mode='escape' if options.mdescape else False, ), title=page.title, )
def handle_temperature_command(sensorid, value): """Receives and log data received from remote sensor.""" LOG.info('Received temperature: sensorid={}, value={}'.format( sensorid, value)) try: sensorid = int(sensorid) value = float(value) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return with session_scope() as session: temp = query.log_temperature(session, value, sensorid, 'BITS') broadcast(temp.jsondict())
def get(self, slug): with session_scope() as session: page = query.get_page(session, slug) if page is None: raise tornado.web.HTTPError(404) self.render( 'templates/mdpage.html', body=markdown.markdown( page.body, safe_mode='escape' if options.mdescape else False, ), title=page.title, )
def handle_message_command(message): """Handles message broadcast requests.""" LOG.info('Received message command: message=%r', message) try: decodedmex = base64.b64decode(message) except TypeError: LOG.error('Received message is not valid base64: %r', message) else: text = decodedmex.decode('utf8') #FIXME maybe get author ID from message? user = "******" with session_scope() as session: user = query.get_user(session, user) if not user: LOG.error("Non-existent user %r, not logging message.", user) return message = query.log_message(session, user, text) broadcast(message.jsondict()) notifier.send_message(text)
def post(self): text = self.get_argument('msgtext') username = self.get_current_user() text = xhtml_escape(text) LOG.info("{} sent message {!r} from web".format(username, text)) with session_scope() as session: user = query.get_user(session, username) message = query.log_message(session, user, text) LOG.info("Broadcasting to clients") broadcast(message.jsondict()) LOG.info("Notifying Fonera") notifier.send_message(text) self.render('templates/message.html', message='Messaggio inviato correttamente!', text=text)
def post(self): username = self.get_argument("username") password = self.get_argument("password") ip_address = self.request.remote_ip next = self.get_argument("next", "/") captcha_challenge = self.get_argument("recaptcha_challenge_field", "") captcha_response = self.get_argument("recaptcha_response_field", "") has_recaptcha = captcha_challenge or captcha_response with session_scope() as session: try: verified = verify(session, username, password, ip_address, has_recaptcha, captcha_challenge, captcha_response) except DoSError as error: LOG.warning("DoS protection: %s", error) self.log_offender_details() self.render( 'templates/login.html', next=next, message="Tentativi dal tuo IP over 9000...", show_recaptcha=True, previous_attempt_incorrect=has_recaptcha ) return if verified: self.set_secure_cookie( self.USER_COOKIE_NAME, username, expires_days=options.cookie_max_age_days ) LOG.info("Authenticating user %r", username) self.redirect(next) else: LOG.warning("Failed authentication for user %r", username) self.log_offender_details() self.render( 'templates/login.html', next=next, message="Password/username sbagliati!", show_recaptcha=has_recaptcha, # If we have a captcha at this point, it means we already failed once previous_attempt_incorrect=True )
def post(self): username = self.get_argument("username", None) password = self.get_argument("password", None) next = self.get_argument("next", "/") with session_scope() as session: authenticated = verify(session, username, password) if authenticated: self.set_secure_cookie(self.USER_COOKIE_NAME, username, expires_days=options.cookie_max_age_days) LOG.info("Authenticating user `{}`".format(username)) self.redirect(next) else: LOG.warning("Wrong authentication for user `{}`".format(username)) self.render('templates/login.html', next=next, message="Password/username sbagliati!")
def post(self): text = self.get_argument('msgtext') username = self.get_current_user() LOG.info("%r sent message %r from web", username, text) with session_scope() as session: user = query.get_user(session, username) message = query.log_message(session, user, text) LOG.info("Broadcasting to clients") broadcast(message.jsondict()) LOG.info("Notifying Fonera") notifier.send_message(text) self.render( 'templates/message.html', message='Messaggio inviato correttamente!', text=text )
def handle_message_command(message): """Handles message broadcast requests.""" LOG.info('Received message command: message={!r}'.format(message)) try: decodedmex = base64.b64decode(message) except TypeError: LOG.error('Received message is not valid base64: {!r}'.format(message)) else: text = decodedmex.decode('utf8') #FIXME maybe get author ID from message? user = "******" with session_scope() as session: user = query.get_user(session, user) if not user: LOG.error( "Non-existent user {}, not logging message.".format(user)) return message = query.log_message(session, user, text) broadcast(message.jsondict()) notifier.send_message(text)
def handle_status_command(status): """Update status. Will reject two identical and consecutive updates (prevents opening when already open and vice-versa).""" LOG.info('Received status: %r', status) try: status = int(status) except ValueError: LOG.error('Wrong type for parameters in temperature command') return if status not in (0, 1): LOG.error('Non existent status %r, ignoring.', status) return textstatus = Status.OPEN if status == 1 else Status.CLOSED with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None or curstatus.value != textstatus: status = query.log_status(session, textstatus, 'BITS') broadcast(status.jsondict()) notifier.send_status(textstatus) else: LOG.error('BITS already open/closed! Ignoring.')
def post(self): username = self.get_argument("username", None) password = self.get_argument("password", None) next = self.get_argument("next", "/") with session_scope() as session: authenticated = verify(session, username, password) if authenticated: self.set_secure_cookie( self.USER_COOKIE_NAME, username, expires_days=options.cookie_max_age_days ) LOG.info("Authenticating user `{}`".format(username)) self.redirect(next) else: LOG.warning("Wrong authentication for user `{}`".format(username)) self.render( 'templates/login.html', next=next, message="Password/username sbagliati!" )
def handle_status_command(status): """Update status. Will reject two identical and consecutive updates (prevents opening when already open and vice-versa).""" LOG.info('Received status: {}'.format(status)) try: status = int(status) except ValueError: LOG.error('Wrong type for parameters in temperature command') return if status not in (0, 1): LOG.error('Non existent status {}, ignoring.'.format(status)) return textstatus = Status.OPEN if status == 1 else Status.CLOSED with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None or curstatus.value != textstatus: status = query.log_status(session, textstatus, 'BITS') broadcast(status.jsondict()) notifier.send_status(textstatus) else: LOG.error('BITS already open/closed! Ignoring.')
def get(self): with session_scope() as session: status = query.get_current_status(session) answer = '1' if status is not None and status.value == Status.OPEN else '0' self.write(answer) self.finish()
# Copyright (C) 2013 Stefano Sanfilippo # Copyright (C) 2013 BITS development team # # This file is part of bitsd, which is released under the terms of # GNU GPLv3. See COPYING at top level for more information. # """ Fill Database with Markdown pages. """ # DO NOT REMOVE import bitsd.properties from bitsd.persistence import start from bitsd.persistence.models import Page from bitsd.persistence.engine import persist, session_scope from tornado.options import parse_config_file if __name__ == '__main__': try: parse_config_file('/etc/bitsd.conf') except IOError: print("No /etc/bitsd.conf found, ignoring.") start() with open('INFO.md', 'r') as info: infopage = Page('Info', info.read().decode('utf-8')) with session_scope() as session: persist(session, infopage)
import sys from getpass import getpass if __name__ == '__main__': try: parse_config_file('/etc/bitsd.conf') except IOError: print ("[INFO] No /etc/bitsd.conf found, ignoring.") action = sys.argv[1] username = sys.argv[2] start() with session_scope() as session: if action == 'add': password = getpass('Password for `{}`:'.format(username)) useradd(session, username, password) elif action == 'delete': userdel(session, username) elif action == 'modify': password = getpass('New password for `{}`:'.format(username)) usermod(session, username, password) else: sys.stderr.write( "Usage: {} add|delete|modify <username>\n".format(sys.argv[0]) ) sys.exit(1)
def get(self): with session_scope() as session: self.write(query.get_latest_data(session)) self.finish()