You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
5.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=80
  3. STOP=10
  4. CFGDIR=/var/etc/radicale2
  5. SYSCFG=$CFGDIR/config
  6. USRCFG=$CFGDIR/users
  7. DATADIR="/srv/radicale2/data"
  8. LOGDIR=""
  9. USE_PROCD=1
  10. # we could start with empty configuration file using defaults
  11. [ -f ${IPKG_INSTROOT}/etc/config/radicale2 ] || touch ${IPKG_INSTROOT}/etc/config/radicale2
  12. conf_line() {
  13. local cfgfile="$1"
  14. local option="$2"
  15. local value="$3"
  16. if [ -n "$value" ]; then
  17. eval "echo '$2' = '$value' >>'$cfgfile'"
  18. fi
  19. }
  20. conf_getline() {
  21. local cfg="$1"
  22. local cfgfile="$2"
  23. local option="$3"
  24. local defval="$4"
  25. local flag="$5"
  26. unset value
  27. if [ "$flag" != "1" ]; then
  28. config_get value "$cfg" "$option" "$defval"
  29. conf_line "$cfgfile" "$option" "$value"
  30. else
  31. config_get_bool value "$cfg" "$option" "$defval"
  32. [ -z "$defval" ] && defval=1
  33. if [ "$value" -ne "$defval" ]; then
  34. if [ "$value" -ne 0 ]; then
  35. conf_line "$cfgfile" "$option" "True"
  36. else
  37. conf_line "$cfgfile" "$option" "False"
  38. fi
  39. fi
  40. fi
  41. }
  42. build_hosts_line() {
  43. local val="$1"
  44. append hostlist "$val" ", "
  45. }
  46. conf_section() {
  47. local cfg="$1"
  48. local cfgfile="$2"
  49. local hostlist=""
  50. local value
  51. echo "[$cfg]
  52. " >>$cfgfile
  53. case $cfg in
  54. server)
  55. config_list_foreach "$cfg" host build_hosts_line
  56. conf_line "$tmpfile" hosts "$hostlist"
  57. conf_getline "$cfg" $tmpfile max_connections
  58. conf_getline "$cfg" $tmpfile max_conntent_length
  59. conf_getline "$cfg" $tmpfile timeout
  60. conf_getline "$cfg" $tmpfile ssl 0 1
  61. if [ "$value" -eq 1 ]; then
  62. conf_getline "$cfg" $tmpfile certificate
  63. conf_getline "$cfg" $tmpfile key
  64. conf_getline "$cfg" $tmpfile certificate_authority
  65. conf_getline "$cfg" $tmpfile protocol
  66. conf_getline "$cfg" $tmpfile ciphers
  67. fi
  68. conf_getline "$cfg" $tmpfile dns_lookup 1 1
  69. conf_getline "$cfg" $tmpfile realm
  70. ;;
  71. encoding)
  72. conf_getline "$cfg" $tmpfile request
  73. conf_getline "$cfg" $tmpfile stock
  74. ;;
  75. auth)
  76. conf_getline "$cfg" $tmpfile "type" htpasswd
  77. if [ "$value" = "htpasswd" ]; then
  78. conf_getline "$cfg" $tmpfile htpasswd_filename $CFGDIR/users
  79. conf_getline "$cfg" "$tmpfile" htpasswd_encryption plain
  80. fi
  81. conf_getline "$cfg" "$tmpfile" delay
  82. ;;
  83. rights)
  84. conf_getline "$cfg" "$tmpfile" "type"
  85. if [ "$value" = "from_file" ]; then
  86. conf_getline "$cfg" "$tmpfile" "file"
  87. fi
  88. ;;
  89. storage)
  90. conf_getline "$cfg" $tmpfile filesystem_folder "$DATADIR"
  91. DATADIR="$value"
  92. conf_getline "$cfg" $tmpfile filesystem_locking 1 1
  93. conf_getline "$cfg" $tmpfile max_sync_token_age
  94. conf_getline "$cfg" $tmpfile filesystem_close_lock_file 0 1
  95. conf_getline "$cfg" $tmpfile hook
  96. ;;
  97. web)
  98. conf_getline "$cfg" $tmpfile "type"
  99. ;;
  100. logging)
  101. conf_getline "$cfg" "$tmpfile" config
  102. conf_getline "$cfg" "$tmpfile" debug 0 1
  103. conf_getline "$cfg" "$tmpfile" full_environment 0 1
  104. conf_getline "$cfg" "$tmpfile" mask_passwords 1 1
  105. ;;
  106. headers)
  107. config_get "$cfg" "$tmpfile" cors
  108. if [ -n "$cors" ]; then
  109. echo "Access-Control-Allow-Origin = $cors" >>$tmpfile
  110. fi
  111. ;;
  112. esac
  113. echo "
  114. " >>$cfgfile
  115. }
  116. add_missing_sections() {
  117. local cfgfile="$1"
  118. for section in server encoding auth rights storage web logging headers; do
  119. if [ "$section" = "server" ]; then
  120. grep -q "\[$section\]" $cfgfile || echo "
  121. [$section]
  122. hosts = 0.0.0.0:5232, [::]:5232
  123. " >>$cfgfile
  124. elif [ "$section" = "auth" ]; then
  125. grep -q "\[$section\]" $cfgfile || echo "
  126. [$section]
  127. type = htpasswd
  128. htpasswd_filename = $CFGDIR/users
  129. htpasswd_encryption = plain
  130. " >>$cfgfile
  131. elif [ "$section" = "storage" ]; then
  132. grep -q "\[$section\]" $cfgfile || echo "
  133. [$section]
  134. filesystem_folder = $DATADIR
  135. " >>$cfgfile
  136. else
  137. grep -q "\[$section\]" $cfgfile || echo "
  138. [$section]
  139. " >>$cfgfile
  140. fi
  141. done
  142. }
  143. add_user() {
  144. local cfg="$1"
  145. local tmpfile="$2"
  146. local name password
  147. config_get name "$cfg" name
  148. config_get password "$cfg" password
  149. [ -n "$name" ] && echo "$name:$password" >>$tmpfile
  150. }
  151. build_users() {
  152. local tmpfile="$1"
  153. # temporary config file
  154. # radicale2 needs read access
  155. chmod 0640 $tmpfile
  156. config_foreach add_user user "$tmpfile"
  157. }
  158. build_config() {
  159. local tmpfile=$(mktemp)
  160. local tmpfile2=$(mktemp)
  161. # temporary config file
  162. # radicale2 need read access
  163. chmod 0640 $tmpfile
  164. config_load radicale2
  165. config_foreach conf_section section $tmpfile
  166. add_missing_sections $tmpfile
  167. build_users $tmpfile2
  168. # move tmp to final
  169. mkdir -m0750 -p $CFGDIR
  170. cat $tmpfile >$SYSCFG
  171. rm -f $tmpfile
  172. cat $tmpfile2 >$USRCFG
  173. rm -f $tmpfile2
  174. }
  175. set_permission() {
  176. # config file permissions (read access for group)
  177. chmod 0750 $CFGDIR
  178. chmod 0640 $SYSCFG
  179. chmod 0640 $USRCFG
  180. chgrp -R radicale2 $CFGDIR
  181. # data directory does not exist
  182. [ -d $DATADIR ] || {
  183. logger -p user.error -t "radicale2[----]" "Data directory '$DATADIR' does not exist. Startup failed !!!"
  184. }
  185. }
  186. interface_triggers() {
  187. local action="$1"
  188. local triggerlist trigger
  189. config_load radicale2
  190. config_get triggerlist server triggerlist
  191. . /lib/functions/network.sh
  192. if [ -n "$triggerlist" ]; then
  193. for trigger in $triggerlist; do
  194. if [ "$action" = "add_trigger" ]; then
  195. procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/radicale2 reload
  196. else
  197. network_is_up "$trigger" && return 0
  198. fi
  199. done
  200. else
  201. if [ "$action" = "add_trigger" ]; then
  202. procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/radicale2 reload
  203. else
  204. ubus call network.device status | grep -q '"up": true' && return 0
  205. fi
  206. fi
  207. [ "$action" = "add_trigger" ] || return 1
  208. }
  209. start_service() {
  210. local haveinterface
  211. if [ ! -r /etc/radicale2/config ]; then
  212. build_config
  213. set_permission
  214. fi
  215. interface_triggers "check_interface_up" || return
  216. procd_open_instance "radicale2"
  217. procd_set_param respawn
  218. procd_set_param stderr 1
  219. procd_set_param stdout 1
  220. if [ ! -r /etc/radicale2/config ]; then
  221. procd_set_param command /usr/bin/radicale2 --config="$SYSCFG"
  222. else
  223. procd_set_param command /usr/bin/radicale2 --config="/etc/radicale2/config"
  224. fi
  225. procd_set_param user radicale2
  226. procd_close_instance
  227. return 0
  228. }
  229. service_triggers() {
  230. interface_triggers "add_trigger"
  231. procd_add_reload_trigger "radicale2"
  232. }