Project

General

Profile

Download (13.7 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 (!$input_errors) {
57
		if (is_array($_POST['interface'])) {
58
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
59
		} elseif (isset($config['ntpd']['interface'])) {
60
			unset($config['ntpd']['interface']);
61
		}
62

    
63
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
64
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
65
		} elseif (isset($config['ntpd']['gpsport'])) {
66
			unset($config['ntpd']['gpsport']);
67
		}
68

    
69
		unset($config['ntpd']['prefer']);
70
		unset($config['ntpd']['noselect']);
71
		unset($config['ntpd']['ispool']);
72
		$timeservers = '';
73

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

    
94
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
95
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
96
		} elseif (isset($config['ntpd']['orphan'])) {
97
			unset($config['ntpd']['orphan']);
98
		}
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
		$changes_applied = true;
155
		$retval = 0;
156
		$retval |= system_ntp_configure();
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

    
196
if ($changes_applied) {
197
	print_apply_result_box($retval);
198
}
199

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

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

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

    
212
$iflist = build_interface_list();
213

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

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

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

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

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

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

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

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

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

    
278
$section->addInput(new Form_StaticText(
279
	null,
280
	$btnaddrow
281
))->setHelp('For best results three to five servers should be configured here, or at least one pool.' . '<br />' .
282
			'The <b>Prefer</b> option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
283
			'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 />' .
284
			'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.');
285

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
384
print($form);
385

    
386
?>
387

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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