zabbix-agent2/src/libs/zbxsysinfo/sysinfo.c
/******************************************************************************
 *                                                                            *
 * Function: finalize_key_access_rules_configuration                          *
 *                                                                            *
 * Purpose: validates key access rules configuration                          *
 *                                                                            *
 ******************************************************************************/
finalize_key_access_rules_configuration

Purpose: validates key access rules configuration
finalize_key_access_rules_configuration Purpose : validates key access rules configuration
void	finalize_key_access_rules_configuration(void)
{
	int			i, j, rules_num, sysrun_index = ZBX_MAX_UINT31_1;
	zbx_key_access_rule_t	*rule, *sysrun_deny;
	char			sysrun_pattern[] = "system.run[*]";
	rules_num = key_access_rules.values_num;
	if (NULL == (sysrun_deny = zbx_key_access_rule_create(sysrun_pattern, ZBX_KEY_ACCESS_DENY)))
	{
		THIS_SHOULD_NEVER_HAPPEN;
		exit(EXIT_FAILURE);
	}
	if (FAIL != (i = zbx_vector_ptr_search(&key_access_rules, sysrun_deny, compare_key_access_rules)))
	{
		rules_num--;
		sysrun_index = i;
	}
	if (0 != rules_num)
	{
		for (i = 0; i < key_access_rules.values_num; i++)
		{
			rule = (zbx_key_access_rule_t*)key_access_rules.values[i];
			if (1 == rule->elements.values_num && 0 == strcmp(rule->elements.values[0], "*"))
			{
				if (i < sysrun_index)
					sysrun_index = i;
				break;
			}
		}
		if (i != key_access_rules.values_num)
		{
			for (j = ++i; j < key_access_rules.values_num; j++)
			{
				rule = (zbx_key_access_rule_t*)key_access_rules.values[j];
				zabbix_log(LOG_LEVEL_WARNING, "removed unreachable %s \"%s\" rule",
						(ZBX_KEY_ACCESS_ALLOW == rule->type ? "AllowKey" : "DenyKey"),
						rule->pattern);
				zbx_key_access_rule_free(rule);
			}
			key_access_rules.values_num = i;
		}
		for (i = key_access_rules.values_num - 1; 0 <= i; i--)
		{
			rule = (zbx_key_access_rule_t*)key_access_rules.values[i];
			if (ZBX_KEY_ACCESS_ALLOW != rule->type)
				break;
			if (0 == rule->elements.values_num || 0 != strcmp(rule->elements.values[0], "system.run"))
			{
				if (i != sysrun_index)
				{
					zabbix_log(LOG_LEVEL_WARNING, "removed redundant trailing AllowKey \"%s\" rule",
							rule->pattern);
				}
				zbx_key_access_rule_free(rule);
				zbx_vector_ptr_remove(&key_access_rules, i);
			}
		}
		if (0 == key_access_rules.values_num)
		{
			zabbix_log(LOG_LEVEL_CRIT, "Item key access rules are configured to match all keys,"
					" indicating possible configuration problem. "
					" Please remove the rules if that was the purpose.");
			exit(EXIT_FAILURE);
		}
	}
	if (ZBX_MAX_UINT31_1 == sysrun_index)
		zbx_vector_ptr_append(&key_access_rules, sysrun_deny);
	else
		zbx_key_access_rule_free(sysrun_deny);
}
void finalize_key_access_rules_configuration ( void ) { int i , j , rules_num , sysrun_index = ZBX_MAX_UINT31_1 ; zbx_key_access_rule_t * rule , * sysrun_deny ; char sysrun_pattern [ ] = " " ; rules_num = key_access_rules . values_num ; if ( NULL == ( sysrun_deny = zbx_key_access_rule_create ( sysrun_pattern , ZBX_KEY_ACCESS_DENY ) ) ) { THIS_SHOULD_NEVER_HAPPEN ; exit ( EXIT_FAILURE ) ; } if ( FAIL != ( i = zbx_vector_ptr_search ( & key_access_rules , sysrun_deny , compare_key_access_rules ) ) ) { rules_num -- ; sysrun_index = i ; } if ( 0 != rules_num ) { for ( i = 0 ; i < key_access_rules . values_num ; i ++ ) { rule = ( zbx_key_access_rule_t * ) key_access_rules . values [ i ] ; if ( 1 == rule -> elements . values_num && 0 == strcmp ( rule -> elements . values [ 0 ] , " " ) ) { if ( i < sysrun_index ) sysrun_index = i ; break ; } } if ( i != key_access_rules . values_num ) { for ( j = ++ i ; j < key_access_rules . values_num ; j ++ ) { rule = ( zbx_key_access_rule_t * ) key_access_rules . values [ j ] ; zabbix_log ( LOG_LEVEL_WARNING , " \" \" " , ( ZBX_KEY_ACCESS_ALLOW == rule -> type ? " " : " " ) , rule -> pattern ) ; zbx_key_access_rule_free ( rule ) ; } key_access_rules . values_num = i ; } for ( i = key_access_rules . values_num - 1 ; 0 <= i ; i -- ) { rule = ( zbx_key_access_rule_t * ) key_access_rules . values [ i ] ; if ( ZBX_KEY_ACCESS_ALLOW != rule -> type ) break ; if ( 0 == rule -> elements . values_num || 0 != strcmp ( rule -> elements . values [ 0 ] , " " ) ) { if ( i != sysrun_index ) { zabbix_log ( LOG_LEVEL_WARNING , " \" \" " , rule -> pattern ) ; } zbx_key_access_rule_free ( rule ) ; zbx_vector_ptr_remove ( & key_access_rules , i ) ; } } if ( 0 == key_access_rules . values_num ) { zabbix_log ( LOG_LEVEL_CRIT , " " " " " " ) ; exit ( EXIT_FAILURE ) ; } } if ( ZBX_MAX_UINT31_1 == sysrun_index ) zbx_vector_ptr_append ( & key_access_rules , sysrun_deny ) ; else zbx_key_access_rule_free ( sysrun_deny ) ; }
Function: finalize_key_access_rules_configuration                          
                                                                            
 Purpose: validates key access rules configuration
Function : finalize_key_access_rules_configuration Purpose : validates key access rules configuration
/* prepare default system.run[*] deny rule to be added at the end */ /* exclude system.run[*] from total number of rules */ /* sysrun_index points at the first rule matching system.run[*] */ /* throw out all rules after '*', because they would never match */ /* 'match all' rule also matches system.run[*] */ /* trailing AllowKey rules are meaningless, because AllowKey=* is default behavior, */ /* system.run allow rules are not redundant because of default system.run[*] deny rule */