freedombone-app-matrix 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # matrix server
  12. #
  13. # https://raw.githubusercontent.com/silvio/docker-matrix
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2016 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. VARIANTS='full full-vim chat'
  33. IN_DEFAULT_INSTALL=0
  34. SHOW_ON_ABOUT=1
  35. MATRIX_DATA_DIR='/var/lib/matrix'
  36. MATRIX_PORT=8448
  37. MATRIX_ID_PORT=8081
  38. MATRIX_REPO="https://github.com/matrix-org/synapse"
  39. MATRIX_COMMIT='f5a4001bb116c468cc5e8e0ae04a1c570e2cb171'
  40. SYDENT_REPO="https://github.com/matrix-org/sydent"
  41. SYDENT_COMMIT='99edbd4c80c42b76e26f696054fcbbceecb25d5f'
  42. REPORT_STATS="no"
  43. MATRIX_SECRET=
  44. matrix_variables=(ONION_ONLY
  45. MY_USERNAME
  46. MATRIX_SECRET
  47. DEFAULT_DOMAIN_NAME)
  48. function matrix_nginx {
  49. matrix_identityserver_proxy_str=' \
  50. location /matrixid { \
  51. proxy_pass http://localhost:8081; \
  52. proxy_buffering on; \
  53. }'
  54. matrix_proxy_str=' \
  55. location /matrix { \
  56. proxy_pass https://localhost:8448; \
  57. proxy_buffering on; \
  58. }'
  59. turn_proxy_str=' \
  60. location /turn { \
  61. proxy_pass https://localhost:3478; \
  62. proxy_buffering on; \
  63. }'
  64. if [[ $ONION_ONLY != 'no' ]]; then
  65. matrix_proxy_str=' \
  66. location /matrix { \
  67. proxy_pass http://localhost:8448; \
  68. proxy_buffering on; \
  69. }'
  70. turn_proxy_str=' \
  71. location /turn { \
  72. proxy_pass http://localhost:3478; \
  73. proxy_buffering on; \
  74. }'
  75. fi
  76. if [ ! -f /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME} ]; then
  77. matrix_nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
  78. if [[ $ONION_ONLY == "no" ]]; then
  79. function_check nginx_http_redirect
  80. nginx_http_redirect $DEFAULT_DOMAIN_NAME
  81. echo 'server {' >> $matrix_nginx_site
  82. echo ' listen 443 ssl;' >> $matrix_nginx_site
  83. echo ' listen [::]:443 ssl;' >> $matrix_nginx_site
  84. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $matrix_nginx_site
  85. echo '' >> $matrix_nginx_site
  86. echo ' # Security' >> $matrix_nginx_site
  87. function_check nginx_ssl
  88. nginx_ssl $DEFAULT_DOMAIN_NAME
  89. function_check nginx_disable_sniffing
  90. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  91. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $matrix_nginx_site
  92. echo '' >> $matrix_nginx_site
  93. echo ' # Logs' >> $matrix_nginx_site
  94. echo ' access_log /dev/null;' >> $matrix_nginx_site
  95. echo ' error_log /dev/null;' >> $matrix_nginx_site
  96. echo '' >> $matrix_nginx_site
  97. echo ' # Root' >> $matrix_nginx_site
  98. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $matrix_nginx_site
  99. echo '' >> $matrix_nginx_site
  100. echo ' # Index' >> $matrix_nginx_site
  101. echo ' index index.html;' >> $matrix_nginx_site
  102. echo '' >> $matrix_nginx_site
  103. echo ' # Location' >> $matrix_nginx_site
  104. echo ' location / {' >> $matrix_nginx_site
  105. function_check nginx_limits
  106. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  107. echo ' }' >> $matrix_nginx_site
  108. echo '' >> $matrix_nginx_site
  109. echo ' # Restrict access that is unnecessary anyway' >> $matrix_nginx_site
  110. echo ' location ~ /\.(ht|git) {' >> $matrix_nginx_site
  111. echo ' deny all;' >> $matrix_nginx_site
  112. echo ' }' >> $matrix_nginx_site
  113. echo '}' >> $matrix_nginx_site
  114. else
  115. echo -n '' > $matrix_nginx_site
  116. fi
  117. echo 'server {' >> $matrix_nginx_site
  118. echo " listen 127.0.0.1:$MATRIX_PORT default_server;" >> $matrix_nginx_site
  119. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $matrix_nginx_site
  120. echo '' >> $matrix_nginx_site
  121. function_check nginx_disable_sniffing
  122. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  123. echo '' >> $matrix_nginx_site
  124. echo ' # Logs' >> $matrix_nginx_site
  125. echo ' access_log /dev/null;' >> $matrix_nginx_site
  126. echo ' error_log /dev/null;' >> $matrix_nginx_site
  127. echo '' >> $matrix_nginx_site
  128. echo ' # Root' >> $matrix_nginx_site
  129. echo " root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $matrix_nginx_site
  130. echo '' >> $matrix_nginx_site
  131. echo ' # Location' >> $matrix_nginx_site
  132. echo ' location / {' >> $matrix_nginx_site
  133. function_check nginx_limits
  134. nginx_limits $DEFAULT_DOMAIN_NAME '15m'
  135. echo ' }' >> $matrix_nginx_site
  136. echo '' >> $matrix_nginx_site
  137. echo ' # Restrict access that is unnecessary anyway' >> $matrix_nginx_site
  138. echo ' location ~ /\.(ht|git) {' >> $matrix_nginx_site
  139. echo ' deny all;' >> $matrix_nginx_site
  140. echo ' }' >> $matrix_nginx_site
  141. echo '}' >> $matrix_nginx_site
  142. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  143. function_check create_site_certificate
  144. create_site_certificate $DEFAULT_DOMAIN_NAME 'yes'
  145. fi
  146. nginx_ensite $DEFAULT_DOMAIN_NAME
  147. fi
  148. if ! grep "localhost:${MATRIX_ID_PORT}" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
  149. sed -i "s|:443 ssl;|:443 ssl;${matrix_identityserver_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  150. sed -i "s| default_server;| default_server;${matrix_identityserver_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  151. fi
  152. if ! grep "localhost:${MATRIX_PORT}" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
  153. sed -i "s|:443 ssl;|:443 ssl;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  154. sed -i "s| default_server;| default_server;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  155. fi
  156. if ! grep "localhost:${TURN_PORT}" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
  157. sed -i "s|:443 ssl;|:443 ssl;${turn_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  158. sed -i "s| default_server;| default_server;${turn_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  159. fi
  160. systemctl restart nginx
  161. # wait for nginx to start otherwise user add fails later
  162. sleep 10
  163. }
  164. function matrix_generate_homeserver_file {
  165. local filepath="${1}"
  166. cd /etc/matrix
  167. python -m synapse.app.homeserver \
  168. --config-path "${filepath}" \
  169. --generate-config \
  170. --report-stats ${REPORT_STATS} \
  171. --server-name ${DEFAULT_DOMAIN_NAME}
  172. }
  173. function matrix_generate_identityserver_file {
  174. local filepath="${1}"
  175. cd /etc/sydent
  176. python -m sydent.sydent \
  177. --config-path "${filepath}" \
  178. --generate-config \
  179. --report-stats ${REPORT_STATS} \
  180. --server-name ${DEFAULT_DOMAIN_NAME}
  181. }
  182. function matrix_configure_homeserver_yaml {
  183. local turnkey="${1}"
  184. local filepath="${2}"
  185. local ymltemp="$(mktemp)"
  186. awk -v TURNURIES="turn_uris: [\"turn:${DEFAULT_DOMAIN_NAME}/turn?transport=udp\", \"turn:${DEFAULT_DOMAIN_NAME}/turn?transport=tcp\"]" \
  187. -v TURNSHAREDSECRET="turn_shared_secret: \"${turnkey}\"" \
  188. -v PIDFILE="pid_file: ${MATRIX_DATA_DIR}/homeserver.pid" \
  189. -v DATABASE="database: \"${MATRIX_DATA_DIR}/homeserver.db\"" \
  190. -v LOGFILE="log_file: \"/dev/null\"" \
  191. -v MEDIASTORE="media_store_path: \"${MATRIX_DATA_DIR}/media_store\"" \
  192. '{
  193. sub(/turn_shared_secret: "YOUR_SHARED_SECRET"/, TURNSHAREDSECRET);
  194. sub(/turn_uris: \[\]/, TURNURIES);
  195. sub(/pid_file: \/homeserver.pid/, PIDFILE);
  196. sub(/database: "\/homeserver.db"/, DATABASE);
  197. sub(/log_file: "\/homeserver.log"/, LOGFILE);
  198. sub(/media_store_path: "\/media_store"/, MEDIASTORE);
  199. print;
  200. }' "${filepath}" > "${ymltemp}"
  201. mv ${ymltemp} "${filepath}"
  202. if [[ $ONION_ONLY != 'no' ]]; then
  203. sed -i 's|no_tls: .*|no_tls: True|g' "${filepath}"
  204. fi
  205. sed -i 's|enable_registration_captcha.*|enable_registration_captcha: False|g' "${filepath}"
  206. sed -i "s|database: \".*|database: \"${MATRIX_DATA_DIR}/homeserver.db\"|g" "${filepath}"
  207. sed -i "s|media_store_path:.*|media_store_path: \"${MATRIX_DATA_DIR}/media_store\"|g" "${filepath}"
  208. sed -i "s|pid_file:.*|pid_file: \"${MATRIX_DATA_DIR}/homeserver.pid\"|g" "${filepath}"
  209. sed -i "s|log_file:.*|log_file: \"/dev/null\"|g" "${filepath}"
  210. sed -i '0,/bind_address:.*/s//bind_address: 127.0.0.1/' "${filepath}"
  211. sed -i '0,/x_forwarded:.*/s//x_forwarded: true/' "${filepath}"
  212. }
  213. function matrix_configure_identityserver {
  214. local filepath=/etc/sydent/sydent.conf
  215. sed -i "s|http.port.*|http.port = $MATRIX_ID_PORT|g" ${filepath}
  216. sed -i "s|db.file.*|db.file = /etc/sydent/sydent.db|g" ${filepath}
  217. sed -i "s|Sydent Validation|Freedombone Matrix Account Validation|g" ${filepath}
  218. sed -i "s|pidfile.path.*|pidfile.path = /etc/sydent/sydent.pid|g" ${filepath}
  219. sed -i "s|log.path.*|log.path = /dev/null|g" ${filepath}
  220. sed -i "s|server.name.*|server.name = ${DEFAULT_DOMAIN_NAME}|g" ${filepath}
  221. }
  222. function matrix_diff {
  223. DIFFPARAMS="${DIFFPARAMS:-Naur}"
  224. DEFAULT_DOMAIN_NAME="${DEFAULT_DOMAIN_NAME:-demo_server_name}"
  225. REPORT_STATS="${REPORT_STATS:-no_or_yes}"
  226. export DEFAULT_DOMAIN_NAME REPORT_STATS
  227. matrix_generate_synapse_file $INSTALL_DIR/homeserver.synapse.yaml
  228. diff -${DIFFPARAMS} $INSTALL_DIR/homeserver.synapse.yaml ${MATRIX_DATA_DIR}/homeserver.yaml
  229. rm $INSTALL_DIR/homeserver.synapse.yaml
  230. }
  231. function matrix_generate {
  232. breakup="0"
  233. [[ -z "${DEFAULT_DOMAIN_NAME}" ]] && echo "STOP! environment variable DEFAULT_DOMAIN_NAME must be set" && breakup="1"
  234. [[ -z "${REPORT_STATS}" ]] && echo "STOP! environment variable REPORT_STATS must be set to 'no' or 'yes'" && breakup="1"
  235. [[ "${breakup}" == "1" ]] && exit 1
  236. [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
  237. echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
  238. homeserver_config=${MATRIX_DATA_DIR}/homeserver.yaml
  239. if [ -f $homeserver_config ]; then
  240. rm $homeserver_config
  241. fi
  242. matrix_generate_homeserver_file $homeserver_config
  243. matrix_configure_homeserver_yaml "${turnkey}" $homeserver_config
  244. }
  245. function remove_user_matrix {
  246. remove_username="$1"
  247. ${PROJECT_NAME}-pass -u $remove_username --rmapp matrix
  248. # TODO: There is no user removal script within synapse
  249. }
  250. function add_user_matrix {
  251. new_username="$1"
  252. new_user_password="$2"
  253. ${PROJECT_NAME}-pass -u $new_username -a matrix -p "$new_user_password"
  254. retval=$(register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml -u "${new_username}" -p "${new_user_password}" -a)
  255. echo "0"
  256. }
  257. function install_interactive_matrix {
  258. APP_INSTALLED=1
  259. }
  260. function change_password_matrix {
  261. curr_username="$1"
  262. new_user_password="$2"
  263. #${PROJECT_NAME}-pass -u "$curr_username" -a matrix -p "$new_user_password"
  264. }
  265. function reconfigure_matrix {
  266. echo -n ''
  267. }
  268. function upgrade_matrix {
  269. function_check set_repo_commit
  270. set_repo_commit /etc/matrix "matrix commit" "$MATRIX_COMMIT" $MATRIX_REPO
  271. cd /etc/matrix
  272. pip install --upgrade --process-dependency-links .
  273. set_repo_commit /etc/sydent "sydent commit" "$SYDENT_COMMIT" $SYDENT_REPO
  274. cd /etc/sydent
  275. pip install --upgrade --process-dependency-links .
  276. chown -R matrix:matrix /etc/matrix
  277. chown -R matrix:matrix /etc/sydent
  278. chown -R matrix:matrix $MATRIX_DATA_DIR
  279. }
  280. function backup_local_matrix {
  281. source_directory=/etc/matrix
  282. if [ -d $source_directory ]; then
  283. systemctl stop matrix
  284. function_check backup_directory_to_usb
  285. backup_directory_to_usb $source_directory matrix
  286. source_directory=$MATRIX_DATA_DIR
  287. if [ -d $source_directory ]; then
  288. backup_directory_to_usb $source_directory matrixdata
  289. fi
  290. systemctl start matrix
  291. fi
  292. }
  293. function restore_local_matrix {
  294. if [ -d /etc/matrix ]; then
  295. systemctl stop matrix
  296. temp_restore_dir=/root/tempmatrix
  297. function_check restore_directory_from_usb
  298. restore_directory_from_usb $temp_restore_dir matrix
  299. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  300. if [ ! "$?" = "0" ]; then
  301. function_check backup_unmount_drive
  302. backup_unmount_drive
  303. exit 3783
  304. fi
  305. rm -rf $temp_restore_dir
  306. chown -R matrix:matrix /etc/matrix
  307. temp_restore_dir=/root/tempmatrixdata
  308. restore_directory_from_usb $temp_restore_dir matrixdata
  309. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  310. if [ ! "$?" = "0" ]; then
  311. function_check backup_unmount_drive
  312. backup_unmount_drive
  313. exit 78352
  314. fi
  315. rm -rf $temp_restore_dir
  316. chown -R matrix:matrix $MATRIX_DATA_DIR
  317. systemctl start matrix
  318. fi
  319. }
  320. function backup_remote_matrix {
  321. source_directory=/etc/matrix
  322. if [ -d $source_directory ]; then
  323. systemctl stop matrix
  324. function_check backup_directory_to_friend
  325. backup_directory_to_friend $source_directory matrix
  326. source_directory=$MATRIX_DATA_DIR
  327. if [ -d $source_directory ]; then
  328. backup_directory_to_friend $source_directory matrixdata
  329. fi
  330. systemctl start matrix
  331. fi
  332. }
  333. function restore_remote_matrix {
  334. if [ -d /etc/matrix ]; then
  335. systemctl stop matrix
  336. temp_restore_dir=/root/tempmatrix
  337. function_check restore_directory_from_friend
  338. restore_directory_from_friend $temp_restore_dir matrix
  339. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  340. if [ ! "$?" = "0" ]; then
  341. exit 38935
  342. fi
  343. rm -rf $temp_restore_dir
  344. chown -R matrix:matrix /etc/matrix
  345. temp_restore_dir=/root/tempmatrixdata
  346. restore_directory_from_friend $temp_restore_dir matrixdata
  347. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  348. if [ ! "$?" = "0" ]; then
  349. exit 60923
  350. fi
  351. rm -rf $temp_restore_dir
  352. chown -R matrix:matrix $MATRIX_DATA_DIR
  353. systemctl start matrix
  354. fi
  355. }
  356. function remove_matrix {
  357. firewall_remove ${MATRIX_PORT}
  358. systemctl stop matrix
  359. systemctl stop sydent
  360. function_check remove_turn
  361. remove_turn
  362. systemctl disable matrix
  363. systemctl disable sydent
  364. if [ -f /etc/systemd/system/matrix.service ]; then
  365. rm /etc/systemd/system/matrix.service
  366. fi
  367. if [ -f /etc/systemd/system/sydent.service ]; then
  368. rm /etc/systemd/system/sydent.service
  369. fi
  370. apt-get -y remove --purge coturn
  371. cd /etc/matrix
  372. pip uninstall .
  373. cd /etc/sydent
  374. pip uninstall .
  375. rm -rf $MATRIX_DATA_DIR
  376. rm -rf /etc/matrix
  377. rm -rf /etc/sydent
  378. deluser matrix
  379. delgroup matrix
  380. remove_onion_service matrix ${MATRIX_PORT}
  381. sed -i "/location \/matrix {/,/}/d" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  382. sed -i "/location \/matrixid {/,/}/d" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  383. sed -i "/location \/turn {/,/}/d" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  384. systemctl restart nginx
  385. remove_completion_param install_matrix
  386. sed -i '/matrix/d' $COMPLETION_FILE
  387. }
  388. function install_identity_server {
  389. if [ ! -d /etc/sydent ]; then
  390. function_check git_clone
  391. git_clone $SYDENT_REPO /etc/sydent
  392. if [ ! -d /etc/sydent ]; then
  393. echo $'Unable to clone sydent repo'
  394. exit 936525
  395. fi
  396. fi
  397. cd /etc/sydent
  398. git checkout $SYDENT_COMMIT -b $SYDENT_COMMIT
  399. set_completion_param "sydent commit" "$SYDENT_COMMIT"
  400. if [ ! -d $INSTALL_DIR/sydent ]; then
  401. mkdir -p $INSTALL_DIR/sydent
  402. fi
  403. pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/sydent
  404. if [ ! "$?" = "0" ]; then
  405. echo $'Failed to install matrix identity server'
  406. exit 798362
  407. fi
  408. function_check matrix_generate_identityserver_file
  409. matrix_generate_identityserver_file /etc/sydent/sydent.conf
  410. if [ ! -f /etc/sydent/sydent.conf ]; then
  411. echo $'Matrix identity server configuration not generated'
  412. exit 72528
  413. fi
  414. function_check matrix_configure_identityserver
  415. matrix_configure_identityserver
  416. if [ ! -f /etc/sydent/sydent.conf ]; then
  417. echo $'Matrix identity server config was not generated'
  418. exit 82352
  419. fi
  420. chmod -R 700 /etc/sydent/sydent.conf
  421. chown -R matrix:matrix /etc/sydent
  422. echo '[Unit]' > /etc/systemd/system/sydent.service
  423. echo 'Description=Sydent Matrix identity server' >> /etc/systemd/system/sydent.service
  424. echo 'After=network.target nginx.target' >> /etc/systemd/system/sydent.service
  425. echo '' >> /etc/systemd/system/sydent.service
  426. echo '[Service]' >> /etc/systemd/system/sydent.service
  427. echo 'Type=simple' >> /etc/systemd/system/sydent.service
  428. echo 'User=matrix' >> /etc/systemd/system/sydent.service
  429. echo "WorkingDirectory=/etc/sydent" >> /etc/systemd/system/sydent.service
  430. echo "ExecStart=/usr/bin/python -m sydent.sydent --config-path /etc/sydent/sydent.conf" >> /etc/systemd/system/sydent.service
  431. echo 'Restart=always' >> /etc/systemd/system/sydent.service
  432. echo 'RestartSec=10' >> /etc/systemd/system/sydent.service
  433. echo '' >> /etc/systemd/system/sydent.service
  434. echo '[Install]' >> /etc/systemd/system/sydent.service
  435. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/sydent.service
  436. systemctl enable sydent
  437. systemctl daemon-reload
  438. systemctl start sydent
  439. if [ ! -f /etc/sydent/sydent.db ]; then
  440. echo $'No matrix identity server database was created'
  441. exit 7354383
  442. fi
  443. chmod -R 700 /etc/sydent/sydent.db
  444. }
  445. function install_home_server {
  446. if [ ! -d /etc/matrix ]; then
  447. function_check git_clone
  448. git_clone $MATRIX_REPO /etc/matrix
  449. if [ ! -d /etc/matrix ]; then
  450. echo $'Unable to clone matrix repo'
  451. exit 6724683
  452. fi
  453. fi
  454. cd /etc/matrix
  455. git checkout $MATRIX_COMMIT -b $MATRIX_COMMIT
  456. set_completion_param "matrix commit" "$MATRIX_COMMIT"
  457. if [ ! -d $INSTALL_DIR/matrix ]; then
  458. mkdir -p $INSTALL_DIR/matrix
  459. fi
  460. pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/matrix
  461. if [ ! "$?" = "0" ]; then
  462. echo $'Failed to install matrix home server'
  463. exit 782542
  464. fi
  465. if [ ! -d $MATRIX_DATA_DIR ]; then
  466. mkdir $MATRIX_DATA_DIR
  467. fi
  468. groupadd matrix
  469. useradd -c "Matrix system account" -d $MATRIX_DATA_DIR -m -r -g matrix matrix
  470. function_check install_turn
  471. install_turn
  472. MATRIX_SECRET="${turnkey}"
  473. function_check matrix_generate
  474. matrix_generate
  475. if [[ -z ${MATRIX_DATA_DIR}/homeserver.yaml ]]; then
  476. echo $'homeserver.yaml is zero size'
  477. exit 783724
  478. fi
  479. chmod -R 700 $MATRIX_DATA_DIR/homeserver.yaml
  480. chown -R matrix:matrix /etc/matrix
  481. chown -R matrix:matrix $MATRIX_DATA_DIR
  482. echo '[Unit]' > /etc/systemd/system/matrix.service
  483. echo 'Description=Synapse Matrix homeserver' >> /etc/systemd/system/matrix.service
  484. echo 'After=network.target nginx.target' >> /etc/systemd/system/matrix.service
  485. echo '' >> /etc/systemd/system/matrix.service
  486. echo '[Service]' >> /etc/systemd/system/matrix.service
  487. echo 'Type=simple' >> /etc/systemd/system/matrix.service
  488. echo 'User=matrix' >> /etc/systemd/system/matrix.service
  489. echo "WorkingDirectory=/etc/matrix" >> /etc/systemd/system/matrix.service
  490. echo "ExecStart=/usr/bin/python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml" >> /etc/systemd/system/matrix.service
  491. echo 'Restart=always' >> /etc/systemd/system/matrix.service
  492. echo 'RestartSec=10' >> /etc/systemd/system/matrix.service
  493. echo '' >> /etc/systemd/system/matrix.service
  494. echo '[Install]' >> /etc/systemd/system/matrix.service
  495. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/matrix.service
  496. systemctl enable matrix
  497. systemctl daemon-reload
  498. systemctl start matrix
  499. if [ ! -f $MATRIX_DATA_DIR/homeserver.db ]; then
  500. echo $'No matrix home server database was created'
  501. fi
  502. chmod -R 700 $MATRIX_DATA_DIR/homeserver.db
  503. firewall_add matrix ${MATRIX_PORT}
  504. MATRIX_ONION_HOSTNAME=$(add_onion_service matrix ${MATRIX_PORT} ${MATRIX_PORT})
  505. if [ ! ${MATRIX_PASSWORD} ]; then
  506. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  507. MATRIX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  508. else
  509. MATRIX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  510. fi
  511. fi
  512. rm -rf ${MATRIX_DATA_DIR}/Maildir
  513. rm -rf ${MATRIX_DATA_DIR}/.mutt
  514. rm -f ${MATRIX_DATA_DIR}/.muttrc
  515. rm -f ${MATRIX_DATA_DIR}/.mutt-alias
  516. rm -f ${MATRIX_DATA_DIR}/.procmailrc
  517. rm -f ${MATRIX_DATA_DIR}/.emacs-mutt
  518. }
  519. function install_matrix {
  520. if [ ! -d $INSTALL_DIR ]; then
  521. mkdir -p $INSTALL_DIR
  522. fi
  523. if [[ ${ONION_ONLY} == 'no' ]]; then
  524. if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
  525. echo $'Obtaining certificate for the main domain'
  526. create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
  527. fi
  528. fi
  529. export DEBIAN_FRONTEND=noninteractive
  530. apt-get -yq install coreutils \
  531. curl file gcc git libevent-2.0-5 \
  532. libevent-dev libffi-dev libffi6 \
  533. libgnutls28-dev libjpeg62-turbo \
  534. libjpeg62-turbo-dev libldap-2.4-2 \
  535. libldap2-dev libsasl2-dev \
  536. libsqlite3-dev libssl-dev \
  537. libssl1.0.0 libtool libxml2 \
  538. libxml2-dev libxslt1-dev libxslt1.1 \
  539. make python python-dev \
  540. python-pip python-psycopg2 \
  541. python-virtualenv sqlite unzip \
  542. zlib1g zlib1g-dev
  543. pip install --upgrade pip
  544. pip install --upgrade python-ldap
  545. pip install --upgrade lxml
  546. pip install --upgrade --force "pynacl==0.3.0"
  547. function_check install_home_server
  548. install_home_server
  549. function_check install_identity_server
  550. install_identity_server
  551. function_check update_default_domain
  552. update_default_domain
  553. function_check matrix_nginx
  554. matrix_nginx
  555. if [[ $(add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}" | tail -n 1) != "0" ]]; then
  556. echo $'Failed to add matrix admin user';
  557. exit 879352
  558. fi
  559. APP_INSTALLED=1
  560. }