freedombone-app-privatebin 16KB

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