Package proxy :: Module rhnAuthProtocol
[hide private]
[frames] | no frames]

Source Code for Module proxy.rhnAuthProtocol

 1  # Communication routines for sockets connecting to the auth token cache daemon 
 2  # 
 3  # Copyright (c) 2008--2018 Red Hat, Inc. 
 4  # 
 5  # This software is licensed to you under the GNU General Public License, 
 6  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 7  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 8  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 9  # along with this software; if not, see 
10  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
11  # 
12  # Red Hat trademarks are not licensed under GPLv2. No permission is 
13  # granted to use or replicate Red Hat trademarks that are incorporated 
14  # in this software or its documentation. 
15  # 
16  #------------------------------------------------------------------------------- 
17   
18  ## system imports 
19  import struct 
20   
21  ## local imports 
22  from xmlrpclib import dumps, loads 
23   
24   
25 -class CommunicationError(Exception):
26
27 - def __init__(self, faultCode, faultString, *params):
28 Exception.__init__(self) 29 self.faultCode = faultCode 30 self.faultString = faultString 31 self.args = params
32 33
34 -def readSocket(fd, n):
35 """ Reads exactly n bytes from the file descriptor fd (if possible) """ 36 result = "" # The result 37 while n > 0: 38 buff = fd.read(n) 39 if not buff: 40 break 41 n = n - len(buff) 42 result = result + buff 43 return result
44 45
46 -def send(fd, methodname=None, fault=None, *params): #pylint: disable=bad-option-value, keyword-arg-before-vararg
47 if methodname: 48 buff = dumps(params, methodname=methodname) 49 elif fault: 50 buff = dumps(fault) 51 else: 52 buff = dumps(params) 53 # Write the length first 54 fd.write(struct.pack("!L", len(buff))) 55 # Then send the data itself 56 fd.write(buff) 57 return len(buff) 58 59
60 -def recv(rfile):
61 # Compute the size of an unsigned int 62 n = struct.calcsize("L") 63 # Read the first bytes to figure out the size 64 buff = readSocket(rfile, n) 65 if len(buff) != n: 66 # Incomplete read 67 raise CommunicationError(0, 68 "Expected %d bytes; got only %d" % (n, len(buff))) 69 70 n, = struct.unpack("!L", buff) 71 72 if n > 65536: 73 # The buffer to be read is too big 74 raise CommunicationError(1, "Block too big: %s" % len(buff)) 75 76 buff = readSocket(rfile, n) 77 if len(buff) != n: 78 # Incomplete read 79 raise CommunicationError(0, 80 "Expected %d bytes; got only %d" % (n, len(buff))) 81 82 return loads(buff)
83