Browse Source

added pyramid_mailer

main
Holger Frey 7 years ago
parent
commit
9b91e13705
  1. 20
      development.ini
  2. 63
      honeypot/__init__.py
  3. 12
      honeypot/utils.py
  4. 14
      production.ini

20
development.ini

@ -11,9 +11,29 @@ pyramid.debug_authorization = false
pyramid.debug_notfound = false pyramid.debug_notfound = false
pyramid.debug_routematch = false pyramid.debug_routematch = false
pyramid.default_locale_name = en pyramid.default_locale_name = en
pyramid.includes =
pyramid_mailer.debug
moin.config_path = /var/www/moin/config/ moin.config_path = /var/www/moin/config/
# email delivery
mail.host = mail.uni-freiburg.de
mail.port = 25
mail.username =
mail.password =
mail.tls = True
#mail.ssl = True
mail.default_sender = cpiserver@imtek.uni-freiburg.de
mail.admin_email =
frey@imtek.de
prucker@imtek.de
# email delivery
mail.host = localhost
mail.port = 2525
mail.default_sender = ordr@example.com
# By default, the toolbar only appears for clients from IP addresses # By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'. # '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1 # debugtoolbar.hosts = 127.0.0.1 ::1

63
honeypot/__init__.py

@ -26,6 +26,7 @@ class RootResource:
@classmethod @classmethod
def pre_init(cls, moin_config_dir): def pre_init(cls, moin_config_dir):
''' parses the moinmoin farmconfig file '''
cls.moin_config_dir = moin_config_dir cls.moin_config_dir = moin_config_dir
moin_farmconfig = os.path.join(moin_config_dir, 'farmconfig.py') moin_farmconfig = os.path.join(moin_config_dir, 'farmconfig.py')
encoding = utils.guess_encoding(moin_farmconfig) encoding = utils.guess_encoding(moin_farmconfig)
@ -34,24 +35,29 @@ class RootResource:
def get_moin_user(self): def get_moin_user(self):
email, name = '', '' ''' returns a name and email address of the current wiki user'''
moin_data_dir = self._get_wiki_data_dir() name, email = '<unknown user>', '<unknown email>'
moin_session_dir = os.path.join( try:
moin_data_dir, moin_data_dir = self._get_wiki_data_dir()
'cache', moin_session_dir = os.path.join(
'__session__' moin_data_dir,
) 'cache',
moin_user_id = self._get_user_id(moin_session_dir) '__session__'
moin_user_file = os.path.join(moin_data_dir, 'user', moin_user_id) )
with open(moin_user_file, 'r') as fh: moin_user_id = self._get_user_id(moin_session_dir)
for line in fh: moin_user_file = os.path.join(moin_data_dir, 'user', moin_user_id)
if line.startswith('email='): with open(moin_user_file, 'r') as fh:
email = line.split('=', 1)[1] for line in fh:
if line.startswith('name='): if line.startswith('email='):
name = line.split('=', 1)[1] email = line.split('=', 1)[1]
return name, email if line.startswith('name='):
name = line.split('=', 1)[1]
except:
pass
return name, email
def _get_wiki_data_dir(self): def _get_wiki_data_dir(self):
''' get the data directory by parsing a wiki config '''
wiki_name = self._get_wiki_name() wiki_name = self._get_wiki_name()
wiki_config = os.path.join(self.moin_config_dir, wiki_name + '.py') wiki_config = os.path.join(self.moin_config_dir, wiki_name + '.py')
encoding = utils.guess_encoding(wiki_config) encoding = utils.guess_encoding(wiki_config)
@ -60,17 +66,20 @@ class RootResource:
return data_dir return data_dir
def _get_wiki_name(self): def _get_wiki_name(self):
''' return the internal wiki name for a url '''
for name, re_url in self.moin_wiki_defs: for name, re_url in self.moin_wiki_defs:
if re.match(re_url, self.request.url): if re.match(re_url, self.request.url):
return name return name
def _get_user_id(self, session_dir): def _get_user_id(self, session_dir):
''' extract the user id from the session store '''
session_path = self._get_session_path(session_dir) session_path = self._get_session_path(session_dir)
with open(session_path, 'rb') as fh: with open(session_path, 'rb') as fh:
session_data = pickle.load(fh) session_data = pickle.load(fh)
return session_data.get('user.id') return session_data.get('user.id')
def _get_session_path(self, session_dir): def _get_session_path(self, session_dir):
''' get the path to the session store for a given cookie '''
for key, value in self.request.cookies.items(): for key, value in self.request.cookies.items():
if key.lower().startswith('moin'): if key.lower().startswith('moin'):
session_path = os.path.join(session_dir, value) session_path = os.path.join(session_dir, value)
@ -81,38 +90,30 @@ class RootResource:
@view_config(context=RootResource) @view_config(context=RootResource)
def the_view(context, request): def the_view(context, request):
''' the one and only view for the app '''
name, email = context.get_moin_user()
body = [ body = [
'Someone Wanted Some Sweet Honey', 'Someone Wanted Some Sweet Honey',
'-------------------------------', '-------------------------------',
'', '',
'wiki user: %s (%s)' % (name, email),
'',
'requested url: %s' % request.url, 'requested url: %s' % request.url,
'request method: %s' % request.method, 'request method: %s' % request.method,
'client ip address: %s' % request.client_addr, 'client ip address: %s' % request.client_addr,
'remote ip address: %s' % request.remote_addr, 'remote ip address: %s' % request.remote_addr,
'', '',
'request.authorization: %s' % request.authorization,
'request.remote_user: %s' % request.remote_user,
'',
'headers:' 'headers:'
] ]
body.extend(utils.dict_helper(request.headers)) headers = [' %s: %s' % (k, v) for k, v in request.headers.items()]
body.extend(headers)
body.extend(['', 'cookies:'])
if request.cookies:
body.extend(utils.dict_helper(request.cookies))
else:
body.append(' (no cookies)')
name, email = context.get_moin_user()
if email or name:
body.extend(['', 'MoinMoin user: %s (%s)' % (name, email) ])
return Response(body='\n'.join(body), content_type='text/plain') return Response(body='\n'.join(body), content_type='text/plain')
def main(global_config, **settings): def main(global_config, **settings):
""" This function returns a Pyramid WSGI application. """ This function returns a Pyramid WSGI application.
""" """

