Changeset 1187 for TI05-delivery/trunk
- Timestamp:
- 15/06/06 10:16:37 (15 years ago)
- Location:
- TI05-delivery/trunk
- Files:
-
- 57 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
TI05-delivery/trunk/lib/python/delivery/__init__.py
r1133 r1187 8 8 @author: Stephen Pascoe 9 9 """ 10 -
TI05-delivery/trunk/lib/python/delivery/server.py
r1144 r1187 1 1 """ 2 Start a bbftp server process and define python callbacks for authentication and authorisation. 3 2 4 Interface 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. 3 8 4 9 @author Stephen Pascoe … … 14 19 calls authHandler.authorise() on each connection to do authentication/authorisation. 15 20 16 @note :because the server process forks, authHandler will not see any changes to the python21 @note because the server process forks, authHandler will not see any changes to the python 17 22 interpreter following the call to start(). 18 23 19 @param authHandler :an instance of AuthHandler.20 @param args :a list of command line arguments.21 @return :the PID of the server process.24 @param authHandler an instance of AuthHandler. 25 @param args a list of command line arguments. 26 @return the PID of the server process. 22 27 """ 23 28 … … 45 50 46 51 def send(self, buffer): 47 """Send a message to the client. 52 """Send a message to the client during authorisation. 53 54 This method must only be used from within self.authorise() which has been called 55 from the bbftp server process started by start(). 48 56 49 57 @param buffer a string containing the message to send. … … 53 61 54 62 def recv(self): 55 """Receive a message from the client. 63 """Receive a message from the client during authorisation. 64 65 This method must only be used from within self.authorise() which has been called 66 from the bbftp server process started by start(). 56 67 57 68 @return a string containing the message received. … … 60 71 return bbftpd.recv() 61 72 62 def auth orise(self):73 def authenticate(self): 63 74 """Authenticate a connection. 64 75 65 76 This function should be overridden in subclasses to implement authentication using 66 the method calls self.send() and send.recv(). 77 the method calls self.send() and send.recv(). It is called by the bbftp server process 78 created using the start() function. 67 79 68 80 @note because the server process forks on each connection, calls to authorise() will … … 71 83 @return an AuthzHandler instance if authentication succeeds. Any false value is considered 72 84 an authorisation failure. 85 @raise AuthorisationFailure the preferred way of signaling authorisation failure to 86 the bbftp server process. 73 87 """ 74 88 … … 79 93 """Abstract base class for implementing authorisation. 80 94 81 @ivar username the client's username 95 @ivar username: the client's username. This attribute must be set before an instance of AuthzHandler 96 is returned to the bbftp server process by AuthHandler.authenticate(). 97 82 98 """ 83 99 … … 85 101 """Authorise a control command. 86 102 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.90 @return :bool for success or failure.103 @param msgcode the command type. 104 @param transferoption extra options specified in the command. 105 @param path the file or directory to which the command applies. 106 @return bool for success or failure. 91 107 """ 92 108 … … 96 112 """Authorise a retrieve request. 97 113 98 @param path :the file being retrieved.99 @return :bool for success or failure.114 @param path the file being retrieved. 115 @return bool for success or failure. 100 116 """ 101 117 … … 105 121 """Authorise a store request. 106 122 107 @param path :the destination file.108 @return :bool for success or failure.123 @param path the destination file. 124 @return bool for success or failure. 109 125 """ 110 126 111 127 raise NotImplementedError 112 128 113 def _raiseNoUsername(self):114 raise ValueError, "No username has been set"115 username = property(_raiseNoUsername)116 129 117 130 #-------------------------------------------------------------------------------------------------------------- … … 119 132 class BasicClientAuthHandler(AuthHandler): 120 133 """ 121 When testing with the basic bbftp client some messages will be NULL terminated. This class122 makes communicating with such clients slightly easier.134 This class makes communicating with the stand alone bbftp client slightly easier. 135 Mainly for use during testing. 123 136 """ 124 137 … … 145 158 """ 146 159 147 username = None148 149 160 def __init__(self, username): 150 161 self.username = username -
TI05-delivery/trunk/setup.py
r1157 r1187 75 75 ] 76 76 77 client_sources = ['%s/bbftpc/%s' % (bbftpc_home, x) for x in bbftpc_src] + ['./src/python_ext/bbftpc.c'] 77 client_sources = ['%s/bbftpc/%s' % (bbftpc_home, x) for x in bbftpc_src] + ['./src/python_ext/bbftpc.c', 78 './src/python_ext/util.c'] 78 79 79 80 bbftpd = Extension('delivery.bbftpd', -
TI05-delivery/trunk/src/python_ext/bbftpc.c
r1157 r1187 13 13 #include <stdio.h> 14 14 15 #include "util.h" 16 15 17 extern char **environ; 16 #define TRUE 117 #define FALSE 018 18 19 19 int bbftpc_main(int argc, char **argv, char **envp); 20 21 /**22 * Routines for constructing a argc/argv style argument list from a python sequence.23 */24 25 struct ndg_argv {26 int argc; /// Total number of arguments.27 char **argv; /// Argument array.28 int next; /// index of next empty (and unallocated) argument.29 };30 31 /**32 * Initialise a structure.33 * @return TRUE on success or FALSE on a memory error.34 */35 int ndg_argv_init(struct ndg_argv *argv_s, int argc) {36 argv_s->argc = argc;37 38 if ((argv_s->argv = (char **)malloc(sizeof(struct ndg_argv))) == NULL) {39 return FALSE;40 }41 42 argv_s->next = 0;43 44 return TRUE;45 }46 47 /**48 * Free a structure. \a argv_s should be cleared first with \c ndg_argv_clear().49 *50 * @return TRUE on success or FALSE if the structure isn't empty.51 */52 int ndg_argv_free(struct ndg_argv *argv_s) {53 if (argv_s->next != 0) return FALSE;54 free(argv_s->argv);55 return TRUE;56 }57 58 /**59 * Clear all arguments from the structure.60 *61 */62 void ndg_argv_clear(struct ndg_argv *argv_s) {63 char *arg;64 while (argv_s->next > 0) {65 argv_s->next--;66 arg = argv_s->argv[argv_s->next];67 free(arg);68 }69 }70 71 /**72 * Add an argument.73 *74 * @return TRUE on success or FALSE on an error.75 */76 int ndg_argv_add(struct ndg_argv *argv_s, char *arg) {77 char **arg_p;78 79 if (argv_s->argc == argv_s->next) return FALSE;80 arg_p = &(argv_s->argv[argv_s->next]);81 if ((*arg_p = (char *)malloc(strlen(arg))) == NULL) {82 return FALSE;83 }84 strcpy(*arg_p, arg);85 argv_s->next++;86 return TRUE;87 }88 89 90 20 91 21 /** … … 106 36 * Convert arguments into a standard argv sequence. 107 37 */ 108 argc = PySequence_Size(client_args); 38 39 if ((argc = PySequence_Size(client_args)) == -1) { 40 return NULL; 41 } 109 42 if (!ndg_argv_init(&argv_s, argc + 1)) { 110 43 PyErr_SetString(PyExc_MemoryError, "malloc failed"); … … 130 63 PyErr_SetString(PyExc_MemoryError, "ndg_argv_add failed"); 131 64 ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s); 65 Py_DECREF(item); 132 66 Py_DECREF(client_args); 133 Py_DECREF(item);67 return NULL; 134 68 } 135 69 … … 143 77 } 144 78 145 146 79 bbftpc_main(argv_s.argc, argv_s.argv, environ); 147 80 ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s); -
TI05-delivery/trunk/src/python_ext/bbftpd.c
r1157 r1187 94 94 * failure rather than error. 95 95 */ 96 if ((authzHandler = PyObject_CallMethod(authHandler, "auth orise", "")) == NULL) {96 if ((authzHandler = PyObject_CallMethod(authHandler, "authenticate", "")) == NULL) { 97 97 ndg_pyerr_to_logmessage(logmessage, "ndg_auth"); 98 98 return -1; … … 101 101 /* Authorisation is considered failed if the return value isn't True. */ 102 102 if (!PyObject_IsTrue(authzHandler)) { 103 sprintf(logmessage, "ndg_auth: python auth orisation failed");103 sprintf(logmessage, "ndg_auth: python authentication failed"); 104 104 return -1; 105 105 } -
TI05-delivery/trunk/test/test_embedded.py
r1157 r1187 28 28 class TestAuthHandler(server.BasicClientAuthHandler): 29 29 30 def authorise(self): 30 def authenticate(self): 31 # Dump the pid so that gdm can pick it up 32 #fh = open('./DS_conn.pid', 'w') 33 #print >>fh, os.getpid() 34 #fh.close() 35 #time.sleep(20) 36 31 37 msg = self.recvCStr() 32 38 … … 215 221 output = fh.read() 216 222 217 print output218 219 223 self.assertLines(['Received Auth handshake: NDG-Delivery-server %s' % VERSION], output) 220 224 … … 331 335 332 336 333 334 import delivery.bbftpc 335 336 class PythonClientAuthOK(AuthOK): 337 """Repeat AuthOK tests with a python-embedded client. 338 """ 339 340 def _runClient(self, cmd, debug=False, user="testcase", privatestr=None): 341 """Run the client. 342 """ 343 344 if debug: 345 f = "-d" 346 else: 347 f = "-m" 348 349 # Tell bbftp to dump output to a file which is then opened for reading. 350 tmp = tempfile.mktemp('test_bbftpd') 351 args = ['-e', cmd, f, '-u', user, '-o', tmp, '-f', tmp, '-r', '1'] 352 353 if privatestr != None: 354 args += ['-P', privatestr] 355 356 delivery.bbftpc.run(args + ['localhost']) 357 358 fh.open(tmp) 359 os.remove(tmp) 360 361 return fh 362 363 #if __name__ == '__main__': 364 # unittest.main() 337 # 338 # Embedded client test case currently segfaults :-( 339 # 340 ## import delivery.bbftpc 341 ## import StringIO 342 343 ## class PythonClientAuthOK(AuthOK): 344 ## """Repeat AuthOK tests with a python-embedded client. 345 ## """ 346 347 ## def _runClient(self, cmd, debug=False, user="testcase", privatestr=None): 348 ## """Run the client. 349 ## """ 350 351 ## #print "Waiting for gdb connect, pid = %d" % os.getpid() 352 ## #time.sleep(15) 353 354 ## if debug: 355 ## f = "-d" 356 ## else: 357 ## f = "-m" 358 359 ## # Tell bbftp to dump output to a file which is then opened for reading. 360 ## tmp = tempfile.mktemp('test_bbftpd') 361 ## args = ['-e', cmd, f, '-u', user, 362 ## #'-o', tmp, '-f', tmp, 363 ## '-r', '1'] 364 365 ## if privatestr != None: 366 ## args += ['-P', privatestr] 367 368 ## args.append('localhost') 369 ## print "Running client with args: %s" % (args,) 370 371 ## delivery.bbftpc.run(args) 372 373 ## #fh = open(tmp) 374 ## #os.remove(tmp) 375 ## fh = StringIO.StringIO('') 376 377 ## return fh 378 379 if __name__ == '__main__': 380 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.