common_values.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #THIS FILE IS NOT REALLY A SCRIPT, BUT RATHER A CENTRAL HOLDING TANK FOR SYSTEM CONFIG VALUES...
  21. #IT SHOULD BE SOURCED BY ALL OTHER SCRIPTS THAT NEED A CONTAINED VALUE, AND MAY SOME DAY BE PARSED
  22. #BY THE NATIVE APPLICATIONS. THUS: Keep it simple, in the following format:
  23. #
  24. ##Comment on meaning of variable
  25. #VARIABLE="value"
  26. #
  27. #(variable name in all caps, no space, equals sign, quoted literal value (if there is even a remote chance that the value
  28. #is one that will be needed by the native app in the future)
  29. #
  30. BASEDIR='/home/bus'
  31. #How long to sleep after a dial attempt before checking if we have a PPP session successfully constructed
  32. SLEEP_AFTER_DIAL="30"
  33. #How long to wait between redial attempts
  34. SLEEP_BETWEEN_REDIALS="60"
  35. # How long to sleep after the GRPS session comes up before testing the SSH tunnel (attempting to connect to the 'hello' server)
  36. #
  37. SLEEP_BEFORE_TUNNEL_TEST="5"
  38. # How long to sleep after an established tunnel goes down before allowing any SSH tunnel establishment retries
  39. #
  40. SLEEP_AFTER_TUNNEL_FAILURE="60"
  41. # How long to sleep after an established tunnel has been aborted
  42. #
  43. SLEEP_AFTER_TUNNEL_ABORT="60"
  44. # How many successive ssh tunnel failures do we accept before forcing a hard termination of pppd and a modem reset and redail
  45. #
  46. MAX_FAIL_HANGUP="5"
  47. # How frequently to test an SSH tunnel that predates the ppp-dialer.sh process (thus having no right to wait for its PID)
  48. #
  49. SLEEP_MONITORING_TUNNEL="120"
  50. # How frequently to perform SSH keepalive messages, and how many of them are allowed to drop before we tear the tunnel down
  51. # and wait to try again. This gets passed to the SSH client in the following form:
  52. # -o ServerAliveInterval=$SSH_SERVER_ALIVE_INTERVAL -o ServerAliveCountMax=$SSH_SERVER_ALIVE_MAXDROP
  53. #
  54. SSH_SERVER_ALIVE_INTERVAL="15"
  55. SSH_SERVER_ALIVE_MAXDROP="3"
  56. # These are the default server parameters the system will use if there is not a configured sync server.
  57. #The system will check
  58. SSH_DEFAULT_TARGET="bus@example.com"
  59. SSH_DEFAULT_PORT="6055"
  60. SSH_DEFAULT_IDENTITY="$BASEDIR/.ssh/id_rsa_bus"
  61. #This regular expression test is used to make sure we have a valid-looking FQDN or IP address
  62. SSH_TARGET_VALIDITY_CHECK="^[a-zA-Z][a-zA-Z0-9]*@([a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$"
  63. SSH_TARGET="$SSH_DEFAULT_TARGET"
  64. SSH_PORT="$SSH_DEFAULT_PORT"
  65. SSH_IDENTITY="$SSH_DEFAULT_IDENTITY"
  66. #--------------------------------------------------------------------------------
  67. #This is the list of forwarded ports we bring live when we set up our SSH tunnel:
  68. # 2857 -> AVLS
  69. AVLS_DAEMON_PORT="2857"
  70. # 7277 -> Bus Pass Daemon
  71. PASSDB_DAEMON_PORT="7277"
  72. # 2455 -> Billing Log Daemon
  73. BILLDB_DAEMON_PORT="2455"
  74. # 3556 -> "Hello" Daemon
  75. HELLO_DAEMON_PORT="3556"
  76. # 8377 -> Update / Version Server
  77. UPDATE_DAEMON_PORT="8377"
  78. # These are used to talk to the "Hello" daemon on the server to verify that
  79. #actual data can flow before notifying the system that the tunnel is up.
  80. HELLO_DAEMON_MESSAGE="Hello."
  81. #--------------------------------------------------------------------------------
  82. # This is a list of base ports on the server that we can use for the reverse-phone-home
  83. #functionality which forwards port BASE + client_id on the server to port 22 (sshd) on
  84. #the client to allow for remote troubleshooting and manual configuration.
  85. #Enable reverse-phone-home feature
  86. REVERSE_PHONE_HOME="1"
  87. # This is the base port (on the server) which the numbering for the reverse-phone-home
  88. #system starts at when numbered by equipment number:
  89. REVERSE_PHONE_HOME_EQNUM_BASE="10000"
  90. # This is the base port (on the server) which the numbering for the reverse-phone-home
  91. #system starts at when numbered by serial number (wireless_ip on transition busses):
  92. REVERSE_PHONE_HOME_SERIALNUM_BASE="30000"
  93. # This is the number above which an equipment number or other token will not be considered
  94. #as a phone home candidate on account of its likelyhood to overflow into the next port range.
  95. REVERSE_PHONE_HOME_MAX_TOKEN="19999"
  96. #--------------------------------------------------------------------------------
  97. #These paths lead to dropfiles in /tmp which are used to signal network state
  98. #This dropfile means we have an established GPRS/PPP session
  99. GPRS_DROPFILE="/tmp/network-is-up"
  100. #This dropfile means we have an established SSH tunnel to the server
  101. TUNNEL_DROPFILE="/tmp/tunnel-is-up"
  102. #This dropfile contains the process ID of our SSH client that's keeping the tunnel open
  103. SSH_TUNNEL_PIDFILE="/tmp/ssh_tunnel.pid"
  104. #This dropfile contains identifying information about our ethernet card, cell modem, and SIM
  105. NETWORK_ID_DROPFILE="/tmp/net_ids"
  106. #This dropfile becomes present to notify the update process to abort:
  107. UPDATE_ABORT_DROPFILE="/tmp/abort_update"
  108. TUNNEL_ABORT_DROPFILE="/tmp/abort_tunnel"
  109. #############################################################
  110. FIX_PACKAGE_PERMS="1"
  111. ########################################################## Used only if FIX_PACKAGE_PERMS is nonzero ##############
  112. #This specifies who should own installed packages
  113. PACKAGE_OWNER_STRING="root:root"
  114. #These are the permissions applied to stuff in the bin directory
  115. PACKAGE_BIN_PERMISSIONS="755"
  116. #This is the egrep expression that must match to qualify something as a binary
  117. PACKAGE_BIN_PATTERN='(^[/]?'$BASEDIR'/bin/[a-z_A-Z0-9.]+$)|(.*\.sh$)'
  118. #These are the permissions applied to SSH related config files and directories
  119. PACKAGE_SSH_STRICT_FILE_PERMISSIONS="600"
  120. PACKAGE_SSH_MILD_FILE_PERMISSIONS="644"
  121. PACKAGE_SSH_DIR_PERMISSIONS="700"
  122. #this one is only used in its ALWAYS case
  123. PACKAGE_SYS_DIR_PERMISSIONS="755"
  124. #These are the egrep expressions that must match to qualify something as an SSH file or directory
  125. PACKAGE_SSH_STRICT_FILE_PATTERN="(^[/]?etc/ppp/id_rsa[^\.]*$)|(^[/]?root/\.ssh/id_rsa[^\.]*$)|(^[/]?root/\.ssh/authorized_keys)"
  126. PACKAGE_SSH_MILD_FILE_PATTERN="(^[/]?root/\.ssh/known_hosts)|(^[/]?etc/ppp/id_rsa[^\.]*.pub$)|(^[/]?root/\.ssh/id_rsa[^\.]*.pub)"
  127. PACKAGE_SSH_DIR_PATTERN="(^[/]?root[/]?$)|(^[/]?root/\.ssh[/]?$)"
  128. #These are the list of files to ALWAYS reset permissions on (whether or not they are in the update)
  129. ALWAYS_SSH_DIR_LIST="/root /root/.ssh"
  130. ALWAYS_SSH_STRICT_FILE_LIST="/root/.ssh/id_rsa /etc/ppp/id_rsa_bus /root/.ssh/authorized_keys $SYNC_PRIVATE_KEY"
  131. ALWAYS_SSH_MILD_FILE_LIST="/root/.ssh/known_hosts /root/.ssh/id_rsa.pub /etc/ppp/id_rsa_bus.pub /etc/ssh/sshd_config"
  132. ALWAYS_BIN_LIST="$BASEDIR/bin/init_bus.sh $BASEDIR/bin/update_loop.sh $BASEDIR/bin/apply_update.sh $BASEDIR/bin/fix_pkg_perm.sh $BASEDIR/bin/get_net_ids.sh"
  133. ALWAYS_SYS_DIR_LIST="/etc /etc/ssh /etc/ppp"
  134. DEFAULT_EXTRACT_PATH="/"
  135. EXTRACT_PATH_FILE="/tmp/pkg_extract_path"
  136. ##################################################################################################################
  137. #These paths point to important bits of information for synchronizing
  138. EQUIP_NUM_FILE="$BASEDIR/config/equipnum.txt"
  139. SERIAL_NUM_FILE="$BASEDIR/config/serial_num"
  140. SERVER_CONFIG_DIR="$BASEDIR/config/server"
  141. SERVER_LIST_FILE="$SERVER_CONFIG_DIR/server_list"
  142. SYNC_DESC_FILE="$SERVER_CONFIG_DIR/sync_server_desc"
  143. SYNC_TARGET_FILE="$SERVER_CONFIG_DIR/sync_target"
  144. SYNC_PORT_FILE="$SERVER_CONFIG_DIR/sync_port"
  145. SYNC_KNOWN_HOSTS="/root/.ssh/known_hosts"
  146. SYNC_PRIVATE_KEY="/etc/ppp/id_rsa_client"
  147. ###############################################################################################
  148. # CONFIGURATION VALUES USED PRIMARILY BY THE UPDATE LOOP SCRIPT #
  149. ###############################################################################################
  150. #How long for update_loop.sh to sleep after spawning the client supervisor before (potentially) respawning it
  151. SLEEP_AFTER_SPAWN="60"
  152. #How long to sleep between attempts to download patches from the server
  153. SLEEP_BETWEEN_UPDATES="60"
  154. #The directory in which the current patch state is to be found
  155. CHECKSUM_AND_VERSION_PATH="$BASEDIR/config/"
  156. ###############################################################################################
  157. # BELOW HERE, COMPOUND OPTIONS ARE ALLOWED (things that use variable substitution, etc...)#
  158. ###############################################################################################
  159. #This looks for all environment variables that specify a port that we must forward...
  160. TUNNEL_PORT_LIST="`set | egrep "^[A-Z]+_DAEMON_PORT=" | cut -d '=' -f2 | egrep '^[0-9]+$' | xargs echo`"
  161. #This gathers the above list into something we can just hand to the SSH client...
  162. SSH_FORWARDS="`for port in $TUNNEL_PORT_LIST; do echo -n " -L$port:localhost:$port"; done`"
  163. #These are the flags to SSH that are specific to setting up the tunnel
  164. SSH_TUNNEL_FLAGS="-n -N -x"
  165. #These are the flags to SSH that are specific to our connection to the target host
  166. SSH_TARGET_OPTIONS="-o StrictHostKeyChecking=no"
  167. #These are the flags to SSH that specify the behavior of the protocol-level keepalive messages
  168. SSH_KEEPALIVE_OPTIONS="-o ServerAliveInterval=$SSH_SERVER_ALIVE_INTERVAL -o ServerAliveCountMax=$SSH_SERVER_ALIVE_MAXDROP"
  169. SSH_OPTIONS="$SSH_TUNNEL_FLAGS $SSH_KEEPALIVE_OPTIONS $SSH_TARGET_OPTIONS"
  170. SCP_OPTIONS="$SSH_KEEPALIVE_OPTIONS $SSH_TARGET_OPTIONS"
  171. # This function kills any currently open SSH or SCP sessions and puts dropfiles in /tmp telling
  172. #ppp-dialer.sh and update_loop.sh to refresh their Sync server parameters with the generate_ssh_targets
  173. #function before attempting to reconnect.
  174. #
  175. abort_tunnel_and_update() {
  176. touch $UPDATE_ABORT_DROPFILE
  177. touch $TUNNEL_ABORT_DROPFILE
  178. sync
  179. sleep 1
  180. killall ssh
  181. killall scp
  182. }
  183. # This function uses the dropfiles left by unpack_server_data.sh and constructs new SSH and SCP
  184. #parameters suitable to connect to the specified server. If there is no server specified, or if the
  185. #specified server does not pass a sanity check then the default server will be used.
  186. #
  187. function generate_ssh_targets
  188. {
  189. if [ -f $SYNC_TARGET_FILE ]; then
  190. alt_target="`cat $SYNC_TARGET_FILE`"
  191. if echo "$alt_target" | egrep -q "$SSH_TARGET_VALIDITY_CHECK"; then
  192. SSH_TARGET="`cat $SYNC_TARGET_FILE`"
  193. SSH_PORT="`cat $SYNC_PORT_FILE`"
  194. SSH_IDENTITY="$SYNC_PRIVATE_KEY"
  195. echo "$0: Using sync server $SSH_TARGET port $SSH_PORT"
  196. else
  197. SSH_TARGET="$SSH_DEFAULT_SYNC_TARGET"
  198. SSH_PORT="$SSH_DEFAULT_SYNC_PORT"
  199. SSH_IDENTITY="$SSH_DEFAULT_IDENTITY"
  200. echo "$0: Specified sync server name $alt_target is malformed. Using default $SSH_DEFAULT_SYNC_TARGET port $SSH_DEFAULT_SYNC_PORT"
  201. fi
  202. else
  203. SSH_TARGET="$SSH_DEFAULT_TARGET"
  204. SSH_PORT="$SSH_DEFAULT_PORT"
  205. SSH_IDENTITY="$SSH_DEFAULT_IDENTITY"
  206. echo "$0: No sync server selected. Using default $SSH_DEFAULT_TARGET port $SSH_DEFAULT_PORT"
  207. fi
  208. }