Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
180
		write_config("Updated NTP Server Settings");
181

    
182
		$retval = 0;
183
		$retval = system_ntp_configure();
184
		$savemsg = get_std_save_message($retval);
185
	}
186
}
187

    
188
function build_interface_list() {
189
	global $pconfig;
190

    
191
	$iflist = array('options' => array(), 'selected' => array());
192

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

    
200
		$iflist['options'][$iface] = $ifacename;
201

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

    
207
	return($iflist);
208
}
209

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

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

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

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

    
237
$section = new Form_Section('NTP Server Configuration');
238

    
239
$iflist = build_interface_list();
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
398
$form->add($section);
399

    
400
print($form);
401

    
402
?>
403

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

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

    
415
	// Show advanced stats options ============================================
416
	var showadvstats = false;
417

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

    
435
		hideCheckbox('clockstats', !showadvstats);
436
		hideCheckbox('loopstats', !showadvstats);
437
		hideCheckbox('peerstats', !showadvstats);
438

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

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

    
451
	// Show advanced leap second options ======================================
452
	var showadvleap = false;
453

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

    
473
		hideInput('leaptext', !showadvleap);
474
		hideInput('leapfile', !showadvleap);
475

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

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

    
488
	// Set initial states
489
	show_advstats(true);
490
	show_advleap(true);
491

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

    
498
<?php include("foot.inc");
(132-132/227)