|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# $Id: roundup.cgi,v 1.1 2001-07-22 11:47:07 richard Exp $ |
| 4 | + |
| 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 | + |
| 11 | +# |
| 12 | +## Configuration |
| 13 | +# |
| 14 | + |
| 15 | +# This indicates where the Roundup instance lives |
| 16 | +ROUNDUPS = { |
| 17 | + 'test': '/tmp/roundup_test', |
| 18 | +} |
| 19 | + |
| 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() |
| 27 | + |
| 28 | +# |
| 29 | +## end configuration |
| 30 | +# |
| 31 | + |
| 32 | + |
| 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>" |
| 45 | + |
| 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 | + l = binascii.a2b_base64(auth.split(' ')[1]).split(':') |
| 53 | + user = l[0] |
| 54 | + password = None |
| 55 | + if len(l) > 1: |
| 56 | + password = l[1] |
| 57 | + try: |
| 58 | + uid = db.user.lookup(user) |
| 59 | + except KeyError: |
| 60 | + auth = None |
| 61 | + message = 'Username not recognised' |
| 62 | + else: |
| 63 | + if password != db.user.get(uid, 'password'): |
| 64 | + message = 'Incorrect password' |
| 65 | + auth = None |
| 66 | + if not auth: |
| 67 | + out.write('Content-Type: text/html\n') |
| 68 | + out.write('Status: 401\n') |
| 69 | + out.write('WWW-Authenticate: basic realm="Roundup"\n\n') |
| 70 | + keys = os.environ.keys() |
| 71 | + keys.sort() |
| 72 | + out.write(message) |
| 73 | + return |
| 74 | + client = instance.Client(out, os.environ, user) |
| 75 | + try: |
| 76 | + client.main() |
| 77 | + except cgi_client.Unauthorised: |
| 78 | + out.write('Content-Type: text/html\n') |
| 79 | + out.write('Status: 403\n\n') |
| 80 | + out.write('Unauthorised') |
| 81 | + |
| 82 | +# |
| 83 | +# Now do the actual CGI handling |
| 84 | +# |
| 85 | +out, err = sys.stdout, sys.stderr |
| 86 | +try: |
| 87 | + sys.stdout = sys.stderr = LOG |
| 88 | + import os, string |
| 89 | + instance = string.split(os.environ['PATH_INFO'], '/')[1] |
| 90 | + if ROUNDUPS.has_key(instance): |
| 91 | + instance_home = ROUNDUPS[instance] |
| 92 | + sys.path.insert(0, instance_home) |
| 93 | + try: |
| 94 | + instance = __import__(instance) |
| 95 | + finally: |
| 96 | + del sys.path[0] |
| 97 | + else: |
| 98 | + raise ValueError, 'No such instance "%s"'%instance |
| 99 | + main(instance, out) |
| 100 | +except: |
| 101 | + sys.stdout, sys.stderr = out, err |
| 102 | + out.write('Content-Type: text/html\n\n') |
| 103 | + cgitb.handler() |
| 104 | +sys.stdout.flush() |
| 105 | +sys.stdout, sys.stderr = out, err |
| 106 | + |
| 107 | +# |
| 108 | +# $Log: not supported by cvs2svn $ |
| 109 | +# |
0 commit comments