<?xml version="1.0" encoding="utf-8" ?>
<packagegui>
	<name>powerdns</name>
	<version>2.9.16_4</version>
	<title>Services: PowerDNS</title>
	<donotsave>true</donotsave>
	<preoutput>yes</preoutput>
	<!-- Menu is where this packages menu will appear -->
	<menu>
		<name>PowerDNS</name>
		<tooltiptext></tooltiptext>
		<section>Services</section>
		<configfile>powerdns.xml</configfile>
	</menu>
        <!-- Do not save invokes a simple input menu and will not update
             the configuration database. -->
	<fields>
		<field>
			<fielddescr>pgsql Password</fielddescr>
			<fieldname>password</fieldname>
			<description>Enter the pgsql users password.</description>
			<type>password</type>
		</field>
		<field>
			<fielddescr>pgsql Password AGAIN</fielddescr>
			<fieldname>passwordagain</fieldname>
			<type>password</type>
		</field>
		<field>
			<fielddescr>IP Address</fielddescr>
			<fieldname>ipaddress</fieldname>
			<type>input</type>
			<value>0.0.0.0</value>
			<description>(0.0.0.0 to bind to all)</description>
		</field>
		<field>
			<fielddescr>Cache Time to Live</fielddescr>
			<fieldname>cachettl</fieldname>
			<type>input</type>
			<value>20</value>
			<description>Recommended value: 20</description>
		</field>
		<field>
			<fielddescr>Master</fielddescr>
			<fieldname>master</fieldname>
			<type>select</type>
			<options>
			<option><name>Yes</name><value>yes</value></option>
			<option><name>No</name><value>no</value></option>
			</options>
		</field>
		<field>
			<fielddescr>Slave</fielddescr>
			<fieldname>slave</fieldname>
			<type>select</type>
			<options>
			<option><name>No</name><value>no</value></option>
			<option><name>Yes</name><value>yes</value></option>
			</options>
		</field>
	</fields>
	<custom_php_deinstall_command>
		system("/usr/local/bin/pg_ctl -m immediate");
		rmdir_recursive("/usr/local/pgsql");
		system("/usr/sbin/pw userdel pgsql");
		system("/usr/bin/killall -9 postgres");
		echo "&lt;p&gt;";
		echo "Reboot required... One moment...";
		echo "&lt;p&gt;";
		/* system("/sbin/shutdown -r now"); */
	</custom_php_deinstall_command>
	<custom_php_install_command>
		echo "&lt;pre&gt;";
		echo "Creating DB...";
		system("/usr/bin/su -l pgsql -c initdb");
		system("/usr/local/etc/rc.d/010.pgsql.sh start");
		system("/usr/bin/su -l pgsql -c \"dropdb powerdns\" ");
		system("/usr/bin/su -l pgsql -c \"createdb powerdns\" ");
		$fout = fopen("/usr/local/pkg/powerdns.schema","w");
		/* DATABASE SCHEMA */
		fwrite($fout, "create table domains (\n");
		fwrite($fout, "id              SERIAL PRIMARY KEY,\n");
		fwrite($fout, "name            VARCHAR(255) NOT NULL,\n");
		fwrite($fout, "master          VARCHAR(20) DEFAULT NULL,\n");
		fwrite($fout, "last_check      INT DEFAULT NULL,\n");
		fwrite($fout, "type            VARCHAR(6) NOT NULL,\n");
		fwrite($fout, "notified_serial INT DEFAULT NULL,\n");
		fwrite($fout, "account         VARCHAR(40) DEFAULT NULL\n");
		fwrite($fout, ");\n");
		fwrite($fout, "CREATE UNIQUE INDEX name_index ON domains(name);\n");
		fwrite($fout, "CREATE TABLE records (\n");
		fwrite($fout, "	   id              SERIAL PRIMARY KEY,\n");
		fwrite($fout, "	   domain_id       INT DEFAULT NULL,\n");
		fwrite($fout, "	   name            VARCHAR(255) DEFAULT NULL,\n");
		fwrite($fout, "		   type            VARCHAR(6) DEFAULT NULL,\n");
		fwrite($fout, "	   content         VARCHAR(255) DEFAULT NULL,\n");
		fwrite($fout, "	   ttl             INT DEFAULT NULL,\n");
		fwrite($fout, "	   prio            INT DEFAULT NULL,\n");
		fwrite($fout, "	   change_date     INT DEFAULT NULL,\n");
		fwrite($fout, "	   CONSTRAINT domain_exists\n");
		fwrite($fout, "	   FOREIGN KEY(domain_id) REFERENCES domains(id)\n");
		fwrite($fout, "	   ON DELETE CASCADE\n");
		fwrite($fout, ");\n");
		fwrite($fout, "CREATE INDEX rec_name_index ON records(name);\n");
		fwrite($fout, "CREATE INDEX nametype_index ON records(name,type);\n");
		fwrite($fout, "CREATE INDEX domain_id ON records(domain_id);\n");
		fwrite($fout, "create table supermasters (\n");
		fwrite($fout, "		 ip VARCHAR(25) NOT NULL,\n");
		fwrite($fout, "		 nameserver VARCHAR(255) NOT NULL,\n");
		fwrite($fout, "		 account VARCHAR(40) DEFAULT NULL\n");
		fwrite($fout, ");\n");
		fwrite($fout, "GRANT SELECT ON supermasters TO pgsql;\n");
		fwrite($fout, "GRANT ALL ON domains TO pgsql;\n");
		fwrite($fout, "GRANT ALL ON domains_id_seq TO pgsql;\n");
		fwrite($fout, "GRANT ALL ON records TO pgsql;\n");
		fwrite($fout, "GRANT ALL ON records_id_seq TO pgsql;\n");
		fclose($fout);
		system("cat /usr/local/pkg/powerdns.schema | /usr/bin/su -l pgsql -c \"psql powerdns\" ");
		echo "Installation completed!";
	</custom_php_install_command>
	<custom_add_php_command>
		if($password == $passwordagain) {
			conf_mount_rw();
			config_lock();
			$fd = popen("/usr/sbin/pw usermod -n pgsql -H 0", "w");
			$salt = md5(time());
			$crypted_pw = crypt($_POST['password'],$salt);
			fwrite($fd, $crypted_pw);
			pclose($fd);
			$fout = fopen("/usr/local/etc/pdns.conf","w");
			fwrite($fout, "launch=gpgsql\n");
			fwrite($fout, "gpgsql-host=127.0.0.1\n");
			fwrite($fout, "gpgsql-dbname=powerdns\n");
			fwrite($fout, "gpgsql-user=pgsql\n");
			fwrite($fout, "gpgsql-password=" . $_POST['password'] . "\n");
			fwrite($fout, "chroot=/var/empty\n");
			fwrite($fout, "disable-tcp=no\n");
			fwrite($fout, "guardian=yes\n");
			fwrite($fout, "webserver=yes\n");
			fwrite($fout, "webserver-address=127.0.0.1\n");
			fwrite($fout, "webserver-port=8081\n");
			fwrite($fout, "local-address=" . $_POST['ipaddress'] . "\n");
			fwrite($fout, "master=" . $_POST['master'] . "\n");
			fwrite($fout, "slave=" . $_POST['slave'] . "\n");
			fwrite($fout, "cache-ttl=" . $_POST['cachettl'] . "\n");
			/* XXX: TODO */
			fwrite($fout, "# allow-axfr-ips=\n");
			fwrite($fout, "# allow-recursion=\n");
			fwrite($fout, "# config-dir=/etc\n");
			fwrite($fout, "# config-name=\n");
			fwrite($fout, "# control-console=no\n");
			fwrite($fout, "# default-soa-name=ns.example.com\n");
			fwrite($fout, "# disable-axfr=no\n");
			fwrite($fout, "# distributor-threads=3\n");
			fwrite($fout, "# fancy-records=no\n");
			fwrite($fout, "# launch=\n");
			fwrite($fout, "# lazy-recursion=yes\n");
			fwrite($fout, "# load-modules=\n");
			fwrite($fout, "# local-ipv6=\n");
			fwrite($fout, "# local-port=53\n");
			fwrite($fout, "# log-dns-details=\n");
			fwrite($fout, "# log-failed-updates=\n");
			fwrite($fout, "# logfile=pdns.log\n");
			fwrite($fout, "# logging-facility=\n");
			fwrite($fout, "# loglevel=4\n");
			fwrite($fout, "# max-queue-length=5000\n");
			fwrite($fout, "# max-tcp-connections=10\n");
			fwrite($fout, "# module-dir=/usr/local/bin/../lib\n");
			fwrite($fout, "# negquery-cache-ttl=60\n");
			fwrite($fout, "# only-soa=org\n");
			fwrite($fout, "# out-of-zone-additional-processing=no\n");
			fwrite($fout, "# query-cache-ttl=20\n");
			fwrite($fout, "# query-logging=no\n");
			fwrite($fout, "# queue-limit=1500\n");
			fwrite($fout, "# receiver-threads=1\n");
			fwrite($fout, "# recursive-cache-ttl=10\n");
			fwrite($fout, "# recursor=no\n");
			fwrite($fout, "# setgid=\n");
			fwrite($fout, "# setuid=\n");
			fwrite($fout, "# skip-cname=no\n");
			fwrite($fout, "# slave=no\n");
			fwrite($fout, "# slave-cycle-interval=60\n");
			fwrite($fout, "# soa-minimum-ttl=3600\n");
			fwrite($fout, "# soa-serial-offset=0\n");
			fwrite($fout, "# socket-dir=/var/run\n");
			fwrite($fout, "# strict-rfc-axfrs=no\n");
			fwrite($fout, "# urlredirector=127.0.0.1\n");
			fwrite($fout, "# use-logfile=no\n");
			fwrite($fout, "# webserver-password=\n");
			fwrite($fout, "# webserver-print-arguments=no\n");
			fwrite($fout, "# wildcard-url=no\n");
			fwrite($fout, "# wildcards=\n");
			fclose($fout);
			conf_mount_ro();
			config_unlock();
			system("/usr/local/bin/pg_ctl -m smart 2>/dev/null");
			system("/usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start 2>/dev/null");
		} else {
			echo "Passwords do not much!";
			exit;
		}
	</custom_add_php_command>
</packagegui>