Files

79 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
DOMAIN="s21.lisglosips.test"
U1="s21-reg-1001"
U2="s21-reg-1002"
IP1="s21-ip-1001"
IP2="s21-ip-1002"
P1=$(openssl rand -base64 18 | tr -d '=+/' | cut -c1-20)
P2=$(openssl rand -base64 18 | tr -d '=+/' | cut -c1-20)
HA1_1=$(printf "%s:%s:%s" "$U1" "$DOMAIN" "$P1" | md5sum | cut -d " " -f1)
HA1_2=$(printf "%s:%s:%s" "$U2" "$DOMAIN" "$P2" | md5sum | cut -d " " -f1)
unset P1 P2
install -d -m 0755 /opt/lisglosips-s21
install -m 0755 /tmp/lisglosips-s21-sip.py /opt/lisglosips-s21/lisglosips-s21-sip.py
for f in /tmp/s21-*; do
install -m 0755 "$f" "/opt/lisglosips-s21/$(basename "$f")"
done
install -d -m 0750 -o root -g hector /etc/lisglosips-s21
cat > /etc/lisglosips-s21/sip-accounts.env <<EOF
S21_SIP_DOMAIN=$DOMAIN
S21_SIP_PRIMARY_USER=$U1
S21_SIP_SECONDARY_USER=$U2
S21_SIP_PRIMARY_HA1=$HA1_1
S21_SIP_SECONDARY_HA1=$HA1_2
S21_IP_PRIMARY_ID=$IP1
S21_IP_SECONDARY_ID=$IP2
EOF
chown root:hector /etc/lisglosips-s21/sip-accounts.env
chmod 0640 /etc/lisglosips-s21/sip-accounts.env
python3 -m py_compile /opt/lisglosips-s21/lisglosips-s21-sip.py
python3 - <<'PY'
from pathlib import Path
p = Path("/etc/opensips/opensips.cfg")
s = p.read_text()
s = s.replace('modparam("auth_db", "calculate_ha1", 1)', 'modparam("auth_db", "calculate_ha1", 0)')
s = s.replace('modparam("mi_fifo", "fifo_mode", 0666)', 'modparam("mi_fifo", "fifo_mode", 0660)')
p.write_text(s)
PY
find /etc/opensips/tls -type f \( -name "*key*.pem" -o -name "cakey.pem" -o -name "*-privkey.pem" \) -exec chmod 0600 {} + 2>/dev/null || true
mysql --protocol=socket -uroot <<SQL
USE opensips;
INSERT INTO domain (domain, attrs, accept_subdomain, last_modified)
VALUES ('$DOMAIN', 'S21 SIP registration test domain', 0, NOW())
ON DUPLICATE KEY UPDATE attrs=VALUES(attrs), accept_subdomain=VALUES(accept_subdomain), last_modified=NOW();
DELETE FROM subscriber WHERE username IN ('$U1','$U2') AND domain='$DOMAIN';
INSERT INTO subscriber (username, domain, password, ha1, ha1_sha256, ha1_sha512t256)
VALUES
('$U1', '$DOMAIN', '', '$HA1_1', '', ''),
('$U2', '$DOMAIN', '', '$HA1_2', '', '');
DELETE FROM address WHERE context_info IN ('$IP1','$IP2');
INSERT INTO address (grp, ip, mask, port, proto, pattern, context_info)
VALUES
(21, '100.93.185.30', 32, 0, 'any', NULL, '$IP1'),
(22, '100.93.185.30', 32, 0, 'any', NULL, '$IP2');
SQL
opensips -C -f /etc/opensips/opensips.cfg >/run/lisglosips_s21_opensips_check.log 2>&1
systemctl restart opensips
sleep 1
systemctl is-active --quiet opensips
mysql --protocol=socket -uroot -N <<SQL
USE opensips;
SELECT 'domain', domain FROM domain WHERE domain='$DOMAIN';
SELECT 'subscriber', username, domain, LENGTH(password), LENGTH(ha1) FROM subscriber WHERE domain='$DOMAIN' ORDER BY username;
SELECT 'address', grp, ip, mask, proto, context_info FROM address WHERE context_info IN ('$IP1','$IP2') ORDER BY context_info;
SQL