IMAP.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012 Daniel Aleksandersen
  5. # Copyright © 2011 Corentin Le Bail
  6. # Copyright © 2011 Guillaume Ayoub
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. IMAP ACL.
  22. Secure authentication based on the ``imaplib`` module.
  23. Validating users against a modern IMAP4rev1 server that awaits STARTTLS on
  24. port 143. Legacy SSL (often on legacy port 993) is deprecated and thus
  25. unsupported. STARTTLS is enforced except if host is ``localhost`` as
  26. passwords are sent in PLAIN.
  27. Python 3.2 or newer is required for TLS.
  28. """
  29. import imaplib
  30. from radicale import acl, config, log
  31. IMAP_SERVER = config.get("acl", "imap_auth_host_name")
  32. IMAP_SERVER_PORT = config.get("acl", "imap_auth_host_port")
  33. def has_right(owner, user, password):
  34. """Check if ``user``/``password`` couple is valid."""
  35. if not user or (owner not in acl.PRIVATE_USERS and user != owner):
  36. # No user given, or owner is not private and is not user, forbidden
  37. return False
  38. log.LOGGER.debug("[IMAP ACL] Connecting to %s:%s." % (IMAP_SERVER, IMAP_SERVER_PORT,))
  39. connection = imaplib.IMAP4(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  40. def secure_connection():
  41. try:
  42. connection.starttls()
  43. log.LOGGER.debug("[IMAP ACL] Server connection changed to TLS.")
  44. return True
  45. except (IMAP4.error, IAMP4.abort) as err:
  46. import sys
  47. PY_MIN = float(3.2)
  48. PY_SYS = float("%s.%s" % (sys.version_info.major, sys.version_info.minor))
  49. if PY_SYS < PY_MIN:
  50. log.LOGGER.error("[IMAP ACL] Python 3.2 or newer is required for TLS.")
  51. log.LOGGER.warning("[IMAP ACL] Server at %s failed to accept TLS connection because of: %s" % (IMAP_SERVER, str(err)))
  52. return False # server is not secure
  53. def server_is_local():
  54. if IMAP_HOST == "localhost":
  55. log.LOGGER.warning("[IMAP ACL] Server is local. Will allow transmitting unencrypted credentials.")
  56. return True
  57. return False # server is not local
  58. """Enforcing security policy of only transmitting credentials over TLS or to local server."""
  59. if secure_connection() or server_is_local():
  60. try:
  61. connection.login( user, password )
  62. connection.logout()
  63. log.LOGGER.debug("[IMAP ACL] Authenticated user %s via %s." % (user, IMAP_SERVER))
  64. return True
  65. except ( imaplib.IMAP4.error, imaplib.IMAP4.abort, Exception ) as err:
  66. log.LOGGER.error("[IMAP ACL] Server could not authenticate user %s because of: %s" % (user, str(err)))
  67. else:
  68. log.LOGGER.critical("[IMAP ACL] Server did not support TLS and is not ``localhost``. Refusing to transmit passwords under these conditions. Authentication attempt aborted.")
  69. return False # authentication failed