Project

General

Profile

Download (14.7 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_once("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['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
161
			$enable_rrd_graphing = true;
162
		}
163
		if (!empty($_POST['statsgraph'])) {
164
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
165
		} elseif (isset($config['ntpd']['statsgraph'])) {
166
			unset($config['ntpd']['statsgraph']);
167
		}
168
		if (isset($enable_rrd_graphing)) {
169
			enable_rrd_graphing();
170
		}
171

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

    
178
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
179
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
180
		}
181

    
182
		write_config("Updated NTP Server Settings");
183

    
184
		$retval = 0;
185
		$retval = system_ntp_configure();
186
		$savemsg = get_std_save_message($retval);
187
	}
188
}
189

    
190
function build_interface_list() {
191
	global $pconfig;
192

    
193
	$iflist = array('options' => array(), 'selected' => array());
194

    
195
	$interfaces = get_configured_interface_with_descr();
196
	foreach ($interfaces as $iface => $ifacename) {
197
		if (!is_ipaddr(get_interface_ip($iface)) &&
198
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
199
			continue;
200
		}
201

    
202
		$iflist['options'][$iface] = $ifacename;
203

    
204
		if (in_array($iface, $pconfig['interface'])) {
205
			array_push($iflist['selected'], $iface);
206
		}
207
	}
208

    
209
	return($iflist);
210
}
211

    
212
$pconfig = &$config['ntpd'];
213
if (empty($pconfig['interface'])) {
214
	$pconfig['interface'] = array();
215
} else {
216
	$pconfig['interface'] = explode(",", $pconfig['interface']);
217
}
218
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
219
$shortcut_section = "ntp";
220
include("head.inc");
221

    
222
if ($input_errors) {
223
	print_input_errors($input_errors);
224
}
225
if ($savemsg) {
226
	print_info_box($savemsg, 'success');
227
}
228

    
229
$tab_array = array();
230
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
231
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
232
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
233
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
234
display_top_tabs($tab_array);
235

    
236
$form = new Form;
237
$form->setMultipartEncoding();	// Allow file uploads
238

    
239
$section = new Form_Section('NTP Server Configuration');
240

    
241
$iflist = build_interface_list();
242

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

    
253
$timeservers = explode(' ', $config['system']['timeservers']);
254
$maxrows = max(count($timeservers), 1);
255
for ($counter=0; $counter < $maxrows; $counter++) {
256
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
257
	$group->addClass('repeatable');
258

    
259
	$group->add(new Form_Input(
260
		'server' . $counter,
261
		null,
262
		'text',
263
		$timeservers[$counter],
264
		['placeholder' => 'Hostname']
265
	 ))->setWidth(3);
266

    
267
	 $group->add(new Form_Checkbox(
268
		'servprefer' . $counter,
269
		null,
270
		null,
271
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
272
	 ))->sethelp('Prefer');
273

    
274
	 $group->add(new Form_Checkbox(
275
		'servselect' . $counter,
276
		null,
277
		null,
278
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
279
	 ))->sethelp('No Select');
280

    
281
	$group->add(new Form_Button(
282
		'deleterow' . $counter,
283
		'Delete',
284
		null,
285
		'fa-trash'
286
	))->addClass('btn-warning');
287

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

    
291
$section->addInput(new Form_Button(
292
	'addrow',
293
	'Add',
294
	null,
295
	'fa-plus'
296
))->addClass('btn-success');
297

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

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

    
314
$section->addInput(new Form_Checkbox(
315
	'statsgraph',
316
	'NTP Graphs',
317
	'Enable RRD graphs of NTP statistics (default: disabled).',
318
	$pconfig['statsgraph']
319
));
320

    
321
$section->addInput(new Form_Checkbox(
322
	'logpeer',
323
	'Logging',
324
	'Log peer messages (default: disabled).',
325
	$pconfig['logpeer']
326
));
327

    
328
$section->addInput(new Form_Checkbox(
329
	'logsys',
330
	null,
331
	'Log system messages (default: disabled).',
332
	$pconfig['logsys']
333
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
334
			'<a href="status_logs.php?logfile=ntpd">' . 'Status > System Logs > NTP' . '</a>.');
335

    
336
// Statistics logging section
337
$btnadv = new Form_Button(
338
	'btnadvstats',
339
	'Display Advanced',
340
	null,
341
	'fa-cog'
342
);
343

    
344
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
345

    
346
$section->addInput(new Form_StaticText(
347
	'Statistics Logging',
348
	$btnadv
349
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
350

    
351
$section->addInput(new Form_Checkbox(
352
	'clockstats',
353
	null,
354
	'Log reference clock statistics (default: disabled).',
355
	$pconfig['clockstats']
356
));
357

    
358
$section->addInput(new Form_Checkbox(
359
	'loopstats',
360
	null,
361
	'Log clock discipline statistics (default: disabled).',
362
	$pconfig['loopstats']
363
));
364

    
365
$section->addInput(new Form_Checkbox(
366
	'peerstats',
367
	null,
368
	'Log NTP peer statistics (default: disabled).',
369
	$pconfig['peerstats']
370
));
371

    
372
// Leap seconds section
373
$btnadv = new Form_Button(
374
	'btnadvleap',
375
	'Display Advanced',
376
	null,
377
	'fa-cog'
378
);
379

    
380
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
381

    
382
$section->addInput(new Form_StaticText(
383
	'Leap seconds',
384
	$btnadv
385
))->setHelp('A leap second file allows NTP to advertise an upcoming leap second addition or subtraction. ' .
386
			'Normally this is only useful if this server is a stratum 1 time server. ');
387

    
388
$section->addInput(new Form_Textarea(
389
	'leaptext',
390
	null,
391
	base64_decode(chunk_split($pconfig['leapsec']))
392
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
393

    
394
$section->addInput(new Form_Input(
395
	'leapfile',
396
	null,
397
	'file'
398
))->addClass('btn-default');
399

    
400
$form->add($section);
401

    
402
print($form);
403

    
404
?>
405

    
406
<script type="text/javascript">
407
//<![CDATA[
408
	// If this variable is declared, any help text will not be deleted when rows are added
409
	// IOW the help text will appear on every row
410
	retainhelp = true;
411
</script>
412

    
413
<script type="text/javascript">
414
//<![CDATA[
415
events.push(function() {
416

    
417
	// Show advanced stats options ============================================
418
	var showadvstats = false;
419

    
420
	function show_advstats(ispageload) {
421
		var text;
422
		// On page load decide the initial state based on the data.
423
		if (ispageload) {
424
<?php
425
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
426
				$showadv = false;
427
			} else {
428
				$showadv = true;
429
			}
430
?>
431
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
432
		} else {
433
			// It was a click, swap the state.
434
			showadvstats = !showadvstats;
435
		}
436

    
437
		hideCheckbox('clockstats', !showadvstats);
438
		hideCheckbox('loopstats', !showadvstats);
439
		hideCheckbox('peerstats', !showadvstats);
440

    
441
		if (showadvstats) {
442
			text = "<?=gettext('Hide Advanced');?>";
443
		} else {
444
			text = "<?=gettext('Display Advanced');?>";
445
		}
446
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
447
	}
448

    
449
	$('#btnadvstats').click(function(event) {
450
		show_advstats();
451
	});
452

    
453
	// Show advanced leap second options ======================================
454
	var showadvleap = false;
455

    
456
	function show_advleap(ispageload) {
457
		var text;
458
		// On page load decide the initial state based on the data.
459
		if (ispageload) {
460
<?php
461
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
462
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
463
			if (empty($pconfig['leapsec'])) {
464
				$showadv = false;
465
			} else {
466
				$showadv = true;
467
			}
468
?>
469
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
470
		} else {
471
			// It was a click, swap the state.
472
			showadvleap = !showadvleap;
473
		}
474

    
475
		hideInput('leaptext', !showadvleap);
476
		hideInput('leapfile', !showadvleap);
477

    
478
		if (showadvleap) {
479
			text = "<?=gettext('Hide Advanced');?>";
480
		} else {
481
			text = "<?=gettext('Display Advanced');?>";
482
		}
483
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
484
	}
485

    
486
	$('#btnadvleap').click(function(event) {
487
		show_advleap();
488
	});
489

    
490
	// Set initial states
491
	show_advstats(true);
492
	show_advleap(true);
493

    
494
	// Suppress "Delete row" button if there are fewer than two rows
495
	checkLastRow();
496
});
497
//]]>
498
</script>
499

    
500
<?php include("foot.inc");
(130-130/225)