courier.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2011 Henry-Nicolas Tourneur
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Courier-Authdaemon authentication.
  18. """
  19. import sys
  20. import socket
  21. from .. import config, log
  22. COURIER_SOCKET = config.get("auth", "courier_socket")
  23. def is_authenticated(user, password):
  24. """Check if ``user``/``password`` couple is valid."""
  25. if not user or not password:
  26. return False
  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