freedombone-app-lychee 16KB

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