Commit b4845e62 authored by Ad Schellevis's avatar Ad Schellevis

(legacy/ldap) trashed almost all old ldap code and made it less obscure..

When a user needs to use the web configurator, import the the user from the remote ldap server using "import users" on system_usermanager.php
For backward compatibility purposes ldap_backed still can authenticate without a local user, although the web config isn't usable then.
(this always costs an extra query to determine the distinguished name of the user)
We may need to consider if "authenticate only" should be a bit more explicit to the user, or choose to always import users into OPNsense....
parent d5640d74
......@@ -391,10 +391,14 @@ function local_sync_accounts()
function local_user_set(&$user)
{
if (empty($user['password'])) {
log_error(sprintf(
gettext('There is something wrong in your config because user %s password is missing!'),
$user['name']
));
if (empty($user['user_dn'])) {
// log error for local users, (ldap) server authenticated users should not be created locally
// and therefore maybe empty
log_error(sprintf(
gettext('There is something wrong in your config because user %s password is missing!'),
$user['name']
));
}
return;
}
......@@ -620,45 +624,6 @@ function local_group_del($group)
mwexecf('/usr/sbin/pw groupdel %s', $group['name']);
}
function ldap_test_connection($authcfg)
{
global $config;
if ($authcfg) {
if (strstr($authcfg['ldap_urltype'], "Standard"))
$ldapproto = "ldap";
else
$ldapproto = "ldaps";
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
$ldapport = $authcfg['ldap_port'];
if (!empty($ldapport))
$ldapserver .= ":{$ldapport}";
$ldapbasedn = $authcfg['ldap_basedn'];
$ldapbindun = $authcfg['ldap_binddn'];
$ldapbindpw = $authcfg['ldap_bindpw'];
} else
return false;
/* first check if there is even an LDAP server populated */
if(!$ldapserver)
return false;
/* Setup CA environment if needed. */
ldap_setup_caenv($authcfg);
/* connect and see if server is up */
$error = false;
if (!($ldap = ldap_connect($ldapserver)))
$error = true;
if ($error == true) {
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
return false;
}
return true;
}
function ldap_setup_caenv($authcfg)
{
......@@ -687,453 +652,66 @@ function ldap_setup_caenv($authcfg)
putenv("LDAPTLS_CACERT=/var/run/certs/{$caref['refid']}.ca");
}
function ldap_test_bind($authcfg)
{
global $config;
if ($authcfg) {
if (strstr($authcfg['ldap_urltype'], "Standard"))
$ldapproto = "ldap";
else
$ldapproto = "ldaps";
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
$ldapport = $authcfg['ldap_port'];
if (!empty($ldapport))
$ldapserver .= ":{$ldapport}";
$ldapbasedn = $authcfg['ldap_basedn'];
$ldapbindun = $authcfg['ldap_binddn'];
$ldapbindpw = $authcfg['ldap_bindpw'];
$ldapver = $authcfg['ldap_protver'];
if (empty($ldapbndun) || empty($ldapbindpw))
$ldapanon = true;
else
$ldapanon = false;
} else
return false;
/* first check if there is even an LDAP server populated */
if(!$ldapserver)
return false;
/* Setup CA environment if needed. */
ldap_setup_caenv($authcfg);
/* connect and see if server is up */
$error = false;
if (!($ldap = ldap_connect($ldapserver)))
$error = true;
if ($error == true) {
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
return false;
}
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
if ($ldapanon == true) {
if (!($res = @ldap_bind($ldap))) {
@ldap_close($ldap);
return false;
}
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
@ldap_close($ldap);
return false;
}
@ldap_unbind($ldap);
return true;
}
function ldap_get_user_ous($show_complete_ou=true, $authcfg)
/**
* authenticate using ldap
*/
function ldap_backed($username, $passwd, $authcfg)
{
global $config;
if(!function_exists("ldap_connect"))
return;
global $config;
$ous = array();
if(!$username)
return;
if ($authcfg) {
if (strstr($authcfg['ldap_urltype'], "Standard"))
$ldapproto = "ldap";
else
$ldapproto = "ldaps";
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
$ldapport = $authcfg['ldap_port'];
if (!empty($ldapport))
$ldapserver .= ":{$ldapport}";
$ldapbasedn = $authcfg['ldap_basedn'];
$ldapbindun = $authcfg['ldap_binddn'];
$ldapbindpw = $authcfg['ldap_bindpw'];
$ldapver = $authcfg['ldap_protver'];
if (empty($ldapbindun) || empty($ldapbindpw))
$ldapanon = true;
else
$ldapanon = false;
$ldapname = $authcfg['name'];
$ldapfallback = false;
$ldapscope = $authcfg['ldap_scope'];
} else
return false;
/* first check if there is even an LDAP server populated */
if(!$ldapserver) {
log_error(gettext("ERROR! ldap_get_user_ous() backed selected with no LDAP authentication server defined."));
return $ous;
}
/* Setup CA environment if needed. */
ldap_setup_caenv($authcfg);
/* connect and see if server is up */
$error = false;
if (!($ldap = ldap_connect($ldapserver)))
$error = true;
if ($error == true) {
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
return $ous;
}
$ldapfilter = "(|(ou=*)(cn=Users))";
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
if ($ldapanon == true) {
if (!($res = @ldap_bind($ldap))) {
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind anonymously to server %s."), $ldapname));
@ldap_close($ldap);
return $ous;
}
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind to server %s."), $ldapname));
@ldap_close($ldap);
return $ous;
}
if ($ldapscope == "one")
$ldapfunc = "ldap_list";
else
$ldapfunc = "ldap_search";
$search = @$ldapfunc($ldap, $ldapbasedn, $ldapfilter);
$info = @ldap_get_entries($ldap, $search);
if (is_array($info)) {
foreach ($info as $inf) {
if (!$show_complete_ou) {
$inf_split = explode(",", $inf['dn']);
$ou = $inf_split[0];
$ou = str_replace("OU=","", $ou);
$ou = str_replace("CN=","", $ou);
} else
if($inf['dn'])
$ou = $inf['dn'];
if($ou)
$ous[] = $ou;
}
}
@ldap_unbind($ldap);
return $ous;
}
function ldap_get_groups($username, $authcfg)
{
global $config;
if(!function_exists("ldap_connect"))
return;
if(!function_exists("ldap_connect"))
return;
if(!$username)
return false;
if(!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
$username_split = explode("@", $username);
$username = $username_split[0];
}
if(stristr($username, "\\")) {
$username_split = explode("\\", $username);
$username = $username_split[0];
}
//log_error("Getting LDAP groups for {$username}.");
if ($authcfg) {
if (strstr($authcfg['ldap_urltype'], "Standard"))
$ldapproto = "ldap";
else
$ldapproto = "ldaps";
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
$ldapport = $authcfg['ldap_port'];
if (!empty($ldapport))
$ldapserver .= ":{$ldapport}";
$ldapbasedn = $authcfg['ldap_basedn'];
$ldapbindun = $authcfg['ldap_binddn'];
$ldapbindpw = $authcfg['ldap_bindpw'];
$ldapauthcont = $authcfg['ldap_authcn'];
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
$ldapgroupattribute = strtolower($authcfg['ldap_attr_member']);
$ldapfilter = "({$ldapnameattribute}={$username})";
$ldaptype = "";
$ldapver = $authcfg['ldap_protver'];
if (empty($ldapbindun) || empty($ldapbindpw))
$ldapanon = true;
else
$ldapanon = false;
$ldapname = $authcfg['name'];
$ldapfallback = false;
$ldapscope = $authcfg['ldap_scope'];
} else
return false;
$ldapdn = $_SESSION['ldapdn'];
/*Convert attribute to lowercase. php ldap arrays put everything in lowercase */
$ldapgroupattribute = strtolower($ldapgroupattribute);
$memberof = array();
/* Setup CA environment if needed. */
ldap_setup_caenv($authcfg);
/* connect and see if server is up */
$error = false;
if (!($ldap = ldap_connect($ldapserver)))
$error = true;
if ($error == true) {
log_error(sprintf(gettext("ERROR! ldap_get_groups() Could not connect to server %s."), $ldapname));
return memberof;
}
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
/* bind as user that has rights to read group attributes */
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
if ($ldapanon == true) {
if (!($res = @ldap_bind($ldap))) {
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind anonymously to server %s."), $ldapname));
@ldap_close($ldap);
return false;
// search user dn in config
$userDN = null;
foreach ($config['system']['user'] as $confUser) {
if (!empty($confUser['user_dn']) && $username == $confUser['name']) {
$userDN = $confUser['user_dn'];
break;
}
}
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind to server %s."), $ldapname));
@ldap_close($ldap);
return memberof;
}
/* get groups from DN found */
/* use ldap_read instead of search so we don't have to do a bunch of extra work */
/* since we know the DN is in $_SESSION['ldapdn'] */
//$search = ldap_read($ldap, $ldapdn, "(objectclass=*)", array($ldapgroupattribute));
if ($ldapscope == "one")
$ldapfunc = "ldap_list";
else
$ldapfunc = "ldap_search";
$search = @$ldapfunc($ldap, $ldapdn, $ldapfilter, array($ldapgroupattribute));
$info = @ldap_get_entries($ldap, $search);
$countem = $info["count"];
if(is_array($info[0][$ldapgroupattribute])) {
/* Iterate through the groups and throw them into an array */
foreach ($info[0][$ldapgroupattribute] as $member) {
if (stristr($member, "CN=") !== false) {
$membersplit = explode(",", $member);
$memberof[] = preg_replace("/CN=/i", "", $membersplit[0]);
// backward compatibility, try to find this user dn to authenticate.
// --> this means the user can't have any roles on OPNsense!
if ($userDN == null) {
$ldap_auth = new OPNsense\Auth\LDAP($authcfg['ldap_basedn'], $authcfg['ldap_protver']);
$ldap_is_connected = $ldap_auth->connect($authcfg['ldap_full_url']
, $authcfg['ldap_binddn']
, $authcfg['ldap_bindpw']
);
if ($ldap_is_connected) {
$result = $ldap_auth->searchUsers($username
, $authcfg['ldap_attr_user']
, $authcfg['ldap_extended_query']
);
if (count($result) > 0) {
$userDN = $result[0]['dn'];
}
} else {
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $authcfg['ldap_full_url']));
}
}
}
/* Time to close LDAP connection */
@ldap_unbind($ldap);
$groups = print_r($memberof,true);
return $memberof;
}
function ldap_format_host($host)
{
return is_ipaddrv6($host) ? "[$host]" : $host ;
}
function ldap_backed($username, $passwd, $authcfg)
{
global $config;
if(!$username)
return;
if(!function_exists("ldap_connect"))
return;
if(!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
$username_split = explode("@", $username);
$username = $username_split[0];
}
if(stristr($username, "\\")) {
$username_split = explode("\\", $username);
$username = $username_split[0];
}
if ($authcfg) {
if (strstr($authcfg['ldap_urltype'], "Standard"))
$ldapproto = "ldap";
else
$ldapproto = "ldaps";
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
$ldapport = $authcfg['ldap_port'];
if (!empty($ldapport))
$ldapserver .= ":{$ldapport}";
$ldapbasedn = $authcfg['ldap_basedn'];
$ldapbindun = $authcfg['ldap_binddn'];
$ldapbindpw = $authcfg['ldap_bindpw'];
if (empty($ldapbindun) || empty($ldapbindpw))
$ldapanon = true;
else
$ldapanon = false;
$ldapauthcont = $authcfg['ldap_authcn'];
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
$ldapextendedqueryenabled = $authcfg['ldap_extended_enabled'];
$ldapextendedquery = $authcfg['ldap_extended_query'];
$ldapfilter = "";
if(!$ldapextendedqueryenabled)
{
$ldapfilter = "({$ldapnameattribute}={$username})";
}
else
{
$ldapfilter =
"(&({$ldapnameattribute}={$username})({$ldapextendedquery}))";
}
$ldaptype = "";
$ldapver = $authcfg['ldap_protver'];
$ldapname = $authcfg['name'];
$ldapscope = $authcfg['ldap_scope'];
} else
return false;
/* first check if there is even an LDAP server populated */
if(!$ldapserver) {
if ($ldapfallback) {
log_error(gettext("ERROR! ldap_backed() called with no LDAP authentication server defined. Defaulting to local user database. Visit System -> User Manager."));
return local_backed($username, $passwd);
} else
log_error(gettext("ERROR! ldap_backed() called with no LDAP authentication server defined."));
return false;
}
/* Setup CA environment if needed. */
ldap_setup_caenv($authcfg);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
/* Make sure we can connect to LDAP */
$error = false;
if (!($ldap = ldap_connect($ldapserver)))
$error = true;
if ($error == true) {
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
return false;
}
/* ok, its up. now, lets bind as the bind user so we can search it */
$error = false;
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
if ($ldapanon == true) {
if (!($res = @ldap_bind($ldap)))
$error = true;
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw)))
$error = true;
if ($error == true) {
@ldap_close($ldap);
log_error(sprintf(gettext("ERROR! Could not bind to server %s."), $ldapname));
return false;
}
/* Get LDAP Authcontainers and split em up. */
$ldac_splits = explode(";", $ldapauthcont);
/* setup the usercount so we think we havn't found anyone yet */
$usercount = 0;
/*****************************************************************/
/* We First find the user based on username and filter */
/* Then, once we find the first occurance of that person */
/* We set seesion variables to ponit to the OU and DN of the */
/* Person. To later be used by ldap_get_groups. */
/* that way we don't have to search twice. */
/*****************************************************************/
/* Iterate through the user containers for search */
foreach ($ldac_splits as $i => $ldac_split) {
$ldac_split = isset($authcfg['ldap_utf8']) ? utf8_encode($ldac_split) : $ldac_split;
$ldapfilter = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapfilter) : $ldapfilter;
$ldapsearchbasedn = isset($authcfg['ldap_utf8']) ? utf8_encode("{$ldac_split},{$ldapbasedn}") : "{$ldac_split},{$ldapbasedn}";
/* Make sure we just use the first user we find */
if ($ldapscope == "one")
$ldapfunc = "ldap_list";
else
$ldapfunc = "ldap_search";
/* Support legacy auth container specification. */
if (stristr($ldac_split, "DC=") || empty($ldapbasedn))
$search = @$ldapfunc($ldap,$ldac_split,$ldapfilter);
else
$search = @$ldapfunc($ldap,$ldapsearchbasedn,$ldapfilter);
if (!$search) {
log_error(sprintf(gettext("Search resulted in error: %s"), ldap_error($ldap)));
continue;
if ( $userDN != null ) {
// setup peer ca
ldap_setup_caenv($authcfg);
// try connect to ldap server, connect tells if this user could authenticate
$ldap_auth = new OPNsense\Auth\LDAP($authcfg['ldap_basedn'], $authcfg['ldap_protver']);
$ldap_is_connected = $ldap_auth->connect($authcfg['ldap_full_url']
, $userDN
, $passwd
);
return $ldap_is_connected;
}
$info = ldap_get_entries($ldap,$search);
$matches = $info['count'];
if ($matches == 1){
$userdn = $_SESSION['ldapdn'] = $info[0]['dn'];
$_SESSION['ldapou'] = $ldac_split[$i];
$_SESSION['ldapon'] = "true";
$usercount = 1;
break;
}
}
if ($usercount != 1){
@ldap_unbind($ldap);
log_error(gettext("ERROR! Either LDAP search failed, or multiple users were found."));
return false;
}
/* Now lets bind as the user we found */
$passwd = isset($authcfg['ldap_utf8']) ? utf8_encode($passwd) : $passwd;
if (!($res = @ldap_bind($ldap, $userdn, $passwd))) {
log_error(sprintf(gettext('ERROR! Could not login to server %1$s as user %2$s: %3$s'), $ldapname, $username, ldap_error($ldap)));
@ldap_unbind($ldap);
return false;
}
/* At this point we are bound to LDAP so the user was auth'd okay. Close connection. */
@ldap_unbind($ldap);
return true;
}
function radius_backed($username, $passwd, $authcfg, &$attributes = array())
......
......@@ -138,28 +138,15 @@ function getPrivPages(& $entry, & $allowed_pages) {
function getAllowedPages($username) {
global $config, $_SESSION;
if (!function_exists("ldap_connect"))
return;
$allowed_pages = array();
$allowed_groups = array();
if (isset($config['system']['webgui']['authmode'])) {
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
} else {
$authcfg['type'] = 'local';
}
// obtain ldap groups if we are in ldap mode
if ($authcfg['type'] == "ldap")
$allowed_groups = @ldap_get_groups($username, $authcfg);
else {
// search for a local user by name
$local_user = getUserEntry($username);
getPrivPages($local_user, $allowed_pages);
// obtain local groups if we have a local user
$allowed_groups = local_user_get_groups($local_user);
}
// search for a local user by name
$local_user = getUserEntry($username);
getPrivPages($local_user, $allowed_pages);
// obtain local groups if we have a local user
$allowed_groups = local_user_get_groups($local_user);
// build a list of allowed pages
if (is_array($config['system']['group']) && is_array($allowed_groups)) {
......@@ -204,6 +191,7 @@ function session_auth() {
} else {
$authcfg = null;
}
// authenticate using config settings, or local if failed
if (authenticate_user($_POST['usernamefld'], $_POST['passwordfld'], $authcfg) ||
authenticate_user($_POST['usernamefld'], $_POST['passwordfld'])) {
// Generate a new id to avoid session fixation
......
......@@ -41,6 +41,9 @@ $pagetitle = gentitle( $pgtitle );
<!-- Favicon -->
<link href="/themes/<?=$g['theme'];?>/assets/images/favicon.png" rel="shortcut icon">
<!-- Font awesome -->
<link rel="stylesheet" href="/ui/css/font-awesome.min.css">
<!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script><![endif]-->
......
......@@ -77,6 +77,9 @@ function get_user_privdesc(& $user)
// start admin user code
$pgtitle = array(gettext("System"),gettext("User Manager"));
// find web ui authentication method
$authcfg_type = auth_get_authserver($config['system']['webgui']['authmode'])['type'];
$input_errors = array();
if (isset($_POST['userid']) && is_numericint($_POST['userid'])) {
......@@ -97,6 +100,7 @@ if (isset($_SERVER['HTTP_REFERER'])) {
if (isset($id) && $a_user[$id]) {
$pconfig['usernamefld'] = $a_user[$id]['name'];
$pconfig['user_dn'] = isset($a_user[$id]['user_dn']) ? $a_user[$id]['user_dn'] : null;
$pconfig['descr'] = $a_user[$id]['descr'];
$pconfig['expires'] = $a_user[$id]['expires'];
$pconfig['groups'] = local_user_get_groups($a_user[$id]);
......@@ -457,6 +461,15 @@ function sshkeyClicked(obj) {
document.getElementById("sshkeychck").style.display="";
}
}
function import_ldap_users() {
url="system_usermanager_import_ldap.php";
var oWin = window.open(url,"OPNsense","width=620,height=400,top=150,left=150");
if (oWin==null || typeof(oWin)=="undefined") {
alert("<?=gettext('Popup blocker detected. Action aborted.');?>");
}
}
//]]>
</script>
......@@ -529,6 +542,16 @@ function sshkeyClicked(obj) {
<input name="oldusername" type="hidden" id="oldusername" value="<?=htmlspecialchars($pconfig['usernamefld']);?>" />
</td>
</tr>
<?php if (!empty($pconfig['user_dn'])):
?>
<tr>
<td width="22%" valign="top" class="vncellreq"><?=gettext("User distinguished name");?></td>
<td width="78%" class="vtable">
<input name="user_dn" type="text" class="formfld user" id="user_dn" size="20" maxlength="16" value="<?=htmlspecialchars($pconfig['user_dn']);?>"/ readonly>
</td>
</tr>
<?php endif;
?>
<tr>
<td width="22%" valign="top" class="vncellreq" rowspan="2"><?=gettext("Password");?></td>
<td width="78%" class="vtable">
......@@ -948,6 +971,16 @@ endif;?>
onclick="document.getElementById('act').value='<?php echo "new";?>';"
title="<?=gettext("add user");?>" data-toggle="tooltip" data-placement="left" ><span class="glyphicon glyphicon-plus"></span>
</button>
<?php if ($authcfg_type == 'ldap') :
?>
<button type="submit" name="import"
class="btn btn-default btn-xs"
onclick="import_ldap_users();"
title="<?=gettext("import users")?>">
<i class="fa fa-cloud-download"></i>
</button>
<?php endif;
?>
</td>
</tr>
<tr>
......
<?php
/*
Copyright (C) 2014-2015 Deciso B.V.
Copyright (C) 2007 Scott Ullrich <sullrich@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require_once("guiconfig.inc");
require_once("auth.inc");
function add_local_user($username, $userdn) {
global $config;
foreach ($config['system']['user'] as &$user) {
if ($user['name'] == $username && $user['name'] != 'root') {
// link local user to remote server by updating user_dn
$user['user_dn'] = $userdn;
return;
}
}
// new user, add
$new_user = array();
$new_user['scope'] = 'user';
$new_user['name'] = $username;
$new_user['user_dn'] = $userdn;
$new_user['uid'] = $config['system']['nextuid']++;
$config['system']['user'][] = $new_user;
}
global $config;
// attributes used in page
$ldap_users= array();
$ldap_is_connected = false;
$exit_form = false;
// find gui auth server
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
if ($authcfg['type'] == 'ldap') {
// setup peer ca
ldap_setup_caenv($authcfg);
// connect to ldap server
$ldap_auth = new OPNsense\Auth\LDAP($authcfg['ldap_basedn'], $authcfg['ldap_protver']);
$ldap_is_connected = $ldap_auth->connect($authcfg['ldap_full_url']
, $authcfg['ldap_binddn']
, $authcfg['ldap_bindpw']
);
if ($ldap_is_connected) {
// collect list of current ldap users from config
$confDNs = array();
foreach ($config['system']['user'] as $confUser) {
if (!empty($confUser['user_dn'])) {
$confDNs[] = trim($confUser['user_dn']);
}
}
// search ldap
$result = $ldap_auth->searchUsers("*"
, $authcfg['ldap_attr_user']
, $authcfg['ldap_extended_query']
);
// actual form action, either save new accounts or list missing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// create selected accounts
$exit_form = true;
if (isset($_POST['user_dn'])) {
$update_count = 0;
foreach ($result as $ldap_user ) {
foreach ($_POST['user_dn'] as $userDN) {
if ($userDN == $ldap_user['dn'] && !in_array($ldap_user['dn'], $confDNs)) {
add_local_user($ldap_user['name'] , $ldap_user['dn']);
$update_count++;
}
}
if ($update_count > 0){
// write config when changed
write_config();
}
}
}
} else {
// list all missing accounts
foreach ($result as $ldap_user ) {
if (!in_array($ldap_user['dn'], $confDNs)) {
$ldap_users[$ldap_user['name']] = $ldap_user['dn'];
}
}
}
}
}
include('head.inc');
?>
<body>
<?php if ($exit_form) :
?>
<script type="text/javascript">
// exit form and reload parent after save
window.opener.location.href = window.opener.location.href;
window.close();
</script>
<?php elseif (!$ldap_is_connected) :
?>
<p><?=gettext("Could not connect to the LDAP server. Please check your LDAP configuration.");?></p>
<input type='button' class="btn btn-default" value='<?=gettext("Close"); ?>' onClick="window.close();">
<?php
else :
?>
<form method="post">
<table class="table table-striped">
<tbody>
<tr>
<th colspan="3">
<?=gettext("Please select users to import:");?>
</th>
</tr>
<?php foreach ($ldap_users as $username => $userDN) :
?>
<tr><td><?=$username?></td><td><?=$userDN?></td><td> <input type='checkbox' value="<?=$userDN?>" id='user_dn' name='user_dn[]'> </td></tr>
<?php endforeach;
?>
<tr>
<td align="left" colspan="3">
<input type='submit' class="btn btn-primary" value='<?=gettext("Save");?>'>
</td>
</tr>
</tbody>
</table>
</form>
<?php
endif; ?>
<!-- bootstrap script -->
<script type="text/javascript" src="/javascript/bootstrap.min.js"></script>
<!-- Fancy select with search options -->
<script type="text/javascript" src="/javascript/bootstrap-select.min.js"></script>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment