Subversion Repositories ALCASAR

Rev

Rev 2446 | Rev 2451 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
318 richard 1
<?php
2449 tom.houday 2
# $Id: ldap.php 2449 2017-12-05 21:42:08Z tom.houdayer $
3
 
2446 richard 4
/* written by steweb57 & Rexy */
318 richard 5
/****************************************************************
2449 tom.houday 6
*			GLOBAL FILE PATHS			*
318 richard 7
*****************************************************************/
2449 tom.houday 8
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
318 richard 9
 
2449 tom.houday 10
/****************************************************************
11
*			FILE reading test			*
12
*****************************************************************/
13
$conf_files = array(CONF_FILE);
14
foreach ($conf_files as $file) {
15
	if (!file_exists($file)) {
16
		exit("Fichier $file non présent");
17
	}
18
	if (!is_readable($file)) {
19
		exit("Vous n'avez pas les droits de lecture sur le fichier $file");
20
	}
21
}
318 richard 22
 
1160 stephane 23
/****************************************************************
2449 tom.houday 24
*			Read CONF_FILE				*
25
*****************************************************************/
26
$file_conf = fopen(CONF_FILE, 'r');
27
if (!$file_conf) {
28
	exit('Error opening the file '.CONF_FILE);
29
}
30
while (!feof($file_conf)) {
31
	$buffer = fgets($file_conf, 4096);
32
	if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
33
		$tmp = explode('=', $buffer, 2);
34
		$conf[trim($tmp[0])] = trim($tmp[1]);
35
	}
36
}
37
fclose($file_conf);
38
 
39
/****************************************************************
1672 richard 40
*			Choice of language			*
1160 stephane 41
*****************************************************************/
42
$Language = 'en';
2449 tom.houday 43
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
44
	$Langue	  = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
45
	$Language = strtolower(substr(chop($Langue[0]), 0, 2));
