freedombone-app-privatebin 17KB

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