config.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. ("protocol", {
  88. "value": "PROTOCOL_TLSv1_2",
  89. "help": "SSL protocol used",
  90. "type": str}),
  91. ("ciphers", {
  92. "value": "",
  93. "help": "available ciphers",
  94. "type": str}),
  95. ("dns_lookup", {
  96. "value": "True",
  97. "help": "use reverse DNS to resolve client address in logs",
  98. "type": bool}),
  99. ("realm", {
  100. "value": "Radicale - Password Required",
  101. "help": "message displayed when a password is needed",
  102. "type": str})])),
  103. ("encoding", OrderedDict([
  104. ("request", {
  105. "value": "utf-8",
  106. "help": "encoding for responding requests",
  107. "type": str}),
  108. ("stock", {
  109. "value": "utf-8",
  110. "help": "encoding for storing local collections",
  111. "type": str})])),
  112. ("auth", OrderedDict([
  113. ("type", {
  114. "value": "none",
  115. "help": "authentication method",
  116. "type": str}),
  117. ("htpasswd_filename", {
  118. "value": "/etc/radicale/users",
  119. "help": "htpasswd filename",
  120. "type": str}),
  121. ("htpasswd_encryption", {
  122. "value": "bcrypt",
  123. "help": "htpasswd encryption method",
  124. "type": str}),
  125. ("delay", {
  126. "value": "1",
  127. "help": "incorrect authentication delay",
  128. "type": positive_float})])),
  129. ("rights", OrderedDict([
  130. ("type", {
  131. "value": "owner_only",
  132. "help": "rights backend",
  133. "type": str}),
  134. ("file", {
  135. "value": "/etc/radicale/rights",
  136. "help": "file for rights management from_file",
  137. "type": str})])),
  138. ("storage", OrderedDict([
  139. ("type", {
  140. "value": "multifilesystem",
  141. "help": "storage backend",
  142. "type": str}),
  143. ("filesystem_folder", {
  144. "value": os.path.expanduser(
  145. "/var/lib/radicale/collections"),
  146. "help": "path where collections are stored",
  147. "type": str}),
  148. ("filesystem_fsync", {
  149. "value": "True",
  150. "help": "sync all changes to filesystem during requests",
  151. "type": bool}),
  152. ("filesystem_locking", {
  153. "value": "True",
  154. "help": "lock the storage while accessing it",
  155. "type": bool}),
  156. ("filesystem_close_lock_file", {
  157. "value": "False",
  158. "help": "close the lock file when no more clients are waiting",
  159. "type": bool}),
  160. ("hook", {
  161. "value": "",
  162. "help": "command that is run after changes to storage",
  163. "type": str})])),
  164. ("web", OrderedDict([
  165. ("type", {
  166. "value": "internal",
  167. "help": "web interface backend",
  168. "type": str})])),
  169. ("logging", OrderedDict([
  170. ("config", {
  171. "value": "",
  172. "help": "logging configuration file",
  173. "type": str}),
  174. ("debug", {
  175. "value": "False",
  176. "help": "print debug information",
  177. "aliases": ["-D", "--debug"],
  178. "type": bool}),
  179. ("full_environment", {
  180. "value": "False",
  181. "help": "store all environment variables",
  182. "type": bool}),
  183. ("mask_passwords", {
  184. "value": "True",
  185. "help": "mask passwords in logs",
  186. "type": bool})]))])
  187. def load(paths=(), extra_config=None, ignore_missing_paths=True):
  188. config = ConfigParser()
  189. for section, values in INITIAL_CONFIG.items():
  190. config.add_section(section)
  191. for key, data in values.items():
  192. config.set(section, key, data["value"])
  193. if extra_config:
  194. for section, values in extra_config.items():
  195. for key, value in values.items():
  196. config.set(section, key, value)
  197. for path in paths:
  198. if path or not ignore_missing_paths:
  199. try:
  200. if not config.read(path) and not ignore_missing_paths:
  201. raise RuntimeError("No such file: %r" % path)
  202. except Exception as e:
  203. raise RuntimeError(
  204. "Failed to load config file %r: %s" % (path, e)) from e
  205. # Check the configuration
  206. for section in config.sections():
  207. if section == "headers":
  208. continue
  209. if section not in INITIAL_CONFIG:
  210. raise RuntimeError("Invalid section %r in config" % section)
  211. for option in config[section]:
  212. if option not in INITIAL_CONFIG[section]:
  213. raise RuntimeError("Invalid option %r in section %r in "
  214. "config" % (option, section))
  215. type_ = INITIAL_CONFIG[section][option]["type"]
  216. try:
  217. if type_ == bool:
  218. config.getboolean(section, option)
  219. else:
  220. type_(config.get(section, option))
  221. except Exception as e:
  222. raise RuntimeError(
  223. "Invalid %s value for option %r in section %r in config: "
  224. "%r" % (type_.__name__, option, section,
  225. config.get(section, option))) from e
  226. return config