config.py 7.7 KB

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