Example #1
0
/**
 * Get user details by name
 */
function getUserDetailsByName($username, $killsession = true)
{
    # check if already in cache
    if ($user = checkCache("user", $username)) {
        return $user;
    } else {
        # for db upgrade!
        if (strpos($_SERVER['SCRIPT_URI'], "databaseUpgrade.php") > 0) {
            global $db;
            $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
        } else {
            global $database;
        }
        /* set query, open db connection and fetch results */
        $query = 'select * from users where `username` = "' . $username . '";';
        /* execute */
        try {
            $details = $database->getArray($query);
        } catch (Exception $e) {
            $error = $e->getMessage();
            print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>";
            return false;
        }
        # result must be more than 1!
        if (!isset($details[0])) {
            if ($killsession) {
                global $phpsessname;
                if (strlen($phpsessname) > 0) {
                    session_name($phpsessname);
                }
                session_start();
                session_destroy();
                return false;
            }
        } else {
            # save cache - id and name
            writeCache("user", $details[0]['id'], $details[0]);
            writeCache("user", $username, $details[0]);
            /* return results */
            return $details[0];
        }
    }
}
Example #2
0
function getAllSlaves($subnetId, $multi = false)
{
    # check cache
    if ($vtmp = checkCache("allslaves", $subnetId . "_{$multi}")) {
        return $vtmp;
    } else {
        global $removeSlaves;
        $end = false;
        # breaks while
        $removeSlaves[] = $subnetId;
        # first
        # db
        global $db;
        # get variables from config file
        $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
        while ($end == false) {
            /* get all immediate slaves */
            $query = "select * from `subnets` where `masterSubnetId` = '{$subnetId}' order by `id` asc; ";
            /* execute query */
            try {
                $slaves2 = $database->getArray($query);
            } catch (Exception $e) {
                $error = $e->getMessage();
                print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>";
                return false;
            }
            # we have more slaves
            if (sizeof($slaves2) != 0) {
                # recursive
                foreach ($slaves2 as $slave) {
                    $removeSlaves[] = $slave['id'];
                    getAllSlaves($slave['id']);
                    $end = true;
                }
            } else {
                $end = true;
            }
        }
        # save cache
        if (sizeof($removeSlaves) > 0) {
            writeCache("allslaves", $subnetId . "_{$multi}", $removeSlaves);
        }
    }
}
Example #3
0
/**
 * Check if resized subnet already exists in section!
 * 
 * Subnet policy:
 *      - inside section subnets cannot overlap!
 *      - same subnet can be configured in different sections
 *              - $subnetNew is exception because it is the 
 */
