瀏覽代碼

Matrix identity server

Bob Mottram 8 年之前
父節點
當前提交
377684646b
共有 1 個文件被更改,包括 162 次插入35 次删除
  1. 162
    35
      src/freedombone-app-matrix

+ 162
- 35
src/freedombone-app-matrix 查看文件

@@ -37,8 +37,11 @@ SHOW_ON_ABOUT=1
37 37
 
38 38
 MATRIX_DATA_DIR='/var/lib/matrix'
39 39
 MATRIX_PORT=8448
40
+MATRID_ID_PORT=8090
40 41
 MATRIX_REPO="https://github.com/matrix-org/synapse"
41 42
 MATRIX_COMMIT='f5a4001bb116c468cc5e8e0ae04a1c570e2cb171'
43
+SYDENT_REPO="https://github.com/matrix-org/sydent"
44
+SYDENT_COMMIT='99edbd4c80c42b76e26f696054fcbbceecb25d5f'
42 45
 REPORT_STATS="no"
43 46
 MATRIX_SECRET=
44 47
 
@@ -48,6 +51,11 @@ matrix_variables=(ONION_ONLY
48 51
                   DEFAULT_DOMAIN_NAME)
49 52
 
50 53
 function matrix_nginx {
54
+    matrix_identityserver_proxy_str=' \
55
+    location /matrix-id { \
56
+        proxy_pass http://localhost:8090; \
57
+        proxy_buffering on; \
58
+    }'
51 59
     matrix_proxy_str=' \
52 60
     location /matrix { \
53 61
         proxy_pass https://localhost:8448; \
@@ -149,6 +157,10 @@ function matrix_nginx {
149 157
         nginx_ensite $DEFAULT_DOMAIN_NAME
150 158
     fi
151 159
 
160
+    if ! grep "localhost:${MATRIX_ID_PORT}" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
161
+        sed -i "s|:443 ssl;|:443 ssl;${matrix_identityserver_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
162
+        sed -i "s| default_server;| default_server;${matrix_identityserver_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
163
+    fi
152 164
     if ! grep "localhost:${MATRIX_PORT}" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
153 165
         sed -i "s|:443 ssl;|:443 ssl;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
154 166
         sed -i "s| default_server;| default_server;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
@@ -164,7 +176,7 @@ function matrix_nginx {
164 176
     sleep 5
165 177
 }
166 178
 
167
-function matrix_generate_synapse_file {
179
+function matrix_generate_homeserver_file {
168 180
     local filepath="${1}"
169 181
 
170 182
     cd /etc/matrix
@@ -175,6 +187,17 @@ function matrix_generate_synapse_file {
175 187
            --server-name ${DEFAULT_DOMAIN_NAME}
176 188
 }
177 189
 
190
+function matrix_generate_identityserver_file {
191
+    local filepath="${1}"
192
+
193
+    cd /etc/sydent
194
+    python -m sydent.sydent \
195
+           --config-path "${filepath}" \
196
+           --generate-config \
197
+           --report-stats ${REPORT_STATS} \
198
+           --server-name ${DEFAULT_DOMAIN_NAME}
199
+}
200
+
178 201
 function matrix_configure_homeserver_yaml {
179 202
     local turnkey="${1}"
180 203
     local filepath="${2}"
@@ -209,6 +232,16 @@ function matrix_configure_homeserver_yaml {
209 232
     sed -i "s|log_file:.*|log_file: \"/dev/null\"|g" "${filepath}"
210 233
 }
211 234
 
235
+function matrix_configure_identityserver_yaml {
236
+    local filepath="${1}"
237
+
238
+    sed -i "s|http.port:.*|http.port: $MATRIX_ID_PORT|g" "${filepath}"
239
+    sed -i "s|db.file:.*|db.file: \"${MATRIX_DATA_DIR}/identityserver.db\"|g" "${filepath}"
240
+    sed -i "s|Sydent Validation|Freedombone Matrix Account Validation|g" "${filepath}"
241
+    sed -i "s|pidfile.path:.*|pidfile.path: \"${MATRIX_DATA_DIR}/identityserver.pid\"|g" "${filepath}"
242
+    sed -i "s|log.path:.*|log.path: \"/dev/null\"|g" "${filepath}"
243
+}
244
+
212 245
 function matrix_diff {
213 246
     DIFFPARAMS="${DIFFPARAMS:-Naur}"
214 247
     DEFAULT_DOMAIN_NAME="${DEFAULT_DOMAIN_NAME:-demo_server_name}"
@@ -229,11 +262,29 @@ function matrix_generate {
229 262
     [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
230 263
         echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
231 264
 
232
-    if [ -f ${MATRIX_DATA_DIR}/homeserver.yaml ]; then
233
-        rm ${MATRIX_DATA_DIR}/homeserver.yaml
265
+    homeserver_config=${MATRIX_DATA_DIR}/homeserver.yaml
266
+    if [ -f $homeserver_config ]; then
267
+        rm $homeserver_config
268
+    fi
269
+    matrix_generate_homeserver_file $homeserver_config
270
+    matrix_configure_homeserver_yaml "${turnkey}" $homeserver_config
271
+}
272
+
273
+function identity_server_generate {
274
+    breakup="0"
275
+    [[ -z "${DEFAULT_DOMAIN_NAME}" ]] && echo "STOP! environment variable DEFAULT_DOMAIN_NAME must be set" && breakup="1"
276
+    [[ -z "${REPORT_STATS}" ]] && echo "STOP! environment variable REPORT_STATS must be set to 'no' or 'yes'" && breakup="1"
277
+    [[ "${breakup}" == "1" ]] && exit 1
278
+
279
+    [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
280
+        echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
281
+
282
+    identityserver_config=${MATRIX_DATA_DIR}/identityserver.yaml
283
+    if [ -f $identityserver_config ]; then
284
+        rm $identityserver_config
234 285
     fi
235
-    matrix_generate_synapse_file ${MATRIX_DATA_DIR}/homeserver.yaml
236
-    matrix_configure_homeserver_yaml "${turnkey}" ${MATRIX_DATA_DIR}/homeserver.yaml
286
+    matrix_generate_identityserver_file $identityserver_config
287
+    matrix_configure_identityserver_yaml $identityserver_config
237 288
 }
238 289
 
239 290
 function remove_user_matrix {
@@ -368,19 +419,27 @@ function restore_remote_matrix {
368 419
 function remove_matrix {
369 420
     firewall_remove ${MATRIX_PORT}
370 421
     systemctl stop matrix
422
+    systemctl stop sydent
371 423
 
372 424
     function_check remove_turn
373 425
     remove_turn
374 426
 
375 427
     systemctl disable matrix
428
+    systemctl disable sydent
376 429
     if [ -f /etc/systemd/system/matrix.service ]; then
377 430
         rm /etc/systemd/system/matrix.service
378 431
     fi
432
+    if [ -f /etc/systemd/system/sydent.service ]; then
433
+        rm /etc/systemd/system/sydent.service
434
+    fi
379 435
     apt-get -y remove --purge coturn
380 436
     cd /etc/matrix
381 437
     pip uninstall .
438
+    cd /etc/sydent
439
+    pip uninstall .
382 440
     rm -rf $MATRIX_DATA_DIR
383 441
     rm -rf /etc/matrix
442
+    rm -rf /etc/sydent
384 443
     deluser matrix
385 444
     delgroup matrix
386 445
     remove_onion_service matrix ${MATRIX_PORT}
@@ -392,37 +451,67 @@ function remove_matrix {
392 451
     sed -i '/matrix/d' $COMPLETION_FILE
393 452
 }
394 453
 
395
-function install_matrix {
396
-    if [ ! -d $INSTALL_DIR ]; then
397
-        mkdir -p $INSTALL_DIR
454
+function install_identity_server {
455
+    if [ ! -d /etc/sydent ]; then
456
+        function_check git_clone
457
+        git_clone $SYDENT_REPO /etc/sydent
458
+        if [ ! -d /etc/sydent ]; then
459
+            echo $'Unable to clone sydent repo'
460
+            exit 936525
461
+        fi
398 462
     fi
399 463
 
400
-    if [[ ${ONION_ONLY} == 'no' ]]; then
401
-        if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
402
-            echo $'Obtaining certificate for the main domain'
403
-            create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
404
-        fi
464
+    cd /etc/sydent
465
+    git checkout $SYDENT_COMMIT -b $SYDENT_COMMIT
466
+    set_completion_param "sydent commit" "$SYDENT_COMMIT"
467
+    if [ ! -d $INSTALL_DIR/sydent ]; then
468
+        mkdir -p $INSTALL_DIR/sydent
469
+    fi
470
+    pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/sydent
471
+    if [ ! "$?" = "0" ]; then
472
+        echo $'Failed to install matrix identity server'
473
+        exit 798362
405 474
     fi
406 475
 
407
-    export DEBIAN_FRONTEND=noninteractive
408
-    apt-get -yq install coreutils \
409
-            curl file gcc git libevent-2.0-5 \
410
-            libevent-dev libffi-dev libffi6 \
411
-            libgnutls28-dev libjpeg62-turbo \
412
-            libjpeg62-turbo-dev libldap-2.4-2 \
413
-            libldap2-dev libsasl2-dev \
414
-            libsqlite3-dev libssl-dev \
415
-            libssl1.0.0 libtool libxml2 \
416
-            libxml2-dev libxslt1-dev libxslt1.1 \
417
-            make python python-dev \
418
-            python-pip python-psycopg2 \
419
-            python-virtualenv sqlite unzip \
420
-            zlib1g zlib1g-dev
476
+    function_check identity_server_generate
477
+    identity_server_generate
421 478
 
422
-    pip install --upgrade pip
423
-    pip install --upgrade python-ldap
424
-    pip install --upgrade lxml
479
+    if [ ! -f $MATRIX_DATA_DIR/identityserver.yaml ]; then
480
+        echo $'Matrix identity server config was not generated'
481
+        exit 82352
482
+    fi
483
+
484
+    chmod -R 700 $MATRIX_DATA_DIR/identityserver.yaml
485
+    chown -R matrix:matrix /etc/sydent
486
+
487
+    echo '[Unit]' > /etc/systemd/system/sydent.service
488
+    echo 'Description=Sydent Matrix identity server' >> /etc/systemd/system/sydent.service
489
+    echo 'After=network.target nginx.target' >> /etc/systemd/system/sydent.service
490
+    echo '' >> /etc/systemd/system/sydent.service
491
+    echo '[Service]' >> /etc/systemd/system/sydent.service
492
+    echo 'Type=simple' >> /etc/systemd/system/sydent.service
493
+    echo 'User=matrix' >> /etc/systemd/system/sydent.service
494
+    echo "WorkingDirectory=/etc/sydent" >> /etc/systemd/system/sydent.service
495
+    echo "ExecStart=/usr/bin/python -m sydent.sydent --config-path ${MATRIX_DATA_DIR}/identityserver.yaml" >> /etc/systemd/system/sydent.service
496
+    echo 'Restart=always' >> /etc/systemd/system/sydent.service
497
+    echo 'RestartSec=10' >> /etc/systemd/system/sydent.service
498
+    echo '' >> /etc/systemd/system/sydent.service
499
+    echo '[Install]' >> /etc/systemd/system/sydent.service
500
+    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/sydent.service
501
+    systemctl enable sydent
502
+    systemctl daemon-reload
503
+    systemctl start sydent
504
+
505
+    sleep 4
506
+
507
+    if [ ! -f $MATRIX_DATA_DIR/identityserver.db ]; then
508
+        echo $'No matrix identity server database was created'
509
+        exit 7354383
510
+    fi
511
+    chmod -R 700 $MATRIX_DATA_DIR/identityserver.db
512
+}
425 513
 
514
+function install_home_server {
426 515
     if [ ! -d /etc/matrix ]; then
427 516
         function_check git_clone
428 517
         git_clone $MATRIX_REPO /etc/matrix
@@ -440,6 +529,7 @@ function install_matrix {
440 529
     fi
441 530
     pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/matrix
442 531
     if [ ! "$?" = "0" ]; then
532
+        echo $'Failed to install matrix home server'
443 533
         exit 782542
444 534
     fi
445 535
 
@@ -458,7 +548,7 @@ function install_matrix {
458 548
     groupadd matrix
459 549
     useradd -c "Matrix system account" -d $MATRIX_DATA_DIR -m -r -g matrix matrix
460 550
 
461
-    chmod -R 700 /var/lib/matrix/homeserver.yaml
551
+    chmod -R 700 $MATRIX_DATA_DIR/homeserver.yaml
462 552
     chown -R matrix:matrix /etc/matrix
463 553
     chown -R matrix:matrix $MATRIX_DATA_DIR
464 554
 
@@ -480,11 +570,12 @@ function install_matrix {
480 570
     systemctl daemon-reload
481 571
     systemctl start matrix
482 572
 
483
-    if [ -f /var/lib/matrix/homeserver.db ]; then
484
-        chmod -R 700 /var/lib/matrix/homeserver.db
485
-    fi
573
+    sleep 4
486 574
 
487
-    update_default_domain
575
+    if [ ! -f $MATRIX_DATA_DIR/homeserver.db ]; then
576
+        echo $'No matrix home server database was created'
577
+    fi
578
+    chmod -R 700 $MATRIX_DATA_DIR/homeserver.db
488 579
 
489 580
     firewall_add matrix ${MATRIX_PORT}
490 581
 
@@ -503,7 +594,43 @@ function install_matrix {
503 594
     rm -f ${MATRIX_DATA_DIR}/.mutt-alias
504 595
     rm -f ${MATRIX_DATA_DIR}/.procmailrc
505 596
     rm -f ${MATRIX_DATA_DIR}/.emacs-mutt
597
+}
598
+
599
+function install_matrix {
600
+    if [ ! -d $INSTALL_DIR ]; then
601
+        mkdir -p $INSTALL_DIR
602
+    fi
603
+
604
+    if [[ ${ONION_ONLY} == 'no' ]]; then
605
+        if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
606
+            echo $'Obtaining certificate for the main domain'
607
+            create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
608
+        fi
609
+    fi
506 610
 
611
+    export DEBIAN_FRONTEND=noninteractive
612
+    apt-get -yq install coreutils \
613
+            curl file gcc git libevent-2.0-5 \
614
+            libevent-dev libffi-dev libffi6 \
615
+            libgnutls28-dev libjpeg62-turbo \
616
+            libjpeg62-turbo-dev libldap-2.4-2 \
617
+            libldap2-dev libsasl2-dev \
618
+            libsqlite3-dev libssl-dev \
619
+            libssl1.0.0 libtool libxml2 \
620
+            libxml2-dev libxslt1-dev libxslt1.1 \
621
+            make python python-dev \
622
+            python-pip python-psycopg2 \
623
+            python-virtualenv sqlite unzip \
624
+            zlib1g zlib1g-dev
625
+
626
+    pip install --upgrade pip
627
+    pip install --upgrade python-ldap
628
+    pip install --upgrade lxml
629
+
630
+    install_home_server
631
+    install_identity_server
632
+
633
+    update_default_domain
507 634
     matrix_nginx
508 635
 
509 636
     if [[ $(add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}" | tail -n 1) != "0" ]]; then