1 ## $Id: nnrpd_auth.py 7906 2008-06-23 05:44:49Z iulius $
3 ## This is a sample authentication module for the Python nnrpd hook.
5 ## See the INN Python Filtering and Authentication Hooks documentation
6 ## for more information.
7 ## The perl_auth: parameter in readers.conf is used to load this script.
9 ## An instance of AUTH class is passed to nnrpd via the set_auth_hook()
10 ## function imported from nnrpd. The following methods of that class
11 ## are known to nnrpd:
13 ## __init__() - Use this method to initialize your
14 ## general variables or open a common
15 ## database connection. May be omitted.
16 ## authen_init() - Init function specific to
17 ## authentication. May be omitted.
18 ## authenticate(attributes) - Called when a python_auth statement
19 ## is reached in the processing of
20 ## readers.conf. Returns a response
21 ## code, an error string and an
22 ## optional string to appear in the
23 ## logs as the username.
24 ## authen_close() - Called on nnrpd termination. Save
25 ## your state variables or close a database
26 ## connection. May be omitted.
28 ## If there is a problem with return codes from any of these methods, then nnrpd
29 ## will die and syslog the exact reason.
31 ## There are also a few Python functions defined in nnrpd:
33 ## set_auth_hook() - Called by nnrpd as this module is loaded.
34 ## It is used to pass a reference to an
35 ## instance of authentication class to nnrpd.
36 ## syslog() - An equivalent replacement for regular syslog.
37 ## One consideration for using it is to
38 ## uniform nnrpd logging.
40 ## Sample authentication class. It defines all auth methods known to nnrpd.
42 """Provide authentication callbacks to nnrpd."""
45 """This is a good place to initialize variables or open a
46 database connection."""
48 # Create a list of NNTP codes to respond on connect.
49 self.connectcodes = { 'READPOST':200,
55 # Create a list of NNTP codes to respond on authentication.
56 self.authcodes = { 'ALLOWED':281,
60 syslog('notice', 'nnrpd authentication class instance created')
62 def authen_init(self):
63 """Called when this script is initialized."""
66 def authenticate(self, attributes):
67 """Called when python_auth: is encountered in readers.conf."""
69 # Just for debugging purposes.
70 syslog('notice', 'n_a authenticate() invoked: hostname %s, ipaddress %s, interface %s, user %s' % (\
71 attributes['hostname'], \
72 attributes['ipaddress'], \
73 attributes['interface'], \
76 # Do username password authentication.
77 if 'foo' == str(attributes['user']) \
78 and 'foo' == str(attributes['pass']):
79 syslog('notice', 'authentication by username succeeded')
80 return (self.authcodes['ALLOWED'], 'No error', 'default_user')
82 syslog('notice', 'authentication by username failed')
83 return (self.authcodes['DENIED'], 'Access Denied!')
85 def authen_close(self):
86 """Called on nnrpd termination."""
90 ## The rest is used to hook up the auth module on nnrpd. It is unlikely
91 ## you will ever need to modify this.
93 ## Import functions exposed by nnrpd. This import must succeed, or nothing
97 ## Create a class instance.
100 ## ...and try to hook up on nnrpd. This would make auth object methods visible
103 set_auth_hook(myauth)
104 syslog('notice', "authentication module successfully hooked into nnrpd")
105 except Exception, errmsg:
106 syslog('error', "Cannot obtain nnrpd hook for authentication method: %s" % errmsg[0])