unpack_server_data.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. export BASEDIR="/home/bus"
  21. . $BASEDIR/bin/common_values.sh
  22. usage() {
  23. echo -e "Usage:" 1>&2
  24. echo -e "\t$0 line_number" 1>&2
  25. exit 1
  26. }
  27. # If the supplied line number is not numeric, complain!
  28. #
  29. if echo "$1" | egrep -v '^[0-9]+$' > /dev/null; then usage; fi
  30. # Create a temporary file to hold the selected config line for disection
  31. #
  32. tmpfile=`mktemp /tmp/unpack_server_tmp.XXXXXX`
  33. # Disect the server list and get the first N lines, and then the last one line of that set will be line N
  34. #
  35. head -n "$1" "$SERVER_LIST_FILE" | tail -n 1 > $tmpfile
  36. # Make sure that we have the expected six fields in this line
  37. #
  38. if [ `cat $tmpfile | tr '\t' '\n' | wc -l` -ne 6 ]; then
  39. echo "Line $1 does not contain the required 6 tab-delimited fields. Not altering server config." 1>&2;
  40. rm -f $tmpfile;
  41. usage;
  42. fi
  43. # Extract the target username...
  44. #
  45. ssh_target_user="`cat $tmpfile | cut -f2`"
  46. # Extract the target hostname...
  47. #
  48. ssh_target_host="`cat $tmpfile | cut -f3`"
  49. # Now, take the username and hostname and make a ssh_target
  50. #
  51. ssh_target="$ssh_target_user@$ssh_target_host"
  52. # If they pass the "smell test", we will proceed, otherwise we bail...
  53. #
  54. if echo "$ssh_target" | egrep -qv "$SSH_TARGET_VALIDITY_CHECK"; then
  55. echo "Target specified on line $1 appears to be invalid \"$ssh_target\". Not altering server config." 1>&2
  56. rm -f $tmpfile
  57. usage
  58. fi
  59. # If we've gotten this far, we are going to go through with it...
  60. #
  61. # Extract and save our description...
  62. #
  63. cat $tmpfile | cut -f1 > $SYNC_DESC_FILE
  64. # Save our new sync target "user@host"...
  65. #
  66. echo "$ssh_target" > $SYNC_TARGET_FILE
  67. # Extract and save our new port number...
  68. #
  69. cat $tmpfile | cut -f4 > $SYNC_PORT_FILE
  70. # Here we need to first put in our target hostname, followed by a space (but no newline)
  71. #
  72. echo -n "$ssh_target_host " > $SYNC_KNOWN_HOSTS
  73. # then append the 5th field (target host's public key)
  74. #
  75. cat $tmpfile | cut -f5 >> $SYNC_KNOWN_HOSTS
  76. # and set the correct permissions
  77. #
  78. chmod $PACKAGE_SSH_MILD_FILE_PERMISSIONS $SYNC_KNOWN_HOSTS
  79. # Here we need to put the correct preface/header on the private key
  80. echo "-----BEGIN RSA PRIVATE KEY-----" > $SYNC_PRIVATE_KEY
  81. # next we need to take the 6th field, and reconstitute all the spaces into newlines
  82. #
  83. cat $tmpfile | cut -f6 | tr ' ' '\n' >> $SYNC_PRIVATE_KEY
  84. # add on the terminating line
  85. echo "-----END RSA PRIVATE KEY-----" >> $SYNC_PRIVATE_KEY
  86. # and set the correct permissions
  87. #
  88. chmod $PACKAGE_SSH_STRICT_FILE_PERMISSIONS $SYNC_PRIVATE_KEY
  89. # remove our temporary file
  90. #
  91. rm -f $tmpfile
  92. # sync the secondary storage
  93. #
  94. sync
  95. # and signal that we want to close any open SSH sessions and start a new one with the new server
  96. #
  97. abort_tunnel_and_update