Subversion Repositories ALCASAR

Rev

Rev 2532 | Rev 2710 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2532 Rev 2705
Line 1... Line 1...
1
<?php
1
<?php
2
# $Id: ldap.php 2532 2018-04-30 03:55:35Z tom.houdayer $
2
# $Id: ldap.php 2705 2019-03-05 22:30:50Z 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
*****************************************************************/
Line 61... Line 61...
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é";
-
 
67
	$l_ldap_ssl_text		= "Utiliser une connexion chiffré avec SSL (LDAPS)";
-
 
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";
-
 
70
	$l_ldap_cert_label		= "Certificat SSL (CA)";
-
 
71
	$l_ldap_cert_text		= "Certificat de l'authorité de certification signant celui du serveur LDAP";
-
 
72
	$l_ldap_cert_status_cur		= "Certificat actuel : ";
-
 
73
	$l_ldap_cert_status_no		= "Aucun certificat installé";
66
	$l_ldap_submit			= "Enregistrer";
74
	$l_ldap_submit			= "Enregistrer";
67
	$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).";
68
	$l_ldap_test_service_ok		= "Un port 389 est actif sur ce serveur";
76
	$l_ldap_test_service_ok		= "Un port 389 (636 avec SSL) est actif sur ce serveur";
69
	$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)";
70
	$l_ldap_test_connection_ok	= "Une connexion LDAP a été établie";
78
	$l_ldap_test_connection_ok	= "Une connexion LDAP a été établie";
71
	$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)";
72
	$l_ldap_test_bind_ok		= "L'authentification a réussie";
80
	$l_ldap_test_bind_ok		= "L'authentification a réussie";
73
	$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)";
74
	$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";
75
	$l_ldap_error			= "erreur LDAP";
83
	$l_ldap_error			= "erreur LDAP";
76
	$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";
77
	$l_check			= "Vérifier cette configuration";
86
	$l_check			= "Vérifier cette configuration";
78
	$l_checkingConf			= "Vérification de cette configuration...";
87
	$l_checkingConf			= "Vérification de cette configuration...";
79
} else {				// English
88
} else {				// English
80
	$l_ldap_update			= "LDAP settings updated";
89
	$l_ldap_update			= "LDAP settings updated";
81
	$l_ldap_title			= "External authentication : LDAP";
90
	$l_ldap_title			= "External authentication : LDAP";
Line 93... Line 102...
93
	$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))'";
94
	$l_ldap_user_label		= "CN of the user operated by ALCASAR:";
103
	$l_ldap_user_label		= "CN of the user operated by ALCASAR:";
95
	$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'";
96
	$l_ldap_password_label		= "Password:";
105
	$l_ldap_password_label		= "Password:";
97
	$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";
-
 
108
	$l_ldap_ssl_text		= "Use an encrypted connection with SSL (LDAPS)";
-
 
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";
-
 
111
	$l_ldap_cert_label		= "SSL certificate (CA)";
-
 
112
	$l_ldap_cert_text		= "Certificate of the certification authority that signed the LDAP server certificate";
-
 
113
	$l_ldap_cert_status_cur		= "Current certificate:";
-
 
114
	$l_ldap_cert_status_no		= "No certificate imported";
98
	$l_ldap_submit			= "Save";
115
	$l_ldap_submit			= "Save";
99
	$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)";
100
	$l_ldap_test_service_ok		= "A port 389 is open on this server";
117
	$l_ldap_test_service_ok		= "A port 389 (636 with SSL) is open on this server";
101
	$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)";
102
	$l_ldap_test_connection_ok	= "A LDAP connexion is established";
119
	$l_ldap_test_connection_ok	= "A LDAP connexion is established";
103
	$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)";
104
	$l_ldap_test_bind_ok		= "Successful authentication";
121
	$l_ldap_test_bind_ok		= "Successful authentication";
105
	$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)";
106
	$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";
