utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 datetime
  20. import os
  21. import ssl
  22. import sys
  23. import textwrap
  24. from importlib import import_module, metadata
  25. from typing import Callable, Sequence, Tuple, Type, TypeVar, Union
  26. from radicale import config
  27. from radicale.log import logger
  28. if sys.platform != "win32":
  29. import grp
  30. import pwd
  31. _T_co = TypeVar("_T_co", covariant=True)
  32. RADICALE_MODULES: Sequence[str] = ("radicale", "vobject", "passlib", "defusedxml",
  33. "bcrypt",
  34. "argon2-cffi",
  35. "pika",
  36. "ldap",
  37. "ldap3",
  38. "pam")
  39. # IPv4 (host, port) and IPv6 (host, port, flowinfo, scopeid)
  40. ADDRESS_TYPE = Union[Tuple[Union[str, bytes, bytearray], int],
  41. Tuple[str, int, int, int]]
  42. # Max/Min YEAR in datetime in unixtime
  43. DATETIME_MAX_UNIXTIME: int = (datetime.MAXYEAR - 1970) * 365 * 24 * 60 * 60
  44. DATETIME_MIN_UNIXTIME: int = (datetime.MINYEAR - 1970) * 365 * 24 * 60 * 60
  45. def load_plugin(internal_types: Sequence[str], module_name: str,
  46. class_name: str, base_class: Type[_T_co],
  47. configuration: "config.Configuration") -> _T_co:
  48. type_: Union[str, Callable] = configuration.get(module_name, "type")
  49. if callable(type_):
  50. logger.info("%s type is %r", module_name, type_)
  51. return type_(configuration)
  52. if type_ in internal_types:
  53. module = "radicale.%s.%s" % (module_name, type_)
  54. else:
  55. module = type_
  56. try:
  57. class_ = getattr(import_module(module), class_name)
  58. except Exception as e:
  59. raise RuntimeError("Failed to load %s module %r: %s" %
  60. (module_name, module, e)) from e
  61. logger.info("%s type is %r", module_name, module)
  62. return class_(configuration)
  63. def package_version(name):
  64. return metadata.version(name)
  65. def packages_version():
  66. versions = []
  67. versions.append("python=%s.%s.%s" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
  68. for pkg in RADICALE_MODULES:
  69. try:
  70. versions.append("%s=%s" % (pkg, package_version(pkg)))
  71. except Exception:
  72. try:
  73. versions.append("%s=%s" % (pkg, package_version("python-" + pkg)))
  74. except Exception:
  75. versions.append("%s=%s" % (pkg, "n/a"))
  76. return " ".join(versions)
  77. def format_address(address: ADDRESS_TYPE) -> str:
  78. host, port, *_ = address
  79. if not isinstance(host, str):
  80. raise NotImplementedError("Unsupported address format: %r" %
  81. (address,))
  82. if host.find(":") == -1:
  83. return "%s:%d" % (host, port)
  84. else:
  85. return "[%s]:%d" % (host, port)
  86. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  87. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  88. # disable any protocol by default
  89. logger.debug("SSL context options, disable ALL by default")
  90. ssl_context_options |= ssl.OP_NO_SSLv2
  91. ssl_context_options |= ssl.OP_NO_SSLv3
  92. ssl_context_options |= ssl.OP_NO_TLSv1
  93. ssl_context_options |= ssl.OP_NO_TLSv1_1
  94. ssl_context_options |= ssl.OP_NO_TLSv1_2
  95. ssl_context_options |= ssl.OP_NO_TLSv1_3
  96. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  97. for entry in protocol.split():
  98. entry = entry.strip('+') # remove trailing '+'
  99. if entry == "ALL":
  100. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  101. ssl_context_options &= ~ssl.OP_NO_SSLv3
  102. ssl_context_options &= ~ssl.OP_NO_TLSv1
  103. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  104. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  105. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  106. elif entry == "SSLv2":
  107. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  108. elif entry == "SSLv3":
  109. ssl_context_options &= ~ssl.OP_NO_SSLv3
  110. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  111. elif entry == "TLSv1":
  112. ssl_context_options &= ~ssl.OP_NO_TLSv1
  113. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  114. elif entry == "TLSv1.1":
  115. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  116. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  117. elif entry == "TLSv1.2":
  118. logger.debug("SSL context options, enable TLSv1.2")
  119. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  120. elif entry == "TLSv1.3":
  121. logger.debug("SSL context options, enable TLSv1.3")
  122. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  123. elif entry == "-ALL":
  124. logger.debug("SSL context options, disable ALL")
  125. ssl_context_options |= ssl.OP_NO_SSLv2
  126. ssl_context_options |= ssl.OP_NO_SSLv3
  127. ssl_context_options |= ssl.OP_NO_TLSv1
  128. ssl_context_options |= ssl.OP_NO_TLSv1_1
  129. ssl_context_options |= ssl.OP_NO_TLSv1_2
  130. ssl_context_options |= ssl.OP_NO_TLSv1_3
  131. elif entry == "-SSLv2":
  132. ssl_context_options |= ssl.OP_NO_SSLv2
  133. logger.debug("SSL context options, disable SSLv2")
  134. elif entry == "-SSLv3":
  135. ssl_context_options |= ssl.OP_NO_SSLv3
  136. logger.debug("SSL context options, disable SSLv3")
  137. elif entry == "-TLSv1":
  138. logger.debug("SSL context options, disable TLSv1")
  139. ssl_context_options |= ssl.OP_NO_TLSv1
  140. elif entry == "-TLSv1.1":
  141. logger.debug("SSL context options, disable TLSv1.1")
  142. ssl_context_options |= ssl.OP_NO_TLSv1_1
  143. elif entry == "-TLSv1.2":
  144. logger.debug("SSL context options, disable TLSv1.2")
  145. ssl_context_options |= ssl.OP_NO_TLSv1_2
  146. elif entry == "-TLSv1.3":
  147. logger.debug("SSL context options, disable TLSv1.3")
  148. ssl_context_options |= ssl.OP_NO_TLSv1_3
  149. else:
  150. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  151. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  152. return ssl_context_options
  153. def ssl_context_minimum_version_by_options(ssl_context_options):
  154. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  155. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  156. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  157. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  158. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  159. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  160. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  161. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  162. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  163. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  164. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  165. ssl_context_minimum_version = 0 # all disabled
  166. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  167. return ssl_context_minimum_version
  168. def ssl_context_maximum_version_by_options(ssl_context_options):
  169. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  170. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  171. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  172. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  173. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  174. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  175. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  176. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  177. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  178. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  179. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  180. ssl_context_maximum_version = 0
  181. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  182. return ssl_context_maximum_version
  183. def ssl_get_protocols(context):
  184. protocols = []
  185. if not (context.options & ssl.OP_NO_SSLv3):
  186. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  187. protocols.append("SSLv3")
  188. if not (context.options & ssl.OP_NO_TLSv1):
  189. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  190. protocols.append("TLSv1")
  191. if not (context.options & ssl.OP_NO_TLSv1_1):
  192. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  193. protocols.append("TLSv1.1")
  194. if not (context.options & ssl.OP_NO_TLSv1_2):
  195. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  196. protocols.append("TLSv1.2")
  197. if not (context.options & ssl.OP_NO_TLSv1_3):
  198. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  199. protocols.append("TLSv1.3")
  200. return protocols
  201. def unknown_if_empty(value):
  202. if value == "":
  203. return "UNKNOWN"
  204. else:
  205. return value
  206. def user_groups_as_string():
  207. if sys.platform != "win32":
  208. euid = os.geteuid()
  209. try:
  210. username = pwd.getpwuid(euid)[0]
  211. user = "%s(%d)" % (unknown_if_empty(username), euid)
  212. except Exception:
  213. # name of user not found
  214. user = "UNKNOWN(%d)" % euid
  215. egid = os.getegid()
  216. groups = []
  217. try:
  218. gids = os.getgrouplist(username, egid)
  219. for gid in gids:
  220. try:
  221. gi = grp.getgrgid(gid)
  222. groups.append("%s(%d)" % (unknown_if_empty(gi.gr_name), gid))
  223. except Exception:
  224. groups.append("UNKNOWN(%d)" % gid)
  225. except Exception:
  226. try:
  227. groups.append("%s(%d)" % (grp.getgrnam(egid)[0], egid))
  228. except Exception:
  229. # workaround to get groupid by name
  230. groups_all = grp.getgrall()
  231. found = False
  232. for entry in groups_all:
  233. if entry[2] == egid:
  234. groups.append("%s(%d)" % (unknown_if_empty(entry[0]), egid))
  235. found = True
  236. break
  237. if not found:
  238. groups.append("UNKNOWN(%d)" % egid)
  239. s = "user=%s groups=%s" % (user, ','.join(groups))
  240. else:
  241. username = os.getlogin()
  242. s = "user=%s" % (username)
  243. return s
  244. def format_ut(unixtime: int) -> str:
  245. if sys.platform == "win32":
  246. # TODO check how to support this better
  247. return str(unixtime)
  248. if unixtime <= DATETIME_MIN_UNIXTIME:
  249. r = str(unixtime) + "(<=MIN:" + str(DATETIME_MIN_UNIXTIME) + ")"
  250. elif unixtime >= DATETIME_MAX_UNIXTIME:
  251. r = str(unixtime) + "(>=MAX:" + str(DATETIME_MAX_UNIXTIME) + ")"
  252. else:
  253. if sys.version_info < (3, 11):
  254. dt = datetime.datetime.utcfromtimestamp(unixtime)
  255. else:
  256. dt = datetime.datetime.fromtimestamp(unixtime, datetime.UTC)
  257. r = str(unixtime) + "(" + dt.strftime('%Y-%m-%dT%H:%M:%SZ') + ")"
  258. return r
  259. def limit_str(content: str, limit: int) -> str:
  260. length = len(content)
  261. if limit > 0 and length >= limit:
  262. return content[:limit] + ("...(shortened because original length %d > limit %d)" % (length, limit))
  263. else:
  264. return content
  265. def textwrap_str(content: str, limit: int = 2000) -> str:
  266. # TODO: add support for config option and prefix
  267. return textwrap.indent(limit_str(content, limit), " ", lambda line: True)