xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / ldap.php (71b5d22b825de5621226b5da6c936ab749593c3e) (14KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('track_errors', 'On');

$rg_cache_debug = TRUE;
$rg_util_debug = TRUE;
$rg_sql_debug = 100;
$test_normal = TRUE;

$INC = dirname(__FILE__) . '/../inc';
require_once(dirname(__FILE__) . '/config.php');
require_once($INC . '/init.inc.php');
require_once($INC . '/util.inc.php');
require_once($INC . '/ldap.inc.php');
require_once($INC . '/ldap_sync.inc.php');
require_once('helpers.inc.php');
require_once('http.inc.php');

rg_log_set_file('ldap.log');

require_once('common.php');

$_testns = 'admin_ldap';


function rg_ldap_start_server(&$l)
{
	$prep = 'ldap/chroot-' . $l['rg_ldap_ns'] . '/prep.done';
	$r = @unlink($prep);
	rg_log('unlinking prep.done returned ' . ($r === FALSE ? 'false' : 'true'));

	$log = __DIR__ . '/ldap-' . $l['rg_ldap_port'];

	$pid = pcntl_fork();
	if ($pid == 0) { // child
		foreach ($l as $k => $v) {
			rg_log($k . '=' . $v);
			putenv($k . '=' . $v);
		}
		rg_exec('cd ldap && bash -x start.sh &> ' . $log . '.log', '', FALSE, FALSE, FALSE);
		rg_exec('cd ldap && bash -x prepare.sh &> ' . $log . '.prep.log', '', FALSE, FALSE, FALSE);
		exit(0);
	}

	rg_log('Child started with pid ' . $pid);

	rg_log('Waiting for preparation to finish...');
	$tries = 200;
	while ($tries-- > 0) {
		$x = @file_get_contents($prep);
		if ($x !== FALSE)
			break;

		rg_log('prep file [' . $prep . '] is not present, wait.');
		usleep(100000);
	}

	if ($tries == 0) {
		rg_log('Could not prepare! Exit!');
		posix_kill($pid, SIGKILL);
		exit(1);
	}

	$l['log'] = $log;
}

function clean($log)
{
	rg_log_set_file('ldap-clean.log');
	rg_log('Cleaning processes attached to file ' . $log . '.log...');
	$r = rg_exec('fuser -k -v -9 ' . escapeshellarg($log . '.log'), '', FALSE, FALSE, FALSE);
	rg_log_ml('fuser returned: ' . print_r($r, TRUE));
}


prepare_http();

$rg_ui = array('is_admin' => 1);
rg_test_create_user($db, $rg_ui);
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}


rg_log('');
rg_log('Generating certificates...');
$r = rg_exec('./ca.sh ldap', '', FALSE, FALSE, FALSE);
if (!strstr($r['data'], 'CA_SH_OK')) {
        rg_log_ml('data: ' . $r['data']);
        rg_log('Cannot generate certificates!');
        exit(1);
}


$bind_addr = '127.' . rand(1, 255) . '.' . rand(1,255) . '.' . rand(2,255);
$bind_port = 65100; $bind_ports = 65101;
$pass = rg_id(16);
$user_pass = rg_id(16);
$user_key = rg_id(16);
rg_log('bind_addr=' . $bind_addr . ' bind_port=' . $bind_port
	. ' bind_ports=' . $bind_ports . ' pass=' . $pass
	. ' user_pass=' . $user_pass . ' user_key=' . $user_key);


rg_log('');
rg_log_enter('Deleting all LDAP servers');
$sql = 'TRUNCATE ldap_servers';
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log('Cannot delete all LDAP servers');
	exit(1);
}
rg_sql_free_result($res);
rg_cache_unset('ldap', RG_SOCKET_NO_WAIT);
rg_log_exit();


rg_log('');
rg_log_enter('Deleting LDAP cache');
$sql = 'TRUNCATE ldap_cache';
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log('Cannot delete LDAP cache');
	exit(1);
}
rg_sql_free_result($res);
//TODO rg_cache_unset('ldap_cache', RG_SOCKET_NO_WAIT);
rg_log_exit();


rg_log('');
rg_log_enter('Loading Admin -> LDAP -> Add...');
$data = array();
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/add', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load add page!');
	exit(1);
}
if (!isset($r['tokens']['ldap_add'])) {
	rg_log_ml('r:' . print_r($r, TRUE));
	rg_log('No ldap_add token?!');
	exit(1);
}
$token = $r['tokens']['ldap_add'];
$id = rg_id(8);
$name = 'server-' . $id . ' <xss>';