107
	$l_ldap_error			= "LDAP error";
125
	$l_ldap_error			= "LDAP error";
108
	$l_ldap_entries			= "entries in the base";
126
	$l_ldap_entries			= "entries in the base";
109
	$l_check			= "Check this config";
127
	$l_check			= "Check this config";
110
	$l_checkingConf			= "Checking this configuration...";
128
	$l_checkingConf			= "Checking this configuration...";
111
}
129
}
112
 
130
 
113
 
-
 
114
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_uid, $f_ldap_port = 389) {
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) {
115
	// Socket to the LDAP port of the server
132
	// Socket to the LDAP port of the server
116
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
133
	if (!$sock = @fsockopen($f_ldap_server, (($f_ldap_ssl) ? 636 : 389), $num, $error, 2)) {
117
		// no network connection
134
		// no network connection
118
		return -2;
135
		return -2;
119
	}
136
	}
120
	fclose($sock);
137
	fclose($sock);
121
 
138
 
-
 
139
	// Set LDAP SSL options
-
 
140
	if ($f_ldap_ssl) {
-
 
141
		if ($f_ldap_cert_required) {
-
 
142
			if ($f_ldap_cert) {
-
 
143
				// ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, $f_ldap_cert);
-
 
144
			}
-
 
145
			ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND);
-
 
146
 
-
 
147
		} else {
-
 
148
			// ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
-
 
149
		}
-
 
150
	}
-
 
151
 
122
	// if ok, Test LDAP connection
152
	// if ok, Test LDAP connection
123
	$ldapconn = ldap_connect($f_ldap_server, $f_ldap_port);
153
	$ldapconn = @ldap_connect((($f_ldap_ssl)?'ldaps':'ldap').'://'.$f_ldap_server);
124
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
-
 
125
	if (!$ldapconn) {
154
	if (!$ldapconn) {
126
		// LDAP connection failed
155
		// LDAP connection failed
127
		return -1;
156
		return -1;
128
	}
157
	}
129
 
158
 
-
 
159
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
-
 
160
	// ldap_get_option($ldapconn, LDAP_OPT_X_KEEPALIVE_INTERVAL, $tmp); var_dump($tmp); ////////// TODO : opti keep-alive
-
 
161
 
130
	// if ok, test a ldap-bind with the user used by ALCASAR
162
	// if ok, test a ldap-bind with the user used by ALCASAR
131
	$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
163
	$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
132
	if (!$ldapbind) {
164
	if (!$ldapbind) {
133
		// Test LDAP Version 3
165
		// Test LDAP Version 3
134
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
166
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
135
		$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
167
		$ldapbind = @ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
136
		if (!$ldapbind) {
168
		if (!$ldapbind) {
137
			// LDAP Bind failed
169
			// LDAP Bind failed
138
			return 0;
170
			return 0;
139
		}
171
		}
140
	}
172
	}
141
 
173
 
142
	// if ok, try to query the directory of users
174
	// if ok, try to query the directory of users
143
	$query = $f_ldap_uid.'=*';
175
	$query = $f_ldap_uid.'=*';
144
	$ldap_result = ldap_search($ldapconn, $f_ldap_basedn, $query);
176
	$ldap_result = @ldap_search($ldapconn, $f_ldap_basedn, $query);
145
	if ($ldap_result) {
177
	if ($ldap_result) {
146
		$ldap_users_count = ldap_count_entries($ldapconn, $ldap_result);
178
		$ldap_users_count = ldap_count_entries($ldapconn, $ldap_result);
147
		return ($ldap_users_count + 2);
179
		return ($ldap_users_count + 2);
148
	} else {
180
	} else {
149
		return 1;
181
		return 1;
150
	}
182
	}
151
	ldap_unbind($ldapconn);
183
	ldap_unbind($ldapconn);
152
}
184
}
153
 
185
 
154
$messages = '';
186
$messages = '';
-
 
