Subversion Repositories ALCASAR

Rev

Rev 1758 | Rev 1934 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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