1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# Copyright (C) 2006-2008 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import sys
from cStringIO import StringIO
from email import message_from_string
from urlparse import urlparse
from Mailman.configuration import config
# XXX Should this be configurable in Defaults.py?
STEALTH_MODE = False
MOVED_RESPONSE = '302 Found'
# Above is for debugging convenience. We should use:
# MOVED_RESPONSE = '301 Moved Permanently'
def websafe(s):
return s
SCRIPTS = ['admin', 'admindb', 'confirm', 'create',
'edithtml', 'listinfo', 'options', 'private',
'rmlist', 'roster', 'subscribe']
ARCHVIEW = ['private']
SLASH = '/'
NL2 = '\n\n'
CRLF2 = '\r\n\r\n'
dotonly = re.compile(r'^\.+$')
# WSGI to CGI wrapper. Mostly copied from scripts/driver.
def mailman_app(environ, start_response):
"""Wrapper to *.py CGI commands"""
global STEALTH_MODE, websafe
try:
try:
if not STEALTH_MODE:
from Mailman.Utils import websafe
except:
STEALTH_MODE = True
raise
import logging
log = logging.getLogger('mailman.error')
from Mailman import i18n
i18n.set_language(config.DEFAULT_SERVER_LANGUAGE)
path = environ['PATH_INFO']
paths = path.split(SLASH)
# sanity check for paths
spaths = [ i for i in paths[1:] if i and not dotonly.match(i) ]
if spaths and spaths != paths[1:]:
newpath = SLASH + SLASH.join(spaths)
start_response(MOVED_RESPONSE, [('Location', newpath)])
return 'Location: ' + newpath
# find script name
for script in SCRIPTS:
if script in spaths:
# Get script position in spaths and break.
scrpos = spaths.index(script)
break
else:
# Can't find valid script.
start_response('404 Not Found', [])
return '404 Not Found'
# Compose CGI SCRIPT_NAME and PATH_INFO from WSGI path.
script_name = SLASH + SLASH.join(spaths[:scrpos+1])
environ['SCRIPT_NAME'] = script_name
if len(paths) > scrpos+2:
path_info = SLASH + SLASH.join(paths[scrpos+2:])
if script in ARCHVIEW \
and path_info.count('/') in (1,2) \
and not paths[-1].split('.')[-1] in ('html', 'txt', 'gz'):
# Add index.html if /private/listname or
# /private/listname/YYYYmm is requested.
newpath = script_name + path_info + '/index.html'
start_response(MOVED_RESPONSE, [('Location', newpath)])
return 'Location: ' + newpath
environ['PATH_INFO'] = path_info
else:
environ['PATH_INFO'] = ''
# Reverse proxy environment.
if environ.has_key('HTTP_X_FORWARDED_HOST'):
environ['HTTP_HOST'] = environ['HTTP_X_FORWARDED_HOST']
if environ.has_key('HTTP_X_FORWARDED_FOR'):
environ['REMOTE_HOST'] = environ['HTTP_X_FORWARDED_FOR']
modname = 'Mailman.Cgi.' + script
# Clear previous cookie before setting new one.
os.environ['HTTP_COOKIE'] = ''
for k, v in environ.items():
os.environ[k] = str(v)
# Prepare for redirection
save_stdin = sys.stdin
# CGI writes its output to sys.stdout, while wsgi app should
# return (list of) strings.
save_stdout = sys.stdout
save_stderr = sys.stderr
tmpstdout = StringIO()
tmpstderr = StringIO()
response = ''
try:
try:
sys.stdin = environ['wsgi.input']
sys.stdout = tmpstdout
sys.stderr = tmpstderr
__import__(modname)
sys.modules[modname].main()
response = sys.stdout.getvalue()
finally:
sys.stdin = save_stdin
sys.stdout = save_stdout
sys.stderr = save_stderr
except SystemExit:
sys.stdout.write(tmpstdout.getvalue())
if response:
try:
head, content = response.split(NL2, 1)
except ValueError:
head, content = response.split(CRLF2, 1)
m = message_from_string(head + CRLF2)
start_response('200 OK', m.items())
return [content]
else:
# TBD: Error Code/Message
start_response('500 Server Error', [])
return '500 Internal Server Error'
except:
start_response('200 OK', [('Content-Type', 'text/html')])
retstring = print_traceback(log)
retstring += print_environment(log)
return retstring
# These functions are extracted and modified from scripts/driver.
#
# If possible, we print the error to two places. One will always be stdout
# and the other will be the log file if a log file was created. It is assumed
# that stdout is an HTML sink.
def print_traceback(log=None):
try:
import traceback
except ImportError:
traceback = None
try:
from mailman.version import VERSION
except ImportError:
VERSION = '<undetermined>'
# Write to the log file first.
if log:
outfp = StringIO()
print >> outfp, '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
print >> outfp, '[----- Mailman Version: %s -----]' % VERSION
print >> outfp, '[----- Traceback ------]'
if traceback:
traceback.print_exc(file=outfp)
else:
print >> outfp, '[failed to import module traceback]'
print >> outfp, '[exc: %s, var: %s]' % sys.exc_info()[0:2]
# Don't use .exception() since that'll give us the exception twice.
# IWBNI we could print directly to the log's stream, or treat a log
# like an output stream.
log.error('%s', outfp.getvalue())
# return HTML sink.
htfp = StringIO()
print >> htfp, """\
<head><title>Bug in Mailman version %(VERSION)s</title></head>
<body bgcolor=#ffffff><h2>Bug in Mailman version %(VERSION)s</h2>
<p><h3>We're sorry, we hit a bug!</h3>
""" % locals()
if not STEALTH_MODE:
print >> htfp, '''<p>If you would like to help us identify the problem,
please email a copy of this page to the webmaster for this site with
a description of what happened. Thanks!
<h4>Traceback:</h4><p><pre>'''
exc_info = sys.exc_info()
if traceback:
for line in traceback.format_exception(*exc_info):
print >> htfp, websafe(line)
else:
print >> htfp, '[failed to import module traceback]'
print >> htfp, '[exc: %s, var: %s]' %\
[websafe(x) for x in exc_info[0:2]]
print >> htfp, '\n\n</pre></body>'
else:
print >> htfp, '''<p>Please inform the webmaster for this site of this
problem. Printing of traceback and other system information has been
explicitly inhibited, but the webmaster can find this information in the
Mailman error logs.'''
return htfp.getvalue()
def print_environment(log=None):
try:
import os
except ImportError:
os = None
if log:
outfp = StringIO()
# Write some information about our Python executable to the log file.
print >> outfp, '[----- Python Information -----]'
print >> outfp, 'sys.version =', sys.version
print >> outfp, 'sys.executable =', sys.executable
print >> outfp, 'sys.prefix =', sys.prefix
print >> outfp, 'sys.exec_prefix =', sys.exec_prefix
print >> outfp, 'sys.path =', sys.exec_prefix
print >> outfp, 'sys.platform =', sys.platform
# Write the same information to the HTML sink.
htfp = StringIO()
if not STEALTH_MODE:
print >> htfp, """\
<p><hr><h4>Python information:</h4>
<p><table>
<tr><th>Variable</th><th>Value</th></tr>
<tr><td><tt>sys.version</tt></td><td> %s </td></tr>
<tr><td><tt>sys.executable</tt></td><td> %s </td></tr>
<tr><td><tt>sys.prefix</tt></td><td> %s </td></tr>
<tr><td><tt>sys.exec_prefix</tt></td><td> %s </td></tr>
<tr><td><tt>sys.path</tt></td><td> %s </td></tr>
<tr><td><tt>sys.platform</tt></td><td> %s </td></tr>
</table>""" % (sys.version, sys.executable, sys.prefix,
sys.exec_prefix, sys.path, sys.platform)
# Write environment variables to the log file.
if log:
print >> outfp, '[----- Environment Variables -----]'
if os:
for k, v in os.environ.items():
print >> outfp, '\t%s: %s' % (k, v)
else:
print >> outfp, '[failed to import module os]'
# Write environment variables to the HTML sink.
if not STEALTH_MODE:
print >> htfp, """\
<p><hr><h4>Environment variables:</h4>
<p><table>
<tr><th>Variable</th><th>Value</th></tr>
"""
if os:
for k, v in os.environ.items():
print >> htfp, '<tr><td><tt>' + websafe(k) + \
'</tt></td><td>' + websafe(v) + \
'</td></tr>'
print >> htfp, '</table>'
else:
print >> htfp, '<p><hr>[failed to import module os]'
# Dump the log output
if log:
log.error('%s', outfp.getvalue())
return htfp.getvalue()
|