function verifyResizedSubnetOverlapping($subnetOld, $subnetNew)
{
    /* we need to get all subnets in section */
    global $db;
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* first we must get all subnets in section (by sectionId) */
    $querySubnets = 'select `id`,`subnet`,`mask`,`description`,`vrfId` from `subnets` where sectionId = "' . $subnetOld['sectionId'] . '" and `masterSubnetId` != "0" and `masterSubnetId` IS NOT NULL;';
    /* execute */
    try {
        $allSubnets = $database->getArray($querySubnets);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>";
        return false;
    }
    /* set new Subnet array */
    $subnet['subnet'] = $subnetNew;
    /* IPv4 or ipv6? */
    $type = IdentifyAddress($subnet['subnet']);
    /* we need network and broadcast address and check for both if the exist in any network! */
    if ($type == "IPv4") {
        /* verify new against each existing if they exist */
        if (!empty($allSubnets)) {
            foreach ($allSubnets as $existingSubnet) {
                /* we need cidr format! */
                $existingSubnet['subnet'] = Transform2long($existingSubnet['subnet']) . '/' . $existingSubnet['mask'];
                $ignore = false;
                /* only check if vrfId's match */
                if ($existingSubnet['vrfId'] == $subnetOld['vrfId']) {
                    # check if it is nested properly - inside its own parent, otherwise check for overlapping
                    $allParents = getAllParents($subnetOld['masterSubnetId']);
                    foreach ($allParents as $kp => $p) {
                        if ($existingSubnet['id'] == $p) {
                            $ignore = true;
                        }
                    }
                    if ($subnetOld['masterSubnetId'] == $existingSubnet['id']) {
                        $ignore = true;
                    }
                    # exclude subnet to be resized from checking
                    if ($subnetOld['id'] == $existingSubnet['id']) {
                        $ignore = true;
                    }
                    if ($ignore == false) {
                        if (verifyIPv4SubnetOverlapping($subnetNew, $existingSubnet['subnet'])) {
                            return _('Subnet overlapps with') . ' ' . $existingSubnet['subnet'] . " ({$existingSubnet['description']})";
                        }
                    }
                }
            }
        }
    } else {
        /* verify new against each existing */
        foreach ($allSubnets as $existingSubnet) {
            /* we need cidr format! */
            $existingSubnet['subnet'] = Transform2long($existingSubnet['subnet']) . '/' . $existingSubnet['mask'];
            /* only check if vrfId's match */
            if ($existingSubnet['vrfId'] == $subnetOld['vrfId']) {
                # check if it is nested properly - inside its own parent, otherwise check for overlapping
                $allParents = getAllParents($subnetOld['masterSubnetId']);
                foreach ($allParents as $kp => $p) {
                    if ($existingSubnet['id'] = $kp) {
                        $ignore = true;
                    }
                }
                if ($ignore == false) {
                    if (verifyIPv6SubnetOverlapping($subnetNew, $existingSubnet['subnet'])) {
                        return _('Subnet overlapps with') . ' ' . $existingSubnet['subnet'] . " ({$existingSubnet['description']})";
                    }
                }
            }
        }
    }
    return false;
}
Example #4
0
/**
 * describe specific table
 */
function fieldExists($table, $fieldName)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* first update request */
    $query = 'describe `' . $table . '` `' . $fieldName . '`;';
    /* execute */
    try {
        $count = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ": {$error}</div>";
        return false;
    }
    /* return true if it exists */
    if (sizeof($count) == 0) {
        return false;
    } else {
        return true;
    }
}
Example #5
0
/**
 *	fetch instructions
 */
function fetchInstructions()
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* execute query */
    $query = "select * from instructions;";
    /* execute */
    try {
        $instructions = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ": {$error}</div>";
        return false;
    }
    /* return result */
    return $instructions;
}
Example #6
0
/**
 * Get API key by id
 */
function getAPIkeyById($id)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    # set query
    $query = "select * from `api` where `id` = {$id};";
    # get result
    try {
        $api = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print $error;
        return false;
    }
    return $api[0];
}
Example #7
0
/**
 *	Get all IP addresses for scan
 */
