1 ## $Id: nnrpd_access.py 7906 2008-06-23 05:44:49Z iulius $
3 ## This is a sample access module for the Python nnrpd hook.
5 ## See the INN Python Filtering and Authentication Hooks documentation
6 ## for more information.
7 ## The perl_access: parameter in readers.conf is used to load this script.
9 ## An instance of ACCESS 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 ## access_init() - Init function specific to access
17 ## control. May be omitted.
18 ## access(attributes) - Called when a python_access
19 ## statement is reached in the
20 ## processing of readers.conf. Returns
21 ## a dictionary of values representing
22 ## statements to be included in an
24 ## access_close() - Called on nnrpd termination. Save
25 ## your state variables or close a
26 ## database 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 access class. It defines all access methods known to nnrpd.
42 """Provide access callbacks to nnrpd."""
45 """This is a good place to initialize variables or open a
46 database connection."""
47 syslog('notice', 'nnrpd access class instance created')
49 def access_init(self):
50 """Called when this script is initialized."""
53 def access(self, attributes):
54 """Called when python_access: is encountered in readers.conf."""
56 # Just for debugging purposes.
57 syslog('notice', 'n_a access() invoked: hostname %s, ipaddress %s, interface %s, user %s' % (\
58 attributes['hostname'], \
59 attributes['ipaddress'], \
60 attributes['interface'], \
63 # Allow newsreading from specific host only.
64 if '127.0.0.1' == str(attributes['ipaddress']):
65 syslog('notice', 'authentication access by IP address succeeded')
66 return {'read':'*', 'post':'*'}
68 syslog('notice', 'authentication access by IP address failed')
69 return {'read':'!*', 'post':'!*'}
71 def access_close(self):
72 """Called on nnrpd termination."""
76 ## The rest is used to hook up the access module on nnrpd. It is unlikely
77 ## you will ever need to modify this.
79 ## Import functions exposed by nnrpd. This import must succeed, or nothing
83 ## Create a class instance.
86 ## ...and try to hook up on nnrpd. This would make auth object methods visible
89 set_auth_hook(myaccess)
90 syslog('notice', "access module successfully hooked into nnrpd")
91 except Exception, errmsg:
92 syslog('error', "Cannot obtain nnrpd hook for access method: %s" % errmsg[0])