freedombone-app-radicale 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Radicale calendar system
  12. #
  13. # Configuration based upon:
  14. # https://gigacog.com/blog/2016/01/radicale-and-uwsgi-on-nginx-with-systemd
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS='full full-vim'
  34. IN_DEFAULT_INSTALL=1
  35. SHOW_ON_ABOUT=1
  36. RADICALE_PASSWORD=
  37. RADICALE_ONION_PORT=8106
  38. RADICALE_PORT=5232
  39. RADICALE_DIRECTORY='/etc/radicale'
  40. radicale_variables=(ONION_ONLY
  41. MY_USERNAME
  42. RADICALE_PASSWORD
  43. DEFAULT_DOMAIN_NAME)
  44. function remove_user_radicale {
  45. remove_username="$1"
  46. if grep "$remove_username:" ${RADICALE_DIRECTORY}/users; then
  47. sed -i "/$remove_username:/d" ${RADICALE_DIRECTORY}/users
  48. systemctl reload radicale
  49. fi
  50. }
  51. function add_user_radicale {
  52. new_username="$1"
  53. new_user_password="$2"
  54. if [ ! -f ${RADICALE_DIRECTORY}/users ]; then
  55. touch ${RADICALE_DIRECTORY}/users
  56. fi
  57. if ! grep "$new_username:" ${RADICALE_DIRECTORY}/users; then
  58. htpasswd -Bb ${RADICALE_DIRECTORY}/users "$new_username" "$new_user_password"
  59. systemctl reload radicale
  60. fi
  61. echo '0'
  62. }
  63. function install_interactive_radicale {
  64. echo -n ''
  65. APP_INSTALLED=1
  66. }
  67. function change_password_radicale {
  68. echo -n ''
  69. }
  70. function reconfigure_radicale {
  71. echo -n ''
  72. }
  73. function upgrade_radicale {
  74. if [ -f /etc/init.d/radicale ]; then
  75. systemctl stop radicale
  76. rm /etc/init.d/radicale
  77. systemctl daemon-reload
  78. systemctl start radicale
  79. fi
  80. }
  81. function backup_local_radicale {
  82. source_directory=${RADICALE_DIRECTORY}
  83. if [ -d $source_directory ]; then
  84. dest_directory=radicale
  85. function_check backup_directory_to_usb
  86. backup_directory_to_usb $source_directory $dest_directory
  87. fi
  88. }
  89. function restore_local_radicale {
  90. if [ -d ${RADICALE_DIRECTORY} ]; then
  91. temp_restore_dir=/root/tempradicale
  92. function_check restore_directory_from_usb
  93. restore_directory_from_usb $temp_restore_dir radicale
  94. cp -r $temp_restore_dir${RADICALE_DIRECTORY}/* ${RADICALE_DIRECTORY}
  95. if [ ! "$?" = "0" ]; then
  96. function_check backup_unmount_drive
  97. backup_unmount_drive
  98. exit 46872
  99. fi
  100. rm -rf $temp_restore_dir
  101. systemctl restart radicale
  102. fi
  103. }
  104. function backup_remote_radicale {
  105. if [ -d ${RADICALE_DIRECTORY} ]; then
  106. echo $"Backing up the radicale settings"
  107. backup_directory_to_friend ${RADICALE_DIRECTORY} radicale
  108. echo $"Backup of radicale settings complete"
  109. fi
  110. }
  111. function restore_remote_radicale {
  112. if [ -d ${RADICALE_DIRECTORY} ]; then
  113. temp_restore_dir=/root/tempradicale
  114. function_check restore_directory_from_friend
  115. restore_directory_from_friend $temp_restore_dir radicale
  116. cp -r $temp_restore_dir${RADICALE_DIRECTORY}/* ${RADICALE_DIRECTORY}
  117. if [ ! "$?" = "0" ]; then
  118. exit 463735
  119. fi
  120. rm -rf $temp_restore_dir
  121. systemctl restart radicale
  122. fi
  123. }
  124. function configure_firewall_for_radicale {
  125. if [ ! -d ${RADICALE_DIRECTORY} ]; then
  126. return
  127. fi
  128. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  129. # docker does its own firewalling
  130. return
  131. fi
  132. if [[ $ONION_ONLY != "no" ]]; then
  133. return
  134. fi
  135. firewall_add radicale ${RADICALE_PORT} tcp
  136. mark_completed $FUNCNAME
  137. }
  138. function remove_radicale {
  139. nginx_dissite radicale
  140. systemctl stop radicale
  141. systemctl stop uwsgi_rundir
  142. systemctl disable radicale
  143. systemctl disable uwsgi_rundir
  144. if [ -f /etc/systemd/system/uwsgi_rundir.service ]; then
  145. rm /etc/systemd/system/uwsgi_rundir.service
  146. fi
  147. if [ -f /etc/systemd/system/radicale.service ]; then
  148. rm /etc/systemd/system/radicale.service
  149. fi
  150. if [ -f /etc/nginx/sites-available/radicale ]; then
  151. rm /etc/nginx/sites-available/radicale
  152. fi
  153. if [ -f /usr/local/bin/uwsgi_rundir.sh ]; then
  154. rm /usr/local/bin/uwsgi_rundir.sh
  155. fi
  156. firewall_remove ${RADICALE_PORT} tcp
  157. function_check remove_onion_service
  158. remove_onion_service radicale ${RADICALE_ONION_PORT}
  159. apt-get -yq remove --purge radicale python-radicale
  160. if [ -d ${RADICALE_DIRECTORY} ]; then
  161. rm -rf ${RADICALE_DIRECTORY}
  162. fi
  163. if [ -d /var/www/radicale ]; then
  164. rm -rf /var/www/radicale
  165. fi
  166. if [ -f touch /var/log/radicale/radicale.log ]; then
  167. rm -rf /var/log/radicale
  168. fi
  169. remove_completion_param install_radicale
  170. sed -i '/radicale/d' $COMPLETION_FILE
  171. sed -i '/Radicale/d' /home/$MY_USERNAME/README
  172. }
  173. function install_radicale {
  174. if [[ $ONION_ONLY == 'no' ]]; then
  175. # obtain a cert for the default domain
  176. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  177. echo $'Obtaining certificate for the main domain'
  178. create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
  179. fi
  180. fi
  181. if [ ! -d /var/log/radicale ]; then
  182. mkdir /var/log/radicale
  183. fi
  184. if [ ! -f /var/log/radicale/radicale.log ]; then
  185. touch /var/log/radicale/radicale.log
  186. fi
  187. chown -R www-data:www-data /var/log/radicale
  188. apt-get -yq install python-radicale uwsgi uwsgi-plugin-python radicale apache2-utils
  189. if [ ! -d ${RADICALE_DIRECTORY} ]; then
  190. echo $"ERROR: radicale does not appear to have installed"
  191. exit 46372
  192. fi
  193. systemctl stop radicale
  194. if [ -f /etc/init.d/radicale ]; then
  195. rm /etc/init.d/radicale
  196. fi
  197. if [ ! -d ${RADICALE_DIRECTORY}/collections ]; then
  198. mkdir -p ${RADICALE_DIRECTORY}/collections
  199. fi
  200. chown www-data:www-data ${RADICALE_DIRECTORY}/collections
  201. echo '[server]' > ${RADICALE_DIRECTORY}/config
  202. echo 'hosts=127.0.0.1:52322' >> ${RADICALE_DIRECTORY}/config
  203. echo 'ssl = False' >> ${RADICALE_DIRECTORY}/config
  204. echo '' >> ${RADICALE_DIRECTORY}/config
  205. echo '[auth]' >> ${RADICALE_DIRECTORY}/config
  206. echo 'type = htpasswd' >> ${RADICALE_DIRECTORY}/config
  207. echo "htpasswd_filename = ${RADICALE_DIRECTORY}/users" >> ${RADICALE_DIRECTORY}/config
  208. echo 'htpasswd_encryption = bcrypt' >> ${RADICALE_DIRECTORY}/config
  209. echo '' >> ${RADICALE_DIRECTORY}/config
  210. echo '[rights]' >> ${RADICALE_DIRECTORY}/config
  211. echo 'type = owner_only' >> ${RADICALE_DIRECTORY}/config
  212. echo '' >> ${RADICALE_DIRECTORY}/config
  213. echo '[storage]' >> ${RADICALE_DIRECTORY}/config
  214. echo 'type = filesystem' >> ${RADICALE_DIRECTORY}/config
  215. echo "filesystem_folder = ${RADICALE_DIRECTORY}/collections" >> ${RADICALE_DIRECTORY}/config
  216. if [ ${#RADICALE_PASSWORD} -lt 8 ]; then
  217. if [ -f $IMAGE_PASSWORD_FILE ]; then
  218. RADICALE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  219. else
  220. RADICALE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  221. fi
  222. fi
  223. add_user_radicale "$MY_USERNAME" "$RADICALE_PASSWORD"
  224. if [ ! -f /var/www/radicale ]; then
  225. mkdir /var/www/radicale
  226. fi
  227. echo 'import radicale' > /var/www/radicale/radicale.py
  228. echo 'radicale.log.start()' >> /var/www/radicale/radicale.py
  229. echo 'application = radicale.Application()' >> /var/www/radicale/radicale.py
  230. #echo '[uwsgi]' > /var/www/radicale/uwsgi.ini
  231. #echo 'plugins = python' >> /var/www/radicale/uwsgi.ini
  232. #echo 'socket = /var/run/uwsgi/radicale.sock' >> /var/www/radicale/uwsgi.ini
  233. #echo 'chmod-socket = 660' >> /var/www/radicale/uwsgi.ini
  234. #echo '' >> /var/www/radicale/uwsgi.ini
  235. #echo 'wsgi-file = /var/www/radicale/radicale.py' >> /var/www/radicale/uwsgi.ini
  236. #echo 'master' >> /var/www/radicale/uwsgi.ini
  237. #echo 'workers = 1' >> /var/www/radicale/uwsgi.ini
  238. #echo 'max-requests = 100' >> /var/www/radicale/uwsgi.ini
  239. #echo 'harakiri = 30' >> /var/www/radicale/uwsgi.ini
  240. #echo 'die-on-term' >> /var/www/radicale/uwsgi.ini
  241. #echo '#!/bin/sh' > /usr/local/bin/uwsgi_rundir.sh
  242. #echo 'mkdir -p /var/run/uwsgi' >> /usr/local/bin/uwsgi_rundir.sh
  243. #echo 'chown www-data:www-data /var/run/uwsgi' >> /usr/local/bin/uwsgi_rundir.sh
  244. #chmod +x /usr/local/bin/uwsgi_rundir.sh
  245. #echo '[Unit]' > /etc/systemd/system/uwsgi_rundir.service
  246. #echo 'Description=UWSGI socket directory' >> /etc/systemd/system/uwsgi_rundir.service
  247. #echo 'After=network.target' >> /etc/systemd/system/uwsgi_rundir.service
  248. #echo '' >> /etc/systemd/system/uwsgi_rundir.service
  249. #echo '[Service]' >> /etc/systemd/system/uwsgi_rundir.service
  250. #echo 'Type=simple' >> /etc/systemd/system/uwsgi_rundir.service
  251. #echo 'User=root' >> /etc/systemd/system/uwsgi_rundir.service
  252. #echo 'ExecStart=/usr/local/bin/uwsgi_rundir.sh' >> /etc/systemd/system/uwsgi_rundir.service
  253. #echo '' >> /etc/systemd/system/uwsgi_rundir.service
  254. #echo '[Install]' >> /etc/systemd/system/uwsgi_rundir.service
  255. #echo 'WantedBy=multi-user.target' >> /etc/systemd/system/uwsgi_rundir.service
  256. #systemctl enable uwsgi_rundir
  257. echo '[Unit]' > /etc/systemd/system/radicale.service
  258. echo 'Description=Radicale CalDAV Server' >> /etc/systemd/system/radicale.service
  259. echo 'After=network.target' >> /etc/systemd/system/radicale.service
  260. echo 'Requires=uwsgi_rundir.service' >> /etc/systemd/system/radicale.service
  261. echo '' >> /etc/systemd/system/radicale.service
  262. echo '[Service]' >> /etc/systemd/system/radicale.service
  263. echo 'Type=simple' >> /etc/systemd/system/radicale.service
  264. echo 'User=www-data' >> /etc/systemd/system/radicale.service
  265. echo 'Group=www-data' >> /etc/systemd/system/radicale.service
  266. echo 'ExecStart=/var/www/radicale/radicale.py' >> /etc/systemd/system/radicale.service
  267. echo '' >> /etc/systemd/system/radicale.service
  268. echo '[Install]' >> /etc/systemd/system/radicale.service
  269. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/radicale.service
  270. systemctl enable radicale
  271. systemctl start radicale
  272. RADICALE_ONION_HOSTNAME=$(add_onion_service radicale 80 ${RADICALE_ONION_PORT})
  273. if [[ $ONION_ONLY == 'no' ]]; then
  274. echo 'server {' > /etc/nginx/sites-available/radicale
  275. echo " listen ${RADICALE_PORT} ssl;" >> /etc/nginx/sites-available/radicale
  276. echo " listen [::]:${RADICALE_PORT} ssl;" >> /etc/nginx/sites-available/radicale
  277. echo '' >> /etc/nginx/sites-available/radicale
  278. function_check nginx_ssl
  279. nginx_ssl radicale
  280. function_check nginx_disable_sniffing
  281. nginx_disable_sniffing radicale
  282. echo '' >> /etc/nginx/sites-available/radicale
  283. echo " server_name $DEFAULT_DOMAIN_NAME;" >> /etc/nginx/sites-available/radicale
  284. echo '' >> /etc/nginx/sites-available/radicale
  285. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/radicale
  286. echo ' error_log /var/log/radicale/radicale.log warn;' >> /etc/nginx/sites-available/radicale
  287. echo '' >> /etc/nginx/sites-available/radicale
  288. echo ' location / {' >> /etc/nginx/sites-available/radicale
  289. echo ' uwsgi_pass unix:/var/run/uwsgi/radicale.sock;' >> /etc/nginx/sites-available/radicale
  290. echo ' include uwsgi_params;' >> /etc/nginx/sites-available/radicale
  291. echo ' }' >> /etc/nginx/sites-available/radicale
  292. echo '}' >> /etc/nginx/sites-available/radicale
  293. echo '' >> /etc/nginx/sites-available/radicale
  294. else
  295. echo -n '' > /etc/nginx/sites-available/radicale
  296. fi
  297. echo 'server {' >> /etc/nginx/sites-available/radicale
  298. echo " listen 127.0.0.1:${RADICALE_ONION_PORT} default_server;" >> /etc/nginx/sites-available/radicale
  299. echo '' >> /etc/nginx/sites-available/radicale
  300. echo " server_name ${RADICALE_ONION_HOSTNAME};" >> /etc/nginx/sites-available/radicale
  301. echo '' >> /etc/nginx/sites-available/radicale
  302. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/radicale
  303. echo ' error_log /var/log/radicale/radicale.log warn;' >> /etc/nginx/sites-available/radicale
  304. echo '' >> /etc/nginx/sites-available/radicale
  305. echo ' location / {' >> /etc/nginx/sites-available/radicale
  306. echo ' proxy_pass http://localhost:52322;' >> /etc/nginx/sites-available/radicale
  307. #echo ' uwsgi_pass unix:/var/run/uwsgi/radicale.sock;' >> /etc/nginx/sites-available/radicale
  308. #echo ' include uwsgi_params;' >> /etc/nginx/sites-available/radicale
  309. echo ' }' >> /etc/nginx/sites-available/radicale
  310. echo '}' >> /etc/nginx/sites-available/radicale
  311. if [ -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  312. sed -i "s|radicale.crt|${DEFAULT_DOMAIN_NAME}.pem|g" /etc/nginx/sites-available/radicale
  313. sed -i "s|radicale.pem|${DEFAULT_DOMAIN_NAME}.pem|g" /etc/nginx/sites-available/radicale
  314. fi
  315. sed -i "s|radicale.key|${DEFAULT_DOMAIN_NAME}.key|g" /etc/nginx/sites-available/radicale
  316. sed -i "s|radicale.dhparam|${DEFAULT_DOMAIN_NAME}.dhparam|g" /etc/nginx/sites-available/radicale
  317. # create a certificate
  318. #if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  319. # if [[ "$(cert_exists $DEFAULT_DOMAIN_NAME)" == "0" ]]; then
  320. # ${PROJECT_NAME}-addcert -h $DEFAULT_DOMAIN_NAME --dhkey ${DH_KEYLENGTH}
  321. # check_certificates $DEFAULT_DOMAIN_NAME
  322. # fi
  323. #fi
  324. update_default_domain
  325. nginx_ensite radicale
  326. systemctl reload nginx
  327. set_completion_param "radicale onion domain" "${RADICALE_ONION_HOSTNAME}"
  328. if ! grep -q "# Radicale" /home/$MY_USERNAME/README; then
  329. echo '' >> /home/$MY_USERNAME/README
  330. echo $'# Radicale' >> /home/$MY_USERNAME/README
  331. echo $"Radicale onion domain: ${RADICALE_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  332. echo $"Your Radicale password is: ${RADICALE_PASSWORD}" >> /home/$MY_USERNAME/README
  333. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  334. chmod 600 /home/$MY_USERNAME/README
  335. else
  336. sed -i "s|Radicale onion domain.*|Radicale onion domain: ${RADICALE_ONION_HOSTNAME}|g" /home/$MY_USERNAME/README
  337. sed -i "s|Your Radicale password is.*|Your Radicale password is: ${RADICALE_PASSWORD}|g" /home/$MY_USERNAME/README
  338. fi
  339. function_check configure_firewall_for_radicale
  340. configure_firewall_for_radicale
  341. APP_INSTALLED=1
  342. }
  343. # NOTE: deliberately no exit 0