Subversion Repositories ALCASAR

Rev

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

Rev 2449 Rev 2451
Line 1... Line 1...
1
<?php
1
<?php
2
# $Id: ldap.php 2449 2017-12-05 21:42:08Z tom.houdayer $
2
# $Id: ldap.php 2451 2017-12-05 22:33:27Z tom.houdayer $
3
 
3
 
4
/* written by steweb57 & Rexy */
4
/* written by steweb57 & Rexy */
5
/****************************************************************
5
/****************************************************************
6
*			GLOBAL FILE PATHS			*
6
*			GLOBAL FILE PATHS			*
7
*****************************************************************/
7
*****************************************************************/
Line 101... Line 101...
101
	$l_ldap_test_dn_failed          = "DN seems to be wrong";
101
	$l_ldap_test_dn_failed          = "DN seems to be wrong";
102
	$l_ldap_error			= "LDAP error";
102
	$l_ldap_error			= "LDAP error";
103
}
103
}
104
 
104
 
105
 
105
 
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>';
-
 
115
}
-
 
116
 
-
 
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'];
-
 
125
 
-
 
126
 
-
 
127
function ldap_checkServerConfig($f_ldap_server, $f_ldap_identity, $f_ldap_password, $f_ldap_basedn, $f_ldap_filter, $f_ldap_port = 389) {
106
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
107
	// Test connect to the LDAP server
129
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
108
	if (!$sock = @fsockopen($f_ldap_server, $f_ldap_port, $num, $error, 2)) {
130
		// no network connection
109
		// no network connection
131
		return -1;
110
		return -1;
Line 160... Line 139...
160
	} else {
139
	} else {
161
		return 1;
140
		return 1;
162
	}
141
	}
163
}
142
}
164
 
143
 
-
 
144
$messages = '';
-
 
145
 
-
 
146
if (isset($_POST['auth_enable'])) {
-
 
147
	if ($_POST['auth_enable'] === '1') {
-
 
148
		$varErrors = [];
-
 
149
		if (isset($_POST['ldap_server']))      $ldap_server      = $_POST['ldap_server'];      else array_push($varErrors, 'Variable error "ldap_server"');
-
 
150
		if (isset($_POST['ldap_base_dn']))     $ldap_base_dn     = $_POST['ldap_base_dn'];     else array_push($varErrors, 'Variable error "ldap_base_dn"');
-
 
151
		if (isset($_POST['ldap_filter']))      $ldap_filter      = $_POST['ldap_filter'];      else array_push($varErrors, 'Variable error "ldap_filter"');
-
 
152
		if (isset($_POST['ldap_base_filter'])) $ldap_base_filter = $_POST['ldap_base_filter']; else array_push($varErrors, 'Variable error "ldap_base_filter"');
-
 
153
		if (isset($_POST['ldap_user']))        $ldap_user        = $_POST['ldap_user'];        else array_push($varErrors, 'Variable error "ldap_user"');
-
 
154
		if (isset($_POST['ldap_password']))    $ldap_password    = $_POST['ldap_password'];    else array_push($varErrors, 'Variable error "ldap_password"');
-
 
155
 
-
 
156
		// Validation
-
 
157
		if (isset($ldap_server)) {
-
 
158
			if ((!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) && (preg_match('/^[a-zA-Z0-9-_.]+$/', $ldap_server))) {
-
 
159
				$ldap_server = gethostbyname($ldap_server);
-
 
160
			}
-
 
161
			if (!preg_match('/^([0-9]{1,3}\.){3}([0-9]{1,3})$/', $ldap_server)) {
-
 
162
				array_push($varErrors, 'Invalid LDAP server IP');
-
 
163
			}
-
 
164
		}
-
 
165
 
-
 
166
		if (!empty($varErrors)) { 
-
 
167
			$messages .= '<div style="text-align: center"><br>';
-
 
168
			foreach ($varErrors as $error) {
-
 
169
				$messages .= '<span style="font-weight: bold; color: red;">'.$error.'</span><br>';
-
 
170
			}
-
 
171
			$messages .= '<br></div>';
-
 
172
		} else {
-
 
173
			exec('sed -i \'s/^LDAP=.*/LDAP=on/g\' '.CONF_FILE);
-
 
174
			exec('sed -i '.escapeshellarg("s/^LDAP_SERVER=.*/LDAP_SERVER=$ldap_server/g").' '.CONF_FILE);
-
 
175
			exec('sed -i '.escapeshellarg("s/^LDAP_BASE=.*/LDAP_BASE=$ldap_base_dn/g").' '.CONF_FILE);
-
 
176
			exec('sed -i '.escapeshellarg("s/^LDAP_UID=.*/LDAP_UID=$ldap_filter/g").' '.CONF_FILE);
-
 
177
			exec('sed -i '.escapeshellarg("s/^LDAP_FILTER=.*/LDAP_FILTER=$ldap_filter/g").' '.CONF_FILE);
-
 
178
			exec('sed -i '.escapeshellarg("s/^LDAP_USER=.*/LDAP_USER=$ldap_user/g").' '.CONF_FILE);
-
 
179
			exec('sed -i '.escapeshellarg("s/^LDAP_PASSWORD=.*/LDAP_PASSWORD=$ldap_password/g").' '.CONF_FILE);
-
 
180
			exec('sudo /usr/local/bin/alcasar-ldap.sh --on');
-
 
181
 
-
 
182
			$messages .= '<div style="text-align: center"><br>';
-
 
183
			$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
-
 
184
			$messages .= '<br></div>';
-
 
185
		}
-
 
186
	} else {
-
 
187
		exec('sed -i "s/^LDAP=.*/LDAP=off/g" '.CONF_FILE);
-
 
188
		exec('sudo /usr/local/bin/alcasar-ldap.sh --off');
-
 
189
 
-
 
190
		$messages .= '<div style="text-align: center"><br>';
-
 
191
		$messages .= '<span style="font-weight: bold; color: green;">'.$l_ldap_update_sucess.'</span><br>';
-
 
192
		$messages .= '<br></div>';
-
 
193
	}
-
 
194
 
-
 
195
	// Reload configuration
-
 
196
	$file_conf = fopen(CONF_FILE, 'r');
-
 
197
	if (!$file_conf) {
-
 
198
		exit('Error opening the file '.CONF_FILE);
-
 
199
	}
-
 
200
	while (!feof($file_conf)) {
-
 
201
		$buffer = fgets($file_conf, 4096);
-
 
202
		if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
-
 
203
			$tmp = explode('=', $buffer, 2);
-
 
204
			$conf[trim($tmp[0])] = trim($tmp[1]);
-
 
205
		}
-
 
206
	}
