Subversion Repositories ALCASAR

Rev

Rev 1733 | Rev 1740 | 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`
1710 richard 21
 
1733 richard 22
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 23
nb_args=$#
1733 richard 24
arg1=$1
1710 richard 25
 
1733 richard 26
 
27
function defaultNdd()
28
{
29
	$SED 's/^DOMAIN=.*/DOMAIN=localdomain/g' /usr/local/etc/alcasar.conf
30
	$SED 's/\.([a-zA-Z][a-zA-Z0-9-]+(\.[a-z]{2,4})?)/.localdomain/g' /etc/hosts
31
	$SED 's/alcasar\.([a-zA-Z0-9-]+(\.[a-z]{2,4})?)/alcasar.localdomain/g' /etc/chilli.conf
32
	$SED 's/^domain.*/domain\t\tlocaldomain/g' /etc/chilli.conf
33
	$SED 's/^ServerName.*/ServerName alcasar.localdomain/g' /etc/httpd/conf/httpd.conf
1736 richard 34
	$SED "s/^domain=.*/domain=localdomain/g" /etc/dnsmasq.conf /etc/dnsmasq-blackhole.conf /etc/dnsmasq-blacklist.conf /etc/dnsmasq-whitelist.conf
1733 richard 35
}
36
 
37
function defaultCert()
38
{
39
	cd $DIR_CERT
40
	rm private/alcasar.key
41
	rm certs/alcasar.crt
42
	mv certs/alcasar.crt.old certs/alcasar.crt
43
	mv private/alcasar.key.old private/alcasar.key
44
	if [ -f certs/server-chain.crt.old ]
45
	then
46
		rm certs/server-chain.crt
47
		mv certs/server-chain.crt.old certs/server-chain.crt
48
	fi
49
}
50
 
1710 richard 51
function domainName() # change the domain name in the conf files
52
{
53
 
1736 richard 54
	fqdn=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p')
55
	hostname=`echo $fqdn | awk -F'.' '{ print $1 }'`
56
	domain=`echo $fqdn | awk -F'.' '{$1="";OFS=".";print $0}' |sed 's/^.//'`
57
	echo "fqdn=$fqdn hostname=$hostname domain=$domain"
58
	if [ "$fqdn" != "" ]
1733 richard 59
	then
1736 richard 60
		$SED "s/^DOMAIN=.*/DOMAIN=$domain/g" /usr/local/etc/alcasar.conf
61
		cat <<EOF > /etc/hosts
62
127.0.0.1	localhost
63
$PRIVATE_IP	$fqdn $hostname
64
EOF
65
		$SED "s/^domain.*/domain\t\t$domain/g" /etc/chilli.conf
66
		$SED "s/^locationname.*/locationname\t$fqdn/g" /etc/chilli.conf
67
		$SED "s/^uamserver.*/uamserver\thttps:\/\/$fqdn\/intercept.php/g" /etc/chilli.conf
68
		$SED "s/^radiusnasid.*/radiusnasid\t$fqdn/g" /etc/chilli.conf
69
		$SED "s/^uamallowed.*/uamallowed\t$hostname,$fqdn/g" /etc/chilli.conf
70
		$SED "s/^ServerName.*/ServerName $fqdn/g" /etc/httpd/conf/httpd.conf
71
		$SED "s/^domain=.*/domain=$domain/g" /etc/dnsmasq.conf /etc/dnsmasq-blackhole.conf /etc/dnsmasq-blacklist.conf /etc/dnsmasq-whitelist.conf
1710 richard 72
	fi
73
}
74
 
75
function certImport()
76
{
77
	cd $DIR_CERT
1733 richard 78
 
1710 richard 79
	if [ ! -f "/etc/pki/tls/certs/alcasar.crt.old" ]
80
	then
81
		echo "Backup of old cert (alcasar.crt)"
82
		mv certs/alcasar.crt certs/alcasar.crt.old
83
	fi
84
	if [ ! -f "/etc/pki/tls/private/alcasar.key.old" ]
85
	then
86
		echo "Backup of old private key (alcasar.key)"
87
		mv private/alcasar.key private/alcasar.key.old
88
	fi
1733 richard 89
 
1710 richard 90
	cp $cert certs/alcasar.crt
91
	cp $key private/alcasar.key
92
 
93
	chown root:apache certs/alcasar.crt
94
	chown root:apache private/alcasar.key
95
 
96
	chmod 750 certs/alcasar.crt
97
	chmod 750 private/alcasar.key
98
 
99
	if [ "$sc" != "" ]
100
	then
101
		echo "cert-chain exists"
102
		if [ ! -f "/etc/pki/tls/certs/server-chain.crt.old" ]
103
		then
104
			echo "Backup of old cert-chain (server-chain.crt)"
105
			mv certs/server-chain.crt certs/server-chain.crt.old
106
		fi
107
		cp $sc certs/server-chain.crt
108
		chown root:apache certs/server-chain.crt
109
		chmod 750 certs/server-chain.crt
110
	fi
111
}
112
 
1733 richard 113
 
114
if [ $nb_args -eq 0 ]
1710 richard 115
then
1733 richard 116
	echo "$usage"
117
	exit 1
1710 richard 118
fi
119
 
1733 richard 120
case $arg1 in
1710 richard 121
	-\? | -h* | --h*)
122
		echo "$usage"
123
		exit 0
124
		;;
125
	-i)
1733 richard 126
		arg3=$3
127
		arg5=$5
128
		cert=$2
129
		key=$4
130
		sc=$6
131
 
132
		if [ "$cert" == "" ] || [ "$key" == "" ]
133
		then
134
			echo "$usage"
135
			exit 1
136
		fi
137
 
138
		if [ ! -f "$cert" -o ! -f "$key" ]
139
		then
140
			echo "Certificate and/or private key not found"
141
			exit 1
142
		fi
143
 
144
		if [ ${cert: -4} != ".crt" ]
145
		then
146
			echo "Invalid certificate file"
147
			exit 1
148
		fi
149
 
150
		if [ ${key: -4} != ".key" ]
151
		then
152
			echo "Invalid private key"
153
			exit 1
154
		fi
155
 
156
		if [ "$arg5" != "-c" ] || [ ! -f "$sc" ]
157
		then
158
			echo "No server-chain given"
159
			echo "Importing certificate $cert with private key $key"
160
			sc=""
161
		else
162
			echo "Importing certificate $cert with private key $key and server-chain $sc"
163
		fi
164
 
165
		domainName $cert
166
		certImport $cert $key $sc
1736 richard 167
		for services in chilli httpd dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist
168
			do
169
				systemctl restart $services
170
			done
1710 richard 171
		;;
1733 richard 172
	-d)
173
		if [ -f "/etc/pki/tls/certs/alcasar.crt.old" -a -f "/etc/pki/tls/private/alcasar.key.old" ]
174
		then
175
			echo "Restoring default certificate"
176
			defaultCert
177
			defaultNdd
178
			systemctl restart chilli.service
179
			systemctl restart httpd.service
180
		fi
181
		;;
1710 richard 182
	*)
183
		echo "$usage"
184
		;;
185
esac