Project

General

Profile

Download (14.9 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 Rubicon Communications, LLC (Netgate)
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
$pglinks = array("", "@self", "@self");
218
$shortcut_section = "ntp";
219
include("head.inc");
220

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

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

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

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

    
240
$iflist = build_interface_list();
241

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

    
252
$timeservers = explode(' ', $config['system']['timeservers']);
253
$maxrows = max(count($timeservers), 1);
254
for ($counter=0; $counter < $maxrows; $counter++) {
255
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
256
	$group->addClass('repeatable');
257
	$group->setAttribute('max_repeats', NUMTIMESERVERS);
258
	$group->setAttribute('max_repeats_alert', sprintf(gettext('%d is the maximum number of configured servers.'), NUMTIMESERVERS));
259

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
402
$form->add($section);
403

    
404
print($form);
405

    
406
?>
407

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

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

    
419
	// Show advanced stats options ============================================
420
	var showadvstats = false;
421

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

    
439
		hideCheckbox('clockstats', !showadvstats);
440
		hideCheckbox('loopstats', !showadvstats);
441
		hideCheckbox('peerstats', !showadvstats);
442

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

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

    
455
	// Show advanced leap second options ======================================
456
	var showadvleap = false;
457

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

    
477
		hideInput('leaptext', !showadvleap);
478
		hideInput('leapfile', !showadvleap);
479

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

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

    
492
	// Set initial states
493
	show_advstats(true);
494
	show_advleap(true);
495

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

    
502
<?php include("foot.inc");
(135-135/230)