aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2014-12-31 15:35:30 +0100
committerMatěj Cepl <mcepl@cepl.eu>2014-12-31 15:43:13 +0100
commit5dd7a797b99fb51e9141fa0a72aaef01af8c9e53 (patch)
tree02a8f3b977b41891f04804a4987dfa26efa47db0
parent527e535279dcceeb6ffcf091bc9e8fc23b46f287 (diff)
downloadpygn-5dd7a797b99fb51e9141fa0a72aaef01af8c9e53.tar.gz
Radical simplification of logging in whitelist.
Use logging instead of home-made stuff. Fixes #15
-rw-r--r--examples/leafnode-investigation/18034-1419586452-163
-rw-r--r--examples/leafnode-investigation/posting-of-new-message-in-slrn.diff49
-rw-r--r--whitelist.py80
3 files changed, 27 insertions, 165 deletions
diff --git a/examples/leafnode-investigation/18034-1419586452-1 b/examples/leafnode-investigation/18034-1419586452-1
deleted file mode 100644
index 7936b17..0000000
--- a/examples/leafnode-investigation/18034-1419586452-1
+++ /dev/null
@@ -1,63 +0,0 @@
-Newsgroups: gmane.network.leafnode
-From: =?UTF-8?Q?Mat=C4=9Bj?= Cepl <mcepl@cepl.eu>
-Subject: Re: FTBFS on RHEL-7
-References: <53D13097.7070409@cepl.eu> <53D55CD3.2090408@gmx.de>
- <549C594A.3050201@cepl.eu> <slrnm9p7q7.1fe.mcepl@mitmanek.ceplovi.cz>
- <549CB8C4.3040601@gmx.de>
-User-Agent: slrn/1.0.1 (Linux)
-Mime-Version: 1.0
-Content-Type: text/plain; charset=UTF-8
-Content-Transfer-Encoding: 8bit
-Message-ID: <slrnm9qask.feu.mcepl@mitmanek.ceplovi.cz>
-Path: mitmanek.ceplovi.cz!not-for-mail
-Date: Fri, 26 Dec 2014 10:34:12 +0100
-
-On 2014-12-26, 01:24 GMT, Matthias Andree wrote:
->> in /etc/leafnode/config. Which is what I do right now with sed
->> in the Fedora/RHEL package, which makes it work.
->>
->> Yuhoooo!
->
-> Well, yes. Where do you propose do I need to update the documentation
-> and commentary? What else should I consider changing to make things easier?
-
-Well, the minimal option is 'do nothing'. I will use this in my
-packages (and I guess Doug will do the same in his Mageia
-packages), so it should just work for users.
-
-The second step would be to add some comment of the situation to
-INSTALL for anybody who wants to build leafnode-2 in some
-building system (e.g., I think the same issue will happen for
-all distributions, like OpenSUSE with OBS, and Debian or Ubuntu
-with whatever they use now).
-
-The third step could be to add some error handler, so that when
-leafnode-2 tries to run as a user which doesn't exist on the
-system (or whatever is the root cause of the error message
-I have shown), leafnode would mention in stderr, that probably
-run_as_user should be set explicitly in /etc/leafnode/config.
-
-Also, I would have another question: what happens when the
---enable-runas-user is not set in ./configure at all? Is
-leafnode then able to run only as a root? Or could I still make
-it run as a user 'news' with run_as_user in
-/etc/leafnode/config? Just that the construct
---enable-runas-user=$(id -un) irritates my engineering soul as
-not having much sense (because obviously we don’t want to run
-leafnode as whatever user is used in our build system). But
-that's just aestethics. I can happily live with it.
-
-Anyway, thank you very much for your years of maintaining this.
-May I gently ask what are your plans for release? Do you really
-still expect some drastic changes which would require leafnode-2
-to be in Alpha? Isn't it time to really close the history of
-leafnode-1 and work for its replacement in all major
-distributions with leafnode-2? Just asking. If you decide not to
-for whatever reason, I will happily maintain leafnode-2 in my
-copr repo (or eventually perhaps even work on it replacing
-leafnode-1 in Fedora, for that I would have to make sure we have
-a good upgrade path from leafnode-1).
-
-Best,
-
-Matěj
diff --git a/examples/leafnode-investigation/posting-of-new-message-in-slrn.diff b/examples/leafnode-investigation/posting-of-new-message-in-slrn.diff
deleted file mode 100644
index 0220d27..0000000
--- a/examples/leafnode-investigation/posting-of-new-message-in-slrn.diff
+++ /dev/null
@@ -1,49 +0,0 @@
---- before 2014-12-26 10:34:09.742471840 +0100
-+++ after 2014-12-26 10:34:18.085431544 +0100
-@@ -8,15 +8,15 @@
- drwxrwsr-x. 7 news news 72 Dec 26 01:03 grc
- drwxrwsr-x. 2 news news 6 Dec 25 21:51 in.coming
- drwxrwsr-x. 2 news news 4096 Dec 26 01:03 interesting.groups
--drwxr-sr-x. 2 news news 4096 Dec 26 10:12 leaf.node
-+drwxr-sr-x. 2 news news 4096 Dec 26 10:34 leaf.node
- drwxrws---. 3 news news 17 Dec 25 21:51 local
- drwxrwsr-x. 1002 news news 16384 Dec 25 21:42 message.id
- drwxr-s---. 3 news news 19 Dec 26 01:03 microsoft
- drwxrwsr-x. 6 news news 58 Dec 26 01:03 mozilla
- drwxrwsr-x. 3 news news 21 Dec 25 21:42 news
--drwxrwsr-x. 2 news news 6 Dec 26 00:35 out.going
-+drwxrwsr-x. 2 news news 31 Dec 26 10:34 out.going
- drwxrwsr-x. 3 news news 17 Dec 25 21:42 rec
--drwxrwsr-x. 2 news news 6 Dec 26 10:12 temp.files
-+drwxrwsr-x. 2 news news 6 Dec 26 10:34 temp.files
-
- /var/spool/news/cz:
- total 0
-@@ -808,7 +808,7 @@
- /var/spool/news/leaf.node:
- total 5240
- -rw-------. 1 news news 0 Dec 25 23:45 :active.read
---rw-rw----. 1 news news 5338650 Dec 26 10:12 groupinfo
-+-rw-rw----. 1 news news 5338650 Dec 26 10:34 groupinfo
- -rw-rwSr--. 1 news news 0 Dec 25 23:45 last:news.gmane.org:0
- -rw-rwSr--. 1 news news 0 Dec 25 23:45 last:news.grc.com:0
- -rw-rwSr--. 1 news news 0 Dec 25 23:45 last:news.mozilla.org:0
-@@ -833,7 +833,7 @@
- total 16
- drwxrwsr-x. 2 news news 6 Dec 25 21:42 000
- drwxrwsr-x. 2 news news 6 Dec 25 21:42 001
--drwxrwsr-x. 2 news news 6 Dec 25 21:42 002
-+drwxrwsr-x. 2 news news 6 Dec 26 10:34 002
- drwxrwsr-x. 2 news news 6 Dec 25 21:42 003
- drwxrwsr-x. 2 news news 6 Dec 25 21:42 004
- drwxrwsr-x. 2 news news 6 Dec 25 21:42 005
-@@ -5027,7 +5027,8 @@
- -rw-rw----. 2 news news 1718 Dec 26 00:35 2
-
- /var/spool/news/out.going:
--total 0
-+total 4
-+-r--r-----. 1 news news 2839 Dec 26 10:34 18034-1419586452-1
-
- /var/spool/news/rec:
- total 0
diff --git a/whitelist.py b/whitelist.py
index d76af85..cd0906c 100644
--- a/whitelist.py
+++ b/whitelist.py
@@ -12,10 +12,9 @@ Thanks to md for this useful formula. Beer is beer.
whitelist manage a list of trusted user.
"""
+import logging
import sys
import time
-import fcntl
-
import wlp
@@ -24,48 +23,33 @@ class whitelist(object):
Do you really want anyone can post? Ah ah ah.
"""
-
wl = {}
- logf = None # filedescriptor
# constants
DENY = 0
ACCEPT = 1
def __init__(self, wlfile='wl.pyg', logfile='pyg.log'):
+ self.logger = logging.getLogger(__name__)
+ self.logger.setLevel(logging.INFO)
+ log_fh = logging.FileHandler(logfile)
+ log_fmt = logging.Formatter(
+ '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+ log_fh.setFormatter(log_fmt)
+ self.logger.addHandler(log_fh)
try:
wlp.setfilebyname(wlfile)
- except (Exception), (errno, message):
- print 'Opening %s: %s (errno %d)' % (wlfile, message, errno)
- sys.exit(0)
+ except Exception as ex:
+ self.logger.exception('Opening %s: %s', wlfile, ex)
+ sys.exit(1)
# dict is a { ownername : {variable: value}} dictionary of dictionaries
self.wl = wlp.mkdict()
-# print 'owner: option = value'
-# for owner in self.wl.keys():
-# for option in self.wl[owner].keys():
-# print '%s: %s = %s' % (owner,option,self.wl[owner][option])
-
- try:
- self.logf = open(logfile, 'a')
- self.lock()
- except (Exception), message:
- print '%s\nAre you authorized to use this program? ' % message
- sys.exit(1)
-
- def lock(self):
- fcntl.flock(self.logf.fileno(), fcntl.LOCK_EX)
-
- # to unlock fd locked, usually fd are unlocked after process exit()
- def unlock(self):
- fcntl.flock(self.logf.fileno(), fcntl.LOCK_UN)
-
def checkfrom(self, fromhead):
"""have you permission to be here, sir?"""
for owner in self.wl.keys():
-# if(self.wl[owner]['From'] == fromhead[:-1]): # remove '\n'
# here colon after 'From' IS required, because binary module wl
# expects it.
# TODO: when switching to the python lexxing, remove this
@@ -75,16 +59,8 @@ class whitelist(object):
else:
return None
- def log(self, string):
- """Captain Diary, Astral Date 962555394 from epoch.
- it rawly write a line in logfile. Remeber to indent it, if you
- like.
- """
- self.logf.write(string + '\n')
-
def logmsg(self, heads, ok=DENY, owner=None):
"""who are walking through my gate?
- log
"""
ltime = time.ctime(time.time())
@@ -95,33 +71,31 @@ class whitelist(object):
tzone = time.tzname[0]
if ok == self.ACCEPT:
- self.logf.write('Permission Accorded ')
+ self.logger.info('Permission Accorded ')
else:
- self.logf.write('Permission Denied ')
+ self.logger.info('Permission Denied ')
- self.logf.write('at %s (%s)\n' % (ltime, tzone))
+ self.logger.info('at %s (%s)', ltime, tzone)
if owner is not None:
- self.logf.write('\tWLOwner: ' + owner + '\n')
- self.logf.write('\tFrom: ' + heads.get('From', 'NOT PRESENT\n'))
- self.logf.write('\tSubject: ' + heads.get('Subject', 'NOT PRESENT\n'))
- self.logf.write('\tSender: ' + heads.get('Sender', 'NOT PRESENT\n'))
- self.logf.write('\tDate: ' + heads.get('Date', 'NOT PRESENT\n'))
+ self.logger.info('\tWLOwner: ' + owner + '')
+ self.logger.info('\tFrom: ' + heads.get('From', 'NOT PRESENT'))
+ self.logger.info('\tSubject: ' + heads.get('Subject', 'NOT PRESENT'))
+ self.logger.info('\tSender: ' + heads.get('Sender', 'NOT PRESENT'))
+ self.logger.info('\tDate: ' + heads.get('Date', 'NOT PRESENT'))
# some client create Message-Id other Message-ID.
if 'Message-ID' in heads:
- self.logf.write('\tMessage-ID: ' + heads.get('Message-ID'))
+ self.logger.info('\tMessage-ID: ' + heads.get('Message-ID'))
else:
- self.logf.write('\tMessage-Id: ' + heads.get('Message-Id',
- 'NOT PRESENT\n'))
+ self.logger.info('\tMessage-Id: ' + heads.get('Message-Id',
+ 'NOT PRESENT'))
# X-Newsgroups: and To: are present if user is trusted, else
# Newsgroup: exists since no changes on nntp headers are done.
if 'X-Newsgroups' in heads:
- self.logf.write('\tTo: ' + heads.get('To', 'NOT PRESENT\n'))
- self.logf.write('\tX-Newsgroups: ' + heads.get('X-Newsgroups',
- 'NOT PRESENT\n'))
+ self.logger.info('\tTo: ' + heads.get('To', 'NOT PRESENT'))
+ self.logger.info('\tX-Newsgroups: ' + heads.get('X-Newsgroups',
+ 'NOT PRESENT'))
else:
- self.logf.write('\tNewsgroups: ' +
- heads.get('Newsgroups', 'NOT PRESENT\n'))
-
- self.logf.write('\n')
+ self.logger.info('\tNewsgroups: ' +
+ heads.get('Newsgroups', 'NOT PRESENT'))