187
$LDAPS_CERT_LOC = '/etc/raddb/certs/alcasar-ldaps.crt';
155
 
188
 
156
if (isset($_POST['auth_enable'])) {
189
if (isset($_POST['auth_enable'])) {
157
	if ($_POST['auth_enable'] === '1') {
190
	if ($_POST['auth_enable'] === '1') {
-
 
191
		// TODO : need to translate messages
158
		$varErrors = [];
192
		$varErrors = [];
159
		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"');
160
		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"');
161
		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"');
162
		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"');
163
		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"');
164
		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"');
-
 
199
		if (isset($_POST['ldap_ssl']))           $ldap_ssl           = $_POST['ldap_ssl'];           //else array_push($varErrors, 'Variable error "ldap_ssl"');
-
 
200
		if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];         //else array_push($varErrors, 'Variable error "ldap_cert"');
-
 
201
		if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = $_POST['ldap_cert_required']; //else array_push($varErrors, 'Variable error "ldap_cert_required"');
165
 
202
 
166
		// Validation
203
		if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
167
		if (isset($ldap_server)) {
204
			if ($ldap_cert_required === 'on') {
-
 
205
				$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
168
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
206
				if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
-
 
207
					$cert_domainName = implode('.', array_reverse($matches[1]));
169
				$ldap_server = gethostbyname($ldap_server);
208
					if ($cert_domainName !== $ldap_server) {
-
 
209
						array_push($varErrors, str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn));
-
 
210
					}
170
			}
211
				}
171
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
-
 
172
				array_push($varErrors, 'Invalid LDAP server IP');
-
 
173
			}
212
			}
174
		}
213
		}
175
 
214
 
176
		if (!empty($varErrors)) { 
215
		if (!empty($varErrors)) { 
177
			foreach ($varErrors as $error) {
216
			foreach ($varErrors as $error) {
Line 182... Line 221...
182
			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);
183
			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);
184
			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);
185
			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);
186
			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);
-
 
226
			exec('sed -i '.escapeshellarg("s/^LDAP_SSL=.*/LDAP_SSL=$ldap_ssl/g").' '.CONF_FILE);
-
 
227
			exec('sed -i '.escapeshellarg("s/^LDAP_CERT_REQUIRED=.*/LDAP_CERT_REQUIRED=$ldap_cert_required/g").' '.CONF_FILE);
187
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
228
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
-
 
229
			if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
-
 
230
				exec('sudo /usr/local/bin/alcasar-ldap.sh --import-cert '.escapeshellarg($ldap_cert['tmp_name']));
-
 
231
			}
188
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
232
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
189
			$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>';
190
		}
234
		}
191
	} else {
235
	} else {
192
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
236
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
Line 208... Line 252...
208
	}
252
	}
209
	fclose($file_conf);
253
	fclose($file_conf);
210
}
254
}
211
 
255
 
212
// LDAP configuration params
256
// LDAP configuration params
213
$ldap_status      = ($conf['LDAP'] === 'on');
257
$ldap_status        = ($conf['LDAP'] === 'on');
214
$ldap_server      = $conf['LDAP_SERVER'];
258
$ldap_server        = $conf['LDAP_SERVER'];
215
$ldap_user        = $conf['LDAP_USER'];
259
$ldap_user          = $conf['LDAP_USER'];
216
$ldap_password    = $conf['LDAP_PASSWORD'];
260
$ldap_password      = $conf['LDAP_PASSWORD'];
217
$ldap_base_dn     = $conf['LDAP_BASE'];
261
$ldap_base_dn       = $conf['LDAP_BASE'];
218
$ldap_uid	  = $conf['LDAP_UID'];
262
$ldap_uid	    = $conf['LDAP_UID'];
219
$ldap_base_filter = $conf['LDAP_FILTER'];
263
$ldap_base_filter   = $conf['LDAP_FILTER'];
-
 
