Implemented authentication modules, and added 2 modules: allowall and simplefile
[bitcoin:eloipool.git] / authentication / simplefile.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2013  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 # Written by Peter Leurs <kinlo@triplemining.com>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 import logging
20 import threading
21 import traceback
22 from time import sleep
23 import os
24
25 _logger = logging.getLogger('authentication.simplefile')
26
27 class simplefile(threading.Thread):
28         def __init__(self, filename, **ka):
29             super().__init__(**ka.get('thropts', {}))
30             self.fn=filename
31             self.userdb = dict()
32             self.lastmodified = os.path.getmtime(self.fn)
33             self.reloadDb()
34             self.start()
35
36         def run(self):
37             while True:
38                 try:
39                         sleep(0.2)
40                         if self.lastmodified!=os.path.getmtime(self.fn):
41                                 self.lastmodified = os.path.getmtime(self.fn)
42                                 sleep(0.2)
43                                 self.reloadDb()
44                 except:
45                         _logger.critical(traceback.format_exc())
46
47         def reloadDb(self):
48             try:
49                 newdb = dict()
50                 fh = open(self.fn, "rb")
51                 data = fh.read()
52                 for line in data.split(b'\n'):
53                         (user, passwd) = line.split(b'\t')
54                         newdb[user.decode('utf8')]=passwd.decode('utf8')
55                 self.userdb = newdb
56                 _logger.info("Reloaded user db")
57             except:
58                 _logger.critical("fatal error reading userdatabase: %s", traceback.format_exc())
59
60         def checkAuthentication(self, user, password):
61                 if user not in self.userdb:
62                         return False
63                 if self.userdb[user] == password:
64                         return True
65                 return False
66
67