Subversion Repositories ALCASAR

Rev

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

Rev 2710 Rev 2714
1
<?php
1
<?php
2
# $Id: ldap.php 2710 2019-03-05 23:37:17Z tom.houdayer $
2
# $Id: ldap.php 2714 2019-03-10 23:43:22Z tom.houdayer $
3
 
3
 
4
/* written by steweb57, Rexy & Tom HOUDAYER */
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			= "Mise à jour des paramètres LDAP effectuée";
48
	$l_ldap_update			= "Mise à jour des paramètres LDAP effectuée";
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	= "Éditer la configuration LDAP:";
51
	$l_ldap_auth_enable_label	= "Éditer la configuration 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";
55
	$l_ldap_server_text		= "Adresse IP du serveur";
56
	$l_ldap_base_dn_label		= "DN de la base:";
56
	$l_ldap_base_dn_label		= "DN de la base:";
57
	$l_ldap_base_dn_text		= "Le DN (Distinguished Name) définit où se situent les informations des utilisateurs dans l'annuaire.<br> - Exemple LDAP: 'o=mycompany, c=FR'.<br> - Exemple AD 'cn=Users,dc=server_name,dc=localdomain'";
57
	$l_ldap_base_dn_text		= "Le DN (Distinguished Name) définit où se situent les informations des utilisateurs dans l'annuaire.<br> - Exemple LDAP: 'o=mycompany, c=FR'.<br> - Exemple AD 'cn=Users,dc=server_name,dc=localdomain'";
58
	$l_ldap_uid_label		= "Identifiant d'utilisateur (UID):";
58
	$l_ldap_uid_label		= "Identifiant d'utilisateur (UID):";
59
	$l_ldap_uid_text		= "Clé utilisée pour rechercher un identifiant de connexion.<br> - Exemple LDAP: 'uid', 'sn', etc.<br> - Pour A.D. mettre 'sAMAccountName'.";
59
	$l_ldap_uid_text		= "Clé utilisée pour rechercher un identifiant de connexion.<br> - Exemple LDAP: 'uid', 'sn', etc.<br> - Pour A.D. mettre 'sAMAccountName'.";
60
	$l_ldap_base_filter_label	= "Filtre de recherche des utilisateurs (optionnel):";
60
	$l_ldap_base_filter_label	= "Filtre de recherche des utilisateurs (optionnel):";
61
	$l_ldap_base_filter_text	= "Vous pouvez limiter les objets recherchés avec des filtres additionnels.<br> Exemple 'objectClass=posixGroup' ajouterait le filtre '(&amp;(uid=username)(objectClass=posixGroup))'";
61
	$l_ldap_base_filter_text	= "Vous pouvez limiter les objets recherchés avec des filtres additionnels.<br> Exemple 'objectClass=posixGroup' ajouterait le filtre '(&amp;(uid=username)(objectClass=posixGroup))'";
62
	$l_ldap_user_label		= "CN de l'utilisateur exploité par ALCASAR:";
62
	$l_ldap_user_label		= "CN de l'utilisateur exploité par ALCASAR:";
63
	$l_ldap_user_text		= "CN=Common Name. Laissez vide pour utiliser un accès invité (ou anonyme). Obligatoire sur un AD.<br> - Exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'.<br> - Exemple AD : 'username' ou 'cn=username,cn=Users,dc=server_name,dc=localdomain'";
63
	$l_ldap_user_text		= "CN=Common Name. Laissez vide pour utiliser un accès invité (ou anonyme). Obligatoire sur un AD.<br> - Exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'.<br> - Exemple AD : 'username' ou 'cn=username,cn=Users,dc=server_name,dc=localdomain'";
64
	$l_ldap_password_label		= "Mot de passe:";
64
	$l_ldap_password_label		= "Mot de passe:";
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_ssl_label		= "Connexion chiffré";
66
	$l_ldap_ssl_label		= "Connexion chiffré";
67
	$l_ldap_ssl_text		= "Utiliser une connexion chiffré avec SSL (LDAPS)";
67
	$l_ldap_ssl_text		= "Utiliser une connexion chiffré avec SSL (LDAPS)";
68
	$l_ldap_cert_required_label	= "Vérifier le certificat SSL";
68
	$l_ldap_cert_required_label	= "Vérifier le certificat SSL";
69
	$l_ldap_cert_required_text	= "Vérifier que le serveur LDAP utilise un certificat connu";
69
	$l_ldap_cert_required_text	= "Vérifier que le serveur LDAP utilise un certificat connu";
70
	$l_ldap_cert_label		= "Certificat SSL (CA)";
70
	$l_ldap_cert_label		= "Certificat SSL (CA)";
71
	$l_ldap_cert_text		= "Certificat de l'authorité de certification signant celui du serveur LDAP";
71
	$l_ldap_cert_text		= "Certificat de l'authorité de certification signant celui du serveur LDAP";
72
	$l_ldap_cert_status_cur		= "Certificat actuel : ";
72
	$l_ldap_cert_status_cur		= "Certificat actuel : ";
73
	$l_ldap_cert_status_no		= "Aucun certificat installé";
73
	$l_ldap_cert_status_no		= "Aucun certificat installé";
74
	$l_ldap_submit			= "Enregistrer";
74
	$l_ldap_submit			= "Enregistrer";
75
	$l_ldap_test_service_failed	= "Service LDAP injoignable sur ce serveur (vérifiez l'@IP).";
75
	$l_ldap_test_service_failed	= "Service LDAP injoignable sur ce serveur (vérifiez l'@IP).";
76
	$l_ldap_test_service_ok		= "Un port 389 (636 avec SSL) est actif sur ce serveur";
76
	$l_ldap_test_service_ok		= "Un port 389 (636 avec SSL) est actif sur ce serveur";
77
	$l_ldap_test_connection_failed	= "Connexion LDAP impossible (vérifiez le service LDAP sur ce serveur)";
77
	$l_ldap_test_connection_failed	= "Connexion LDAP impossible (vérifiez le service LDAP sur ce serveur)";
78
	$l_ldap_test_connection_ok	= "Une connexion LDAP a été établie";
78
	$l_ldap_test_connection_ok	= "Une connexion LDAP a été établie";
79
	$l_ldap_test_bind_failed	= "Echec d'authentification (vérifiez l'utilisateur et le mot de passe)";
79
	$l_ldap_test_bind_failed	= "Echec d'authentification (vérifiez l'utilisateur et le mot de passe)";
80
	$l_ldap_test_bind_ok		= "L'authentification a réussie";
80
	$l_ldap_test_bind_ok		= "L'authentification a réussie";
81
	$l_ldap_test_dn_failed		= "Le DN de la base semble incorrect (vérifiez le)";
81
	$l_ldap_test_dn_failed		= "Le DN de la base semble incorrect (vérifiez le)";
82
	$l_ldap_test_dn_ok		= "Le DN de la base semble correct";
82
	$l_ldap_test_dn_ok		= "Le DN de la base semble correct";
83
	$l_ldap_error			= "erreur LDAP";
83
	$l_ldap_error			= "erreur LDAP";
84
	$l_ldap_entries			= "entrées dans la base";
84
	$l_ldap_entries			= "entrées dans la base";
85
	$l_ldap_cert_cn_diff_dn		= "Le CommonName du certificat (§cert_domainName§) est différent du nom de domaine du serveur";
85
	$l_ldap_cert_cn_diff_dn		= "Le CommonName du certificat (§cert_domainName§) est différent du nom de domaine du serveur";
86
	$l_check			= "Vérifier cette configuration";
86
	$l_check			= "Vérifier cette configuration";
87
	$l_checkingConf			= "Vérification de cette configuration...";
87
	$l_checkingConf			= "Vérification de cette configuration...";
88
} else {				// English
88
} else {				// English
89
	$l_ldap_update			= "LDAP settings updated";
89
	$l_ldap_update			= "LDAP settings updated";
90
	$l_ldap_title			= "External authentication : LDAP";
90
	$l_ldap_title			= "External authentication : LDAP";
91
	$l_ldap_legend			= "LDAP authentication";
91
	$l_ldap_legend			= "LDAP authentication";
92
	$l_ldap_auth_enable_label	= "Edit the LDAP configuration :";
92
	$l_ldap_auth_enable_label	= "Edit the LDAP configuration :";
93
	$l_ldap_YES			= "YES";
93
	$l_ldap_YES			= "YES";
94
	$l_ldap_NO			= "NO";
94
	$l_ldap_NO			= "NO";
95
	$l_ldap_server_label		= "LDAP server :";
95
	$l_ldap_server_label		= "LDAP server :";
96
	$l_ldap_server_text		= "IP address of the LDAP server.";
96
	$l_ldap_server_text		= "IP address of the LDAP server.";
97
	$l_ldap_base_dn_label		= "DN of the base:";
97
	$l_ldap_base_dn_label		= "DN of the base:";
98
	$l_ldap_base_dn_text		= "The DN (Distinguished Name) is used to locate the users information in the directory.<br> e.g. LDAP : 'o=MyCompany,c=US'.<br> e.g. AD : 'cn=Users,dc=server_name,dc=localdomain'";
98
	$l_ldap_base_dn_text		= "The DN (Distinguished Name) is used to locate the users information in the directory.<br> e.g. LDAP : 'o=MyCompany,c=US'.<br> e.g. AD : 'cn=Users,dc=server_name,dc=localdomain'";
99
	$l_ldap_uid_label		= "User IDentifier (UID):";
99
	$l_ldap_uid_label		= "User IDentifier (UID):";
100
	$l_ldap_uid_text		= "Key used to search for a given login identity.<br>e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
100
	$l_ldap_uid_text		= "Key used to search for a given login identity.<br>e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
101
	$l_ldap_base_filter_label	= "User search filter (optional):";
101
	$l_ldap_base_filter_label	= "User search filter (optional):";
102
	$l_ldap_base_filter_text	= "You can further limit the searched objects with additional filters.<br> For example 'objectClass=posixGroup' would result in the use of '(&amp;(uid=username)(objectClass=posixGroup))'";
102
	$l_ldap_base_filter_text	= "You can further limit the searched objects with additional filters.<br> For example 'objectClass=posixGroup' would result in the use of '(&amp;(uid=username)(objectClass=posixGroup))'";
103
	$l_ldap_user_label		= "CN of the user operated by ALCASAR:";
103
	$l_ldap_user_label		= "CN of the user operated by ALCASAR:";
104
	$l_ldap_user_text		= "CN=Common Name. Leave blank to use anonymous binding. Mandatory for AD.<br> e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'.<br> e.g. AD : 'username' or 'cn=username,cn=Users,dc=server_name,dc=localdomain'";
104
	$l_ldap_user_text		= "CN=Common Name. Leave blank to use anonymous binding. Mandatory for AD.<br> e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'.<br> e.g. AD : 'username' or 'cn=username,cn=Users,dc=server_name,dc=localdomain'";
105
	$l_ldap_password_label		= "Password:";
105
	$l_ldap_password_label		= "Password:";
106
	$l_ldap_password_text		= "Leave blank to use anonymous binding. Mandatory for AD.";
106
	$l_ldap_password_text		= "Leave blank to use anonymous binding. Mandatory for AD.";
107
	$l_ldap_ssl_label		= "Secure connection";
107
	$l_ldap_ssl_label		= "Secure connection";
108
	$l_ldap_ssl_text		= "Use an encrypted connection with SSL (LDAPS)";
108
	$l_ldap_ssl_text		= "Use an encrypted connection with SSL (LDAPS)";
109
	$l_ldap_cert_required_label	= "Check the SSL certificate";
109
	$l_ldap_cert_required_label	= "Check the SSL certificate";
110
	$l_ldap_cert_required_text	= "Verify that the LDAP server uses a trusted certificate";
110
	$l_ldap_cert_required_text	= "Verify that the LDAP server uses a trusted certificate";
111
	$l_ldap_cert_label		= "SSL certificate (CA)";
111
	$l_ldap_cert_label		= "SSL certificate (CA)";
112
	$l_ldap_cert_text		= "Certificate of the certification authority that signed the LDAP server certificate";
112
	$l_ldap_cert_text		= "Certificate of the certification authority that signed the LDAP server certificate";
113
	$l_ldap_cert_status_cur		= "Current certificate:";
113
	$l_ldap_cert_status_cur		= "Current certificate:";
114
	$l_ldap_cert_status_no		= "No certificate imported";
114
	$l_ldap_cert_status_no		= "No certificate imported";
115
	$l_ldap_submit			= "Save";
115
	$l_ldap_submit			= "Save";
116
	$l_ldap_test_service_failed	= "LDAP service is not reachable on that server (check IP)";
116
	$l_ldap_test_service_failed	= "LDAP service is not reachable on that server (check IP)";
117
	$l_ldap_test_service_ok		= "A port 389 (636 with SSL) is open on this server";
117
	$l_ldap_test_service_ok		= "A port 389 (636 with SSL) is open on this server";
118
	$l_ldap_test_connection_failed	= "LDAP connexion failed (check the LDAP service on this server)";
118
	$l_ldap_test_connection_failed	= "LDAP connexion failed (check the LDAP service on this server)";
119
	$l_ldap_test_connection_ok	= "A LDAP connexion is established";
119
	$l_ldap_test_connection_ok	= "A LDAP connexion is established";
120
	$l_ldap_test_bind_failed	= "LDAP authentication failed (check the LDAP user and password)";
120
	$l_ldap_test_bind_failed	= "LDAP authentication failed (check the LDAP user and password)";
121
	$l_ldap_test_bind_ok		= "Successful authentication";
121
	$l_ldap_test_bind_ok		= "Successful authentication";
122
	$l_ldap_test_dn_failed		= "DN of the base seems to be wrong (check it)";
122
	$l_ldap_test_dn_failed		= "DN of the base seems to be wrong (check it)";
123
	$l_ldap_test_dn_ok		= "DN of the base seems to be ok";
123
	$l_ldap_test_dn_ok		= "DN of the base seems to be ok";
124
	$l_ldap_cert_cn_diff_dn		= "Certificate CommonName (§cert_domainName§) is different from the server domain name";
124
	$l_ldap_cert_cn_diff_dn		= "Certificate CommonName (§cert_domainName§) is different from the server domain name";
125
	$l_ldap_error			= "LDAP error";
125
	$l_ldap_error			= "LDAP error";
126
	$l_ldap_entries			= "entries in the base";
126
	$l_ldap_entries			= "entries in the base";
127
	$l_check			= "Check this config";
127
	$l_check			= "Check this config";
128
	$l_checkingConf			= "Checking this configuration...";
128
	$l_checkingConf			= "Checking this configuration...";
129
}
129
}
130
 
130
 
131
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_uid, $f_ldap_ssl, $f_ldap_cert, $f_ldap_cert_required) {
131
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_base_filter, $f_ldap_uid, $f_ldap_ssl, $f_ldap_cert, $f_ldap_cert_required) {
132
	// Socket to the LDAP port of the server
132
	// Socket to the LDAP port of the server
133
	if (!$sock = @fsockopen($f_ldap_server, (($f_ldap_ssl) ? 636 : 389), $num, $error, 2)) {
133
	if (!$sock = @fsockopen($f_ldap_server, (($f_ldap_ssl) ? 636 : 389), $num, $error, 2)) {
134
		// no network connection
134
		// no network connection
135
		return -2;
135
		return -2;
136
	}
136
	}
137
	fclose($sock);
137
	fclose($sock);
138
 
138
 
139
	// Set LDAP SSL options
139
	// Set LDAP SSL options
140
	if ($f_ldap_ssl) {
140
	if ($f_ldap_ssl) {
141
		if ($f_ldap_cert_required) {
141
		if ($f_ldap_cert_required) {
142
			if ($f_ldap_cert) {
142
			if ($f_ldap_cert) {
143
				ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, $f_ldap_cert);
143
				ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, $f_ldap_cert);
144
			}
144
			}
145
			ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND);
145
			ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND);
146
 
146
 
147
		} else {
147
		} else {
148
			ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
148
			ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
149
		}
149
		}
150
	}
150
	}
151
 
151
 
152
	// if ok, Test LDAP connection
152
	// if ok, Test LDAP connection
153
	$ldapconn = @ldap_connect((($f_ldap_ssl)?'ldaps':'ldap').'://'.$f_ldap_server);
153
	$ldapconn = @ldap_connect((($f_ldap_ssl)?'ldaps':'ldap').'://'.$f_ldap_server);
154
	if (!$ldapconn) {
154
	if (!$ldapconn) {
155
		// LDAP connection failed
155
		// LDAP connection failed
156
		return -1;
156
		return -1;
157
	}
157
	}
158
 
158
 
159
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
159
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
160
 
160
 
161
	// if ok, test a ldap-bind with the user used by ALCASAR
161
	// if ok, test a ldap-bind with the user used by ALCASAR
162
	$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
162
	$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
163
	if (!$ldapbind) {
163
	if (!$ldapbind) {
164
		// Test LDAP Version 3
164
		// Test LDAP Version 3
165
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
165
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
166
		$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
166
		$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
167
		if (!$ldapbind) {
167
		if (!$ldapbind) {
168
			// LDAP Bind failed
168
			// LDAP Bind failed
169
			return 0;
169
			return 0;
170
		}
170
		}
171
	}
171
	}
172
 
172
 
173
	// if ok, try to query the directory of users
173
	// if ok, try to query the directory of users
-
 
174
	$filter = ((!empty($f_ldap_base_filter)) ? $f_ldap_base_filter : '&');
174
	$query = $f_ldap_uid.'=*';
175
	$query = "(&($f_ldap_uid=*)($filter))";
175
	$ldap_result = @ldap_search($ldapconn, $f_ldap_basedn, $query);
176
	$ldap_result = @ldap_search($ldapconn, $f_ldap_basedn, $query);
176
	if ($ldap_result) {
177
	if ($ldap_result) {
177
		$ldap_users_count = ldap_count_entries($ldapconn, $ldap_result);
178
		$ldap_users_count = ldap_count_entries($ldapconn, $ldap_result);
178
		return ($ldap_users_count + 2);
179
		return ($ldap_users_count + 2);
179
	} else {
180
	} else {
180
		return 1;
181
		return 1;
181
	}
182
	}
182
	ldap_unbind($ldapconn);
183
	ldap_unbind($ldapconn);
183
}
184
}
184
 
185
 
185
$messages = '';
186
$messages = '';
186
$LDAPS_CERT_LOC = '/etc/raddb/certs/alcasar-ldaps.crt';
187
$LDAPS_CERT_LOC = '/etc/raddb/certs/alcasar-ldaps.crt';
187
 
188
 
188
if (isset($_POST['auth_enable'])) {
189
if (isset($_POST['auth_enable'])) {
189
	if ($_POST['auth_enable'] === '1') {
190
	if ($_POST['auth_enable'] === '1') {
190
		// TODO : need to translate messages
191
		// TODO : need to translate messages
191
		$varErrors = [];
192
		$varErrors = [];
192
		if (isset($_POST['ldap_server']))        $ldap_server        = $_POST['ldap_server'];        else array_push($varErrors, 'Variable error "ldap_server"');
193
		if (isset($_POST['ldap_server']))        $ldap_server        = $_POST['ldap_server'];        else array_push($varErrors, 'Variable error "ldap_server"');
193
		if (isset($_POST['ldap_base_dn']))       $ldap_base_dn       = $_POST['ldap_base_dn'];       else array_push($varErrors, 'Variable error "ldap_base_dn"');
194
		if (isset($_POST['ldap_base_dn']))       $ldap_base_dn       = $_POST['ldap_base_dn'];       else array_push($varErrors, 'Variable error "ldap_base_dn"');
194
		if (isset($_POST['ldap_uid']))           $ldap_uid           = $_POST['ldap_uid'];           else array_push($varErrors, 'Variable error "ldap_uid"');
195
		if (isset($_POST['ldap_uid']))           $ldap_uid           = $_POST['ldap_uid'];           else array_push($varErrors, 'Variable error "ldap_uid"');
195
		if (isset($_POST['ldap_base_filter']))   $ldap_base_filter   = $_POST['ldap_base_filter'];   else array_push($varErrors, 'Variable error "ldap_base_filter"');
196
		if (isset($_POST['ldap_base_filter']))   $ldap_base_filter   = $_POST['ldap_base_filter'];   else array_push($varErrors, 'Variable error "ldap_base_filter"');
196
		if (isset($_POST['ldap_user']))          $ldap_user          = $_POST['ldap_user'];          else array_push($varErrors, 'Variable error "ldap_user"');
197
		if (isset($_POST['ldap_user']))          $ldap_user          = $_POST['ldap_user'];          else array_push($varErrors, 'Variable error "ldap_user"');
197
		if (isset($_POST['ldap_password']))      $ldap_password      = $_POST['ldap_password'];      else array_push($varErrors, 'Variable error "ldap_password"');
198
		if (isset($_POST['ldap_password']))      $ldap_password      = $_POST['ldap_password'];      else array_push($varErrors, 'Variable error "ldap_password"');
198
		if (isset($_POST['ldap_ssl']))           $ldap_ssl           = $_POST['ldap_ssl'];           //else array_push($varErrors, 'Variable error "ldap_ssl"');
199
		if (isset($_POST['ldap_ssl']))           $ldap_ssl           = $_POST['ldap_ssl'];           //else array_push($varErrors, 'Variable error "ldap_ssl"');
199
		if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];         //else array_push($varErrors, 'Variable error "ldap_cert"');
200
		if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];         //else array_push($varErrors, 'Variable error "ldap_cert"');
200
		if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = $_POST['ldap_cert_required']; //else array_push($varErrors, 'Variable error "ldap_cert_required"');
201
		if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = $_POST['ldap_cert_required']; //else array_push($varErrors, 'Variable error "ldap_cert_required"');
201
 
202
 
202
		if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
203
		if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
203
			if ($ldap_cert_required === 'on') {
204
			if ($ldap_cert_required === 'on') {
204
				$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
205
				$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
205
				if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
206
				if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
206
					$cert_domainName = implode('.', array_reverse($matches[1]));
207
					$cert_domainName = implode('.', array_reverse($matches[1]));
207
					if ($cert_domainName !== $ldap_server) {
208
					if ($cert_domainName !== $ldap_server) {
208
						array_push($varErrors, str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn));
209
						array_push($varErrors, str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn));
209
					}
210
					}
210
				}
211
				}
211
			}
212
			}
212
		}
213
		}
213
 
214
 
214
		if (!empty($varErrors)) { 
215
		if (!empty($varErrors)) { 
215
			foreach ($varErrors as $error) {
216
			foreach ($varErrors as $error) {
216
				$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
217
				$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
217
			}
218
			}
218
		} else {
219
		} else {
219
			exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
220
			exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
220
			exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
221
			exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
221
			exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_uid/g").' '.CONF_FILE);
222
			exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_uid/g").' '.CONF_FILE);
222
			exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_base_filter/g").' '.CONF_FILE);
223
			exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_base_filter/g").' '.CONF_FILE);
223
			exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
224
			exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
224
			exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
225
			exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
225
			exec('sed -i '.escapeshellarg("s/^LDAP_SSL=.*/LDAP_SSL=$ldap_ssl/g").' '.CONF_FILE);
226
			exec('sed -i '.escapeshellarg("s/^LDAP_SSL=.*/LDAP_SSL=$ldap_ssl/g").' '.CONF_FILE);
226
			exec('sed -i '.escapeshellarg("s/^LDAP_CERT_REQUIRED=.*/LDAP_CERT_REQUIRED=$ldap_cert_required/g").' '.CONF_FILE);
227
			exec('sed -i '.escapeshellarg("s/^LDAP_CERT_REQUIRED=.*/LDAP_CERT_REQUIRED=$ldap_cert_required/g").' '.CONF_FILE);
227
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
228
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
228
			if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
229
			if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
229
				exec('sudo /usr/local/bin/alcasar-ldap.sh --import-cert '.escapeshellarg($ldap_cert['tmp_name']));
230
				exec('sudo /usr/local/bin/alcasar-ldap.sh --import-cert '.escapeshellarg($ldap_cert['tmp_name']));
230
			}
231
			}
231
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
232
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
232
			$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update.'</span><br>';
233
			$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update.'</span><br>';
233
		}
234
		}
234
	} else {
235
	} else {
235
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
236
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
236
		exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
237
		exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
237
		$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update.'</span><br>';
238
		$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update.'</span><br>';
238
	}
239
	}
239
 
240
 
240
	// Reload configuration
241
	// Reload configuration
241
	$file_conf = fopen(CONF_FILE, 'r');
242
	$file_conf = fopen(CONF_FILE, 'r');
242
	if (!$file_conf) {
243
	if (!$file_conf) {
243
		exit('Error opening the file '.CONF_FILE);
244
		exit('Error opening the file '.CONF_FILE);
244
	}
245
	}
245
	while (!feof($file_conf)) {
246
	while (!feof($file_conf)) {
246
		$buffer = fgets($file_conf, 4096);
247
		$buffer = fgets($file_conf, 4096);
247
		if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
248
		if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
248
			$tmp = explode('=', $buffer, 2);
249
			$tmp = explode('=', $buffer, 2);
249
			$conf[trim($tmp[0])] = trim($tmp[1]);
250
			$conf[trim($tmp[0])] = trim($tmp[1]);
250
		}
251
		}
251
	}
252
	}
252
	fclose($file_conf);
253
	fclose($file_conf);
253
}
254
}
254
 
255
 
255
// LDAP configuration params
256
// LDAP configuration params
256
$ldap_status        = ($conf['LDAP'] === 'on');
257
$ldap_status        = ($conf['LDAP'] === 'on');
257
$ldap_server        = $conf['LDAP_SERVER'];
258
$ldap_server        = $conf['LDAP_SERVER'];
258
$ldap_user          = $conf['LDAP_USER'];
259
$ldap_user          = $conf['LDAP_USER'];
259
$ldap_password      = $conf['LDAP_PASSWORD'];
260
$ldap_password      = $conf['LDAP_PASSWORD'];
260
$ldap_base_dn       = $conf['LDAP_BASE'];
261
$ldap_base_dn       = $conf['LDAP_BASE'];
261
$ldap_uid	    = $conf['LDAP_UID'];
262
$ldap_uid	    = $conf['LDAP_UID'];
262
$ldap_base_filter   = $conf['LDAP_FILTER'];
263
$ldap_base_filter   = $conf['LDAP_FILTER'];
263
$ldap_ssl           = ($conf['LDAP_SSL'] === 'on');
264
$ldap_ssl           = ($conf['LDAP_SSL'] === 'on');
264
$ldap_cert_required = ($conf['LDAP_CERT_REQUIRED'] === 'on');
265
$ldap_cert_required = ($conf['LDAP_CERT_REQUIRED'] === 'on');
265
 
266
 
266
$ldap_cert_subject = NULL;
267
$ldap_cert_subject = NULL;
267
if (file_exists($LDAPS_CERT_LOC)) {
268
if (file_exists($LDAPS_CERT_LOC)) {
268
	$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
269
	$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
269
	if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
270
	if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
270
		$ldap_cert_subject = implode('.', array_reverse($matches[1]));
271
		$ldap_cert_subject = implode('.', array_reverse($matches[1]));
271
	}
272
	}
272
}
273
}
273
 
274
 
274
// AJAX LDAP configuration checker
275
// AJAX LDAP configuration checker
275
if (isset($_GET['conf_check'])) {
276
if (isset($_GET['conf_check'])) {
276
	$response = [
277
	$response = [
277
		'enable' => $ldap_status
278
		'enable' => $ldap_status
278
	];
279
	];
279
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
280
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
280
		$varErrors = [];
281
		$varErrors = [];
281
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
282
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
282
			// TODO : need to translate messages
283
			// TODO : need to translate messages
283
			if (isset($_POST['ldap_server']))        $ldap_server        = $_POST['ldap_server'];                   else array_push($varErrors, 'Variable error "ldap_server"');
284
			if (isset($_POST['ldap_server']))        $ldap_server        = $_POST['ldap_server'];                   else array_push($varErrors, 'Variable error "ldap_server"');
284
			if (isset($_POST['ldap_base_dn']))       $ldap_base_dn       = $_POST['ldap_base_dn'];                  else array_push($varErrors, 'Variable error "ldap_base_dn"');
285
			if (isset($_POST['ldap_base_dn']))       $ldap_base_dn       = $_POST['ldap_base_dn'];                  else array_push($varErrors, 'Variable error "ldap_base_dn"');
285
			if (isset($_POST['ldap_uid']))           $ldap_uid           = $_POST['ldap_uid'];                      else array_push($varErrors, 'Variable error "ldap_uid"');
286
			if (isset($_POST['ldap_uid']))           $ldap_uid           = $_POST['ldap_uid'];                      else array_push($varErrors, 'Variable error "ldap_uid"');
286
			if (isset($_POST['ldap_base_filter']))   $ldap_base_filter   = $_POST['ldap_base_filter'];              else array_push($varErrors, 'Variable error "ldap_base_filter"');
287
			if (isset($_POST['ldap_base_filter']))   $ldap_base_filter   = $_POST['ldap_base_filter'];              else array_push($varErrors, 'Variable error "ldap_base_filter"');
287
			if (isset($_POST['ldap_user']))          $ldap_user          = $_POST['ldap_user'];                     else array_push($varErrors, 'Variable error "ldap_user"');
288
			if (isset($_POST['ldap_user']))          $ldap_user          = $_POST['ldap_user'];                     else array_push($varErrors, 'Variable error "ldap_user"');
288
			if (isset($_POST['ldap_password']))      $ldap_password      = $_POST['ldap_password'];                 else array_push($varErrors, 'Variable error "ldap_password"');
289
			if (isset($_POST['ldap_password']))      $ldap_password      = $_POST['ldap_password'];                 else array_push($varErrors, 'Variable error "ldap_password"');
289
			if (isset($_POST['ldap_ssl']))           $ldap_ssl           = ($_POST['ldap_ssl'] === 'on');           //else array_push($varErrors, 'Variable error "ldap_ssl"');
290
			if (isset($_POST['ldap_ssl']))           $ldap_ssl           = ($_POST['ldap_ssl'] === 'on');           //else array_push($varErrors, 'Variable error "ldap_ssl"');
290
			if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];                    //else array_push($varErrors, 'Variable error "ldap_cert"');
291
			if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];                    //else array_push($varErrors, 'Variable error "ldap_cert"');
291
			if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = ($_POST['ldap_cert_required'] === 'on'); //else array_push($varErrors, 'Variable error "ldap_cert_required"');
292
			if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = ($_POST['ldap_cert_required'] === 'on'); //else array_push($varErrors, 'Variable error "ldap_cert_required"');
292
		}
293
		}
293
 
294
 
294
		if (($ldap_cert_required) && isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
295
		if (($ldap_cert_required) && isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
295
			$ldap_cert_tmpFilename = $ldap_cert['tmp_name'];
296
			$ldap_cert_tmpFilename = $ldap_cert['tmp_name'];
296
		} else {
297
		} else {
297
			$ldap_cert_tmpFilename = NULL;
298
			$ldap_cert_tmpFilename = NULL;
298
		}
299
		}
299
 
300
 
300
		if (!empty($varErrors)) {
301
		if (!empty($varErrors)) {
301
			$response['errors'] = $varErrors;
302
			$response['errors'] = $varErrors;
302
		} else {
303
		} else {
303
			$result = ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
304
			$result = ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_base_filter, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
304
 
305
 
305
			if (($result === 0) && ($ldap_ssl && $ldap_cert_required && ((isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) || (file_exists($LDAPS_CERT_LOC))))) {
306
			if (($result === 0) && ($ldap_ssl && $ldap_cert_required && ((isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) || (file_exists($LDAPS_CERT_LOC))))) {
306
				if (filter_var($ldap_server, FILTER_VALIDATE_IP) !== false) {
307
				if (filter_var($ldap_server, FILTER_VALIDATE_IP) !== false) {
307
					if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
308
					if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
308
						$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
309
						$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
309
					} else {
310
					} else {
310
						$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
311
						$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
311
					}
312
					}
312
					if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
313
					if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
313
						$cert_domainName = implode('.', array_reverse($matches[1]));
314
						$cert_domainName = implode('.', array_reverse($matches[1]));
314
						if ($cert_domainName !== $ldap_server) {
315
						if ($cert_domainName !== $ldap_server) {
315
							$response['warnings'][] = str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn);
316
							$response['warnings'][] = str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn);
316
						}
317
						}
317
					}
318
					}
318
					$res = ldap_checkServerConfig($cert_domainName, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
319
					$res = ldap_checkServerConfig($cert_domainName, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_base_filter, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
319
					if ($res > 1) {
320
					if ($res > 1) {
320
						$result = $res;
321
						$result = $res;
321
					}
322
					}
322
					$result = max($result, $res);
323
					$result = max($result, $res);
323
				}
324
				}
324
			}
325
			}
325
 
326
 
326
			$response['result'] = $result;
327
			$response['result'] = $result;
327
		}
328
		}
328
	}
329
	}
329
 
330
 
330
	header('Content-Type: application/json');
331
	header('Content-Type: application/json');
331
	echo json_encode($response);
332
	echo json_encode($response);
332
	exit();
333
	exit();
333
}
334
}
334
 
335
 
335
?>
336
?>
336
<!DOCTYPE html>
337
<!DOCTYPE html>
337
<html>
338
<html>
338
<head>
339
<head>
339
	<meta charset="UTF-8">
340
	<meta charset="UTF-8">
340
	<title><?= $l_ldap_title ?></title>
341
	<title><?= $l_ldap_title ?></title>
341
	<link type="text/css" href="/css/style.css" rel="stylesheet">
342
	<link type="text/css" href="/css/style.css" rel="stylesheet">
342
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
343
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
343
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
344
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
344
	<script>
345
	<script>
345
	function onLdapStatusChange() {
346
	function onLdapStatusChange() {
346
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_uid', 'ldap_base_filter', 'ldap_user', 'ldap_password', 'ldap_ssl', 'ldap_cert_required', 'ldap_cert'];
347
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_uid', 'ldap_base_filter', 'ldap_user', 'ldap_password', 'ldap_ssl', 'ldap_cert_required', 'ldap_cert'];
347
		var formSubmit = document.querySelector('form input[type="submit"]');
348
		var formSubmit = document.querySelector('form input[type="submit"]');
348
		var btn_checkConf = document.getElementById('btn-checkconf');
349
		var btn_checkConf = document.getElementById('btn-checkconf');
349
		var isChecked = false;
350
		var isChecked = false;
350
 
351
 
351
		if (document.getElementById('auth_enable').value === '1') {
352
		if (document.getElementById('auth_enable').value === '1') {
352
			for (var i=0; i<listToDisables.length; i++) {
353
			for (var i=0; i<listToDisables.length; i++) {
353
				document.getElementById(listToDisables[i]).style.backgroundColor = null;
354
				document.getElementById(listToDisables[i]).style.backgroundColor = null;
354
				document.getElementById(listToDisables[i]).disabled = false;
355
				document.getElementById(listToDisables[i]).disabled = false;
355
			}
356
			}
356
			if (document.getElementById('ldap_ssl').value === 'off') {
357
			if (document.getElementById('ldap_ssl').value === 'off') {
357
				document.getElementById('ldap_cert_required').style.backgroundColor = '#c0c0c0';
358
				document.getElementById('ldap_cert_required').style.backgroundColor = '#c0c0c0';
358
				document.getElementById('ldap_cert_required').disabled = true;
359
				document.getElementById('ldap_cert_required').disabled = true;
359
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
360
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
360
				document.getElementById('ldap_cert').disabled = true;
361
				document.getElementById('ldap_cert').disabled = true;
361
			}
362
			}
362
			else if (document.getElementById('ldap_cert_required').value === 'off') {
363
			else if (document.getElementById('ldap_cert_required').value === 'off') {
363
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
364
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
364
				document.getElementById('ldap_cert').disabled = true;
365
				document.getElementById('ldap_cert').disabled = true;
365
			}
366
			}
366
			formSubmit.style.display = 'none';
367
			formSubmit.style.display = 'none';
367
			btn_checkConf.style.display = null;
368
			btn_checkConf.style.display = null;
368
			<?php if (PHP_VERSION_ID < 70100): ?>
369
			<?php if (PHP_VERSION_ID < 70100): ?>
369
			// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
370
			// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
370
			formSubmit.style.display = null;
371
			formSubmit.style.display = null;
371
			<?php endif; ?>
372
			<?php endif; ?>
372
		} else {
373
		} else {
373
			for (var i=0; i<listToDisables.length; i++) {
374
			for (var i=0; i<listToDisables.length; i++) {
374
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
375
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
375
				document.getElementById(listToDisables[i]).disabled = true;
376
				document.getElementById(listToDisables[i]).disabled = true;
376
			}
377
			}
377
			formSubmit.style.display = null;
378
			formSubmit.style.display = null;
378
			btn_checkConf.style.display = 'none';
379
			btn_checkConf.style.display = 'none';
379
		}
380
		}
380
	}
381
	}
381
 
382
 
382
	function checkConfig() {
383
	function checkConfig() {
383
		var messagesElem = document.querySelector('fieldset > legend > div');
384
		var messagesElem = document.querySelector('fieldset > legend > div');
384
		var formSubmit   = document.querySelector('form input[type="submit"]');
385
		var formSubmit   = document.querySelector('form input[type="submit"]');
385
		var btn_checkConf = document.getElementById('btn-checkconf');
386
		var btn_checkConf = document.getElementById('btn-checkconf');
386
		var form = document.getElementById('form-config_ldap');
387
		var form = document.getElementById('form-config_ldap');
387
		var post_data = new FormData(form);
388
		var post_data = new FormData(form);
388
		post_data.delete('auth_enable');
389
		post_data.delete('auth_enable');
389
 
390
 
390
		messagesElem.innerHTML = '<?= $l_checkingConf ?>';
391
		messagesElem.innerHTML = '<?= $l_checkingConf ?>';
391
 
392
 
392
		var xhr = new XMLHttpRequest();
393
		var xhr = new XMLHttpRequest();
393
		xhr.onreadystatechange = function() {
394
		xhr.onreadystatechange = function() {
394
			if (this.readyState == 4) {
395
			if (this.readyState == 4) {
395
				if (this.status == 200) {
396
				if (this.status == 200) {
396
					var data = JSON.parse(this.responseText);
397
					var data = JSON.parse(this.responseText);
397
 
398
 
398
					var messages = '';
399
					var messages = '';
399
 
400
 
400
					if (typeof data.result !== 'undefined') {
401
					if (typeof data.result !== 'undefined') {
401
						if (data.result === -2) {
402
						if (data.result === -2) {
402
							messages += "<span style=\"color: red\"><?= $l_ldap_test_service_failed ?></span>";
403
							messages += "<span style=\"color: red\"><?= $l_ldap_test_service_failed ?></span>";
403
						} else {
404
						} else {
404
							messages += "<span style=\"color: green\"><?= $l_ldap_test_service_ok ?></span>";
405
							messages += "<span style=\"color: green\"><?= $l_ldap_test_service_ok ?></span>";
405
							if (data.result === -1) {
406
							if (data.result === -1) {
406
								messages += "<br><span style=\"color: red\"><?= $l_ldap_test_connection_failed ?></span>";
407
								messages += "<br><span style=\"color: red\"><?= $l_ldap_test_connection_failed ?></span>";
407
							} else {
408
							} else {
408
								messages += "<br><span style=\"color: green\"><?= $l_ldap_test_connection_ok ?></span>";
409
								messages += "<br><span style=\"color: green\"><?= $l_ldap_test_connection_ok ?></span>";
409
								if (data.result === 0) {
410
								if (data.result === 0) {
410
									messages += "<br><span style=\"color: red\"><?= $l_ldap_test_bind_failed ?></span>";
411
									messages += "<br><span style=\"color: red\"><?= $l_ldap_test_bind_failed ?></span>";
411
								} else {
412
								} else {
412
									messages += "<br><span style=\"color: green\"><?= $l_ldap_test_bind_ok ?></span>";
413
									messages += "<br><span style=\"color: green\"><?= $l_ldap_test_bind_ok ?></span>";
413
									if (data.result === 1) {
414
									if (data.result === 1) {
414
										messages += "<br><span style=\"color: red\"><?= $l_ldap_test_dn_failed ?></span>";
415
										messages += "<br><span style=\"color: red\"><?= $l_ldap_test_dn_failed ?></span>";
415
									} else {
416
									} else {
416
										messages += "<br><span style=\"color: green\"><?= $l_ldap_test_dn_ok ?> (" + (data.result - 2) + " <?= $l_ldap_entries?>)</span>";
417
										messages += "<br><span style=\"color: green\"><?= $l_ldap_test_dn_ok ?> (" + (data.result - 2) + " <?= $l_ldap_entries?>)</span>";
417
									}
418
									}
418
								}
419
								}
419
							}
420
							}
420
						}
421
						}
421
					}
422
					}
422
 
423
 
423
					if (data.result > 1) {
424
					if (data.result > 1) {
424
						formSubmit.style.display = null;
425
						formSubmit.style.display = null;
425
						btn_checkConf.style.display = 'none';
426
						btn_checkConf.style.display = 'none';
426
					} else {
427
					} else {
427
						formSubmit.style.display = 'none';
428
						formSubmit.style.display = 'none';
428
						btn_checkConf.style.display = null;
429
						btn_checkConf.style.display = null;
429
					}
430
					}
430
					<?php if (PHP_VERSION_ID < 70100): ?>
431
					<?php if (PHP_VERSION_ID < 70100): ?>
431
					// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
432
					// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
432
					formSubmit.style.display = null;
433
					formSubmit.style.display = null;
433
					<?php endif; ?>
434
					<?php endif; ?>
434
 
435
 
435
					if (typeof data.errors !== 'undefined') {
436
					if (typeof data.errors !== 'undefined') {
436
						messages = '<span style=\"color: red\">' + data.errors.join('</span><br><span style=\"color: red\">') + '</span><br>';
437
						messages = '<span style=\"color: red\">' + data.errors.join('</span><br><span style=\"color: red\">') + '</span><br>';
437
					}
438
					}
438
					if (typeof data.warnings !== 'undefined') {
439
					if (typeof data.warnings !== 'undefined') {
439
						messages += '<br><span style=\"color: orange\">' + data.warnings.join('</span><br><span style=\"color: orange\">') + '</span><br>';
440
						messages += '<br><span style=\"color: orange\">' + data.warnings.join('</span><br><span style=\"color: orange\">') + '</span><br>';
440
					}
441
					}
441
 
442
 
442
					messagesElem.innerHTML = messages;
443
					messagesElem.innerHTML = messages;
443
				} else {
444
				} else {
444
					messagesElem.innerHTML = 'server error';
445
					messagesElem.innerHTML = 'server error';
445
				}
446
				}
446
			}
447
			}
447
		};
448
		};
448
		xhr.open('POST', 'ldap.php?conf_check', true);
449
		xhr.open('POST', 'ldap.php?conf_check', true);
449
		xhr.send(post_data);
450
		xhr.send(post_data);
450
	}
451
	}
451
	</script>
452
	</script>
452
</head>
453
</head>
453
<body onLoad="onLdapStatusChange();">
454
<body onLoad="onLdapStatusChange();">
454
	<div class="panel">
455
	<div class="panel">
455
		<div class="panel-header"><?= $l_ldap_legend ?></div>
456
		<div class="panel-header"><?= $l_ldap_legend ?></div>
456
		<div class="panel-body">
457
		<div class="panel-body">
457
			<form id="form-config_ldap" name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data">
458
			<form id="form-config_ldap" name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data">
458
				<fieldset>
459
				<fieldset>
459
					<legend>
460
					<legend>
460
						<br>
461
						<br>
461
						<div style="text-align: center">
462
						<div style="text-align: center">
462
							<?php if ($messages): ?>
463
							<?php if ($messages): ?>
463
								<?= $messages ?>
464
								<?= $messages ?>
464
							<?php endif; ?>
465
							<?php endif; ?>
465
						</div>
466
						</div>
466
					</legend>
467
					</legend>
467
					<dl>
468
					<dl>
468
						<dt>
469
						<dt>
469
							<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
470
							<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
470
						</dt>
471
						</dt>
471
						<dd>
472
						<dd>
472
							<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
473
							<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
473
								<option value="1"<?= ($ldap_status)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
474
								<option value="1"<?= ($ldap_status)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
474
								<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
475
								<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
475
							</select>
476
							</select>
476
						</dd>
477
						</dd>
477
					</dl>
478
					</dl>
478
					<dl>
479
					<dl>
479
						<dt>
480
						<dt>
480
							<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
481
							<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
481
							<?= $l_ldap_server_text ?>
482
							<?= $l_ldap_server_text ?>
482
						</dt>
483
						</dt>
483
						<dd>
484
						<dd>
484
							<input type="text" id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>" oninput="onLdapStatusChange();">
485
							<input type="text" id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>" oninput="onLdapStatusChange();">
485
						</dd>
486
						</dd>
486
					</dl>
487
					</dl>
487
					<dl>
488
					<dl>
488
						<dt>
489
						<dt>
489
							<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
490
							<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
