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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
242
	$aliaslist = get_configured_ip_aliases_list();
243

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

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

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

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

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

    
262
	return($iflist);
263
}
264

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

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

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

    
287
require_once('classes/Form.class.php');
288

    
289
$form = new Form;
290

    
291
$section = new Form_Section('NTP server configuration');
292

    
293
$iflist = build_interface_list();
294

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

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

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

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

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

    
333
	$group->add(new Form_Button(
334
		'deleterow' . $counter,
335
		'Delete'
336
	))->removeClass('btn-primary')->addClass('btn-warning');
337

    
338
	 $section->add($group);
339
}
340

    
341
$section->addInput(new Form_Button(
342
	'addrow',
343
	'Add'
344
))->removeClass('btn-primary')->addClass('btn-success');
345

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

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

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

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

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

    
384
// Statistics logging section
385
$btnadvstats = new Form_Button(
386
	'btnadvstats',
387
	'Advanced'
388
);
389

    
390
$btnadvstats->removeClass('btn-primary')->addClass('btn-default btn-sm');
391

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

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

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

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

    
418
// Access restrictions section
419
$btnadvrestr = new Form_Button(
420
	'btnadvrestr',
421
	'Advanced'
422
);
423

    
424
$btnadvrestr->removeClass('btn-primary')->addClass('btn-default btn-sm');
425

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

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

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

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

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

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

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

    
473
// Leap seconds section
474
$btnleap = new Form_Button(
475
	'btnleap',
476
	'Advanced'
477
);
478

    
479
$btnleap->removeClass('btn-primary')->addClass('btn-default btn-sm');
480

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

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

    
493
$section->addInput(new Form_Input(
494
	'leapfile',
495
	null,
496
	'file'
497
))->addClass('btn-default');
498

    
499
$form->add($section);
500
print($form);
501

    
502
?>
503

    
504
<script>
505
	// If this variable is declared, any help text will not be deleted when rows are added
506
	// IOW the help text will appear on every row
507
	retainhelp = true;
508
</script>
509

    
510
<script>
511
//<![CDATA[
512
events.push(function(){
513

    
514
	// Make the ‘clear’ button a plain button, not a submit button
515
	$('#btnadvstats').prop('type','button');
516

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

    
524
	// Make the ‘clear’ button a plain button, not a submit button
525
	$('#btnadvrestr').prop('type','button');
526

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

    
536
	// Make the ‘btnleap’ button a plain button, not a submit button
537
	$('#btnleap').prop('type','button');
538

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

    
545
	// Set intial states
546
	hideCheckbox('clockstats', true);
547
	hideCheckbox('loopstats', true);
548
	hideCheckbox('peerstats', true);
549
	hideCheckbox('kod', true);
550
	hideCheckbox('nomodify', true);
551
	hideCheckbox('noquery', true);
552
	hideCheckbox('noserve', true);
553
	hideCheckbox('nopeer', true);
554
	hideCheckbox('notrap', true);
555
	hideInput('leaptext', true);
556
	hideInput('leapfile', true);
557
});
558
//]]>
559
</script>
560

    
561
<?php include("foot.inc");
(146-146/235)