Subversion Repositories ALCASAR

Rev

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

Rev 1941 Rev 1961
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
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
	fqdn=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p' | cut -d'/' -f 1)
57
	fqdn=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p' | cut -d'/' -f 1)
58
 
58
 
59
        #check if there is a wildcard in $fqdn
59
        #check if there is a wildcard in $fqdn
60
        if [[ $fqdn == *"*"* ]];
60
        if [[ $fqdn == *"*"* ]];
61
        then
61
        then
62
                hostname="alcasar"
62
                hostname="alcasar"
63
                fqdn=${fqdn/"*"/$hostname}
63
                fqdn=${fqdn/"*"/$hostname}
64
        else
64
        else
65
                hostname=`echo $fqdn | awk -F'.' '{ print $1 }'`
65
                hostname=`echo $fqdn | awk -F'.' '{ print $1 }'`
66
        fi
66
        fi
67
        domain=`echo $fqdn | awk -F'.' '{$1="";OFS=".";print $0}' |sed 's/^.//'`
67
        domain=`echo $fqdn | awk -F'.' '{$1="";OFS=".";print $0}' |sed 's/^.//'`
68
        echo "fqdn=$fqdn hostname=$hostname domain=$domain"
68
        echo "fqdn=$fqdn hostname=$hostname domain=$domain"
69
 
69
 
70
        #check fqdn format      
70
        #check fqdn format      
71
        if [[ "$fqdn" != "" && "$domain" != "" && "$hostname" == "alcasar" ]];
71
        if [[ "$fqdn" != "" && "$domain" != "" && "$hostname" == "alcasar" ]];
72
	then
72
	then
73
		$SED "s/^HOSTNAME=.*/HOSTNAME=$hostname/g" /usr/local/etc/alcasar.conf
73
		$SED "s/^HOSTNAME=.*/HOSTNAME=$hostname/g" /usr/local/etc/alcasar.conf
74
		$SED "s/^DOMAIN=.*/DOMAIN=$domain/g" /usr/local/etc/alcasar.conf
74
		$SED "s/^DOMAIN=.*/DOMAIN=$domain/g" /usr/local/etc/alcasar.conf
75
		cat <<EOF > /etc/hosts
75
		cat <<EOF > /etc/hosts
76
127.0.0.1	localhost
76
127.0.0.1	localhost
77
$PRIVATE_IP	$fqdn $hostname
77
$PRIVATE_IP	$fqdn $hostname
78
EOF
78
EOF
79
		$SED "s/^domain.*/domain\t\t$domain/g" /etc/chilli.conf
-
 
80
		$SED "s/^locationname.*/locationname\t$fqdn/g" /etc/chilli.conf
79
		$SED "s/^locationname.*/locationname\t$fqdn/g" /etc/chilli.conf
81
		$SED "s/^uamserver.*/uamserver\thttps:\/\/$fqdn\/intercept.php/g" /etc/chilli.conf
80
		$SED "s/^uamserver.*/uamserver\thttps:\/\/$fqdn\/intercept.php/g" /etc/chilli.conf
82
		$SED "s/^radiusnasid.*/radiusnasid\t$fqdn/g" /etc/chilli.conf
81
		$SED "s/^radiusnasid.*/radiusnasid\t$fqdn/g" /etc/chilli.conf
83
		$SED "s/^uamallowed.*/uamallowed\t$hostname,$fqdn/g" /etc/chilli.conf
82
		$SED "s/^uamallowed.*/uamallowed\t$hostname,$fqdn/g" /etc/chilli.conf
84
		$SED "s/^ServerName.*/ServerName $fqdn/g" /etc/httpd/conf/httpd.conf
83
		$SED "s/^ServerName.*/ServerName $fqdn/g" /etc/httpd/conf/httpd.conf
85
		$SED "s/^domain=.*/domain=$domain/g" /etc/dnsmasq.conf /etc/dnsmasq-blackhole.conf /etc/dnsmasq-blacklist.conf /etc/dnsmasq-whitelist.conf
-
 
86
		hostnamectl set-hostname $fqdn
84
		hostnamectl set-hostname $fqdn
