utils.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 hashlib import sha256
  25. from importlib import import_module, metadata
  26. from string import ascii_letters, digits, punctuation
  27. from typing import Callable, Sequence, Tuple, Type, TypeVar, Union
  28. from radicale import config
  29. from radicale.log import logger
  30. if sys.platform != "win32":
  31. import grp
  32. import pwd
  33. _T_co = TypeVar("_T_co", covariant=True)
  34. RADICALE_MODULES: Sequence[str] = ("radicale", "vobject", "passlib", "defusedxml",
  35. "bcrypt",
  36. "argon2-cffi",
  37. "pika",
  38. "ldap",
  39. "ldap3",
  40. "pam")
  41. # IPv4 (host, port) and IPv6 (host, port, flowinfo, scopeid)
  42. ADDRESS_TYPE = Union[Tuple[Union[str, bytes, bytearray], int],
  43. Tuple[str, int, int, int]]
  44. # Max/Min YEAR in datetime in unixtime
  45. DATETIME_MAX_UNIXTIME: int = (datetime.MAXYEAR - 1970) * 365 * 24 * 60 * 60
  46. DATETIME_MIN_UNIXTIME: int = (datetime.MINYEAR - 1970) * 365 * 24 * 60 * 60
  47. # Number units
  48. UNIT_g: int = (1000 * 1000 * 1000)
  49. UNIT_m: int = (1000 * 1000)
  50. UNIT_k: int = (1000)
  51. UNIT_G: int = (1024 * 1024 * 1024)
  52. UNIT_M: int = (1024 * 1024)
  53. UNIT_K: int = (1024)
  54. def load_plugin(internal_types: Sequence[str], module_name: str,
  55. class_name: str, base_class: Type[_T_co],
  56. configuration: "config.Configuration") -> _T_co:
  57. type_: Union[str, Callable] = configuration.get(module_name, "type")
  58. if callable(type_):
  59. logger.info("%s type is %r", module_name, type_)
  60. return type_(configuration)
  61. if type_ in internal_types:
  62. module = "radicale.%s.%s" % (module_name, type_)
  63. else:
  64. module = type_
  65. try:
  66. class_ = getattr(import_module(module), class_name)
  67. except Exception as e:
  68. raise RuntimeError("Failed to load %s module %r: %s" %
  69. (module_name, module, e)) from e
  70. logger.info("%s type is %r", module_name, module)
  71. return class_(configuration)
  72. def package_version(name):
  73. return metadata.version(name)
  74. def vobject_supports_vcard4() -> bool:
  75. """Check if vobject supports vCard 4.0 (requires version >= 1.0.0)."""
  76. try:
  77. version = package_version("vobject")
  78. parts = version.split(".")
  79. major = int(parts[0])
  80. return major >= 1
  81. except Exception:
  82. return False
  83. def packages_version():
  84. versions = []
  85. versions.append("python=%s.%s.%s" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
  86. for pkg in RADICALE_MODULES:
  87. try:
  88. versions.append("%s=%s" % (pkg, package_version(pkg)))
  89. except Exception:
  90. try:
  91. versions.append("%s=%s" % (pkg, package_version("python-" + pkg)))
  92. except Exception:
  93. versions.append("%s=%s" % (pkg, "n/a"))
  94. return " ".join(versions)
  95. def format_address(address: ADDRESS_TYPE) -> str:
  96. host, port, *_ = address
  97. if not isinstance(host, str):
  98. raise NotImplementedError("Unsupported address format: %r" %
  99. (address,))
  100. if host.find(":") == -1:
  101. return "%s:%d" % (host, port)
  102. else:
  103. return "[%s]:%d" % (host, port)
  104. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  105. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  106. # disable any protocol by default
  107. logger.debug("SSL context options, disable ALL by default")
  108. ssl_context_options |= ssl.OP_NO_SSLv2
  109. ssl_context_options |= ssl.OP_NO_SSLv3
  110. ssl_context_options |= ssl.OP_NO_TLSv1
  111. ssl_context_options |= ssl.OP_NO_TLSv1_1
  112. ssl_context_options |= ssl.OP_NO_TLSv1_2
  113. ssl_context_options |= ssl.OP_NO_TLSv1_3
  114. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  115. for entry in protocol.split():
  116. entry = entry.strip('+') # remove trailing '+'
  117. if entry == "ALL":
  118. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  119. ssl_context_options &= ~ssl.OP_NO_SSLv3
  120. ssl_context_options &= ~ssl.OP_NO_TLSv1
  121. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  122. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  123. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  124. elif entry == "SSLv2":
  125. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  126. elif entry == "SSLv3":
  127. ssl_context_options &= ~ssl.OP_NO_SSLv3
  128. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  129. elif entry == "TLSv1":
  130. ssl_context_options &= ~ssl.OP_NO_TLSv1
  131. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  132. elif entry == "TLSv1.1":
  133. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  134. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  135. elif entry == "TLSv1.2":
  136. logger.debug("SSL context options, enable TLSv1.2")
  137. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  138. elif entry == "TLSv1.3":
  139. logger.debug("SSL context options, enable TLSv1.3")
  140. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  141. elif entry == "-ALL":
  142. logger.debug("SSL context options, disable ALL")
  143. ssl_context_options |= ssl.OP_NO_SSLv2
  144. ssl_context_options |= ssl.OP_NO_SSLv3
  145. ssl_context_options |= ssl.OP_NO_TLSv1
  146. ssl_context_options |= ssl.OP_NO_TLSv1_1
  147. ssl_context_options |= ssl.OP_NO_TLSv1_2
  148. ssl_context_options |= ssl.OP_NO_TLSv1_3
  149. elif entry == "-SSLv2":
  150. ssl_context_options |= ssl.OP_NO_SSLv2
  151. logger.debug("SSL context options, disable SSLv2")
  152. elif entry == "-SSLv3":
  153. ssl_context_options |= ssl.OP_NO_SSLv3
  154. logger.debug("SSL context options, disable SSLv3")
  155. elif entry == "-TLSv1":
  156. logger.debug("SSL context options, disable TLSv1")
  157. ssl_context_options |= ssl.OP_NO_TLSv1
  158. elif entry == "-TLSv1.1":
  159. logger.debug("SSL context options, disable TLSv1.1")
  160. ssl_context_options |= ssl.OP_NO_TLSv1_1
  161. elif entry == "-TLSv1.2":
  162. logger.debug("SSL context options, disable TLSv1.2")
  163. ssl_context_options |= ssl.OP_NO_TLSv1_2
  164. elif entry == "-TLSv1.3":
  165. logger.debug("SSL context options, disable TLSv1.3")
  166. ssl_context_options |= ssl.OP_NO_TLSv1_3
  167. else:
  168. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  169. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  170. return ssl_context_options
  171. def ssl_context_minimum_version_by_options(ssl_context_options):
  172. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  173. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  174. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  175. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  176. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  177. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  178. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  179. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  180. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  181. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  182. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  183. ssl_context_minimum_version = 0 # all disabled
  184. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  185. return ssl_context_minimum_version
  186. def ssl_context_maximum_version_by_options(ssl_context_options):
  187. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  188. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  189. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  190. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  191. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  192. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  193. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  194. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  195. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  196. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  197. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  198. ssl_context_maximum_version = 0
  199. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  200. return ssl_context_maximum_version
  201. def ssl_get_protocols(context):
  202. protocols = []
  203. if not (context.options & ssl.OP_NO_SSLv3):
  204. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  205. protocols.append("SSLv3")
  206. if not (context.options & ssl.OP_NO_TLSv1):
  207. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  208. protocols.append("TLSv1")
  209. if not (context.options & ssl.OP_NO_TLSv1_1):
  210. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  211. protocols.append("TLSv1.1")
  212. if not (context.options & ssl.OP_NO_TLSv1_2):
  213. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  214. protocols.append("TLSv1.2")
  215. if not (context.options & ssl.OP_NO_TLSv1_3):
  216. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  217. protocols.append("TLSv1.3")
  218. return protocols
  219. def unknown_if_empty(value):
  220. if value == "":
  221. return "UNKNOWN"
  222. else:
  223. return value
  224. def user_groups_as_string():
  225. if sys.platform != "win32":
  226. euid = os.geteuid()
  227. try:
  228. username = pwd.getpwuid(euid)[0]
  229. user = "%s(%d)" % (unknown_if_empty(username), euid)
  230. except Exception:
  231. # name of user not found
  232. user = "UNKNOWN(%d)" % euid
  233. egid = os.getegid()
  234. groups = []
  235. try:
  236. gids = os.getgrouplist(username, egid)
  237. for gid in gids:
  238. try:
  239. gi = grp.getgrgid(gid)
  240. groups.append("%s(%d)" % (unknown_if_empty(gi.gr_name), gid))
  241. except Exception:
  242. groups.append("UNKNOWN(%d)" % gid)
  243. except Exception:
  244. try:
  245. groups.append("%s(%d)" % (grp.getgrnam(egid)[0], egid))
  246. except Exception:
  247. # workaround to get groupid by name
  248. groups_all = grp.getgrall()
  249. found = False
  250. for entry in groups_all:
  251. if entry[2] == egid:
  252. groups.append("%s(%d)" % (unknown_if_empty(entry[0]), egid))
  253. found = True
  254. break
  255. if not found:
  256. groups.append("UNKNOWN(%d)" % egid)
  257. s = "user=%s groups=%s" % (user, ','.join(groups))
  258. else:
  259. username = os.getlogin()
  260. s = "user=%s" % (username)
  261. return s
  262. def format_ut(unixtime: int) -> str:
  263. if sys.platform == "win32":
  264. # TODO check how to support this better
  265. return str(unixtime)
  266. if unixtime <= DATETIME_MIN_UNIXTIME:
  267. r = str(unixtime) + "(<=MIN:" + str(DATETIME_MIN_UNIXTIME) + ")"
  268. elif unixtime >= DATETIME_MAX_UNIXTIME:
  269. r = str(unixtime) + "(>=MAX:" + str(DATETIME_MAX_UNIXTIME) + ")"
  270. else:
  271. if sys.version_info < (3, 11):
  272. dt = datetime.datetime.utcfromtimestamp(unixtime)
  273. else:
  274. dt = datetime.datetime.fromtimestamp(unixtime, datetime.UTC)
  275. r = str(unixtime) + "(" + dt.strftime('%Y-%m-%dT%H:%M:%SZ') + ")"
  276. return r
  277. def format_unit(value: float, binary: bool = False) -> str:
  278. if binary:
  279. if value > UNIT_G:
  280. value = value / UNIT_G
  281. unit = "G"
  282. elif value > UNIT_M:
  283. value = value / UNIT_M
  284. unit = "M"
  285. elif value > UNIT_K:
  286. value = value / UNIT_K
  287. unit = "K"
  288. else:
  289. unit = ""
  290. else:
  291. if value > UNIT_g:
  292. value = value / UNIT_g
  293. unit = "g"
  294. elif value > UNIT_m:
  295. value = value / UNIT_m
  296. unit = "m"
  297. elif value > UNIT_k:
  298. value = value / UNIT_k
  299. unit = "k"
  300. else:
  301. unit = ""
  302. return ("%.1f %s" % (value, unit))
  303. def limit_str(content: str, limit: int) -> str:
  304. length = len(content)
  305. if limit > 0 and length >= limit:
  306. return content[:limit] + ("...(shortened because original length %d > limit %d)" % (length, limit))
  307. else:
  308. return content
  309. def textwrap_str(content: str, limit: int = 2000) -> str:
  310. # TODO: add support for config option and prefix
  311. return textwrap.indent(limit_str(content, limit), " ", lambda line: True)
  312. def dataToHex(data, count):
  313. result = ''
  314. for item in range(count):
  315. if ((item > 0) and ((item % 8) == 0)):
  316. result += ' '
  317. if (item < len(data)):
  318. result += '%02x' % data[item] + ' '
  319. else:
  320. result += ' '
  321. return result
  322. def dataToAscii(data, count):
  323. result = ''
  324. for item in range(count):
  325. if (item < len(data)):
  326. char = chr(data[item])
  327. if char in ascii_letters or \
  328. char in digits or \
  329. char in punctuation or \
  330. char == ' ':
  331. result += char
  332. else:
  333. result += '.'
  334. return result
  335. def dataToSpecial(data, count):
  336. result = ''
  337. for item in range(count):
  338. if (item < len(data)):
  339. char = chr(data[item])
  340. if char == '\r':
  341. result += 'C'
  342. elif char == '\n':
  343. result += 'L'
  344. elif (ord(char) & 0xf8) == 0xf0: # assuming UTF-8
  345. result += '4'
  346. elif (ord(char) & 0xf0) == 0xf0: # assuming UTF-8
  347. result += '3'
  348. elif (ord(char) & 0xe0) == 0xe0: # assuming UTF-8
  349. result += '2'
  350. else:
  351. result += '.'
  352. return result
  353. def hexdump_str(content: str, limit: int = 2000) -> str:
  354. result = "Hexdump of string: index <bytes> | <ASCII> | <CTRL: C=CR L=LF 2/3/4=UTF-8-length> |\n"
  355. index = 0
  356. size = 16
  357. bytestring = content.encode("utf-8") # assuming UTF-8
  358. length = len(bytestring)
  359. while (index < length) and (index < limit):
  360. data = bytestring[index:index+size]
  361. hex = dataToHex(data, size)
  362. ascii = dataToAscii(data, size)
  363. special = dataToSpecial(data, size)
  364. result += '%08x ' % index
  365. result += hex
  366. result += '|'
  367. result += '%-16s' % ascii
  368. result += '|'
  369. result += '%-16s' % special
  370. result += '|'
  371. result += '\n'
  372. index += size
  373. return result
  374. def hexdump_line(line: str, limit: int = 200) -> str:
  375. result = ""
  376. length_str = len(line)
  377. bytestring = line.encode("utf-8") # assuming UTF-8
  378. length = len(bytestring)
  379. size = length
  380. if (size > limit):
  381. size = limit
  382. hex = dataToHex(bytestring, size)
  383. ascii = dataToAscii(bytestring, size)
  384. special = dataToSpecial(bytestring, size)
  385. result += '%3d/%3d' % (length_str, length)
  386. result += ': '
  387. result += hex
  388. result += '|'
  389. result += ascii
  390. result += '|'
  391. result += special
  392. result += '|'
  393. result += '\n'
  394. return result
  395. def hexdump_lines(lines: str, limit: int = 200) -> str:
  396. result = "Hexdump of lines: nr chars/bytes: <bytes> | <ASCII> | <CTRL: C=CR L=LF 2/3/4=UTF-8-length> |\n"
  397. counter = 0
  398. for line in lines.splitlines(True):
  399. result += '% 4d ' % counter
  400. result += hexdump_line(line)
  401. counter += 1
  402. return result
  403. def sha256_str(content: str) -> str:
  404. _hash = sha256()
  405. _hash.update(content.encode("utf-8")) # assuming UTF-8
  406. return _hash.hexdigest()
  407. def sha256_bytes(content: bytes) -> str:
  408. _hash = sha256()
  409. _hash.update(content)
  410. return _hash.hexdigest()