radius_das.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/python
  2. #
  3. # RADIUS DAS extensions to pyrad
  4. # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import hashlib
  9. import random
  10. import struct
  11. import pyrad.packet
  12. class DisconnectPacket(pyrad.packet.Packet):
  13. def __init__(self, code=pyrad.packet.DisconnectRequest, id=None,
  14. secret=None, authenticator=None, **attributes):
  15. pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
  16. **attributes)
  17. def RequestPacket(self):
  18. attr = self._PktEncodeAttributes()
  19. if self.id is None:
  20. self.id = random.randrange(0, 256)
  21. header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
  22. self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
  23. + self.secret).digest()
  24. return header + self.authenticator + attr
  25. class CoAPacket(pyrad.packet.Packet):
  26. def __init__(self, code=pyrad.packet.CoARequest, id=None,
  27. secret=None, authenticator=None, **attributes):
  28. pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
  29. **attributes)
  30. def RequestPacket(self):
  31. attr = self._PktEncodeAttributes()
  32. if self.id is None:
  33. self.id = random.randrange(0, 256)
  34. header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
  35. self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
  36. + self.secret).digest()
  37. return header + self.authenticator + attr