2017-03-29 08:22:02 -05:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
|
2018-09-11 07:51:35 -05:00
|
|
|
use LibreNMS\Authentication\LegacyAuth;
|
2017-03-29 08:22:02 -05:00
|
|
|
|
2018-03-29 05:40:27 -05:00
|
|
|
$options = getopt('u:rldvh');
|
|
|
|
if (isset($options['h']) || (!isset($options['l']) && !isset($options['u']))) {
|
2017-03-29 08:22:02 -05:00
|
|
|
echo ' -u <username> (Required) username to test
|
2018-03-29 05:40:27 -05:00
|
|
|
-l List all users (checks that auth can enumerate all allowed users)
|
2017-03-29 08:22:02 -05:00
|
|
|
-d Enable debug output
|
|
|
|
-v Enable verbose debug output
|
|
|
|
-h Display this help message
|
|
|
|
';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['d'])) {
|
|
|
|
$debug = true;
|
|
|
|
}
|
|
|
|
|
2018-09-11 07:51:35 -05:00
|
|
|
$init_modules = [];
|
2018-04-05 16:43:54 -05:00
|
|
|
require realpath(__DIR__ . '/..') . '/includes/init.php';
|
|
|
|
|
2017-03-29 08:22:02 -05:00
|
|
|
if (isset($options['v'])) {
|
2018-03-29 05:40:27 -05:00
|
|
|
// Enable debug mode for auth methods that have it
|
|
|
|
$config['auth_ad_debug'] = 1;
|
|
|
|
$config['auth_ldap_debug'] = 1;
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "Authentication Method: {$config['auth_mechanism']}\n";
|
|
|
|
|
|
|
|
// if ldap like, check selinux
|
2018-09-11 07:51:35 -05:00
|
|
|
if ($config['auth_mechanism'] == 'ldap' || $config['auth_mechanism'] == "active_directory") {
|
2017-03-29 08:22:02 -05:00
|
|
|
$enforce = shell_exec('getenforce 2>/dev/null');
|
|
|
|
if (str_contains($enforce, 'Enforcing')) {
|
|
|
|
// has selinux
|
|
|
|
$output = shell_exec('getsebool httpd_can_connect_ldap');
|
|
|
|
if ($output != "httpd_can_connect_ldap --> on\n") {
|
|
|
|
print_error("You need to run: setsebool -P httpd_can_connect_ldap=1");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 11:33:03 +01:00
|
|
|
try {
|
2018-09-11 07:51:35 -05:00
|
|
|
$authorizer = LegacyAuth::get();
|
2017-11-18 11:33:03 +01:00
|
|
|
|
|
|
|
// AD bind tests
|
|
|
|
if ($authorizer instanceof \LibreNMS\Authentication\ActiveDirectoryAuthorizer) {
|
|
|
|
// peek inside the class
|
|
|
|
$lc_rp = new ReflectionProperty($authorizer, 'ldap_connection');
|
|
|
|
$lc_rp->setAccessible(true);
|
2017-11-28 09:19:34 -06:00
|
|
|
$adbind_rm = new ReflectionMethod($authorizer, 'bind');
|
2017-11-18 11:33:03 +01:00
|
|
|
$adbind_rm->setAccessible(true);
|
|
|
|
|
|
|
|
$bind_success = false;
|
|
|
|
if (isset($config['auth_ad_binduser']) && isset($config['auth_ad_bindpassword'])) {
|
|
|
|
$bind_success = $adbind_rm->invoke($authorizer, false, true);
|
|
|
|
if (!$bind_success) {
|
|
|
|
$ldap_error = ldap_error($lc_rp->getValue($authorizer));
|
|
|
|
echo $ldap_error . PHP_EOL;
|
|
|
|
if ($ldap_error == 'Invalid credentials') {
|
|
|
|
print_error('AD bind failed for user ' . $config['auth_ad_binduser'] . '@' . $config['auth_ad_domain'] .
|
|
|
|
'. Check $config[\'auth_ad_binduser\'] and $config[\'auth_ad_bindpassword\'] in your config.php');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_message('AD bind success');
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-18 11:33:03 +01:00
|
|
|
$bind_success = $adbind_rm->invoke($authorizer, true, true);
|
|
|
|
if (!$bind_success) {
|
|
|
|
echo ldap_error($lc_rp->getValue($authorizer)) . PHP_EOL;
|
|
|
|
print_message("Could not anonymous bind to AD");
|
|
|
|
} else {
|
|
|
|
print_message('AD bind anonymous successful');
|
|
|
|
}
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 11:33:03 +01:00
|
|
|
if (!$bind_success) {
|
|
|
|
print_error("Could not bind to AD, you will not be able to use the API or alert AD users");
|
|
|
|
}
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:40:27 -05:00
|
|
|
if (isset($options['l'])) {
|
|
|
|
$users = $authorizer->getUserlist();
|
2018-08-24 03:05:23 -05:00
|
|
|
$output = array_map(function ($user) {
|
|
|
|
return "{$user['username']} ({$user['user_id']})";
|
|
|
|
}, $users);
|
|
|
|
|
|
|
|
echo "Users: " . implode(', ', $output) . PHP_EOL;
|
2018-03-29 05:40:27 -05:00
|
|
|
echo "Total users: " . count($users) . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$test_username = $options['u'];
|
2017-11-18 11:33:03 +01:00
|
|
|
$auth = false;
|
2017-03-29 08:22:02 -05:00
|
|
|
|
2018-09-11 07:51:35 -05:00
|
|
|
echo 'Password: ';
|
|
|
|
`stty -echo`;
|
|
|
|
$test_password = trim(fgets(STDIN));
|
|
|
|
`stty echo`;
|
|
|
|
echo PHP_EOL;
|
2017-03-29 08:22:02 -05:00
|
|
|
|
2018-09-11 07:51:35 -05:00
|
|
|
echo "Authenticate user $test_username: \n";
|
|
|
|
$auth = $authorizer->authenticate($test_username, $test_password);
|
|
|
|
unset($test_password);
|
|
|
|
|
|
|
|
if ($auth) {
|
|
|
|
print_message("AUTH SUCCESS\n");
|
2017-03-29 08:22:02 -05:00
|
|
|
} else {
|
2018-09-11 07:51:35 -05:00
|
|
|
if (isset($ldap_connection)) {
|
|
|
|
echo ldap_error($ldap_connection) . PHP_EOL;
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
2018-09-11 07:51:35 -05:00
|
|
|
print_error('AUTH FAILURE');
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 11:33:03 +01:00
|
|
|
if ($auth) {
|
|
|
|
$user_id = $authorizer->getUserid($test_username);
|
2017-03-29 08:22:02 -05:00
|
|
|
|
2017-11-18 11:33:03 +01:00
|
|
|
echo "User ($user_id):\n";
|
|
|
|
if (method_exists($authorizer, 'getUser')) {
|
|
|
|
$user = $authorizer->getUser($user_id);
|
2017-03-29 08:22:02 -05:00
|
|
|
|
2017-11-18 11:33:03 +01:00
|
|
|
unset($user['password']);
|
|
|
|
unset($user['remember_token']);
|
|
|
|
foreach ($user as $property => $value) {
|
|
|
|
echo " $property => $value\n";
|
|
|
|
}
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 11:33:03 +01:00
|
|
|
if (method_exists($authorizer, 'getGroupList')) {
|
|
|
|
echo 'Groups: ' . implode('; ', $authorizer->getGroupList()) . PHP_EOL;
|
|
|
|
}
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|
2017-11-18 11:33:03 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
echo "Error: " . get_class($e) . " thrown!\n";
|
|
|
|
echo $e->getMessage() . PHP_EOL;
|
2017-03-29 08:22:02 -05:00
|
|
|
}
|