Advertisement
adamchilcott

SSLConfiguratorForLinux.sh

Feb 7th, 2019
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.14 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. WORKING_DIRECTORY=$(pwd)
  4. SCREENCONNECT_DIRECTORY=${scdir:-/opt/screenconnect}
  5. HTTPLISTENER_DIRECTORY="$SCREENCONNECT_DIRECTORY/App_Runtime/etc/.mono/httplistener"
  6. SCREENCONNECT_KEY="ScreenConnectPrivateKey.key"
  7. SCREENCONNECT_CSR="ScreenConnectCertSignRequest.csr"
  8. SCREENCONNECT_CERT="ScreenConnectCertificate.cer"
  9.  
  10.  
  11. LOG_FILE="SSL_Configurator.log"
  12. printf "\n\n" >> "$LOG_FILE"
  13. date >> "$LOG_FILE"
  14.  
  15. {
  16.  
  17. set -e
  18.  
  19. while true; do
  20.  
  21.     echo Welcome to the ScreenConnect SSL Configurator for Linux.
  22.     echo
  23.     echo What do you want to do?
  24.     echo "1. Change working directory where certificate files are stored."
  25.     echo "   (currently: $WORKING_DIRECTORY )"
  26.     echo
  27.     echo "2. Change directory where ScreenConnect is currently installed."
  28.     echo "   (currently: $SCREENCONNECT_DIRECTORY )"
  29.     echo
  30.     echo "3. Create a private key and generate a certificate signing request (CSR) to send to your certificate authority (CA)."
  31.     echo
  32.     echo "4. Convert and rename your certificate and private key."
  33.     echo
  34.     echo "5. Install your certificate and private key."
  35.     echo
  36.     echo "6. Edit the web.config file."
  37.     echo
  38.     echo "7. Exit."
  39.     echo
  40.     printf "? "
  41.     read COMMAND
  42.  
  43.     case "$COMMAND" in
  44.        1)
  45.          echo "Current working directory: $WORKING_DIRECTORY"
  46.          echo "Enter new working directory:"
  47.          printf "?"
  48.          read WORKING_DIRECTORY
  49.          if [ ! -d "$WORKING_DIRECTORY" ];
  50.             then
  51.                echo "Directory $WORKING_DIRECTORY not found."
  52.                break;
  53.             fi       
  54.          echo "Changed working directory to $WORKING_DIRECTORY."
  55.          echo
  56.          sleep 2
  57.          ;;
  58.        2)
  59.          echo "Current ScreenConnect installation directory: $SCREENCONNECT_DIRECTORY"
  60.          echo "Enter new ScreenConnect directory."
  61.          printf '? '    
  62.          read SCREENCONNECT_DIRECTORY
  63.          if [ ! -d "$SCREENCONNECT_DIRECTORY" ];
  64.             then
  65.                echo "Directory $SCREENCONNECT_DIRECTORY not found."
  66.                break;
  67.             fi
  68.          echo "Changed ScreenConnect directory to $SCREENCONNECT_DIRECTORY."
  69.          echo
  70.          sleep 2
  71.          ;;
  72.        3)
  73.         echo "Creating a new private key and CSR"
  74.         echo
  75.         openssl genrsa -out "$WORKING_DIRECTORY"/"$SCREENCONNECT_KEY" 2048 2>&1 tee "$LOG_FILE"
  76.         echo
  77.          
  78.         echo "Now generating $SCREENCONNECT_CSR..."
  79.         openssl req -new -key "$WORKING_DIRECTORY"/"$SCREENCONNECT_KEY" -sha256 -out "$WORKING_DIRECTORY"/"$SCREENCONNECT_CSR"
  80.         if [ -f "$WORKING_DIRECTORY/$SCREENCONNECT_CSR" ];  
  81.            then
  82.               echo "Created $WORKING_DIRECTORY/$SCREENCONNECT_KEY and $WORKING_DIRECTORY/$SCREENCONNECT_CSR"
  83.               echo "Send $SCREENCONNECT_CSR to your certificate authority. When you receive your server certificate, name it $SCREENCONNECT_CERT and put it in your working directory ($WORKING_DIRECTORY)."
  84.               echo
  85.               sleep 2
  86.            else
  87.               printf "\nThere was a problem creating $WORKING_DIRECTORY/$SCREENCONNECT_CSR\n"
  88.            fi
  89.          ;;    
  90.        4)
  91.          echo "Searching for $SCREENCONNECT_CERT..."
  92.          if [ -f "$WORKING_DIRECTORY/$SCREENCONNECT_CERT" ];
  93.             then
  94.                echo "Making sure your certificate and private key match..."
  95.                
  96.                KEY_HASH="$( openssl rsa -noout -modulus -in "$WORKING_DIRECTORY/$SCREENCONNECT_KEY" | openssl md5 )"
  97.                CERT_HASH="$( openssl x509 -noout -modulus -in "$WORKING_DIRECTORY/$SCREENCONNECT_CERT" | openssl md5 )"
  98.                
  99.                if [ "$KEY_HASH" != "$CERT_HASH" ];
  100.                   then
  101.                     echo "Your certificate does not match your private key."
  102.                     break
  103.                   fi
  104.                echo "Converting $SCREENCONNECT_KEY to PVK file..."
  105.                openssl rsa -in "$WORKING_DIRECTORY"/"$SCREENCONNECT_KEY" -inform PEM -outform PVK -pvk-none -out "$WORKING_DIRECTORY/443.pvk"
  106.                
  107.                if [ ! -f "$WORKING_DIRECTORY/443.pvk" ];
  108.                   then
  109.                      echo "Could not create $WORKING_DIRECTORY/443.pvk"
  110.                      break
  111.                   fi
  112.                    
  113.          
  114.                echo "Copying and renaming $SCREENCONNECT_CERT to 443.cer..."
  115.                cp "$WORKING_DIRECTORY/$SCREENCONNECT_CERT" "$WORKING_DIRECTORY/443.cer"
  116.                
  117.                echo "Done. Returning to menu."
  118.                sleep 2
  119.                echo
  120.                
  121.             else
  122.                echo "Could not find $WORKING_DIRECTORY/$SCREENCONNECT_CERT. Remember to rename your server certificate to $SCREENCONNECT_CERT."
  123.                
  124.             fi
  125.          
  126.          ;;
  127.  
  128.        5)
  129.           echo "Moving 443.cer and 443.pvk to $SCREENCONNECT_DIRECTORY/httplistener..."
  130.           mkdir -p "$HTTPLISTENER_DIRECTORY"
  131.           mv -t "$HTTPLISTENER_DIRECTORY" "$WORKING_DIRECTORY"/"443.cer" "$WORKING_DIRECTORY"/"443.pvk"
  132.           echo "Done."
  133.           echo
  134.           sleep 2
  135.           ;;
  136.        
  137.        6)
  138.          echo "Creating a backup of the web.config file..."
  139.          cp "$SCREENCONNECT_DIRECTORY/web.config" "$WORKING_DIRECTORY/web.config.$(date +%s).backup"
  140.          echo "Setting WebServerListenUri to https://+:443/..."
  141.          sed -i -r 's|(WebServerListenUri.*value=)"[^"]*"|\1"https://+:443/"|' "$SCREENCONNECT_DIRECTORY/web.config"
  142.          echo "Completed."
  143.          echo "Restart the screenconnect services with one of the following commands:"
  144.          echo "sudo service screenconnect restart"
  145.          echo "OR"
  146.          echo "sudo /etc/init.d/screenconnect stop"
  147.          echo "sudo /etc/init.d/screenconnect start"
  148.          echo
  149.          sleep 2
  150.          ;;
  151.        7)
  152.          break
  153.          ;;
  154.        "")
  155.          ;;
  156.        *)
  157.          echo 'Invalid input'
  158.          exit 1
  159.           ;;
  160.     esac
  161.    
  162. done
  163. }  2>&1 | tee -a "$LOG_FILE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement