source: TI05-delivery/trunk/lib/python/delivery/server.py @ 1144

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/lib/python/delivery/server.py@1144
Revision 1144, 4.2 KB checked in by spascoe, 14 years ago (diff)

Authorisation in python is confirmed as working with 2 test cases.

RevLine 
[1133]1"""
2Interface to the BBftp server.
3
4@author Stephen Pascoe
5"""
6
7import bbftpd
8
[1141]9def start(authHandler, args):
[1133]10    """Start the bbftp server.
11
12    The server performs a fork() during initialisation, the child process remains in
13    the server's main loop and the parent returns from this function.  The server process
[1141]14    calls authHandler.authorise() on each connection to do authentication/authorisation.
[1133]15
16    @note: because the server process forks, authHandler will not see any changes to the python
17        interpreter following the call to start().
18
[1141]19    @param authHandler: an instance of AuthHandler.
20    @param args: a list of command line arguments.
[1133]21    @return: the PID of the server process.
22    """
23   
[1141]24    return bbftpd.run(authHandler, args)
[1133]25
26
[1144]27class AuthenticationFailure(Exception):
28    """Signals authentication failed.
29    """
30    pass
31
32class AuthorisationFailure(Exception):
33    """Signals authorisation failed.
34    """
35    pass
36
37
38
[1133]39class AuthHandler(object):
40    """Abstract base class for implementing authentication.
41
42    Subclasses should implement the authorise() method to authenticate clients connecting to
43    the server.  An AuthHandler object is passed to start() when initialising the server.
44    """
45   
46    def send(self, buffer):
47        """Send a message to the client.
48
49        @param buffer a string containing the message to send.
50        """
51   
52        return bbftpd.send(buffer)
53
54    def recv(self):
55        """Receive a message from the client.
56
57        @return a string containing the message received.
58        """
59
60        return bbftpd.recv()
61
62    def authorise(self):
63        """Authenticate a connection.
64
65        This function should be overridden in subclasses to implement authentication using
66        the method calls self.send() and send.recv().
67
68        @note because the server process forks on each connection, calls to authorise() will
69            see the state of the python interpreter as it was when start() was called.
70
[1144]71        @return an AuthzHandler instance if authentication succeeds.  Any false value is considered
72            an authorisation failure.
[1133]73        """
74
75        raise NotImplementedError
76
77
78class AuthzHandler(object):
79    """Abstract base class for implementing authorisation.
[1141]80
81    @ivar username the client's username
[1133]82    """
83
84    def authzControl(self, msgcode, transferoption, path):
85        """Authorise a control command.
86
87        @param msgcode: the command type.
88        @param transferoption: extra options specified in the command.
89        @param path: the file or directory to which the command applies.
[1144]90        @return: bool for success or failure.
[1133]91        """
92
93        raise NotImplementedError
94
95    def authzRetr(self, path):
96        """Authorise a retrieve request.
97
98        @param path: the file being retrieved.
[1144]99        @return: bool for success or failure.
[1133]100        """
101
102        raise NotImplementedError
103
[1141]104    def authzStore(self, path):
105        """Authorise a store request.
[1133]106
107        @param path: the destination file.
[1144]108        @return: bool for success or failure.
[1133]109        """
110
[1141]111        raise NotImplementedError
112   
113    def _raiseNoUsername(self):
114        raise ValueError, "No username has been set"
115    username = property(_raiseNoUsername)
[1133]116
[1141]117#--------------------------------------------------------------------------------------------------------------
[1133]118
119class BasicClientAuthHandler(AuthHandler):
120    """
121    When testing with the basic bbftp client some messages will be NULL terminated.  This class
122    makes communicating with such clients slightly easier.
123    """
124
125    def recvCStr(self):
126        """Receive a message from the client which may contain \\0 characters indicating message end.
127
128        The message is truncated to the first \\0 character.
129
130        @return a string containing the message received.
131        """
132
133        msg = self.recv()
134        # Trim to first '\0'
135        x = msg.find('\0')
136        if x:
137            msg = msg[:x]
138
139        return msg
140
[1141]141
142
143class LiberalAuthzHandler(AuthzHandler):
144    """Allow everything.
145    """
146
147    username = None
148
149    def __init__(self, username):
150        self.username = username
151
152    def authzControl(self, m, t, p):
[1144]153        return True;
[1141]154
155    def authzRetr(self, p):
[1144]156        return True;
[1141]157
158    def authzStore(self, p):
[1144]159        return True;
[1141]160   
Note: See TracBrowser for help on using the repository browser.