Subversion Repositories ALCASAR

Rev

Rev 2980 | Rev 2990 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
672 richard 1
#!/bin/bash
2454 tom.houday 2
#  $Id: alcasar.sh 2981 2021-07-23 14:37:14Z rexy $
1 root 3
 
2969 rexy 4
# ALCASAR is a Free and open source NAC (Network Access Controler) created by Franck BOUIJOUX (3abtux), Pascal LEVANT and Richard REY (Rexy)
5
# ALCASAR is based on a stripped Mageia (LSB) with the following open source softwares Coovachilli, freeradius, mariaDB, lighttpd, php, netfilter, e2guardian, ntpd, openssl, dnsmasq, unbound, gammu, clamav, Ulog, fail2ban, vnstat, wkhtml2pdf, ipt_NETFLOW, NFsen and NFdump
6
# contact : info@alcasar.net
959 franck 7
 
1 root 8
# Install script for ALCASAR (a secured and authenticated Internet access control captive portal)
2969 rexy 9
# This script is distributed under the Gnu General Public License (GPLV3)
2466 richard 10
 
1 root 11
# Options :
376 franck 12
#       -i or --install
13
#       -u or --uninstall
14
# Functions :
1378 richard 15
#	testing			: connectivity tests, free space test and mageia version test
1221 richard 16
#	init			: Installation of RPM and scripts
17
#	network			: Network parameters
2552 rexy 18
#	ACC				: ALCASAR Control Center installation
19
#	CA				: Certification Authority initialization
1837 richard 20
#	time_server		: NTPd configuration
1221 richard 21
#	init_db			: Initilization of radius database managed with MariaDB
2421 richard 22
#	freeradius		: FreeRadius initialisation
1389 richard 23
#	chilli			: coovachilli initialisation (+authentication page)
2521 armand.ito 24
#	e2guardian		: E2Guardian filtering HTTP proxy configuration
2840 rexy 25
#	antivirus		: clamav & freshclam configuration
1389 richard 26
#	ulogd			: log system in userland (match NFLOG target of iptables)
2775 rexy 27
#	nfsen			: Configuration of Netflow grapher (nfsen) & netflow collector (nfcapd)
2688 lucas.echa 28
#	unbound			: Name server configuration
29
#	dnsmasq			: Name server configuration (for whitelist ipset support)
1541 richard 30
#	vnstat			: little network stat daemon
2688 lucas.echa 31
#	BL				: Adaptation of Toulouse University BlackList : split into 3 BL (for unbound, for e2guardian and for Netfilter)
1266 richard 32
#	cron			: Logs export + watchdog + connexion statistics
1389 richard 33
#	fail2ban		: Fail2ban IDS installation and configuration
34
#	gammu_smsd		: Autoregister addon via SMS (gammu-smsd)
2969 rexy 35
#	msec			: Mageia security package configuration
2304 tom.houday 36
#	letsencrypt		: Let's Encrypt client
2552 rexy 37
#	post_install	: Security, log rotation, etc.
1 root 38
 
2499 tom.houday 39
DEBUG_ALCASAR='off'; export DEBUG_ALCASAR	# Debug mode = wait (hit key) after each function
1 root 40
DATE=`date '+%d %B %Y - %Hh%M'`
41
DATE_SHORT=`date '+%d/%m/%Y'`
595 richard 42
Lang=`echo $LANG|cut -c 1-2`
1362 richard 43
mode="install"
1 root 44
# ******* Files parameters - paramètres fichiers *********
2552 rexy 45
DIR_INSTALL=`pwd`						# current directory
1015 richard 46
DIR_CONF="$DIR_INSTALL/conf"			# install directory (with conf files)
47
DIR_SCRIPTS="$DIR_INSTALL/scripts"		# install directory (with script files)
2552 rexy 48
DIR_BLACKLIST="$DIR_INSTALL/blacklist"	# install directory (with blacklist files)
49
DIR_SAVE="/var/Save"					# backup directory (traceability_log, user_db, security_log)
50
DIR_WEB="/var/www/html"					# directory of Lighttpd
51
DIR_DG="/etc/e2guardian"				# directory of E2Guardian
52
DIR_ACC="$DIR_WEB/acc"					# directory of the 'ALCASAR Control Center'
1015 richard 53
DIR_DEST_BIN="/usr/local/bin"			# directory of ALCASAR scripts
54
DIR_DEST_ETC="/usr/local/etc"			# directory of ALCASAR conf files
2688 lucas.echa 55
DIR_DEST_SHARE="/usr/local/share"		# directory of share files used by ALCASAR (unbound for instance)
2552 rexy 56
CONF_FILE="$DIR_DEST_ETC/alcasar.conf"	# central ALCASAR conf file
1015 richard 57
PASSWD_FILE="/root/ALCASAR-passwords.txt"	# text file with the passwords and shared secrets
1 root 58
# ******* DBMS parameters - paramètres SGBD ********
2552 rexy 59
DB_RADIUS="radius"						# database name used by FreeRadius server
60
DB_USER="radius"						# user name allows to request the users database
61
DB_GAMMU="gammu"						# database name used by Gammu-smsd
1 root 62
# ******* Network parameters - paramètres réseau *******
2552 rexy 63
HOSTNAME="alcasar"						# default hostname
64
DOMAIN="localdomain"					# default local domain
2736 rexy 65
EXTIF=''								# EXTIF is connected to the ISP broadband modem/router (In France : Box-FAI)
66
INTIF=''								# INTIF is connected to the consultation network
1148 crox53 67
MTU="1500"
1243 richard 68
DEFAULT_PRIVATE_IP_MASK="192.168.182.1/24"	# Default ALCASAR IP address
1 root 69
# ****** Paths - chemin des commandes *******
70
SED="/bin/sed -i"
71
# ****************** End of global parameters *********************
72
 
2724 rexy 73
license()
959 franck 74
{
75
	if [ $Lang == "fr" ]
1538 richard 76
	then
77
		cat $DIR_INSTALL/gpl-warning.fr.txt | more
78
	else
79
		cat $DIR_INSTALL/gpl-warning.txt | more
959 franck 80
	fi
1538 richard 81
	response=0
2760 lucas.echa 82
	PTN='^[oOyYnN]?$'
83
	until [[ "$response" =~ $PTN ]]
1538 richard 84
	do
85
		if [ $Lang == "fr" ]
1563 franck 86
			then echo -n "Acceptez-vous les termes de cette licence (O/n)? : "
1538 richard 87
			else echo -n "Do you accept the terms of this license (Y/n)? : "
88
		fi
89
		read response
90
	done
91
	if [ "$response" = "n" ] || [ "$response" = "N" ]
92
	then
93
		exit 1
94
	fi
2724 rexy 95
} # End of license()
959 franck 96
 
2724 rexy 97
header_install()
1 root 98
{
99
	clear
100
	echo "-----------------------------------------------------------------------------"
460 richard 101
	echo "                     ALCASAR V$VERSION Installation"
1 root 102
	echo "Application Libre pour le Contrôle d'Accès Sécurisé et Authentifié au Réseau"
103
	echo "-----------------------------------------------------------------------------"
2724 rexy 104
} # End of header_install()
1 root 105
 
