Subversion Repositories ALCASAR

Rev

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

Rev 2452 Rev 2453
1
<?php
1
<?php
2
# $Id: ldap.php 2452 2017-12-05 22:51:01Z tom.houdayer $
2
# $Id: ldap.php 2453 2017-12-06 00:06:41Z tom.houdayer $
3
 
3
 
4
/* written by steweb57 & Rexy */
4
/* written by steweb57, Rexy & Tom HOUDAYER */
5
/****************************************************************
5
/****************************************************************
6
*			GLOBAL FILE PATHS			*
6
*			GLOBAL FILE PATHS			*
7
*****************************************************************/
7
*****************************************************************/
8
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
8
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
9
 
9
 
10
/****************************************************************
10
/****************************************************************
11
*			FILE reading test			*
11
*			FILE reading test			*
12
*****************************************************************/
12
*****************************************************************/
13
$conf_files = array(CONF_FILE);
13
$conf_files = array(CONF_FILE);
14
foreach ($conf_files as $file) {
14
foreach ($conf_files as $file) {
15
	if (!file_exists($file)) {
15
	if (!file_exists($file)) {
16
		exit("Fichier $file non présent");
16
		exit("Fichier $file non présent");
17
	}
17
	}
18
	if (!is_readable($file)) {
18
	if (!is_readable($file)) {
19
		exit("Vous n'avez pas les droits de lecture sur le fichier $file");
19
		exit("Vous n'avez pas les droits de lecture sur le fichier $file");
20
	}
20
	}
21
}
21
}
22
 
22
 
23
/****************************************************************
23
/****************************************************************
24
*			Read CONF_FILE				*
24
*			Read CONF_FILE				*
25
*****************************************************************/
25
*****************************************************************/
26
$file_conf = fopen(CONF_FILE, 'r');
26
$file_conf = fopen(CONF_FILE, 'r');
27
if (!$file_conf) {
27
if (!$file_conf) {
28
	exit('Error opening the file '.CONF_FILE);
28
	exit('Error opening the file '.CONF_FILE);
29
}
29
}
30
while (!feof($file_conf)) {
30
while (!feof($file_conf)) {
31
	$buffer = fgets($file_conf, 4096);
31
	$buffer = fgets($file_conf, 4096);
32
	if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
32
	if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
33
		$tmp = explode('=', $buffer, 2);
33
		$tmp = explode('=', $buffer, 2);
34
		$conf[trim($tmp[0])] = trim($tmp[1]);
34
		$conf[trim($tmp[0])] = trim($tmp[1]);
35
	}
35
	}
36
}
36
}
37
fclose($file_conf);
37
fclose($file_conf);
38
 
38
 
39
/****************************************************************
39
/****************************************************************
40
*			Choice of language			*
40
*			Choice of language			*
41
*****************************************************************/
41
*****************************************************************/
42
$Language = 'en';
42
$Language = 'en';
43
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
43
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
44
	$Langue	  = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
44
	$Langue	  = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
45
	$Language = strtolower(substr(chop($Langue[0]), 0, 2));
45
	$Language = strtolower(substr(chop($Langue[0]), 0, 2));
46
}
46
}
47
if ($Language === 'fr') {		// French
47
if ($Language === 'fr') {		// French
48
	$l_ldap_update_sucess		= "Mise à jour des paramètres LDAP réalisée avec succès";
48
	$l_ldap_update_sucess		= "Mise à jour des paramètres LDAP réalisée avec succès";
49
	$l_ldap_title			= "Authentification externe : LDAP";
49
	$l_ldap_title			= "Authentification externe : LDAP";
50
	$l_ldap_legend			= "Authentification LDAP";
50
	$l_ldap_legend			= "Authentification LDAP";
51
	$l_ldap_auth_enable_label	= "Activer l'authentification LDAP:";
51
	$l_ldap_auth_enable_label	= "Activer l'authentification LDAP:";
52
	$l_ldap_YES			= "OUI";
52
	$l_ldap_YES			= "OUI";
53
	$l_ldap_NO			= "NON";
53
	$l_ldap_NO			= "NON";
54
	$l_ldap_server_label		= "Serveur LDAP:";
54
	$l_ldap_server_label		= "Serveur LDAP:";
55
	$l_ldap_server_text		= "Adresse IP du serveur LDAP";
55
	$l_ldap_server_text		= "Adresse IP du serveur LDAP";
56
	$l_ldap_base_dn_label		= "DN de la base LDAP:";
56
	$l_ldap_base_dn_label		= "DN de la base LDAP:";
57
	$l_ldap_base_dn_text		= "DN est le 'Distinguished Name', il définit où se situent les informations des utilisateurs dans l'annuaire. Exemple LDAP: 'o=mycompany, c=FR'. Exemple AD 'ou=my_lan,dc=server_name,dc=localdomain'";
57
	$l_ldap_base_dn_text		= "DN est le 'Distinguished Name', il définit où se situent les informations des utilisateurs dans l'annuaire. Exemple LDAP: 'o=mycompany, c=FR'. Exemple AD 'ou=my_lan,dc=server_name,dc=localdomain'";
58
	$l_ldap_filter_label		= "Identifiant LDAP:";
58
	$l_ldap_filter_label		= "Identifiant LDAP:";
59
	$l_ldap_filter_text		= "Clé utilisée lors de la recherche d'un identifiant de connexion, exemple: 'uid', 'sn', etc. Pour un AD mettre 'sAMAccountName'.";
59
	$l_ldap_filter_text		= "Clé utilisée lors de la recherche d'un identifiant de connexion, exemple: 'uid', 'sn', etc. Pour un AD mettre 'sAMAccountName'.";
60
	$l_ldap_base_filter_label	= "Filtre de recherche d'utilisateurs LDAP:";
60
	$l_ldap_base_filter_label	= "Filtre de recherche d'utilisateurs LDAP:";
61
	$l_ldap_base_filter_text	= "En option, vous pouvez limiter les objets recherchés avec des filtres additionnels. Par exemple 'objectClass=posixGroup' aurait comme conséquence l'utilisation de '(&amp;(uid=username)(objectClass=posixGroup))'";
61
	$l_ldap_base_filter_text	= "En option, vous pouvez limiter les objets recherchés avec des filtres additionnels. Par exemple 'objectClass=posixGroup' aurait comme conséquence l'utilisation de '(&amp;(uid=username)(objectClass=posixGroup))'";
62
	$l_ldap_user_label		= "Utilisateur LDAP:";
62
	$l_ldap_user_label		= "Utilisateur LDAP:";
63
	$l_ldap_user_text		= "Nom d'utilisateur utilisé par ALCASAR pour se connecter au serveur LDAP. Laissez vide pour utiliser un accès invité (ou anomyme). Obligatoire sur un AD. exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'. Exemple AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
63
	$l_ldap_user_text		= "Nom d'utilisateur utilisé par ALCASAR pour se connecter au serveur LDAP. Laissez vide pour utiliser un accès invité (ou anomyme). Obligatoire sur un AD. exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'. Exemple AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
64
	$l_ldap_password_label		= "Mot de passe LDAP:";
64
	$l_ldap_password_label		= "Mot de passe LDAP:";
65
	$l_ldap_password_text		= "Laissez vide pour un accès invité (ou anonyme). Obligatoire sur un AD.";
65
	$l_ldap_password_text		= "Laissez vide pour un accès invité (ou anonyme). Obligatoire sur un AD.";
66
	$l_ldap_submit			= "Enregistrer";
66
	$l_ldap_submit			= "Enregistrer";
67
	$l_ldap_reset			= "Annuler";
67
	$l_ldap_reset			= "Annuler";
68
	$l_ldap_test_network_failed	= "Pas de connectivité réseau avec le serveur LDAP.";
68
	$l_ldap_test_network_failed	= "Pas de connectivité réseau avec le serveur LDAP.";
69
	$l_ldap_test_connection_failed	= "Impossible de se connecter au serveur LDAP.";
69
	$l_ldap_test_connection_failed	= "Impossible de se connecter au serveur LDAP.";
70
	$l_ldap_test_bind_ok		= "Connexion LDAP réussie...";
70
	$l_ldap_test_bind_ok		= "Connexion LDAP réussie...";
71
	$l_ldap_test_bind_failed	= "Echec d'authentification sur le serveur LDAP... Vérifiez votre configuration";
71
	$l_ldap_test_bind_failed	= "Echec d'authentification sur le serveur LDAP... Vérifiez votre configuration";
72
	$l_ldap_test_dn_ok		= "DN semble bon";
72
	$l_ldap_test_dn_ok		= "DN semble bon";
73
	$l_ldap_test_dn_failed		= "DN semble mauvais";
73
	$l_ldap_test_dn_failed		= "DN semble mauvais";
74
	$l_ldap_error			= "erreur LDAP";
74
	$l_ldap_error			= "erreur LDAP";
75
} else {				// English
75
} else {				// English
76
	$l_ldap_update_sucess		= "Successfull LDAP settings update";
76
	$l_ldap_update_sucess		= "Successfull LDAP settings update";
77
	$l_ldap_title			= "External authentication : LDAP";
77
	$l_ldap_title			= "External authentication : LDAP";
78
	$l_ldap_legend			= "LDAP authentication";
78
	$l_ldap_legend			= "LDAP authentication";
79
	$l_ldap_auth_enable_label	= "Use LDAP authentication :";
79
	$l_ldap_auth_enable_label	= "Use LDAP authentication :";
80
	$l_ldap_YES			= "YES";
80
	$l_ldap_YES			= "YES";
81
	$l_ldap_NO			= "NO";
81
	$l_ldap_NO			= "NO";
82
	$l_ldap_server_label		= "LDAP server :";
82
	$l_ldap_server_label		= "LDAP server :";
83
	$l_ldap_server_text		= "This is the IP address of the LDAP server.";
83
	$l_ldap_server_text		= "This is the IP address of the LDAP server.";
84
	$l_ldap_base_dn_label		= "LDAP base dn:";
84
	$l_ldap_base_dn_label		= "LDAP base dn:";
85
	$l_ldap_base_dn_text		= "This is the 'Distinguished Name', locating the user information in the directory. e.g. LDAP : 'o=MyCompany,c=US'. e.g. AD : 'ou=my_lan,dc=server_name,dc=localdomain'";
85
	$l_ldap_base_dn_text		= "This is the 'Distinguished Name', locating the user information in the directory. e.g. LDAP : 'o=MyCompany,c=US'. e.g. AD : 'ou=my_lan,dc=server_name,dc=localdomain'";
86
	$l_ldap_filter_label		= "LDAP uid:";
86
	$l_ldap_filter_label		= "LDAP uid:";
87
	$l_ldap_filter_text		= "This is the key used to search for a given login identity, e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
87
	$l_ldap_filter_text		= "This is the key used to search for a given login identity, e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
88
	$l_ldap_base_filter_label	= "LDAP user filter:";
88
	$l_ldap_base_filter_label	= "LDAP user filter:";
89
	$l_ldap_base_filter_text	= "Optionally you can further limit the searched objects with additional filters. For example 'objectClass=posixGroup' would result in the use of '(&amp;(uid=username)(objectClass=posixGroup))'";
89
	$l_ldap_base_filter_text	= "Optionally you can further limit the searched objects with additional filters. For example 'objectClass=posixGroup' would result in the use of '(&amp;(uid=username)(objectClass=posixGroup))'";
90
	$l_ldap_user_label		= "LDAP user dn:";
90
	$l_ldap_user_label		= "LDAP user dn:";
91
	$l_ldap_user_text		= "Username used by ALCASAR to connect to the LDAP server. Leave blank to use anonymous binding. Required for AD. e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'. e.g. AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
91
	$l_ldap_user_text		= "Username used by ALCASAR to connect to the LDAP server. Leave blank to use anonymous binding. Required for AD. e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'. e.g. AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
92
	$l_ldap_password_label		= "LDAP password:";
92
	$l_ldap_password_label		= "LDAP password:";
93
	$l_ldap_password_text		= "Leave blank to use anonymous binding. Required for AD.";
93
	$l_ldap_password_text		= "Leave blank to use anonymous binding. Required for AD.";
94
	$l_ldap_submit			= "Save";
94
	$l_ldap_submit			= "Save";
95
	$l_ldap_reset			= "Reset";
95
	$l_ldap_reset			= "Reset";
96
	$l_ldap_test_network_failed	= "LDAP server is not reachable.";
96
	$l_ldap_test_network_failed	= "LDAP server is not reachable.";
97
	$l_ldap_test_connection_failed	= "LDAP connexion failed...";
97
	$l_ldap_test_connection_failed	= "LDAP connexion failed...";
98
	$l_ldap_test_bind_ok		= "LDAP connexion success...";
98
	$l_ldap_test_bind_ok		= "LDAP connexion success...";
99
	$l_ldap_test_bind_failed	= "LDAP authentication failed...Check your ldap setup...";
99
	$l_ldap_test_bind_failed	= "LDAP authentication failed...Check your ldap setup...";
100
	$l_ldap_test_dn_ok              = "DN seems to be right";
100
	$l_ldap_test_dn_ok              = "DN seems to be right";
101
	$l_ldap_test_dn_failed          = "DN seems to be wrong";
101
	$l_ldap_test_dn_failed          = "DN seems to be wrong";
102
	$l_ldap_error			= "LDAP error";
102
	$l_ldap_error			= "LDAP error";
103
}
103
}
104
 
104
 
105
 
105
 
106
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_filter, $f_ldap_port = 389) {
106
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_filter, $f_ldap_port = 389) {
107
	// Test connect to the LDAP server
107
	// Test connect to the LDAP server
108
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
108
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
109
		// no network connection
109
		// no network connection
110
		return -1;
110
		return -1;
111
	}
111
	}
112
	fclose($sock);
112
	fclose($sock);
113
 
113
 
114
	// Test connect to the LDAP server
114
	// Test connect to the LDAP server
115
	$ldapconn = ldap_connect($f_ldap_server, $f_ldap_port);
115
	$ldapconn = ldap_connect($f_ldap_server, $f_ldap_port);
116
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
116
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
117
	if (!$ldapconn) {
117
	if (!$ldapconn) {
118
		// LDAP connection failed
118
		// LDAP connection failed
119
		return -2;
119
		return -2;
120
	}
120
	}
121
 
121
 
122
	$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
122
	$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
123
	if (!$ldapbind) {
123
	if (!$ldapbind) {
124
		// Test LDAP Version 3
124
		// Test LDAP Version 3
125
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
125
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
126
		$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
126
		$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
127
 
127
 
128
		if (!$ldapbind) {
128
		if (!$ldapbind) {
129
			// LDAP Bind failed
129
			// LDAP Bind failed
130
			return 0;
130
			return 0;
131
		}
131
		}
132
	}
132
	}
