Code

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