Project

General

Profile

Download (16.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_ntpd.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2013 Dagorlad
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56

    
57
##|+PRIV
58
##|*IDENT=page-services-ntpd
59
##|*NAME=Services: NTP
60
##|*DESCR=Allow access to the 'Services: NTP' page.
61
##|*MATCH=services_ntpd.php*
62
##|-PRIV
63

    
64
define(NUMTIMESERVERS, 10);		// The maximum number of configurable time servers
65
require("guiconfig.inc");
66
require_once('rrd.inc');
67
require_once("shaper.inc");
68

    
69
if (!is_array($config['ntpd'])) {
70
	$config['ntpd'] = array();
71
}
72

    
73
if (empty($config['ntpd']['interface'])) {
74
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
75
	    is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
76
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
77
		unset($config['installedpackages']['openntpd']);
78
		write_config("Upgraded settings from openttpd");
79
	} else {
80
		$pconfig['interface'] = array();
81
	}
82
} else {
83
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
84
}
85

    
86
if ($_POST) {
87
	unset($input_errors);
88
	$pconfig = $_POST;
89

    
90
	if (!$input_errors) {
91
		if (is_array($_POST['interface'])) {
92
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
93
		} elseif (isset($config['ntpd']['interface'])) {
94
			unset($config['ntpd']['interface']);
95
		}
96

    
97
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
98
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
99
		} elseif (isset($config['ntpd']['gpsport'])) {
100
			unset($config['ntpd']['gpsport']);
101
		}
102

    
103
		unset($config['ntpd']['prefer']);
104
		unset($config['ntpd']['noselect']);
105
		$timeservers = '';
106

    
107
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
108
			$tserver = trim($_POST["server{$i}"]);
109
			if (!empty($tserver)) {
110
				$timeservers .= "{$tserver} ";
111
				if (!empty($_POST["servprefer{$i}"])) {
112
					$config['ntpd']['prefer'] .= "{$tserver} ";
113
				}
114
				if (!empty($_POST["servselect{$i}"])) {
115
					$config['ntpd']['noselect'] .= "{$tserver} ";
116
				}
117
			}
118
		}
119
		if (trim($timeservers) == "") {
120
			$timeservers = "pool.ntp.org";
121
		}
122
		$config['system']['timeservers'] = trim($timeservers);
123

    
124
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
125
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
126
		} elseif (isset($config['ntpd']['orphan'])) {
127
			unset($config['ntpd']['orphan']);
128
		}
129

    
130
		if (!empty($_POST['logpeer'])) {
131
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
132
		} elseif (isset($config['ntpd']['logpeer'])) {
133
			unset($config['ntpd']['logpeer']);
134
		}
135

    
136
		if (!empty($_POST['logsys'])) {
137
			$config['ntpd']['logsys'] = $_POST['logsys'];
138
		} elseif (isset($config['ntpd']['logsys'])) {
139
			unset($config['ntpd']['logsys']);
140
		}
141

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

    
148
		if (!empty($_POST['loopstats'])) {
149
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
150
		} elseif (isset($config['ntpd']['loopstats'])) {
151
			unset($config['ntpd']['loopstats']);
152
		}
153

    
154
		if (!empty($_POST['peerstats'])) {
155
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
156
		} elseif (isset($config['ntpd']['peerstats'])) {
157
			unset($config['ntpd']['peerstats']);
158
		}
159

    
160
		if (empty($_POST['kod'])) {
161
			$config['ntpd']['kod'] = 'on';
162
		} elseif (isset($config['ntpd']['kod'])) {
163
			unset($config['ntpd']['kod']);
164
		}
165

    
166
		if (empty($_POST['nomodify'])) {
167
			$config['ntpd']['nomodify'] = 'on';
168
		} elseif (isset($config['ntpd']['nomodify'])) {
169
			unset($config['ntpd']['nomodify']);
170
		}
171

    
172
		if (!empty($_POST['noquery'])) {
173
			$config['ntpd']['noquery'] = $_POST['noquery'];
174
		} elseif (isset($config['ntpd']['noquery'])) {
175
			unset($config['ntpd']['noquery']);
176
		}
