1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
6// Please see http://pandorafms.org for full contribution list
7
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation for version 2.
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16require_once ('include/functions_network_components.php');
17enterprise_include_once('include/functions_local_components.php');
18
19if (is_ajax ()) {
20	$get_network_component = (bool) get_parameter ('get_network_component');
21	$snmp_walk = (bool) get_parameter ('snmp_walk');
22	$get_module_component = (bool) get_parameter ('get_module_component');
23	$get_module_components = (bool) get_parameter ('get_module_components');
24	$get_module_local_components = (bool) get_parameter('get_module_local_components');
25	$get_module_local_component = (bool) get_parameter('get_module_local_component');
26
27	if ($get_module_component) {
28		$id_component = (int) get_parameter ('id_module_component');
29
30		$component = db_get_row ('tnetwork_component', 'id_nc', $id_component);
31
32		$component['throw_unknown_events'] =
33			!network_components_is_disable_type_event($id_component, EVENTS_GOING_UNKNOWN);
34
35		// Decrypt passwords in the component.
36		$component['plugin_pass'] = io_output_password($component['plugin_pass']);
37
38		echo io_json_mb_encode ($component);
39		return;
40	}
41
42	if ($get_module_components) {
43		require_once ('include/functions_modules.php');
44		$id_module_group = (int) get_parameter ('id_module_component_group');
45		$id_module_component = (int) get_parameter ('id_module_component_type');
46
47		$components = network_components_get_network_components ($id_module_component,
48			array ('id_group' => $id_module_group,
49				'order' => 'name ASC'),
50			array ('id_nc', 'name'));
51
52		echo io_json_mb_encode ($components);
53		return;
54	}
55
56	if ($get_module_local_components) {
57		require_once ($config['homedir'] . '/' . ENTERPRISE_DIR .
58			'/include/functions_local_components.php');
59
60		$id_module_group = (int) get_parameter ('id_module_component_group');
61		$localComponents = local_components_get_local_components(
62			array('id_network_component_group' => $id_module_group),
63			array('id', 'name'));
64
65		echo io_json_mb_encode($localComponents);
66		return;
67	}
68
69	if ($get_module_local_component) {
70		$id_component = (int) get_parameter ('id_module_component');
71
72		$component = db_get_row ('tlocal_component', 'id', $id_component);
73		foreach ($component as $index => $element) {
74			$component[$index] = html_entity_decode($element, ENT_QUOTES, "UTF-8");
75		}
76
77		$typeName = local_components_parse_module_extract_value('module_type',$component['data']);
78
79		switch ($config["dbtype"]) {
80			case "mysql":
81				$component['type'] = db_get_value_sql('
82					SELECT id_tipo
83					FROM ttipo_modulo
84					WHERE nombre LIKE "' . $typeName . '"');
85				break;
86			case "postgresql":
87			case "oracle":
88				$component['type'] = db_get_value_sql('
89					SELECT id_tipo
90					FROM ttipo_modulo
91					WHERE nombre LIKE \'' . $typeName . '\'');
92				break;
93		}
94
95		$component['throw_unknown_events'] =
96			!local_components_is_disable_type_event($id_component, EVENTS_GOING_UNKNOWN);
97
98		echo io_json_mb_encode ($component);
99		return;
100	}
101
102	if ($snmp_walk) {
103		$test_ip_type = get_parameter ('ip_target');
104		if (is_array($test_ip_type))
105			$ip_target = (string)array_shift($test_ip_type);
106		else
107			$ip_target = (string) get_parameter ('ip_target');
108		$test_snmp_community = get_parameter ('snmp_community');
109		if (is_array($test_snmp_community))
110			$snmp_community = (string)array_shift($test_snmp_community);
111		else
112			$snmp_community = (string) get_parameter ('snmp_community');
113		$snmp_version = get_parameter('snmp_version');
114		$snmp3_auth_user = get_parameter('snmp3_auth_user');
115		$snmp3_security_level = get_parameter('snmp3_security_level');
116		$snmp3_auth_method = get_parameter('snmp3_auth_method');
117		$snmp3_auth_pass = get_parameter('snmp3_auth_pass');
118		$snmp3_privacy_method = get_parameter('snmp3_privacy_method');
119		$snmp3_privacy_pass = get_parameter('snmp3_privacy_pass');
120		$snmp_port = get_parameter('snmp_port');
121
122		$snmpwalk = get_snmpwalk($ip_target, $snmp_version, $snmp_community,
123			$snmp3_auth_user, $snmp3_security_level, $snmp3_auth_method,
124			$snmp3_auth_pass, $snmp3_privacy_method, $snmp3_privacy_pass,
125			1, "", $snmp_port);
126
127		if ($snmpwalk === false) {
128			echo io_json_mb_encode ($snmpwalk);
129			return;
130		}
131
132		$result = array ();
133		foreach ($snmpwalk as $id => $value) {
134			$value = substr ($id, 0, 35)." - ".substr ($value, 0, 20);
135			$result[$id] = substr ($value, 0, 55);
136		}
137		asort ($result);
138		echo io_json_mb_encode ($result);
139		return;
140	}
141
142	return;
143}
144
145require_once ("include/functions_exportserver.php");
146require_once($config['homedir'] . "/include/functions_modules.php");
147require_once($config['homedir'] . "/include/functions_agents.php");
148
149// Reading a module
150if ($id_agent_module) {
151	$module = modules_get_agentmodule ($id_agent_module);
152	$moduletype = $module['id_modulo'];
153	$name = $module['nombre'];
154	$description = $module['descripcion'];
155	$id_module_group = $module['id_module_group'];
156	$id_module_type = $module['id_tipo_modulo'];
157	$max = $module['max'];
158	$min = $module['min'];
159	$interval = $module['module_interval'];
160	if ($interval == 0) {
161		$interval = agents_get_interval ($id_agente);
162	}
163	$ff_interval = $module['module_ff_interval'];
164	$quiet_module = $module['quiet'];
165	$unit = $module['unit'];
166	$tcp_port = $module['tcp_port'];
167	$tcp_send = $module['tcp_send'];
168	$tcp_rcv = $module['tcp_rcv'];
169	$snmp_community = $module['snmp_community'];
170	$snmp_oid = $module['snmp_oid'];
171
172	// New support for snmp v3
173	$snmp_version = $module['tcp_send'];
174	$snmp3_auth_user = $module["plugin_user"];
175	$snmp3_auth_pass = io_output_password($module["plugin_pass"]);
176
177	// Auth method could be MD5 or SHA
178	$snmp3_auth_method = $module["plugin_parameter"];
179
180	// Privacy method could be DES or AES
181	$snmp3_privacy_method = $module["custom_string_1"];
182	$snmp3_privacy_pass = io_output_password($module["custom_string_2"]);
183
184	// Security level Could be noAuthNoPriv | authNoPriv | authPriv
185	$snmp3_security_level = $module["custom_string_3"];
186
187	$ip_target = $module['ip_target'];
188	$disabled = $module['disabled'];
189	$id_export = $module['id_export'];
190	$plugin_user = $module['plugin_user'];
191	$plugin_pass = io_output_password($module['plugin_pass']);
192	$plugin_parameter = $module['plugin_parameter'];
193	$id_plugin = $module['id_plugin'];
194	$post_process = $module['post_process'];
195	$prediction_module = $module['prediction_module'];
196	$custom_integer_2 = $module ['custom_integer_2'];
197	$max_timeout = $module['max_timeout'];
198	$max_retries = $module['max_retries'];
199	$custom_id = $module['custom_id'];
200	$history_data = $module['history_data'];
201	$min_warning = $module['min_warning'];
202	$max_warning = $module['max_warning'];
203	$str_warning = $module['str_warning'];
204	$min_critical = $module['min_critical'];
205	$max_critical = $module['max_critical'];
206	$str_critical = $module['str_critical'];
207	$ff_event = $module['min_ff_event'];
208	$ff_event_normal = $module['min_ff_event_normal'];
209	$ff_event_warning = $module['min_ff_event_warning'];
210	$ff_event_critical = $module['min_ff_event_critical'];
211	$each_ff = $module['each_ff'];
212	$ff_timeout = $module['ff_timeout'];
213	// Select tag info.
214	$id_tag = tags_get_module_tags ($id_agent_module);
215
216	$critical_instructions = $module['critical_instructions'];
217	$warning_instructions = $module['warning_instructions'];
218	$unknown_instructions = $module['unknown_instructions'];
219
220	$critical_inverse = $module['critical_inverse'];
221	$warning_inverse = $module['warning_inverse'];
222
223	$id_category = $module['id_category'];
224
225	$cron_interval = explode (" ", $module['cron_interval']);
226	if (isset ($cron_interval[4])) {
227		$minute = $cron_interval[0];
228		$hour = $cron_interval[1];
229		$mday = $cron_interval[2];
230		$month = $cron_interval[3];
231		$wday = $cron_interval[4];
232	}
233	else {
234		$minute = '*';
235		$hour = '*';
236		$mday = '*';
237		$month = '*';
238		$wday = '*';
239	}
240
241	$module_macros = null;
242	if (isset($module['module_macros'])) {
243		$module_macros = json_decode(base64_decode($module['module_macros']), true);
244	}
245}
246else {
247	if (!isset ($moduletype)) {
248		$moduletype = (string) get_parameter ('moduletype');
249
250		// Clean up specific network modules fields
251		$name = '';
252		$description = '';
253		$id_module_group = 1;
254		$id_module_type = 1;
255		$post_process = '';
256		$max_timeout = 0;
257		$max_retries = 0;
258		$min = '';
259		$max = '';
260		$interval = '';
261		$quiet_module = 0;
262		$unit = '';
263		$prediction_module = '';
264		$custom_integer_2 = 0;
265		$id_plugin = '';
266		$id_export = '';
267		$disabled = "0";
268		$tcp_send = '';
269		$tcp_rcv = '';
270		$tcp_port = '';
271
272		if ($moduletype == "wmiserver")
273			$snmp_community = '';
274		else
275			$snmp_community = "public";
276		$snmp_oid = '';
277		$ip_target = agents_get_address ($id_agente);
278		$plugin_user = '';
279		$plugin_pass = '';
280		$plugin_parameter = '';
281		$custom_id = '';
282		$history_data = 1;
283		$min_warning = 0;
284		$max_warning = 0;
285		$str_warning = '';
286		$min_critical = 0;
287		$max_critical = 0;
288		$str_critical = '';
289		$ff_event = 0;
290
291		// New support for snmp v3
292		$snmp_version = 1;
293		$snmp3_auth_user = "";
294		$snmp3_auth_pass = "";
295		$snmp3_auth_method = "";
296		$snmp3_privacy_method = "";
297		$snmp3_privacy_pass = "";
298		$snmp3_security_level = "";
299
300		$critical_instructions = '';
301		$warning_instructions = '';
302		$unknown_instructions = '';
303
304		$critical_inverse = '';
305		$warning_inverse = '';
306
307		$each_ff = 0;
308		$ff_event_normal = '';
309		$ff_event_warning = '';
310		$ff_event_critical = '';
311
312		$id_category = 0;
313
314		$cron_interval = '* * * * *';
315		$hour = '*';
316		$minute = '*';
317		$mday = '*';
318		$month = '*';
319		$wday = '*';
320
321		$ff_interval = 0;
322
323		$ff_timeout = 0;
324
325		$module_macros = array ();
326	}
327}
328
329$is_function_policies = enterprise_include_once('include/functions_policies.php');
330
331if ($is_function_policies !== ENTERPRISE_NOT_HOOK) {
332	$relink_policy = get_parameter('relink_policy', 0);
333	$unlink_policy = get_parameter('unlink_policy', 0);
334
335	if ($relink_policy) {
336		$policy_info = policies_info_module_policy($id_agent_module);
337		$policy_id = $policy_info['id_policy'];
338
339		if ($relink_policy && policies_get_policy_queue_status ($policy_id) == STATUS_IN_QUEUE_APPLYING) {
340			ui_print_error_message(__('This policy is applying and cannot be modified'));
341		}
342		else {
343			$result = policies_relink_module($id_agent_module);
344			ui_print_result_message($result, __('Module will be linked in the next application'));
345
346			db_pandora_audit("Agent management", "Re-link module " . $id_agent_module);
347		}
348	}
349
350	if ($unlink_policy) {
351		$result = policies_unlink_module($id_agent_module);
352		ui_print_result_message($result, __('Module will be unlinked in the next application'));
353
354		db_pandora_audit("Agent management", "Unlink module " . $id_agent_module);
355	}
356
357}
358global $__code_from;
359$__code_from = 'modules';
360$remote_conf = false;
361
362if ($__code_from !== 'policies') {
363	//Only check in the module editor.
364
365	//Check ACL tags
366	$tag_acl = true;
367
368	// If edit a existing module.
369	if (!empty($id_agent_module))
370		$tag_acl = tags_check_acl_by_module($id_agent_module);
371
372	if (!$tag_acl) {
373		db_pandora_audit("ACL Violation",
374			"Trying to access agent manager");
375		require ("general/noaccess.php");
376		return;
377	}
378}
379
380
381switch ($moduletype) {
382	case "dataserver":
383	case MODULE_DATA:
384		$moduletype = MODULE_DATA;
385		// Has remote configuration ?
386		$remote_conf = false;
387		if (enterprise_installed()) {
388			enterprise_include_once('include/functions_config_agents.php');
389			$remote_conf = config_agents_has_remote_configuration($id_agente);
390		}
391
392		/* Categories is an array containing the allowed module types
393		 (generic_data, generic_string, etc) from ttipo_modulo (field categoria) */
394		$categories = array (0, 1, 2, 6, 7, 8, -1);
395		require ('module_manager_editor_common.php');
396		require ('module_manager_editor_data.php');
397		if ($config['enterprise_installed'] && $remote_conf) {
398			if($id_agent_module) {
399				enterprise_include_once('include/functions_config_agents.php');
400				$configuration_data = enterprise_hook('config_agents_get_module_from_conf',
401					array($id_agente, io_safe_output(modules_get_agentmodule_name($id_agent_module))));
402			}
403			enterprise_include ('godmode/agentes/module_manager_editor_data.php');
404		}
405		break;
406	case "networkserver":
407	case MODULE_NETWORK:
408		$moduletype = MODULE_NETWORK;
409		$categories = array (3, 4, 5);
410		require ('module_manager_editor_common.php');
411		require ('module_manager_editor_network.php');
412		break;
413	case "pluginserver":
414	case MODULE_PLUGIN:
415		$moduletype = MODULE_PLUGIN;
416
417		$categories = array (0, 1, 2);
418		require ('module_manager_editor_common.php');
419		require ('module_manager_editor_plugin.php');
420		break;
421	case "predictionserver":
422	case MODULE_PREDICTION:
423		$moduletype = MODULE_PREDICTION;
424
425		$categories = array (0, 1);
426		require ('module_manager_editor_common.php');
427		require ('module_manager_editor_prediction.php');
428		break;
429	case "wmiserver":
430	case MODULE_WMI:
431		$moduletype = MODULE_WMI;
432
433		$categories = array (0, 1, 2);
434		require ('module_manager_editor_common.php');
435		require ('module_manager_editor_wmi.php');
436		break;
437	/* WARNING: type 7 is reserved on enterprise */
438	default:
439		if (enterprise_include ('godmode/agentes/module_manager_editor.php') === ENTERPRISE_NOT_HOOK) {
440			ui_print_error_message(sprintf(__('DEBUG: Invalid module type specified in %s:%s'), __FILE__, __LINE__));
441			echo __('Most likely you have recently upgraded from an earlier version of Pandora and either <br />
442				1) forgot to use the database converter<br />
443				2) used a bad version of the database converter (see Bugreport #2124706 for the solution)<br />
444				3) found a new bug - please report a way to duplicate this error');
445
446			return;
447		}
448		break;
449}
450
451
452if ($config['enterprise_installed'] && $id_agent_module) {
453	if (policies_is_module_in_policy($id_agent_module)) {
454		policies_add_policy_linkation($id_agent_module);
455	}
456}
457
458echo '<h3 id="message" class="error invisible"></h3>';
459
460// TODO: Change to the ui_print_error system
461echo '<form method="post" id="module_form">';
462html_print_table ($table_simple);
463
464ui_toggle(html_print_table ($table_advanced, true),
465	__('Advanced options'));
466ui_toggle(html_print_table ($table_macros, true),
467	__('Custom macros') . ui_print_help_icon ('module_macros', true));
468ui_toggle(html_print_table ($table_new_relations, true) .
469	html_print_table ($table_relations, true), __('Module relations'));
470
471
472// Submit
473echo '<div class="action-buttons" style="width: '.$table_simple->width.'">';
474if ($id_agent_module) {
475	html_print_submit_button(__('Update'), 'updbutton', false,
476		'class="sub upd"');
477	html_print_input_hidden('update_module', 1);
478	html_print_input_hidden('id_agent_module', $id_agent_module);
479	html_print_input_hidden('id_module_type', $id_module_type);
480
481	if ($config['enterprise_installed'] && $remote_conf) {
482		?>
483		<script type="text/javascript">
484		var check_remote_conf = true;
485		</script>
486		<?php
487	}
488}
489else {
490	html_print_submit_button (__('Create'), 'crtbutton', false,
491		'class="sub wand"');
492	html_print_input_hidden ('id_module', $moduletype);
493	html_print_input_hidden ('create_module', 1);
494
495	if ($config['enterprise_installed'] && $remote_conf) {
496		?>
497		<script type="text/javascript">
498		var check_remote_conf = true;
499		</script>
500		<?php
501	}
502}
503echo '</div>';
504echo '</form>';
505
506ui_require_jquery_file ('ui');
507ui_require_jquery_file ('form');
508ui_require_jquery_file ('pandora');
509ui_require_jquery_file ('pandora.controls');
510
511ui_require_javascript_file ('pandora_modules');
512?>
513<script language="javascript">
514/* <![CDATA[ */
515var no_name_lang = "<?php echo __('No module name provided') ?>";
516var no_target_lang = "<?php echo __('No target IP provided') ?>";
517var no_oid_lang = "<?php echo __('No SNMP OID provided') ?>";
518var no_prediction_module_lang = "<?php echo __('No module to predict') ?>";
519var no_plugin_lang = "<?php echo __('No plug-in provided') ?>";
520
521$(document).ready (function () {
522	configure_modules_form ();
523
524	$("#module_form").submit(function() {
525		if (typeof(check_remote_conf) != 'undefined') {
526			if (check_remote_conf) {
527				//Check the name
528				name = $("#text-name").val();
529				remote_config = $("#textarea_configuration_data").val();
530
531				regexp_name = new RegExp('module_name\\s*' + name.replace(/([^0-9A-Za-z_])/g, "\\$1") +"\n");
532
533				regexp_plugin = new RegExp('^module_plugin\\s*');
534
535				if (remote_config == '' || remote_config.match(regexp_name) ||
536					remote_config.match(regexp_plugin) ||
537					$("#id_module_type").val()==100 ||
538					$("#hidden-id_module_type_hidden").val()==100) {
539					return true;
540				}
541				else {
542					alert("<?php echo __("Error, The field name and name in module_name in data configuration are different.");?>");
543					return false;
544				}
545			}
546		}
547
548		return true;
549	});
550
551	function checkKeepaliveModule() {
552		// keepalive modules have id = 100
553		if ($("#id_module_type").val()==100 ||
554			$("#hidden-id_module_type_hidden").val()==100) {
555			$("#simple-configuration_data").hide();
556		}
557		else {
558			// If exists macros dont show configuration data because
559			// this visibility is controled by a form button
560			if($('#hidden-macros').val() == '') {
561				$("#simple-configuration_data").show();
562			}
563		}
564
565	}
566
567	checkKeepaliveModule();
568
569	$("#id_module_type").change (function () {
570		checkKeepaliveModule();
571	});
572});
573/* ]]> */
574</script>
575