config.py 7.9 KB

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