Subversion Repositories ALCASAR

Rev

Rev 1744 | Rev 1750 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

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