Project

General

Profile

Download (16.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_ntpd.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2013 Dagorlad
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56

    
57
##|+PRIV
58
##|*IDENT=page-services-ntpd
59
##|*NAME=Services: NTP Settings
60
##|*DESCR=Allow access to the 'Services: NTP Settings' page.
61
##|*MATCH=services_ntpd.php*
62
##|-PRIV
63

    
64
define('NUMTIMESERVERS', 10);		// The maximum number of configurable time servers
65
require("guiconfig.inc");
66
require_once('rrd.inc');
67
require_once("shaper.inc");
68

    
69
if (!is_array($config['ntpd'])) {
70
	$config['ntpd'] = array();
71
}
72

    
73
if (empty($config['ntpd']['interface'])) {
74
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
75
	    is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
76
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
77
		unset($config['installedpackages']['openntpd']);
78
		write_config(gettext("Upgraded settings from openttpd"));
79
	} else {
80
		$pconfig['interface'] = array();
81
	}
82
} else {
83
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
84
}
85

    
86
if ($_POST) {
87
	unset($input_errors);
88
	$pconfig = $_POST;
89

    
90
	if (!$input_errors) {
91
		if (is_array($_POST['interface'])) {
92
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
93
		} elseif (isset($config['ntpd']['interface'])) {
94
			unset($config['ntpd']['interface']);
95
		}
96

    
97
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
98
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
99
		} elseif (isset($config['ntpd']['gpsport'])) {
100
			unset($config['ntpd']['gpsport']);
101
		}
102

    
103
		unset($config['ntpd']['prefer']);
104
		unset($config['ntpd']['noselect']);
105
		$timeservers = '';
106

    
107
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
108
			$tserver = trim($_POST["server{$i}"]);
109
			if (!empty($tserver)) {
110
				$timeservers .= "{$tserver} ";
111
				if (!empty($_POST["servprefer{$i}"])) {
112
					$config['ntpd']['prefer'] .= "{$tserver} ";
113
				}
114
				if (!empty($_POST["servselect{$i}"])) {
115
					$config['ntpd']['noselect'] .= "{$tserver} ";
116
				}
117
			}
118
		}
119
		if (trim($timeservers) == "") {
120
			$timeservers = "pool.ntp.org";
121
		}
122
		$config['system']['timeservers'] = trim($timeservers);
123

    
124
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
125
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
126
		} elseif (isset($config['ntpd']['orphan'])) {
127
			unset($config['ntpd']['orphan']);
128
		}
129

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

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

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

    
148
		if (!empty($_POST['loopstats'])) {
149
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
150
		} elseif (isset($config['ntpd']['loopstats'])) {
151
			unset($config['ntpd']['loopstats']);
152
		}
153

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

    
160
		if (empty($_POST['kod'])) {
161
			$config['ntpd']['kod'] = 'on';
162
		} elseif (isset($config['ntpd']['kod'])) {
163
			unset($config['ntpd']['kod']);
164
		}
165

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

    
172
		if (!empty($_POST['noquery'])) {
173
			$config['ntpd']['noquery'] = $_POST['noquery'];
174
		} elseif (isset($config['ntpd']['noquery'])) {
175
			unset($config['ntpd']['noquery']);
176
		}
177

    
178
		if (!empty($_POST['noserve'])) {
179
			$config['ntpd']['noserve'] = $_POST['noserve'];
180
		} elseif (isset($config['ntpd']['noserve'])) {
181
			unset($config['ntpd']['noserve']);
182
		}
183

    
184
		if (empty($_POST['nopeer'])) {
185
			$config['ntpd']['nopeer'] = 'on';
186
		} elseif (isset($config['ntpd']['nopeer'])) {
187
			unset($config['ntpd']['nopeer']);
188
		}
189

    
190
		if (empty($_POST['notrap'])) {
191
			$config['ntpd']['notrap'] = 'on';
192
		} elseif (isset($config['ntpd']['notrap'])) {
193
			unset($config['ntpd']['notrap']);
194
		}
195

    
196
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
197
			$enable_rrd_graphing = true;
198
		}
199
		if (!empty($_POST['statsgraph'])) {
200
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
201
		} elseif (isset($config['ntpd']['statsgraph'])) {
202
			unset($config['ntpd']['statsgraph']);
203
		}
204
		if (isset($enable_rrd_graphing)) {
205
			enable_rrd_graphing();
206
		}
207

    
208
		if (!empty($_POST['leaptxt'])) {
209
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptxt']);
210
		} elseif (isset($config['ntpd']['leapsec'])) {
211
			unset($config['ntpd']['leapsec']);
212
		}
213

    
214
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
215
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
216
		}
217

    
218
		write_config("Updated NTP Server Settings");
219

    
220
		$retval = 0;
221
		$retval = system_ntp_configure();
222
		$savemsg = get_std_save_message($retval);
223
	}
