oublis de fichiers divers et symlinks for testlibs
This commit is contained in:
parent
e2611a770a
commit
b004906464
12 changed files with 1165 additions and 135 deletions
88
impression/digicode_server.py
Executable file
88
impression/digicode_server.py
Executable file
|
@ -0,0 +1,88 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import SocketServer
|
||||
|
||||
CODES = "/var/impression/codes"
|
||||
PIDFILE = "/var/run/digicode.pid"
|
||||
HOST, PORT = "zamok.adm.crans.org", 1200
|
||||
|
||||
def log(message = "", logfile = "/var/log/crans/digicode.log"):
|
||||
"""Log a message to the default logfile"""
|
||||
log = open(logfile, "a")
|
||||
if message:
|
||||
log.write("%s %s\n" % (time.strftime("%b %d %H:%M:%S"), message))
|
||||
log.flush()
|
||||
log.close()
|
||||
|
||||
def runme():
|
||||
os.chdir(CODES)
|
||||
|
||||
#lpadmin
|
||||
os.setegid(108)
|
||||
#freerad
|
||||
os.seteuid(120)
|
||||
|
||||
log("Starting server!")
|
||||
server = SocketServer.UDPServer((HOST, PORT), VigileHandler)
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
class VigileHandler(SocketServer.BaseRequestHandler):
|
||||
"""Handler class for SocketServers, answering to door requests"""
|
||||
def handle(self):
|
||||
"""Handle the request the door sent us"""
|
||||
data = self.request[0].lower()
|
||||
socket = self.request[1]
|
||||
log("%s wrote: %s" % (self.client_address[0], data))
|
||||
|
||||
# if data starts with o, opened door validation, else should
|
||||
# be a code
|
||||
if not data.startswith("o"):
|
||||
valide, contents = self.check_code(data)
|
||||
if valide:
|
||||
socket.sendto("passoir,o=1", self.client_address)
|
||||
log("valid code! (%s)" % contents.strip())
|
||||
|
||||
def check_code(self, data):
|
||||
"""Check the given code against the available codes list."""
|
||||
path = os.path.join(CODES, data)
|
||||
if os.path.exists(path):
|
||||
contents = open(path).read()
|
||||
os.remove(path)
|
||||
return True, contents
|
||||
return False, ""
|
||||
|
||||
if __name__ == "__main__":
|
||||
# do the UNIX double-fork magic, see Stevens' "Advanced
|
||||
# Programming in the UNIX Environment" for details (ISBN 0201563177)
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# exit first parent
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
|
||||
sys.exit(1)
|
||||
|
||||
# decouple from parent environment
|
||||
os.chdir("/") #don't prevent unmounting....
|
||||
os.setsid()
|
||||
os.umask(0)
|
||||
|
||||
# do second fork
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# exit from second parent, print eventual PID before
|
||||
#print "Daemon PID %d" % pid
|
||||
open(PIDFILE,'w').write("%d"%pid)
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
|
||||
sys.exit(1)
|
||||
|
||||
# start the daemon main loop
|
||||
runme()
|
Loading…
Add table
Add a link
Reference in a new issue