532e971f322994fd92a535d7ac6a48772215bcd5
1 #!/usr/bin/env python
3 # $Id: roundup.cgi,v 1.3 2001-07-23 04:33:30 richard Exp $
5 # python version check
6 import sys
7 if int(sys.version[0]) < 2:
8 print "Content-Type: text/plain\n"
9 print "Roundup requires Python 2.0 or newer."
11 #
12 ## Configuration
13 #
15 # This indicates where the Roundup instance lives
16 ROUNDUPS = {
17 'test': '/tmp/roundup_test',
18 }
20 # Where to log debugging information to. Use an instance of DevNull if you
21 # don't want to log anywhere.
22 class DevNull:
23 def write(self, info):
24 pass
25 LOG = open('/var/log/roundup.cgi.log', 'a')
26 #LOG = DevNull()
28 #
29 ## end configuration
30 #
33 #
34 # Set up the error handler
35 #
36 try:
37 import traceback, StringIO, cgi
38 from roundup import cgitb
39 except:
40 print "Content-Type: text/html\n"
41 print "Failed to import cgitb.<pre>"
42 s = StringIO.StringIO()
43 traceback.print_exc(None, s)
44 print cgi.escape(s.getvalue()), "</pre>"
46 def main(instance, out):
47 from roundup import cgi_client
48 db = instance.open('admin')
49 auth = os.environ.get("HTTP_CGI_AUTHORIZATION", None)
50 message = 'Unauthorised'
51 if auth:
52 import binascii
53 l = binascii.a2b_base64(auth.split(' ')[1]).split(':')
54 user = l[0]
55 password = None
56 if len(l) > 1:
57 password = l[1]
58 try:
59 uid = db.user.lookup(user)
60 except KeyError:
61 auth = None
62 message = 'Username not recognised'
63 else:
64 if password != db.user.get(uid, 'password'):
65 message = 'Incorrect password'
66 auth = None
67 if not auth:
68 out.write('Content-Type: text/html\n')
69 out.write('Status: 401\n')
70 out.write('WWW-Authenticate: basic realm="Roundup"\n\n')
71 keys = os.environ.keys()
72 keys.sort()
73 out.write(message)
74 return
75 client = instance.Client(out, db, os.environ, user)
76 try:
77 client.main()
78 except cgi_client.Unauthorised:
79 out.write('Content-Type: text/html\n')
80 out.write('Status: 403\n\n')
81 out.write('Unauthorised')
83 #
84 # Now do the actual CGI handling
85 #
86 out, err = sys.stdout, sys.stderr
87 try:
88 sys.stdout = sys.stderr = LOG
89 import os, string
90 path = string.split(os.environ['PATH_INFO'], '/')
91 instance = path[1]
92 os.environ['PATH_INFO'] = string.join(path[2:], '/')
93 if ROUNDUPS.has_key(instance):
94 instance_home = ROUNDUPS[instance]
95 module_path, instance = os.path.split(instance_home)
96 sys.path.insert(0, module_path)
97 try:
98 instance = __import__(instance)
99 finally:
100 del sys.path[0]
101 else:
102 raise ValueError, 'No such instance "%s"'%instance
103 main(instance, out)
104 except:
105 sys.stdout, sys.stderr = out, err
106 out.write('Content-Type: text/html\n\n')
107 cgitb.handler()
108 sys.stdout.flush()
109 sys.stdout, sys.stderr = out, err
111 #
112 # $Log: not supported by cvs2svn $
113 # Revision 1.2 2001/07/23 04:31:40 richard
114 # Fixed the roundup CGI script for updates to cgi_client.py
115 #
116 # Revision 1.1 2001/07/22 11:47:07 richard
117 # More Grande Splite
118 #
119 #