Subversion Repositories ALCASAR

Rev

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

Rev 2714 Rev 2715
1
#!/bin/bash
1
#!/bin/bash
2
 
2
 
3
# $Id: alcasar-ldap.sh 2714 2019-03-10 23:43:22Z tom.houdayer $
3
# $Id: alcasar-ldap.sh 2715 2019-03-10 23:53:44Z tom.houdayer $
4
 
4
 
5
# alcasar-ldap.sh
5
# alcasar-ldap.sh
6
# by Rexy
6
# by Rexy
7
# This script is distributed under the Gnu General Public License (GPL)
7
# This script is distributed under the Gnu General Public License (GPL)
8
 
8
 
9
# activation / désactivation de l'authentification des utilisateurs via un serveur LDAP externe
9
# activation / désactivation de l'authentification des utilisateurs via un serveur LDAP externe
10
# enable / disable authentication of users via an extern LDAP server
10
# enable / disable authentication of users via an extern LDAP server
11
 
11
 
12
usage="Usage: alcasar-ldap.sh {--on or -on } | {--off or -off} | --import-cert {certificatePath} | --test [-d]"
12
usage="Usage: alcasar-ldap.sh {--on or -on } | {--off or -off} | --import-cert {certificatePath} | --test [-d]"
13
SED="/bin/sed -i"
13
SED="/bin/sed -i"
14
CONF_FILE="/usr/local/etc/alcasar.conf"
14
CONF_FILE="/usr/local/etc/alcasar.conf"
15
LDAP_MODULE="/etc/raddb/mods-available/ldap-alcasar"
15
LDAP_MODULE="/etc/raddb/mods-available/ldap-alcasar"
16
OPENLDAP_CONF='/etc/openldap/ldap.conf'
16
OPENLDAP_CONF='/etc/openldap/ldap.conf'
17
LDAPS_CERT_LOC='/etc/raddb/certs/alcasar-ldaps.crt'
17
LDAPS_CERT_LOC='/etc/raddb/certs/alcasar-ldaps.crt'
18
LDAP_SERVER=$(grep '^LDAP_SERVER=' $CONF_FILE | cut -d"=" -f2)                # hostname/IP address of the LDAP server
18
LDAP_SERVER=$(grep '^LDAP_SERVER=' $CONF_FILE | cut -d"=" -f2)                # hostname/IP address of the LDAP server
19
LDAP_USER=$(grep '^LDAP_USER=' $CONF_FILE | cut -d"=" -f2-)                   # LDAP username used by ALCASAR to read the remote directory
19
LDAP_USER=$(grep '^LDAP_USER=' $CONF_FILE | cut -d"=" -f2-)                   # LDAP username used by ALCASAR to read the remote directory
20
LDAP_PASSWORD=$(grep '^LDAP_PASSWORD=' $CONF_FILE | cut -d"=" -f2-)           # its password
20
LDAP_PASSWORD=$(grep '^LDAP_PASSWORD=' $CONF_FILE | cut -d"=" -f2-)           # its password
21
LDAP_BASE=$(grep '^LDAP_BASE=' $CONF_FILE | cut -d"=" -f2-)                   # Where to find the users (cn=**,dc=**,dc=**)
21
LDAP_BASE=$(grep '^LDAP_BASE=' $CONF_FILE | cut -d"=" -f2-)                   # Where to find the users (cn=**,dc=**,dc=**)
22
LDAP_UID=$(grep '^LDAP_UID=' $CONF_FILE | cut -d"=" -f2)                      # 'samaccountname' for A.D. - 'UID' for LDAP
22
LDAP_UID=$(grep '^LDAP_UID=' $CONF_FILE | cut -d"=" -f2)                      # 'samaccountname' for A.D. - 'UID' for LDAP
23
LDAP_FILTER=$(grep '^LDAP_FILTER=' $CONF_FILE | cut -d"=" -f2-)               # LDAP filter
23
LDAP_FILTER=$(grep '^LDAP_FILTER=' $CONF_FILE | cut -d"=" -f2-)               # LDAP filter
24
LDAP_SSL=$(grep '^LDAP_SSL=' $CONF_FILE | cut -d"=" -f2-)                     # LDAP SSL status
24
LDAP_SSL=$(grep '^LDAP_SSL=' $CONF_FILE | cut -d"=" -f2-)                     # LDAP SSL status
25
LDAP_CERT_REQUIRED=$(grep '^LDAP_CERT_REQUIRED=' $CONF_FILE | cut -d"=" -f2-) # LDAP SSL certificate verifying
25
LDAP_CERT_REQUIRED=$(grep '^LDAP_CERT_REQUIRED=' $CONF_FILE | cut -d"=" -f2-) # LDAP SSL certificate verifying
26
 
