forked from stef/stash
/
stash.py
executable file
·652 lines (608 loc) · 27 KB
/
stash.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/env python
# (c) 2013 s@ctrlc.hu
#
# This 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.
from cfg import CONFIG
class Dropper(object):
""" class implementing a WSGI app for uploading files
"""
def __init__(self,environ, start_response):
self.environ=environ
self.resp=start_response
self.stashid = environ.get('REQUEST_URI').split('/')[1]
self.path = environ.get('REQUEST_URI').split('/')[2].split('?')[0]
if not self.stashid.isalnum() and (self.path and not self.path.isalnum()):
raise
# parse params
tmp = environ.get('REQUEST_URI').split('?')
self.params = {}
if len(tmp)>1:
for exp in '?'.join(tmp[1:]).split('&'):
tmp2 = exp.split('=')
self.params[tmp2[0]]='='.join(tmp2[1:])
self.recipients, self.users = self.loadstash()
def loadstash(self):
""" loads an upload directory configuration
"""
with open(CONFIG['root']+'/drop/'+self.stashid+'.cfg','r') as fd:
recipients=fd.readline().split()
users=[x.strip() for x in fd.readlines()]
return recipients, users
def handle(self):
if self.path in ['/', '']:
return self.uploadform()
if self.path in ['feed', 'feed/']:
return self.feed()
return self.fetch('drop/'+self.stashid+'/'+self.path)
try:
return self.fetch('drop/'+self.stashid+'/'+self.path)
except:
return _404(self.environ,self.resp)
def uploadform(self):
msg=''
if self.environ.get('REQUEST_METHOD') == 'POST':
if int(self.environ.get('CONTENT_LENGTH', 0)) != 0:
p=PostUploadParser(self)
if not p.csrf:
status = '303 See Other'
# todo add hash of uploaded file to params
response_headers = [('Location', '/%s/' % self.stashid)]
self.resp(status, response_headers)
return []
else:
status = '302 Found'
# todo add hash of uploaded file to params
response_headers = [('Location', '/%s/?ok=%s' % (self.stashid, p.hash.hexdigest()))]
self.resp(status, response_headers)
return []
if not msg and self.params.get('ok','').isalnum():
msg="Successful upload.<br />SHA256 was %s" % self.params.get('ok')
return send_template(self.resp,
'uploadform.html',
msg=msg,
jobid=hmac.new(CONFIG['secret'],
repr(time.time()),
hashlib.sha256).hexdigest(),
csrf=getcsrf(),
isadmin=authorized(self.environ, CONFIG['admins']),
)
def feed(self):
gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
knownkeys={x['keyid']: x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]
for x in gpg.list_keys()}
if not authorized(self.environ, [knownkeys[x] for x in self.recipients]):
return _404(self.environ, self.resp)
result = getsubmissions(self.stashid)
BASEURL="%s://%s" % (self.environ.get('wsgi.url_scheme'),
self.environ.get('SERVER_NAME'))
rss = RSS2.RSS2(
title = "submission feed",
link = "%s/feed" % BASEURL,
description = 'submission feed',
lastBuildDate = datetime.datetime.utcnow(),
items = [RSS2.RSSItem( title = "%s" % item[1],
link = "%s/%s" % (BASEURL, item[1]),
description = 'size: %s' % sizeof_fmt(item[2]),
guid = RSS2.Guid("%s/%s" % (BASEURL, item[1])),
pubDate = item[0])
for item in result])
data = rss.to_xml()
status = '200 OK'
response_headers = [('Content-type', 'application/rss+xml'),
('Content-Length', str(len(data)))]
self.resp(status, response_headers)
return [data]
def fetch(self, filename):
gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
knownkeys={x['keyid']: x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]
for x in gpg.list_keys()}
if not authorized(self.environ, [knownkeys[x] for x in self.recipients]):
return _404(self.environ, self.resp)
status = '200 OK'
response_headers = [('Content-type', 'application/pgp'),
('Content-Length', str(os.path.getsize(filename)))]
self.resp(status, response_headers)
filelike = file(filename, 'rb')
block_size = 4096
if 'wsgi.file_wrapper' in self.environ:
return self.environ['wsgi.file_wrapper'](filelike, block_size)
else:
return iter(lambda: filelike.read(block_size), '')
@staticmethod
def newstash(owners, uploaders, stashid=None):
if not os.path.exists(CONFIG['root']+'/drop'): os.mkdir(CONFIG['root']+'/drop')
# create stash root dir
if stashid:
os.mkdir(CONFIG['root']+'/drop/'+stashid)
else:
stashid=tempfile.mkdtemp(dir=CONFIG['root']+'/drop', prefix='').split('/')[-1]
# create stash.cfg
with open(CONFIG['root']+'/drop/'+stashid+'.cfg', 'w') as fd:
fd.write(' '.join(owners)+'\n')
fd.write('\n'.join(uploaders))
# init gpghome if not existing
if not os.path.exists(CONFIG['gpghome']):
os.mkdir(CONFIG['gpghome'])
os.chmod(CONFIG['gpghome'], 0700)
# auto-import public keys of owners into local pubkeyring
gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
allkeyids=[x['keyid'] for x in gpg.list_keys()]
for kid in owners:
if not kid in allkeyids:
f = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search=%s' % kid)
key = f.read()
gpg.import_keys(key)
return stashid
startfile='Content-Disposition: form-data; name="file"; filename="'
startvar='Content-Disposition: form-data; name="%s"'
contenttxt='Content-Type: '
class PostUploadParser(object):
""" File-like Object implementing rudimentary parsing of post data
with uploaded file and optional encryption. The POST data is
filtered in buffered memory and written through gpg to the
disk if crypto is enabled (default).
"""
def __init__(self, dropper):
self.dropper = dropper
self.fd=dropper.environ['wsgi.input']
self.delim='\r\n'+self.fd.readline().strip()
self.readbuf=''
self.finished=False
self.hash=hashlib.sha256()
self.size=0
self.gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
self.crypto = True
self.csrf = None
while True:
# parse the stream
line=self.fd.readline()
if len(line)==0: break
line=line.strip()
if line.startswith(startfile):
if not (self.csrf and csrf({'csrf': self.csrf})):
self.csrf=False
return
# handle file upload part of POST
self.filterfile(line)
elif line.startswith(startvar % 'crypto'):
# handle disabling crypto POST param
line=self.fd.readline().strip()
if len(line)>0:
raise Exception("y u no empty line!!#@#!@")
line=self.fd.readline().strip()
if line.lower()=="on":
self.crypto=False
elif line.startswith(startvar % 'csrf'):
# handle csrf post param
line=self.fd.readline().strip()
if len(line)>0:
raise Exception("y u no empty line!!#@#!@")
line=self.fd.readline().strip()
self.csrf=line
def filterfile(self, line):
""" parsing and handling the mime part of a POST file upload
param.
param line: is the first line of the mime part.
"""
name=line[len(startfile):-1]
line=self.fd.readline().strip()
if not line.startswith(contenttxt):
raise Exception("y u no content_type?!?")
content_type=line[len(contenttxt):]
line=self.fd.readline() # drop separating empty line
dropDir = CONFIG['root'] + '/drop/'+ self.dropper.stashid
fd, fname = mkstemp(dir=dropDir + '/')
if self.crypto:
os.close(fd)
self.gpg.encrypt_file(self,
self.dropper.recipients,
output=fname,
always_trust=True)
else:
fd=os.fdopen(fd, 'w')
while True:
buf=self.read(1024)
if buf=='': break
fd.write(buf)
meta="%s\n%s\n%s\n%s" % (
name,
content_type,
self.dropper.environ.get('dn'),
self.hash.hexdigest())
if self.crypto:
meta=self.gpg.encrypt(meta,
self.dropper.recipients,
always_trust=True,
armor=False).data
with open(fname+'.meta','w') as fd:
fd.write(meta)
url=urlunparse((self.dropper.environ.get('wsgi.url_scheme'),
self.dropper.environ.get('HTTP_HOST') + \
(':'+self.dropper.environ.get('HTTP_HOST')
if (self.dropper.environ['wsgi.url_scheme']=='https' and
self.dropper.environ['SERVER_PORT'] != '443') or
(self.dropper.environ['wsgi.url_scheme']=='http' and
self.dropper.environ['SERVER_PORT'] != '80')
else ''),
"/%s/%s" % (self.dropper.stashid, fname.split('/')[-1]),
'',
'',
''))
if CONFIG.get('notify'):
sender=(todn(self.dropper.environ.get('dn',''))['emailAddress']
if todn(self.dropper.environ.get('dn','')).get('emailAddress')
else "anon@localhost")
txt = render_template('notification.txt',
path=fname,
name=name,
type=content_type,
url=url,
size=sizeof_fmt(self.size),
sender=sender,
hash=self.hash.hexdigest())
txt=self.gpg.encrypt(txt,
self.dropper.recipients,
always_trust=True).data
res=mail(txt,
[x['uids'][0]
for x in self.gpg.list_keys()
if x['keyid'] in self.dropper.recipients])
def read(self, bytes):
""" implements a file-like read interface which is used to
calculate a sha256 sum on the fly and parse until the end
of the mime part.
"""
if self.finished: return ''
if len(self.readbuf)>0:
# add prefetch and clean it
data=self.readbuf+self.fd.read(bytes-len(self.readbuf))
self.readbuf=''
else:
# read next block
data=self.fd.read(bytes)
# eof
if len(data)==0:
return data
# simple check for delim
end=data.find(self.delim)
if end>=0:
self.hash.update(data[:end])
self.size+=len(data[:end])
self.finished=True
return data[:end]
# check for delim fragments on packet boundary
end=len(data)
while True:
end=self.delim.rfind(data[-1], 0, end)
if end < 0: break
end=end+1
if self.delim[:end]==data[-end:]:
trailer=self.fd.read(len(self.delim) - end)
if trailer.startswith(self.delim[end:]):
self.finished=True
self.hash.update(data[:-end])
self.size+=len(data[:-end])
return data[:-end]
self.readbuf=trailer
end-=1
self.hash.update(data)
self.size+=len(data)
return data
class AdminHandler(object):
""" class for handling user registration, and creating new stashes over the web
provides similar urls as Flask-TLSAuth
/settings/register - using keygen tag
/settings/request - submit your own CSR
/settings/requests - list all user account requests (admin only)
/settings/newstash - create a new stash (admin only)
/settings/accept/<certhash> - accept a new user request (admin only)
/settings/reject/<certhash> - reject a new user request (admin only)
"""
def __init__(self,environ, start_response):
self.ca=CertAuthority(CONFIG['ca'])
self.environ=environ
self.resp=start_response
self.action = self.environ.get('REQUEST_URI').split('/')[2]
if not self.action.isalnum():
raise
# parse params
tmp = environ.get('REQUEST_URI').split('?')
self.params = {}
if len(tmp)>1:
for exp in '?'.join(tmp[1:]).split('&'):
tmp2 = exp.split('=')
self.params[tmp2[0]]='='.join(tmp2[1:])
def handle(self):
""" main dispatcher of AdminHandler
"""
if self.action == 'newstash':
return self.newstash()
elif self.action == 'register':
return self.register()
elif self.action == 'request':
return self.submitcsr()
elif self.action == 'requests':
return self.showcsrs()
elif self.action == 'accept':
return self.accept()
elif self.action == 'reject':
return self.reject()
elif self.action == 'stashes':
return self.stashes()
elif self.action == 'delete':
return self.delete()
return _404(self.environ,self.resp)
def register(self):
msg=''
if self.environ.get('REQUEST_METHOD') == 'POST':
if int(self.environ.get('CONTENT_LENGTH', 0)) != 0:
fd=self.environ['wsgi.input']
params={}
for exp in fd.readline().split('&'):
tmp2 = exp.split('=')
params[tmp2[0]]=urllib.unquote('='.join(tmp2[1:]))
if not csrf(params):
msg="Try again from our fine server please."
elif params.get('email') and params.get('key'):
csr=spkac2cert(''.join(params['key']),
params['email'],
name=params.get('name'))
self.ca.submit(csr)
msg="Success<br />Your request will be reviewed soon."
else:
msg="Sorry but you must supply an email address"
return send_template(self.resp,
'register.html',
isadmin=authorized(self.environ, CONFIG['admins']),
csrf=getcsrf(),
msg=msg)
def submitcsr(self):
msg=''
if self.environ.get('REQUEST_METHOD') == 'POST':
if int(self.environ.get('CONTENT_LENGTH', 0)) != 0:
fd=self.environ['wsgi.input']
params={}
for exp in fd.readline().split('&'):
tmp2 = exp.split('=')
params[tmp2[0]]=urllib.unquote('='.join(tmp2[1:])).strip()
if not csrf(params):
msg="Try again from our fine server please."
elif params.get('csr'):
ca=CertAuthority(CONFIG['ca'])
tmp=params['csr'].split('\n')
csr='\n'.join([urllib.unquote_plus(tmp[0]),
'\n'.join(tmp[1:-1]),
urllib.unquote_plus(tmp[-1])])
try:
self.ca.submit(csr)
except:
msg="Fail<br />please submit a valid Certificate Signing Request containing your email."
else:
msg="Success<br />Your request will be reviewed soon."
return send_template(self.resp, 'certify.html',
isadmin=authorized(self.environ, CONFIG['admins']),
csrf=getcsrf(),
msg=msg)
def showcsrs(self):
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
return send_template(self.resp,
'csrs.html',
isadmin=authorized(self.environ, CONFIG['admins']),
certs=[(todn(cert.get_subject()),
datetime.datetime.fromtimestamp(os.stat(path).st_mtime),
os.path.basename(path))
for cert, path
in self.ca.incoming()])
def accept(self):
""" provides facility for users belonging to `groups` to sign incoming CSRs
"""
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
path=self.ca._incoming+'/'+self.environ.get('REQUEST_URI').split('/')[3]
print "certifying", path
cert=self.ca.signcsr(load(path))
mailsigned([cert])
os.unlink(path)
status = '302 Found'
response_headers = [('Location', '/settings/requests')]
self.resp(status, response_headers)
return []
def reject(self):
""" provides facility for users belonging to `groups` to reject incoming CSRs
"""
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
path=self.ca._incoming+'/'+self.environ.get('REQUEST_URI').split('/')[3]
os.unlink(path)
status = '302 Found'
response_headers = [('Location', '/settings/requests')]
self.resp(status, response_headers)
return []
def newstash(self):
msg=''
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
knownkeys={x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]:x for x in gpg.list_keys()}
keyid=knownkeys.get(email, {}).get('keyid')
if self.environ.get('REQUEST_METHOD') == 'POST':
if int(self.environ.get('CONTENT_LENGTH', 0)) != 0:
fd=self.environ['wsgi.input']
params=defaultdict(list)
for exp in fd.readline().split('&'):
tmp2 = exp.split('=')
params[tmp2[0]]=urllib.unquote('='.join(tmp2[1:])).strip()
if not csrf(params):
msg="Try again from our fine server please."
elif keyid or params.get('keyid') or params.get('pk'):
if params.get('pk'):
gpg.import_keys(params.get('pk'))
knownkeys={x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]:x for x in gpg.list_keys()}
elif params.get('keyid'):
f = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search=%s' % params.get('keyid'))
key = f.read()
gpg.import_keys(key)
knownkeys={x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]:x for x in gpg.list_keys()}
keyid=knownkeys.get(email)['keyid']
if not keyid:
msg="Couldn't locate PGP key for owner, try uploading one"
elif not params.get('invited'):
msg="You must invite someone to share this stash with"
else:
stashid=Dropper.newstash([knownkeys[email]['keyid']],
urllib.unquote_plus(params['invited']).split('\n'),
urllib.unquote_plus(params.get('name')))
# todo sent invitation mail to uploaders
status = '302 Found'
response_headers = [('Location', '/%s/' % stashid )]
self.resp(status, response_headers)
return []
else:
msg="You must supply a PGP public key for enabling encryption of stored data"
return send_template(self.resp, 'newstash.html',
isadmin=authorized(self.environ, CONFIG['admins']),
msg=msg,
csrf=getcsrf(),
knownkey=keyid)
def stashes(self):
""" lists all stashes for admins
"""
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
gpg = gnupg.GPG(gnupghome=CONFIG['gpghome'])
knownkeys={x['keyid']: x['uids'][0][ x['uids'][0].rfind('<')+1: x['uids'][0].rfind('>')]
for x in gpg.list_keys()}
stashes=[]
for fname in os.listdir(CONFIG['root']+'/drop'):
if not fname.endswith('.cfg'):
continue
with open(CONFIG['root']+'/drop/'+fname,'r') as fd:
owner=[knownkeys[kid] for kid in fd.readline().strip().split()]
friends=[x.strip() for x in fd.readlines()]
files=[os.stat(CONFIG['root']+'/drop/'+fname[:-4]+'/'+f).st_size
for f in os.listdir(CONFIG['root']+'/drop/'+fname[:-4])
if not f.endswith('.meta')]
stashes.append((fname[:-4],
owner,
friends,
sizeof_fmt(sum(files)),
len(files)))
return send_template(self.resp,
'stashes.html',
isadmin=authorized(self.environ, CONFIG['admins']),
stashes=stashes)
def delete(self):
""" provides facility for users belonging to `groups` to reject incoming CSRs
"""
email=authorized(self.environ, CONFIG['admins'])
if not email:
return _404(self.environ, self.resp)
stashid=self.environ.get('REQUEST_URI').split('/')[3]
if stashid.isalnum():
path=CONFIG['root']+'/drop/'+stashid
os.unlink(path+'.cfg')
shutil.rmtree(path)
status = '302 Found'
response_headers = [('Location', '/settings/stashes')]
self.resp(status, response_headers)
return []
# various helper functions
def _404(environ, start_response):
""" helper for displaying an error message"""
status = '404 Not Found'
res = """<html><head><title>Not Found</title></head><body><h1>404 Not found</h1></body></html>"""
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(res)))]
start_response(status, response_headers)
return [res]
def mail(data, to):
msg = Message()
msg.set_type('application/pgp')
msg.set_param('format', 'text')
msg.set_param('x-action', 'encrypt')
msg.set_payload(data)
composed = msg.as_string()
s = smtplib.SMTP('localhost', timeout=3)
if s:
s.sendmail(CONFIG['sender'], to, composed)
return True
else:
return False
def visitor(res, dirname, names):
for name in names:
if name.endswith('.meta'): continue
res.append((datetime.datetime.fromtimestamp(os.stat(dirname+'/'+name).st_mtime).isoformat(' '),
'/'.join(dirname.rsplit('/')[-1:])+'/'+name,
os.path.getsize(dirname+'/'+name)))
def getsubmissions(stash):
result = []
os.path.walk(CONFIG['root'] + '/drop/'+stash, visitor, result)
return reversed(sorted(result))
def sizeof_fmt(num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
def render_template(template, **kwargs):
t = env.get_template(template)
return str(t.render(**kwargs))
def send_template(resp, template, **kwargs):
r = render_template(template, **kwargs)
status = '200 OK'
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(r)))]
resp(status, response_headers)
return [r]
def authorized(env, users=None):
email=todn(env.get('dn')).get('emailAddress')
if env.get('verified') == 'SUCCESS':
#print self.users
if users and email not in users:
return False
return email
return False
def csrf(params):
if params.get('csrf','').isalnum() and os.path.exists('/tmp/csrf'+params.get('csrf')):
if os.path.getmtime('/tmp/csrf'+params.get('csrf'))>time.time()-7200:
return True
os.unlink('/tmp/csrf'+params.get('csrf'))
def getcsrf():
token=hmac.new(CONFIG['secret'],
str(time.time())+ssl.rand.bytes(8).encode('hex'),
hashlib.sha256).hexdigest()
with open('/tmp/csrf'+token,'w') as fd:
fd.write(u"\U0001F34F".encode('utf8'))
return token
def application(environ, start_response):
if environ.get('REQUEST_URI').startswith('/settings/'):
try:
handler=AdminHandler(environ, start_response)
except:
return _404(environ,start_response)
else:
try:
handler=Dropper(environ, start_response)
except:
return _404(environ,start_response)
return handler.handle()
# imports
from tempfile import mkdtemp, mkstemp
from datetime import datetime
from email.message import Message
from collections import defaultdict
from urlparse import urlunparse
from tlsauth import CertAuthority, spkac2cert, MBSTRING_ASC, todn, mailsigned, load
import smtplib, subprocess, gnupg, time, hashlib, urllib, datetime, tempfile, shutil, hmac, sys, os
import PyRSS2Gen as RSS2
import OpenSSL as ssl
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader(['./templates']))