From 9b91e137051c48da242ad0670314c21f13c349bc Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 30 May 2018 12:19:45 +0200 Subject: [PATCH] added pyramid_mailer --- development.ini | 20 ++++++++++++++ honeypot/__init__.py | 63 ++++++++++++++++++++++---------------------- honeypot/utils.py | 12 +++------ production.ini | 14 ++++++++++ 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/development.ini b/development.ini index 9a5ed7e..4e3f4ca 100644 --- a/development.ini +++ b/development.ini @@ -11,9 +11,29 @@ pyramid.debug_authorization = false pyramid.debug_notfound = false pyramid.debug_routematch = false pyramid.default_locale_name = en +pyramid.includes = + pyramid_mailer.debug 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 # '127.0.0.1' and '::1'. # debugtoolbar.hosts = 127.0.0.1 ::1 diff --git a/honeypot/__init__.py b/honeypot/__init__.py index 4dbdfd3..3cfc9b1 100644 --- a/honeypot/__init__.py +++ b/honeypot/__init__.py @@ -26,6 +26,7 @@ class RootResource: @classmethod def pre_init(cls, moin_config_dir): + ''' parses the moinmoin farmconfig file ''' cls.moin_config_dir = moin_config_dir moin_farmconfig = os.path.join(moin_config_dir, 'farmconfig.py') encoding = utils.guess_encoding(moin_farmconfig) @@ -34,24 +35,29 @@ class RootResource: def get_moin_user(self): - email, name = '', '' - moin_data_dir = self._get_wiki_data_dir() - moin_session_dir = os.path.join( - moin_data_dir, - 'cache', - '__session__' - ) - moin_user_id = self._get_user_id(moin_session_dir) - moin_user_file = os.path.join(moin_data_dir, 'user', moin_user_id) - with open(moin_user_file, 'r') as fh: - for line in fh: - if line.startswith('email='): - email = line.split('=', 1)[1] - if line.startswith('name='): - name = line.split('=', 1)[1] - return name, email + ''' returns a name and email address of the current wiki user''' + name, email = '', '' + try: + moin_data_dir = self._get_wiki_data_dir() + moin_session_dir = os.path.join( + moin_data_dir, + 'cache', + '__session__' + ) + moin_user_id = self._get_user_id(moin_session_dir) + moin_user_file = os.path.join(moin_data_dir, 'user', moin_user_id) + with open(moin_user_file, 'r') as fh: + for line in fh: + if line.startswith('email='): + email = line.split('=', 1)[1] + if line.startswith('name='): + name = line.split('=', 1)[1] + except: + pass + return name, email def _get_wiki_data_dir(self): + ''' get the data directory by parsing a wiki config ''' wiki_name = self._get_wiki_name() wiki_config = os.path.join(self.moin_config_dir, wiki_name + '.py') encoding = utils.guess_encoding(wiki_config) @@ -60,17 +66,20 @@ class RootResource: return data_dir def _get_wiki_name(self): + ''' return the internal wiki name for a url ''' for name, re_url in self.moin_wiki_defs: if re.match(re_url, self.request.url): return name def _get_user_id(self, session_dir): + ''' extract the user id from the session store ''' session_path = self._get_session_path(session_dir) with open(session_path, 'rb') as fh: session_data = pickle.load(fh) return session_data.get('user.id') 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(): if key.lower().startswith('moin'): session_path = os.path.join(session_dir, value) @@ -81,38 +90,30 @@ class RootResource: @view_config(context=RootResource) def the_view(context, request): + ''' the one and only view for the app ''' + + name, email = context.get_moin_user() body = [ 'Someone Wanted Some Sweet Honey', '-------------------------------', '', + 'wiki user: %s (%s)' % (name, email), + '', 'requested url: %s' % request.url, 'request method: %s' % request.method, 'client ip address: %s' % request.client_addr, 'remote ip address: %s' % request.remote_addr, '', - 'request.authorization: %s' % request.authorization, - 'request.remote_user: %s' % request.remote_user, - '', 'headers:' ] - body.extend(utils.dict_helper(request.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) ]) + headers = [' %s: %s' % (k, v) for k, v in request.headers.items()] + body.extend(headers) return Response(body='\n'.join(body), content_type='text/plain') - def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ diff --git a/honeypot/utils.py b/honeypot/utils.py index 6d4e8ea..0702913 100644 --- a/honeypot/utils.py +++ b/honeypot/utils.py @@ -2,6 +2,7 @@ from chardet.universaldetector import UniversalDetector def guess_encoding(path): + ''' guess the encoding of a file at a given path ''' detector = UniversalDetector() with open(path, 'rb') as fh: for line in fh: @@ -12,6 +13,7 @@ def guess_encoding(path): def extract_wiki_definitions(file_handle): + ''' extract the wiki definitions from a moinmoin farmconfig file ''' for line in file_handle: if line.startswith('wikis = ['): break @@ -24,6 +26,7 @@ def extract_wiki_definitions(file_handle): def split_wiki_definitions(line): + ''' small helper, returns the wiki name and wiki url regex ''' for quote in ('"', "'"): parts = line.split(quote) if len(parts) == 5: @@ -32,6 +35,7 @@ def split_wiki_definitions(line): def extract_data_dir(fh): + ''' returns the data directory from a single moinmoin wiki config ''' for line in fh: parts = line.split('=', 1) if len(parts) == 2: @@ -40,11 +44,3 @@ def extract_data_dir(fh): value = value.strip() return value[1:-1] - -def dict_helper(dict_like): - return [' %s: %s' % (k, v) for k, v in dict_like.items()] - - - - - diff --git a/production.ini b/production.ini index 232aff3..8a5e7e5 100644 --- a/production.ini +++ b/production.ini @@ -11,9 +11,23 @@ pyramid.debug_authorization = false pyramid.debug_notfound = false pyramid.debug_routematch = false pyramid.default_locale_name = en +pyramid.includes = + pyramid_mailer 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 ###