settings.cgi.in 3.3 KB
Newer Older
1 2
#!{{ python_executable }}

3
import os
4 5 6
import cgi
import cgitb
import ConfigParser
7
import Cookie
8 9 10

cgitb.enable(display=0, logdir="/tmp/cgi.log")

11 12 13
form = cgi.FieldStorage()                                                                                
cookie = Cookie.SimpleCookie()                                                                           

14 15
print "Content-Type: text/html"

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
if "password" in form:                                                                                   
  password = form['password'].value                                                                      
  if password == '{{ password }}' :                                                                     
    cookie['password'] = password                                                                        
    print cookie
else:                                                                                                    
  cookie_string = os.environ.get('HTTP_COOKIE')                                                          
  if cookie_string:
    cookie.load(cookie_string)                                                                           
    try:
      password = cookie['password'].value                                                                
    except KeyError: 
      password = None                                                                                    
  else: 
    password = None                                                                                      

print 
33

34 35 36 37 38 39 40 41 42 43 44 45
if not password or password != '{{ password }}':
  print "<html><body>"
  if password is None:
    print "<h1>This is the monitoring interface</h1>"
  else:
    print "<h1>Error</h1><p>Wrong password</p>"
  print """
  <p>Please enter the monitor_password in the next field to access the data</p>                          
  <form action="/settings.cgi" method="post">                                                            
    Password : <input type="password" name="password">                                                   
    <input type="submit" value="Access">                                                                 
  </form></body></html>"""
46 47
  
else:
48 49 50 51 52
  config_file = "{{ config_cfg }}"
  if not os.path.exists(config_file):
    print "Your software does <b>not</b> embed 0-knowledge. \
    This interface is useless in this case"
    exit(0)
53
  parser = ConfigParser.ConfigParser()
54
  parser.read(config_file)
55 56 57 58 59 60 61
  if len(form) > 1:
    for name in form:
      if name != 'password':
        parser.set('public', name, form[name].value)
    with open("{{ config_cfg }}", 'w') as file:
      parser.write(file)
  print "<html><body>"
62
  print "<h1>Values that can be defined by the user :</h1>"
63
  print "<form action=\"/{{ this_filename }}\" method=\"post\">"
64 65 66 67 68 69
  print "<input type=\"hidden\" name=\"password\" value=\"{{ password }}\">"
  for option in parser.options("public"):
    print "%s : <input type=\"text\" name=\"%s\" \
    value=\"%s\"><br>"% (option, option, parser.get('public', option))
  print "<input type=\"submit\" value=\"Save\"></form>"
  
70
  print "<h1>Other values :</h1>"
71 72 73 74 75 76
  for section in parser.sections():
    if section != 'public':
      for option in parser.options(section):
        print "<b>%s</b> : value=\"%s\"<br>" % (option,
        parser.get(section, option))
  
77
  print "</body></html>"