Changeset 1187 for TI05-delivery


Ignore:
Timestamp:
15/06/06 10:16:37 (13 years ago)
Author:
spascoe
Message:

Auto-generated documentation included for the python-server interface.

Some file clean up prior to alpha. Fixed some confusion between authentication
and authorisation in the API.

The embedded client test case is disabled because it doesn't work :-(.

Location:
TI05-delivery/trunk
Files:
57 added
6 edited

Legend:

Unmodified
Added
Removed
  • TI05-delivery/trunk/lib/python/delivery/__init__.py

    r1133 r1187  
    88@author: Stephen Pascoe 
    99""" 
     10 
  • TI05-delivery/trunk/lib/python/delivery/server.py

    r1144 r1187  
    11""" 
     2Start a bbftp server process and define python callbacks for authentication and authorisation.  
     3 
    24Interface 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. 
    38 
    49@author Stephen Pascoe 
     
    1419    calls authHandler.authorise() on each connection to do authentication/authorisation. 
    1520 
    16     @note: because the server process forks, authHandler will not see any changes to the python 
     21    @note because the server process forks, authHandler will not see any changes to the python 
    1722        interpreter following the call to start(). 
    1823 
    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. 
    2227    """ 
    2328     
     
    4550     
    4651    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(). 
    4856 
    4957        @param buffer a string containing the message to send. 
     
    5361 
    5462    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(). 
    5667 
    5768        @return a string containing the message received. 
     
    6071        return bbftpd.recv() 
    6172 
    62     def authorise(self): 
     73    def authenticate(self): 
    6374        """Authenticate a connection. 
    6475 
    6576        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. 
    6779 
    6880        @note because the server process forks on each connection, calls to authorise() will 
     
    7183        @return an AuthzHandler instance if authentication succeeds.  Any false value is considered 
    7284            an authorisation failure. 
     85        @raise AuthorisationFailure the preferred way of signaling authorisation failure to 
     86            the bbftp server process. 
    7387        """ 
    7488 
     
    7993    """Abstract base class for implementing authorisation. 
    8094 
    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         
    8298    """ 
    8399 
     
    85101        """Authorise a control command. 
    86102 
    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. 
    91107        """ 
    92108 
     
    96112        """Authorise a retrieve request. 
    97113 
    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. 
    100116        """ 
    101117 
     
    105121        """Authorise a store request. 
    106122 
    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. 
    109125        """ 
    110126 
    111127        raise NotImplementedError 
    112128     
    113     def _raiseNoUsername(self): 
    114         raise ValueError, "No username has been set" 
    115     username = property(_raiseNoUsername) 
    116129 
    117130#-------------------------------------------------------------------------------------------------------------- 
     
    119132class BasicClientAuthHandler(AuthHandler): 
    120133    """ 
    121     When testing with the basic bbftp client some messages will be NULL terminated.  This class 
    122     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. 
    123136    """ 
    124137 
     
    145158    """ 
    146159 
    147     username = None 
    148  
    149160    def __init__(self, username): 
    150161        self.username = username 
  • TI05-delivery/trunk/setup.py

    r1157 r1187  
    7575                ] 
    7676 
    77 client_sources = ['%s/bbftpc/%s' % (bbftpc_home, x) for x in bbftpc_src] + ['./src/python_ext/bbftpc.c'] 
     77client_sources = ['%s/bbftpc/%s' % (bbftpc_home, x) for x in bbftpc_src] + ['./src/python_ext/bbftpc.c',  
     78                                                                            './src/python_ext/util.c'] 
    7879 
    7980bbftpd = Extension('delivery.bbftpd', 
  • TI05-delivery/trunk/src/python_ext/bbftpc.c

    r1157 r1187  
    1313#include <stdio.h> 
    1414 
     15#include "util.h" 
     16 
    1517extern char **environ; 
    16 #define TRUE 1 
    17 #define FALSE 0 
    1818 
    1919int 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  
    9020 
    9121/** 
     
    10636   * Convert arguments into a standard argv sequence. 
    10737   */ 
    108   argc = PySequence_Size(client_args); 
     38 
     39  if ((argc = PySequence_Size(client_args)) == -1) { 
     40    return NULL; 
     41  } 
    10942  if (!ndg_argv_init(&argv_s, argc + 1)) { 
    11043    PyErr_SetString(PyExc_MemoryError, "malloc failed"); 
     
    13063      PyErr_SetString(PyExc_MemoryError, "ndg_argv_add failed"); 
    13164      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s); 
     65      Py_DECREF(item); 
    13266      Py_DECREF(client_args); 
    133       Py_DECREF(item); 
     67      return NULL; 
    13468    } 
    13569 
     
    14377  } 
    14478   
    145  
    14679  bbftpc_main(argv_s.argc, argv_s.argv, environ); 
    14780  ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s); 
  • TI05-delivery/trunk/src/python_ext/bbftpd.c

    r1157 r1187  
    9494   *      failure rather than error. 
    9595   */ 
    96   if ((authzHandler = PyObject_CallMethod(authHandler, "authorise", "")) == NULL) { 
     96  if ((authzHandler = PyObject_CallMethod(authHandler, "authenticate", "")) == NULL) { 
    9797    ndg_pyerr_to_logmessage(logmessage, "ndg_auth"); 
    9898    return -1; 
     
    101101  /* Authorisation is considered failed if the return value isn't True.  */ 
    102102  if (!PyObject_IsTrue(authzHandler)) { 
    103     sprintf(logmessage, "ndg_auth: python authorisation failed"); 
     103    sprintf(logmessage, "ndg_auth: python authentication failed"); 
    104104    return -1; 
    105105  } 
  • TI05-delivery/trunk/test/test_embedded.py

    r1157 r1187  
    2828class TestAuthHandler(server.BasicClientAuthHandler): 
    2929     
    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         
    3137        msg = self.recvCStr() 
    3238 
     
    215221        output = fh.read() 
    216222 
    217         print output 
    218  
    219223        self.assertLines(['Received Auth handshake: NDG-Delivery-server %s' % VERSION], output) 
    220224 
     
    331335 
    332336 
    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 
     379if __name__ == '__main__': 
     380    unittest.main() 
Note: See TracChangeset for help on using the changeset viewer.