1##  $Id: nnrpd_access_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 access control.
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 ACCESS 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 MYACCESS:
30    """Provide access callbacks to nnrpd."""
31    def access_init(self):
32        self.old = OLDAUTH()
33
34    def access(self, attributes):
35        # Python 3.x uses memoryview(b'connect') because buffers
36        # do not exist any longer.  Note that the argument is
37        # a bytes object.
38        #  attributes['type'] = memoryview(b'connect')
39        #  perm = (self.old).authenticate(attributes)
40        # whereas in Python 2.x:
41        #  attributes['type'] = buffer('connect')
42        #  perm = (self.old).authenticate(attributes)
43        result = dict({'users': '*'})
44        #if perm[1] == 1:
45        #    result['read'] = perm[3]
46        #if perm[2] == 1:
47        #    result['post'] = perm[3]
48        return result
49
50    def access_close(self):
51        (self.old).close()
52
53
54##  The rest is used to hook up the access module on nnrpd.  It is unlikely
55##  you will ever need to modify this.
56
57##  Import functions exposed by nnrpd.  This import must succeed, or nothing
58##  will work!
59from nnrpd import *
60
61##  Create a class instance.
62myaccess = MYACCESS()
63
64##  ...and try to hook up on nnrpd.  This would make access object methods
65##  visible to nnrpd.
66import sys
67try:
68    set_auth_hook(myaccess)
69    syslog('notice', "access module successfully hooked into nnrpd")
70except Exception: # Syntax valid in both Python 2.x and 3.x.
71    e = sys.exc_info()[1]
72    syslog('error', "Cannot obtain nnrpd hook for access method: %s" % e.args[0])
73