Rev 2449 | Rev 2452 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log
<?php
# $Id: ldap.php 2451 2017-12-05 22:33:27Z tom.houdayer $
/* written by steweb57 & Rexy */
/****************************************************************
* GLOBAL FILE PATHS *
*****************************************************************/
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
/****************************************************************
* FILE reading test *
*****************************************************************/
$conf_files = array(CONF_FILE);
foreach ($conf_files as $file) {
if (!file_exists($file)) {
exit("Fichier $file non présent");
}
if (!is_readable($file)) {
exit("Vous n'avez pas les droits de lecture sur le fichier $file");
}
}
/****************************************************************
* Read CONF_FILE *
*****************************************************************/
$file_conf = fopen(CONF_FILE, 'r');
if (!$file_conf) {
exit('Error opening the file '.CONF_FILE);
}
while (!feof($file_conf)) {
$buffer = fgets($file_conf, 4096);
if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
$tmp = explode('=', $buffer, 2);
$conf[trim($tmp[0])] = trim($tmp[1]);
}
}
fclose($file_conf);
/****************************************************************
* Choice of language *
*****************************************************************/
$Language = 'en';
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$Langue = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$Language = strtolower(substr(chop($Langue[0]), 0, 2));
}
if ($Language === 'fr') { // French
$l_ldap_update_sucess = "Mise à jour des paramètres LDAP réalisée avec succès";
$l_ldap_title = "Authentification externe : LDAP";
$l_ldap_legend = "Authentification LDAP";
$l_ldap_auth_enable_label = "Activer l'authentification LDAP:";
$l_ldap_YES = "OUI";
$l_ldap_NO = "NON";
$l_ldap_server_label = "Serveur LDAP:";
$l_ldap_server_text = "Adresse IP du serveur LDAP";
$l_ldap_base_dn_label = "DN de la base LDAP:";
$l_ldap_base_dn_text = "DN est le 'Distinguished Name', il définit où se situent les informations des utilisateurs dans l'annuaire. Exemple LDAP: 'o=mycompany, c=FR'. Exemple AD 'ou=my_lan,dc=server_name,dc=localdomain'";
$l_ldap_filter_label = "Identifiant LDAP:";
$l_ldap_filter_text = "Clé utilisée lors de la recherche d'un identifiant de connexion, exemple: 'uid', 'sn', etc. Pour un AD mettre 'sAMAccountName'.";
$l_ldap_base_filter_label = "Filtre de recherche d'utilisateurs LDAP:";
$l_ldap_base_filter_text = "En option, vous pouvez limiter les objets recherchés avec des filtres additionnels. Par exemple 'objectClass=posixGroup' aurait comme conséquence l'utilisation de '(&(uid=username)(objectClass=posixGroup))'";
$l_ldap_user_label = "Utilisateur LDAP:";
$l_ldap_user_text = "Nom d'utilisateur utilisé par ALCASAR pour se connecter au serveur LDAP. Laissez vide pour utiliser un accès invité (ou anomyme). Obligatoire sur un AD. exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'. Exemple AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
$l_ldap_password_label = "Mot de passe LDAP:";
$l_ldap_password_text = "Laissez vide pour un accès invité (ou anonyme). Obligatoire sur un AD.";
$l_ldap_submit = "Enregistrer";
$l_ldap_reset = "Annuler";
$l_ldap_test_network_failed = "Pas de connectivité réseau avec le serveur LDAP.";
$l_ldap_test_connection_failed = "Impossible de se connecter au serveur LDAP.";
$l_ldap_test_bind_ok = "Connexion LDAP réussie...";
$l_ldap_test_bind_failed = "Echec d'authentification sur le serveur LDAP... Vérifiez votre configuration";
$l_ldap_test_dn_ok = "DN semble bon";
$l_ldap_test_dn_failed = "DN semble mauvais";
$l_ldap_error = "erreur LDAP";
} else { // English
$l_ldap_update_sucess = "Successfull LDAP settings update";
$l_ldap_title = "External authentication : LDAP";
$l_ldap_legend = "LDAP authentication";
$l_ldap_auth_enable_label = "Use LDAP authentication :";
$l_ldap_YES = "YES";
$l_ldap_NO = "NO";
$l_ldap_server_label = "LDAP server :";
$l_ldap_server_text = "This is the IP address of the LDAP server.";
$l_ldap_base_dn_label = "LDAP base dn:";
$l_ldap_base_dn_text = "This is the 'Distinguished Name', locating the user information in the directory. e.g. LDAP : 'o=MyCompany,c=US'. e.g. AD : 'ou=my_lan,dc=server_name,dc=localdomain'";
$l_ldap_filter_label = "LDAP uid:";
$l_ldap_filter_text = "This is the key used to search for a given login identity, e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
$l_ldap_base_filter_label = "LDAP user filter:";
$l_ldap_base_filter_text = "Optionally you can further limit the searched objects with additional filters. For example 'objectClass=posixGroup' would result in the use of '(&(uid=username)(objectClass=posixGroup))'";
$l_ldap_user_label = "LDAP user dn:";
$l_ldap_user_text = "Username used by ALCASAR to connect to the LDAP server. Leave blank to use anonymous binding. Required for AD. e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'. e.g. AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
$l_ldap_password_label = "LDAP password:";
$l_ldap_password_text = "Leave blank to use anonymous binding. Required for AD.";
$l_ldap_submit = "Save";
$l_ldap_reset = "Reset";
$l_ldap_test_network_failed = "LDAP server is not reachable.";
$l_ldap_test_connection_failed = "LDAP connexion failed...";
$l_ldap_test_bind_ok = "LDAP connexion success...";
$l_ldap_test_bind_failed = "LDAP authentication failed...Check your ldap setup...";
$l_ldap_test_dn_ok = "DN seems to be right";
$l_ldap_test_dn_failed = "DN seems to be wrong";
$l_ldap_error = "LDAP error";
}
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_filter, $f_ldap_port = 389) {
// Test connect to the LDAP server
if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
// no network connection
return -1;
}
fclose($sock);
// Test connect to the LDAP server
$ldapconn = ldap_connect($f_ldap_server, $f_ldap_port);
ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
if (!$ldapconn) {
// LDAP connection failed
return -2;
}
$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
if (!$ldapbind) {
// Test LDAP Version 3
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
if (!$ldapbind) {
// LDAP Bind failed
return 0;
}
}
ldap_unbind($ldapconn);
// try search
$query = $f_ldap_filter.'=*';
if (ldap_search($ldapconn, $f_ldap_basedn, $query)) {
return 2;
} else {
return 1;
}
}
$messages = '';
if (isset($_POST['auth_enable'])) {
if ($_POST['auth_enable'] === '1') {
$varErrors = [];
if (isset($_POST['ldap_server'])) $ldap_server = $_POST['ldap_server']; else array_push($varErrors, 'Variable error "ldap_server"');
if (isset($_POST['ldap_base_dn'])) $ldap_base_dn = $_POST['ldap_base_dn']; else array_push($varErrors, 'Variable error "ldap_base_dn"');
if (isset($_POST['ldap_filter'])) $ldap_filter = $_POST['ldap_filter']; else array_push($varErrors, 'Variable error "ldap_filter"');
if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
if (isset($_POST['ldap_user'])) $ldap_user = $_POST['ldap_user']; else array_push($varErrors, 'Variable error "ldap_user"');
if (isset($_POST['ldap_password'])) $ldap_password = $_POST['ldap_password']; else array_push($varErrors, 'Variable error "ldap_password"');
// Validation
if (isset($ldap_server)) {
if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
$ldap_server = gethostbyname($ldap_server);
}
if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
array_push($varErrors, 'Invalid LDAP server IP');
}
}
if (!empty($varErrors)) {
$messages .= '<div style="text-align: center"><br>';
foreach ($varErrors as $error) {
$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
}
$messages .= '<br></div>';
} else {
exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_filter/g").' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_filter/g").' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
$messages .= '<div style="text-align: center"><br>';
$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
$messages .= '<br></div>';
}
} else {
exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
$messages .= '<div style="text-align: center"><br>';
$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
$messages .= '<br></div>';
}
// Reload configuration
$file_conf = fopen(CONF_FILE, 'r');
if (!$file_conf) {
exit('Error opening the file '.CONF_FILE);
}
while (!feof($file_conf)) {
$buffer = fgets($file_conf, 4096);
if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
$tmp = explode('=', $buffer, 2);
$conf[trim($tmp[0])] = trim($tmp[1]);
}
}
fclose($file_conf);
}
// LDAP configuration params
$ldap_status = ($conf['LDAP'] === 'on');
$ldap_server = $conf['LDAP_SERVER'];
$ldap_identity = $conf['LDAP_USER'];
$ldap_password = $conf['LDAP_PASSWORD'];
$ldap_basedn = $conf['LDAP_BASE'];
$ldap_filter = $conf['LDAP_UID'];
$ldap_base_filter = $conf['LDAP_FILTER'];
// TODO : check LDAP PHP extension loaded?
// if (!extension_loaded('ldap')) {
// exit();
// }
$pos = strpos($ldap_server, '//');
if ($pos !== false) {
// TODO : useless?
$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"
} else {
$new_ldap_server = $ldap_server;
}
if ($ldap_status) {
$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_identity, $ldap_password, $ldap_basedn, $ldap_filter);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $l_ldap_title ?></title>
<link type="text/css" href="/css/style.css" rel="stylesheet">
<link type="text/css" href="/css/acc.css" rel="stylesheet">
<link type="text/css" href="/css/ldap.css" rel="stylesheet">
<script>
function onLdapStatusChange() {
var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
if (document.getElementById("auth_enable").value === '1') {
for (var i=0; i<listToDisables.length; i++) {
document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
document.getElementById(listToDisables[i]).disabled = false;
}
} else {
for (var i=0; i<listToDisables.length; i++) {
document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
document.getElementById(listToDisables[i]).disabled = true;
}
}
}
</script>
</head>
<body onLoad="onLdapStatusChange();">
<div class="panel">
<div class="panel-header"><?= $l_ldap_legend ?></div>
<div class="panel-body">
<form name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<fieldset>
<legend>
<?= $messages ?>
<?php if ($ldap_status): ?>
<div style="text-align: center"><br>
<?php if ($serverCheckResult === -2): ?>
<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
<?php elseif ($serverCheckResult === -1): ?>
<span style="color: red"><?= $l_ldap_test_network_failed ?></span>
<?php elseif ($serverCheckResult === 0): ?>
<span style="color: red"><?= $l_ldap_test_bind_failed ?></span>
<?php elseif ($serverCheckResult === 1): ?>
<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
<br>";
<span style="color: red"><?= $l_ldap_test_dn_failed ?></span>
<?php elseif ($serverCheckResult === 2): ?>
<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
<br>";
<span style="color: green"><?= $l_ldap_test_dn_ok ?></span>
<?php else: ?>
<span><?= $l_ldap_error ?></span>
<?php endif ?>
<br><br>
</div>
<?php endif ?>
</legend>
<dl>
<dt>
<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
</dt>
<dd>
<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
<option value="1"<?= ($ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
</select>
</dd>
</dl>
<dl>
<dt>
<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
<?= $l_ldap_server_text ?>
</dt>
<dd>
<input id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>">
</dd>
</dl>
<dl>
<dt>
<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
<?= $l_ldap_base_dn_text ?>
</dt>
<dd>
<input id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_basedn) ?>">
</dd>
</dl>
<dl>
<dt>
<label for="ldap_filter"><?= $l_ldap_filter_label ?></label><br>
<?= $l_ldap_filter_text ?>
</dt>
<dd>
<input id="ldap_filter" size="40" name="ldap_filter" value="<?= htmlspecialchars($ldap_filter) ?>">
</dd>
</dl>
<dl>
<dt>
<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
<?= $l_ldap_base_filter_text ?>
</dt>
<dd>
<input id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>">
</dd>
</dl>
<dl>
<dt>
<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
<?= $l_ldap_user_text ?>
</dt>
<dd>
<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_identity) ?>">
</dd>
</dl>
<dl>
<dt>
<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
<?= $l_ldap_password_text ?>
</dt>
<dd>
<input id="ldap_password" type="password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>">
</dd>
</dl>
<p>
<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
</p>
</fieldset>
</form>
</div>
</div>
</body>
</html>