Subversion Repositories ALCASAR

Rev

Rev 2261 | Rev 2309 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

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