* 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"); $authFactory = new \OPNsense\Auth\AuthenticationFactory(); $authCNFOptions = $authFactory->listConfigOptions(); config_read_array('system', 'authserver'); config_read_array('ca'); $a_server = []; foreach (auth_get_authserver_list() as $servers) { $a_server[] = $servers; } $act = null; if ($_SERVER['REQUEST_METHOD'] === 'GET') { // input record id, if valid if (isset($_GET['id']) && isset($a_server[$_GET['id']])) { $id = $_GET['id']; } if (isset($_GET['act'])) { $act = $_GET['act']; } $pconfig = []; $pconfig['sync_memberof_groups'] = []; if ($act == "new") { $pconfig['ldap_protver'] = 3; $pconfig['radius_srvcs'] = "both"; $pconfig['radius_auth_port'] = "1812"; $pconfig['radius_acct_port'] = "1813"; $pconfig['type'] = 'ldap'; // gather auth plugin defaults // the hotplug properties should be different per type, if not the default won't function correctly foreach ($authCNFOptions as $authType) { foreach ($authType['additionalFields'] as $fieldname => $field) { if (!empty($field['default']) && empty($pconfig[$fieldname])) { $pconfig[$fieldname] = $field['default']; } } } } elseif ($act == "edit" && isset($id)) { $pconfig['type'] = $a_server[$id]['type']; $pconfig['name'] = $a_server[$id]['name']; if (in_array($pconfig['type'], array("ldap", "ldap-totp"))) { $pconfig['ldap_host'] = $a_server[$id]['host']; $pconfig['ldap_port'] = $a_server[$id]['ldap_port']; $pconfig['ldap_urltype'] = $a_server[$id]['ldap_urltype']; $pconfig['ldap_protver'] = $a_server[$id]['ldap_protver']; $pconfig['ldap_scope'] = $a_server[$id]['ldap_scope']; $pconfig['ldap_basedn'] = $a_server[$id]['ldap_basedn']; $pconfig['ldap_authcn'] = $a_server[$id]['ldap_authcn']; $pconfig['ldap_extended_query'] = $a_server[$id]['ldap_extended_query']; $pconfig['ldap_attr_user'] = $a_server[$id]['ldap_attr_user']; if (!empty($a_server[$id]['ldap_binddn'])) { $pconfig['ldap_binddn'] = $a_server[$id]['ldap_binddn']; } if (!empty($a_server[$id]['ldap_bindpw'])) { $pconfig['ldap_bindpw'] = $a_server[$id]['ldap_bindpw']; } $pconfig['ldap_read_properties'] = !empty($a_server[$id]['ldap_read_properties']); $pconfig['sync_memberof'] = !empty($a_server[$id]['ldap_sync_memberof']); $pconfig['sync_create_local_users'] = !empty($a_server[$id]['ldap_sync_create_local_users']); if (!empty($a_server[$id]['ldap_sync_memberof_groups'])) { $pconfig['sync_memberof_groups'] = explode(",", $a_server[$id]['ldap_sync_memberof_groups']); } } elseif ($pconfig['type'] == "radius") { $pconfig['radius_host'] = $a_server[$id]['host'] ?? ''; $pconfig['radius_auth_port'] = $a_server[$id]['radius_auth_port'] ?? ''; $pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'] ?? ''; $pconfig['radius_secret'] = $a_server[$id]['radius_secret'] ?? ''; $pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'] ?? ''; if (!empty($pconfig['radius_auth_port']) && !empty($pconfig['radius_acct_port'])) { $pconfig['radius_srvcs'] = "both"; } else { $pconfig['radius_srvcs'] = "auth"; } if (empty($pconfig['radius_auth_port'])) { $pconfig['radius_auth_port'] = 1812; } $pconfig['sync_memberof'] = !empty($a_server[$id]['sync_memberof']); $pconfig['sync_create_local_users'] = !empty($a_server[$id]['sync_create_local_users']); if (!empty($a_server[$id]['sync_memberof_groups'])) { $pconfig['sync_memberof_groups'] = explode(",", $a_server[$id]['sync_memberof_groups']); } } elseif ($pconfig['type'] == 'local') { foreach (['password_policy_duration', 'enable_password_policy_constraints', 'password_policy_complexity', 'password_policy_length'] as $fieldname) { if (!empty($config['system']['webgui'][$fieldname])) { $pconfig[$fieldname] = $config['system']['webgui'][$fieldname]; } else { $pconfig[$fieldname] = null; } } } if (!empty($authCNFOptions[$pconfig['type']])) { foreach ($authCNFOptions[$pconfig['type']]['additionalFields'] as $fieldname => $field) { $pconfig[$fieldname] = $a_server[$id][$fieldname]; } } } } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { $input_errors = []; $pconfig = $_POST; if (isset($pconfig['id']) && isset($a_server[$pconfig['id']])) { $id = $pconfig['id']; } if (isset($pconfig['act'])) { $act = $pconfig['act']; } if (isset($pconfig['save'])) { /* input validation */ if (in_array($pconfig['type'], array("ldap", "ldap-totp"))) { $reqdfields = explode(" ", "name type ldap_host ldap_port ". "ldap_urltype ldap_protver ldap_scope ". "ldap_attr_user ldapauthcontainers"); $reqdfieldsn = array( gettext("Descriptive name"), gettext("Type"), gettext("Hostname or IP"), gettext("Port value"), gettext("Transport"), gettext("Protocol version"), gettext("Search level"), gettext("User naming Attribute"), gettext("Authentication container")); if (!empty($pconfig['ldap_binddn']) && !empty($pconfig['ldap_bindpw'])) { $reqdfields[] = "ldap_binddn"; $reqdfields[] = "ldap_bindpw"; $reqdfieldsn[] = gettext("Bind user DN"); $reqdfieldsn[] = gettext("Bind Password"); } } elseif ($pconfig['type'] == "radius") { $reqdfields = explode(" ", "name type radius_host radius_srvcs"); $reqdfieldsn = array( gettext("Descriptive name"), gettext("Type"), gettext("Hostname or IP"), gettext("Services")); if ($pconfig['radius_srvcs'] == "both" || $pconfig['radius_srvcs'] == "auth") { $reqdfields[] = "radius_auth_port"; $reqdfieldsn[] = gettext("Authentication port value"); } if ($id == null) { $reqdfields[] = "radius_secret"; $reqdfieldsn[] = gettext("Shared Secret"); } } if (!empty($authCNFOptions[$pconfig['type']])) { foreach ($authCNFOptions[$pconfig['type']]['additionalFields'] as $fieldname => $field) { if (!empty($field['validate'])) { foreach ($field['validate']($pconfig[$fieldname]) as $input_error) { $input_errors[] = $input_error; } } } } do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors); if (!empty($pconfig['ldap_host']) && !(is_hostname($pconfig['ldap_host']) || is_ipaddr($pconfig['ldap_host']))) { $input_errors[] = gettext("The host name contains invalid characters."); } if (!empty($pconfig['radius_host']) && !(is_hostname($pconfig['radius_host']) || is_ipaddr($pconfig['radius_host']))) { $input_errors[] = gettext("The host name contains invalid characters."); } if (auth_get_authserver($pconfig['name']) && !isset($id)) { $input_errors[] = gettext("An authentication server with the same name already exists."); } if (($pconfig['type'] == "radius") && isset($pconfig['radius_timeout']) && !empty($pconfig['radius_timeout']) && (!is_numeric($pconfig['radius_timeout']) || (is_numeric($pconfig['radius_timeout']) && ($pconfig['radius_timeout'] <= 0)))) { $input_errors[] = gettext("RADIUS Timeout value must be numeric and positive."); } if (empty($pconfig['name'])) { $input_errors[] = gettext('A server name must be provided.'); } elseif (strpos($pconfig['name'], ',') !== false) { $input_errors[] = gettext('Invalid server name given.'); } if (count($input_errors) == 0) { $server = []; $server['refid'] = uniqid(); if (isset($id)) { $server = $a_server[$id]; } else { $server['type'] = $pconfig['type']; $server['name'] = $pconfig['name']; } if (in_array($server['type'], array("ldap", "ldap-totp"))) { $server['host'] = $pconfig['ldap_host']; $server['ldap_port'] = $pconfig['ldap_port']; $server['ldap_urltype'] = $pconfig['ldap_urltype']; $server['ldap_protver'] = $pconfig['ldap_protver']; $server['ldap_scope'] = $pconfig['ldap_scope']; $server['ldap_basedn'] = $pconfig['ldap_basedn']; $server['ldap_authcn'] = $pconfig['ldapauthcontainers']; $server['ldap_extended_query'] = $pconfig['ldap_extended_query']; $server['ldap_attr_user'] = $pconfig['ldap_attr_user']; if (!empty($pconfig['ldap_binddn']) && !empty($pconfig['ldap_bindpw']) ){ $server['ldap_binddn'] = $pconfig['ldap_binddn']; $server['ldap_bindpw'] = $pconfig['ldap_bindpw']; } else { if (isset($server['ldap_binddn'])) { unset($server['ldap_binddn']); } if (isset($server['ldap_bindpw'])) { unset($server['ldap_bindpw']); } } $server['ldap_read_properties'] = !empty($pconfig['ldap_read_properties']); $server['ldap_sync_memberof'] = !empty($pconfig['sync_memberof']); $server['ldap_sync_memberof_groups'] = !empty($pconfig['sync_memberof_groups']) ? implode(",", $pconfig['sync_memberof_groups']) : []; $server['ldap_sync_create_local_users'] = !empty($pconfig['sync_create_local_users']); } elseif ($server['type'] == "radius") { $server['host'] = $pconfig['radius_host']; if (!empty($pconfig['radius_secret'])) { $server['radius_secret'] = $pconfig['radius_secret']; } if (!empty($pconfig['radius_timeout'])) { $server['radius_timeout'] = $pconfig['radius_timeout']; } else { $server['radius_timeout'] = 5; } if ($pconfig['radius_srvcs'] == "both") { $server['radius_auth_port'] = $pconfig['radius_auth_port']; $server['radius_acct_port'] = $pconfig['radius_acct_port']; } if ($pconfig['radius_srvcs'] == "auth") { $server['radius_auth_port'] = $pconfig['radius_auth_port']; unset($server['radius_acct_port']); } $server['sync_memberof'] = !empty($pconfig['sync_memberof']); $server['sync_memberof_groups'] = !empty($pconfig['sync_memberof_groups']) ? implode(",", $pconfig['sync_memberof_groups']) : []; $server['sync_create_local_users'] = !empty($pconfig['sync_create_local_users']); } elseif ($server['type'] == 'local') { foreach (array('password_policy_duration', 'enable_password_policy_constraints', 'password_policy_complexity', 'password_policy_length') as $fieldname) { if (!empty($pconfig[$fieldname])) { $config['system']['webgui'][$fieldname] = $pconfig[$fieldname]; } elseif (isset($config['system']['webgui'][$fieldname])) { unset($config['system']['webgui'][$fieldname]); } } } if (!empty($authCNFOptions[$server['type']])) { foreach ($authCNFOptions[$server['type']]['additionalFields'] as $fieldname => $field) { $server[$fieldname] = $pconfig[$fieldname]; } } if ($server['type'] != 'local') { if (isset($id) && isset($config['system']['authserver'][$id])) { $config['system']['authserver'][$id] = $server; } else { $config['system']['authserver'][] = $server; } } write_config(); header(url_safe('Location: /system_authservers.php')); exit; } else { $act = "edit"; } } elseif (isset($pconfig['act']) && $pconfig['act'] == 'del' && isset($id)) { /* Remove server from main list. */ $serverdeleted = $a_server[$id]['name']; foreach ($config['system']['authserver'] as $k => $as) { if ($config['system']['authserver'][$k]['name'] == $serverdeleted) { unset($config['system']['authserver'][$k]); } } write_config(sprintf('Authentication server "%s" deleted.', $serverdeleted)); header(url_safe('Location: /system_authservers.php')); exit; } } // list of all possible fields for auth item (used for form init) $all_authfields = array( 'type','name','ldap_host','ldap_port','ldap_urltype','ldap_protver','ldap_scope', 'ldap_basedn','ldap_authcn','ldap_extended_query','ldap_binddn','ldap_bindpw','ldap_attr_user', 'ldap_read_properties', 'sync_memberof', 'sync_create_local_users', 'radius_host', 'radius_auth_port','radius_acct_port','radius_secret','radius_timeout','radius_srvcs', 'password_policy_duration', 'enable_password_policy_constraints', 'password_policy_complexity', 'password_policy_length' ); foreach ($all_authfields as $fieldname) { if (!isset($pconfig[$fieldname])) { $pconfig[$fieldname] = null; } } legacy_html_escape_form_data($pconfig); legacy_html_escape_form_data($a_server); include("head.inc"); ?>