133
	ldap_unbind($ldapconn);
133
	ldap_unbind($ldapconn);
134
 
134
 
135
	// try search
135
	// try search
136
	$query = $f_ldap_filter.'=*';
136
	$query = $f_ldap_filter.'=*';
137
	if (ldap_search($ldapconn, $f_ldap_basedn, $query)) {
137
	if (ldap_search($ldapconn, $f_ldap_basedn, $query)) {
138
		return 2;
138
		return 2;
139
	} else {
139
	} else {
140
		return 1;
140
		return 1;
141
	}
141
	}
142
}
142
}
143
 
143
 
144
$messages = '';
144
$messages = '';
145
 
145
 
146
if (isset($_POST['auth_enable'])) {
146
if (isset($_POST['auth_enable'])) {
147
	if ($_POST['auth_enable'] === '1') {
147
	if ($_POST['auth_enable'] === '1') {
148
		$varErrors = [];
148
		$varErrors = [];
149
		if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');
149
		if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');
150
		if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');
150
		if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');
151
		if (isset($_POST['ldap_filter']))      $ldap_filter      = $_POST['ldap_filter'];      else array_push($varErrors, 'Variable error "ldap_filter"');
151
		if (isset($_POST['ldap_filter']))      $ldap_filter      = $_POST['ldap_filter'];      else array_push($varErrors, 'Variable error "ldap_filter"');
152
		if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
152
		if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
153
		if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');
153
		if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');
154
		if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');
154
		if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');
155
 
155
 
156
		// Validation
156
		// Validation
157
		if (isset($ldap_server)) {
157
		if (isset($ldap_server)) {
158
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
158
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
159
				$ldap_server = gethostbyname($ldap_server);
159
				$ldap_server = gethostbyname($ldap_server);
160
			}
160
			}
161
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
161
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
162
				array_push($varErrors, 'Invalid LDAP server IP');
162
				array_push($varErrors, 'Invalid LDAP server IP');
163
			}
163
			}
164
		}
164
		}
165
 
165
 
166
		if (!empty($varErrors)) { 
166
		if (!empty($varErrors)) { 
167
			$messages .= '<div style="text-align: center"><br>';
167
			$messages .= '<div style="text-align: center"><br>';
168
			foreach ($varErrors as $error) {
168
			foreach ($varErrors as $error) {
169
				$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
169
				$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
170
			}
170
			}
171
			$messages .= '<br></div>';
171
			$messages .= '<br></div>';
172
		} else {
172
		} else {
173
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
173
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
174
			exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
174
			exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
175
			exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
175
			exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
176
			exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_filter/g").' '.CONF_FILE);
176
			exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_filter/g").' '.CONF_FILE);
177
			exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_base_filter/g").' '.CONF_FILE);
177
			exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_base_filter/g").' '.CONF_FILE);
178
			exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
178
			exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
179
			exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
179
			exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
180
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
180
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
181
 
181
 
182
			$messages .= '<div style="text-align: center"><br>';
182
			$messages .= '<div style="text-align: center"><br>';
183
			$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
183
			$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
184
			$messages .= '<br></div>';
184
			$messages .= '<br></div>';
185
		}
185
		}
186
	} else {
186
	} else {
187
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
187
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
188
		exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
188
		exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
189
 
189
 
190
		$messages .= '<div style="text-align: center"><br>';
190
		$messages .= '<div style="text-align: center"><br>';
191
		$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
191
		$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
192
		$messages .= '<br></div>';
192
		$messages .= '<br></div>';
193
	}
193
	}
194
 
194
 
195
	// Reload configuration
195
	// Reload configuration
196
	$file_conf = fopen(CONF_FILE, 'r');
196
	$file_conf = fopen(CONF_FILE, 'r');
197
	if (!$file_conf) {
197
	if (!$file_conf) {
198
		exit('Error opening the file '.CONF_FILE);
198
		exit('Error opening the file '.CONF_FILE);
199
	}
199
	}
200
	while (!feof($file_conf)) {
200
	while (!feof($file_conf)) {
201
		$buffer = fgets($file_conf, 4096);
201
		$buffer = fgets($file_conf, 4096);
202
		if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
202
		if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
203
			$tmp = explode('=', $buffer, 2);
203
			$tmp = explode('=', $buffer, 2);
204
			$conf[trim($tmp[0])] = trim($tmp[1]);
204
			$conf[trim($tmp[0])] = trim($tmp[1]);
205
		}
205
		}
206
	}
206
	}
207
	fclose($file_conf);
207
	fclose($file_conf);
208
}
208
}
209
 
209
 
210
// LDAP configuration params
210
// LDAP configuration params
211
$ldap_status      = ($conf['LDAP'] === 'on');
211
$ldap_status      = ($conf['LDAP'] === 'on');
212
$ldap_server      = $conf['LDAP_SERVER'];
212
$ldap_server      = $conf['LDAP_SERVER'];
213
$ldap_identity    = $conf['LDAP_USER'];
213
$ldap_user        = $conf['LDAP_USER'];
214
$ldap_password    = $conf['LDAP_PASSWORD'];
214
$ldap_password    = $conf['LDAP_PASSWORD'];
215
$ldap_base_dn     = $conf['LDAP_BASE'];
215
$ldap_base_dn     = $conf['LDAP_BASE'];
216
$ldap_filter      = $conf['LDAP_UID'];
216
$ldap_filter      = $conf['LDAP_UID'];
217
$ldap_base_filter = $conf['LDAP_FILTER'];
217
$ldap_base_filter = $conf['LDAP_FILTER'];
218
 
218
 
219
// TODO : check LDAP PHP extension loaded?
219
// TODO : check LDAP PHP extension loaded?
220
// if (!extension_loaded('ldap')) {
220
// if (!extension_loaded('ldap')) {
221
// 	exit();
221
// 	exit();
222
// }
222
// }
223
 
223
 
224
$pos = strpos($ldap_server, '//');
224
$pos = strpos($ldap_server, '//');
225
if ($pos !== false) {
225
if ($pos !== false) {
226
	// TODO : useless?
226
	// TODO : useless?
227
	$new_ldap_server = explode('//', $ldap_server); // pour discriminer le host et le protocole dans la notation "ldap://192.168.182.10" ou "ldaps://monldap.monentreperise.com"
227
	$new_ldap_server = explode('//', $ldap_server); // pour discriminer le host et le protocole dans la notation "ldap://192.168.182.10" ou "ldaps://monldap.monentreperise.com"
228
} else {
228
} else {
229
	$new_ldap_server = $ldap_server;
229
	$new_ldap_server = $ldap_server;
230
}
230
}
231
if ($ldap_status) {
231
if ($ldap_status) {
232
	$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_identity, $ldap_password, $ldap_base_dn, $ldap_filter);
232
	$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_filter);
-
 
233
}
-
 
234
 
-
 
235
// AJAX LDAP configuration checker
-
 
236
if (isset($_GET['conf_check'])) {
-
 
237
	$response = [
-
 
238
		'enable' => $ldap_status
-
 
239
	];
-
 
240
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
-
 
241
		$varErrors = [];
-
 
242
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
-
 
243
			if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');
-
 
244
			if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');
-
 
245
			if (isset($_POST['ldap_filter']))      $ldap_filter      = $_POST['ldap_filter'];      else array_push($varErrors, 'Variable error "ldap_filter"');
-
 
246
			if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
-
 
247
			if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');
-
 
248
			if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');
-
 
249
		}
-
 
250
 
-
 
251
		// Validation
-
 
252
		if (isset($ldap_server)) {
-
 
253
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
-
 
254
				$ldap_server = gethostbyname($ldap_server);
-
 
255
			}
-
 
256
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
-
 
257
				array_push($varErrors, 'Invalid LDAP server IP');
-
 
258
			}
-
 
259
		}
-
 
260
 
-
 
261
		$response['result'] = (ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_filter) > 0);
-
 
262
 
-
 
263
		if (!empty($varErrors)) {
-
 
264
			$response['errors'] = $varErrors;
-
 
265
		}
-
 
266
	}
-
 
267
 
-
 
268
	header('Content-Type: application/json');
-
 
269
	echo json_encode($response);
-
 
270
	exit();
233
}
271
}
234
 
272
 
235
?>
273
?>
236
<!DOCTYPE html>
274
<!DOCTYPE html>
237
<html>
275
<html>
238
<head>
276
<head>
239
	<meta charset="UTF-8">
277
	<meta charset="UTF-8">
240
	<title><?= $l_ldap_title ?></title>
278
	<title><?= $l_ldap_title ?></title>
241
	<link type="text/css" href="/css/style.css" rel="stylesheet">
279
	<link type="text/css" href="/css/style.css" rel="stylesheet">
242
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
280
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
243
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
281
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
244
	<script>
282
	<script>
245
	function onLdapStatusChange() {
283
	function onLdapStatusChange() {
246
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
284
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
247
 
285
 
248
		if (document.getElementById("auth_enable").value === '1') {
286
		if (document.getElementById('auth_enable').value === '1') {
249
			for (var i=0; i<listToDisables.length; i++) {
287
			for (var i=0; i<listToDisables.length; i++) {
250
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
288
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
251
				document.getElementById(listToDisables[i]).disabled = false;
289
				document.getElementById(listToDisables[i]).disabled = false;
252
			}
290
			}
253
		} else {
291
		} else {
254
			for (var i=0; i<listToDisables.length; i++) {
292
			for (var i=0; i<listToDisables.length; i++) {
255
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
293
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
256
				document.getElementById(listToDisables[i]).disabled = true;
294
				document.getElementById(listToDisables[i]).disabled = true;
257
			}
295
			}
258
		}
296
		}
259
	}
297
	}
-
 
298
 
-
 
299
	function checkConfig() {
-
 
300
		var ldap_config = {
-
 
301
			ldap_status:     (document.getElementById('auth_enable').value === '1'),
-
 
302
			ldap_server:      document.getElementById('ldap_server').value,
-
 
303
			ldap_user:        document.getElementById('ldap_user').value,
-
 
304
			ldap_password:    document.getElementById('ldap_password').value,
-
 
305
			ldap_base_dn:     document.getElementById('ldap_dn').value,
-
 
306
			ldap_filter:      document.getElementById('ldap_filter').value,
-
 
307
			ldap_base_filter: document.getElementById('ldap_base_filter').value
-
 
308
		};
-
 
309
 
-
 
310
		console.log(ldap_config);
-
 
311
 
-
 
312
		var params = Object.keys(ldap_config).map( function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(ldap_config[k]) } ).join('&');
-
 
313
 
-
 
314
		var xhr = new XMLHttpRequest();
-
 
315
		xhr.onreadystatechange = function() {
-
 
316
			if (this.readyState == 4) {
-
 
317
				if (this.status == 200) {
-
 
318
					var data = JSON.parse(this.responseText);
-
 
319
 
-
 
320
					if (data.result === true) {
-
 
321
						alert('Configuration valide');
-
 
322
					} else {
-
 
323
						alert('Configuration non valide');
-
 
324
					}
-
 
325
 
-
 
326
					if (typeof data.errors !== 'undefined') {
-
 
327
						alert(data.errors.join("\n"));
-
 
328
					}
-
 
329
 
-
 
330
					console.log(data)
-
 
331
				} else {
-
 
332
					console.log(this.responseText)
-
 
333
				}
-
 
334
			}
-
 
335
		};
-
 
336
		xhr.open('POST', 'ldap.php?conf_check', true);
-
 
337
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-
 
338
		xhr.send(params);
-
 
339
	}
260
	</script>
340
	</script>
261
</head>
341
</head>
262
<body onLoad="onLdapStatusChange();">
342
<body onLoad="onLdapStatusChange();">
263
	<div class="panel">
343
	<div class="panel">
264
		<div class="panel-header"><?= $l_ldap_legend ?></div>
344
		<div class="panel-header"><?= $l_ldap_legend ?></div>
265
		<div class="panel-body">
345
		<div class="panel-body">
266
			<form name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
346
			<form name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
267
				<fieldset>
347
				<fieldset>
268
					<legend>
348
					<legend>
269
						<?= $messages ?>
349
						<?= $messages ?>
270
						<?php if ($ldap_status): ?>
350
						<?php if ($ldap_status): ?>
271
							<div style="text-align: center"><br>
351
							<div style="text-align: center"><br>
272
								<?php if ($serverCheckResult === -2): ?>
352
								<?php if ($serverCheckResult === -2): ?>
273
									<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
353
									<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
274
								<?php elseif ($serverCheckResult === -1): ?>
354
								<?php elseif ($serverCheckResult === -1): ?>
275
									<span style="color: red"><?= $l_ldap_test_network_failed ?></span>
355
									<span style="color: red"><?= $l_ldap_test_network_failed ?></span>
276
								<?php elseif ($serverCheckResult === 0): ?>
356
								<?php elseif ($serverCheckResult === 0): ?>
277
									<span style="color: red"><?= $l_ldap_test_bind_failed ?></span>
357
									<span style="color: red"><?= $l_ldap_test_bind_failed ?></span>
278
								<?php elseif ($serverCheckResult === 1): ?>
358
								<?php elseif ($serverCheckResult === 1): ?>
279
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
359
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
280
										<br>";
360
										<br>";
281
										<span style="color: red"><?= $l_ldap_test_dn_failed ?></span>
361
										<span style="color: red"><?= $l_ldap_test_dn_failed ?></span>
282
								<?php elseif ($serverCheckResult === 2): ?>
362
								<?php elseif ($serverCheckResult === 2): ?>
283
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
363
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
284
										<br>";
364
										<br>";
285
										<span style="color: green"><?= $l_ldap_test_dn_ok ?></span>
365
										<span style="color: green"><?= $l_ldap_test_dn_ok ?></span>
286
								<?php else: ?>
366
								<?php else: ?>
287
									<span><?= $l_ldap_error ?></span>
367
									<span><?= $l_ldap_error ?></span>
