function ona_get_next_id($tablename) { global $onadb, $self; // Debugging printmsg("DEBUG => ona_get_next_id() called", 3); // Find the sequence value for the specified tablename list($status, $rows, $record) = db_get_record($onadb, 'sequences', array('name' => $tablename)); // Init a new sequence when the tablename is not found if (!$rows) { list($status, $rows) = db_insert_record($onadb, 'sequences', array('name' => $tablename, 'seq' => 2)); return 1; } else { // if we did find something increment the sequence in the table $seq_inc = $record['seq'] + 1; list($status, $rows) = db_update_record($onadb, 'sequences', array('name' => $tablename), array('seq' => $seq_inc)); if ($status) { $self['error'] = 'ERROR => ona_get_next_id() Unable to update sequence value!'; printmsg($self['error'], 4); return 0; } // If we got an ID, return it. if ($record['seq'] > 0) { printmsg("DEBUG => ona_get_next_id() Returning ID: " . $record['seq'], 4); return $record['seq']; } else { $self['error'] = 'ERROR => ona_get_next_id() Something went wrong!'; printmsg($self['error'], 4); return 0; } } }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { // Get the device type record before updating (logging) list($status, $rows, $original_type) = ona_get_device_type_record(array('id' => $form['id'])); list($status, $rows) = db_update_record($onadb, 'device_types', array('id' => $form['id']), array('model_id' => $form['model_id'], 'role_id' => $form['role_id'])); if ($status or !$rows) { $self['error'] = "ERROR => device_type_edit update ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { // Return the success notice $self['error'] = "INFO => Device Type UPDATED:{$original_type['id']}"; printmsg($self['error'], 0); // $self['error'] = "INFO => Device Type UPDATED:{$original_type['id']}: DEVICE_TYPE_DESCRIPTION[{$original_type['DEVICE_TYPE_DESCRIPTION']}=>{$form['device_type_description']}]"; // printmsg($self['error'], 0); } } else { $id = ona_get_next_id('device_types'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id() call failed!"; printmsg($self['error'], 0); } else { printmsg("DEBUG => id for new device type: {$id}", 3); list($status, $rows) = db_insert_record($onadb, 'device_types', array('id' => $id, 'model_id' => $form['model_id'], 'role_id' => $form['role_id'])); if ($status or !$rows) { $self['error'] = "ERROR => device_type_edit add ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => Device Type ADDED: {$form['id']} "; printmsg($self['error'], 0); } } } // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed. " . trim($self['error']) . " (Hint: All fields are required!)');"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_device_type_list', xajax.getFormValues('app_device_type_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
function host_modify($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.07'; printmsg("DEBUG => host_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['interface'] and !$options['host'] or !$options['set_host'] and !$options['set_type'] and !$options['set_location'] and !$options['set_notes']) { // 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_modify-v{$version} Modify a host record Synopsis: host_modify [KEY=VALUE] ... Where: host=NAME[.DOMAIN] or ID Select host by hostname or ID or interface=[ID|IP|MAC] Select host by IP or MAC Update: set_type=TYPE or ID Change device/model type or ID set_notes=NOTES Change the textual notes set_location=REF Reference for location set_device=NAME|ID Name or ID of the device this host is associated with EOM ); } // clean up what is passed in $options['interface'] = trim($options['interface']); $options['host'] = trim($options['host']); // // Find the host record we're modifying // // If they provided a hostname / ID let's look it up if ($options['host']) { list($status, $rows, $host) = ona_find_host($options['host']); } else { if ($options['interface']) { // Find an interface record by something in that interface's record list($status, $rows, $interface) = ona_find_interface($options['interface']); if ($status or !$rows) { printmsg("DEBUG => Interface not found ({$options['interface']})!", 3); $self['error'] = "ERROR => Interface not found ({$options['interface']})!"; return array(4, $self['error'] . "\n"); } // Load the associated host record list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); } } // If we didn't get a record then exit if (!$host['id']) { printmsg("DEBUG => Host not found ({$options['host']})!", 3); $self['error'] = "ERROR => Host not found ({$options['host']})!"; return array(4, $self['error'] . "\n"); } // Get related Device record info list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); // // Define the records we're updating // // This variable will contain the updated info we'll insert into the DB $SET = array(); // Set options['set_type']? if ($options['set_type']) { // Find the Device Type ID (i.e. Type) to use list($status, $rows, $device_type) = ona_find_device_type($options['set_type']); if ($status or $rows != 1 or !$device_type['id']) { printmsg("DEBUG => The device type specified, {$options['set_type']}, does not exist!", 3); $self['error'] = "ERROR => The device type specified, {$options['set_type']}, does not exist!"; return array(6, $self['error'] . "\n"); } printmsg("DEBUG => Device type ID: {$device_type['id']}", 3); // Everything looks ok, add it to $SET if it changed... if ($device['device_type_id'] != $device_type['id']) { $SET_DEV['device_type_id'] = $device_type['id']; } } // Set options['set_notes'] (it can be a null string!) if (array_key_exists('set_notes', $options)) { // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters $options['set_notes'] = str_replace('\\=', '=', $options['set_notes']); $options['set_notes'] = str_replace('\\&', '&', $options['set_notes']); // If it changed... if ($host['notes'] != $options['set_notes']) { $SET['notes'] = $options['set_notes']; } } if (array_key_exists('set_device', $options)) { list($status, $rows, $devid) = ona_find_device($options['set_device']); if (!$rows) { printmsg("DEBUG => The device specified, {$options['set_device']}, does not exist!", 3); $self['error'] = "ERROR => The device specified, {$options['set_device']}, does not exist!"; return array(7, $self['error'] . "\n"); } // set the device id if ($host['device_id'] != $devid['id']) { $SET['device_id'] = $devid['id']; } } if (array_key_exists('set_location', $options)) { if (!$options['set_location']) { unset($SET_DEV['location_id']); } else { list($status, $rows, $loc) = ona_find_location($options['set_location']); if (!$rows) { printmsg("DEBUG => The location specified, {$options['set_location']}, does not exist!", 3); $self['error'] = "ERROR => The location specified, {$options['set_location']}, does not exist!"; return array(7, $self['error'] . "\n"); } // If location is changing, then set the variable if ($device['location_id'] != $loc['id']) { $SET_DEV['location_id'] = $loc['id']; } } } // Check permissions if (!auth('host_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Get the host record before updating (logging) $original_host = $host; // Update the host record if necessary if (count($SET) > 0) { list($status, $rows) = db_update_record($onadb, 'hosts', array('id' => $host['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => host_modify() SQL Query failed for host: " . $self['error']; printmsg($self['error'], 0); return array(8, $self['error'] . "\n"); } } // Update device table if necessary if (count($SET_DEV) > 0) { list($status, $rows) = db_update_record($onadb, 'devices', array('id' => $host['device_id']), $SET_DEV); if ($status or !$rows) { $self['error'] = "ERROR => host_modify() SQL Query failed for device type: " . $self['error']; printmsg($self['error'], 0); return array(9, $self['error'] . "\n"); } } // Get the host record after updating (logging) list($status, $rows, $new_host) = ona_get_host_record(array('id' => $host['id'])); // Return the success notice $self['error'] = "INFO => Host UPDATED:{$host['id']}: {$new_host['fqdn']}"; $log_msg = "INFO => Host UPDATED:{$host['id']}: "; $more = ""; foreach (array_keys($host) as $key) { if ($host[$key] != $new_host[$key]) { $log_msg .= "{$more}{$key}: {$host[$key]} => {$new_host[$key]}"; $more = "; "; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $self['error'] . "\n"); }
function dns_record_del($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => dns_record_del({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.03'; // 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['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_del-v{$version} Deletes a DNS record from the database Synopsis: dns_record_del [KEY=VALUE] ... Required: name=NAME[.DOMAIN] or ID hostname or ID of the record to delete type=TYPE record type (A,CNAME,PTR...) Optional: ip=ADDRESS ip address (numeric or dotted) commit=[yes|no] commit db transaction (no) EOM ); } /* thoughts on the flow of things: A records: remove any CNAMES using this A record remove any PTR records using this A record test that it is not a primary_dns_id, if it is, it must be reassigned should make a find_dns_record(s) function. a find by host option would be good. need to do a better delete of DNS records when deleting a host.. currently its a problem. MP: TODO: this delete will not handle DNS views unless you use the ID of the record to delete. add a view option at some point. */ // If the name we were passed has a leading . in it then remove the dot. $options['name'] = preg_replace("/^\\./", '', $options['name']); // FIXME: MP Fix this to use a find_dns_record function ID only for now // Find the DNS record from $options['name'] list($status, $rows, $dns) = ona_find_dns_record($options['name'], $options['type']); printmsg("DEBUG => dns_record_del() DNS record: {$options['name']}", 3); if (!$dns['id']) { printmsg("DEBUG => Unknown DNS record: {$options['name']} ({$options['type']})", 3); $self['error'] = "ERROR => Unknown DNS record: {$options['name']} ({$options['type']})"; return array(2, $self['error'] . "\n"); } // Check permissions if (!auth('host_del') or !authlvl($host['LVL'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // If "commit" is yes, delete the host if ($options['commit'] == 'Y') { $text = ""; $add_to_error = ""; // SUMMARY: // Display any associated PTR records for an A record // Display any associated CNAMEs for an A record // Test if it is used as a primary_dns_id unless it is the host_del module calling if (!isset($options['delete_by_module'])) { list($status, $rows, $srecord) = db_get_record($onadb, 'hosts', array('primary_dns_id' => $dns['id'])); if ($rows) { $self['error'] = "ERROR => dns_record_del() The DNS record, {$dns['name']}.{$dns['domain_fqdn']}[{$dns['id']}], is a primary A record for a host! You can not delete it until you associate a new primary record, or delete the host."; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } } // Delete related Points to records // get list for logging list($status, $rows, $records) = db_get_records($onadb, 'dns', array('dns_id' => $dns['id'])); // do the delete list($status, $rows) = db_delete_records($onadb, 'dns', array('dns_id' => $dns['id'])); if ($status) { $self['error'] = "ERROR => dns_record_del() Child record delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } if ($rows) { // log deletions // FIXME: do better logging here printmsg("INFO => {$rows} child DNS record(s) DELETED from {$dns['fqdn']}", 0); $add_to_error .= "INFO => {$rows} child record(s) DELETED from {$dns['fqdn']}\n"; } // TRIGGER: flag the domains for rebuild foreach ($records as $record) { list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $record['domain_id']), array('rebuild_flag' => 1)); if ($status) { $self['error'] = "ERROR => dns_record_del() Unable to update rebuild flags for domain.: {$self['error']}"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } } // Delete the DNS record list($status, $rows) = db_delete_records($onadb, 'dns', array('id' => $dns['id'])); if ($status) { $self['error'] = "ERROR => dns_record_del() DNS record delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $add_to_error . $self['error'] . "\n"); } // TRIGGER: flag the current dnsrecords domain for rebuild list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $dns['domain_id']), array('rebuild_flag' => 1)); if ($status) { $self['error'] = "ERROR => dns_record_del() Unable to update rebuild flags for domain.: {$self['error']}"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } // FIXME: if it is a NS or something display a proper FQDN message here // Display proper PTR information if ($dns['type'] == 'PTR') { list($status, $rows, $pointsto) = ona_get_dns_record(array('id' => $dns['dns_id']), ''); list($status, $rows, $ptrint) = ona_get_interface_record(array('id' => $dns['interface_id']), ''); $ipflip = ip_mangle($ptrint['ip_addr'], 'flip'); $octets = explode(".", $ipflip); if (count($octets) > 4) { $arpa = '.ip6.arpa'; $octcount = 31; } else { $arpa = '.in-addr.arpa'; $octcount = 3; } $dns['fqdn'] = "{$ipflip}{$arpa} -> {$pointsto['fqdn']}"; } // Return the success notice $self['error'] = "INFO => DNS {$dns['type']} record DELETED: {$dns['fqdn']}"; printmsg($self['error'], 0); return array(0, $add_to_error . $self['error'] . "\n"); } // // We are just displaying records that would have been deleted // // SUMMARY: // Display any associated PTR records for an A record // Display any associated CNAMEs for an A record // Otherwise just display the host record for the host we would have deleted $text = "Record(s) NOT DELETED (see \"commit\" option)\n" . "Displaying record(s) that would have been deleted:\n"; // Test if it is used as a primary_dns_id list($status, $rows, $srecord) = db_get_record($onadb, 'hosts', array('primary_dns_id' => $dns['id'])); if ($rows) { $text .= "\nWARNING! This DNS record is a primary A record for a host\n"; } // Display the complete dns record list($status, $tmp) = dns_record_display("name={$dns['id']}&verbose=N"); $text .= "\n" . $tmp; // Display associated Child records list($status, $rows, $records) = db_get_records($onadb, 'dns', array('dns_id' => $dns['id'])); if ($rows) { $text .= "\nASSOCIATED POINTS-TO RECORDS ({$rows}):\n"; } foreach ($records as $record) { if ($record['type'] == 'NS') { $record['name'] = ''; } // FIXME:I could fix this but I'm lazy if ($record['type'] == 'PTR') { $record['name'] = '??'; } list($status, $rows, $domain) = ona_get_domain_record(array('id' => $record['domain_id']), ''); $text .= " {$record['type']}: {$record['name']}.{$domain['fqdn']} -> {$dns['fqdn']}\n"; } return array(7, $text); }
function sess_write($key, $value) { global $SESS_DBH, $SESS_LIFE; //printmsg("sess_write($key, $value) called", 6); $expiry = time() + $SESS_LIFE; // Try inserting the value into the DB list($status, $rows) = db_insert_record($SESS_DBH, 'sessions', array('sesskey' => $key, 'expiry' => $expiry, 'sessvalue' => $value)); // If the insert failed try an update if (!$status or $rows == 0) { list($status, $rows) = db_update_record($SESS_DBH, 'sessions', array('sesskey' => $key), array('expiry' => $expiry, 'sessvalue' => $value)); } return $rows; }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { // Get the option record before updating (logging) list($status, $rows, $original_option) = ona_get_dhcp_option_record(array('id' => $form['id'])); list($status, $rows) = db_update_record($onadb, 'dhcp_options', array('id' => $form['id']), array('display_name' => $form['display_name'], 'type' => $form['type'], 'number' => $form['number'], 'name' => $form['name'])); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_option update ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { // Get the record after updating (logging) list($status, $rows, $new_option) = ona_get_dhcp_option_record(array('id' => $form['id'])); // Return the success notice $self['error'] = "INFO => DHCP Option UPDATED:{$new_option['id']}: {$new_option['name']}"; $log_msg = "INFO => DHCP Option UPDATED:{$new_option['id']}: "; $more = ""; foreach (array_keys($original_option) as $key) { if ($original_option[$key] != $new_option[$key]) { $log_msg .= $more . $key . "[" . $original_option[$key] . "=>" . $new_option[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } } } else { $id = ona_get_next_id('dhcp_options'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id() call failed!"; printmsg($self['error'], 0); } else { printmsg("DEBUG => ID for new dhcp option: {$id}", 3); list($status, $rows) = db_insert_record($onadb, "dhcp_options", array('id' => $id, 'display_name' => $form['display_name'], 'type' => $form['type'], 'number' => $form['number'], 'name' => $form['name'])); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_option_edit add ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => DHCP Option ADDED: {$form['name']} "; printmsg($self['error'], 0); } } } // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed. " . trim($self['error']) . " (Hint: All fields are required!)');"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_dhcp_option_list', xajax.getFormValues('app_dhcp_option_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
/** * Return user info [ MUST BE OVERRIDDEN ] or false * * Returns info about the given user needs to contain * at least these fields: * * username string name of the user * grps array list of groups the user is in * $user['grps']['groupname']=groupidnum * * sets a variable ($this->founduser) to show if a user was * found by this function * * @author Matt Pascoe <*****@*****.**> * @return array containing user data or false */ function getUserData($login_name) { global $onadb; list($status, $rows, $user) = db_get_record($onadb, 'users', "username LIKE '{$login_name}'"); if (!$rows) { $this->founduser = false; return false; } else { $this->founduser = true; // Update the access time for the user db_update_record($onadb, 'users', array('id' => $user['id']), array('atime' => date_mangle(time()))); // Load the user's groups list($status, $rows, $records) = db_get_records($onadb, 'group_assignments', array('user_id' => $user['id'])); foreach ($records as $record) { list($status, $rows, $group) = db_get_record($onadb, 'groups', array('id' => $record['group_id'])); $user['grps'][$group['name']] = $group['id']; if ($group['level'] > $user['level']) { $user['level'] = $group['level']; } } return $user; } }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('user_admin')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Validate input if (!$form['name']) { $js .= "alert('Error! All fields are required!');"; $response->addScript($js); return $response->getXML(); } if (!preg_match('/^[A-Za-z0-9.\\-_ ]+$/', $form['name'])) { $js .= "alert('Invalid group name! Valid characters: A-Z 0-9 .-_ and space');"; $response->addScript($js); return $response->getXML(); } //MP: zero out the level for now //TODO: fix or remove level at some point $form['level'] = 0; // Create a new record? if (!$form['id']) { list($status, $rows) = db_insert_record($onadb, 'groups', array('name' => $form['name'], 'description' => $form['description'], 'level' => $form['level'])); if ($status or !$rows) { $self['error'] = "ERROR => group_edit add ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => Group ADDED: {$form['name']} "; printmsg($self['error'], 0); } } else { list($status, $rows, $record) = db_get_record($onadb, 'groups', array('id' => $form['id'])); if ($rows != 1 or $record['id'] != $form['id']) { $js .= "alert('Error! The record requested could not be loaded from the database!');"; $response->addScript($js); return $response->getXML(); } list($status, $rows) = db_update_record($onadb, 'groups', array('id' => $form['id']), array('name' => $form['name'], 'description' => $form['description'])); if ($status or !$rows) { $self['error'] = "ERROR => group_edit update ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { list($status, $rows, $new_record) = db_get_record($onadb, 'groups', array('id' => $form['id'])); // Return the success notice $self['error'] = "INFO => Group UPDATED:{$record['id']}: {$record['name']}"; $log_msg = "INFO => Group UPDATED:{$record['id']}: "; $more = ""; foreach (array_keys($record) as $key) { if ($record[$key] != $new_record[$key]) { $log_msg .= $more . $key . "[" . $record[$key] . "=>" . $new_record[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } } } // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed. Contact the webmaster if this problem persists.');"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_group_list', xajax.getFormValues('app_group_list_filter_form'), 'display_list');"; } // Insert the new table into the window $response->addScript($js); return $response->getXML(); }
function vlan_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => vlan_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['vlan'] or !($options['set_name'] or $options['set_number'] or $options['set_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_modify-v{$version} Modifies an vlan entry in the database Synopsis: vlan_modify [KEY=VALUE] ... Where: vlan=ID vlan ID Update: set_name=NAME change vlan name set_number=NUMBER change vlan number set_campus=NAME or ID change campus the vlan belongs to EOM ); } // The formatting rule on vlan names/campus names is all upper and trim it, spaces to - $options['set_name'] = strtoupper(trim($options['set_name'])); $options['set_name'] = preg_replace('/\\s+/', '-', $options['set_name']); $options['set_campus'] = strtoupper(trim($options['set_campus'])); $options['set_number'] = trim($options['set_number']); // Load the record we will be modifying list($status, $rows, $vlan) = ona_get_vlan_record(array('id' => $options['vlan'])); // Validate that we got a record back, or return an error if (!$vlan['id']) { printmsg("DEBUG => The VLAN ID specified ({$options['vlan']}) does not exist!", 3); $self['error'] = "ERROR => The VLAN ID specified, {$options['vlan']}, does not exist!"; return array(2, $self['error'] . "\n"); } printmsg("DEBUG => Found VLAN: {$vlan['name']}", 3); // This variable will contain the updated info we'll insert into the DB $SET = array(); // If they are specifying a new name, process it. if ($options['set_name']) { // Validate that there isn't already an vlan with this name $options['set_name'] = strtoupper(trim($options['set_name'])); list($status, $rows, $record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$vlan['vlan_campus_id']} AND name LIKE '{$options['set_name']}' AND number != {$vlan['number']}"); if ($status or $rows) { printmsg("DEBUG => The VLAN ({$options['set_name']}) already exists on this campus!", 3); $self['error'] = "ERROR => The VLAN {$options['set_name']} already exists on this campus!"; return array(4, $self['error'] . "\n"); } if ($vlan['name'] != $options['set_name']) { $SET['name'] = $options['set_name']; } } if ($options['set_number']) { // Validate that there isn't already an vlan on this campus with this vlan number list($status, $rows, $record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$vlan['vlan_campus_id']} AND number = {$options['set_number']} AND name NOT LIKE '{$vlan['name']}'"); if ($status or $rows) { printmsg("DEBUG => The VLAN with the number ({$options['set_number']}) already exists on this campus!", 3); $self['error'] = "ERROR => The vlan with the number {$options['set_number']} already exists on this campus!"; return array(3, $self['error'] . "\n"); } // Add the new info to $SET if ($vlan['number'] != $options['set_number']) { $SET['number'] = $options['set_number']; } } // FIXME: yes I'm lazy.. test that the new campus does not have the vlan name or number already on it. // If they are changing the campus the vlan points to, process it if ($options['set_campus']) { $options['set_campus'] = strtoupper(trim($options['set_campus'])); if (is_numeric($options['set_campus'])) { list($status, $rows, $record) = ona_get_vlan_campus_record(array('id' => $options['set_campus'])); } if (!array_key_exists('id', $record)) { list($status, $rows, $record) = ona_get_vlan_campus_record(array('name' => $options['set_campus'])); } // Make sure that worked - or return an error if (!$record['id']) { printmsg("DEBUG => The campus ({$options['set_campus']}) does not exist!", 3); $self['error'] = "ERROR => The campus specified, {$options['set_campus']}, does not exist!"; return array(5, $self['error'] . "\n"); } // test that the new campus does not have the vlan name or number already on it. // only check if the campus has changed if ($record['id'] != $vlan['vlan_campus_id']) { // build where clause for checking the new campus for the vlan name/number $where = ''; $OR = ''; if (array_key_exists('number', $SET)) { $where .= " number = {$SET['number']} "; $OR = " OR "; } if (array_key_exists('name', $SET)) { $where .= "{$OR} name LIKE '{$SET['name']}' "; } list($status, $rows, $new_campus_record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$record['id']} AND ({$where})"); if ($rows > 0) { printmsg("DEBUG => The campus ({$options['set_campus']}) already contains this VLAN name or number ({$SET['name']} {$SET['number']})!", 3); $self['error'] = "ERROR => The campus specified, {$options['set_campus']}, already contains this VLAN name or number ({$SET['name']} {$SET['number']})!"; return array(7, $self['error'] . "\n"); } } // Add the new info to $SET if ($vlan['vlan_campus_id'] != $record['id']) { $SET['vlan_campus_id'] = $record['id']; } } // Check permissions if (!auth('vlan_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } if ($SET) { // Update the record list($status, $rows) = db_update_record($onadb, 'vlans', array('id' => $vlan['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => vlan_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } } // Get the VLAN record after updating (logging) list($status, $rows, $new_vlan) = ona_get_vlan_record(array('id' => $options['vlan'])); list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $new_vlan['vlan_campus_id'])); $text = <<<EOL NAME: {$new_vlan['name']} NUMBER: {$new_vlan['number']} CAMPUS: {$campus['name']} EOL; // Return the success notice $renamed = ''; if ($new_vlan['name'] != $vlan['name']) { $renamed .= "{$vlan['name']} => {$new_vlan['name']} "; } if ($new_vlan['number'] != $vlan['number']) { $renamed .= "VLAN Num {$vlan['number']} => {$new_vlan['number']} "; } if ($new_vlan['vlan_campus_id'] != $vlan['vlan_campus_id']) { $renamed .= "Campus ID {$vlan['vlan_campus_id']} => {$new_vlan['vlan_campus_id']}"; } $self['error'] = "INFO => VLAN UPDATED: {$renamed}"; return array(0, $self['error'] . "\n {$text}"); }
function ws_save($window_name, $form = '') { global $conf, $self, $mysql; // Make sure they have permission if (!auth('admin')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Don't allow this in the demo account! if ($_SESSION['auth']['client']['url'] == 'demo') { $response = new xajaxResponse(); $response->addScript("alert('Feature disabled in this demo!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Make sure they're logged in if (!loggedIn()) { return $response->getXML(); } // Validate input if (!$form['fname'] or !$form['lname'] or !$form['username']) { $js .= "alert('Error! First name, last name, and username are required fields!');"; $response->addScript($js); return $response->getXML(); } if (!$form['id'] and !$form['passwd']) { $js .= "alert('Error! A password is required to create a new employee!');"; $response->addScript($js); return $response->getXML(); } // Usernames are stored in lower case $form['username'] = strtolower($form['username']); // md5sum the password if there is one if ($form['passwd']) { $form['passwd'] = md5($form['passwd']); } // Create a new record? if (!$form['id']) { list($status, $rows) = db_insert_record($mysql, 'users', array('client_id' => $_SESSION['auth']['client']['id'], 'active' => 1, 'fname' => $form['fname'], 'lname' => $form['lname'], 'username' => $form['username'], 'passwd' => $form['passwd'], 'ctime' => date_mangle(time()), 'mtime' => date_mangle(time()))); printmsg("NOTICE => Added new user: {$form['username']} client url: {$_SESSION['auth']['client']['url']}", 0); } else { list($status, $rows, $record) = db_get_record($mysql, 'users', array('id' => $form['id'], 'client_id' => $_SESSION['auth']['client']['id'])); if ($rows != 1 or $record['id'] != $form['id']) { $js .= "alert('Error! The record requested could not be loaded from the database!');"; $response->addScript($js); return $response->getXML(); } if (strlen($form['passwd']) < 32) { $form['passwd'] = $record['passwd']; } list($status, $rows) = db_update_record($mysql, 'users', array('id' => $form['id']), array('fname' => $form['fname'], 'lname' => $form['lname'], 'username' => $form['username'], 'passwd' => $form['passwd'], 'mtime' => date_mangle(time()), 'active' => 1)); printmsg("NOTICE => Updated user: {$form['username']} client url: {$_SESSION['auth']['client']['url']}", 0); } // If the module returned an error code display a popup warning if ($status) { printmsg("ERROR => User add/edit failed! {$self['error']}", 0); $js .= "alert('Save failed. Contact the webmaster if this problem persists.');"; $response->addScript($js); return $response->getXML(); } $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('user_list', xajax.getFormValues('user_list_filter_form'), 'display_list');"; // Handle the "admin" flag list($status, $rows, $user) = db_get_record($mysql, 'users', array('username' => $form['username'], 'client_id' => $_SESSION['auth']['client']['id'], 'active' => 1)); list($status, $rows, $perm) = db_get_record($mysql, 'permissions', array('name' => 'admin')); list($status, $rows, $acl) = db_get_record($mysql, 'acl', array('user_id' => $user['id'], 'perm_id' => $perm['id'])); if ($form['admin'] and !$acl['id'] and $user['id'] and $perm['id']) { // Give the user the permission list($status, $rows) = db_insert_record($mysql, 'acl', array('user_id' => $user['id'], 'perm_id' => $perm['id'])); } else { if (!$form['admin'] and $acl['id'] and $user['id'] and $perm['id'] and $_SESSION['auth']['user']['id'] != $user['id']) { // Take the permission away, UNLESS THEY ARE TRYING TO MODIFY THEIR OWN ACCOUNT! list($status, $rows) = db_delete_record($mysql, 'acl', array('user_id' => $user['id'], 'perm_id' => $perm['id'])); } else { if ($_SESSION['auth']['user']['id'] == $user['id']) { // IF they did try to remove their own admin status, give them a popup and tell them they can't do that. $js .= "alert('WARNING => You can\\'t change your own admin status!');"; } } } // Insert the new table into the window $response->addScript($js); return $response->getXML(); }
function location_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => location_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['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 location_modify-v{$version} Modifies an existing location entry in the database Synopsis: location_modify [KEY=VALUE] ... Where: reference=STRING or ID location reference or ID Update: set_reference=NAME change location reference set_name=NAME change location name set_address=STRING set_city=STRING set_state=STRING set_zip_code=NUMBER set_latitude=STRING set_longitude=STRING set_misc=STRING EOM ); } // See if it's an vlan_campus_id list($status, $rows, $loc) = ona_find_location($options['reference']); if (!$loc['id']) { printmsg("DEBUG => Unable to find location using: {$options['reference']}!", 3); $self['error'] = "ERROR => Unable to find location using: {$options['reference']}!"; return array(1, $self['error'] . "\n"); } printmsg("DEBUG => Found location: {$loc['reference']}", 3); // This variable will contain the updated info we'll insert into the DB $SET = array(); if ($loc['reference'] != $options['set_reference']) { $SET['reference'] = $options['set_reference']; $msg .= "INFO => Location UPDATED reference: {$loc['reference']} => {$options['set_reference']}\n"; } // If they are specifying a new name, process it. if ($loc['name'] != $options['set_name']) { $SET['name'] = $options['set_name']; $msg .= "INFO => Location UPDATED name: {$loc['name']} => {$options['set_name']}\n"; } if ($loc['address'] != $options['set_address']) { $SET['address'] = $options['set_address']; $msg .= "INFO => Location UPDATED address: {$loc['address']} => {$options['set_address']}\n"; } if ($loc['city'] != $options['set_city']) { $SET['city'] = $options['set_city']; $msg .= "INFO => Location UPDATED city: {$loc['city']} => {$options['set_city']}\n"; } if ($loc['state'] != $options['set_state']) { $SET['state'] = $options['set_state']; $msg .= "INFO => Location UPDATED state: {$loc['state']} => {$options['set_state']}\n"; } if ($loc['zip_code'] != $options['set_zip_code']) { $SET['zip_code'] = $options['set_zip_code']; $msg .= "INFO => Location UPDATED zip_code: {$loc['zip_code']} => {$options['set_zip_code']}\n"; } if ($loc['latitude'] != $options['set_latitude']) { $SET['latitude'] = $options['set_latitude']; $msg .= "INFO => Location UPDATED latitude: {$loc['latitude']} => {$options['set_latitude']}\n"; } if ($loc['longitude'] != $options['set_longitude']) { $SET['longitude'] = $options['set_longitude']; $msg .= "INFO => Location UPDATED longitude: {$loc['longitude']} => {$options['set_longitude']}\n"; } if ($loc['misc'] != $options['set_misc']) { $SET['misc'] = $options['set_misc']; $msg .= "INFO => Location UPDATED misc: {$loc['misc']} => {$options['set_misc']}\n"; } if (!$SET) { $self['error'] = "ERROR => You did not update anything."; printmsg($self['error'], 1); return array(2, $self['error'] . "\n"); } // Check permissions if (!auth('location_add')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(2, $self['error'] . "\n"); } // Update the record list($status, $rows) = db_update_record($onadb, 'locations', array('id' => $loc['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => location_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(3, $self['error'] . "\n"); } // Return the success notice $self['error'] = $msg; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); }
function ws_enablerecord($window_name, $form = '') { global $include, $conf, $self, $onadb; // Check permissions if (!auth('dns_record_modify')) { $response = new xajaxResponse(); $response->addScript("alert('Failed to enable record: 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); // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; $SET = array(); $SET['ebegin'] = date('Y-m-j G:i:s', time()); // Do the actual update list($status, $rows) = db_update_record($onadb, 'dns', array('id' => $form['dns_record_id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => enablerecord() SQL Query failed to update dnsrecord: " . $self['error']; printmsg($self['error'], 0); $js .= "alert('Enable DNS record failed. " . preg_replace('/[\\s\']+/', ' ', $self['error']) . "');"; } else { if ($form['js']) { // Hardcoding so that it always refreshes the display host page. //$js .= "xajax_window_submit('work_space', 'xajax_window_submit(\'{$window_name}\',\'host_id=>{$form['host_id']}\', \'display\')');"; $js .= $form['js']; } } // Return an XML response $response->addScript($js); return $response->getXML(); }
function domain_modify($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => domain_modify({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.05'; // 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'] and ($options['set_admin'] or $options['set_name'] or $options['set_primary_master'] or $options['set_refresh'] or $options['set_retry'] or $options['set_expiry'] or $options['set_minimum'] or $options['set_ttl'] or $options['set_parent']))) { // 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_modify-v{$version} Modifies a DNS domain in the database Synopsis: domain_modify [KEY=VALUE] ... Where: domain=STRING or ID full name of domain (i.e. name.something.com) Optional: set_name=STRING new domain name set_admin=STRING Default ({$conf['dns_admin_email']}) set_primary_master=STRING Default ({$conf['dns_primary_master']}) set_refresh=NUMBER Default ({$conf['dns_refresh']}) set_retry=NUMBER Default ({$conf['dns_retry']}) set_expiry=NUMBER Default ({$conf['dns_expiry']}) set_minimum=NUMBER Default ({$conf['dns_minimum']}) set_ttl=NUMBER Default ({$conf['dns_default_ttl']}) set_parent=DOMAIN_NAME Default ({$conf['dns_parent']}) EOM ); } $options['domain'] = trim($options['domain']); $options['set_name'] = trim($options['set_name']); $options['set_parent'] = trim($options['set_parent']); $options['set_admin'] = trim($options['set_admin']); $domainsearch = array(); // setup a domain search based on name or id if (is_numeric($options['domain'])) { $domainsearch['id'] = $options['domain']; } else { $domainsearch['name'] = $options['domain']; } // Determine the entry itself exists list($status, $rows, $entry) = ona_get_domain_record($domainsearch); // Test to see that we were able to find the specified record if (!$entry['id']) { printmsg("DEBUG => Unable to find a domain record using ID {$options['domain']}!", 3); $self['error'] = "ERROR => Unable to find the domain record using {$options['domain']}!"; return array(4, $self['error'] . "\n"); } printmsg("DEBUG => domain_modify(): Found entry, {$entry['name']}", 3); // This variable will contain the updated info we'll insert into the DB $SET = array(); if (array_key_exists('set_parent', $options) and $options['set_parent']) { $parentsearch = array(); // setup a domain search based on name or id if (is_numeric($options['set_parent'])) { $parentsearch['id'] = $options['set_parent']; } else { $parentsearch['name'] = $options['set_parent']; } // Determine the host is valid list($status, $rows, $domain) = ona_get_domain_record($parentsearch); if (!$domain['id']) { printmsg("DEBUG => The parent domain specified ({$options['set_parent']}) does not exist!", 3); $self['error'] = "ERROR => The parent domain specified ({$options['set_parent']}) does not exist!"; return array(2, $self['error'] . "\n"); } if ($entry['parent_id'] != $domain['id']) { $SET['parent_id'] = $domain['id']; } } else { if ($entry['parent_id'] != 0) { $SET['parent_id'] = 0; } } // FIXME: currently renaming zones may not work when using // parent zones. https://github.com/opennetadmin/ona/issues/36 if (is_string($options['set_name'])) { // trim leading and trailing whitespace from 'value' if ($entry['name'] != trim($options['set_name'])) { $SET['name'] = trim($options['set_name']); } // Determine the entry itself exists list($status, $rows, $domain) = ona_get_domain_record(array('name' => $options['set_name'])); // Test to see that the new entry isnt already used if ($domain['id'] and $domain['id'] != $entry['id']) { printmsg("DEBUG => The domain specified ({$options['set_name']}) already exists!", 3); $self['error'] = "ERROR => The domain specified ({$options['set_name']}) already exists!"; return array(6, $self['error'] . "\n"); } } // define the remaining entries if ($options['set_primary_master'] and $entry['primary_master'] != $options['set_primary_master']) { $SET['primary_master'] = trim($options['set_primary_master']); } if ($options['set_admin'] and $entry['admin_email'] != $options['set_admin']) { $SET['admin_email'] = $options['set_admin']; } if ($options['set_refresh'] and $entry['refresh'] != $options['set_refresh']) { $SET['refresh'] = $options['set_refresh']; } if ($options['set_retry'] and $entry['retry'] != $options['set_retry']) { $SET['retry'] = $options['set_retry']; } if ($options['set_expiry'] and $entry['expiry'] != $options['set_expiry']) { $SET['expiry'] = $options['set_expiry']; } if ($options['set_minimum'] and $entry['minimum'] != $options['set_minimum']) { $SET['minimum'] = $options['set_minimum']; } if ($options['set_ttl'] and $entry['default_ttl'] != $options['set_ttl']) { $SET['default_ttl'] = $options['set_ttl']; } // FIXME: MP for now this is removed. it is a chicken/egg issue on setting this name // Also it cant use find_host as the name is not always primary. /* if ($SET['primary_master']) { // Determine if the primary master is a valid host list($status, $rows, $host) = ona_find_host($SET['primary_master']); if (!$host['id']) { printmsg("DEBUG => The primary master host specified ({$SET['primary_master']}) does not exist!",3); $self['error'] = "ERROR => The primary master host specified ({$SET['primary_master']}) does not exist!"; return(array(2, $self['error'] . "\n")); } } */ // come up with a serial_number // Calculate a serial based on time // concatinate year,month,day,percentage of day // FIXME: MP this needs more work to be more accurate. maybe not use date.. pretty limiting at 10 characters as suggested here: http://www.zytrax.com/books/dns/ch8/soa.html // for now I'm going with non zero padded(zp) month,zp day, zp hour, zp minute, zp second. The only issue I can see at this point with this is when it rolls to january.. // will that be too much of an increment for it to properly zone xfer? i.e. 1209230515 = 12/09 23:05:15 in time format // MP: FOR NOW SERIAL WONT EVER GET USED... LEFT IT IN HERE FOR AWHILE THOUGH //$SET['serial'] = date('njHis'); // Serial numbers are now built based on the timeformat // Check permissions if (!auth('advanced')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Get the domain record before updating (logging) list($status, $rows, $original_domain) = ona_get_domain_record(array('id' => $entry['id'])); // Update the record if (count($SET) > 0) { list($status, $rows) = db_update_record($onadb, 'domains', array('id' => $entry['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => domain_modify() SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } } // Get the entry again to display details list($status, $rows, $new_domain) = ona_get_domain_record(array('id' => $entry['id'])); // Return the success notice $self['error'] = "INFO => Domain UPDATED:{$entry['id']}: {$new_domain['name']}"; $log_msg = "INFO => Domain UPDATED:{$entry['id']}: "; $more = ""; foreach (array_keys($original_domain) as $key) { if ($original_domain[$key] != $new_domain[$key]) { $log_msg .= $more . $key . "[" . $original_domain[$key] . "=>" . $new_domain[$key] . "]"; $more = ";"; } } // TRIGGER:Now that we have updated the domain, lets mark the domain on all the servers for a rebuild to pick up any new SOA info. list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $entry['id']), array('rebuild_flag' => 1)); if ($status) { $self['error'] = "ERROR => domain_modify() Unable to update rebuild flags for domain. SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $self['error'] . "\n"); }
function subnet_modify($options = "") { global $conf, $self, $onadb; //printmsg('DEBUG => subnet_modify('.implode (";",$options).') called', 3); // Version - UPDATE on every edit! $version = '1.08'; // 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'] or !($options['set_ip'] or $options['set_netmask'] or $options['set_type'] or $options['set_name'] or array_key_exists('set_vlan', $options) or $options['set_security_level'])) { // 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_modify-v{$version} Modify a subnet (subnet) record Synopsis: subnet_modify [KEY=VALUE] ... Where: subnet=[ID|IP] select subnet by search string Update: set_ip=IP change subnet "subnet" address set_netmask=MASK change subnet netmask set_name=TEXT change subnet name (i.e. "LAN-1234") set_type=TYPE change subnet type by name or id set_vlan=VLAN change vlan by name, number campus=CAMPUS vlan campus name or id to help identify vlan set_security_level=LEVEL numeric security level ({$conf['ona_lvl']}) EOM ); } $check_boundaries = 0; // Find the subnet record we're modifying list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); if ($status or !$rows) { $self['error'] = "ERROR => Subnet not found"; return array(2, $self['error'] . "\n"); } // Check permissions if (!auth('subnet_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(3, $self['error'] . "\n"); } // Validate the ip address if (!$options['set_ip']) { $options['set_ip'] = $subnet['ip_addr']; } else { $check_boundaries = 1; $options['set_ip'] = $setip = ip_mangle($options['set_ip'], 'numeric'); // FIXME: what if ip_mangle returns a GMP object? if ($options['set_ip'] == -1) { $self['error'] = "ERROR => The IP address specified is invalid!"; return array(4, $self['error'] . "\n"); } } // Validate the netmask is okay if (!$options['set_netmask']) { $options['set_netmask'] = $subnet['ip_mask']; $cidr = ip_mangle($options['set_netmask'], 'cidr'); } else { $check_boundaries = 1; $cidr = ip_mangle($options['set_netmask'], 'cidr'); // FIXME: what if ip_mangle returns a GMP object? $options['set_netmask'] = ip_mangle($options['set_netmask'], 'numeric'); if ($cidr == -1 or $options['set_netmask'] == -1) { $self['error'] = "ERROR => The netmask specified is invalid!"; return array(5, $self['error'] . "\n"); } } if (is_ipv4($setip)) { $padding = 32; $fmt = 'dotted'; $ip1 = ip_mangle($setip, 'binary'); $num_hosts = 0xffffffff - $options['set_netmask']; $first_host = $options['set_ip'] + 1; $last_host = $options['set_ip'] + $num_hosts; $str_last_host = $last_host; $last_last_host = $last_host - 1; } else { $padding = 128; $fmt = 'ipv6gz'; $ip1 = ip_mangle($setip, 'bin128'); $first_host = gmp_strval(gmp_add($options['set_ip'], 1)); $sub = gmp_sub("340282366920938463463374607431768211455", $options['set_netmask']); $last_host = gmp_add($options['set_ip'], $sub); $str_last_host = gmp_strval($last_host); $last_last_host = gmp_strval(gmp_sub($last_host, 1)); } // Validate that the subnet IP & netmask combo are valid together. $ip2 = str_pad(substr($ip1, 0, $cidr), $padding, '0'); $ip1 = ip_mangle($ip1, $fmt); $ip2 = ip_mangle($ip2, $fmt); if ($ip1 != $ip2) { $self['error'] = "ERROR => Invalid subnet specified - did you mean: {$ip2}/{$cidr}?"; return array(6, $self['error'] . "\n"); } // If our IP or netmask changed we need to make sure that // we won't abandon any host interfaces. // We also need to verify that the new boundaries are valid and // don't interefere with any other subnets. if ($check_boundaries == 1) { // *** Check to see if the new subnet overlaps any existing ONA subnets *** // // I convert the IP address to dotted format when calling ona_find_subnet() // because it saves it from doing a few unnecessary sql queries. // Look for overlaps like this (where new subnet address starts inside an existing subnet): // [ -- new subnet -- ] // [ -- old subnet --] list($status, $rows, $record) = ona_find_subnet(ip_mangle($options['set_ip'], 'dotted')); if ($rows and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet starts inside an existing subnet."; return array(7, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for overlaps like this (where the new subnet ends inside an existing subnet): // [ -- new subnet -- ] // [ -- old subnet --] // Find last address of our subnet, and see if it's inside of any other subnet: list($status, $rows, $record) = ona_find_subnet(ip_mangle($str_last_host, 'dotted')); if ($rows and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet ends inside an existing subnet."; return array(8, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for overlaps like this (where the new subnet entirely overlaps an existing subnet): // [ -------- new subnet --------- ] // [ -- old subnet --] // // Do a cool SQL query to find all subnets whose start address is >= or <= the // new subnet base address. $where = "ip_addr >= {$options['set_ip']} AND ip_addr <= {$str_last_host}"; list($status, $rows, $record) = ona_get_subnet_record($where); if ($rows > 1 or $rows == 1 and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet would encompass an existing subnet."; return array(9, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for any hosts that are currently in our subnet that would be // abandoned if we were to make the proposed changes. // Look for hosts on either side of the new subnet boundaries: // [--- new subnet ---] // * ** * * <-- Hosts: the first and last host would be a problem! // [------- old subnet --------] // $where1 = "subnet_id = {$subnet['id']} AND ip_addr < {$first_host}"; $where2 = "subnet_id = {$subnet['id']} AND ip_addr > {$last_last_host}"; list($status, $rows1, $record) = ona_get_interface_record($where1); list($status, $rows2, $record) = ona_get_interface_record($where2); if ($rows1 or $rows2) { $num = $rows1 + $rows2; $self['error'] = "ERROR => Changes would abandon {$num} hosts in an unallocated ip space"; return array(10, $self['error'] . "\n"); } // Look for any dhcp pools that are currently in our subnet that would be // abandoned if we were to make the proposed changes. // Look for existin pools with start/end values outside of new subnet range // [--- new subnet ---] // [--cur pool--] // [------- old subnet --------] // $where1 = "subnet_id = {$subnet['id']} AND ip_addr_start < {$options['set_ip']}"; $where2 = "subnet_id = {$subnet['id']} AND ip_addr_end > {$str_last_host}"; list($status, $rows1, $record) = ona_get_dhcp_pool_record($where1); list($status, $rows2, $record) = ona_get_dhcp_pool_record($where2); if ($rows1 or $rows2) { $num = $rows1 + $rows2; $self['error'] = "ERROR => Changes would abandon a DHCP pool in an unallocated ip space, adjust pool sizes first"; return array(10, $self['error'] . "\n"); } } // // Define the fields we're updating // // This variable will contain the updated info we'll insert into the DB $SET = array(); $SET['ip_addr'] = $options['set_ip']; $SET['ip_mask'] = $options['set_netmask']; // Set options['set_security_level']? // Sanitize "security_level" option if (array_key_exists('set_security_level', $options)) { $options['set_security_level'] = sanitize_security_level($options['set_security_level']); if ($options['set_security_level'] == -1) { return array(11, $self['error'] . "\n"); } $SET['lvl'] = $options['set_security_level']; } // Set options['set_name']? if ($options['set_name']) { // BUSINESS RULE: We require subnet names to be in upper case and spaces are converted to -'s. $options['set_name'] = trim($options['set_name']); $options['set_name'] = preg_replace('/\\s+/', '-', $options['set_name']); $options['set_name'] = strtoupper($options['set_name']); // Make sure there's not another subnet with this name list($status, $rows, $tmp) = ona_get_subnet_record(array('name' => $options['set_name'])); if ($status or $rows > 1 or $rows == 1 and $tmp['id'] != $subnet['id']) { $self['error'] = "ERROR => That name is already used by another subnet!"; return array(12, $self['error'] . "\n"); } $SET['name'] = $options['set_name']; } // Set options['set_type']? if ($options['set_type']) { // Find the type from $options[type] list($status, $rows, $subnet_type) = ona_find_subnet_type($options['set_type']); if ($status or $rows != 1) { $self['error'] = "ERROR => Invalid subnet type specified!"; return array(13, $self['error'] . "\n"); } printmsg("Subnet type selected: {$subnet_type['display_name']} ({$subnet_type['short_name']})", 1); $SET['subnet_type_id'] = $subnet_type['id']; } // Set options['set_vlan']? if (array_key_exists('set_vlan', $options) or $options['campus']) { if (!$options['set_vlan']) { $SET['vlan_id'] = ''; } else { // Find the VLAN ID from $options[set_vlan] and $options[campus] list($status, $rows, $vlan) = ona_find_vlan($options['set_vlan'], $options['campus']); if ($status or $rows != 1) { $self['error'] = "ERROR => The vlan/campus pair specified is invalid!"; return array(15, $self['error'] . "\n"); } printmsg("VLAN selected: {$vlan['name']} in {$vlan['vlan_campus_name']} campus", 1); $SET['vlan_id'] = $vlan['id']; } } // Update the subnet record list($status, $rows) = db_update_record($onadb, 'subnets', array('id' => $subnet['id']), $SET); if ($status or !$rows) { return array(16, $self['error'] . "\n"); } // Load the updated record for display list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $subnet['id'])); // Return the (human-readable) success notice $text = format_array($SET); $self['error'] = "INFO => Subnet UPDATED"; return array(0, $self['error'] . ":\n{$text}\n"); }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('user_admin')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; $exit_status = 0; // Validate input if (!$form['username']) { $js .= "alert('Error! All fields are required!');"; $response->addScript($js); return $response->getXML(); } if (!preg_match('/^[A-Za-z0-9.\\-_]+$/', $form['username'])) { $js .= "alert('Invalid username! Valid characters: A-Z 0-9 .-_');"; $response->addScript($js); return $response->getXML(); } // Create a new record? if (!$form['user_id']) { list($status, $rows) = db_insert_record($onadb, 'users', array('username' => $form['username'], 'password' => $form['password'])); if ($status or !$rows) { $self['error'] = "ERROR => user_edit_add ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => User ADDED: {$form['username']} "; printmsg($self['error'], 0); } } else { list($status, $rows, $user) = db_get_record($onadb, 'users', array('id' => $form['user_id'])); if ($rows != 1 or $user['id'] != $form['user_id']) { $js .= "alert('Error! The record requested could not be loaded from the database!');"; $response->addScript($js); return $response->getXML(); } list($status, $rows) = db_update_record($onadb, 'users', array('id' => $user['id']), array('username' => $form['username'], 'password' => $form['password'])); if ($status) { $self['error'] = "ERROR => user_edit update ws_save() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); } else { list($status, $rows, $new_record) = db_get_record($onadb, 'users', array('id' => $user['id'])); // Return the success notice $self['error'] = "INFO => User UPDATED:{$user['id']}: {$new_record['username']}"; $log_msg = "INFO => User UPDATED:{$user['id']}: "; $more = ""; foreach (array_keys($user) as $key) { if ($user[$key] != $new_record[$key]) { $log_msg .= $more . $key . "[" . $user[$key] . "=>" . $new_record[$key] . "]"; $more = ";"; } } } } // Make sure we can load the user record from the db list($status, $rows, $user) = db_get_record($onadb, 'users', array('username' => $form['username'])); if ($status or $rows != 1) { $js .= "alert('Save failed: " . trim($self['error']) . "');"; // Return some javascript to the browser $response->addScript($js); return $response->getXML(); } // This is a bit tricky because we want to make sure the user has all the groups // that are checked in the form, but no others. And of course we want to make as // few sql queries as possible. It's tricky because the form only submits us the // groups that are checked. // Get a list of every group list($status, $rows, $groups) = db_get_records($onadb, 'groups', 'id > 0'); // Loop through each group foreach ($groups as $group) { // See if the user is assigned to this group or not list($status, $rows, $tmp) = db_get_record($onadb, 'group_assignments', array('user_id' => $user['id'], 'group_id' => $group['id'])); $exit_status += $status; // If the user is supposed to be assigned to this group, make sure she is. if (array_key_exists($group['name'], $form['groups'])) { if ($status == 0 and $rows == 0) { list($status, $rows) = db_insert_record($onadb, 'group_assignments', array('user_id' => $user['id'], 'group_id' => $group['id'])); $log_msg .= $more . "group_add[" . $group['name'] . "]"; $more = ";"; $exit_status += $status; } } else { if ($status == 0 and $rows == 1) { list($status, $rows) = db_delete_records($onadb, 'group_assignments', array('user_id' => $user['id'], 'group_id' => $group['id'])); $log_msg .= $more . "group_del[" . $group['name'] . "]"; $more = ";"; $exit_status += $status; } } } // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed: " . trim($self['error']) . "');"; } else { // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_user_list', xajax.getFormValues('app_user_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
function dhcp_pool_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.03'; printmsg("DEBUG => dhcp_pool_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !($options['pool'] and ($options['set_failover_group'] or $options['set_start'] or $options['set_end'] or $options['set_llength'] or $options['set_lgrace'] or $options['set_lrenewal'] or $options['set_lrebind']))) { // 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_modify-v{$version} Updates a dhcp pool in the database pointing to the specified identifier Synopsis: dhcp_pool_modify [KEY=VALUE] ... Where: pool=ID Table ID for the pool Optional: set_failover_group=ID group identifier set_server=NAME[.DOMAIN] or ID server identifier set_start=IP Start ip address of pool set_end=IP End IP of pool set_llength=NUMBER Lease Time. Default ({$conf['dhcp_pool']['llength']}) set_lgrace=NUMBER Lease Grace Period. Default ({$conf['dhcp_pool']['lgrace']}) set_lrenewal=NUMBER Lease Renewal. Default ({$conf['dhcp_pool']['lrenewal']}) set_lrebind=NUMBER Lease Rebind. Default ({$conf['dhcp_pool']['lrebind']}) EOM ); } // get the existing pool to edit list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', array('id' => $options['pool'])); if (!$rows) { printmsg("DEBUG => Unable to find the DHCP pool record using id: {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find a pool using id: {$options['pool']}"; return array(1, $self['error'] . "\n"); } // set the pool id in the set variable $SET['id'] = $pool['id']; // NOTE: currently modify pool does not allow you to change subnets // Get subnet info.. list($status, $rows, $subnet) = ona_find_subnet($pool['subnet_id']); $SET['subnet_id'] = $subnet['id']; // make sure that the start address is actually part of an existing subnet if ($options['set_start']) { list($status, $rows, $subnetstart) = ona_find_subnet(ip_mangle($options['set_start'], 'dotted')); if (!$rows) { printmsg("DEBUG => Unable to find a subnet related to starting address ({$options['set_start']})!", 3); $self['error'] = "ERROR => Unable to find a subnet related to your starting address of {$options['set_start']}."; return array(1, $self['error'] . "\n"); } if ($subnetstart['id'] != $pool['subnet_id']) { printmsg("DEBUG => The starting address ({$options['set_start']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!", 3); $self['error'] = "ERROR => The starting address ({$options['set_start']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!"; return array(1, $self['error'] . "\n"); } } // make sure that the end address is actually part of an existing subnet if ($options['set_end']) { list($status, $rows, $subnetend) = ona_find_subnet(ip_mangle($options['set_end'], 'dotted')); if (!$rows) { printmsg("DEBUG => Unable to find a subnet related to ending address ({$options['set_end']})!", 3); $self['error'] = "ERROR => Unable to find a subnet related to your ending address of {$options['set_end']}."; return array(1, $self['error'] . "\n"); } if ($subnetend['id'] != $pool['subnet_id']) { printmsg("DEBUG => The ending address ({$options['set_end']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!", 3); $self['error'] = "ERROR => The ending address ({$options['set_end']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!"; return array(1, $self['error'] . "\n"); } } // Assign which failover group to use if ($options['set_failover_group'] == 0) { $desc = 'Not using a failover group'; $SET['dhcp_failover_group_id'] = 0; } else { list($status, $rows, $fg) = ona_get_dhcp_failover_group_record(array('id' => $options['set_failover_group'])); if (!$fg['id']) { printmsg("DEBUG => The failover_group specified ({$options['set_failover_group']}) does not exist", 3); $self['error'] = "ERROR => The failover_group specified ({$options['set_failover_group']}) does not exist!"; return array(4, $self['error'] . "\n"); } // get the server names for the two servers list($fail_host1, $fail_zone1) = ona_find_host($fg['primary_server_id']); list($fail_host2, $fail_zone2) = ona_find_host($fg['secondary_server_id']); $desc = $fail_host1['fqdn'] . '/' . $fail_host2['fqdn']; $SET['dhcp_failover_group_id'] = $fg['id']; } // check that start and end are not the same //if ($options['set_start'] and $options['set_end'] and $options['set_start'] == $options['set_end']) { // printmsg("DEBUG => The start and end IP addresses (" . ip_mangle($options['set_start'],'dotted') . ") cannot be the same!",3); // $self['error'] = "ERROR => The start and end IP addresses (" . ip_mangle($options['set_start'],'dotted') . ") cannot be the same!"; // return(array(2, $self['error'] . "\n")); //} if ($options['set_start']) { $start_dec = ip_mangle($options['set_start'], 'numeric'); } else { $start_dec = $pool['ip_addr_start']; } if ($options['set_end']) { $end_dec = ip_mangle($options['set_end'], 'numeric'); } else { $end_dec = $pool['ip_addr_end']; } $net_end = 4294967295 - $subnet['ip_mask'] + $subnet['ip_addr']; // Validate that the IP address supplied isn't the base or broadcast of the subnet if ($start_dec == $subnet['ip_addr'] or $end_dec == $subnet['ip_addr']) { printmsg("DEBUG => IP address can't be a subnet's base address (" . ip_mangle($subnet['ip_addr'], 'dotted') . ")!", 3); $self['error'] = "ERROR => IP address can't be a subnet's base address (" . ip_mangle($subnet['ip_addr'], 'dotted') . ")!"; return array(7, $self['error'] . "\n"); } if ($start_dec == $net_end or $end_dec == $net_end) { printmsg("DEBUG => IP address can't be a subnet's broadcast address (" . ip_mangle($net_end, 'dotted') . ")!", 3); $self['error'] = "ERROR => IP address can't be the subnet broadcast address(" . ip_mangle($net_end, 'dotted') . ")!"; return array(8, $self['error'] . "\n"); } // check that start is not after the end if ($start_dec > $end_dec) { printmsg("DEBUG => The start IP addresses (" . ip_mangle($start_dec, 'dotted') . ") falls after the end IP address (" . ip_mangle($end_dec, 'dotted') . ")!", 3); $self['error'] = "ERROR => The start IP addresses (" . ip_mangle($start_dec, 'dotted') . ") falls after the end IP address(" . ip_mangle($end_dec, 'dotted') . ")!"; return array(2, $self['error'] . "\n"); } // check for existing hosts inside the pool range list($status, $rows, $interface) = db_get_records($onadb, 'interfaces', 'subnet_id = ' . $subnet['id'] . ' AND ip_addr BETWEEN ' . $start_dec . ' AND ' . $end_dec, '', 0); if ($rows) { printmsg("DEBUG => IP conflict: Specified range (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") encompasses {$rows} host(s)!", 3); $self['error'] = "ERROR => IP conflict: Specified range (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") encompasses {$rows} host(s)"; return array(4, $self['error'] . "\n"); } // *** Check to see if the new pool overlaps any existing pools *** // // Look for overlaps like this (where new pool address starts inside an existing pool): // [ -- new pool -- ] // [ -- old pool --] list($status, $rows, $tmp) = db_get_record($onadb, 'dhcp_pools', 'id != ' . $SET['id'] . ' AND ' . $start_dec . ' BETWEEN ip_addr_start AND ip_addr_end'); if ($rows != 0) { printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") starts inside an existing pool!", 3); $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") starts inside an existing pool."; return array(5, $self['error'] . "\n" . "INFO => Conflicting pool record ID: {$tmp['id']}\n"); } // Look for overlaps like this (where the new pool ends inside an existing pool): // [ -- new pool -- ] // [ -- old pool --] list($status, $rows, $tmp) = db_get_record($onadb, 'dhcp_pools', 'id != ' . $SET['id'] . ' AND ' . $end_dec . ' BETWEEN ip_addr_start AND ip_addr_end'); if ($rows != 0) { printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") ends inside an existing pool!", 3); $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") ends inside an existing pool."; return array(6, $self['error'] . "\n" . "INFO => Conflicting pool record ID: {$tmp['id']}\n"); } // Look for overlaps like this (where the new pool entirely overlaps an existing pool): // [ -------- new pool --------- ] // [ -- old pool --] list($status, $rows, $tmp) = db_get_record($onadb, 'dhcp_pools', 'id != ' . $SET['id'] . ' AND (ip_addr_start BETWEEN ' . $start_dec . ' AND ' . $end_dec . ' OR ip_addr_end BETWEEN ' . $start_dec . ' AND ' . $end_dec . ')'); if ($rows != 0) { printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") would encompass an existing pool!", 3); $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec, 'dotted') . "-" . ip_mangle($end_dec, 'dotted') . ") would encompass an existing pool."; return array(7, $self['error'] . "\n" . "INFO => Conflicting pool record ID: {$tmp['id']}\n"); } // Check permissions if (!auth('advanced') or !authlvl($subnet['lvl'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(8, $self['error'] . "\n"); } // define the remaining entries if (array_key_exists('set_lgrace', $options)) { $SET['lease_grace_period'] = $options['set_lgrace']; } if (array_key_exists('set_llength', $options)) { $SET['lease_length'] = $options['set_llength']; } if (array_key_exists('set_lrenewal', $options)) { $SET['lease_renewal_time'] = $options['set_lrenewal']; } if (array_key_exists('set_lrebind', $options)) { $SET['lease_rebind_time'] = $options['set_lrebind']; } // Set the IPs if you got this far $SET['ip_addr_start'] = $start_dec; $SET['ip_addr_end'] = $end_dec; // Get the DHCP pool record before updating (logging) list($status, $rows, $original_pool) = ona_get_dhcp_pool_record(array('id' => $SET['id'])); // Update the record list($status, $rows) = db_update_record($onadb, 'dhcp_pools', array('id' => $SET['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_pool_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $add_to_error . $self['error'] . "\n"); } $success_start = ip_mangle($SET['ip_addr_start'], 'dotted'); $success_end = ip_mangle($SET['ip_addr_end'], 'dotted'); // Get the DHCP pool record after updating (logging) list($status, $rows, $new_pool) = ona_get_dhcp_pool_record(array('id' => $SET['id'])); // Return the success notice $self['error'] = "INFO => DHCP pool UPDATED:{$original_pool['id']}: {$success_start}-{$success_end} on {$subnet['name']}."; $log_msg = "INFO => DHCP pool UPDATED:{$original_pool['id']}: "; $more = ""; foreach (array_keys($original_pool) as $key) { if ($original_pool[$key] != $new_pool[$key]) { $log_msg .= $more . $key . "[" . $original_pool[$key] . "=>" . $new_pool[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $add_to_error . $self['error'] . "\n"); }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Validate Input if ($form['short_name'] == '' or $form['display_name'] == '') { $response->addScript("alert('Please complete all fields to continue!');"); return $response->getXML(); } // BUSINESS RULE: Force short_name to be console friendly (a-z,-, & _ only) $form['short_name'] = strtolower($form['short_name']); if (!preg_match('/^[\\w-_]+$/', $form['short_name'])) { $response->addScript("alert('Invalid short name! Please use only script-friendly characters: a-z - _ (no spaces)');"); return $response->getXML(); } // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { list($status, $rows) = db_update_record($onadb, 'subnet_types', array('id' => $form['id']), array('short_name' => $form['short_name'], 'display_name' => $form['display_name'], 'notes' => $form['notes'])); } else { $id = ona_get_next_id('subnet_types'); list($status, $rows) = db_insert_record($onadb, 'subnet_types', array('id' => $id, 'display_name' => $form['display_name'], 'short_name' => $form['short_name'], 'notes' => $form['notes'])); } // If the module returned an error code display a popup warning if ($status) { $js .= "alert('Save failed. " . trim($self['error']) . " (Hint: All fields are required!)');"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_subnet_type_list', xajax.getFormValues('app_subnet_type_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Strip whitespace // FIXME: (PK) What about SQL injection attacks? This is a user-entered string... $form['value'] = trim($form['value']); $form['name'] = trim($form['name']); // Don't insert a string of all white space! if (trim($form['name']) == "") { $self['error'] = "ERROR => Blank names not allowed."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } // If you get a numeric in $form, update the record if ($form['id']) { // Get the record before updating (logging) list($status, $rows, $original_sysconf) = ona_get_record(array('name' => $form['id']), 'sys_config'); // Bail if it is a non editable entry if ($original_sysconf['editable'] == 0) { $self['error'] = "ERROR => This system config entry is not editable."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } if ($form['value'] !== $original_sysconf['value'] or $form['description'] !== $original_sysconf['description']) { list($status, $rows) = db_update_record($onadb, 'sys_config', array('name' => $form['name']), array('value' => $form['value'], 'description' => $form['description'])); if ($status or !$rows) { $self['error'] = "ERROR => sys_config_edit update ws_save() failed: " . $self['error']; printmsg($self['error'], 0); } else { // Get the record after updating (logging) list($status, $rows, $new_sysconf) = ona_get_record(array('name' => $form['id']), 'sys_config'); // Return the success notice $self['error'] = "INFO => Sys_config UPDATED:{$new_sysconf['name']}: {$new_sysconf['value']}"; printmsg($self['error'], 0); $log_msg = "INFO => Sys_config UPDATED:{$new_sysconf['name']} NAME[{$original_sysconf['name']}]{$original_sysconf['value']}=>{$new_sysconf['value']}"; printmsg($log_msg, 0); } } else { $self['error'] = "INFO => You have not made a change to the value or description."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } } else { // check for an existing entry like this list($status, $rows, $test) = ona_get_record(array('name' => $form['name']), 'sys_config'); if ($rows) { $self['error'] = "ERROR => The name you are trying to use already exists."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } list($status, $rows) = db_insert_record($onadb, "sys_config", array('name' => $form['name'], 'value' => $form['value'], 'description' => $form['description'], 'editable' => 1, 'deleteable' => 1)); if ($status or !$rows) { $self['error'] = "ERROR => Sys_config_edit add ws_save() failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => Sys_config ADDED: {$form['name']} "; printmsg($self['error'], 0); } } // If the module returned an error code display a popup warning if ($status) { $js .= "alert(\"Save failed. " . trim($self['error']) . "\");"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_sysconf_list', xajax.getFormValues('app_sysconf_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
function dhcp_failover_group_modify($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => dhcp_failover_group_modify({$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['id'] and ($options['set_pri_server'] or $options['set_sec_server'] or $options['set_response_delay'] or $options['set_unacked_updates'] or $options['set_max_balance'] or $options['set_priport'] or $options['set_peerport'] or $options['set_mclt'] or $options['set_split']))) { // 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_modify-v{$version} Modifies a DHCP failover group in the database Synopsis: dhcp_failover_group_modify [KEY=VALUE] ... Where: id=id id for failover record Optional: set_pri_server=NAME[.DOMAIN] or id identifier of the primary server set_sec_server=NAME[.DOMAIN] or id identifier of the secondary server set_response_delay=NUMBER Default ({$conf['dhcp_response_delay']}) set_unacked_updates=NUMBER Default ({$conf['dhcp_unacked_updates']}) set_max_balance=NUMBER Default ({$conf['dhcp_max_balance']}) set_priport=NUMBER Default ({$conf['dhcp_priport']}) set_peerport=NUMBER Default ({$conf['dhcp_peerport']}) set_mclt=NUMBER Default ({$conf['dhcp_mclt']}) set_split=NUMBER Default ({$conf['dhcp_split']}) EOM ); } // Determine the entry itself exists list($status, $rows, $failovergroup) = ona_get_dhcp_failover_group_record(array('id' => $options['id'])); // Test to see that we were able to find the specified record if (!$failovergroup['id']) { printmsg("DEBUG => Unable to find the DHCP failover group record using {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find the DHCP failover group record using {$options['id']}!"; return array(4, $self['error'] . "\n"); } list($status, $rows, $pri_server) = ona_find_host($failovergroup['primary_server_id']); list($status, $rows, $sec_server) = ona_find_host($failovergroup['secondary_server_id']); // Debugging printmsg("DEBUG => dhcp_failover_group_display(): Found id:{$failovergroup['id']}", 3); // This variable will contain the updated info we'll insert into the DB $SET = array(); if (array_key_exists('set_pri_server', $options) and $options['set_pri_server']) { // Determine the server is valid list($status, $rows, $pri_host) = ona_find_host($options['set_pri_server']); if (!$pri_host['id']) { printmsg("DEBUG => The server specified, {$options['set_pri_server']}, does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['set_pri_server']}, does not exist!"; return array(2, $self['error'] . "\n"); } // Determine the host that was found is actually a server // MP: FIXME: dont think I'm going to pursue doing a seperate server table.. lets remove // list($status, $rows, $pri_server) = ona_get_server_record(array('host_id' => $pri_host['id'])); // // if (!$pri_server['id']) { // printmsg("DEBUG => The host specified, {$pri_host['fqdn']}, is not a server!",3); // $self['error'] = "ERROR => The host specified, {$pri_host['fqdn']}, is not a server!"; // return(array(5, $self['error'] . "\n")); // } $SET['primary_server_id'] = $pri_server['id']; } if (array_key_exists('set_sec_server', $options) and $options['set_sec_server']) { // Determine the server is valid list($status, $rows, $sec_host) = ona_find_host($options['set_sec_server']); if (!$sec_host['id']) { printmsg("DEBUG => The server specified, {$options['set_sec_server']}, does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['set_sec_server']}, does not exist!"; return array(2, $self['error'] . "\n"); } // Determine the host that was found is actually a server // MP: FIXME: dont think I'm going to pursue doing a seperate server table.. lets remove // list($status, $rows, $sec_server) = ona_get_server_record(array('host_id' => $sec_host['id'])); // // if (!$sec_server['id']) { // printmsg("DEBUG => The host specified, {$sec_host['fqdn']}, is not a server!",3); // $self['error'] = "ERROR => The host specified, {$sec_host['fqdn']}, is not a server!"; // return(array(5, $self['error'] . "\n")); // } $SET['secondary_server_id'] = $sec_server['id']; } // define the remaining entries if ($options['set_response_delay']) { $SET['max_response_delay'] = $options['set_response_delay']; } if ($options['set_unacked_updates']) { $SET['max_unacked_updates'] = $options['set_unacked_updates']; } if ($options['set_max_balance']) { $SET['max_load_balance'] = $options['set_max_balance']; } if ($options['set_priport']) { $SET['primary_port'] = $options['set_priport']; } if ($options['set_peerport']) { $SET['peer_port'] = $options['set_peerport']; } if ($options['set_mclt']) { $SET['mclt'] = $options['set_mclt']; } if ($options['set_split']) { $SET['split'] = $options['set_split']; } // Check permissions if (!auth('advanced')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Get the alias record before updating (logging) list($status, $rows, $original_fgroup) = ona_get_dhcp_failover_group_record(array('id' => $failovergroup['id'])); // Update the record list($status, $rows) = db_update_record($onadb, 'dhcp_failover_groups', array('id' => $failovergroup['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_failover_group_modify() SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } list($status, $rows, $fgroup) = ona_get_dhcp_failover_group_record(array('id' => $failovergroup['id'])); list($status, $rows, $pri_host) = ona_find_host($fgroup['primary_server_id']); list($status, $rows, $sec_host) = ona_find_host($fgroup['secondary_server_id']); // Return the success notice $self['error'] = "INFO => DHCP failover group UPDATED:{$failovergroup['id']}: PRI:{$pri_host['fqdn']} SEC:{$sec_host['fqdn']}"; $log_msg = "INFO => DHCP failover group UPDATED:{$failovergroup['id']}: "; $more = ""; foreach (array_keys($original_fgroup) as $key) { if ($original_fgroup[$key] != $fgroup[$key]) { $log_msg .= $more . $key . "[" . $original_fgroup[$key] . "=>" . $fgroup[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $self['error'] . "\n"); }
function vlan_campus_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => vlan_campus_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['name'] or !$options['set_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 vlan_campus_modify-v{$version} Modifies an existing vlan campus entry in the database Synopsis: vlan_campus_modify [KEY=VALUE] ... Where: name=NAME or ID campus Name or ID Update: set_name=NAME change VLAN campus name EOM ); } // The formatting rule on vlan campus names is all upper and trim it, spaces to - $options['name'] = strtoupper(trim($options['name'])); $options['set_name'] = strtoupper(trim($options['set_name'])); $options['set_name'] = preg_replace('/\\s+/', '-', $options['set_name']); // If the vlan provided is numeric, check to see if it's an vlan if (is_numeric($options['name'])) { // See if it's an vlan_campus_id list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $options['name'])); if (!$campus['id']) { printmsg("DEBUG => Unable to find VLAN campus using the ID {$options['name']}!", 3); $self['error'] = "ERROR => Unable to find VLAN campus using the ID {$options['name']}!"; return array(2, $self['error'] . "\n"); } } else { $options['name'] = strtoupper(trim($options['name'])); list($status, $rows, $campus) = ona_get_vlan_campus_record(array('name' => $options['name'])); if (!$campus['id']) { printmsg("DEBUG => Unable to find VLAN campus using the name {$options['name']}!", 3); $self['error'] = "ERROR => Unable to find VLAN campus using the name {$options['name']}!"; return array(2, $self['error'] . "\n"); } } printmsg("DEBUG => Found VLAN campus: {$campus['name']}", 3); // This variable will contain the updated info we'll insert into the DB $SET = array(); // If they are specifying a new name, process it. if ($options['set_name']) { // Validate that there isn't already an vlan with this name list($status, $rows, $record) = db_get_records($onadb, 'vlan_campuses', "id != {$campus['id']} AND name LIKE '{$options['set_name']}'"); if ($status or $rows) { printmsg("DEBUG => The VLAN campus ({$options['set_name']}) already exists!", 3); $self['error'] = "ERROR => The VLAN campus {$options['set_name']} already exists!"; return array(4, $self['error'] . "\n"); } $SET['name'] = $options['set_name']; } // Check permissions if (!auth('vlan_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Update the record list($status, $rows) = db_update_record($onadb, 'vlan_campuses', array('id' => $campus['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => vlan_campus_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Return the success notice if ($options['set_name'] != $campus['name']) { $renamed = "=> {$options['set_name']}"; } $self['error'] = "INFO => VLAN Campus UPDATED: {$campus['name']} {$renamed}"; return array(0, $self['error'] . "\n"); }
function ws_delete($window_name, $form = '') { global $conf, $self, $mysql; // Make sure they have permission if (!auth('admin')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Don't allow this in the demo account! if ($_SESSION['auth']['client']['url'] == 'demo') { $response = new xajaxResponse(); $response->addScript("alert('Feature disabled in this demo!');"); return $response->getXML(); } // Don't allow a user to delete their own account! if ($_SESSION['auth']['user']['id'] == $form) { $response = new xajaxResponse(); $response->addScript("alert('Sorry, but you can\\'t delete your own admin account!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Set the user to inactive (which will make them "dissapear" for all practical purposes) printmsg("NOTICE => Deleting (disabling) user: {$form} client url: {$_SESSION['auth']['client']['url']}", 0); list($status, $rows) = db_update_record($mysql, 'users', array('client_id' => $_SESSION['auth']['client']['id'], 'id' => $form), array('active' => 0)); // If the module returned an error code display a popup warning if ($status != 0 or $rows != 1) { $js .= "alert('Delete failed');"; } else { // Refresh the current list of templates.. it's changed! $js .= "xajax_window_submit('{$window_name}', xajax.getFormValues('{$window_name}_filter_form'), 'display_list');"; } // Insert the new table into the window $response->addScript($js); return $response->getXML(); }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Strip whitespace // FIXME: (PK) What about SQL injection attacks? This is a user-entered string... $form['cust_attrib_type_name'] = trim($form['cust_attrib_type_name']); // Don't insert a string of all white space! if ($form['cust_attrib_type_name'] == "") { $self['error'] = "ERROR => Blank names not allowed."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { // Get the manufacturer record before updating (logging) list($status, $rows, $original_manufacturer) = ona_get_custom_attribute_type_record(array('id' => $form['id'])); if ($form['cust_attrib_type_name'] !== $original_type['name']) { list($status, $rows) = db_update_record($onadb, 'custom_attribute_types', array('id' => $form['id']), array('name' => $form['cust_attrib_type_name'], 'field_validation_rule' => $form['field_validation_rule'], 'failed_rule_text' => $form['failed_rule_text'], 'notes' => $form['notes'])); if ($status or !$rows) { $self['error'] = "ERROR => cust_attrib_type edit update ws_save() failed: " . $self['error']; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); } else { // Get the manufacturer record after updating (logging) list($status, $rows, $new_type) = ona_get_custom_attribute_type_record(array('id' => $form['id'])); // Return the success notice $self['error'] = "INFO => Custom Attribute Type UPDATED:{$new_type['id']}: {$new_type['name']}"; printmsg($self['error'], 0); $log_msg = "INFO => Custom Attribute Type UPDATED:{$new_type['id']}: name[{$original_type['name']}=>{$new_type['name']}]"; printmsg($log_msg, 0); } } } else { $id = ona_get_next_id('custom_attribute_types'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id('custom_attribute_types') call failed!"; printmsg($self['error'], 0); } else { list($status, $rows) = db_insert_record($onadb, "custom_attribute_types", array('id' => $id, 'name' => $form['cust_attrib_type_name'], 'field_validation_rule' => $form['field_validation_rule'], 'failed_rule_text' => $form['failed_rule_text'], 'notes' => $form['notes'])); if ($status or !$rows) { $self['error'] = "ERROR => Custom attribute type add ws_save() failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => Custom Attribute Type ADDED: {$form['cust_attrib_type_name']} "; printmsg($self['error'], 0); } } } // If the module returned an error code display a popup warning if ($status or !$rows) { $js .= "alert(\"Save failed. " . trim($self['error']) . " (Hint: Does the name you're trying to insert already exist?)\");"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_custom_attribute_type_list', xajax.getFormValues('app_custom_attribute_type_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
function dhcp_entry_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.04'; printmsg("DEBUG => dhcp_entry_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !($options['id'] and ($options['set_option'] and array_key_exists('set_value', $options) or array_key_exists('set_value', $options)))) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_entry_modify-v{$version} Modifies a DHCP entry in the database Synopsis: dhcp_entry_modify [KEY=VALUE] ... Where: id=ID DHCP entry ID Options: set_option=DHCP type DHCP parameter type set_value=STRING string value for the DHCP type Notes: If you specify a type, you must specify a value. EOM ); } // Determine the entry itself exists list($status, $rows, $entry) = ona_get_dhcp_option_entry_record(array('id' => $options['id'])); if ($status or !$rows) { printmsg("DEBUG => Invalid DHCP entry record ID ({$options['id']})!", 3); $self['error'] = "ERROR => Invalid DHCP entry record ID ({$options['id']})!"; return array(2, $self['error'] . "\n"); } printmsg("DEBUG => dhcp_entry_modify(): Found entry, {$entry['display_name']} => {$entry['value']}", 3); $desc = ''; // Load associated host, subnet or server record $host = $subnet = $server = array(); if ($entry['host_id']) { list($status, $rows, $host) = ona_find_host($entry['host_id']); $desc = $host['fqdn']; } if ($entry['subnet_id']) { list($status, $rows, $subnet) = ona_find_subnet($entry['subnet_id']); $desc = "{$subnet['name']} (" . ip_mangle($subnet['ip_addr']) . ")"; } if ($entry['server_id']) { list($status, $rows, $server) = ona_find_host($entry['server_id']); $desc = $server['fqdn']; } // Check permissions on source identifier $lvl = 100; if ($host['id']) { $lvl = $host['lvl']; } if ($subnet['id']) { $lvl = $subnet['lvl']; } if (!auth('advanced') or !authlvl($lvl)) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // unset $host if $server is defined .. we don't need it anymore if ($server['id']) { $host = array(); } // This variable will contain the updated info we'll insert into the DB $SET = array(); if (array_key_exists('set_value', $options)) { // trim leading and trailing whitespace from 'value' $SET['value'] = trim($options['set_value']); // trim leading and trailing whitespace from 'value' and check that a value exists $SET['value'] = trim($options['set_value']); if (strlen($SET['value']) == 0) { printmsg("DEBUG => The DHCP value was blank", 3); $self['error'] = "ERROR => DHCP value was blank"; return array(2, $self['error'] . "\n"); } } if ($options['set_option']) { // Make sure they specified a value if (!array_key_exists('set_value', $options)) { printmsg("DEBUG => No value specified for given DHCP parameter type ({$options['set_option']})!", 3); $self['error'] = "ERROR => No value specified for given DHCP parameter type ({$options['set_option']})!"; return array(8, $self['error'] . "\n"); } // Determine the type is valid list($status, $rows, $type) = ona_find_dhcp_option(trim($options['set_option'])); if ($status or !$rows) { printmsg("DEBUG => Invalid DHCP parameter type specified ({$options['set_option']})!", 3); $self['error'] = "ERROR => Invalid DHCP parameter type specified ({$options['set_option']})!"; return array(8, $self['error'] . "\n"); } printmsg("DEBUG => dhcp_entry_modify(): Found parameter type {$type['display_name']}", 3); $SET['dhcp_option_id'] = $type['id']; // Make sure this isn't a duplicate // TODO: this code seems a bit suspect of being nasty.. possibly fix it up $search = array('dhcp_option_id' => $type['id'], 'host_id' => 0, 'subnet_id' => 0); if ($host['id']) { $search['host_id'] = $host['id']; } if ($subnet['id']) { $search['subnet_id'] = $subnet['id']; } if ($server['id']) { $search['server_id'] = $server['id']; } list($status, $rows, $record) = ona_get_dhcp_option_entry_record($search); if ($status or $rows > 1 or $rows == 1 and $record['id'] != $entry['id']) { printmsg("DEBUG => That DHCP parameter type is already defined ({$search})!", 3); $self['error'] = "ERROR => That DHCP parameter type is already defined ({$search})!"; return array(11, $self['error'] . "\n"); } } // Get the dhcp entry record before updating (logging) list($status, $rows, $original_entry) = ona_get_dhcp_option_entry_record(array('id' => $entry['id'])); // Update the record list($status, $rows) = db_update_record($onadb, 'dhcp_option_entries', array('id' => $entry['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => dhcp_entry_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Get the entry again to display details list($status, $tmp_rows, $entry) = ona_get_dhcp_option_entry_record(array('id' => $entry['id'])); // Return the success notice $self['error'] = "INFO => DHCP entry UPDATED:{$entry['id']}: \"{$entry['display_name']}\"={$entry['value']} on {$desc} "; $log_msg = "INFO => DHCP entry UPDATED:{$entry['id']}: "; $more = ""; foreach (array_keys($original_entry) as $key) { if ($original_entry[$key] != $entry[$key]) { $log_msg .= $more . $key . "[" . $original_entry[$key] . "=>" . $entry[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $self['error'] . "\n"); }
function custom_attribute_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => custom_attribute_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !($options['id'] and ($options['set_type'] and array_key_exists('set_value', $options) or array_key_exists('set_value', $options) or array_key_exists('set_type', $options)))) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM custom_attribute_modify-v{$version} Modifies the custom attribute specified Synopsis: custom_attribute_modify Where: id=ID custom attribute ID Options: set_type=ID|STRING the name or ID of the attribute type set_value="STRING" the value of the attribute Notes: If you specify a type, you must specify a value. EOM ); } // Determine the entry itself exists list($status, $rows, $entry) = ona_get_custom_attribute_record(array('id' => $options['id'])); if ($status or !$rows) { printmsg("DEBUG => Invalid Custom Atribute record ID ({$options['id']})!", 3); $self['error'] = "ERROR => Invalid Custom Atribute record ID ({$options['id']})!"; return array(2, $self['error'] . "\n"); } printmsg("DEBUG => custom_attribute_modify(): Found entry, {$entry['name']} => {$entry['value']}", 3); $desc = ''; // If they provided a hostname / ID let's look it up if ($entry['table_name_ref'] == "hosts") { list($status, $rows, $host) = ona_find_host($entry['table_id_ref']); $table_name_ref = 'hosts'; $table_id_ref = $host['id']; $desc = $host['fqdn']; } if ($entry['table_name_ref'] == "subnets") { list($status, $rows, $subnet) = ona_find_subnet($entry['table_id_ref']); $table_name_ref = 'subnets'; $table_id_ref = $subnet['id']; $desc = $subnet['name']; } // This variable will contain the updated info we'll insert into the DB $SET = array(); $typesearch = 'id'; $typeval = $entry['custom_attribute_type_id']; // determine the attribute type if (array_key_exists('set_type', $options)) { if (!is_numeric($options['set_type'])) { $typesearch = 'name'; } $typeval = $options['set_type']; } // Find the attribute type list($status, $rows, $catype) = ona_get_custom_attribute_type_record(array($typesearch => $typeval)); if (!$rows) { printmsg("DEBUG => Unable to find custom attribute type: {$typeval}", 3); $self['error'] = "ERROR => Unable to find custom attribute type: {$typeval}"; return array(3, $self['error'] . "\n"); } // default to whatever was in the record you are editing $SET['value'] = $entry['value']; if (array_key_exists('set_value', $options)) { // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters $options['set_value'] = str_replace('\\=', '=', $options['set_value']); $options['set_value'] = str_replace('\\&', '&', $options['set_value']); // trim leading and trailing whitespace from 'value' $SET['value'] = $valinfo = trim($options['set_value']); } if (!$catype['failed_rule_text']) { $catype['failed_rule_text'] = "Not specified."; } // validate the inpute value against the field_validation_rule. if ($catype['field_validation_rule'] and !preg_match($catype['field_validation_rule'], $SET['value'])) { printmsg("DEBUG => The value '{$SET['value']}' does not match field validation rule: {$catype['field_validation_rule']}", 3); $self['error'] = "ERROR => The value: '{$SET['value']}', does not match field validation rule: {$catype['field_validation_rule']}\\nReason: {$catype['failed_rule_text']}"; return array(4, $self['error'] . "\n"); } // if the value has not changed, skip it if ($SET['value'] == $entry['value']) { unset($SET['value']); $valinfo = "Value Not Changed"; } // if we change the type do a few things if ($catype['id'] != $entry['custom_attribute_type_id']) { // check for existing attributes like this that might already be assigned 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 => The type '{$catype['name']}' is already in use on {$desc}", 3); $self['error'] = "ERROR => The type '{$catype['name']}' is already in use on {$desc}"; return array(5, $self['error'] . "\n"); } // if we are good to go.. set the new type $SET['custom_attribute_type_id'] = $catype['id']; } $msg = "INFO => Updated Custom Attribute type: {$catype['name']} => '{$valinfo}'."; // If nothing at all changed up to this point, bail out if (!$SET) { $self['error'] = "ERROR => custom_attribute_modify() You didn't change anything. Make sure you have a new value."; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Update the record list($status, $rows) = db_update_record($onadb, 'custom_attributes', array('id' => $entry['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => custom_attribute_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } // Return the success notice $self['error'] = $msg; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); }
function ws_change_user_password($window_name, $form) { global $conf, $self, $onadb; $username = $_SESSION['ona']['auth']['user']['username']; // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = "el('passchangemsg').innerHTML = '<span style=\"color: green;\">Changed!</span>'"; $exit_status = 0; // Validate the userid was passed and is "clean" if (!preg_match('/^[A-Za-z0-9.\\-_]+$/', $username)) { $js = "el('passchangemsg').innerHTML = 'Invalid username format';"; $response->addScript($js); return $response->getXML(); } list($status, $rows, $user) = db_get_record($onadb, 'users', "username LIKE '{$username}'"); if (!$rows) { $js = "el('passchangemsg').innerHTML = 'Unknown user';"; // Return some javascript to the browser $response->addScript($js); return $response->getXML(); } if ($user['password'] != $form['old']) { $js = "el('passchangemsg').innerHTML = 'Password incorrect (old)';"; // Return some javascript to the browser $response->addScript($js); return $response->getXML(); } if ($form['new1'] != $form['new2']) { $js = "el('passchangemsg').innerHTML = 'New passwords dont match.';"; // Return some javascript to the browser $response->addScript($js); return $response->getXML(); } list($status, $rows) = db_update_record($onadb, 'users', array('username' => $username), array('password' => $form['new2'])); // If the module returned an error code display a popup warning if ($status) { $js = "alert('Save failed: " . trim($self['error']) . "');"; } if ($js) { $response->addScript($js); } return $response->getXML(); }
function ws_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Strip whitespace // FIXME: (PK) What about SQL injection attacks? This is a user-entered string... // Sanitize "name" option // We require view names to be in upper case and spaces are converted to -'s. $form['dns_view_name'] = strtoupper(trim($form['dns_view_name'])); $form['dns_view_name'] = preg_replace('/\\s+/', '-', $form['dns_view_name']); $form['dns_view_description'] = trim($form['dns_view_description']); // Don't insert a string of all white space! if (trim($form['dns_view_name']) == "") { $self['error'] = "ERROR => Blank names not allowed."; printmsg($self['error'], 1); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { // Get the record before updating (logging) list($status, $rows, $original_type) = ona_get_record(array('id' => $form['id']), 'dns_views'); $SET = array(); if (strtoupper($form['dns_view_name']) != $original_type['name']) { // check for an existing entry like this list($status, $rows, $test) = ona_get_record(array('name' => $form['dns_view_name']), 'dns_views'); if ($rows) { $self['error'] = "ERROR => The name you are trying to use already exists."; printmsg($self['error'], 1); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } $SET['name'] = strtoupper($form['dns_view_name']); } if ($form['dns_view_description'] != $original_type['description']) { $SET['description'] = $form['dns_view_description']; } list($status, $rows) = db_update_record($onadb, 'dns_views', array('id' => $form['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => dns_view_edit update ws_save() failed: " . $self['error']; printmsg($self['error'], 1); $response->addScript("alert('{$self['error']}');"); } else { // Get the record after updating (logging) list($status, $rows, $new_type) = ona_get_record(array('id' => $form['id']), 'dns_views'); // Return the success notice $self['error'] = "INFO => DNS view UPDATED:{$new_type['id']}: {$new_type['name']}"; printmsg($self['error'], 0); $log_msg = "INFO => DNS view UPDATED:{$new_type['id']}: name[{$original_type['name']}=>{$new_type['name']}]"; printmsg($log_msg, 0); } } else { // check for an existing entry like this list($status, $rows, $test) = ona_get_record(array('name' => $form['dns_view_name']), 'dns_views'); if ($rows) { $self['error'] = "ERROR => The name you are trying to use already exists."; printmsg($self['error'], 1); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } $id = ona_get_next_id('dns_views'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id() call failed!"; printmsg($self['error'], 1); } else { printmsg("DEBUG => id for new dns view record: {$id}", 3); list($status, $rows) = db_insert_record($onadb, "dns_views", array('id' => $id, 'name' => strtoupper(trim($form['dns_view_name'])), 'description' => $form['dns_view_description'])); if ($status or !$rows) { $self['error'] = "ERROR => dns_view_edit add ws_save() failed: " . $self['error']; printmsg($self['error'], 1); } else { $self['error'] = "INFO => DNS view ADDED: {$form['dns_view_name']} "; printmsg($self['error'], 0); } } } // If the module returned an error code display a popup warning if ($status or !$rows) { $js .= "alert(\"Save failed. " . trim($self['error']) . " (Hint: Does the name you're trying to insert already exist?)\");"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_dns_view_list', xajax.getFormValues('app_dns_view_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }
if (!$interface['ip_addr']) { echo "Possible orphan PTR record in dns table at ID: {$ptr['id']}. You should delete this record manually.\n"; continue; } $ipflip = ip_mangle($interface['ip_addr'], 'flip'); $octets = explode(".", $ipflip); // Find a pointer domain for this record to associate with. list($status, $rows, $ptrdomain) = ona_find_domain($ipflip . ".in-addr.arpa", 0); // CRAPPY security cludge $_SESSION['ona']['auth']['user']['username'] = '******'; $_SESSION['ona']['auth']['perms']['advanced'] = 'Y'; $_SESSION['ona']['auth']['perms']['host_modify'] = 'Y'; if (!$ptrdomain['id']) { echo " {$interface['ip_addr_text']}: Unable to find a pointer domain for this IP! Creating the following DNS domain: {$octets[3]}.in-addr.arpa\n"; list($status, $output) = run_module('domain_add', array('name' => $octets[3] . '.in-addr.arpa')); if ($status) { echo "ERROR => {$output}\n"; exit($status); } list($status, $rows, $ptrdomain) = ona_find_domain($ipflip . ".in-addr.arpa", 0); } // Found a domain to put them in. echo " Updating PTR for IP {$interface['ip_addr_text']} to domain {$ptrdomain['fqdn']}\n"; // Change the actual DNS record list($status, $rows) = db_update_record($onadb, 'dns', array('id' => $ptr['id']), array('domain_id' => $ptrdomain['id'])); if ($status or !$rows) { echo "ERROR => SQL Query failed updating dns record: " . $self['error']; exit(2); } } exit(0);
function block_modify($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.02'; printmsg("DEBUG => block_modify({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['block'] or !($options['set_name'] or $options['set_start'] or $options['set_end'] or $options['set_notes'])) { // 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_modify-v{$version} Modifies a block entry in the database Synopsis: block_modify [KEY=VALUE] ... Where: block=NAME or ID block Name or ID Update: set_name=NAME change block name set_start=STRING change block starting IP address set_end=STRING change block end set_notes=STRING change block notes EOM ); } // The formatting rule on block names is all upper and trim it $options['set_name'] = trim($options['set_name']); $options['set_name'] = preg_replace('/\\s+/', '-', $options['set_name']); $options['set_name'] = strtoupper($options['set_name']); // 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); // This variable will contain the updated info we'll insert into the DB $SET = array(); // If they are specifying a new name, process it. if ($options['set_name']) { // Validate that there isn't already an block with this name list($status, $rows, $record) = db_get_records($onadb, 'blocks', "id != {$block['id']} AND name LIKE '{$options['set_name']}'"); if ($status or $rows) { printmsg("DEBUG => The block {$options['set_name']} already exists!", 3); $self['error'] = "ERROR => The block {$options['set_name']} already exists!"; return array(4, $self['error'] . "\n"); } $SET['name'] = $options['set_name']; } if ($options['set_start']) { $SET['ip_addr_start'] = ip_mangle($options['set_start'], 'numeric'); } if ($options['set_end']) { $SET['ip_addr_end'] = ip_mangle($options['set_end'], 'numeric'); } if (array_key_exists('set_notes', $options)) { // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters $options['set_notes'] = str_replace('\\=', '=', $options['set_notes']); $options['set_notes'] = str_replace('\\&', '&', $options['set_notes']); if ($options['set_notes'] != $block['notes']) { $SET['notes'] = $options['set_notes']; } } // Check permissions if (!auth('advanced')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // Get the block record before updating (logging) list($status, $rows, $original_block) = ona_get_block_record(array('id' => $block['id'])); // Update the record list($status, $rows) = db_update_record($onadb, 'blocks', array('id' => $block['id']), $SET); if ($status or !$rows) { $self['error'] = "ERROR => block_modify() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(6, $self['error'] . "\n"); } // Get the block record before updating (logging) list($status, $rows, $new_block) = ona_get_block_record(array('id' => $block['id'])); if ($SET['name'] != $block['name']) { $new_name = " => {$SET['name']}"; } // Return the success notice $self['error'] = "INFO => Block UPDATED:{$block['id']}: {$block['name']} {$new_name}"; $log_msg = "INFO => Block UPDATED:{$block['id']}: "; $more = ""; foreach (array_keys($original_block) as $key) { if ($original_block[$key] != $new_block[$key]) { $log_msg .= $more . $key . "[" . $original_block[$key] . "=>" . $new_block[$key] . "]"; $more = ";"; } } // only print to logfile if a change has been made to the record if ($more != '') { printmsg($self['error'], 0); printmsg($log_msg, 0); } return array(0, $self['error'] . "\n"); }
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"); }