26
 
-
 
27
add_ldap_server_to_static_dhcp() {
-
 
28
	if [[ "$LDAP_SERVER" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
-
 
29
		ldap_server_ip="$LDAP_SERVER"
-
 
30
	else
-
 
31
		ldap_server_ip=$(dig +short $LDAP_SERVER)
-
 
32
		[ -z "$ldap_server_ip" ] && return 1
-
 
33
	fi
-
 
34
 
-
 
35
	if [ -z "$(cat /usr/local/etc/alcasar-ethers | awk -v ldap_server_ip="$ldap_server_ip" '($2==ldap_server_ip)')" ]; then
-
 
36
		ldap_server_mac=$(chilli_query list | awk -v ldap_server_ip="$ldap_server_ip" '($2==ldap_server_ip) {print $1}')
-
 
37
		[ -z "$ldap_server_mac" ] && return 1
-
 
38
 
-
 
39
		echo "$ldap_server_mac $ldap_server_ip" >> /usr/local/etc/alcasar-ethers
-
 
40
		echo "$ldap_server_mac $ldap_server_ip #LDAP Server" >> /usr/local/etc/alcasar-ethers-info
-
 
41
	fi
-
 
42
}
-
 
43
 
27
nb_args=$#
44
nb_args=$#
28
args=$1
45
args=$1
29
if [ $nb_args -eq 0 ]; then
46
if [ $nb_args -eq 0 ]; then
30
	nb_args=1
47
	nb_args=1
31
	args="-h"
48
	args="-h"
32
fi
49
fi
33
 
50
 
34
case $args in
51
case $args in
35
	-\? | -h* | --h*)
52
	-\? | -h* | --h*)
36
		echo "$usage"
53
		echo "$usage"
37
		exit 0
54
		exit 0
38
		;;
55
		;;
39
	--on | -on)
56
	--on | -on)
40
		$SED "s/^LDAP=.*/LDAP=on/g" $CONF_FILE
57
		$SED "s/^LDAP=.*/LDAP=on/g" $CONF_FILE
41
		if [ "$LDAP_SSL" == 'on' ]; then
58
		if [ "$LDAP_SSL" == 'on' ]; then
42
			$SED "s/^\tserver =.*/\tserver = \"ldaps:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
59
			$SED "s/^\tserver =.*/\tserver = \"ldaps:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
43
			$SED "s/^\tport =.*/\tport = 636/g" $LDAP_MODULE
60
			$SED "s/^\tport =.*/\tport = 636/g" $LDAP_MODULE
44
			[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
61
			[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
45
			$SED "s/^\t\t#?require_cert =.*/\t\trequire_cert = '$require_cert'/g" $LDAP_MODULE
62
			$SED "s/^\t\t#?require_cert =.*/\t\trequire_cert = '$require_cert'/g" $LDAP_MODULE
46
			echo -e "TLS_CACERT $LDAPS_CERT_LOC\nTLS_REQCERT $require_cert" > $OPENLDAP_CONF
63
			echo -e "TLS_CACERT $LDAPS_CERT_LOC\nTLS_REQCERT $require_cert" > $OPENLDAP_CONF
47
		else
64
		else
48
			$SED "s/^\tserver =.*/\tserver = \"ldap:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
65
			$SED "s/^\tserver =.*/\tserver = \"ldap:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
49
			$SED "s/^\tport =.*/\tport = 389/g" $LDAP_MODULE
66
			$SED "s/^\tport =.*/\tport = 389/g" $LDAP_MODULE
50
			echo  '' > $OPENLDAP_CONF
67
			echo  '' > $OPENLDAP_CONF
51
		fi
68
		fi
52
		$SED "s/^\tidentity =.*/\tidentity = \"${LDAP_USER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
69
		$SED "s/^\tidentity =.*/\tidentity = \"${LDAP_USER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
53
		$SED "s/^\tpassword =.*/\tpassword = \"${LDAP_PASSWORD//\"/\\\\\\\"}\"/g" $LDAP_MODULE
70
		$SED "s/^\tpassword =.*/\tpassword = \"${LDAP_PASSWORD//\"/\\\\\\\"}\"/g" $LDAP_MODULE
54
		$SED "s/^\tbase_dn =.*/\tbase_dn = \"${LDAP_BASE//\"/\\\\\\\"}\"/g" $LDAP_MODULE
71
		$SED "s/^\tbase_dn =.*/\tbase_dn = \"${LDAP_BASE//\"/\\\\\\\"}\"/g" $LDAP_MODULE
55
		[ -n "$LDAP_FILTER" ] && filter="$LDAP_FILTER" || filter='&'
72
		[ -n "$LDAP_FILTER" ] && filter="$LDAP_FILTER" || filter='&'
56
		$SED "s/^\t\tfilter =.*/\t\tfilter = \"(\&(${LDAP_UID//\"/\\\\\\\"}=%{%{Stripped-User-Name}:-%{User-Name}})($filter))\"/g" $LDAP_MODULE
73
		$SED "s/^\t\tfilter =.*/\t\tfilter = \"(\&(${LDAP_UID//\"/\\\\\\\"}=%{%{Stripped-User-Name}:-%{User-Name}})($filter))\"/g" $LDAP_MODULE
57
		if [ ! -e /etc/raddb/mods-enabled/ldap ]; then
74
		if [ ! -e /etc/raddb/mods-enabled/ldap ]; then
58
			ln -s $LDAP_MODULE /etc/raddb/mods-enabled/ldap
75
			ln -s $LDAP_MODULE /etc/raddb/mods-enabled/ldap
59
		fi
76
		fi
60
		[ -e /etc/raddb/sites-enabled/alcasar ] && rm /etc/raddb/sites-enabled/alcasar
77
		[ -e /etc/raddb/sites-enabled/alcasar ] && rm /etc/raddb/sites-enabled/alcasar
61
		ln -s /etc/raddb/sites-available/alcasar-with-ldap /etc/raddb/sites-enabled/alcasar
78
		ln -s /etc/raddb/sites-available/alcasar-with-ldap /etc/raddb/sites-enabled/alcasar
-
 
79
		add_ldap_server_to_static_dhcp
62
		/usr/bin/systemctl restart radiusd.service
80
		/usr/bin/systemctl restart radiusd.service
63
		;;
81
		;;
64
	--off | -off)
82
	--off | -off)
