freedombone-app-privatebin 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # privatebin application
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 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. PRIVATEBIN_DOMAIN_NAME=
  32. PRIVATEBIN_CODE=
  33. PRIVATEBIN_ONION_PORT=8150
  34. PRIVATEBIN_REPO="https://github.com/PrivateBin/PrivateBin"
  35. PRIVATEBIN_COMMIT='9c132cd839fd5e91da18e4a1e8ebef64fce605fb'
  36. PRIVATEBIN_ADMIN_PASSWORD=
  37. privatebin_variables=(ONION_ONLY
  38. PRIVATEBIN_DOMAIN_NAME
  39. PRIVATEBIN_CODE
  40. DDNS_PROVIDER
  41. MY_USERNAME)
  42. function secure_privatebin {
  43. pbpath="/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  44. pbdata="/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/data"
  45. htgroup='www-data'
  46. rootuser='root'
  47. find "${pbpath}/" -type f -print0 | xargs -0 chmod 0640
  48. find "${pbpath}/" -type d -print0 | xargs -0 chmod 0550
  49. chown -R ${rootuser}:${htgroup} "${pbpath}/"
  50. chown -R www-data:www-data "${pbdata}"
  51. }
  52. function logging_on_privatebin {
  53. echo -n ''
  54. }
  55. function logging_off_privatebin {
  56. echo -n ''
  57. }
  58. function remove_user_privatebin {
  59. echo -n ''
  60. # remove_username="$1"
  61. }
  62. function add_user_privatebin {
  63. # new_username="$1"
  64. # new_user_password="$2"
  65. echo '0'
  66. }
  67. function install_interactive_privatebin {
  68. if [ ! "$ONION_ONLY" ]; then
  69. ONION_ONLY='no'
  70. fi
  71. if [[ "$ONION_ONLY" != "no" ]]; then
  72. PRIVATEBIN_DOMAIN_NAME='privatebin.local'
  73. else
  74. PRIVATEBIN_DETAILS_COMPLETE=
  75. while [ ! $PRIVATEBIN_DETAILS_COMPLETE ]
  76. do
  77. data=$(mktemp 2>/dev/null)
  78. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  79. dialog --backtitle $"Freedombone Configuration" \
  80. --title $"PrivateBin Configuration" \
  81. --form $"\\nPlease enter your PrivateBin details. The background image URL can be left blank.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  82. $"Domain:" 1 1 "$(grep 'PRIVATEBIN_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 33 40 \
  83. $"Code:" 2 1 "$(grep 'PRIVATEBIN_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 33 255 \
  84. 2> "$data"
  85. else
  86. dialog --backtitle $"Freedombone Configuration" \
  87. --title $"PrivateBin Configuration" \
  88. --form $"\\nPlease enter your PrivateBin details. The background image URL can be left blank.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  89. $"Domain:" 1 1 "$(grep 'PRIVATEBIN_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 33 40 \
  90. 2> "$data"
  91. fi
  92. sel=$?
  93. case $sel in
  94. 1) rm -f "$data"
  95. exit 1;;
  96. 255) rm -f "$data"
  97. exit 1;;
  98. esac
  99. PRIVATEBIN_DOMAIN_NAME=$(sed -n 1p < "$data")
  100. if [ "$PRIVATEBIN_DOMAIN_NAME" ]; then
  101. if [[ "$PRIVATEBIN_DOMAIN_NAME" == "$HUBZILLA_DOMAIN_NAME" ]]; then
  102. PRIVATEBIN_DOMAIN_NAME=""
  103. fi
  104. TEST_DOMAIN_NAME=$PRIVATEBIN_DOMAIN_NAME
  105. validate_domain_name
  106. if [[ "$TEST_DOMAIN_NAME" != "$PRIVATEBIN_DOMAIN_NAME" ]]; then
  107. PRIVATEBIN_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. PRIVATEBIN_CODE=$(sed -n 2p < "$data")
  112. validate_freedns_code "$PRIVATEBIN_CODE"
  113. if [ ! "$VALID_CODE" ]; then
  114. PRIVATEBIN_DOMAIN_NAME=
  115. fi
  116. fi
  117. fi
  118. fi
  119. if [ $PRIVATEBIN_DOMAIN_NAME ]; then
  120. PRIVATEBIN_DETAILS_COMPLETE="yes"
  121. fi
  122. rm -f "$data"
  123. done
  124. write_config_param "PRIVATEBIN_CODE" "$PRIVATEBIN_CODE"
  125. fi
  126. write_config_param "PRIVATEBIN_DOMAIN_NAME" "$PRIVATEBIN_DOMAIN_NAME"
  127. APP_INSTALLED=1
  128. }
  129. function change_password_privatebin {
  130. # curr_username="$1"
  131. # new_user_password="$2"
  132. echo -n ''
  133. }
  134. function reconfigure_privatebin {
  135. echo -n ''
  136. }
  137. function upgrade_privatebin {
  138. CURR_PRIVATEBIN_COMMIT=$(get_completion_param "privatebin commit")
  139. if [[ "$CURR_PRIVATEBIN_COMMIT" == "$PRIVATEBIN_COMMIT" ]]; then
  140. return
  141. fi
  142. if grep -q "privatebin domain" "$COMPLETION_FILE"; then
  143. PRIVATEBIN_DOMAIN_NAME=$(get_completion_param "privatebin domain")
  144. fi
  145. # update to the next commit
  146. function_check set_repo_commit
  147. set_repo_commit "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs" "privatebin commit" "$PRIVATEBIN_COMMIT" "$PRIVATEBIN_REPO"
  148. secure_privatebin
  149. }
  150. function backup_local_privatebin {
  151. PRIVATEBIN_DOMAIN_NAME='privatebin'
  152. if grep -q "privatebin domain" "$COMPLETION_FILE"; then
  153. PRIVATEBIN_DOMAIN_NAME=$(get_completion_param "privatebin domain")
  154. fi
  155. source_directory="/var/www/${PRIVATEBIN_DOMAIN_NAME}/htdocs/data"
  156. function_check suspend_site
  157. suspend_site "${PRIVATEBIN_DOMAIN_NAME}"
  158. function_check backup_directory_to_usb
  159. dest_directory=privatebin
  160. backup_directory_to_usb "$source_directory" "$dest_directory"
  161. function_check restart_site
  162. restart_site
  163. }
  164. function restore_local_privatebin {
  165. if ! grep -q "privatebin domain" "$COMPLETION_FILE"; then
  166. return
  167. fi
  168. PRIVATEBIN_DOMAIN_NAME=$(get_completion_param "privatebin domain")
  169. if [ "$PRIVATEBIN_DOMAIN_NAME" ]; then
  170. echo $"Restoring privatebin"
  171. temp_restore_dir=/root/tempprivatebin
  172. privatebin_dir="/var/www/${PRIVATEBIN_DOMAIN_NAME}/htdocs/data"
  173. function_check restore_directory_from_usb
  174. restore_directory_from_usb $temp_restore_dir privatebin
  175. if [ -d $temp_restore_dir ]; then
  176. if [ -d "$temp_restore_dir$privatebin_dir" ]; then
  177. cp -rp "$temp_restore_dir$privatebin_dir/"* "$privatebin_dir/"
  178. else
  179. cp -rp "$temp_restore_dir/"* "$privatebin_dir/"
  180. fi
  181. secure_privatebin
  182. rm -rf $temp_restore_dir
  183. fi
  184. echo $"Restore of privatebin complete"
  185. fi
  186. }
  187. function backup_remote_privatebin {
  188. PRIVATEBIN_DOMAIN_NAME='privatebin'
  189. if grep -q "privatebin domain" "$COMPLETION_FILE"; then
  190. PRIVATEBIN_DOMAIN_NAME=$(get_completion_param "privatebin domain")
  191. fi
  192. source_directory="/var/www/${PRIVATEBIN_DOMAIN_NAME}/htdocs/data"
  193. function_check suspend_site
  194. suspend_site "${PRIVATEBIN_DOMAIN_NAME}"
  195. function_check backup_directory_to_friend
  196. dest_directory=privatebin
  197. backup_directory_to_friend "$source_directory" "$dest_directory"
  198. function_check restart_site
  199. restart_site
  200. }
  201. function restore_remote_privatebin {
  202. if ! grep -q "privatebin domain" "$COMPLETION_FILE"; then
  203. return
  204. fi
  205. PRIVATEBIN_DOMAIN_NAME=$(get_completion_param "privatebin domain")
  206. if [ "$PRIVATEBIN_DOMAIN_NAME" ]; then
  207. temp_restore_dir=/root/tempprivatebin
  208. privatebin_dir=/var/www/${PRIVATEBIN_DOMAIN_NAME}/htdocs/data
  209. function_check restore_directory_from_friend
  210. restore_directory_from_friend $temp_restore_dir privatebin
  211. if [ -d $temp_restore_dir ]; then
  212. if [ -d "$temp_restore_dir$privatebin_dir" ]; then
  213. cp -rp "$temp_restore_dir$privatebin_dir/"* "$privatebin_dir/"
  214. else
  215. cp -rp "$temp_restore_dir/"* "$privatebin_dir/"
  216. fi
  217. secure_privatebin
  218. rm -rf $temp_restore_dir
  219. fi
  220. fi
  221. }
  222. function remove_privatebin {
  223. if [ ${#PRIVATEBIN_DOMAIN_NAME} -eq 0 ]; then
  224. return
  225. fi
  226. read_config_param "PRIVATEBIN_DOMAIN_NAME"
  227. read_config_param "MY_USERNAME"
  228. echo "Removing $PRIVATEBIN_DOMAIN_NAME"
  229. nginx_dissite "$PRIVATEBIN_DOMAIN_NAME"
  230. remove_certs "$PRIVATEBIN_DOMAIN_NAME"
  231. if [ -d "/var/www/$PRIVATEBIN_DOMAIN_NAME" ]; then
  232. rm -rf "/var/www/$PRIVATEBIN_DOMAIN_NAME"
  233. fi
  234. if [ -f "/etc/nginx/sites-available/$PRIVATEBIN_DOMAIN_NAME" ]; then
  235. rm "/etc/nginx/sites-available/$PRIVATEBIN_DOMAIN_NAME"
  236. fi
  237. function_check remove_onion_service
  238. remove_onion_service privatebin ${PRIVATEBIN_ONION_PORT}
  239. if grep -q "privatebin" /etc/crontab; then
  240. sed -i "/privatebin/d" /etc/crontab
  241. fi
  242. remove_app privatebin
  243. remove_completion_param install_privatebin
  244. sed -i '/privatebin/d' "$COMPLETION_FILE"
  245. function_check remove_ddns_domain
  246. remove_ddns_domain "$PRIVATEBIN_DOMAIN_NAME"
  247. }
  248. function install_privatebin {
  249. if [ ! "$ONION_ONLY" ]; then
  250. ONION_ONLY='no'
  251. fi
  252. if [ ! "$PRIVATEBIN_DOMAIN_NAME" ]; then
  253. echo $'No domain name was given for privatebin'
  254. exit 7359
  255. fi
  256. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  257. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  258. apt-get -yq install php-libsodium libsodium18 php-mcrypt
  259. if [ ! -d "/var/www/$PRIVATEBIN_DOMAIN_NAME" ]; then
  260. mkdir "/var/www/$PRIVATEBIN_DOMAIN_NAME"
  261. fi
  262. if [ ! -d "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs" ]; then
  263. if [ -d /repos/privatebin ]; then
  264. mkdir "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  265. cp -r -p /repos/privatebin/. "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  266. cd "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs" || exit 3468246824
  267. git pull
  268. else
  269. function_check git_clone
  270. git_clone "$PRIVATEBIN_REPO" "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  271. fi
  272. if [ ! -d "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs" ]; then
  273. echo $'Unable to clone privatebin repo'
  274. exit 63763873
  275. fi
  276. fi
  277. cd "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs" || exit 24682462
  278. git checkout "$PRIVATEBIN_COMMIT" -b "$PRIVATEBIN_COMMIT"
  279. set_completion_param "privatebin commit" "$PRIVATEBIN_COMMIT"
  280. chmod g+w "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  281. chown -R www-data:www-data "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs"
  282. function_check add_ddns_domain
  283. add_ddns_domain "$PRIVATEBIN_DOMAIN_NAME"
  284. PRIVATEBIN_ONION_HOSTNAME=$(add_onion_service privatebin 80 ${PRIVATEBIN_ONION_PORT})
  285. privatebin_nginx_site=/etc/nginx/sites-available/$PRIVATEBIN_DOMAIN_NAME
  286. if [[ $ONION_ONLY == "no" ]]; then
  287. function_check nginx_http_redirect
  288. nginx_http_redirect "$PRIVATEBIN_DOMAIN_NAME" "index index.php"
  289. { echo 'server {';
  290. echo ' listen 443 ssl;';
  291. echo ' #listen [::]:443 ssl;';
  292. echo " server_name $PRIVATEBIN_DOMAIN_NAME;";
  293. echo ''; } >> "$privatebin_nginx_site"
  294. function_check nginx_compress
  295. nginx_compress "$PRIVATEBIN_DOMAIN_NAME"
  296. echo '' >> "$privatebin_nginx_site"
  297. echo ' # Security' >> "$privatebin_nginx_site"
  298. function_check nginx_ssl
  299. nginx_ssl "$PRIVATEBIN_DOMAIN_NAME"
  300. function_check nginx_security_options
  301. nginx_security_options "$PRIVATEBIN_DOMAIN_NAME"
  302. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  303. echo '';
  304. echo ' # Logs';
  305. echo ' access_log /dev/null;';
  306. echo ' error_log /dev/null;';
  307. echo '';
  308. echo " root /var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs;";
  309. echo '';
  310. echo ' index index.php;';
  311. echo '';
  312. echo ' location ~ \.php {';
  313. echo ' include snippets/fastcgi-php.conf;';
  314. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  315. echo ' fastcgi_read_timeout 30;';
  316. echo ' }';
  317. echo '';
  318. echo ' # Location';
  319. echo ' location / {'; } >> "$privatebin_nginx_site"
  320. function_check nginx_limits
  321. nginx_limits "$PRIVATEBIN_DOMAIN_NAME" '15m'
  322. { echo " try_files \$uri \$uri/ @privatebin;";
  323. echo ' }';
  324. echo '';
  325. echo ' # Restrict access that is unnecessary anyway';
  326. echo ' location ~ /\.(ht|git) {';
  327. echo ' deny all;';
  328. echo ' }';
  329. echo '}';
  330. echo ''; } >> "$privatebin_nginx_site"
  331. else
  332. echo -n '' > "$privatebin_nginx_site"
  333. fi
  334. { echo 'server {';
  335. echo " listen 127.0.0.1:$PRIVATEBIN_ONION_PORT default_server;";
  336. echo " server_name $PRIVATEBIN_ONION_HOSTNAME;";
  337. echo ''; } >> "$privatebin_nginx_site"
  338. function_check nginx_compress
  339. nginx_compress "$PRIVATEBIN_DOMAIN_NAME"
  340. echo '' >> "$privatebin_nginx_site"
  341. function_check nginx_security_options
  342. nginx_security_options "$PRIVATEBIN_DOMAIN_NAME"
  343. { echo '';
  344. echo ' # Logs';
  345. echo ' access_log /dev/null;';
  346. echo ' error_log /dev/null;';
  347. echo '';
  348. echo " root /var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs;";
  349. echo '';
  350. echo ' index index.php;';
  351. echo '';
  352. echo ' location ~ \.php {';
  353. echo ' include snippets/fastcgi-php.conf;';
  354. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  355. echo ' fastcgi_read_timeout 30;';
  356. echo ' }';
  357. echo '';
  358. echo ' # Location';
  359. echo ' location / {'; } >> "$privatebin_nginx_site"
  360. function_check nginx_limits
  361. nginx_limits "$PRIVATEBIN_DOMAIN_NAME" '15m'
  362. { echo " try_files \$uri \$uri/ @privatebin;";
  363. echo ' }';
  364. echo '';
  365. echo ' # Restrict access that is unnecessary anyway';
  366. echo ' location ~ /\.(ht|git) {';
  367. echo ' deny all;';
  368. echo ' }';
  369. echo '}'; } >> "$privatebin_nginx_site"
  370. function_check configure_php
  371. configure_php
  372. function_check create_site_certificate
  373. create_site_certificate "$PRIVATEBIN_DOMAIN_NAME" 'yes'
  374. function_check nginx_ensite
  375. nginx_ensite "$PRIVATEBIN_DOMAIN_NAME"
  376. cp "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.sample.php" "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  377. # Change some defaults
  378. sed -i 's|; qrcode|qrcode|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  379. sed -i 's|default =.*|default = "1day"|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  380. sed -i 's|languagedefault =.*|languagedefault = "en"|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  381. sed -i 's|1week =|; 1week =|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  382. sed -i 's|1month =|; 1month =|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  383. sed -i 's|1year =|; 1year =|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  384. sed -i 's|never =|; never =|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  385. sed -i 's|limit = 10|limit = 30|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  386. sed -i 's|limit = 300|limit = 0|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  387. sed -i 's|batchsize =.*|batchsize = 100|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  388. sed -i 's|sizelimit =.*|sizelimit = 32768|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  389. sed -i 's|defaultformatter =.*|defaultformatter = "markdown"|g' "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/cfg/conf.php"
  390. mkdir -p "/var/www/$PRIVATEBIN_DOMAIN_NAME/htdocs/data"
  391. secure_privatebin
  392. systemctl restart php7.0-fpm
  393. systemctl restart nginx
  394. set_completion_param "privatebin domain" "$PRIVATEBIN_DOMAIN_NAME"
  395. APP_INSTALLED=1
  396. }
  397. # NOTE: deliberately there is no "exit 0"