Courier-Authdaemon.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. import socket,os,sys
  19. from radicale import acl, config, log
  20. COURIER_SOCKET = config.get("acl", "courier-auth_socket")
  21. def has_right(owner, user, password):
  22. """Check if ``user``/``password`` couple is valid."""
  23. if not user or (owner not in acl.PRIVATE_USERS and user != owner):
  24. # No user given, or owner is not private and is not user, forbidden
  25. return False
  26. line = sys.argv[0] . "\nlogin\n" + user + "\n" + password
  27. line = len(line) + "\n" + line
  28. try:
  29. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  30. s.connect(COURIER_SOCKET)
  31. log.LOGGER.debug("Sending to socket the request: %s" % line)
  32. s.send(line)
  33. data = s.recv(1024)
  34. s.close()
  35. except socket.error, (value,message):
  36. log.LOGGER.debug("Unable to communicate with the socket (error: %s)" % message)
  37. return False
  38. log.LOGGER.debug("Got socket response: %s" % repr(data))
  39. if repr(data) == "FAIL":
  40. return False
  41. return True