Subversion Repositories ALCASAR

Rev

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

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