Project

General

Profile

Download (13.8 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
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-services-ntpd
25
##|*NAME=Services: NTP Settings
26
##|*DESCR=Allow access to the 'Services: NTP Settings' page.
27
##|*MATCH=services_ntpd.php*
28
##|-PRIV
29

    
30
define('NUMTIMESERVERS', 10);		// The maximum number of configurable time servers
31
require_once("guiconfig.inc");
32
require_once('rrd.inc');
33
require_once("shaper.inc");
34

    
35
if (!is_array($config['ntpd'])) {
36
	$config['ntpd'] = array();
37
}
38

    
39
if (empty($config['ntpd']['interface'])) {
40
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
41
	    is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
42
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
43
		unset($config['installedpackages']['openntpd']);
44
		write_config(gettext("Upgraded settings from openttpd"));
45
	} else {
46
		$pconfig['interface'] = array();
47
	}
48
} else {
49
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
50
}
51

    
52
if ($_POST) {
53
	unset($input_errors);
54
	$pconfig = $_POST;
55

    
56
	if ((strlen($pconfig['ntporphan']) > 0) && (!is_numericint($pconfig['ntporphan']) || ($pconfig['ntporphan'] < 1) || ($pconfig['ntporphan'] > 15))) {
57
		$input_errors[] = gettext("The supplied value for NTP Orphan Mode is invalid.");
58
	}
59

    
60
	if (!$input_errors) {
61
		if (is_array($_POST['interface'])) {
62
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
63
		} elseif (isset($config['ntpd']['interface'])) {
64
			unset($config['ntpd']['interface']);
65
		}
66

    
67
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
68
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
69
		} elseif (isset($config['ntpd']['gpsport'])) {
70
			unset($config['ntpd']['gpsport']);
71
		}
72

    
73
		unset($config['ntpd']['prefer']);
74
		unset($config['ntpd']['noselect']);
75
		unset($config['ntpd']['ispool']);
76
		$timeservers = '';
77

    
78
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
79
			$tserver = trim($_POST["server{$i}"]);
80
			if (!empty($tserver)) {
81
				$timeservers .= "{$tserver} ";
82
				if (!empty($_POST["servprefer{$i}"])) {
83
					$config['ntpd']['prefer'] .= "{$tserver} ";
84
				}
85
				if (!empty($_POST["servselect{$i}"])) {
86
					$config['ntpd']['noselect'] .= "{$tserver} ";
87
				}
88
				if (!empty($_POST["servispool{$i}"])) {
89
					$config['ntpd']['ispool'] .= "{$tserver} ";
90
				}
91
			}
92
		}
93
		if (trim($timeservers) == "") {
94
			$timeservers = "pool.ntp.org";
95
		}
96
		$config['system']['timeservers'] = trim($timeservers);
97

    
98
		$config['ntpd']['orphan'] = trim($pconfig['ntporphan']);
99

    
100
		if (!empty($_POST['logpeer'])) {
101
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
102
		} elseif (isset($config['ntpd']['logpeer'])) {
103
			unset($config['ntpd']['logpeer']);
104
		}
105

    
106
		if (!empty($_POST['logsys'])) {
107
			$config['ntpd']['logsys'] = $_POST['logsys'];
108
		} elseif (isset($config['ntpd']['logsys'])) {
109
			unset($config['ntpd']['logsys']);
110
		}
111

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

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

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

    
130
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
131
			$enable_rrd_graphing = true;
132
		}
133
		if (!empty($_POST['statsgraph'])) {
134
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
135
		} elseif (isset($config['ntpd']['statsgraph'])) {
136
			unset($config['ntpd']['statsgraph']);
137
		}
138
		if (isset($enable_rrd_graphing)) {
139
			enable_rrd_graphing();
140
		}
141

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

    
148
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
149
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
150
		}
151

    
152
		write_config("Updated NTP Server Settings");
153

    
154
		$retval = 0;
155
		$retval = system_ntp_configure();
156
		$savemsg = get_std_save_message($retval);
157
	}
