1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 #include "alias.h"
22 #include "sysinfo.h"
23 #include "log.h"
24 
25 static ALIAS	*aliasList = NULL;
26 
test_aliases(void)27 void	test_aliases(void)
28 {
29 	ALIAS	*alias;
30 
31 	for (alias = aliasList; NULL != alias; alias = alias->next)
32 		test_parameter(alias->name);
33 }
34 
add_alias(const char * name,const char * value)35 void	add_alias(const char *name, const char *value)
36 {
37 	ALIAS	*alias = NULL;
38 
39 	for (alias = aliasList; ; alias = alias->next)
40 	{
41 		/* add new Alias */
42 		if (NULL == alias)
43 		{
44 			alias = (ALIAS *)zbx_malloc(alias, sizeof(ALIAS));
45 
46 			alias->name = strdup(name);
47 			alias->value = strdup(value);
48 			alias->next = aliasList;
49 			aliasList = alias;
50 
51 			zabbix_log(LOG_LEVEL_DEBUG, "Alias added: \"%s\" -> \"%s\"", name, value);
52 			break;
53 		}
54 
55 		/* treat duplicate Alias as error */
56 		if (0 == strcmp(alias->name, name))
57 		{
58 			zabbix_log(LOG_LEVEL_CRIT, "failed to add Alias \"%s\": duplicate name", name);
59 			exit(EXIT_FAILURE);
60 		}
61 	}
62 }
63 
alias_list_free(void)64 void	alias_list_free(void)
65 {
66 	ALIAS	*curr, *next;
67 
68 	next = aliasList;
69 
70 	while (NULL != next)
71 	{
72 		curr = next;
73 		next = curr->next;
74 		zbx_free(curr->value);
75 		zbx_free(curr->name);
76 		zbx_free(curr);
77 	}
78 
79 	aliasList = NULL;
80 }
81 
zbx_alias_get(const char * orig)82 const char	*zbx_alias_get(const char *orig)
83 {
84 	ALIAS				*alias;
85 	size_t				len_name, len_value;
86 	ZBX_THREAD_LOCAL static char	*buffer = NULL;
87 	ZBX_THREAD_LOCAL static size_t	buffer_alloc = 0;
88 	size_t				buffer_offset = 0;
89 	const char			*p = orig;
90 
91 	if (SUCCEED != parse_key(&p) || '\0' != *p)
92 		return orig;
93 
94 	for (alias = aliasList; NULL != alias; alias = alias->next)
95 	{
96 		if (0 == strcmp(alias->name, orig))
97 			return alias->value;
98 	}
99 
100 	for (alias = aliasList; NULL != alias; alias = alias->next)
101 	{
102 		len_name = strlen(alias->name);
103 		if (3 >= len_name || 0 != strcmp(alias->name + len_name - 3, "[*]"))
104 			continue;
105 
106 		if (0 != strncmp(alias->name, orig, len_name - 2))
107 			continue;
108 
109 		len_value = strlen(alias->value);
110 		if (3 >= len_value || 0 != strcmp(alias->value + len_value - 3, "[*]"))
111 			return alias->value;
112 
113 		zbx_strncpy_alloc(&buffer, &buffer_alloc, &buffer_offset, alias->value, len_value - 3);
114 		zbx_strcpy_alloc(&buffer, &buffer_alloc, &buffer_offset, orig + len_name - 3);
115 		return buffer;
116 	}
117 
118 	return orig;
119 }
120