chiark / gitweb /
better stats for missing
[inn-innduct.git] / samples / nnrpd_access.py
1 ##  $Id: nnrpd_access.py 7906 2008-06-23 05:44:49Z iulius $
2 ##
3 ##  This is a sample access module for the Python nnrpd hook.
4 ##
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.
8 ##
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:
12 ##
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
23 ##                                access group.
24 ##  access_close()              - Called on nnrpd termination.  Save
25 ##                                your state variables or close a
26 ##                                database connection.  May be omitted.
27 ##
28 ##  If there is a problem with return codes from any of these methods, then nnrpd
29 ##  will die and syslog the exact reason.
30 ##
31 ##  There are also a few Python functions defined in nnrpd:
32 ##
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.
39
40 ##  Sample access class.  It defines all access methods known to nnrpd.
41 class ACCESS:
42     """Provide access callbacks to nnrpd."""
43
44     def __init__(self):
45         """This is a good place to initialize variables or open a
46            database connection."""
47         syslog('notice', 'nnrpd access class instance created')
48
49     def access_init(self):
50         """Called when this script is initialized."""
51         pass
52
53     def access(self, attributes):
54         """Called when python_access: is encountered in readers.conf."""
55
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'], \
61                 attributes['user']))
62
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':'*'}
67         else:
68             syslog('notice', 'authentication access by IP address failed')
69             return {'read':'!*', 'post':'!*'}
70
71     def access_close(self):
72         """Called on nnrpd termination."""
73         pass
74
75
76 ##  The rest is used to hook up the access module on nnrpd.  It is unlikely
77 ##  you will ever need to modify this.
78
79 ##  Import functions exposed by nnrpd.  This import must succeed, or nothing
80 ##  will work!
81 from nnrpd import *
82
83 ##  Create a class instance.
84 myaccess = ACCESS()
85
86 ##  ...and try to hook up on nnrpd.  This would make auth object methods visible
87 ##  to nnrpd.
88 try:
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])