Code

383754be7cae810b6a6e74e20b1329190c771ea4
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL THE BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18
19 # $Id: roundup.cgi,v 1.9 2001-08-07 00:15:51 richard Exp $
21 # python version check
22 import sys
23 if int(sys.version[0]) < 2:
24     print "Content-Type: text/plain\n"
25     print "Roundup requires Python 2.0 or newer."
26     sys.exit(0)
28 #
29 ##  Configuration
30 #
32 # This indicates where the Roundup instance lives
33 ROUNDUP_INSTANCE_HOMES = {
34     'test': '/tmp/roundup_test',
35 }
37 # Where to log debugging information to. Use an instance of DevNull if you
38 # don't want to log anywhere.
39 class DevNull:
40     def write(self, info):
41         pass
42 LOG = open('/var/log/roundup.cgi.log', 'a')
43 #LOG = DevNull()
45 #
46 ##  end configuration
47 #
50 #
51 # Set up the error handler
52
53 try:
54     import traceback, StringIO, cgi
55     from roundup import cgitb
56 except:
57     print "Content-Type: text/html\n"
58     print "Failed to import cgitb.<pre>"
59     s = StringIO.StringIO()
60     traceback.print_exc(None, s)
61     print cgi.escape(s.getvalue()), "</pre>"
63 def main(instance, out):
64     from roundup import cgi_client
65     db = instance.open('admin')
66     auth = os.environ.get("HTTP_CGI_AUTHORIZATION", None)
67     message = 'Unauthorised'
68     if auth:
69         import binascii
70         l = binascii.a2b_base64(auth.split(' ')[1]).split(':')
71         user = l[0]
72         password = None
73         if len(l) > 1:
74             password = l[1]
75         try:
76             uid = db.user.lookup(user)
77         except KeyError:
78             auth = None
79             message = 'Username not recognised'
80         else:
81             if password != db.user.get(uid, 'password'):
82                 message = 'Incorrect password'
83                 auth = None
84     if not auth:
85         out.write('Content-Type: text/html\n')
86         out.write('Status: 401\n')
87         out.write('WWW-Authenticate: basic realm="Roundup"\n\n')
88         keys = os.environ.keys()
89         keys.sort()
90         out.write(message)
91         return
92     client = instance.Client(out, db, os.environ, user)
93     try:
94         client.main()
95     except cgi_client.Unauthorised:
96         out.write('Content-Type: text/html\n')
97         out.write('Status: 403\n\n')
98         out.write('Unauthorised')
101 # Now do the actual CGI handling
102
103 out, err = sys.stdout, sys.stderr
104 try:
105     sys.stdout = sys.stderr = LOG
106     import os, string
107     import roundup.instance
108     path = string.split(os.environ['PATH_INFO'], '/')
109     instance = path[1]
110     os.environ['PATH_INFO'] = string.join(path[2:], '/')
111     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
112         instance_home = ROUNDUP_INSTANCE_HOMES[instance]
113         instance = roundup.instance.open(instance_home)
114     else:
115         raise ValueError, 'No such instance "%s"'%instance
116     main(instance, out)
117 except:
118     sys.stdout, sys.stderr = out, err
119     out.write('Content-Type: text/html\n\n')
120     cgitb.handler()
121 sys.stdout.flush()
122 sys.stdout, sys.stderr = out, err
125 # $Log: not supported by cvs2svn $
126 # Revision 1.8  2001/08/05 07:43:52  richard
127 # Instances are now opened by a special function that generates a unique
128 # module name for the instances on import time.
130 # Revision 1.7  2001/08/03 01:28:33  richard
131 # Used the much nicer load_package, pointed out by Steve Majewski.
133 # Revision 1.6  2001/08/03 00:59:34  richard
134 # Instance import now imports the instance using imp.load_module so that
135 # we can have instance homes of "roundup" or other existing python package
136 # names.
138 # Revision 1.5  2001/07/29 07:01:39  richard
139 # Added vim command to all source so that we don't get no steenkin' tabs :)
141 # Revision 1.4  2001/07/23 04:47:27  anthonybaxter
142 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
143 # sys.exit(0) if python version wrong.
145 # Revision 1.3  2001/07/23 04:33:30  richard
146 # brought the CGI instance config dict in line with roundup-server
148 # Revision 1.2  2001/07/23 04:31:40  richard
149 # Fixed the roundup CGI script for updates to cgi_client.py
151 # Revision 1.1  2001/07/22 11:47:07  richard
152 # More Grande Splite
155 # vim: set filetype=python ts=4 sw=4 et si