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
		$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['ntporphan']
290
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
291
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
292
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
293

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
382
print($form);
383

    
384
?>
385

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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