Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * services_snmp.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-services-snmp
30
##|*NAME=Services: SNMP
31
##|*DESCR=Allow access to the 'Services: SNMP' page.
32
##|*MATCH=services_snmp.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37

    
38
if (!is_array($config['snmpd'])) {
39
	$config['snmpd'] = array();
40
	$config['snmpd']['rocommunity'] = "public";
41
	$config['snmpd']['pollport'] = "161";
42
}
43

    
44
if (!is_array($config['snmpd']['modules'])) {
45
	$config['snmpd']['modules'] = array();
46
	$config['snmpd']['modules']['mibii'] = true;
47
	$config['snmpd']['modules']['netgraph'] = true;
48
	$config['snmpd']['modules']['pf'] = true;
49
	$config['snmpd']['modules']['hostres'] = true;
50
	$config['snmpd']['modules']['bridge'] = true;
51
	$config['snmpd']['modules']['ucd'] = true;
52
	$config['snmpd']['modules']['regex'] = true;
53
}
54

    
55
$pconfig['enable'] = isset($config['snmpd']['enable']);
56
$pconfig['pollport'] = $config['snmpd']['pollport'];
57
$pconfig['syslocation'] = $config['snmpd']['syslocation'];
58
$pconfig['syscontact'] = $config['snmpd']['syscontact'];
59
$pconfig['rocommunity'] = $config['snmpd']['rocommunity'];
60
/* disabled until some docs show up on what this does.
61
$pconfig['rwenable'] = isset($config['snmpd']['rwenable']);
62
$pconfig['rwcommunity'] = $config['snmpd']['rwcommunity'];
63
*/
64
$pconfig['trapenable'] = isset($config['snmpd']['trapenable']);
65
$pconfig['trapserver'] = $config['snmpd']['trapserver'];
66
$pconfig['trapserverport'] = $config['snmpd']['trapserverport'];
67
$pconfig['trapstring'] = $config['snmpd']['trapstring'];
68

    
69
$pconfig['mibii'] = isset($config['snmpd']['modules']['mibii']);
70
$pconfig['netgraph'] = isset($config['snmpd']['modules']['netgraph']);
71
$pconfig['pf'] = isset($config['snmpd']['modules']['pf']);
72
$pconfig['hostres'] = isset($config['snmpd']['modules']['hostres']);
73
$pconfig['bridge'] = isset($config['snmpd']['modules']['bridge']);
74
$pconfig['ucd'] = isset($config['snmpd']['modules']['ucd']);
75
$pconfig['regex'] = isset($config['snmpd']['modules']['regex']);
76
if (empty($config['snmpd']['bindip'])) {
77
	$pconfig['bindip'] = array();
78
} else {
79
	$pconfig['bindip'] = explode(",", $config['snmpd']['bindip']);
80
}
81

    
82
if ($_POST) {
83

    
84
	unset($input_errors);
85
	$pconfig = $_POST;
86

    
87
	/* input validation */
88
	if ($_POST['enable']) {
89
		if (strstr($_POST['syslocation'], "#")) {
90
			$input_errors[] = gettext("Invalid character '#' in system location");
91
		}
92
		if (strstr($_POST['syscontact'], "#")) {
93
			$input_errors[] = gettext("Invalid character '#' in system contact");
94
		}
95
		if (strstr($_POST['rocommunity'], "#")) {
96
			$input_errors[] = gettext("Invalid character '#' in read community string");
97
		}
98

    
99
		$reqdfields = explode(" ", "rocommunity");
100
		$reqdfieldsn = array(gettext("Community"));
101
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
102

    
103
		$reqdfields = explode(" ", "pollport");
104
		$reqdfieldsn = array(gettext("Polling Port"));
105
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
106

    
107

    
108
	}
109

    
110
	if ($_POST['trapenable']) {
111
		if (strstr($_POST['trapstring'], "#")) {
112
			$input_errors[] = gettext("Invalid character '#' in SNMP trap string");
113
		}
114

    
115
		$reqdfields = explode(" ", "trapserver");
116
		$reqdfieldsn = array(gettext("Trap server"));
117
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
118

    
119
		$reqdfields = explode(" ", "trapserverport");
120
		$reqdfieldsn = array(gettext("Trap server port"));
121
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
122

    
123
		$reqdfields = explode(" ", "trapstring");
124
		$reqdfieldsn = array(gettext("Trap string"));
125
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
126
	}
127

    
128
/* disabled until some docs show up on what this does.
129
	if ($_POST['rwenable']) {
130
		$reqdfields = explode(" ", "rwcommunity");
131
		$reqdfieldsn = explode(",", "Write community string");
132
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
133
	}
134
*/
135

    
136

    
137

    
138
	if (!$input_errors) {
139
		$config['snmpd']['enable'] = $_POST['enable'] ? true : false;
140
		$config['snmpd']['pollport'] = $_POST['pollport'];
141
		$config['snmpd']['syslocation'] = $_POST['syslocation'];
142
		$config['snmpd']['syscontact'] = $_POST['syscontact'];
143
		$config['snmpd']['rocommunity'] = $_POST['rocommunity'];
144
		/* disabled until some docs show up on what this does.
145
		$config['snmpd']['rwenable'] = $_POST['rwenable'] ? true : false;
146
		$config['snmpd']['rwcommunity'] = $_POST['rwcommunity'];
147
		*/
148
		$config['snmpd']['trapenable'] = $_POST['trapenable'] ? true : false;
149
		$config['snmpd']['trapserver'] = $_POST['trapserver'];
150
		$config['snmpd']['trapserverport'] = $_POST['trapserverport'];
151
		$config['snmpd']['trapstring'] = $_POST['trapstring'];
152

    
153
		$config['snmpd']['modules']['mibii'] = $_POST['mibii'] ? true : false;
154
		$config['snmpd']['modules']['netgraph'] = $_POST['netgraph'] ? true : false;
155
		$config['snmpd']['modules']['pf'] = $_POST['pf'] ? true : false;
156
		$config['snmpd']['modules']['hostres'] = $_POST['hostres'] ? true : false;
157
		$config['snmpd']['modules']['bridge'] = $_POST['bridge'] ? true : false;
158
		$config['snmpd']['modules']['ucd'] = $_POST['ucd'] ? true : false;
159
		$config['snmpd']['modules']['regex'] = $_POST['regex'] ? true : false;
160
		if (is_array($_POST['bindip']) && !empty($_POST['bindip'])) {
161
			$config['snmpd']['bindip'] = implode(",", $_POST['bindip']);
162
		}
163

    
164
		write_config("SNMP settings saved");
165

    
166
		$changes_applied = true;
167
		$retval = 0;
168
		$retval |= services_snmpd_configure();
169
	}
170
}
171

    
172
function build_if_list($selectedifs) {
173
	$interface_addresses = get_possible_listen_ips(true);
174
	$iflist = array('options' => array(), 'selected' => array());
175

    
176
	$iflist['options']['all']	= gettext("All");
177
	if (empty($selectedifs) || empty($selectedifs[0]) || in_array("all", $selectedifs)) {
178
		array_push($iflist['selected'], "all");
179
	}
180

    
181
	foreach ($interface_addresses as $laddr => $ldescr) {
182
		if (is_ipaddr(get_interface_ip($laddr))) {
183
			$iflist['options'][$laddr] = htmlspecialchars($ldescr);
184
		}
185

    
186
		if ($selectedifs && in_array($laddr, $selectedifs)) {
187
			array_push($iflist['selected'], $laddr);
188
		}
189
	}
190

    
191
	unset($interface_addresses);
192

    
193
	return($iflist);
194
}
195

    
196
$pgtitle = array(gettext("Services"), gettext("SNMP"));
197
$shortcut_section = "snmp";
198

    
199
include("head.inc");
200

    
201
if ($input_errors) {
202
	print_input_errors($input_errors);
203
}
204

    
205
if ($changes_applied) {
206
	print_apply_result_box($retval);
207
}
208

    
209
$form = new Form();
210

    
211
$section = new Form_Section('SNMP Daemon');
212

    
213
$section->addInput(new Form_Checkbox(
214
	'enable',
215
	'Enable',
216
	'Enable the SNMP Daemon and its controls',
217
	$pconfig['enable']
218
));
219

    
220
$form->add($section);
221

    
222
$section = new Form_Section('SNMP Daemon Settings');
223

    
224
$section->addInput(new Form_Input(
225
	'pollport',
226
	'Polling Port',
227
	'text',
228
	($pconfig['pollport'] ? $pconfig['pollport']:'161')
229
))->setHelp('Enter the port to accept polling events on (default 161).');
230

    
231
$section->addInput(new Form_Input(
232
	'syslocation',
233
	'System Location',
234
	'text',
235
	$pconfig['syslocation']
236
));
237

    
238
$section->addInput(new Form_Input(
239
	'syscontact',
240
	'System Contact',
241
	'text',
242
	$pconfig['syscontact']
243
));
244

    
245
$section->addInput(new Form_Input(
246
	'rocommunity',
247
	'Read Community String',
248
	'text',
249
	$pconfig['rocommunity']
250
))->setHelp('The community string is like a password, restricting access to querying SNMP to hosts knowing the community string. Use a strong value here to protect from unauthorized information disclosure.');
251

    
252
$form->add($section);
253

    
254
$section = new Form_Section('SNMP Traps Enable');
255

    
256
$section->addInput(new Form_Checkbox(
257
	'trapenable',
258
	'Enable',
259
	'Enable the SNMP Trap and its controls',
260
	$pconfig['trapenable']
261
))->toggles('.toggle-traps');
262

    
263
$form->add($section);
264

    
265
$section = new Form_Section('SNMP Trap Settings');
266

    
267
if ($pconfig['trapenable']) {
268
	$section->addClass('toggle-traps', 'in');
269
} else {
270
	$section->addClass('toggle-traps', 'collapse');
271
}
272

    
273
$section->addInput(new Form_Input(
274
	'trapserver',
275
	'Trap server',
276
	'text',
277
	$pconfig['trapserver']
278
))->setHelp('Enter the trap server name');
279

    
280
$section->addInput(new Form_Input(
281
	'trapserverport',
282
	'Trap Server Port',
283
	'text',
284
	($pconfig['trapserverport'] ? $pconfig['trapserverport']:'162')
285
))->setHelp('Enter the port to send the traps to (default 162)');
286

    
287
$section->addInput(new Form_Input(
288
	'trapstring',
289
	'SNMP Trap String',
290
	'text',
291
	$pconfig['trapstring']
292
));
293

    
294
$form->add($section);
295

    
296
$section = new Form_Section('SNMP Modules');
297

    
298
$group = new Form_MultiCheckboxGroup('SNMP modules');
299

    
300
$group->add(new Form_MultiCheckbox(
301
	'mibii',
302
	null,
303
	'MibII',
304
	$pconfig['mibii']
305
));
306

    
307
$group->add(new Form_MultiCheckbox(
308
	'netgraph',
309
	null,
310
	'Netgraph',
311
	$pconfig['netgraph']
312
));
313

    
314
$group->add(new Form_MultiCheckbox(
315
	'pf',
316
	null,
317
	'PF',
318
	$pconfig['pf']
319
));
320

    
321
$group->add(new Form_MultiCheckbox(
322
	'hostres',
323
	null,
324
	'Host Resources',
325
	$pconfig['hostres']
326
));
327

    
328
$group->add(new Form_MultiCheckbox(
329
	'ucd',
330
	null,
331
	'UCD',
332
	$pconfig['ucd']
333
));
334

    
335
$group->add(new Form_MultiCheckbox(
336
	'regex',
337
	null,
338
	'Regex',
339
	$pconfig['regex']
340
));
341

    
342
$section->add($group);
343
$form->add($section);
344

    
345
$section = new Form_Section('Interface Binding');
346

    
347
$iflist = build_if_list($pconfig['bindip']);
348

    
349
$section->addInput(new Form_Select(
350
	'bindip',
351
	'Bind Interfaces',
352
	$iflist['selected'],
353
	$iflist['options'],
354
	true
355
));
356

    
357
$form->add($section);
358

    
359
print($form);
360
?>
361

    
362
<script type="text/javascript">
363
//<![CDATA[
364

    
365
// hostres requires mibii so we force that here
366
events.push(function() {
367

    
368
	noMibii = false;
369

    
370
	$('#junk').hide();
371
	enableChange();
372
	trapenableChange();
373
	hostresChange();
374

    
375
	function enableChange() {
376
		setRequired('pollport', $('#enable').prop('checked'));
377
		setRequired('rocommunity', $('#enable').prop('checked'));
378
	}
379

    
380
	function trapenableChange() {
381
		setRequired('trapserver', $('#trapenable').prop('checked'));
382
		setRequired('trapserverport', $('#trapenable').prop('checked'));
383
		setRequired('trapstring', $('#trapenable').prop('checked'));
384
	}
385

    
386
	function hostresChange() {
387
		if ($('#hostres').prop('checked')) {
388
			$('#mibii').prop('checked', true);
389
			noMibii = true;
390
		} else {
391
			noMibii = false;
392
		}
393
	}
394

    
395
	$('#enable').change(function() {
396
		enableChange();
397
	});
398

    
399
	$('#trapenable').change(function() {
400
		trapenableChange();
401
	});
402

    
403
	$('#hostres').change(function() {
404
		hostresChange();
405
	});
406

    
407
	$('#mibii').change(function() {
408
		if (noMibii) {
409
			$('#mibii').prop('checked', 'true');
410
		}
411
	});
412

    
413
	$('[name=btntoggleall]').hide();
414
});
415
//]]>
416
</script>
417

    
418
<?php include("foot.inc");
(140-140/229)