courier.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. """
  21. import sys
  22. import socket
  23. from radicale import acl, config, log
  24. COURIER_SOCKET = config.get("acl", "courier_socket")
  25. def has_right(owner, user, password):
  26. """Check if ``user``/``password`` couple is valid."""
  27. if not user or (owner not in acl.PRIVATE_USERS and user != owner):
  28. # No user given, or owner is not private and is not user, forbidden
  29. return False
  30. line = "%s\nlogin\n%s\n%s" % (sys.argv[0], user, password)
  31. line = "AUTH %i\n%s" % (len(line), line)
  32. try:
  33. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  34. sock.connect(COURIER_SOCKET)
  35. log.LOGGER.debug("Sending to Courier socket the request: %s" % line)
  36. sock.send(line)
  37. data = sock.recv(1024)
  38. sock.close()
  39. except socket.error as exception:
  40. log.LOGGER.debug(
  41. "Unable to communicate with Courier socket: %s" % exception)
  42. return False
  43. log.LOGGER.debug("Got Courier socket response: %r" % data)
  44. # Address, HOME, GID, and either UID or USERNAME are mandatory in resposne
  45. # see http://www.courier-mta.org/authlib/README_authlib.html#authpipeproto
  46. for line in data.split():
  47. if 'GID' in line:
  48. return True
  49. # default is reject
  50. # this alleviates the problem of a possibly empty reply from courier authlib
  51. # see http://www.courier-mta.org/authlib/README_authlib.html#authpipeproto
  52. return False