177

    
178
		if (!empty($_POST['noserve'])) {
179
			$config['ntpd']['noserve'] = $_POST['noserve'];
180
		} elseif (isset($config['ntpd']['noserve'])) {
181
			unset($config['ntpd']['noserve']);
182
		}
183

    
184
		if (empty($_POST['nopeer'])) {
185
			$config['ntpd']['nopeer'] = 'on';
186
		} elseif (isset($config['ntpd']['nopeer'])) {
187
			unset($config['ntpd']['nopeer']);
188
		}
189

    
190
		if (empty($_POST['notrap'])) {
191
			$config['ntpd']['notrap'] = 'on';
192
		} elseif (isset($config['ntpd']['notrap'])) {
193
			unset($config['ntpd']['notrap']);
194
		}
195

    
196
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
197
			$enable_rrd_graphing = true;
198
		}
199
		if (!empty($_POST['statsgraph'])) {
200
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
201
		} elseif (isset($config['ntpd']['statsgraph'])) {
202
			unset($config['ntpd']['statsgraph']);
203
		}
204
		if (isset($enable_rrd_graphing)) {
205
			enable_rrd_graphing();
206
		}
207

    
208
		if (!empty($_POST['leaptxt'])) {
209
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptxt']);
210
		} elseif (isset($config['ntpd']['leapsec'])) {
211
			unset($config['ntpd']['leapsec']);
212
		}
213

    
214
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
215
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
216
		}
217

    
218
		write_config("Updated NTP Server Settings");
219

    
220
		$retval = 0;
221
		$retval = system_ntp_configure();
222
		$savemsg = get_std_save_message($retval);
223
	}
