freedombone-app-riot 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Riot Web user interface for Matrix
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2017-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 chat'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. RIOT_VERSION='0.13.3'
  32. RIOT_FILENAME="riot-v${RIOT_VERSION}"
  33. RIOT_HASH='bcd6c2f4be018612ac76a71b58749a5edab1e02de7d145a22d9b9aa6e6a89129'
  34. RIOT_DOWNLOAD_URL="https://github.com/vector-im/riot-web/releases/download/v${RIOT_VERSION}"
  35. RIOT_ONION_PORT=8115
  36. RIOT_ONION_HOSTNAME=
  37. RIOT_DOMAIN_NAME=
  38. RIOT_CODE=
  39. riot_variables=(MY_USERNAME
  40. RIOT_DOMAIN_NAME
  41. MATRIX_DOMAIN_NAME
  42. SYSTEM_TYPE
  43. ONION_ONLY
  44. DDNS_PROVIDER)
  45. function logging_on_riot {
  46. echo -n ''
  47. }
  48. function logging_off_riot {
  49. echo -n ''
  50. }
  51. function remove_user_riot {
  52. echo -n ''
  53. }
  54. function add_user_riot {
  55. echo '0'
  56. }
  57. function riot_remove_bad_links {
  58. sed -i '/riot.im/d' /var/www/$RIOT_DOMAIN_NAME/htdocs/home.html
  59. }
  60. function install_interactive_riot {
  61. if [[ $ONION_ONLY != "no" ]]; then
  62. RIOT_DOMAIN_NAME='riot.local'
  63. else
  64. RIOT_DETAILS_COMPLETE=
  65. while [ ! $RIOT_DETAILS_COMPLETE ]
  66. do
  67. data=$(mktemp 2>/dev/null)
  68. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  69. dialog --backtitle $"Freedombone Configuration" \
  70. --title $"Riot Web user interface for Matrix" \
  71. --form $"\\nPlease enter your details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
  72. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  73. $"Code:" 2 1 "$(grep 'RIOT_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
  74. 2> "$data"
  75. else
  76. dialog --backtitle $"Freedombone Configuration" \
  77. --title $"Riot Web user interface for Matrix" \
  78. --form $"\\nPlease enter your details. The background image URL can be left blank.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 2 \
  79. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  80. 2> "$data"
  81. fi
  82. sel=$?
  83. case $sel in
  84. 1) rm -f "$data"
  85. exit 1;;
  86. 255) rm -f "$data"
  87. exit 1;;
  88. esac
  89. RIOT_DOMAIN_NAME=$(sed -n 1p < "$data")
  90. if [ "$RIOT_DOMAIN_NAME" ]; then
  91. TEST_DOMAIN_NAME=$RIOT_DOMAIN_NAME
  92. validate_domain_name
  93. if [[ "$TEST_DOMAIN_NAME" != "$RIOT_DOMAIN_NAME" ]]; then
  94. RIOT_DOMAIN_NAME=
  95. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  96. else
  97. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  98. RIOT_CODE=$(sed -n 2p < "$data")
  99. validate_freedns_code "$RIOT_CODE"
  100. if [ ! "$VALID_CODE" ]; then
  101. RIOT_DOMAIN_NAME=
  102. fi
  103. fi
  104. fi
  105. fi
  106. if [ $RIOT_DOMAIN_NAME ]; then
  107. RIOT_DETAILS_COMPLETE="yes"
  108. fi
  109. rm -f "$data"
  110. done
  111. # save the results in the config file
  112. write_config_param "RIOT_CODE" "$RIOT_CODE"
  113. fi
  114. write_config_param "RIOT_DOMAIN_NAME" "$RIOT_DOMAIN_NAME"
  115. APP_INSTALLED=1
  116. }
  117. function reconfigure_riot {
  118. echo -n ''
  119. }
  120. function riot_download {
  121. # download
  122. if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
  123. wget "${RIOT_DOWNLOAD_URL}/${RIOT_FILENAME}.tar.gz" -O "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz"
  124. fi
  125. if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
  126. echo $'Unable to download Riot Web from releases'
  127. exit 62836
  128. fi
  129. cd "$INSTALL_DIR" || exit 2468724628
  130. # check the hash
  131. curr_hash=$(sha256sum ${RIOT_FILENAME}.tar.gz | awk -F ' ' '{print $1}')
  132. if [[ "$curr_hash" != "$RIOT_HASH" ]]; then
  133. echo $'Riot download hash does not match'
  134. exit 78352
  135. fi
  136. tar -xzvf ${RIOT_FILENAME}.tar.gz
  137. if [ ! -d "$INSTALL_DIR/${RIOT_FILENAME}" ]; then
  138. echo $'Unable to extract Riot Web tarball'
  139. exit 542826
  140. fi
  141. cp -r "$INSTALL_DIR/${RIOT_FILENAME}/"* "/var/www/$RIOT_DOMAIN_NAME/htdocs"
  142. chown -R www-data:www-data "/var/www/$RIOT_DOMAIN_NAME/htdocs"
  143. }
  144. function upgrade_riot {
  145. if ! grep -q 'riot version:' "$COMPLETION_FILE"; then
  146. return
  147. fi
  148. CURR_RIOT_VERSION=$(get_completion_param "riot version")
  149. echo "riot current version: ${CURR_RIOT_VERSION}"
  150. echo "riot app version: ${RIOT_VERSION}"
  151. if [[ "${CURR_RIOT_VERSION}" == "${RIOT_VERSION}" ]]; then
  152. return
  153. fi
  154. riot_download
  155. sed -i "s|riot version.*|riot version:$RIOT_VERSION|g" "${COMPLETION_FILE}"
  156. riot_remove_bad_links
  157. systemctl restart nginx
  158. }
  159. function backup_local_riot {
  160. echo -n ''
  161. }
  162. function restore_local_riot {
  163. echo -n ''
  164. }
  165. function backup_remote_riot {
  166. echo -n ''
  167. }
  168. function restore_remote_riot {
  169. echo -n ''
  170. }
  171. function remove_riot {
  172. function_check remove_onion_service
  173. remove_onion_service riot ${RIOT_ONION_PORT}
  174. nginx_dissite $RIOT_DOMAIN_NAME
  175. if [ -f /etc/nginx/sites-available/$RIOT_DOMAIN_NAME ]; then
  176. rm /etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  177. fi
  178. if [ -d /var/www/$RIOT_DOMAIN_NAME ]; then
  179. rm -rf /var/www/$RIOT_DOMAIN_NAME
  180. fi
  181. remove_completion_param install_riot
  182. sed -i '/riot /d' "$COMPLETION_FILE"
  183. }
  184. function install_riot {
  185. if [[ $ONION_ONLY != 'no' ]]; then
  186. return
  187. fi
  188. # check that matrix has been installed
  189. if [ ! "$MATRIX_DOMAIN_NAME" ]; then
  190. exit 687292
  191. fi
  192. if [[ "$MATRIX_DOMAIN_NAME" != *'.'* ]]; then
  193. exit 256288
  194. fi
  195. if [ ! -d /var/lib/matrix ]; then
  196. exit 827334
  197. fi
  198. function_check get_completion_param
  199. MATRIX_ONION_DOMAIN_NAME=$(get_completion_param "matrix onion domain")
  200. apt-get -yq install wget
  201. if [ ! -d /var/www/$RIOT_DOMAIN_NAME/htdocs ]; then
  202. mkdir -p /var/www/$RIOT_DOMAIN_NAME/htdocs
  203. fi
  204. if [ ! -d "$INSTALL_DIR" ]; then
  205. mkdir "$INSTALL_DIR"
  206. fi
  207. riot_download
  208. cd "/var/www/$RIOT_DOMAIN_NAME/htdocs" || exit 4628462876
  209. if [[ "$ONION_ONLY" == 'no' ]]; then
  210. riot_config_file="config.${RIOT_DOMAIN_NAME}.json"
  211. cp config.sample.json $riot_config_file
  212. sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
  213. sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
  214. sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"https://${MATRIX_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
  215. sed -i "/\"servers\":/a \"${MATRIX_DOMAIN_NAME}\"," "$riot_config_file"
  216. else
  217. riot_config_file="config.${MATRIX_ONION_DOMAIN_NAME}.json"
  218. cp config.sample.json "$riot_config_file"
  219. sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
  220. sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
  221. sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
  222. sed -i "/\"servers\":/a \"${MATRIX_ONION_DOMAIN_NAME}\"," "$riot_config_file"
  223. fi
  224. sed -i "s|\"integrations_ui_url\":.*|\"integrations_ui_url\": \"\",|g" "$riot_config_file"
  225. sed -i "s|\"integrations_rest_url\":.*|\"integrations_rest_url\": \"\",|g" "$riot_config_file"
  226. sed -i 's|https://piwik.riot.im/||g' "$riot_config_file"
  227. RIOT_ONION_HOSTNAME=$(add_onion_service riot 80 ${RIOT_ONION_PORT})
  228. riot_nginx_site=/etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  229. if [[ $ONION_ONLY == "no" ]]; then
  230. function_check nginx_http_redirect
  231. nginx_http_redirect $RIOT_DOMAIN_NAME
  232. { echo 'server {';
  233. echo ' listen 443 ssl;';
  234. echo ' #listen [::]:443 ssl;';
  235. echo " server_name $RIOT_DOMAIN_NAME;";
  236. echo '';
  237. echo ' # Security'; } >> $riot_nginx_site
  238. function_check nginx_ssl
  239. nginx_ssl $RIOT_DOMAIN_NAME
  240. function_check nginx_security_options
  241. nginx_security_options $RIOT_DOMAIN_NAME
  242. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  243. echo '';
  244. echo ' # Logs';
  245. echo ' access_log /dev/null;';
  246. echo ' error_log /dev/null;';
  247. echo '';
  248. echo ' # Root';
  249. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
  250. echo '';
  251. echo ' index index.html;';
  252. echo '';
  253. echo ' location / {'; } >> $riot_nginx_site
  254. function_check nginx_limits
  255. nginx_limits $RIOT_DOMAIN_NAME '15m'
  256. { echo ' }';
  257. echo '}';
  258. echo ''; } >> $riot_nginx_site
  259. else
  260. echo -n '' > $riot_nginx_site
  261. fi
  262. { echo 'server {';
  263. echo " listen 127.0.0.1:$RIOT_ONION_PORT default_server;";
  264. echo " server_name $RIOT_ONION_HOSTNAME;";
  265. echo ''; } >> $riot_nginx_site
  266. function_check nginx_security_options
  267. nginx_security_options $RIOT_DOMAIN_NAME
  268. { echo '';
  269. echo ' # Logs';
  270. echo ' access_log /dev/null;';
  271. echo ' error_log /dev/null;';
  272. echo '';
  273. echo ' # Root';
  274. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
  275. echo '';
  276. echo ' index index.html;';
  277. echo '';
  278. echo ' location / {'; } >> $riot_nginx_site
  279. function_check nginx_limits
  280. nginx_limits $RIOT_DOMAIN_NAME '15m'
  281. echo ' }' >> "$riot_nginx_site"
  282. echo '}' >> "$riot_nginx_site"
  283. sed '/Content-Security-Policy/d' "$riot_nginx_site"
  284. sed -i 's| DENY;| SAMEORIGIN;|g' "$riot_nginx_site"
  285. sed -i 's|limit_conn conn_limit_per_ip.*|limit_conn conn_limit_per_ip 50;|g' "$riot_nginx_site"
  286. sed -i 's|limit_req zone.*|limit_req zone=req_limit_per_ip burst=20 nodelay;|g' "$riot_nginx_site"
  287. function_check create_site_certificate
  288. if [ ! -f "/etc/ssl/certs/${RIOT_DOMAIN_NAME}.pem" ]; then
  289. create_site_certificate "$RIOT_DOMAIN_NAME" 'yes'
  290. fi
  291. function_check nginx_ensite
  292. nginx_ensite $RIOT_DOMAIN_NAME
  293. function_check add_ddns_domain
  294. add_ddns_domain $RIOT_DOMAIN_NAME
  295. riot_remove_bad_links
  296. chown -R www-data:www-data /var/www/$RIOT_DOMAIN_NAME/htdocs
  297. systemctl restart nginx
  298. set_completion_param "riot domain" "$RIOT_DOMAIN_NAME"
  299. if ! grep -q "riot version:" "${COMPLETION_FILE}"; then
  300. echo "riot version:${RIOT_VERSION}" >> "${COMPLETION_FILE}"
  301. else
  302. sed -i "s|riot version.*|riot version:${RIOT_VERSION}|g" "${COMPLETION_FILE}"
  303. fi
  304. APP_INSTALLED=1
  305. }
  306. # NOTE: deliberately no exit 0