function ona_find_dns_record($search = "", $type = '', $int_id = 0) { global $conf, $self, $onadb; printmsg("DEBUG => ona_find_dns_record({$search}) called", 3); $type = strtoupper($type); $search = strtolower($search); // By record ID? if (is_numeric($search)) { list($status, $rows, $dns) = ona_get_dns_record(array('id' => $search)); if ($rows) { printmsg("DEBUG => ona_find_dns_record({$search}) called, found: {$dns['fqdn']}({$dns['type']})", 3); return array($status, $rows, $dns); } } // // It's an FQDN, do a bunch of stuff! // // lets test out if it has a / in it to strip the view name portion $view['id'] = 0; if (strstr($search, '/')) { list($dnsview, $search) = explode('/', $search); list($status, $rows, $view) = db_get_record($onadb, 'dns_views', array('name' => strtoupper($dnsview))); if (!$rows) { $view['id'] = 0; } } // Find the domain name piece of $search list($status, $rows, $domain) = ona_find_domain($search); printmsg("DEBUG => ona_find_domain({$search}) returned: {$domain['fqdn']}", 3); // Now find what the host part of $search is $hostname = str_replace(".{$domain['fqdn']}", '', $search); // If the hostname we came up with and the domain name are the same, then assume this is // meant to be a domain specific record, like A, MX, NS type records. if ($hostname == $domain['fqdn']) { $hostname = ''; } // Setup the search array $searcharray = array('domain_id' => $domain['id'], 'name' => $hostname, 'dns_view_id' => $view['id']); // If an interface_id was passed, add it to the array if ($int_id > 0) { $searcharray['interface_id'] = $int_id; } // If a type was passed, add it to the array if ($type) { $searcharray['type'] = $type; } // Let's see if that hostname is valid or not in $domain['id'] list($status, $rows, $dns) = ona_get_dns_record($searcharray); if ($rows) { // Return good status, one row, and $dns array printmsg("DEBUG => ona_find_dns_record({$search}) called, found: {$dns['fqdn']}({$dns['type']})", 3); return array(0, 1, $dns); } // Otherwise, build a fake dns record with only a few entries in it and return that $dns = array('id' => 0, 'name' => $hostname, 'fqdn' => "{$hostname}.{$domain['fqdn']}", 'domain_id' => $domain['id'], 'domain_fqdn' => $domain['fqdn'], 'type' => '', 'dns_id' => 0); printmsg("DEBUG => ona_find_dns_record({$search}) called, Nothing found, returning fake entry: {$dns['fqdn']}({$dns['type']})", 3); return array(0, 1, $dns); }
function ws_display_list($window_name, $form = '') { global $conf, $self, $onadb; global $images, $color, $style; $html = ''; $js = ''; // If the user supplied an array in a string, transform it into an array $form = parse_options_string($form); // Find the "tab" we're on $tab = $_SESSION['ona'][$form['form_id']]['tab']; // Build js to refresh this list $refresh = "xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'display_list');"; // If it's not a new query, load the previous query from the session // into $form and save the current page and filter in the session. // Also find/set the "page" we're viewing $page = 1; if ($form['page'] and is_numeric($form['page'])) { $form = array_merge($form, (array) $_SESSION['ona'][$form['form_id']][$tab]['q']); $_SESSION['ona'][$form['form_id']][$tab]['page'] = $page = $form['page']; $_SESSION['ona'][$form['form_id']][$tab]['filter'] = $form['filter']; } printmsg("DEBUG => Displaying records list page: {$page}", 1); // Calculate the SQL query offset (based on the page being displayed) $offset = $conf['search_results_per_page'] * ($page - 1); if ($offset == 0) { $offset = -1; } // Search results go in here $results = array(); $count = 0; // // *** ADVANCED RECORD SEARCH *** // FIND RESULT SET // // Start building the "where" clause for the sql query to find the records to display $where = ""; $and = ""; $orderby = ""; // enable or disable wildcards $wildcard = '%'; if ($form['nowildcard']) { $wildcard = ''; } // RECORD ID if ($form['record_id']) { $where .= $and . "id = " . $onadb->qstr($form['record_id']); $and = " AND "; } // DNS VIEW ID if ($form['dns_view']) { if (is_string($form['dns_view'])) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('name' => $form['dns_view'])); } if (is_numeric($form['dns_view'])) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('id' => $form['dns_view'])); } $where .= $and . "dns_view_id = " . $onadb->qstr($dnsview['id']); $and = " AND "; } // INTERFACE ID if ($form['interface_id']) { $where .= $and . "interface_id = " . $onadb->qstr($form['interface_id']); $and = " AND "; } // DNS RECORD note if ($form['notes']) { $where .= $and . "notes LIKE " . $onadb->qstr($wildcard . $form['notes'] . $wildcard); $and = " AND "; } // DNS RECORD TYPE if ($form['dnstype']) { $where .= $and . "type = " . $onadb->qstr($form['dnstype']); $and = " AND "; } // HOSTNAME if ($form['hostname']) { $where .= $and . "id IN (SELECT id " . " FROM dns " . " WHERE name LIKE " . $onadb->qstr($wildcard . $form['hostname'] . $wildcard) . " )"; $and = " AND "; } // DOMAIN if ($form['domain']) { // FIXME: MP test if this clause works correctly? Not sure that anything even uses this? list($status, $rows, $tmpdomain) = ona_find_domain($form['domain']); $where .= $and . "domain_id = " . $onadb->qstr($tmpdomain['id']); $orderby .= "name, domain_id"; $and = " AND "; } // DOMAIN ID if ($form['domain_id']) { //$where .= $and . "primary_dns_id IN ( SELECT id " . // " FROM dns " . // " WHERE domain_id = " . $onadb->qstr($form['domain_id']) . " ) "; $where .= $and . "domain_id = " . $onadb->qstr($form['domain_id']); $orderby .= "name, domain_id"; $and = " AND "; } // IP ADDRESS $ip = $ip_end = ''; if ($form['ip']) { // Build $ip and $ip_end from $form['ip'] and $form['ip_thru'] $ip = ip_complete($form['ip'], '0'); if ($form['ip_thru']) { $ip_end = ip_complete($form['ip_thru'], '255'); } else { $ip_end = ip_complete($form['ip'], '255'); } // Find out if $ip and $ip_end are valid $ip = ip_mangle($ip, 'numeric'); $ip_end = ip_mangle($ip_end, 'numeric'); if ($ip != -1 and $ip_end != -1) { // We do a sub-select to find interface id's between the specified ranges $where .= $and . "interface_id IN ( SELECT id " . " FROM interfaces " . " WHERE ip_addr >= " . $onadb->qstr($ip) . " AND ip_addr <= " . $onadb->qstr($ip_end) . " )"; $and = " AND "; } } // display a nice message when we dont find all the records if ($where == '' and $form['content_id'] == 'search_results_list') { $js .= "el('search_results_msg').innerHTML = 'Unable to find DNS records matching your query, showing all records';"; } // Wild card .. if $while is still empty, add a 'ID > 0' to it so you see everything. if ($where == '') { $where = 'id > 0'; } // If we dont have DNS views turned on, limit data to just the default view. // Even if there is data associated with other views, ignore it if (!$conf['dns_views']) { $where .= ' AND dns_view_id = 0'; } // Do the SQL Query $filter = ''; if ($form['filter']) { // Host names should always be lower case $form['filter'] = strtolower($form['filter']); $filter = ' AND name LIKE ' . $onadb->qstr('%' . $form['filter'] . '%'); } // If we get a specific host to look for we must do the following // 1. get (A) records that match any interface_id associated with the host // 2. get CNAMES that point to dns records that are using an interface_id associated with the host if ($form['host_id']) { // If we dont have DNS views turned on, limit data to just the default view. // Even if there is data associated with other views, ignore it // MP: something strange with this, it should only limit to default view.. sometimes it does not??? if (!$conf['dns_views']) { $hwhere .= 'dns_view_id = 0 AND '; } // Get the host record so we know what the primary interface is list($status, $rows, $host) = ona_get_host_record(array('id' => $form['host_id']), ''); list($status, $rows, $results) = db_get_records($onadb, 'dns', $hwhere . 'interface_id in (select id from interfaces where host_id = ' . $onadb->qstr($form['host_id']) . ') OR interface_id in (select interface_id from interface_clusters where host_id = ' . $onadb->qstr($form['host_id']) . ')', "type", $conf['search_results_per_page'], $offset); // If we got less than search_results_per_page, add the current offset to it // so that if we're on the last page $rows still has the right number in it. if ($rows > 0 and $rows < $conf['search_results_per_page']) { $rows += $conf['search_results_per_page'] * ($page - 1); } else { if ($rows >= $conf['search_results_per_page']) { list($status, $rows, $records) = db_get_records($onadb, 'dns', $hwhere . 'interface_id in (select id from interfaces where host_id = ' . $onadb->qstr($form['host_id']) . ') OR interface_id in (select interface_id from interface_clusters where host_id = ' . $onadb->qstr($form['host_id']) . ')' . $filter, "", 0); } } } else { list($status, $rows, $results) = db_get_records($onadb, 'dns', $where . $filter, $orderby, $conf['search_results_per_page'], $offset); // If we got less than search_results_per_page, add the current offset to it // so that if we're on the last page $rows still has the right number in it. if ($rows > 0 and $rows < $conf['search_results_per_page']) { $rows += $conf['search_results_per_page'] * ($page - 1); } else { if ($rows >= $conf['search_results_per_page']) { list($status, $rows, $records) = db_get_records($onadb, 'dns', $where . $filter, "", 0); } } } $count = $rows; // // *** BUILD HTML LIST *** // $html .= <<<EOL <!-- dns record Results --> <table id="{$form['form_id']}_dns_record_list" class="list-box" cellspacing="0" border="0" cellpadding="0"> <!-- Table Header --> <tr> <td colspan="2" class="list-header" align="center" style="{$style['borderR']};">Name</td> <td class="list-header" align="center" style="{$style['borderR']};">Time to Live</td> <td class="list-header" align="center" style="{$style['borderR']};">Type</td> <td class="list-header" align="center" style="{$style['borderR']};">Data</td> <td class="list-header" align="center" style="{$style['borderR']};">Effective</td> EOL; if ($conf['dns_views']) { $html .= "<td class=\"list-header\" align=\"center\" style=\"{$style['borderR']};\">DNS View</td>"; } $html .= <<<EOL <td class="list-header" align="center" style="{$style['borderR']};">Notes</td> <td class="list-header" align="center"> </td> </tr> EOL; // Loop and display each record // $last_record = array('name' => $results[0]['name'], 'domain_id' => $results[0]['domain_id']); // $last_record_count = 0; for ($i = 1; $i <= count($results); $i++) { $record = $results[$i]; // Get additional info about each host record $record = $results[$i - 1]; // if the interface is the primary_dns_id for the host then mark it $primary_record = ' '; if ($host['primary_dns_id'] == $record['id']) { $primary_record = '<img title="Primary DNS record" src="' . $images . '/silk/font_go.png" border="0">'; } // Check for interface records (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('id' => $record['interface_id']), ''); if ($interfaces) { // Get the host record so we know what the primary interface is //list($status, $rows, $inthost) = ona_get_host_record(array('id' => $interface['host_id']), ''); // Make the type correct based on the IP passed in if (strlen($interface['ip_addr']) > 11 and $record['type'] == 'A') { $record['type'] = 'AAAA'; } $record['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $record['subnet'] = $subnet['name']; $record['ip_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); // Create string to be embedded in HTML for display $data = <<<EOL {$record['ip_addr']} EOL; } else { // Get other DNS records which name this record as parent list($status, $rows, $dns_other) = ona_get_host_record(array('id' => $record['dns_id'])); // Create string to be embedded in HTML for display if ($rows) { $data = <<<EOL <a title="View host. ID: {$dns_other['id']}" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$dns_other['id']}\\', \\'display\\')');" >{$dns_other['name']}</a >.<a title="View domain. ID: {$dns_other['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$dns_other['domain_id']}\\', \\'display\\')');" >{$dns_other['domain_fqdn']}</a> EOL; } } $record['notes_short'] = truncate($record['notes'], 30); // Add a dot to the end of record name for display purposes $record['name'] = $record['name'] . '.'; // Process PTR record if ($record['type'] == 'PTR') { list($status, $rows, $pointsto) = ona_get_dns_record(array('id' => $record['dns_id']), ''); list($status, $rows, $pdomain) = ona_get_domain_record(array('id' => $record['domain_id']), ''); // Flip the IP address $record['name'] = ip_mangle($record['ip_addr'], 'flip'); $record['domain'] = $pdomain['name']; if ($pdomain['parent_id']) { list($status, $rows, $parent) = ona_get_domain_record(array('id' => $pdomain['parent_id'])); $parent['name'] = ona_build_domain_name($parent['id']); $record['domain'] = $pdomain['name'] . '.' . $parent['name']; unset($parent['name']); } // strip down the IP to just the "host" part as it relates to the domain its in if (strstr($record['domain'], 'in-addr.arpa')) { $domain_part = preg_replace("/.in-addr.arpa\$/", '', $record['domain']); } else { $domain_part = preg_replace("/.ip6.arpa\$/", '', $record['domain']); } $record['name'] = preg_replace("/{$domain_part}\$/", '', $record['name']); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$pointsto['name']}</a>.<a title="View domain. ID: {$pointsto['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$pointsto['domain_id']}\\', \\'display\\')');" >{$pointsto['domain_fqdn']}</a>. EOL; } // Process CNAME record if ($record['type'] == 'CNAME') { list($status, $rows, $cname) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$cname['name']}</a>.<a title="View domain. ID: {$cname['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$cname['domain_id']}\\', \\'display\\')');" >{$cname['domain_fqdn']}</a>. EOL; } // Process NS record if ($record['type'] == 'NS') { // clear out the $record['domain'] value so it shows properly in the list $record['name'] = ''; list($status, $rows, $ns) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$ns['name']}</a>.<a title="View domain. ID: {$ns['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$ns['domain_id']}\\', \\'display\\')');" >{$ns['domain_fqdn']}</a>. EOL; } // Process MX record if ($record['type'] == 'MX') { // show the preference value next to the type $record['type'] = "{$record['type']} ({$record['mx_preference']})"; list($status, $rows, $mx) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$mx['name']}</a>.<a title="View domain. ID: {$mx['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$mx['domain_id']}\\', \\'display\\')');" >{$mx['domain_fqdn']}</a>. EOL; } // Process SRV record if ($record['type'] == 'SRV') { // show the preference value next to the type $record['type'] = "{$record['type']} ({$record['srv_port']})"; list($status, $rows, $srv) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$srv['name']}</a>.<a title="View domain. ID: {$srv['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$srv['domain_id']}\\', \\'display\\')');" >{$srv['domain_fqdn']}</a>. EOL; } // Process TXT record if ($record['type'] == 'TXT') { // some records will have an interfaceid and dnsid when associated to another dns name // some will just be un associated txt records or domain only records. Determine that here and // display appropriately. This is to ensure associated DNS records match up if the name changes if ($record['interface_id'] and $record['dns_id']) { list($status, $rows, $txtmain) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $record['name'] = $txtmain['name'] . '.'; } $data = truncate($record['txt'], 70); } // Get the domain name and domain ttl $ttl_style = 'title="Time-to-Live"'; list($status, $rows, $domain) = ona_get_domain_record(array('id' => $record['domain_id'])); // Make record['domain'] have the right name in it if ($record['type'] != 'PTR') { $record['domain'] = $domain['fqdn']; } // clear out the $record['domain'] value so it shows properly in the list for NS records if ($record['type'] == 'NS') { $record['domain'] = $domain['fqdn']; } // if the ttl is blank, use the one in the domain (minimum) if ($record['ttl'] == 0) { $record['ttl'] = $domain['default_ttl']; $ttl_style = 'style="font-style: italic;" title="Using TTL from domain"'; } // format the ebegin using the configured date format $ebegin = ''; // If it is in the future, print the time if (strtotime($record['ebegin']) > time()) { $ebegin = '<span title="Active in DNS on: ' . $record['ebegin'] . '">' . date($conf['date_format'], strtotime($record['ebegin'])) . '</span>'; } // If it is 0 then show as disabled if (strtotime($record['ebegin']) < 0) { $ebegin = <<<EOL <span style="background-color:#FFFF99;" title="Disabled: Won't build in DNS" onClick="var doit=confirm('Are you sure you want to enable this DNS record?'); if (doit == true) xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'enablerecord');" >Disabled</span> EOL; } // If we get this far and the name we have built has a leading . in it then remove the dot. $record['name'] = preg_replace("/^\\./", '', $record['name']); // Get the name of the view and the description if ($conf['dns_views']) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('id' => $record['dns_view_id'])); $record['view_name'] = $dnsview['name']; $record['view_desc'] = $dnsview['description']; } // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } //$primary_object_js = "xajax_window_submit('work_space', 'xajax_window_submit(\'display_host\', \'host_id=>{$record['id']}\', \'display\')');"; $html .= <<<EOL <tr onMouseOver="this.className='row-highlight';" onMouseOut="this.className='row-normal';"> <td class="list-row" style="padding-right: 2px; padding-left: 4px;" width="16px"> {$primary_record} </td> <td class="list-row"> <span title="Record. ID: {$record['id']}" onClick="" >{$record['name']}</span ><a title="View domain. ID: {$domain['id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$domain['id']}\\', \\'display\\')');" >{$record['domain']}.</a> </td> <td class="list-row"> <span onClick="" {$ttl_style} >{$record['ttl']} seconds</span> </td> <td class="list-row"> <span title="Record Type" onClick="" >{$record['type']}</span> </td> <td class="list-row" align="left"> EOL; // Put the data in! $html .= $data; $html .= <<<EOL </td> <td class="list-row" align="center"> {$ebegin} </td> EOL; // Display the view we are part of if ($conf['dns_views']) { $html .= <<<EOL <td class="list-row" align="center" title="{$record['view_desc']}"> {$record['view_name']} </td> EOL; } $html .= <<<EOL <td class="list-row"> <span title="{$record['notes']}">{$record['notes_short']}</span> </td> <!-- ACTION ICONS --> <td class="list-row" align="right"> <form id="{$form['form_id']}_list_record_{$record['id']}" ><input type="hidden" name="dns_record_id" value="{$record['id']}" ><input type="hidden" name="host_id" value="{$host['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('dns_record_modify')) { // If it is an A record but not the primary, display an option to make it primary. and only if we are dealing with a specific host if (($record['type'] == 'A' or $record['type'] == 'AAAA') and $host['primary_dns_id'] != $record['id'] and $form['host_id']) { $html .= <<<EOL <a title="Make this the primary DNS record" class="act" onClick="var doit=confirm('Are you sure you want to make this the primary DNS record for this host?'); if (doit == true) xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'makeprimary');" ><img src="{$images}/silk/font_go.png" border="0"></a> EOL; } } // display a view host button on the dns record search form list if ($form['search_form_id'] == 'dns_record_search_form') { $html .= <<<EOL <a title="View associated host record: {$interface['host_id']}" class="act" onClick="xajax_window_submit('display_host', 'host_id=>{$interface['host_id']}', 'display');" ><img src="{$images}/silk/computer_go.png" border="0"></a> EOL; } if (auth('dns_record_modify')) { $html .= <<<EOL <a title="Edit DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('dns_record_del')) { $html .= <<<EOL <a title="Delete DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL </td> </tr> EOL; // reset the record counter before we go back for the next iteration $last_record = array('name' => $record['name'], 'domain_id' => $record['domain_id']); $last_record_count = 1; } $html .= <<<EOL </table> EOL; // Build page links if there are any $html .= get_page_links($page, $conf['search_results_per_page'], $count, $window_name, $form['form_id']); // Insert the new html into the content div specified // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("{$form['form_id']}_{$tab}_count", "innerHTML", "({$count})"); $response->addAssign($form['content_id'], "innerHTML", $html); if ($js) { $response->addScript($js); } return $response->getXML(); }
function dns_record_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => dns_record_display({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[verbose] (default is yes) $options['verbose'] = sanitize_YN($options['verbose'], 'Y'); // Return the usage summary if we need to if ($options['help'] or !$options['name']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dns_record_display-v{$version} Displays a DNS record from the database Synopsis: dns_record_display [KEY=VALUE] ... Required: name=NAME[.DOMAIN] or ID hostname or ID of the dns record to display Optional: verbose=[yes|no] display additional info (yes) EOM ); } // If the name we were passed has a leading . in it then remove the dot. $options['name'] = preg_replace("/^\\./", '', $options['name']); // Find the DNS record from $options['name'] list($status, $rows, $record) = ona_find_dns_record($options['name']); printmsg("DEBUG => dns_record_del() DNS record: {$record['name']}", 3); if (!$record['id']) { printmsg("DEBUG => Unknown DNS record: {$options['name']}", 3); $self['error'] = "ERROR => Unknown DNS record: {$options['name']}"; return array(2, $self['error'] . "\n"); } // Build text to return $text = "DNS {$record['type']} RECORD ({$record['fqdn']})\n"; $text .= format_array($record); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // PTR record(s) $i = 0; do { list($status, $rows, $ptr) = ona_get_dns_record(array('dns_id' => $record['id'], 'type' => 'PTR')); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED PTR RECORD ({$i} of {$rows})\n"; $text .= format_array($ptr); } while ($i < $rows); // CNAME record(s) $i = 0; do { list($status, $rows, $cname) = ona_get_dns_record(array('dns_id' => $record['id'], 'type' => 'CNAME')); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED CNAME RECORD ({$i} of {$rows})\n"; $text .= format_array($cname); } while ($i < $rows); // FIXME: MP display other types of records like NS,MX,SRV etc etc, also support dns views better } // Return the success notice return array(0, $text); }
function ws_display_list($window_name, $form) { global $conf, $self, $mysql, $onadb; global $font_family, $color, $style, $images; // Check permissions // if (!auth('advanced')) { // $response = new xajaxResponse(); // $response->addScript("alert('Permission denied!');"); // return($response->getXML()); // } // If the user supplied an array in a string, build the array and store it in $form $form = parse_options_string($form); // Build js to refresh this list $refresh = "xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'display_list');"; // Find out what page we're on $page = 1; if ($form['page'] and is_numeric($form['page'])) { $page = $form['page']; } $html = <<<EOL <!-- Results Table --> <table cellspacing="0" border="0" cellpadding="0" width="100%" class="list-box"> <!-- Table Header --> <tr> <td class="list-header" align="center" style="{$style['borderR']};">Server name</td> <td class="list-header" align="center" style="{$style['borderR']};">Domain count</td> <td class="list-header" align="center"> </td> </tr> EOL; $where = 'id in (select host_id from dns_server_domains group by host_id)'; if (is_array($form) and $form['filter']) { //$where = 'name like ' . $onadb->qstr('%'.$form['filter'].'%'); } // Offset for SQL query $offset = $conf['search_results_per_page'] * ($page - 1); if ($offset == 0) { $offset = -1; } // Get list of elements list($status, $rows, $records) = db_get_records($onadb, 'hosts', $where, '', $conf['search_results_per_page'], $offset); //$records = array_unique(array_slice($dnsservers,1,1)); // If we got less than search_results_per_page, add the current offset to it // so that if we're on the last page $rows still has the right number in it. if ($rows > 0 and $rows < $conf['search_results_per_page']) { $rows += $conf['search_results_per_page'] * ($page - 1); } else { if ($rows >= $conf['search_results_per_page']) { list($status, $rows, $tmp) = db_get_records($onadb, 'hosts', $where, '', 0); } } $count = $rows; // Loop through and display foreach ($records as $record) { list($status, $rows, $dns) = ona_get_dns_record(array('id' => $record['primary_dns_id'])); $record['fqdn'] = $dns['fqdn']; // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } list($status, $usage_rows, $tmp) = db_get_records($onadb, 'dns_server_domains', "host_id = {$record['id']}", '', 0); $html .= <<<EOL <tr onMouseOver="this.className='row-highlight'" onMouseOut="this.className='row-normal'"> <form id="{$form['form_id']}_list_domain_{$record['id']}" ><input type="hidden" name="id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <td class="list-row"> <a title="View DNS server. ID: {$record['id']}" class="act" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain_server\\', \\'host_id=>{$record['id']}\\', \\'display\\')');" >{$record['fqdn']}</a> </td> <td class="list-row"> {$usage_rows} </td> <td align="right" class="list-row" nowrap="true"> <a title="View DNS server. ID: {$record['id']}" class="act" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain_server\\', \\'host_id=>{$record['id']}\\', \\'display\\')');" ><img src="{$images}/silk/zoom.png" border="0"></a> </td> </tr> EOL; } $html .= <<<EOL </table> <!-- Add a new record --> <div class="act-box" style="padding: 2px 4px; border-top: 1px solid {$color['border']}; border-bottom: 1px solid {$color['border']};"> <form id="{$form['form_id']}_add_domain_{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <!-- ADD domain LINK --> <a title="New DNS domain" class="act" onClick="xajax_window_submit('edit_domain', xajax.getFormValues('{$form['form_id']}_add_domain_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_add.png" border="0"></a> <a title="New DNS domain" class="act" onClick="xajax_window_submit('edit_domain', xajax.getFormValues('{$form['form_id']}_add_domain_{$record['id']}'), 'editor');" >Add DNS domain</a> <!-- ADD server LINK --> <a title="New DNS server" class="act" onClick="xajax_window_submit('edit_domain_server', xajax.getFormValues('{$form['form_id']}_add_domain_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_add.png" border="0"></a> <a title="New DNS server" class="act" onClick="xajax_window_submit('edit_domain_server', xajax.getFormValues('{$form['form_id']}_add_domain_{$record['id']}'), 'editor');" >Add DNS server</a> </div> EOL; // Build page links if there are any $html .= get_page_links($page, $conf['search_results_per_page'], $count, $window_name, $form['form_id']); // Insert the new table into the window // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("{$form['form_id']}_domain_count", "innerHTML", "({$count})"); $response->addAssign("{$form['content_id']}", "innerHTML", $html); // $response->addScript($js); return $response->getXML(); }
function rpt_get_data($form) { global $base, $onadb; // If they want to perform a scan on an existing file if ($form['subnet']) { $rptdata['scansource'] = "Based on an existing scan file for '{$form['subnet']}'"; //$xml = shell_exec("{$nmapcommand} -sP -R -oX - {$form['subnet']}"); list($status, $rows, $subnet) = ona_find_subnet($form['subnet']); if ($rows) { $netip = ip_mangle($subnet['ip_addr'], 'dotted'); $netcidr = ip_mangle($subnet['ip_mask'], 'cidr'); $nmapxmlfile = "{$base}/local/nmap_scans/subnets/{$netip}-{$netcidr}.xml"; if (file_exists($nmapxmlfile)) { $xml[0] = xml2ary(file_get_contents($nmapxmlfile)); } else { $self['error'] = "ERROR => The subnet '{$form['subnet']}' does not have an nmap scan XML file on this server. {$nmapxmlfile}"; return array(2, $self['error'] . "\n"); } } else { $self['error'] = "ERROR => The subnet '{$form['subnet']}' does not exist."; return array(2, $self['error'] . "\n"); } } // If they want to build a report on ALL the nmap data if ($form['all']) { $rptdata['scansource'] = "Showing all scan data"; $nmapdir = "{$base}/local/nmap_scans/subnets"; $dh = @opendir($nmapdir); $c = 0; while (false !== ($filename = @readdir($dh))) { if (strpos($filename, 'xml')) { $xml[$c] = xml2ary(file_get_contents($nmapdir . '/' . $filename)); } $c++; } } // If they pass a file from the remote host via CLI if ($form['file']) { $rptdata['scansource'] = "Based on an uploaded XML file"; $nmapxmlfile = $form['file']; // clean up escaped characters $nmapxmlfile = preg_replace('/\\\\"/', '"', $nmapxmlfile); $nmapxmlfile = preg_replace('/\\\\=/', '=', $nmapxmlfile); $nmapxmlfile = preg_replace('/\\\\&/', '&', $nmapxmlfile); $xml[0] = xml2ary($nmapxmlfile); } // loop through all the xml arrays that have been built. for ($z = 0; $z < count($xml); $z++) { // Find out how many total hosts we have in the array $rptdata['totalhosts'] = $xml[$z]['nmaprun']['_c']['runstats']['_c']['hosts']['_a']['total']; $rptdata['runtime'] = $xml[$z]['nmaprun']['_c']['runstats']['_c']['finished']['_a']['timestr']; // pull args to find subnet/cidr $rptdata['args'] = $xml[$z]['nmaprun']['_a']['args']; // process args list($subnetaddr, $netcidr) = explode('/', preg_replace("/.* (.*)\\/(\\d+)\$/", "\\1/\\2", $rptdata['args'])); $netip = ip_mangle($subnetaddr, 'dotted'); $netcidr = ip_mangle($netcidr, 'cidr'); // Process the array for the total amount of hosts reported for ($i = 0; $i < $rptdata['totalhosts']; $i++) { // Clear MAC each itteration of the loop $macaddr = ''; // Gather some info from the nmap XML file $netstatus = $xml[$z]['nmaprun']['_c']['host'][$i]['_c']['status']['_a']['state']; $ipaddr = $xml[$z]['nmaprun']['_c']['host'][$i]['_c']['address']['_a']['addr']; //$macaddr = $xml['nmaprun']['_c']['host'][$i]['_c']['address']['_a']['addr']; $dnsname = $xml[$z]['nmaprun']['_c']['host'][$i]['_c']['hostnames']['_c']['hostname']['_a']['name']; $dnsrows = 0; $dns = array(); // Try the older nmap format if no IP found.. not sure of what differences there are in the XSL used? if (!$ipaddr) { $ipaddr = $xml[$z]['nmaprun']['_c']['host'][$i]['_c']['address']['0']['_a']['addr']; $macaddr = $xml[$z]['nmaprun']['_c']['host'][$i]['_c']['address']['1']['_a']['addr']; } // Lookup the IP address in the database if ($ipaddr) { list($status, $introws, $interface) = ona_find_interface($ipaddr); if (!$introws) { $interface['ip_addr_text'] = 'NOT FOUND'; list($status, $introws, $tmp) = ona_find_subnet($ipaddr); $interface['subnet_id'] = $tmp['id']; } else { // Lookup the DNS name in the database list($status, $dnsrows, $dnscount) = db_get_records($onadb, 'dns', "interface_id = {$interface['id']}", "", 0); list($status, $dnsptrrows, $dnsptr) = ona_get_dns_record(array('interface_id' => $interface['id'], 'type' => 'PTR')); list($status, $dnsprows, $dns) = ona_get_dns_record(array('id' => $dnsptr['dns_id'])); } } // Find out if this IP falls inside of a pool $inpool = 0; $ip = ip_mangle($ipaddr, 'numeric'); if ($ip > 0) { list($status, $poolrows, $pool) = ona_get_dhcp_pool_record("ip_addr_start <= '{$ip}' AND ip_addr_end >= '{$ip}'"); } if ($poolrows) { $inpool = 1; } // some base logic // if host is up in nmap but no db ip then put in $nodb // if host is up and is in db then put in $noissue // if host is down and not in db then skip // if host is down and in db then put in $nonet // if host is up an in db, does DNS match? // in DNS but not DB // in DB but not DNS // DNS and DB dont match // Setup the base array element for the IP $rptdata['ip'][$ipaddr] = array(); $rptdata['ip'][$ipaddr]['netstatus'] = $netstatus; $rptdata['ip'][$ipaddr]['netip'] = $ipaddr; $rptdata['ip'][$ipaddr]['netdnsname'] = strtolower($dnsname); if ($macaddr != -1) { $rptdata['ip'][$ipaddr]['netmacaddr'] = $macaddr; } $rptdata['ip'][$ipaddr]['inpool'] = $inpool; $rptdata['ip'][$ipaddr]['dbip'] = $interface['ip_addr_text']; $rptdata['ip'][$ipaddr]['dbsubnetid'] = $interface['subnet_id']; $rptdata['ip'][$ipaddr]['dbdnsrows'] = $dnsrows; if (!$dns['fqdn']) { // lets see if its a PTR record if ($dnsptrrows) { // If we have a PTR for this interface, use it (never if built from ona?) $rptdata['ip'][$ipaddr]['dbdnsname'] = $dns['fqdn']; $rptdata['ip'][$ipaddr]['dbdnsptrname'] = $dnsp['fqdn']; } else { // find the hosts primary DNS record list($status, $hostrows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); if ($host['fqdn']) { $host['fqdn'] = "({$host['fqdn']})"; } if ($dnsrows) { list($status, $dnstmprows, $dnstmp) = ona_get_dns_record(array('interface_id' => $interface['id'])); $rptdata['ip'][$ipaddr]['dbdnsname'] = $dnstmp['fqdn']; } else { $rptdata['ip'][$ipaddr]['dbdnsname'] = 'NO PTR'; } $rptdata['ip'][$ipaddr]['dbdnsptrname'] = $host['fqdn']; } } else { if ($dnsptrrows > 1) { $rptdata['ip'][$ipaddr]['dbdnsname'] = $dns['fqdn']; $rptdata['ip'][$ipaddr]['dbdnsptrname'] = $dnsp['fqdn']; } else { $rptdata['ip'][$ipaddr]['dbdnsname'] = $dns['fqdn']; $rptdata['ip'][$ipaddr]['dbdnsptrname'] = $dnsp['fqdn']; } } $rptdata['ip'][$ipaddr]['dbmacaddr'] = $interface['mac_addr']; $rptdata['netip'] = $netip; $rptdata['netcidr'] = $netcidr; if ($form['all']) { $rptdata['all'] = 1; } if ($form['update_response']) { $rptdata['update_response'] = 1; } } } return array(0, $rptdata); }
function host_add($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.11'; printmsg("DEBUG => host_add({$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['host'] and $options['type'] and $options['ip'])) { // 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_add-v{$version} Add a new host Synopsis: host_add [KEY=VALUE] ... Required: host=NAME[.DOMAIN] Hostname for new DNS record type=TYPE or ID Device/model type or ID ip=ADDRESS IP address (numeric or dotted) Optional: notes=NOTES Textual notes location=REF Reference of location device=NAME|ID The device this host is associated with Optional, add an interface too: mac=ADDRESS Mac address (most formats are ok) name=NAME Interface name (i.e. "FastEthernet0/1.100") description=TEXT Brief description of the interface addptr=Y|N Auto add a PTR record for new host/IP (default: Y) EOM ); } // Sanitize addptr.. set it to Y if it is not set $options['addptr'] = sanitize_YN($options['addptr'], 'Y'); // clean up what is passed in $options['ip'] = trim($options['ip']); $options['mac'] = trim($options['mac']); $options['name'] = trim($options['name']); $options['host'] = trim($options['host']); // Validate that there isn't already another interface with the same IP address list($status, $rows, $interface) = ona_get_interface_record(array('ip_addr' => $options['ip'])); if ($rows) { printmsg("DEBUG => host_add() IP conflict: That IP address (" . ip_mangle($orig_ip, 'dotted') . ") is already in use!", 3); $self['error'] = "ERROR => host_add() IP conflict: That IP address (" . ip_mangle($orig_ip, 'dotted') . ") is already in use!"; return array(4, $self['error'] . "\n" . "INFO => Conflicting interface record ID: {$interface['id']}\n"); } // Find the Location ID to use if ($options['location']) { list($status, $rows, $loc) = ona_find_location($options['location']); if ($status or !$rows) { printmsg("DEBUG => The location specified, {$options['location']}, does not exist!", 3); $self['error'] = "ERROR => The location specified, {$options['location']}, does not exist!"; return array(2, "{$self['error']}\n"); } printmsg("DEBUG => Location selected: {$loc['reference']}, location name: {$loc['name']}", 3); } else { $loc['id'] = 0; } // Find the Device Type ID (i.e. Type) to use list($status, $rows, $device_type) = ona_find_device_type($options['type']); if ($status or $rows != 1 or !$device_type['id']) { printmsg("DEBUG => The device type specified, {$options['type']}, does not exist!", 3); return array(3, "ERROR => The device type specified, {$options['type']}, does not exist!\n"); } printmsg("DEBUG => Device type selected: {$device_type['model_description']} Device ID: {$device_type['id']}", 3); // Sanitize "security_level" option $options['security_level'] = sanitize_security_level($options['security_level']); if ($options['security_level'] == -1) { printmsg("DEBUG => Sanitize security level failed either ({$options['security_level']}) is invalid or is higher than user's level!", 3); return array(3, $self['error'] . "\n"); } // Determine the real hostname to be used -- // i.e. add .something.com, or find the part of the name provided // that will be used as the "domain". This means testing many // domain names against the DB to see what's valid. // // Find the domain name piece of $search. // If we are specifically passing in a domain, use its value. If we dont have a domain // then try to find it in the name that we are setting. if ($options['domain']) { // Find the domain name piece of $search list($status, $rows, $domain) = ona_find_domain($options['domain'], 0); } else { list($status, $rows, $domain) = ona_find_domain($options['host'], 0); } if (!isset($domain['id'])) { printmsg("ERROR => Unable to determine domain name portion of ({$options['host']})!", 3); $self['error'] = "ERROR => Unable to determine domain name portion of ({$options['host']})!"; return array(3, $self['error'] . "\n"); } printmsg("DEBUG => ona_find_domain({$options['host']}) returned: {$domain['fqdn']}", 3); // Now find what the host part of $search is $hostname = str_replace(".{$domain['fqdn']}", '', $options['host']); // Validate that the DNS name has only valid characters in it $hostname = sanitize_hostname($hostname); if (!$hostname) { printmsg("ERROR => Invalid host name ({$options['host']})!", 3); $self['error'] = "ERROR => Invalid host name ({$options['host']})!"; return array(4, $self['error'] . "\n"); } // Debugging printmsg("DEBUG => Using hostname: {$hostname} Domainname: {$domain['fqdn']}, Domain ID: {$domain['id']}", 3); // Validate that there isn't already any dns record named $host['name'] in the domain $host_domain_id. $h_status = $h_rows = 0; // does the domain $host_domain_id even exist? list($d_status, $d_rows, $d_record) = ona_get_dns_record(array('name' => $hostname, 'domain_id' => $domain['id'])); if ($d_status or $d_rows) { printmsg("DEBUG => The name {$hostname}.{$domain['fqdn']} is already in use, the primary name for a host should be unique!", 3); $self['error'] = "ERROR => Another DNS record named {$hostname}.{$domain['fqdn']} is already in use, the primary name for a host should be unique!"; return array(5, $self['error'] . "\n"); } // Check permissions if (!auth('host_add')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Get the next ID for the new host record $id = ona_get_next_id('hosts'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id('hosts') call failed!"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } printmsg("DEBUG => ID for new host record: {$id}", 3); // Get the next ID for the new device record or use the one passed in the CLI if (!$options['device']) { $host['device_id'] = ona_get_next_id('devices'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id('device') call failed!"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } printmsg("DEBUG => ID for new device record: {$id}", 3); } else { list($status, $rows, $devid) = ona_find_device($options['device']); if (!$rows) { printmsg("DEBUG => The device specified, {$options['device']}, does not exist!", 3); $self['error'] = "ERROR => The device specified, {$options['device']}, does not exist!"; return array(7, $self['error'] . "\n"); } $host['device_id'] = $devid['id']; } // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters $options['notes'] = str_replace('\\=', '=', $options['notes']); $options['notes'] = str_replace('\\&', '&', $options['notes']); // Add the device record // FIXME: (MP) quick add of device record. more detail should be looked at here to ensure it is done right // FIXME: MP this should use the run_module('device_add')!!! when it is ready list($status, $rows) = db_insert_record($onadb, 'devices', array('id' => $host['device_id'], 'device_type_id' => $device_type['id'], 'location_id' => $loc['id'], 'primary_host_id' => $id)); if ($status or !$rows) { $self['error'] = "ERROR => host_add() SQL Query failed adding device: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Add the host record // FIXME: (PK) Needs to insert to multiple tables for e.g. name and domain_id. list($status, $rows) = db_insert_record($onadb, 'hosts', array('id' => $id, 'primary_dns_id' => '', 'device_id' => $host['device_id'], 'notes' => $options['notes'])); if ($status or !$rows) { $self['error'] = "ERROR => host_add() SQL Query failed adding host: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Else start an output message $text = "INFO => Host ADDED: {$hostname}.{$domain['fqdn']}"; printmsg($text, 0); $text .= "\n"; // We must always have an IP now to add an interface, call that module now: // since we have no name yet, we need to use the ID of the new host as the host option for the following module calls $options['host'] = $id; // for annoying reasons we need to keep track of what was set first $options['addptrsave'] = $options['addptr']; // Interface adds can add PTR records, lets let the A record add that happens next add it instead. $options['addptr'] = '0'; printmsg("DEBUG => host_add() ({$hostname}.{$domain['fqdn']}) calling interface_add() ({$options['ip']})", 3); list($status, $output) = run_module('interface_add', $options); if ($status) { return array($status, $output); } $text .= $output; // Find the interface_id for the interface we just added list($status, $rows, $int) = ona_find_interface($options['ip']); // make the dns record type A $options['type'] = 'A'; // FIXME: MP I had to force the name value here. name is comming in as the interface name. this is nasty! $options['name'] = "{$hostname}.{$domain['fqdn']}"; $options['domain'] = $domain['fqdn']; // And we will go ahead and auto add the ptr. the user can remove it later if they dont want it. FIXME: maybe create a checkbox on the host edit $options['addptr'] = $options['addptrsave']; // Add the DNS entry with the IP address etc printmsg("DEBUG => host_add() ({$hostname}.{$domain['fqdn']}) calling dns_record_add() ({$options['ip']})", 3); list($status, $output) = run_module('dns_record_add', $options); if ($status) { return array($status, $output); } $text .= $output; // find the dns record we just added so we can use its ID as the primary_dns_id for the host. list($status, $rows, $dnsrecord) = ona_get_dns_record(array('name' => $hostname, 'domain_id' => $domain['id'], 'interface_id' => $int['id'], 'type' => 'A')); // Set the primary_dns_id to the dns record that was just added list($status, $rows) = db_update_record($onadb, 'hosts', array('id' => $id), array('primary_dns_id' => $dnsrecord['id'])); if ($status or !$rows) { $self['error'] = "ERROR => host_add() SQL Query failed to update primary_dns_id for host: " . $self['error']; printmsg($self['error'], 0); return array(8, $self['error'] . "\n"); } return array(0, $text); }
function build_bind_domain($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.50'; printmsg("DEBUG => build_bind_domain({$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['domain']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOF build_bind_domain-v{$version} Builds a zone file for a dns server from the database Synopsis: build_bind_domain [KEY=VALUE] ... Required: domain=DOMAIN or ID build zone file for specified domain EOF ); } // Get the domain information list($status, $rows, $domain) = ona_find_domain($options['domain']); printmsg("DEBUG => build_bind_domain() Domain record: {$domain['domain']}", 3); if (!$domain['id']) { printmsg("DEBUG => Unknown domain record: {$options['domain']}", 3); $self['error'] = "ERROR => Unknown domain record: {$options['domain']}"; return array(2, $self['error'] . "\n"); } // if for some reason the domains default_ttl is not set, use the one from the $conf['dns']['default_ttl'] if ($domain['default_ttl'] == 0) { $domain['default_ttl'] = $conf['dns']['default_ttl']; } if ($domain['primary_master'] == '') { $domain['primary_master'] = 'localhost'; } // loop through records and display them $q = "\n SELECT *\n FROM dns\n WHERE domain_id = {$domain['id']}\n ORDER BY type"; // exectue the query $rs = $onadb->Execute($q); if ($rs === false or !$rs->RecordCount()) { $self['error'] = 'ERROR => build_zone(): SQL query failed: ' . $onadb->ErrorMsg(); printmsg($self['error'], 0); $exit += 1; } $rows = $rs->RecordCount(); // check if this is a ptr domain that has delegation if (strpos(str_replace('in-addr.arpa', '', $domain['fqdn']), '-')) { $ptrdelegation = true; } // Start building the named.conf - save it in $text $text = "; DNS zone file for {$domain['fqdn']} built on " . date($conf['date_format']) . "\n"; // print the opening host comment with row count $text .= "; TOTAL RECORDS (count={$rows})\n\n"; // FIXME: MP do more to ensure that dots are at the end as appropriate $text .= "\$ORIGIN {$domain['fqdn']}.\n"; $text .= "\$TTL {$domain['default_ttl']}\n"; $text .= ";Serial number is current unix timestamp (seconds since UTC)\n\n"; // NOTE: There are various ways that one could generate the serial. The bind book suggests YYYYMMDDXX where XX is 1/100th of the day or some counter in the day. // I feel this is too limiting. I prefer the Unix timestamp (seconds since UTC) method. TinyDNS uses this method as well and it allows for much more granularity. // Referr to the following for some discussion on the topic: http://www.lifewithdjbdns.com/#Migration // NOTE: for now I am generating the serial each time the zone is built. I'm ignoring, and may remove, the one stored in the database. $serial_number = time(); // Build the SOA record // FIXME: MP do a bit more to ensure that dots are where they should be $text .= "@ IN SOA {$domain['primary_master']}. {$domain['admin_email']} ({$serial_number} {$domain['refresh']} {$domain['retry']} {$domain['expiry']} {$domain['minimum']})\n\n"; // Loop through the record set while ($dnsrecord = $rs->FetchRow()) { // Dont build records that begin in the future if (strtotime($dnsrecord['ebegin']) > time()) { continue; } if (strtotime($dnsrecord['ebegin']) < 0) { continue; } // If there are notes, put the comment character in front of it if ($dnsrecord['notes']) { $dnsrecord['notes'] = '; ' . str_replace("\n", "; ", $dnsrecord['notes']); } // If the ttl is empty then make it truely empty if ($dnsrecord['ttl'] == 0) { $dnsrecord['ttl'] = ''; } // Also, if the records ttl is the same as the domains ttl then dont display it, just to keep it "cleaner" if (!strcmp($dnsrecord['ttl'], $domain['default_ttl'])) { $dnsrecord['ttl'] = ''; } // Dont print a dot unless hostname has a value if ($dnsrecord['name']) { $dnsrecord['name'] = $dnsrecord['name'] . '.'; } if ($dnsrecord['type'] == 'A') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], $interface['ip_addr_text'], $dnsrecord['notes']); } if ($dnsrecord['type'] == 'PTR') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $ptr) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); // If this is a delegation domain, find the subnet cidr if ($ptrdelegation) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $ip_last = ip_mangle($interface['ip_addr'], 'flip'); $ip_last_digit = substr($ip_last, 0, strpos($ip_last, '.')); $ip_remainder = substr($ip_last, strpos($ip_last, '.')) . '.in-addr.arpa.'; $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $ip_last_digit . '-' . ip_mangle($subnet['ip_mask'], 'cidr') . $ip_remainder, $dnsrecord['ttl'], $dnsrecord['type'], $ptr['name'], $ptr['domain_fqdn'] . '.', $dnsrecord['notes']); } else { $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", ip_mangle($interface['ip_addr'], 'flip') . '.in-addr.arpa.', $dnsrecord['ttl'], $dnsrecord['type'], $ptr['name'], $ptr['domain_fqdn'] . '.', $dnsrecord['notes']); } } if ($dnsrecord['type'] == 'CNAME') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $cname) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], $cname['name'], $cname['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'NS') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $ns) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $domain['fqdn'] . '.', $dnsrecord['ttl'], $dnsrecord['type'], $ns['name'], $ns['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'MX') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $mx) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); if ($dnsrecord['name']) { $name = $dnsrecord['name'] . $domain['fqdn']; } else { $name = $domain['name']; } $text .= sprintf("%-50s %-8s IN %s %-5s %s.%-30s %s\n", $name . '.', $dnsrecord['ttl'], $dnsrecord['type'], $dnsrecord['mx_preference'], $mx['name'], $mx['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'SRV') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $srv) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); if ($dnsrecord['name']) { $name = $dnsrecord['name'] . $domain['fqdn']; } else { $name = $domain['name']; } $text .= sprintf("%-50s %-8s IN %s %s %s %-8s %-30s %s\n", $name . '.', $dnsrecord['ttl'], $dnsrecord['type'], $dnsrecord['srv_pri'], $dnsrecord['srv_weight'], $dnsrecord['srv_port'], $srv['fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'TXT') { $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], '"' . $dnsrecord['txt'] . '"', $dnsrecord['notes']); } } ////////////// Footer stuff ////////////////// // MP: FIXME: For now I"m not using this.. bind errors out if the file doesnt exist. need a deterministic way to do this. // Allow for a local footer include.. I expect this to rarely be used // $text .= "\n; Allow for a local footer include.. I expect this to rarely be used.\n"; // $text .= "\$INCLUDE named-{$domain['fqdn']}-footer\n"; // Return the zone file return array(0, $text); }
function ws_display_list($window_name, $form = '') { global $conf, $self, $onadb; global $images, $color, $style; $html = ''; $js = ''; // If the user supplied an array in a string, transform it into an array $form = parse_options_string($form); // Find the "tab" we're on $tab = $_SESSION['ona'][$form['form_id']]['tab']; // Build js to refresh this list $refresh = "xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'display_list');"; // If it's not a new query, load the previous query from the session // into $form and save the current page and filter in the session. // Also find/set the "page" we're viewing $page = 1; if ($form['page'] and is_numeric($form['page'])) { $form = array_merge($form, (array) $_SESSION['ona'][$form['form_id']][$tab]['q']); $_SESSION['ona'][$form['form_id']][$tab]['page'] = $page = $form['page']; $_SESSION['ona'][$form['form_id']][$tab]['filter'] = $form['filter']; } printmsg("DEBUG => Displaying hosts list page: {$page}", 1); // Calculate the SQL query offset (based on the page being displayed) $offset = $conf['search_results_per_page'] * ($page - 1); if ($offset == 0) { $offset = -1; } // Search results go in here $results = array(); $count = 0; // // *** ADVANCED HOST SEARCH *** // FIND RESULT SET // // Start building the "where" clause for the sql query to find the hosts to display $where = ""; $and = ""; $orderby = ""; $from = 'hosts h'; // enable or disable wildcards $wildcard = '%'; if ($form['nowildcard']) { $wildcard = ''; } // DISPLAY ALL // MP: I dont think this is used.. remove it if you can if ($form['all_flag']) { $where .= $and . "h.id > 0"; $and = " AND "; } // HOST ID if ($form['host_id']) { $where .= $and . "h.id = " . $onadb->qstr($form['host_id']); $and = " AND "; } // DEVICE ID if ($form['device_id']) { $where .= $and . "h.device_id = " . $onadb->qstr($form['device_id']); $and = " AND "; } // HOSTNAME if ($form['hostname']) { // Find the domain name piece of the hostname assuming it was passed in as an fqdn. // FIXME: MP this was taken from the ona_find_domain function. make that function have the option // to NOT return a default domain. // lets test out if it has a / in it to strip the view name portion $view['id'] = 0; if (strstr($form['hostname'], '/')) { list($dnsview, $form['hostname']) = explode('/', $form['hostname']); list($status, $viewrows, $view) = db_get_record($onadb, 'dns_views', array('name' => strtoupper($dnsview))); if (!$viewrows) { $view['id'] = 0; } } // Split it up on '.' and put it in an array backwards $parts = array_reverse(explode('.', $form['hostname'])); // Find the domain name that best matches $name = ''; $domain = array(); foreach ($parts as $part) { if (!$rows) { if (!$name) { $name = $part; } else { $name = "{$part}.{$name}"; } list($status, $rows, $record) = ona_get_domain_record(array('name' => $name)); if ($rows) { $domain = $record; } } else { list($status, $rows, $record) = ona_get_domain_record(array('name' => $part, 'parent_id' => $domain['id'])); if ($rows) { $domain = $record; } } } $withdomain = ''; $hostname = $form['hostname']; // If you found a domain in the query, add it to the search, and strip the domain from the host portion. if (array_key_exists('id', $domain) and !$form['domain']) { $withdomain = "AND b.domain_id = {$domain['id']}"; // Now find what the host part of $search is $hostname = str_replace(".{$domain['fqdn']}", '', $form['hostname']); } // If we have a hostname and a domain name then use them both if ($form['domain']) { list($status, $rows, $record) = ona_find_domain($form['domain']); if ($record['id']) { $withdomain = "AND b.domain_id = {$record['id']}"; } // Now find what the host part of $search is $hostname = trim($form['hostname']); } // MP: Doing the many select IN statements was too slow.. I did this kludge: // 1. get a list of all the interfaces // 2. loop through the array and build a list of comma delimited host_ids to use in the final select list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces a, dns b', "a.id = b.interface_id and b.name LIKE '{$wildcard}{$hostname}{$wildcard}' {$withdomain}"); $commait = ''; $hostids = ''; foreach ($tmp as $item) { $hostids .= $commait . $item['host_id']; $commait = ','; } // Just look for the host itself list($status, $rows, $r) = ona_find_host($form['hostname']); if ($rows) { $hostids .= ',' . $r['id']; } // MP: this is the old, slow query for reference. // // TODO: MP this seems to be kinda slow (gee I wonder why).. look into speeding things up somehow. // This also does not search for CNAME records etc. only things with interface_id.. how to fix that issue.......? // $where .= $and . "id IN (select host_id from interfaces where id in (SELECT interface_id " . // " FROM dns " . // " WHERE name LIKE '%{$hostname}%' {$withdomain} ))"; // Trim off extra commas $hostids = trim($hostids, ","); // If we got a list of hostids from interfaces then use them if ($hostids) { $idqry = "h.id IN ({$hostids})"; } else { $idqry = ""; } $where .= $and . $idqry; $and = " AND "; } // DOMAIN if ($form['domain'] and !$form['hostname']) { // FIXME: does this clause work correctly? printmsg("FIXME: => Does \$form['domain'] work correctly in list_hosts.inc.php?", 2); // Find the domain name piece of the hostname. // FIXME: MP this was taken from the ona_find_domain function. make that function have the option // to NOT return a default domain. // Split it up on '.' and put it in an array backwards $parts = array_reverse(explode('.', $form['domain'])); // Find the domain name that best matches $name = ''; $domain = array(); foreach ($parts as $part) { if (!$rows) { if (!$name) { $name = $part; } else { $name = "{$part}.{$name}"; } list($status, $rows, $record) = ona_get_domain_record(array('name' => $name)); if ($rows) { $domain = $record; } } else { list($status, $rows, $record) = ona_get_domain_record(array('name' => $part, 'parent_id' => $domain['id'])); if ($rows) { $domain = $record; } } } if (array_key_exists('id', $domain)) { // Crappy way of writing the query but it makes it fast. $from = "(\nSELECT distinct a.*\nfrom hosts as a, interfaces as i, dns as d\nwhere a.id = i.host_id\nand i.id = d.interface_id\nand d.domain_id = " . $onadb->qstr($domain['id']) . "\n) h"; $and = " AND "; } } // DOMAIN ID if ($form['domain_id'] and !$form['hostname']) { $where .= $and . "h.primary_dns_id IN ( SELECT id " . " FROM dns " . " WHERE domain_id = " . $onadb->qstr($form['domain_id']) . " ) "; $and = " AND "; } // MAC if ($form['mac']) { // Clean up the mac address $form['mac'] = strtoupper($form['mac']); $form['mac'] = preg_replace('/[^%0-9A-F]/', '', $form['mac']); // We do a sub-select to find interface id's that match $where .= $and . "h.id IN ( SELECT host_id " . " FROM interfaces " . " WHERE mac_addr LIKE " . $onadb->qstr($wildcard . $form['mac'] . $wildcard) . " ) "; $and = " AND "; } // IP ADDRESS $ip = $ip_end = ''; if ($form['ip']) { // Build $ip and $ip_end from $form['ip'] and $form['ip_thru'] $ip = ip_complete($form['ip'], '0'); if ($form['ip_thru']) { $ip_end = ip_complete($form['ip_thru'], '255'); } else { $ip_end = ip_complete($form['ip'], '255'); } // Find out if $ip and $ip_end are valid $ip = ip_mangle($ip, 'numeric'); $ip_end = ip_mangle($ip_end, 'numeric'); if ($ip != -1 and $ip_end != -1) { // We do a sub-select to find interface id's between the specified ranges $where .= $and . "h.id IN ( SELECT host_id " . " FROM interfaces " . " WHERE ip_addr >= " . $onadb->qstr($ip) . " AND ip_addr <= " . $onadb->qstr($ip_end) . " )"; $and = " AND "; } } // NOTES if ($form['notes']) { $where .= $and . "h.notes LIKE " . $onadb->qstr($wildcard . $form['notes'] . $wildcard); $and = " AND "; } // DEVICE MODEL if ($form['model_id']) { $where .= $and . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id = {$form['model_id']}))"; $and = " AND "; } if ($form['model']) { $where .= $and . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id in (select id from models where name like '{$form['model']}')))"; $and = " AND "; } // DEVICE TYPE if ($form['role']) { // Find model_id's that have a device_type_id of $form['role'] list($status, $rows, $records) = db_get_records($onadb, 'roles', array('name' => $form['role'])); // If there were results, add each one to the $where clause if ($rows > 0) { $where .= $and . " ( "; $and = " AND "; $or = ""; foreach ($records as $record) { // Yes this is one freakin nasty query but it works. $where .= $or . "h.device_id in (select id from devices where device_type_id in (select id from device_types where role_id = " . $onadb->qstr($record['id']) . "))"; $or = " OR "; } $where .= " ) "; } } // DEVICE MANUFACTURER if ($form['manufacturer']) { // Find model_id's that have a device_type_id of $form['manufacturer'] if (is_numeric($form['manufacturer'])) { list($status, $rows, $records) = db_get_records($onadb, 'models', array('manufacturer_id' => $form['manufacturer'])); } else { list($status, $rows, $manu) = db_get_record($onadb, 'manufacturers', array('name' => $form['manufacturer'])); list($status, $rows, $records) = db_get_records($onadb, 'models', array('manufacturer_id' => $manu['id'])); } // If there were results, add each one to the $where clause if ($rows > 0) { $where .= $and . " ( "; $and = " AND "; $or = ""; foreach ($records as $record) { // Yes this is one freakin nasty query but it works. $where .= $or . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id = " . $onadb->qstr($record['id']) . "))"; $or = " OR "; } $where .= " ) "; } } // tag if ($form['tag_host']) { $where .= $and . "h.id in (select reference from tags where type like 'host' and name like " . $onadb->qstr($form['tag_host']) . ")"; $and = " AND "; } // custom attribute type if ($form['custom_attribute_type']) { $where .= $and . "h.id in (select table_id_ref from custom_attributes where table_name_ref like 'hosts' and custom_attribute_type_id = (SELECT id FROM custom_attribute_types WHERE name = " . $onadb->qstr($form['custom_attribute_type']) . "))"; $and = " AND "; $cavaluetype = "and custom_attribute_type_id = (SELECT id FROM custom_attribute_types WHERE name = " . $onadb->qstr($form['custom_attribute_type']) . ")"; } // custom attribute value if ($form['ca_value']) { $where .= $and . "h.id in (select table_id_ref from custom_attributes where table_name_ref like 'hosts' {$cavaluetype} and value like " . $onadb->qstr($wildcard . $form['ca_value'] . $wildcard) . ")"; $and = " AND "; } // LOCATION No. if ($form['location']) { list($status, $rows, $loc) = ona_find_location($form['location']); $where .= $and . "h.device_id in (select id from devices where location_id = " . $onadb->qstr($loc['id']) . ")"; $and = " AND "; } // subnet ID if (is_numeric($form['subnet_id'])) { // We do a sub-select to find interface id's that match $from = "(\nSELECT distinct a.*\nfrom hosts as a, interfaces as b\nwhere a.id = b.host_id\nand b.subnet_id = " . $onadb->qstr($form['subnet_id']) . "\norder by b.ip_addr) h"; $and = " AND "; } // display a nice message when we dont find all the records if ($where == '' and $form['content_id'] == 'search_results_list') { $js .= "el('search_results_msg').innerHTML = 'Unable to find hosts matching your query, showing all records';"; } // Wild card .. if $while is still empty, add a 'ID > 0' to it so you see everything. if ($where == '') { $where = 'h.id > 0'; } // Do the SQL Query $filter = ''; if ($form['filter']) { // Host names should always be lower case $form['filter'] = strtolower($form['filter']); // FIXME (MP) for now this uses primary_dns_id, this will NOT find multiple A records or other record types. Find a better way some day $filter = " AND h.primary_dns_id IN (SELECT id " . " FROM dns " . " WHERE name LIKE " . $onadb->qstr('%' . $form['filter'] . '%') . " ) "; } list($status, $rows, $results) = db_get_records($onadb, $from, $where . $filter, $orderby, $conf['search_results_per_page'], $offset); // If we got less than serach_results_per_page, add the current offset to it // so that if we're on the last page $rows still has the right number in it. if ($rows > 0 and $rows < $conf['search_results_per_page']) { $rows += $conf['search_results_per_page'] * ($page - 1); } else { if ($rows >= $conf['search_results_per_page']) { list($status, $rows, $records) = db_get_records($onadb, $from, $where . $filter, "", 0); } } $count = $rows; // // *** BUILD HTML LIST *** // $html .= <<<EOL <!-- Host Results --> <table id="{$form['form_id']}_host_list" class="list-box" cellspacing="0" border="0" cellpadding="0"> <!-- Table Header --> <tr> <td class="list-header" align="center" style="{$style['borderR']};">Name</td> <td class="list-header" align="center" style="{$style['borderR']};">Subnet</td> <td class="list-header" align="center" style="{$style['borderR']};">Interface</td> <td class="list-header" align="center" style="{$style['borderR']};">Device Type</td> <td class="list-header" align="center" style="{$style['borderR']};">Location</td> <td class="list-header" align="center" style="{$style['borderR']};">Notes</td> <td class="list-header" align="center"> </td> </tr> EOL; // Loop and display each record foreach ($results as $record) { // Get additional info about eash host record // If a subnet_id was passed use it as part of the search. Used to display the IP of the subnet you searched if (is_numeric($form['subnet_id'])) { list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id'], 'subnet_id' => $form['subnet_id']), ''); // Count how many rows and assign it back to the interfaces variable list($status, $rows, $records) = db_get_records($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']), "ip_addr", 0); $interfaces = $rows; } else { if (is_numeric($ip)) { list($status, $interfaces, $interface) = db_get_record($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']) . ' AND ip_addr >= ' . $onadb->qstr($ip) . ' AND ip_addr <= ' . $onadb->qstr($ip_end), "ip_addr", 0); // Count how many rows and assign it back to the interfaces variable list($status, $rows, $records) = db_get_records($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']), "ip_addr", 0); $interfaces = $rows; } else { // Interface (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id']), ''); } } // bz: why did someone add this?? You especially want to show hosts with no interfaces so you can fix them! // if (!$interfaces) {$count -1; continue;} // get interface cluster info $clusterhtml = ''; list($status, $intclusterrows, $intcluster) = db_get_records($onadb, 'interface_clusters', "interface_id = {$interface['id']}"); if ($intclusterrows > 0) { $clusterscript = "onMouseOver=\"wwTT(this, event,\n 'id', 'tt_interface_cluster_list_{$record['id']}',\n 'type', 'velcro',\n 'styleClass', 'wwTT_niceTitle',\n 'direction', 'south',\n 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>interface_cluster_list,id=>tt_interface_cluster_list_{$record['id']},interface_id=>{$interface['id']}\\');'\n );\""; $clusterhtml .= <<<EOL <img src="{$images}/silk/sitemap.png" {$clusterscript} /> EOL; } $record['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); $interface_style = ''; if ($interfaces > 1) { $interface_style = 'font-weight: bold;'; } // DNS A record list($status, $rows, $dns) = ona_get_dns_record(array('id' => $record['primary_dns_id'])); $record['name'] = $dns['name']; // Domain Name list($status, $rows, $domain) = ona_get_domain_record(array('id' => $dns['domain_id'])); $record['domain'] = $domain['fqdn']; // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $record['subnet'] = $subnet['name']; $record['ip_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); // Device Description list($status, $rows, $device) = ona_get_device_record(array('id' => $record['device_id'])); list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $record['devicefull'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; $record['device'] = str_replace('Unknown', '?', $record['devicefull']); $record['notes_short'] = truncate($record['notes'], 40); // Get location_number from the location_id list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id'])); // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } $primary_object_js = "xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$record['id']}\\', \\'display\\')');"; $html .= <<<EOL <tr onMouseOver="this.className='row-highlight';" onMouseOut="this.className='row-normal';"> <td class="list-row"> <a title="View host. ID: {$record['id']}" class="nav" onClick="{$primary_object_js}" >{$record['name']}</a >.<a title="View domain. ID: {$domain['id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$domain['id']}\\', \\'display\\')');" >{$record['domain']}</a> </td> <td class="list-row"> <a title="View subnet. ID: {$subnet['id']}" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_subnet\\', \\'subnet_id=>{$subnet['id']}\\', \\'display\\')');" >{$record['subnet']}</a> </td> <td class="list-row" align="left"> <span style="{$interface_style}" EOL; if ($interfaces > 1) { $html .= <<<EOL onMouseOver="wwTT(this, event, 'id', 'tt_host_interface_list_{$record['id']}', 'type', 'velcro', 'styleClass', 'wwTT_niceTitle', 'direction', 'south', 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>host_interface_list,id=>tt_host_interface_list_{$record['id']},host_id=>{$record['id']}\\');' );" EOL; } $html .= <<<EOL >{$record['ip_addr']}</span> <span title="{$record['ip_mask']}">/{$record['ip_mask_cidr']}</span> <span>{$clusterhtml}</span> </td> <td class="list-row" title="{$record['devicefull']}">{$record['device']} </td> <td class="list-row" align="right"> <span onMouseOver="wwTT(this, event, 'id', 'tt_location_{$device['location_id']}', 'type', 'velcro', 'styleClass', 'wwTT_niceTitle', 'direction', 'south', 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>location,id=>tt_location_{$device['location_id']},location_id=>{$device['location_id']}\\');' );" >{$location['reference']}</span> </td> <td class="list-row"> <span title="{$record['notes']}">{$record['notes_short']}</span> </td> <!-- ACTION ICONS --> <td class="list-row" align="right"> <form id="{$form['form_id']}_list_host_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('host_modify')) { $html .= <<<EOL <a title="Edit host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('{$form['form_id']}_list_host_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('host_del')) { $html .= <<<EOL <a title="Delete host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('{$form['form_id']}_list_host_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL </td> </tr> EOL; } if ($count == 0 and $form['subnet_id'] and !$form['filter']) { $html .= <<<EOL <tr><td colspan="99" align="center" style="color: red;">Please add the gateway host (router) to this subnet</td></tr> EOL; } $html .= <<<EOL </table> EOL; // Build page links if there are any $html .= get_page_links($page, $conf['search_results_per_page'], $count, $window_name, $form['form_id']); // If there was only 1 result, and we're about to display results in the "Search Results" window, display it. if ($count == 1 and $form['content_id'] == 'search_results_list' and $form['filter'] == '') { $js .= $primary_object_js; } // Insert the new html into the content div specified // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("{$form['form_id']}_{$tab}_count", "innerHTML", "({$count})"); $response->addAssign($form['content_id'], "innerHTML", $html); if ($js) { $response->addScript($js); } return $response->getXML(); }
function ws_editor($window_name, $form = '') { global $conf, $self, $onadb; global $font_family, $color, $style, $images; $window = array(); $window['js'] = ''; $typedisable = ''; // Check permissions if (!(auth('dns_record_modify') and auth('dns_record_add'))) { $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); // Load an existing DNS record (and associated info) if $form is a dns_record_id $host = array('fqdn' => '.'); $interface = array(); if (is_numeric($form['dns_record_id'])) { list($status, $rows, $dns_record) = ona_get_dns_record(array('id' => $form['dns_record_id'])); if ($rows) { // Load associated INTERFACE record(s) list($status, $interfaces, $interface) = ona_get_interface_record(array('id' => $dns_record['interface_id'])); $interface['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); list($status, $rows, $domain) = ona_get_domain_record(array('id' => $dns_record['domain_id'])); $dns_record['domain_fqdn'] = $domain['fqdn']; } } // Load a domain record if we got passed a domain_id if ($form['domain_id']) { list($status, $rows, $domain) = ona_get_domain_record(array('id' => $form['domain_id'])); $dns_record['domain_fqdn'] = $domain['fqdn']; } // Load a interface record if we got passed a interface_id if ($form['interface_id']) { list($status, $rows, $int) = ona_get_interface_record(array('id' => $form['interface_id'])); $interface['ip_addr'] = $int['ip_addr_text']; $window['js'] .= "el('set_ip_{$window_name}').value = '{$int['ip_addr_text']}'"; $form['js'] = "xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host=>{$int['host_id']}\\', \\'display\\')');"; } // Escape data for display in html foreach (array_keys((array) $dns_record) as $key) { $dns_record[$key] = htmlentities($dns_record[$key], ENT_QUOTES, $conf['php_charset']); } foreach (array_keys((array) $interface) as $key) { $interface[$key] = htmlentities($interface[$key], ENT_QUOTES, $conf['php_charset']); } // set the type to AAAA if it is an ipv6 address if ($dns_record['type'] == 'A' and strstr($interface['ip_addr'], ':')) { $dns_record['type'] = 'AAAA'; } // If its a CNAME, get the dns name for the A record it points to if ($dns_record['type'] == 'CNAME' or $dns_record['type'] == 'MX' or $dns_record['type'] == 'PTR' or $dns_record['type'] == 'NS' or $dns_record['type'] == 'SRV') { list($status, $rows, $existinga_data) = ona_get_dns_record(array('id' => $dns_record['dns_id'])); $dns_record['existinga_data'] = $existinga_data['fqdn']; } // If its a PTR we need to build the hostname part if ($dns_record['type'] == 'PTR') { // Flip the IP address $dns_record['name'] = ip_mangle($interface['ip_addr'], 'flip'); // strip down the IP to just the "host" part as it relates to the domain its in $domain_part = preg_replace("/.in-addr.arpa\$/", '', $dns_record['domain_fqdn']); $dns_record['name'] = preg_replace("/.{$domain_part}\$/", '', $dns_record['name']); // Disable the edit boxes related to the A record info $window['js'] .= "el('set_hostname_{$window_name}').disabled='1';el('set_domain_{$window_name}').disabled='1';el('set_a_record_{$window_name}').disabled='1';el('set_ip_{$window_name}').disabled='1';"; } // If its an A record,check to se if it has a PTR associated with it //FIXME: MP dont forget that if you change the ip of an A record that you must also update any PTR records reference to that interface $ptr_readonly = ''; if ($dns_record['type'] == 'A' or $dns_record['type'] == 'AAAA') { list($status, $rows, $hasptr) = ona_get_dns_record(array('interface_id' => $dns_record['interface_id'], 'type' => 'PTR')); if ($rows) { $hasptr_msg = '<- Already has PTR record'; $ptr_readonly = 'disabled="1"'; } } $ttl_style = ''; $editdisplay = ''; $record_types = array(); // Set the window title: if ($dns_record['id']) { $typedisable = 'disabled="1"'; if ($dns_record['dns_id']) { $viewdisable = 'disabled="1"'; } $auto_ptr_checked = ''; $window['title'] = "Edit DNS Record"; $window['js'] .= "el('record_type_select').onchange('fake event');updatednsinfo('{$window_name}');el('set_hostname_{$window_name}').focus();"; // If you are editing and there is no ttl set, use the one from the domain. if (!$dns_record['ttl']) { $ttl_style = 'style="font-style: italic;" title="Using TTL from domain"'; } // add PTR type as an editable option to the record_types array array_push($record_types, "PTR"); // if we are passing in default values for a record, set them here from form data. if (strlen($form['ip_addr']) > 1) { $interface['ip_addr'] = ip_mangle($form['ip_addr'], 'dotted'); } if (strlen($form['hostname']) > 1) { $dns_record['name'] = $form['hostname']; } } else { $auto_ptr_checked = 'checked="1"'; $window['title'] = "Add DNS Record"; $dns_record['srv_pri'] = 0; $dns_record['srv_weight'] = 0; $dns_record['ebegin'] = date('Y-m-j G:i:s', time()); $window['js'] .= "el('record_type_select').onchange('fake event');updatednsinfo('{$window_name}');el('set_hostname_{$window_name}').focus();"; // if we are passing in default values for a new record, set them here from form data. if (strlen($form['ip_addr']) > 1) { $interface['ip_addr'] = ip_mangle($form['ip_addr'], 'dotted'); } if (strlen($form['hostname']) > 1) { $dns_record['name'] = $form['hostname']; } } // Set up the types of records we can edit with this form //$record_types = array('A','CNAME','TXT','NS','MX','AAAA','SRV'); // FIXME: MP cool idea here-- support the loc record and have a google map popup to search for the location then have it populate the coords from that. // FIXME: MP it would probably be much better to use ajax to pull back the right form content than all this other javascript crap. array_push($record_types, 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'SRV', 'TXT', 'PTR'); foreach (array_keys((array) $record_types) as $id) { $record_types[$id] = htmlentities($record_types[$id]); $selected = ''; if ($record_types[$id] == $dns_record['type']) { $selected = 'SELECTED'; } $record_type_list .= "<option value=\"{$record_types[$id]}\" {$selected}>{$record_types[$id]}</option>\n"; } //Get the list of DNS views if ($conf['dns_views']) { list($status, $rows, $dnsviews) = db_get_records($onadb, 'dns_views', 'id >= 0', 'name'); foreach ($dnsviews as $entry) { $selected = ''; $dnsviews['name'] = htmlentities($dnsviews['name']); // If this entry matches the record you are editing, set it to selected if ($dns_record['id'] and $entry['id'] == $dns_record['dns_view_id']) { $selected = "SELECTED=\"selected\""; } elseif (!$dns_record['id'] and $entry['id'] == 0) { // Otherwise use the default record if we are adding a new entry $selected = "SELECTED=\"selected\""; } $dns_view_list .= "<option {$selected} value=\"{$entry['id']}\">{$entry['name']}</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 = ' <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 = ' <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; suggest_setup('set_domain_{$window_name}', 'suggest_set_domain_{$window_name}'); suggest_setup('set_a_record_{$window_name}', 'suggest_set_a_record_{$window_name}'); el('set_hostname_{$window_name}').focus(); EOL; // Define the window's inner html $window['html'] = <<<EOL <!-- DNS Record Edit Form --> <form id="{$window_name}_edit_form" onSubmit="return false;"> <input type="hidden" name="dns_id" value="{$dns_record['id']}"> <input type="hidden" name="name" value="{$host['fqdn']}"> <input type="hidden" name="js" value="{$form['js']}"> EOL; // If we are editing and thus disabling the type selector, we need to put a hidden input field if ($typedisable) { $window['html'] .= "<input type=\"hidden\" name=\"set_type\" value=\"{$dns_record['type']}\">"; } // If we are editing and thus disabling the view selector, we need to put a hidden input field if ($viewdisable) { $window['html'] .= "<input type=\"hidden\" name=\"set_view\" value=\"{$dns_record['dns_view_id']}\">"; } $window['html'] .= <<<EOL <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;" width="100%"> <!-- DNS RECORD --> <tr> <td align="right" nowrap="true"> <b><u>DNS Record</u></b> </td> <td class="padding" align="left" width="100%"> </td> </tr> </table> <!-- RECORD TYPE CONTAINER --> <div id="type_container"> <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;" width="100%"> EOL; // Print a dns view selector if ($conf['dns_views']) { $window['html'] .= <<<EOL <tr> <td align="right" nowrap="true"> DNS View </td> <td class="padding" align="left" width="100%"> <select {$viewdisable} id="dns_view_select" name="set_view" alt="DNS View" class="edit" >{$dns_view_list}</select> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr> <td class="input_required" align="right" nowrap="true"> DNS Record Type </td> <td class="padding" align="left" width="100%"> <select {$typedisable} id="record_type_select" name="set_type" alt="Record type" class="edit" onchange="var selectBox = el('record_type_select'); el('info_{$window_name}').innerHTML = ''; el('ptr_info_{$window_name}').innerHTML = ''; el('a_container').style.display = (selectBox.value == 'AAAA' || selectBox.value == 'A' || selectBox.value == 'PTR') ? '' : 'none'; el('autoptr_container').style.display = (selectBox.value == 'AAAA' || selectBox.value == 'A') ? '' : 'none'; el('mx_container').style.display = (selectBox.value == 'MX') ? '' : 'none'; el('srv_container').style.display = (selectBox.value == 'SRV') ? '' : 'none'; el('txt_container').style.display = (selectBox.value == 'TXT') ? '' : 'none'; el('name_container').style.display = (selectBox.value == 'NS' || selectBox.value == 'PTR') ? 'none' : ''; el('domain_name_container').style.display = (selectBox.value == 'PTR') ? 'none' : ''; el('existing_a_container').style.display = (selectBox.value == 'MX' || selectBox.value == 'PTR'|| selectBox.value == 'CNAME' || selectBox.value == 'NS' || selectBox.value == 'SRV') ? '' : 'none';" >{$record_type_list}</select> </td> </tr> </table> </div> <!-- COMMON CONTAINER --> <div id="common_container" style="background-color: #F2F2F2;"> <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;"> <tr id="name_container"> <td class="input_required" align="right" nowrap="true"> Host Name </td> <td class="padding" align="left" width="100%"> <input id="set_hostname_{$window_name}" name="set_name" alt="Hostname" value="{$dns_record['name']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="domain_name_container"> <td class="input_required" align="right" nowrap="true"> Domain </td> <td class="padding" align="left" width="100%"> <input id="set_domain_{$window_name}" name="set_domain" alt="Domain name" value="{$dns_record['domain_fqdn']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> <div id="suggest_set_domain_{$window_name}" class="suggest"></div> </td> </tr> EOL; // If there is a ttl in the record then display it instead of the domain setting message $ttlrow_style = ''; if ($dns_record['ttl'] == 0) { $ttlrow_style = 'style="display:none;"'; $window['html'] .= <<<EOL <tr id="ttlrowdesc"> <td align="right" nowrap="true"> TTL </td> <td class="padding" align="left" width="100%" nowrap="true"> Defaults to domain setting, <a onclick="el('ttlrowdesc').style.display = 'none';el('ttlrow').style.display = '';">override</a> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr id="ttlrow" {$ttlrow_style}> <td align="right" nowrap="true"> TTL </td> <td class="padding" align="left" width="100%"> <input {$ttl_style} id="set_ttl" name="set_ttl" alt="TTL" value="{$dns_record['ttl']}" class="edit" type="text" size="20" maxlength="20" onblur="updatednsinfo('{$window_name}');" onfocus="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="ebeginrow"> <td align="right" nowrap="true"> Begin </td> <td class="padding" align="left" width="100%"> EOL; if (strtotime($dns_record['ebegin']) < time() && strtotime($dns_record['ebegin']) > 1) { $window['html'] .= <<<EOL <input id="ebegin_input" style="display:none;" id="set_ebegin" name="set_ebegin" alt="Set a future begin time" value="{$dns_record['ebegin']}" class="edit" type="text" size="16" maxlength="30" /> <img id="ebegin_clock" style="margin-top: -6px;" src='{$images}/silk/clock.png' border='0' title="Set a future begin time" onclick="el('ebegin_clock').style.display = 'none';el('ebegin_input').style.display = '';" > -or- <input name="disable" alt="Disable this DNS entry" type="checkbox" > Disable EOL; } else { $ebegin_clockstyle = ''; $ebegin_style = 'style="display:none;"'; // If record is disabled, then check the box and hide the input box. Also set up a fake ebegin for now() in case they re-enable if (strtotime($dns_record['ebegin']) < 0) { $ebegin_disabled = 'checked="1"'; $dns_record['ebegin'] = date('Y-m-j G:i:s', time()); } if (strtotime($dns_record['ebegin']) > time()) { $ebegin_clockstyle = 'display:none;'; $ebegin_style = ''; } $window['html'] .= <<<EOL <input id="ebegin_input" {$ebegin_style} id="set_ebegin" name="set_ebegin" alt="TTL" value="{$dns_record['ebegin']}" class="edit" type="text" size="16" maxlength="30" /> <img id="ebegin_clock" style="margin-top: -6px;{$ebegin_clockstyle}" src='{$images}/silk/clock.png' border='0' title="Set a future begin time" onclick="el('ebegin_clock').style.display = 'none';el('ebegin_input').style.display = '';" > -or- <input name="disable" alt="Disable this DNS entry" type="checkbox" {$ebegin_disabled} > Disable EOL; } $window['html'] .= <<<EOL </td> </tr> <!-- A RECORD CONTAINER --> <tr id="a_container"> <td class="input_required" align="right" nowrap="true"> IP Address </td> <td class="padding" align="left" width="100%" nowrap="true"> <input id="set_ip_{$window_name}" name="set_ip" alt="IP Address" value="{$interface['ip_addr']}" class="edit" type="text" size="45" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="autoptr_container"> <td align="right" nowrap="true"> Create PTR </td> <td class="padding" align="left" width="100%" nowrap> <input id="set_auto_ptr" name="set_addptr" alt="Automaticaly create PTR record" type="checkbox" {$auto_ptr_checked} {$ptr_readonly} onchange="updatednsinfo('{$window_name}');" />{$hasptr_msg} </td> </tr> <!-- TXT CONTAINER --> <tr id="txt_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> TXT value </td> <td class="padding" align="left" width="100%"> <input id="set_txt_{$window_name}" name="set_txt" alt="TXT value" value="{$dns_record['txt']}" class="edit" type="text" size="25" maxlength="255" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- MX CONTAINER --> <tr id="mx_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> MX Preference </td> <td class="padding" align="left" width="100%"> <input id="set_mx_preference_{$window_name}" name="set_mx_preference" alt="MX preference" value="{$dns_record['mx_preference']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- SRV CONTAINER --> <tr id="srv_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> Priority<br>Weight<br>Port </td> <td class="padding" align="left" width="100%"> <input style="margin-bottom:3px;" id="set_srv_pri_{$window_name}" name="set_srv_pri" alt="SRV Priority" value="{$dns_record['srv_pri']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /><br /> <input style="margin-bottom:3px;" id="set_srv_weight_{$window_name}" name="set_srv_weight" alt="SRV Weight" value="{$dns_record['srv_weight']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /><br /> <input id="set_srv_port_{$window_name}" name="set_srv_port" alt="SRV Port" value="{$dns_record['srv_port']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- CNAME CONTAINER --> <tr id="existing_a_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> Existing A record </td> <td class="padding" align="left" width="100%"> <input id="set_a_record_{$window_name}" name="set_pointsto" alt="Points to existing A record" value="{$dns_record['existinga_data']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> <div id="suggest_set_a_record_{$window_name}" class="suggest"></div> </td> </tr> <!-- NOTES CONTAINER --> <tr id="notes_container"> <td align="right" nowrap="true"> Notes </td> <td class="padding" align="left" width="100%"> <input id="set_notes_{$window_name}" name="set_notes" alt="Notes" value="{$dns_record['notes']}" class="edit" type="text" size="45" maxlength="64" /> </td> </tr> <!-- RECORD INFO --> <tr> <td colspan="2" class="padding" align="center" width="100%" nowrap="true"> <span id="info_{$window_name}" style="color: green;font-family: monospace;"></span> </td> </tr> <tr> <td colspan="2" class="padding" align="center" width="100%" nowrap="true"> <span id="ptr_info_{$window_name}" style="color: green;font-family: monospace;"></span> </td> </tr> </table> </div> <table cellspacing="0" border="0" cellpadding="0" width="100%" style="background-color: {$color['window_content_bg']}; padding-left: 20px; padding-right: 20px; padding-top: 5px; padding-bottom: 5px;"> EOL; if (!$dns_record['id']) { $window['html'] .= <<<EOL <tr> <td align="right" nowrap="true"> </td> <td class="padding" align="left" width="100%" nowrap="true"> <input name="keepadding" alt="Keep adding more DNS records" type="checkbox" onfocus="updatednsinfo('{$window_name}');" > Keep adding more DNS records </td> </tr> <tr> <td colspan="2" class="padding" align="center" width="100%"> <span id="statusinfo_{$window_name}" style="color: green;" ></span> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr> <td align="right" valign="top" nowrap="true"> </td> <td colspan="2" class="padding" align="right" width="100%"> <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}_edit_form'), 'save');" > </td> </tr> </table> </form> EOL; return window_open($window_name, $window); }
function interface_move_host($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => interface_move_host({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.00'; // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !($options['host'] and $options['ip'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM interface_move_host-v{$version} Move an interface to a new host Synopsis: interface_move_host [KEY=VALUE] ... Required: ip=[address|ID] the IP address or ID of the interface host=[fqdn|ID] the fqdn or ID of the new host EOM ); } // Find the Host they are looking for list($status, $rows, $host) = ona_find_host($options['host']); if (!$host['id']) { printmsg("DEBUG => The host specified, {$options['host']}, does not exist!", 3); $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!"; return array(2, $self['error'] . "\n"); } printmsg("DEBUG => Host selected: {$options['host']}", 3); // Find the interface that is moving list($status, $rows, $interface) = ona_find_interface($options['ip']); if (!$interface['id']) { printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!", 3); $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!"; return array(3, $self['error'] . "\n"); } // check if this interface is the primary DNS interface address. list($status, $rows, $primaryhost) = ona_get_host_record(array('id' => $interface['host_id'])); list($status, $rows, $primarydns) = ona_get_dns_record(array('id' => $primaryhost['primary_dns_id'])); if ($primarydns['interface_id'] == $interface['id']) { printmsg("DEBUG => This interface is part of the primary DNS name for {$primaryhost['fqdn']}, please assign a new primary DNS.", 3); $self['error'] = "ERROR => This interface is part of the primary DNS name for {$primaryhost['fqdn']}, please assign a new primary DNS."; return array(4, $self['error'] . "\n"); } // if this is the last interface on the host display a message // TODO: MP is this best? I would think a lot of people WANT to move the last IP before removing the host // it would cut some steps of having to delete/re-add when moving an IP. maybe allow this?!? // ------ Since most hosts use the last interface as a primary dns id then they cant move the last interface.-------- // list($status, $rows, $int) = db_get_records($onadb, 'interfaces', array('host_id' => $interface['host_id'], '', 0); // if ($rows == 1) { // printmsg("DEBUG => You cannot delete the last interface on a host, you must delete the host itself ({$host['fqdn']}).",3); // $self['error'] = "ERROR => You can not delete the last interface on a host, you must delete the host itself ({$host['fqdn']})."; // return(array(5, $self['error'] . "\n")); // } printmsg("DEBUG => Interface selected: {$options['ip']}", 3); // Check that this interface is not associated with this host via an interface_cluster list($status, $rows, $int_cluster) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id'], 'interface_id' => $interface['id']), '', 0); printmsg("DEBUG => interface_move_host() New host is clustered with this IP, Deleting cluster record", 3); if ($rows == 1) { // Delete the interface_cluster if there is one list($status, $rows) = db_delete_records($onadb, 'interface_clusters', array('interface_id' => $interface['id'], 'host_id' => $host['id'])); if ($status or !$rows) { $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(14, $self['error'] . "\n"); } } // If the interface being moved has a NAT IP then the ext interface needs the host_id updated as well if ($interface['nat_interface_id'] > 0) { printmsg("DEBUG => interface_move_host() Moving interface with NAT IP.", 3); list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['nat_interface_id']), array('host_id' => $host['id'])); if ($status or !$rows) { $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(15, $self['error'] . "\n"); } } // Update the interface record list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), array('host_id' => $host['id'])); if ($status or !$rows) { $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(16, $self['error'] . "\n"); } $text = "INFO => Interface " . ip_mangle($interface['ip_addr'], 'dotted') . " moved to {$host['fqdn']}"; printmsg($text, 0); // Return the success notice return array(0, $text . "\n"); }