46
}
47
if ($Language === 'fr') {		// French
1160 stephane 48
	$l_ldap_update_sucess		= "Mise à jour des paramètres LDAP réalisée avec succès";
1672 richard 49
	$l_ldap_title			= "Authentification externe : LDAP";
50
	$l_ldap_legend			= "Authentification LDAP";
1160 stephane 51
	$l_ldap_auth_enable_label	= "Activer l'authentification LDAP:";
1672 richard 52
	$l_ldap_YES			= "OUI";
53
	$l_ldap_NO			= "NON";
1677 richard 54
	$l_ldap_server_label		= "Serveur LDAP:";
2306 tom.houday 55
	$l_ldap_server_text		= "Adresse IP du serveur LDAP";
1160 stephane 56
	$l_ldap_base_dn_label		= "DN de la base LDAP:";
1673 richard 57
	$l_ldap_base_dn_text		= "DN est le 'Distinguished Name', il définit où se situent les informations des utilisateurs dans l'annuaire. Exemple LDAP: 'o=mycompany, c=FR'. Exemple AD 'ou=my_lan,dc=server_name,dc=localdomain'";
1160 stephane 58
	$l_ldap_filter_label		= "Identifiant LDAP:";
1673 richard 59
	$l_ldap_filter_text		= "Clé utilisée lors de la recherche d'un identifiant de connexion, exemple: 'uid', 'sn', etc. Pour un AD mettre 'sAMAccountName'.";
60
	$l_ldap_base_filter_label	= "Filtre de recherche d'utilisateurs LDAP:";
61
	$l_ldap_base_filter_text	= "En option, vous pouvez limiter les objets recherchés avec des filtres additionnels. Par exemple 'objectClass=posixGroup' aurait comme conséquence l'utilisation de '(&amp;(uid=username)(objectClass=posixGroup))'";
1672 richard 62
	$l_ldap_user_label		= "Utilisateur LDAP:";
1673 richard 63
	$l_ldap_user_text		= "Nom d'utilisateur utilisé par ALCASAR pour se connecter au serveur LDAP. Laissez vide pour utiliser un accès invité (ou anomyme). Obligatoire sur un AD. exemple LDAP : 'uid=username,ou=my_lan,o=mycompany,c=FR'. Exemple AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
1160 stephane 64
	$l_ldap_password_label		= "Mot de passe LDAP:";
1673 richard 65
	$l_ldap_password_text		= "Laissez vide pour un accès invité (ou anonyme). Obligatoire sur un AD.";
1672 richard 66
	$l_ldap_submit			= "Enregistrer";
67
	$l_ldap_reset			= "Annuler";
1160 stephane 68
	$l_ldap_test_network_failed	= "Pas de connectivité réseau avec le serveur LDAP.";
69
	$l_ldap_test_connection_failed	= "Impossible de se connecter au serveur LDAP.";
70
	$l_ldap_test_bind_ok		= "Connexion LDAP réussie...";
1673 richard 71
	$l_ldap_test_bind_failed	= "Echec d'authentification sur le serveur LDAP... Vérifiez votre configuration";
1395 richard 72
	$l_ldap_test_dn_ok		= "DN semble bon";
73
	$l_ldap_test_dn_failed		= "DN semble mauvais";
2449 tom.houday 74
	$l_ldap_error			= "erreur LDAP";
75
} else {				// English
1160 stephane 76
	$l_ldap_update_sucess		= "Successfull LDAP settings update";
1672 richard 77
	$l_ldap_title			= "External authentication : LDAP";
78
	$l_ldap_legend			= "LDAP authentication";
1160 stephane 79
	$l_ldap_auth_enable_label	= "Use LDAP authentication :";
1672 richard 80
	$l_ldap_YES			= "YES";
81
	$l_ldap_NO			= "NO";
1677 richard 82
	$l_ldap_server_label		= "LDAP server :";
2306 tom.houday 83
	$l_ldap_server_text		= "This is the IP address of the LDAP server.";
1160 stephane 84
	$l_ldap_base_dn_label		= "LDAP base dn:";
1673 richard 85
	$l_ldap_base_dn_text		= "This is the 'Distinguished Name', locating the user information in the directory. e.g. LDAP : 'o=MyCompany,c=US'. e.g. AD : 'ou=my_lan,dc=server_name,dc=localdomain'";
1160 stephane 86
	$l_ldap_filter_label		= "LDAP uid:";
1673 richard 87
	$l_ldap_filter_text		= "This is the key used to search for a given login identity, e.g. 'uid', 'sn', etc.. For AD use 'sAMAccountName'.";
1160 stephane 88
	$l_ldap_base_filter_label	= "LDAP user filter:";
89
	$l_ldap_base_filter_text	= "Optionally you can further limit the searched objects with additional filters. For example 'objectClass=posixGroup' would result in the use of '(&amp;(uid=username)(objectClass=posixGroup))'";
1672 richard 90
	$l_ldap_user_label		= "LDAP user dn:";
1673 richard 91
	$l_ldap_user_text		= "Username used by ALCASAR to connect to the LDAP server. Leave blank to use anonymous binding. Required for AD. e.g. LDAP :'uid=Username,ou=my_lan,o=mycompany,c=US'. e.g. AD : cn=username,ou=my_lan,dc=server_name,dc=localdomain'";
1160 stephane 92
	$l_ldap_password_label		= "LDAP password:";
1673 richard 93
	$l_ldap_password_text		= "Leave blank to use anonymous binding. Required for AD.";
1672 richard 94
	$l_ldap_submit			= "Save";
95
	$l_ldap_reset			= "Reset";
1160 stephane 96
	$l_ldap_test_network_failed	= "LDAP server is not reachable.";
97
	$l_ldap_test_connection_failed	= "LDAP connexion failed...";
98
	$l_ldap_test_bind_ok		= "LDAP connexion success...";
99
	$l_ldap_test_bind_failed	= "LDAP authentication failed...Check your ldap setup...";
1395 richard 100
	$l_ldap_test_dn_ok              = "DN seems to be right";
101
	$l_ldap_test_dn_failed          = "DN seems to be wrong";
2449 tom.houday 102
	$l_ldap_error			= "LDAP error";
1160 stephane 103
}
318 richard 104
 
105
 
2449 tom.houday 106
$message = '';
107
if ((isset($_GET['erreur'])) && (!empty($_GET['erreur']))) { 
108
	$message  = '<div style="text-align: center"><br>';
109
	$message .= '<span style="font-weight: bold; color: red;">'.htmlspecialchars($erreur).'</span><br>';
110
	$message .= '<br></div>';
111
} else if (isset($_GET['update']) && ($_GET['update'] === 'ok')) {
112
	$message  = '<div style="text-align: center"><br>';
113
	$message .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
114
	$message .= '<br></div>';
318 richard 115
}
116
 
