- # TODO: use addr to determine the client for logging
- # get some data from the client (enough to hold any valid command)
- data = conn.recv (32)
- # log the command
- log("received command: %s" % str(data))
- # lookup the command, if it's not in the dict, use the doNothing function instead
- # and execute the looked up command or doNothing with the connection, so it can respond to the client
- pindict.get(data,doNothing)(conn)
- # close connection cleanly
- conn.close()
+ 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
+ log("received command from %s (uid %d): %s" % (pwd.getpwuid(uid).pw_name,uid, str(data)))
+ # lookup the command, if it's not in the dict, use the doNothing function instead
+ # and execute the looked up command or doNothing with the connection, so it can respond to the client
+ pindict.get(data,doNothing)(conn)
+ log("done")
+ # close connection cleanly
+ conn.close()
+ except Exception as e:
+ log("Something went wrong: %s\n...continuing." % str(e))