Project

General

Profile

Download (13 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
		$timeservers = '';
72

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

    
90
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
91
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
92
		} elseif (isset($config['ntpd']['orphan'])) {
93
			unset($config['ntpd']['orphan']);
94
		}
95

    
96
		if (!empty($_POST['logpeer'])) {
97
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
98
		} elseif (isset($config['ntpd']['logpeer'])) {
99
			unset($config['ntpd']['logpeer']);
100
		}
101

    
102
		if (!empty($_POST['logsys'])) {
103
			$config['ntpd']['logsys'] = $_POST['logsys'];
104
		} elseif (isset($config['ntpd']['logsys'])) {
105
			unset($config['ntpd']['logsys']);
106
		}
107

    
108
		if (!empty($_POST['clockstats'])) {
109
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
110
		} elseif (isset($config['ntpd']['clockstats'])) {
111
			unset($config['ntpd']['clockstats']);
112
		}
113

    
114
		if (!empty($_POST['loopstats'])) {
115
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
116
		} elseif (isset($config['ntpd']['loopstats'])) {
117
			unset($config['ntpd']['loopstats']);
118
		}
119

    
120
		if (!empty($_POST['peerstats'])) {
121
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
122
		} elseif (isset($config['ntpd']['peerstats'])) {
123
			unset($config['ntpd']['peerstats']);
124
		}
125

    
126
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
127
			$enable_rrd_graphing = true;
128
		}
129
		if (!empty($_POST['statsgraph'])) {
130
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
131
		} elseif (isset($config['ntpd']['statsgraph'])) {
132
			unset($config['ntpd']['statsgraph']);
133
		}
134
		if (isset($enable_rrd_graphing)) {
135
			enable_rrd_graphing();
136
		}
137

    
138
		if (!empty($_POST['leaptext'])) {
139
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptext']);
140
		} elseif (isset($config['ntpd']['leapsec'])) {
141
			unset($config['ntpd']['leapsec']);
142
		}
143

    
144
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
145
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
146
		}
147

    
148
		write_config("Updated NTP Server Settings");
149

    
150
		$retval = 0;
151
		$retval = system_ntp_configure();
152
		$savemsg = get_std_save_message($retval);
153
	}