2552 rexy 106
########################################################
2870 rexy 107
##              Function "testing_system"             ##
2552 rexy 108
## - Test Mageia version                              ##
109
## - Test ALCASAR version (if already installed)      ##
110
## - Test free space on /var  (>10G)                  ##
111
## - Test Internet access                             ##
112
########################################################
2870 rexy 113
testing_system()
29 richard 114
{
1529 richard 115
# Test of Mageia version
116
# extract the current Mageia version and hardware architecture (i586 ou X64)
117
	fic=`cat /etc/product.id`
118
	unknown_os=0
119
	old="$IFS"
120
	IFS=","
121
	set $fic
2688 lucas.echa 122
	for i in "$@"
1529 richard 123
	do
124
		if [ "`echo $i|grep distribution|cut -d'=' -f1`" == "distribution" ]
2454 tom.houday 125
			then
1529 richard 126
			DISTRIBUTION=`echo $i|cut -d"=" -f2`
127
			unknown_os=`expr $unknown_os + 1`
128
		fi
129
		if [ "`echo $i|grep version|cut -d'=' -f1`" == "version" ]
2454 tom.houday 130
			then
1529 richard 131
			CURRENT_VERSION=`echo $i|cut -d"=" -f2`
132
			unknown_os=`expr $unknown_os + 1`
133
		fi
134
		if [ "`echo $i|grep arch|cut -d'=' -f1`" == "arch" ]
2454 tom.houday 135
			then
1529 richard 136
			ARCH=`echo $i|cut -d"=" -f2`
137
			unknown_os=`expr $unknown_os + 1`
138
		fi
139
	done
2669 tom.houday 140
	if [ "$ARCH" != "x86_64" ]
2149 richard 141
		then
142
		if [ $Lang == "fr" ]
2669 tom.houday 143
			then echo "Votre architecture matérielle doit être en 64bits"
144
			else echo "You hardware architecture must be 64bits"
2149 richard 145
		fi
2482 lucas.echa 146
		exit 1
2149 richard 147
	fi
1529 richard 148
	IFS="$old"
2757 rexy 149
	if [[ ( $unknown_os != 3 ) || ("$DISTRIBUTION" != "Mageia" ) || ( "$CURRENT_VERSION" != "7" ) ]]
2688 lucas.echa 150
	then
2669 tom.houday 151
		if [ -e /var/tmp/alcasar-conf.tar.gz ] # update
152
			then
153
			echo
154
			if [ $Lang == "fr" ]
155
				then
156
				echo "La mise à jour automatique d'ALCASAR ne peut pas être réalisée."
157
				echo "1 - Effectuez une sauvegarde des fichiers de traçabilité et de la base des usagers via l'ACC"
2757 rexy 158
				echo "2 - Installez Linux-Mageia 7.1 (64bits) et ALCASAR (cf. doc d'installation)"
2669 tom.houday 159
				echo "3 - Importez votre base des usagers"
160
			else
161
				echo "The automatic update of ALCASAR can't be performed."
162
				echo "1 - Save your traceability files and the user database"
2757 rexy 163
				echo "2 - Install Linux-Mageia 7.1 (64bits) & ALCASAR (cf. installation doc)"
2669 tom.houday 164
				echo "3 - Import your users database"
165
			fi
166
		else
167
			if [ $Lang == "fr" ]
168
				then echo "L'installation d'ALCASAR ne peut pas être réalisée."
169
				else echo "The installation of ALCASAR can't be performed."
170
			fi
171
		fi
172
		echo
173
		if [ $Lang == "fr" ]
2757 rexy 174
			then echo "Le système d'exploitation doit être remplacé (Mageia7.1-64bits)"
175
			else echo "The OS must be replaced (Mageia7.1-64bits)"
2669 tom.houday 176
		fi
2688 lucas.echa 177
		exit 1
2669 tom.houday 178
	fi
179
 
1362 richard 180
# Test if ALCASAR is already installed
181
	if [ -e $CONF_FILE ]
182
	then
2396 tom.houday 183
		current_version=`grep ^VERSION= $CONF_FILE | cut -d"=" -f2`
1342 richard 184
		if [ $Lang == "fr" ]
2669 tom.houday 185
			then echo "La version $current_version d'ALCASAR est déjà installée"
186
			else echo "ALCASAR version $current_version is already installed"
1342 richard 187
		fi
1362 richard 188
		response=0
2458 richard 189
		PTN='^[12]$'
2760 lucas.echa 190
		until [[ "$response" =~ $PTN ]]
1362 richard 191
		do
192
			if [ $Lang == "fr" ]
2669 tom.houday 193
				then echo -n "Tapez '1' pour une mise à jour; Tapez '2' pour une réinstallation : "
194
				else echo -n "Hit '1' for an update; Hit '2' for a reinstallation : "
2499 tom.houday 195
			fi
1362 richard 196
			read response
197
		done
2458 richard 198
		if [ "$response" = "2" ]
1362 richard 199
		then
2560 rexy 200
			rm -f /var/tmp/alcasar-conf*
1362 richard 201
		else
2870 rexy 202
# Create the archive of conf files
1362 richard 203
			$DIR_SCRIPTS/alcasar-conf.sh --create
2970 rexy 204
# avoid lack of local DNS resolution when unbound is updated (alcasar <= V5.3)
205
			local_unbound_unit=`ls /etc/systemd/system/unbound* 2>/dev/null | wc -w`
206
			if [ $local_unbound_unit -ne 4 ]; then
207
				cp -f /lib/systemd/system/unbound*.service /etc/systemd/system/
208
			fi
1362 richard 209
			mode="update"
210
		fi
1529 richard 211
	fi
2847 rexy 212
# Free /var (when updating) and test free space
213
	[ -d /var/log/netflow ] && rm -rf /var/log/netflow  # remove old porttracker RRD database
2850 rexy 214
	[ -d /var/lib/clamav ] && rm -rf /var/lib/clamav/* # remove old clamav database
2847 rexy 215
	journalctl -q --vacuum-files 1  # remove previous journal logs
2771 rexy 216
	free_space=`df -BG --output=avail /var|tail -1|tr -d '[:space:]G'`
217
	if [ $free_space -lt 10 ]
1529 richard 218
		then
2771 rexy 219
		if [ $Lang == "fr" ]
2847 rexy 220
			then echo "Espace disponible insuffisant sur /var ($free_space Go au lieu de 10 Go au minimum)"
2771 rexy 221
			else echo "not enough free space on /var ($free_space GB instead of at least 10 GB)"
1529 richard 222
		fi
2771 rexy 223
	exit 0
1529 richard 224
	fi
2870 rexy 225
} # End of testing_system
2669 tom.houday 226
 
2870 rexy 227
########################################################
228
##             Function "testing_network"             ##
229
## - Test Internet access                             ##
230
########################################################
231
testing_network()
232
{
2669 tom.houday 233
# Detect external/internal interfaces
234
	if [ -z "$EXTIF" ]; then
2981 rexy 235
		EXTIF=$(/usr/sbin/ip route list | awk '/ via / {print $5}' | uniq)
2669 tom.houday 236
		if [ -z "$EXTIF" ]; then
237
			if [ "$Lang" == 'fr' ]
2771 rexy 238
				then echo "Aucune passerelle par défaut configurée"
239
				else echo "No default gateway configured"
2669 tom.houday 240
			fi
241
			exit 1
242
		fi
243
	fi
244
	if [ "$Lang" == 'fr' ]
245
		then echo "Interface externe (Internet) utilisée : $EXTIF"
246
		else echo "External interface (Internet) used: $EXTIF"
247
	fi
248
 
249
	if [ -z "$INTIF" ]; then
250
		interfacesList=$(/usr/sbin/ip -br link show | cut -d' ' -f1 | grep -v "^\(lo\|tun0\|$EXTIF\)\$")
2724 rexy 251
		interfacesCount=$(echo "$interfacesList" | wc -w)
2669 tom.houday 252
		if [ $interfacesCount -eq 0 ]; then
253
			if [ "$Lang" == 'fr' ]
254
				then echo "Aucune interface de disponible pour le réseau interne"
255
				else echo "No interface available for the internal network"
256
			fi
257
			exit 1
258
		elif [ $interfacesCount -eq 1 ]; then
259
			INTIF="$interfacesList"
260
		else
261
			interfacesSorted=$(/usr/sbin/ip -br addr | grep -v "^\(lo\|tun0\|$EXTIF\) " | sort -b -k3n -k2r -k1)
262
			interfacePreferred=$(echo "$interfacesSorted" | head -1 | cut -d' ' -f1)
263
			if [ "$Lang" == 'fr' ]
264
				then echo 'Liste des interfaces disponible :'
265
				else echo 'List of available interfaces:'
266
			fi
267
			echo "$interfacesSorted"
268
			response=''
269
			while true; do
270
				if [ "$Lang" == 'fr' ]
271
					then echo -n "Choix de l'interface interne ? [$interfacePreferred] "
272
					else echo -n "Choice of internal interface ? [$interfacePreferred] "
273
				fi
274
				read response
275
 
276
				[ -z "$response" ] && response="$interfacePreferred"
277
 
278
				# Check if interface exist
2688 lucas.echa 279
				if [ "$(echo "$interfacesList" | grep -c "^$response\$")" -eq 1 ]; then
2669 tom.houday 280
					INTIF="$response"
281
					break
282
				else
283
					if [ "$Lang" == 'fr' ]
284
						then echo "Interface \"$response\" introuvable"
285
						else echo "Interface \"$response\" not found"
286
					fi
287
				fi
288
			done
289
		fi
290
	fi
291
	if [ "$Lang" == 'fr' ]
292
		then echo "Interface interne utilisée : $INTIF"
293
		else echo "Internal interface used: $INTIF"
294
	fi
295
 
2290 richard 296
	if [ $Lang == "fr" ]
297
		then echo -n "Tests des paramètres réseau : "
2549 tom.houday 298
		else echo -n "Network parameters tests: "
2290 richard 299
	fi
300
# Remove conf file if NIC is not plugged (ie : GSM/WIFI/Bt dongles)
2688 lucas.echa 301
	cd /etc/sysconfig/network-scripts/ || { echo "Unable to find /etc/sysconfig/network-scripts directory"; exit 1; }
2290 richard 302
	IF_INTERFACES=`ls ifcfg-*|cut -d"-" -f2|grep -v "^lo"|cut -d"*" -f1`
2282 richard 303
	for i in $IF_INTERFACES
304
	do
2688 lucas.echa 305
		if [ "$(/usr/sbin/ip link | grep -c " $i:")" -eq 0 ]; then
2282 richard 306
			rm -f ifcfg-$i
2454 tom.houday 307
 
2282 richard 308
			if [ $Lang == "fr" ]
309
				then echo "Suppression : ifcfg-$i"
2549 tom.houday 310
				else echo "Deleting: ifcfg-$i"
2282 richard 311
			fi
312
		fi
313
	done
2688 lucas.echa 314
	cd $DIR_INSTALL || { echo "Unable to find $DIR_INSTALL directory"; exit 1; }
2290 richard 315
	echo -n "."
2454 tom.houday 316
# Test Ethernet NIC links state
2669 tom.houday 317
	interfacesDown=$(/usr/sbin/ip -br link | grep "^\($EXTIF\|$INTIF\) " | grep 'NO-CARRIER' | cut -d' ' -f1)
318
	if [ ! -z "$interfacesDown" ]; then
319
		for i in $interfacesDown; do
320
			if [ $Lang == "fr" ]
321
			then
322
				echo -e "\nÉchec"
323
				echo "Le lien réseau de la carte $i n'est pas actif."
324
				echo "Assurez-vous que cette carte est bien connectée à un équipement (commutateur, A.P., etc.)"
325
			else
326
				echo -e "\nFailed"
327
				echo "The link state of $i interface is down."
328
				echo "Make sure that this network card is connected to a switch or an A.P."
329
			fi
330
		done
331
		exit 1
332
	fi
1471 richard 333
	echo -n "."
334
# Test EXTIF config files
2681 tom.houday 335
	PUBLIC_IP_MASK=`/usr/sbin/ip addr show $EXTIF | grep '^\s*inet\s' | awk '{ print $2 }'`
336
	PUBLIC_IP=`echo $PUBLIC_IP_MASK | cut -d'/' -f1`
2981 rexy 337
	PUBLIC_GATEWAY=`/usr/sbin/ip route list | awk -v EXTIF="$EXTIF" '(/ via / && $5 == EXTIF) {print $3}' | head -n 1`
2688 lucas.echa 338
	if [ "$(echo $PUBLIC_IP|wc -c)" -lt 7 ] || [ "$(echo $PUBLIC_GATEWAY|wc -c)" -lt 7 ]
1471 richard 339
	then
784 richard 340
		if [ $Lang == "fr" ]
2454 tom.houday 341
		then
2669 tom.houday 342
			echo -e "\nÉchec"
784 richard 343
			echo "La carte réseau connectée à Internet ($EXTIF) n'est pas correctement configurée."
344
			echo "Renseignez les champs suivants dans le fichier '/etc/sysconfig/network-scripts/ifcfg-$EXTIF' :"
1362 richard 345
			echo "Appliquez les changements : 'systemctl restart network'"
784 richard 346
		else
2669 tom.houday 347
			echo -e "\nFailed"
784 richard 348
			echo "The Internet connected network card ($EXTIF) isn't well configured."
349
			echo "The folowing parametres must be set in the file '/etc/sysconfig/network-scripts/ifcfg-$EXTIF' :"
2669 tom.houday 350
			echo "Apply the new configuration: 'systemctl restart network'"
784 richard 351
		fi
830 richard 352
		echo "DEVICE=$EXTIF"
784 richard 353
		echo "IPADDR="
354
		echo "NETMASK="
355
		echo "GATEWAY="
356
		echo "DNS1="
357
		echo "DNS2="
830 richard 358
		echo "ONBOOT=yes"
2669 tom.houday 359
		exit 1
784 richard 360
	fi
361
	echo -n "."
2290 richard 362
# Test if default GW is alive
1499 richard 363
	arp_reply=`/usr/sbin/arping -b -I$EXTIF -c1 -w2 $PUBLIC_GATEWAY|grep response|cut -d" " -f2`
2688 lucas.echa 364
	if [ "$(expr $arp_reply)" -eq 0 ]
2454 tom.houday 365
		then
595 richard 366
		if [ $Lang == "fr" ]
2454 tom.houday 367
		then
2669 tom.houday 368
			echo -e "\nÉchec"
2290 richard 369
			echo "Le routeur de sortie ou la Box Internet ($PUBLIC_GATEWAY) ne répond pas."
595 richard 370
			echo "Réglez ce problème puis relancez ce script."
371
		else
2669 tom.houday 372
			echo -e "\nFailed"
2290 richard 373
			echo "The Internet gateway or the ISP equipment ($PUBLIC_GATEWAY) doesn't answered."
595 richard 374
			echo "Resolv this problem, then restart this script."
375
		fi
2669 tom.houday 376
		exit 1
308 richard 377
	fi
378
	echo -n "."
2290 richard 379
# Test Internet connectivity
2669 tom.houday 380
	domainTested='www.google.com'
381
	/usr/bin/curl -s --head "$domainTested" &>/dev/null
382
	if [ $? -ne 0 ]; then
595 richard 383
		if [ $Lang == "fr" ]
2454 tom.houday 384
		then
2669 tom.houday 385
			echo -e "\nLa tentative de connexion vers Internet a échoué ($domainTested)."
595 richard 386
			echo "Vérifiez que la carte $EXTIF est bien connectée au routeur du FAI."
387
			echo "Vérifiez la validité des adresses IP des DNS."
388
		else
2669 tom.houday 389
			echo -e "\nThe Internet connection try failed ($domainTested)."
595 richard 390
			echo "Please, verify that the $EXTIF card is connected with the Internet gateway."
391
			echo "Verify the DNS IP addresses"
392
		fi
2669 tom.houday 393
		exit 1
29 richard 394
	fi
308 richard 395
	echo ". : ok"
2870 rexy 396
} # End of testing_network()
302 richard 397
 
2552 rexy 398
#######################################################################
399
##                    Function "init"                                ##
400
## - Creation of ALCASAR conf file "/usr/local/etc/alcasar.conf      ##
401
## - Creation of random password for GRUB, mariadb (admin and user)  ##
402
#######################################################################
2724 rexy 403
init()
302 richard 404
{
527 richard 405
	if [ "$mode" != "update" ]
302 richard 406
	then
407
# On affecte le nom d'organisme
597 richard 408
		header_install
302 richard 409
		ORGANISME=!
410
		PTN='^[a-zA-Z0-9-]*$'
2760 lucas.echa 411
		until [[ "$ORGANISME" =~ $PTN ]]
2454 tom.houday 412
		do
595 richard 413
			if [ $Lang == "fr" ]
2454 tom.houday 414
				then echo -n "Entrez le nom de votre organisme : "
597 richard 415
				else echo -n "Enter the name of your organism : "
595 richard 416
			fi
330 franck 417
			read ORGANISME
613 richard 418
			if [ "$ORGANISME" == "" ]
2688 lucas.echa 419
			then
330 franck 420
				ORGANISME=!
421
			fi
422
		done
302 richard 423
	fi
1 root 424
# On crée aléatoirement les mots de passe et les secrets partagés
2419 richard 425
# We create random passwords and shared secrets
628 richard 426
	rm -f $PASSWD_FILE
2419 richard 427
	echo "#####  ALCASAR ($ORGANISME) security passwords  #####" > $PASSWD_FILE
2688 lucas.echa 428
	grub2pwd=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c8`
2454 tom.houday 429
	pbkdf2=`( echo $grub2pwd ; echo $grub2pwd ) | \
430
		LC_ALL=C /usr/bin/grub2-mkpasswd-pbkdf2 | \
431
		grep -v '[eE]nter password:' | \
432
		sed -e "s/PBKDF2 hash of your password is //"`
433
	echo "GRUB2_PASSWORD=$pbkdf2" > /boot/grub2/user.cfg
434
	[ -e /root/grub.default ] || cp /etc/grub.d/10_linux /root/grub.default
435
	cp -f $DIR_CONF/grub-10_linux /etc/grub.d/10_linux  # Request password only on menu editing attempts (not when selecting an entry)
436
	chmod 0600 /boot/grub2/user.cfg
2419 richard 437
	echo "# Login name and password to protect GRUB2 boot menu (!!!qwerty keyboard) : " > $PASSWD_FILE
2454 tom.houday 438
	echo "GRUB2_user=root" >> $PASSWD_FILE
439
	echo "GRUB2_password=$grub2pwd" >> $PASSWD_FILE
2688 lucas.echa 440
	mysqlpwd=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c16`
2419 richard 441
	echo "# Login name and Password of MariaDB administrator:" >> $PASSWD_FILE
2412 tom.houday 442
	echo "db_root=$mysqlpwd" >> $PASSWD_FILE
2688 lucas.echa 443
	radiuspwd=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c16`
2419 richard 444
	echo "# Login name and password of MariaDB user:" >> $PASSWD_FILE
2421 richard 445
	echo "db_user=$DB_USER" >> $PASSWD_FILE
446
	echo "db_password=$radiuspwd" >> $PASSWD_FILE
2688 lucas.echa 447
	secretuam=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c16`
2412 tom.houday 448
	echo "# Shared secret between the script 'intercept.php' and coova-chilli:" >> $PASSWD_FILE
449
	echo "secret_uam=$secretuam" >> $PASSWD_FILE
2688 lucas.echa 450
	secretradius=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c16`
2412 tom.houday 451
	echo "# Shared secret between coova-chilli and FreeRadius:" >> $PASSWD_FILE
452
	echo "secret_radius=$secretradius" >> $PASSWD_FILE
628 richard 453
	chmod 640 $PASSWD_FILE
1828 richard 454
#  copy scripts in in /usr/local/bin
2664 tom.houday 455
	cp -fr $DIR_SCRIPTS/alcasar* $DIR_DEST_BIN/. ; chown -R root:root $DIR_DEST_BIN/alcasar* ; chmod -R 740 $DIR_DEST_BIN/alcasar*
1828 richard 456
#  copy conf files in /usr/local/etc
1954 richard 457
	cp -f $DIR_CONF/etc/alcasar* $DIR_DEST_ETC/. ; chown -R root:apache $DIR_DEST_ETC ; chmod 770 $DIR_DEST_ETC ; chmod 660 $DIR_DEST_ETC/alcasar*
1828 richard 458
	$SED "s?^DB_RADIUS=.*?DB_RADIUS=\"$DB_RADIUS\"?g" $DIR_DEST_BIN/alcasar-mysql.sh
628 richard 459
# generate central conf file
460
	cat <<EOF > $CONF_FILE
612 richard 461
##########################################
462
##                                      ##
463
##          ALCASAR Parameters          ##
464
##                                      ##
465
##########################################
1 root 466
 
612 richard 467
INSTALL_DATE=$DATE
468
VERSION=$VERSION
469
ORGANISM=$ORGANISME
470
EOF
628 richard 471
	chmod o-rwx $CONF_FILE
2724 rexy 472
} # End of init()
1 root 473
 
2552 rexy 474
#########################################################
475
##                    Function "network"               ##
476
## - Define the several network address                ##
477
## - Define the DNS naming                             ##
478
## - INTIF parameters (consultation network)           ##
479
## - Write "/etc/hosts" file                           ##
480
## - write "hosts.allow" & "hosts.deny" files          ##
481
#########################################################
2724 rexy 482
network()
1 root 483
{
484
	header_install
636 richard 485
	if [ "$mode" != "update" ]
486
		then
487
		if [ $Lang == "fr" ]
488
			then echo "Par défaut, l'adresse IP d'ALCASAR sur le réseau de consultation est : $DEFAULT_PRIVATE_IP_MASK"
489
			else echo "The default ALCASAR IP address on consultation network is : $DEFAULT_PRIVATE_IP_MASK"
490
		fi
491
		response=0
2760 lucas.echa 492
		PTN='^[oOyYnN]?$'
493
		until [[ "$response" =~ $PTN ]]
1 root 494
		do
595 richard 495
			if [ $Lang == "fr" ]
659 richard 496
				then echo -n "Voulez-vous utiliser cette adresse et ce plan d'adressage (recommandé) (O/n)? : "
618 richard 497
				else echo -n "Do you want to use this IP address and this IP addressing plan (recommanded) (Y/n)? : "
595 richard 498
			fi
1 root 499
			read response
500
		done
636 richard 501
		if [ "$response" = "n" ] || [ "$response" = "N" ]
502
		then
503
			PRIVATE_IP_MASK="0"
504
			PTN='^\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\)/[012]\?[[:digit:]]$'
2760 lucas.echa 505
			until [[ $(expr "$PRIVATE_IP_MASK" : $PTN) -gt 0 ]]
1 root 506
			do
595 richard 507
				if [ $Lang == "fr" ]
597 richard 508
					then echo -n "Entrez l'adresse IP d'ALCASAR au format CIDR (a.b.c.d/xx) : "
509
					else echo -n "Enter ALCASAR IP address in CIDR format (a.b.c.d/xx) : "
595 richard 510
				fi
597 richard 511
				read PRIVATE_IP_MASK
1 root 512
			done
636 richard 513
		else
2688 lucas.echa 514
			PRIVATE_IP_MASK=$DEFAULT_PRIVATE_IP_MASK
636 richard 515
		fi
595 richard 516
	else
2834 rexy 517
		PRIVATE_IP_MASK=`grep ^PRIVATE_IP= /var/tmp/conf/etc/alcasar.conf|cut -d"=" -f2`
518
		rm -rf /var/tmp/conf
1 root 519
	fi
861 richard 520
# Define LAN side global parameters
1740 richard 521
	hostnamectl set-hostname $HOSTNAME.$DOMAIN
977 richard 522
	PRIVATE_NETWORK=`/bin/ipcalc -n $PRIVATE_IP_MASK | cut -d"=" -f2`				# private network address (ie.: 192.168.182.0)
1499 richard 523
	private_network_ending=`echo $PRIVATE_NETWORK | cut -d"." -f4`					# last octet of LAN address
977 richard 524
	PRIVATE_NETMASK=`/bin/ipcalc -m $PRIVATE_IP_MASK | cut -d"=" -f2`				# private network mask (ie.: 255.255.255.0)
1499 richard 525
	PRIVATE_PREFIX=`/bin/ipcalc -p $PRIVATE_IP_MASK |cut -d"=" -f2`					# network prefix (ie. 24)
977 richard 526
	PRIVATE_IP=`echo $PRIVATE_IP_MASK | cut -d"/" -f1`						# ALCASAR private ip address (consultation LAN side)
1499 richard 527
	if [ $PRIVATE_IP == $PRIVATE_NETWORK ]								# when entering network address instead of ip address
2688 lucas.echa 528
	then
2454 tom.houday 529
		PRIVATE_IP=`echo $PRIVATE_NETWORK | cut -d"." -f1-3`"."`expr $private_network_ending + 1`
1499 richard 530
		PRIVATE_IP_MASK=`echo $PRIVATE_IP/$PRIVATE_PREFIX`
2454 tom.houday 531
	fi
1499 richard 532
	private_ip_ending=`echo $PRIVATE_IP | cut -d"." -f4`						# last octet of LAN address
533
	PRIVATE_SECOND_IP=`echo $PRIVATE_IP | cut -d"." -f1-3`"."`expr $private_ip_ending + 1`		# second network address (ex.: 192.168.182.2)
977 richard 534
	PRIVATE_NETWORK_MASK=$PRIVATE_NETWORK/$PRIVATE_PREFIX						# ie.: 192.168.182.0/24
1499 richard 535
	classe=$((PRIVATE_PREFIX/8))									# ie.: 2=classe B, 3=classe C
977 richard 536
	PRIVATE_NETWORK_SHORT=`echo $PRIVATE_NETWORK | cut -d"." -f1-$classe`.				# compatibility with hosts.allow et hosts.deny (ie.: 192.168.182.)
1828 richard 537
	PRIVATE_MAC=`/usr/sbin/ip link show $INTIF | grep ether | cut -d" " -f6| sed 's/:/-/g'| awk '{print toupper($0)}'` 	# MAC address of INTIF
841 richard 538
# Define Internet parameters
2809 rexy 539
	DNS1=`cat /etc/sysconfig/network-scripts/ifcfg-$EXTIF | grep '^DNS1='| cut -d"=" -f2`	# 1st DNS server
540
	DNS2=`cat /etc/sysconfig/network-scripts/ifcfg-$EXTIF | grep '^DNS2=' | cut -d"=" -f2`	# 2nd DNS server
2870 rexy 541
	DNS1=${DNS1:=208.67.220.220}
70 franck 542
	DNS2=${DNS2:=208.67.222.222}
1499 richard 543
	PUBLIC_NETMASK=`/bin/ipcalc -m $PUBLIC_IP_MASK | cut -d"=" -f2`
1052 richard 544
	PUBLIC_PREFIX=`/bin/ipcalc -p $PUBLIC_IP $PUBLIC_NETMASK|cut -d"=" -f2`
1069 richard 545
	PUBLIC_NETWORK=`/bin/ipcalc -n $PUBLIC_IP/$PUBLIC_PREFIX|cut -d"=" -f2`
2552 rexy 546
# Write network parameters in the conf file
2737 rexy 547
	echo "HOSTNAME=$HOSTNAME" >> $CONF_FILE
548
	echo "DOMAIN=$DOMAIN" >> $CONF_FILE
1469 richard 549
	echo "EXTIF=$EXTIF" >> $CONF_FILE
550
	echo "INTIF=$INTIF" >> $CONF_FILE
2770 rexy 551
# Retrieve NIC name of other consultation LAN
2282 richard 552
	INTERFACES=`/usr/sbin/ip link|grep '^[[:digit:]]:'|grep -v "^lo\|$EXTIF\|tun0"|cut -d " " -f2|tr -d ":"`
553
	for i in $INTERFACES
554
	do
555
		SUB=`echo ${i:0:2}`
556
		if [ $SUB = "wl" ]
557
			then WIFIF=$i
2454 tom.houday 558
		elif [ "$i" != "$INTIF" ] && [ $SUB != "ww" ]
2282 richard 559
			then LANIF=$i
560
		fi
561
	done
562
	if [ -n "$WIFIF" ]
563
		then echo "WIFIF=$WIFIF" >> $CONF_FILE
564
	elif [ -n "$LANIF" ]
565
		then echo "LANIF=$LANIF" >> $CONF_FILE
566
	fi
2552 rexy 567
	IP_SETTING=`grep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-$EXTIF|cut -d"=" -f2` # test static or dynamic
1499 richard 568
	if [ $IP_SETTING == "dhcp" ]
2688 lucas.echa 569
	then
2947 rexy 570
		DHCP_DNS_servers=`cat /var/lib/dhclient/dhclient--$EXTIF.lease |grep domain-name-servers|sed -n "1 p"| rev|cut -d" " -f1|rev|tr -d ';'`
571
		DNS1=`echo $DHCP_DNS_servers | cut -d"," -f1`
572
		DNS2=`echo $DHCP_DNS_servers | cut -d"," -f2`
1499 richard 573
		echo "PUBLIC_IP=dhcp" >> $CONF_FILE
1585 richard 574
		echo "GW=dhcp" >> $CONF_FILE
1499 richard 575
	else
576
		echo "PUBLIC_IP=$PUBLIC_IP/$PUBLIC_PREFIX" >> $CONF_FILE
1585 richard 577
		echo "GW=$PUBLIC_GATEWAY" >> $CONF_FILE
1499 richard 578
	fi
1587 richard 579
	echo "DNS1=$DNS1" >> $CONF_FILE
580
	echo "DNS2=$DNS2" >> $CONF_FILE
2956 rexy 581
	echo "PROXY=off" >> $CONF_FILE
582
	echo "PROXY_IP=\"192.168.0.100:80\"" >> $CONF_FILE
583
	echo "PUBLIC_WEIGHT=1" >> $CONF_FILE
994 franck 584
	echo "PUBLIC_MTU=$MTU" >> $CONF_FILE
628 richard 585
	echo "PRIVATE_IP=$PRIVATE_IP_MASK" >> $CONF_FILE
1484 richard 586
	echo "DHCP=on" >> $CONF_FILE
2708 tom.houday 587
	echo "EXT_DHCP_IP=" >> $CONF_FILE
588
	echo "RELAY_DHCP_IP=" >> $CONF_FILE
589
	echo "RELAY_DHCP_PORT=" >> $CONF_FILE
2709 tom.houday 590
	echo "INT_DNS_DOMAIN=" >> $CONF_FILE
591
	echo "INT_DNS_IP=" >> $CONF_FILE
1610 franck 592
	echo "INT_DNS_ACTIVE=off" >> $CONF_FILE
1499 richard 593
# network default
597 richard 594
	[ -e /etc/sysconfig/network.default ] || cp /etc/sysconfig/network /etc/sysconfig/network.default
1 root 595
	cat <<EOF > /etc/sysconfig/network
596
NETWORKING=yes
597
FORWARD_IPV4=true
598
EOF
2552 rexy 599
# write "/etc/hosts"
1 root 600
	[ -e /etc/hosts.default ] || cp /etc/hosts /etc/hosts.default
601
	cat <<EOF > /etc/hosts
503 richard 602
127.0.0.1	localhost
2558 rexy 603
$PRIVATE_IP	$HOSTNAME
1 root 604
EOF
2552 rexy 605
# write EXTIF (Internet) config
1499 richard 606
	[ -e /etc/sysconfig/network-scripts/default-ifcfg-$EXTIF ] || cp /etc/sysconfig/network-scripts/ifcfg-$EXTIF /etc/sysconfig/network-scripts/default-ifcfg-$EXTIF
607
	if [ $IP_SETTING == "dhcp" ]
2688 lucas.echa 608
	then
1499 richard 609
		cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$EXTIF
14 richard 610
DEVICE=$EXTIF
1585 richard 611
BOOTPROTO=dhcp
612
DNS1=127.0.0.1
613
PEERDNS=no
614
RESOLV_MODS=yes
615
ONBOOT=yes
1613 franck 616
NOZEROCONF=yes
1585 richard 617
METRIC=10
618
MII_NOT_SUPPORTED=yes
619
IPV6INIT=no
620
IPV6TO4INIT=no
621
ACCOUNTING=no
622
USERCTL=no
623
MTU=$MTU
624
EOF
2688 lucas.echa 625
	else
1585 richard 626
		cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$EXTIF
627
DEVICE=$EXTIF
14 richard 628
BOOTPROTO=static
597 richard 629
IPADDR=$PUBLIC_IP
630
NETMASK=$PUBLIC_NETMASK
631
GATEWAY=$PUBLIC_GATEWAY
2870 rexy 632
DNS1=$DNS1
633
DNS2=$DNS2
1499 richard 634
RESOLV_MODS=yes
14 richard 635
ONBOOT=yes
636
METRIC=10
1610 franck 637
NOZEROCONF=yes
14 richard 638
MII_NOT_SUPPORTED=yes
639
IPV6INIT=no
640
IPV6TO4INIT=no
641
ACCOUNTING=no
642
USERCTL=no
994 franck 643
MTU=$MTU
14 richard 644
EOF
1499 richard 645
	fi
2552 rexy 646
# write INTIF (consultation LAN) in normal mode
2868 rexy 647
cp -f /etc/sysconfig/network-scripts/ifcfg-$INTIF /etc/sysconfig/network-scripts/default-ifcfg-$INTIF
841 richard 648
	cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$INTIF
649
DEVICE=$INTIF
650
BOOTPROTO=static
651
ONBOOT=yes
652
NOZEROCONF=yes
653
MII_NOT_SUPPORTED=yes
654
IPV6INIT=no
655
IPV6TO4INIT=no
656
ACCOUNTING=no
657
USERCTL=no
658
EOF
2552 rexy 659
# write INTIF in bypass mode (see "alcasar-bypass.sh")
1554 richard 660
	cat <<EOF > /etc/sysconfig/network-scripts/bypass-ifcfg-$INTIF
1 root 661
DEVICE=$INTIF
662
BOOTPROTO=static
663
IPADDR=$PRIVATE_IP
604 richard 664
NETMASK=$PRIVATE_NETMASK
1 root 665
ONBOOT=yes
666
METRIC=10
667
NOZEROCONF=yes
668
MII_NOT_SUPPORTED=yes
14 richard 669
IPV6INIT=no
670
IPV6TO4INIT=no
671
ACCOUNTING=no
672
USERCTL=no
1 root 673
EOF
2282 richard 674
######### Config WIFIF (consultation WIFI) ou LANIF (consultation LAN) in normal mode #################
675
	if [ -n "$WIFIF" ] && [ "$WIFIF" != "$INTIF" ]
676
	then
677
		cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$WIFIF
678
DEVICE=$WIFIF
679
BOOTPROTO=static
680
ONBOOT=yes
681
NOZEROCONF=yes
682
MII_NOT_SUPPORTED=yes
683
IPV6INIT=no
684
IPV6TO4INIT=no
685
ACCOUNTING=no
686
USERCTL=no
687
EOF
688
	elif [ -n "$LANIF" ]
689
	then
690
		cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$LANIF
691
DEVICE=$LANIF
692
BOOTPROTO=static
693
ONBOOT=yes
694
NOZEROCONF=yes
695
MII_NOT_SUPPORTED=yes
696
IPV6INIT=no
697
IPV6TO4INIT=no
698
ACCOUNTING=no
699
USERCTL=no
700
EOF
701
	fi
2552 rexy 702
# write hosts.allow & hosts.deny
1 root 703
	[ -e /etc/hosts.allow.default ]  || cp /etc/hosts.allow /etc/hosts.allow.default
704
	cat <<EOF > /etc/hosts.allow
705
ALL: LOCAL, 127.0.0.1, localhost, $PRIVATE_IP
604 richard 706
sshd: ALL
1 root 707
ntpd: $PRIVATE_NETWORK_SHORT
708
EOF
709
	[ -e /etc/host.deny.default ]  || cp /etc/hosts.deny /etc/hosts.deny.default
710
	cat <<EOF > /etc/hosts.deny
711
ALL: ALL: spawn ( /bin/echo "service %d demandé par %c" | /bin/mail -s "Tentative d'accès au service %d par %c REFUSE !!!" security ) &
712
EOF
790 richard 713
	chmod o+r $DIR_DEST_BIN/alcasar-iptables.sh #lecture possible pour apache (interface php du filtrage réseau)
860 richard 714
# create the ip_blocked file with a first line (LAN between ALCASAR and the Internet GW)
1069 richard 715
	echo "#$PUBLIC_NETWORK/$PUBLIC_PREFIX LAN-ALCASAR-BOX" > $DIR_DEST_ETC/alcasar-ip-blocked
790 richard 716
# load conntrack ftp module
717
	[ -e /etc/modprobe.preload.default ] || cp /etc/modprobe.preload /etc/modprobe.preload.default
1705 richard 718
	echo "nf_conntrack_ftp" >>  /etc/modprobe.preload
1159 crox53 719
# load ipt_NETFLOW module
720
	echo "ipt_NETFLOW" >>  /etc/modprobe.preload
1513 richard 721
# modify iptables service files (start with "alcasar-iptables.sh" and stop with flush)
2937 rexy 722
	cp /lib/systemd/system/iptables.service /etc/systemd/system/iptables.service
723
	$SED 's/ExecStart=\/usr\/libexec\/iptables.init start/ExecStart=\/usr\/local\/bin\/alcasar-iptables.sh/' /etc/systemd/system/iptables.service
2688 lucas.echa 724
	[ -e /usr/libexec/iptables.init.default ] || cp /usr/libexec/iptables.init /usr/libexec/iptables.init.default
725
	$SED "s?\[ -f \$IPTABLES_CONFIG \] .*?#&?" /usr/libexec/iptables.init # comment the test (flush all rules & policies)
2956 rexy 726
# create the alcasar-network unit
727
	cat << EOF > /etc/systemd/system/alcasar-network.service
728
#  This file is part of systemd.
2454 tom.houday 729
#
2956 rexy 730
#  systemd is free software; you can redistribute it and/or modify it
731
#  under the terms of the GNU General Public License as published by
732
#  the Free Software Foundation; either version 2 of the License, or
733
#  (at your option) any later version.
734
 
735
# This unit starts alcasar-network.sh script.
736
[Unit]
737
Description=alcasar-network.sh execution
738
After=network.target iptables.service
739
 
740
[Service]
741
Type=oneshot
742
RemainAfterExit=yes
743
ExecStart=$DIR_DEST_BIN/alcasar-network.sh
744
ExecStop=$DIR_DEST_BIN/alcasar-network.sh
745
TimeoutSec=0
746
 
747
[Install]
748
WantedBy=multi-user.target
749
EOF
750
	/usr/bin/systemctl daemon-reload
751
 
752
# the script "$DIR_DEST_BIN/alcasar-iptables.sh" is started at the end of this script in order not to cut network flow in case of using ssh
2724 rexy 753
} # End of network()
1 root 754
 
2763 rexy 755
##################################################################
756
##                      Fonction "CA"                           ##
757
## - Creating the CA and the server certificate (lighttpd)      ##
758
##################################################################
759
CA()
760
{
761
	$DIR_DEST_BIN/alcasar-CA.sh
2814 rexy 762
	chmod 755 /etc/pki/
2801 rexy 763
	chown root:apache /etc/pki/CA; chmod 750 /etc/pki/CA
2821 rexy 764
	chown root:apache /etc/pki/CA/alcasar-ca.crt; chmod 640 /etc/pki/CA/alcasar-ca.crt
2811 rexy 765
	chown root:root /etc/pki/CA/private; chmod 700 /etc/pki/CA/private
766
	chmod 600 /etc/pki/CA/private/*
767
	chown -R root:apache /etc/pki/tls/private; chmod 750 /etc/pki/tls/private
768
	chmod 640 /etc/pki/tls/private/*
2814 rexy 769
	chmod 644 /etc/pki/tls/certs/* # "freshclam" need to access to that bundle
2763 rexy 770
} # End of CA()
771
 
2552 rexy 772
###################################################
773
##                  Function "ACC"               ##
774
## - copy ALCASAR Control Center (ACC) files     ##
775
## - configuration of the web server (Lighttpd)  ##
776
## - creation of the first ACC admin account     ##
777
## - secure the ACC access                       ##
778
###################################################
2724 rexy 779
ACC()
1 root 780
{
781
	[ -d $DIR_WEB ] && rm -rf $DIR_WEB
782
	mkdir $DIR_WEB
1833 richard 783
# Copy & adapt ACC files
316 richard 784
	cp -rf $DIR_INSTALL/web/* $DIR_WEB/
785
	$SED "s?99/99/9999?$DATE_SHORT?g" $DIR_ACC/menu.php
2815 rexy 786
	$SED "s?\$DB_RADIUS = .*?\$DB_RADIUS = \"$DB_RADIUS\"\;?g" $DIR_ACC/welcome.php
787
	$SED "s?\$DB_USER = .*?\$DB_USER = \"$DB_USER\"\;?g" $DIR_ACC/welcome.php
788
	$SED "s?\$radiuspwd = .*?\$radiuspwd = \"$radiuspwd\"\;?g" $DIR_ACC/welcome.php
5 franck 789
	chown -R apache:apache $DIR_WEB/*
1833 richard 790
# copy & adapt "freeradius-web" files
791
	cp -rf $DIR_CONF/freeradius-web/ /etc/
792
	[ -e /etc/freeradius-web/admin.conf.default ] || cp /etc/freeradius-web/admin.conf /etc/freeradius-web/admin.conf.default
793
	$SED "s?^general_domain:.*?general_domain: $DOMAIN?g" /etc/freeradius-web/admin.conf
794
	$SED "s?^sql_username:.*?sql_username: $DB_USER?g" /etc/freeradius-web/admin.conf
795
	$SED "s?^sql_password:.*?sql_password: $radiuspwd?g" /etc/freeradius-web/admin.conf
796
	cat <<EOF > /etc/freeradius-web/naslist.conf
797
nas1_name: alcasar-$ORGANISME
798
nas1_model: Network Access Controler
799
nas1_ip: $PRIVATE_IP
800
nas1_port_num: 0
801
nas1_community: public
802
EOF
803
	chown -R apache:apache /etc/freeradius-web/
804
# create the log & backup structure :
1489 richard 805
# - base = users database
806
# - archive = tarball of "base + http firewall + netflow"
1833 richard 807
# - security = watchdog log
2829 rexy 808
# - conf_file = archive conf file (usefull in updating process)
2887 rexy 809
	for i in base archive security activity_report iot_captures;
1 root 810
	do
811
		[ -d $DIR_SAVE/$i ] || mkdir -p $DIR_SAVE/$i
812
	done
5 franck 813
	chown -R root:apache $DIR_SAVE
1833 richard 814
# Configuring & securing php
2937 rexy 815
	[ -e /etc/php.d/05_date.ini.default ] || cp /etc/php.d/05_date.ini /etc/php.d/05_date.ini.default
2887 rexy 816
	timezone=`timedatectl show --property=Timezone|cut -d"=" -f2`
817
	$SED "s?^;date.timezone =.*?date.timezone = $timezone?g" /etc/php.d/05_date.ini
71 richard 818
	[ -e /etc/php.ini.default ] || cp /etc/php.ini /etc/php.ini.default
411 richard 819
	$SED "s?^upload_max_filesize.*?upload_max_filesize = 100M?g" /etc/php.ini
820
	$SED "s?^post_max_size.*?post_max_size = 100M?g" /etc/php.ini
2397 tom.houday 821
	$SED "s?^display_errors.*?display_errors = Off?" /etc/php.ini
822
	$SED "s?^display_startup_errors.*?display_startup_errors = Off?" /etc/php.ini
71 richard 823
	$SED "s?^html_errors.*?html_errors = Off?g" /etc/php.ini
824
	$SED "s?^expose_php.*?expose_php = Off?g" /etc/php.ini
2397 tom.houday 825
	$SED "s?^allow_url_fopen.*?allow_url_fopen = Off?" /etc/php.ini
2488 lucas.echa 826
# Configuring & securing Lighttpd
790 richard 827
	rm -rf /var/www/cgi-bin/* /var/www/perl/* /var/www/icons/README* /var/www/error/README*
2488 lucas.echa 828
	[ -e /etc/lighttpd/lighttpd.conf.default ] || cp /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.default
829
	$SED "s?^server\.use-ipv6.*?server\.use-ipv6 = \"disable\"?g" /etc/lighttpd/lighttpd.conf
2688 lucas.echa 830
	$SED "s?^#server\.bind.*?server\.bind = \"$PRIVATE_IP\"?g" /etc/lighttpd/lighttpd.conf
831
	$SED "s?^server\.bind.*?server\.bind = \"$PRIVATE_IP\"?g" /etc/lighttpd/lighttpd.conf
2488 lucas.echa 832
	$SED "s?^#server\.tag.*?server\.tag = \"\"?g" /etc/lighttpd/lighttpd.conf
833
	echo "include \"vhosts.d/alcasar.conf\"" >> /etc/lighttpd/lighttpd.conf
2592 rexy 834
 
835
	[ -e /etc/lighttpd/modules.conf.default ] || cp /etc/lighttpd/modules.conf /etc/lighttpd/modules.conf.default
2812 rexy 836
	$SED "s?^#[ ]*\"mod_auth\",.*?\"mod_auth\",?g" /etc/lighttpd/modules.conf
837
	$SED "s?^#[ ]*\"mod_alias\",.*?\"mod_alias\",?g" /etc/lighttpd/modules.conf
838
	$SED "s?^#[ ]*\"mod_redirect\",.*?\"mod_redirect\",?g" /etc/lighttpd/modules.conf
839
	$SED "/^[ ]*\"mod_redirect\",/a\"mod_openssl\"," /etc/lighttpd/modules.conf
2488 lucas.echa 840
	$SED "s?^#include \"conf.d/fastcgi.conf\".*?include \"conf.d/fastcgi.conf\"?g" /etc/lighttpd/modules.conf
841
 
2592 rexy 842
	[ -e /etc/lighttpd/conf.d/fastcgi.conf.default ] || cp /etc/lighttpd/conf.d/fastcgi.conf /etc/lighttpd/conf.d/fastcgi.conf.default
2739 rexy 843
	cp $DIR_CONF/lighttpd/conf.d/fastcgi.conf /etc/lighttpd/conf.d/fastcgi.conf
2592 rexy 844
 
845
	[ -e /etc/php-fpm.conf.default ] || cp /etc/php-fpm.conf /etc/php-fpm.conf.default
846
	$SED "s?^;listen\.owner.*?listen\.owner = apache?g" /etc/php-fpm.conf
847
	$SED "s?^;listen\.group.*?listen\.group = apache?g" /etc/php-fpm.conf
848
	$SED "s?^;listen\.mode.*?listen\.mode = 0660?g" /etc/php-fpm.conf
849
 
850
	[ -d /etc/lighttpd/vhosts.d ] || mkdir /etc/lighttpd/vhosts.d
851
	cp $DIR_CONF/lighttpd/vhosts.d/* /etc/lighttpd/vhosts.d/
2964 rexy 852
	$SED 's/^$SERVER\["socket"\] == ".*:443.*/$SERVER\["socket"\] == "'"$PRIVATE_IP"':443" {/g' /etc/lighttpd/vhosts.d/alcasar-with-ssl.conf /etc/lighttpd/vhosts.d/alcasar-without-ssl.conf
853
	$SED "s/^\([\t ]*\)var.server_name.*/\1var.server_name = \"$PRIVATE_IP\"/g" /etc/lighttpd/vhosts.d/alcasar-with-ssl.conf /etc/lighttpd/vhosts.d/alcasar-without-ssl.conf
2818 rexy 854
	ln -s /etc/lighttpd/vhosts.d/alcasar-without-ssl.conf /etc/lighttpd/vhosts.d/alcasar.conf
2592 rexy 855
 
2588 rexy 856
	[ -d /var/log/lighttpd ] || mkdir /var/log/lighttpd
857
	[ -e /var/log/lighttpd/access.log ] || touch /var/log/lighttpd/access.log
858
	[ -e /var/log/lighttpd/error.log ] || touch /var/log/lighttpd/error.log
2688 lucas.echa 859
 
2588 rexy 860
	chown -R apache:apache /var/log/lighttpd
2488 lucas.echa 861
 
2552 rexy 862
# Creation of the first account (in 'admin' profile)
2293 tom.houday 863
	if [ "$mode" = "install" ]
2688 lucas.echa 864
	then
865
		header_install
1268 richard 866
# Creation of keys file for the admin account ("admin")
2688 lucas.echa 867
		[ -d $DIR_DEST_ETC/digest ] && rm -rf $DIR_DEST_ETC/digest
868
		mkdir -p $DIR_DEST_ETC/digest
869
		chmod 755 $DIR_DEST_ETC/digest
2737 rexy 870
		if [ $Lang == "fr" ]
2760 lucas.echa 871
			then echo "Création du premier compte administrateur : "
872
			else echo "Creation of the first admin account : "
2737 rexy 873
		fi
2688 lucas.echa 874
		until [ -s $DIR_DEST_ETC/digest/key_admin ]
875
		do
876
			$DIR_DEST_BIN/alcasar-profil.sh --add admin
877
		done
2293 tom.houday 878
	fi
2818 rexy 879
# Creation of ACC certs links
880
	[ -d /var/www/html/certs ] || mkdir /var/www/html/certs
881
	ln -s /etc/pki/CA/alcasar-ca.crt /var/www/html/certs/certificat_alcasar_ca.crt
882
# Run lighttpd after coova (in order waiting tun0 to be up)
2937 rexy 883
	cp /lib/systemd/system/lighttpd.service /etc/systemd/system/lighttpd.service
884
	$SED "s?^After=.*?After=network.target remote-fs.target nss-lookup.target chilli.service?g" /etc/systemd/system/lighttpd.service
2293 tom.houday 885
	# Log file for ACC access imputability
2887 rexy 886
	[ -e $DIR_SAVE/security/acc_access.log ] || touch $DIR_SAVE/security/acc_access.log
887
	chown root:apache $DIR_SAVE/security/acc_access.log
888
	chmod 664 $DIR_SAVE/security/acc_access.log
2888 rexy 889
# Copy IEEE-MAC-manuf list (origin from sanitized nmac file : see linuxnet.ca)
890
    cp $DIR_CONF/nmap-mac-prefixes /usr/local/share/
2724 rexy 891
} # End of ACC()
1 root 892
 
2552 rexy 893
#############################################################
894
##               Function "time_server"                    ##
895
## - Configuring NTP server                                ##
896
#############################################################
2724 rexy 897
time_server()
1837 richard 898
{
899
# Set the Internet time server
900
	[ -e /etc/ntp/step-tickers.default ] || cp /etc/ntp/step-tickers /etc/ntp/step-tickers.default
901
	cat <<EOF > /etc/ntp/step-tickers
902
0.fr.pool.ntp.org	# adapt to your country
903
1.fr.pool.ntp.org
904
2.fr.pool.ntp.org
905
EOF
906
	[ -e /etc/ntp.conf.default ] || cp /etc/ntp.conf /etc/ntp.conf.default
907
	cat <<EOF > /etc/ntp.conf
908
server 0.fr.pool.ntp.org	# adapt to your country
909
server 1.fr.pool.ntp.org
910
server 2.fr.pool.ntp.org
911
server 127.127.1.0   		# local clock si NTP internet indisponible ...
912
fudge 127.127.1.0 stratum 10
913
restrict $PRIVATE_NETWORK mask $PRIVATE_NETMASK nomodify notrap
914
restrict 127.0.0.1
915
driftfile /var/lib/ntp/drift
916
logfile /var/log/ntp.log
917
disable monitor
918
EOF
919
	chown -R ntp:ntp /var/lib/ntp
920
# Synchronize now
2688 lucas.echa 921
	ntpd -4 -q -g &
2724 rexy 922
} # End of time_server()
1837 richard 923
 
2541 rexy 924
#####################################################################
925
##                     Function "init_db"                          ##
926
## - Mysql initialization                                          ##
927
## - Set admin (root) password                                     ##
928
## - Remove unused users & databases                               ##
929
## - Radius database creation                                      ##
930
## - Copy of accounting tables (mtotacct, totacct) & userinfo      ##
931
#####################################################################
2724 rexy 932
init_db()
1 root 933
{
2688 lucas.echa 934
	if [ "`systemctl is-active mysqld`" == "active" ]
1990 richard 935
	then
936
		systemctl stop mysqld
937
	fi
1355 richard 938
	rm -rf /var/lib/mysql # to be sure that there is no former installation
1 root 939
	[ -e /etc/my.cnf.default ] || cp /etc/my.cnf /etc/my.cnf.default
1355 richard 940
	$SED "s?^tmpdir.*?tmpdir=/tmp?g" /etc/my.cnf
1979 richard 941
	$SED "s?^port.*?#&?g" /etc/my.cnf # we use unix socket only
1980 richard 942
	$SED "s?^;collation_server =.*?collation_server = utf8_unicode_ci?g" /etc/my.cnf
943
	$SED "s?^;character_set_server =.*?character_set_server = utf8?g" /etc/my.cnf  # accentuated user names are allowed
2591 rexy 944
	[ -e /etc/my.cnf.d/feedback.cnf ] && $SED "s?^plugin-load.*?#&?g" /etc/my.cnf.d/feedback.cnf # remove the feedback plugin (ALCASAR doesn't report anything !)
2724 rexy 945
	[ -e /etc/my.cnf.d/auth_gssapi.cnf ] && $SED "s?^plugin-load.*?#&?g" /etc/my.cnf.d/auth_gssapi.cnf # remove GSS plugin (ALCASAR doesn't use Kerberos)
2416 richard 946
	/usr/sbin/mysqld-prepare-db-dir > /dev/null 2>&1
947
	/usr/bin/systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
948
	/usr/bin/systemctl start mysqld
1963 richard 949
	nb_round=1
1981 richard 950
	while [ ! -S /var/lib/mysql/mysql.sock ] && [ $nb_round -lt 10 ] # we wait until mariadb is on
1963 richard 951
	do
952
		nb_round=`expr $nb_round + 1`
953
		sleep 2
954
	done
1981 richard 955
	if [ ! -S /var/lib/mysql/mysql.sock ]
1963 richard 956
	then
1981 richard 957
		echo "Problème : la base données 'MariaDB' ne s'est pas lancée !"
1963 richard 958
		exit
1955 richard 959
	fi
1355 richard 960
# Secure the server
2688 lucas.echa 961
	/usr/bin/mysql --execute "GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$mysqlpwd';"
2416 richard 962
	MYSQL="/usr/bin/mysql -uroot -p$mysqlpwd --execute"
2688 lucas.echa 963
	$MYSQL "DROP DATABASE IF EXISTS test;DROP DATABASE IF EXISTS tmp;"
964
	$MYSQL "CONNECT mysql;DELETE from user where User='';DELETE FROM user WHERE User='root' AND Host NOT IN ('localhost','127.0.0.1','::1');FLUSH PRIVILEGES;"
615 richard 965
# Create 'radius' database
2688 lucas.echa 966
	$MYSQL "CREATE DATABASE IF NOT EXISTS $DB_RADIUS;GRANT ALL ON $DB_RADIUS.* TO $DB_USER@localhost IDENTIFIED BY '$radiuspwd';FLUSH PRIVILEGES;"
615 richard 967
# Add an empty radius database structure
2688 lucas.echa 968
	/usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_RADIUS < $DIR_CONF/empty-radiusd-db.sql
615 richard 969
# modify the start script in order to close accounting connexion when the system is comming down or up
2937 rexy 970
	cp /lib/systemd/system/mysqld.service /etc/systemd/system/mysqld.service
971
	$SED "/^ExecStart=/a ExecStop=$DIR_DEST_BIN/alcasar-mysql.sh -acct_stop" /etc/systemd/system/mysqld.service
972
	$SED "/^ExecStop=/a ExecStartPost=$DIR_DEST_BIN/alcasar-mysql.sh -acct_stop" /etc/systemd/system/mysqld.service
2416 richard 973
	/usr/bin/systemctl unset-environment MYSQLD_OPTS
1574 richard 974
	/usr/bin/systemctl daemon-reload
2724 rexy 975
} # End of init_db()
1 root 976
 
2423 richard 977
###################################################################
978
##                       Function "freeradius"                   ##
979
## - Set the configuration files                                 ##
980
## - Set the shared secret between coova-chilli and freeradius   ##
981
## - Adapt the Mysql conf file and counters                      ##
982
###################################################################
2724 rexy 983
freeradius()
1 root 984
{
1800 richard 985
	cp -f $DIR_CONF/empty-radiusd-db.sql /etc/raddb/
1 root 986
	chown -R radius:radius /etc/raddb
987
	[ -e /etc/raddb/radiusd.conf.default ] || cp /etc/raddb/radiusd.conf /etc/raddb/radiusd.conf.default
2420 richard 988
# Set radius global parameters (radius.conf)
1 root 989
	$SED "s?^[\t ]*#[\t ]*user =.*?user = radius?g" /etc/raddb/radiusd.conf
990
	$SED "s?^[\t ]*#[\t ]*group =.*?group = radius?g" /etc/raddb/radiusd.conf
991
	$SED "s?^[\t ]*status_server =.*?status_server = no?g" /etc/raddb/radiusd.conf
2420 richard 992
	$SED "s?^[\t ]*proxy_requests.*?proxy_requests = no?g" /etc/raddb/radiusd.conf # remove the proxy function
993
	$SED "s?^[\t ]*\$INCLUDE proxy.conf.*?#\$INCLUDE proxy.conf?g" /etc/raddb/radiusd.conf # remove the proxy function
2728 rexy 994
# Add ALCASAR & Coovachilli dictionaries
995
	[ -e /etc/raddb/dictionary.default ] || cp /etc/raddb/dictionary /etc/raddb/dictionary.default
2730 rexy 996
	cp $DIR_CONF/radius/dictionary.alcasar /etc/raddb/
997
	echo '$INCLUDE dictionary.alcasar' > /etc/raddb/dictionary
998
	cp /usr/share/doc/coova-chilli/dictionary.coovachilli /etc/raddb/
999
	echo '$INCLUDE dictionary.coovachilli' >> /etc/raddb/dictionary
2420 richard 1000
# Set "client.conf" to describe radius clients (coova on 127.0.0.1)
1 root 1001
	[ -e /etc/raddb/clients.conf.default ] || cp -f /etc/raddb/clients.conf /etc/raddb/clients.conf.default
1002
	cat << EOF > /etc/raddb/clients.conf
2438 richard 1003
client localhost {
1004
	ipaddr = 127.0.0.1
1 root 1005
	secret = $secretradius
2438 richard 1006
	shortname = chilli
2454 tom.houday 1007
	nas_type = other
1 root 1008
}
1009
EOF
2758 rexy 1010
# Set Virtual server
1011
    # Remvoveing all except "alcasar virtual site")
1012
	# INFO : To enable 802.1X, add the "innser-tunnel" virtual server (link in sites-enabled)  Change the firewall rules to allow "radius" extern connections.
2467 richard 1013
	cp $DIR_CONF/radius/alcasar /etc/raddb/sites-available/alcasar
1014
	cp $DIR_CONF/radius/alcasar-with-ldap /etc/raddb/sites-available/alcasar-with-ldap
1015
	chown radius:apache /etc/raddb/sites-available/alcasar*
1016
	chmod 660 /etc/raddb/sites-available/alcasar*
2758 rexy 1017
	rm -f /etc/raddb/sites-enabled/*
2420 richard 1018
	ln -s /etc/raddb/sites-available/alcasar /etc/raddb/sites-enabled/alcasar
2454 tom.houday 1019
# Set modules
2728 rexy 1020
	# Add custom LDAP "available module"
2758 rexy 1021
	# INFO : To enable 802.1X, add the "eap" module and verify access to the keys (/etc/pki/tls/private/radius.pem). Change the firewall rules to allow "radius" extern connections.
2465 richard 1022
	cp -f $DIR_CONF/radius/ldap-alcasar /etc/raddb/mods-available/
1023
	chown -R radius:radius /etc/raddb/mods-available/ldap-alcasar
2728 rexy 1024
	# Set only usefull modules for ALCASAR (! the module 'ldap-alcasar' is enabled only via ACC)
2454 tom.houday 1025
	rm -rf  /etc/raddb/mods-enabled/*
2615 tom.houday 1026
	for mods in sql sqlcounter attr_filter expiration logintime pap expr always
2454 tom.houday 1027
	do
1028
		ln -s /etc/raddb/mods-available/$mods /etc/raddb/mods-enabled/$mods
1029
	done
2758 rexy 1030
# Configure SQL module
2420 richard 1031
	[ -e /etc/raddb/mods-available/sql.default ] || cp /etc/raddb/mods-available/sql /etc/raddb/mods-available/sql.default
2423 richard 1032
	$SED "s?^[\t ]*driver =.*?driver = \"rlm_sql_mysql\"?g" /etc/raddb/mods-available/sql
1033
	$SED "s?^[\t ]*dialect =.*?dialect = \"mysql\"?g" /etc/raddb/mods-available/sql
2420 richard 1034
	$SED "s?^[\t ]*radius_db =.*?radius_db = \"$DB_RADIUS\"?g" /etc/raddb/mods-available/sql
2423 richard 1035
	$SED "s?^#[\t ]*server =.*?server = \"localhost\"?g" /etc/raddb/mods-available/sql
1036
	$SED "s?^#[\t ]*port =.*?port = \"3306\"?g" /etc/raddb/mods-available/sql
1037
	$SED "s?^#[\t ]*login =.*?login = \"$DB_USER\"?g" /etc/raddb/mods-available/sql
1038
	$SED "s?^#[\t ]*password =.*?password = \"$radiuspwd\"?g" /etc/raddb/mods-available/sql
2758 rexy 1039
	# no TLS encryption on 127.0.0.1
2763 rexy 1040
	$SED "s?^[\t ]*ca_file =.*?#&?g" /etc/raddb/mods-available/sql
1041
	$SED "s?^[\t ]*ca_path =.*?#&?g" /etc/raddb/mods-available/sql
1042
	$SED "s?^[\t ]*certificate_file =.*?#&?g" /etc/raddb/mods-available/sql
1043
	$SED "s?^[\t ]*private_key_file =.*?#&?g" /etc/raddb/mods-available/sql
1044
	$SED "s?^[\t ]*cipher =.*?#&?g" /etc/raddb/mods-available/sql
1045
	$SED "s?^[\t ]*tls_required =.*?tls_required = no?g" /etc/raddb/mods-available/sql
2454 tom.houday 1046
# queries.conf modifications : case sensitive for username, check simultaneous use, patch on 'postauth' table, etc.
2420 richard 1047
	[ -e /etc/raddb/mods-config/sql/main/mysql/queries.conf.default ] || cp /etc/raddb/mods-config/sql/main/mysql/queries.conf /etc/raddb/mods-config/sql/main/mysql/queries.conf.default
1048
	cp -f $DIR_CONF/radius/queries.conf /etc/raddb/mods-config/sql/main/mysql/queries.conf
1049
	chown -R radius:radius /etc/raddb/mods-config/sql/main/mysql/queries.conf
2421 richard 1050
# sqlcounter modifications
2470 richard 1051
	[ -e /etc/raddb/mods-available/sqlcounter.default ] || cp /etc/raddb/mods-available/sqlcounter /etc/raddb/mods-available/sqlcounter.default
1052
	cp -f $DIR_CONF/radius/sqlcounter /etc/raddb/mods-available/sqlcounter
1053
	chown -R radius:radius /etc/raddb/mods-available/sqlcounter
2421 richard 1054
# make certain that mysql is up before freeradius start
2937 rexy 1055
	cp /lib/systemd/system/radiusd.service /etc/systemd/system/radiusd.service
1056
	$SED "s?^After=.*?After=syslog.target network.target mysqld.service?g" /etc/systemd/system/radiusd.service
1574 richard 1057
	/usr/bin/systemctl daemon-reload
2597 tom.houday 1058
# Allow apache to change some conf files (ie : ldap on/off)
1059
	chgrp apache /etc/raddb /etc/raddb/sites-available /etc/raddb/mods-available
2770 rexy 1060
	chmod 750 /etc/raddb /etc/raddb/sites-available /etc/raddb/mods-available
2724 rexy 1061
} # End of freeradius()
1 root 1062
 
2423 richard 1063
#############################################################################
2552 rexy 1064
##                           Function "chilli"                             ##
2423 richard 1065
## - Creation of the conf file and init file (systemd) for coova-chilli    ##
1066
## - Adapt the authentication web page (intercept.php)                     ##
1067
#############################################################################
2724 rexy 1068
chilli()
1 root 1069
{
1370 richard 1070
# chilli unit for systemd
2937 rexy 1071
	cat << EOF > /etc/systemd/system/chilli.service
1372 richard 1072
#  This file is part of systemd.
1073
#
1074
#  systemd is free software; you can redistribute it and/or modify it
1075
#  under the terms of the GNU General Public License as published by
1076
#  the Free Software Foundation; either version 2 of the License, or
1077
#  (at your option) any later version.
2771 rexy 1078
 
1079
# This unit launches coova-chilli a captive portal
1370 richard 1080
[Unit]
1081
Description=chilli is a captive portal daemon
1082
After=network.target
1083
 
1084
[Service]
1379 richard 1085
Type=forking
1370 richard 1086
ExecStart=/usr/libexec/chilli start
1087
ExecStop=/usr/libexec/chilli stop
1088
ExecReload=/usr/libexec/chilli reload
2775 rexy 1089
PIDFile=/run/chilli.pid
1370 richard 1090
 
1091
[Install]
1092
WantedBy=multi-user.target
1093
EOF
799 richard 1094
# init file creation
1370 richard 1095
	[ -e /etc/init.d/chilli.default ] || mv /etc/init.d/chilli /etc/init.d/chilli.default
1801 richard 1096
	cat <<EOF > /etc/init.d/chilli
799 richard 1097
#!/bin/sh
1098
#
1099
# chilli CoovaChilli init
1100
#
1101
# chkconfig: 2345 65 35
1102
# description: CoovaChilli
1103
### BEGIN INIT INFO
1104
# Provides:       chilli
2454 tom.houday 1105
# Required-Start: network
1106
# Should-Start:
799 richard 1107
# Required-Stop:  network
2454 tom.houday 1108
# Should-Stop:
799 richard 1109
# Default-Start:  2 3 5
1110
# Default-Stop:
1111
# Description:    CoovaChilli access controller
1112
### END INIT INFO
1113
 
1114
[ -f /usr/sbin/chilli ] || exit 0
1115
. /etc/init.d/functions
1116
CONFIG=/etc/chilli.conf
2775 rexy 1117
pidfile=/run/chilli.pid
799 richard 1118
[ -f \$CONFIG ] || {
2394 tom.houday 1119
	echo "\$CONFIG Not found"
1120
	exit 0
799 richard 1121
}
2882 rexy 1122
current_users_file="/tmp/current_users.txt"	# file containing active users
799 richard 1123
RETVAL=0
1124
prog="chilli"
1125
case \$1 in
2394 tom.houday 1126
	start)
2454 tom.houday 1127
		if [ -f \$pidfile ] ; then
2394 tom.houday 1128
			gprintf "chilli is already running"
1129
		else
1130
			gprintf "Starting \$prog: "
2884 rexy 1131
			echo '' > \$current_users_file && chown root:apache \$current_users_file && chmod 660 \$current_users_file
2775 rexy 1132
			rm -f /run/chilli* # cleaning
2394 tom.houday 1133
			/usr/sbin/modprobe tun >/dev/null 2>&1
1134
			echo 1 > /proc/sys/net/ipv4/ip_forward
1135
			[ -e /dev/net/tun ] || {
2454 tom.houday 1136
				(cd /dev;
1137
				mkdir net;
1138
				cd net;
2394 tom.houday 1139
				mknod tun c 10 200)
1140
			}
1141
			ifconfig $INTIF 0.0.0.0
1142
			/usr/sbin/ethtool -K $INTIF gro off
1143
			daemon /usr/sbin/chilli -c \$CONFIG --pidfile=\$pidfile &
1144
			RETVAL=\$?
1145
		fi
1146
		;;
799 richard 1147
 
2394 tom.houday 1148
	reload)
1149
		killall -HUP chilli
1150
		;;
799 richard 1151
 
2394 tom.houday 1152
	restart)
1153
		\$0 stop
1154
		sleep 2
1155
		\$0 start
1156
		;;
799 richard 1157
 
2394 tom.houday 1158
	status)
1159
		status chilli
1160
		RETVAL=0
1161
		;;
1162
 
1163
	stop)
2454 tom.houday 1164
		if [ -f \$pidfile ] ; then
2394 tom.houday 1165
			gprintf "Shutting down \$prog: "
1166
			killproc /usr/sbin/chilli
1167
			RETVAL=\$?
1168
			[ \$RETVAL = 0 ] && rm -f \$pidfile
1169
			[ -e \$current_users_file ] && rm -f \$current_users_file
2454 tom.houday 1170
		else
2394 tom.houday 1171
			gprintf "chilli is not running"
1172
		fi
1173
		;;
1174
 
1175
	*)
1176
		echo "Usage: \$0 {start|stop|restart|reload|status}"
1177
		exit 1
799 richard 1178
esac
1179
echo
1180
EOF
2324 tom.houday 1181
	chmod a+x /etc/init.d/chilli
1182
	ln -s /etc/init.d/chilli /usr/libexec/chilli
799 richard 1183
# conf file creation
346 richard 1184
	[ -e /etc/chilli.conf.default ] || cp /etc/chilli.conf /etc/chilli.conf.default
2016 raphael.pi 1185
	#NTP Option configuration for DHCP
2032 richard 1186
	#DHCP Options : rfc2132
1187
		#dhcp option value will be convert in hexa.
1188
		#NTP option (or 'option 42') is like :
2454 tom.houday 1189
		#
2032 richard 1190
		#    Code   Len         Address 1               Address 2
1191
		#   +-----+-----+-----+-----+-----+-----+-----+-----+--
1192
		#   |  42 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
1193
		#   +-----+-----+-----+-----+-----+-----+-----+-----+--
1194
		#
1195
		#Code : 42 => 2a
1196
		#Len : 4 => 04
2688 lucas.echa 1197
	PRIVATE_IP_HEXA=$(printf "%02x\n" "$(echo $PRIVATE_IP | cut -d'.' -f1)")$(printf "%02x\n" "$(echo $PRIVATE_IP | cut -d'.' -f2)")$(printf "%02x\n" "$(echo $PRIVATE_IP | cut -d'.' -f3)")$(printf "%02x\n" "$(echo $PRIVATE_IP | cut -d'.' -f4)")
346 richard 1198
	cat <<EOF > /etc/chilli.conf
1199
# coova config for ALCASAR
2775 rexy 1200
cmdsocket	/run/chilli.sock
1336 richard 1201
unixipc		chilli.$INTIF.ipc
2775 rexy 1202
pidfile		/run/chilli.pid
346 richard 1203
net		$PRIVATE_NETWORK_MASK
595 richard 1204
dhcpif		$INTIF
841 richard 1205
ethers		$DIR_DEST_ETC/alcasar-ethers
861 richard 1206
#nodynip
865 richard 1207
#statip
1208
dynip		$PRIVATE_NETWORK_MASK
1249 richard 1209
domain		$DOMAIN
355 richard 1210
dns1		$PRIVATE_IP
1211
dns2		$PRIVATE_IP
346 richard 1212
uamlisten	$PRIVATE_IP
503 richard 1213
uamport		3990
2370 tom.houday 1214
uamuiport	3991
837 richard 1215
macauth
1216
macpasswd	password
1697 richard 1217
strictmacauth
1243 richard 1218
locationname	$HOSTNAME.$DOMAIN
346 richard 1219
radiusserver1	127.0.0.1
1220
radiusserver2	127.0.0.1
1221
radiussecret	$secretradius
1222
radiusauthport	1812
1223
radiusacctport	1813
2818 rexy 1224
uamserver	http://$HOSTNAME.$DOMAIN/intercept.php
2374 tom.houday 1225
redirurl
1243 richard 1226
radiusnasid	$HOSTNAME.$DOMAIN
346 richard 1227
uamsecret	$secretuam
1249 richard 1228
uamallowed	$HOSTNAME,$HOSTNAME.$DOMAIN
346 richard 1229
coaport		3799
1379 richard 1230
conup		$DIR_DEST_BIN/alcasar-conup.sh
1231
condown		$DIR_DEST_BIN/alcasar-condown.sh
2594 tom.houday 1232
macup		$DIR_DEST_BIN/alcasar-macup.sh
503 richard 1233
include		$DIR_DEST_ETC/alcasar-uamallowed
1234
include		$DIR_DEST_ETC/alcasar-uamdomain
2016 raphael.pi 1235
dhcpopt		2a04$PRIVATE_IP_HEXA
1613 franck 1236
#dhcpgateway		none
1237
#dhcprelayagent		none
1610 franck 1238
#dhcpgatewayport	none
2234 richard 1239
sslkeyfile	/etc/pki/tls/private/alcasar.key
1240
sslcertfile	/etc/pki/tls/certs/alcasar.crt
2922 rexy 1241
sslcafile	/etc/pki/tls/certs/server-chain.pem
2818 rexy 1242
#redirssl
1243
#uamuissl
346 richard 1244
EOF
2274 richard 1245
# create files for "DHCP static ip" and "DHCP static ip info". Reserve the second IP address for INTIF (the first one is for tun0)
977 richard 1246
	echo "$PRIVATE_MAC $PRIVATE_SECOND_IP" > $DIR_DEST_ETC/alcasar-ethers
2274 richard 1247
	echo "$PRIVATE_MAC $PRIVATE_SECOND_IP" > $DIR_DEST_ETC/alcasar-ethers-info
840 richard 1248
# create files for trusted domains and urls
1148 crox53 1249
	touch $DIR_DEST_ETC/alcasar-uamallowed $DIR_DEST_ETC/alcasar-uamdomain
503 richard 1250
	chown root:apache $DIR_DEST_ETC/alcasar-*
1251
	chmod 660 $DIR_DEST_ETC/alcasar-*
847 richard 1252
# Configuration des fichier WEB d'interception (secret partagé avec coova-chilli)
526 stephane 1253
	$SED "s?^\$uamsecret =.*?\$uamsecret = \"$secretuam\";?g" $DIR_WEB/intercept.php
796 richard 1254
# user 'chilli' creation (in order to run conup/off and up/down scripts
2396 tom.houday 1255
	chilli_exist=`grep -c ^chilli: /etc/passwd`
796 richard 1256
	if [ "$chilli_exist" == "1" ]
1257
	then
2454 tom.houday 1258
		userdel -r chilli 2>/dev/null
796 richard 1259
	fi
1260
	groupadd -f chilli
1261
	useradd -r -g chilli -s /bin/false -c "system user for coova-chilli" chilli
2724 rexy 1262
}  # End of chilli()
1349 richard 1263
 
2541 rexy 1264
################################################################
2521 armand.ito 1265
##                   Function "e2guardian"                    ##
2541 rexy 1266
## - Set the parameters of this HTML proxy (as controler)     ##
1267
################################################################
2724 rexy 1268
e2guardian()
1 root 1269
{
2758 rexy 1270
# Adapt systemd unit
2937 rexy 1271
	cp /lib/systemd/system/e2guardian.service /etc/systemd/system/e2guardian.service
1272
	$SED "s?^ExecStart=.*?ExecStart=/usr/sbin/e2guardian -c /etc/e2guardian/e2guardian.conf?g" /etc/systemd/system/e2guardian.service
1273
	$SED "s?^After=.*?After=network.target chilli.service?g" /etc/systemd/system/e2guardian.service
1274
# Adapt the main conf file
2521 armand.ito 1275
	[ -e $DIR_DG/e2guardian.conf.default ] || cp $DIR_DG/e2guardian.conf $DIR_DG/e2guardian.conf.default
1293 richard 1276
# French deny HTML page
2764 rexy 1277
	$SED "s?^language =.*?language = 'french'?g" $DIR_DG/e2guardian.conf
2840 rexy 1278
# 2 filtergroups (8080 & 8090)
1279
	$SED "s?^filtergroups =.*?filtergroups = 2?g" $DIR_DG/e2guardian.conf
1280
# Listen on 8080 (HTTP for BL users) only on LAN side
1281
	$SED "s?^filterip =.*?filterip = $PRIVATE_IP?g" $DIR_DG/e2guardian.conf
2775 rexy 1282
	$SED "s?^filterports =.*?filterports = 8080?g" $DIR_DG/e2guardian.conf
2840 rexy 1283
# Listen on 8090 (HTTP for WL/AV users) only on LAN side
1284
	$SED "/^filterip = $PRIVATE_IP/a filterip = $PRIVATE_IP" $DIR_DG/e2guardian.conf
1285
	$SED "/^filterports = 8080/a filterports = 8090" $DIR_DG/e2guardian.conf
2844 rexy 1286
# E2guardian doesn't listen transparently on 8443 (HTTPS) (only in future version)
1287
	$SED "s?^transparenthttpsport =.*?#transparenthttpsport = 8443?g" $DIR_DG/e2guardian.conf
1293 richard 1288
# Don't log
2521 armand.ito 1289
	$SED "s?^loglevel =.*?loglevel = 0?g" $DIR_DG/e2guardian.conf
2775 rexy 1290
# Disable HTML content control (weighted & banned)
2521 armand.ito 1291
	$SED "s?^weightedphrasemode =.*?weightedphrasemode = 0?g" $DIR_DG/e2guardian.conf
2840 rexy 1292
# Enable authport plugin
1293
	$SED "s?^#authplugin = '/etc/e2guardian/authplugins/port.conf'?authplugin = '/etc/e2guardian/authplugins/port.conf'?g" $DIR_DG/e2guardian.conf
1294
	$SED "s?^#mapauthtoports =.*?mapauthtoports = off?g" $DIR_DG/e2guardian.conf
1295
# Enable clamd scanner
1296
	$SED "s?^#contentscanner = '/etc/e2guardian/contentscanners/clamdscan.conf'?contentscanner = '/etc/e2guardian/contentscanners/clamdscan.conf'?g" $DIR_DG/e2guardian.conf
1297
 
2841 rexy 1298
# Adapt the first group conf file
1299
	[ -e $DIR_DG/e2guardianf1.conf.default ] || cp $DIR_DG/e2guardianf1.conf $DIR_DG/e2guardianf1.conf.default
1300
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" $DIR_DG/e2guardianf1.conf
1301
	$SED "s/^groupname =.*/groupname = 'blacklisted users'/g" $DIR_DG/e2guardianf1.conf
2866 rexy 1302
	$SED "s/^#htmltemplate =.*/htmltemplate = 'alcasar-e2g.html'/g" $DIR_DG/e2guardianf1.conf
2841 rexy 1303
 
2867 rexy 1304
# copy & adapt HTML templates
2866 rexy 1305
	cp $DIR_CONF/alcasar-e2g-fr.html /usr/share/e2guardian/languages/french/alcasar-e2g.html
1306
	cp $DIR_CONF/alcasar-e2g-en.html /usr/share/e2guardian/languages/ukenglish/alcasar-e2g.html
2867 rexy 1307
	$SED "s?\/\/[a-z.]*\/?\/\/$HOSTNAME.$DOMAIN\/?g" /usr/share/e2guardian/languages/french/alcasar-e2g.html
1308
	$SED "s?\/\/[a-z.]*\/?\/\/$HOSTNAME.$DOMAIN\/?g" /usr/share/e2guardian/languages/ukenglish/alcasar-e2g.html
2866 rexy 1309
 
2841 rexy 1310
###### ALCASAR special filtering ####
2840 rexy 1311
# RAZ bannedphraselist
1312
	cp $DIR_DG/lists/bannedphraselist $DIR_DG/lists/bannedphraselist.default
2758 rexy 1313
	$SED "s?^[^#]?#&?g" $DIR_DG/lists/bannedphraselist # (comment what is not)
2519 rexy 1314
# Disable URL control with regex
2764 rexy 1315
    cp $DIR_DG/lists/bannedregexpurllist $DIR_DG/lists/bannedregexpurllist.default
2758 rexy 1316
	$SED "s?^[^#]?#&?g" $DIR_DG/lists/bannedregexpurllist # (comment what is not)
2775 rexy 1317
# Replace the default deny HTML page (only fr & uk) --> !!! search why our pages make the server crash... 
2764 rexy 1318
#	[ -e /usr/share/e2guardian/languages/french/template.html.default ] || mv /usr/share/e2guardian/languages/french/template.html /usr/share/e2guardian/languages/french/template.html.default
1319
#	cp -f $DIR_CONF/template-fr.html /usr/share/e2guardian/languages/french/template.html
1320
#	[ -e /usr/share/e2guardian/languages/ukenglish/template.html.default ] || mv /usr/share/e2guardian/languages/ukenglish/template.html /usr/share/e2guardian/languages/ukenglish/template.html.default
1321
#	cp -f $DIR_CONF/template.html /usr/share/e2guardian/languages/ukenglish/template.html
2758 rexy 1322
# Dont filtering files by extension or mime-type (empty list)
497 richard 1323
	[ -e $DIR_DG/lists/bannedextensionlist.default ] || mv $DIR_DG/lists/bannedextensionlist $DIR_DG/lists/bannedextensionlist.default
2764 rexy 1324
	touch $DIR_DG/lists/bannedextensionlist
497 richard 1325
	[ -e $DIR_DG/lists/bannedmimetypelist.default ] || mv $DIR_DG/lists/bannedmimetypelist $DIR_DG/lists/bannedmimetypelist.default
1326
	touch $DIR_DG/lists/bannedmimetypelist
2758 rexy 1327
# Empty LAN IP list that won't be WEB filtered
497 richard 1328
	[ -e $DIR_DG/lists/exceptioniplist.default ] || mv $DIR_DG/lists/exceptioniplist $DIR_DG/lists/exceptioniplist.default
1329
	touch $DIR_DG/lists/exceptioniplist
2758 rexy 1330
# Creation of ALCASAR banned site list
2841 rexy 1331
	[ -e $DIR_DG/lists/greysitelist.default ] || mv $DIR_DG/lists/greysitelist $DIR_DG/lists/greysitelist.default
1332
	cat <<EOF > $DIR_DG/lists/greysitelist
2775 rexy 1333
# E2guardian filter config for ALCASAR
1334
# In ALCASAR E2guardian filters only URLs (domains are filtered with unbound)
2758 rexy 1335
# block all SSL and CONNECT tunnels
1336
**s
1337
# block all SSL and CONNECT tunnels specified only as an IP
1338
*ips
1339
# block all sites specified only by an IP
1340
*ip
1341
EOF
2775 rexy 1342
# Creation of ALCASAR empty banned URLs list (filled later with Toulouse BL --> see BL function)
497 richard 1343
	[ -e $DIR_DG/lists/bannedurllist.default ] || mv $DIR_DG/lists/bannedurllist $DIR_DG/lists/bannedurllist.default
2758 rexy 1344
	cat <<EOF > $DIR_DG/lists/bannedurllist
1345
# E2guardian filter config for ALCASAR
1346
EOF
2775 rexy 1347
# Creation of files for rehabilited domains and urls
2758 rexy 1348
	[ -e $DIR_DG/lists/exceptionsitelist.default ] || mv $DIR_DG/lists/exceptionsitelist $DIR_DG/lists/exceptionsitelist.default
1349
	[ -e $DIR_DG/lists/exceptionurllist.default ] || mv $DIR_DG/lists/exceptionurllist $DIR_DG/lists/exceptionurllist.default
1350
	touch $DIR_DG/lists/exceptionsitelist
1351
	touch $DIR_DG/lists/exceptionurllist
1352
# Add Bing to the safesearch url regext list (parental control)
2775 rexy 1353
	[ -e $DIR_DG/lists/urlregexplist.default ] || cp $DIR_DG/lists/urlregexplist $DIR_DG/lists/urlregexplist.default
2758 rexy 1354
	cat <<EOF >> $DIR_DG/lists/urlregexplist
2776 rexy 1355
 
2758 rexy 1356
# Bing - add 'adlt=strict'
1357
#"(^http://[0-9a-z]+\.bing\.[a-z]+[-/%.0-9a-z]*\?)(.*)"->"\1\2&adlt=strict"
1358
EOF
1359
# 'Safesearch' regex actualisation
1360
	$SED "s?images?search?g" $DIR_DG/lists/urlregexplist
1361
# change the google safesearch ("safe=strict" instead of "safe=vss")
1362
	$SED "s?safe=vss?safe=strict?g" $DIR_DG/lists/urlregexplist
2841 rexy 1363
 
1364
# Create & adapt the second group conf file (av + av_wl)
1365
	cp $DIR_DG/e2guardianf1.conf.default $DIR_DG/e2guardianf2.conf
1366
	$SED "s?^reportinglevel =.*?reportinglevel = 3?g" $DIR_DG/e2guardianf2.conf
2842 rexy 1367
	$SED "s?^groupname =.*?groupname = 'antimalware + whitelested users'?g" $DIR_DG/e2guardianf2.conf
1368
	$SED "s?^urllist = 'name=banned,messageno=501,path=/etc/e2guardian/lists/bannedurllist'?urllist = 'name=banned,messageno=501,path=/etc/e2guardian/lists/bannedurllist.default'?g" $DIR_DG/e2guardianf2.conf # no banned urls
2841 rexy 1369
 
2775 rexy 1370
# create log folder
1371
    mkdir -p /var/log/e2guardian
1372
	chown -R e2guardian /etc/e2guardian /var/log/e2guardian
2724 rexy 1373
} # End of e2guardian()
1 root 1374
 
71 richard 1375
##################################################################
2519 rexy 1376
##                     Function "antivirus"                     ##
2840 rexy 1377
## - Set the parameters of clamav and freshclam                 ##
71 richard 1378
##################################################################
2724 rexy 1379
antivirus()
71 richard 1380
{
2937 rexy 1381
# Clamd unit adaptation to e2guardian
1382
	cp /lib/systemd/system/clamav-daemon.service /etc/systemd/system/clamav-daemon.service
1383
	$SED "/^[Service]/a ExecStartPre=\/bin\/chown e2guardian:e2guardian \/run\/clamav" /etc/systemd/system/clamav-daemon.service
1384
	$SED "/^[Service]/a ExecStartPre=\/bin\/mkdir -p \/run\/clamav" /etc/systemd/system/clamav-daemon.service
1385
	cp /lib/systemd/system/clamav-daemon.socket /etc/systemd/system/clamav-daemon.socket
1386
	$SED "s?^SocketUser=.*?SocketUser=e2guardian?g" /etc/systemd/system/clamav-daemon.socket
1387
	$SED "s?^SocketGroup=.*?SocketGroup=e2guardian?g" /etc/systemd/system/clamav-daemon.socket
1388
# Clamd conf adaptation to e2guardian
2840 rexy 1389
[ -e /etc/clamd.conf.default ] || cp /etc/clamd.conf /etc/clamd.conf.default
2841 rexy 1390
	$SED "s?^MaxThreads.*?MaxThreads 32?g" /etc/clamd.conf
1391
	$SED "s?^#LogTime.*?LogTime yes?g" /etc/clamd.conf # enable logtime for each message
1392
	$SED "s?^LogVerbose.*?LogVerbose no?g" /etc/clamd.conf
1393
	$SED "s?^#LogRotate.*?LogRotate yes?g" /etc/clamd.conf
2865 rexy 1394
	$SED "s?^User.*?User e2guardian?g" /etc/clamd.conf
1395
	$SED "s?^TemporaryDirectory.*?TemporaryDirectory /var/lib/e2guardian/tmp?g" /etc/clamd.conf
1396
	chown -R e2guardian:e2guardian /var/log/clamav /var/lib/clamav
2840 rexy 1397
	chmod 775 /var/log/clamav /var/lib/clamav
1398
	chmod 664 /var/log/clamav/*
1358 richard 1399
# update virus database every 4 hours (24h/6)
1357 richard 1400
	[ -e /etc/freshclam.conf.default ] || cp /etc/freshclam.conf /etc/freshclam.conf.default
1401
	$SED "s?^Checks.*?Checks 6?g" /etc/freshclam.conf
489 richard 1402
	$SED "s?^NotifyClamd.*?# NotifyClamd /etc/clamd.conf?g" /etc/freshclam.conf
2865 rexy 1403
	$SED "s?^DatabaseOwner.*?DatabaseOwner e2guardian?g" /etc/freshclam.conf
2764 rexy 1404
	$SED "/^DatabaseMirror/a DatabaseMirror db.fr.clamav.net" /etc/freshclam.conf
2841 rexy 1405
	$SED "s?^MaxAttempts.*?MaxAttempts 3?g" /etc/freshclam.conf
2980 rexy 1406
	# update now
1407
        /usr/bin/freshclam --no-warnings --quiet
2724 rexy 1408
} # End of antivirus()
71 richard 1409
 
2724 rexy 1410
##############################################################
1411
##                            function "ulogd"              ##
1412
## - Ulog config for multi-log files                        ##
1413
##############################################################
1414
ulogd()
476 richard 1415
{
1416
# Three instances of ulogd (three different logfiles)
1417
	[ -d /var/log/firewall ] || mkdir -p /var/log/firewall
478 richard 1418
	nl=1
1358 richard 1419
	for log_type in traceability ssh ext-access
478 richard 1420
	do
2937 rexy 1421
		cp -f /lib/systemd/system/ulogd.service /etc/systemd/system/ulogd-$log_type.service
1369 richard 1422
		[ -e /var/log/firewall/$log_type.log ] || echo "" > /var/log/firewall/$log_type.log
1375 richard 1423
		cp -f $DIR_CONF/ulogd-sample.conf /etc/ulogd-$log_type.conf
1704 richard 1424
		$SED "s?^group=.*?group=$nl?g" /etc/ulogd-$log_type.conf
478 richard 1425
		cat << EOF >> /etc/ulogd-$log_type.conf
1452 richard 1426
[emu1]
478 richard 1427
file="/var/log/firewall/$log_type.log"
1428
sync=1
1429
EOF
2938 rexy 1430
		$SED "s?^ExecStart=.*?ExecStart=/usr/sbin/ulogd -c /etc/ulogd-$log_type.conf $ULOGD_OPTIONS?g" /etc/systemd/system/ulogd-$log_type.service
478 richard 1431
		nl=`expr $nl + 1`
1432
	done
476 richard 1433
	chown -R root:apache /var/log/firewall
1434
	chmod 750 /var/log/firewall
1435
	chmod 640 /var/log/firewall/*
2724 rexy 1436
}  # End of ulogd()
476 richard 1437
 
1159 crox53 1438
##########################################################
2519 rexy 1439
##                    Function "nfsen"                  ##
2771 rexy 1440
## - configure NetFlow collector (nfcapd)               ##
1441
## - configure NetFlow grapher (nfsen-ng)               ##
1159 crox53 1442
##########################################################
1389 richard 1443
nfsen()
1 root 1444
{
2772 rexy 1445
	groupadd -f nfcapd
2868 rexy 1446
	id -u nfcapd >/dev/null 2>&1 || useradd -r -g nfcapd -s /bin/false -c "system user for nfcapd" nfcapd
2771 rexy 1447
# nfcapd unit for systemd
2937 rexy 1448
	cat << EOF > /etc/systemd/system/nfcapd.service
1372 richard 1449
#  This file is part of systemd.
1450
#
1451
#  systemd is free software; you can redistribute it and/or modify it
1452
#  under the terms of the GNU General Public License as published by
1453
#  the Free Software Foundation; either version 2 of the License, or
1454
#  (at your option) any later version.
1455
 
2771 rexy 1456
# This unit launches nfcapd (a Netflow collector).
1372 richard 1457
[Unit]
2771 rexy 1458
Description=Netflow Capture Daemon
1459
After=network-online.target iptables.service
1372 richard 1460
 
1461
[Service]
2771 rexy 1462
Type=exec
2776 rexy 1463
ExecStartPre=/bin/mkdir -p /run/nfcapd
1464
ExecStartPre=/bin/chown nfcapd:nfcapd /run/nfcapd
2771 rexy 1465
PIDFile=/run/nfcapd/nfcapd.pid
2825 rexy 1466
ExecStart=/usr/bin/nfcapd -w -D -b 127.0.0.1 -p 2055 -u nfcapd -g nfcapd -B 200000 -t 300 -S 7 -z -P /run/nfcapd/nfcapd.pid -I alcasar_netflow -l /var/log/nfsen/profiles-data/live/alcasar_netflow
2771 rexy 1467
ExecReload=/bin/kill -HUP $MAINPID
1372 richard 1468
 
1469
[Install]
1470
WantedBy=multi-user.target
1471
EOF
2825 rexy 1472
    [ -d /var/log/nfsen/profiles-data/live/alcasar_netflow ] || mkdir -p /var/log/nfsen/profiles-data/live/alcasar_netflow
2775 rexy 1473
    [ -d /run/nfcapd ] || mkdir -p /run/nfcapd
1474
    chown -R nfcapd:nfcapd /var/log/nfsen /run/nfcapd
2724 rexy 1475
} # End of nfsen()
1 root 1476
 
2552 rexy 1477
###########################################################
1478
##                     Function "vnstat"                 ##
2809 rexy 1479
## - Initialization of vnstat and vnstat-dashboard       ##
2552 rexy 1480
###########################################################
2724 rexy 1481
vnstat()
1541 richard 1482
{
2933 rexy 1483
	# vnstat
1484
	[ -e /etc/vnstat.conf.default ] || cp /etc/vnstat.conf /etc/vnstat.conf.default
2589 rexy 1485
	$SED "s?^Interface.*?Interface \"$EXTIF\"?g" /etc/vnstat.conf
2688 lucas.echa 1486
	$SED "s?^DatabaseDir.*?DatabaseDir /var/log/vnstat?g" /etc/vnstat.conf
2933 rexy 1487
	$SED "s?^MaxBandwidth.*?MaxBandwidth 10000?g" /etc/vnstat.conf
1488
	# vnstat-dashboard
1489
	$SED "s?^\$thisInterface.*?\$thisInterface = \"$EXTIF\";?" $DIR_ACC/manager/vnstat/index.php
2937 rexy 1490
	cp /lib/systemd/system/vnstat.service /etc/systemd/system/vnstat.service
2938 rexy 1491
	$SED "s?^PIDFile=.*?PIDFile=/run/vnstat/vnstat.pid?g" /etc/systemd/system/vnstat.service
2724 rexy 1492
} # End of vnstat()
2281 tom.houday 1493
 
2724 rexy 1494
###################################################################
1495
##                     Function "dnsmasq"                        ##
1496
## - creation of the conf files of dnsmasq (whitelist for ipset )##
1497
###################################################################
1498
dnsmasq()
219 jeremy 1499
{
1500
	[ -d /var/log/dnsmasq ] || mkdir /var/log/dnsmasq
2688 lucas.echa 1501
	[ -e /etc/dnsmasq.conf.default ] || mv /etc/dnsmasq.conf /etc/dnsmasq.conf.default
2724 rexy 1502
	# dnsmasq listen on udp 55 ("dnsmasq with whitelist")
1928 richard 1503
	cat << EOF > /etc/dnsmasq-whitelist.conf
1390 richard 1504
# Configuration file for "dnsmasq with whitelist"
1873 richard 1505
# ADD Toulouse university whitelist domains
2775 rexy 1506
pid-file=/run/dnsmasq-whitelist.pid
2688 lucas.echa 1507
listen-address=127.0.0.1
1356 richard 1508
port=55
1472 richard 1509
no-dhcp-interface=lo
1356 richard 1510
bind-interfaces
1721 richard 1511
cache-size=1024
1356 richard 1512
domain-needed
1513
expand-hosts
1514
bogus-priv
1515
filterwin2k
2688 lucas.echa 1516
ipset=/#/wl_ip_allowed	# dynamically add the resolv IP address in the Firewall rules
1517
server=$DNS1
1518
server=$DNS2
1356 richard 1519
EOF
2937 rexy 1520
	# Don't run dnsmasq service. Create dnsmasq-whitelist unit
1521
	systemctl disable dnsmasq.service
1522
	cp -f /lib/systemd/system/dnsmasq.service /etc/systemd/system/dnsmasq-whitelist.service
1523
	$SED "s?^ExecStart=.*?ExecStart=/usr/sbin/dnsmasq -C /etc/dnsmasq-whitelist.conf?g" /etc/systemd/system/dnsmasq-whitelist.service
1524
	$SED "s?^PIDFile=.*?PIDFile=/run/dnsmasq-whitelist.pid?g" /etc/systemd/system/dnsmasq-whitelist.service
2724 rexy 1525
} # End of dnsmasq()
2688 lucas.echa 1526
 
2724 rexy 1527
#########################################################
1528
##              Function "unbound"                     ##
1529
## - create the conf files for 4 unbound services      ##
1530
## - create the systemd files for 4 unbound services   ##
1531
#########################################################
2688 lucas.echa 1532
unbound ()
1533
{
1534
	[ -d /etc/unbound/conf.d ] || mkdir -p /etc/unbound/conf.d
1535
	[ -d /etc/unbound/conf.d/common ] || mkdir /etc/unbound/conf.d/common
1536
	[ -d /etc/unbound/conf.d/common/local-forward ] || mkdir /etc/unbound/conf.d/common/local-forward
1537
	[ -d /etc/unbound/conf.d/common/local-dns ] || mkdir /etc/unbound/conf.d/common/local-dns
1538
	[ -d /etc/unbound/conf.d/forward ] || mkdir /etc/unbound/conf.d/forward
1539
	[ -d /etc/unbound/conf.d/blacklist ] || mkdir /etc/unbound/conf.d/blacklist
1540
	[ -d /etc/unbound/conf.d/whitelist ] || mkdir /etc/unbound/conf.d/whitelist
1541
	[ -d /etc/unbound/conf.d/blackhole ] || mkdir /etc/unbound/conf.d/blackhole
2833 rexy 1542
	[ -d /var/log/unbound ] || mkdir /var/log/unbound
1543
	chown unbound:unbound /var/log/unbound
2688 lucas.echa 1544
	[ -e /etc/unbound/unbound.conf.default ] || cp /etc/unbound/unbound.conf /etc/unbound/unbound.conf.default
1545
 
2724 rexy 1546
# Forward zone configuration file for all unbound dns servers
2688 lucas.echa 1547
	cat << EOF > /etc/unbound/conf.d/common/forward-zone.conf
1548
forward-zone:
1549
	name: "."
1550
	forward-addr: $DNS1
1551
	forward-addr: $DNS2
1472 richard 1552
EOF
1553
 
2724 rexy 1554
# Custom configuration file for manual DNS configuration
2688 lucas.echa 1555
	cat << EOF > /etc/unbound/conf.d/common/local-forward/custom.conf
1556
## Ajouter un bloc pour chaque nom de domaine géré par un autre seveur DNS
1557
## Add one block for each domain name managed by an other DNS server
1558
##
1559
## Example:
1560
##
1561
## server:
1562
##     local-zone: "<your_domain>." transparent
1563
## forward-zone:
1564
##     name: "<your_domain>."
1565
##     forward-addr: <@IP_domain_server>
1566
##
2558 rexy 1567
EOF
1568
 
2724 rexy 1569
# Configuration file of ALCASAR main domains for $INTIF
2688 lucas.echa 1570
	cat << EOF > /etc/unbound/conf.d/common/local-dns/${INTIF}.conf
1571
server:
1572
	local-data: "$HOSTNAME.$DOMAIN A $PRIVATE_IP"
2831 rexy 1573
	local-data-ptr: "$PRIVATE_IP $HOSTNAME.$DOMAIN"
2688 lucas.echa 1574
EOF
1575
 
2724 rexy 1576
# Configuration file for lo of forward unbound
2688 lucas.echa 1577
	cat << EOF > /etc/unbound/conf.d/forward/iface.lo.conf
1578
server:
1579
	interface: 127.0.0.1@53
1580
	access-control-view: 127.0.0.1/8 lo
1581
view:
1582
	name: "lo"
2864 rexy 1583
	local-data: "$HOSTNAME A 127.0.0.1"
2688 lucas.echa 1584
	local-data: "$HOSTNAME.$DOMAIN A 127.0.0.1"
2864 rexy 1585
	local-data-ptr: "127.0.0.1 $HOSTNAME.$DOMAIN"
2688 lucas.echa 1586
	view-first: yes
1587
EOF
1588
 
2724 rexy 1589
# Configuration file for $INTIF of forward unbound
2688 lucas.echa 1590
	cat << EOF > /etc/unbound/conf.d/forward/iface.${INTIF}.conf
1591
server:
1592
	interface: ${PRIVATE_IP}@53
1593
	access-control-view: $PRIVATE_NETWORK_MASK $INTIF
1594
view:
1595
	name: "$INTIF"
1596
	view-first: yes
1597
EOF
1598
 
2831 rexy 1599
# Configuration file for main unbound
2688 lucas.echa 1600
	cat << EOF > /etc/unbound/unbound.conf
1601
server:
1602
	verbosity: 1
1603
	hide-version: yes
1604
	hide-identity: yes
1605
	do-ip6: no
1606
	include: /etc/unbound/conf.d/common/forward-zone.conf
1607
	include: /etc/unbound/conf.d/common/local-forward/*
1608
	include: /etc/unbound/conf.d/common/local-dns/*
1609
	include: /etc/unbound/conf.d/forward/*
1610
EOF
1611
 
2724 rexy 1612
# Configuration file for $INTIF of blacklist unbound
2688 lucas.echa 1613
	cat << EOF > /etc/unbound/conf.d/blacklist/iface.${INTIF}.conf
1614
server:
1615
	interface: ${PRIVATE_IP}@54
1616
	access-control: $PRIVATE_IP_MASK allow
1617
	access-control-tag: $PRIVATE_IP_MASK "blacklist"
1618
	access-control-tag-action: $PRIVATE_IP_MASK "blacklist" redirect
1619
	access-control-tag-data: $PRIVATE_IP_MASK "blacklist" "A $PRIVATE_IP"
1620
EOF
1621
 
2724 rexy 1622
# Configuration file for blacklist unbound
2688 lucas.echa 1623
	cat << EOF > /etc/unbound/unbound-blacklist.conf
1624
server:
1625
	verbosity: 1
1626
	hide-version: yes
1627
	hide-identity: yes
1628
	do-ip6: no
1629
	logfile: "/var/log/unbound/unbound-blacklist.log"
1630
	chroot: ""
1631
	define-tag: "blacklist"
1632
	log-local-actions: yes
1633
	include: /etc/unbound/conf.d/common/forward-zone.conf
1634
	include: /etc/unbound/conf.d/common/local-forward/*
1635
	include: /etc/unbound/conf.d/common/local-dns/*
1636
	include: /etc/unbound/conf.d/blacklist/*
1637
	include: /usr/local/share/unbound-bl-enabled/*
1638
EOF
1639
 
2724 rexy 1640
# Configuration file for $INTIF of whitelist unbound
2688 lucas.echa 1641
	cat << EOF > /etc/unbound/conf.d/whitelist/iface.${INTIF}.conf
1642
server:
1643
	interface: ${PRIVATE_IP}@55
1644
	access-control: $PRIVATE_IP_MASK allow
1645
	access-control-tag: $PRIVATE_IP_MASK "whitelist"
1646
	access-control-tag-action: $PRIVATE_IP_MASK "whitelist" redirect
1647
	access-control-tag-data: $PRIVATE_IP_MASK "whitelist" "A $PRIVATE_IP"
1648
EOF
1649
 
2724 rexy 1650
# Configuration file for whitelist unbound
2688 lucas.echa 1651
	cat << EOF > /etc/unbound/unbound-whitelist.conf
1652
server:
1653
	verbosity: 1
1654
	hide-version: yes
1655
	hide-identity: yes
1656
	do-ip6: no
1657
	do-not-query-localhost: no
1658
	define-tag: "whitelist"
1659
	local-zone: "." transparent
1660
	local-zone-tag: "." "whitelist"
2861 rexy 1661
	include: /etc/unbound/conf.d/common/local-forward/*
1662
	include: /etc/unbound/conf.d/common/local-dns/*
1663
	include: /etc/unbound/conf.d/whitelist/*
2688 lucas.echa 1664
	include: /usr/local/share/unbound-wl-enabled/*
1665
forward-zone:
1666
	name: "."
2881 rexy 1667
	forward-addr: 127.0.0.1@55
2688 lucas.echa 1668
EOF
1669
 
2724 rexy 1670
# Configuration file for $INTIF of blackhole unbound
2688 lucas.echa 1671
	cat << EOF > /etc/unbound/conf.d/blackhole/iface.${INTIF}.conf
1672
server:
1673
	interface: ${PRIVATE_IP}@56
1674
	access-control-view: $PRIVATE_NETWORK_MASK $INTIF
1675
view:
1676
	name: "$INTIF"
1677
	local-zone: "." redirect
1678
	local-data: ". A $PRIVATE_IP"
1679
EOF
1680
 
2724 rexy 1681
# Configuration file for blackhole unbound
2688 lucas.echa 1682
	cat << EOF > /etc/unbound/unbound-blackhole.conf
1683
server:
1684
	verbosity: 1
1685
	hide-version: yes
1686
	hide-identity: yes
1687
	do-ip6: no
2861 rexy 1688
	include: /etc/unbound/conf.d/common/local-forward/*
1689
	include: /etc/unbound/conf.d/common/local-dns/*
2688 lucas.echa 1690
	include: /etc/unbound/conf.d/blackhole/*
1691
EOF
1692
 
2937 rexy 1693
	cp /lib/systemd/system/unbound.service /etc/systemd/system/unbound.service
1694
	$SED "s?^ExecStart=.*?ExecStart=/usr/sbin/unbound -d -c /etc/unbound/unbound.conf?g" /etc/systemd/system/unbound.service
1695
	$SED "s?^After=.*?After=syslog.target network-online.target chilli.service?g" /etc/systemd/system/unbound.service
2688 lucas.echa 1696
	for list in blacklist blackhole whitelist
1474 richard 1697
	do
2937 rexy 1698
		cp -f /lib/systemd/system/unbound.service /etc/systemd/system/unbound-$list.service
1699
		$SED "s?^ExecStart=.*?ExecStart=/usr/sbin/unbound -d -c /etc/unbound/unbound-$list.conf?g" /etc/systemd/system/unbound-$list.service
1700
		$SED "s?^PIDFile=.*?PIDFile=/run/unbound-$list.pid?g" /etc/systemd/system/unbound-$list.service
1474 richard 1701
	done
2937 rexy 1702
	$SED "s?^After=.*?After=syslog.target network-online.target chilli.service dnsmasq-whitelist.service?g" /etc/systemd/system/unbound-whitelist.service
2724 rexy 1703
} # End of unbound()
2688 lucas.echa 1704
 
2689 lucas.echa 1705
##################################################
1706
##              Function "dhcpd"                ##
1707
##################################################
2724 rexy 1708
dhcpd()
2689 lucas.echa 1709
{
1710
	[ -e /etc/dhcpd.conf.default ] || cp /etc/dhcpd.conf /etc/dhcpd.conf.default
1711
	cat <<EOF > /etc/dhcpd.conf
1712
ddns-update-style none;
1713
subnet $PRIVATE_NETWORK netmask $PRIVATE_NETMASK {
1714
	option routers $PRIVATE_IP;
1715
	option subnet-mask $PRIVATE_NETMASK;
1716
	option domain-name-servers $PRIVATE_IP;
1717
	range dynamic-bootp $PRIVATE_SECOND_IP $PRIVATE_LAST_IP;
1718
	default-lease-time 21600;
1719
	max-lease-time 43200;
1720
}
1721
EOF
2724 rexy 1722
} # End of dhcpd()
2689 lucas.echa 1723
 
2552 rexy 1724
##########################################################
1725
##                      Function "BL"                   ##
2769 rexy 1726
## - copy & adapt Toulouse BL to ALCASAR architecture   ##
2688 lucas.echa 1727
##     - domain names for unbound-bl & unbound-wl       ##
2552 rexy 1728
##     - URLs for E²guardian                            ##
1729
##     - IPs for NetFilter                              ##
2769 rexy 1730
## - copy additional BLs (TOR + Ultrasurf + C&C)        ##
2552 rexy 1731
##########################################################
2724 rexy 1732
BL()
308 richard 1733
{
1930 richard 1734
	# copy the Toulouse university BL in order to be adapted to ALCASAR architecture (alcasar-bl.sh -adapt)
648 richard 1735
	rm -rf $DIR_DG/lists/blacklists
1930 richard 1736
	mkdir -p /tmp/blacklists
1938 richard 1737
	cp $DIR_BLACKLIST/blacklists.tar.gz /tmp/blacklists/
2769 rexy 1738
# creation of the additional BL and WL categorie named "ossi" (for domain names & ip only)
1957 richard 1739
	mkdir -p $DIR_DG/lists/blacklists/ossi-bl
1740
	touch $DIR_DG/lists/blacklists/ossi-bl/domains
1741
	echo "ossi-bl" >> $DIR_DEST_ETC/alcasar-bl-categories-enabled
1742
	mkdir -p $DIR_DG/lists/blacklists/ossi-wl
1743
	touch $DIR_DG/lists/blacklists/ossi-wl/domains
1744
	echo "ossi-wl" >> $DIR_DEST_ETC/alcasar-wl-categories-enabled
2769 rexy 1745
# add additional BL files
2770 rexy 1746
	for x in $(ls $DIR_BLACKLIST | grep -v "^blacklists")
1957 richard 1747
	do
1748
		mkdir $DIR_DG/lists/blacklists/ossi-bl-$x
1749
		cp $DIR_BLACKLIST/$x  $DIR_DG/lists/blacklists/ossi-bl-$x/domains
1750
		echo "ossi-bl-$x" >> $DIR_DEST_ETC/alcasar-bl-categories-enabled
1751
	done
2521 armand.ito 1752
	chown -R e2guardian:apache $DIR_DG
1957 richard 1753
	chown -R root:apache $DIR_DEST_SHARE
1754
	chmod -R g+rw $DIR_DG $DIR_DEST_SHARE
1927 richard 1755
# adapt the Toulouse BL to ALCASAR architecture
1957 richard 1756
	$DIR_DEST_BIN/alcasar-bl.sh --adapt
1925 richard 1757
# enable the default categories
1957 richard 1758
	$DIR_DEST_BIN/alcasar-bl.sh --cat_choice
2560 rexy 1759
	rm -rf /tmp/blacklists
2724 rexy 1760
} # End of BL()
219 jeremy 1761
 
2552 rexy 1762
#######################################################
1763
##                  Function "cron"                  ##
1764
## - write all cron & anacron files                  ##
1765
#######################################################
2724 rexy 1766
cron()
1 root 1767
{
2640 rexy 1768
# 'crontab' with standard cron at midnight instead of 4:0 am (default)
1 root 1769
	[ -e /etc/crontab.default ] || cp /etc/crontab /etc/crontab.default
1770
	cat <<EOF > /etc/crontab
1828 richard 1771
SHELL=/usr/bin/bash
2640 rexy 1772
PATH=/sbin:/bin:/usr/sbin:/usr/bin
1 root 1773
MAILTO=root
1774
HOME=/
1775
 
1776
# run-parts
1777
01 * * * * root nice -n 19 run-parts --report /etc/cron.hourly
1778
02 0 * * * root nice -n 19 run-parts --report /etc/cron.daily
1779
22 0 * * 0 root nice -n 19 run-parts --report /etc/cron.weekly
1780
42 0 1 * * root nice -n 19 run-parts --report /etc/cron.monthly
1781
EOF
1782
	[ -e /etc/anacrontab.default ] || cp /etc/anacrontab /etc/anacrontab.default
1783
	cat <<EOF >> /etc/anacrontab
2454 tom.houday 1784
7	8	cron.MysqlDump		nice /etc/cron.d/alcasar-mysql
1785
7	10	cron.logExport		nice /etc/cron.d/alcasar-archive
1 root 1786
EOF
811 richard 1787
	cat <<EOF > /etc/cron.d/alcasar-mysql
2640 rexy 1788
# Verify, repair and export users database (every monday at 4:45 am)
1828 richard 1789
45 4 * * 1 root $DIR_DEST_BIN/alcasar-mysql.sh --dump
2640 rexy 1790
# Remove users whose expiration date is exceeded for more more than 7 days (every Monday at 4:40 am)
1828 richard 1791
40 4 * * * root $DIR_DEST_BIN/alcasar-mysql.sh --expire_user 2>&1 >/dev/null
1 root 1792
EOF
952 franck 1793
	cat <<EOF > /etc/cron.d/alcasar-archive
2640 rexy 1794
# Archiving logs (traceability & users database) (every Monday at 5:35 am)
952 franck 1795
35 5 * * 1 root $DIR_DEST_BIN/alcasar-archive.sh --now
1796
EOF
2454 tom.houday 1797
	cat <<EOF > /etc/cron.d/alcasar-ticket-clean
2640 rexy 1798
# Remove password files (created when importing users by CSV files) and user's PDF voucher (every hours at 30')
1566 richard 1799
30 * * * *  root $DIR_DEST_BIN/alcasar-ticket-clean.sh
168 franck 1800
EOF
2454 tom.houday 1801
	cat <<EOF > /etc/cron.d/alcasar-distrib-updates
2967 rexy 1802
# Update the system and reboot if needed (everyday at 3:30 am)
1803
30 3 * * *  root /usr/sbin/urpmi --auto-update --auto 2>&1 ; /usr/local/bin/alcasar-daemon.sh -after-update
722 franck 1804
EOF
2454 tom.houday 1805
	cat <<EOF > /etc/cron.d/alcasar-connections-stats
1808 richard 1806
# Connection stats update (accounting). These Perl scripts are from "dialup_admin" (cf. wiki.freeradius.org/Dialup_admin).
2640 rexy 1807
# 'alcasar-tot_stats' : aggregate the daily connections of users and write it in the table 'totacct' (everyday at 1:01 pm)
1808
# 'alcasar-monthly_tot_stat' : aggregate the monthly connections of users and write it in table 'mtotacct' (everyday at 1h05 pm)
1809
# 'alcasar-truncate_raddact' : remove the user' session log older than 365 days (applying French law : "LCEN") (every month, the first at 01:10 pm)
1810
# 'alcasar-clean_radacct' : close the sessions openned for more than 30 days (every month, the first at 01:15 pm)
1811
# 'alcasar-activity_report.sh' : generate an activity report in PDF (every sunday at 5:35 pm)
1808 richard 1812
1 1 * * * root $DIR_DEST_BIN/alcasar-tot_stats > /dev/null 2>&1
1813
5 1 * * * root $DIR_DEST_BIN/alcasar-monthly_tot_stats > /dev/null 2>&1
1814
10 1 1 * * root $DIR_DEST_BIN/alcasar-truncate_radacct > /dev/null 2>&1
1815
15 1 1 * * root $DIR_DEST_BIN/alcasar-clean_radacct > /dev/null 2>&1
2009 raphael.pi 1816
35 5 * * 0 root $DIR_DEST_BIN/alcasar-activity_report.sh > /dev/null 2>&1
1 root 1817
EOF
2454 tom.houday 1818
	cat <<EOF > /etc/cron.d/alcasar-watchdog
2640 rexy 1819
# 'alcasar-watchdog.sh' : run the "watchdog" (every 10')
1820
# 'alcasar-flush_ipset_wl.sh' : empty the IPSET of the whitelisted IP loaded dynamically with dnsmasq-whitelist hook (every sunday at 0:05 am)
2886 rexy 1821
# 'alcasar-watchdog.sh --disconnect-permanent-users' : disconnect users with attribute "Alcasar-Status-Page-Must-Stay-Open" (daily --> see "cron.daily")
2640 rexy 1822
# 'alcasar-watchdog-hl.sh' : (optionnaly) remove the IP 0.0.0.0 from chilli cache memory
2395 tom.houday 1823
*/10 * * * * root $DIR_DEST_BIN/alcasar-watchdog.sh > /dev/null 2>&1
1905 raphael.pi 1824
 
2886 rexy 1825
@daily root $DIR_DEST_BIN/alcasar-watchdog.sh --disconnect-permanent-users > /dev/null 2>&1
2228 franck 1826
#* * * * * root $DIR_DEST_BIN/alcasar-watchdog-hl.sh > /dev/null 2>&1
1 root 1827
EOF
2454 tom.houday 1828
	cat <<EOF > /etc/cron.d/alcasar-daemon-watchdog
2886 rexy 1829
# start dead daemons (after boot process and every 20')
1851 franck 1830
@reboot root $DIR_DEST_BIN/alcasar-daemon.sh > /dev/null 2>&1
2886 rexy 1831
*/20 * * * * root $DIR_DEST_BIN/alcasar-daemon.sh > /dev/null 2>&1
808 franck 1832
EOF
2454 tom.houday 1833
	cat <<EOF > /etc/cron.d/alcasar-rsync-bl
2769 rexy 1834
# Automatic update the BL (every 12 hours). The enabled categories are listed in '/usr/local/etc/update_cat.conf' (no sync if empty).
1835
 
1874 raphael.pi 1836
EOF
2769 rexy 1837
	cat <<EOF > /etc/cron.d/alcasar-rsync-ossi_bl
1838
# Automatic update the OSSI BLs (every 12 hours) by running the custom update scripts specified in '/usr/local/etc/update_ossi_cat.conf'.
1839
 
1840
EOF
2304 tom.houday 1841
	cat <<EOF > /etc/cron.d/alcasar-letsencrypt
2640 rexy 1842
# Automatic renew the Let's Encrypt certificate (daily --> see "cron.daily")
2304 tom.houday 1843
@daily root $DIR_DEST_BIN/alcasar-letsencrypt.sh --cron > /dev/null 2>&1
1844
EOF
2771 rexy 1845
	cat <<EOF > /etc/cron.d/alcasar-nfcapd-expire
2886 rexy 1846
# Remove netflow files older than one year (daily --> see "cron.daily")
2825 rexy 1847
@daily root /usr/bin/nfexpire -e /var/log/nfsen/profiles-data/live/alcasar_netflow -t 365d
2771 rexy 1848
EOF
1808 richard 1849
# removing the users crons
522 richard 1850
	rm -f /var/spool/cron/*
2724 rexy 1851
} # End of cron()
1 root 1852
 
2867 rexy 1853
########################################################################
1854
##                        Fonction "Fail2Ban"                         ##
1855
##- Adapt conf file to ALCASAR                                        ##
1856
##- Secure items : DDOS, SSH-Brute-Force, Intercept & ACC brute-Force ##
1857
########################################################################
1163 crox53 1858
fail2ban()
1859
{
2868 rexy 1860
# adapt fail2ban to Mageia (fedora like) & ALCASAR behaviour
2867 rexy 1861
[ -e /etc/fail2ban/jail.conf.default ] || cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.default
1862
$SED "s?^before =.*?before = paths-fedora.conf?g" /etc/fail2ban/jail.conf
1863
 
1864
# add 5 jails and their filters
1865
## sshd : Ban after 3 failed attempts (ie. brute-force). This "jail" uses the default "sshd" f2b filter.
2868 rexy 1866
cat << EOF > /etc/fail2ban/jail.d/01-alcasar_sshd.conf
2867 rexy 1867
[sshd]
1868
enabled = true
1869
#enabled  = false
1870
maxretry = 3
2868 rexy 1871
bantime = 3m
1872
findtime = 5m
2867 rexy 1873
EOF
1874
 
1875
## lighttpd-auth : Ban after 3 failed attempts on ACC. This "jail" uses the default "lighttpd-auth" f2b filter.
2868 rexy 1876
cat << EOF > /etc/fail2ban/jail.d/02-alcasar_lighttpd-auth.conf
2867 rexy 1877
[lighttpd-auth]
1878
enabled = true
1879
#enabled  = false
1880
maxretry = 3
2868 rexy 1881
bantime = 3m
1882
findtime = 3m
2867 rexy 1883
EOF
1884
 
1885
## mod-evasive : Ban after 3 failed retrieve page attempts (ie : unknown page)
2868 rexy 1886
cat << EOF > /etc/fail2ban/jail.d/03-alcasar_mod-evasive.conf
2867 rexy 1887
[alcasar_mod-evasive]
1888
#enabled = true
1889
enabled = false
1890
backend = auto
1891
filter = alcasar_mod-evasive
1892
action = iptables-allports[name=alcasar_mod-evasive]
1893
logpath = /var/log/lighttpd/access.log
1894
maxretry = 3
2868 rexy 1895
bantime = 3m
1896
findtime = 3m
2867 rexy 1897
EOF
1898
cat << EOF > /etc/fail2ban/filter.d/alcasar_mod-evasive.conf
1899
[Definition]
1900
failregex =  <HOST> .+\] "[^"]+" 403
1901
ignoreregex =
1902
EOF
1903
 
1904
### alcasar_intercept : ban after 5 failed user login attemps on intercept.php
2868 rexy 1905
cat << EOF > /etc/fail2ban/jail.d/04-alcasar_intercept.conf
2867 rexy 1906
[alcasar_intercept]
1907
enabled = true
1908
#enabled = false
1909
backend = auto
1910
filter = alcasar_intercept
1911
action = iptables-allports[name=alcasar_intercept]
1912
logpath = /var/log/lighttpd/access.log
1913
maxretry = 5
2868 rexy 1914
bantime = 3m
1915
findtime = 3m
1916
EOF
2867 rexy 1917
cat << EOF > /etc/fail2ban/filter.d/alcasar_intercept.conf
1918
[Definition]
1919
failregex = <HOST> .* \"GET \/intercept\.php\?res=failed\&reason=reject
1920
ignoreregex =
1921
EOF
1922
 
1923
## alcasar_change-pwd : ban after 5 failed user change password attempts
2868 rexy 1924
cat << EOF > /etc/fail2ban/jail.d/05-alcasar_change-pwd.conf
2867 rexy 1925
[alcasar_change-pwd]
1926
enabled = true
1927
#enabled = false
1928
backend = auto
1929
filter = alcasar_change-pwd
1930
action = iptables-allports[name=alcasar_change-pwd]
1931
logpath = /var/log/lighttpd/access.log
1932
maxretry = 5
2868 rexy 1933
bantime = 3m
1934
findtime = 3m
2867 rexy 1935
EOF
1936
cat << EOF > /etc/fail2ban/filter.d/alcasar_change-pwd.conf
1937
[Definition]
1938
failregex = <HOST> .* \"POST \/password\.php
1939
ignoreregex =
1940
EOF
1941
 
2840 rexy 1942
# allow reading of 2 log files (fail2ban & watchdog).
2744 rexy 1943
	[ -e /var/log/fail2ban.log ] || /usr/bin/touch /var/log/fail2ban.log
2887 rexy 1944
	[ -e $DIR_SAVE/security/watchdog.log ] || /usr/bin/touch $DIR_SAVE/security/watchdog.log
1165 crox53 1945
	chmod 644 /var/log/fail2ban.log
2887 rexy 1946
	chmod 644 $DIR_SAVE/security/watchdog.log
1418 richard 1947
	/usr/bin/touch /var/log/auth.log
1515 richard 1948
# fail2ban unit
2937 rexy 1949
cp /lib/systemd/system/fail2ban.service /etc/systemd/system/fail2ban.service
1950
$SED '/ExecStart=/a\ExecStop=/usr/bin/fail2ban-client stop' /etc/systemd/system/fail2ban.service
1951
$SED '/Type=/a\PIDFile=/run/fail2ban/fail2ban.pid' /etc/systemd/system/fail2ban.service
1952
$SED '/After=*/c After=syslog.target network.target lighttpd.service' /etc/systemd/system/fail2ban.service
2724 rexy 1953
} # End of fail2ban()
1163 crox53 1954
 
2552 rexy 1955
#########################################################
1956
##                   Fonction "gammu_smsd"             ##
1957
## - Creating of SMS management database               ##
1958
## - Write the gammu a gammu_smsd conf files           ##
1959
#########################################################
1376 richard 1960
gammu_smsd()
1961
{
2601 tom.houday 1962
# Create 'gammu' system user
1963
	groupadd -f gammu_smsd
2868 rexy 1964
	useradd -r -g gammu_smsd -s /bin/false -c "system user for gammu_smsd" gammu_smsd
2601 tom.houday 1965
	usermod -a -G dialout gammu_smsd
1966
 
1967
# Create 'gammu' database
1968
	MYSQL="/usr/bin/mysql -uroot -p$mysqlpwd --execute"
2688 lucas.echa 1969
	$MYSQL "CREATE DATABASE IF NOT EXISTS $DB_GAMMU; GRANT ALL ON $DB_GAMMU.* TO $DB_USER@localhost IDENTIFIED BY '$radiuspwd'; FLUSH PRIVILEGES;"
1376 richard 1970
# Add a gammu database structure
2688 lucas.echa 1971
	/usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_GAMMU < $DIR_CONF/empty-gammu-smsd-db.sql
1376 richard 1972
 
2552 rexy 1973
# Config file for the gammu_smsd daemon & gammu (ttyUSB0 as default com port)
2601 tom.houday 1974
	cat << EOF > /etc/gammurc
2552 rexy 1975
[gammu]
1976
device = /dev/ttyUSB0
1977
connection = at115200
1978
EOF
1979
 
2601 tom.houday 1980
	cat << EOF > /etc/gammu_smsd_conf
1376 richard 1981
[gammu]
1982
port = /dev/ttyUSB0
1983
connection = at115200
1984
 
1985
[smsd]
1986
PIN = 1234
1987
logfile = /var/log/gammu-smsd/gammu-smsd.log
1988
logformat = textall
1989
debuglevel = 0
1990
 
1991
service = sql
1992
driver = native_mysql
1993
user = $DB_USER
1994
password = $radiuspwd
1995
pc = localhost
1996
database = $DB_GAMMU
1997
 
2631 rexy 1998
RunOnReceive = sudo $DIR_DEST_BIN/alcasar-sms.sh --new_sms
1376 richard 1999
 
2000
StatusFrequency = 30
1380 richard 2001
;LoopSleep = 2
1376 richard 2002
 
2003
;ResetFrequency = 300
2004
;HardResetFrequency = 120
2005
 
2454 tom.houday 2006
CheckSecurity = 1
1376 richard 2007
CheckSignal = 1
2008
CheckBattery = 0
2009
EOF
2601 tom.houday 2010
	chmod 755 /etc/gammu_smsd_conf /etc/gammurc
1376 richard 2011
 
2601 tom.houday 2012
# Create the systemd unit
2937 rexy 2013
	cat << EOF > /etc/systemd/system/gammu-smsd.service
2601 tom.houday 2014
[Unit]
2015
Description=SMS daemon for Gammu
2016
Documentation=man:gammu-smsd(1)
2017
After=network.target mysql.service
1376 richard 2018
 
2601 tom.houday 2019
[Service]
2020
Type=forking
2775 rexy 2021
ExecStart=/usr/bin/gammu-smsd --config /etc/gammu_smsd_conf --user=gammu_smsd --group=gammu_smsd --pid=/run/gammu-smsd.pid --daemon
2601 tom.houday 2022
ExecReload=/bin/kill -HUP $MAINPID
2775 rexy 2023
ExecStopPost=/bin/rm -f /run/gammu-smsd.pid
2024
PIDFile=/run/gammu-smsd.pid
2601 tom.houday 2025
 
2026
[Install]
2027
WantedBy=multi-user.target
2028
EOF
2029
 
2314 richard 2030
# Log folder for gammu-smsd
2772 rexy 2031
	[ -d /var/log/gammu-smsd ] || mkdir /var/log/gammu-smsd
2601 tom.houday 2032
	chmod 755 /var/log/gammu-smsd
1376 richard 2033
 
2552 rexy 2034
# Udev rule for Modeswitch (switch from "mass_storage" mode to "ttyUSB" modem) needed with some Huawei MODEM (idVendor: 12d1)
2035
# normally not needed now since modeswitch is managed by udev (see Mageia RPM)
2542 rexy 2036
#cat << EOF > /lib/udev/rules.d/66-huawei.rules
2037
#KERNEL=="ttyUSB0",ATTRS{idVendor}=="12d1",RUN+="$DIR_DEST_BIN/alcasar-sms.sh --mode"
2038
#EOF
2552 rexy 2039
# Udev rule for fixing the enumeration of ttyUSB port on some MODEM (when they switch randomly the order of their ports at boot time)
2040
# example : http://hintshop.ludvig.co.nz/show/persistent-names-usb-serial-devices/
2041
 
2724 rexy 2042
} # End of gammu_smsd()
1376 richard 2043
 
2552 rexy 2044
############################################################
2045
##                 Fonction "msec"                        ##
2046
## - Apply the "fileserver" security level                ##
2863 rexy 2047
## - remove the "system request" for rebooting            ##
2552 rexy 2048
## - Fix several file permissions                         ##
2049
############################################################
2202 richard 2050
msec()
2051
{
2052
 
2053
# Apply fileserver security level
2211 richard 2054
[ -e /etc/security/msec/security.conf.default ] || cp /etc/security/msec/security.conf /etc/security/msec/security.conf.default
2055
echo "BASE_LEVEL=fileserver" > /etc/security/msec/security.conf
2202 richard 2056
 
2203 richard 2057
# Set permissions monitoring and enforcement
2202 richard 2058
cat <<EOF > /etc/security/msec/perm.local
2801 rexy 2059
/var/log/firewall/                      root.apache     750
2202 richard 2060
/var/log/firewall/*                     root.apache     640
2061
/etc/security/msec/perm.local           root.root       640
2062
/etc/security/msec/level.local          root.root       640
2063
/etc/freeradius-web                     root.apache     750
2064
/etc/freeradius-web/admin.conf          root.apache     640
2420 richard 2065
/etc/raddb/client.conf                  radius.radius   640
2066
/etc/raddb/radius.conf                  radius.radius   640
2067
/etc/raddb/mods-available/ldap          radius.apache   660
2202 richard 2068
/etc/raddb/sites-available/alcasar      radius.apache   660
2863 rexy 2069
/etc/pki/CA/                            root.apache     750 force
2070
/etc/pki/CA/*                           root.apache     640 force 
2071
/etc/pki/CA/private/                    root.root       700 force
2072
/etc/pki/CA/private/*                   root.root       600 force
2073
/etc/pki/tls/private/                   root.apache     750 force
2074
/etc/pki/tls/private/*                  root.apache     640 force
2865 rexy 2075
/var/log/clamav/                        e2guardian.e2guardian   755 force
2076
/var/log/clamav/*                       e2guardian.e2guardian   764 force
2077
/var/lib/clamav/                        e2guardian.e2guardian   755 force
2202 richard 2078
EOF
2454 tom.houday 2079
# apply now hourly & daily checks
2202 richard 2080
/usr/sbin/msec
2211 richard 2081
/etc/cron.weekly/msec
2202 richard 2082
 
2724 rexy 2083
} # End of msec()
2202 richard 2084
 
2085
##################################################################
2552 rexy 2086
##                   Fonction "letsencrypt"                     ##
2087
## - Install Let's Encrypt client                               ##
2088
## - Prepare Let's Encrypt ALCASAR configuration file           ##
2304 tom.houday 2089
##################################################################
2090
letsencrypt()
2091
{
2092
	echo "Installing Let's Encrypt client..."
2586 tom.houday 2093
	# Remove potential old installers
2094
	rm -rf /tmp/acme.sh-*
2304 tom.houday 2095
	# Extract acme.sh
2096
	tar xzf ./conf/letsencrypt-client/acme.sh-*.tar.gz -C /tmp/
2097
	pwdInstall=$(pwd)
2688 lucas.echa 2098
	cd /tmp/acme.sh-* || { echo "Unable to find ACME directory"; exit 1; }
2304 tom.houday 2099
	acmesh_installDir="/opt/acme.sh"
2100
	acmesh_confDir="/usr/local/etc/letsencrypt"
2354 tom.houday 2101
	acmesh_userAgent="ALCASAR"
2304 tom.houday 2102
	# Install acme.sh
2103
	./acme.sh --install \
2104
		--home $acmesh_installDir \
2105
		--config-home $acmesh_confDir/data \
2106
		--certhome $acmesh_confDir/certs \
2107
		--accountkey $acmesh_confDir/ca/account.key \
2108
		--accountconf $acmesh_confDir/data/account.conf \
2109
		--useragent $acmesh_userAgent \
2308 tom.houday 2110
		--nocron \
2111
		> /dev/null
2304 tom.houday 2112
	if [ $? -ne 0 ]; then
2113
		echo "Error during installation of Let's Encrypt client (acme.sh)."
2114
	fi
2115
	# Create configuration file
2116
	cat <<EOF > /usr/local/etc/alcasar-letsencrypt
2117
email=
2118
dateIssueRequest=
2119
domainRequest=
2120
challenge=
2121
dateIssued=
2122
dnsapi=
2123
dateNextRenewal=
2124
EOF
2688 lucas.echa 2125
	cd $pwdInstall || { echo "Unable to find $pwdInstall directory"; exit 1; }
2304 tom.houday 2126
	rm -rf /tmp/acme.sh-*
2724 rexy 2127
} # End of letsencrypt()
2304 tom.houday 2128
 
2129
##################################################################
2552 rexy 2130
##                    Fonction "post_install"                   ##
2131
## - Modifying banners (locals et ssh) & prompts                ##
2132
## - SSH config                                                 ##
2133
## - sudoers config & files security                            ##
2134
## - log rotate & ANSSI security parameters                     ##
2135
## - Apply former conf in case of an update                     ##
2136
##################################################################
1 root 2137
post_install()
2138
{
2840 rexy 2139
# change the SSHD options
2195 richard 2140
	cp -f $DIR_CONF/banner /etc/ssh/alcasar-banner-ssh
2141
	echo " V$VERSION" >> /etc/ssh/alcasar-banner-ssh
5 franck 2142
	chmod 644 /etc/ssh/alcasar-banner-ssh ; chown root:root /etc/ssh/alcasar-banner-ssh
1 root 2143
	[ -e /etc/ssh/sshd_config.default ] || cp /etc/ssh/sshd_config /etc/ssh/sshd_config.default
2144
	$SED "s?^Banner.*?Banner /etc/ssh/alcasar-banner-ssh?g" /etc/ssh/sshd_config
2145
	$SED "s?^#Banner.*?Banner /etc/ssh/alcasar-banner-ssh?g" /etc/ssh/sshd_config
2840 rexy 2146
# sshd listens on EXTIF & INTIF
2147
	$SED "s?^#ListenAddress 0\.0\.0\.0.*?ListenAddress 0\.0\.0\.0?g" /etc/ssh/sshd_config
2148
# sshd authorized certificate for root login
2149
	$SED "s?^PermitRootLogin.*?PermitRootLogin without-password?g" /etc/ssh/sshd_config
2150
	$SED "s?^X11Forwarding.*?#X11Forwarding yes?g" /etc/ssh/sshd_config
2151
 
793 richard 2152
# postfix banner anonymisation
2688 lucas.echa 2153
	$SED "s?^smtpd_banner =.*?smtpd_banner = \$myhostname ESMTP?g" /etc/postfix/main.cf
1841 richard 2154
	chown -R postfix:postfix /var/lib/postfix
2195 richard 2155
# ALCASAR conf file
2818 rexy 2156
	echo "HTTPS_LOGIN=off" >> $CONF_FILE
2409 tom.houday 2157
	echo "HTTPS_CHILLI=off" >> $CONF_FILE
1839 richard 2158
	echo "SSH=on" >> $CONF_FILE
1631 richard 2159
	echo "SSH_ADMIN_FROM=0.0.0.0/0.0.0.0" >> $CONF_FILE
628 richard 2160
	echo "LDAP=off" >> $CONF_FILE
2447 richard 2161
	echo "LDAP_SERVER=127.0.0.1" >> $CONF_FILE
2461 richard 2162
	echo "LDAP_BASE=cn=Users;dc=serverad;dc=localdomain" >> $CONF_FILE
2454 tom.houday 2163
	echo "LDAP_UID=sAMAccountName" >> $CONF_FILE
2164
	echo "LDAP_FILTER=" >> $CONF_FILE
2165
	echo "LDAP_USER=alcasar" >> $CONF_FILE
2166
	echo "LDAP_PASSWORD=" >> $CONF_FILE
2705 tom.houday 2167
	echo "LDAP_SSL=on" >> $CONF_FILE
2168
	echo "LDAP_CERT_REQUIRED=" >> $CONF_FILE
2600 tom.houday 2169
	echo "SMS=off" >> $CONF_FILE
2170
	echo "SMS_NUM=" >> $CONF_FILE
2688 lucas.echa 2171
	echo "BL_PUREIP=on" >> $CONF_FILE
2172
	echo "BL_SAFESEARCH=off" >> $CONF_FILE
2173
	echo "WL_SAFESEARCH=off" >> $CONF_FILE
2770 rexy 2174
	echo "IOT_CAPTURE=off" >> $CONF_FILE
2926 rexy 2175
	echo "WIFI4EU=off" >> $CONF_FILE
2176
	echo "WIFI4EU_CODE=123e4567-e89b-12d3-a456-426655440000" >> $CONF_FILE
2979 rexy 2177
	echo "MULTIWAN=off" >> $CONF_FILE
2195 richard 2178
# Prompt customisation (colors)
1 root 2179
	[ -e /etc/bashrc.default ]  || cp /etc/bashrc /etc/bashrc.default
5 franck 2180
	cp -f $DIR_CONF/bashrc /etc/. ; chmod 644 /etc/bashrc ; chown root:root /etc/bashrc
630 franck 2181
	$SED "s?^ORGANISME.*?ORGANISME=$ORGANISME?g" /etc/bashrc
2941 rexy 2182
	$SED "s?^alias ll=.*?alias ll=\"ls -al --color\"?g" /etc/profile.d/60alias.sh
2195 richard 2183
# sudoers configuration for "apache" & "sysadmin"
1 root 2184
	[ -e /etc/sudoers.default ]  || cp /etc/sudoers /etc/sudoers.default
2850 rexy 2185
	cp -f $DIR_CONF/sudoers /etc/ ; chmod 440 /etc/sudoers ; chown root:root /etc/sudoers
629 richard 2186
	$SED "s?^Host_Alias.*?Host_Alias	LAN_ORG=$PRIVATE_NETWORK/$PRIVATE_NETMASK,localhost		#réseau de l'organisme?g" /etc/sudoers
1543 richard 2187
# Modify some logrotate files (gammu, ulogd)
1 root 2188
	cp -f $DIR_CONF/logrotate.d/* /etc/logrotate.d/
2189
	chmod 644 /etc/logrotate.d/*
2195 richard 2190
# Log compression
706 franck 2191
	$SED "s?^delaycompress.*?#&?g" /etc/logrotate.conf
2192
# actualisation des fichiers logs compressés
2521 armand.ito 2193
	for dir in firewall e2guardian lighttpd
706 franck 2194
	do
2688 lucas.echa 2195
		find /var/log/$dir -type f -name "*.log-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -exec gzip {} \;
706 franck 2196
	done
2601 tom.houday 2197
	/usr/bin/systemctl daemon-reload
2956 rexy 2198
# processes started at boot time (Systemctl)
2199
	for i in alcasar-network mysqld lighttpd php-fpm ntpd iptables unbound unbound-blacklist unbound-whitelist dnsmasq-whitelist unbound-blackhole radiusd nfcapd e2guardian clamav-daemon clamav-freshclam ulogd-ssh ulogd-traceability ulogd-ext-access chilli fail2ban vnstat sshd
1221 richard 2200
	do
1574 richard 2201
		/usr/bin/systemctl -q enable $i.service
1221 richard 2202
	done
2454 tom.houday 2203
 
1452 richard 2204
# disable processes at boot time (Systemctl)
2688 lucas.echa 2205
	for i in ulogd gpm dhcpd
1452 richard 2206
	do
1574 richard 2207
		/usr/bin/systemctl -q disable $i.service
1452 richard 2208
	done
2454 tom.houday 2209
 
2864 rexy 2210
# Apply some security rules (some are from French cybersecurity Agency - ANSSI)
1362 richard 2211
# ignore ICMP broadcast (smurf attack)
2212
	echo "net.ipv4.icmp_echo_ignore_broadcasts = 1" > /etc/sysctl.d/alcasar.conf
2213
# ignore ICMP errors bogus
2214
	echo "net.ipv4.icmp_ignore_bogus_error_responses = 1" >> /etc/sysctl.d/alcasar.conf
2215
# remove ICMP redirects responces
2216
	echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.d/alcasar.conf
2217
	echo "net.ipv4.conf.all.send_redirects = 0" >> /etc/sysctl.d/alcasar.conf
2218
# enable SYN Cookies (Syn flood attacks)
2219
	echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.d/alcasar.conf
2220
# enable kernel antispoofing
2221
	echo "net.ipv4.conf.all.rp_filter = 1" >> /etc/sysctl.d/alcasar.conf
2222
# ignore source routing
2223
	echo "net.ipv4.conf.all.accept_source_route = 0" >> /etc/sysctl.d/alcasar.conf
2224
# set conntrack timer to 1h (3600s) instead of 5 weeks
2225
	echo "net.netfilter.nf_conntrack_tcp_timeout_established = 3600" >> /etc/sysctl.d/alcasar.conf
2454 tom.houday 2226
# disable log_martians (ALCASAR is often installed between two private network addresses)
1363 richard 2227
	echo "net.ipv4.conf.all.log_martians = 0" >> /etc/sysctl.d/alcasar.conf
1778 richard 2228
# disable iptables_helpers
2229
	echo "net.netfilter.nf_conntrack_helper = 0" >> /etc/sysctl.d/alcasar.conf
1788 richard 2230
# Switch to the router mode
2231
	echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/alcasar.conf
1917 franck 2232
# Remove unused service ipv6
1964 franck 2233
	echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.d/alcasar.conf
2234
	echo "net.ipv6.conf.all.autoconf = 0" >> /etc/sysctl.d/alcasar.conf
2235
	echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.d/alcasar.conf
2236
	echo "net.ipv6.conf.default.autoconf = 0" >> /etc/sysctl.d/alcasar.conf
1003 richard 2237
# switch to multi-users runlevel (instead of x11)
1221 richard 2238
	ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
2864 rexy 2239
# disable Core dump file
2240
	[ -e /etc/security/limits.conf.default ]  || cp /etc/security/limits.conf /etc/security/limits.conf.default
2241
	$SED "/^# End of file.*/i*\tsoft\tcore\t0\n*\thard\tcore\t0" /etc/security/limits.conf
2242
 
2432 richard 2243
# GRUB2 modifications (Wait time : 3s - ALCASAR entry - VGA=791 - Change the default banner
2244
	[ -e /etc/default/grub.default ]  || cp /etc/default/grub /etc/default/grub.default
2245
	$SED "s?^GRUB_TIMEOUT=.*?GRUB_TIMEOUT=3?g" /etc/default/grub
2433 richard 2246
	$SED "s?^GRUB_DISTRIBUTOR=.*?GRUB_DISTRIBUTOR=ALCASAR?g" /etc/default/grub
2432 richard 2247
	[ -e /etc/mageia-release.default ]  || cp /etc/mageia-release /etc/mageia-release.default
2841 rexy 2248
	vm_vga=`lsmod | egrep -c "virtio|vmwgfx"` # test if in VM
2454 tom.houday 2249
	if [ $vm_vga == 0 ] # is not a VM
2432 richard 2250
	then
2454 tom.houday 2251
		cp -f $DIR_CONF/banner /etc/mageia-release # ALCASAR ASCII-Art
2252
		echo >> /etc/mageia-release
2253
		$SED "s?^GRUB_CMDLINE_LINUX_DEFAULT=\"?&vga=791 ?" /etc/default/grub
2254
	fi
2255
	if [ $Lang == "fr" ]
2256
	then
2257
		echo "Bienvenue sur ALCASAR V$VERSION" >> /etc/mageia-release
2688 lucas.echa 2258
		echo "Connectez-vous à l'URL 'https://$HOSTNAME.$DOMAIN/acc'" >> /etc/mageia-release
2454 tom.houday 2259
	else
2260
		echo "Welcome on ALCASAR V$VERSION" >> /etc/mageia-release
2688 lucas.echa 2261
		echo "Connect to 'https://$HOSTNAME.$DOMAIN/acc'" >> /etc/mageia-release
2454 tom.houday 2262
	fi
2263
	/usr/bin/update-grub2
1221 richard 2264
# Load and apply the previous conf file
2265
	if [ "$mode" = "update" ]
532 richard 2266
	then
2887 rexy 2267
		$DIR_DEST_BIN/alcasar-archive.sh --now # exports current logs in $DIR_SAVE/archive
1221 richard 2268
		$DIR_DEST_BIN/alcasar-conf.sh --load
2269
		PARENT_SCRIPT=`basename $0`
2270
		export PARENT_SCRIPT # to avoid stop&start process during the installation process
2271
		$DIR_DEST_BIN/alcasar-conf.sh --apply
2454 tom.houday 2272
		$DIR_DEST_BIN/alcasar-file-clean.sh # Clean & sort conf files. Add uamallowed domains to the dns-blackhole conf
1221 richard 2273
		$SED "s?^INSTALL_DATE=.*?INSTALL_DATE=$DATE?g" $CONF_FILE
2274
		$SED "s?^VERSION=.*?VERSION=$VERSION?g" $CONF_FILE
532 richard 2275
	fi
2560 rexy 2276
	rm -f /var/tmp/alcasar-conf*
1221 richard 2277
	chown -R root:apache $DIR_DEST_ETC/*
2278
	chmod -R 660 $DIR_DEST_ETC/*
2279
	chmod ug+x $DIR_DEST_ETC/digest
2688 lucas.echa 2280
	cd $DIR_INSTALL || { echo "Unable to find $DIR_INSTALL directory"; exit 1; }
5 franck 2281
	echo ""
1 root 2282
	echo "#############################################################################"
638 richard 2283
	if [ $Lang == "fr" ]
2284
		then
2285
		echo "#                        Fin d'installation d'ALCASAR                       #"
2286
		echo "#                                                                           #"
2287
		echo "#         Application Libre pour le Contrôle Authentifié et Sécurisé        #"
2288
		echo "#                     des Accès au Réseau ( ALCASAR )                       #"
2289
		echo "#                                                                           #"
2290
		echo "#############################################################################"
2291
		echo
2292
		echo "- ALCASAR sera fonctionnel après redémarrage du système"
2293
		echo
2294
		echo "- Lisez attentivement la documentation d'exploitation"
2295
		echo
2688 lucas.echa 2296
		echo "- Le centre de controle d'ALCASAR (ACC) est à l'adresse http://$HOSTNAME.$DOMAIN"
638 richard 2297
		echo
2298
		echo "                   Appuyez sur 'Entrée' pour continuer"
2454 tom.houday 2299
	else
2396 tom.houday 2300
		echo "#                        End of ALCASAR install process                     #"
638 richard 2301
		echo "#                                                                           #"
2302
		echo "#         Application Libre pour le Contrôle Authentifié et Sécurisé        #"
2303
		echo "#                     des Accès au Réseau ( ALCASAR )                       #"
2304
		echo "#                                                                           #"
2305
		echo "#############################################################################"
2306
		echo
2307
		echo "- The system will be rebooted in order to operate ALCASAR"
2308
		echo
2309
		echo "- Read the exploitation documentation"
2310
		echo
2688 lucas.echa 2311
		echo "- The ALCASAR Control Center (ACC) is at http://$HOSTNAME.$DOMAIN"
638 richard 2312
		echo
2313
		echo "                   Hit 'Enter' to continue"
2314
	fi
1782 franck 2315
	sleep 2
2560 rexy 2316
	if [ "$mode" == "install" ] || [ "$DEBUG_ALCASAR" == "on" ]
820 richard 2317
	then
2688 lucas.echa 2318
		read
815 richard 2319
	fi
774 richard 2320
	clear
1 root 2321
	reboot
2724 rexy 2322
} # End of post_install()
1 root 2323
 
2552 rexy 2324
#####################################################################################
2325
#                                   Main Install loop                               #
2326
#####################################################################################
832 richard 2327
dir_exec=`dirname "$0"`
2328
if [ $dir_exec != "." ]
2329
then
2330
	echo "Lancez ce programme depuis le répertoire de l'archive d'ALCASAR"
2331
	echo "Launch this program from the ALCASAR archive directory"
2332
	exit 0
2333
fi
2688 lucas.echa 2334
if [ $EUID -gt 0 ]
2220 richard 2335
then
2688 lucas.echa 2336
	echo "Vous devez être \"root\" pour installer ALCASAR (commande 'su')"
2337
	echo "You must be \"root\" to install ALCASAR ('su' command)"
2220 richard 2338
	exit 0
2339
fi
832 richard 2340
VERSION=`cat $DIR_INSTALL/VERSION`
291 franck 2341
usage="Usage: alcasar.sh {-i or --install} | {-u or --uninstall}"
1 root 2342
nb_args=$#
2343
args=$1
2344
if [ $nb_args -eq 0 ]
2345
then
2346
	nb_args=1
2347
	args="-h"
2348
fi
1062 richard 2349
chmod -R u+x $DIR_SCRIPTS/*
1 root 2350
case $args in
2351
	-\? | -h* | --h*)
2352
		echo "$usage"
2353
		exit 0
2354
		;;
291 franck 2355
	-i | --install)
2937 rexy 2356
		for func in license testing_system testing_network
2724 rexy 2357
		do
2358
			header_install
2359
			$func
2360
			if [ $DEBUG_ALCASAR == "on" ]
2361
			then
2870 rexy 2362
				echo "*** 'debug' : end of function '$func' ***"
2724 rexy 2363
				read
2364
			fi
2365
		done
595 richard 2366
# RPMs install
2941 rexy 2367
		$DIR_SCRIPTS/alcasar-rpm.sh
595 richard 2368
		if [ "$?" != "0" ]
1 root 2369
		then
595 richard 2370
			exit 0
2371
		fi
1249 richard 2372
		if [ -e $CONF_FILE ]
595 richard 2373
		then
2458 richard 2374
# Uninstall or update the running version
2375
			if [ "$mode" == "update" ]
2376
			then
2560 rexy 2377
				$DIR_DEST_BIN/alcasar-uninstall.sh -update
2458 richard 2378
			else
2560 rexy 2379
				$DIR_DEST_BIN/alcasar-uninstall.sh -full
2458 richard 2380
			fi
595 richard 2381
		fi
2847 rexy 2382
		if [ $DEBUG_ALCASAR == "on" ]
2383
		then
2384
			echo "*** 'debug' : end of cleaning ***"
2385
			read
2386
		fi
2980 rexy 2387
# Avoid some RPM automatic updates
2388
		echo "/^kernel/" > /etc/urpmi/skip.list
2389
		echo "/^wkhtmltopdf/" >> /etc/urpmi/skip.list
2390
		echo "/^clamd/" >> /etc/urpmi/skip.list
2391
		echo "/^clamav/" >> /etc/urpmi/skip.list
2392
		echo "/^freeradius/" >> /etc/urpmi/skip.list
2393
		if [ `egrep '^exclude=' /etc/dnf/dnf.conf |wc -l` -eq "1" ]; then
2394
			$SED "s?^exclude=.*?exclude=kernel\* wkhtmltopdf clamd clamav freeradius?g" /etc/dnf/dnf.conf
2395
		else
2396
			echo "exclude=kernel* wkhtmltopdf clamd clamav freeradius" >> /etc/dnf/dnf.conf
2397
		fi
2835 rexy 2398
# Test if conf file
2399
		if [ -e /var/tmp/alcasar-conf.tar.gz ]
595 richard 2400
		then
2826 rexy 2401
# Extract some info from the previous configuration file
2834 rexy 2402
			cd /var/tmp
2403
			tar -xf /var/tmp/alcasar-conf.tar.gz conf/etc/alcasar.conf
2847 rexy 2404
			if [ "$mode" == "install" ] # don't display this if updating a running version
2834 rexy 2405
			then
2406
				header_install
2407
				ORGANISME=`grep ^ORGANISM= conf/etc/alcasar.conf|cut -d"=" -f2`
2408
				PREVIOUS_VERSION=`grep ^VERSION= conf/etc/alcasar.conf|cut -d"=" -f2`
2409
				MAJ_PREVIOUS_VERSION=`echo $PREVIOUS_VERSION|cut -d"." -f1`
2410
				MIN_PREVIOUS_VERSION=`echo $PREVIOUS_VERSION|cut -d"." -f2`
2411
				UPD_PREVIOUS_VERSION=`echo $PREVIOUS_VERSION|cut -d"." -f3|cut -c1`
597 richard 2412
				if [ $Lang == "fr" ]
2834 rexy 2413
					then echo "Le fichier de configuration d'une version $MAJ_PREVIOUS_VERSION.$MIN_PREVIOUS_VERSION.$UPD_PREVIOUS_VERSION a été trouvé";
2956 rexy 2414
					else echo "The configuration file of a version $MAJ_PREVIOUS_VERSION.$MIN_PREVIOUS_VERSION.$UPD_PREVIOUS_VERSION has been found";
597 richard 2415
				fi
2834 rexy 2416
				response=0
2417
				PTN='^[oOnNyY]?$'
2418
				until [[ "$response" =~ $PTN ]]
2419
				do
2420
					if [ $Lang == "fr" ]
2421
						then echo -n "Voulez-vous l'utiliser (O/n)? ";
2422
						else echo -n "Do you want to use it (Y/n)?";
2423
					fi
2424
					read response
2425
					if [ "$response" = "n" ] || [ "$response" = "N" ]
2426
					then
2427
						rm -f /var/tmp/alcasar-conf*
2428
						rm -rf /var/tmp/conf
2429
					fi
2430
				done
2431
			fi
2847 rexy 2432
			cd $DIR_INSTALL
597 richard 2433
		fi
636 richard 2434
# Test if update
2834 rexy 2435
		if [ -e /var/tmp/alcasar-conf.tar.gz ]
597 richard 2436
		then
2437
			if [ $Lang == "fr" ]
2438
				then echo "#### Installation avec mise à jour ####";
2439
				else echo "#### Installation with update     ####";
2440
			fi
5 franck 2441
			mode="update"
1 root 2442
		fi
2937 rexy 2443
		for func in init network CA ACC time_server init_db freeradius chilli e2guardian antivirus ulogd nfsen vnstat dnsmasq unbound dhcpd BL cron fail2ban gammu_smsd msec letsencrypt post_install
5 franck 2444
		do
2445
			$func
2466 richard 2446
			if [ $DEBUG_ALCASAR == "on" ]
2688 lucas.echa 2447
			then
2870 rexy 2448
				echo "*** 'debug' : end of function '$func' ***"
2688 lucas.echa 2449
				read
2466 richard 2450
			fi
14 richard 2451
		done
5 franck 2452
		;;
291 franck 2453
	-u | --uninstall)
1828 richard 2454
		if [ ! -e $DIR_DEST_BIN/alcasar-uninstall.sh ]
1 root 2455
		then
597 richard 2456
			if [ $Lang == "fr" ]
2457
				then echo "ALCASAR n'est pas installé!";
2458
				else echo "ALCASAR isn't installed!";
2459
			fi
1 root 2460
			exit 0
2461
		fi
5 franck 2462
		response=0
2760 lucas.echa 2463
		PTN='^[oOyYnN]?$'
2464
		until [[ "$response" =~ $PTN ]]
5 franck 2465
		do
597 richard 2466
			if [ $Lang == "fr" ]
2760 lucas.echa 2467
				then echo -n "Voulez-vous créer le fichier de configuration de la version actuelle (O/n)? ";
854 richard 2468
				else echo -n "Do you want to create the running version configuration file (Y/n)? ";
597 richard 2469
			fi
5 franck 2470
			read response
2471
		done
1103 richard 2472
		if [ "$response" = "o" ] || [ "$response" = "O" ] || [ "$response" = "Y" ] || [ "$response" = "y" ]
1 root 2473
		then
1103 richard 2474
			$DIR_SCRIPTS/alcasar-conf.sh --create
2454 tom.houday 2475
		else
2560 rexy 2476
			rm -f /var/tmp/alcasar-conf*
1 root 2477
		fi
597 richard 2478
# Uninstall the running version
2560 rexy 2479
		$DIR_DEST_BIN/alcasar-uninstall.sh -full
1 root 2480
		;;
2481
	*)
2482
		echo "Argument inconnu :$1";
460 richard 2483
		echo "Unknown argument :$1";
1 root 2484
		echo "$usage"
2485
		exit 1
2486
		;;
2487
esac
10 franck 2488
# end of script