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
require_once('classes/Form.class.php');
287

    
288
$form = new Form;
289

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

    
292
$iflist = build_interface_list();
293

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
501
?>
502

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

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

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

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

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

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

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

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

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

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

    
563
<?php include("foot.inc");
(145-145/234)