Subversion Repositories ALCASAR

Rev

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

Rev 1710 Rev 1733
1
#!/bin/sh
1
#!/bin/sh
2
 
2
 
3
# alcasar-importcert.sh
3
# alcasar-importcert.sh
4
# by Raphaël, Hugo, Clément, Bettyna
4
# by Raphaël, Hugo, Clément, Bettyna
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
 
11
 
11
# This script allows
12
# This script allows
12
# - to import certificate in Alcasar
13
# - to import a certificate in Alcasar
-
 
14
# - to go back to the default certificate
13
 
15
 
14
SED="/bin/sed -ri"
16
SED="/bin/sed -ri"
15
 
17
 
16
DIR_CERT="/etc/pki/tls"
18
DIR_CERT="/etc/pki/tls"
17
 
19
 
18
usage="Usage: alcasar-importcert.sh -i YourCertificate.crt -k YourAlcasar.key -c Chaîne.com"
20
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)"
-
 
21
 
19
 
22
 
20
nb_args=$#
23
nb_args=$#
21
args=$1
24
arg1=$1
-
 
25
 
-
 
26
 
-
 
27
# nb_args=$#
-
 
28
# args=$1
22
args1=$3
29
# args1=$3
23
args2=$5
30
# args2=$5
24
cert=$2
31
# cert=$2
25
key=$4
32
# key=$4
26
sc=$6
33
# sc=$6
-
 
34
 
-
 
35
function defaultNdd()
-
 
36
{
-
 
37
	$SED 's/^DOMAIN=.*/DOMAIN=localdomain/g' /usr/local/etc/alcasar.conf
-
 
38
	$SED 's/\.([a-zA-Z][a-zA-Z0-9-]+(\.[a-z]{2,4})?)/.localdomain/g' /etc/hosts
-
 
39
	$SED 's/alcasar\.([a-zA-Z0-9-]+(\.[a-z]{2,4})?)/alcasar.localdomain/g' /etc/chilli.conf
-
 
40
	$SED 's/^domain.*/domain\t\tlocaldomain/g' /etc/chilli.conf
-
 
41
	$SED 's/^ServerName.*/ServerName alcasar.localdomain/g' /etc/httpd/conf/httpd.conf
-
 
42
}
-
 
43
 
-
 
44
function defaultCert()
-
 
45
{
-
 
46
	cd $DIR_CERT
-
 
47
	rm private/alcasar.key
-
 
48
	rm certs/alcasar.crt
-
 
49
	mv certs/alcasar.crt.old certs/alcasar.crt
-
 
50
	mv private/alcasar.key.old private/alcasar.key
-
 
51
	if [ -f certs/server-chain.crt.old ]
-
 
52
	then
-
 
53
		rm certs/server-chain.crt
-
 
54
		mv certs/server-chain.crt.old certs/server-chain.crt
-
 
55
	fi
-
 
56
}
27
 
57
 
28
function domainName() # change the domain name in the conf files
58
function domainName() # change the domain name in the conf files
29
{
59
{
30
 
60
 
31
	ndd=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p')
61
	ndd=$(openssl x509 -noout -subject -in $cert | sed -n '/^subject/s/^.*CN=//p')
32
	echo $ndd
62
	echo $ndd
33
	if [ "$ndd" != "" ]
63
	if [ "$ndd" != "" ]
34
	then	
64
	then
35
		$SED "s/^DOMAIN=.*/DOMAIN=$ndd/g" /usr/local/etc/alcasar.conf
65
		$SED "s/^DOMAIN=.*/DOMAIN=$ndd/g" /usr/local/etc/alcasar.conf
36
		$SED "s/\.([a-zA-Z][a-zA-Z0-9-]+(\.[a-z]{2,4})?)/.$ndd/g" /etc/hosts
66
		$SED "s/\.([a-zA-Z][a-zA-Z0-9-]+(\.[a-z]{2,4})?)/.$ndd/g" /etc/hosts
37
		$SED "s/alcasar\.([a-zA-Z0-9-]+(\.[a-z]{2,4})?)/alcasar.$ndd/g" /etc/chilli.conf
67
		$SED "s/alcasar\.([a-zA-Z0-9-]+(\.[a-z]{2,4})?)/alcasar.$ndd/g" /etc/chilli.conf
38
		$SED "s/^domain.*/domain\t\t$ndd/g" /etc/chilli.conf
68
		$SED "s/^domain.*/domain\t\t$ndd/g" /etc/chilli.conf
39
		$SED "s/^ServerName.*/ServerName alcasar.$ndd/g" /etc/httpd/conf/httpd.conf
69
		$SED "s/^ServerName.*/ServerName alcasar.$ndd/g" /etc/httpd/conf/httpd.conf
40
	fi
70
	fi
41
}
71
}
42
 
72
 