490
							<?= $l_ldap_base_dn_text ?>
491
							<?= $l_ldap_base_dn_text ?>
491
						</dt>
492
						</dt>
492
						<dd>
493
						<dd>
493
							<input type="text" id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_base_dn) ?>" oninput="onLdapStatusChange();">
494
							<input type="text" id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_base_dn) ?>" oninput="onLdapStatusChange();">
494
						</dd>
495
						</dd>
495
					</dl>
496
					</dl>
496
					<dl>
497
					<dl>
497
						<dt>
498
						<dt>
498
							<label for="ldap_uid"><?= $l_ldap_uid_label ?></label><br>
499
							<label for="ldap_uid"><?= $l_ldap_uid_label ?></label><br>
499
							<?= $l_ldap_uid_text ?>
500
							<?= $l_ldap_uid_text ?>
500
						</dt>
501
						</dt>
501
						<dd>
502
						<dd>
502
							<input type="text" id="ldap_uid" size="40" name="ldap_uid" value="<?= htmlspecialchars($ldap_uid) ?>" oninput="onLdapStatusChange();">
503
							<input type="text" id="ldap_uid" size="40" name="ldap_uid" value="<?= htmlspecialchars($ldap_uid) ?>" oninput="onLdapStatusChange();">
503
						</dd>
504
						</dd>
504
					</dl>
505
					</dl>
505
					<dl>
506
					<dl>
506
						<dt>
507
						<dt>
507
							<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
508
							<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
508
							<?= $l_ldap_base_filter_text ?>
509
							<?= $l_ldap_base_filter_text ?>
509
						</dt>
510
						</dt>
510
						<dd>
511
						<dd>
511
							<input type="text" id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>" oninput="onLdapStatusChange();">
512
							<input type="text" id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>" oninput="onLdapStatusChange();">
512
						</dd>
513
						</dd>
513
					</dl>
514
					</dl>
514
					<dl>
515
					<dl>
515
						<dt>
516
						<dt>
516
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
517
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
517
							<?= $l_ldap_user_text ?>
518
							<?= $l_ldap_user_text ?>
518
						</dt>
519
						</dt>
519
						<dd>
520
						<dd>
520
							<input type="text" id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_user) ?>" oninput="onLdapStatusChange();">
521
							<input type="text" id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_user) ?>" oninput="onLdapStatusChange();">
521
						</dd>
522
						</dd>
522
					</dl>
523
					</dl>
523
					<dl>
524
					<dl>
524
						<dt>
525
						<dt>
525
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
526
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
526
							<?= $l_ldap_password_text ?>
527
							<?= $l_ldap_password_text ?>
527
						</dt>
528
						</dt>
528
						<dd>
529
						<dd>
529
							<input type="password" id="ldap_password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>" oninput="onLdapStatusChange();">
530
							<input type="password" id="ldap_password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>" oninput="onLdapStatusChange();">
530
						</dd>
531
						</dd>
531
					</dl>
532
					</dl>
532
					<dl>
533
					<dl>
533
						<dt>
534
						<dt>
534
							<label for="ldap_ssl"><?= $l_ldap_ssl_label ?></label><br>
535
							<label for="ldap_ssl"><?= $l_ldap_ssl_label ?></label><br>
535
							<?= $l_ldap_ssl_text ?><br>
536
							<?= $l_ldap_ssl_text ?><br>
536
						</dt>
537
						</dt>
537
						<dd>
538
						<dd>
538
							<select id="ldap_ssl" name="ldap_ssl" onchange="onLdapStatusChange();">
539
							<select id="ldap_ssl" name="ldap_ssl" onchange="onLdapStatusChange();">
539
								<option value="on"<?= ($ldap_ssl)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
540
								<option value="on"<?= ($ldap_ssl)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
540
								<option value="off"<?= (!$ldap_ssl) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
541
								<option value="off"<?= (!$ldap_ssl) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
541
							</select>
542
							</select>
542
						</dd>
543
						</dd>
543
					</dl>
544
					</dl>
544
					<dl>
545
					<dl>
545
						<dt>
546
						<dt>
546
							<label for="ldap_cert_required"><?= $l_ldap_cert_required_label ?></label><br>
547
							<label for="ldap_cert_required"><?= $l_ldap_cert_required_label ?></label><br>
547
							<?= $l_ldap_cert_required_text ?><br>
548
							<?= $l_ldap_cert_required_text ?><br>
548
						</dt>
549
						</dt>
549
						<dd>
550
						<dd>
550
							<select id="ldap_cert_required" name="ldap_cert_required" onchange="onLdapStatusChange();">
551
							<select id="ldap_cert_required" name="ldap_cert_required" onchange="onLdapStatusChange();">
551
								<option value="on"<?= ($ldap_cert_required)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
552
								<option value="on"<?= ($ldap_cert_required)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
552
								<option value="off"<?= (!$ldap_cert_required) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
553
								<option value="off"<?= (!$ldap_cert_required) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
553
							</select>
554
							</select>
554
						</dd>
555
						</dd>
555
					</dl>
556
					</dl>
556
					<dl>
557
					<dl>
557
						<dt>
558
						<dt>
558
							<label for="ldap_cert"><?= $l_ldap_cert_label ?></label><br>
559
							<label for="ldap_cert"><?= $l_ldap_cert_label ?></label><br>
559
							<?= $l_ldap_cert_text ?><br>
560
							<?= $l_ldap_cert_text ?><br>
560
							<?= (($ldap_cert_subject) ? "$l_ldap_cert_status_cur $ldap_cert_subject" : $l_ldap_cert_status_no ) ?>
561
							<?= (($ldap_cert_subject) ? "$l_ldap_cert_status_cur $ldap_cert_subject" : $l_ldap_cert_status_no ) ?>
561
						</dt>
562
						</dt>
562
						<dd>
563
						<dd>
563
							<input type="file" id="ldap_cert" name="ldap_cert" oninput="onLdapStatusChange();">
564
							<input type="file" id="ldap_cert" name="ldap_cert" oninput="onLdapStatusChange();">
564
						</dd>
565
						</dd>
565
					</dl>
566
					</dl>
566
					<p>
567
					<p>
567
						<button id="btn-checkconf" onclick="checkConfig(); return false;"><?= $l_check ?></button>
568
						<button id="btn-checkconf" onclick="checkConfig(); return false;"><?= $l_check ?></button>
568
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
569
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
569
					</p>
570
					</p>
570
				</fieldset>
571
				</fieldset>
571
			</form>
572
			</form>
572
		</div>
573
		</div>
573
	</div>
574
	</div>
574
</body>
575
</body>
575
</html>
576
</html>
576
 
577