Code

Again still PoC, but reworked w/o CherryPy
[nagixsc.git] / nagixsc_conf2http.py
1 #!/usr/bin/python
3 import BaseHTTPServer
4 import base64
5 import md5
6 import os
7 import re
8 import subprocess
10 config = {      'ip':                   '',
11                         'port':                 15666,
12                 }
14 users = {       'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
15                 }
17 CONFDIR='./examples'
18 C2X='./nagixsc_conf2xml.py'
20 class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
22         def http_error(code, output):
23                 self.send_response(code)
24                 self.send_header('Content-Type', 'text/plain')
25                 self.end_headers()
26                 self.wfile.write(output)
27                 return
30         def do_GET(self):
31                 cmdline = C2X
33                 path = self.path.split('/')
35                 # Check Basic Auth
36                 try:
37                         authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
38                         if not users[authdata[0]] == md5.md5(authdata[1]).hexdigest():
39                                 raise Exception
40                 except:
41                         self.send_response(401)
42                         self.send_header('WWW-Authenticate', 'Basic realm="Nag(ix)SC Pull"')
43                         self.send_header('Content-Type', 'text/plain')
44                         self.end_headers()
45                         self.wfile.write('Sorry! No action without login!')
46                         return
49                 if len(path) >= 4:
50                         service = path[3]
51                 else:
52                         service = ''
54                 if len(path) >= 3:
55                         host = path[2]
56                 else:
57                         host = ''
59                 if len(path) >= 2:
60                         configfile = path[1]
61                 else:
62                         configfile =''
64                 if re.search('\.\.', configfile):
65                         http_error(500, 'Found ".." in config file name')
66                         return
67                 if configfile and not re.search('^[a-zA-Z0-9-_\.]+$', configfile):
68                         http_error(500, 'Config file name contains invalid characters')
69                         return
71                 if configfile:
72                         configfile += '.conf'
73                         cmdline    += ' -c ' + os.path.join(CONFDIR, configfile)
75                 if host:
76                         cmdline += ' -H %s' % host
77                         if service:
78                                 cmdline += ' -D %s' % service
80                 try:
81                         cmd     = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
82                         output  = cmd.communicate()[0].rstrip()
83                         retcode = cmd.returncode
84                 except OSError:
85                         http_error(500, 'Could not execute "%s"' % cmdline)
86                         return
88                 if retcode == 0:
89                         self.send_response(200)
90                         self.send_header('Content-Type', 'text/xml')
91                         self.end_headers()
92                         self.wfile.write(output)
93                 else:
94                         http_error(500, output)
96                 return
100 def main():
101         try:
102                 server = BaseHTTPServer.HTTPServer((config['ip'], config['port']), Conf2HTTPHandler)
103                 server.serve_forever()
104         except:
105                 server.socket.close()
107 if __name__ == '__main__':
108         main()