43
function certImport()
73
function certImport()
44
{
74
{
45
	cd $DIR_CERT
75
	cd $DIR_CERT
46
	
76
 
47
	if [ ! -f "/etc/pki/tls/certs/alcasar.crt.old" ]
77
	if [ ! -f "/etc/pki/tls/certs/alcasar.crt.old" ]
48
	then
78
	then
49
		echo "Backup of old cert (alcasar.crt)"
79
		echo "Backup of old cert (alcasar.crt)"
50
		mv certs/alcasar.crt certs/alcasar.crt.old
80
		mv certs/alcasar.crt certs/alcasar.crt.old
51
	fi
81
	fi
52
	if [ ! -f "/etc/pki/tls/private/alcasar.key.old" ]
82
	if [ ! -f "/etc/pki/tls/private/alcasar.key.old" ]
53
	then
83
	then
54
		echo "Backup of old private key (alcasar.key)"
84
		echo "Backup of old private key (alcasar.key)"
55
		mv private/alcasar.key private/alcasar.key.old
85
		mv private/alcasar.key private/alcasar.key.old
56
	fi
86
	fi
57
	
87
 
58
	cp $cert certs/alcasar.crt
88
	cp $cert certs/alcasar.crt
59
	cp $key private/alcasar.key
89
	cp $key private/alcasar.key
60
 
90
 
61
	chown root:apache certs/alcasar.crt
91
	chown root:apache certs/alcasar.crt
62
	chown root:apache private/alcasar.key
92
	chown root:apache private/alcasar.key
63
 
93
 
64
	chmod 750 certs/alcasar.crt
94
	chmod 750 certs/alcasar.crt
65
	chmod 750 private/alcasar.key
95
	chmod 750 private/alcasar.key
66
 
96
 
67
	if [ "$sc" != "" ]
97
	if [ "$sc" != "" ]
68
	then
98
	then
69
		echo "cert-chain exists"
99
		echo "cert-chain exists"
70
		if [ ! -f "/etc/pki/tls/certs/server-chain.crt.old" ]
100
		if [ ! -f "/etc/pki/tls/certs/server-chain.crt.old" ]
71
		then
101
		then
72
			echo "Backup of old cert-chain (server-chain.crt)"
102
			echo "Backup of old cert-chain (server-chain.crt)"
73
			mv certs/server-chain.crt certs/server-chain.crt.old
103
			mv certs/server-chain.crt certs/server-chain.crt.old
74
		fi
104
		fi
75
		cp $sc certs/server-chain.crt
105
		cp $sc certs/server-chain.crt
76
		chown root:apache certs/server-chain.crt
106
		chown root:apache certs/server-chain.crt
77
		chmod 750 certs/server-chain.crt
107
		chmod 750 certs/server-chain.crt
78
	fi
108
	fi
79
}
109
}
80
 
110
 
-
 
111
 
81
if [ $nb_args -eq 0 ] || [ "$cert" == "" ] || [ "$key" == "" ]
112
if [ $nb_args -eq 0 ]
82
then
113
then
83
	nb_args=1
114
	echo "$usage"
84
	args="-h"
115
	exit 1
85
fi
116
fi
86
 
117
 
87
case $args in
118
case $arg1 in
88
	-\? | -h* | --h*)
119
	-\? | -h* | --h*)
89
		echo "$usage"
120
		echo "$usage"
90
		exit 0
121
		exit 0
91
		;;
122
		;;
92
	-i)
123
	-i)
93
		echo "You want import the certificate: $2"
-
 
94
		;;
124
		arg3=$3
95
	*)
-
 
96
	echo "Unknown argument: $1"
-
 
97
	echo "$usage"
125
		arg5=$5
98
	exit 1
126
		cert=$2
99
	;;
127
		key=$4
100
esac
128
		sc=$6
101
 
129
 
102
case $args1 in
130
		if [ "$cert" == "" ] || [ "$key" == "" ]
103
	-\? | -h* | --h*)
131
		then
104
		echo "$usage"
132
			echo "$usage"
-
 
133
			exit 1
-
 
134
		fi
-
 
135
 
-
 
136
		if [ ! -f "$cert" -o ! -f "$key" ]
-
 
137
		then
-
 
138
			echo "Certificate and/or private key not found"
-
 
139
			exit 1
-
 
140
		fi
-
 
141
 
-
 
142
		if [ ${cert: -4} != ".crt" ]
-
 
143
		then
-
 
144
			echo "Invalid certificate file"
-
 
145
			exit 1
-
 
146
		fi
-
 
147
 
-
 
148
		if [ ${key: -4} != ".key" ]
-
 
149
		then
-
 
150
			echo "Invalid private key"
105
		exit 0
151
			exit 1
-
 
152
		fi
-
 
153
 
-
 
154
		if [ "$arg5" != "-c" ] || [ ! -f "$sc" ]
-
 
155
		then
-
 
156
			echo "No server-chain given"
-
 
157
			echo "Importing certificate $cert with private key $key"
-
 
158
			sc=""
-
 
159
		else
-
 
160
			echo "Importing certificate $cert with private key $key and server-chain $sc"
-
 
161
		fi
-
 
162
 
-
 
163
		domainName $cert
-
 
164
		certImport $cert $key $sc
-
 
165
		systemctl restart chilli.service
-
 
166
		systemctl restart httpd.service
106
		;;
167
		;;
107
	-k)
168
	-d)
-
 
169
		if [ -f "/etc/pki/tls/certs/alcasar.crt.old" -a -f "/etc/pki/tls/private/alcasar.key.old" ]
-
 
170
		then
108
		echo "With the private key: $4"
171
			echo "Restoring default certificate"
-
 
172
			defaultCert
-
 
173
			defaultNdd
-
 
174
			systemctl restart chilli.service
-
 
175
			systemctl restart httpd.service
-
 
176
		fi
109
		;;
177
		;;
110
	*)
178
	*)
111
	echo "Unknown argument: $3"
-
 
112
	echo "$usage"
179
		echo "$usage"
113
	exit 1
-
 
114
	;;
180
		;;
115
esac
181
esac
116
 
-
 
117
if [ "$args2" == "-c" ]
-
 
118
then
-
 
119
	echo "And the cert-chain: $6"
-
 
120
	if [ "$sc" == "" ]
-
 
121
	then
-
 
122
		echo "! Can't find the file of the chain-cert"
-
 
123
	fi
-
 
124
else
-
 
125
	echo "Without a cert-chain"
-
 
126
	sc=""
-
 
127
fi
-
 
128
 
-
 
129
domainName
-
 
130
certImport $cert $key $sc
-
 
131
systemctl restart chilli.service
-
 
132
systemctl restart httpd.service
-
 
133
 
182