freedombone-app-nextcloud 25KB

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