65
		$SED "s/^LDAP=.*/LDAP=off/g" $CONF_FILE
83
		$SED "s/^LDAP=.*/LDAP=off/g" $CONF_FILE
66
		rm -f /etc/raddb/mods-enabled/ldap
84
		rm -f /etc/raddb/mods-enabled/ldap
67
		[ -e /etc/raddb/sites-enabled/alcasar ] && rm /etc/raddb/sites-enabled/alcasar
85
		[ -e /etc/raddb/sites-enabled/alcasar ] && rm /etc/raddb/sites-enabled/alcasar
68
		ln -s /etc/raddb/sites-available/alcasar /etc/raddb/sites-enabled/alcasar
86
		ln -s /etc/raddb/sites-available/alcasar /etc/raddb/sites-enabled/alcasar
69
		/usr/bin/systemctl restart radiusd.service
87
		/usr/bin/systemctl restart radiusd.service
70
		;;
88
		;;
71
	--import-cert)
89
	--import-cert)
72
		cert=$2
90
		cert=$2
73
		[ -z "$cert" ] && echo "$usage" && exit 1
91
		[ -z "$cert" ] && echo "$usage" && exit 1
74
 
92
 
75
		if [ "$LDAP_CERT_REQUIRED" == 'on' ]; then
93
		if [ "$LDAP_CERT_REQUIRED" == 'on' ]; then
76
			domainName=$(openssl x509 -noout -subject -in $LDAPS_CERT_LOC | cut -d' ' -f2- | sed 's@/[A-Za-z]\+=@\n@g' | tac | tr '\n' '.' | sed  's@\.\+$@@')
94
			domainName=$(openssl x509 -noout -subject -in $LDAPS_CERT_LOC | cut -d' ' -f2- | sed 's@/[A-Za-z]\+=@\n@g' | tac | tr '\n' '.' | sed  's@\.\+$@@')
77
			if [ "$domainName" != "$LDAP_SERVER" ]; then
95
			if [ "$domainName" != "$LDAP_SERVER" ]; then
78
				echo 'WARN: the common name of the certificate is different from the server domain name'
96
				echo 'WARN: the common name of the certificate is different from the server domain name'
79
			fi
97
			fi
80
		fi
98
		fi
81
		# TODO : convert DER format to PEM ?
99
		# TODO : convert DER format to PEM ?
82
		cp -f "$cert" $LDAPS_CERT_LOC
100
		cp -f "$cert" $LDAPS_CERT_LOC
83
		chown root:radius $LDAPS_CERT_LOC
101
		chown root:radius $LDAPS_CERT_LOC
84
		chmod 644 $LDAPS_CERT_LOC
102
		chmod 644 $LDAPS_CERT_LOC
85
 