224
}
225

    
226
function build_interface_list() {
227
	global $pconfig;
228

    
229
	$iflist = array('options' => array(), 'selected' => array());
230

    
231
	$interfaces = get_configured_interface_with_descr();
232
	foreach ($interfaces as $iface => $ifacename) {
233
		if (!is_ipaddr(get_interface_ip($iface)) &&
234
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
235
			continue;
236
		}
237

    
238
		$iflist['options'][$iface] = $ifacename;
239

    
240
		if (in_array($iface, $pconfig['interface'])) {
241
			array_push($iflist['selected'], $iface);
242
		}
243
	}
244

    
245
	return($iflist);
246
}
247

    
248
$pconfig = &$config['ntpd'];
249
if (empty($pconfig['interface'])) {
250
	$pconfig['interface'] = array();
251
} else {
252
	$pconfig['interface'] = explode(",", $pconfig['interface']);
253
}
254
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
255
$shortcut_section = "ntp";
256
include("head.inc");
257

    
258
if ($input_errors) {
259
	print_input_errors($input_errors);
260
}
261
if ($savemsg) {
262
	print_info_box($savemsg, 'success');
263
}
264

    
265
$tab_array = array();
266
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
267
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
268
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
269
display_top_tabs($tab_array);
270

    
271
$form = new Form;
272

    
273
$section = new Form_Section('NTP Server Configuration');
274

    
275
$iflist = build_interface_list();
276

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

    
287
$timeservers = explode(' ', $config['system']['timeservers']);
288
$maxrows = max(count($timeservers), 1);
289
for ($counter=0; $counter < $maxrows; $counter++) {
290
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
291
	$group->addClass('repeatable');
292

    
293
	$group->add(new Form_Input(
294
		'server' . $counter,
295
		null,
296
		'text',
297
		$timeservers[$counter],
298
		['placeholder' => 'Hostname']
299
	 ))->setWidth(3);
300

    
301
	 $group->add(new Form_Checkbox(
302
		'servprefer' . $counter,
303
		null,
304
		null,
305
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
306
	 ))->sethelp('Prefer');
307

    
308
	 $group->add(new Form_Checkbox(
309
		'servselect' . $counter,
310
		null,
311
		null,
312
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
313
	 ))->sethelp('No Select');
314

    
315
	$group->add(new Form_Button(
316
		'deleterow' . $counter,
317
		'Delete'
318
	))->removeClass('btn-primary')->addClass('btn-warning');
319

    
320
	 $section->add($group);
321
}
322

    
323
$section->addInput(new Form_Button(
324
	'addrow',
325
	'Add'
326
))->removeClass('btn-primary')->addClass('btn-success');
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
	'Logging',
354
	'Log peer messages (default: disabled).',
355
	$pconfig['logpeer']
356
));
357

    
358
$section->addInput(new Form_Checkbox(
359
	'logsys',
360
	null,
361
	'Log 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="status_logs.php?logfile=ntpd">' . '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 persistent daily log files in /var/log/ntp.');
378

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

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

    
393
$section->addInput(new Form_Checkbox(
394
	'peerstats',
395
	null,
396
	'Log 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 server.');
412

    
413
$section->addInput(new Form_Checkbox(
414
	'kod',
415
	null,
416
	'Enable Kiss-o\'-death packets (default: checked).',
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: checked).',
424
	!$pconfig['nomodify']
425
));
426

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

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

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

    
448
$section->addInput(new Form_Checkbox(
449
	'notrap',
450
	null,
451
	'Deny mode 6 control message trap service (default: checked).',
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 type="text/javascript">
487
//<![CDATA[
488
	// If this variable is declared, any help text will not be deleted when rows are added
489
	// IOW the help text will appear on every row
490
	retainhelp = true;
491
</script>
492

    
493
<script type="text/javascript">
494
//<![CDATA[
495
events.push(function() {
496

    
497
	// Make the ‘clear’ button a plain button, not a submit button
498
	$('#btnadvstats').prop('type','button');
499

    
500
	// On click, show the controls in the stats section
501
	$("#btnadvstats").click(function() {
502
		hideCheckbox('clockstats', false);
503
		hideCheckbox('loopstats', false);
504
		hideCheckbox('peerstats', false);
505
	});
506

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

    
510
	// On click, show the controls in the restrictions section
511
	$("#btnadvrestr").click(function() {
512
		hideCheckbox('kod', false);
513
		hideCheckbox('nomodify', false);
514
		hideCheckbox('noquery', false);
515
		hideCheckbox('noserve', false);
516
		hideCheckbox('nopeer', false);
517
		hideCheckbox('notrap', false);
518
	});
519

    
520
	// Make the ‘btnleap’ button a plain button, not a submit button
521
	$('#btnleap').prop('type','button');
522

    
523
	// On click, show the controls in the leap seconds section
524
	$("#btnleap").click(function() {
525
		hideInput('leaptext', false);
526
		hideInput('leapfile', false);
527
	});
528

    
529
	// Set intial states
530
	hideCheckbox('clockstats', true);
531
	hideCheckbox('loopstats', true);
532
	hideCheckbox('peerstats', true);
533
	hideCheckbox('kod', true);
534
	hideCheckbox('nomodify', true);
535
	hideCheckbox('noquery', true);
536
	hideCheckbox('noserve', true);
537
	hideCheckbox('nopeer', true);
538
	hideCheckbox('notrap', true);
539
	hideInput('leaptext', true);
540
	hideInput('leapfile', true);
541

    
542
	// Suppress "Delete row" button if there are fewer than two rows
543
	checkLastRow();
544
});
545
//]]>
546
</script>
547

    
548
<?php include("foot.inc");
(131-131/229)