Project

General

Profile

Download (15.8 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

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

    
64
if($_GET['addrow'])
65
	$maxrows = $_GET['addrow'] + 1;
66
else
67
	$maxrows = 3;
68

    
69
if ($_POST) {
70
	unset($input_errors);
71
	$pconfig = $_POST;
72

    
73
	if (!$input_errors) {
74
		if (is_array($_POST['interface'])) {
75
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
76
		} elseif (isset($config['ntpd']['interface'])) {
77
			unset($config['ntpd']['interface']);
78
		}
79

    
80
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
81
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
82
		} elseif (isset($config['ntpd']['gpsport'])) {
83
			unset($config['ntpd']['gpsport']);
84
		}
85

    
86
		unset($config['ntpd']['prefer']);
87
		unset($config['ntpd']['noselect']);
88
		$timeservers = '';
89

    
90
		for ($i = 0; $i < 10; $i++) {
91
			$tserver = trim($_POST["server{$i}"]);
92
			if (!empty($tserver)) {
93
				$timeservers .= "{$tserver} ";
94
				if (!empty($_POST["servprefer{$i}"])) {
95
					$config['ntpd']['prefer'] .= "{$tserver} ";
96
				}
97
				if (!empty($_POST["servselect{$i}"])) {
98
					$config['ntpd']['noselect'] .= "{$tserver} ";
99
				}
100
			}
101
		}
102
		if (trim($timeservers) == "") {
103
			$timeservers = "pool.ntp.org";
104
		}
105
		$config['system']['timeservers'] = trim($timeservers);
106

    
107
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
108
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
109
		} elseif (isset($config['ntpd']['orphan'])) {
110
			unset($config['ntpd']['orphan']);
111
		}
112

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

    
119
		if (!empty($_POST['logsys'])) {
120
			$config['ntpd']['logsys'] = $_POST['logsys'];
121
		} elseif (isset($config['ntpd']['logsys'])) {
122
			unset($config['ntpd']['logsys']);
123
		}
124

    
125
		if (!empty($_POST['clockstats'])) {
126
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
127
		} elseif (isset($config['ntpd']['clockstats'])) {
128
			unset($config['ntpd']['clockstats']);
129
		}
130

    
131
		if (!empty($_POST['loopstats'])) {
132
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
133
		} elseif (isset($config['ntpd']['loopstats'])) {
134
			unset($config['ntpd']['loopstats']);
135
		}
136

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

    
143
		if (empty($_POST['kod'])) {
144
			$config['ntpd']['kod'] = 'on';
145
		} elseif (isset($config['ntpd']['kod'])) {
146
			unset($config['ntpd']['kod']);
147
		}
148

    
149
		if (empty($_POST['nomodify'])) {
150
			$config['ntpd']['nomodify'] = 'on';
151
		} elseif (isset($config['ntpd']['nomodify'])) {
152
			unset($config['ntpd']['nomodify']);
153
		}
154

    
155
		if (!empty($_POST['noquery'])) {
156
			$config['ntpd']['noquery'] = $_POST['noquery'];
157
		} elseif (isset($config['ntpd']['noquery'])) {
158
			unset($config['ntpd']['noquery']);
159
		}
160

    
161
		if (!empty($_POST['noserve'])) {
162
			$config['ntpd']['noserve'] = $_POST['noserve'];
163
		} elseif (isset($config['ntpd']['noserve'])) {
164
			unset($config['ntpd']['noserve']);
165
		}
166

    
167
		if (empty($_POST['nopeer'])) {
168
			$config['ntpd']['nopeer'] = 'on';
169
		} elseif (isset($config['ntpd']['nopeer'])) {
170
			unset($config['ntpd']['nopeer']);
171
		}
172

    
173
		if (empty($_POST['notrap'])) {
174
			$config['ntpd']['notrap'] = 'on';
175
		} elseif (isset($config['ntpd']['notrap'])) {
176
			unset($config['ntpd']['notrap']);
177
		}
178

    
179
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
180
			$enable_rrd_graphing = true;
181
		}
