/** * Validates the input parameters for the update() method. * * @param array $hosts hosts data array * @param array $db_hosts db hosts data array * * @throws APIException if the input is invalid. */ protected function validateUpdate(array $hosts, array $db_hosts) { $host_db_fields = ['hostid' => null]; $hosts_full = []; foreach ($hosts as $host) { // Validate mandatory fields. if (!check_db_fields($host_db_fields, $host)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong fields for host "%1$s".', array_key_exists('host', $host) ? $host['host'] : '')); } // Validate host permissions. if (!array_key_exists($host['hostid'], $db_hosts)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } // Validate "groups" field. if (array_key_exists('groups', $host) && (!is_array($host['groups']) || !$host['groups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%1$s".', $db_hosts[$host['hostid']]['host'])); } // Permissions to host groups is validated in massUpdate(). } $inventory_fields = zbx_objectValues(getHostInventories(), 'db_field'); $status_validator = new CLimitedSetValidator(['values' => [HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED], 'messageInvalid' => _('Incorrect status for host "%1$s".')]); $update_discovered_validator = new CUpdateDiscoveredValidator(['allowed' => ['hostid', 'status', 'inventory', 'description'], 'messageAllowedField' => _('Cannot update "%2$s" for a discovered host "%1$s".')]); $host_names = []; foreach ($hosts as &$host) { $db_host = $db_hosts[$host['hostid']]; $host_name = array_key_exists('host', $host) ? $host['host'] : $db_host['host']; if (array_key_exists('status', $host)) { $status_validator->setObjectName($host_name); $this->checkValidator($host['status'], $status_validator); } if (array_key_exists('inventory', $host) && $host['inventory']) { if (array_key_exists('inventory_mode', $host) && $host['inventory_mode'] == HOST_INVENTORY_DISABLED) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set inventory fields for disabled inventory.')); } $fields = array_keys($host['inventory']); foreach ($fields as $field) { if (!in_array($field, $inventory_fields)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect inventory field "%s".', $field)); } } } // cannot update certain fields for discovered hosts $update_discovered_validator->setObjectName($host_name); $this->checkPartialValidator($host, $update_discovered_validator, $db_host); if (array_key_exists('interfaces', $host)) { if (!is_array($host['interfaces']) || !$host['interfaces']) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host'])); } } if (array_key_exists('host', $host)) { if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $host['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for host name "%s".', $host['host'])); } if (array_key_exists('host', $host_names) && array_key_exists($host['host'], $host_names['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same host name "%s" already exists in data.', $host['host'])); } $host_names['host'][$host['host']] = $host['hostid']; } if (array_key_exists('name', $host)) { // if visible name is empty replace it with host name if (zbx_empty(trim($host['name']))) { if (!array_key_exists('host', $host)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Visible name cannot be empty if host name is missing.')); } $host['name'] = $host['host']; } if (array_key_exists('name', $host_names) && array_key_exists($host['name'], $host_names['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same visible name "%s" already exists in data.', $host['name'])); } $host_names['name'][$host['name']] = $host['hostid']; } $hosts_full[] = zbx_array_merge($db_host, $host); } unset($host); if (array_key_exists('host', $host_names) || array_key_exists('name', $host_names)) { $filter = []; if (array_key_exists('host', $host_names)) { $filter['host'] = array_keys($host_names['host']); } if (array_key_exists('name', $host_names)) { $filter['name'] = array_keys($host_names['name']); } $hosts_exists = $this->get(['output' => ['hostid', 'host', 'name'], 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true]); foreach ($hosts_exists as $host_exists) { if (array_key_exists('host', $host_names) && array_key_exists($host_exists['host'], $host_names['host']) && bccomp($host_exists['hostid'], $host_names['host'][$host_exists['host']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same name "%s" already exists.', $host_exists['host'])); } if (array_key_exists('name', $host_names) && array_key_exists($host_exists['name'], $host_names['name']) && bccomp($host_exists['hostid'], $host_names['name'][$host_exists['name']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same visible name "%s" already exists.', $host_exists['name'])); } } $templates_exists = API::Template()->get(['output' => ['hostid', 'host', 'name'], 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true]); foreach ($templates_exists as $template_exists) { if (array_key_exists('host', $host_names) && array_key_exists($template_exists['host'], $host_names['host']) && bccomp($template_exists['templateid'], $host_names['host'][$template_exists['host']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same name "%s" already exists.', $template_exists['host'])); } if (array_key_exists('name', $host_names) && array_key_exists($template_exists['name'], $host_names['name']) && bccomp($template_exists['templateid'], $host_names['name'][$template_exists['name']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same visible name "%s" already exists.', $template_exists['name'])); } } } $this->validateEncryption($hosts_full); }
/** * @param $httpTest * * @throws APIException if bad value for "verify_peer" parameter. * @throws APIException if bad value for "verify_host" parameter. * @throws APIException if SSL cert is present but SSL key is not. */ protected function checkSslParameters($httpTest) { $verifyPeerValidator = new CLimitedSetValidator(array('values' => array(HTTPTEST_VERIFY_PEER_ON, HTTPTEST_VERIFY_PEER_OFF), 'messageInvalid' => _('Incorrect SSL verify peer value for web scenario "%1$s".'))); $verifyPeerValidator->setObjectName($httpTest['name']); $this->checkValidator($httpTest['verify_peer'], $verifyPeerValidator); $verifyHostValidator = new CLimitedSetValidator(array('values' => array(HTTPTEST_VERIFY_HOST_ON, HTTPTEST_VERIFY_HOST_OFF), 'messageInvalid' => _('Incorrect SSL verify host value for web scenario "%1$s".'))); $verifyHostValidator->setObjectName($httpTest['name']); $this->checkValidator($httpTest['verify_host'], $verifyHostValidator); if ($httpTest['ssl_key_password'] != '' && $httpTest['ssl_key_file'] == '') { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Empty SSL key file for web scenario "%1$s".', $httpTest['name'])); } if ($httpTest['ssl_key_file'] != '' && $httpTest['ssl_cert_file'] == '') { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Empty SSL certificate file for web scenario "%1$s".', $httpTest['name'])); } }
protected function checkInput(&$hosts, $method) { $create = $method == 'create'; $update = $method == 'update'; $hostDBfields = $update ? array('hostid' => null) : array('host' => null); if ($update) { $dbHosts = $this->get(array('output' => array('hostid', 'host', 'flags'), 'hostids' => zbx_objectValues($hosts, 'hostid'), 'editable' => true, 'preservekeys' => true)); foreach ($hosts as $host) { if (!isset($dbHosts[$host['hostid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } } } else { $groupids = array(); foreach ($hosts as $host) { if (!isset($host['groups'])) { continue; } $groupids = array_merge($groupids, zbx_objectValues($host['groups'], 'groupid')); } if (!empty($groupids)) { $dbGroups = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'groupids' => $groupids, 'editable' => true, 'preservekeys' => true)); } } foreach ($hosts as $host) { // validate mandatory fields if (!check_db_fields($hostDBfields, $host)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong fields for host "%s".', isset($host['host']) ? $host['host'] : '')); } if ($update) { $hostId = $host['hostid']; // validate host permissions if (!isset($dbHosts[$hostId])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } if (isset($host['groups']) && (!is_array($host['groups']) || !$host['groups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $dbHosts[$hostId]['host'])); } } else { if (!isset($host['groups']) || !is_array($host['groups']) || !$host['groups']) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $host['host'])); } foreach ($host['groups'] as $group) { if (!isset($dbGroups[$group['groupid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } } } $inventoryFields = getHostInventories(); $inventoryFields = zbx_objectValues($inventoryFields, 'db_field'); $statusValidator = new CLimitedSetValidator(array('values' => array(HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED), 'messageInvalid' => _('Incorrect status for host "%1$s".'))); $hostNames = array(); foreach ($hosts as &$host) { if (isset($host['status'])) { $hostName = isset($host['host']) ? $host['host'] : $dbHosts[$host['hostid']]['host']; $statusValidator->setObjectName($hostName); $this->checkValidator($host['status'], $statusValidator); } if (isset($host['inventory']) && !empty($host['inventory'])) { if (isset($host['inventory_mode']) && $host['inventory_mode'] == HOST_INVENTORY_DISABLED) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set inventory fields for disabled inventory.')); } $fields = array_keys($host['inventory']); foreach ($fields as $field) { if (!in_array($field, $inventoryFields)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect inventory field "%s".', $field)); } } } $updateDiscoveredValidator = new CUpdateDiscoveredValidator(array('allowed' => array('hostid', 'status', 'inventory', 'description'), 'messageAllowedField' => _('Cannot update "%1$s" for a discovered host.'))); if ($update) { // cannot update certain fields for discovered hosts $this->checkPartialValidator($host, $updateDiscoveredValidator, $dbHosts[$host['hostid']]); } else { // if visible name is not given or empty it should be set to host name if (!isset($host['name']) || zbx_empty(trim($host['name']))) { $host['name'] = $host['host']; } if (!isset($host['interfaces'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host'])); } } if (isset($host['interfaces'])) { if (!is_array($host['interfaces']) || empty($host['interfaces'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host'])); } } if (isset($host['host'])) { if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $host['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for host name "%s".', $host['host'])); } if (isset($hostNames['host'][$host['host']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same host name "%s" already exists in data.', $host['host'])); } $hostNames['host'][$host['host']] = $update ? $host['hostid'] : 1; } if (isset($host['name'])) { if ($update) { // if visible name is empty replace it with host name if (zbx_empty(trim($host['name']))) { if (!isset($host['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Visible name cannot be empty if host name is missing.')); } $host['name'] = $host['host']; } } if (isset($hostNames['name'][$host['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same visible name "%s" already exists in data.', $host['name'])); } $hostNames['name'][$host['name']] = $update ? $host['hostid'] : 1; } } unset($host); if ($update || $create) { if (isset($hostNames['host']) || isset($hostNames['name'])) { $filter = array(); if (isset($hostNames['host'])) { $filter['host'] = array_keys($hostNames['host']); } if (isset($hostNames['name'])) { $filter['name'] = array_keys($hostNames['name']); } $options = array('output' => array('hostid', 'host', 'name'), 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true); $hostsExists = $this->get($options); foreach ($hostsExists as $hostExists) { if (isset($hostNames['host'][$hostExists['host']])) { if (!$update || bccomp($hostExists['hostid'], $hostNames['host'][$hostExists['host']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same name "%s" already exists.', $hostExists['host'])); } } if (isset($hostNames['name'][$hostExists['name']])) { if (!$update || bccomp($hostExists['hostid'], $hostNames['name'][$hostExists['name']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same visible name "%s" already exists.', $hostExists['name'])); } } } $templatesExists = API::Template()->get($options); foreach ($templatesExists as $templateExists) { if (isset($hostNames['host'][$templateExists['host']])) { if (!$update || bccomp($templateExists['templateid'], $hostNames['host'][$templateExists['host']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same name "%s" already exists.', $templateExists['host'])); } } if (isset($hostNames['name'][$templateExists['name']])) { if (!$update || bccomp($templateExists['templateid'], $hostNames['name'][$templateExists['name']]) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same visible name "%s" already exists.', $templateExists['name'])); } } } } } return $update ? $dbHosts : $hosts; }