utils.py 14 KB

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