158
}
159

    
160
function build_interface_list() {
161
	global $pconfig;
162

    
163
	$iflist = array('options' => array(), 'selected' => array());
164

    
165
	$interfaces = get_configured_interface_with_descr();
166
	foreach ($interfaces as $iface => $ifacename) {
167
		if (!is_ipaddr(get_interface_ip($iface)) &&
168
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
169
			continue;
170
		}
171

    
172
		$iflist['options'][$iface] = $ifacename;
173

    
174
		if (in_array($iface, $pconfig['interface'])) {
175
			array_push($iflist['selected'], $iface);
176
		}
177
	}
178

    
179
	return($iflist);
180
}
181

    
182
$pconfig = &$config['ntpd'];
183
if (empty($pconfig['interface'])) {
184
	$pconfig['interface'] = array();
185
} else {
186
	$pconfig['interface'] = explode(",", $pconfig['interface']);
187
}
188
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
189
$shortcut_section = "ntp";
190
include("head.inc");
191

    
192
if ($input_errors) {
193
	print_input_errors($input_errors);
194
}
195
if ($savemsg) {
196
	print_info_box($savemsg, 'success');
197
}
198

    
199
$tab_array = array();
200
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
201
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
202
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
203
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
204
display_top_tabs($tab_array);
205

    
206
$form = new Form;
207
$form->setMultipartEncoding();	// Allow file uploads
208

    
209
$section = new Form_Section('NTP Server Configuration');
210

    
211
$iflist = build_interface_list();
212

    
213
$section->addInput(new Form_Select(
214
	'interface',
215
	'Interface',
216
	$iflist['selected'],
217
	$iflist['options'],
218
	true
219
))->setHelp('Interfaces without an IP address will not be shown.' . '<br />' .
220
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
221
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
222

    
223
$timeservers = explode(' ', $config['system']['timeservers']);
224
$maxrows = max(count($timeservers), 1);
225
$auto_pool_suffix = "pool.ntp.org";
226
for ($counter=0; $counter < $maxrows; $counter++) {
227
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
228
	$group->addClass('repeatable');
229

    
230
	$group->add(new Form_Input(
231
		'server' . $counter,
232
		null,
233
		'text',
234
		$timeservers[$counter],
235
		['placeholder' => 'Hostname']
236
	 ))->setWidth(3);
237

    
238
	 $group->add(new Form_Checkbox(
239
		'servprefer' . $counter,
240
		null,
241
		null,
242
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
243
	 ))->sethelp('Prefer');
244

    
245
	 $group->add(new Form_Checkbox(
246
		'servselect' . $counter,
247
		null,
248
		null,
249
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
250
	 ))->sethelp('No Select');
251

    
252
	$group->add(new Form_Checkbox(
253
		'servispool' . $counter,
254
		null,
255
		null,
256
		(substr_compare($timeservers[$counter], $auto_pool_suffix, strlen($timeservers[$counter]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0)
257
		 || (isset($config['ntpd']['ispool']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['ispool'], $timeservers[$counter]))
258
	 ))->sethelp('Is a Pool');
259

    
260
	$group->add(new Form_Button(
261
		'deleterow' . $counter,
262
		'Delete',
263
		null,
264
		'fa-trash'
265
	))->addClass('btn-warning');
266

    
267
	 $section->add($group);
268
}
269

    
270
$section->addInput(new Form_Button(
271
	'addrow',
272
	'Add',
273
	null,
274
	'fa-plus'
275
))->addClass('btn-success');
276

    
277
$section->addInput(new Form_StaticText(
278
	null,
279
	$btnaddrow
280
))->setHelp('For best results three to five servers should be configured here, or at least one pool.' . '<br />' .
281
			'The <b>Prefer</b> option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
282
			'The <b>No Select</b> option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.' . '<br />' .
283
			'The <b>Is a Pool</b> option indicates this entry is a pool of NTP servers and not a single address. This is assumed for *.pool.ntp.org.');
284

    
285
$section->addInput(new Form_Input(
286
	'ntporphan',
287
	'Orphan Mode',
288
	'text',
289
	$pconfig['orphan'],
290
	['placeholder' => "12"]
291
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
292
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
293
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
294

    
295
$section->addInput(new Form_Checkbox(
296
	'statsgraph',
297
	'NTP Graphs',
298
	'Enable RRD graphs of NTP statistics (default: disabled).',
299
	$pconfig['statsgraph']
300
));
301

    
302
$section->addInput(new Form_Checkbox(
303
	'logpeer',
304
	'Logging',
305
	'Log peer messages (default: disabled).',
306
	$pconfig['logpeer']
307
));
308

    
309
$section->addInput(new Form_Checkbox(
310
	'logsys',
311
	null,
312
	'Log system messages (default: disabled).',
313
	$pconfig['logsys']
314
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
315
			'<a href="status_logs.php?logfile=ntpd">' . 'Status > System Logs > NTP' . '</a>.');
316

    
317
// Statistics logging section
318
$btnadv = new Form_Button(
319
	'btnadvstats',
320
	'Display Advanced',
321
	null,
322
	'fa-cog'
323
);
324

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

    
327
$section->addInput(new Form_StaticText(
328
	'Statistics Logging',
329
	$btnadv
330
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
331

    
332
$section->addInput(new Form_Checkbox(
333
	'clockstats',
334
	null,
335
	'Log reference clock statistics (default: disabled).',
336
	$pconfig['clockstats']
337
));
338

    
339
$section->addInput(new Form_Checkbox(
340
	'loopstats',
341
	null,
342
	'Log clock discipline statistics (default: disabled).',
343
	$pconfig['loopstats']
344
));
345

    
346
$section->addInput(new Form_Checkbox(
347
	'peerstats',
348
	null,
349
	'Log NTP peer statistics (default: disabled).',
350
	$pconfig['peerstats']
351
));
352

    
353
// Leap seconds section
354
$btnadv = new Form_Button(
355
	'btnadvleap',
356
	'Display Advanced',
357
	null,
358
	'fa-cog'
359
);
360

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

    
363
$section->addInput(new Form_StaticText(
364
	'Leap seconds',
365
	$btnadv
366
))->setHelp('A leap second file allows NTP to advertise an upcoming leap second addition or subtraction. ' .
367
			'Normally this is only useful if this server is a stratum 1 time server. ');
368

    
369
$section->addInput(new Form_Textarea(
370
	'leaptext',
371
	null,
372
	base64_decode(chunk_split($pconfig['leapsec']))
373
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
374

    
375
$section->addInput(new Form_Input(
376
	'leapfile',
377
	null,
378
	'file'
379
))->addClass('btn-default');
380

    
381
$form->add($section);
382

    
383
print($form);
384

    
385
?>
386

    
387
<script type="text/javascript">
388
//<![CDATA[
389
	// If this variable is declared, any help text will not be deleted when rows are added
390
	// IOW the help text will appear on every row
391
	retainhelp = true;
392
</script>
393

    
394
<script type="text/javascript">
395
//<![CDATA[
396
events.push(function() {
397

    
398
	// Show advanced stats options ============================================
399
	var showadvstats = false;
400

    
401
	function show_advstats(ispageload) {
402
		var text;
403
		// On page load decide the initial state based on the data.
404
		if (ispageload) {
405
<?php
406
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
407
				$showadv = false;
408
			} else {
409
				$showadv = true;
410
			}
411
?>
412
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
413
		} else {
414
			// It was a click, swap the state.
415
			showadvstats = !showadvstats;
416
		}
417

    
418
		hideCheckbox('clockstats', !showadvstats);
419
		hideCheckbox('loopstats', !showadvstats);
420
		hideCheckbox('peerstats', !showadvstats);
421

    
422
		if (showadvstats) {
423
			text = "<?=gettext('Hide Advanced');?>";
424
		} else {
425
			text = "<?=gettext('Display Advanced');?>";
426
		}
427
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
428
	}
429

    
430
	$('#btnadvstats').click(function(event) {
431
		show_advstats();
432
	});
433

    
434
	// Show advanced leap second options ======================================
435
	var showadvleap = false;
436

    
437
	function show_advleap(ispageload) {
438
		var text;
439
		// On page load decide the initial state based on the data.
440
		if (ispageload) {
441
<?php
442
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
443
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
444
			if (empty($pconfig['leapsec'])) {
445
				$showadv = false;
446
			} else {
447
				$showadv = true;
448
			}
449
?>
450
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
451
		} else {
452
			// It was a click, swap the state.
453
			showadvleap = !showadvleap;
454
		}
455

    
456
		hideInput('leaptext', !showadvleap);
457
		hideInput('leapfile', !showadvleap);
458

    
459
		if (showadvleap) {
460
			text = "<?=gettext('Hide Advanced');?>";
461
		} else {
462
			text = "<?=gettext('Display Advanced');?>";
463
		}
464
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
465
	}
466

    
467
	$('#btnadvleap').click(function(event) {
468
		show_advleap();
469
	});
470

    
471
	// Set initial states
472
	show_advstats(true);
473
	show_advleap(true);
474

    
475
	// Suppress "Delete row" button if there are fewer than two rows
476
	checkLastRow();
477
});
478
//]]>
479
</script>
480

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