start implementing the new all-great tuerd
[saartuer.git] / tysock.py
diff --git a/tysock.py b/tysock.py
new file mode 100644 (file)
index 0000000..3951d1f
--- /dev/null
+++ b/tysock.py
@@ -0,0 +1,80 @@
+import socket, os, stat
+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'open': StateMachine.CMD_OPEN,
+       }
+       
+       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):
+                       # this is called in another thread, so it should be quick and not touch the TySocket
+                       waynesend(conn, msg)
+                       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
+                               else:
+                                       waynesend(conn, 'Command not found')
+                                       conn.close()
+                       except KeyboardInterrupt:
+                               raise # forward Ctrl-C to the outside
+                       except Exception as e:
+                               logger.error("TySocket: Something went wrong: %s" % str(e))
+       
+       def stop(self):
+               pass