we MOVED
[saartuer.git] / tysock.py
diff --git a/tysock.py b/tysock.py
deleted file mode 100644 (file)
index b987567..0000000
--- a/tysock.py
+++ /dev/null
@@ -1,86 +0,0 @@
-import socket, os, stat, struct, pwd, errno
-from statemachine import StateMachine
-from libtuer import logger
-SO_PEERCRED = 17 # DO - NOT - TOUCH
-
-tuergroupid = 1005
-socketname = "/run/tuer.sock"
-
-# send to client for information but don't care if it arrives
-def waynesend (conn, what):
-       try:
-               conn.send(what.encode())
-       except:
-               pass # we do not care
-
-# delete a file, don't care if it did not exist in the first place
-def forcerm(name):
-       try:
-               os.unlink (name)
-       except OSError as e:
-               # only ignore error if it was "file didn't exist"
-               if e.errno != errno.ENOENT:
-                       raise
-
-# the class doing the actual work
-class TySocket():
-       CMDs = {
-               b'buzz': StateMachine.CMD_BUZZ,
-               b'unlock': StateMachine.CMD_UNLOCK,
-               b'lock': StateMachine.CMD_LOCK,
-               b'fallback_mode_on': StateMachine.CMD_FALLBACK_ON,
-               b'fallback_mode_off': StateMachine.CMD_FALLBACK_OFF,
-               b'status': StateMachine.CMD_STATUS,
-       }
-       
-       def __init__(self, sm):
-               self._sm = sm
-               # create socket
-               self._sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
-               # delete old socket file and don't bitch around if it's not there
-               forcerm(socketname)
-               # bind socket to file name
-               self._sock.bind (socketname)
-               # allow only users in the tuergroup to write to the socket
-               os.chown (socketname, 0, tuergroupid)
-               os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
-               # listen to the people, but not too many at once
-               self._sock.listen(1)
-       
-       def _answer(self, conn):
-               def answer(msg, lastMsg = True):
-                       # this is called in another thread, so it should be quick and not touch the TySocket
-                       waynesend(conn, msg)
-                       if lastMsg:
-                               conn.close()
-               return answer
-       
-       def accept(self):
-               '''Handles incoming connections and keyboard events'''
-               self._sock.settimeout(None)
-               while True:
-                       # accept connections
-                       conn, addr = self._sock.accept()
-                       conn.settimeout(0.1)
-                       try:
-                               # get peer information
-                               (pid, uid, gid) = struct.unpack('3i', conn.getsockopt(socket.SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i')))
-                               # get some data from the client (enough to hold any valid command)
-                               data = conn.recv (32)
-                               # log the command
-                               logger.info("TySocket: Received command from %s (uid %d): %s" % (pwd.getpwuid(uid).pw_name, uid, str(data)))
-                               # lookup the command, send it to state machine
-                               if data in self.CMDs:
-                                       self._sm.callback(self.CMDs[data], self._answer(conn))
-                                       # _answer will be called, and it will close the connection
-                                       continue # make sure we break so we don't close it
-                               else:
-                                       waynesend(conn, 'Command not found')
-                       except KeyboardInterrupt:
-                               raise # forward Ctrl-C to the outside
-                       except socket.timeout:
-                               # it's okay
-                               logger.info("TySocket: Connection timed out")
-                       except Exception as e:
-                               logger.critical("TySocket: Something went wrong: %s" % str(e))
-                       conn.close()