Skip to content

Instantly share code, notes, and snippets.

@Kashkovsky
Created August 6, 2018 17:56
Show Gist options
  • Save Kashkovsky/87040b80ff6ec73a1f3d87123e990eb9 to your computer and use it in GitHub Desktop.
Save Kashkovsky/87040b80ff6ec73a1f3d87123e990eb9 to your computer and use it in GitHub Desktop.
Mount SMB share and import certificate if it is newer than the installed one
#!/bin/bash
# Define keychain
kc="/users/$USER/Library/Keychains/login.keychain"
# Open SMB share
open smb://$USER@<domain.com>/fs/users/$USER
sleep 5
# Parse password from csv file in share
pwd=$(cat "/Volumes/$USER/$USER.csv" | awk -F';' '! /"Password"/ {print $2}' | cut -d "\"" -f 2)
# Find existing certificate
c=$(echo "security find-certificate -c $USER.<domain.com>")
# Define if certificate has been found
success=$($c | awk '! /SecKeychainSearchCopyNext/ {print $1}')
if [[ -z $success ]]; then
# If the certificate hasn't been installed before - import
security import "/Volumes/$USER/$USER.pfx" -k "$kc" -P "$pwd"
else
# End date of the existing certificate
currentCertEndDate=$(security find-certificate -c $USER.<domain.com> -p | openssl x509 -enddate | cut -f2 -d= | head -n 1)
currentCertEndDateformat=$(date -j -f "%b %d %T %Y %Z" "$currentCertEndDate" "+%s")
# Creation date of the new certificate + 1 year
newCertEndDate=$(GetFileInfo -d /Volumes/$USER/$USER.pfx)
newCertEndDateFormat=$(date -j -f "%m/%d/%Y %H:%M:%S" -v+1y "$newCertEndDate" "+%s")
# If existing one expires earlier, delete it and import new one
if [[ $currentCertEndDateformat < $newCertEndDateFormat ]]; then
security delete-certificate -c $USER.<domain.com>
security import "/Volumes/$USER/$USER.pfx" -k "$kc" -P "$pwd"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment