nicer version uses screen on its own.
[feisty_meow.git] / scripts / security / start_tunnels.sh
1 #!/bin/bash
2 # this script makes a tunnel for SMTP traffic and others.  a remote ssh server
3 # is required.  this is especially useful for routing around firewalls using
4 # a web proxy like squid.  when used for SMTP, it ensures that none of the
5 # text is seen on whatever network one is on before it's sent from the remote
6 # server.
7
8 #hmmm:  none of the user info below will work for others: parameterize it.
9
10 #hmmm: maybe we need a base function that takes all the disparate values,
11 #      and this script could call it with known feisty meow settings.
12
13 ##############
14
15 # check for parameters on the command line.
16 launch_it="$1"; shift
17
18 ##############
19
20 LAUNCHING_TUNNEL=0
21 if [ "$launch_it" == "go" ]; then
22   LAUNCHING_TUNNEL=1
23 fi
24
25 ##############
26
27 # these variables are configurable from plug-ins.
28 #hmmm: what?
29
30 soundfile=$FEISTY_MEOW_DIR/database/sounds/woouoo.wav
31 if [ ! -z "$1" ]; then
32   soundfile=$1
33 fi
34
35 ##############
36
37 # provides a list of properly formatted tunnels for ssh to create.  if this list
38 # is empty, then we do nothing.
39 TUNNEL_LIST=()
40
41 # set this to the hostname that will be providing the tunnel.  this is
42 # usually a remote system.
43 TUNNEL_USER_PLUS_HOST=""
44
45 # set this to your key file, plus the -i flag, such as: 
46 #   SECURITY_KEY="-i $HOME/.ssh/id_rsa" 
47 TUNNEL_SECURITY_KEY=""
48
49 # this variable should be set to the name for the tunnel.  one can then
50 # open the tunnel screen with: screen -r -S "name"
51 TUNNEL_SCREEN_NAME="tunnely"
52
53 # a comment for when we make the connection
54 TUNNEL_COMMENT="Connecting tunnel to destination..."
55
56 ##############
57
58 #hmmm:move to fred configs!
59 TUNNEL_LIST+=(-L 14008:localhost:25)
60 TUNNEL_USER_PLUS_HOST="fred@serene.feistymeow.org"
61 TUNNEL_SECURITY_KEY="-i $HOME/.ssh/id_dsa_fred" 
62 TUNNEL_COMMENT="Connecting sendmail to serenely zooty."
63 TUNNEL_SCREEN_NAME="zooty"
64
65 ##############
66
67 # how often to play sounds when reconnecting.
68 NOISE_PERIOD=180
69
70 # when we last played a sound.
71 LAST_SOUND_TIME=0
72
73 play_sound_periodically()
74 {
75   CURRENT_TIME=$(date +"%s")
76   if (( $CURRENT_TIME - $LAST_SOUND_TIME >= $NOISE_PERIOD )); then
77     echo playing sound now.
78     bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh $soundfile &>/dev/null </dev/null &
79 #hmmm: parameterize this for the sound to be played.  doofus.
80     LAST_SOUND_TIME=$CURRENT_TIME
81   fi
82 }
83
84 ##############
85
86 function main_tunnely_loop()
87 {
88   while true; do
89     echo "$TUNNEL_COMMENT"
90     ssh -2 -N -v ${TUNNEL_LIST[*]} "$TUNNEL_SECURITY_KEY" "$TUNNEL_USER_PLUS_HOST"
91     echo "Got dumped from tunnels; re-establishing connection."
92     play_sound_periodically
93     echo "Note: if you're being asked for a password, you haven't set up an RSA key yet."
94     sleep 1
95   done
96 }
97
98 # notes...
99
100 #-L 8028:localhost:3128 
101
102 #-L 8043:localhost:443 
103
104 # ports sometimes used:
105 #     25 is the sendmail tunnel.
106 #   3128 is the squid proxy server.
107 #    443 is the https version of squid.
108
109 # ssh flags in use sometimes:
110 #   -f   go into the background once connected.
111 #   -2   enforce ssh version 2.
112 #   -N   don't execute any command; just forward data between the ports.
113 #   -L   (port:host:hostport) connect the local machine's "port" to the
114 #        remote port "hostport" on the "host" specified.  the local "port"
115 #        becomes an alias for the remote port.  note that the connection
116 #        being made to host and hostport is from the perspective of the ssh
117 #        server, not the local host.
118
119 if [ $LAUNCHING_TUNNEL -eq 1 ]; then
120   # this version is already ready to tunnel already, so let's tunnel.
121   main_tunnely_loop
122   # loop does not exit on its own.
123 else
124   # this version re-launches the script but tells it to start the tunnel.
125   screen -L -S "$TUNNEL_SCREEN_NAME" -d -m bash $0 go
126 fi
127
128