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 "zbxconf.h"
22 
23 #include "cfg.h"
24 #include "log.h"
25 #include "alias.h"
26 #include "sysinfo.h"
27 #ifdef _WINDOWS
28 #	include "perfstat.h"
29 #endif
30 #include "comms.h"
31 
32 /******************************************************************************
33  *                                                                            *
34  * Function: load_aliases                                                     *
35  *                                                                            *
36  * Purpose: load aliases from configuration                                   *
37  *                                                                            *
38  * Parameters: lines - aliase entries from configuration file                 *
39  *                                                                            *
40  * Comments: calls add_alias() for each entry                                 *
41  *                                                                            *
42  ******************************************************************************/
load_aliases(char ** lines)43 void	load_aliases(char **lines)
44 {
45 	char	**pline;
46 
47 	for (pline = lines; NULL != *pline; pline++)
48 	{
49 		char		*c;
50 		const char	*r = *pline;
51 
52 		if (SUCCEED != parse_key(&r) || ':' != *r)
53 		{
54 			zabbix_log(LOG_LEVEL_CRIT, "cannot add alias \"%s\": invalid character at position %d",
55 					*pline, (int)((r - *pline) + 1));
56 			exit(EXIT_FAILURE);
57 		}
58 
59 		c = (char *)r++;
60 
61 		if (SUCCEED != parse_key(&r) || '\0' != *r)
62 		{
63 			zabbix_log(LOG_LEVEL_CRIT, "cannot add alias \"%s\": invalid character at position %d",
64 					*pline, (int)((r - *pline) + 1));
65 			exit(EXIT_FAILURE);
66 		}
67 
68 		*c++ = '\0';
69 
70 		add_alias(*pline, c);
71 
72 		*--c = ':';
73 	}
74 }
75 
76 /******************************************************************************
77  *                                                                            *
78  * Function: load_user_parameters                                             *
79  *                                                                            *
80  * Purpose: load user parameters from configuration                           *
81  *                                                                            *
82  * Parameters: lines - user parameter entries from configuration file         *
83  *                                                                            *
84  * Author: Vladimir Levijev                                                   *
85  *                                                                            *
86  * Comments: calls add_user_parameter() for each entry                        *
87  *                                                                            *
88  ******************************************************************************/
load_user_parameters(char ** lines)89 void	load_user_parameters(char **lines)
90 {
91 	char	*p, **pline, error[MAX_STRING_LEN];
92 
93 	for (pline = lines; NULL != *pline; pline++)
94 	{
95 		if (NULL == (p = strchr(*pline, ',')))
96 		{
97 			zabbix_log(LOG_LEVEL_CRIT, "cannot add user parameter \"%s\": not comma-separated", *pline);
98 			exit(EXIT_FAILURE);
99 		}
100 		*p = '\0';
101 
102 		if (FAIL == add_user_parameter(*pline, p + 1, error, sizeof(error)))
103 		{
104 			*p = ',';
105 			zabbix_log(LOG_LEVEL_CRIT, "cannot add user parameter \"%s\": %s", *pline, error);
106 			exit(EXIT_FAILURE);
107 		}
108 		*p = ',';
109 	}
110 }
111 
112 /******************************************************************************
113  *                                                                            *
114  * Function: load_key_access_rule                                             *
115  *                                                                            *
116  * Purpose: Adds key access rule from configuration                           *
117  *                                                                            *
118  * Parameters: value - [IN] key access rule parameter value                   *
119  *             cfg   - [IN] configuration parameter information               *
120  *                                                                            *
121  * Return value: SUCCEED - successful execution                               *
122  *               FAIL    - failed to add rule                                 *
123  *                                                                            *
124  * Author: Andrejs Tumilovics                                                 *
125  *                                                                            *
126  ******************************************************************************/
load_key_access_rule(const char * value,const struct cfg_line * cfg)127 int	load_key_access_rule(const char *value, const struct cfg_line *cfg)
128 {
129 	unsigned char	rule_type;
130 
131 	if (0 == strcmp(cfg->parameter, "AllowKey"))
132 		rule_type = ZBX_KEY_ACCESS_ALLOW;
133 	else if (0 == strcmp(cfg->parameter, "DenyKey"))
134 		rule_type = ZBX_KEY_ACCESS_DENY;
135 	else
136 		return FAIL;
137 
138 	return add_key_access_rule(cfg->parameter, (char *)value, rule_type);
139 }
140 
141 #ifdef _WINDOWS
142 /******************************************************************************
143  *                                                                            *
144  * Function: load_perf_counters                                               *
145  *                                                                            *
146  * Purpose: load performance counters from configuration                      *
147  *                                                                            *
148  * Parameters: def_lines - array of PerfCounter configuration entries         *
149  *             eng_lines - array of PerfCounterEn configuration entries       *
150  *                                                                            *
151  * Return value:                                                              *
152  *                                                                            *
153  * Author: Vladimir Levijev                                                   *
154  *                                                                            *
155  * Comments:                                                                  *
156  *                                                                            *
157  ******************************************************************************/
load_perf_counters(const char ** def_lines,const char ** eng_lines)158 void	load_perf_counters(const char **def_lines, const char **eng_lines)
159 {
160 	char		name[MAX_STRING_LEN], counterpath[PDH_MAX_COUNTER_PATH], interval[8];
161 	const char	**pline, **lines;
162 	char		*error = NULL;
163 	LPTSTR		wcounterPath;
164 	int		period;
165 
166 	for (lines = def_lines;; lines = eng_lines)
167 	{
168 		zbx_perf_counter_lang_t lang = (lines == def_lines) ? PERF_COUNTER_LANG_DEFAULT : PERF_COUNTER_LANG_EN;
169 
170 		for (pline = lines; NULL != *pline; pline++)
171 		{
172 			if (3 < num_param(*pline))
173 			{
174 				error = zbx_strdup(error, "Required parameter missing.");
175 				goto pc_fail;
176 			}
177 
178 			if (0 != get_param(*pline, 1, name, sizeof(name), NULL))
179 			{
180 				error = zbx_strdup(error, "Cannot parse key.");
181 				goto pc_fail;
182 			}
183 
184 			if (0 != get_param(*pline, 2, counterpath, sizeof(counterpath), NULL))
185 			{
186 				error = zbx_strdup(error, "Cannot parse counter path.");
187 				goto pc_fail;
188 			}
189 
190 			if (0 != get_param(*pline, 3, interval, sizeof(interval), NULL))
191 			{
192 				error = zbx_strdup(error, "Cannot parse interval.");
193 				goto pc_fail;
194 			}
195 
196 			wcounterPath = zbx_acp_to_unicode(counterpath);
197 			zbx_unicode_to_utf8_static(wcounterPath, counterpath, PDH_MAX_COUNTER_PATH);
198 			zbx_free(wcounterPath);
199 
200 			if (FAIL == check_counter_path(counterpath, lang == PERF_COUNTER_LANG_DEFAULT))
201 			{
202 				error = zbx_strdup(error, "Invalid counter path.");
203 				goto pc_fail;
204 			}
205 
206 			period = atoi(interval);
207 
208 			if (1 > period || MAX_COLLECTOR_PERIOD < period)
209 			{
210 				error = zbx_strdup(NULL, "Interval out of range.");
211 				goto pc_fail;
212 			}
213 
214 			if (NULL == add_perf_counter(name, counterpath, period, lang, &error))
215 			{
216 				if (NULL == error)
217 					error = zbx_strdup(error, "Failed to add new performance counter.");
218 				goto pc_fail;
219 			}
220 
221 			continue;
222 	pc_fail:
223 			zabbix_log(LOG_LEVEL_CRIT, "cannot add performance counter \"%s\": %s", *pline, error);
224 			zbx_free(error);
225 
226 			exit(EXIT_FAILURE);
227 		}
228 
229 		if (lines == eng_lines)
230 			break;
231 	}
232 }
233 #endif	/* _WINDOWS */
234 
235 #ifdef _AIX
tl_version(void)236 void	tl_version(void)
237 {
238 #ifdef _AIXVERSION_610
239 #	define ZBX_AIX_TL	"6100 and above"
240 #elif _AIXVERSION_530
241 #	ifdef HAVE_AIXOSLEVEL_530006
242 #		define ZBX_AIX_TL	"5300-06 and above"
243 #	else
244 #		define ZBX_AIX_TL	"5300-00,01,02,03,04,05"
245 #	endif
246 #elif _AIXVERSION_520
247 #	define ZBX_AIX_TL	"5200"
248 #elif _AIXVERSION_510
249 #	define ZBX_AIX_TL	"5100"
250 #endif
251 #ifdef ZBX_AIX_TL
252 	printf("Supported technology levels: %s\n", ZBX_AIX_TL);
253 #endif /* ZBX_AIX_TL */
254 #undef ZBX_AIX_TL
255 }
256 #endif /* _AIX */
257