2449 tom.houday 117
// LDAP configuration params
118
$ldap_status      = ($conf['LDAP'] === 'on');
119
$ldap_server      = $conf['LDAP_SERVER'];
120
$ldap_identity    = $conf['LDAP_USER'];
121
$ldap_password    = $conf['LDAP_PASSWORD'];
122
$ldap_basedn      = $conf['LDAP_BASE'];
123
$ldap_filter      = $conf['LDAP_UID'];
124
$ldap_base_filter = $conf['LDAP_FILTER'];
318 richard 125
 
776 stephane 126
 
2449 tom.houday 127
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_filter, $f_ldap_port = 389) {
128
	// Test connect to the LDAP server
620 stephane 129
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
658 stephane 130
		// no network connection
620 stephane 131
		return -1;
2449 tom.houday 132
	}
133
	fclose($sock);
1395 richard 134
 
2449 tom.houday 135
	// Test connect to the LDAP server
136
	$ldapconn = ldap_connect($f_ldap_server, $f_ldap_port);
137
	ldap_set_option($ldapconn, LDAP_OPT_TIMELIMIT, 2);
138
	if (!$ldapconn) {
139
		// LDAP connection failed
140
		return -2;
620 stephane 141
	}
142
 
2449 tom.houday 143
	$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
144
	if (!$ldapbind) {
145
		// Test LDAP Version 3
146
		ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
147
		$ldapbind = ldap_bind($ldapconn, $f_ldap_identity, $f_ldap_password);
318 richard 148
 
2449 tom.houday 149
		if (!$ldapbind) {
150
			// LDAP Bind failed
151
			return 0;
318 richard 152
		}
2449 tom.houday 153
	}
154
	ldap_unbind($ldapconn);
155
 
156
	// try search
157
	$query = $f_ldap_filter.'=*';
158
	if (ldap_search($ldapconn, $f_ldap_basedn, $query)) {
159
		return 2;
318 richard 160
	} else {
2449 tom.houday 161
		return 1;
318 richard 162
	}
163
}
2449 tom.houday 164
 
165
// TODO : check LDAP PHP extension loaded?
166
// if (!extension_loaded('ldap')) {
167
// 	exit();
168
// }
169
 
170
$pos = strpos($ldap_server, '//');
171
if ($pos !== false) {
172
	// TODO : useless?
173
	$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"
658 stephane 174
} else {
659 richard 175
	$new_ldap_server = $ldap_server;
658 stephane 176
}
2449 tom.houday 177
if ($ldap_status) {
178
	$serverCheckResult = ldap_checkServerConfig($new_ldap_server, $ldap_identity, $ldap_password, $ldap_basedn, $ldap_filter);
179
}
1395 richard 180
 
620 stephane 181
?>
2449 tom.houday 182
<!DOCTYPE html>
183
<html>
184
<head>
185
	<meta charset="UTF-8">
186
	<title><?= $l_ldap_title ?></title>
187
	<link type="text/css" href="/css/style.css" rel="stylesheet">
188
	<link type="text/css" href="/css/acc.css" rel="stylesheet">
189
	<link type="text/css" href="/css/ldap.css" rel="stylesheet">
190
	<script>
191
	function onLdapStatusChange() {
192
		var listToDisables = ['ldap_server', 'ldap_dn', 'ldap_filter', 'ldap_base_filter', 'ldap_user', 'ldap_password'];
318 richard 193
 
2449 tom.houday 194
		if (document.getElementById("auth_enable").value === '1') {
195
			for (var i=0; i<listToDisables.length; i++) {
196
				document.getElementById(listToDisables[i]).style.backgroundColor = '#ffffff';
197
				document.getElementById(listToDisables[i]).disabled = false;
198
			}
199
		} else {
200
			for (var i=0; i<listToDisables.length; i++) {
201
				document.getElementById(listToDisables[i]).style.backgroundColor = '#c0c0c0';
202
				document.getElementById(listToDisables[i]).disabled = true;
203
			}
204
		}
205
	}
206
	</script>
207
</head>
208
<body onLoad="onLdapStatusChange();">
209
	<div class="panel">
210
		<div class="panel-header"><?= $l_ldap_legend ?></div>
211
		<div class="panel-body">
212
			<form name="config_ldap" method="post" action="update_ldap.php">
213
				<fieldset>
214
					<legend>
215
						<?= $message ?>
216
						<?php if ($ldap_status): ?>
217
							<div style="text-align: center"><br>
