utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import os
  20. import ssl
  21. import sys
  22. from importlib import import_module, metadata
  23. from typing import Callable, Sequence, Tuple, Type, TypeVar, Union
  24. from radicale import config
  25. from radicale.log import logger
  26. if sys.platform != "win32":
  27. import grp
  28. import pwd
  29. _T_co = TypeVar("_T_co", covariant=True)
  30. RADICALE_MODULES: Sequence[str] = ("radicale", "vobject", "passlib", "defusedxml",
  31. "bcrypt",
  32. "argon2-cffi",
  33. "pika",
  34. "ldap",
  35. "ldap3",
  36. "pam")
  37. # IPv4 (host, port) and IPv6 (host, port, flowinfo, scopeid)
  38. ADDRESS_TYPE = Union[Tuple[Union[str, bytes, bytearray], int],
  39. Tuple[str, int, int, int]]
  40. def load_plugin(internal_types: Sequence[str], module_name: str,
  41. class_name: str, base_class: Type[_T_co],
  42. configuration: "config.Configuration") -> _T_co:
  43. type_: Union[str, Callable] = configuration.get(module_name, "type")
  44. if callable(type_):
  45. logger.info("%s type is %r", module_name, type_)
  46. return type_(configuration)
  47. if type_ in internal_types:
  48. module = "radicale.%s.%s" % (module_name, type_)
  49. else:
  50. module = type_
  51. try:
  52. class_ = getattr(import_module(module), class_name)
  53. except Exception as e:
  54. raise RuntimeError("Failed to load %s module %r: %s" %
  55. (module_name, module, e)) from e
  56. logger.info("%s type is %r", module_name, module)
  57. return class_(configuration)
  58. def package_version(name):
  59. return metadata.version(name)
  60. def packages_version():
  61. versions = []
  62. versions.append("python=%s.%s.%s" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
  63. for pkg in RADICALE_MODULES:
  64. try:
  65. versions.append("%s=%s" % (pkg, package_version(pkg)))
  66. except Exception:
  67. try:
  68. versions.append("%s=%s" % (pkg, package_version("python-" + pkg)))
  69. except Exception:
  70. versions.append("%s=%s" % (pkg, "n/a"))
  71. return " ".join(versions)
  72. def format_address(address: ADDRESS_TYPE) -> str:
  73. host, port, *_ = address
  74. if not isinstance(host, str):
  75. raise NotImplementedError("Unsupported address format: %r" %
  76. (address,))
  77. if host.find(":") == -1:
  78. return "%s:%d" % (host, port)
  79. else:
  80. return "[%s]:%d" % (host, port)
  81. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  82. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  83. # disable any protocol by default
  84. logger.debug("SSL context options, disable ALL by default")
  85. ssl_context_options |= ssl.OP_NO_SSLv2
  86. ssl_context_options |= ssl.OP_NO_SSLv3
  87. ssl_context_options |= ssl.OP_NO_TLSv1
  88. ssl_context_options |= ssl.OP_NO_TLSv1_1
  89. ssl_context_options |= ssl.OP_NO_TLSv1_2
  90. ssl_context_options |= ssl.OP_NO_TLSv1_3
  91. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  92. for entry in protocol.split():
  93. entry = entry.strip('+') # remove trailing '+'
  94. if entry == "ALL":
  95. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  96. ssl_context_options &= ~ssl.OP_NO_SSLv3
  97. ssl_context_options &= ~ssl.OP_NO_TLSv1
  98. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  99. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  100. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  101. elif entry == "SSLv2":
  102. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  103. elif entry == "SSLv3":
  104. ssl_context_options &= ~ssl.OP_NO_SSLv3
  105. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  106. elif entry == "TLSv1":
  107. ssl_context_options &= ~ssl.OP_NO_TLSv1
  108. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  109. elif entry == "TLSv1.1":
  110. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  111. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  112. elif entry == "TLSv1.2":
  113. logger.debug("SSL context options, enable TLSv1.2")
  114. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  115. elif entry == "TLSv1.3":
  116. logger.debug("SSL context options, enable TLSv1.3")
  117. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  118. elif entry == "-ALL":
  119. logger.debug("SSL context options, disable ALL")
  120. ssl_context_options |= ssl.OP_NO_SSLv2
  121. ssl_context_options |= ssl.OP_NO_SSLv3
  122. ssl_context_options |= ssl.OP_NO_TLSv1
  123. ssl_context_options |= ssl.OP_NO_TLSv1_1
  124. ssl_context_options |= ssl.OP_NO_TLSv1_2
  125. ssl_context_options |= ssl.OP_NO_TLSv1_3
  126. elif entry == "-SSLv2":
  127. ssl_context_options |= ssl.OP_NO_SSLv2
  128. logger.debug("SSL context options, disable SSLv2")
  129. elif entry == "-SSLv3":
  130. ssl_context_options |= ssl.OP_NO_SSLv3
  131. logger.debug("SSL context options, disable SSLv3")
  132. elif entry == "-TLSv1":
  133. logger.debug("SSL context options, disable TLSv1")
  134. ssl_context_options |= ssl.OP_NO_TLSv1
  135. elif entry == "-TLSv1.1":
  136. logger.debug("SSL context options, disable TLSv1.1")
  137. ssl_context_options |= ssl.OP_NO_TLSv1_1
  138. elif entry == "-TLSv1.2":
  139. logger.debug("SSL context options, disable TLSv1.2")
  140. ssl_context_options |= ssl.OP_NO_TLSv1_2
  141. elif entry == "-TLSv1.3":
  142. logger.debug("SSL context options, disable TLSv1.3")
  143. ssl_context_options |= ssl.OP_NO_TLSv1_3
  144. else:
  145. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  146. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  147. return ssl_context_options
  148. def ssl_context_minimum_version_by_options(ssl_context_options):
  149. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  150. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  151. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  152. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  153. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  154. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  155. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  156. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  157. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  158. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  159. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  160. ssl_context_minimum_version = 0 # all disabled
  161. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  162. return ssl_context_minimum_version
  163. def ssl_context_maximum_version_by_options(ssl_context_options):
  164. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  165. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  166. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  167. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  168. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  169. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  170. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  171. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  172. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  173. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  174. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  175. ssl_context_maximum_version = 0
  176. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  177. return ssl_context_maximum_version
  178. def ssl_get_protocols(context):
  179. protocols = []
  180. if not (context.options & ssl.OP_NO_SSLv3):
  181. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  182. protocols.append("SSLv3")
  183. if not (context.options & ssl.OP_NO_TLSv1):
  184. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  185. protocols.append("TLSv1")
  186. if not (context.options & ssl.OP_NO_TLSv1_1):
  187. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  188. protocols.append("TLSv1.1")
  189. if not (context.options & ssl.OP_NO_TLSv1_2):
  190. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  191. protocols.append("TLSv1.2")
  192. if not (context.options & ssl.OP_NO_TLSv1_3):
  193. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  194. protocols.append("TLSv1.3")
  195. return protocols
  196. def user_groups_as_string():
  197. if sys.platform != "win32":
  198. euid = os.geteuid()
  199. egid = os.getegid()
  200. username = pwd.getpwuid(euid)[0]
  201. gids = os.getgrouplist(username, egid)
  202. groups = []
  203. for gid in gids:
  204. try:
  205. gi = grp.getgrgid(gid)
  206. groups.append("%s(%d)" % (gi.gr_name, gid))
  207. except Exception:
  208. groups.append("%s(%d)" % (gid, gid))
  209. s = "user=%s(%d) groups=%s" % (username, euid, ','.join(groups))
  210. else:
  211. username = os.getlogin()
  212. s = "user=%s" % (username)
  213. return s