#!/bin/bash
VERSION=2015-10-02
# Tento skript vyzaduje curl!!!
# Testovano na curl 7.9.8
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

CURL=curl # Nebo nastavte cestu natvrdo
! which curl >/dev/null 2>&1 && { echo Nemohu najit curl, koncim... ; exit 1; }
umask 022

CERT=`mktemp /tmp/cert_chain.pem.XXXXXX`
cat <<EOF > $CERT
# subject: CN=TERENA SSL CA 3, O=TERENA, L=Amsterdam, ST=Noord-Holland, C=NL
# issuer:  CN=DigiCert Assured ID Root CA, OU=www.digicert.com, O=DigiCert Inc, C=US
# SHA256 Fingerprint: BE:B8:EF:E9:B1:A7:3C:84:1B:37:5A:90:E5:FF:F8:04:88:48:E3:A2:AF:66:F6:C4:DD:7B:93:8D:6F:E8:C5:D8
-----BEGIN CERTIFICATE-----
MIIE+zCCA+OgAwIBAgIQCHC8xa8/25Wakctq7u/kZTANBgkqhkiG9w0BAQsFADBl
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVkIElEIFJv
b3QgQ0EwHhcNMTQxMTE4MTIwMDAwWhcNMjQxMTE4MTIwMDAwWjBkMQswCQYDVQQG
EwJOTDEWMBQGA1UECBMNTm9vcmQtSG9sbGFuZDESMBAGA1UEBxMJQW1zdGVyZGFt
MQ8wDQYDVQQKEwZURVJFTkExGDAWBgNVBAMTD1RFUkVOQSBTU0wgQ0EgMzCCASIw
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMV2Dw/ZQyk7bG3RR63eEL8jwnio
Snc18SNb4EweQefCMQC9iDdFdd25AhCAHo/tZCMERaegOTuBTc9jP8JJ/yKeiLDS
lrlcinQfkioq8hLIt2hUtVhBgUBoBhpPhSn7tU08D08/QJYbzqjMXjX/ZJj1dd10
VAWgNhEEEiRVY++Udy538RV27tOkWUUhn6i+0SftCuirOMo/h9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nA9Ng==
-----END CERTIFICATE-----
# subject: CN=DigiCert Assured ID Root CA, OU=www.digicert.com, O=DigiCert Inc, C=US
# issuer:  CN=DigiCert Assured ID Root CA, OU=www.digicert.com, O=DigiCert Inc, C=US
# SHA256 Fingerprint: 3E:90:99:B5:01:5E:8F:48:6C:00:BC:EA:9D:11:1E:E7:21:FA:BA:35:5A:89:BC:F1:DF:69:56:1E:3D:C6:32:5C
-----BEGIN CERTIFICATE-----
MIIDtzCCAp+gAwIBAgIQDOfg5RfYRv6P5WD8G/AwOTANBgkqhkiG9w0BAQUFADBl
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVkIElEIFJv
b3QgQ0EwHhcNMDYxMTEwMDAwMDAwWhcNMzExMTEwMDAwMDAwWjBlMQswCQYDVQQG
EwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNl
cnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVkIElEIFJvb3QgQ0EwggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCtDhXO5EOAXLGH87dg+XESpa7c
JpSIqvTO9SA5KFhgDPiA2qkVlTJhPLWxKISKityfCgyDF3qPkKyK53lTXDGEKvYP
mDI2dsze3Tyoou9q+yHyUmHfnyDXH+Kx2f4YZNISW1/5WBg1vEfNoTb5a3/UsDg+
wRvDjDPZ2C8Y/igPs6eD1sNuRMBhNZYW/lmci3Zt1/GiSw0r/wty2p5g0I6QNcZ4
VYcgoc/lbQrISXwxmDNsIumH0DJaoroTghHtORedmTpyoeb6pNnVFzF1roV9Iq4/
AUaG9ih5yLHa5FcXxH4cDrC0kqZWs72yl+2qp/C3xag/lRbQ/6GW6whfGHdPAgMB
AAGjYzBhMA4GA1UdDwEB/wQEAwIBhjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW
BBRF66Kv9JLLgjEtUYunpyGd823IDzAfBgNVHSMEGDAWgBRF66Kv9JLLgjEtUYun
pyGd823IDzANBgkqhkiG9w0BAQUFAAOCAQEAog683+Lt8ONyc3pklL/3cmbYMuRC
dWKuh+vy1dneVrOfzM4UKLkNl2BcEkxY5NM9g0lFWJc1aRqoR+pWxnmrEthngYTf
fwk8lOa4JiwgvT2zKIn3X/8i4peEH+ll74fg38FnSbNd67IJKusm7Xi+fT8r87cm
NW1fiQG2SVufAQWbqz0lwcy2f8Lxb4bG+mRo64EtlOtCt/qMHt1i8b5QZ7dsvfPx
H2sMNgcWfzd8qVttevESRmCD1ycEvkvOl77DZypoEd+A5wwzZr8TDRRu838fYxAe
+o0bJW1sj6W3YQGx0qMmoRBxna3iw/nDmVG3KwcIzi7mULKn+gpFL6Lw8g==
-----END CERTIFICATE-----
EOF

