Project

General

Profile

Download (15.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_ntpd.php
4

    
5
	Copyright (C) 2013	Dagorlad
6
	Copyright (C) 2012	Jim Pingle
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE: ntpd
33
*/
34

    
35
##|+PRIV
36
##|*IDENT=page-services-ntpd
37
##|*NAME=Services: NTP
38
##|*DESCR=Allow access to the 'Services: NTP' page.
39
##|*MATCH=services_ntpd.php*
40
##|-PRIV
41

    
42
define(NUMTIMESERVERS, 10);		// The maximum number of configurable time servers
43
require("guiconfig.inc");
44
require_once('rrd.inc');
45
require_once("shaper.inc");
46

    
47
if (!is_array($config['ntpd']))
48
	$config['ntpd'] = array();
49

    
50
if (empty($config['ntpd']['interface'])) {
51
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
52
		is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
53
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
54
		unset($config['installedpackages']['openntpd']);
55
		write_config("Upgraded settings from openttpd");
56
	} else
57
		$pconfig['interface'] = array();
58
} else
59
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
60

    
61
if($_GET['addrow'])
62
	$maxrows = $_GET['addrow'] + 1;
63
else
64
	$maxrows = 3;
65

    
66
if ($_POST) {
67
	unset($input_errors);
68
	$pconfig = $_POST;
69

    
70
	if (!$input_errors) {
71
		if (is_array($_POST['interface']))
72
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
73
		elseif (isset($config['ntpd']['interface']))
74
			unset($config['ntpd']['interface']);
75

    
76
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport']))
77
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
78
		elseif (isset($config['ntpd']['gpsport']))
79
			unset($config['ntpd']['gpsport']);
80

    
81
		unset($config['ntpd']['prefer']);
82
		unset($config['ntpd']['noselect']);
83
		$timeservers = '';
84

    
85
		for ($i = 0; $i < 10; $i++) {
86
			$tserver = trim($_POST["server{$i}"]);
87
			if (!empty($tserver)) {
88
				$timeservers .= "{$tserver} ";
89
				if (!empty($_POST["servprefer{$i}"])) $config['ntpd']['prefer'] .= "{$tserver} ";
90
				if (!empty($_POST["servselect{$i}"])) $config['ntpd']['noselect'].= "{$tserver} ";
91
			}
92
		}
93

    
94
		if (trim($timeservers) == "")
95
			$timeservers = "pool.ntp.org";
96
		$config['system']['timeservers'] = trim($timeservers);
97

    
98
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12'))
99
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
100
		elseif (isset($config['ntpd']['orphan']))
101
			unset($config['ntpd']['orphan']);
102

    
103
		if (!empty($_POST['logpeer']))
104
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
105
		elseif (isset($config['ntpd']['logpeer']))
106
			unset($config['ntpd']['logpeer']);
107

    
108
		if (!empty($_POST['logsys']))
109
			$config['ntpd']['logsys'] = $_POST['logsys'];
110
		elseif (isset($config['ntpd']['logsys']))
111
			unset($config['ntpd']['logsys']);
112

    
113
		if (!empty($_POST['clockstats']))
114
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
115
		elseif (isset($config['ntpd']['clockstats']))
116
			unset($config['ntpd']['clockstats']);
117

    
118
		if (!empty($_POST['loopstats']))
119
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
120
		elseif (isset($config['ntpd']['loopstats']))
121
			unset($config['ntpd']['loopstats']);
122

    
123
		if (!empty($_POST['peerstats']))
124
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
125
		elseif (isset($config['ntpd']['peerstats']))
126
			unset($config['ntpd']['peerstats']);
127

    
128
		if (empty($_POST['kod']))
129
			$config['ntpd']['kod'] = 'on';
130
		elseif (isset($config['ntpd']['kod']))
131
			unset($config['ntpd']['kod']);
132

    
133
		if (empty($_POST['nomodify']))
134
			$config['ntpd']['nomodify'] = 'on';
135
		elseif (isset($config['ntpd']['nomodify']))
136
			unset($config['ntpd']['nomodify']);
137

    
138
		if (!empty($_POST['noquery']))
139
			$config['ntpd']['noquery'] = $_POST['noquery'];
140
		elseif (isset($config['ntpd']['noquery']))
141
			unset($config['ntpd']['noquery']);
142

    
143
		if (!empty($_POST['noserve']))
144
			$config['ntpd']['noserve'] = $_POST['noserve'];
145
		elseif (isset($config['ntpd']['noserve']))
146
			unset($config['ntpd']['noserve']);
147

    
148
		if (empty($_POST['nopeer']))
149
			$config['ntpd']['nopeer'] = 'on';
150
		elseif (isset($config['ntpd']['nopeer']))
151
			unset($config['ntpd']['nopeer']);
152

    
153
		if (empty($_POST['notrap']))
154
			$config['ntpd']['notrap'] = 'on';
155
		elseif (isset($config['ntpd']['notrap']))
156
			unset($config['ntpd']['notrap']);
157

    
158
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph'])))
159
			$enable_rrd_graphing = true;
160
		if (!empty($_POST['statsgraph']))
161
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
162
		elseif (isset($config['ntpd']['statsgraph']))
163
			unset($config['ntpd']['statsgraph']);
164
		if (isset($enable_rrd_graphing))
165
			enable_rrd_graphing();
166

    
167
		if (!empty($_POST['leaptxt']))
168
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptxt']);
169
		elseif (isset($config['ntpd']['leapsec']))
170
			unset($config['ntpd']['leapsec']);
171

    
172
		if (is_uploaded_file($_FILES['leapfile']['tmp_name']))
173
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
174

    
175
		write_config("Updated NTP Server Settings");
176

    
177
		$retval = 0;
178
		$retval = system_ntp_configure();
179
		$savemsg = get_std_save_message($retval);
180
	}
181
}
182

    
183
function build_interface_list() {
184
	global $pconfig;
185

    
186
	$iflist = array('options' => array(), 'selected' => array());
187

    
188
	$interfaces = get_configured_interface_with_descr();
189
	$carplist = get_configured_carp_interface_list();
190

    
191
	foreach ($carplist as $cif => $carpip)
192
		$interfaces[$cif] = $carpip . " (" . get_vip_descr($carpip) .")";
193

    
194
	$aliaslist = get_configured_ip_aliases_list();
195

    
196
	foreach ($aliaslist as $aliasip => $aliasif)
197
		$interfaces[$aliasip] = $aliasip." (".get_vip_descr($aliasip).")";
198

    
199
	$size = (count($interfaces) < 10) ? count($interfaces) : 10;
200

    
201
	foreach ($interfaces as $iface => $ifacename) {
202
		if (!is_ipaddr(get_interface_ip($iface)) && !is_ipaddr($iface))
203
			continue;
204

    
205
		$iflist['options']['$iface'] = $ifacename;
206

    
207
		if (in_array($iface, $pconfig['interface']))
208
			array_push($iflist['slected'], $iface);
209

    
210
	}
211

    
212
	return($iflist);
213
}
214

    
215
$closehead = false;
216
$pconfig = &$config['ntpd'];
217

    
218
if (empty($pconfig['interface']))
219
	$pconfig['interface'] = array();
