Code

convenience cutnpaste for redhat users
[roundup.git] / roundup-server
1 #!/usr/bin/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 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 """ HTTP Server that serves roundup.
21 $Id: roundup-server,v 1.25 2002-01-05 02:21:21 richard Exp $
22 """
24 # python version check
25 from roundup import version_check
27 import sys, os, urllib, StringIO, traceback, cgi, binascii, getopt, imp
28 import BaseHTTPServer
30 # Roundup modules of use here
31 from roundup import cgitb, cgi_client
32 import roundup.instance
33 from roundup.i18n import _
35 #
36 ##  Configuration
37 #
39 # This indicates where the Roundup instance lives
40 ROUNDUP_INSTANCE_HOMES = {
41     'bar': '/tmp/bar',
42 }
44 ROUNDUP_USER = None
47 # Where to log debugging information to. Use an instance of DevNull if you
48 # don't want to log anywhere.
49 # TODO: actually use this stuff
50 #class DevNull:
51 #    def write(self, info):
52 #        pass
53 #LOG = open('/var/log/roundup.cgi.log', 'a')
54 #LOG = DevNull()
56 #
57 ##  end configuration
58 #
61 class RoundupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
62     ROUNDUP_INSTANCE_HOMES = ROUNDUP_INSTANCE_HOMES
63     ROUNDUP_USER = ROUNDUP_USER
65     def run_cgi(self):
66         """ Execute the CGI command. Wrap an innner call in an error
67             handler so all errors can be caught.
68         """
69         save_stdin = sys.stdin
70         sys.stdin = self.rfile
71         try:
72             self.inner_run_cgi()
73         except cgi_client.NotFound:
74             self.send_error(404, self.path)
75         except cgi_client.Unauthorised:
76             self.send_error(403, self.path)
77         except:
78             # it'd be nice to be able to detect if these are going to have
79             # any effect...
80             self.send_response(400)
81             self.send_header('Content-Type', 'text/html')
82             self.end_headers()
83             try:
84                 reload(cgitb)
85                 self.wfile.write(cgitb.breaker())
86                 self.wfile.write(cgitb.html())
87             except:
88                 self.wfile.write("<pre>")
89                 s = StringIO.StringIO()
90                 traceback.print_exc(None, s)
91                 self.wfile.write(cgi.escape(s.getvalue()))
92                 self.wfile.write("</pre>\n")
93         sys.stdin = save_stdin
95     do_GET = do_POST = do_HEAD = send_head = run_cgi
97     def index(self):
98         ''' Print up an index of the available instances
99         '''
100         self.send_response(200)
101         self.send_header('Content-Type', 'text/html')
102         self.end_headers()
103         w = self.wfile.write
104         w(_('<html><head><title>Roundup instances index</title></head>\n'))
105         w(_('<body><h1>Roundup instances index</h1><ol>\n'))
106         for instance in self.ROUNDUP_INSTANCE_HOMES.keys():
107             w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
108                 'instance_url': urllib.quote(instance),
109                 'instance_name': cgi.escape(instance)})
110         w(_('</ol></body></html>'))
112     def inner_run_cgi(self):
113         ''' This is the inner part of the CGI handling
114         '''
116         rest = self.path
117         i = rest.rfind('?')
118         if i >= 0:
119             rest, query = rest[:i], rest[i+1:]
120         else:
121             query = ''
123         # figure the instance
124         if rest == '/':
125             return self.index()
126         l_path = rest.split('/')
127         instance_name = urllib.unquote(l_path[1])
128         if self.ROUNDUP_INSTANCE_HOMES.has_key(instance_name):
129             instance_home = self.ROUNDUP_INSTANCE_HOMES[instance_name]
130             instance = roundup.instance.open(instance_home)
131         else:
132             raise cgi_client.NotFound
134         # figure out what the rest of the path is
135         if len(l_path) > 2:
136             rest = '/'.join(l_path[2:])
137         else:
138             rest = '/'
140         # Set up the CGI environment
141         env = {}
142         env['INSTANCE_NAME'] = instance_name
143         env['REQUEST_METHOD'] = self.command
144         env['PATH_INFO'] = urllib.unquote(rest)
145         if query:
146             env['QUERY_STRING'] = query
147         host = self.address_string()
148         if self.headers.typeheader is None:
149             env['CONTENT_TYPE'] = self.headers.type
150         else:
151             env['CONTENT_TYPE'] = self.headers.typeheader
152         length = self.headers.getheader('content-length')
153         if length:
154             env['CONTENT_LENGTH'] = length
155         co = filter(None, self.headers.getheaders('cookie'))
156         if co:
157             env['HTTP_COOKIE'] = ', '.join(co)
158         env['SCRIPT_NAME'] = ''
159         env['SERVER_NAME'] = self.server.server_name
160         env['SERVER_PORT'] = str(self.server.server_port)
162         decoded_query = query.replace('+', ' ')
164         # do the roundup thang
165         client = instance.Client(instance, self, env)
166         client.main()
168 def usage(message=''):
169     if message:
170         message = _('Error: %(error)s\n\n')%{'error': message}
171     print _('''%(message)sUsage:
172 roundup-server [-n hostname] [-p port] [name=instance home]*
174  -n: sets the host name
175  -p: sets the port to listen on
177  name=instance home
178    Sets the instance home(s) to use. The name is how the instance is
179    identified in the URL (it's the first part of the URL path). The
180    instance home is the directory that was identified when you did
181    "roundup-admin init". You may specify any number of these name=home
182    pairs on the command-line. For convenience, you may edit the
183    ROUNDUP_INSTANCE_HOMES variable in the roundup-server file instead.
184 ''')%locals()
185     sys.exit(0)
187 def main():
188     hostname = ''
189     port = 8080
190     try:
191         # handle the command-line args
192         try:
193             optlist, args = getopt.getopt(sys.argv[1:], 'n:p:u:')
194         except getopt.GetoptError, e:
195             usage(str(e))
197         user = ROUNDUP_USER
198         for (opt, arg) in optlist:
199             if opt == '-n': hostname = arg
200             elif opt == '-p': port = int(arg)
201             elif opt == '-u': user = arg
202             elif opt == '-h': usage()
204         if hasattr(os, 'getuid'):
205             # if root, setuid to the running user
206             if not os.getuid() and user is not None:
207                 try:
208                     import pwd
209                 except ImportError:
210                     raise ValueError, _("Can't change users - no pwd module")
211                 try:
212                     uid = pwd.getpwnam(user)[2]
213                 except KeyError:
214                     raise ValueError, _("User %(user)s doesn't exist")%locals()
215                 os.setuid(uid)
216             elif os.getuid() and user is not None:
217                 print _('WARNING: ignoring "-u" argument, not root')
219             # People can remove this check if they're really determined
220             if not os.getuid() and user is None:
221                 raise ValueError, _("Can't run as root!")
223         # handle instance specs
224         if args:
225             d = {}
226             for arg in args:
227                 try:
228                     name, home = arg.split('=')
229                 except ValueError:
230                     raise ValueError, _("Instances must be name=home")
231                 d[name] = home
232             RoundupRequestHandler.ROUNDUP_INSTANCE_HOMES = d
233     except SystemExit:
234         raise
235     except:
236         exc_type, exc_value = sys.exc_info()[:2]
237         usage('%s: %s'%(exc_type, exc_value))
239     # we don't want the cgi module interpreting the command-line args ;)
240     sys.argv = sys.argv[:1]
241     address = (hostname, port)
242     httpd = BaseHTTPServer.HTTPServer(address, RoundupRequestHandler)
243     print _('Roundup server started on %(address)s')%locals()
244     httpd.serve_forever()
246 if __name__ == '__main__':
247     main()
250 # $Log: not supported by cvs2svn $
251 # Revision 1.24  2002/01/05 02:19:03  richard
252 # i18n'ification
254 # Revision 1.23  2001/12/15 23:47:07  richard
255 # sys module went away...
257 # Revision 1.22  2001/12/13 00:20:01  richard
258 #  . Centralised the python version check code, bumped version to 2.1.1 (really
259 #    needs to be 2.1.2, but that isn't released yet :)
261 # Revision 1.21  2001/12/02 05:06:16  richard
262 # . We now use weakrefs in the Classes to keep the database reference, so
263 #   the close() method on the database is no longer needed.
264 #   I bumped the minimum python requirement up to 2.1 accordingly.
265 # . #487480 ] roundup-server
266 # . #487476 ] INSTALL.txt
268 # I also cleaned up the change message / post-edit stuff in the cgi client.
269 # There's now a clearly marked "TODO: append the change note" where I believe
270 # the change note should be added there. The "changes" list will obviously
271 # have to be modified to be a dict of the changes, or somesuch.
273 # More testing needed.
275 # Revision 1.20  2001/11/26 22:55:56  richard
276 # Feature:
277 #  . Added INSTANCE_NAME to configuration - used in web and email to identify
278 #    the instance.
279 #  . Added EMAIL_SIGNATURE_POSITION to indicate where to place the roundup
280 #    signature info in e-mails.
281 #  . Some more flexibility in the mail gateway and more error handling.
282 #  . Login now takes you to the page you back to the were denied access to.
284 # Fixed:
285 #  . Lots of bugs, thanks Roché and others on the devel mailing list!
287 # Revision 1.19  2001/11/12 22:51:04  jhermann
288 # Fixed option & associated error handling
290 # Revision 1.18  2001/11/01 22:04:37  richard
291 # Started work on supporting a pop3-fetching server
292 # Fixed bugs:
293 #  . bug #477104 ] HTML tag error in roundup-server
294 #  . bug #477107 ] HTTP header problem
296 # Revision 1.17  2001/10/29 23:55:44  richard
297 # Fix to CGI top-level index (thanks Juergen Hermann)
299 # Revision 1.16  2001/10/27 00:12:21  richard
300 # Fixed roundup-server for windows, thanks Juergen Hermann.
302 # Revision 1.15  2001/10/12 02:23:26  richard
303 # Didn't clean up after myself :)
305 # Revision 1.14  2001/10/12 02:20:32  richard
306 # server now handles setuid'ing much better
308 # Revision 1.13  2001/10/05 02:23:24  richard
309 #  . roundup-admin create now prompts for property info if none is supplied
310 #    on the command-line.
311 #  . hyperdb Class getprops() method may now return only the mutable
312 #    properties.
313 #  . Login now uses cookies, which makes it a whole lot more flexible. We can
314 #    now support anonymous user access (read-only, unless there's an
315 #    "anonymous" user, in which case write access is permitted). Login
316 #    handling has been moved into cgi_client.Client.main()
317 #  . The "extended" schema is now the default in roundup init.
318 #  . The schemas have had their page headings modified to cope with the new
319 #    login handling. Existing installations should copy the interfaces.py
320 #    file from the roundup lib directory to their instance home.
321 #  . Incorrectly had a Bizar Software copyright on the cgitb.py module from
322 #    Ping - has been removed.
323 #  . Fixed a whole bunch of places in the CGI interface where we should have
324 #    been returning Not Found instead of throwing an exception.
325 #  . Fixed a deviation from the spec: trying to modify the 'id' property of
326 #    an item now throws an exception.
328 # Revision 1.12  2001/09/29 13:27:00  richard
329 # CGI interfaces now spit up a top-level index of all the instances they can
330 # serve.
332 # Revision 1.11  2001/08/07 00:24:42  richard
333 # stupid typo
335 # Revision 1.10  2001/08/07 00:15:51  richard
336 # Added the copyright/license notice to (nearly) all files at request of
337 # Bizar Software.
339 # Revision 1.9  2001/08/05 07:44:36  richard
340 # Instances are now opened by a special function that generates a unique
341 # module name for the instances on import time.
343 # Revision 1.8  2001/08/03 01:28:33  richard
344 # Used the much nicer load_package, pointed out by Steve Majewski.
346 # Revision 1.7  2001/08/03 00:59:34  richard
347 # Instance import now imports the instance using imp.load_module so that
348 # we can have instance homes of "roundup" or other existing python package
349 # names.
351 # Revision 1.6  2001/07/29 07:01:39  richard
352 # Added vim command to all source so that we don't get no steenkin' tabs :)
354 # Revision 1.5  2001/07/24 01:07:59  richard
355 # Added command-line arg handling to roundup-server so it's more useful
356 # out-of-the-box.
358 # Revision 1.4  2001/07/23 10:31:45  richard
359 # disabled the reloading until it can be done properly
361 # Revision 1.3  2001/07/23 08:53:44  richard
362 # Fixed the ROUNDUPS decl in roundup-server
363 # Move the installation notes to INSTALL
365 # Revision 1.2  2001/07/23 04:05:05  anthonybaxter
366 # actually quit if python version wrong
368 # Revision 1.1  2001/07/23 03:46:48  richard
369 # moving the bin files to facilitate out-of-the-boxness
371 # Revision 1.1  2001/07/22 11:15:45  richard
372 # More Grande Splite stuff
375 # vim: set filetype=python ts=4 sw=4 et si