freedombone-app-lychee 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Lychee photo album
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS="full full-vim writer"
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. LYCHEE_DOMAIN_NAME=
  32. LYCHEE_CODE=
  33. LYCHEE_ONION_PORT=8105
  34. LYCHEE_REPO="https://github.com/electerious/Lychee"
  35. LYCHEE_COMMIT='27f207dcbac8488629ffc3b5a9cac78ae123bee9'
  36. lychee_variables=(LYCHEE_REPO
  37. LYCHEE_DOMAIN_NAME
  38. LYCHEE_CODE
  39. ONION_ONLY
  40. DDNS_PROVIDER
  41. MY_USERNAME)
  42. function logging_on_lychee {
  43. echo -n ''
  44. }
  45. function logging_off_lychee {
  46. echo -n ''
  47. }
  48. function lychee_create_database {
  49. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  50. LYCHEE_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  51. else
  52. if [ ! "${LYCHEE_ADMIN_PASSWORD}" ]; then
  53. LYCHEE_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  54. fi
  55. fi
  56. if [ ! "$LYCHEE_ADMIN_PASSWORD" ]; then
  57. return
  58. fi
  59. function_check create_database
  60. create_database lychee "$LYCHEE_ADMIN_PASSWORD"
  61. }
  62. function remove_user_lychee {
  63. remove_username="$1"
  64. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp lychee
  65. }
  66. function add_user_lychee {
  67. if [[ $(app_is_installed lychee) == "0" ]]; then
  68. echo '0'
  69. return
  70. fi
  71. new_username="$1"
  72. new_user_password="$2"
  73. "${PROJECT_NAME}-pass" -u "$new_username" -a lychee -p "$new_user_password"
  74. echo '0'
  75. }
  76. function install_interactive_lychee {
  77. if [ ! "$ONION_ONLY" ]; then
  78. ONION_ONLY='no'
  79. fi
  80. if [[ $ONION_ONLY != "no" ]]; then
  81. LYCHEE_DOMAIN_NAME='lychee.local'
  82. write_config_param "LYCHEE_DOMAIN_NAME" "$LYCHEE_DOMAIN_NAME"
  83. else
  84. function_check interactive_site_details
  85. interactive_site_details "lychee" "LYCHEE_DOMAIN_NAME" "LYCHEE_CODE"
  86. fi
  87. APP_INSTALLED=1
  88. }
  89. function configure_interactive_lychee {
  90. function_check get_mariadb_password
  91. get_mariadb_password
  92. dialog --title $"Lychee Configuration" \
  93. --msgbox $"\\nYou can initially install the system with:\\n\\n Username: root\\n Password: $MARIADB_PASSWORD" 10 70
  94. }
  95. function change_password_lychee {
  96. # LYCHEE_USERNAME="$1"
  97. LYCHEE_PASSWORD="$2"
  98. if [ ${#LYCHEE_PASSWORD} -lt 8 ]; then
  99. echo $'Lychee password is too short'
  100. return
  101. fi
  102. # TODO: This doesn't actually change the password
  103. #${PROJECT_NAME}-pass -u $LYCHEE_USERNAME -a lychee -p "$LYCHEE_PASSWORD"
  104. }
  105. function reconfigure_lychee {
  106. echo -n ''
  107. }
  108. function upgrade_lychee {
  109. CURR_LYCHEE_COMMIT=$(get_completion_param "lychee commit")
  110. if [[ "$CURR_LYCHEE_COMMIT" == "$LYCHEE_COMMIT" ]]; then
  111. return
  112. fi
  113. read_config_param "LYCHEE_DOMAIN_NAME"
  114. function_check set_repo_commit
  115. set_repo_commit /var/www/$LYCHEE_DOMAIN_NAME/htdocs "lychee commit" "$LYCHEE_COMMIT" $LYCHEE_REPO
  116. }
  117. function backup_local_lychee {
  118. LYCHEE_DOMAIN_NAME='lychee.local'
  119. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  120. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  121. fi
  122. lychee_path="/var/www/${LYCHEE_DOMAIN_NAME}/htdocs"
  123. if [ -d "$lychee_path" ]; then
  124. function_check backup_database_to_usb
  125. backup_database_to_usb lychee
  126. backup_directory_to_usb "$lychee_path" lychee
  127. restart_site
  128. fi
  129. }
  130. function restore_local_lychee {
  131. LYCHEE_DOMAIN_NAME='lychee.local'
  132. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  133. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  134. fi
  135. if [ "$LYCHEE_DOMAIN_NAME" ]; then
  136. suspend_site "${LYCHEE_DOMAIN_NAME}"
  137. function_check lychee_create_database
  138. lychee_create_database
  139. function_check restore_database
  140. restore_database lychee "${LYCHEE_DOMAIN_NAME}"
  141. if [ -f "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php" ]; then
  142. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  143. sed -i "s|dbPassword.*|dbPassword = '$MARIADB_PASSWORD';|g" "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php"
  144. MARIADB_PASSWORD=
  145. fi
  146. restart_site
  147. chown -R lychee: "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/"
  148. fi
  149. }
  150. function backup_remote_lychee {
  151. LYCHEE_DOMAIN_NAME='lychee.local'
  152. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  153. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  154. fi
  155. temp_backup_dir=/var/www/${LYCHEE_DOMAIN_NAME}/htdocs
  156. if [ -d "$temp_backup_dir" ]; then
  157. suspend_site "${LYCHEE_DOMAIN_NAME}"
  158. backup_database_to_friend lychee
  159. backup_directory_to_friend "$temp_backup_dir" lychee
  160. restart_site
  161. else
  162. echo $"Lychee domain specified but not found in /var/www/${LYCHEE_DOMAIN_NAME}"
  163. exit 2578
  164. fi
  165. }
  166. function restore_remote_lychee {
  167. LYCHEE_DOMAIN_NAME='lychee.local'
  168. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  169. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  170. fi
  171. suspend_site "${LYCHEE_DOMAIN_NAME}"
  172. function_check restore_database_from_friend
  173. function_check lychee_create_database
  174. lychee_create_database
  175. restore_database_from_friend lychee "${LYCHEE_DOMAIN_NAME}"
  176. if [ -f "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php" ]; then
  177. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  178. sed -i "s|dbPassword.*|dbPassword = '$MARIADB_PASSWORD';|g" "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php"
  179. MARIADB_PASSWORD=
  180. fi
  181. restart_site
  182. chown -R lychee: "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/"
  183. }
  184. function remove_lychee {
  185. if [ ${#LYCHEE_DOMAIN_NAME} -eq 0 ]; then
  186. return
  187. fi
  188. read_config_param "LYCHEE_DOMAIN_NAME"
  189. nginx_dissite "$LYCHEE_DOMAIN_NAME"
  190. remove_certs "${LYCHEE_DOMAIN_NAME}"
  191. drop_database lychee
  192. remove_backup_database_local lychee
  193. if [ -f "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME" ]; then
  194. rm -f "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  195. fi
  196. if [ -d "/var/www/$LYCHEE_DOMAIN_NAME" ]; then
  197. rm -rf "/var/www/$LYCHEE_DOMAIN_NAME"
  198. fi
  199. remove_config_param LYCHEE_DOMAIN_NAME
  200. remove_config_param LYCHEE_CODE
  201. function_check remove_onion_service
  202. remove_onion_service lychee "${LYCHEE_ONION_PORT}"
  203. remove_completion_param "install_lychee"
  204. sed -i '/Lychee/d' "$COMPLETION_FILE"
  205. sed -i '/lychee/d' "$COMPLETION_FILE"
  206. function_check remove_ddns_domain
  207. remove_ddns_domain "$LYCHEE_DOMAIN_NAME"
  208. }
  209. function install_lychee_website {
  210. function_check nginx_http_redirect
  211. nginx_http_redirect "$LYCHEE_DOMAIN_NAME"
  212. { echo 'server {';
  213. echo ' listen 443 ssl;';
  214. echo ' #listen [::]:443 ssl;';
  215. echo " root /var/www/$LYCHEE_DOMAIN_NAME/htdocs;";
  216. echo " server_name $LYCHEE_DOMAIN_NAME;";
  217. echo ' access_log /dev/null;';
  218. echo " error_log /dev/null;";
  219. echo ' index index.html;';
  220. echo ' charset utf-8;';
  221. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  222. function_check nginx_ssl
  223. nginx_ssl "$LYCHEE_DOMAIN_NAME"
  224. function_check nginx_security_options
  225. nginx_security_options "$LYCHEE_DOMAIN_NAME"
  226. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  227. echo '';
  228. echo ' # rewrite to front controller as default rule';
  229. echo ' location / {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  230. function_check nginx_limits
  231. nginx_limits "$LYCHEE_DOMAIN_NAME"
  232. { echo ' }';
  233. echo '';
  234. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  235. echo ' # or a unix socket';
  236. echo ' location ~* \.php$ {';
  237. echo ' # Zero-day exploit defense.';
  238. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  239. echo " # Won't work properly (404 error) if the file is not stored on this";
  240. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  241. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  242. echo " # another machine. And then cross your fingers that you won't get hacked.";
  243. echo " try_files \$uri \$uri/ /index.html;";
  244. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  245. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  246. echo ' # With php-cgi alone:';
  247. echo ' # fastcgi_pass 127.0.0.1:9000;';
  248. echo ' # With php-fpm:';
  249. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  250. echo ' include fastcgi_params;';
  251. echo ' fastcgi_read_timeout 30;';
  252. echo ' fastcgi_index index.html;';
  253. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  254. echo ' }';
  255. echo '';
  256. echo ' # deny access to all dot files';
  257. echo ' location ~ /\. {';
  258. echo ' deny all;';
  259. echo ' }';
  260. echo '';
  261. echo ' #deny access to store';
  262. echo ' location ~ /store {';
  263. echo ' deny all;';
  264. echo ' }';
  265. echo ' location ~ /(data|conf|bin|inc)/ {';
  266. echo ' deny all;';
  267. echo ' }';
  268. echo ' location ~ /\.ht {';
  269. echo ' deny all;';
  270. echo ' }';
  271. echo '}';
  272. echo ''; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  273. }
  274. function install_lychee_website_onion {
  275. { echo 'server {';
  276. echo " listen 127.0.0.1:${LYCHEE_ONION_PORT} default_server;";
  277. echo " root /var/www/$LYCHEE_DOMAIN_NAME/htdocs;";
  278. echo " server_name $LYCHEE_ONION_HOSTNAME;";
  279. echo ' access_log /dev/null;';
  280. echo " error_log /dev/null;";
  281. echo ' index index.html;';
  282. echo ' charset utf-8;';
  283. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  284. function_check nginx_security_options
  285. nginx_security_options "$LYCHEE_DOMAIN_NAME"
  286. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  287. echo '';
  288. echo ' # rewrite to front controller as default rule';
  289. echo ' location / {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  290. function_check nginx_limits
  291. nginx_limits "$LYCHEE_DOMAIN_NAME"
  292. { echo ' }';
  293. echo '';
  294. echo ' # block these file types';
  295. echo ' location ~* \.(tpl|md|tgz|log|out)$ {';
  296. echo ' deny all;';
  297. echo ' }';
  298. echo '';
  299. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  300. echo ' # or a unix socket';
  301. echo ' location ~* \.php$ {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  302. function_check nginx_limits
  303. nginx_limits "$LYCHEE_DOMAIN_NAME"
  304. { echo ' # Zero-day exploit defense.';
  305. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  306. echo " # Won't work properly (404 error) if the file is not stored on this";
  307. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  308. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  309. echo " # another machine. And then cross your fingers that you won't get hacked.";
  310. echo " try_files \$uri \$uri/ /index.html;";
  311. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  312. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  313. echo ' # With php-cgi alone:';
  314. echo ' # fastcgi_pass 127.0.0.1:9000;';
  315. echo ' # With php-fpm:';
  316. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  317. echo ' include fastcgi_params;';
  318. echo ' fastcgi_read_timeout 30;';
  319. echo ' fastcgi_index index.html;';
  320. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  321. echo ' }';
  322. echo '';
  323. echo ' # deny access to all dot files';
  324. echo ' location ~ /\. {';
  325. echo ' deny all;';
  326. echo ' }';
  327. echo '';
  328. echo ' #deny access to store';
  329. echo ' location ~ /store {';
  330. echo ' deny all;';
  331. echo ' }';
  332. echo ' location ~ /(data|conf|bin|inc)/ {';
  333. echo ' deny all;';
  334. echo ' }';
  335. echo ' location ~ /\.ht {';
  336. echo ' deny all;';
  337. echo ' }';
  338. echo '}'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  339. }
  340. function install_lychee_from_repo {
  341. if [ ! -d "/var/www/$LYCHEE_DOMAIN_NAME" ]; then
  342. mkdir "/var/www/$LYCHEE_DOMAIN_NAME"
  343. fi
  344. cd "/var/www/$LYCHEE_DOMAIN_NAME" || exit 682468246
  345. if [ -d /repos/lychee ]; then
  346. mkdir htdocs
  347. cp -r -p /repos/lychee/. htdocs
  348. cd htdocs || exit 963756345
  349. git pull
  350. else
  351. git_clone "$LYCHEE_REPO" htdocs
  352. fi
  353. cd htdocs || exit 1437534858
  354. git checkout "$LYCHEE_COMMIT" -b "$LYCHEE_COMMIT"
  355. set_completion_param "lychee commit" "$LYCHEE_COMMIT"
  356. }
  357. function install_lychee {
  358. if [ ! $ONION_ONLY ]; then
  359. ONION_ONLY='no'
  360. fi
  361. if [ ! "$LYCHEE_DOMAIN_NAME" ]; then
  362. echo $'The lychee domain name was not specified'
  363. exit 543672
  364. fi
  365. # for the avatar changing command
  366. apt-get -yq install imagemagick exif zip php-mcrypt mcrypt libfcgi0ldbl
  367. function_check install_lychee_from_repo
  368. install_lychee_from_repo
  369. if [[ $ONION_ONLY == "no" ]]; then
  370. function_check install_lychee_website
  371. install_lychee_website
  372. else
  373. echo -n '' > "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  374. fi
  375. LYCHEE_ONION_HOSTNAME=$(add_onion_service lychee 80 ${LYCHEE_ONION_PORT})
  376. function_check install_lychee_website_onion
  377. install_lychee_website_onion
  378. function_check create_site_certificate
  379. create_site_certificate "$LYCHEE_DOMAIN_NAME" 'yes'
  380. function_check configure_php
  381. configure_php
  382. chmod -R 1777 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/"
  383. chmod -R 1777 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/"
  384. chown -R www-data:www-data "/var/www/$LYCHEE_DOMAIN_NAME/htdocs"
  385. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/big/index.html"
  386. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/medium/index.html"
  387. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/import/index.html"
  388. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/thumb/index.html"
  389. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/.gitignore"
  390. function_check nginx_ensite
  391. nginx_ensite "$LYCHEE_DOMAIN_NAME"
  392. function_check install_mariadb
  393. install_mariadb
  394. function_check get_mariadb_password
  395. get_mariadb_password
  396. function_check lychee_create_database
  397. lychee_create_database
  398. systemctl restart mariadb
  399. systemctl restart php7.0-fpm
  400. systemctl restart nginx
  401. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a lychee -p "$LYCHEE_ADMIN_PASSWORD"
  402. function_check add_ddns_domain
  403. add_ddns_domain "$LYCHEE_DOMAIN_NAME"
  404. set_completion_param "lychee domain" "$LYCHEE_DOMAIN_NAME"
  405. APP_INSTALLED=1
  406. }
  407. # NOTE: deliberately no exit 0