220
else
221
	$pconfig['interface'] = explode(",", $pconfig['interface']);
222
	
223
$pgtitle = array(gettext("Services"),gettext("NTP"));
224
$shortcut_section = "ntp";
225
include("head.inc");
226

    
227
if ($input_errors)
228
	print_input_errors($input_errors);
229
if ($savemsg)
230
	print_info_box($savemsg, 'success');
231

    
232
$tab_array = array();
233
$tab_array[] = array(gettext("NTP"), true, "services_ntpd.php");
234
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
235
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
236
display_top_tabs($tab_array);
237

    
238
require('classes/Form.class.php');
239

    
240
$form = new Form;
241

    
242
$section = new Form_Section('NTP server configuration');
243

    
244
$iflist = build_interface_list();
245

    
246
$section->addInput(new Form_Select(
247
	'interface',
248
	'Interface',
249
	$iflist['selected'],
250
	$iflist['options'],
251
	true
252
))->setHelp('Interfaces without an IP address will not be shown.' . '<br />' .
253
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
254
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
255

    
256
// NUMTIMESERVERS time servers are always available, but we only display a smaller number of these ($maxrows)
257
// Clicking the 'Add Row' button increments $maxrows so you can see more of time servers
258
$timeservers = explode( ' ', $config['system']['timeservers']);
259
for ($i = $j = 0; $i < NUMTIMESERVERS; $i++){
260

    
261
	if($i >= $maxrows)
262
		continue;
263

    
264
	$group = new Form_Group($i == 0 ? 'Time servers':'');
265

    
266
	$group->add(new Form_Input(
267
		'server' . $i,
268
		null,
269
		'text',
270
		$timeservers[$i]
271
	 ));
272

    
273
	 $group->add(new Form_Checkbox(
274
		'servprefer' . $i,
275
		null,
276
		'Prefer',
277
		isset($config['ntpd']['prefer']) && isset($timeservers[$i]) && substr_count($config['ntpd']['prefer'], $timeservers[$i])
278
	 ));
279

    
280
	 $group->add(new Form_Checkbox(
281
		'servselect' . $i,
282
		null,
283
		'NoSelect',
284
		isset($config['ntpd']['noselect']) && isset($timeservers[$i]) && substr_count($config['ntpd']['noselect'], $timeservers[$i])
285
	 ));
286

    
287
	 $section->add($group);
288
}
289

    
290
// Show the 'Add Rows' button only if we are currently displaying less than the maximum
291
// number of configured servers
292
if($maxrows < NUMTIMESERVERS) {
293
	$btnaddrow = new Form_Button(
294
		'btnaddrow',
295
		'Add Server',
296
		'services_ntpd.php?addrow=' . $maxrows
297
		);
298

    
299
	$btnaddrow->removeClass('btn-primary')->addClass('btn-success btn-sm');
300
} else
301
	$btnaddrow = false;
302

    
303
$section->addInput(new Form_StaticText(
304
	null,
305
	$btnaddrow
306
))->setHelp('For best results three to five servers should be configured here.' . '<br />' .
307
			'The prefer option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
308
			'The noselect option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.');
309

    
310
$section->addInput(new Form_Input(
311
	'ntporphan',
312
	'Orphan mode',
313
	'text',
314
	$pconfig['ntporphan']
315
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
316
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
317
			'to insure that any other servers available to clients are preferred over this server. (default: 12).');
318

    
319
$section->addInput(new Form_Checkbox(
320
	'statsgraph',
321
	'NTP Graphs',
322
	'Enable RRD graphs of NTP statistics (default: disabled).',
323
	$pconfig['statsgraph']
324
));
325

    
326
$section->addInput(new Form_Checkbox(
327
	'logpeer',
328
	'Syslog logging',
329
	'Enable logging of peer messages (default: disabled).',
330
	$pconfig['logpeer']
331
));
332

    
333
$section->addInput(new Form_Checkbox(
334
	'logsys',
335
	null,
336
	'Enable logging of system messages (default: disabled).',
337
	$pconfig['logsys']
338
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
339
			'<a href="diag_logs_ntpd.php">' . 'Status > System Logs > NTP' . '</a>');
340

    
341
// Statistics logging section
342
$btnadvstats = new Form_Button(
343
	'btnadvstats',
344
	'Advanced'
345
);
346

    
347
$btnadvstats->removeClass('btn-primary')->addClass('btn-default btn-sm');
348

    
349
$section->addInput(new Form_StaticText(
350
	'Statistics logging',
351
	$btnadvstats
352
))->setHelp('Warning: These options will create persistant daily log files in /var/log/ntp.');
353

    
354
$section->addInput(new Form_Checkbox(
355
	'clockstats',
356
	null,
357
	'Enable logging of reference clock statistics (default: disabled).',
358
	$pconfig['clockstats']
359
));
360

    
361
$section->addInput(new Form_Checkbox(
362
	'loopstats',
363
	null,
364
	'Enable logging of clock discipline statistics (default: disabled).',
365
	$pconfig['loopstats']
366
));
367

    
368
$section->addInput(new Form_Checkbox(
369
	'peerstats',
370
	null,
371
	'Enable logging of NTP peer statistics (default: disabled).',
372
	$pconfig['peerstats']
373
));
374

    
375
// Access restrictions section
376
$btnadvrestr = new Form_Button(
377
	'btnadvrestr',
378
	'Advanced'
379
);
380

    
381
$btnadvrestr->removeClass('btn-primary')->addClass('btn-default btn-sm');
382

    
383
$section->addInput(new Form_StaticText(
384
	'Access Restrictions',
385
	$btnadvrestr
386
))->setHelp('These options control access to NTP from the WAN.');
387

    
388
$section->addInput(new Form_Checkbox(
389
	'kod',
390
	null,
391
	'Enable Kiss-o\'-death packets (default: enabled).',
392
	$pconfig['kod']
393
));
394

    
395
$section->addInput(new Form_Checkbox(
396
	'nomodify',
397
	null,
398
	'Deny state modifications (i.e. run time configuration) by ntpq and ntpdc (default: enabled).',
399
	$pconfig['nomodify']
400
));
401

    
402
$section->addInput(new Form_Checkbox(
403
	'noquery',
404
	null,
405
	'Disable ntpq and ntpdc queries (default: disabled).',
406
	$pconfig['noquery']
407
));
408

    
409
$section->addInput(new Form_Checkbox(
410
	'noserve',
411
	null,
412
	'Disable all except ntpq and ntpdc queries (default: disabled).',
413
	$pconfig['noserve']
414
));
415

    
416
$section->addInput(new Form_Checkbox(
417
	'nopeer',
418
	null,
419
	'Deny packets that attempt a peer association (default: enabled).',
420
	$pconfig['nopeer']
421
));
422

    
423
$section->addInput(new Form_Checkbox(
424
	'notrap',
425
	null,
426
	'Deny mode 6 control message trap service (default: enabled).',
427
	$pconfig['notrap']
428
))->addClass('advrestrictions');
429

    
430
// Leap seconds section
431
$btnleap = new Form_Button(
432
	'btnleap',
433
	'Advanced'
434
);
435

    
436
$btnleap->removeClass('btn-primary')->addClass('btn-default btn-sm');
437

    
438
$section->addInput(new Form_StaticText(
439
	'Leap seconds',
440
	$btnleap
441
))->setHelp('A leap second file allows NTP to advertize an upcoming leap second addition or subtraction. ' .
442
			'Normally this is only useful if this server is a stratum 1 time server. ');
443

    
444
$section->addInput(new Form_Textarea(
445
	'leaptext',
446
	null,
447
	base64_decode(chunk_split($pconfig['leapsec']))
448
))->setHelp('Enter Leap second configuration as text OR select a file to upload');
449

    
450
$section->addInput(new Form_Input(
451
	'leapfile',
452
	null,
453
	'file'
454
))->addClass('btn-default');
455

    
456
$form->add($section);
457
print($form);
458

    
459
?>
460

    
461
<script>
462
//<![CDATA[
463
events.push(function(){
464

    
465
	// Hides the <div> in which the specified input element lives so that the input, its label and help text are hidden
466
	function hideInput(id, hide) {
467
		if(hide)
468
			$('#' + id).parent().parent('div').addClass('hidden');
469
		else
470
			$('#' + id).parent().parent('div').removeClass('hidden');
471
	}
472

    
473
	// Hides the <div> in which the specified checkbox lives so that the checkbox, its label and help text are hidden
474
	function hideCheckbox(id, hide) {
475
		if(hide)
476
			$('#' + id).parent().parent().parent('div').addClass('hidden');
477
		else
478
			$('#' + id).parent().parent().parent('div').removeClass('hidden');
479
	}
480

    
481
	// Make the ‘clear’ button a plain button, not a submit button
482
	$('#btnadvstats').prop('type','button');
483

    
484
	// On click, show the controls in the stats section
485
	$("#btnadvstats").click(function() {
486
		hideCheckbox('clockstats', false);
487
		hideCheckbox('loopstats', false);
488
		hideCheckbox('peerstats', false);
489
	});
490

    
491
	// Make the ‘clear’ button a plain button, not a submit button
492
	$('#btnadvrestr').prop('type','button');
493

    
494
	// On click, show the controls in the restrictions section
495
	$("#btnadvrestr").click(function() {
496
		hideCheckbox('nomodify', false);
497
		hideCheckbox('noquery', false);
498
		hideCheckbox('noserve', false);
499
		hideCheckbox('nopeer', false);
500
		hideCheckbox('notrap', false);
501
	});
502

    
503
	// Make the ‘btnleap’ button a plain button, not a submit button
504
	$('#btnleap').prop('type','button');
505

    
506
	// On click, show the controls in the leap seconds section
507
	$("#btnleap").click(function() {
508
		hideInput('leaptext', false);
509
		hideInput('leapfile', false);
510
	});
511

    
512
	// Set intial states
513
	hideCheckbox('clockstats', true);
514
	hideCheckbox('loopstats', true);
515
	hideCheckbox('peerstats', true);
516
	hideCheckbox('kod', true);
517
	hideCheckbox('nomodify', true);
518
	hideCheckbox('noquery', true);
519
	hideCheckbox('noserve', true);
520
	hideCheckbox('nopeer', true);
521
	hideCheckbox('notrap', true);
522
	hideInput('leaptext', true);
523
	hideInput('leapfile', true);
524
});
525
//]]>
526
</script>
527

    
528
<?php include("foot.inc");
(149-149/241)