Subversion Repositories ALCASAR

Rev

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

Rev 2452 Rev 2453
Line 1... Line 1...
1
<?php
1
<?php
2
# $Id: ldap.php 2452 2017-12-05 22:51:01Z tom.houdayer $
2
# $Id: ldap.php 2453 2017-12-06 00:06:41Z tom.houdayer $
3
 
3
 
4
/* written by steweb57 & Rexy */
4
/* written by steweb57, Rexy & Tom HOUDAYER */
5
/****************************************************************
5
/****************************************************************
6
*			GLOBAL FILE PATHS			*
6
*			GLOBAL FILE PATHS			*
7
*****************************************************************/
7
*****************************************************************/
8
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
8
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
9
 
9
 
Line 208... Line 208...
208
}
208
}
209
 
209
 
210
// LDAP configuration params
210
// LDAP configuration params
211
$ldap_status      = ($conf['LDAP'] === 'on');
211
$ldap_status      = ($conf['LDAP'] === 'on');
212
$ldap_server      = $conf['LDAP_SERVER'];
212
$ldap_server      = $conf['LDAP_SERVER'];
213
$ldap_identity    = $conf['LDAP_USER'];
213
$ldap_user        = $conf['LDAP_USER'];
214
$ldap_password    = $conf['LDAP_PASSWORD'];
214
$ldap_password    = $conf['LDAP_PASSWORD'];
215
$ldap_base_dn     = $conf['LDAP_BASE'];
215
$ldap_base_dn     = $conf['LDAP_BASE'];
216
$ldap_filter      = $conf['LDAP_UID'];
216
$ldap_filter      = $conf['LDAP_UID'];
217
$ldap_base_filter = $conf['LDAP_FILTER'];
217
$ldap_base_filter = $conf['LDAP_FILTER'];
218
 
218
 
Line 227... Line 227...
227
	$new_ldap_server = explode('//', $ldap_server); // pour discriminer le host et le protocole dans la notation "ldap://192.168.182.10" ou "ldaps://monldap.monentreperise.com"
227
	$new_ldap_server = explode('//', $ldap_server); // pour discriminer le host et le protocole dans la notation "ldap://192.168.182.10" ou "ldaps://monldap.monentreperise.com"
228
} else {
228
} else {
229
	$new_ldap_server = $ldap_server;
229
	$new_ldap_server = $ldap_server;
230
}
230
}
231
if ($ldap_status) {
231
if ($ldap_status) {
232
	$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_identity, $ldap_password, $ldap_base_dn, $ldap_filter);
232
	$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_filter);
-
 
233
}
-
 
234
 
-
 
235
// AJAX LDAP configuration checker
-
 
236
if (isset($_GET['conf_check'])) {
-
 
237
	$response = [
-
 
238
		'enable' => $ldap_status
-
 
239
	];
-
 
240
	if ($ldap_status || ($_SERVER['REQUEST_METHOD'] === 'POST')) {
-
 
241
		$varErrors = [];
-
 
242
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
-
 
243
			if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');
-
 
244
			if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');
-
 
245
			if (isset($_POST['ldap_filter']))      $ldap_filter      = $_POST['ldap_filter'];      else array_push($varErrors, 'Variable error "ldap_filter"');
-
 
246
			if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
-
 
247
			if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');
-
 
248
			if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');
-
 
249
		}
-
 
250
 
-
 
251
		// Validation
-
 
252
		if (isset($ldap_server)) {
-
 
253
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
-
 
254
				$ldap_server = gethostbyname($ldap_server);
-
 
255
			}
-
 
256
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
-
 
257
				array_push($varErrors, 'Invalid LDAP server IP');
-
 
258
			}
-
 
259
		}
-
 
260
 
-
 
261
		$response['result'] = (ldap_checkServerConfig($ldap_server, $ldap_user, $ldap_password, $ldap_base_dn, $ldap_filter) > 0);
-
 
262
 
-
 
263
		if (!empty($varErrors)) {
-
 
264
			$response['errors'] = $varErrors;
-
 
265
		}
-
 
266
	}
-
 
267
 
-
 
268
	header('Content-Type: application/json');
-
 
269
	echo json_encode($response);
-
 
270
	exit();
233
}
271
}
234
 
272
 
