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

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

Defined constants used by bbftp in delivery.server.

Line 
1"""
2Start a bbftp server process and define python callbacks for authentication and authorisation.
3
4Interface to the BBftp server.
5
6@copyright Copyright (C) 2006 CCLRC & NERC
7@license This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
8
9@author Stephen Pascoe
10"""
11
12import bbftpd
13
14class TROPT:
15    """Static class containing constants used by bbftp in the transferoption parameter.
16
17    TROPT.X always corresponds to TROPT_X as defined in bbftpd/structures.h.
18    Options that aren't likely to be used for the delivery service are not
19    included (e.g. TROPT_RFIO).
20
21    @cvar TMP always use temporary name.
22    @cvar ACC keep access and modification time.
23    @cvar MODE keep mode.
24    @cvar DIR create all dirs in path.
25    """
26    TMP = bbftpd.TROPT_TMP
27    ACC = bbftpd.TROPT_ACC
28    MODE = bbftpd.TROPT_MODE
29    DIR = bbftpd.TROPT_DIR
30
31class MSG:
32    """Static class containing message codes used by bbftp.
33
34    MSG.X corresponds to MSG_X or MSG_X_V2 as defined in bbftpd/structures.h.
35    Message codes not returned by the auth/authz interface are not included.
36
37    @cvar CHDIR a chdir request.
38    @cvar LIST a ls/dir request.
39    @cvar MKDIR a mkdir request.
40    @cvar RM a rm request.
41    @cvar STAT a stat request.
42    @cvar DF a statfs request.
43    """
44    CHDIR = bbftpd.MSG_CHDIR_V2
45    LIST = bbftpd.MSG_LIST_V2
46    MKDIR = bbftpd.MSG_MKDIR_V2
47    RM = bbftpd.MSG_RM
48    STAT = bbftpd.MSG_STAT
49    DF = bbftpd.MSG_DF
50   
51
52#--------------------------------------------------------------------------------------------------------------
53
54def start(authHandler, args):
55    """Start the bbftp server.
56
57    The server performs a fork() during initialisation, the child process remains in
58    the server's main loop and the parent returns from this function.  The server process
59    calls authHandler.authorise() on each connection to do authentication/authorisation.
60
61    @note because the server process forks, authHandler will not see any changes to the python
62        interpreter following the call to start().
63
64    @param authHandler an instance of AuthHandler.
65    @param args a list of command line arguments.
66    @return the PID of the server process.
67    """
68   
69    return bbftpd.run(authHandler, args)
70
71
72class AuthenticationFailure(Exception):
73    """Signals authentication failed.
74    """
75    pass
76
77class AuthorisationFailure(Exception):
78    """Signals authorisation failed.
79    """
80    pass
81
82
83
84class AuthHandler(object):
85    """Abstract base class for implementing authentication.
86
87    Subclasses should implement the authorise() method to authenticate clients connecting to
88    the server.  An AuthHandler object is passed to start() when initialising the server.
89    """
90   
91    def send(self, buffer):
92        """Send a message to the client during authorisation.
93
94        This method must only be used from within self.authorise() which has been called
95        from the bbftp server process started by start().
96
97        @param buffer a string containing the message to send.
98        """
99   
100        return bbftpd.send(buffer)
101
102    def recv(self):
103        """Receive a message from the client during authorisation.
104
105        This method must only be used from within self.authorise() which has been called
106        from the bbftp server process started by start().
107
108        @return a string containing the message received.
109        """
110
111        return bbftpd.recv()
112
113    def authenticate(self):
114        """Authenticate a connection.
115
116        This function should be overridden in subclasses to implement authentication using
117        the method calls self.send() and send.recv().  It is called by the bbftp server process
118        created using the start() function.
119
120        @note because the server process forks on each connection, calls to authorise() will
121            see the state of the python interpreter as it was when start() was called.
122
123        @return an AuthzHandler instance if authentication succeeds.  Any false value is considered
124            an authorisation failure.
125        @raise AuthorisationFailure the preferred way of signaling authorisation failure to
126            the bbftp server process.
127        """
128
129        raise NotImplementedError
130
131
132class AuthzHandler(object):
133    """Abstract base class for implementing authorisation.
134
135    @ivar username: the client's username.  This attribute must be set before an instance of AuthzHandler
136        is returned to the bbftp server process by AuthHandler.authenticate().
137       
138    """
139
140    def authzControl(self, msgcode, transferoption, path):
141        """Authorise a control command.
142
143        @param msgcode the command type.
144        @param transferoption extra options specified in the command.
145        @param path the file or directory to which the command applies.
146        @return bool for success or failure.
147        """
148
149        raise NotImplementedError
150
151    def authzRetr(self, path):
152        """Authorise a retrieve request.
153
154        @param path the file being retrieved.
155        @return bool for success or failure.
156        """
157
158        raise NotImplementedError
159
160    def authzStore(self, path):
161        """Authorise a store request.
162
163        @param path the destination file.
164        @return bool for success or failure.
165        """
166
167        raise NotImplementedError
168   
169
170#--------------------------------------------------------------------------------------------------------------
171
172class BasicClientAuthHandler(AuthHandler):
173    """
174    This class makes communicating with the stand alone bbftp client slightly easier.
175    Mainly for use during testing.
176    """
177
178    def recvCStr(self):
179        """Receive a message from the client which may contain \\0 characters indicating message end.
180
181        The message is truncated to the first \\0 character.
182
183        @return a string containing the message received.
184        """
185
186        msg = self.recv()
187        # Trim to first '\0'
188        x = msg.find('\0')
189        if x:
190            msg = msg[:x]
191
192        return msg
193
194
195
196class LiberalAuthzHandler(AuthzHandler):
197    """Allow everything.
198    """
199
200    def __init__(self, username):
201        self.username = username
202
203    def authzControl(self, m, t, p):
204        return True;
205
206    def authzRetr(self, p):
207        return True;
208
209    def authzStore(self, p):
210        return True;
211   
Note: See TracBrowser for help on using the repository browser.