rg_log('Posting the form...');
$data = array(
	'doit' => 1,
	'token' => $token,
	'ldap::id' => 0,
	'ldap::plan_id' => 0,
	'ldap::prio' => 10,
	'ldap::session_time' => 600,
	'ldap::name' => $name,
	'ldap::url' => 'ldap://' . $bind_addr . ':' . $bind_port,
	'ldap::bind_dn' => '',
	'ldap::bind_pass' => '',
	'ldap::user_base' => 'dc=my-domain,dc=com',
	'ldap::uid_attr' => 'uid',
	'ldap::filter' => 'memberOf=cn=group1,ou=Group,dc=my-domain,dc=com',
	'ldap::group_base' => 'dc=my-domain,dc=com',
	'ldap::group_attr' => 'memberOf',
	'ldap::group_filter' => '',
	'ldap::admin_group' => 'cn=(Admins|Admins2),ou=Group,dc=my-domain,dc=com',
	'ldap::ca_cert' => ''
);
$r = do_req($test_url . '/op/admin/ldap/add', $data, $headers);
if (!strstr($r['body'], 'LDAP server has been successfully added/edited.')) {
	rg_log_ml('body: ' . $r['body']);
	rg_log('Success message not found!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading Admin -> LDAP -> List...');
$data = array();
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/list', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load list page!');
	exit(1);
}
// TODO, here test that everything is present in the list
$token = $r['tokens']['ldap_list'];
rg_log_exit();


rg_log('');
rg_log_enter('Setting up two LDAP servers in mirror mode...');
// We are forced to close the connection, else will get a nasty error,
// Even if the child is not doing anything with the connection.
rg_sql_close($db);
$l1 = array(
	'rg_ldap_ns' => 's1',
	'rg_ldap_addr' => $bind_addr,
	'rg_ldap_port' => $bind_port,
	'rg_ldap_ports' => $bind_ports,
	'rg_ldap_server_id' => '001',
	'rg_ldap_producer_url' => 'ldap://' . $bind_addr . ':' . ($bind_port + 2),
	'rg_ldap_producer_rid' => '002',
	'rg_ldap_add_data' => 1,
	'rg_ldap_pass' => $pass,
	'rg_ldap_user_pass' => $user_pass,
	'rg_ldap_user_key' => $user_key
);
rg_ldap_start_server($l1);

$l2 = array(
	'rg_ldap_ns' => 's2',
	'rg_ldap_addr' => $bind_addr,
	'rg_ldap_port' => $bind_port + 2,
	'rg_ldap_ports' => $bind_ports + 2,
	'rg_ldap_server_id' => '002',
	'rg_ldap_producer_url' => 'ldap://' . $bind_addr . ':' . $bind_port,
	'rg_ldap_producer_rid' => '001',
	'rg_ldap_add_data' => 0,
	'rg_ldap_pass' => $pass,
	'rg_ldap_user_pass' => $user_pass,
	'rg_ldap_user_key' => $user_key
);
rg_ldap_start_server($l2);

register_shutdown_function('clean', $l1['log']);
register_shutdown_function('clean', $l2['log']);

$data = array(
	'addr' => $bind_addr,
	'port' => $bind_port,
	'bind_user' => 'cn=Manager,dc=my-domain,dc=com',
	'bind_pass' => $pass,
	'base' => 'dc=my-domain,dc=com'
);
rg_log_exit();


rg_log('');
rg_log_enter('Find out the id of the server...');
$r = rg_ldap_list($db);
if ($r['ok'] !== 1) {
	rg_log('Cannot get the ldap servers list: ' . $r['errmsg']);
	exit(1);
}
$found = FALSE;
foreach ($r['list'] as $id => $info) {
	if (strcmp($info['url'], 'ldap://' . $bind_addr . ':' . $bind_port) != 0)
		continue;

	$found = TRUE;
	break;
}
if (!$found) {
	rg_log('Could not find the server in database!');
	exit(1);
}
$data['server_id'] = $id;
rg_log_exit();