SSH_KNOWN_HOSTS=`mktemp /tmp/ssh_known_hosts.XXXXXX`
test -r /etc/ssh/ssh_known_hosts.head && cat /etc/ssh/ssh_known_hosts.head >> $SSH_KNOWN_HOSTS
CURL_STATUS=`mktemp /tmp/curl.status.XXXXXX`

CURL_SELHALO="false"
$CURL --cacert $CERT https://fadmin.fi.muni.cz/noauth/sshkh/ssh_known_hosts >$SSH_KNOWN_HOSTS 2>$CURL_STATUS || CURL_SELHALO=true

if [ "$CURL_SELHALO" = "true" ]; then
    # mozna se zmenil certificate chain, zkusime bez
    CURL_SELHALO="false"
    $CURL https://fadmin.fi.muni.cz/noauth/sshkh/ssh_known_hosts >$SSH_KNOWN_HOSTS 2>$CURL_STATUS || CURL_SELHALO=true
fi

if [ "$CURL_SELHALO" = "true" ]; then
    echo
    cat $CURL_STATUS
    rm -f $SSH_KNOWN_HOSTS 2>/dev/null
    echo
    echo "Soubor ssh_known_hosts se nepodarilo stahnout."
    echo "Verze skriptu: $VERSION"
    echo "Ujistete se, ze mate nejnovejsi verzi (https://fadmin.fi.muni.cz/noauth/sshkh/get_ssh_hosts.tar.gz)"
    exit 3
fi

POCET_RADKU=`tail -1 $SSH_KNOWN_HOSTS | grep "### EOF - this file has not been truncated." | sed 's/[^0-9]*\([0-9][0-9]*\)[^0-9]*/\1/'`
[ "$POCET_RADKU" = `wc -l < $SSH_KNOWN_HOSTS` ] && STAZENO=true
if [ "$STAZENO" != "true" ]; then
    echo
    echo "Stahovani souboru ssh_known_hosts bylo preruseno a soubor neni kompletni"
    echo
    rm -f $SSH_KNOWN_HOSTS 2>/dev/null
    exit 2
fi

tty >/dev/null 2>&1 && cat $CURL_STATUS
tty >/dev/null 2>&1 && echo "Soubor ssh_known_hosts byl uspesne stazen."
chmod 644 $SSH_KNOWN_HOSTS
mv -f $SSH_KNOWN_HOSTS /etc/ssh/ssh_known_hosts >/dev/null 2>&1 || echo "Nepodarilo se prepsat soubor /etc/ssh/ssh_known_hosts"
test -x /sbin/restorecon && /sbin/restorecon /etc/ssh/ssh_known_hosts >/dev/null 2>&1

rm -f $CERT
rm -f $CURL_STATUS
sync
exit 0

