1 #!{{ extra_eggs_interpreter }}
14 cgitb.enable(display=0, logdir="/tmp/cgi.log")
16 form = cgi.FieldStorage()
17 cookie = Cookie.SimpleCookie()
19 cgi_path = "{{ cgi_directory }}"
21 monitor_password_path = "{{ monitor_password_path }}"
22 monitor_password_script_path = "{{ monitor_password_script_path }}"
24 monitor_apache_password_command = "{{ apache_update_command }}"
29 def crypt(word, salt="$$"):
30 salt = salt.split("$")
31 algo = salt[0] or 'sha1'
32 if algo in hashlib.algorithms:
33 H = getattr(hashlib, algo)
35 return "%s$%s" % (algo, word)
38 rounds = min(max(0, int(salt[1])), 30) if salt[1] else 9
39 salt = salt[2] or base64.b64encode(os.urandom(12), "./")
40 h = hmac.new(salt, word, H).digest()
41 for x in xrange(1, 1 << rounds):
43 return "%s$%s$%s$%s" % (algo, rounds, salt,
44 base64.b64encode(h, "./").rstrip("="))
46 def is_password_set():
47 if not os.path.exists(monitor_password_path):
49 hashed_password = open(monitor_password_path, 'r').read()
51 void, algo, salt, hsh = hashed_password.split('$')
56 def set_password(raw_password):
57 hashed_password = crypt(raw_password)
58 subprocess.check_call(monitor_apache_password_command + " %s" % raw_password,
60 open(monitor_password_path, 'w').write(hashed_password)
63 def check_password(raw_password):
65 Returns a boolean of whether the raw_password was correct. Handles
66 encryption formats behind the scenes.
68 if not os.path.exists(monitor_password_path) or not raw_password:
70 hashed_password = open(monitor_password_path, 'r').read()
71 return hashed_password == crypt(raw_password, hashed_password)
72 ### End of password functions
75 command = os.path.join(cgi_path, form['posting-script'].value)
78 params_dict[f] = form[f].value
79 del params_dict['posting-script']
80 os.environ['QUERY_STRING'] = urllib.urlencode(params_dict)
82 if os.access(command, os.X_OK):
83 print '\n', subprocess.check_output([command])
84 except subprocess.CalledProcessError:
85 print "There is a problem with sub-process"
89 def return_document(command=None):
91 script = form['script'].value
92 command = os.path.join(cgi_path, script)
93 #XXX this functions should be called only for display,
94 #so a priori it doesn't need form data
95 os.environ['QUERY_STRING'] = ''
97 if os.access(command, os.X_OK):
98 print '\n', subprocess.check_output([command])
99 elif os.access(command, os.R_OK):
100 print open(command).read()
103 except (subprocess.CalledProcessError, OSError) as e:
104 print "<p>Error :</p><pre>%s</pre>" % e
108 # Transform deep-2 tree in json
110 for folder in os.listdir(cgi_path):
111 if os.path.isdir(os.path.join(cgi_path, folder)):
112 folder_list[folder] = []
113 for folder in folder_list:
114 for file in os.listdir(os.path.join(cgi_path, folder)):
115 if os.path.isfile(os.path.join(cgi_path, folder, file)):
116 folder_list[folder].append(file)
120 def get_cookie_password():
121 cookie_string = os.environ.get('HTTP_COOKIE')
123 cookie.load(cookie_string)
125 return cookie['password'].value
130 def set_cookie_password(password):
131 cookie['password'] = password
132 print cookie, "; Path=/; HttpOnly"
135 # Beginning of response
136 print "Content-Type: text/html"
140 # Check if user is logged
141 if "password_2" in form and "password" in form:
142 password_2 = form['password_2'].value
143 password_1 = form['password'].value
144 password = get_cookie_password()
145 if not is_password_set() or check_password(password):
146 if password_2 == password_1:
147 password = password_1
148 set_password(password)
149 set_cookie_password(password)
150 elif "password" in form:
151 password = form['password'].value
152 if is_password_set() and check_password(password):
153 set_cookie_password(password)
155 password = get_cookie_password()
159 if not is_password_set():
160 return_document(monitor_password_script_path)
161 elif not check_password(password):
164 <link rel="stylesheet" href="pure-min.css">
165 <link rel="stylesheet" href="/style.css">"""
166 print "</head><body>"
168 print "<h1>This is the monitoring interface</h1>"
170 print "<h1>Error</h1><p>Wrong password</p>"
172 <p>Please enter the monitor_password in the next field to access the data</p>
173 <form action="/index.cgi" method="post" class="pure-form-aligned">
174 Password : <input type="password" name="password">
175 <button type="submit" class="pure-button pure-button-primary">Access</button>
178 # redirection to the required script/page
181 if "posting-script" in form:
183 elif "script" in form:
186 html_base = jinja2.Template(open('{{ index_template }}').read())
188 print html_base.render(tree=make_menu(), default_page="{{ default_page }}")