Subversion Repositories ALCASAR

Rev

Rev 1963 | Rev 2260 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1710 richard 1
#!/bin/sh
2223 tom.houday 2
#
3
# $Id: alcasar-importcert.sh 2223 2017-05-14 14:38:01Z tom.houdayer $
4
#
1710 richard 5
# alcasar-importcert.sh
1736 richard 6
# by Raphaël, Hugo, Clément, Bettyna & rexy
2223 tom.houday 7
#
1710 richard 8
# This script is distributed under the Gnu General Public License (GPL)
2223 tom.houday 9
#
1710 richard 10
# Script permettant
11
# - d'importer des certificats sur Alcasar
1733 richard 12
# - de revenir au certificat par default
2223 tom.houday 13
#
1710 richard 14
# This script allows
1733 richard 15
# - to import a certificate in Alcasar
16
# - to go back to the default certificate
1710 richard 17
 
18
SED="/bin/sed -ri"
19
DIR_CERT="/etc/pki/tls"
1736 richard 20
CONF_FILE="/usr/local/etc/alcasar.conf"
21
PRIVATE_IP_MASK=`grep PRIVATE_IP $CONF_FILE|cut -d"=" -f2`
22
PRIVATE_IP=`echo $PRIVATE_IP_MASK | cut -d"/" -f1`
1747 richard 23
DEFAULT_FQDN='alcasar.localdomain'
1710 richard 24
 
1733 richard 25
usage="Usage: alcasar-importcert.sh -i /path/to/certificate.crt -k /path/to/privatekey.key (-c /path/to/serverchain.crt) || alcasar-importcert.sh -d (Cette utilisation permet de revenir au certificat par default)"
1710 richard 26
nb_args=$#
1733 richard 27
arg1=$1
1710 richard 28
 
1733 richard 29
 
30
function defaultNdd()
31
{
1758 richard 32
	$SED "s/^HOSTNAME=.*/HOSTNAME=alcasar/g" /usr/local/etc/alcasar.conf
33
	$SED "s/^DOMAIN=.*/DOMAIN=localdomain/g" /usr/local/etc/alcasar.conf
1963 richard 34
	cat <<EOF > /etc/hosts
35
127.0.0.1	localhost
36
$PRIVATE_IP	alcasar alcasar.localdomain
37
EOF
38
	$SED "s/^locationname.*/locationname\talcasar.localdomain/g" /etc/chilli.conf
39
	$SED "s/^uamserver.*/uamserver\thttps:\/\/alcasar.localdomain\/intercept.php/g" /etc/chilli.conf
40
	$SED "s/^radiusnasid.*/radiusnasid\talcasar.localdomain/g" /etc/chilli.conf
41
	$SED "s/^uamallowed.*/uamallowed\talcasar,alcasar.localdomain/g" /etc/chilli.conf
1758 richard 42
	$SED "s/^ServerName.*/ServerName alcasar.localdomain/g" /etc/httpd/conf/httpd.conf
1740 richard 43
	hostnamectl set-hostname alcasar.localdomain
44
	$SED "s/^\tErrorDocument.*/\tErrorDocument 404 https:\/\/alcasar.localdomain\//g" /etc/httpd/conf/webapps.d/alcasar.conf
1747 richard 45
	$SED "s/^\tAuthDigestDomain.*/\tAuthDigestDomain alcasar.localdomain/g" /etc/httpd/conf/webapps.d/alcasar.conf
46
	$SED "s/^    ServerName.*/    ServerName alcasar.localdomain/g" /etc/httpd/conf/sites.d/00_default_vhosts.conf /etc/httpd/conf/sites.d/00_default_ssl_vhost.conf
1733 richard 47
}
48
 
49
function defaultCert()
50
{
1740 richard 51
	mv -f $DIR_CERT/certs/alcasar.crt.old $DIR_CERT/certs/alcasar.crt
52
	mv -f $DIR_CERT/private/alcasar.key.old $DIR_CERT/private/alcasar.key
53
	if [ -f $DIR_CERT/certs/server-chain.crt.old ]
1733 richard 54
	then
1740 richard 55
		mv $DIR_CERT/certs/server-chain.crt.old $DIR_CERT/certs/server-chain.crt
1743 clement.si 56
	#else 
57
	#	rm -f $DIR_CERT/certs/server-chain.crt
1733 richard 58
	fi
59
}
60
 