218
								<?php if ($serverCheckResult === -2): ?>
219
									<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
220
								<?php elseif ($serverCheckResult === -1): ?>
221
									<span style="color: red"><?= $l_ldap_test_network_failed ?></span>
222
								<?php elseif ($serverCheckResult === 0): ?>
223
									<span style="color: red"><?= $l_ldap_test_bind_failed ?></span>
224
								<?php elseif ($serverCheckResult === 1): ?>
225
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
226
										<br>";
227
										<span style="color: red"><?= $l_ldap_test_dn_failed ?></span>
228
								<?php elseif ($serverCheckResult === 2): ?>
229
										<span style="color: green"><?= $l_ldap_test_bind_ok ?></span>
230
										<br>";
231
										<span style="color: green"><?= $l_ldap_test_dn_ok ?></span>
232
								<?php else: ?>
233
									<span><?= $l_ldap_error ?></span>
234
								<?php endif ?>
235
								<br><br>
236
							</div>
237
						<?php endif ?>
238
					</legend>
239
					<dl>
240
						<dt>
241
							<label for="auth_enable"><?= $l_ldap_auth_enable_label ?></label>
242
						</dt>
243
						<dd>
244
							<select id="auth_enable" name="auth_enable" onchange="onLdapStatusChange();">
245
								<option value="1"<?= ($ldap_status)  ? ' selected="selected"' : '' ?>><?= $l_ldap_YES ?></option>
246
								<option value="0"<?= (!$ldap_status) ? ' selected="selected"' : '' ?>><?= $l_ldap_NO ?></option>
247
							</select>
248
						</dd>
249
					</dl>
250
					<dl>
251
						<dt>
252
							<label for="ldap_server"><?= $l_ldap_server_label ?></label><br>
253
							<?= $l_ldap_server_text ?>
254
						</dt>
255
						<dd>
256
							<input id="ldap_server" size="40" name="ldap_server" value="<?= htmlspecialchars($ldap_server) ?>">
257
						</dd>
258
					</dl>
259
					<dl>
260
						<dt>
261
							<label for="ldap_dn"><?= $l_ldap_base_dn_label ?></label><br>
262
							<?= $l_ldap_base_dn_text ?>
263
						</dt>
264
						<dd>
265
							<input id="ldap_dn" size="40" name="ldap_base_dn" value="<?= htmlspecialchars($ldap_basedn) ?>">
266
						</dd>
267
					</dl>
268
					<dl>
269
						<dt>
270
							<label for="ldap_filter"><?= $l_ldap_filter_label ?></label><br>
271
							<?= $l_ldap_filter_text ?>
272
						</dt>
273
						<dd>
274
							<input id="ldap_filter" size="40" name="ldap_filter" value="<?= htmlspecialchars($ldap_filter) ?>">
275
						</dd>
276
					</dl>
277
					<dl>
278
						<dt>
279
							<label for="ldap_base_filter"><?= $l_ldap_base_filter_label ?></label><br>
280
							<?= $l_ldap_base_filter_text ?>
281
						</dt>
282
						<dd>
283
							<input id="ldap_base_filter" size="40" name="ldap_base_filter" value="<?= htmlspecialchars($ldap_base_filter) ?>">
284
						</dd>
285
					</dl>
286
					<dl>
287
						<dt>
288
							<label for="ldap_user"><?= $l_ldap_user_label ?></label><br>
289
							<?= $l_ldap_user_text ?>
290
						</dt>
291
						<dd>
292
							<input id="ldap_user" size="40" name="ldap_user" value="<?= htmlspecialchars($ldap_identity) ?>">
293
						</dd>
294
					</dl>
295
					<dl>
296
						<dt>
297
							<label for="ldap_password"><?= $l_ldap_password_label ?></label><br>
298
							<?= $l_ldap_password_text ?>
299
						</dt>
300
						<dd>
301
							<input id="ldap_password" type="password" size="40" name="ldap_password" value="<?= htmlspecialchars($ldap_password) ?>">
302
						</dd>
303
					</dl>
304
					<p>
305
						<input id="submit" type="submit" value="<?= $l_ldap_submit ?>" name="submit">
306
						<input id="reset" type="reset" value="<?= $l_ldap_reset ?>" name="reset">
307
					</p>
308
				</fieldset>
309
			</form>
310
		</div>
311
	</div>
318 richard 312
</body>
313
</html>