Code

e55dfbcd44143ce0e645e13d08f950dd5144c816
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
2 # $Id: roundup.cgi,v 1.5 2001-07-29 07:01:39 richard Exp $
4 # python version check
5 import sys
6 if int(sys.version[0]) < 2:
7     print "Content-Type: text/plain\n"
8     print "Roundup requires Python 2.0 or newer."
9     sys.exit(0)
11 #
12 ##  Configuration
13 #
15 # This indicates where the Roundup instance lives
16 ROUNDUP_INSTANCE_HOMES = {
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 ROUNDUP_INSTANCE_HOMES.has_key(instance):
94         instance_home = ROUNDUP_INSTANCE_HOMES[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
112 # $Log: not supported by cvs2svn $
113 # Revision 1.4  2001/07/23 04:47:27  anthonybaxter
114 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
115 # sys.exit(0) if python version wrong.
117 # Revision 1.3  2001/07/23 04:33:30  richard
118 # brought the CGI instance config dict in line with roundup-server
120 # Revision 1.2  2001/07/23 04:31:40  richard
121 # Fixed the roundup CGI script for updates to cgi_client.py
123 # Revision 1.1  2001/07/22 11:47:07  richard
124 # More Grande Splite
127 # vim: set filetype=python ts=4 sw=4 et si