/* TODO: this has to be made available after I deal with the sync
rg_log('');
rg_log_enter('sync_ro...');
$r = rg_ldap_sync_ro($db, $data);
if ($r['ok'] != 1) {
	rg_log('Cannot sync: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Deleting user user4...');
$r = rg_ldap_core_connect('ldap://' . $l1['rg_ldap_addr']
	. ':' . $l1['rg_ldap_port'], 3);
if ($r['ok'] !== 1) {
	rg_log('Cannot connect to second server: ' . $r['errmsg'] . '!');
	exit(1);
}
$con = $r['con'];
$r = rg_ldap_core_bind($con, 'cn=Manager,dc=my-domain,dc=com', $pass);
if ($r['ok'] !== 1) {
	rg_log('cannot bind: ' . $r['errmsg']);
	exit(1);
}
$r = rg_ldap_core_del($con, 'uid=user4,ou=People,dc=my-domain,dc=com');
if ($r['ok'] !== 1) {
	rg_log('Cannot delete: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();

// TODO: needed?!
sleep(3);


rg_log('');
rg_log_enter('get server CSN field...');
$res = rg_sql_query($db, 'SELECT csn FROM ldap_servers'
	. ' WHERE id = ' . $data['server_id']);
if ($res === FALSE) {
	rg_log('Cannot select csn from db: ' . rg_sql_error() . '!');
	exit(1);
}
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$csn = $row['csn'];
rg_log('csn: ' . $csn);
rg_log_exit();


rg_log('');
rg_log_enter('sync_ro (after a delete)...');
$data['rid'] = '001'; $data['csn'] = $csn; // not sure if rid is correct TODO
$r = rg_ldap_sync_ro($db, $data);
if ($r['ok'] != 1) {
	rg_log('Cannot sync: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();
*/


rg_log('');
rg_log_enter('Login using a LDAP user using ldap uid (first login)');
$_ui = array(
	'username' => 'user1-' . $user_key,
	'pass' => $user_pass,
	't' => 'first login by uid'
);
$r = test_login($test_url, $_ui);
if ($r === FALSE)
	exit(1);
rg_log_exit();


rg_log('');
rg_log_enter('Setting ldap_cache.uid to 0 to trigger a conflict in inserting'
	. ' in \'users\' table');
$params = array('user' => 'user1-' . $user_key);
$sql = 'UPDATE ldap_cache SET uid = 0 WHERE ldap_uid = @@user@@';
$res = rg_sql_query_params($db, $sql, $params);
if ($res === FALSE) {
	rg_log('Cannot update ldap_cache.uid!');
	exit(1);
}
rg_sql_free_result($res);
rg_log_exit();


rg_log('');
// User will not be found in 'users' table because we search by e-mail
rg_log_enter('Login using a LDAP user: mail');
$_ui = array(
	'username' => 'user1-' . $user_key . '@my-domain.com',
	'pass' => $user_pass,
	't' => 'login by e-mail expecting conflict inserting into users table'
);
$r = test_login($test_url, $_ui);
if ($r === FALSE)
	exit(1);
rg_log_exit();


