This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
startStunnel.sh
144 lines (120 loc) · 4.28 KB
/
startStunnel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
. /etc/include.properties
. /etc/device.properties
. /usr/bin/stunnelCertUtil.sh
source /etc/log_timestamp.sh
if [ -f /lib/rdk/t2Shared_api.sh ]; then
source /lib/rdk/t2Shared_api.sh
fi
export TERM=xterm
export HOME=/home/root
LOG_FILE="$LOG_PATH/stunnel.log"
usage()
{
echo_t "STUNNEL USAGE: startSTunnel.sh <localport> <jumpfqdn> <umpserver> <jumpserverport> <reverseSSHArgs>"
}
if [ $# -lt 5 ]; then
usage
exit 1
fi
# don't do anything if the feature is disabled.
if [ -f "/lib/rdk/shortsDownload.sh" ]; then
isShortsDLEnabled=`syscfg get ShortsDL`
if [ "x$isShortsDLEnabled" != "xtrue" ]; then
echo_t "ShortsDL RFC disabled, SHORTS feature not available" >> $LOG_FILE
exit 0
fi
fi
if [ $DEVICE_TYPE == "broadband" ]; then
DEVICE_CERT_PATH=/nvram/certs
elif [ $DEVICE_TYPE == "mediaclient" -o $DEVICE_TYPE == "hybrid" ]; then
DEVICE_CERT_PATH=/opt/certs
else
echo_t "STUNNEL: $DEVICE_CERT_PATH, not expected"
DEVICE_CERT_PATH=/tmp/certs
fi
#collect the arguments
# 1) CPE's available port starting from 3000
# 2) FQDN of jump server
# 3) Port number of stunnel's server instance at jump server
LOCAL_PORT=$1
JUMP_FQDN=$2
JUMP_SERVER=$3
JUMP_PORT=$4
REVERSESSHARGS=$5
#RDM parameters
DNLD_SCRIPT=/lib/rdk/shortsDownload.sh
SOCAT_PATH=/tmp/socat_dnld/usr/bin/socat
STUNNEL_PATH=/tmp/stunnel_dnld/usr/bin/stunnel
STUNNEL_PID_FILE=/tmp/stunnel_$LOCAL_PORT.pid
REVSSH_PID_FILE=/var/tmp/rssh.pid
STUNNEL_CONF_FILE=/tmp/stunnel_$LOCAL_PORT.conf
echo "pid = $STUNNEL_PID_FILE" > $STUNNEL_CONF_FILE
echo "output=$LOG_FILE" >> $STUNNEL_CONF_FILE
echo "debug = 7" >> $STUNNEL_CONF_FILE
echo "[ssh]" >> $STUNNEL_CONF_FILE
echo "client = yes" >> $STUNNEL_CONF_FILE
# Use localhost to listen on both IPv4 and IPv6
echo "accept = localhost:$LOCAL_PORT" >> $STUNNEL_CONF_FILE
echo "connect = $JUMP_SERVER:$JUMP_PORT" >> $STUNNEL_CONF_FILE
extract_stunnel_client_cert
if [ ! -f $CERT_FILE -o ! -f $CA_FILE ]; then
echo_t "STUNNEL: Required cert/CA file not found. Exiting..." >> $LOG_FILE
t2CountNotify "SHORTS_STUNNEL_CERT_FAILURE"
exit 1
fi
# this might change once we get proper certificates
echo "cert = $CERT_FILE" >> $STUNNEL_CONF_FILE
echo "CAfile = $CA_FILE" >> $STUNNEL_CONF_FILE
echo "verifyChain = yes" >> $STUNNEL_CONF_FILE
echo "checkHost = $JUMP_FQDN" >> $STUNNEL_CONF_FILE
if [ -f "/usr/bin/stunnel" ]; then
/usr/bin/stunnel $STUNNEL_CONF_FILE
elif [ -f $STUNNEL_PATH ]; then
$STUNNEL_PATH $STUNNEL_CONF_FILE
else
# Ideally shorts(socat and stunnel) packages should download at bootup time.
# In case if some problem in downloading, shorts pacakages shell download here
echo_t "stunnel/socat not found, need to download" >> $LOG_FILE
sh $DNLD_SCRIPT
DNLD_RES=$?
if [ $DNLD_RES -eq 0 ]; then
$STUNNEL_PATH $STUNNEL_CONF_FILE
else
echo_t "RDM not able to download shorts packages" >> $LOG_FILE
rm -f $STUNNEL_CONF_FILE
rm -f $D_FILE
exit 1
fi
fi
# cleanup sensitive files early
rm -f $STUNNEL_CONF_FILE
rm -f $D_FILE
REVSSHPID1=`cat $REVSSH_PID_FILE`
STUNNELPID=`cat $STUNNEL_PID_FILE`
if [ -z "$STUNNELPID" ]; then
rm -f $STUNNEL_PID_FILE
echo_t "STUNNEL: stunnel-client failed to establish. Exiting..." >> $LOG_FILE
t2CountNotify "SHORTS_STUNNEL_CLIENT_FAILURE"
exit
fi
#Starting startTunnel
/bin/sh /lib/rdk/startTunnel.sh start $REVERSESSHARGS
REVSSHPID2=`cat $REVSSH_PID_FILE`
#Terminate stunnel if revssh fails.
if [ -z "$REVSSHPID2" ] || [ "$REVSSHPID1" == "$REVSSHPID2" ]; then
kill -9 $STUNNELPID
rm -f $STUNNEL_PID_FILE
echo_t "STUNNEL: Reverse SSH failed to connect. Exiting..." >> $LOG_FILE
t2CountNotify "SHORTS_SSH_CLIENT_FAILURE"
exit
fi
echo_t "STUNNEL: Reverse SSH pid = $REVSSHPID2, Stunnel pid = $STUNNELPID" >> $LOG_FILE
t2CountNotify "SHORTS_CONN_SUCCESS"
#watch for termination of ssh-client to terminate stunnel
while test -d "/proc/$REVSSHPID2"; do
sleep 5
done
echo_t "STUNNEL: Reverse SSH session ended. Exiting..." >> $LOG_FILE
kill -9 $STUNNELPID
rm -f $STUNNEL_PID_FILE