Changeset 1538 for TI05-delivery


Ignore:
Timestamp:
28/09/06 14:06:31 (13 years ago)
Author:
spascoe
Message:

Probably too many changes for one commit :-(.

I've reorganised the auth/authz API to separate interface from
implementation. Some C bug fixes but the example client now segfaults
on exit :-(.

I havn't refactored the test cases to match the new API, so I expect
they fail.

Location:
TI05-delivery/trunk
Files:
2 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • TI05-delivery/trunk/examples/deliveryclient.py

    r1441 r1538  
    77from glob import glob 
    88 
    9 HOME = os.path.abspath(os.getenv('NDG_DELIVERY_HOME', os.curdir)) 
    10 VERSION = open('%s/VERSION' % HOME).read() 
    11 NDG_MESSAGE_LEN = 256 
    12  
    13 NDG_HANDSHAKE = "NDG-Delivery-client %s" % VERSION 
    14  
    159import ndg.delivery.client.pybbftp as DC 
    1610 
    17 class AuthClientHandler(DC.AuthClientHandler): 
    18     def __init__(self, privatestr="none"): 
    19         self.privatestr = privatestr 
    20  
    21     def authenticate(self): 
    22         self.send(NDG_HANDSHAKE) 
    23         resp = self.recv() 
    24  
    25         print "Received Auth handshake: %s" % resp 
    26  
    27         self.send(self.privatestr) 
    28  
    29         return True 
     11from ndg.delivery.auth_plugin import * 
    3012 
    3113def makeClient(username, hostname): 
    32     return DC.connect(AuthClientHandler(), ['-m', '-u', username, '-r', '1', hostname]) 
     14    c = DC.ClientConnector() 
     15    try: 
     16        return DC.connect(AuthClientHandler(c), ['-m', '-u', username, '-r', '1', hostname]) 
     17    except Exception, e: 
     18        if hasattr(e, 'clientMessages'): 
     19            print e.clientMessages 
     20        raise e 
    3321 
    3422def do(cmd): 
  • TI05-delivery/trunk/examples/deliveryd.py

    r1448 r1538  
    99import re, tempfile, getopt 
    1010 
    11 HOME = os.path.abspath(os.getenv('NDG_DELIVERY_HOME', os.curdir)) 
    12 DATADIR = '%s/test/data' % HOME 
    13 VERSION = open('%s/VERSION' % HOME).read() 
    14 NDG_MESSAGE_LEN = 256 
    15  
    16 NDG_HANDSHAKE = "NDG-Delivery-server %s" % VERSION 
    17  
    1811import logging 
    1912logging.basicConfig(level=logging.DEBUG, filename='./bbftpd.log') 
     
    2114 
    2215import ndg.delivery.server.pybbftp as server 
    23  
    24  
    25 class AuthHandler(server.AuthHandler): 
    26      
    27     def authenticate(self): 
    28         msg = self.recv() 
    29         logger.debug('AuthContext received Auth message: %s' % msg) 
    30                  
    31         self.send(NDG_HANDSHAKE) 
    32  
    33         privatestr = self.recv() 
    34         logger.debug("AuthContext received privatestr: %s" % privatestr) 
    35          
    36         return self.makeAuthzHandler(msg, "TestCaseUser") 
    37  
    38     def makeAuthzHandler(self, msg, user): 
    39         return AuthzHandler(msg, "TestCaseUser") 
    40  
    41 class AuthzHandler(server.LiberalAuthzHandler): 
    42     def __init__(self, version, username): 
    43         super(AuthzHandler, self).__init__(username) 
    44         self.version = version 
    45  
    46     def authzControl(self, msgcode, transferopt, path): 
    47         logger.debug('AuthzHandler.authzControl: msgcode = %s' % hex(msgcode)) 
    48         return self.authzPath(path) 
    49  
    50     def authzRetr(self, path): 
    51         return self.authzPath(path) 
    52  
    53     def authzStore(self, path): 
    54         return self.authzPath(path) 
    55  
    56     def authzPath(self, path): 
    57         # For dir commands the path will end with '*' 
    58         if path[-2:] == '/*': 
    59             path = path[:-2] 
    60         mode = stat.S_IMODE(os.stat(path)[stat.ST_MODE]) 
    61         if (mode & 0004): 
    62             logger.debug('AuthzHandler OK') 
    63             return True 
    64         else: 
    65             logger.debug('AuthzHandler FAIL') 
    66             raise server.AuthorisationFailure, "AuthzHandler: no read perms" 
     16from ndg.delivery.auth_plugin import * 
    6717 
    6818 
     
    8030logfile = './bbftpd.log' 
    8131n = len(open(logfile).readlines()) 
    82 pid = server.start(AuthHandler(), ['-l', 'DEBUG']) 
     32c = server.ServerConnector() 
     33pid = server.start(AuthHandler(c), ['-l', 'DEBUG']) 
    8334print 'Server process started at pid = %d' % pid 
    8435print 'Monitoring %s' % logfile 
  • TI05-delivery/trunk/lib/python/ndg/delivery/client/pybbftp.py

    r1441 r1538  
    5151 
    5252 
    53  
    54 class AuthClientHandler(object): 
    55     """Abstract base class specifying the client authentication callback interface. 
    56     """ 
    57  
     53class ClientConnector(object): 
    5854    def send(self, buffer): 
    5955        """Send a message to the server during authentication. 
     
    7672        return bbftpc.recv() 
    7773     
    78     def authenticate(self): 
    79         """Authenticate with the server. 
    8074 
    81         This method should be overridden in subclasses to implement authentication using 
    82         the method calls self.send() and send.recv().  It is called by the bbftp layer from within 
    83         connect(). 
    84  
    85         @return a value indicating success or failure. 
    86         """ 
    87          
    88         raise NotImplementedError 
  • TI05-delivery/trunk/lib/python/ndg/delivery/server/pybbftp.py

    r1441 r1538  
    9292    pass 
    9393 
    94  
    95  
    96 class AuthHandler(object): 
    97     """Abstract base class for implementing authentication. 
    98  
    99     Subclasses should implement the authorise() method to authenticate clients connecting to 
    100     the server.  An AuthHandler object is passed to start() when initialising the server. 
    101     """ 
    102      
     94class ServerConnector(object): 
    10395    def send(self, buffer): 
    10496        """Send a message to the client during authentication 
    10597 
    106         This method must only be used from within self.authenticate() which has been called 
     98        This method must only be used from within auth.AuthHandler.authenticate() which has been called 
    10799        from the bbftp server process started by start(). 
    108100 
     
    115107        """Receive a message from the client during authentication 
    116108 
    117         This method must only be used from within self.authenticate() which has been called 
     109        This method must only be used from within auth.AuthHandler.authenticate() which has been called 
    118110        from the bbftp server process started by start(). 
    119111 
     
    122114 
    123115        return bbftpd.recv() 
    124  
    125     def authenticate(self): 
    126         """Authenticate a connection. 
    127  
    128         This function should be overridden in subclasses to implement authentication using 
    129         the method calls self.send() and send.recv().  It is called by the bbftp server process 
    130         created using the start() function. 
    131  
    132         @note because the server process forks on each connection, calls to authorise() will 
    133             see the state of the python interpreter as it was when start() was called. 
    134  
    135         @return an AuthzHandler instance if authentication succeeds.  Any false value is considered 
    136             an authorisation failure. 
    137         @raise AuthorisationFailure the preferred way of signaling authorisation failure to 
    138             the bbftp server process. 
    139         """ 
    140  
    141         raise NotImplementedError 
    142116 
    143117    def log(self, priority, msg): 
     
    150124 
    151125 
    152 class AuthzHandler(object): 
    153     """Abstract base class for implementing authorisation. 
    154  
    155     @ivar username: the client's username.  This attribute must be set before an instance of AuthzHandler 
    156         is returned to the bbftp server process by AuthHandler.authenticate(). 
    157          
    158     """ 
    159  
    160     def authzControl(self, msgcode, transferoption, path): 
    161         """Authorise a control command. 
    162  
    163         @param msgcode the command type. 
    164         @param transferoption extra options specified in the command. 
    165         @param path the file or directory to which the command applies. 
    166         @return bool for success or failure. 
    167         """ 
    168  
    169         raise NotImplementedError 
    170  
    171     def authzRetr(self, path): 
    172         """Authorise a retrieve request. 
    173  
    174         @param path the file being retrieved. 
    175         @return bool for success or failure. 
    176         """ 
    177  
    178         raise NotImplementedError 
    179  
    180     def authzStore(self, path): 
    181         """Authorise a store request. 
    182  
    183         @param path the destination file. 
    184         @return bool for success or failure. 
    185         """ 
    186  
    187         raise NotImplementedError 
    188  
    189     def log(self, priority, msg): 
    190         """Send a message to the bbftpd logger. 
    191  
    192         @param priority one of server.LOG_* 
    193         @param msg the message to log 
    194         """ 
    195         bbftpd.log(priority, msg) 
    196  
    197  
    198 #-------------------------------------------------------------------------------------------------------------- 
    199  
    200  
    201 class LiberalAuthzHandler(AuthzHandler): 
    202     """Allow everything. 
    203     """ 
    204  
    205     def __init__(self, username): 
    206         self.username = username 
    207  
    208     def authzControl(self, m, t, p): 
    209         return True; 
    210  
    211     def authzRetr(self, p): 
    212         return True; 
    213  
    214     def authzStore(self, p): 
    215         return True; 
    216126     
  • TI05-delivery/trunk/src/python_ext/bbftpc.c

    r1265 r1538  
    148148     */ 
    149149    PyErr_SetString(PyExc_RuntimeError, "bbftpc fatal error"); 
     150 
     151    /* Escape from deep within the bbftpc code. */ 
    150152    longjmp(error_jmp, flag); 
    151153  } 
     
    238240    ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s); 
    239241    /** @todo clientMessages is lost when an exception is raised.  This isn't ideal. */ 
     242    /* Just dump clientMessages to stderr for now. */ 
     243    fprintf(stderr, "clientMessages: "); 
     244    PyObject_Print(clientMessages, stderr, 0); 
    240245    Py_DECREF(clientMessages); clientMessages = NULL; 
    241246    return NULL; 
     
    378383  if (ndg_client_message_recv(&buffer, &len, logmessage) == -1) { 
    379384    PyErr_SetString(PyExc_IOError, logmessage); 
    380     free(buffer); 
    381385    return NULL; 
    382386  } 
  • TI05-delivery/trunk/src/python_ext/bbftpd.c

    r1448 r1538  
    382382  if (ndg_message_recv(&buffer, &len, logmessage) == -1) { 
    383383    PyErr_SetString(PyExc_IOError, logmessage); 
    384     free(buffer); 
    385384    return NULL; 
    386385  } 
Note: See TracChangeset for help on using the changeset viewer.