12
honeypot/utils.py

@ -2,6 +2,7 @@ from chardet.universaldetector import UniversalDetector
def guess_encoding(path): def guess_encoding(path):
''' guess the encoding of a file at a given path '''
detector = UniversalDetector() detector = UniversalDetector()
with open(path, 'rb') as fh: with open(path, 'rb') as fh:
for line in fh: for line in fh:
@ -12,6 +13,7 @@ def guess_encoding(path):
def extract_wiki_definitions(file_handle): def extract_wiki_definitions(file_handle):
''' extract the wiki definitions from a moinmoin farmconfig file '''
for line in file_handle: for line in file_handle:
if line.startswith('wikis = ['): if line.startswith('wikis = ['):
break break
@ -24,6 +26,7 @@ def extract_wiki_definitions(file_handle):
def split_wiki_definitions(line): def split_wiki_definitions(line):
''' small helper, returns the wiki name and wiki url regex '''
for quote in ('"', "'"): for quote in ('"', "'"):
parts = line.split(quote) parts = line.split(quote)
if len(parts) == 5: if len(parts) == 5:
@ -32,6 +35,7 @@ def split_wiki_definitions(line):
def extract_data_dir(fh): def extract_data_dir(fh):
''' returns the data directory from a single moinmoin wiki config '''
for line in fh: for line in fh:
parts = line.split('=', 1) parts = line.split('=', 1)
if len(parts) == 2: if len(parts) == 2:
@ -40,11 +44,3 @@ def extract_data_dir(fh):
value = value.strip() value = value.strip()
return value[1:-1] return value[1:-1]
def dict_helper(dict_like):
return [' %s: %s' % (k, v) for k, v in dict_like.items()]

14
production.ini

@ -11,9 +11,23 @@ pyramid.debug_authorization = false
pyramid.debug_notfound = false pyramid.debug_notfound = false
pyramid.debug_routematch = false pyramid.debug_routematch = false
pyramid.default_locale_name = en pyramid.default_locale_name = en
pyramid.includes =
pyramid_mailer
moin.config_path = /var/www/moin/config/ moin.config_path = /var/www/moin/config/
# email delivery
mail.host = mail.uni-freiburg.de
mail.port = 25
mail.username =
mail.password =
mail.tls = True
#mail.ssl = True
mail.default_sender = cpiserver@imtek.uni-freiburg.de
mail.admin_email =
frey@imtek.de
prucker@imtek.de
### ###
# wsgi server configuration # wsgi server configuration
### ###

Loading…
Cancel
Save