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
$pconfig = &$config['ntpd'];
262
if (empty($pconfig['interface'])) {
263
	$pconfig['interface'] = array();
264
} else {
265
	$pconfig['interface'] = explode(",", $pconfig['interface']);
266
}
267
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("NTP"));
268
$shortcut_section = "ntp";
269
include("head.inc");
270

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

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

    
284
$form = new Form;
285

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

    
288
$iflist = build_interface_list();
289

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
497
?>
498

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

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

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

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

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

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

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

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

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

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

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