/usr/share/pyshared/pyrad/tools.py is in python-pyrad 2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # tools.py
#
# Utility functions
import struct
import six
def EncodeString(str):
if len(str) > 253:
raise ValueError('Can only encode strings of <= 253 characters')
if isinstance(str, six.text_type):
return str.encode('utf-8')
else:
return str
def EncodeOctets(str):
if len(str) > 253:
raise ValueError('Can only encode strings of <= 253 characters')
return str
def EncodeAddress(addr):
if not isinstance(addr, six.string_types):
raise TypeError('Address has to be a string')
(a, b, c, d) = map(int, addr.split('.'))
return struct.pack('BBBB', a, b, c, d)
def EncodeInteger(num):
if not isinstance(num, six.integer_types):
raise TypeError('Can not encode non-integer as integer')
return struct.pack('!I', num)
def EncodeDate(num):
if not isinstance(num, int):
raise TypeError('Can not encode non-integer as date')
return struct.pack('!I', num)
def DecodeString(str):
return str.decode('utf-8')
def DecodeOctets(str):
return str
def DecodeAddress(addr):
return '.'.join(map(str, struct.unpack('BBBB', addr)))
def DecodeInteger(num):
return (struct.unpack('!I', num))[0]
def DecodeDate(num):
return (struct.unpack('!I', num))[0]
def EncodeAttr(datatype, value):
if datatype == 'string':
return EncodeString(value)
elif datatype == 'octets':
return EncodeOctets(value)
elif datatype == 'ipaddr':
return EncodeAddress(value)
elif datatype == 'integer':
return EncodeInteger(value)
elif datatype == 'date':
return EncodeDate(value)
else:
raise ValueError('Unknown attribute type %s' % datatype)
def DecodeAttr(datatype, value):
if datatype == 'string':
return DecodeString(value)
elif datatype == 'octets':
return DecodeOctets(value)
elif datatype == 'ipaddr':
return DecodeAddress(value)
elif datatype == 'integer':
return DecodeInteger(value)
elif datatype == 'date':
return DecodeDate(value)
else:
raise ValueError('Unknown attribute type %s' % datatype)
|