1710 richard 61
function domainName() # change the domain name in the conf files
62
{
1744 clement.si 63
	fqdn=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p' | cut -d'/' -f 1)
1934 raphael.pi 64
 
65
        #check if there is a wildcard in $fqdn
1941 raphael.pi 66
        if [[ $fqdn == *"*"* ]];
1934 raphael.pi 67
        then
68
                hostname="alcasar"
1941 raphael.pi 69
                fqdn=${fqdn/"*"/$hostname}
1934 raphael.pi 70
        else
71
                hostname=`echo $fqdn | awk -F'.' '{ print $1 }'`
72
        fi
73
        domain=`echo $fqdn | awk -F'.' '{$1="";OFS=".";print $0}' |sed 's/^.//'`
74
        echo "fqdn=$fqdn hostname=$hostname domain=$domain"
75
 
76
        #check fqdn format      
77
        if [[ "$fqdn" != "" && "$domain" != "" && "$hostname" == "alcasar" ]];
1733 richard 78
	then
1758 richard 79
		$SED "s/^HOSTNAME=.*/HOSTNAME=$hostname/g" /usr/local/etc/alcasar.conf
1736 richard 80
		$SED "s/^DOMAIN=.*/DOMAIN=$domain/g" /usr/local/etc/alcasar.conf
81
		cat <<EOF > /etc/hosts
82
127.0.0.1	localhost
1963 richard 83
$PRIVATE_IP	$hostname $hostname.localdomain $fqdn
1736 richard 84
EOF
85
		$SED "s/^locationname.*/locationname\t$fqdn/g" /etc/chilli.conf
86
		$SED "s/^uamserver.*/uamserver\thttps:\/\/$fqdn\/intercept.php/g" /etc/chilli.conf
87
		$SED "s/^radiusnasid.*/radiusnasid\t$fqdn/g" /etc/chilli.conf
88
		$SED "s/^uamallowed.*/uamallowed\t$hostname,$fqdn/g" /etc/chilli.conf
89
		$SED "s/^ServerName.*/ServerName $fqdn/g" /etc/httpd/conf/httpd.conf
1740 richard 90
		hostnamectl set-hostname $fqdn
91
		$SED "s/^\tErrorDocument.*/\tErrorDocument 404 https:\/\/$fqdn\//g" /etc/httpd/conf/webapps.d/alcasar.conf
1747 richard 92
		$SED "s/^\tAuthDigestDomain.*/\tAuthDigestDomain $fqdn/g" /etc/httpd/conf/webapps.d/alcasar.conf
93
		$SED "s/^    ServerName.*/    ServerName $fqdn/g" /etc/httpd/conf/sites.d/00_default_vhosts.conf /etc/httpd/conf/sites.d/00_default_ssl_vhost.conf /etc/httpd/conf/vhosts-ssl.default
1710 richard 94
	fi
95
}
96
 
97
function certImport()
98
{
1740 richard 99
	if [ ! -f "$DIR_CERT/certs/alcasar.crt.old" ]
1710 richard 100
	then
101
		echo "Backup of old cert (alcasar.crt)"
1740 richard 102
		mv $DIR_CERT/certs/alcasar.crt $DIR_CERT/certs/alcasar.crt.old
1710 richard 103
	fi
1740 richard 104
	if [ ! -f "$DIR_CERT/private/alcasar.key.old" ]
1710 richard 105
	then
106
		echo "Backup of old private key (alcasar.key)"
1740 richard 107
		mv $DIR_CERT/private/alcasar.key $DIR_CERT/private/alcasar.key.old
1710 richard 108
	fi
1740 richard 109
	cp $cert $DIR_CERT/certs/alcasar.crt
110
	cp $key $DIR_CERT/private/alcasar.key
1733 richard 111
 
1743 clement.si 112
	rm $cert $key
113
 
1740 richard 114
	chown root:apache $DIR_CERT/certs/alcasar.crt
115
	chown root:apache $DIR_CERT/private/alcasar.key
1710 richard 116
 
1740 richard 117
	chmod 750 $DIR_CERT/certs/alcasar.crt
118
	chmod 750 $DIR_CERT/private/alcasar.key
1710 richard 119
	if [ "$sc" != "" ]
120
	then
121
		echo "cert-chain exists"
1740 richard 122
		if [ ! -f "$DIR_CERT/certs/server-chain.crt.old" ]
1710 richard 123
		then
124
			echo "Backup of old cert-chain (server-chain.crt)"
1740 richard 125
			mv $DIR_CERT/certs/server-chain.crt $DIR_CERT/certs/server-chain.crt.old
1710 richard 126
		fi
1740 richard 127
		cp $sc $DIR_CERT/certs/server-chain.crt
1743 clement.si 128
		rm $sc
1740 richard 129
		chown root:apache $DIR_CERT/certs/server-chain.crt
130
		chmod 750 $DIR_CERT/certs/server-chain.crt
1710 richard 131
	fi
132
}
133
 
1733 richard 134
 
135
if [ $nb_args -eq 0 ]
1710 richard 136
then
1733 richard 137
	echo "$usage"
138
	exit 1
1710 richard 139
fi
140
 
1733 richard 141
case $arg1 in
1710 richard 142
	-\? | -h* | --h*)
143
		echo "$usage"
144
		exit 0
145
		;;
146
	-i)
1733 richard 147
		arg3=$3
148
		arg5=$5
149
		cert=$2
150
		key=$4
151
		sc=$6
152
 
153
		if [ "$cert" == "" ] || [ "$key" == "" ]
154
		then
155
			echo "$usage"
156
			exit 1
157
		fi
158
 
159
		if [ ! -f "$cert" -o ! -f "$key" ]
160
		then
161
			echo "Certificate and/or private key not found"
162
			exit 1
163
		fi
164
 
165
		if [ ${cert: -4} != ".crt" ]
166
		then
167
			echo "Invalid certificate file"
168
			exit 1
169
		fi
170
 
171
		if [ ${key: -4} != ".key" ]
172
		then
173
			echo "Invalid private key"
174
			exit 1
175
		fi
176
 
177
		if [ "$arg5" != "-c" ] || [ ! -f "$sc" ]
178
		then
179
			echo "No server-chain given"
180
			echo "Importing certificate $cert with private key $key"
181
			sc=""
182
		else
183
			echo "Importing certificate $cert with private key $key and server-chain $sc"
184
		fi
185
		domainName $cert
186
		certImport $cert $key $sc
1765 richard 187
		for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
1740 richard 188
		do
189
			echo "restarting $services"; systemctl restart $services; sleep 1
190
		done
1710 richard 191
		;;
1733 richard 192
	-d)
193
		if [ -f "/etc/pki/tls/certs/alcasar.crt.old" -a -f "/etc/pki/tls/private/alcasar.key.old" ]
194
		then
195
			echo "Restoring default certificate"
196
			defaultCert
197
			defaultNdd
1765 richard 198
			for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
1740 richard 199
			do
200
				echo "restarting $services"; systemctl restart $services; sleep 1
201
			done
1733 richard 202
		fi
203
		;;
1710 richard 204
	*)
205
		echo "$usage"
206
		;;
207
esac