config.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. def positive_int(value):
  27. value = int(value)
  28. if value < 0:
  29. raise ValueError("value is negative: %d" % value)
  30. return value
  31. def positive_float(value):
  32. value = float(value)
  33. if not math.isfinite(value):
  34. raise ValueError("value is infinite")
  35. if math.isnan(value):
  36. raise ValueError("value is not a number")
  37. if value < 0:
  38. raise ValueError("value is negative: %f" % value)
  39. return value
  40. # Default configuration
  41. INITIAL_CONFIG = OrderedDict([
  42. ("server", OrderedDict([
  43. ("hosts", {
  44. "value": "127.0.0.1:5232",
  45. "help": "set server hostnames including ports",
  46. "aliases": ["-H", "--hosts"],
  47. "type": str}),
  48. ("daemon", {
  49. "value": "False",
  50. "help": "launch as daemon",
  51. "aliases": ["-d", "--daemon"],
  52. "opposite": ["-f", "--foreground"],
  53. "type": bool}),
  54. ("pid", {
  55. "value": "",
  56. "help": "set PID filename for daemon mode",
  57. "aliases": ["-p", "--pid"],
  58. "type": str}),
  59. ("max_connections", {
  60. "value": "20",
  61. "help": "maximum number of parallel connections",
  62. "type": positive_int}),
  63. ("max_content_length", {
  64. "value": "10000000",
  65. "help": "maximum size of request body in bytes",
  66. "type": positive_int}),
  67. ("timeout", {
  68. "value": "10",
  69. "help": "socket timeout",
  70. "type": positive_int}),
  71. ("ssl", {
  72. "value": "False",
  73. "help": "use SSL connection",
  74. "aliases": ["-s", "--ssl"],
  75. "opposite": ["-S", "--no-ssl"],
  76. "type": bool}),
  77. ("certificate", {
  78. "value": "/etc/ssl/radicale.cert.pem",
  79. "help": "set certificate file",
  80. "aliases": ["-c", "--certificate"],
  81. "type": str}),
  82. ("key", {
  83. "value": "/etc/ssl/radicale.key.pem",
  84. "help": "set private key file",
  85. "aliases": ["-k", "--key"],
  86. "type": str}),
  87. ("certificate_authority", {
  88. "value": "",
  89. "help": "set CA certificate for validating clients",
  90. "aliases": ["--certificate-authority"],
  91. "type": str}),
  92. ("protocol", {
  93. "value": "PROTOCOL_TLSv1_2",
  94. "help": "SSL protocol used",
  95. "type": str}),
  96. ("ciphers", {
  97. "value": "",
  98. "help": "available ciphers",
  99. "type": str}),
  100. ("dns_lookup", {
  101. "value": "True",
  102. "help": "use reverse DNS to resolve client address in logs",
  103. "type": bool}),
  104. ("realm", {
  105. "value": "Radicale - Password Required",
  106. "help": "message displayed when a password is needed",
  107. "type": str})])),
  108. ("encoding", OrderedDict([
  109. ("request", {
  110. "value": "utf-8",
  111. "help": "encoding for responding requests",
  112. "type": str}),
  113. ("stock", {
  114. "value": "utf-8",
  115. "help": "encoding for storing local collections",
  116. "type": str})])),
  117. ("auth", OrderedDict([
  118. ("type", {
  119. "value": "none",
  120. "help": "authentication method",
  121. "type": str}),
  122. ("htpasswd_filename", {
  123. "value": "/etc/radicale/users",
  124. "help": "htpasswd filename",
  125. "type": str}),
  126. ("htpasswd_encryption", {
  127. "value": "bcrypt",
  128. "help": "htpasswd encryption method",
  129. "type": str}),
  130. ("delay", {
  131. "value": "1",
  132. "help": "incorrect authentication delay",
  133. "type": positive_float})])),
  134. ("rights", OrderedDict([
  135. ("type", {
  136. "value": "owner_only",
  137. "help": "rights backend",
  138. "type": str}),
  139. ("file", {
  140. "value": "/etc/radicale/rights",
  141. "help": "file for rights management from_file",
  142. "type": str})])),
  143. ("storage", OrderedDict([
  144. ("type", {
  145. "value": "multifilesystem",
  146. "help": "storage backend",
  147. "type": str}),
  148. ("filesystem_folder", {
  149. "value": os.path.expanduser(
  150. "/var/lib/radicale/collections"),
  151. "help": "path where collections are stored",
  152. "type": str}),
  153. ("max_sync_token_age", {
  154. "value": 2592000, # 30 days
  155. "help": "delete sync token that are older",
  156. "type": int}),
  157. ("filesystem_fsync", {
  158. "value": "True",
  159. "help": "sync all changes to filesystem during requests",
  160. "type": bool}),
  161. ("filesystem_locking", {
  162. "value": "True",
  163. "help": "lock the storage while accessing it",
  164. "type": bool}),
  165. ("filesystem_close_lock_file", {
  166. "value": "False",
  167. "help": "close the lock file when no more clients are waiting",
  168. "type": bool}),
  169. ("hook", {
  170. "value": "",
  171. "help": "command that is run after changes to storage",
  172. "type": str})])),
  173. ("web", OrderedDict([
  174. ("type", {
  175. "value": "internal",
  176. "help": "web interface backend",
  177. "type": str})])),
  178. ("logging", OrderedDict([
  179. ("config", {
  180. "value": "",
  181. "help": "logging configuration file",
  182. "type": str}),
  183. ("debug", {
  184. "value": "False",
  185. "help": "print debug information",
  186. "aliases": ["-D", "--debug"],
  187. "type": bool}),
  188. ("full_environment", {
  189. "value": "False",
  190. "help": "store all environment variables",
  191. "type": bool}),
  192. ("mask_passwords", {
  193. "value": "True",
  194. "help": "mask passwords in logs",
  195. "type": bool})]))])
  196. def load(paths=(), extra_config=None, ignore_missing_paths=True):
  197. config = ConfigParser()
  198. for section, values in INITIAL_CONFIG.items():
  199. config.add_section(section)
  200. for key, data in values.items():
  201. config.set(section, key, data["value"])
  202. if extra_config:
  203. for section, values in extra_config.items():
  204. for key, value in values.items():
  205. config.set(section, key, value)
  206. for path in paths:
  207. if path or not ignore_missing_paths:
  208. try:
  209. if not config.read(path) and not ignore_missing_paths:
  210. raise RuntimeError("No such file: %r" % path)
  211. except Exception as e:
  212. raise RuntimeError(
  213. "Failed to load config file %r: %s" % (path, e)) from e
  214. # Check the configuration
  215. for section in config.sections():
  216. if section == "headers":
  217. continue
  218. if section not in INITIAL_CONFIG:
  219. raise RuntimeError("Invalid section %r in config" % section)
  220. for option in config[section]:
  221. if option not in INITIAL_CONFIG[section]:
  222. raise RuntimeError("Invalid option %r in section %r in "
  223. "config" % (option, section))
  224. type_ = INITIAL_CONFIG[section][option]["type"]
  225. try:
  226. if type_ == bool:
  227. config.getboolean(section, option)
  228. else:
  229. type_(config.get(section, option))
  230. except Exception as e:
  231. raise RuntimeError(
  232. "Invalid %s value for option %r in section %r in config: "
  233. "%r" % (type_.__name__, option, section,
  234. config.get(section, option))) from e
  235. return config