-
 
207
	fclose($file_conf);
-
 
208
}
-
 
209
 
-
 
210
// LDAP configuration params
-
 
211
$ldap_status      = ($conf['LDAP'] === 'on');
-
 
212
$ldap_server      = $conf['LDAP_SERVER'];
-
 
213
$ldap_identity    = $conf['LDAP_USER'];
-
 
214
$ldap_password    = $conf['LDAP_PASSWORD'];
-
 
215
$ldap_basedn      = $conf['LDAP_BASE'];
-
 
216
$ldap_filter      = $conf['LDAP_UID'];
-
 
217
$ldap_base_filter = $conf['LDAP_FILTER'];
-
 
218
 
165
// TODO : check LDAP PHP extension loaded?
219
// TODO : check LDAP PHP extension loaded?
166
// if (!extension_loaded('ldap')) {
220
// if (!extension_loaded('ldap')) {
167
// 	exit();
221
// 	exit();
168
// }
222
// }
169
 
223
 
Line 207... Line 261...
207
</head>
261
</head>
208
<body onLoad="onLdapStatusChange();">
262
<body onLoad="onLdapStatusChange();">
209
	<div class="panel">
263
	<div class="panel">
210
		<div class="panel-header"><?= $l_ldap_legend ?></div>
264
		<div class="panel-header"><?= $l_ldap_legend ?></div>
211
		<div class="panel-body">
265
		<div class="panel-body">
212
			<form name="config_ldap" method="post" action="update_ldap.php">
266
			<form name="config_ldap" method="POST" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
213
				<fieldset>
267
				<fieldset>
214
					<legend>
268
					<legend>
215
						<?= $message ?>
269
						<?= $messages ?>
216
						<?php if ($ldap_status): ?>
270
						<?php if ($ldap_status): ?>
217
							<div style="text-align: center"><br>
271
							<div style="text-align: center"><br>
218
								<?php if ($serverCheckResult === -2): ?>
272
								<?php if ($serverCheckResult === -2): ?>
219
									<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
273
									<span style="color: red"><?= $l_ldap_test_connection_failed ?></span>
220
								<?php elseif ($serverCheckResult === -1): ?>
274
								<?php elseif ($serverCheckResult === -1): ?>