function getAllIPsforScan($cli = false)
{
    global $db;
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    //set query
    $query = 'select `i`.`id`,`subnetId`,`ip_addr`,`lastSeen`,`lastSeen` as `oldStamp` from `ipaddresses` as `i`, `subnets` as `s` where `i`.`subnetId`=`s`.`id` and `s`.`pingSubnet` = 1 and `i`.`excludePing` != 1 order by `lastSeen` desc;';
    //get IP addresses
    try {
        $res = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        //output error
        if ($cli) {
            print "Error:{$error}";
        } else {
            print "<div class='alert alert-error'>" . _('Error') . ":{$error}</div>";
        }
        return false;
    }
    //return
    return $res;
}
    $query = "select `id`,`ip_addr`,`dns_name` from `ipaddresses` where ";
    # go through subnets
    foreach ($resCnf['subnets'] as $subnetId) {
        $query .= '`subnetId` = "' . $subnetId . '" or ';
    }
    # remove last or
    $query = substr($query, 0, -3);
    # get ony ip's with empty DNS
    if ($resCnf['emptyonly'] == 1) {
        $query .= ' and `dns_name` like "" ';
    }
    $query .= 'order by `ip_addr` ASC;';
}
# fetch records
$database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
$ipaddresses = $database->getArray($query);
# try to update dns records
foreach ($ipaddresses as $ip) {
    # try to resolve
    $hostname = gethostbyaddr(Transform2long($ip['ip_addr']));
    if ($hostname != Transform2long($ip['ip_addr'])) {
        # update
        $query = 'update `ipaddresses` set `dns_name` = "' . $hostname . '" where `id` = "' . $ip['id'] . '"';
        $database->executeQuery($query);
        # set text
        $res[] = 'updated ip address ' . Transform2long($ip['ip_addr']) . ' with hostname ' . $hostname;
    }
}
# if verbose print result so it can be emailed via cron!
if ($resCnf['verbose'] == 1) {
    foreach ($res as $line) {
Example #9
0
/**
 *	Get all changelogs
 */
function getAllChangelogs($filter = false, $expr, $limit = 100)
{
    /* set query, open db connection and fetch results */
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    //no filter
    if (!$filter) {
        $query = "select * from (\n\t\t\t\t\tselect `cid`, `coid`,`ctype`,`real_name`,`caction`,`cresult`,`cdate`,`cdiff`,`ip_addr`,'mask',`sectionId`,`subnetId`,`ip`.`id` as `tid`,`u`.`id` as `userid`,`su`.`isFolder` as `isFolder`,`su`.`description` as `sDescription`\n\t\t\t\t\tfrom `changelog` as `c`, `users` as `u`,`ipaddresses` as `ip`,`subnets` as `su`\n\t\t\t\t\twhere `c`.`ctype` = 'ip_addr' and `c`.`cuser` = `u`.`id` and `c`.`coid`=`ip`.`id` and `ip`.`subnetId` = `su`.`id`\n\t\t\t\t\tunion all\n\t\t\t\t\tselect `cid`, `coid`,`ctype`,`real_name`,`caction`,`cresult`,`cdate`,`cdiff`,`subnet`,`mask`,`sectionId`,'subnetId',`su`.`id` as `tid`,`u`.`id` as `userid`,`su`.`isFolder` as `isFolder`,`su`.`description` as `sDescription`\n\t\t\t\t\tfrom `changelog` as `c`, `users` as `u`,`subnets` as `su`\n\t\t\t\t\twhere `c`.`ctype` = 'subnet' and  `c`.`cuser` = `u`.`id` and `c`.`coid`=`su`.`id`\t\n\t\t\t\t) as `ips` order by `cid` desc limit {$limit};";
    } else {
        /* replace * with % */
        if (substr($expr, 0, 1) == "*") {
            $expr[0] = "%";
        }
        if (substr($expr, -1, 1) == "*") {
            $expr = substr_replace($expr, "%", -1);
        }
        $query = "select * from (\n\t\t\t\t\tselect `cid`, `coid`,`ctype`,`real_name`,`caction`,`cresult`,`cdate`,`cdiff`,`ip_addr`,'mask',`sectionId`,`subnetId`,`ip`.`id` as `tid`,`u`.`id` as `userid`,`su`.`isFolder` as `isFolder`,`su`.`description` as `sDescription`\n\t\t\t\t\tfrom `changelog` as `c`, `users` as `u`,`ipaddresses` as `ip`,`subnets` as `su`\n\t\t\t\t\twhere `c`.`ctype` = 'ip_addr' and `c`.`cuser` = `u`.`id` and `c`.`coid`=`ip`.`id` and `ip`.`subnetId` = `su`.`id`\n\t\t\t\t\tunion all\n\t\t\t\t\tselect `cid`, `coid`,`ctype`,`real_name`,`caction`,`cresult`,`cdate`,`cdiff`,`subnet`,`mask`,`sectionId`,'subnetId',`su`.`id` as `tid`,`u`.`id` as `userid`,`su`.`isFolder` as `isFolder`,`su`.`description` as `sDescription`\n\t\t\t\t\tfrom `changelog` as `c`, `users` as `u`,`subnets` as `su`\n\t\t\t\t\twhere `c`.`ctype` = 'subnet' and  `c`.`cuser` = `u`.`id` and `c`.`coid`=`su`.`id`\t\n\t\t\t\t) as `ips` \n\t\t\t\twhere `coid`='{$expr}' or `ctype`='{$expr}' or `real_name` like '{$expr}' or `cdate` like '{$expr}' or `cdiff` like '{$expr}' or INET_NTOA(`ip_addr`) like '{$expr}'\n\t\t\t\torder by `cid` desc limit {$limit};";
    }
    # execute
    try {
        $res = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>";
        return false;
    }
    # return result
    return $res;
}