config.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008-2017 Guillaume Ayoub
  3. # Copyright © 2008 Nicolas Kandel
  4. # Copyright © 2008 Pascal Halter
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale configuration module.
  20. Give a configparser-like interface to read and write configuration.
  21. """
  22. import math
  23. import os
  24. from collections import OrderedDict
  25. from configparser import RawConfigParser as ConfigParser
  26. from . import auth, rights, storage, web
  27. def positive_int(value):
  28. value = int(value)
  29. if value < 0:
  30. raise ValueError("value is negative: %d" % value)
  31. return value
  32. def positive_float(value):
  33. value = float(value)
  34. if not math.isfinite(value):
  35. raise ValueError("value is infinite")
  36. if math.isnan(value):
  37. raise ValueError("value is not a number")
  38. if value < 0:
  39. raise ValueError("value is negative: %f" % value)
  40. return value
  41. # Default configuration
  42. INITIAL_CONFIG = OrderedDict([
  43. ("server", OrderedDict([
  44. ("hosts", {
  45. "value": "127.0.0.1:5232",
  46. "help": "set server hostnames including ports",
  47. "aliases": ["-H", "--hosts"],
  48. "type": str}),
  49. ("daemon", {
  50. "value": "False",
  51. "help": "launch as daemon",
  52. "aliases": ["-d", "--daemon"],
  53. "opposite": ["-f", "--foreground"],
  54. "type": bool}),
  55. ("pid", {
  56. "value": "",
  57. "help": "set PID filename for daemon mode",
  58. "aliases": ["-p", "--pid"],
  59. "type": str}),
  60. ("max_connections", {
  61. "value": "20",
  62. "help": "maximum number of parallel connections",
  63. "type": positive_int}),
  64. ("max_content_length", {
  65. "value": "10000000",
  66. "help": "maximum size of request body in bytes",
  67. "type": positive_int}),
  68. ("timeout", {
  69. "value": "10",
  70. "help": "socket timeout",
  71. "type": positive_int}),
  72. ("ssl", {
  73. "value": "False",
  74. "help": "use SSL connection",
  75. "aliases": ["-s", "--ssl"],
  76. "opposite": ["-S", "--no-ssl"],
  77. "type": bool}),
  78. ("certificate", {
  79. "value": "/etc/ssl/radicale.cert.pem",
  80. "help": "set certificate file",
  81. "aliases": ["-c", "--certificate"],
  82. "type": str}),
  83. ("key", {
  84. "value": "/etc/ssl/radicale.key.pem",
  85. "help": "set private key file",
  86. "aliases": ["-k", "--key"],
  87. "type": str}),
  88. ("certificate_authority", {
  89. "value": "",
  90. "help": "set CA certificate for validating clients",
  91. "aliases": ["--certificate-authority"],
  92. "type": str}),
  93. ("protocol", {
  94. "value": "PROTOCOL_TLSv1_2",
  95. "help": "SSL protocol used",
  96. "type": str}),
  97. ("ciphers", {
  98. "value": "",
  99. "help": "available ciphers",
  100. "type": str}),
  101. ("dns_lookup", {
  102. "value": "True",
  103. "help": "use reverse DNS to resolve client address in logs",
  104. "type": bool}),
  105. ("realm", {
  106. "value": "Radicale - Password Required",
  107. "help": "message displayed when a password is needed",
  108. "type": str})])),
  109. ("encoding", OrderedDict([
  110. ("request", {
  111. "value": "utf-8",
  112. "help": "encoding for responding requests",
  113. "type": str}),
  114. ("stock", {
  115. "value": "utf-8",
  116. "help": "encoding for storing local collections",
  117. "type": str})])),
  118. ("auth", OrderedDict([
  119. ("type", {
  120. "value": "none",
  121. "help": "authentication method",
  122. "type": str,
  123. "internal": auth.INTERNAL_TYPES}),
  124. ("htpasswd_filename", {
  125. "value": "/etc/radicale/users",
  126. "help": "htpasswd filename",
  127. "type": str}),
  128. ("htpasswd_encryption", {
  129. "value": "bcrypt",
  130. "help": "htpasswd encryption method",
  131. "type": str}),
  132. ("delay", {
  133. "value": "1",
  134. "help": "incorrect authentication delay",
  135. "type": positive_float})])),
  136. ("rights", OrderedDict([
  137. ("type", {
  138. "value": "owner_only",
  139. "help": "rights backend",
  140. "type": str,
  141. "internal": rights.INTERNAL_TYPES}),
  142. ("file", {
  143. "value": "/etc/radicale/rights",
  144. "help": "file for rights management from_file",
  145. "type": str})])),
  146. ("storage", OrderedDict([
  147. ("type", {
  148. "value": "multifilesystem",
  149. "help": "storage backend",
  150. "type": str,
  151. "internal": storage.INTERNAL_TYPES}),
  152. ("filesystem_folder", {
  153. "value": os.path.expanduser(
  154. "/var/lib/radicale/collections"),
  155. "help": "path where collections are stored",
  156. "type": str}),
  157. ("max_sync_token_age", {
  158. "value": 2592000, # 30 days
  159. "help": "delete sync token that are older",
  160. "type": int}),
  161. ("filesystem_fsync", {
  162. "value": "True",
  163. "help": "sync all changes to filesystem during requests",
  164. "type": bool}),
  165. ("filesystem_locking", {
  166. "value": "True",
  167. "help": "lock the storage while accessing it",
  168. "type": bool}),
  169. ("filesystem_close_lock_file", {
  170. "value": "False",
  171. "help": "close the lock file when no more clients are waiting",
  172. "type": bool}),
  173. ("hook", {
  174. "value": "",
  175. "help": "command that is run after changes to storage",
  176. "type": str})])),
  177. ("web", OrderedDict([
  178. ("type", {
  179. "value": "internal",
  180. "help": "web interface backend",
  181. "type": str,
  182. "internal": web.INTERNAL_TYPES})])),
  183. ("logging", OrderedDict([
  184. ("config", {
  185. "value": "",
  186. "help": "logging configuration file",
  187. "type": str}),
  188. ("debug", {
  189. "value": "False",
  190. "help": "print debug information",
  191. "aliases": ["-D", "--debug"],
  192. "type": bool}),
  193. ("full_environment", {
  194. "value": "False",
  195. "help": "store all environment variables",
  196. "type": bool}),
  197. ("mask_passwords", {
  198. "value": "True",
  199. "help": "mask passwords in logs",
  200. "type": bool})]))])
  201. def load(paths=(), extra_config=None, ignore_missing_paths=True):
  202. config = ConfigParser()
  203. for section, values in INITIAL_CONFIG.items():
  204. config.add_section(section)
  205. for key, data in values.items():
  206. config.set(section, key, data["value"])
  207. if extra_config:
  208. for section, values in extra_config.items():
  209. for key, value in values.items():
  210. config.set(section, key, value)
  211. for path in paths:
  212. if path or not ignore_missing_paths:
  213. try:
  214. if not config.read(path) and not ignore_missing_paths:
  215. raise RuntimeError("No such file: %r" % path)
  216. except Exception as e:
  217. raise RuntimeError(
  218. "Failed to load config file %r: %s" % (path, e)) from e
  219. # Check the configuration
  220. for section in config.sections():
  221. if section == "headers":
  222. continue
  223. if section not in INITIAL_CONFIG:
  224. raise RuntimeError("Invalid section %r in config" % section)
  225. allow_extra_options = ("type" in INITIAL_CONFIG[section] and
  226. config.get(section, "type") not in
  227. INITIAL_CONFIG[section]["type"].get("internal",
  228. ()))
  229. for option in config[section]:
  230. if option not in INITIAL_CONFIG[section]:
  231. if allow_extra_options:
  232. continue
  233. raise RuntimeError("Invalid option %r in section %r in "
  234. "config" % (option, section))
  235. type_ = INITIAL_CONFIG[section][option]["type"]
  236. try:
  237. if type_ == bool:
  238. config.getboolean(section, option)
  239. else:
  240. type_(config.get(section, option))
  241. except Exception as e:
  242. raise RuntimeError(
  243. "Invalid %s value for option %r in section %r in config: "
  244. "%r" % (type_.__name__, option, section,
  245. config.get(section, option))) from e
  246. return config