154
}
155

    
156
function build_interface_list() {
157
	global $pconfig;
158

    
159
	$iflist = array('options' => array(), 'selected' => array());
160

    
161
	$interfaces = get_configured_interface_with_descr();
162
	foreach ($interfaces as $iface => $ifacename) {
163
		if (!is_ipaddr(get_interface_ip($iface)) &&
164
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
165
			continue;
166
		}
167

    
168
		$iflist['options'][$iface] = $ifacename;
169

    
170
		if (in_array($iface, $pconfig['interface'])) {
171
			array_push($iflist['selected'], $iface);
172
		}
173
	}
174

    
175
	return($iflist);
176
}
177

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

    
188
if ($input_errors) {
189
	print_input_errors($input_errors);
190
}
191
if ($savemsg) {
192
	print_info_box($savemsg, 'success');
193
}
194

    
195
$tab_array = array();
196
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
197
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
198
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
199
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
200
display_top_tabs($tab_array);
201

    
202
$form = new Form;
203
$form->setMultipartEncoding();	// Allow file uploads
204

    
205
$section = new Form_Section('NTP Server Configuration');
206

    
207
$iflist = build_interface_list();
208

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

    
219
$timeservers = explode(' ', $config['system']['timeservers']);
220
$maxrows = max(count($timeservers), 1);
221
for ($counter=0; $counter < $maxrows; $counter++) {
222
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
223
	$group->addClass('repeatable');
224

    
225
	$group->add(new Form_Input(
226
		'server' . $counter,
227
		null,
228
		'text',
229
		$timeservers[$counter],
230
		['placeholder' => 'Hostname']
231
	 ))->setWidth(3);
232

    
233
	 $group->add(new Form_Checkbox(
234
		'servprefer' . $counter,
235
		null,
236
		null,
237
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
238
	 ))->sethelp('Prefer');
239

    
240
	 $group->add(new Form_Checkbox(
241
		'servselect' . $counter,
242
		null,
243
		null,
244
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
245
	 ))->sethelp('No Select');
246

    
247
	$group->add(new Form_Button(
248
		'deleterow' . $counter,
249
		'Delete',
250
		null,
251
		'fa-trash'
252
	))->addClass('btn-warning');
253

    
254
	 $section->add($group);
255
}
256

    
257
$section->addInput(new Form_Button(
258
	'addrow',
259
	'Add',
260
	null,
261
	'fa-plus'
262
))->addClass('btn-success');
263

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

    
271
$section->addInput(new Form_Input(
272
	'ntporphan',
273
	'Orphan Mode',
274
	'text',
275
	$pconfig['ntporphan']
276
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
277
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
278
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
279

    
280
$section->addInput(new Form_Checkbox(
281
	'statsgraph',
282
	'NTP Graphs',
283
	'Enable RRD graphs of NTP statistics (default: disabled).',
284
	$pconfig['statsgraph']
285
));
286

    
287
$section->addInput(new Form_Checkbox(
288
	'logpeer',
289
	'Logging',
290
	'Log peer messages (default: disabled).',
291
	$pconfig['logpeer']
292
));
293

    
294
$section->addInput(new Form_Checkbox(
295
	'logsys',
296
	null,
297
	'Log system messages (default: disabled).',
298
	$pconfig['logsys']
299
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
300
			'<a href="status_logs.php?logfile=ntpd">' . 'Status > System Logs > NTP' . '</a>.');
301

    
302
// Statistics logging section
303
$btnadv = new Form_Button(
304
	'btnadvstats',
305
	'Display Advanced',
306
	null,
307
	'fa-cog'
308
);
309

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

    
312
$section->addInput(new Form_StaticText(
313
	'Statistics Logging',
314
	$btnadv
315
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
316

    
317
$section->addInput(new Form_Checkbox(
318
	'clockstats',
319
	null,
320
	'Log reference clock statistics (default: disabled).',
321
	$pconfig['clockstats']
322
));
323

    
324
$section->addInput(new Form_Checkbox(
325
	'loopstats',
326
	null,
327
	'Log clock discipline statistics (default: disabled).',
328
	$pconfig['loopstats']
329
));
330

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

    
338
// Leap seconds section
339
$btnadv = new Form_Button(
340
	'btnadvleap',
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
	'Leap seconds',
350
	$btnadv
351
))->setHelp('A leap second file allows NTP to advertise an upcoming leap second addition or subtraction. ' .
352
			'Normally this is only useful if this server is a stratum 1 time server. ');
353

    
354
$section->addInput(new Form_Textarea(
355
	'leaptext',
356
	null,
357
	base64_decode(chunk_split($pconfig['leapsec']))
358
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
359

    
360
$section->addInput(new Form_Input(
361
	'leapfile',
362
	null,
363
	'file'
364
))->addClass('btn-default');
365

    
366
$form->add($section);
367

    
368
print($form);
369

    
370
?>
371

    
372
<script type="text/javascript">
373
//<![CDATA[
374
	// If this variable is declared, any help text will not be deleted when rows are added
375
	// IOW the help text will appear on every row
376
	retainhelp = true;
377
</script>
378

    
379
<script type="text/javascript">
380
//<![CDATA[
381
events.push(function() {
382

    
383
	// Show advanced stats options ============================================
384
	var showadvstats = false;
385

    
386
	function show_advstats(ispageload) {
387
		var text;
388
		// On page load decide the initial state based on the data.
389
		if (ispageload) {
390
<?php
391
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
392
				$showadv = false;
393
			} else {
394
				$showadv = true;
395
			}
396
?>
397
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
398
		} else {
399
			// It was a click, swap the state.
400
			showadvstats = !showadvstats;
401
		}
402

    
403
		hideCheckbox('clockstats', !showadvstats);
404
		hideCheckbox('loopstats', !showadvstats);
405
		hideCheckbox('peerstats', !showadvstats);
406

    
407
		if (showadvstats) {
408
			text = "<?=gettext('Hide Advanced');?>";
409
		} else {
410
			text = "<?=gettext('Display Advanced');?>";
411
		}
412
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
413
	}
414

    
415
	$('#btnadvstats').click(function(event) {
416
		show_advstats();
417
	});
418

    
419
	// Show advanced leap second options ======================================
420
	var showadvleap = false;
421

    
422
	function show_advleap(ispageload) {
423
		var text;
424
		// On page load decide the initial state based on the data.
425
		if (ispageload) {
426
<?php
427
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
428
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
429
			if (empty($pconfig['leapsec'])) {
430
				$showadv = false;
431
			} else {
432
				$showadv = true;
433
			}
434
?>
435
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
436
		} else {
437
			// It was a click, swap the state.
438
			showadvleap = !showadvleap;
439
		}
440

    
441
		hideInput('leaptext', !showadvleap);
442
		hideInput('leapfile', !showadvleap);
443

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

    
452
	$('#btnadvleap').click(function(event) {
453
		show_advleap();
454
	});
455

    
456
	// Set initial states
457
	show_advstats(true);
458
	show_advleap(true);
459

    
460
	// Suppress "Delete row" button if there are fewer than two rows
461
	checkLastRow();
462
});
463
//]]>
464
</script>
465

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