rg_log('');
rg_log_enter('Login using a LDAP user: uid (second time, expect user present)');
$_ui = array(
	'username' => 'user1-' . $user_key,
	'pass' => $user_pass,
	't' => 'second login by uid, expecting user present in users table'
);
$r = test_login($test_url, $_ui);
if ($r === FALSE) {
	rg_log('Cannot login!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Login using a LDAP user: uid (third time, but delete from'
	. ' \'users\' table first)');
$params = array(
	'new' => 'user1-fake-' . $user_key,
	'old' => 'user1-' . $user_key
);
// First, find out the uid
$sql = 'SELECT uid FROM users WHERE username = @@old@@';
$res = rg_sql_query_params($db, $sql, $params);
if ($res === FALSE) {
	rg_log('Cannot get info user1 username!');
	exit(1);
}
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$params['uid'] = $row['uid'];

$sql = 'UPDATE users SET username = @@new@@ WHERE uid = @@uid@@';
$res = rg_sql_query_params($db, $sql, $params);
if ($res === FALSE) {
	rg_log('Cannot update user1 username!');
	exit(1);
}

$k = 'username_to_uid::user1-' . $user_key;
rg_cache_unset($k, RG_SOCKET_NO_WAIT);

$k = 'user::' . $params['uid'];
rg_cache_unset($k, RG_SOCKET_NO_WAIT);

$_ui = array(
	'username' => 'user1-' . $user_key,
	'pass' => $user_pass,
	't' => 'login by uid, but delete from users before'
);
$r = test_login($test_url, $_ui);
if ($r === FALSE) {
	rg_log('Cannot login!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('We try to login with user2, which have the description as the uid');
rg_log('Updating LDAP server...');
rg_cache_set('ldap::list::' . $data['server_id'] . '::uid_attr',
	'DescriptioN', RG_SOCKET_NO_WAIT);
$_ui = array(
	'username' => 'user2-' . $user_key,
	'pass' => $user_pass,
	't' => 'now, the uid attr field is description'
);
$r = test_login($test_url, $_ui);
if ($r === FALSE) {
	rg_log('Cannot login!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Login again as the admin user...');
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading Admin -> LDAP -> List -> [Edit]...');
$data = array();
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/edit/' . $id, $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load edit page!');
	exit(1);
}
$token = $r['tokens']['ldap_add'];
rg_log_exit();

rg_log('');
rg_log_enter('Posting new data...');
$data = array(
	'doit' => 1,
	'token' => $token,
	'ldap::id' => $id,
	'ldap::plan_id' => 9,
	'ldap::prio' => 11,
	'ldap::session_time' => 700,
	'ldap::name' => $name . '<xss>',
	'ldap::url' => 'ldaps://' . $bind_addr . ':' . $bind_port,
	'ldap::bind_dn' => 'binddn',
	'ldap::bind_pass' => 'bind_pass',
	'ldap::user_base' => 'o=a,dc=my-domain,dc=com',
	'ldap::uid_attr' => 'uid2',
	'ldap::filter' => 'memberOf=cn=groupX,ou=Group,dc=my-domain,dc=com',
	'ldap::group_base' => 'o=b,dc=my-domain,dc=com',
	'ldap::group_attr' => 'memberOfNot',
	'ldap::group_filter' => 'group_filter',
	'ldap::admin_group' => 'cn=(Admins4|Admins5),ou=Group,dc=my-domain,dc=com',
	'ldap::timeout' => 13,
	'ldap::ca_cert' => 'ca_cert'
);
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/add', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load edit page!');
	exit(1);
}
if (!strstr($r['body'], 'LDAP server has been successfully added/edited.')) {
	rg_log_ml('body: ' . $r['body']);
	rg_log('Success message not found!');
	exit(1);
}
$sql = 'SELECT * FROM ldap_servers WHERE id = ' . $id;
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$key = 'ldap::list::' . $id;
$c = test_wait_cache($key);
foreach ($data as $k => $v) {
	// ignore some fields
	if (strncmp($k, 'ldap::', 6) != 0)
		continue;

	$k = substr($k, 6);

	if (!isset($row[$k])) {
		rg_log('Field [' . $k . '] was not found in db!');
		exit(1);
	}
	if (strcmp($row[$k], $v) != 0) {
		rg_log('Field [' . $k . '] seems was missed in edit (db)!');
		rg_log('db=' . $row[$k] . ' sent=' . $v);
		exit(1);
	}

	if (!isset($c[$k])) {
		rg_log('Field [' . $k . '] was not found in cache!');
		exit(1);
	}
	if (strcmp($c[$k], $v) != 0) {
		rg_log('Field [' . $k . '] seems was missed in edit (cache)!');
		rg_log('cache=' . $c[$k] . ' sent=' . $v);
		exit(1);
	}
}
// Testing if all fields in the database were updated
unset($row['itime']);
unset($row['who']);
unset($row['csn']);
foreach ($row as $k => $v) {
	if (isset($data['ldap::' . $k]))
		continue;

	rg_log('Field [' . $k . '] was missing from the http update!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading Admin -> LDAP -> List (for delete)...');
$data = array();
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/list', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load list page!');
	exit(1);
}
$token = $r['tokens']['ldap_list'];
rg_log_exit();


rg_log('');
rg_log_enter('Deleting a LDAP server...');
$data = array(
	'delete' => 1,
	'token' => $token,
	'delete_list[' . $id . ']' => 'on'
);
$headers = array();
$r = do_req($test_url . '/op/admin/ldap/list', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot load list page!');
	exit(1);
}
if (!strstr($r['body'], 'The selected LDAP servers have been successfully deleted.')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Cannot delete LDAP server(s)!');
	exit(1);
}
rg_log_exit();


rg_log('OK!');
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master