freedombone-app-privatebin 16KB

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