Ejemplo n.º 1
0
    $modwsmenu[1]['commandjs'] = "xajax_window_submit('edit_dhcp_option_entry', xajax.getFormValues('form_global_{$record['id']}'), 'editor');";
    $modwsmenu[1]['image'] = '/images/silk/page_add.png';
}
if ($rows) {
    // DHCP ENTRIES LIST
    $modbodyhtml .= <<<EOL
        <!-- DHCP INFORMATION -->
        <table width=100% cellspacing="0" border="0" cellpadding="0" style="margin-bottom: 8px; margin-top: 0px;">
EOL;
    if ($debug_display) {
        $modbodyhtml .= <<<EOL
        <tr><td><pre>{$rec_content}</pre><pre>{$extra_content}</pre></td></tr>
EOL;
    }
    foreach ($dhcp_entries as $entry) {
        list($status, $rows, $dhcp_type) = ona_get_dhcp_option_entry_record(array('id' => $entry['id']));
        foreach (array_keys($dhcp_type) as $key) {
            $dhcp_type[$key] = htmlentities($dhcp_type[$key], ENT_QUOTES);
        }
        $rowstyle = '';
        if ($dhcp_type['display_name'] == "Default Gateway") {
            $hasgateway = 1;
        }
        // check that the hostname value matches the primary dns name.. they should be the same. warn only
        if ($kind == 'host' and $dhcp_type['display_name'] == "Host Name" and (strpos($record['fqdn'], $dhcp_type['value'] . '.') != 0 or strpos($record['fqdn'], $dhcp_type['value'] . '.') === FALSE)) {
            $rowstyle = 'style="background-color: #F7FF5E;" title="Hostname value does not match the primary DNS name"';
        }
        $modbodyhtml .= <<<EOL
            <tr {$rowstyle} onMouseOver="this.className='row-highlight';"
                onMouseOut="this.className='row-normal';">
Ejemplo n.º 2
0
function build_hosts($server_id = 0)
{
    global $self;
    global $onadb;
    global $dhcp_entry_options;
    printmsg("DEBUG => build_hosts() Processing hosts for server: {$server_id}", 3);
    // ipv6: for now we are going to skip over any v6 address space
    //       need to pass in if we want v6 or not
    // For the given server, select all host entries that have mac addresses
    // This is to build the static, mac based, host entries
    // NOTE: I use the concat and inet_ntoa functions.. not sure how portable they really are.
    $q = "\n        SELECT  H.id,\n                concat(D.name,'.',Z.name) primary_dns_name,\n                inet_ntoa(I.IP_ADDR) ip_addr,\n                UPPER(I.MAC_ADDR) mac,\n                B.ID dhcp_entry_id\n        FROM    hosts H,\n                dns D,\n                domains Z,\n                interfaces I LEFT OUTER JOIN dhcp_option_entries B ON I.host_id = B.host_id\n        WHERE   I.mac_addr NOT like ''\n        AND     I.host_id = H.id\n        AND     D.domain_id = Z.id\n        AND     D.id = H.primary_dns_id\n        AND     (\n                 ( I.subnet_id IN (\n                        SELECT  subnet_id\n                        FROM    dhcp_server_subnets\n                        WHERE   host_id = {$server_id}\n                        AND     ip_addr < 4294967295)\n                 )\n                 OR\n                 ( I.subnet_id IN (\n                        SELECT  subnet_id\n                        FROM    dhcp_pools\n                        WHERE   dhcp_failover_group_id IN (\n                                SELECT id\n                                FROM dhcp_failover_groups\n                                WHERE   primary_server_id = {$server_id}\n                                OR      secondary_server_id = {$server_id}))\n                 )\n                )\n        ORDER BY I.ip_addr";
    // exectue the query
    $rs = $onadb->Execute($q);
    if ($rs === false or !$rs->RecordCount()) {
        $self['error'] = 'ERROR => build_hosts(): SQL query failed: ' . $onadb->ErrorMsg();
        printmsg($self['error'], 0);
        $exit += 1;
    }
    $rows = $rs->RecordCount();
    // print the opening host comment with row count
    if ($rows) {
        $text = "\n# --------HOSTS (count={$rows})--------\n\n";
    }
    // Loop through the record set
    $last_host = 0;
    while ($host = $rs->FetchRow()) {
        printmsg('DEBUG => build_host() Processing host: ' . $host['primary_dns_name'], 5);
        // print closing brace only if this is a new host, AND it is not the first row
        if ($last_host != $host['ip_addr'] && $last_host != 0) {
            $text .= "}\n\n";
        }
        if ($last_host != $host['ip_addr']) {
            $text .= "host {$host['ip_addr']} {  # {$host['primary_dns_name']}\n";
            // TODO: ipv6 may be fun here
            // https://lists.isc.org/pipermail/dhcp-users/2009-February/008463.html
            // https://lists.isc.org/pipermail/dhcp-users/2009-March/008678.html
            // http://www.ietf.org/mail-archive/web/dhcwg/current/msg12455.html
            $text .= "    fixed-address {$host['ip_addr']};\n";
            // Currently we are not supporting other hardware types available
            // tokenring and fddi are other options than ethernet here,
            // if it is needed.. use the hardware type option. possible TODO to fix this
            $text .= "    hardware ethernet " . mac_mangle($host['mac']) . ";\n";
            // hostname option does not seem to be found in the "dhcp handbook" so I'm leaving it out for now
            //$text .= "    option hostname \"{$host['fqdn']}\";\n";
        }
        // process any dhcp options
        if ($host['dhcp_entry_id']) {
            list($status, $rows, $dhcp_entry) = ona_get_dhcp_option_entry_record(array('id' => $host['dhcp_entry_id']));
            if (!$rows) {
                break;
            }
            if ($status) {
                $exit++;
                break;
            }
            // format the tag appropriatly
            list($status, $formatted_entry) = format_tag($dhcp_entry);
            if ($formatted_entry != '') {
                $text .= "    option {$dhcp_entry['name']} {$formatted_entry};\n";
            } else {
                $exit++;
                break;
            }
        }
        // increment the host anchor to determine if we are dealing with a new host in the next loop
        $last_host = $host['ip_addr'];
    }
    // print the final closing brace
    if ($rows) {
        $text .= "}\n\n";
    }
    // close the record set
    $rs->Close();
    // return host config text
    return array($exit, $text);
}
Ejemplo n.º 3
0
function ws_editor($window_name, $form = '')
{
    global $conf, $self, $onadb;
    global $font_family, $color, $style, $images;
    $window = array();
    // Check permissions
    if (!auth('advanced')) {
        $response = new xajaxResponse();
        $response->addScript("alert('Permission denied!');");
        return $response->getXML();
    }
    // If an array in a string was provided, build the array and store it in $form
    $form = parse_options_string($form);
    // If $form is a number, it's an dhcp entry record id- so we transform $form into an array
    if ($form['id']) {
        list($status, $rows, $dhcp_entry) = ona_get_dhcp_option_entry_record(array('id' => $form['id']));
        $window['title'] = "Edit DHCP Entry";
    } else {
        $window['title'] = "Add DHCP Entry";
    }
    // If they are adding a global option
    $global_id = 'N';
    if (is_numeric($form['global_id'])) {
        // Setup a title description for this edit type
        $window['edit_type'] = "Global";
        $window['edit_type_value'] = 'This will be a Global DHCP option';
        $global_id = 'Y';
    }
    // Load the subnet record and associated info.
    if (is_numeric($form['subnet_id'])) {
        list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $form['subnet_id']));
        // Setup a title description for this edit type
        $window['edit_type'] = "Subnet";
        $window['edit_type_value'] = "{$subnet['name']}";
    }
    // If they are adding a new DHCP entry they will usually pass a host_id in
    if (is_numeric($form['host_id'])) {
        list($status, $rows, $host) = ona_find_host($form['host_id']);
        // Setup a title description for this edit type
        $window['edit_type'] = "Host";
        $window['edit_type_value'] = $host['fqdn'];
    }
    // If they are adding a new server level DHCP entry they will usually pass a server_id in
    if (is_numeric($form['server_id'])) {
        list($status, $rows, $server) = ona_find_host($form['server_id']);
        // Setup a title description for this edit type
        $window['edit_type'] = "Server";
        $window['edit_type_value'] = $server['fqdn'];
    }
    // Escape data for display in html
    foreach (array_keys((array) $subnet) as $key) {
        $subnet[$key] = htmlentities($subnet[$key], ENT_QUOTES, $conf['php_charset']);
    }
    foreach (array_keys((array) $zone) as $key) {
        $zone[$key] = htmlentities($zone[$key], ENT_QUOTES, $conf['php_charset']);
    }
    foreach (array_keys((array) $host) as $key) {
        $host[$key] = htmlentities($host[$key], ENT_QUOTES, $conf['php_charset']);
    }
    foreach (array_keys((array) $server) as $key) {
        $server[$key] = htmlentities($server[$key], ENT_QUOTES, $conf['php_charset']);
    }
    // Build dhcp option list
    list($status, $rows, $dhcpoptions) = db_get_records($onadb, 'dhcp_options', 'id >= 1', 'display_name');
    $dhcp_option_list = '<option value="">&nbsp;</option>\\n';
    $dhcpoptions['dhcp_options'] = htmlentities($dhcpoptions['display_name']);
    foreach ($dhcpoptions as $record) {
        $selected = "";
        if ($record['id'] == $dhcp_entry['dhcp_option_id']) {
            $selected = "SELECTED=\"selected\"";
        }
        if ($record['id']) {
            $dhcp_option_list .= "<option {$selected} value=\"{$record['id']}\">{$record['display_name']} ({$record['number']})</option>\n";
        }
    }
    // Javascript to run after the window is built
    $window['js'] = <<<EOL
        /* Put a minimize icon in the title bar */
        el('{$window_name}_title_r').innerHTML =
            '&nbsp;<a onClick="toggle_window(\\'{$window_name}\\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' +
            el('{$window_name}_title_r').innerHTML;

        /* Put a help icon in the title bar */
        el('{$window_name}_title_r').innerHTML =
            '&nbsp;<a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' +
            el('{$window_name}_title_r').innerHTML;

        el('{$window_name}_form').onsubmit = function() { return false; };
EOL;
    // Define the window's inner html
    $window['html'] = <<<EOL

    <!-- DHCP entry Edit Form -->
    <form id="{$window_name}_form" onSubmit="return false;">
    <input type="hidden" name="host" value="{$host['id']}">
    <input type="hidden" name="subnet" value="{$subnet['id']}">
    <input type="hidden" name="server" value="{$server['id']}">
    <input type="hidden" name="global" value="{$global_id}">
    <input type="hidden" name="id" value="{$dhcp_entry['id']}">
    <input type="hidden" name="js" value="{$form['js']}">
    <table cellspacing="0" border="0" cellpadding="0" style="background-color: {$color['window_content_bg']}; padding-left: 20px; padding-right: 20px; padding-top: 5px; padding-bottom: 5px;">

        <!-- DHCP ENTRY RECORD -->
        <tr>
            <td align="left" nowrap="true"><b><u>DHCP Entry Record</u></b>&nbsp;</td>
            <td class="padding" align="left" width="100%">&nbsp;</td>
        </tr>

        <tr>
            <td align="right" nowrap="true">
                {$window['edit_type']}:
            </td>
            <td class="padding" align="left" width="100%">
                {$window['edit_type_value']}
            </td>
        </tr>

        <tr>
            <td class="input_required" align="right" nowrap="true" >
                DHCP Option
            </td>
            <td class="padding" align="left" width="100%">
                <select id="option" name="option" class="edit" accesskey="l">
                    {$dhcp_option_list}
                </select>
            </td>
        </tr>

        <tr>
            <td class="input_required" align="right" nowrap="true">
                Value
            </td>
            <td class="padding" align="left" width="100%">
                <input
                    name="value"
                    alt="Value"
                    value="{$dhcp_entry['value']}"
                    class="edit"
                    type="text"
                    size="31" maxlength="255"
                >
            </td>
        </tr>



        <tr>
            <td align="right" valign="top" nowrap="true">
                &nbsp;
            </td>
            <td class="padding" align="right" width="100%">
                <input type="hidden" name="overwrite" value="{$overwrite}">
                <input class="edit" type="button" name="cancel" value="Cancel" onClick="removeElement('{$window_name}');">
                <input class="edit" type="button"
                    name="submit"
                    value="Save"
                    accesskey=" "
                    onClick="xajax_window_submit('{$window_name}', xajax.getFormValues('{$window_name}_form'), 'save');"
                >
            </td>
        </tr>

    </table>
    </form>
EOL;
    return window_open($window_name, $window);
}
Ejemplo n.º 4
0
function host_del($options = "")
{
    global $conf, $self, $onadb;
    printmsg("DEBUG => host_del({$options}) called", 3);
    // Version - UPDATE on every edit!
    $version = '1.19';
    // Parse incoming options string to an array
    $options = parse_options($options);
    // Sanitize options[commit] (default is no)
    $options['commit'] = sanitize_YN($options['commit'], 'N');
    // Return the usage summary if we need to
    if ($options['help'] or !$options['host']) {
        // NOTE: Help message lines should not exceed 80 characters for proper display on a console
        $self['error'] = 'ERROR => Insufficient parameters';
        return array(1, <<<EOM

host_del-v{$version}
Deletes a host, and all related records from the database

  Synopsis: host_del [KEY=VALUE] ...

  Required:
    host=NAME[.DOMAIN] or ID      Hostname or ID of the host to delete

  Optional:
    commit=[yes|no]               Commit db transaction (no)

  Notes:
    * A host won't be deleted if it has config text records
    * A host won't be deleted if it's configured as a dns or dhcp server


EOM
);
    }
    // Find the host (and domain) record from $options['host']
    list($status, $rows, $host) = ona_find_host($options['host']);
    printmsg("DEBUG => host_del() Host: {$host['fqdn']} ({$host['id']})", 3);
    if (!$host['id']) {
        printmsg("DEBUG => Unknown host: {$host['fqdn']}", 3);
        $self['error'] = "ERROR => Unknown host: {$host['fqdn']}";
        return array(2, $self['error'] . "\n");
    }
    // Check permissions
    if (!auth('host_del') or !authlvl($host['LVL'])) {
        $self['error'] = "Permission denied!";
        printmsg($self['error'], 0);
        return array(10, $self['error'] . "\n");
    }
    // If "commit" is yes, delete the host
    if ($options['commit'] == 'Y') {
        $text = "";
        $add_to_error = "";
        $add_to_status = 0;
        // SUMMARY:
        //   Don't allow a delete if it is performing server duties
        //   Don't allow a delete if config text entries exist
        //   Delete Interfaces
        //   Delete interface cluster entries
        //   Delete dns records
        //   Delete custom attributes
        //   Delete DHCP entries
        //   Delete device record if it is the last host associated with it.
        //
        // IDEA: If it's the last host in a domain (maybe do the same for or a networks & vlans in the interface delete)
        //       It could just print a notice or something.
        // Check that it is the host is not performing server duties
        // FIXME: MP mostly fixed..needs testing
        $serverrow = 0;
        // check ALL the places server_id is used and remove the entry from server_b if it is not used
        list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_server_subnets', array('host_id' => $host['id']));
        if ($rows) {
            $serverrow++;
        }
        list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('primary_server_id' => $host['id']));
        if ($rows) {
            $serverrow++;
        }
        list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('secondary_server_id' => $host['id']));
        if ($rows) {
            $serverrow++;
        }
        if ($serverrow > 0) {
            printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DHCP server!", 3);
            $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DHCP server!";
            return array(5, $self['error'] . "\n");
        }
        // Check if host is a dns server
        $serverrow = 0;
        list($status, $rows, $srecord) = db_get_record($onadb, 'dns_server_domains', array('host_id' => $host['id']));
        if ($rows) {
            $serverrow++;
        }
        if ($serverrow > 0) {
            printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DNS server!", 3);
            $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DNS server!";
            return array(5, $self['error'] . "\n");
        }
        // Display an error if it has any entries in configurations
        list($status, $rows, $server) = db_get_record($onadb, 'configurations', array('host_id' => $host['id']));
        if ($rows) {
            printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it has config archives!", 3);
            $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it has config archives!";
            return array(5, $self['error'] . "\n");
        }
        // Delete interface(s)
        // get list for logging
        $clustcount = 0;
        $dnscount = 0;
        list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('host_id' => $host['id']));
        // Cant delete if one of the interfaces is primary for a cluster
        foreach ($interfaces as $int) {
            list($status, $rows, $records) = db_get_records($onadb, 'interface_clusters', array('interface_id' => $int['id']));
            $clustcount = $clustcount + $rows;
        }
        if ($clustcount) {
            $self['error'] = "ERROR => host_del() An interface on this host is primary for some interface shares, delete the share or move the interface first.";
            printmsg($self['error'], 0);
            return array(5, $self['error'] . "\n");
        }
        // do the interface_cluster delete.  This just removes this host from the cluster, not the whole cluster itself
        // It will error out as well if this interface is the primary in the cluster
        list($status, $rows) = db_delete_records($onadb, 'interface_clusters', array('host_id' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() interface_cluster delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $self['error'] . "\n");
        }
        // log deletions
        printmsg("INFO => {$rows} Shared interface(s) DELETED from {$host['fqdn']}", 0);
        $add_to_error .= "INFO => {$rows} Shared interface(s) DELETED from {$host['fqdn']}\n";
        // Delete each DNS record associated with this hosts interfaces.
        //         foreach ($interfaces as $int) {
        //             // Loop through each dns record associated with this interface.
        //             list($status, $rows, $records) = db_get_records($onadb, 'dns', array('interface_id' => $int['id']));
        //             if ($rows) {
        //                 foreach($records as $record) {
        //                     // Run the module
        //                     list($status, $output) = run_module('dns_record_del', array('name' => $record['id'], 'type' => $record['type'], 'commit' => 'Y', 'delete_by_module' => 'Y'));
        //                     $add_to_error .= $output;
        //                     $add_to_status = $add_to_status + $status;
        //                 }
        //             }
        //         }
        // Delete messages
        // get list for logging
        list($status, $rows, $records) = db_get_records($onadb, 'messages', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
        // do the delete
        list($status, $rows) = db_delete_records($onadb, 'messages', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() message delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $self['error'] . "\n");
        }
        // log deletions
        printmsg("INFO => {$rows} Message(s) DELETED from {$host['fqdn']}", 0);
        $add_to_error .= "INFO => {$rows} Message(s) DELETED from {$host['fqdn']}\n";
        // Delete the interfaces.. this should delete dns names and other things associated with interfaces..
        foreach ($interfaces as $record) {
            // Run the module
            list($status, $output) = run_module('interface_del', array('interface' => $record['id'], 'commit' => 'on', 'delete_by_module' => 'Y'));
            $add_to_error .= $output;
            $add_to_status = $add_to_status + $status;
        }
        // Delete device record
        // Count how many hosts use this same device
        list($status, $rows, $records) = db_get_records($onadb, 'hosts', array('device_id' => $host['device_id']));
        // if device count is just 1 do the delete
        if ($rows == 1) {
            list($status, $rows) = db_delete_records($onadb, 'devices', array('id' => $host['device_id']));
            if ($status) {
                $self['error'] = "ERROR => host_del() device delete SQL Query failed: {$self['error']}";
                printmsg($self['error'], 0);
                return array(5, $add_to_error . $self['error'] . "\n");
            }
            // log deletions
            printmsg("INFO => Device record DELETED: [{$record['id']}] no remaining hosts using this device", 0);
        } else {
            printmsg("INFO => Device record NOT DELETED: [{$record['id']}] there are other hosts using this device.", 1);
        }
        // Delete tag entries
        list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
        $log = array();
        $i = 0;
        foreach ($records as $record) {
            $log[$i] = "INFO => Tag DELETED: {$record['name']} from {$host['fqdn']}";
            $i++;
        }
        //do the delete
        list($status, $rows) = db_delete_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() Tag delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $add_to_error . $self['error'] . "\n");
        }
        //log deletions
        foreach ($log as $log_msg) {
            printmsg($log_msg, 0);
            $add_to_error .= $log_msg . "\n";
        }
        // Delete custom attribute entries
        // get list for logging
        list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
        $log = array();
        $i = 0;
        foreach ($records as $record) {
            list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
            $log[$i] = "INFO => Custom Attribute DELETED: {$ca['name']} ({$ca['value']}) from {$host['fqdn']}";
            $i++;
        }
        //do the delete
        list($status, $rows) = db_delete_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() Custom attribute delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $add_to_error . $self['error'] . "\n");
        }
        //log deletions
        foreach ($log as $log_msg) {
            printmsg($log_msg, 0);
            $add_to_error .= $log_msg . "\n";
        }
        // Delete DHCP options
        // get list for logging
        list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
        $log = array();
        $i = 0;
        foreach ($records as $record) {
            list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id']));
            $log[$i] = "INFO => DHCP entry DELETED: {$dhcp['display_name']}={$dhcp['value']} from {$host['fqdn']}";
            $i++;
        }
        // do the delete
        list($status, $rows) = db_delete_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() DHCP option entry delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $add_to_error . $self['error'] . "\n");
        }
        // log deletions
        foreach ($log as $log_msg) {
            printmsg($log_msg, 0);
            $add_to_error .= $log_msg . "\n";
        }
        // Delete the host
        list($status, $rows) = db_delete_records($onadb, 'hosts', array('id' => $host['id']));
        if ($status) {
            $self['error'] = "ERROR => host_del() host delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $add_to_error . $self['error'] . "\n");
        }
        // Return the success notice
        if ($add_to_status == 0) {
            $self['error'] = "INFO => Host DELETED: {$host['fqdn']}";
        }
        printmsg($self['error'], 0);
        return array($add_to_status, $add_to_error . $self['error'] . "\n");
    }
    //
    // We are just displaying records that would have been deleted
    //
    // SUMMARY:
    //   Display a warning if it is a server
    //   Display a warning if it has config text entries
    //   Display Interfaces
    //   Display dns records
    //   Display custom attributes
    //   Display DHCP entries
    // Otherwise just display the host record for the host we would have deleted
    $text = "Record(s) NOT DELETED (see \"commit\" option)\n" . "Displaying record(s) that would have been deleted:\n";
    // Display a warning if host is performing server duties
    list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_server_subnets', array('host_id' => $host['id']));
    if ($rows) {
        $text .= "\nWARNING!  This host is a DHCP server for {$rows} subnet(s)\n";
    }
    list($status, $rows, $srecord) = db_get_record($onadb, 'dns_server_domains', array('host_id' => $host['id']));
    if ($rows) {
        $text .= "\nWARNING!  This host is a DNS server for one or more domains!\n";
    }
    list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('primary_server_id' => $host['id']));
    if ($rows) {
        $text .= "\nWARNING!  This host is a server that is primary in a DHCP failover group\n";
    }
    list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('secondary_server_id' => $host['id']));
    if ($rows) {
        $text .= "\nWARNING!  This host is a server that is secondary in a DHCP failover group\n";
    }
    // Display a warning if it has any configurations
    list($status, $rows, $server) = db_get_record($onadb, 'configurations', array('host_id' => $host['id']));
    if ($rows) {
        $text .= "\nWARNING!  Host can not be deleted, it has config archives!\n";
    }
    if ($rows) {
        $text .= "\nWARNING!  Host will NOT be deleted, due to previous warnings!\n";
    }
    // Display the Host's complete record
    list($status, $tmp) = host_display("host={$host['id']}&verbose=N");
    $text .= "\n" . $tmp;
    // Display count of messages
    list($status, $rows, $records) = db_get_records($onadb, 'messages', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
    if ($rows) {
        $text .= "\nASSOCIATED MESSAGE RECORDS ({$rows}):\n";
    }
    // Display associated interface(s)
    list($status, $int_rows, $interfaces) = db_get_records($onadb, 'interfaces', array('host_id' => $host['id']));
    // show the dns records associated
    foreach ($interfaces as $record) {
        list($status, $rows, $dnsrec) = db_get_records($onadb, 'dns', array('interface_id' => $record['id']));
        if ($rows) {
            $text .= "\nASSOCIATED DNS RECORDS ({$rows}) ON INTERFACE (" . ip_mangle($record['ip_addr'], 'dotted') . "):\n";
            foreach ($dnsrec as $rec) {
                // show AAAA or A type as needed
                if ($record['ip_addr'] > 4294967295 and $rec['type'] == 'A') {
                    $rec['type'] = 'AAAA';
                }
                $text .= "  TYPE: [ID:{$rec['id']}] {$rec['type']}, {$rec['name']} -> " . ip_mangle($record['ip_addr'], 'dotted') . "\n";
            }
        }
    }
    if ($int_rows) {
        $text .= "\nASSOCIATED INTERFACE RECORDS ({$int_rows}):\n";
    }
    foreach ($interfaces as $record) {
        $text .= "  [ID:{$record['id']}] " . ip_mangle($record['ip_addr'], 'dotted') . "\n";
    }
    // Display associated interface_clusters(s)
    list($status, $clust_rows, $interfaceclusters) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id']));
    if ($clust_rows) {
        $text .= "\nASSOCIATED SHARED INTERFACE RECORDS ({$clust_rows}):\n";
    }
    foreach ($interfaceclusters as $record) {
        list($status, $rows, $int) = ona_get_interface_record(array('id' => $record['interface_id']));
        $text .= "  [ID:{$int['id']}] {$int['ip_addr_text']}\n";
    }
    // Display associated tags
    list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
    if ($rows) {
        $text .= "\nASSOCIATED TAG RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        $text .= "  {$record['name']}\n";
    }
    // Display associated custom attributes
    list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
    if ($rows) {
        $text .= "\nASSOCIATED CUSTOM ATTRIBUTE RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
        $text .= "  {$ca['name']} => {$ca['value']}\n";
    }
    // Display associated DHCP entries
    list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
    if ($rows) {
        $text .= "\nASSOCIATED DHCP OPTION RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id']));
        $text .= "  {$dhcp['display_name']} => {$dhcp['value']}\n";
    }
    return array(7, $text);
}
Ejemplo n.º 5
0
function subnet_del($options = "")
{
    global $conf, $self, $onadb;
    // Version - UPDATE on every edit!
    $version = '1.06';
    printmsg('DEBUG => subnet_del(' . $options . ') called', 3);
    // Parse incoming options string to an array
    $options = parse_options($options);
    // Sanitize options[commit] (default is no)
    $options['commit'] = sanitize_YN($options['commit'], 'N');
    // Return the usage summary if we need to
    if ($options['help'] or !$options['subnet']) {
        // NOTE: Help message lines should not exceed 80 characters for proper display on a console
        $self['error'] = 'ERROR => Insufficient parameters';
        return array(1, <<<EOM

subnet_del-v{$version}
Deletes a subnet (subnet) from the database

  Synopsis: subnet_del [KEY=VALUE] ...

  Required:
    subnet=IP or ID              select subnet by search string

  Optional:
    commit=[yes|no]               commit db transaction (no)


EOM
);
    }
    // Find the subnet record we're deleting
    list($status, $rows, $subnet) = ona_find_subnet($options['subnet']);
    if ($status or !$rows) {
        $self['error'] = "ERROR => Subnet not found";
        return array(2, $self['error'] . "\n");
    }
    // Check permissions
    if (!auth('subnet_del') or !authlvl($subnet['lvl'])) {
        $self['error'] = "Permission denied!";
        printmsg($self['error'], 0);
        return array(3, $self['error'] . "\n");
    }
    // If "commit" is yes, delete the subnet
    if ($options['commit'] == 'Y') {
        $text = "";
        // FIXME: (add all this) ...
        // SUMMARY:
        //   Delete assignments to any DHCP servers
        //   Delete any DHCP pools on the current subnet
        //   Delete any DHCP options associated with this subnet
        //   Delete any interfaces belonging to hosts with more than one interface
        //   Delete any hosts (and all their associated info) that have only one interface
        //   Delete subnet Record
        //   Delete custom attributes
        //
        //   FIXME: display a warning if there are no more subnets that a dhcp server is serving dhcp for?
        // Delete DHCP server assignments
        list($status, $rows) = db_delete_records($onadb, 'dhcp_server_subnets', array('subnet_id' => $subnet['id']));
        if ($status) {
            $self['error'] = "ERROR => DHCP server assignment delete failed: {$self['error']}";
            return array(5, $self['error'] . "\n");
        }
        // Delete DHCP pools
        list($status, $rows) = db_delete_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id']));
        if ($status) {
            $self['error'] = "ERROR => DHCP pool delete failed: {$self['error']}";
            return array(5, $self['error'] . "\n");
        }
        // Delete DHCP options
        list($status, $rows) = db_delete_records($onadb, 'dhcp_option_entries', array('subnet_id' => $subnet['id']));
        if ($status) {
            $self['error'] = "ERROR => DHCP parameter delete failed: {$self['error']}";
            return array(5, $self['error'] . "\n");
        }
        // Delete tag entries
        list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id']));
        $log = array();
        $i = 0;
        foreach ($records as $record) {
            $log[$i] = "INFO => Tag DELETED: {$record['name']} from {$subnet['name']}";
            $i++;
        }
        //do the delete
        list($status, $rows) = db_delete_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id']));
        if ($status) {
            $self['error'] = "ERROR => subnet_del() Tag delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $add_to_error . $self['error'] . "\n");
        }
        //log deletions
        foreach ($log as $log_msg) {
            printmsg($log_msg, 0);
            $add_to_error .= $log_msg . "\n";
        }
        // Delete custom attribute entries
        // get list for logging
        list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id']));
        $log = array();
        $i = 0;
        foreach ($records as $record) {
            list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
            $log[$i] = "INFO => Custom Attribute DELETED: {$ca['name']} ({$ca['value']}) from {$subnet['name']}";
            $i++;
        }
        //do the delete
        list($status, $rows) = db_delete_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id']));
        if ($status) {
            $self['error'] = "ERROR => subnet_del() Custom attribute delete SQL Query failed: {$self['error']}";
            printmsg($self['error'], 0);
            return array(5, $self['error'] . "\n");
        }
        //log deletions
        foreach ($log as $log_msg) {
            printmsg($log_msg, 0);
            //$add_to_error .= $log_msg . "\n";
        }
        // Delete associated host / interface records that need to be deleted
        // BUSINESS RULE: We delete hosts that have only one interface (and it's on this subnet)
        // BUSINESS RULE: We delete interfaces from hosts that have multiple interfaces
        list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('subnet_id' => $subnet['id']));
        $hosts_to_delete = array();
        $interfaces_to_delete = array();
        foreach ($interfaces as $interface) {
            // Select all  interfaces for the associated host where the subnet ID is not our subnet ID
            $where = "host_id = {$interface['host_id']} AND subnet_id != {$subnet['id']}";
            list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces', $where, '', 0);
            // We'll delete hosts that have only one interface (i.e. no interfaces on any other subnets)
            if ($rows == 0) {
                array_push($hosts_to_delete, $interface['host_id']);
            } else {
                array_push($interfaces_to_delete, $interface['id']);
            }
        }
        unset($interfaces);
        // make sure we only have one reference for each host and interface
        $interfaces_to_delete = array_unique($interfaces_to_delete);
        $hosts_to_delete = array_unique($hosts_to_delete);
        // Delete interfaces we have selected
        foreach ($interfaces_to_delete as $interface_id) {
            list($status, $output) = run_module('interface_del', array('interface' => $interface_id, 'commit' => 'Y'));
            if ($status) {
                return array(5, $output);
            }
        }
        // Delete hosts we have selected
        foreach ($hosts_to_delete as $host_id) {
            list($status, $output) = run_module('host_del', array('host' => $host_id, 'commit' => 'Y'));
            if ($status) {
                return array(5, $output);
            }
        }
        // Delete the subnet
        list($status, $rows) = db_delete_records($onadb, 'subnets', array('id' => $subnet['id']));
        if ($status or !$rows) {
            $self['error'] = "ERROR => Subnet delete failed: {$self['error']}";
            return array(5, $self['error'] . "\n");
        }
        // Return the success notice
        $ip = ip_mangle($subnet['ip_addr'], 'dotted');
        $cidr = ip_mangle($subnet['ip_mask'], 'cidr');
        $self['error'] = "INFO => Subnet DELETED: {$subnet['name']} IP: {$ip}/{$cidr}";
        printmsg($self['error'], 0);
        return array(0, $self['error'] . "\n");
    }
    //
    // We are just displaying records that would have been deleted
    //
    // SUMMARY:
    //   Display assignments to any DHCP servers
    //   Display any DHCP pools on the current subnet
    //   Display any DHCP parameters associated with this subnet
    //   Display subnet Record
    //   Display Host records (and all their sub-records)
    //   Display custom attributes
    // Otherwise just display the host record for the host we would have deleted
    $text = "Record(s) NOT DELETED (see \"commit\" option)\n" . "Displaying record(s) that would have been deleted:\n";
    // Display the Subnet's complete record
    list($status, $tmp) = subnet_display("subnet={$subnet['id']}&verbose=N");
    $text .= "\n" . $tmp;
    // Display assignments to any DHCP servers
    list($status, $rows, $records) = db_get_records($onadb, 'dhcp_server_subnets', array('subnet_id' => $subnet['id']));
    if ($rows) {
        $text .= "\nASSOCIATED DHCP SERVER ASSIGNMENT RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        $text .= format_array($record);
    }
    // Display any DHCP pools on the current subnet
    list($status, $rows, $records) = db_get_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id']));
    if ($rows) {
        $text .= "\nASSOCIATED DHCP POOL RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        $text .= format_array($record);
    }
    // Display associated DHCP entries
    list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('subnet_id' => $subnet['id']));
    if ($rows) {
        $text .= "\nASSOCIATED DHCP ENTRY RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id']));
        $text .= "  {$dhcp['display_name']} => {$dhcp['value']}\n";
    }
    // Display associated tags
    list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id']));
    if ($rows) {
        $text .= "\nASSOCIATED TAG RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        $text .= "  {$record['name']}\n";
    }
    // Display associated custom attributes
    list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id']));
    if ($rows) {
        $text .= "\nASSOCIATED CUSTOM ATTRIBUTE RECORDS ({$rows}):\n";
    }
    foreach ($records as $record) {
        list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
        $text .= "  {$ca['name']} => {$ca['value']}\n";
    }
    // Display associated host  / interface records that would be deleted
    // BUSINESS RULE: We delete hosts that have only one interface (and it's on this subnet)
    // BUSINESS RULE: We delete interfaces from hosts that have multiple interfaces (including at least one on a different subnet)
    list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('subnet_id' => $subnet['id']));
    $hosts_to_delete = array();
    $interfaces_to_delete = array();
    foreach ($interfaces as $interface) {
        // Select all  interfaces for the associated host where the subnet ID is not our subnet ID
        $where = "host_id = {$interface['host_id']} AND subnet_id != {$subnet['id']}";
        list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces', $where, '', 0);
        // We'll delete hosts that have only one interface (i.e. no interfaces on any other subnets)
        if ($rows == 0) {
            array_push($hosts_to_delete, $interface['host_id']);
        } else {
            array_push($interfaces_to_delete, $interface['id']);
        }
    }
    unset($interfaces);
    // make sure we only have one reference for each host and interface
    $interfaces_to_delete = array_unique($interfaces_to_delete);
    $hosts_to_delete = array_unique($hosts_to_delete);
    // Display interfaces we would have deleted
    $rows = count($interfaces_to_delete);
    if ($rows) {
        $text .= "\n----- ASSOCIATED HOST INTERFACE RECORDS ({$rows}) -----\n";
    }
    foreach ($interfaces_to_delete as $interface_id) {
        list($status, $output) = run_module('interface_del', array('interface' => $interface_id), false);
        $output = preg_replace('/^(.*)?\\n(.*)?\\n/', '', $output);
        $text .= $output;
    }
    // Display hosts we would have deleted
    $rows = count($hosts_to_delete);
    if ($rows) {
        $text .= "\n-----ASSOCIATED HOSTS ({$rows}) -----\n";
    }
    foreach ($hosts_to_delete as $host_id) {
        list($status, $output) = run_module('host_del', array('host' => $host_id), false);
        $output = preg_replace('/^(.*)?\\n(.*)?\\n/', '', $output);
        $text .= $output;
    }
    return array(7, $text);
}
Ejemplo n.º 6
0
function dhcp_entry_modify($options = "")
{
    // The important globals
    global $conf, $self, $onadb;
    // Version - UPDATE on every edit!
    $version = '1.04';
    printmsg("DEBUG => dhcp_entry_modify({$options}) called", 3);
    // Parse incoming options string to an array
    $options = parse_options($options);
    // Return the usage summary if we need to
    if ($options['help'] or !($options['id'] and ($options['set_option'] and array_key_exists('set_value', $options) or array_key_exists('set_value', $options)))) {
        // NOTE: Help message lines should not exceed 80 characters for proper display on a console
        $self['error'] = 'ERROR => Insufficient parameters';
        return array(1, <<<EOM

dhcp_entry_modify-v{$version}
Modifies a DHCP entry in the database

  Synopsis: dhcp_entry_modify [KEY=VALUE] ...

  Where:
    id=ID                                       DHCP entry ID

  Options:
    set_option=DHCP type                        DHCP parameter type
    set_value=STRING                            string value for the DHCP type

  Notes:
    If you specify a type, you must specify a value.


EOM
);
    }
    // Determine the entry itself exists
    list($status, $rows, $entry) = ona_get_dhcp_option_entry_record(array('id' => $options['id']));
    if ($status or !$rows) {
        printmsg("DEBUG => Invalid DHCP entry record ID ({$options['id']})!", 3);
        $self['error'] = "ERROR => Invalid DHCP entry record ID ({$options['id']})!";
        return array(2, $self['error'] . "\n");
    }
    printmsg("DEBUG => dhcp_entry_modify(): Found entry, {$entry['display_name']} => {$entry['value']}", 3);
    $desc = '';
    // Load associated host, subnet or server record
    $host = $subnet = $server = array();
    if ($entry['host_id']) {
        list($status, $rows, $host) = ona_find_host($entry['host_id']);
        $desc = $host['fqdn'];
    }
    if ($entry['subnet_id']) {
        list($status, $rows, $subnet) = ona_find_subnet($entry['subnet_id']);
        $desc = "{$subnet['name']} (" . ip_mangle($subnet['ip_addr']) . ")";
    }
    if ($entry['server_id']) {
        list($status, $rows, $server) = ona_find_host($entry['server_id']);
        $desc = $server['fqdn'];
    }
    // Check permissions on source identifier
    $lvl = 100;
    if ($host['id']) {
        $lvl = $host['lvl'];
    }
    if ($subnet['id']) {
        $lvl = $subnet['lvl'];
    }
    if (!auth('advanced') or !authlvl($lvl)) {
        $self['error'] = "Permission denied!";
        printmsg($self['error'], 0);
        return array(10, $self['error'] . "\n");
    }
    // unset $host if $server is defined .. we don't need it anymore
    if ($server['id']) {
        $host = array();
    }
    // This variable will contain the updated info we'll insert into the DB
    $SET = array();
    if (array_key_exists('set_value', $options)) {
        // trim leading and trailing whitespace from 'value'
        $SET['value'] = trim($options['set_value']);
        // trim leading and trailing whitespace from 'value' and check that a value exists
        $SET['value'] = trim($options['set_value']);
        if (strlen($SET['value']) == 0) {
            printmsg("DEBUG => The DHCP value was blank", 3);
            $self['error'] = "ERROR => DHCP value was blank";
            return array(2, $self['error'] . "\n");
        }
    }
    if ($options['set_option']) {
        // Make sure they specified a value
        if (!array_key_exists('set_value', $options)) {
            printmsg("DEBUG => No value specified for given DHCP parameter type ({$options['set_option']})!", 3);
            $self['error'] = "ERROR => No value specified for given DHCP parameter type ({$options['set_option']})!";
            return array(8, $self['error'] . "\n");
        }
        // Determine the type is valid
        list($status, $rows, $type) = ona_find_dhcp_option(trim($options['set_option']));
        if ($status or !$rows) {
            printmsg("DEBUG => Invalid DHCP parameter type specified ({$options['set_option']})!", 3);
            $self['error'] = "ERROR => Invalid DHCP parameter type specified ({$options['set_option']})!";
            return array(8, $self['error'] . "\n");
        }
        printmsg("DEBUG => dhcp_entry_modify(): Found parameter type {$type['display_name']}", 3);
        $SET['dhcp_option_id'] = $type['id'];
        // Make sure this isn't a duplicate
        // TODO: this code seems a bit suspect of being nasty.. possibly fix it up
        $search = array('dhcp_option_id' => $type['id'], 'host_id' => 0, 'subnet_id' => 0);
        if ($host['id']) {
            $search['host_id'] = $host['id'];
        }
        if ($subnet['id']) {
            $search['subnet_id'] = $subnet['id'];
        }
        if ($server['id']) {
            $search['server_id'] = $server['id'];
        }
        list($status, $rows, $record) = ona_get_dhcp_option_entry_record($search);
        if ($status or $rows > 1 or $rows == 1 and $record['id'] != $entry['id']) {
            printmsg("DEBUG => That DHCP parameter type is already defined ({$search})!", 3);
            $self['error'] = "ERROR => That DHCP parameter type is already defined ({$search})!";
            return array(11, $self['error'] . "\n");
        }
    }
    // Get the dhcp entry record before updating (logging)
    list($status, $rows, $original_entry) = ona_get_dhcp_option_entry_record(array('id' => $entry['id']));
    // Update the record
    list($status, $rows) = db_update_record($onadb, 'dhcp_option_entries', array('id' => $entry['id']), $SET);
    if ($status or !$rows) {
        $self['error'] = "ERROR => dhcp_entry_modify() SQL Query failed: " . $self['error'];
        printmsg($self['error'], 0);
        return array(6, $self['error'] . "\n");
    }
    // Get the entry again to display details
    list($status, $tmp_rows, $entry) = ona_get_dhcp_option_entry_record(array('id' => $entry['id']));
    // Return the success notice
    $self['error'] = "INFO => DHCP entry UPDATED:{$entry['id']}: \"{$entry['display_name']}\"={$entry['value']} on {$desc} ";
    $log_msg = "INFO => DHCP entry UPDATED:{$entry['id']}: ";
    $more = "";
    foreach (array_keys($original_entry) as $key) {
        if ($original_entry[$key] != $entry[$key]) {
            $log_msg .= $more . $key . "[" . $original_entry[$key] . "=>" . $entry[$key] . "]";
            $more = ";";
        }
    }
    // only print to logfile if a change has been made to the record
    if ($more != '') {
        printmsg($self['error'], 0);
        printmsg($log_msg, 0);
    }
    return array(0, $self['error'] . "\n");
}