csd-wrapper.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. # Cisco Anyconnect CSD wrapper for OpenConnect
  3. #
  4. # [05 May 2015] Written by Nikolay Panin <nick_panin@mail.ru>:
  5. # - source: https://gist.github.com/l0ki000/56845c00fd2a0e76d688
  6. # [27 Oct 2017] Updated by Daniel Lenski <dlenski@gmail.com>:
  7. # - use -url argument
  8. # - kill cstub after timeout
  9. # - fix small typos:
  10. # [31 May 2018] Updated by Daniel Lenski <dlenski@gmail.com>:
  11. # - use curl with --pinnedpubkey to rely on sha256 hash of peer cert passed by openconnect
  12. TIMEOUT=30
  13. URL="https://${CSD_HOSTNAME}/CACHE"
  14. HOSTSCAN_DIR="$HOME/.cisco/hostscan"
  15. LIB_DIR="$HOSTSCAN_DIR/lib"
  16. BIN_DIR="$HOSTSCAN_DIR/bin"
  17. # cURL 7.39 (https://bugzilla.redhat.com/show_bug.cgi?id=1195771)
  18. # is required to support pin-based certificate validation. Must set this
  19. # to false if using an older version of cURL.
  20. INSECURE=false
  21. if [[ "$INSECURE" == "true" ]]; then
  22. echo "*********************************************************************" >&2
  23. echo "WARNING: running insecurely; will not validate CSD server certificate" >&2
  24. echo "*********************************************************************" >&2
  25. PINNEDPUBKEY="-k"
  26. else
  27. PINNEDPUBKEY="${CSD_SHA256:+"-k --pinnedpubkey sha256//$CSD_SHA256"}"
  28. fi
  29. BINS=("cscan" "cstub" "cnotify")
  30. # parsing command line
  31. shift
  32. URL=
  33. TICKET=
  34. STUB=
  35. GROUP=
  36. CERTHASH=
  37. LANGSELEN=
  38. while [ "$1" ]; do
  39. if [ "$1" == "-ticket" ]; then shift; TICKET=$1; fi
  40. if [ "$1" == "-stub" ]; then shift; STUB=$1; fi
  41. if [ "$1" == "-group" ]; then shift; GROUP=$1; fi
  42. if [ "$1" == "-certhash" ]; then shift; CERTHASH=$1; fi
  43. if [ "$1" == "-url" ]; then shift; URL=$(echo $1|tr -d '"'); fi # strip quotes
  44. if [ "$1" == "-langselen" ];then shift; LANGSELEN=$1; fi
  45. shift
  46. done
  47. ARCH=$(uname -m)
  48. if [[ "$ARCH" == "x86_64" ]]
  49. then
  50. ARCH="linux_x64"
  51. else
  52. ARCH="linux_i386"
  53. fi
  54. # creating dirs
  55. for dir in $HOSTSCAN_DIR $LIB_DIR $BIN_DIR ; do
  56. if [[ ! -f $dir ]]
  57. then
  58. mkdir -p $dir
  59. fi
  60. done
  61. # getting manifest, and checking binaries
  62. curl $PINNEDPUBKEY -s "${URL}/sdesktop/hostscan/$ARCH/manifest" -o "$HOSTSCAN_DIR/manifest"
  63. # generating md5.sum with full paths from manifest
  64. export HOSTSCAN_DIR=$HOSTSCAN_DIR
  65. while read HASHTYPE FILE EQU HASHVAL; do
  66. FILE="${FILE%*)}"
  67. FILE="${FILE#(}"
  68. if grep --extended-regexp --quiet --invert-match ".so|tables.dat" <<< "$FILE"; then
  69. PATHNAME="${BIN_DIR}/$FILE"
  70. IS_BIN=yes
  71. else
  72. PATHNAME="${LIB_DIR}/$FILE"
  73. IS_BIN=no
  74. fi
  75. DOWNLOAD=yes
  76. case $HASHTYPE in
  77. MD5)
  78. if [ -r "$PATHNAME" ] && md5sum --status -c <<< "$HASHVAL $PATHNAME"; then
  79. DOWNLOAD=no
  80. fi
  81. ;;
  82. SHA1)
  83. if [ -r "$PATHNAME" ] && sha1sum --status -c <<< "$HASHVAL $PATHNAME"; then
  84. DOWNLOAD=no
  85. fi
  86. ;;
  87. SHA256)
  88. if [ -r "$PATHNAME" ] && sha256sum --status -c <<< "$HASHVAL $PATHNAME"; then
  89. DOWNLOAD=no
  90. fi
  91. ;;
  92. *)
  93. echo "Unsupported hash type $HASHTYPE"
  94. ;;
  95. esac
  96. if [ "$DOWNLOAD" = "yes" ]; then
  97. echo "Downloading: $FILE"
  98. TMPFILE="${PATHNAME}.tmp"
  99. curl $PINNEDPUBKEY -s "${URL}/sdesktop/hostscan/$ARCH/$FILE" -o "${TMPFILE}"
  100. # some files are in gz (don't understand logic here)
  101. if [[ ! -f "${TMPFILE}" || ! -s "${TMPFILE}" ]]
  102. then
  103. # remove 0 size files
  104. if [[ ! -s ${TMPFILE} ]]; then
  105. rm ${TMPFILE}
  106. fi
  107. echo "Failure on $FILE, trying gz"
  108. FILE_GZ="${FILE}.gz"
  109. curl $PINNEDPUBKEY -s "${URL}/sdesktop/hostscan/$ARCH/$FILE_GZ" -o "${TMPFILE}.gz" &&
  110. gunzip --verbose --decompress "${TMPFILE}.gz"
  111. fi
  112. if [ -r "${TMPFILE}" ]; then
  113. if [ "$IS_BIN" = "yes" ]; then
  114. chmod +x "${TMPFILE}"
  115. fi
  116. mv "${TMPFILE}" "${PATHNAME}"
  117. fi
  118. fi
  119. done < $HOSTSCAN_DIR/manifest
  120. # cstub doesn't care about logging options, sic!
  121. #ARGS="-log debug -ticket $TICKET -stub $STUB -group $GROUP -host "$URL" -certhash $CERTHASH"
  122. ARGS="-log error -ticket $TICKET -stub $STUB -group $GROUP -host \"$URL\" -certhash $CERTHASH"
  123. echo "Launching: $BIN_DIR/cstub $ARGS"
  124. $BIN_DIR/cstub $ARGS & CSTUB_PID=$!
  125. sleep $TIMEOUT
  126. if kill -0 $CSTUB_PID 2> /dev/null; then
  127. echo "Killing cstub process after $TIMEOUT seconds"
  128. kill $CSTUB_PID 2> /dev/null || kill -9 $CSTUB_PID 2> /dev/null
  129. fi