freedombone-app-matrix 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # matrix server
  10. #
  11. # Test by visiting https://$MATRIX_DOMAIN_NAME/_matrix/key/v2/server/auto
  12. #
  13. # If working then telnet $MATRIX_DOMAIN_NAME 8448 should return a response
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU Affero General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU Affero General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU Affero General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. # Login username is @username:domain
  33. VARIANTS='full full-vim chat'
  34. IN_DEFAULT_INSTALL=0
  35. SHOW_ON_ABOUT=1
  36. NOT_ON_ONION=1
  37. MINIMUM_RAM_MB=1500
  38. MATRIX_DOMAIN_NAME=
  39. MATRIX_CODE=
  40. MATRIX_DATA_DIR='/var/lib/matrix'
  41. MATRIX_HTTP_PORT=8448
  42. MATRIX_PORT=8009
  43. MATRIX_FEDERATION_ONION_PORT=8111
  44. MATRIX_ONION_PORT=8109
  45. MATRIX_REPO="https://github.com/matrix-org/synapse"
  46. MATRIX_COMMIT='ab5e888927e6a38190a65e10af7acf67873124e3'
  47. REPORT_STATS="no"
  48. MATRIX_SECRET=
  49. MATRIX_EXPIRE_MONTHS=1
  50. matrix_variables=(ONION_ONLY
  51. MY_USERNAME
  52. MATRIX_SECRET
  53. DEFAULT_DOMAIN_NAME
  54. MATRIX_DOMAIN_NAME
  55. MATRIX_EXPIRE_MONTHS
  56. MATRIX_CODE)
  57. function matrix_expire_old_posts {
  58. read_config_param MY_USERNAME
  59. read_config_param MATRIX_DOMAIN_NAME
  60. read_config_param MATRIX_EXPIRE_MONTHS
  61. read_config_param MATRIX_PORT
  62. matrix_remove_posts=/usr/bin/matrix-remove
  63. { echo '#!/bin/bash';
  64. echo "cd $MATRIX_DATA_DIR";
  65. echo "ROOM=\$1";
  66. echo "ADMIN=\"@${MY_USERNAME}:$MATRIX_DOMAIN_NAME\"";
  67. echo "TIME='$MATRIX_EXPIRE_MONTHS months ago'";
  68. echo "UNIX_TIMESTAMP=\$(date +%s%3N --date='TZ=\"UTC+0\" '\"\$TIME\")";
  69. echo 'BUSY="pragma busy_timeout=20000"';
  70. echo "BUFFER=\$(sqlite3 homeserver.db \"\$BUSY;select event_id from events where type='m.room.message' and received_ts<'\$UNIX_TIMESTAMP' and room_id='\$ROOM' order by received_ts desc limit 1;\")";
  71. echo "EVENT_ID=\$(echo \$BUFFER|awk '{print \$2}')";
  72. echo "BUFFER=\$(sqlite3 homeserver.db \"\$BUSY;select token from access_tokens where user_id like '\$ADMIN' order by id desc limit 1;\")";
  73. echo "TOKEN=\$(echo \$BUFFER|awk '{print \$2}')";
  74. echo 'set -x';
  75. echo "curl -v -X POST 'https://$MATRIX_DOMAIN_NAME/_matrix/client/r0/admin/purge_history/'\$ROOM'/'\$EVENT_ID'?access_token='\$TOKEN"; } > $matrix_remove_posts
  76. chmod +x $matrix_remove_posts
  77. }
  78. function logging_on_matrix {
  79. if [ -f /var/lib/matrix/homeserver.yaml ]; then
  80. if ! grep -q "log_file: /etc/matrix/homeserver.log" /var/lib/matrix/homeserver.yaml; then
  81. sed -i 's|log_file:.*|log_file: /etc/matrix/homeserver.log|g' /var/lib/matrix/homeserver.yaml
  82. fi
  83. if ! grep -q "#log_config:" /var/lib/matrix/homeserver.yaml; then
  84. sed -i 's|log_config:|#log_config:|g' /var/lib/matrix/homeserver.yaml
  85. fi
  86. fi
  87. }
  88. function logging_off_matrix {
  89. if [ -f /var/lib/matrix/homeserver.yaml ]; then
  90. if ! grep -q "log_file: /dev/null" /var/lib/matrix/homeserver.yaml; then
  91. sed -i 's|log_file:.*|log_file: /dev/null|g' /var/lib/matrix/homeserver.yaml
  92. fi
  93. if ! grep -q "#log_config:" /var/lib/matrix/homeserver.yaml; then
  94. sed -i 's|log_config:|#log_config:|g' /var/lib/matrix/homeserver.yaml
  95. fi
  96. if [ -f /etc/matrix/homeserver.log ]; then
  97. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log
  98. fi
  99. if [ -f /etc/matrix/homeserver.log.1 ]; then
  100. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log.1
  101. fi
  102. if [ -f /etc/matrix/homeserver.log.2 ]; then
  103. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log.2
  104. fi
  105. if [ -f /etc/matrix/homeserver.log.3 ]; then
  106. $REMOVE_FILES_COMMAND /etc/matrix/homeserver.log.3
  107. fi
  108. fi
  109. }
  110. function matrix_nginx {
  111. matrix_nginx_site=/etc/nginx/sites-available/$MATRIX_DOMAIN_NAME
  112. if [[ $ONION_ONLY == "no" ]]; then
  113. { echo 'server {';
  114. echo " listen 443;";
  115. echo " server_name ${MATRIX_DOMAIN_NAME};";
  116. echo '';
  117. echo ' # Security'; } > $matrix_nginx_site
  118. function_check nginx_ssl
  119. nginx_ssl ${MATRIX_DOMAIN_NAME}
  120. function_check nginx_security_options
  121. nginx_security_options ${MATRIX_DOMAIN_NAME}
  122. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  123. echo '';
  124. echo ' # Logs';
  125. echo ' access_log /dev/null;';
  126. echo ' error_log /dev/null;';
  127. echo '';
  128. echo ' root /var/lib/matrix/media_store;';
  129. echo '';
  130. echo ' # Index';
  131. echo ' index index.html;';
  132. echo '';
  133. echo ' location /_matrix {'; } >> $matrix_nginx_site
  134. function_check nginx_limits
  135. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  136. { echo " proxy_pass http://localhost:${MATRIX_PORT};";
  137. echo ' }';
  138. echo '}';
  139. echo '';
  140. echo 'server {';
  141. echo " listen ${MATRIX_HTTP_PORT};";
  142. echo " server_name ${MATRIX_DOMAIN_NAME};";
  143. echo '';
  144. echo ' # Security'; } >> $matrix_nginx_site
  145. function_check nginx_ssl
  146. nginx_ssl ${MATRIX_DOMAIN_NAME}
  147. function_check nginx_security_options
  148. nginx_security_options ${MATRIX_DOMAIN_NAME}
  149. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  150. echo '';
  151. echo ' # Logs';
  152. echo ' access_log /dev/null;';
  153. echo ' error_log /dev/null;';
  154. echo '';
  155. echo ' root /var/lib/matrix/media_store;';
  156. echo '';
  157. echo ' # Index';
  158. echo ' index index.html;';
  159. echo '';
  160. echo ' # Location';
  161. echo ' location /_matrix {'; } >> $matrix_nginx_site
  162. function_check nginx_limits
  163. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  164. { echo " proxy_pass http://localhost:${MATRIX_PORT};";
  165. echo ' }';
  166. echo '}';
  167. echo ''; } >> $matrix_nginx_site
  168. else
  169. echo -n '' > $matrix_nginx_site
  170. fi
  171. { echo 'server {';
  172. echo " listen 127.0.0.1:$MATRIX_FEDERATION_ONION_PORT default_server;";
  173. echo " server_name $MATRIX_DOMAIN_NAME;";
  174. echo ''; } >> $matrix_nginx_site
  175. function_check nginx_security_options
  176. nginx_security_options $MATRIX_DOMAIN_NAME
  177. { echo '';
  178. echo ' # Logs';
  179. echo ' access_log /dev/null;';
  180. echo ' error_log /dev/null;';
  181. echo '';
  182. echo ' root /var/lib/matrix/media_store;';
  183. echo '';
  184. echo ' # Location';
  185. echo ' location /_matrix {'; } >> $matrix_nginx_site
  186. function_check nginx_limits
  187. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  188. { echo " proxy_pass http://localhost:${MATRIX_PORT};";
  189. echo ' }';
  190. echo '}';
  191. echo '';
  192. echo 'server {';
  193. echo " listen 127.0.0.1:$MATRIX_ONION_PORT default_server;";
  194. echo " server_name $MATRIX_DOMAIN_NAME;";
  195. echo ''; } >> $matrix_nginx_site
  196. function_check nginx_security_options
  197. nginx_security_options $MATRIX_DOMAIN_NAME
  198. { echo '';
  199. echo ' # Logs';
  200. echo ' access_log /dev/null;';
  201. echo ' error_log /dev/null;';
  202. echo '';
  203. echo ' root /var/lib/matrix/media_store;';
  204. echo '';
  205. echo ' # Location';
  206. echo ' location /_matrix {'; } >> $matrix_nginx_site
  207. function_check nginx_limits
  208. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  209. { echo " proxy_pass http://localhost:${MATRIX_PORT};";
  210. echo ' }';
  211. echo '}'; } >> $matrix_nginx_site
  212. if [ ! -d /var/www/$MATRIX_DOMAIN_NAME ]; then
  213. mkdir -p /var/www/$MATRIX_DOMAIN_NAME/htdocs
  214. fi
  215. function_check add_ddns_domain
  216. add_ddns_domain $MATRIX_DOMAIN_NAME
  217. }
  218. function matrix_generate_homeserver_file {
  219. local filepath="${1}"
  220. cd /etc/matrix || exit 468246824
  221. python -m synapse.app.homeserver \
  222. --config-path "${filepath}" \
  223. --generate-config \
  224. --report-stats ${REPORT_STATS} \
  225. --server-name ${MATRIX_DOMAIN_NAME}
  226. }
  227. function matrix_configure_homeserver_yaml {
  228. turnkey="${1}"
  229. filepath="${2}"
  230. ymltemp="$(mktemp)"
  231. awk -v TURNURIES="turn_uris: [\"turn:${MATRIX_DOMAIN_NAME}:${TURN_HTTP_PORT}?transport=udp\", \"turn:${DEFAULT_DOMAIN_NAME}:${TURN_HTTP_PORT}?transport=tcp\"]" \
  232. -v TURNSHAREDSECRET="turn_shared_secret: \"${turnkey}\"" \
  233. -v PIDFILE="pid_file: ${MATRIX_DATA_DIR}/homeserver.pid" \
  234. -v DATABASE="database: \"${MATRIX_DATA_DIR}/homeserver.db\"" \
  235. -v LOGFILE="log_file: \"/dev/null\"" \
  236. -v MEDIASTORE="media_store_path: \"${MATRIX_DATA_DIR}/media_store\"" \
  237. '{
  238. sub(/turn_shared_secret: "YOUR_SHARED_SECRET"/, TURNSHAREDSECRET);
  239. sub(/turn_uris: \[\]/, TURNURIES);
  240. sub(/pid_file: \/homeserver.pid/, PIDFILE);
  241. sub(/database: "\/homeserver.db"/, DATABASE);
  242. sub(/log_file: "\/homeserver.log"/, LOGFILE);
  243. sub(/media_store_path: "\/media_store"/, MEDIASTORE);
  244. print;
  245. }' "${filepath}" > "${ymltemp}"
  246. # shellcheck disable=SC2086
  247. mv ${ymltemp} ${filepath}
  248. if [[ $ONION_ONLY == "no" ]]; then
  249. sed -i "s|tls_certificate_path:.*|tls_certificate_path: \"/etc/ssl/certs/${MATRIX_DOMAIN_NAME}.pem\"|g" "${filepath}"
  250. if ! grep -q '#tls_private_key_path' "${filepath}"; then
  251. sed -i 's|tls_private_key_path|#tls_private_key_path|g' "${filepath}"
  252. fi
  253. sed -i "s|tls_dh_params_path:.*|tls_dh_params_path: \"/etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam\"|g" "${filepath}"
  254. fi
  255. sed -i "s|${MATRIX_HTTP_PORT}|8449|g" "${filepath}"
  256. sed -i "s|8008|${MATRIX_PORT}|g" "${filepath}"
  257. sed -i 's|tls:.*|tls: False|g' "${filepath}"
  258. sed -i 's|no_tls: .*|no_tls: True|g' "${filepath}"
  259. sed -i ':a;N;$!ba;s/ tls: [^\n]*/ tls: False/2' "${filepath}"
  260. sed -i 's|enable_registration_captcha.*|enable_registration_captcha: False|g' "${filepath}"
  261. sed -i "s|database: \".*|database: \"${MATRIX_DATA_DIR}/homeserver.db\"|g" "${filepath}"
  262. sed -i "s|media_store_path:.*|media_store_path: \"${MATRIX_DATA_DIR}/media_store\"|g" "${filepath}"
  263. sed -i "s|pid_file:.*|pid_file: \"${MATRIX_DATA_DIR}/homeserver.pid\"|g" "${filepath}"
  264. sed -i "s|log_file:.*|log_file: \"/dev/null\"|g" "${filepath}"
  265. sed -i "s|log_config:|#log_config:|g" "${filepath}"
  266. sed -i 's|bind_address:.*|bind_address: 127.0.0.1|g' "${filepath}"
  267. sed -i "s|bind_addresses:.*|bind_addresses: ['127.0.0.1']|g" "${filepath}"
  268. sed -i 's|x_forwarded:.*|x_forwarded: false|g' "${filepath}"
  269. sed -i "s|server_name:.*|server_name: \"${MATRIX_DOMAIN_NAME}\"|g" "${filepath}"
  270. sed -i "/trusted_third_party_id_servers:/a - ${MATRIX_DOMAIN_NAME}" "${filepath}"
  271. sed -i "s|- ${MATRIX_DOMAIN_NAME}| - ${MATRIX_DOMAIN_NAME}|g" "${filepath}"
  272. sed -i "s|enable_registration:.*|enable_registration: False|g" "${filepath}"
  273. }
  274. function matrix_diff {
  275. DIFFPARAMS="${DIFFPARAMS:-Naur}"
  276. MATRIX_DOMAIN_NAME="${MATRIX_DOMAIN_NAME:-demo_server_name}"
  277. REPORT_STATS="${REPORT_STATS:-no_or_yes}"
  278. export MATRIX_DOMAIN_NAME REPORT_STATS
  279. matrix_generate_synapse_file "$INSTALL_DIR/homeserver.synapse.yaml"
  280. # shellcheck disable=SC2086
  281. diff -${DIFFPARAMS} "$INSTALL_DIR/homeserver.synapse.yaml" "${MATRIX_DATA_DIR}/homeserver.yaml"
  282. rm "$INSTALL_DIR/homeserver.synapse.yaml"
  283. }
  284. function matrix_generate {
  285. breakup="0"
  286. [[ -z "${MATRIX_DOMAIN_NAME}" ]] && echo "STOP! environment variable MATRIX_DOMAIN_NAME must be set" && breakup="1"
  287. [[ -z "${REPORT_STATS}" ]] && echo "STOP! environment variable REPORT_STATS must be set to 'no' or 'yes'" && breakup="1"
  288. [[ "${breakup}" == "1" ]] && exit 1
  289. [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
  290. echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
  291. homeserver_config=${MATRIX_DATA_DIR}/homeserver.yaml
  292. if [ -f $homeserver_config ]; then
  293. rm $homeserver_config
  294. fi
  295. matrix_generate_homeserver_file $homeserver_config
  296. matrix_configure_homeserver_yaml "${turnkey}" $homeserver_config
  297. }
  298. function create_matrix_user_removal_script {
  299. read_config_param MY_USERNAME
  300. read_config_param MATRIX_DOMAIN_NAME
  301. matrix_remove_user=/usr/bin/matrix-remove-user
  302. { echo '#!/bin/bash';
  303. echo "cd $MATRIX_DATA_DIR";
  304. echo "remove_username=\$1";
  305. echo "ADMIN=\"@${MY_USERNAME}:$MATRIX_DOMAIN_NAME\"";
  306. echo 'BUSY="pragma busy_timeout=20000"';
  307. echo "BUFFER=\$(sqlite3 homeserver.db \"\$BUSY;select token from access_tokens where user_id like '\$ADMIN' order by id desc limit 1;\")";
  308. echo "TOKEN=\$(echo \$BUFFER|awk '{print \$2}')";
  309. echo 'set -x';
  310. echo "curl -X POST 'https://$MATRIX_DOMAIN_NAME/_matrix/client/r0/admin/deactivate/%40\$remove_username%3A$MATRIX_DOMAIN_NAME?access_token=\$TOKEN' --data '{}'"; } > $matrix_remove_user
  311. chmod +x $matrix_remove_user
  312. }
  313. function remove_user_matrix {
  314. remove_username="$1"
  315. create_matrix_user_removal_script
  316. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp matrix
  317. $matrix_remove_user "$remove_username"
  318. }
  319. function add_user_matrix {
  320. new_username="$1"
  321. new_user_password="$2"
  322. read_config_param MY_USERNAME
  323. read_config_param MATRIX_DOMAIN_NAME
  324. "${PROJECT_NAME}-pass" -u "$new_username" -a matrix -p "$new_user_password"
  325. if [[ "$new_username" != "$MY_USERNAME" ]]; then
  326. echo 'no' | register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml -u "${new_username}" -p "${new_user_password}" http://localhost:${MATRIX_PORT}
  327. else
  328. echo 'yes' | register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml -u "${new_username}" -p "${new_user_password}" -a http://localhost:${MATRIX_PORT}
  329. fi
  330. echo "0"
  331. }
  332. function install_interactive_matrix {
  333. if [ ! "$ONION_ONLY" ]; then
  334. ONION_ONLY='no'
  335. fi
  336. if [[ $ONION_ONLY != "no" ]]; then
  337. MATRIX_DOMAIN_NAME='matrix.local'
  338. write_config_param "MATRIX_DOMAIN_NAME" "$MATRIX_DOMAIN_NAME"
  339. else
  340. function_check interactive_site_details
  341. interactive_site_details "matrix" "MATRIX_DOMAIN_NAME" "MATRIX_CODE"
  342. if [ ! $MATRIX_DOMAIN_NAME ]; then
  343. return
  344. fi
  345. read_config_param "MATRIX_DOMAIN_NAME"
  346. read_config_param "MATRIX_CODE"
  347. fi
  348. APP_INSTALLED=1
  349. }
  350. function change_password_matrix {
  351. #curr_username="$1"
  352. new_user_password="$2"
  353. #${PROJECT_NAME}-pass -u "$curr_username" -a matrix -p "$new_user_password"
  354. }
  355. function reconfigure_matrix {
  356. echo -n ''
  357. }
  358. function upgrade_matrix {
  359. if [ ! -d /etc/matrix ]; then
  360. return
  361. fi
  362. CURR_MATRIX_COMMIT=$(get_completion_param "matrix commit")
  363. if [[ "$CURR_MATRIX_COMMIT" == "$MATRIX_COMMIT" ]]; then
  364. return
  365. fi
  366. systemctl stop turn
  367. systemctl stop matrix
  368. function_check set_repo_commit
  369. set_repo_commit /etc/matrix "matrix commit" "$MATRIX_COMMIT" $MATRIX_REPO
  370. cd /etc/matrix || exit 62476724
  371. pip install --upgrade --process-dependency-links .
  372. pip install --upgrade --force "pynacl>=1.2.1"
  373. sed -i 's/ssl.PROTOCOL_SSLv23/ssl.PROTOCOL_TLSv1/g' /usr/local/bin/register_new_matrix_user
  374. chown -R matrix:matrix /etc/matrix
  375. chown -R matrix:matrix $MATRIX_DATA_DIR
  376. pip install --upgrade --force "pynacl==0.3.0"
  377. pip install --upgrade --force "phonenumbers>=8.2.0"
  378. if [ -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam ]; then
  379. chmod 755 /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam
  380. fi
  381. systemctl start turn
  382. systemctl start matrix
  383. }
  384. function backup_local_matrix {
  385. source_directory=/etc/matrix
  386. if [ -d $source_directory ]; then
  387. systemctl stop turn
  388. systemctl stop matrix
  389. function_check backup_directory_to_usb
  390. backup_directory_to_usb $source_directory matrix
  391. source_directory=$MATRIX_DATA_DIR
  392. if [ -d $source_directory ]; then
  393. backup_directory_to_usb $source_directory matrixdata
  394. fi
  395. systemctl start turn
  396. systemctl start matrix
  397. fi
  398. }
  399. function restore_local_matrix {
  400. if [ -d /etc/matrix ]; then
  401. systemctl stop turn
  402. systemctl stop matrix
  403. temp_restore_dir=/root/tempmatrix
  404. function_check restore_directory_from_usb
  405. restore_directory_from_usb $temp_restore_dir matrix
  406. if [ -d $temp_restore_dir/etc/matrix ]; then
  407. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  408. else
  409. cp -r $temp_restore_dir/* /etc/matrix/
  410. fi
  411. # shellcheck disable=SC2181
  412. if [ ! "$?" = "0" ]; then
  413. function_check backup_unmount_drive
  414. backup_unmount_drive
  415. exit 3783
  416. fi
  417. rm -rf $temp_restore_dir
  418. chown -R matrix:matrix /etc/matrix
  419. temp_restore_dir=/root/tempmatrixdata
  420. restore_directory_from_usb $temp_restore_dir matrixdata
  421. if [ -d $temp_restore_dir$MATRIX_DATA_DIR ]; then
  422. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  423. else
  424. cp -r $temp_restore_dir/* $MATRIX_DATA_DIR/
  425. fi
  426. # shellcheck disable=SC2181
  427. if [ ! "$?" = "0" ]; then
  428. function_check backup_unmount_drive
  429. backup_unmount_drive
  430. exit 78352
  431. fi
  432. rm -rf $temp_restore_dir
  433. chown -R matrix:matrix $MATRIX_DATA_DIR
  434. if [[ "$ONION_ONLY" == "no" ]]; then
  435. if [ -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam ]; then
  436. chmod 755 /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam
  437. fi
  438. fi
  439. systemctl start turn
  440. systemctl start matrix
  441. fi
  442. }
  443. function backup_remote_matrix {
  444. source_directory=/etc/matrix
  445. if [ -d $source_directory ]; then
  446. systemctl stop turn
  447. systemctl stop matrix
  448. function_check backup_directory_to_friend
  449. backup_directory_to_friend $source_directory matrix
  450. source_directory=$MATRIX_DATA_DIR
  451. if [ -d $source_directory ]; then
  452. backup_directory_to_friend $source_directory matrixdata
  453. fi
  454. systemctl start turn
  455. systemctl start matrix
  456. fi
  457. }
  458. function restore_remote_matrix {
  459. if [ -d /etc/matrix ]; then
  460. systemctl stop turn
  461. systemctl stop matrix
  462. temp_restore_dir=/root/tempmatrix
  463. function_check restore_directory_from_friend
  464. restore_directory_from_friend $temp_restore_dir matrix
  465. if [ -d $temp_restore_dir/etc/matrix ]; then
  466. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  467. else
  468. cp -r $temp_restore_dir/* /etc/matrix/
  469. fi
  470. # shellcheck disable=SC2181
  471. if [ ! "$?" = "0" ]; then
  472. exit 38935
  473. fi
  474. rm -rf $temp_restore_dir
  475. chown -R matrix:matrix /etc/matrix
  476. temp_restore_dir=/root/tempmatrixdata
  477. restore_directory_from_friend $temp_restore_dir matrixdata
  478. if [ -d $temp_restore_dir$MATRIX_DATA_DIR ]; then
  479. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  480. else
  481. cp -r $temp_restore_dir/* $MATRIX_DATA_DIR/
  482. fi
  483. # shellcheck disable=SC2181
  484. if [ ! "$?" = "0" ]; then
  485. exit 60923
  486. fi
  487. rm -rf $temp_restore_dir
  488. chown -R matrix:matrix $MATRIX_DATA_DIR
  489. systemctl start turn
  490. systemctl start matrix
  491. fi
  492. }
  493. function remove_matrix {
  494. firewall_remove ${MATRIX_HTTP_PORT}
  495. nginx_dissite $MATRIX_DOMAIN_NAME
  496. remove_certs $MATRIX_DOMAIN_NAME
  497. if [ -f /etc/nginx/sites-available/$MATRIX_DOMAIN_NAME ]; then
  498. rm /etc/nginx/sites-available/$MATRIX_DOMAIN_NAME
  499. fi
  500. if [ ! -d /var/www/$MATRIX_DOMAIN_NAME ]; then
  501. rm -rf /var/www/$MATRIX_DOMAIN_NAME
  502. fi
  503. function_check remove_ddns_domain
  504. remove_ddns_domain $MATRIX_DOMAIN_NAME
  505. systemctl stop matrix
  506. function_check remove_turn
  507. remove_turn
  508. systemctl disable matrix
  509. if [ -f /etc/systemd/system/matrix.service ]; then
  510. rm /etc/systemd/system/matrix.service
  511. fi
  512. systemctl daemon-reload
  513. apt-get -y remove --purge coturn
  514. cd /etc/matrix || exit 26472462
  515. pip uninstall .
  516. rm -rf $MATRIX_DATA_DIR
  517. rm -rf /etc/matrix
  518. groupdel -f matrix
  519. userdel -r matrix
  520. remove_onion_service matrix ${MATRIX_ONION_PORT}
  521. remove_onion_service matrix ${MATRIX_FEDERATION_ONION_PORT}
  522. #sed -i "/# Matrix Server/,/# End of Matrix Server/d" /etc/nginx/sites-available/${MATRIX_DOMAIN_NAME}
  523. systemctl restart nginx
  524. remove_completion_param install_matrix
  525. sed -i '/matrix/d' "$COMPLETION_FILE"
  526. rm /etc/avahi/services/matrix.service
  527. systemctl restart avahi-daemon
  528. }
  529. function install_home_server {
  530. if [ ! -d /etc/matrix ]; then
  531. if [ -d /repos/matrix ]; then
  532. mkdir /etc/matrix
  533. cp -r -p /repos/matrix/. /etc/matrix
  534. cd /etc/matrix || exit 2324962946
  535. git pull
  536. else
  537. function_check git_clone
  538. git_clone $MATRIX_REPO /etc/matrix
  539. fi
  540. if [ ! -d /etc/matrix ]; then
  541. echo $'Unable to clone matrix repo'
  542. exit 6724683
  543. fi
  544. fi
  545. cd /etc/matrix || exit 7385452724
  546. git checkout $MATRIX_COMMIT -b $MATRIX_COMMIT
  547. set_completion_param "matrix commit" "$MATRIX_COMMIT"
  548. if [ ! -d "$INSTALL_DIR/matrix" ]; then
  549. mkdir -p "$INSTALL_DIR/matrix"
  550. fi
  551. rm -rf /usr/local/lib/python2.7/dist-packages/ldap*
  552. if [ -d "$INSTALL_DIR/matrix" ]; then
  553. rm -rf "$INSTALL_DIR/matrix/"*
  554. fi
  555. if ! pip install --upgrade --process-dependency-links . -b "$INSTALL_DIR/matrix"; then
  556. echo $'Failed to install matrix home server'
  557. exit 782542
  558. fi
  559. if [ ! -d $MATRIX_DATA_DIR ]; then
  560. mkdir $MATRIX_DATA_DIR
  561. fi
  562. groupadd matrix
  563. useradd -c "Matrix system account" -d $MATRIX_DATA_DIR -m -r -g matrix matrix
  564. usermod -a -G www-data matrix
  565. usermod -a -G ssl-cert matrix
  566. function_check install_turn
  567. install_turn
  568. MATRIX_SECRET="${turnkey}"
  569. function_check matrix_generate
  570. matrix_generate
  571. # Disable the web client
  572. sed -i 's|web_client:.*|web_client: False|g' $MATRIX_DATA_DIR/homeserver.yaml
  573. sed -i 's|, webclient||g' $MATRIX_DATA_DIR/homeserver.yaml
  574. sed -i "/- '0.0.0.0'/d" $MATRIX_DATA_DIR/homeserver.yaml
  575. chmod -R 700 $MATRIX_DATA_DIR/homeserver.yaml
  576. chown -R matrix:matrix /etc/matrix
  577. chown -R matrix:matrix $MATRIX_DATA_DIR
  578. sed -i 's/ssl.PROTOCOL_SSLv23/ssl.PROTOCOL_TLSv1/g' /usr/local/bin/register_new_matrix_user
  579. { echo '[Unit]';
  580. echo 'Description=Synapse Matrix homeserver';
  581. echo 'After=network.target nginx.target';
  582. echo '';
  583. echo '[Service]';
  584. echo 'Type=simple';
  585. echo 'User=matrix';
  586. echo "WorkingDirectory=/etc/matrix";
  587. echo "ExecStart=/usr/bin/python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml";
  588. echo 'Restart=on-failure';
  589. echo 'RestartSec=10';
  590. echo '';
  591. echo '[Install]';
  592. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/matrix.service
  593. systemctl enable matrix
  594. systemctl daemon-reload
  595. systemctl start matrix
  596. # wait for the database to be created
  597. sleep 5
  598. if [ ! -f $MATRIX_DATA_DIR/homeserver.db ]; then
  599. # On low power systems more sleeping may be needed
  600. sleep 10
  601. if [ ! -f $MATRIX_DATA_DIR/homeserver.db ]; then
  602. echo $'No matrix home server database was created'
  603. exit 23782
  604. fi
  605. fi
  606. chmod -R 700 $MATRIX_DATA_DIR/homeserver.db
  607. cd "$MATRIX_DATA_DIR" || exit 365856835
  608. sqlite3 homeserver.db "PRAGMA auto_vacuum = FULL;"
  609. #MATRIX_ONION_HOSTNAME=$(add_onion_service matrix ${MATRIX_PORT} ${MATRIX_ONION_PORT})
  610. add_onion_service matrix ${MATRIX_PORT} ${MATRIX_ONION_PORT}
  611. echo "HiddenServicePort ${MATRIX_HTTP_PORT} 127.0.0.1:${MATRIX_FEDERATION_ONION_PORT}" >> "$ONION_SERVICES_FILE"
  612. systemctl restart tor
  613. if [ ! "${MATRIX_PASSWORD}" ]; then
  614. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  615. MATRIX_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  616. else
  617. MATRIX_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  618. fi
  619. fi
  620. firewall_add matrix ${MATRIX_HTTP_PORT}
  621. rm -rf ${MATRIX_DATA_DIR}/Maildir
  622. rm -rf ${MATRIX_DATA_DIR}/.mutt
  623. rm -f ${MATRIX_DATA_DIR}/.muttrc
  624. rm -f ${MATRIX_DATA_DIR}/.mutt-alias
  625. rm -f ${MATRIX_DATA_DIR}/.procmailrc
  626. rm -f ${MATRIX_DATA_DIR}/.emacs-mutt
  627. }
  628. function install_matrix {
  629. if [[ $ONION_ONLY != 'no' ]]; then
  630. return
  631. fi
  632. check_ram_availability 1500
  633. if [ ! -d "$INSTALL_DIR" ]; then
  634. mkdir -p "$INSTALL_DIR"
  635. fi
  636. if [[ ${ONION_ONLY} == 'no' ]]; then
  637. if [ ! -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.pem ]; then
  638. if [ -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.crt ]; then
  639. rm /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.crt
  640. fi
  641. if [ -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam ]; then
  642. rm /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam
  643. fi
  644. echo $'Obtaining certificate for the matrix domain'
  645. function_check create_site_certificate
  646. create_site_certificate ${MATRIX_DOMAIN_NAME} 'yes'
  647. chmod 755 /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.dhparam
  648. if [ -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.crt ]; then
  649. echo $'Incorrect certificate generated'
  650. exit 73572
  651. fi
  652. fi
  653. chgrp -R ssl-cert /etc/letsencrypt
  654. chmod -R g=rX /etc/letsencrypt
  655. fi
  656. export DEBIAN_FRONTEND=noninteractive
  657. apt-get -yq install coreutils \
  658. curl file gcc git libevent-2.0-5 \
  659. libevent-dev libffi-dev libffi6 \
  660. libgnutls28-dev libjpeg62-turbo \
  661. libjpeg62-turbo-dev libldap-2.4-2 \
  662. libldap2-dev libsasl2-dev \
  663. libsqlite3-dev libssl-dev \
  664. libssl1.1 libtool libxml2 \
  665. libxml2-dev libxslt1-dev libxslt1.1 \
  666. make python python-dev \
  667. python-pip python-psycopg2 \
  668. python-virtualenv sqlite unzip \
  669. zlib1g zlib1g-dev
  670. pip install --upgrade pip
  671. pip install --upgrade python-ldap
  672. pip install --upgrade lxml
  673. pip install --upgrade --force "phonenumbers>=8.2.0"
  674. function_check matrix_nginx
  675. matrix_nginx
  676. function_check install_home_server
  677. install_home_server
  678. systemctl restart nginx
  679. systemctl restart turn
  680. systemctl restart matrix
  681. function_check update_default_domain
  682. update_default_domain
  683. # wait for nginx to start otherwise user add fails later
  684. sleep 5
  685. pip install --upgrade --force "pynacl==0.3.0"
  686. if [[ $(add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}" | tail -n 1) != "0" ]]; then
  687. echo $'Failed to add matrix admin user';
  688. exit 879352
  689. fi
  690. create_matrix_user_removal_script
  691. set_completion_param "matrix domain" "$MATRIX_DOMAIN_NAME"
  692. # Add avahi services
  693. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  694. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  695. echo '<service-group>';
  696. echo ' <name replace-wildcards="yes">%h MATRIX</name>';
  697. echo ' <service>';
  698. echo ' <type>_matrix._tcp</type>';
  699. echo " <port>$MATRIX_HTTP_PORT</port>";
  700. echo ' </service>';
  701. echo ' <service>';
  702. echo ' <type>_matrix._udp</type>';
  703. echo " <port>$MATRIX_HTTP_PORT</port>";
  704. echo ' </service>';
  705. echo '</service-group>'; } > /etc/avahi/services/matrix.service
  706. systemctl restart avahi-daemon
  707. APP_INSTALLED=1
  708. }