264
$ldap_ssl           = ($conf['LDAP_SSL'] === 'on');
-
 
265
$ldap_cert_required = ($conf['LDAP_CERT_REQUIRED'] === 'on');
-
 
266
 
-
 
267
$ldap_cert_subject = NULL;
-
 
268
if (file_exists($LDAPS_CERT_LOC)) {
-
 
269
	$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
-
 
270
	if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
-
 
271
		$ldap_cert_subject = implode('.', array_reverse($matches[1]));
-
 
272
	}
-
 
273
}
220
 
274
 
221
// AJAX LDAP configuration checker
275
// AJAX LDAP configuration checker
222
if (isset($_GET['conf_check'])) {
276
if (isset($_GET['conf_check'])) {
223
	$response = [
277
	$response = [
224
		'enable' => $ldap_status
278
		'enable' => $ldap_status
225
	];
279
	];
226
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
280
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
227
		$varErrors = [];
281
		$varErrors = [];
228
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
282
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
-
 
283
			// TODO : need to translate messages
229
			if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');		// TODO: need to translate
284
			if (isset($_POST['ldap_server']))        $ldap_server        = $_POST['ldap_server'];                   else array_push($varErrors, 'Variable error "ldap_server"');
230
			if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');		// TODO: need to translate
285
			if (isset($_POST['ldap_base_dn']))       $ldap_base_dn       = $_POST['ldap_base_dn'];                  else array_push($varErrors, 'Variable error "ldap_base_dn"');
231
			if (isset($_POST['ldap_uid']))         $ldap_uid         = $_POST['ldap_uid'];         else array_push($varErrors, 'Variable error "ldap_uid"');		// TODO: need to translate
286
			if (isset($_POST['ldap_uid']))           $ldap_uid           = $_POST['ldap_uid'];                      else array_push($varErrors, 'Variable error "ldap_uid"');
232
			if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');	// TODO: need to translate
287
			if (isset($_POST['ldap_base_filter']))   $ldap_base_filter   = $_POST['ldap_base_filter'];              else array_push($varErrors, 'Variable error "ldap_base_filter"');
233
			if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');		// TODO: need to translate
288
			if (isset($_POST['ldap_user']))          $ldap_user          = $_POST['ldap_user'];                     else array_push($varErrors, 'Variable error "ldap_user"');
234
			if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');		// TODO: need to translate
289
			if (isset($_POST['ldap_password']))      $ldap_password      = $_POST['ldap_password'];                 else array_push($varErrors, 'Variable error "ldap_password"');
-
 
290
			if (isset($_POST['ldap_ssl']))           $ldap_ssl           = ($_POST['ldap_ssl'] === 'on');           //else array_push($varErrors, 'Variable error "ldap_ssl"');
-
 
291
			if (isset($_FILES['ldap_cert']))         $ldap_cert          = $_FILES['ldap_cert'];                    //else array_push($varErrors, 'Variable error "ldap_cert"');
-
 
292
			if (isset($_POST['ldap_cert_required'])) $ldap_cert_required = ($_POST['ldap_cert_required'] === 'on'); //else array_push($varErrors, 'Variable error "ldap_cert_required"');
235
		}
293
		}
236
 
294
 
237
		// Validation
-
 
238
		if (isset($ldap_server)) {
-
 
239
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
295
		if (($ldap_cert_required) && isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
240
				$ldap_server = gethostbyname($ldap_server);
296
			$ldap_cert_tmpFilename = $ldap_cert['tmp_name'];
241
			}
297
		} else {
242
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
298
			$ldap_cert_tmpFilename = NULL;
243
				array_push($varErrors, 'Invalid LDAP server IP');	// TODO: need to translate
-
 
244
			}
-
 
245
		}
299
		}
246
 
300
 
