connection_tether-ppp.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. . $HOME/bin/common_values.sh
  21. # Do this once at boot time, but do it again after a tunnel abort request...
  22. #
  23. generate_ssh_targets
  24. ssh_fail_counter=0
  25. debug_print()
  26. {
  27. echo $@
  28. }
  29. # Every once in a while pppd will report connection, and still have a ppp session open to the modem, but
  30. # the router on the other end will have croaked, or the underlying GPRS connection will be too unreliable, or
  31. # sometimes even a pppd will hang such that it needs a SIGKILL to get rid of it... This function does that
  32. # and then cleans up after the dead pppd by removing its dropfiles and locks.
  33. #
  34. force_kill_pppd()
  35. {
  36. /usr/bin/killall pppd
  37. rm -rf /var/lock/*ttyGPRS
  38. rm -rf $GPRS_DROPFILE
  39. }
  40. # This function goes and looks at the version dropfiles for packages, gathering their package names
  41. # and versions to report when we check in with the server to log that we connected, and at what firmware and
  42. # config revision.
  43. #
  44. output_versions()
  45. {
  46. for file in `ls $CHECKSUM_AND_VERSION_PATH/*.version`; do
  47. echo -n " `echo $file | sed -r 's/^.*\/(.*)\.version$/\1/'`=`cat $file`";
  48. done
  49. }
  50. # This function extracts a field from the network ID dropfile by name (also used by the checkin process)
  51. #
  52. output_net_ids_field()
  53. {
  54. field="$1"
  55. cat $NETWORK_ID_DROPFILE | grep "$field" | cut -d'=' -f2 | xargs -n1 echo -n
  56. }
  57. # This function attempts to check in with the version server and report hardware and network serial numbers
  58. # so that us sysadmin types can see each time a unit attached to the network, which unit it was, and what software
  59. # it was running at the time.
  60. #
  61. perform_post_connect_checkin()
  62. {
  63. if [ -f $SERIAL_NUM_FILE ]; then
  64. busunitnum="`cat $SERIAL_NUM_FILE 2> /dev/null`"
  65. fi
  66. equipnum="`cat $EQUIP_NUM_FILE 2> /dev/null || echo 0`"
  67. version="`output_versions`"
  68. imei="`output_net_ids_field IMEI`"
  69. imsi="`output_net_ids_field IMSI`"
  70. mac="`output_net_ids_field ETH0`"
  71. # Send these gathered data to the update daemon. The leading '#' tells the server that this is a
  72. #checkin, not an update request.
  73. echo -e "#$busunitnum\t$equipnum\t$mac\t$imei\t$imsi\t$version" | nc localhost $UPDATE_DAEMON_PORT
  74. }
  75. # This function generates the server->client port forwards to allow a sysadmin to log into any unit that is on
  76. # the network by equipment number, serial number, or bus number. The three parameters are:
  77. #
  78. # 1: The path to the file containing the identifying number
  79. # 2: The base port number on the remote server to add the identifying number to to get the server-side port that will
  80. # forward to port 22 (sshd) on the client side.
  81. # 3: An optional parameter which if present is taken as a set of command line flags to cut to apply to the contents
  82. # of the file specified by $1 to extract the numeric component (for instance, serial numbers may be in the form XYZ-1234
  83. # in which case it's really the 1234 part we're after...
  84. #
  85. generate_reverse_phonehome_component()
  86. {
  87. file="$1"
  88. base="$2"
  89. cut_cmdline="$3"
  90. #Make sure the candidate dropfile exists
  91. if [ -f "$file" ]; then
  92. if [ -n "$cut_cmdline" ]; then
  93. #Grab the desired substring
  94. num="`cat $file | cut $cut_cmdline`"
  95. else
  96. #Grab its contents
  97. num="`cat $file`";
  98. fi
  99. #Make sure those contents are indeed numeric...
  100. if (echo "$num" | egrep -q '^[0-9]+$'); then
  101. #Make sure that number is within an acceptable range so as not to overflow
  102. if [ "$num" -gt "0" -a "$num" -le "$REVERSE_PHONE_HOME_MAX_TOKEN" ]; then
  103. echo -n " -R$((base + num)):localhost:22";
  104. fi
  105. fi
  106. fi
  107. }
  108. # This function calls the above component function for each identifying number we want to do a port forward based on...
  109. #
  110. generate_reverse_phonehome_string()
  111. {
  112. #If the reverse phone home feature is disabled, return without printing any commandline args
  113. if [ "$REVERSE_PHONE_HOME" -eq "0" ]; then return; fi
  114. generate_reverse_phonehome_component $EQUIP_NUM_FILE $REVERSE_PHONE_HOME_EQNUM_BASE
  115. generate_reverse_phonehome_component $SERIAL_NUM_FILE $REVERSE_PHONE_HOME_SERIALNUM_BASE "-d- -f3"
  116. }
  117. # This function performs teardown on a dead ssh connection, and increments the ssh connect failure counter. If
  118. # that counter has reached its maximum value, we force pppd down and try a clean redial. Otherwise, we just sleep and
  119. # try again.
  120. #
  121. clean_up_after_tunnel_teardown()
  122. {
  123. #If the tunnel was intentionally aborted for the purpose of switching servers
  124. if [ -f $TUNNEL_ABORT_DROPFILE ]; then
  125. debug_print "SSH tunnel aborted, removing dropfiles..."
  126. #Remove the dropfiles...
  127. /bin/rm -f $TUNNEL_DROPFILE $SSH_TUNNEL_PIDFILE
  128. #Generate new ssh target from server config dropfiles
  129. generate_ssh_targets
  130. /bin/rm -f $TUNNEL_ABORT_DROPFILE
  131. #Reset the failure counter
  132. ssh_fail_counter=0
  133. #Sleep until it is time to try again
  134. /bin/sleep $SLEEP_AFTER_TUNNEL_ABORT
  135. else
  136. #OTHERWISE, we assume that the modem lost signal, or the router or remote server went wonky...
  137. debug_print "SSH client dead, removing dropfiles..."
  138. #Remove the dropfiles...
  139. /bin/rm -f $TUNNEL_DROPFILE $SSH_TUNNEL_PIDFILE
  140. ssh_fail_counter=$((ssh_fail_counter + 1))
  141. if [ "$ssh_fail_counter" -ge "$MAX_FAIL_HANGUP" ]; then
  142. debug_print "pppd claims to be up; tunnel failed $ssh_fail_counter times, killing pppd to force redial."
  143. force_kill_pppd
  144. fi
  145. debug_print "Sleeping before any retry attempts..."
  146. #Sleep before trying this again...
  147. /bin/sleep $SLEEP_AFTER_TUNNEL_FAILURE
  148. fi
  149. }
  150. while true; do
  151. # If our GRPS connection has died...
  152. #
  153. if [ ! -f $GPRS_DROPFILE ]; then
  154. ssh_fail_counter=0
  155. debug_print "Attempting to dial..."
  156. # Try to re-"dial" our ISP
  157. #
  158. /usr/bin/pon gprs
  159. # Give the modem a minute to do its thing...
  160. #
  161. /usr/bin/pon gprs
  162. # Give the modem a minute to do its thing...
  163. #
  164. /bin/sleep $SLEEP_AFTER_DIAL
  165. fi
  166. # If we've just been asked to abort the SSH tunnel
  167. #
  168. if [ -f $TUNNEL_ABORT_DROPFILE ]; then
  169. # Generate new ssh target from server config dropfiles
  170. #
  171. generate_ssh_targets
  172. /bin/rm -f $TUNNEL_ABORT_DROPFILE
  173. # Reset the failure counter
  174. #
  175. ssh_fail_counter=0
  176. fi
  177. # If we now have an active GPRS network connection, try and bring a tunnel up...
  178. #
  179. if [ -f $GPRS_DROPFILE ]; then
  180. # If we have no active tunnel already...
  181. #
  182. if [ ! -f $TUNNEL_DROPFILE ]; then
  183. debug_print "Attempting to establish SSH tunnel... (Attempt number $((ssh_fail_counter + 1)))"
  184. # Attempt to create our tunnel... (incliding (if REVERSE_PHONE_HOME != 0) reverse phone home support
  185. #
  186. ssh $SSH_OPTIONS -i $SSH_IDENTITY $SSH_FORWARDS `generate_reverse_phonehome_string` -p $SSH_PORT $SSH_TARGET &
  187. # Remember its PID
  188. #
  189. echo "$!" > $SSH_TUNNEL_PIDFILE
  190. # Wait a few seconds to allow SSH negotiations
  191. #
  192. /bin/sleep $SLEEP_BEFORE_TUNNEL_TEST
  193. debug_print -n "Testing our new tunnel..."
  194. # Test to see if our tunnel is really up...
  195. #
  196. if [ "`nc localhost $HELLO_DAEMON_PORT < /dev/null`" = $HELLO_DAEMON_MESSAGE ]; then
  197. debug_print " It works."
  198. ssh_fail_counter=0
  199. debug_print "Checking in with server to report net IDs and package versions... "
  200. perform_post_connect_checkin
  201. debug_print "Touching dropfile and waiting for SSH to terminate..."
  202. # Touch our dropfile indicating the tunnel is up...
  203. #
  204. /bin/touch $TUNNEL_DROPFILE
  205. # and wait for the the SSH client process to end
  206. #
  207. wait `cat $SSH_TUNNEL_PIDFILE`
  208. # Clean Up...
  209. #
  210. clean_up_after_tunnel_teardown
  211. else
  212. debug_print " No luck..."
  213. debug_print "Issuing kill to SSH client...."
  214. # Kill the defunct and/or too slow to use SSH client
  215. #
  216. /bin/kill `cat $SSH_TUNNEL_PIDFILE`
  217. # Wait for the process to terminate
  218. #
  219. wait `cat $SSH_TUNNEL_PIDFILE`
  220. # Clean Up...
  221. #
  222. clean_up_after_tunnel_teardown
  223. fi
  224. else
  225. # This means we _think_ we have an SSH tunnel, but it's not one we set up...
  226. #
  227. debug_print -n "We seem to already have a pre-existing tunnel... Monitoring it."
  228. # Loop and periodically test this tunnel... When this condition fails, we're done...
  229. #
  230. while [ "`nc localhost $HELLO_DAEMON_PORT < /dev/null`" = $HELLO_DAEMON_MESSAGE ]; do
  231. # Sleep for a while before testing this tunnel again...
  232. #
  233. /bin/sleep $SLEEP_MONITORING_TUNNEL
  234. done
  235. # Clean Up...
  236. #
  237. clean_up_after_tunnel_teardown
  238. fi
  239. else
  240. # If we don't have an active GPRS session, that means we just failed at dialing
  241. #
  242. debug_print "Dialing failed... Sleeping"
  243. /bin/sleep $SLEEP_BETWEEN_REDIALS
  244. fi
  245. done