1 #!/usr/bin/python
3 import BaseHTTPServer
4 import ConfigParser
5 import base64
6 import optparse
7 import os
8 import re
9 import subprocess
10 import sys
12 try:
13 from hashlib import md5
14 except ImportError:
15 from md5 import md5
17 ##############################################################################
19 parser = optparse.OptionParser()
21 parser.add_option('-c', '', dest='cfgfile', help='Config file')
23 parser.set_defaults(cfgfile='conf2http.cfg')
25 (options, args) = parser.parse_args()
27 cfgread = ConfigParser.SafeConfigParser()
28 cfgread.optionxform = str # We need case-sensitive options
29 cfg_list = cfgread.read(options.cfgfile)
31 if cfg_list == []:
32 print 'Config file "%s" could not be read!' % options.cfgfile
33 sys.exit(1)
35 config = {}
36 try:
37 config['ip'] = cfgread.get('server', 'ip')
38 config['port'] = cfgread.getint('server', 'port')
40 config['conf_dir'] = cfgread.get('server', 'conf_dir')
41 config['conf2xml_cmdline'] = cfgread.get('server', 'conf2xml_cmdline')
43 except ConfigParser.NoOptionError, e:
44 print 'Config file error: %s ' % e
45 sys.exit(1)
47 users = {}
48 for u in cfgread.options('users'):
49 users[u] = cfgread.get('users', u)
51 ##############################################################################
53 class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
55 def http_error(self, code, output):
56 self.send_response(code)
57 self.send_header('Content-Type', 'text/plain')
58 self.end_headers()
59 self.wfile.write(output)
60 return
63 def do_GET(self):
64 cmdline = config['conf2xml_cmdline']
66 path = self.path.split('/')
68 # Check Basic Auth
69 try:
70 authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
71 if not users[authdata[0]] == md5(authdata[1]).hexdigest():
72 raise Exception
73 except:
74 self.send_response(401)
75 self.send_header('WWW-Authenticate', 'Basic realm="Nag(ix)SC Pull"')
76 self.send_header('Content-Type', 'text/plain')
77 self.end_headers()
78 self.wfile.write('Sorry! No action without login!')
79 return
82 if len(path) >= 4:
83 service = path[3]
84 else:
85 service = ''
87 if len(path) >= 3:
88 host = path[2]
89 else:
90 host = ''
92 if len(path) >= 2:
93 configfile = path[1]
94 else:
95 configfile =''
97 if re.search('\.\.', configfile):
98 self.http_error(500, 'Found ".." in config file name')
99 return
100 if configfile and not re.search('^[a-zA-Z0-9-_\.]+$', configfile):
101 self.http_error(500, 'Config file name contains invalid characters')
102 return
104 if configfile:
105 configfile += '.conf'
106 cmdline += ' -c ' + os.path.join(config['conf_dir'], configfile)
108 if host:
109 cmdline += ' -H %s' % host
110 if service:
111 cmdline += ' -D %s' % service
113 try:
114 cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
115 output = cmd.communicate()[0].rstrip()
116 retcode = cmd.returncode
117 except OSError:
118 self.http_error(500, 'Could not execute "%s"' % cmdline)
119 return
121 if retcode == 0:
122 self.send_response(200)
123 self.send_header('Content-Type', 'text/xml')
124 self.end_headers()
125 self.wfile.write(output)
126 else:
127 self.http_error(500, output)
129 return
133 def main():
134 try:
135 server = BaseHTTPServer.HTTPServer((config['ip'], config['port']), Conf2HTTPHandler)
136 server.serve_forever()
137 except:
138 server.socket.close()
140 if __name__ == '__main__':
141 main()