Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bufgix djbns #32

Merged
merged 2 commits into from Mar 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
193 changes: 159 additions & 34 deletions kloxo/bin/misc/djbdnsstart.php
@@ -1,39 +1,164 @@
<?php

include_once "htmllib/lib/include.php";
include_once 'htmllib/lib/include.php';

if (!posix_getpwnam('tinydns')) {
system("useradd tinydns");
class DjbDNS
{
/**
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
* @var $dns_users The users needed for DjbDNS service
*/
private static $dns_users = array('tinydns', 'dnslog', 'dnscache', 'axfrdns');

/**
* Create the users needed for init DjbDNS.
*
* The users needed are: tinydns, dnslog, dnscache, axfrdns
*
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @return void
*/
public function __construct()
{
self::createDjbDNSUsers();
self::checkPaths();
self::getIPs();
}

/**
* Create the users needed for init DjbDNS.
*
* The users needed are: tinydns, dnslog, dnscache, axfrdns
*
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @return void
*/
private static function createDjbDNSUsers()
{
foreach (self::$dns_users as $user) {
// Create the user only if not exist previously
if(!posix_getpwnam($user)) {
$result = system('useradd ' . $user);

if($result !== 0) {
echo 'Error: Adding ' . $user . ' user failed.' . PHP_EOL;
exit();
}
else {
echo 'Added ' . $user . ' user.' . PHP_EOL;
}
}
}
}

/**
* Check if the config paths are init
*
* The config paths are:
* - /var/tinydns
* - /var/axfrdns
*
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @return void
*/
private static function checkPaths()
{
if(!file_exists('/var/tinydns')) {
$result = system('tinydns-conf tinydns dnslog /var/tinydns 127.0.0.1');

if($result !== 0) {
echo 'Error: enabling /var/tinydns config.' . PHP_EOL;
exit();
}
else {
echo 'Enabling /var/tinydns config.' . PHP_EOL;
}
}

if(!file_exists('/var/axfrdns')) {
$result = system('axfrdns-conf axfrdns dnslog /var/axfrdns /var/tinydns 0.0.0.0');

if($result !== 0) {
echo 'Error: enabling /var/axfrdns config.' . PHP_EOL;
exit();
}
else {
echo 'Enabling /var/axfrdns config.' . PHP_EOL;
}
}
}

/**
* Get the IPs availables.
*
* Configure the IP for Djbdns and write properly configs.
*
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @return void
*/
private static function getIPs()
{
echo 'Getting the list of IPs availables.' . PHP_EOL;

$ip_address_list = os_get_allips();
//var_dump($ip_address_list);

if(empty($ip_address_list))
{
echo 'Error: no IPs availaibles configured.' . PHP_EOL;
exit();
}
else
{
// If array merge as IP/IP/...
if(is_array($ip_address_list))
{
$ip_address_list = implode('/', $ip_address_list);
}

if(!lfile_put_contents('/var/tinydns/env/IP', $ip_address_list)) {
echo 'Error: could not write the IP ' . $ip_address_list . ' for tinydns file /var/tinydns/env/IP. ' . PHP_EOL;
exit();
}

if (!file_exists('/var/dnscache')) {
$result = system('dnscache-conf dnscache dnslog /var/dnscache 127.0.0.1');

if($result !== 0) {
echo 'Error: enabling /var/dnscache config.' . PHP_EOL;
exit();
}
else {
echo 'Enabling /var/dnscache config.' . PHP_EOL;
}
}

if(!lfile_put_contents('/var/axfrdns/tcp', ':allow')) {
echo 'Error: could not write the config on /var/axfrdns/tcp.' . PHP_EOL;
exit();
}

$result = system('cd /var/axfrdns; /usr/local/bin/tcprules tcp.cdb tcp.tmp < tcp');

if(intval($result) !== 0) {
var_dump($result);
echo 'Error: enabling tcprules config.' . PHP_EOL;
exit();
}
else {
echo 'Enabling tcprules config.' . PHP_EOL;
}

if(!lfile_put_contents('/var/dnscache/env/IP', '127.0.0.1')) {
echo 'Error: could not write the localhost IP on /var/dnscache/env/IP.' . PHP_EOL;
exit();
}
}
}
}
if (!posix_getpwnam('dnslog')) {
system("useradd dnslog");
}

if (!posix_getpwnam('dnscache')) {
system("useradd dnscache");
}
if (!posix_getpwnam('axfrdns')) {
system("useradd axfrdns");
}

if (!lxfile_exists("/var/tinydns")) {
system("tinydns-conf tinydns dnslog /var/tinydns 127.0.0.1");
}

if (!lxfile_exists("/var/axfrdns")) {
system("axfrdns-conf axfrdns dnslog /var/axfrdns /var/tinydns 0.0.0.0");
}
$list = os_get_allips();

$out = implode("/", $list);

lfile_put_contents("/var/tinydns/env/IP", "$out");

if (!lxfile_exists("/var/dnscache")) {
system("dnscache-conf dnscache dnslog /var/dnscache 127.0.0.1");
}

lfile_put_contents("/var/axfrdns/tcp", ":allow");
system("cd /var/axfrdns; /usr/local/bin/tcprules tcp.cdb tcp.tmp < tcp");

lfile_put_contents("/var/dnscache/env/IP", "127.0.0.1");
$djbdns = new DjbDNS();
37 changes: 28 additions & 9 deletions kloxo/httpdocs/htmllib/lib/linuxlib.php
Expand Up @@ -192,18 +192,37 @@ function getIPs_from_ifcfg()

global $gbl, $sgbl, $login, $ghtml;

$driverapp = $gbl->getSyncClass(null, null, 'ipaddress');
// print($driverapp);

if ($driverapp === 'redhat') {
$list = Ipaddress__Redhat::getCurrentIps();
$master = NULL;
$sync_server = NULL;
// This actually does nothing, because it doesn't return any driver app on djbdns
// (Commented by Angel, added by Mustafa previously)
// @deprecated This should be deprecated and confirmed.
$driverapp = $gbl->getSyncClass($master, $sync_server, 'ipaddress');

$current_ip_address_list = array();
//var_dump($driverapp);

if(!empty($driverapp))
{ // @deprecated This is probably always empty, just left here some time and deprecate
if ($driverapp === 'redhat') {
$current_ip_address_list = Ipaddress__Redhat::getCurrentIps();
}
else {
$current_ip_address_list = Ipaddress__Redhat::getCurrentIps();
}
}

else {
// This is the real way to get the IP from djbdns, OS detection should make previously. Assuming Redhat by default.
$current_ip_address_list = Ipaddress__Redhat::getCurrentIps();
}

// var_dump($current_ip_address_list);

$iplist = array(); // Initialize return value
if(!empty($list)) {
foreach($list as $k => $v) {
if(!empty($current_ip_address_list)) {
foreach($current_ip_address_list as $index => $data) {
// Check ipaddr index
$ip_address = isset($v['ipaddr']) ? $v['ipaddr'] : NULL;
$ip_address = isset($data['ipaddr']) ? $data['ipaddr'] : NULL;

// Skip localhost IP or empty values
if ($ip_address !== '127.0.0.1' && !empty($ip_address)) {
Expand Down