288
								<?php endif ?>
368
								<?php endif ?>
289
								<br><br>
369
								<br><br>
290
							</div>
370
							</div>
291
						<?php endif ?>
371
						<?php endif ?>
292
					</legend>
372
					</legend>
293
					<dl>
373
					<dl>
294
						<dt>
374
						<dt>
295
							<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
375
							<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
296
						</dt>
376
						</dt>
297
						<dd>
377
						<dd>
298
							<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
378
							<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
299
								<option value="1"<?= ($ldap_status)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
379
								<option value="1"<?= ($ldap_status)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
300
								<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
380
								<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
301
							</select>
381
							</select>
302
						</dd>
382
						</dd>
303
					</dl>
383
					</dl>
304
					<dl>
384
					<dl>
305
						<dt>
385
						<dt>
306
							<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
386
							<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
307
							<?= $l_ldap_server_text ?>
387
							<?= $l_ldap_server_text ?>
308
						</dt>
388
						</dt>
309
						<dd>
389
						<dd>
310
							<input id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>">
390
							<input id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>">
311
						</dd>
391
						</dd>
312
					</dl>
392
					</dl>
313
					<dl>
393
					<dl>
314
						<dt>
394
						<dt>
315
							<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
395
							<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
316
							<?= $l_ldap_base_dn_text ?>
396
							<?= $l_ldap_base_dn_text ?>
317
						</dt>
397
						</dt>
318
						<dd>
398
						<dd>
319
							<input id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_base_dn) ?>">
399
							<input id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_base_dn) ?>">
320
						</dd>
400
						</dd>
321
					</dl>
401
					</dl>
322
					<dl>
402
					<dl>
323
						<dt>
403
						<dt>
324
							<label for="ldap_filter"><?= $l_ldap_filter_label ?></label><br>
404
							<label for="ldap_filter"><?= $l_ldap_filter_label ?></label><br>
325
							<?= $l_ldap_filter_text ?>
405
							<?= $l_ldap_filter_text ?>
326
						</dt>
406
						</dt>
327
						<dd>
407
						<dd>
328
							<input id="ldap_filter" size="40" name="ldap_filter" value="<?= htmlspecialchars($ldap_filter) ?>">
408
							<input id="ldap_filter" size="40" name="ldap_filter" value="<?= htmlspecialchars($ldap_filter) ?>">
329
						</dd>
409
						</dd>
330
					</dl>
410
					</dl>
331
					<dl>
411
					<dl>
332
						<dt>
412
						<dt>
333
							<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
413
							<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
334
							<?= $l_ldap_base_filter_text ?>
414
							<?= $l_ldap_base_filter_text ?>
335
						</dt>
415
						</dt>
336
						<dd>
416
						<dd>
337
							<input id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>">
417
							<input id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>">
338
						</dd>
418
						</dd>
339
					</dl>
419
					</dl>
340
					<dl>
420
					<dl>
341
						<dt>
421
						<dt>
342
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
422
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
343
							<?= $l_ldap_user_text ?>
423
							<?= $l_ldap_user_text ?>
344
						</dt>
424
						</dt>
345
						<dd>
425
						<dd>
346
							<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_identity) ?>">
426
							<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_user) ?>">
347
						</dd>
427
						</dd>
348
					</dl>
428
					</dl>
349
					<dl>
429
					<dl>
350
						<dt>
430
						<dt>
351
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
431
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
352
							<?= $l_ldap_password_text ?>
432
							<?= $l_ldap_password_text ?>
353
						</dt>
433
						</dt>
354
						<dd>
434
						<dd>
355
							<input id="ldap_password" type="password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>">
435
							<input id="ldap_password" type="password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>">
356
						</dd>
436
						</dd>
357
					</dl>
437
					</dl>
358
					<p>
438
					<p>
359
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
439
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
360
						<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
440
						<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
-
 
441
						<button onclick="checkConfig(); return false;">Check [BETA]</button>
361
					</p>
442
					</p>
362
				</fieldset>
443
				</fieldset>
363
			</form>
444
			</form>
364
		</div>
445
		</div>
365
	</div>
446
	</div>
366
</body>
447
</body>
367
</html>
448
</html>
368
 
449