87
		$SED "s/^\tErrorDocument.*/\tErrorDocument 404 https:\/\/$fqdn\//g" /etc/httpd/conf/webapps.d/alcasar.conf
85
		$SED "s/^\tErrorDocument.*/\tErrorDocument 404 https:\/\/$fqdn\//g" /etc/httpd/conf/webapps.d/alcasar.conf
88
		$SED "s/^\tAuthDigestDomain.*/\tAuthDigestDomain $fqdn/g" /etc/httpd/conf/webapps.d/alcasar.conf
86
		$SED "s/^\tAuthDigestDomain.*/\tAuthDigestDomain $fqdn/g" /etc/httpd/conf/webapps.d/alcasar.conf
89
		$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
87
		$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
90
	fi
88
	fi
91
}
89
}
92
 
90
 
93
function certImport()
91
function certImport()
94
{
92
{
95
	if [ ! -f "$DIR_CERT/certs/alcasar.crt.old" ]
93
	if [ ! -f "$DIR_CERT/certs/alcasar.crt.old" ]
96
	then
94
	then
97
		echo "Backup of old cert (alcasar.crt)"
95
		echo "Backup of old cert (alcasar.crt)"
98
		mv $DIR_CERT/certs/alcasar.crt $DIR_CERT/certs/alcasar.crt.old
96
		mv $DIR_CERT/certs/alcasar.crt $DIR_CERT/certs/alcasar.crt.old
99
	fi
97
	fi
100
	if [ ! -f "$DIR_CERT/private/alcasar.key.old" ]
98
	if [ ! -f "$DIR_CERT/private/alcasar.key.old" ]
101
	then
99
	then
102
		echo "Backup of old private key (alcasar.key)"
100
		echo "Backup of old private key (alcasar.key)"
103
		mv $DIR_CERT/private/alcasar.key $DIR_CERT/private/alcasar.key.old
101
		mv $DIR_CERT/private/alcasar.key $DIR_CERT/private/alcasar.key.old
104
	fi
102
	fi
105
	cp $cert $DIR_CERT/certs/alcasar.crt
103
	cp $cert $DIR_CERT/certs/alcasar.crt
106
	cp $key $DIR_CERT/private/alcasar.key
104
	cp $key $DIR_CERT/private/alcasar.key
107
 
105
 
108
	rm $cert $key
106
	rm $cert $key
109
 
107
 
110
	chown root:apache $DIR_CERT/certs/alcasar.crt
108
	chown root:apache $DIR_CERT/certs/alcasar.crt
111
	chown root:apache $DIR_CERT/private/alcasar.key
109
	chown root:apache $DIR_CERT/private/alcasar.key
112
 
110
 
113
	chmod 750 $DIR_CERT/certs/alcasar.crt
111
	chmod 750 $DIR_CERT/certs/alcasar.crt
114
	chmod 750 $DIR_CERT/private/alcasar.key
112
	chmod 750 $DIR_CERT/private/alcasar.key
115
	if [ "$sc" != "" ]
113
	if [ "$sc" != "" ]
116
	then
114
	then
117
		echo "cert-chain exists"
115
		echo "cert-chain exists"
118
		if [ ! -f "$DIR_CERT/certs/server-chain.crt.old" ]
116
		if [ ! -f "$DIR_CERT/certs/server-chain.crt.old" ]
119
		then
117
		then
120
			echo "Backup of old cert-chain (server-chain.crt)"
118
			echo "Backup of old cert-chain (server-chain.crt)"
121
			mv $DIR_CERT/certs/server-chain.crt $DIR_CERT/certs/server-chain.crt.old
119
			mv $DIR_CERT/certs/server-chain.crt $DIR_CERT/certs/server-chain.crt.old
122
		fi
120
		fi
123
		cp $sc $DIR_CERT/certs/server-chain.crt
121
		cp $sc $DIR_CERT/certs/server-chain.crt
124
		rm $sc
122
		rm $sc
125
		chown root:apache $DIR_CERT/certs/server-chain.crt
123
		chown root:apache $DIR_CERT/certs/server-chain.crt
126
		chmod 750 $DIR_CERT/certs/server-chain.crt
124
		chmod 750 $DIR_CERT/certs/server-chain.crt
127
	fi
125
	fi
128
}
126
}
129
 