103
 
86
		$SED "s/^LDAP_SSL=.*/LDAP_SSL=on/g" $CONF_FILE
104
		$SED "s/^LDAP_SSL=.*/LDAP_SSL=on/g" $CONF_FILE
87
		$SED "s/^\tserver =.*/\tserver = \"ldaps:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
105
		$SED "s/^\tserver =.*/\tserver = \"ldaps:\/\/${LDAP_SERVER//\"/\\\\\\\"}\"/g" $LDAP_MODULE
88
		$SED "s/^\tport =.*/\tport = 636/g" $LDAP_MODULE
106
		$SED "s/^\tport =.*/\tport = 636/g" $LDAP_MODULE
89
		$SED "s@^#\?\t\tca_file =.*@\t\tca_file = $LDAPS_CERT_LOC@g" $LDAP_MODULE
107
		$SED "s@^#\?\t\tca_file =.*@\t\tca_file = $LDAPS_CERT_LOC@g" $LDAP_MODULE
90
		[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
108
		[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
91
		$SED "s/^#\?\t\trequire_cert =.*/\t\trequire_cert = '$require_cert'/g" $LDAP_MODULE
109
		$SED "s/^#\?\t\trequire_cert =.*/\t\trequire_cert = '$require_cert'/g" $LDAP_MODULE
92
		echo -e "TLS_CACERT $LDAPS_CERT_LOC\nTLS_REQCERT $require_cert" > $OPENLDAP_CONF
110
		echo -e "TLS_CACERT $LDAPS_CERT_LOC\nTLS_REQCERT $require_cert" > $OPENLDAP_CONF
93
		/usr/bin/systemctl restart radiusd.service
111
		/usr/bin/systemctl restart radiusd.service
94
		;;
112
		;;
95
	--delete-cert)
113
	--delete-cert)
96
		[ -f "$LDAPS_CERT_LOC" ] && rm -f $LDAPS_CERT_LOC
114
		[ -f "$LDAPS_CERT_LOC" ] && rm -f $LDAPS_CERT_LOC
97
		;;
115
		;;
98
	--test)
116
	--test)
99
		[ -n "$2" ] && [ "$2" == '-d' ] && debugOpt='-d229'
117
		[ -n "$2" ] && [ "$2" == '-d' ] && debugOpt='-d229'
100
		command -v ldapsearch &>/dev/null || { echo >&2 -e "ERR: ldapsearch is not installed\nrun 'dnf install openldap-clients'" ; exit 1; }
118
		command -v ldapsearch &>/dev/null || { echo >&2 -e "ERR: ldapsearch is not installed\nrun 'dnf install openldap-clients'" ; exit 1; }
101
		if [ "$LDAP_SSL" == 'on' ]; then
119
		if [ "$LDAP_SSL" == 'on' ]; then
102
			protocol='ldaps'
120
			protocol='ldaps'
103
			[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
121
			[ "$LDAP_CERT_REQUIRED" == 'on' ] && require_cert='demand' || require_cert='never'
104
			export LDAPTLS_REQCERT="$require_cert"
122
			export LDAPTLS_REQCERT="$require_cert"
105
			[ -f "$LDAPS_CERT_LOC" ] && export LDAPTLS_CACERT="$LDAPS_CERT_LOC"
123
			[ -f "$LDAPS_CERT_LOC" ] && export LDAPTLS_CACERT="$LDAPS_CERT_LOC"
106
		else
124
		else
107
			protocol='ldap'
125
			protocol='ldap'
108
		fi
126
		fi
109
		[ -n "$LDAP_FILTER" ] && filter="$LDAP_FILTER" || filter='&'
127
		[ -n "$LDAP_FILTER" ] && filter="$LDAP_FILTER" || filter='&'
110
		/usr/bin/ldapsearch $debugOpt -LLL -H "$protocol://$LDAP_SERVER" -x -D "$LDAP_USER" -w "$LDAP_PASSWORD" -b "$LDAP_BASE" "(&($LDAP_UID=*)($filter))" 1.1
128
		/usr/bin/ldapsearch $debugOpt -LLL -H "$protocol://$LDAP_SERVER" -x -D "$LDAP_USER" -w "$LDAP_PASSWORD" -b "$LDAP_BASE" "(&($LDAP_UID=*)($filter))" 1.1
111
		;;
129
		;;
112
	*)
130
	*)
113
		echo "Argument inconnu : $1";
131
		echo "Argument inconnu : $1";
114
		echo "$usage"
132
		echo "$usage"
115
		exit 1
133
		exit 1
116
		;;
134
		;;
117
esac
135
esac
118
 
136