1 #!/usr/bin/python
3 import BaseHTTPServer
4 import base64
5 import os
6 import re
7 import subprocess
9 try:
10 from hashlib import md5
11 except ImportError:
12 from md5 import md5
14 config = { 'ip': '',
15 'port': 15666,
16 }
18 users = { 'nagixsc': '019b0966d98fb71d1a4bc4ca0c81d5cc', # PW: nagixsc
19 }
21 CONFDIR='./examples'
22 C2X='./nagixsc_conf2xml.py'
24 class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
26 def http_error(self, code, output):
27 self.send_response(code)
28 self.send_header('Content-Type', 'text/plain')
29 self.end_headers()
30 self.wfile.write(output)
31 return
34 def do_GET(self):
35 cmdline = C2X
37 path = self.path.split('/')
39 # Check Basic Auth
40 try:
41 authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
42 if not users[authdata[0]] == md5(authdata[1]).hexdigest():
43 raise Exception
44 except:
45 self.send_response(401)
46 self.send_header('WWW-Authenticate', 'Basic realm="Nag(ix)SC Pull"')
47 self.send_header('Content-Type', 'text/plain')
48 self.end_headers()
49 self.wfile.write('Sorry! No action without login!')
50 return
53 if len(path) >= 4:
54 service = path[3]
55 else:
56 service = ''
58 if len(path) >= 3:
59 host = path[2]
60 else:
61 host = ''
63 if len(path) >= 2:
64 configfile = path[1]
65 else:
66 configfile =''
68 if re.search('\.\.', configfile):
69 self.http_error(500, 'Found ".." in config file name')
70 return
71 if configfile and not re.search('^[a-zA-Z0-9-_\.]+$', configfile):
72 self.http_error(500, 'Config file name contains invalid characters')
73 return
75 if configfile:
76 configfile += '.conf'
77 cmdline += ' -c ' + os.path.join(CONFDIR, configfile)
79 if host:
80 cmdline += ' -H %s' % host
81 if service:
82 cmdline += ' -D %s' % service
84 try:
85 cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
86 output = cmd.communicate()[0].rstrip()
87 retcode = cmd.returncode
88 except OSError:
89 self.http_error(500, 'Could not execute "%s"' % cmdline)
90 return
92 if retcode == 0:
93 self.send_response(200)
94 self.send_header('Content-Type', 'text/xml')
95 self.end_headers()
96 self.wfile.write(output)
97 else:
98 self.http_error(500, output)
100 return
104 def main():
105 try:
106 server = BaseHTTPServer.HTTPServer((config['ip'], config['port']), Conf2HTTPHandler)
107 server.serve_forever()
108 except:
109 server.socket.close()
111 if __name__ == '__main__':
112 main()