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 }}"
26 monitor_rewrite = "{{ ' '.join(rewrite_element.keys()) }}"
31 def crypt(word, salt="$$"):
32 salt = salt.split("$")
33 algo = salt[0] or 'sha1'
34 if algo in hashlib.algorithms:
35 H = getattr(hashlib, algo)
37 return "%s$%s" % (algo, word)
40 rounds = min(max(0, int(salt[1])), 30) if salt[1] else 9
41 salt = salt[2] or base64.b64encode(os.urandom(12), "./")
42 h = hmac.new(salt, word, H).digest()
43 for x in xrange(1, 1 << rounds):
45 return "%s$%s$%s$%s" % (algo, rounds, salt,
46 base64.b64encode(h, "./").rstrip("="))
48 def is_password_set():
49 if not os.path.exists(monitor_password_path):
51 hashed_password = open(monitor_password_path, 'r').read()
53 void, algo, salt, hsh = hashed_password.split('$')
58 def set_password(raw_password):
59 hashed_password = crypt(raw_password)
60 subprocess.check_call(monitor_apache_password_command + " %s" % raw_password,
62 open(monitor_password_path, 'w').write(hashed_password)
65 def check_password(raw_password):
67 Returns a boolean of whether the raw_password was correct. Handles
68 encryption formats behind the scenes.
70 if not os.path.exists(monitor_password_path) or not raw_password:
72 hashed_password = open(monitor_password_path, 'r').read()
73 return hashed_password == crypt(raw_password, hashed_password)
74 ### End of password functions
77 command = os.path.join(cgi_path, form['posting-script'].value)
80 params_dict[f] = form[f].value
81 del params_dict['posting-script']
82 os.environ['QUERY_STRING'] = urllib.urlencode(params_dict)
84 if os.access(command, os.X_OK):
85 print '\n', subprocess.check_output([command])
86 except subprocess.CalledProcessError:
87 print "There is a problem with sub-process"
91 def return_document(command=None):
93 script = form['script'].value
94 command = os.path.join(cgi_path, script)
95 #XXX this functions should be called only for display,
96 #so a priori it doesn't need form data
97 os.environ['QUERY_STRING'] = ''
99 if os.access(command, os.X_OK):
100 print '\n', subprocess.check_output([command])
101 elif os.access(command, os.R_OK):
102 print open(command).read()
105 except (subprocess.CalledProcessError, OSError) as e:
106 print "<p>Error :</p><pre>%s</pre>" % e
110 # Transform deep-2 tree in json
112 for folder in os.listdir(cgi_path):
113 if os.path.isdir(os.path.join(cgi_path, folder)):
114 folder_list[folder] = []
115 for folder in folder_list:
116 for file in os.listdir(os.path.join(cgi_path, folder)):
117 if os.path.isfile(os.path.join(cgi_path, folder, file)):
118 folder_list[folder].append(file)
122 def get_cookie_password():
123 cookie_string = os.environ.get('HTTP_COOKIE')
125 cookie.load(cookie_string)
127 return cookie['password'].value
132 def set_cookie_password(password):
133 cookie['password'] = password
134 print cookie, "; Path=/; HttpOnly"
137 # Beginning of response
138 print "Content-Type: text/html"
142 # Check if user is logged
143 if "password_2" in form and "password" in form:
144 password_2 = form['password_2'].value
145 password_1 = form['password'].value
146 password = get_cookie_password()
147 if not is_password_set() or check_password(password):
148 if password_2 == password_1:
149 password = password_1
150 set_password(password)
151 set_cookie_password(password)
152 elif "password" in form:
153 password = form['password'].value
154 if is_password_set() and check_password(password):
155 set_cookie_password(password)
157 password = get_cookie_password()
161 if not is_password_set():
162 return_document(monitor_password_script_path)
163 elif not check_password(password):
166 <link rel="stylesheet" href="static/pure-min.css">
167 <link rel="stylesheet" href="static/style.css">"""
168 print "</head><body>"
170 print "<h1>This is the monitoring interface</h1>"
172 print "<h1>Error</h1><p>Wrong password</p>"
174 <p>Please enter the monitor_password in the next field to access the data</p>
175 <form action="/index.cgi" method="post" class="pure-form-aligned">
176 Password : <input type="password" name="password">
177 <button type="submit" class="pure-button pure-button-primary">Access</button>
180 # redirection to the required script/page
183 if "posting-script" in form:
185 elif "script" in form:
188 html_base = jinja2.Template(open('{{ index_template }}').read())
190 print html_base.render(tree=make_menu(), default_page="{{ default_page }}", monitor_rewrite=monitor_rewrite)