#!/usr/bin/env bash

echo 
echo "*** Configure SIP endpoints provisioning ***"
echo

# disable pam_shells.so at /etc/pam.d/vsftpd
sed -i 's/^auth\s*required\s*pam_shells.so$/#auth       required     pam_shells.so/' /etc/pam.d/vsftpd

HOME=/home/PlcmSpIp
DIRS="CONTACTS LICENSE  LOGS  OVERRIDES"

# make PlcmSpIp
useradd PlcmSpIp
echo PlcmSpIp:PlcmSpIp | chpasswd
chsh -s /sbin/nologin PlcmSpIp

[ "`grep -c -e "^DenyUsers PlcmSpIp" /etc/ssh/sshd_config`" = "0" ] && echo -e "\n\n#block PlcmSpIp from ssh tunneling\nDenyUsers PlcmSpIp\n" >> /etc/ssh/sshd_config

#cp -f /var/www/html/index.php /home/PlcmSpIp/

chmod 751 $HOME
chown -R root:root /home/PlcmSpIp
cd $HOME
mkdir $DIRS
chown -R PlcmSpIp:PlcmSpIp $DIRS

# adjust TFTP config
mkdir -p /etc/xinetd.d/
cat > /etc/xinetd.d/tftp <<-EOF
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        disable = no
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -vvvvvvv -s $HOME
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
EOF

# adjust Thirdlane
cat > /etc/asterisk/provisioning.txt <<EOF
directory=$HOME
servicetype=2
EOF

chown asterisk:asterisk /etc/asterisk/provisioning.txt

# adjust VSFTPD
yum install vsftpd -y

cd /etc/vsftpd/
touch chroot_list
cat >vsftpd.patch <<-EOF
--- vsftpd.conf.orig    2023-05-10 13:26:54.774057140 +0300
+++ vsftpd.conf 2023-05-10 13:29:40.344274989 +0300
@@ -124,3 +124,22 @@
 
 pam_service_name=vsftpd
 userlist_enable=YES
+
+userlist_deny=YES
+user_config_dir=/etc/vsftpd/
+
+ssl_enable=YES
+allow_anon_ssl=NO
+force_local_data_ssl=NO
+force_local_logins_ssl=NO
+ssl_tlsv1=YES
+ssl_sslv2=NO
+ssl_sslv3=NO
+rsa_cert_file=/etc/vsftpd/vsftpd.pem
+
+#debug_ssl=YES
+dual_log_enable=YES
+#log_ftp_protocol=YES
+
+pasv_min_port=49152 
+pasv_max_port=65534
EOF
patch -p0 < vsftpd.patch

echo "dirlist_enable=NO" > PlcmSpIp
echo "deny_file={*.php,*.cgi,*.shtml,*.phtml,*.pl,*.js,*.htm,*.html,.bash*}" >> PlcmSpIp

cat >vsftpd.pem <<-EOF
-----BEGIN CERTIFICATE-----
MIIDYTCCAkkCFEm0pfrBhbJhk0Wtd+/2IPFXhtpzMA0GCSqGSIb3DQEBCwUAMG0x
CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEQMA4GA1UEBwwHRmFpcmZheDEWMBQG
A1UECgwNVGhpcmRsYW5lIExMQzELMAkGA1UECwwCSVQxGjAYBgNVBAMMEWdpdC50
aGlyZGxhbmUuY29tMB4XDTIzMDUxMDEyMzYwOVoXDTI3MDUwOTEyMzYwOVowbTEL
MAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRAwDgYDVQQHDAdGYWlyZmF4MRYwFAYD
VQQKDA1UaGlyZGxhbmUgTExDMQswCQYDVQQLDAJJVDEaMBgGA1UEAwwRZ2l0LnRo
aXJkbGFuZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDD1BqN
GSyRm+orsoY9WRwIXShUFI9FalniUWdlS921uKsVDteoaSEAkukP0hHbFw0KJgFt
GVX3ajbUmrnBWFYT3aOcQYz3LRO8d6hmfQU/G6HNdOvVwILKSj3S+pu4s2ydpIrt
pXcYDxZxG/obEUgvIHAd382i2QQpf6rPa+5ro2Wv0btO8gpzp61AvZ72F38xW2m4
ZnNu5axbnXQuW8DTtdKEhap/Rfdd1D2H/h+Jwya8xFiPOdLSy1+/kfLQK+VM4PQb
+ykp2cDr3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