224
}
225

    
226
function build_interface_list() {
227
	global $pconfig;
228

    
229
	$iflist = array('options' => array(), 'selected' => array());
230

    
231
	$interfaces = get_configured_interface_with_descr();
232
	$carplist = get_configured_carp_interface_list();
233

    
234
	foreach ($carplist as $cif => $carpip) {
235
		$interfaces[$cif] = $carpip . " (" . get_vip_descr($carpip) .")";
236
	}
237

    
238
	$aliaslist = get_configured_ip_aliases_list();
239

    
240
	foreach ($aliaslist as $aliasip => $aliasif) {
241
		$interfaces[$aliasip] = $aliasip." (".get_vip_descr($aliasip).")";
242
	}
243

    
244
	$size = (count($interfaces) < 10) ? count($interfaces) : 10;
245

    
246
	foreach ($interfaces as $iface => $ifacename) {
247
		if (!is_ipaddr(get_interface_ip($iface)) && !is_ipaddr($iface)) {
248
			continue;
249
		}
250

    
251
		$iflist['options'][$iface] = $ifacename;
252

    
253
		if (in_array($iface, $pconfig['interface'])) {
254
			array_push($iflist['selected'], $iface);
255
		}
256
	}
257

    
258
	return($iflist);
259
}
260

    
261
$closehead = false;
262
$pconfig = &$config['ntpd'];
263
if (empty($pconfig['interface'])) {
264
	$pconfig['interface'] = array();
265
} else {
266
	$pconfig['interface'] = explode(",", $pconfig['interface']);
267
}
268
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("NTP"));
269
$shortcut_section = "ntp";
270
include("head.inc");
271

    
272
if ($input_errors) {
273
	print_input_errors($input_errors);
274
}
275
if ($savemsg) {
276
	print_info_box($savemsg, 'success');
277
}
278

    
279
$tab_array = array();
280
$tab_array[] = array(gettext("NTP"), true, "services_ntpd.php");
281
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
282
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
283
display_top_tabs($tab_array);
284

    
285
$form = new Form;
286

    
287
$section = new Form_Section('NTP server configuration');
288

    
289
$iflist = build_interface_list();
290

    
291
$section->addInput(new Form_Select(
292
	'interface',
293
	'Interface',
294
	$iflist['selected'],
295
	$iflist['options'],
296
	true
297
))->setHelp('Interfaces without an IP address will not be shown.' . '<br />' .
298
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
299
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
300

    
301
$timeservers = explode(' ', $config['system']['timeservers']);
302
$maxrows = max(count($timeservers), 1);
303
for ($counter=0; $counter < $maxrows; $counter++) {
304
	$group = new Form_Group($counter == 0 ? 'Time servers':'');
305
	$group->addClass('repeatable');
306

    
307
	$group->add(new Form_Input(
308
		'server' . $counter,
309
		null,
310
		'text',
311
		$timeservers[$counter],
312
		['placeholder' => 'Hostname']
313
	 ))->setWidth(3);
314

    
315
	 $group->add(new Form_Checkbox(
316
		'servprefer' . $counter,
317
		null,
318
		null,
319
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
320
	 ))->sethelp('Prefer');
321

    
322
	 $group->add(new Form_Checkbox(
323
		'servselect' . $counter,
324
		null,
325
		null,
326
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
327
	 ))->sethelp('No Select');
328

    
329
	$group->add(new Form_Button(
330
		'deleterow' . $counter,
331
		'Delete'
332
	))->removeClass('btn-primary')->addClass('btn-warning');
333

    
334
	 $section->add($group);
335
}
336

    
337
$section->addInput(new Form_Button(
338
	'addrow',
339
	'Add'
340
))->removeClass('btn-primary')->addClass('btn-success');
341

    
342
$section->addInput(new Form_StaticText(
343
	null,
344
	$btnaddrow
345
))->setHelp('For best results three to five servers should be configured here.' . '<br />' .
346
			'The prefer option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
347
			'The noselect option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.');
348

    
349
$section->addInput(new Form_Input(
350
	'ntporphan',
351
	'Orphan mode',
352
	'text',
353
	$pconfig['ntporphan']
354
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
355
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
356
			'to insure that any other servers available to clients are preferred over this server. (default: 12).');
357

    
358
$section->addInput(new Form_Checkbox(
359
	'statsgraph',
360
	'NTP Graphs',
361
	'Enable RRD graphs of NTP statistics (default: disabled).',
362
	$pconfig['statsgraph']
363
));
364

    
365
$section->addInput(new Form_Checkbox(
366
	'logpeer',
367
	'Syslog logging',
368
	'Enable logging of peer messages (default: disabled).',
369
	$pconfig['logpeer']
370
));
371

    
372
$section->addInput(new Form_Checkbox(
373
	'logsys',
374
	null,
375
	'Enable logging of system messages (default: disabled).',
376
	$pconfig['logsys']
377
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
378
			'<a href="diag_logs_ntpd.php">' . 'Status > System Logs > NTP' . '</a>');
379

    
380
// Statistics logging section
381
$btnadvstats = new Form_Button(
382
	'btnadvstats',
383
	'Advanced'
384
);
385

    
386
$btnadvstats->removeClass('btn-primary')->addClass('btn-default btn-sm');
387

    
388
$section->addInput(new Form_StaticText(
389
	'Statistics logging',
390
	$btnadvstats
391
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
392

    
393
$section->addInput(new Form_Checkbox(
394
	'clockstats',
395
	null,
396
	'Enable logging of reference clock statistics (default: disabled).',
397
	$pconfig['clockstats']
398
));
399

    
400
$section->addInput(new Form_Checkbox(
401
	'loopstats',
402
	null,
403
	'Enable logging of clock discipline statistics (default: disabled).',
404
	$pconfig['loopstats']
405
));
406

    
407
$section->addInput(new Form_Checkbox(
408
	'peerstats',
409
	null,
410
	'Enable logging of NTP peer statistics (default: disabled).',
411
	$pconfig['peerstats']
412
));
413

    
414
// Access restrictions section
415
$btnadvrestr = new Form_Button(
416
	'btnadvrestr',
417
	'Advanced'
418
);
419

    
420
$btnadvrestr->removeClass('btn-primary')->addClass('btn-default btn-sm');
421

    
422
$section->addInput(new Form_StaticText(
423
	'Access Restrictions',
424
	$btnadvrestr
425
))->setHelp('These options control access to NTP from the WAN.');
426

    
427
$section->addInput(new Form_Checkbox(
428
	'kod',
429
	null,
430
	'Enable Kiss-o\'-death packets (default: enabled).',
431
	$pconfig['kod']
432
));
433

    
434
$section->addInput(new Form_Checkbox(
435
	'nomodify',
436
	null,
437
	'Deny state modifications (i.e. run time configuration) by ntpq and ntpdc (default: enabled).',
438
	$pconfig['nomodify']
439
));
440

    
441
$section->addInput(new Form_Checkbox(
442
	'noquery',
443
	null,
444
	'Disable ntpq and ntpdc queries (default: disabled).',
445
	$pconfig['noquery']
446
));
447

    
448
$section->addInput(new Form_Checkbox(
449
	'noserve',
450
	null,
451
	'Disable all except ntpq and ntpdc queries (default: disabled).',
452
	$pconfig['noserve']
453
));
454

    
455
$section->addInput(new Form_Checkbox(
456
	'nopeer',
457
	null,
458
	'Deny packets that attempt a peer association (default: enabled).',
459
	$pconfig['nopeer']
460
));
461

    
462
$section->addInput(new Form_Checkbox(
463
	'notrap',
464
	null,
465
	'Deny mode 6 control message trap service (default: enabled).',
466
	$pconfig['notrap']
467
))->addClass('advrestrictions');
468

    
469
// Leap seconds section
470
$btnleap = new Form_Button(
471
	'btnleap',
472
	'Advanced'
473
);
474

    
475
$btnleap->removeClass('btn-primary')->addClass('btn-default btn-sm');
476

    
477
$section->addInput(new Form_StaticText(
478
	'Leap seconds',
479
	$btnleap
480
))->setHelp('A leap second file allows NTP to advertize an upcoming leap second addition or subtraction. ' .
481
			'Normally this is only useful if this server is a stratum 1 time server. ');
482

    
483
$section->addInput(new Form_Textarea(
484
	'leaptext',
485
	null,
486
	base64_decode(chunk_split($pconfig['leapsec']))
487
))->setHelp('Enter Leap second configuration as text OR select a file to upload');
488

    
489
$section->addInput(new Form_Input(
490
	'leapfile',
491
	null,
492
	'file'
493
))->addClass('btn-default');
494

    
495
$form->add($section);
496
print($form);
497

    
498
?>
499

    
500
<script type="text/javascript">
501
//<![CDATA[
502
	// If this variable is declared, any help text will not be deleted when rows are added
503
	// IOW the help text will appear on every row
504
	retainhelp = true;
505
</script>
506

    
507
<script type="text/javascript">
508
//<![CDATA[
509
events.push(function() {
510

    
511
	// Make the ‘clear’ button a plain button, not a submit button
512
	$('#btnadvstats').prop('type','button');
513

    
514
	// On click, show the controls in the stats section
515
	$("#btnadvstats").click(function() {
516
		hideCheckbox('clockstats', false);
517
		hideCheckbox('loopstats', false);
518
		hideCheckbox('peerstats', false);
519
	});
520

    
521
	// Make the ‘clear’ button a plain button, not a submit button
522
	$('#btnadvrestr').prop('type','button');
523

    
524
	// On click, show the controls in the restrictions section
525
	$("#btnadvrestr").click(function() {
526
		hideCheckbox('nomodify', false);
527
		hideCheckbox('noquery', false);
528
		hideCheckbox('noserve', false);
529
		hideCheckbox('nopeer', false);
530
		hideCheckbox('notrap', false);
531
	});
532

    
533
	// Make the ‘btnleap’ button a plain button, not a submit button
534
	$('#btnleap').prop('type','button');
535

    
536
	// On click, show the controls in the leap seconds section
537
	$("#btnleap").click(function() {
538
		hideInput('leaptext', false);
539
		hideInput('leapfile', false);
540
	});
541

    
542
	// Set intial states
543
	hideCheckbox('clockstats', true);
544
	hideCheckbox('loopstats', true);
545
	hideCheckbox('peerstats', true);
546
	hideCheckbox('kod', true);
547
	hideCheckbox('nomodify', true);
548
	hideCheckbox('noquery', true);
549
	hideCheckbox('noserve', true);
550
	hideCheckbox('nopeer', true);
551
	hideCheckbox('notrap', true);
552
	hideInput('leaptext', true);
553
	hideInput('leapfile', true);
554

    
555
	// Suppress "Delete row" button if there are fewer than two rows
556
	checkLastRow();
557
});
558
//]]>
559
</script>
560

    
561
<?php include("foot.inc");
(131-131/228)