chiark / gitweb /
debugging for thing that crashed
[innduct.git] / samples / nnrpd_auth.py
1 ##  $Id: nnrpd_auth.py 7906 2008-06-23 05:44:49Z iulius $
2 ##
3 ##  This is a sample authentication module for the Python nnrpd hook.
4 ##
5 ##  See the INN Python Filtering and Authentication Hooks documentation
6 ##  for more information.
7 ##  The perl_auth: parameter in readers.conf is used to load this script.
8 ##
9 ##  An instance of AUTH 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 ##  authen_init()               - Init function specific to
17 ##                                authentication.  May be omitted.
18 ##  authenticate(attributes)    - Called when a python_auth statement
19 ##                                is reached in the processing of
20 ##                                readers.conf.  Returns a response
21 ##                                code, an error string and an
22 ##                                optional string to appear in the
23 ##                                logs as the username.
24 ##  authen_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 authentication class.  It defines all auth methods known to nnrpd.
41 class AUTH:
42     """Provide authentication callbacks to nnrpd."""
43
44     def __init__(self):
45         """This is a good place to initialize variables or open a
46            database connection."""
47
48         # Create a list of NNTP codes to respond on connect.
49         self.connectcodes = {   'READPOST':200,
50                                 'READ':201,
51                                 'AUTHNEEDED':480,
52                                 'PERMDENIED':502
53         }
54
55         # Create a list of NNTP codes to respond on authentication.
56         self.authcodes = {  'ALLOWED':281,
57                             'DENIED':502
58         }
59
60         syslog('notice', 'nnrpd authentication class instance created')
61
62     def authen_init(self):
63         """Called when this script is initialized."""
64         pass
65
66     def authenticate(self, attributes):
67         """Called when python_auth: is encountered in readers.conf."""
68
69         # Just for debugging purposes.
70         syslog('notice', 'n_a authenticate() invoked: hostname %s, ipaddress %s, interface %s, user %s' % (\
71                 attributes['hostname'], \
72                 attributes['ipaddress'], \
73                 attributes['interface'], \
74                 attributes['user']))
75
76         # Do username password authentication.
77         if 'foo' == str(attributes['user'])  \
78           and 'foo' == str(attributes['pass']):
79             syslog('notice', 'authentication by username succeeded')
80             return (self.authcodes['ALLOWED'], 'No error', 'default_user')
81         else:
82             syslog('notice', 'authentication by username failed')
83             return (self.authcodes['DENIED'], 'Access Denied!')
84
85     def authen_close(self):
86         """Called on nnrpd termination."""
87         pass
88
89
90 ##  The rest is used to hook up the auth module on nnrpd.  It is unlikely
91 ##  you will ever need to modify this.
92
93 ##  Import functions exposed by nnrpd.  This import must succeed, or nothing
94 ##  will work!
95 from nnrpd import *
96
97 ##  Create a class instance.
98 myauth = AUTH()
99
100 ##  ...and try to hook up on nnrpd.  This would make auth object methods visible
101 ##  to nnrpd.
102 try:
103     set_auth_hook(myauth)
104     syslog('notice', "authentication module successfully hooked into nnrpd")
105 except Exception, errmsg:
106     syslog('error', "Cannot obtain nnrpd hook for authentication method: %s" % errmsg[0])