freedombone-app-nextcloud 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # nextcloud application
  10. # In tests this is much too slow, and it appeared to self-destruct after a few minutes
  11. # i.e. becoming totally unusable.
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim cloud'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. NEXTCLOUD_DOMAIN_NAME=
  34. NEXTCLOUD_CODE=
  35. NEXTCLOUD_ONION_PORT=8112
  36. NEXTCLOUD_REPO="https://github.com/nextcloud/server"
  37. # Stable 13 branch
  38. NEXTCLOUD_COMMIT='b16824db31cd00e26e72216bf995d52389b9c93c'
  39. NEXTCLOUD_ADMIN_PASSWORD=
  40. nextcloud_variables=(ONION_ONLY
  41. NEXTCLOUD_DOMAIN_NAME
  42. NEXTCLOUD_CODE
  43. DDNS_PROVIDER
  44. MY_USERNAME)
  45. function logging_on_nextcloud {
  46. echo -n ''
  47. }
  48. function logging_off_nextcloud {
  49. echo -n ''
  50. }
  51. function remove_user_nextcloud {
  52. remove_username="$1"
  53. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp nextcloud
  54. }
  55. function add_user_nextcloud {
  56. new_username="$1"
  57. new_user_password="$2"
  58. "${PROJECT_NAME}-pass" -u "$new_username" -a nextcloud -p "$new_user_password"
  59. echo '0'
  60. }
  61. function change_password_nextcloud {
  62. curr_username="$1"
  63. export OC_PASS="$2"
  64. su -s /bin/sh www-data -c "php occ user:resetpassword --password-from-env $curr_username"
  65. "${PROJECT_NAME}-pass" -u "$curr_username" -a nextcloud -p "$OC_PASS"
  66. export OC_PASS=""
  67. }
  68. function install_interactive_nextcloud {
  69. if [ ! "$ONION_ONLY" ]; then
  70. ONION_ONLY='no'
  71. fi
  72. if [[ $ONION_ONLY != "no" ]]; then
  73. NEXTCLOUD_DOMAIN_NAME='nextcloud.local'
  74. else
  75. NEXTCLOUD_DETAILS_COMPLETE=
  76. while [ ! $NEXTCLOUD_DETAILS_COMPLETE ]
  77. do
  78. data=$(mktemp 2>/dev/null)
  79. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  80. dialog --backtitle $"Freedombone Configuration" \
  81. --title $"NextCloud Configuration" \
  82. --form $"\\nPlease enter your NextCloud details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
  83. $"Domain:" 1 1 "$(grep 'NEXTCLOUD_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  84. $"Code:" 2 1 "$(grep 'NEXTCLOUD_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
  85. 2> "$data"
  86. else
  87. dialog --backtitle $"Freedombone Configuration" \
  88. --title $"NextCloud Configuration" \
  89. --form $"\\nPlease enter your NextCloud details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 2 \
  90. $"Domain:" 1 1 "$(grep 'NEXTCLOUD_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  91. 2> "$data"
  92. fi
  93. sel=$?
  94. case $sel in
  95. 1) rm -f "$data"
  96. exit 1;;
  97. 255) rm -f "$data"
  98. exit 1;;
  99. esac
  100. NEXTCLOUD_DOMAIN_NAME=$(sed -n 1p < "$data")
  101. if [ "$NEXTCLOUD_DOMAIN_NAME" ]; then
  102. TEST_DOMAIN_NAME=$NEXTCLOUD_DOMAIN_NAME
  103. validate_domain_name
  104. if [[ "$TEST_DOMAIN_NAME" != "$NEXTCLOUD_DOMAIN_NAME" ]]; then
  105. NEXTCLOUD_DOMAIN_NAME=
  106. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  107. else
  108. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  109. NEXTCLOUD_CODE=$(sed -n 2p < "$data")
  110. validate_freedns_code "$NEXTCLOUD_CODE"
  111. if [ ! "$VALID_CODE" ]; then
  112. NEXTCLOUD_DOMAIN_NAME=
  113. fi
  114. fi
  115. fi
  116. fi
  117. if [ $NEXTCLOUD_DOMAIN_NAME ]; then
  118. NEXTCLOUD_DETAILS_COMPLETE="yes"
  119. fi
  120. rm -f "$data"
  121. done
  122. # remove any invalid characters
  123. if [ ${#NEXTCLOUD_TITLE} -gt 0 ]; then
  124. new_title=${NEXTCLOUD_TITLE//\'/}
  125. NEXTCLOUD_TITLE="$new_title"
  126. fi
  127. # save the results in the config file
  128. write_config_param "NEXTCLOUD_CODE" "$NEXTCLOUD_CODE"
  129. fi
  130. write_config_param "NEXTCLOUD_DOMAIN_NAME" "$NEXTCLOUD_DOMAIN_NAME"
  131. APP_INSTALLED=1
  132. }
  133. function change_password_nextcloud {
  134. curr_username="$1"
  135. new_user_password="$2"
  136. read_config_param ${NEXTCLOUD_DOMAIN_NAME}
  137. "${PROJECT_NAME}-pass" -u "$curr_username" -a nextcloud -p "$new_user_password"
  138. }
  139. function nextcloud_create_database {
  140. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  141. NEXTCLOUD_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  142. else
  143. if [ ! "$NEXTCLOUD_ADMIN_PASSWORD" ]; then
  144. NEXTCLOUD_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  145. fi
  146. fi
  147. if [ ! "$NEXTCLOUD_ADMIN_PASSWORD" ]; then
  148. return
  149. fi
  150. function_check create_database
  151. create_database nextcloud "$NEXTCLOUD_ADMIN_PASSWORD" "$MY_USERNAME"
  152. }
  153. function reconfigure_nextcloud {
  154. echo -n ''
  155. }
  156. function configure_interactive_nextcloud {
  157. echo -n ''
  158. }
  159. function upgrade_nextcloud_base {
  160. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs
  161. chown -R www-data:www-data /var/www/$NEXTCLOUD_DOMAIN_NAME/data
  162. cd "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" || exit 232547985
  163. sudo -u www-data ./occ maintenance:repair
  164. sudo -u www-data ./occ files:cleanup
  165. sudo -u www-data ./occ files:scan --all
  166. sudo -u www-data ./occ maintenance:mode --off
  167. }
  168. function upgrade_nextcloud {
  169. CURR_NEXTCLOUD_COMMIT=$(get_completion_param "nextcloud commit")
  170. if [[ "$CURR_NEXTCLOUD_COMMIT" == "$NEXTCLOUD_COMMIT" ]]; then
  171. upgrade_nextcloud_base
  172. return
  173. fi
  174. if grep -q "nextcloud domain" "$COMPLETION_FILE"; then
  175. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  176. fi
  177. # update to the next commit
  178. function_check set_repo_commit
  179. set_repo_commit "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" "nextcloud commit" "$NEXTCLOUD_COMMIT" "$NEXTCLOUD_REPO"
  180. upgrade_nextcloud_base
  181. sudo -u www-data ./occ upgrade
  182. }
  183. function backup_local_nextcloud {
  184. NEXTCLOUD_DOMAIN_NAME='nextcloud'
  185. if grep -q "nextcloud domain" "$COMPLETION_FILE"; then
  186. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  187. fi
  188. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  189. if [ -d "$source_directory" ]; then
  190. function_check suspend_site
  191. suspend_site "${NEXTCLOUD_DOMAIN_NAME}"
  192. dest_directory=nextcloudfiles
  193. function_check backup_directory_to_usb
  194. backup_directory_to_usb "$source_directory" "$dest_directory"
  195. source_directory=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  196. dest_directory=nextcloudconfig
  197. backup_directory_to_usb "$source_directory" "$dest_directory"
  198. function_check backup_database_to_usb
  199. backup_database_to_usb nextcloud
  200. function_check restart_site
  201. restart_site
  202. fi
  203. }
  204. function restore_local_nextcloud {
  205. if ! grep -q "nextcloud domain" "$COMPLETION_FILE"; then
  206. return
  207. fi
  208. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  209. if [ "$NEXTCLOUD_DOMAIN_NAME" ]; then
  210. temp_restore_dir=/root/tempnextcloud
  211. function_check nextcloud_create_database
  212. nextcloud_create_database
  213. restore_database nextcloud "${NEXTCLOUD_DOMAIN_NAME}"
  214. temp_restore_dir=/root/tempnextcloudfiles
  215. restore_directory_from_usb $temp_restore_dir nextcloudfiles
  216. if [ -d "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/data" ]; then
  217. cp -r "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/data" "/var/www/${NEXTCLOUD_DOMAIN_NAME}/"
  218. else
  219. cp -r $temp_restore_dir/* "/var/www/${NEXTCLOUD_DOMAIN_NAME}/"
  220. fi
  221. # shellcheck disable=SC2181
  222. if [ ! "$?" = "0" ]; then
  223. set_user_permissions
  224. backup_unmount_drive
  225. exit 346723
  226. fi
  227. rm -rf ${temp_restore_dir}
  228. temp_restore_dir=/root/tempnextcloudconfig
  229. restore_directory_from_usb $temp_restore_dir nextcloudconfig
  230. if [ -d "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config" ]; then
  231. cp -r "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config" "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs"
  232. else
  233. cp -r $temp_restore_dir/* "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/"
  234. fi
  235. # shellcheck disable=SC2181
  236. if [ ! "$?" = "0" ]; then
  237. set_user_permissions
  238. backup_unmount_drive
  239. exit 3467343
  240. fi
  241. rm -rf ${temp_restore_dir}
  242. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs"
  243. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/data"
  244. cd "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs" || exit 14893545875
  245. sudo -u www-data ./occ maintenance:repair
  246. sudo -u www-data ./occ files:cleanup
  247. sudo -u www-data ./occ files:scan --all
  248. fi
  249. }
  250. function backup_remote_nextcloud {
  251. if grep -q "nextcloud domain" "$COMPLETION_FILE"; then
  252. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  253. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/data
  254. if [ -d "$temp_backup_dir" ]; then
  255. function_check suspend_site
  256. suspend_site "${NEXTCLOUD_DOMAIN_NAME}"
  257. function_check backup_database_to_friend
  258. backup_database_to_friend nextcloud
  259. function_check backup_directory_to_friend
  260. backup_directory_to_friend "$temp_backup_dir" nextclouddata
  261. temp_backup_dir=/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config
  262. backup_directory_to_friend "$temp_backup_dir" nextcloudconfig
  263. function_check restart_site
  264. restart_site
  265. else
  266. echo $"nextcloud domain specified but not found in ${temp_backup_dir}"
  267. fi
  268. fi
  269. }
  270. function restore_remote_nextcloud {
  271. if grep -q "nextcloud domain" "$COMPLETION_FILE"; then
  272. echo $"Restoring nextcloud"
  273. NEXTCLOUD_DOMAIN_NAME=$(get_completion_param "nextcloud domain")
  274. function_check nextcloud_create_database
  275. nextcloud_create_database
  276. function_check restore_database_from_friend
  277. restore_database_from_friend nextcloud "${NEXTCLOUD_DOMAIN_NAME}"
  278. if [ -d /root/tempnextcloud ]; then
  279. rm -rf /root/tempnextcloud
  280. fi
  281. temp_restore_dir=/root/tempnextcloudfiles
  282. restore_directory_from_friend $temp_restore_dir nextcloudfiles
  283. if [ -d "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/data" ]; then
  284. cp -r "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/data" "/var/www/${NEXTCLOUD_DOMAIN_NAME}/"
  285. else
  286. cp -r $temp_restore_dir/* "/var/www/${NEXTCLOUD_DOMAIN_NAME}/"
  287. fi
  288. # shellcheck disable=SC2181
  289. if [ ! "$?" = "0" ]; then
  290. exit 768254
  291. fi
  292. rm -rf ${temp_restore_dir}
  293. temp_restore_dir=/root/tempnextcloudconfig
  294. restore_directory_from_friend $temp_restore_dir nextcloudconfig
  295. if [ -d "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config" ]; then
  296. cp -r "$temp_restore_dir/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config" "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs"
  297. else
  298. cp -r $temp_restore_dir/* "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/"
  299. fi
  300. # shellcheck disable=SC2181
  301. if [ ! "$?" = "0" ]; then
  302. exit 573427
  303. fi
  304. rm -rf ${temp_restore_dir}
  305. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs"
  306. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/data"
  307. cd "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs" || exit 2642846824
  308. sudo -u www-data ./occ maintenance:repair
  309. sudo -u www-data ./occ files:cleanup
  310. sudo -u www-data ./occ files:scan --all
  311. fi
  312. }
  313. function remove_nextcloud {
  314. if [ ${#NEXTCLOUD_DOMAIN_NAME} -eq 0 ]; then
  315. return
  316. fi
  317. function_check remove_nodejs
  318. remove_nodejs pleroma-nextcloud
  319. sed -i 's|env[PATH]|;env[PATH]|g' /etc/php/7.0/fpm/pool.d/www.conf
  320. read_config_param "NEXTCLOUD_DOMAIN_NAME"
  321. read_config_param "MY_USERNAME"
  322. echo "Removing $NEXTCLOUD_DOMAIN_NAME"
  323. nginx_dissite "$NEXTCLOUD_DOMAIN_NAME"
  324. remove_certs "$NEXTCLOUD_DOMAIN_NAME"
  325. if [ -d "/var/www/$NEXTCLOUD_DOMAIN_NAME" ]; then
  326. rm -rf "/var/www/$NEXTCLOUD_DOMAIN_NAME"
  327. fi
  328. if [ -f "/etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME" ]; then
  329. rm "/etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME"
  330. fi
  331. function_check drop_database
  332. drop_database nextcloud
  333. function_check remove_onion_service
  334. remove_onion_service nextcloud ${NEXTCLOUD_ONION_PORT}
  335. remove_app nextcloud
  336. remove_completion_param install_nextcloud
  337. sed -i '/nextcloud/d' "$COMPLETION_FILE"
  338. remove_backup_database_local nextcloud
  339. function_check remove_ddns_domain
  340. remove_ddns_domain "$NEXTCLOUD_DOMAIN_NAME"
  341. systemctl restart nginx
  342. systemctl restart php7.0-fpm
  343. }
  344. function install_nextcloud_main {
  345. if [ ! "$NEXTCLOUD_DOMAIN_NAME" ]; then
  346. echo $'No domain name was given for nextcloud'
  347. exit 7359
  348. fi
  349. if [[ $(app_is_installed nextcloud_main) == "1" ]]; then
  350. return
  351. fi
  352. function_check install_mariadb
  353. install_mariadb
  354. function_check get_mariadb_password
  355. get_mariadb_password
  356. function_check repair_databases_script
  357. repair_databases_script
  358. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  359. apt-get -yq install php-intl memcached php-memcached libfcgi0ldbl
  360. apt-get -yq install php-zip
  361. # Ensure PATH is available to php
  362. if [ ! -f /etc/php/7.0/fpm/pool.d/www.conf ]; then
  363. echo $'No php www configuration file found'
  364. exit 628757
  365. fi
  366. sed -i 's|;env[PATH]|env[PATH]|g' /etc/php/7.0/fpm/pool.d/www.conf
  367. if [ ! -d "/var/www/$NEXTCLOUD_DOMAIN_NAME" ]; then
  368. mkdir "/var/www/$NEXTCLOUD_DOMAIN_NAME"
  369. fi
  370. if [ ! -d "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" ]; then
  371. if [ -d /repos/nextcloud ]; then
  372. mkdir "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs"
  373. cp -r -p /repos/nextcloud/. "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs"
  374. cd "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" || exit 2783539793
  375. git pull
  376. else
  377. function_check git_clone
  378. git_clone "$NEXTCLOUD_REPO" "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs"
  379. fi
  380. if [ ! -d "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" ]; then
  381. echo $'Unable to clone nextcloud repo'
  382. exit 87525
  383. fi
  384. fi
  385. cd "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs" || exit 3468346834
  386. git submodule update --init
  387. git checkout $NEXTCLOUD_COMMIT -b $NEXTCLOUD_COMMIT
  388. set_completion_param "nextcloud commit" "$NEXTCLOUD_COMMIT"
  389. chmod g+w "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs"
  390. chown -R www-data:www-data "/var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs"
  391. function_check nextcloud_create_database
  392. nextcloud_create_database
  393. if [ ! -f "/etc/aliases" ]; then
  394. touch /etc/aliases
  395. fi
  396. if ! grep -q "www-data: root" /etc/aliases; then
  397. echo 'www-data: root' >> /etc/aliases
  398. fi
  399. function_check add_ddns_domain
  400. add_ddns_domain "$NEXTCLOUD_DOMAIN_NAME"
  401. nextcloud_nginx_site=/etc/nginx/sites-available/$NEXTCLOUD_DOMAIN_NAME
  402. if [[ $ONION_ONLY == "no" ]]; then
  403. function_check nginx_http_redirect
  404. nginx_http_redirect "$NEXTCLOUD_DOMAIN_NAME"
  405. { echo 'server {';
  406. echo ' listen 443 ssl;';
  407. echo ' #listen [::]:443 ssl;';
  408. echo " server_name $NEXTCLOUD_DOMAIN_NAME;";
  409. echo '';
  410. echo ' # Security'; } >> "$nextcloud_nginx_site"
  411. function_check nginx_ssl
  412. nginx_ssl "$NEXTCLOUD_DOMAIN_NAME" mobile
  413. function_check nginx_security_options
  414. nginx_security_options "$NEXTCLOUD_DOMAIN_NAME"
  415. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  416. echo '';
  417. echo ' # Logs';
  418. echo ' access_log /dev/null;';
  419. echo ' error_log /dev/null;';
  420. echo '';
  421. echo ' # Root';
  422. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;";
  423. echo '';
  424. echo ' # Index';
  425. echo ' index index.php;';
  426. echo '';
  427. # By default nextcloud advertises highly specific version information
  428. # on status.php, which can obviously be used by adversaries.
  429. # Blocking status.php prevents this information leak
  430. echo ' location = /status.php {';
  431. echo ' return 404;';
  432. echo ' }';
  433. echo '';
  434. echo ' # PHP';
  435. echo ' location ~ \.php {';
  436. echo ' include snippets/fastcgi-php.conf;';
  437. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  438. echo ' fastcgi_read_timeout 30;';
  439. echo ' }';
  440. echo '';
  441. echo ' # Location';
  442. echo ' location / {'; } >> "$nextcloud_nginx_site"
  443. function_check nginx_limits
  444. nginx_limits "$NEXTCLOUD_DOMAIN_NAME" '15m'
  445. { echo " try_files \$uri \$uri/ @nextcloud;";
  446. echo ' }';
  447. echo '';
  448. echo ' # Fancy URLs';
  449. echo ' location @nextcloud {';
  450. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  451. echo ' }';
  452. echo '';
  453. echo ' # Restrict access that is unnecessary anyway';
  454. echo ' location ~ /\.(ht|git) {';
  455. echo ' deny all;';
  456. echo ' }';
  457. echo '';
  458. echo ' location = /.well-known/carddav {';
  459. echo " return 301 \$scheme://\$host/remote.php/dav;";
  460. echo ' }';
  461. echo ' location = /.well-known/caldav {';
  462. echo " return 301 \$scheme://\$host/remote.php/dav;";
  463. echo ' }';
  464. echo '';
  465. echo ' location /.well-known/acme-challenge { }';
  466. echo '}'; } >> "$nextcloud_nginx_site"
  467. else
  468. echo -n '' > "$nextcloud_nginx_site"
  469. fi
  470. { echo 'server {';
  471. echo " listen 127.0.0.1:$NEXTCLOUD_ONION_PORT default_server;";
  472. echo " server_name $NEXTCLOUD_DOMAIN_NAME;";
  473. echo ''; } >> "$nextcloud_nginx_site"
  474. function_check nginx_security_options
  475. nginx_security_options "$NEXTCLOUD_DOMAIN_NAME"
  476. { echo '';
  477. echo ' # Logs';
  478. echo ' access_log /dev/null;';
  479. echo ' error_log /dev/null;';
  480. echo '';
  481. echo ' # Root';
  482. echo " root /var/www/$NEXTCLOUD_DOMAIN_NAME/htdocs;";
  483. echo '';
  484. echo ' # Index';
  485. echo ' index index.php;';
  486. echo '';
  487. # By default nextcloud advertises highly specific version information
  488. # on status.php, which can obviously be used by adversaries.
  489. # Blocking status.php prevents this information leak
  490. echo ' location = /status.php {';
  491. echo ' return 404;';
  492. echo ' }';
  493. echo '';
  494. echo ' # PHP';
  495. echo ' location ~ \.php {';
  496. echo ' include snippets/fastcgi-php.conf;';
  497. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  498. echo ' fastcgi_read_timeout 30;';
  499. echo ' }';
  500. echo '';
  501. echo ' # Location';
  502. echo ' location / {'; } >> "$nextcloud_nginx_site"
  503. function_check nginx_limits
  504. nginx_limits "$NEXTCLOUD_DOMAIN_NAME" '15m'
  505. { echo " try_files \$uri \$uri/ @nextcloud;";
  506. echo ' }';
  507. echo '';
  508. echo ' # Fancy URLs';
  509. echo ' location @nextcloud {';
  510. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  511. echo ' }';
  512. echo '';
  513. echo ' # Restrict access that is unnecessary anyway';
  514. echo ' location ~ /\.(ht|git) {';
  515. echo ' deny all;';
  516. echo ' }';
  517. echo '';
  518. echo ' location = /.well-known/carddav {';
  519. echo " return 301 \$scheme://\$host/remote.php/dav;";
  520. echo ' }';
  521. echo ' location = /.well-known/caldav {';
  522. echo " return 301 \$scheme://\$host/remote.php/dav;";
  523. echo ' }';
  524. echo '';
  525. echo ' location /.well-known/acme-challenge { }';
  526. echo '}'; } >> "$nextcloud_nginx_site"
  527. sed -i 's| DENY;| SAMEORIGIN;|g' "$nextcloud_nginx_site"
  528. # NextCloud doesn't like content-security-policy at all
  529. sed -i '/Content-Security-Policy/d' "$nextcloud_nginx_site"
  530. function_check configure_php
  531. configure_php
  532. function_check create_site_certificate
  533. create_site_certificate "$NEXTCLOUD_DOMAIN_NAME" 'yes'
  534. if [[ "$ONION_ONLY" == "no" ]]; then
  535. if [ ! -f "/etc/ssl/certs/${NEXTCLOUD_DOMAIN_NAME}.pem" ]; then
  536. echo $'Certificate not generated for nextcloud'
  537. exit 725762
  538. fi
  539. fi
  540. # Ensure that the database gets backed up locally, if remote
  541. # backups are not being used
  542. function_check backup_databases_script_header
  543. backup_databases_script_header
  544. function_check backup_database_local
  545. backup_database_local nextcloud
  546. function_check nginx_ensite
  547. nginx_ensite "$NEXTCLOUD_DOMAIN_NAME"
  548. NEXTCLOUD_ONION_HOSTNAME=$(add_onion_service nextcloud 80 ${NEXTCLOUD_ONION_PORT})
  549. systemctl restart php7.0-fpm
  550. systemctl restart nginx
  551. "${PROJECT_NAME}-addemail" -u "$MY_USERNAME" -e "noreply@$NEXTCLOUD_DOMAIN_NAME" -g nextcloud --public no
  552. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a nextcloud -p "$NEXTCLOUD_ADMIN_PASSWORD"
  553. cd "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs" || exit 2467245247
  554. if [ -d config ]; then
  555. chown -R www-data:www-data config
  556. fi
  557. if [ -d data ]; then
  558. chown -R www-data:www-data data
  559. fi
  560. chmod +x occ
  561. ./occ maintenance:install --database-name nextcloud --admin-user "${MY_USERNAME}" --admin-pass "${NEXTCLOUD_ADMIN_PASSWORD}" --database mysql --database-user root --database-pass "${MARIADB_PASSWORD}"
  562. if [ ! -d data ]; then
  563. echo $'Nextcloud data directory was not found. This probably means that the installation failed.'
  564. echo ''
  565. echo $'Install command was:'
  566. echo "./occ maintenance:install --database-name nextcloud --admin-user ${MY_USERNAME} --admin-pass \"${NEXTCLOUD_ADMIN_PASSWORD}\" --database mysql --database-user root --database-pass \"${MARIADB_PASSWORD}\""
  567. exit 83522
  568. fi
  569. chown -R www-data:www-data config
  570. chown -R www-data:www-data data
  571. sudo -u www-data ./occ check
  572. sudo -u www-data ./occ status
  573. sudo -u www-data ./occ app:list
  574. sudo -u www-data ./occ app:enable encryption
  575. if ! sudo -u www-data ./occ encryption:enable; then
  576. echo $'Encryption not enabled'
  577. exit 73527
  578. fi
  579. sudo -u www-data ./occ encryption:status
  580. sudo -u www-data ./occ config:system:set appstoreenabled --value=false
  581. chmod g+w "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php"
  582. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs"
  583. chmod 0644 .htaccess
  584. chmod 0750 data
  585. chown -R www-data:www-data "/var/www/${NEXTCLOUD_DOMAIN_NAME}/data"
  586. sudo -u www-data ./occ config:system:set trusted_domains 1 --value="$NEXTCLOUD_DOMAIN_NAME"
  587. sudo -u www-data ./occ config:system:set trusted_domains 2 --value="$NEXTCLOUD_ONION_HOSTNAME"
  588. sudo -u www-data ./occ files:cleanup
  589. sudo -u www-data ./occ files:scan --all
  590. sudo -u www-data ./occ maintenance:repair
  591. sudo -u www-data ./occ maintenance:mode --off
  592. systemctl restart mariadb
  593. # move the data directory
  594. # shellcheck disable=SC2086
  595. mv /var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/data /var/www/${NEXTCLOUD_DOMAIN_NAME}/
  596. sed -i "s|'datadirectory'.*|'datadirectory' => '/var/www/$NEXTCLOUD_DOMAIN_NAME/data',|g" "/var/www/${NEXTCLOUD_DOMAIN_NAME}/htdocs/config/config.php"
  597. set_completion_param "nextcloud domain" "$NEXTCLOUD_DOMAIN_NAME"
  598. install_completed nextcloud_main
  599. }
  600. function install_nextcloud {
  601. if [ ! "$ONION_ONLY" ]; then
  602. ONION_ONLY='no'
  603. fi
  604. install_nextcloud_main
  605. APP_INSTALLED=1
  606. }
  607. # NOTE: deliberately there is no "exit 0"