freedombone-app-nextcloud 25KB

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