summaryrefslogtreecommitdiffstats
path: root/usr/local/pkg/openntpd.inc
blob: dcbb50a7d17d836b91f7f0776a55a2b97f6dec27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
require_once('config.inc');
require_once('globals.inc');
require_once('service-utils.inc');

function openntpd_install() {
	global $g;

	$config_file = $g['varetc_path'] . '/openntpd.conf';
	$rcfile = array();
	$rcfile['file'] = 'openntpd.sh';
	$rcfile['start'] = <<<EOD
if [ -f $config_file ]; then
	ntpd -f $config_file
fi

EOD;
	$rcfile['stop'] = "killall ntpd";
	write_rcfile($rcfile);
}

function openntpd_get_iface_ip($iface) {
	$iface = convert_friendly_interface_to_real_interface_name($iface);
	$line = trim(shell_exec("ifconfig " . escapeshellarg($iface) . " | grep inet | grep -v inet6"));
	list($dummy, $ip, $dummy2, $dummy3) = explode(' ', $line);

	return $ip;
}

function openntpd_resync() {
	global $g, $config;

	$settings = $config['installedpackages']['openntpd']['config'][0];
	$config_file = $g['varetc_path'] . '/openntpd.conf';

	if ($settings['enable'] == 'on') {
		$conf = '';

		$ifaces = explode(',', $settings['interface']);
		$ips = array_map('openntpd_get_iface_ip', $ifaces);
		foreach ($ips as $ip)
			$conf .= "listen on $ip\n";

		$servers = explode(',', $config['system']['timeservers']);
		foreach ($servers as $server)
			$conf .= "servers $server\n";

		file_put_contents($config_file, $conf);
	}
	else {
		if (file_exists($config_file))
			unlink($config_file);
	}
	restart_service('openntpd');
}
OpenPOWER on IntegriCloud