radius_das.py 1.6 KB

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