182
		if (!empty($_POST['statsgraph'])) {
183
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
184
		} elseif (isset($config['ntpd']['statsgraph'])) {
185
			unset($config['ntpd']['statsgraph']);
186
		}
187
		if (isset($enable_rrd_graphing)) {
188
			enable_rrd_graphing();
189
		}
190

    
191
		if (!empty($_POST['leaptxt'])) {
192
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptxt']);
193
		} elseif (isset($config['ntpd']['leapsec'])) {
194
			unset($config['ntpd']['leapsec']);
195
		}
196

    
197
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
198
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
199
		}
200

    
201
		write_config("Updated NTP Server Settings");
202

    
203
		$retval = 0;
204
		$retval = system_ntp_configure();
205
		$savemsg = get_std_save_message($retval);
206
	}
207
}
208

    
209
function build_interface_list() {
210
	global $pconfig;
211

    
212
	$iflist = array('options' => array(), 'selected' => array());
213

    
214
	$interfaces = get_configured_interface_with_descr();
215
	$carplist = get_configured_carp_interface_list();
216

    
217
	foreach ($carplist as $cif => $carpip)
218
		$interfaces[$cif] = $carpip . " (" . get_vip_descr($carpip) .")";
219

    
220
	$aliaslist = get_configured_ip_aliases_list();
221

    
222
	foreach ($aliaslist as $aliasip => $aliasif)
223
		$interfaces[$aliasip] = $aliasip." (".get_vip_descr($aliasip).")";
224

    
225
	$size = (count($interfaces) < 10) ? count($interfaces) : 10;
226

    
227
	foreach ($interfaces as $iface => $ifacename) {
228
		if (!is_ipaddr(get_interface_ip($iface)) && !is_ipaddr($iface))
229
			continue;
230

    
231
		$iflist['options']['$iface'] = $ifacename;
232

    
233
		if (in_array($iface, $pconfig['interface']))
234
			array_push($iflist['slected'], $iface);
235

    
236
	}
237

    
238
	return($iflist);
239
}
240

    
241
$closehead = false;
242
$pconfig = &$config['ntpd'];
243
if (empty($pconfig['interface'])) {
244
	$pconfig['interface'] = array();
245
} else {
246
	$pconfig['interface'] = explode(",", $pconfig['interface']);
247
}
248
$pgtitle = array(gettext("Services"), gettext("NTP"));
249
$shortcut_section = "ntp";
250
include("head.inc");
251

    
252
if ($input_errors)
253
	print_input_errors($input_errors);
254
if ($savemsg)
255
	print_info_box($savemsg, 'success');
256

    
257
$tab_array = array();
258
$tab_array[] = array(gettext("NTP"), true, "services_ntpd.php");
259
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
260
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
261
display_top_tabs($tab_array);
262

    
263
require('classes/Form.class.php');
264

    
265
$form = new Form;
266

    
267
$section = new Form_Section('NTP server configuration');
268

    
269
$iflist = build_interface_list();
270

    
271
$section->addInput(new Form_Select(
272
	'interface',
273
	'Interface',
274
	$iflist['selected'],
275
	$iflist['options'],
276
	true
277
))->setHelp('Interfaces without an IP address will not be shown.' . '<br />' .
278
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
279
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
280

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

    
286
	if($i >= $maxrows)
287
		continue;
288

    
289
	$group = new Form_Group($i == 0 ? 'Time servers':'');
290

    
291
	$group->add(new Form_Input(
292
		'server' . $i,
293
		null,
294
		'text',
295
		$timeservers[$i]
296
	 ));
297

    
298
	 $group->add(new Form_Checkbox(
299
		'servprefer' . $i,
300
		null,
301
		'Prefer',
302
		isset($config['ntpd']['prefer']) && isset($timeservers[$i]) && substr_count($config['ntpd']['prefer'], $timeservers[$i])
303
	 ));
304

    
305
	 $group->add(new Form_Checkbox(
306
		'servselect' . $i,
307
		null,
308
		'NoSelect',
309
		isset($config['ntpd']['noselect']) && isset($timeservers[$i]) && substr_count($config['ntpd']['noselect'], $timeservers[$i])
310
	 ));
311

    
312
	 $section->add($group);
313
}
314

    
315
// Show the 'Add Rows' button only if we are currently displaying less than the maximum
316
// number of configured servers
317
if($maxrows < NUMTIMESERVERS) {
318
	$btnaddrow = new Form_Button(
319
		'btnaddrow',
320
		'Add Server',
321
		'services_ntpd.php?addrow=' . $maxrows
322
		);
323

    
324
	$btnaddrow->removeClass('btn-primary')->addClass('btn-success btn-sm');
325
} else
326
	$btnaddrow = false;
