Subversion Repositories ALCASAR

Rev

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

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