127
 
130
 
128
 
131
if [ $nb_args -eq 0 ]
129
if [ $nb_args -eq 0 ]
132
then
130
then
133
	echo "$usage"
131
	echo "$usage"
134
	exit 1
132
	exit 1
135
fi
133
fi
136
 
134
 
137
case $arg1 in
135
case $arg1 in
138
	-\? | -h* | --h*)
136
	-\? | -h* | --h*)
139
		echo "$usage"
137
		echo "$usage"
140
		exit 0
138
		exit 0
141
		;;
139
		;;
142
	-i)
140
	-i)
143
		arg3=$3
141
		arg3=$3
144
		arg5=$5
142
		arg5=$5
145
		cert=$2
143
		cert=$2
146
		key=$4
144
		key=$4
147
		sc=$6
145
		sc=$6
148
 
146
 
149
		if [ "$cert" == "" ] || [ "$key" == "" ]
147
		if [ "$cert" == "" ] || [ "$key" == "" ]
150
		then
148
		then
151
			echo "$usage"
149
			echo "$usage"
152
			exit 1
150
			exit 1
153
		fi
151
		fi
154
 
152
 
155
		if [ ! -f "$cert" -o ! -f "$key" ]
153
		if [ ! -f "$cert" -o ! -f "$key" ]
156
		then
154
		then
157
			echo "Certificate and/or private key not found"
155
			echo "Certificate and/or private key not found"
158
			exit 1
156
			exit 1
159
		fi
157
		fi
160
 
158
 
161
		if [ ${cert: -4} != ".crt" ]
159
		if [ ${cert: -4} != ".crt" ]
162
		then
160
		then
163
			echo "Invalid certificate file"
161
			echo "Invalid certificate file"
164
			exit 1
162
			exit 1
165
		fi
163
		fi
166
 
164
 
167
		if [ ${key: -4} != ".key" ]
165
		if [ ${key: -4} != ".key" ]
168
		then
166
		then
169
			echo "Invalid private key"
167
			echo "Invalid private key"
170
			exit 1
168
			exit 1
171
		fi
169
		fi
172
 
170
 
173
		if [ "$arg5" != "-c" ] || [ ! -f "$sc" ]
171
		if [ "$arg5" != "-c" ] || [ ! -f "$sc" ]
174
		then
172
		then
175
			echo "No server-chain given"
173
			echo "No server-chain given"
176
			echo "Importing certificate $cert with private key $key"
174
			echo "Importing certificate $cert with private key $key"
177
			sc=""
175
			sc=""
178
		else
176
		else
179
			echo "Importing certificate $cert with private key $key and server-chain $sc"
177
			echo "Importing certificate $cert with private key $key and server-chain $sc"
180
		fi
178
		fi
181
		domainName $cert
179
		domainName $cert
182
		certImport $cert $key $sc
180
		certImport $cert $key $sc
183
		for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
181
		for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
184
		do
182
		do
185
			echo "restarting $services"; systemctl restart $services; sleep 1
183
			echo "restarting $services"; systemctl restart $services; sleep 1
186
		done
184
		done
187
		;;
185
		;;
188
	-d)
186
	-d)
189
		if [ -f "/etc/pki/tls/certs/alcasar.crt.old" -a -f "/etc/pki/tls/private/alcasar.key.old" ]
187
		if [ -f "/etc/pki/tls/certs/alcasar.crt.old" -a -f "/etc/pki/tls/private/alcasar.key.old" ]
190
		then
188
		then
191
			echo "Restoring default certificate"
189
			echo "Restoring default certificate"
192
			defaultCert
190
			defaultCert
193
			defaultNdd
191
			defaultNdd
194
			for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
192
			for services in chilli dnsmasq dnsmasq-blackhole dnsmasq-blacklist dnsmasq-whitelist httpd
195
			do
193
			do
196
				echo "restarting $services"; systemctl restart $services; sleep 1
194
				echo "restarting $services"; systemctl restart $services; sleep 1
197
			done
195
			done
198
		fi
196
		fi
199
		;;
197
		;;
200
	*)
198
	*)
201
		echo "$usage"
199
		echo "$usage"
202
		;;
200
		;;
203
esac
201
esac
204
 
202