235
?>
273
?>
236
<!DOCTYPE html>
274
<!DOCTYPE html>
237
<html>
275
<html>
Line 243... Line 281...
243
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
281
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
244
	<script>
282
	<script>
245
	function onLdapStatusChange() {
283
	function onLdapStatusChange() {
246
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
284
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
247
 
285
 
248
		if (document.getElementById("auth_enable").value === '1') {
286
		if (document.getElementById('auth_enable').value === '1') {
249
			for (var i=0; i<listToDisables.length; i++) {
287
			for (var i=0; i<listToDisables.length; i++) {
250
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
288
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
251
				document.getElementById(listToDisables[i]).disabled = false;
289
				document.getElementById(listToDisables[i]).disabled = false;
252
			}
290
			}
253
		} else {
291
		} else {
Line 255... Line 293...
255
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
293
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
256
				document.getElementById(listToDisables[i]).disabled = true;
294
				document.getElementById(listToDisables[i]).disabled = true;
257
			}
295
			}
258
		}
296
		}
259
	}
297
	}
-
 
298
 
-
 
299
	function checkConfig() {
-
 
300
		var ldap_config = {
-
 
301
			ldap_status:     (document.getElementById('auth_enable').value === '1'),
-
 
302
			ldap_server:      document.getElementById('ldap_server').value,
-
 
303
			ldap_user:        document.getElementById('ldap_user').value,
-
 
304
			ldap_password:    document.getElementById('ldap_password').value,
-
 
305
			ldap_base_dn:     document.getElementById('ldap_dn').value,
-
 
306
			ldap_filter:      document.getElementById('ldap_filter').value,
-
 
307
			ldap_base_filter: document.getElementById('ldap_base_filter').value
-
 
308
		};
-
 
309
 
-
 
310
		console.log(ldap_config);
-
 
311
 
-
 
312
		var params = Object.keys(ldap_config).map( function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(ldap_config[k]) } ).join('&');
-
 
313
 
-
 
314
		var xhr = new XMLHttpRequest();
-
 
315
		xhr.onreadystatechange = function() {
-
 
316
			if (this.readyState == 4) {
-
 
317
				if (this.status == 200) {
-
 
318
					var data = JSON.parse(this.responseText);
-
 
319
 
-
 
320
					if (data.result === true) {
-
 
321
						alert('Configuration valide');
-
 
322
					} else {
-
 
323
						alert('Configuration non valide');
-
 
324
					}
-
 
325
 
-
 
326
					if (typeof data.errors !== 'undefined') {
-
 
327
						alert(data.errors.join("\n"));
-
 
328
					}
-
 
329
 
-
 
330
					console.log(data)
-
 
331
				} else {
-
 
332
					console.log(this.responseText)
-
 
333
				}
-
 
334
			}
-
 
335
		};
-
 
336
		xhr.open('POST', 'ldap.php?conf_check', true);
-
 
337
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-
 
338
		xhr.send(params);
-
 
339
	}
260
	</script>
340
	</script>
261
</head>
341
</head>
262
<body onLoad="onLdapStatusChange();">
342
<body onLoad="onLdapStatusChange();">
263
	<div class="panel">
343
	<div class="panel">
264
		<div class="panel-header"><?= $l_ldap_legend ?></div>
344
		<div class="panel-header"><?= $l_ldap_legend ?></div>
Line 341... Line 421...
341
						<dt>
421
						<dt>
342
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
422
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
343
							<?= $l_ldap_user_text ?>
423
							<?= $l_ldap_user_text ?>
344
						</dt>
424
						</dt>
345
						<dd>
425
						<dd>
346
							<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_identity) ?>">
426
							<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_user) ?>">
347
						</dd>
427
						</dd>
348
					</dl>
428
					</dl>
349
					<dl>
429
					<dl>
350
						<dt>
430
						<dt>
351
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
431
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
Line 356... Line 436...
356
						</dd>
436
						</dd>
357
					</dl>
437
					</dl>
358
					<p>
438
					<p>
359
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
439
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
360
						<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
440
						<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
-
 
441
						<button onclick="checkConfig(); return false;">Check [BETA]</button>
361
					</p>
442
					</p>
362
				</fieldset>
443
				</fieldset>
363
			</form>
444
			</form>
364
		</div>
445
		</div>
365
	</div>
446
	</div>