247
		if (!empty($varErrors)) {
301
		if (!empty($varErrors)) {
248
			$response['errors'] = $varErrors;
302
			$response['errors'] = $varErrors;
249
		} else {
303
		} else {
250
			$response['result'] = ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_uid);
304
			$result = ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
-
 
305
 
-
 
306
			if (($result === 0) && ($ldap_ssl && $ldap_cert_required && ((isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) || (file_exists($LDAPS_CERT_LOC))))) {
-
 
307
				if (filter_var($ldap_server, FILTER_VALIDATE_IP) !== false) {
-
 
308
					if (isset($ldap_cert) && ($ldap_cert['error'] === UPLOAD_ERR_OK)) {
-
 
309
						$certificateInfos = openssl_x509_parse(file_get_contents($ldap_cert['tmp_name']));
-
 
310
					} else {
-
 
311
						$certificateInfos = openssl_x509_parse(file_get_contents($LDAPS_CERT_LOC));
-
 
312
					}
-
 
313
					if (preg_match_all('@/[a-zA-Z]+=([^/]+)@', $certificateInfos['name'], $matches)) {
-
 
314
						$cert_domainName = implode('.', array_reverse($matches[1]));
-
 
315
						if ($cert_domainName !== $ldap_server) {
-
 
316
							$response['warnings'][] = str_replace("§cert_domainName§", $cert_domainName, $l_ldap_cert_cn_diff_dn);
-
 
317
						}
-
 
318
					}
-
 
319
					$res = ldap_checkServerConfig($cert_domainName, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_uid, $ldap_ssl, $ldap_cert_tmpFilename, $ldap_cert_required);
-
 
320
					if ($res > 1) {
-
 
321
						$result = $res;
-
 
322
					}
-
 
323
					$result = max($result, $res);
-
 
324
				}
-
 
325
			}
-
 
326
 
-
 
327
			$response['result'] = $result;
251
		}
328
		}
252
	}
329
	}
253
 
330
 
254
	header('Content-Type: application/json');
331
	header('Content-Type: application/json');
255
	echo json_encode($response);
332
	echo json_encode($response);
Line 265... Line 342...
265
	<link type="text/css" href="/css/style.css" rel="stylesheet">
342
	<link type="text/css" href="/css/style.css" rel="stylesheet">
266
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
343
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
267
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
344
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
268
	<script>
345
	<script>
