chiark / gitweb /
Commit 2.4.5-5 as unpacked
[innduct.git] / samples / nnrpd_dynamic.py
1 ##  $Id: nnrpd_dynamic.py 7906 2008-06-23 05:44:49Z iulius $
2 ##
3 ##  This is a sample dynamic 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_dynamic: parameter in readers.conf is used to load this script.
8 ##
9 ##  An instance of DYNACCESS 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 ##  dynamic_init()              - Init function specific to
17 ##                                authentication.  May be omitted.
18 ##  dynamic(attributes)         - Called whenever a reader requests either
19 ##                                read or post access to a
20 ##                                newsgroup.  Returns None to grant
21 ##                                access, or a non-empty string (which
22 ##                                will be reported back to reader)
23 ##                                otherwise.
24 ##  dynamic_close()             - Called on nnrpd termination.  Save
25 ##                                your state variables or close a database
26 ##                                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 dynamic access class.  It defines all dynamic access methods known
41 ##  to nnrpd.
42 class DYNACCESS:
43     """Provide dynamic access callbacks to nnrpd."""
44
45     def __init__(self):
46         """This is a good place to initialize variables or open a
47            database connection."""
48         syslog('notice', 'nnrpd dynamic access class instance created')
49
50     def dynamic_init(self):
51         """Called when this script is initialized."""
52         pass
53
54     def dynamic(self, attributes):
55         """Called when python_dynamic: is reached in the processing of
56            readers.conf and a reader requests either read or post
57            permission for particular newsgroup."""
58
59         # Just for debugging purposes.
60         syslog('notice', 'n_a dynamic() invoked against type %s, hostname %s, ipaddress %s, interface %s, user %s' % (\
61                 attributes['type'], \
62                 attributes['hostname'], \
63                 attributes['ipaddress'], \
64                 attributes['interface'], \
65                 attributes['user']))
66
67         # Allow reading of any newsgroup but not posting.
68         if 'post' == str(attributes['type']):
69             syslog('notice', 'dynamic authorization access for post access denied')
70             return "no posting for you"
71         elif 'read' == str(attributes['type']):
72             syslog('notice', 'dynamic authorization access for read access granted')
73             return None
74         else:
75             syslog('notice', 'dynamic authorization access type is not known: %s' % attributes['type'])
76             return "Internal error";
77
78     def dynamic_close(self):
79         """Called on nnrpd termination."""
80         pass
81
82
83 ##  The rest is used to hook up the dynamic access module on nnrpd.  It is unlikely
84 ##  you will ever need to modify this.
85
86 ##  Import functions exposed by nnrpd.  This import must succeed, or nothing
87 ##  will work!
88 from nnrpd import *
89
90 ##  Create a class instance.
91 mydynaccess = DYNACCESS()
92
93 ##  ...and try to hook up on nnrpd.  This would make auth object methods visible
94 ##  to nnrpd.
95 try:
96     set_auth_hook(mydynaccess)
97     syslog('notice', "dynamic access module successfully hooked into nnrpd")
98 except Exception, errmsg:
99     syslog('error', "Cannot obtain nnrpd hook for dynamic access method: %s" % errmsg[0])