327

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

    
335
$section->addInput(new Form_Input(
336
	'ntporphan',
337
	'Orphan mode',
338
	'text',
339
	$pconfig['ntporphan']
340
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
341
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
342
			'to insure that any other servers available to clients are preferred over this server. (default: 12).');
343

    
344
$section->addInput(new Form_Checkbox(
345
	'statsgraph',
346
	'NTP Graphs',
347
	'Enable RRD graphs of NTP statistics (default: disabled).',
348
	$pconfig['statsgraph']
349
));
350

    
351
$section->addInput(new Form_Checkbox(
352
	'logpeer',
353
	'Syslog logging',
354
	'Enable logging of peer messages (default: disabled).',
355
	$pconfig['logpeer']
356
));
357

    
358
$section->addInput(new Form_Checkbox(
359
	'logsys',
360
	null,
361
	'Enable logging of system messages (default: disabled).',
362
	$pconfig['logsys']
363
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
364
			'<a href="diag_logs_ntpd.php">' . 'Status > System Logs > NTP' . '</a>');
365

    
366
// Statistics logging section
367
$btnadvstats = new Form_Button(
368
	'btnadvstats',
369
	'Advanced'
370
);
371

    
372
$btnadvstats->removeClass('btn-primary')->addClass('btn-default btn-sm');
373

    
374
$section->addInput(new Form_StaticText(
375
	'Statistics logging',
376
	$btnadvstats
377
))->setHelp('Warning: These options will create persistant daily log files in /var/log/ntp.');
378

    
379
$section->addInput(new Form_Checkbox(
380
	'clockstats',
381
	null,
382
	'Enable logging of reference clock statistics (default: disabled).',
383
	$pconfig['clockstats']
384
));
385

    
386
$section->addInput(new Form_Checkbox(
387
	'loopstats',
388
	null,
389
	'Enable logging of clock discipline statistics (default: disabled).',
390
	$pconfig['loopstats']
391
));
392

    
393
$section->addInput(new Form_Checkbox(
394
	'peerstats',
395
	null,
396
	'Enable logging of NTP peer statistics (default: disabled).',
397
	$pconfig['peerstats']
398
));
399

    
400
// Access restrictions section
401
$btnadvrestr = new Form_Button(
402
	'btnadvrestr',
403
	'Advanced'
404
);
405

    
406
$btnadvrestr->removeClass('btn-primary')->addClass('btn-default btn-sm');
407

    
408
$section->addInput(new Form_StaticText(
409
	'Access Restrictions',
410
	$btnadvrestr
411
))->setHelp('These options control access to NTP from the WAN.');
412

    
413
$section->addInput(new Form_Checkbox(
414
	'kod',
415
	null,
416
	'Enable Kiss-o\'-death packets (default: enabled).',
417
	$pconfig['kod']
418
));
419

    
420
$section->addInput(new Form_Checkbox(
421
	'nomodify',
422
	null,
423
	'Deny state modifications (i.e. run time configuration) by ntpq and ntpdc (default: enabled).',
424
	$pconfig['nomodify']
425
));
426

    
427
$section->addInput(new Form_Checkbox(
428
	'noquery',
429
	null,
430
	'Disable ntpq and ntpdc queries (default: disabled).',
431
	$pconfig['noquery']
432
));
433

    
434
$section->addInput(new Form_Checkbox(
435
	'noserve',
436
	null,
437
	'Disable all except ntpq and ntpdc queries (default: disabled).',
438
	$pconfig['noserve']
439
));
440

    
441
$section->addInput(new Form_Checkbox(
442
	'nopeer',
443
	null,
444
	'Deny packets that attempt a peer association (default: enabled).',
445
	$pconfig['nopeer']
446
));
447

    
448
$section->addInput(new Form_Checkbox(
449
	'notrap',
450
	null,
451
	'Deny mode 6 control message trap service (default: enabled).',
452
	$pconfig['notrap']
453
))->addClass('advrestrictions');
454

    
455
// Leap seconds section
456
$btnleap = new Form_Button(
457
	'btnleap',
458
	'Advanced'
459
);
460

    
461
$btnleap->removeClass('btn-primary')->addClass('btn-default btn-sm');
462

    
463
$section->addInput(new Form_StaticText(
464
	'Leap seconds',
465
	$btnleap
466
))->setHelp('A leap second file allows NTP to advertize an upcoming leap second addition or subtraction. ' .
467
			'Normally this is only useful if this server is a stratum 1 time server. ');
468

    
469
$section->addInput(new Form_Textarea(
470
	'leaptext',
471
	null,
472
	base64_decode(chunk_split($pconfig['leapsec']))
473
))->setHelp('Enter Leap second configuration as text OR select a file to upload');
474

    
475
$section->addInput(new Form_Input(
476
	'leapfile',
477
	null,
478
	'file'
479
))->addClass('btn-default');
480

    
481
$form->add($section);
482
print($form);
483

    
484
?>
485

    
486
<script>
487
//<![CDATA[
488
events.push(function(){
489

    
490
	// Hides the <div> in which the specified input element lives so that the input, its label and help text are hidden
491
	function hideInput(id, hide) {
492
		if(hide)
493
			$('#' + id).parent().parent('div').addClass('hidden');
494
		else
495
			$('#' + id).parent().parent('div').removeClass('hidden');
496
	}
497

    
498
	// Hides the <div> in which the specified checkbox lives so that the checkbox, its label and help text are hidden
499
	function hideCheckbox(id, hide) {
500
		if(hide)
501
			$('#' + id).parent().parent().parent('div').addClass('hidden');
502
		else
503
			$('#' + id).parent().parent().parent('div').removeClass('hidden');
504
	}
505

    
506
	// Make the ‘clear’ button a plain button, not a submit button
507
	$('#btnadvstats').prop('type','button');
508

    
509
	// On click, show the controls in the stats section
510
	$("#btnadvstats").click(function() {
511
		hideCheckbox('clockstats', false);
512
		hideCheckbox('loopstats', false);
513
		hideCheckbox('peerstats', false);
514
	});
515

    
516
	// Make the ‘clear’ button a plain button, not a submit button
517
	$('#btnadvrestr').prop('type','button');
518

    
519
	// On click, show the controls in the restrictions section
520
	$("#btnadvrestr").click(function() {
521
		hideCheckbox('nomodify', false);
522
		hideCheckbox('noquery', false);
523
		hideCheckbox('noserve', false);
524
		hideCheckbox('nopeer', false);
525
		hideCheckbox('notrap', false);
526
	});
527

    
528
	// Make the ‘btnleap’ button a plain button, not a submit button
529
	$('#btnleap').prop('type','button');
530

    
531
	// On click, show the controls in the leap seconds section
532
	$("#btnleap").click(function() {
533
		hideInput('leaptext', false);
534
		hideInput('leapfile', false);
535
	});
536

    
537
	// Set intial states
538
	hideCheckbox('clockstats', true);
539
	hideCheckbox('loopstats', true);
540
	hideCheckbox('peerstats', true);
541
	hideCheckbox('kod', true);
542
	hideCheckbox('nomodify', true);
543
	hideCheckbox('noquery', true);
544
	hideCheckbox('noserve', true);
545
	hideCheckbox('nopeer', true);
546
	hideCheckbox('notrap', true);
547
	hideInput('leaptext', true);
548
	hideInput('leapfile', true);
549
});
550
//]]>
551
</script>
552

    
553
<?php include("foot.inc");
(145-145/237)