269
	function onLdapStatusChange() {
346
	function onLdapStatusChange() {
270
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_uid', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
347
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_uid', 'ldap_base_filter', 'ldap_user', 'ldap_password', 'ldap_ssl', 'ldap_cert_required', 'ldap_cert'];
271
		var formSubmit = document.querySelector('form input[type="submit"]');
348
		var formSubmit = document.querySelector('form input[type="submit"]');
272
		var btn_checkConf = document.getElementById('btn-checkconf');
349
		var btn_checkConf = document.getElementById('btn-checkconf');
273
		var isChecked = false;
350
		var isChecked = false;
274
 
351
 
275
		if (document.getElementById('auth_enable').value === '1') {
352
		if (document.getElementById('auth_enable').value === '1') {
276
			for (var i=0; i<listToDisables.length; i++) {
353
			for (var i=0; i<listToDisables.length; i++) {
277
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
354
				document.getElementById(listToDisables[i]).style.backgroundColor = null;
278
				document.getElementById(listToDisables[i]).disabled = false;
355
				document.getElementById(listToDisables[i]).disabled = false;
279
			}
356
			}
-
 
357
			if (document.getElementById('ldap_ssl').value === 'off') {
-
 
358
				document.getElementById('ldap_cert_required').style.backgroundColor = '#c0c0c0';
-
 
359
				document.getElementById('ldap_cert_required').disabled = true;
-
 
360
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
-
 
361
				document.getElementById('ldap_cert').disabled = true;
-
 
362
			}
-
 
363
			else if (document.getElementById('ldap_cert_required').value === 'off') {
-
 
364
				document.getElementById('ldap_cert').style.backgroundColor = '#c0c0c0';
-
 
365
				document.getElementById('ldap_cert').disabled = true;
-
 
366
			}
280
			formSubmit.style.display = 'none';
367
			formSubmit.style.display = 'none';
281
			btn_checkConf.style.display = null;
368
			btn_checkConf.style.display = null;
-
 
369
			<?php if (PHP_VERSION_ID < 70100): ?>
-
 
370
			// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
-
 
371
			formSubmit.style.display = null;
-
 
372
			<?php endif; ?>
282
		} else {
373
		} else {
283
			for (var i=0; i<listToDisables.length; i++) {
374
			for (var i=0; i<listToDisables.length; i++) {
284
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
375
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
285
				document.getElementById(listToDisables[i]).disabled = true;
376
				document.getElementById(listToDisables[i]).disabled = true;
286
			}
377
			}
Line 291... Line 382...
291
 
382
 
292
	function checkConfig() {
383
	function checkConfig() {
293
		var messagesElem = document.querySelector('fieldset > legend > div');
384
		var messagesElem = document.querySelector('fieldset > legend > div');
294
		var formSubmit   = document.querySelector('form input[type="submit"]');
385
		var formSubmit   = document.querySelector('form input[type="submit"]');
295
		var btn_checkConf = document.getElementById('btn-checkconf');
386
		var btn_checkConf = document.getElementById('btn-checkconf');
296
 
-
 
297
		var ldap_config = {
-
 
298
			ldap_status:     (document.getElementById('auth_enable').value === '1'),
-
 
299
			ldap_server:      document.getElementById('ldap_server').value,
-
 
300
			ldap_user:        document.getElementById('ldap_user').value,
387
		var form = document.getElementById('form-config_ldap');
301
			ldap_password:    document.getElementById('ldap_password').value,
-
 
302
			ldap_base_dn:     document.getElementById('ldap_dn').value,
388
		var post_data = new FormData(form);
303
			ldap_uid:         document.getElementById('ldap_uid').value,
-
 
304
			ldap_base_filter: document.getElementById('ldap_base_filter').value
-
 
305
		};
-
 
306
 
-
 
307
		// Format HTTP POST data
389
		post_data.delete('auth_enable');
308
		var post_data = Object.keys(ldap_config).map( function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(ldap_config[k]) } ).join('&');
-
 
309
 
390
 
310
		messagesElem.innerHTML = '<?= $l_checkingConf ?>';
391
		messagesElem.innerHTML = '<?= $l_checkingConf ?>';
311
 
392
 
312
		var xhr = new XMLHttpRequest();
393
		var xhr = new XMLHttpRequest();
313
		xhr.onreadystatechange = function() {
394
		xhr.onreadystatechange = function() {
Line 345... Line 426...
345
						btn_checkConf.style.display = 'none';
426
						btn_checkConf.style.display = 'none';
346
					} else {
427
					} else {
347
						formSubmit.style.display = 'none';
428
						formSubmit.style.display = 'none';
348
						btn_checkConf.style.display = null;
429
						btn_checkConf.style.display = null;
349
					}
430
					}
-
 
431
					<?php if (PHP_VERSION_ID < 70100): ?>
-
 
432
					// Compatibility with PHP < 7.1.0 # TODO : remove if ever Mageia 7 is released
-
 
433
					formSubmit.style.display = null;
-
 
434
					<?php endif; ?>
350
 
435
 
351
					if (typeof data.errors !== 'undefined') {
436
					if (typeof data.errors !== 'undefined') {
352
						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>';
353
					}
438
					}
-
 
439
					if (typeof data.warnings !== 'undefined') {
-
 
440
						messages += '<br><span style=\"color: orange\">' + data.warnings.join('</span><br><span style=\"color: orange\">') + '</span><br>';
-
 
441
					}
354
 
442
 
355
					messagesElem.innerHTML = messages;
443
					messagesElem.innerHTML = messages;
356
				} else {
444
				} else {
357
					messagesElem.innerHTML = 'server error';
445
					messagesElem.innerHTML = 'server error';
358
				}
446
				}
359
			}
447
			}
360
		};
