1##  $Id: nnrpd_dynamic_wrapper.py 10282 2018-05-14 12:42:14Z iulius $
2##
3##  Example wrapper for support of old Python authentication scripts,
4##  by Erik Klavon.
5##
6##  This file contains a sample Python script which can be used to
7##  duplicate the behaviour of the old nnrppythonauth functionality.
8##  This script only supports dynamic access control by group.
9##
10##  How to use this wrapper:
11##    - insert your authentication class into this file;
12##    - rename your authentication class OLDAUTH.
13##
14##  See the INN Python Filtering and Authentication Hooks documentation
15##  for more information.
16##  The use of this file is *discouraged*.
17
18##  Old AUTH class.
19##  Insert your old auth class here.
20##  Do not include the code which sets the hook.
21
22
23
24
25##  Wrapper DYNACCESS class.  It creates an instance of the old class and
26##  calls its methods.  Arguments and return values are munged as
27##  needed to fit the new way of doing things.
28
29class MYDYNACCESS:
30    """Provide dynamic access callbacks to nnrpd."""
31    def dynamic_init(self):
32        self.old = OLDAUTH()
33
34    def dynamic(self, attributes):
35        return (self.old).authorize(attributes)
36
37    def dynamic_close(self):
38        (self.old).close()
39
40
41##  The rest is used to hook up the dynamic access module on nnrpd.
42##  It is unlikely you will ever need to modify this.
43
44##  Import functions exposed by nnrpd.  This import must succeed, or nothing
45##  will work!
46from nnrpd import *
47
48##  Create a class instance.
49mydynaccess = MYDYNACCESS()
50
51##  ...and try to hook up on nnrpd.  This would make auth object methods visible
52##  to nnrpd.
53import sys
54try:
55    set_auth_hook(mydynaccess)
56    syslog('notice', "dynamic access module successfully hooked into nnrpd")
57except Exception: # Syntax valid in both Python 2.x and 3.x.
58    e = sys.exc_info()[1]
59    syslog('error', "Cannot obtain nnrpd hook for dynamic access method: %s" % e.args[0])
60