Project

General

Profile

Download (16.4 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
	pfSense_MODULE: ntpd
58
*/
59

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

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

    
72
if (!is_array($config['ntpd'])) {
73
	$config['ntpd'] = array();
74
}
75

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

    
89
if ($_POST) {
90
	unset($input_errors);
91
	$pconfig = $_POST;
92

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

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

    
106
		unset($config['ntpd']['prefer']);
107
		unset($config['ntpd']['noselect']);
108
		$timeservers = '';
109

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

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

    
133
		if (!empty($_POST['logpeer'])) {
134
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
135
		} elseif (isset($config['ntpd']['logpeer'])) {
136
			unset($config['ntpd']['logpeer']);
137
		}
138

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

    
145
		if (!empty($_POST['clockstats'])) {
146
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
147
		} elseif (isset($config['ntpd']['clockstats'])) {
148
			unset($config['ntpd']['clockstats']);
149
		}
150

    
151
		if (!empty($_POST['loopstats'])) {
152
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
153
		} elseif (isset($config['ntpd']['loopstats'])) {
154
			unset($config['ntpd']['loopstats']);
155
		}
156

    
157
		if (!empty($_POST['peerstats'])) {
158
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
159
		} elseif (isset($config['ntpd']['peerstats'])) {
160
			unset($config['ntpd']['peerstats']);
161
		}
162

    
163
		if (empty($_POST['kod'])) {
164
			$config['ntpd']['kod'] = 'on';
165
		} elseif (isset($config['ntpd']['kod'])) {
166
			unset($config['ntpd']['kod']);
167
		}
168

    
169
		if (empty($_POST['nomodify'])) {
170
			$config['ntpd']['nomodify'] = 'on';
171
		} elseif (isset($config['ntpd']['nomodify'])) {
172
			unset($config['ntpd']['nomodify']);
173
		}
174

    
175
		if (!empty($_POST['noquery'])) {
176
			$config['ntpd']['noquery'] = $_POST['noquery'];
177
		} elseif (isset($config['ntpd']['noquery'])) {
178
			unset($config['ntpd']['noquery']);
179
		}
180

    
181
		if (!empty($_POST['noserve'])) {
182
			$config['ntpd']['noserve'] = $_POST['noserve'];
183
		} elseif (isset($config['ntpd']['noserve'])) {
184
			unset($config['ntpd']['noserve']);
185
		}
186

    
187
		if (empty($_POST['nopeer'])) {
188
			$config['ntpd']['nopeer'] = 'on';
189
		} elseif (isset($config['ntpd']['nopeer'])) {
190
			unset($config['ntpd']['nopeer']);
191
		}
192

    
193
		if (empty($_POST['notrap'])) {
194
			$config['ntpd']['notrap'] = 'on';
195
		} elseif (isset($config['ntpd']['notrap'])) {
196
			unset($config['ntpd']['notrap']);
197
		}
198

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

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

    
217
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
218
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
219
		}
220

    
221
		write_config("Updated NTP Server Settings");
222

    
223
		$retval = 0;
224
		$retval = system_ntp_configure();
225
		$savemsg = get_std_save_message($retval);
226
	}
227
}
228

    
229
function build_interface_list() {
230
	global $pconfig;
231

    
232
	$iflist = array('options' => array(), 'selected' => array());
233

    
234
	$interfaces = get_configured_interface_with_descr();
235
	$carplist = get_configured_carp_interface_list();
236

    
237
	foreach ($carplist as $cif => $carpip) {
238
		$interfaces[$cif] = $carpip . " (" . get_vip_descr($carpip) .")";
239
	}
240

    
241
	$aliaslist = get_configured_ip_aliases_list();
242

    
243
	foreach ($aliaslist as $aliasip => $aliasif) {
244
		$interfaces[$aliasip] = $aliasip." (".get_vip_descr($aliasip).")";
245
	}
246

    
247
	$size = (count($interfaces) < 10) ? count($interfaces) : 10;
248

    
249
	foreach ($interfaces as $iface => $ifacename) {
250
		if (!is_ipaddr(get_interface_ip($iface)) && !is_ipaddr($iface)) {
251
			continue;
252
		}
253

    
254
		$iflist['options'][$iface] = $ifacename;
255

    
256
		if (in_array($iface, $pconfig['interface'])) {
257
			array_push($iflist['selected'], $iface);
258
		}
259
	}
260

    
261
	return($iflist);
262
}
263

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

    
275
if ($input_errors)
276
	print_input_errors($input_errors);
277
if ($savemsg)
278
	print_info_box($savemsg, 'success');
279

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

    
286
$form = new Form;
287

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

    
290
$iflist = build_interface_list();
291

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
499
?>
500

    
501
<script>
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>
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");
(143-143/228)