448
		};
361
		xhr.open('POST', 'ldap.php?conf_check', true);
449
		xhr.open('POST', 'ldap.php?conf_check', true);
362
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-
 
363
		xhr.send(post_data);
450
		xhr.send(post_data);
364
	}
451
	}
365
	</script>
452
	</script>
366
</head>
453
</head>
367
<body onLoad="onLdapStatusChange();">
454
<body onLoad="onLdapStatusChange();">
368
	<div class="panel">
455
	<div class="panel">
369
		<div class="panel-header"><?= $l_ldap_legend ?></div>
456
		<div class="panel-header"><?= $l_ldap_legend ?></div>
370
		<div class="panel-body">
457
		<div class="panel-body">
371
			<form name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
458
			<form id="form-config_ldap" name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data">
372
				<fieldset>
459
				<fieldset>
373
					<legend>
460
					<legend>
374
						<br>
461
						<br>
375
						<div style="text-align: center">
462
						<div style="text-align: center">
376
							<?php if ($messages): ?>
463
							<?php if ($messages): ?>
Line 438... Line 525...
438
						<dt>
525
						<dt>
439
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
526
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
440
							<?= $l_ldap_password_text ?>
527
							<?= $l_ldap_password_text ?>
441
						</dt>
528
						</dt>
442
						<dd>
529
						<dd>
443
							<input type="text" id="ldap_password" type="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();">
-
 
531
						</dd>
-
 
532
					</dl>
-
 
533
					<dl>
-
 
534
						<dt>
-
 
535
							<label for="ldap_ssl"><?= $l_ldap_ssl_label ?></label><br>
-
 
536
							<?= $l_ldap_ssl_text ?><br>
-
 
537
						</dt>
-
 
538
						<dd>
-
 
539
							<select id="ldap_ssl" name="ldap_ssl" onchange="onLdapStatusChange();">
-
 
540
								<option value="on"<?= ($ldap_ssl)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
-
 
541
								<option value="off"<?= (!$ldap_ssl) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
-
 
542
							</select>
-
 
543
						</dd>
-
 
544
					</dl>
-
 
545
					<dl>
-
 
546
						<dt>
-
 
547
							<label for="ldap_cert_required"><?= $l_ldap_cert_required_label ?></label><br>
-
 
548
							<?= $l_ldap_cert_required_text ?><br>
-
 
549
						</dt>
-
 
550
						<dd>
-
 
551
							<select id="ldap_cert_required" name="ldap_cert_required" onchange="onLdapStatusChange();">
-
 
552
								<option value="on"<?= ($ldap_cert_required)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
-
 
553
								<option value="off"<?= (!$ldap_cert_required) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
-
 
554
							</select>
-
 
555
						</dd>
-
 
556
					</dl>
-
 
557
					<dl>
-
 
558
						<dt>
-
 
559
							<label for="ldap_cert"><?= $l_ldap_cert_label ?></label><br>
-
 
560
							<?= $l_ldap_cert_text ?><br>
-
 
561
							<?= (($ldap_cert_subject) ? "$l_ldap_cert_status_cur $ldap_cert_subject" : $l_ldap_cert_status_no ) ?>
-
 
562
						</dt>
-
 
563
						<dd>
-
 
564
							<input type="file" id="ldap_cert" name="ldap_cert" oninput="onLdapStatusChange();">
444
						</dd>
565
						</dd>
445
					</dl>
566
					</dl>
446
					<p>
567
					<p>
447
						<button id="btn-checkconf" onclick="checkConfig(); return false;"><?= $l_check ?></button>
568
						<button id="btn-checkconf" onclick="checkConfig(); return false;"><?= $l_check ?></button>
448
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
569
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">