courier.py 2.0 KB

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