Courier-Authdaemon.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2011 Henry-Nicolas Tourneur
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Courier-Authdaemon ACL.
  20. Authentication based on the ``python-ldap`` module
  21. (http://www.python-ldap.org/).
  22. """
  23. import socket,os,sys
  24. from radicale import acl, config, log
  25. COURIER_SOCKET = config.get("acl", "courier-auth_socket")
  26. def has_right(owner, user, password):
  27. """Check if ``user``/``password`` couple is valid."""
  28. if not user or (owner not in acl.PRIVATE_USERS and user != owner):
  29. # No user given, or owner is not private and is not user, forbidden
  30. return False
  31. line = sys.argv[0] . "\nlogin\n" + user + "\n" + password
  32. line = len(line) + "\n" + line
  33. try:
  34. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  35. s.connect(COURIER_SOCKET)
  36. log.LOGGER.debug("Sending to socket the request: %s" % line)
  37. s.send(line)
  38. data = s.recv(1024)
  39. s.close()
  40. except socket.error, (value,message):
  41. log.LOGGER.debug("Unable to communicate with the socket (error: %s)" % message)
  42. return False
  43. log.LOGGER.debug("Got socket response: %s" % repr(data))
  44. if repr(data) == "FAIL":
  45. return False
  46. return True