function domain_server_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.02'; printmsg("DEBUG => domain_server_del({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !($options['domain'] and $options['server'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM domain_server_del-v{$version} Removes a domain record from a DNS server Synopsis: domain_server_del [KEY=VALUE] ... Required: domain=NAME or ID domain name or ID server=NAME[.DOMAIN] or ID server name or ID Optional: commit=[Y|N] commit db transaction (no) EOM ); } if (is_numeric($options['domain'])) { $domainsearch['id'] = $options['domain']; } else { $domainsearch['name'] = strtoupper($options['domain']); } // Determine the entry itself exists list($status, $rows, $domain) = ona_get_domain_record($domainsearch); // Test to see that we were able to find the specified record if (!$domain['id']) { printmsg("DEBUG => Unable to find the domain record using {$options['domain']}!", 3); $self['error'] = "ERROR => Unable to find the domain record using {$options['domain']}!"; return array(4, $self['error'] . "\n"); } printmsg("DEBUG => domain_server_del(): Found domain, {$domain['name']}", 3); if ($options['server']) { // Determine the server is valid list($status, $rows, $host) = ona_find_host($options['server']); if (!$host['id']) { printmsg("DEBUG => The server ({$options['server']}) does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!"; return array(2, $self['error'] . "\n"); } } // Test that this domain is even assigned to the server list($status, $rows, $domainserver) = ona_get_dns_server_domain_record(array('host_id' => $host['id'], 'domain_id' => $domain['id'])); if (!$rows) { printmsg("DEBUG => Unable to find {$domain['name']} on server {$host['fqdn']}", 3); $self['error'] = "ERROR => Unable to find {$domain['name']} on server {$host['fqdn']}"; return array(11, $self['error'] . "\n"); } // Test that there are no NS records for this pair // ASSUMPTION: MP this will always be just one record?? // depending on how the user has their NS records set up, we may not find anything. list($status, $dnsrows, $dnsrec) = db_get_record($onadb, 'dns', "domain_id = {$domain['id']} AND type = 'NS' AND interface_id in (select id from interfaces where host_id = {$host['id']})"); // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced') or !authlvl($host['LVL']) or !authlvl($domain['LVL'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // delete record from domain_server_domains list($status, $rows) = db_delete_records($onadb, 'dns_server_domains', array('id' => $domainserver['id'])); if ($status) { $self['error'] = "ERROR => domain_server_del() SQL Query failed:" . $self['error']; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } // Run the module to delete the associated NS record.. Only if we found a dns record for NS if ($dnsrec['id']) { list($status, $output) = run_module('dns_record_del', array('name' => $dnsrec['id'], 'type' => 'NS', 'commit' => 'Y')); if ($status) { $self['error'] = "ERROR => domain_server_del() NS record delete failed:" . $output; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } else { // add the output to self error for display $add_to_error = $output; } } // Return the success notice $self['error'] = "INFO => DNS Domain/Server Pair DELETED: {$domain['name']}/{$host['fqdn']} "; printmsg($self['error'], 0); return array(0, $add_to_error . $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been removed: {$domain['name']} from: {$host['fqdn']} EOL; if ($dnsrows) { $text .= " Removing related NS record, if any. Please double check your NS records for this domain.\n"; } return array(6, $text); }
function custom_attribute_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => custom_attribute_del({$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['subnet'] and !$options['host'] and !$options['vlan'] or !$options['type']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM custom_attribute_del-v{$version} Deletes a custom attribute from the database Synopsis: custom_attribute_del [KEY=VALUE] ... Required: host=NAME[.DOMAIN]|IP hostname or IP of the host OR subnet=NAME|IP name or IP of the subnet OR vlan=NAME name of the VLAN type=ID|STRING the name or ID of the attribute type Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // If they provided a hostname / ID let's look it up if ($options['host']) { list($status, $rows, $host) = ona_find_host($options['host']); $table_name_ref = 'hosts'; $table_id_ref = $host['id']; $desc = $host['fqdn']; } else { if ($options['subnet']) { list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); $table_name_ref = 'subnets'; $table_id_ref = $subnet['id']; $desc = $subnet['name']; } else { if ($options['vlan']) { list($status, $rows, $vlan) = ona_find_vlan($options['vlan']); $table_name_ref = 'vlans'; $table_id_ref = $vlan['id']; $desc = $vlan['name']; } } } // If we didn't get a record then exit if (!$host['id'] and !$subnet['id'] and !$vlan['id']) { printmsg("DEBUG => No host, subnet or vlan found!", 3); $self['error'] = "ERROR => No host, subnet or vlan found!"; return array(1, $self['error'] . "\n"); } // If the type provided is numeric, check to see if it's an vlan if (is_numeric($options['type'])) { // See if it's an vlan_campus_id list($status, $rows, $catype) = ona_get_custom_attribute_type_record(array('id' => $options['type'])); if (!$catype['id']) { printmsg("DEBUG => Unable to find custom attribute type using the ID {$options['name']}!", 3); $self['error'] = "ERROR => Unable to find custom attribute type using the ID {$options['name']}!"; return array(2, $self['error'] . "\n"); } } else { $options['type'] = trim($options['type']); list($status, $rows, $catype) = ona_get_custom_attribute_type_record(array('name' => $options['type'])); if (!$catype['id']) { printmsg("DEBUG => Unable to find custom attribute type using the name {$options['type']}!", 3); $self['error'] = "ERROR => Unable to find custom attribute type using the name {$options['type']}!"; return array(3, $self['error'] . "\n"); } } list($status, $rows, $record) = ona_get_custom_attribute_record(array('table_name_ref' => $table_name_ref, 'table_id_ref' => $table_id_ref, 'custom_attribute_type_id' => $catype['id'])); if (!$rows) { printmsg("DEBUG => Unable to find custom attribute!", 3); $self['error'] = "ERROR => Unable to find custom attribute!"; return array(4, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('custom_attribute_del')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'custom_attributes', array('id' => $record['id'])); if ($status or !$rows) { $self['error'] = "ERROR => custom_attribute_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => Custom Attribute DELETED: {$record['name']} ({$record['value']}) from {$desc}"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: ASSOCIATED WITH: {$desc} NAME: {$record['name']} VALUE: {$record['value']} EOL; return array(6, $text); }
function dhcp_server_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.03'; printmsg("DEBUG => dhcp_server_del({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !($options['subnet'] and $options['server'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_server_del-v{$version} Removes a subnet record from a DHCP server Synopsis: dhcp_server_del [KEY=VALUE] ... Required: subnet=NAME or ID subnet name or ID server=NAME[.DOMAIN] or ID server name or ID Optional: commit=[Y|N] commit db transaction (no) Notes: DOMAIN will default to {$conf['dns_defaultdomain']} if not specified EOM ); } // Determine the entry itself exists list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); // Test to see that we were able to find the specified record if (!$subnet['id']) { printmsg("DEBUG => Unable to find the subnet record using {$options['subnet']}!", 3); $self['error'] = "ERROR => Unable to find the subnet record using {$options['subnet']}!"; return array(4, $self['error'] . "\n"); } printmsg("DEBUG => dhcp_server_del(): Found subnet, {$subnet['name']}", 3); if ($options['server']) { // Determine the server is valid list($status, $rows, $host) = ona_find_host($options['server']); if (!$host['id']) { printmsg("DEBUG => The server ({$options['server']}) does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!"; return array(2, $self['error'] . "\n"); } } //printmsg("DEBUG => dhcp_server_del(): Found server, {$host['FQDN']}", 3); // Test that this subnet is even assigned to the server list($status, $rows, $dhcpserver) = ona_get_dhcp_server_subnet_record(array('host_id' => $host['id'], 'subnet_id' => $subnet['id'])); if (!$rows) { printmsg("DEBUG => Unable to find {$subnet['name']} on server {$host['fqdn']}", 3); $self['error'] = "ERROR => Unable to find {$subnet['name']} on server {$host['fqdn']}"; return array(11, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced') or !authlvl($host['LVL']) or !authlvl($subnet['LVL'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // check if allowed to remove subnet from server // check for pool assigned to the server itself list($status, $rows, $pools) = db_get_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id'])); foreach ($pools as $pool) { if ($pool['dhcp_failover_group_id']) { $foundfg = 0; list($status, $rows, $primary) = ona_get_dhcp_failover_group_record(array('id' => $pool['dhcp_failover_group_id'], 'primary_server_id' => $host['id'])); if ($rows) { $foundfg++; } list($status, $rows, $secondary) = ona_get_dhcp_failover_group_record(array('id' => $pool['dhcp_failover_group_id'], 'secondary_server_id' => $host['id'])); if ($rows) { $foundfg++; } // if a subnet/server pair is found in dhcp pools, don't allow removal if ($foundfg > 0) { printmsg("DEBUG => Subnet ({$subnet['name']}) has a pool assigned to this Server ({$host['fqdn']}), which is part of a failover group. The server must be removed from the failover group first.", 3); $self['error'] = "ERROR => Subnet ({$subnet['name']}) has a pool assigned to this Server ({$host['fqdn']}), which is part of a failover group. The server must be removed from the failover group first."; return array(12, $self['error'] . "\n"); } } } // MP: remove this after testing. dhcp options should not stop us from dis-associating a subnet from a server // Not really sure why I have this.. probably left over cruft from old thoughts // // check if there are any DHCP parameters assigned to the subnet // list($status, $rows, $tmp) = ona_get_dhcp_option_entry_record(array('subnet_id' => $subnet['id'])); // // // if so, check that this is not the last DHCP server that services this subnet // if ($rows > 0) { // list($status, $rows, $tmp) = ona_get_dhcp_server_subnet_record(array('subnet_id' => $subnet['id'])); // // // If this is the last DHCP server that services this subnet, don't allow removal until DHCP parameters are removed // if($rows <= 1){ // printmsg("DEBUG => Subnet ({$subnet['name']}) has DHCP parameters assigned which need to be removed first",3); // $self['error'] = "ERROR => Subnet ({$subnet['name']}) has DHCP parameters assigned which need to be removed first"; // return(array(12, $self['error'] . "\n")); // } // } // delete record from dhcp_server_subnets list($status, $rows) = db_delete_records($onadb, 'dhcp_server_subnets', array('id' => $dhcpserver['id'])); if ($status) { $self['error'] = "ERROR => dhcp_server_del() SQL Query failed:" . $self['error']; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => DHCP Subnet/Server Pair DELETED: {$subnet['name']}/{$host['fqdn']} "; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been removed: {$subnet['name']} from: {$host['fqdn']} EOL; return array(6, $text); }
function host_display($options = "") { global $conf, $self, $onadb; $text_array = array(); // Version - UPDATE on every edit! $version = '1.04'; printmsg("DEBUG => host_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['host']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM host_display-v{$version} Displays a host record from the database Synopsis: host_display [KEY=VALUE] ... Required: host=NAME[.DOMAIN] or ID hostname or ID of the host display Optional: verbose=[yes|no] display additional info (yes) EOM ); } // Find the host (and domain) record from $options['host'] list($status, $rows, $host) = ona_find_host($options['host']); printmsg("DEBUG => Host: {$host['fqdn']}", 3); if (!$host['id']) { printmsg("DEBUG => Unknown host: {$options['host']}", 3); $self['error'] = "ERROR => Unknown host: {$options['host']}"; return array(2, $self['error'] . "\n"); } $text_array = $host; // Build text to return $text = "HOST RECORD ({$host['fqdn']})\n"; $text .= format_array($host); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // TODO: if it is a nat interface, maybe process that IP and make it visible? // Interface record(s) $i = 0; do { list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED INTERFACE RECORD ({$i} of {$rows})\n"; $text .= format_array($interface); $text_array['interfaces'][$i] = $interface; unset($text_array['interfaces'][$i]['host_id']); } while ($i < $rows); $text_array['interface_count'] = $rows; // Device record list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); if ($rows >= 1) { // Fill out some other device info list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $device['device_type'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id'])); $text_array['location'] = $location; $text_array['device'] = $device; $text .= "\nASSOCIATED DEVICE RECORD\n"; $text .= format_array($device); } // Tag records list($status, $rows, $tags) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id'])); if ($rows) { $text .= "\nASSOCIATED TAG RECORDS\n"; foreach ($tags as $tag) { $text_array['tags'][] = $tag['name']; $text .= " {$tag['name']}\n"; } } } // Cleanup unused info unset($text_array['device_id']); unset($text_array['device']['asset_tag']); unset($text_array['device']['location_id']); unset($text_array['device']['serial_number']); // change the output format if other than default if ($options['format'] == 'json') { $text = $text_array; } if ($options['format'] == 'yaml') { $text = $text_array; } // Return the success notice return array(0, $text); }
function mysql_purge_logs($options) { global $conf, $self, $ona_db; printmsg('DEBUG => mysql_purge_logs(' . $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['slaves']) { $self['error'] = 'ERROR => Insufficient parameters'; // NOTE: Help message lines should not exceed 80 characters for proper display on a console return array(1, <<<EOM mysql_purge_logs-v{$version} Connects to a specified list of MySQL slave servers, checks where they are in reading/replicating the master server's binary logs, and deletes logs from the associated master(s) which are no longer needed by any slave system. A list of slave servers is supplied as input, and master servers are detected automatically. Synopsis: mysql_purge_logs [KEY=VALUE] Required: slaves=NAME[,NAME ...] list of slave server(s) to connect to Optional: commit=[yes|no] commit changes to database (default: no) user=NAME mysql username (default: root) password=STRING mysql password (default: blank) EOM ); } // Set default user ID, if none was provided. if (!$options['user']) { $options['user'] = '******'; } // Sanitize "options[commit]" (no is the default) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Split out the list of slave servers into an array (comma-delimited). $slaves = preg_split('/,/', $options['slaves']); // Now we begin... $masters = array(); foreach ($slaves as $slave_host) { if (!$slave_host or $slave_host == "") { continue; } printmsg("DEBUG => Connect to slave host mysql://{$options['user']}:{$options['password']}@{$slave_host}", 4); $dbh = db_connect('mysql', $slave_host, $options['user'], $options['password'], 'mysql'); if (!$dbh || !$dbh->IsConnected()) { continue; } // Find out this slave's replication status. $q = "show slave status;"; $rs = $dbh->Execute($q); $array = $rs->FetchRow(); // Check if our master is listed, and if so, make sure the oldest // binary logfile (by name) is stored in the array. $matched = 0; foreach ($masters as $host => $binlog) { if ($host == $array['Master_Host'] && $binlog > $array['Master_Log_File']) { $masters['$host'] = $array['Master_Log_File']; $matched = 1; break; } } // If our master wasn't listed, then create a new entry. if ($matched == 0) { $masters[$array['Master_Host']] = $array['Master_Log_File']; } } // Now the "output" step... $retval_string = ""; $retval_errlvl = 0; foreach ($masters as $host => $binlog) { if ($options['commit'] == 'Y') { $dbh = db_connect('mysql', $host, $options['user'], $options['password'], 'mysql'); if (!$dbh || !$dbh->IsConnected()) { $self['error'] .= "ERROR => Could not connect to host '{$host}' to execute query. Skipping.\n"; $retval_errlvl = 2; continue; } } $q = "purge master logs to '{$binlog}'"; if ($options['commit'] == 'Y') { $rs = $dbh->Execute($q); $error = $dbh->ErrorMsg(); // Report any errors if ($rs === false or $error) { $self['error'] .= 'ERROR => SQL query on host {$host} failed: ' . $error . "\n"; $retval_errlvl = 2; } else { $retval_string .= "Successfully executed ({$q}) on host '{$host}'.\n"; } } else { $retval_string .= "Not commiting changes. Would have executed: ({$q}) on host '{$host}'.\n"; } } // Return our results, as success strings and (perhaps) error strings. return array($retval_errlvl, $retval_string); }
function config_display($options = "") { // The important globals global $conf; global $self; global $onadb; // Version - UPDATE on every edit! $version = '1.02'; printmsg('DEBUG => config_display(' . $options . ') called', 3); // Parse incoming options string to an array $options = parse_options($options); // Set "options[verbose] to yes if it's not set if (!array_key_exists('verbose', $options)) { $options['verbose'] = 'Y'; } else { $options['verbose'] = sanitize_YN($options['verbose']); } // Return the usage summary if we need to if ($options['help'] or !$options['config'] and (!$options['host'] or !$options['type'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console return array(1, <<<EOM config_display-v{$version} Displays a config text record from the database Synopsis: config_display [KEY=VALUE] ... Required: config=ID display config by record ID - or - host=ID or NAME[.DOMAIN] display most recent config for specified host type=TYPE type of config to display - usually "IOS_VERSION" or "IOS_CONFIG" Optional: verbose=[yes|no] display entire record (yes) "no" displays only the actual config text EOM ); } // Get a config record if there is one $self['error'] = ""; list($status, $rows, $config) = ona_find_config($options); // Error if an error was returned if ($status or !$config['id']) { $text = ""; if ($self['error']) { $text = $self['error'] . "\n"; } $text .= "ERROR => No config text entries found!\n"; return array(2, $text); } // If 'verbose' is enabled, we display the entire record if ($options['verbose'] == 'Y') { // Build text to return $text = "CONFIG TEXT RECORD (1 of {$rows})\n"; $text .= format_array($config); } else { $text = $config['config_body']; } // Return the success notice return array(0, $text); }
function domain_del($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => domain_del({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.02'; // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // 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, <<<EOM domain_del-v{$version} Deletes a DNS domain from the database Synopsis: domain_del [KEY=VALUE] ... Required: domain=NAME or ID name or ID of the domain to delete Optional: commit=[Y|N] commit db transaction (no) EOM ); } // Check if it is an ID or NAME if (is_numeric($options['domain'])) { $domainsearch = array('id' => $options['domain']); } else { $domainsearch = array('name' => $options['domain']); } // Test that the domain actually exists. list($status, $tmp_rows, $entry) = ona_get_domain_record($domainsearch); if (!$entry['id']) { printmsg("DEBUG => Unable to find a domain record using ID {$options['domain']}!", 3); $self['error'] = "ERROR => Unable to find a domain record using ID {$options['domain']}!"; return array(4, $self['error'] . "\n"); } // Debugging list($status, $tmp_rows, $tmp_parent) = ona_get_domain_record(array('id' => $entry['parent_id'])); printmsg("DEBUG => Domain selected: {$entry['name']}.{$tmp_parent['name']}", 3); // Display an error if DNS records are using this domain list($status, $rows, $dns) = db_get_records($onadb, 'dns', array('domain_id' => $entry['id'])); if ($rows) { printmsg("DEBUG => Domain ({$entry['name']}) can't be deleted, it is in use by {$rows} DNS entries!", 3); $self['error'] = "ERROR => Domain ({$entry['name']}) can't be deleted, it is in use by {$rows} DNS entries!"; return array(5, $self['error'] . "\n"); } // Display an error if it is a parent of other domains list($status, $rows, $parent) = db_get_records($onadb, 'domains', array('parent_id' => $entry['id'])); if ($rows) { printmsg("DEBUG => Domain ({$entry['name']}) can't be deleted, it is the parent of {$rows} other domain(s)!", 3); $self['error'] = "ERROR => Domain ({$entry['name']}) can't be deleted, it is the parent of {$rows} other domain(s)!"; return array(7, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Delete association with any servers list($status, $rows) = db_delete_records($onadb, 'dns_server_domains', array('domain_id' => $entry['id'])); if ($status) { $self['error'] = "ERROR => domain_del() SQL Query (dns_server_domains) failed: {$self['error']}"; printmsg($self['error'], 0); return array(8, $self['error'] . "\n"); } // Delete actual domain list($status, $rows) = db_delete_records($onadb, 'domains', array('id' => $entry['id'])); if ($status) { $self['error'] = "ERROR => domain_del() SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => Domain DELETED: {$entry['name']}"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: NAME: {$entry['name']} EOL; return array(6, $text); }
function subnet_nextip($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg('DEBUG => subnet_del(' . $options . ') called', 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !$options['subnet']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM subnet_del-v{$version} Return the next available IP address on a subnet. Synopsis: subnet_nextip [KEY=VALUE] ... Required: subnet=IP or ID select subnet by search string Optional: offset=NUMBER Starting offset to find next available IP output=[dotted|numeric] Return the number as a dotted or numeric value DEFAULT: numeric EOM ); } // Find the subnet record we're deleting list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); if ($status or !$rows) { $self['error'] = "ERROR => Subnet not found"; return array(2, $self['error'] . "\n"); } // Create a few variables that will be handy later $num_ips = 0xffffffff - $subnet['ip_mask']; $last_ip = $subnet['ip_addr'] + $num_ips - 1; // check that offset is a number if (isset($options['offset']) and !is_numeric($options['offset'])) { $self['error'] = "ERROR => Offset must be a numeric number"; return array(3, $self['error'] . "\n"); } else { $offsetmsg = " beyond offset {$options['offset']}"; } // make sure the offset does not extend beyond the specified subnet if ($options['offset'] >= $num_ips - 1) { $self['error'] = "ERROR => Offset extends beyond specified subnet boundary"; return array(4, $self['error'] . "\n"); } if (!isset($options['output'])) { $options['output'] = '1'; } else { if ($options['output'] != 'dotted' && $options['output'] != 'numeric') { $self['error'] = "ERROR => Output option must be 'dotted' or 'numeric'"; return array(5, $self['error'] . "\n"); } } // Find the first number based on our subnet and offset $ip = $subnet['ip_addr'] + $options['offset']; // Make sure we skip past the subnet IP to the first usable IP if ($ip == $subnet['ip_addr']) { $ip++; } // Start looping through our IP addresses until we find an available one while ($ip <= $last_ip) { // Find out if the ip is used in an interface list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('ip_addr' => $ip)); // If we find a free address.. check that it is not in a DHCP pool if (!$rows) { list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', "{$ip} >= ip_addr_start AND {$ip} <= ip_addr_end"); if ($rows) { $ip = $pool['ip_addr_end']; } else { break; } } $ip++; // increment by one and check again } // If we checked all the IPs, make sure we are not on the broadcast IP of the subnet if ($ip == $last_ip + 1) { $self['error'] = "ERROR => No available IP addresses found on subnet{$offsetmsg}"; return array(5, $self['error'] . "\n"); } // return the IP return array(0, ip_mangle($ip, $options['output']) . "\n"); }
function dhcp_pool_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => dhcp_pool_del({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !$options['id']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_pool_del-v{$version} Deletes a DHCP pool from the database Synopsis: dhcp_pool_del [KEY=VALUE] ... Required: id=ID ID of the DHCP pool to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // If the option provided is numeric, check to see if it exists if (is_numeric($options['id'])) { list($status, $tmp_rows, $pool) = ona_get_dhcp_pool_record(array('id' => $options['id'])); // Test to see that we were able to find the specified pool record if (!$pool['id']) { printmsg("DEBUG => Unable to find the DHCP pool record using ID: {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find the DHCP pool record using ID: {$options['id']}!"; return array(2, $self['error'] . "\n"); } $start = ip_mangle($pool['ip_addr_start'], 'dotted'); $end = ip_mangle($pool['ip_addr_end'], 'dotted'); list($status, $tmp_rows, $subnet) = ona_get_subnet_record(array('id' => $pool['subnet_id'])); } else { printmsg("DEBUG => {$options['id']} is not a numeric value!", 3); $self['error'] = "ERROR => {$options['id']} is not a numeric value"; return array(3, $self['error'] . "\n"); } // If "commit" is yes, delte the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced') or !authlvl($subnet['lvl'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'dhcp_pools', array('id' => $pool['id'])); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_pool_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => DHCP pool DELETED: {$start}-{$end} from {$subnet['name']}."; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: Delete the following dhcp pool: ENTRY: {$start}=>{$end} from {$subnet['name']} EOL; return array(6, $text); }
function dhcp_failover_group_del($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => dhcp_failover_group_del({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.00'; // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !$options['id']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_failover_group_del-v{$version} Deletes a DHCP failover group from the database Synopsis: dhcp_failover_group_del [KEY=VALUE] ... Required: id=id id of the failover group to delete Optional: commit=[Y|N] commit db transaction (no) EOM ); } // Test that the group actually exists. list($status, $tmp_rows, $entry) = ona_get_dhcp_failover_group_record(array('id' => $options['id'])); if (!$entry['id']) { printmsg("DEBUG => Unable to find a DHCP failover group record using id {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find a DHCP failover group record using id {$options['id']}!"; return array(4, $self['error'] . "\n"); } // Debugging printmsg("DEBUG => DHCP failover group selected: {$entry['id']}", 3); // Display an error if pools are using this zone list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', array('id' => $entry['id'])); if ($rows) { printmsg("DEBUG => DHCP failover group ({$entry['id']}) can't be deleted, it is in use on 1 or more pools!", 3); $self['error'] = "ERROR => DHCP failover group ({$entry['id']}) can't be deleted, it is in use on 1 or more pools!"; return array(5, $self['error'] . "\n"); } list($status, $rows, $pri_host) = ona_find_host($entry['primary_server_id']); list($status, $rows, $sec_host) = ona_find_host($entry['secondary_server_id']); // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Delete actual zone list($status, $rows) = db_delete_records($onadb, 'dhcp_failover_groups', array('id' => $entry['id'])); if ($status) { $self['error'] = "ERROR => dhcp_failover_group_del() SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => DHCP failover group DELETED: {$entry['id']} => PRI:{$pri_host['fqdn']} SEC:{$sec_host['fqdn']}"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: NAME: {$entry['id']} PRIMARY: {$pri_host['fqdn']} SECONDARY: {$sec_host['fqdn']} EOL; return array(6, $text); }
function dhcp_entry_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => dhcp_entry_del({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is yes) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !$options['id']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_entry_del-v{$version} Deletes a DHCP entry from the database Synopsis: dhcp_entry_del [KEY=VALUE] ... Required: id=ID ID of the dhcp entry to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); $desc = 'Global level'; // If the option provided is numeric, check to see if it exists if (is_numeric($options['id'])) { // Debugging printmsg("DEBUG => DHCP entry ID selected: {$options['id']}", 3); list($status, $tmp_rows, $entry) = ona_get_dhcp_option_entry_record(array('id' => $options['id'])); // Test to see that we were able to find the specified record if (!$entry['id']) { printmsg("DEBUG => Unable to find the DHCP entry record using ID {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find the DHCP entry record using ID {$options['id']}!"; return array(4, $self['error'] . "\n"); } // Assign a search option based on host or server id if ($entry['host_id']) { $search = $entry['host_id']; } if ($entry['server_id']) { $search = $entry['server_id']; } if ($entry['host_id'] or $entry['server_id']) { // Get some host information to display later and determine its valid list($status, $rows, $host) = ona_find_host($search); // Bail out if you cant find a host if (!$host['id']) { printmsg("DEBUG => The ID specified, {$search}, does not exist!", 3); $self['error'] = "ERROR => The ID specified, {$search}, does not exist!"; return array(3, $self['error'] . "\n"); } printmsg("DEBUG => dhcp_entry_del(): Using host: {$host['fqdn']} ID: {$host['id']}", 3); $desc = $host['fqdn']; $lvl = $host['lvl']; } elseif ($entry['subnet_id']) { // Determine the subnet is valid list($status, $rows, $subnet) = ona_find_subnet($entry['subnet_id']); if (!$subnet['id']) { printmsg("DEBUG => The subnet specified, {$options['subnet']}, does not exist!", 3); $self['error'] = "ERROR => The subnet specified, {$options['subnet']}, does not exist!"; return array(3, $self['error'] . "\n"); } printmsg("DEBUG => dhcp_entry_del(): Using subnet: {$subnet['name']} ID: {$subnet['id']}", 3); $desc = "{$subnet['name']} (" . ip_mangle($subnet['ip_addr']) . ")"; $lvl = $subnet['lvl']; } } else { printmsg("DEBUG => {$options['id']} is not a numeric value", 3); $self['error'] = "ERROR => {$options['id']} is not a numeric value"; return array(15, $self['error'] . "\n"); } // If "commit" is yes, delte the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('advanced') or !authlvl($lvl)) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'dhcp_option_entries', array('id' => $entry['id'])); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_entry_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => DHCP entry DELETED: {$entry['display_name']}={$entry['value']} from {$desc} "; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: ON: {$desc} Delete the following dhcp entry: ENTRY: {$entry['display_name']} = {$entry['value']} EOL; return array(6, $text); }
function vlan_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => vlan_del({$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['vlan']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM vlan_del-v{$version} Deletes an vlan from the database Synopsis: vlan_del [KEY=VALUE] ... Required: vlan=ID ID of the vlan to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // If the vlan provided is numeric, check to see if it's an vlan if (is_numeric($options['vlan'])) { // See if it's an vlan_id list($status, $rows, $vlan) = ona_get_vlan_record(array('id' => $options['vlan'])); } if (!$vlan['id']) { printmsg("DEBUG => Unable to find VLAN ({$options['vlan']})!", 3); $self['error'] = "ERROR => Unable to find VLAN ({$options['vlan']})!"; return array(2, $self['error'] . "\n"); } list($status, $rows, $network) = db_get_records($onadb, 'subnets', array('vlan_id' => $vlan['id']), '', 0); if ($rows != 0) { printmsg("DEBUG => This VLAN ({$vlan['name']}) is in use by {$rows} network(s)!", 3); $self['error'] = "ERROR => This VLAN ({$vlan['name']}) is in use by {$rows} network(s)!"; return array(6, $self['error'] . "\n" . "INFO => Please dis-associate those networks from this vlan before deleting.\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('vlan_del')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'vlans', array('id' => $vlan['id'])); if ($status or !$rows) { $self['error'] = "ERROR => vlan_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => VLAN DELETED: {$vlan['name']}"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $vlan['vlan_campus_id'])); // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: NAME: {$vlan['name']} NUMBER: {$vlan['number']} CAMPUS: {$campus['name']} EOL; return array(6, $text); }
function location_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => location_del({$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['reference']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM locaiton_del-v{$version} Deletes a location from the database Synopsis: location_del [KEY=VALUE] ... Required: reference=NAME or ID Reference or ID of the location to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Find the Location to use list($status, $rows, $loc) = ona_find_location($options['reference']); if ($status or !$rows) { printmsg("DEBUG => The location specified, {$options['reference']}, does not exist!", 3); return array(2, "ERROR => The location specified, {$options['reference']}, does not exist!\n"); } printmsg("DEBUG => Location selected: {$loc['reference']}, location name: {$loc['name']}", 3); list($status, $rows, $usage) = db_get_records($onadb, 'devices', array('location_id' => $loc['id']), '', 0); if ($rows != 0) { printmsg("DEBUG => The location ({$loc['reference']}) is in use by {$rows} devices(s)!", 3); $self['error'] = "ERROR => The location ({$loc['reference']}) is in use by {$rows} devices(s)!"; return array(6, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('location_del')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'locations', array('id' => $loc['id'])); if ($status or !$rows) { $self['error'] = "ERROR => location_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => Location DELETED: {$loc['reference']} ({$loc['name']})"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: EOL; $text .= format_array($loc); $text .= "\n"; return array(6, $text); }
function ws_save($window_name, $form = '') { global $include, $conf, $self, $onadb; // Check permissions if (!(auth('dns_record_modify') and auth('dns_record_add'))) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Validate input // if ($form['set_domain'] == '' or // $form['set_type'] == '' // ) { // $response->addScript("alert('Please complete all fields to continue!');"); // return($response->getXML()); // } // we need to do a little validation here to make sure things // have a good chance of working! // If the name we were passed has a leading . in it then remove the dot. $form['set_name'] = preg_replace("/^\\./", '', trim($form['set_name'])); $form['set_ip'] = trim($form['set_ip']); // Validate the "set_name" name is valid if ($form['set_name'] and $form['set_type'] != 'NS') { $form['set_name'] = sanitize_hostname($form['set_name']); if (!$form['set_name']) { $response->addScript("alert('Invalid hostname!');"); return $response->getXML(); } } // Make sure the IP address specified is valid if ($form['set_name'] != '.' and $form['set_ip']) { $form['set_ip'] = ip_mangle($form['set_ip'], 'dotted'); if ($form['set_ip'] == -1) { $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } } $form['set_addptr'] = sanitize_YN($form['set_addptr'], 'N'); // Set the effective date to 0 to disable if ($form['disable']) { $form['set_ebegin'] = 0; } // Decide if we're editing or adding $module = 'modify'; // If we're adding, re-map some the array names to match what the "add" module wants if (!$form['dns_id']) { $module = 'add'; // options $form['domain'] = $form['set_domain']; $form['name'] = $form['set_name'] . '.' . $form['set_domain']; unset($form['set_name']); unset($form['set_domain']); $form['type'] = $form['set_type']; unset($form['set_type']); $form['ebegin'] = $form['set_ebegin']; unset($form['set_ebegin']); $form['notes'] = $form['set_notes']; unset($form['set_notes']); $form['ip'] = $form['set_ip']; unset($form['set_ip']); $form['ttl'] = $form['set_ttl']; unset($form['set_ttl']); $form['addptr'] = $form['set_addptr']; unset($form['set_addptr']); $form['view'] = $form['set_view']; unset($form['set_view']); // if this is a cname. then set the pointsto option if ($form['type'] == 'CNAME' or $form['type'] == 'MX' or $form['type'] == 'NS' or $form['type'] == 'SRV') { $form['pointsto'] = $form['set_pointsto']; } if ($form['type'] == 'MX') { $form['mx_preference'] = $form['set_mx_preference']; } if ($form['type'] == 'TXT') { $form['txt'] = $form['set_txt']; } if ($form['type'] == 'SRV') { $form['srv_pri'] = $form['set_srv_pri']; } if ($form['type'] == 'SRV') { $form['srv_weight'] = $form['set_srv_weight']; } if ($form['type'] == 'SRV') { $form['srv_port'] = $form['set_srv_port']; } // If it is an NS record, blank the name out //if ($form['type'] == 'NS') $form['name'] = $form['set_domain']; // If we are adding a PTR.. switch existing a record to name if ($form['type'] == 'PTR') { $form['name'] = $form['set_pointsto']; } // If there's no "refresh" javascript, add a command to view the new dns record if (!preg_match('/\\w/', $form['js'])) { $form['js'] = "xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host=>{$form['name']}\\', \\'display\\')');"; } } else { $form['set_name'] .= '.' . $form['set_domain']; //FIXME: MP temporary kludge to get around not having a proper find_dns_record module.. ID is the only way to find a record now and it is done via the name field $form['name'] = $form['dns_id']; // if this is a cname. then set the pointsto option if ($form['set_type'] != 'CNAME') { $form['set_pointsto'] == ''; } } // Run the module to ADD the DNS record, or MODIFY THE DNS record. list($status, $output) = run_module('dns_record_' . $module, $form); // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed.\\n" . preg_replace('/[\\s\']+/', ' ', $self['error']) . "');"; } else { // if they have checked the keep adding records box then dont remove the window if (!$form['keepadding']) { $js .= "removeElement('{$window_name}');"; } else { $js .= "el('statusinfo_{$window_name}').innerHTML = 'Previously added:<br>{$form['name']} Type: {$form['type']}';"; } if ($form['js']) { $js .= $form['js']; } } // Insert the new table into the window $response->addScript($js); return $response->getXML(); }
function ona_sql($options = "") { // The important globals global $conf, $onadb, $base; // Version - UPDATE on every edit! $version = '1.05'; // TODO: Maybe make this into a sys_config option $srvdir = dirname($base) . "/sql"; printmsg('DEBUG => ona_sql(' . $options . ') called', 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize delimeter if (!$options['delimiter']) { $options['delimiter'] = ':'; } // fix up the escaped ' marks. may need the = and & stuff too???? $options['sql'] = str_replace('\\\'', '\'', $options['sql']); $options['sql'] = str_replace('\\=', '=', $options['sql']); // Set "options[commit] to no if it's not set if (!array_key_exists('commit', $options)) { $options['commit'] = 'N'; } else { $options['commit'] = sanitize_YN($options['commit'], 'N'); } // Set "options[commit] to no if it's not set if (!array_key_exists('dataarray', $options)) { $options['dataarray'] = 'N'; } else { $options['dataarray'] = sanitize_YN($options['dataarray'], 'N'); } // Set "options[header] to yes if it's not set if (!array_key_exists('header', $options)) { $options['header'] = 'Y'; } else { $options['header'] = sanitize_YN($options['header'], 'Y'); } // Check permissions if (!auth('ona_sql')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Return the usage summary if we need to if ($options['help'] or !($options['list'] and !$options['sql'] or !$options['list'] and $options['sql'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console return array(1, <<<EOM ona_sql-v{$version} Runs the specified SQL query on the database and prints the result Synopsis: ona_sql [KEY=VALUE] ... Required: sql=STATEMENT|FILENAME quoted SQL statement to execute OR list lists the SQL files available on the server side Optional: show displays contents of SQL, gives usage etc commit=yes|no commit the transaction (no) header=yes|no display record header (yes) delimiter=DELIMITER record delimiter for output (:) (1,2,..)=VALUE bind variables, replaces ? in query sequentially. the first ? found is replaced by 1=value, and so on Notes: * Query is sent to the configured OpenNetAdmin database server. * The use of bind variables requires your options to match positionally. * The SQL option will be tried first as a local file, then as a server file, then as a raw text SQL query. Filenames are case sensitive. * Server based SQL files are located in {$srvdir} * Some plugins may provide their own SQL dir inside the plugin directory * Use the show option to display contents of SQL files, this should contain a long description and any usage information that is needed. EOM ); } // TODO: check that the user has admin privs? or at least a ona_sql priv // Get a list of the files $plugins = plugin_list(); $files = array(); $srvdirs = array(); array_push($srvdirs, $srvdir); // add a local sql dir as well so they don't get overrriden by installs array_push($srvdirs, dirname($base) . '/www/local/sql'); // loop through the plugins and find files inside of their sql directories. foreach ($plugins as $plug) { array_push($srvdirs, $plug['path'] . '/sql'); } // Loop through each of our plugin directories and the default directory to find .sql files foreach ($srvdirs as $srvdir) { if ($handle = @opendir($srvdir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && substr($file, -4) == '.sql') { // Build an array of filenames array_push($files, $srvdir . '/' . $file); } } closedir($handle); } } // sort the file names asort($files); // List the sql files on the server side if ($options['list'] == 'Y') { $text .= sprintf("\n%-25s%s\n", 'FILE', 'DESCRIPTION'); $text .= sprintf("%'-80s\n", ''); // Loop through and display info about the files foreach ($files as $file) { // Open the file and get the first line, this is the short description $fh = fopen($file, 'r'); $desc = rtrim(fgets($fh)); fclose($fh); // Print the info $text .= sprintf("%-25s%s\n", basename($file), $desc); } $text .= "\n"; return array(0, $text); } // Check that the sql variable passsed matches a file name locally, if it does, open it and replace $options['sql'] with it // Loop through files array till we find the right file $foundfile = false; foreach ($files as $file) { if (strstr($file, $options['sql'])) { $options['sql'] = trim(file_get_contents($file)); $foundfile = true; } } // if we have not found a file on the server and the sql option does end in .sql then print a message that we coulnt find a file // otherwise assume it is a sql statement being passed at the cli if ($foundfile == false and substr($options['sql'], -4) == '.sql') { $self['error'] = "ERROR => Unable to find specified SQL stored on server: {$options['sql']}"; printmsg($self['error'], 2); return array(10, $self['error'] . "\n"); } // Show the contents of the sql query for usage info etc. if ($options['show'] == 'Y') { $text .= $options['sql'] . "\n\n"; return array(0, $text); } // Count how many ?s there are in the sql query. that must match how many sqlopts are passed // if this is an oracle database you could change the ? to a :.. more work on this however needs to be done $qvars = substr_count($options['sql'], '?'); // loop through the options based on how many qvars are in the sql statement. print an error if we didnt // get a variable to use in the sql statement for ($i = 1; $i <= $qvars; $i++) { if (!array_key_exists($i, $options)) { $self['error'] = "ERROR => You did not supply a value for bind variable {$i}!"; printmsg($self['error'], 2); return array(10, $self['error'] . "\n"); } // assign the variables to sqlopts $sqlopts[$i] = $options[$i]; } // One last check to be sure // Count how many times ? is in the sql statement. there should be that many elements in sqlopts if (count($sqlopts) != $qvars) { $self['error'] = "ERROR => SQL query and bind variable count did not match."; printmsg($self['error'], 2); return array(1, $self['error'] . "\n"); } printmsg("DEBUG => [ona_sql] Running SQL query: {$options['sql']}", 5); // Run the query $rs = $onadb->Execute($options['sql'], $sqlopts); if ($rs === false) { $self['error'] = "ERROR => SQL query failed: " . $onadb->ErrorMsg() . "\n"; return array(2, $self['error']); } $text = ""; $dataarr = array(); // If we got a record, that means they did a select .. display it if ($rs->RecordCount()) { $build_header = 1; $i = 0; // Loop through each record returned by the sql query while (!$rs->EOF) { $i++; $record = $rs->FetchRow(); $dataarr[$i] = $record; // Build the header if we need to if ($build_header == 1 and $options['header'] == 'Y') { $build_header = 0; foreach (array_keys($record) as $key) { $text .= $key . $options['delimiter']; } $text = preg_replace("/{$options['delimiter']}\$/", "", $text); $text .= "\n"; } // Display the row foreach (array_keys($record) as $key) { $text .= $record[$key] . $options['delimiter']; } $text = preg_replace("/{$options['delimiter']}\$/", "", $text); $text .= "\n"; } } else { $text .= "NOTICE => SQL executed successfully - no records returned\n"; } // If we want the recordset returned instead of the text if ($options['dataarray'] == 'Y') { return array(0, $dataarr); } // Unless the user said YES to commit, return a non-zero // exit status so that module_run.php doesn't commit the DB transaction. $return = 1; if ($options['commit'] == 'Y') { $return = 0; } return array($return, $text); }
function vlan_campus_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => vlan_campus_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['campus']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM vlan_campus_display-v{$version} Displays a vlan campus record from the database Synopsis: vlan_campus_display [KEY=VALUE] ... Required: campus=NAME or ID Campus name or ID of the campus display Optional: verbose=[yes|no] Display additional info (DEFAULT: yes) EOM ); } // The formatting rule on campus names is all upper and trim it $options['campus'] = strtoupper(trim($options['campus'])); // If the campus provided is numeric, check to see if it's valid if (is_numeric($options['campus'])) { // See if it's an vlan_campus_id list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $options['campus'])); if (!$campus['id']) { printmsg("DEBUG => Unable to find campus using the ID {$options['campus']}!", 3); $self['error'] = "ERROR => Unable to find campus using the ID {$options['campus']}!"; return array(2, $self['error'] . "\n"); } } else { list($status, $rows, $campus) = ona_get_vlan_campus_record(array('name' => $options['campus'])); if (!$campus['id']) { $self['error'] = "ERROR => Unable to find campus using the name {$options['campus']}!"; printmsg("DEBUG => Unable to find campus using the name {$options['campus']}!", 3); return array(2, $self['error'] . "\n"); } } printmsg("DEBUG => Found campus: {$campus['name']}", 3); // Build text to return $text = "VLAN CAMPUS RECORD\n"; $text .= format_array($campus); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // vlan record(s) $i = 0; do { list($status, $rows, $vlan) = ona_get_vlan_record(array('vlan_campus_id' => $campus['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED VLAN RECORD ({$i} of {$rows})\n"; $text .= format_array($vlan); } while ($i < $rows); } // Return the success notice return array(0, $text); }
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 tag_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => tag_del({$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['tag']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM tag_del-v{$version} Deletes an tag from the database Synopsis: tag_del [KEY=VALUE] ... Required: tag=ID ID of the tag to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // If the tag provided is numeric, check to see if it's an tag if (is_numeric($options['tag'])) { // See if it's a tag_id list($status, $rows, $tag) = db_get_record($onadb, 'tags', array('id' => $options['tag'])); } if (!$tag['id']) { printmsg("DEBUG => Unable to find tag ({$options['tag']})!", 3); $self['error'] = "ERROR => Unable to find tag ({$options['tag']})!"; return array(2, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!(auth('host_del') or auth('subnet_del'))) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'tags', array('id' => $tag['id'])); if ($status or !$rows) { $self['error'] = "ERROR => tag_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => TAG DELETED: {$tag['name']} from {$tag['type']}[{$tag['reference']}]"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: NAME: {$tag['name']} TYPE: {$tag['type']} REFERENCE: {$tag['reference']} EOL; return array(6, $text); }
function block_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => block_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['block']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM block_display-v{$version} Displays a block record from the database Synopsis: block_display [KEY=VALUE] ... Required: block=NAME or ID Block name or ID of the block display Optional: verbose=[yes|no] Display additional info (DEFAULT: yes) EOM ); } // The formatting rule on block names is all upper and trim it $options['block'] = trim($options['block']); $options['block'] = preg_replace('/\\s+/', '-', $options['block']); $options['block'] = strtoupper($options['block']); // If the block provided is numeric, check to see if it's an block if (is_numeric($options['block'])) { // See if it's an block_id list($status, $rows, $block) = ona_get_block_record(array('id' => $options['block'])); if (!$block['id']) { printmsg("DEBUG => Unable to find block using the ID {$options['block']}!", 3); $self['error'] = "ERROR => Unable to find block using the ID {$options['block']}!"; return array(2, $self['error'] . "\n"); } } else { list($status, $rows, $block) = ona_get_block_record(array('name' => $options['block'])); if (!$block['id']) { $self['error'] = "ERROR => Unable to find block using the name {$options['block']}!"; printmsg("DEBUG => Unable to find block using the name {$options['block']}!", 3); return array(2, $self['error'] . "\n"); } } printmsg("DEBUG => Found block: {$block['name']}", 3); // Build text to return $text = "BLOCK RECORD\n"; $text .= format_array($block); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { $where .= " ip_addr >= " . $block['ip_addr_start'] . " AND ip_addr <= " . $block['ip_addr_end']; list($status, $netrows, $nets) = db_get_records($onadb, 'subnets', $where, "ip_addr"); // subnet record(s) $i = 0; foreach ($nets as $record) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $record['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED SUBNET RECORD ({$i} of {$netrows})\n"; $text .= format_array($subnet); } } // Return the success notice return array(0, $text); }
function nat_del($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => nat_del({$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['natip'] 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 nat_del-v{$version} Delete a NAT entry from an existing IP This will delete the NAT IP interface from the subnet as well. Synopsis: nat_del [KEY=VALUE] ... Required: ip=[address|ID] the IP address or ID of the existing inside interface natip=[address|ID] the IP address or ID of the external NAT entry Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize "options[commit]" (no is the default) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Find the internal interface 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(2, $self['error'] . "\n"); } printmsg("DEBUG => Interface selected: {$options['ip']}", 3); // Find the NAT interface list($status, $rows, $natinterface) = ona_find_interface($options['natip']); if (!$natinterface['id']) { printmsg("DEBUG => The NAT interface specified, {$options['natip']}, does not exist!", 3); $self['error'] = "ERROR => The NAT interface specified, {$options['natip']}, does not exist!"; return array(3, $self['error'] . "\n"); } printmsg("DEBUG => NAT Interface selected: {$options['natip']}", 3); // Check that the two IP addresses are really paired with each other if ($interface['nat_interface_id'] != $natinterface['id']) { $self['error'] = "ERROR => nat_del() The provided IP addresses are not associated with each other for NAT."; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } printmsg("DEBUG => nat_del() calling interface_del() for ip: {$options['natip']}", 3); $natint['interface'] = $natinterface['id']; $natint['commit'] = $options['commit']; list($status, $output) = run_module('interface_del', $natint); if ($status) { return array($status, $output); } $self['error'] .= $output; // update the existing inside interface and remove the old nat_interface_id value list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), array('nat_interface_id' => '0')); if ($status or !$rows) { $self['error'] = "ERROR => nat_del() SQL Query failed to update nat_interface_id for interface: " . $self['error']; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => External NAT entry deleted: {$natinterface['ip_addr_text']} from {$interface['ip_addr_text']}."; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); }