<?php

/*
	$Id$
	avahi.inc
	part of pfSense (http://www.pfSense.com)
	Copyright (C) 2009-2012 Scott Ullrich, Jim Pingle
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	 1. Redistributions of source code must retain the above copyright notice,
	    this list of conditions and the following disclaimer.

	 2. Redistributions in binary form must reproduce the above copyright
	    notice, this list of conditions and the following disclaimer in the
	    documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

$pfs_version = substr(trim(file_get_contents("/etc/version")),0,3);
switch ($pfs_version) {
	case "1.2":
	case "2.0":
		define('AVAHI_BASE', '/usr/local');
		break;
	default:
		define('AVAHI_BASE', '/usr/pbi/avahi-' . php_uname("m"));
}

function avahi_start() {
	 mwexec_bg("/usr/local/etc/rc.d/avahi-daemon.sh start");
}

function avahi_stop() {
	 mwexec_bg("/usr/local/etc/rc.d/avahi-daemon.sh stop");
}

function avahi_install() {
	global $g, $config;
	conf_mount_rw();

	if (!file_exists('/usr/local/etc/gnome.subr')) {
		@symlink(AVAHI_BASE . '/etc/gnome.subr', '/usr/local/etc/gnome.subr');
	}

	// Add needed users and groups
	exec("/usr/sbin/pw useradd  avahi -u 558");
	exec("/usr/sbin/pw groupadd avahi -g 558");

	// Make image RO
	conf_mount_ro();
}

function avahi_write_config() {
	global $g, $config;
	// Make image RW
	conf_mount_rw();

	// Pull some various values out of config.xml
	$hostname = $config['system']['hostname'];
	$domain = $config['system']['domain'];
	$enable = $config['installedpackages']['avahi']['config'][0]['enable'];
	$browsedomains = $config['installedpackages']['avahi']['config'][0]['browsedomains'];
	$denyif = $config['installedpackages']['avahi']['config'][0]['denyinterfaces'];
	$useipv4 = ($config['installedpackages']['avahi']['config'][0]['disable_ipv4']) ? "no" : "yes";
	$useipv6 = ($config['installedpackages']['avahi']['config'][0]['disable_ipv6']) ? "no" : "yes";

	// No supplied domains?  Use the defaults.
	if(!$browsedomains)
		$browsedomains = "local, 0pointer.de, zeroconf.org";

	// Never pass along WAN.  Bad.
	$denyinterfaces = $config['interfaces']['wan']['if'];

	// Process interfaces defined by user to deny.
	if($denyif) {
		$if = explode(",", $denyif);
		foreach($if as $i) {
			$ifreal = convert_friendly_interface_to_real_interface_name($i);
			if($ifreal)
				$denyinterfaces .= ", " . $ifreal;
		}
	}

	// Construct the avahi configuration
	$avahiconfig   = <<<EOF

# avahi.conf - This file was automatically generated by the pfSense pacakge
# manager.  Do not edit this file, it will be overwritten automatically.
# See /usr/local/pkg/avahi.inc to make changes to this file!

[server]
host-name={$hostname}
domain-name=local
browse-domains={$browsedomains}
deny-interfaces={$denyinterfaces}
use-ipv4={$useipv4}
use-ipv6={$useipv6}
enable-dbus=yes
#check-response-ttl=no
#use-iff-running=no
#disallow-other-stacks=no
allow-point-to-point=yes

[wide-area]
enable-wide-area=yes

[publish]
#disable-publishing=no
#disable-user-service-publishing=no
#add-service-cookie=no
#publish-addresses=yes
#publish-hinfo=yes
#publish-workstation=yes
#publish-domain=yes
#publish-dns-servers=192.168.50.1, 192.168.50.2
#publish-resolv-conf-dns-servers=yes
#publish-aaaa-on-ipv4=yes
#publish-a-on-ipv6=no

[reflector]
enable-reflector=yes
#reflect-ipv=no

[rlimits]
rlimit-core=0
rlimit-data=4194304
rlimit-fsize=0
rlimit-nofile=300
rlimit-stack=4194304
rlimit-nproc=3
#rlimit-as=

EOF;

	/* Write out .conf file */
	safe_mkdir(AVAHI_BASE . "/etc/avahi");
	$fd = fopen(AVAHI_BASE . "/etc/avahi/avahi-daemon.conf", "w");
	fwrite($fd, $avahiconfig);
	fclose($fd);
	/* Write out rc.d startup file */
	$start = "/etc/rc.conf_mount_rw\n";
	$start .= "if [ ! -d /proc/0 ]; then\n";
	$start .= "	mkdir -p /proc\n";
	$start .= "	mount -t procfs procfs /proc\n";
	$start .= "fi\n";
	$start .= "if [ ! -f /usr/local/etc/gnome.subr ]; then\n";
	$start .= "	ln -sf " . AVAHI_BASE . "/etc/gnome.subr /usr/local/etc/gnome.subr\n";
	$start .= "fi\n";
	$start .= "if [ ! -d /var/run/dbus ]; then\n";
	$start .= "        mkdir /var/run/dbus\n";
	$start .= "        chown messagebus:messagebus /var/run/dbus\n";
	$start .= "fi\n";
	$start .= "/usr/bin/killall avahi-daemon >/dev/null 2>&1\n";
	if (file_exists(AVAHI_BASE . "/etc/rc.d/dbus")) {
		$start .= AVAHI_BASE . "/etc/rc.d/dbus onestop\n";
		$start .= "rm /var/run/dbus/dbus.pid >/dev/null 2>&1\n";
		$start .= AVAHI_BASE . "/etc/rc.d/dbus onestart\n";
	}
	$start .= "sleep 5\n";
	$start .= AVAHI_BASE . "/sbin/avahi-daemon -D\n";
	$start .= "/etc/rc.conf_mount_ro\n";

	$stop = "/usr/bin/killall avahi-daemon >/dev/null 2>&1\n";
	if (file_exists(AVAHI_BASE . "/etc/rc.d/dbus")) {
		$stop .= AVAHI_BASE . "/etc/rc.d/dbus onestop\n";
		$stop .= "rm /var/run/dbus/dbus.pid >/dev/null 2>&1\n";
	}

	write_rcfile(array(
		"file" => "avahi-daemon.sh",
		"start" => $start,
		"stop" =>  $stop
		)
	);
	// Make image RO
	conf_mount_ro();

}

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

	avahi_stop();

	avahi_write_config();

	// Is package enabled?
	if (($config['installedpackages']['avahi']['config'][0]['enable'])
	    && file_exists("/usr/local/etc/rc.d/avahi-daemon.sh")) {
		avahi_start();
	}
}

?>