ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

interface.inc.php
Go to the documentation of this file.
1 <?php
2 
4 // Function: interface_add (string $options='')
5 //
6 // Input Options:
7 // $options = key=value pairs of options for this function.
8 // multiple sets of key=value pairs should be separated
9 // by an "&" symbol.
10 //
11 // Output:
12 // Returns a two part list:
13 // 1. The exit status of the function. 0 on success, non-zero on
14 // error. All errors messages are stored in $self['error'].
15 // 2. A textual message for display on the console or web interface.
16 //
17 // Example: list($status, $result) = interface_add('');
19 function interface_add($options="") {
20  global $conf, $self, $onadb;
21  printmsg("DEBUG => interface_add({$options}) called", 3);
22 
23  // Version - UPDATE on every edit!
24  $version = '1.11';
25 
26  // Parse incoming options string to an array
27  $options = parse_options($options);
28 
29  // Return the usage summary if we need to
30  if ($options['help'] or !($options['host'] and $options['ip']) ) {
31  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
32  $self['error'] = 'ERROR => Insufficient parameters';
33  return(array(1,
34 <<<EOM
35 
37 Adds a new interface to an existing host record
38 
39  Synopsis: interface_add [KEY=VALUE] ...
40 
41  Required:
42  host=NAME[.DOMAIN] or ID hostname or ID new interface is associated with
43  ip=ADDRESS ip address (numeric or dotted)
44 
45  Optional:
46  mac=ADDRESS mac address (most formats are ok)
47  name=NAME interface name (i.e. "FastEthernet0/1.100")
48  description=TEXT brief description of the interface
49  natip=ADDRESS IP of NAT address to add with this new interface
50  addptr Auto add a PTR record for new IP
51 
52  Notes:
53  * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified
54 \n
55 EOM
56  ));
57  }
58 
59  // clean up what is passed in
60  $options['ip'] = trim($options['ip']);
61 
62  // Set options[force] to N if it's not set
63  $options['force'] = sanitize_YN($options['force'], 'N');
64 
65  // Set options[addptr] and options[create_a] to Y if they're not set
66  $options['addptr'] = sanitize_YN($options['addptr'], 'Y');
67 
68  // Warn about 'name' and 'description' fields exceeding max lengths
69  if ($options['force'] == 'N') {
70  if(strlen($options['name']) > 255) {
71  $self['error'] = "ERROR => 'name' exceeds maximum length of 255 characters.";
72  return(array(2, $self['error'] . "\n" .
73  "NOTICE => You may ignore this error and add the interface anyway with the \"force=yes\" option.\n"));
74  }
75 
76  if(strlen($options['description']) > 255) {
77  $self['error'] = "ERROR => 'description' exceeds maximum length of 255 characters.";
78  return(array(2, $self['error'] . "\n" .
79  "NOTICE => You may ignore this error and add the interface anyway with the \"force=yes\" option.\n"));
80  }
81  }
82 
83  // Find the Host they are looking for
84  list($status, $rows, $host) = ona_find_host($options['host']);
85  if (!$host['id']) {
86  printmsg("DEBUG => The host specified, {$options['host']}, does not exist!",3);
87  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
88  return(array(2, $self['error'] . "\n"));
89  }
90  printmsg("DEBUG => Host selected: {$options['host']}", 3);
91 
92  // Translate IP address to a number
93  $orig_ip= $options['ip'];
94  $options['ip'] = ip_mangle($options['ip'], 1);
95  if ($options['ip'] == -1) {
96  printmsg("DEBUG => Invalid IP address ({$orig_ip})",3);
97  $self['error'] = "ERROR => Invalid IP address ({$orig_ip})!";
98  return(array(3, $self['error'] . "\n"));
99  }
100 
101  // Validate that there isn't already another interface with the same IP address
102  list($status, $rows, $interface) = ona_get_interface_record("ip_addr = {$options['ip']}");
103  if ($rows) {
104  printmsg("DEBUG => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") is already in use!",3);
105  $self['error'] = "ERROR => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") is already in use!";
106  return(array(4, $self['error'] . "\n" .
107  "INFO => Conflicting interface record ID: {$interface['id']}\n"));
108  }
109 
110  // Since the IP seems available, let's double check and make sure it's not in a DHCP address pool
111  list($status, $rows, $pool) = ona_get_dhcp_pool_record("ip_addr_start <= '{$options['ip']}' AND ip_addr_end >= '{$options['ip']}'");
112  if ($status or $rows) {
113  printmsg("DEBUG => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") falls within a DHCP address pool!",3);
114  $self['error'] = "ERROR => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") falls within a DHCP address pool!";
115  return(array(5, $self['error'] . "\nINFO => Conflicting DHCP pool record ID: {$pool['id']}\n"));
116  }
117 
118  // Find the Subnet ID to use from the IP address
119  list($status, $rows, $subnet) = ona_find_subnet($options['ip']);
120  if ($status or $rows != 1 or !$subnet['id']) {
121  printmsg("DEBUG => That IP address (" . ip_mangle($orig_ip,'dotted') . ") is not inside a defined subnet!",3);
122  $self['error'] = "ERROR => That IP address (" . ip_mangle($orig_ip,'dotted') . ") is not inside a defined subnet!";
123  return(array(6, $self['error'] . "\n"));
124  }
125  printmsg("DEBUG => Subnet selected: {$subnet['description']}", 3);
126 
127  // Validate that the IP address supplied isn't the base or broadcast of the subnet, as long as it is not /32 or /31
128  if ($subnet['ip_mask'] < 4294967294) {
129  if ($options['ip'] == $subnet['ip_addr']) {
130  printmsg("DEBUG => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's base address!{$subnet['ip_addr']}",3);
131  $self['error'] = "ERROR => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's base address!";
132  return(array(7, $self['error'] . "\n"));
133  }
134  if ($options['ip'] == ((4294967295 - $subnet['ip_mask']) + $subnet['ip_addr']) ) {
135  printmsg("DEBUG => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's broadcast address!",3);
136  $self['error'] = "ERROR => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be the subnet broadcast address!";
137  return(array(8, $self['error'] . "\n"));
138  }
139  }
140 
141 
142  // Remove any MAC address formatting
143  if ($options['mac']) {
144  $options['mac'] = trim($options['mac']);
145  $orig_mac = $options['mac'];
146  $options['mac'] = mac_mangle($options['mac'], 1);
147  if ($options['mac'] == -1) {
148  printmsg("DEBUG => The MAC address specified ({$orig_mac}) is invalid!",3);
149  $self['error'] = "ERROR => The MAC address specified ({$orig_mac}) is invalid!";
150  return(array(10, $self['error'] . "\n"));
151  }
152 
153  // Unless they have opted to allow duplicate mac addresses ...
154  if ($options['force'] == 'N') {
155  // Validate that there isn't already another interface with the same MAC address on another host
156  // Assume duplicate macs on the same host are ok
157  list($status, $rows, $interface) = db_get_record($onadb, 'interfaces', "mac_addr LIKE '{$options['mac']}' AND host_id != {$host['id']}");
158  if ($status or $rows) {
159  printmsg("DEBUG => MAC conflict: That MAC address ({$options['mac']}) is already in use on another host!",3);
160  $self['error'] = "WARNING => MAC conflict: That MAC address ({$options['mac']}) is already in use on another host!";
161  return(array(11, $self['error'] . "\n" .
162  "NOTICE => You may ignore this warning and add the interface anyway with the \"force=yes\" option.\n" .
163  "INFO => Conflicting interface record ID: {$interface['id']}\n"));
164  }
165  }
166  } else {
167  $options['mac'] = '';
168  }
169 
170  if (!$options['name']) {
171  $options['name'] = '';
172  }
173  // Check permissions
174  if (!auth('host_add')) {
175  $self['error'] = "Permission denied!";
176  printmsg($self['error'], 0);
177  return(array(12, $self['error'] . "\n"));
178  }
179 
180  // Get the next ID for the new interface
181  $id = ona_get_next_id('interfaces');
182  if (!$id) {
183  $self['error'] = "ERROR => The ona_get_next_id('interfaces') call failed!";
184  printmsg($self['error'], 0);
185  return(array(13, $self['error'] . "\n"));
186  }
187  printmsg("DEBUG => ID for new interface: $id", 3);
188 
189  // Add the interface
190  list($status, $rows) =
192  $onadb,
193  'interfaces',
194  array(
195  'id' => $id,
196  'host_id' => $host['id'],
197  'subnet_id' => $subnet['id'],
198  'ip_addr' => $options['ip'],
199  'mac_addr' => $options['mac'],
200  'name' => trim($options['name']),
201  'description' => trim($options['description'])
202  )
203  );
204  if ($status or !$rows) {
205  $self['error'] = "ERROR => interface_add() SQL Query failed: " . $self['error'];
206  printmsg($self['error'], 0);
207  return(array(14, $self['error'] . "\n"));
208  }
209 
210  // Run the module to add a PTR record if requested
211  if ($options['addptr'] == 'Y') {
212  $ptropts['name'] = $host['fqdn'];
213  $ptropts['ip'] = $options['ip'];
214  $ptropts['view'] = $options['view'];
215  $ptropts['type'] = 'PTR';
216  printmsg("DEBUG => interface_add() calling dns_record_add() for new PTR record: {$options['ip']}", 3);
217  list($status, $output) = run_module('dns_record_add', $ptropts);
218  if ($status) { return(array($status, $output)); }
219  $self['error'] .= $output;
220  }
221 
222  // if natip is passed, add the nat interface first
223  if ($options['natip']) {
224  $natint['ip'] = $id;
225  $natint['natip'] = $options['natip'];
226  printmsg("DEBUG => interface_add() calling nat_add() for new ip: {$options['natip']}", 3);
227  list($status, $output) = run_module('nat_add', $natint);
228  if ($status) { return(array($status, $output)); }
229  $self['error'] .= $output;
230  }
231 
232  // Return the success notice
233  $self['error'] = "INFO => Interface ADDED: " . ip_mangle($options['ip'], 'dotted');
234  printmsg($self['error'], 0);
235  return(array(0, $self['error'] . "\n"));
236 }
237 
238 
239 
240 
241 
242 
243 
244 
245 
247 // Function: interface_modify (string $options='')
248 //
249 // Input Options:
250 // $options = key=value pairs of options for this function.
251 // multiple sets of key=value pairs should be separated
252 // by an "&" symbol.
253 //
254 // Output:
255 // Returns a two part list:
256 // 1. The exit status of the function. 0 on success, non-zero on
257 // error. All errors messages are stored in $self['error'].
258 // 2. A textual message for display on the console or web interface.
259 //
260 // Example: list($status, $result) = interface_modify('');
262 function interface_modify($options="") {
263  global $conf, $self, $onadb;
264  printmsg("DEBUG => interface_modify({$options}) called", 3);
265 
266  // Version - UPDATE on every edit!
267  $version = '1.11';
268 
269  // Parse incoming options string to an array
270  $options = parse_options($options);
271 
272  // Set options[use_primary] to N if they're not set
273  $options['use_primary'] = sanitize_YN($options['use_primary'], 'N');
274 
275  // Set options[force] to N if it's not set
276  $options['force'] = sanitize_YN($options['force'], 'N');
277 
278  // Return the usage summary if we need to
279  if ($options['help'] or
280  (!$options['interface'] and !$options['host']) or
281  (!$options['set_ip'] and
282  !$options['set_mac'] and
283  !$options['set_description'] and
284  !$options['set_last_response'] and
285  !$options['set_name']
286  ) ) {
287  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
288  $self['error'] = 'ERROR => Insufficient parameters';
289  return(array(1,
290 <<<EOM
291 
293 Modify an interface record
294 
295  Synopsis: interface_modify [KEY=VALUE] ...
296 
297  Required:
298  interface=ID or IP or MAC interface ID or IP address
299  or
300  host=NAME[.DOMAIN] or ID find interface by hostname or host_id
301 
302  set_ip=IP change IP address (numeric or dotted format)
303  set_mac=ADDRESS change the mac address (most formats ok)
304  set_name=NAME interface name (i.e. "FastEthernet0/1.100")
305  set_description=TEXT description (i.e. "VPN link to building 3")
306  set_last_response=DATE date ip was last seen
307 
308  Optional:
309  use_primary[=Y] use the host's primary interface (only applies
310  when "host" option is used!). NOTE: dcm.pl
311  requires a value ("Y").
312 \n
313 EOM
314  ));
315  }
316 
317 
318  // They provided a interface ID, IP address, interface name, or MAC address
319  if ($options['interface']) {
320  // Find an interface record by something in that interface's record
321  list($status, $rows, $interface) = ona_find_interface($options['interface']);
322  }
323 
324  // If a hostname was provided, do a search based on that
325  else if ($options['host']) {
326  // Find a host by the user's input
327  list($status, $rows, $host) = ona_find_host($options['host']);
328  if (!$host['id']) {
329  printmsg("DEBUG => Host not found ({$options['host']})!",3);
330  $self['error'] = "ERROR => Host not found ({$options['host']})!";
331  return(array(2, $self['error'] . "\n"));
332  }
333  // If we got one, load an associated interface
334  // ... or the primary interface, if the use_primary option is present
335  if ($options['use_primary'] == 'Y') {
336  list($status, $rows, $interface) = ona_get_interface_record(array('id' => $host['primary_interface_id']));
337  }
338  else {
339  list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id']));
340  if ($rows > 1) {
341  printmsg("DEBUG => Specified host ({$options['host']}) has more than one interface!",3);
342  $self['error'] = "ERROR => Specified host ({$options['host']}) has more than one interface!";
343  return(array(3, $self['error'] . "\n"));
344  }
345  }
346  }
347 
348  // If we didn't get a record then exit
349  if (!$interface or !$interface['id']) {
350  printmsg("DEBUG => Interface not found ({$options['interface']})!",3);
351  $self['error'] = "ERROR => Interface not found ({$options['interface']})!";
352  return(array(4, $self['error'] . "\n"));
353  }
354 
355  // This array will contain the updated info we'll insert into the DB
356  $SET = array();
357 
358  // Setting an IP address?
359  if ($options['set_ip']) {
360  $options['set_ip'] = trim($options['set_ip']);
361  $orig_ip = $options['set_ip'];
362  $options['set_ip'] = ip_mangle($options['set_ip'], 'numeric');
363  if ($options['set_ip'] == -1) {
364  printmsg("DEBUG => Invalid IP address ({$orig_ip})",3);
365  $self['error'] = "ERROR => Invalid IP address ({$orig_ip})";
366  return(array(5, $self['error'] . "\n"));
367  }
368 
369  // Validate that there isn't already another interface with the same IP address
370  list($status, $rows, $record) = ona_get_interface_record("ip_addr = {$options['set_ip']}");
371  if ($rows and $record['id'] != $interface['id']) {
372  printmsg("DEBUG => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") is already in use!",3);
373  $self['error'] = "ERROR => IP conflict: specified IP (" . ip_mangle($orig_ip,'dotted') . ") is already in use!";
374  return(array(6, $self['error'] . "\nINFO => Conflicting interface record ID: {$record['ID']}\n"));
375  }
376 
377  // Since the IP seems available, let's double check and make sure it's not in a DHCP address pool
378  list($status, $rows, $pool) = ona_get_dhcp_pool_record("ip_addr_start <= '{$options['set_ip']}' AND ip_addr_end >= '{$options['set_ip']}'");
379  if ($status or $rows) {
380  printmsg("DEBUG => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") falls within a DHCP address pool!",3);
381  $self['error'] = "ERROR => IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") falls within a DHCP address pool!";
382  return(array(5, $self['error'] . "\n" .
383  "INFO => Conflicting DHCP pool record ID: {$pool['id']}\n"));
384  }
385 
386  // Find the Subnet (network) ID to use from the IP address
387  list($status, $rows, $subnet) = ona_find_subnet(ip_mangle($options['set_ip'], 'dotted'));
388  if ($status or !$rows) {
389  printmsg("DEBUG => That IP address (" . ip_mangle($orig_ip,'dotted') . ") is not inside a defined subnet!",3);
390  $self['error'] = "ERROR => That IP address (" . ip_mangle($orig_ip,'dotted') . ") is not inside a defined subnet!";
391  return(array(7, $self['error'] . "\n"));
392  }
393 
394  // Validate that the IP address supplied isn't the base or broadcast of the subnet
395  if ((is_ipv4($options['set_ip']) && ($options['set_ip'] == $subnet['ip_addr'])) || (!is_ipv4($options['set_ip']) && (!gmp_cmp(gmp_init($options['set_ip']),gmp_init($subnet['ip_addr'])))) ) {
396  printmsg("DEBUG => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's base address!",3);
397  $self['error'] = "ERROR => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's base address!";
398  return(array(8, $self['error'] . "\n"));
399  }
400  if (is_ipv4($options['set_ip']) && ($options['set_ip'] == ((4294967295 - $subnet['ip_mask']) + $subnet['ip_addr']) )
401  || (!is_ipv4($options['set_ip']) && (!gmp_cmp(gmp_init($options['set_ip']),gmp_add(gmp_init($subnet['ip_addr']),gmp_sub("340282366920938463463374607431768211455", $subnet['ip_mask'])))))
402  ) {
403  printmsg("DEBUG => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be a subnet's broadcast address!",3);
404  $self['error'] = "ERROR => IP address (" . ip_mangle($orig_ip,'dotted') . ") can't be the subnet broadcast address!";
405  return(array(9, $self['error'] . "\n"));
406  }
407 
408  // Allow some overrides.
409  if ($options['force'] != 'Y') {
410  // Search for any existing interfaces on the same subnet
411 // list($status, $rows, $record) = ona_get_interface_record(array('subnet_id' => $subnet['id'],
412 // 'host_id' => $interface['host_id']));
413 
414  // Check to be sure we don't exceed maximum lengths
415  if(strlen($options['name']) > 255) {
416  $self['error'] = "ERROR => 'name' exceeds maximum length of 255 characters.";
417  return(array(2, $self['error'] . "\n" .
418  "NOTICE => You may ignore this error and add the interface anyway with the \"force=yes\" option.\n"));
419  }
420 
421  if(strlen($options['description']) > 255) {
422  $self['error'] = "ERROR => 'description' exceeds maximum length of 255 characters.";
423  return(array(2, $self['error'] . "\n" .
424  "NOTICE => You may ignore this error and add the interface anyway with the \"force=yes\" option.\n"));
425  }
426  }
427 
428  // Make sure we update the ptr record domain if needed.
429  // MP: TODO: would it be better to run the dns_modify module vs doing a direct db_update_record???
430  $ipflip = ip_mangle($options['set_ip'],'flip');
431  $octets = explode(".",$ipflip);
432  if (count($octets) > 4) {
433  $arpa = '.ip6.arpa';
434  $octcount = 31;
435  } else {
436  $arpa = '.in-addr.arpa';
437  $octcount = 3;
438  }
439  // Find a pointer zone for this record to associate with.
440  list($status, $prows, $ptrdomain) = ona_find_domain($ipflip.$arpa);
441  if (isset($ptrdomain['id'])) {
442  list($status, $rows, $dnsrec) = ona_get_dns_record(array('type' => 'PTR','interface_id' => $interface['id']));
443 
444  // If the new ptrdomain does not match an existing ptr records domain then we need to change it.
445  if ($rows>0 and $dnsrec['domain_id'] != $ptrdomain['id']) {
446  list($status, $rows) = db_update_record($onadb, 'dns', array('id' => $dnsrec['id']), array('domain_id' => $ptrdomain['id'], 'ebegin' => date('Y-m-j G:i:s')));
447  if ($status or !$rows) {
448  $self['error'] = "ERROR => interface_modify() PTR record domain update failed: " . $self['error'];
449  printmsg($self['error'], 0);
450  return(array(14, $self['error'] . "\n"));
451  }
452  }
453  }
454 
455  // TRIGGER: Since we are changing the IP of an interface that dns records may point to, we need to loop through them all
456  if($interface['ip_addr'] != $options['set_ip']) {
457  // Get all the DNS records using this interface ID
458  list($status, $rows, $records) = db_get_records($onadb, 'dns', array('interface_id' => $interface['id']));
459  // Loop them and set their domains for rebuild
460  foreach($records as $record) {
461  list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $record['domain_id']), array('rebuild_flag' => 1));
462  if ($status) {
463  $self['error'] = "ERROR => dns_record_add() Unable to update rebuild flags for domain.: {$self['error']}";
464  printmsg($self['error'],0);
465  return(array(7, $self['error'] . "\n"));
466  }
467  }
468  }
469 
470  // Check permissions
471 // if (!authlvl($subnet['LVL'])) {
472 // $self['error'] = "Permission denied!";
473 // printmsg($self['error'], 0);
474 // return(array(13, $self['error'] . "\n"));
475 // }
476 
477  // Everything looks ok, add it to $SET
478  if($interface['subnet_id'] != $subnet['id'])
479  $SET['subnet_id'] = $subnet['id'];
480  if($interface['ip_addr'] != $options['set_ip'])
481  $SET['ip_addr'] = $options['set_ip'];
482  }
483 
484 
485  // Setting an MAC address?
486  if (array_key_exists('set_mac', $options)) {
487  if ($options['set_mac']) { // allow null mac addresses (to unset one for example)
488  $options['set_mac'] = trim($options['set_mac']);
489  $orig_mac = $options['set_mac'];
490  $options['set_mac'] = mac_mangle($options['set_mac'], 1);
491  if ($options['set_mac'] == -1) {
492  printmsg("DEBUG => The MAC address specified ({$orig_mac}) is invalid!",3);
493  $self['error'] = "ERROR => The MAC address specified ({$orig_mac}) is invalid!";
494  return(array(11, $self['error'] . "\n"));
495  }
496 
497  // Unless they have opted to allow duplicate mac addresses ...
498  if ($options['force'] != 'Y') {
499  // Validate that there isn't already another interface with the same MAC address on another host
500  // Assume duplicate macs on the same host are ok
501  list($status, $rows, $record) = db_get_record($onadb, 'interfaces', "mac_addr LIKE '{$options['set_mac']}' AND host_id != {$interface['host_id']}");
502  if (($rows and $record['id'] != $interface['id']) or $rows > 1) {
503  printmsg("DEBUG => MAC conflict: That MAC address ({$options['set_mac']}) is already in use on another host!",3);
504  $self['error'] = "ERROR => MAC conflict: That MAC address ({$options['set_mac']}) is already in use on another host!";
505  return(array(12, $self['error'] . "\n" .
506  "NOTICE => You may ignore this error and update the interface anyway with the \"force=yes\" option.\n" .
507  "INFO => Conflicting interface record ID: {$record['id']}\n"));
508  }
509  }
510  }
511  if($interface['mac_addr'] != $options['set_mac'])
512  $SET['mac_addr'] = $options['set_mac'];
513  }
514 
515  // Check the date formatting etc
516  if (isset($options['set_last_response'])) {
517  // format the time that was passed in for the database
518  $SET['last_response']=date('Y-m-j G-i-s',strtotime($options['set_last_response']));
519  }
520 
521  // Set options[set_name]?
522  if (array_key_exists('set_name', $options) && $interface['name'] != $options['set_name']) {
523  $SET['name'] = trim($options['set_name']);
524  }
525 
526  // Set options[set_description]?
527  if (array_key_exists('set_description', $options) && $interface['description'] != $options['set_description']) {
528  $SET['description'] = $options['set_description'];
529  }
530 
531  // Check permissions
532  list($status, $rows, $host) = ona_find_host($interface['host_id']);
533  if (!auth('interface_modify')) {
534  $self['error'] = "Permission denied!";
535  printmsg($self['error'], 0);
536  return(array(13, $self['error'] . "\n"));
537  }
538 
539  // Get the interface record before updating (logging)
540  list($status, $rows, $original_interface) = ona_get_interface_record(array('id' => $interface['id']));
541 
542  // Update the interface record
543  if(count($SET) > 0) {
544  list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), $SET);
545  if ($status or !$rows) {
546  $self['error'] = "ERROR => interface_modify() SQL Query failed: " . $self['error'];
547  printmsg($self['error'], 0);
548  return(array(14, $self['error'] . "\n"));
549  }
550  }
551 
552  // Get the interface record after updating (logging)
553  list($status, $rows, $new_interface) = ona_get_interface_record(array('id' => $interface['id']));
554 
555  list($status, $rows, $new_int) = ona_find_interface($interface['id']);
556 
557  // Return the success notice
558  $text = format_array($SET);
559  $self['error'] = "INFO => Interface UPDATED:{$interface['id']}: {$new_int['ip_addr_text']}";
560 
561  $log_msg = "INFO => Interface UPDATED:{$interface['id']}:{$new_int['ip_addr_text']}: ";
562  $more="";
563  foreach(array_keys($original_interface) as $key) {
564  if($original_interface[$key] != $new_interface[$key]) {
565  $log_msg .= $more . $key . "[" .$original_interface[$key] . "=>" . $new_interface[$key] . "]";
566  $more= ";";
567  }
568  }
569 
570  // only print to logfile if a change has been made to the record
571  if($more != '') printmsg($log_msg, 0);
572 
573  return(array(0, $self['error'] . "\n{$text}\n"));
574 
575 }
576 
577 
578 
579 
580 
581 
582 
583 
584 
586 // Function: interface_del (string $options='')
587 //
588 // Input Options:
589 // $options = key=value pairs of options for this function.
590 // multiple sets of key=value pairs should be separated
591 // by an "&" symbol.
592 //
593 // Output:
594 // Returns a two part list:
595 // 1. The exit status of the function. 0 on success, non-zero on
596 // error. All errors messages are stored in $self['error'].
597 // 2. A textual message for display on the console or web interface.
598 //
599 // Example: list($status, $message) = interface_del('host=lnx100');
601 function interface_del($options="") {
602  global $conf, $self, $onadb;
603 
604  // Version - UPDATE on every edit!
605  $version = '1.05';
606 
607  printmsg("DEBUG => interface_del({$options}) called", 3);
608 
609  // Parse incoming options string to an array
610  $options = parse_options($options);
611 
612  // Return the usage summary if we need to
613  if ($options['help'] or (!$options['interface'])) {
614  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
615  $self['error'] = 'ERROR => Insufficient parameters';
616  return(array(1,
617 <<<EOM
618 
620 Deletes an interface (ip address) from the database
621 
622  Synopsis: interface_del [KEY=VALUE] ...
623 
624  Required:
625  interface=[ID|IP|MAC] delete interface by search string
626 
627  Optional:
628  commit=[yes|no] commit db transaction (no)
629 
630  Notes:
631  * If search returns more than one interface, the first will be deleted
632  * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified
633 \n
634 EOM
635  ));
636  }
637 
638 
639  // Sanitize "options[commit]" (no is the default)
640  $options['commit'] = sanitize_YN($options['commit'], 'N');
641 
642  // Check permissions
643  if (!auth('interface_del') or !auth('host_del')) {
644  $self['error'] = "Permission denied!";
645  printmsg($self['error'], 0);
646  return(array(13, $self['error'] . "\n"));
647  }
648 
649  // They provided a interface ID, IP address, interface name, or MAC address
650  if ($options['interface']) {
651  // Find an interface record by something in that interface's record
652  list($status, $rows, $interface) = ona_find_interface($options['interface']);
653  }
654 
655  // If we didn't get a record then exit
656  if (!$interface or !$interface['id']) {
657  printmsg("DEBUG => Interface not found ({$options['interface']})!",3);
658  $self['error'] = "ERROR => Interface not found ({$options['interface']})!";
659  return(array(4, $self['error'] . "\n"));
660  }
661 
662  // Load associated records
663  list($status, $rows, $host) = ona_find_host($interface['host_id']);
664  list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id']));
665 
666 
667  // If "commit" is yes, delete the interface
668  if ($options['commit'] == 'Y') {
669 
670  $add_to_error = '';
671  $add_to_status = 0;
672 
673  // Check for shared interfaces
674  list($status, $clust_rows, $clust) = db_get_records($onadb, 'interface_clusters', array('interface_id' => $interface['id']), '', 0);
675  printmsg("DEBUG => total shared host records => {$clust_rows}", 3);
676  if ($clust_rows > 0) {
677  printmsg("DEBUG => There are {$clust_rows} hosts sharing this interface, remove the shares first.",3);
678  $self['error'] = "ERROR => There are {$clust_rows} hosts sharing this interface, remove the shares first.";
679  return(array(10, $self['error'] . "\n"));
680  }
681 
682  // Check if this is the last interface on a host but skip it if its the delete host function calling us
683  if (!isset($options['delete_by_module'])) {
684  list($status, $total_interfaces, $ints) = db_get_records($onadb, 'interfaces', array('host_id' => $interface['host_id']), '', 0);
685  printmsg("DEBUG => total interfaces => {$total_interfaces}", 3);
686  if ($total_interfaces == 1) {
687  printmsg("DEBUG => You cannot delete the last interface on a host, you must delete the host itself ({$host['fqdn']}).",3);
688  $self['error'] = "ERROR => You can not delete the last interface on a host, you must delete the host itself ({$host['fqdn']}).";
689  return(array(13, $self['error'] . "\n"));
690  }
691  }
692 
693  printmsg("DEBUG => Deleting interface: ID {$interface['id']}", 3);
694 
695  if ($interface['nat_interface_id'] > 0) {
696  list($status, $output) = run_module('interface_del', array('interface' => $interface['nat_interface_id'], 'commit' => 'Y', 'delete_by_module' => 'Y'));
697  $add_to_status = $add_to_status + $status;
698  }
699 
700  // Delete any DNS records are associated with the host.
701  list($status, $rows, $records) = db_get_records($onadb, 'dns', array('interface_id' => $interface['id']), 'dns_id desc');
702  // Loop through all the records and delete them
703  // This deletes the primary records last based on sort of dns_id and expects dns_record_del to delete child records
704  // but will pick up any PTR records when deleting interfaces with only PTR records.
705  if ($rows) {
706  foreach($records as $record) {
707  $int_dns_deloptions = array('name' => $record['id'], 'type' => $record['type'], 'commit' => 'Y');
708  // If delete_by_module is passed in, add it to the dns_record_del option list
709  // This allows host/subnet deletes to delete what they need but does not allow you to delete a
710  // interface that is used in a primary dns record
711  if (isset($options['delete_by_module'])) $int_dns_deloptions['delete_by_module'] = 'Y';
712  list($status, $output) = run_module('dns_record_del', $int_dns_deloptions);
713  $add_to_error .= $output;
714  $add_to_status = $add_to_status + $status;
715  }
716  }
717 
718 
719  // Drop the record
720  list($status, $rows) = db_delete_records($onadb, 'interfaces', array('id' => $interface['id']));
721  if ($status or !$rows) {
722  $self['error'] = "ERROR => interface_delete() SQL Query failed: " . $self['error'];
723  printmsg($self['error'], 0);
724  return(array(5, $self['error']));
725  }
726  // Build a success notice to return to the user
727  $text = "INFO => Interface DELETED: " . ip_mangle($interface['ip_addr'], 'dotted') . " from {$host['fqdn']}";
728  printmsg($text, 0);
729 
730  // Check to see if there are any other interfaces for the current host_id
731  // If there aren't, we need to tell the user to delete the host!
732  // since we've disallowed removal of the last interface, this should never happen!!!!!
733  if (!isset($options['delete_by_module'])) {
734  list($status, $rows, $record) = ona_get_interface_record(array('host_id' => $interface['host_id']));
735  if ($rows == 0) {
736  printmsg("WARNING => Host {$host['fqdn']} has NO remaining interfaces!", 0);
737  $text .= "\n" . "WARNING => Host {$host['fqdn']} has NO remaining interfaces!\n" .
738  " Delete this host or add an interface to it now!\n";
739  }
740  }
741 
742  // Return the success notice
743  return(array($add_to_status, $add_to_error . $text));
744  }
745 
746  // Otherwise, just display the interface that we will be deleting
747  list($status, $displaytext) = interface_display("interface={$interface['id']}&verbose=N");
748 
749  $text = "Record(s) NOT DELETED (see \"commit\" option)\n" .
750  "Displaying record(s) that would have been deleted:\n";
751 
752  // Display records if this is a shared interface
753  list($status, $clust_rows, $clust) = db_get_records($onadb, 'interface_clusters', array('interface_id' => $interface['id']));
754  if ($clust_rows) {
755  $text .= "\nWARNING! This interface is shared with {$clust_rows} other host(s).\n";
756  $text .= " Please remove all associated hosts first.\n";
757  }
758 
759  // Display records if this is the last interface
760  list($status, $total_interfaces, $ints) = db_get_records($onadb, 'interfaces', array('host_id' => $interface['host_id']), '', 0);
761  if ($total_interfaces == 1) {
762  $text .= "\nWARNING! You cannot delete the last interface on a host,\n";
763  $text .= " you must delete the host itself ({$host['fqdn']})\n";
764  $text .= " Or move the interface to another host and delete {$host['fqdn']}.\n";
765  }
766  $text .= "\n" . $displaytext;
767  if ($clust_rows) $text .= "\nASSOCIATED SHARED INTERFACE RECORDS ({$clust_rows}):\n";
768  foreach ($clust as $record) {
769  list($status, $rows, $clusthost) = ona_get_host_record(array('id' => $record['host_id']));
770  $text .= " {$clusthost['fqdn']}\n";
771  }
772 
773  // Display DNS records associated with this interface
774  list($status, $total_dns, $dns) = db_get_records($onadb, 'dns', array('interface_id' => $interface['id']), '');
775  if ($total_dns) $text .= "\nASSOCIATED DNS RECORDS ({$total_dns}):\n";
776  foreach ($dns as $rec) {
777  $text .= " TYPE: [ID:{$rec['id']}] {$rec['type']}, {$rec['name']} -> {$interface['ip_addr_text']}\n";
778  }
779 
780  if ($interface['nat_interface_id'] > 0) {
781  printmsg("DEBUG => interface_del() calling interface_del() for external NAT ip: {$options['nat_interface_id']}", 3);
782  $natint['interface'] = $interface['nat_interface_id'];
783  $natint['commit'] = $options['commit'];
784  list($status, $output) = run_module('interface_del', $natint);
785  // if ($status) { return(array($status, $output)); }
786  $text .= "\nASSOCIATED NAT INTERFACE DELETE:\n\n";
787  $text .= $output . "\n";
788  }
789 
790  return(array(6, $text));
791 
792 }
793 
794 
795 
796 
797 
798 
799 
800 
801 
802 
803 
805 // Function: interface_display (string $options='')
806 //
807 // Input Options:
808 // $options = key=value pairs of options for this function.
809 // multiple sets of key=value pairs should be separated
810 // by an "&" symbol.
811 //
812 // Output:
813 // Returns a two part list:
814 // 1. The exit status of the function. 0 on success, non-zero on
815 // error. All errors messages are stored in $self['error'].
816 // 2. A textual message for display on the console or web interface.
817 //
818 // Example: list($status, $result) = interface_display('host=lnx100');
820 function interface_display($options="") {
821  global $conf, $self;
822 
823  $text_array = array();
824 
825  // Version - UPDATE on every edit!
826  $version = '1.03';
827 
828  printmsg("DEBUG => interface_display({$options}) called", 3);
829 
830  // Parse incoming options string to an array
831  $options = parse_options($options);
832 
833  // Return the usage summary if we need to
834  if ($options['help'] or (!$options['host'] and !$options['interface'])) {
835  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
836  $self['error'] = 'ERROR => Insufficient parameters';
837  return(array(1,
838 <<<EOM
839 
841 Displays an interface record from the database
842 
843  Synopsis: interface_display [KEY=VALUE] ...
844 
845  Required:
846  interface=[ID|IP|MAC] display interface by search string
847  or
848  host=NAME[.DOMAIN] or ID display interface by hostname or host_id
849 
850  Optional:
851  verbose=[yes|no] display additional info (yes)
852 
853  Notes:
854  * If search returns more than one interface, an error is displayed
855  * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified
856 \n
857 EOM
858  ));
859  }
860 
861  // Sanitize "options[verbose]" (yes is the default)
862  $options['verbose'] = sanitize_YN($options['verbose'], 'Y');
863 
864  // They provided a interface ID, IP address, interface name, or MAC address
865  if ($options['interface']) {
866  // Find an interface record by something in that interface's record
867  list($status, $rows, $interface) = ona_find_interface($options['interface']);
868  }
869 
870  // If a hostname was provided, do a search based on that
871  else if ($options['host']) {
872  // Find a host by the user's input
873  list($status, $rows, $host) = ona_find_host($options['host']);
874  if (!$host['id']) {
875  printmsg("DEBUG => Host not found ({$options['host']})!",3);
876  $self['error'] = "ERROR => Host not found ({$options['host']})";
877  return(array(2, $self['error'] . "\n"));
878  }
879  // If we got one, load an associated interface
880  list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id']));
881  }
882 
883  // If we didn't get a record then exit
884  if (!$interface['id']) {
885  if ($rows > 1)
886  $self['error'] = "ERROR => More than one interface matches";
887  else
888  $self['error'] = "ERROR => Interface not found ({$options['interface']})";
889  return(array(4, $self['error'] . "\n"));
890  }
891 
892  $text_array = $interface;
893 
894  // Build text to return
895  $text = "INTERFACE RECORD\n";
896  $text .= format_array($interface);
897 
898  // If 'verbose' is enabled, grab some additional info to display
899  if ($options['verbose'] == 'Y') {
900 
901  // Host record
902  list($status, $rows, $host) = ona_get_host_record(array('ID' => $interface['host_id']));
903  if ($rows >= 1) {
904  $text .= "\nASSOCIATED HOST RECORD\n";
906  }
907 
908  // Subnet record
909  list($status, $rows, $subnet) = ona_get_subnet_record(array('ID' => $interface['subnet_id']));
910  if ($rows >= 1) {
911  $text .= "\nASSOCIATED SUBNET RECORD\n";
912  $text .= format_array($subnet);
913  }
914 
915  // Device record
916  list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id']));
917  if ($rows >= 1) {
918  $text .= "\nASSOCIATED DEVICE RECORD\n";
919  $text .= format_array($device);
920  }
921 
922  }
923 
924  // change the output format if other than default
925  if ($options['format'] == 'json') {
926  $text = $text_array;
927  }
928  if ($options['format'] == 'yaml') {
929  $text = $text_array;
930  }
931 
932  // Return the success notice
933  return(array(0, $text));
934 
935 }
936 
937 
938 
939 
940 
941 
942 
943 
944 
945 
946 
948 // Function: interface_move (string $options='')
949 //
950 // Input Options:
951 // $options = key=value pairs of options for this function.
952 // multiple sets of key=value pairs should be separated
953 // by an "&" symbol.
954 //
955 // Output:
956 // Returns a two part list:
957 // 1. The exit status of the function. 0 on success, non-zero on
958 // error. All errors messages are stored in $self['error'].
959 // 2. A textual message for display on the console or web interface.
960 //
961 // Example: list($status, $result) = interface_move_subnet('');
963 function interface_move($options="") {
964  global $conf, $self, $onadb;
965  printmsg("DEBUG => interface_move({$options}) called", 3);
966 
967  // Version - UPDATE on every edit!
968  $version = '1.04';
969 
970  // Parse incoming options string to an array
971  $options = parse_options($options);
972 
973  // Return the usage summary if we need to
974  if ($options['help'] or
975  (!$options['start']) or
976  (!$options['new_start'])
977  ) {
978  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
979  $self['error'] = 'ERROR => Insufficient parameters';
980  return(array(1,
981 <<<EOM
982 
984  Moves all interface addresses from one subnet to another.
985  The initial range of IPs does not have to be consecutive.
986  The new range of IPs will be used sequentially.
987 
988  Synopsis: interface_move [KEY=VALUE] ...
989 
990  IP block to move: (source)
991  start=IP first IP to move
992  [end=IP] last IP to move
993 
994  New IP block: (destination)
995  new_start=IP first new IP address
996  [new_end=IP] last new IP address
997 
998  Optional:
999  commit=[yes|no] commit db transaction (no)
1000 \n
1001 EOM
1002  ));
1003  }
1004 
1005 
1006  // Set options[force] and options[create_a] to N if it's not set
1007  $options['commit'] = sanitize_YN($options['commit'], 'N');
1008 
1009  // Find the "start" subnet record by IP address
1010  list($status, $rows, $old_subnet) = ona_find_subnet($options['start']);
1011  if (!$old_subnet or !$old_subnet['id']) {
1012  printmsg("DEBUG => Source start address ({$options['start']}) isn't valid!", 3);
1013  $self['error'] = "ERROR => Source (start) address specified isn't valid!";
1014  return(array(2, $self['error'] . "\n"));
1015  }
1016 
1017  // If they specified an "END" address, make sure it's valid and on the same subnet
1018  if ($options['end']) {
1019  // Find an interface record by something in that interface's record
1020  list($status, $rows, $old_subnet_end) = ona_find_subnet($options['end']);
1021 
1022  // If we didn't get a record then exit
1023  if (!$old_subnet_end or !$old_subnet_end['id']) {
1024  printmsg("DEBUG => Source end address ({$options['end']}) isn't valid!", 3);
1025  $self['error'] = "ERROR => Source (end) address specified isn't valid!";
1026  return(array(3, $self['error'] . "\n"));
1027  }
1028  if ($old_subnet_end['id'] != $old_subnet['id']) {
1029  printmsg("DEBUG => Both source addresses ({$options['start']} and {$options['end']}) must be on the same subnet!", 3);
1030  $self['error'] = "ERROR => Both the source addresses (start and end) must be on the same subnet!";
1031  return(array(4, $self['error'] . "\n"));
1032  }
1033  }
1034  // If they didn't give an end, they're moving one host..
1035  // but to make our lives easier we set the "end" = "start";
1036  else {
1037  printmsg("DEBUG => Only moving one host source={$options['start']}!", 3);
1038  $options['end'] = $options['start'];
1039  }
1040 
1041 
1042  // Find the "end" subnet record by IP address
1043  list($status, $rows, $new_subnet) = ona_find_subnet($options['new_start']);
1044 
1045  // If we didn't get a record then exit
1046  if (!$new_subnet or !$new_subnet['id']) {
1047  printmsg("DEBUG => Destination start address ({$options['new_start']}) isn't valid!", 3);
1048  $self['error'] = "ERROR => Destination (new_start) address specified isn't valid!";
1049  return(array(2, $self['error'] . "\n"));
1050  }
1051  // Make sure the "old" and "new" subnets are different subnets
1052  if ($old_subnet['id'] == $new_subnet['id']) {
1053  printmsg("DEBUG => Both the source IP range ({$options['start']}+) and the destination IP range ({$options['new_start']}+) are on the same subnet!", 3);
1054  $self['error'] = "ERROR => Both the source IP range and the destination IP range are on the same subnet!";
1055  return(array(2, $self['error'] . "\n"));
1056  }
1057 
1058  // If they specified a "new_end" address, make sure it's valid and on the same subnet as the new_start subnet
1059  if ($options['new_end']) {
1060  // Find an interface record by something in that interface's record
1061  list($status, $rows, $new_subnet_end) = ona_find_subnet($options['new_end']);
1062 
1063  // If we didn't get a record then exit
1064  if (!$new_subnet_end or !$new_subnet_end['id']) {
1065  printmsg("DEBUG => Destination end address ({$options['new_end']}) isn't valid!", 3);
1066  $self['error'] = "ERROR => Destination (new_end) address specified isn't valid!";
1067  return(array(3, $self['error'] . "\n"));
1068  }
1069  if ($new_subnet_end['id'] != $new_subnet['id']) {
1070  printmsg("DEBUG => Both destination addresses ({$options['new_start']} and {$options['new_end']}) must be on the same subnet!", 3);
1071  $self['error'] = "ERROR => Both the destination addresses (new_start and new_end) must be on the same subnet!";
1072  return(array(4, $self['error'] . "\n"));
1073  }
1074  }
1075  // If they didn't give an end, they're moving one host..
1076  // but to make our lives easier we set the "end" = "start";
1077  else {
1078  printmsg("DEBUG => Only moving one host destination={$options['new_start']}!", 3);
1079  $options['new_end'] = $options['new_start'];
1080  }
1081 
1082 
1083  // Check permissions at the subnet level
1084  if (!auth('interface_modify')) {
1085  $self['error'] = "Permission denied!";
1086  printmsg($self['error'], 0);
1087  return(array(13, $self['error'] . "\n"));
1088  }
1089 
1090 
1091  // An array for all the interface records we'll be moving
1092  $to_move = array();
1093 
1094  // Message to display if we succeed
1095  $message = "";
1096 
1097  // Load all the interface records we'll be moving
1098  $i = 0;
1099  do {
1100  // FIXME: this should do a more advanced query someday! (like checking that the ipaddress is >= start and <= end
1101  list($status, $rows, $interface) = ona_get_interface_record(array('subnet_id' => $old_subnet['id']), 'ip_addr');
1102  if ($rows == 0) break;
1103  $i++;
1104  if ($interface['ip_addr'] >= ip_mangle($options['start'], 'numeric')) {
1105  if ($interface['ip_addr'] <= ip_mangle($options['end'], 'numeric')) {
1106  $to_move[$i] = $interface;
1107  }
1108  }
1109  } while ($i < $rows);
1110 
1111  $total_to_move = count($to_move);
1112  $total_assigned = 0;
1113 
1114  // If there's nothing to do, tell them
1115  if ($total_to_move == 0) {
1116  printmsg("DEBUG => There are no interfaces in the source address block!", 3);
1117  $self['error'] = "ERROR => There are no interfaces in the source address block!";
1118  return(array(6, $self['error'] . "\n"));
1119  }
1120 
1121  // Make sure we have a high enough "LVL" to modify the associated hosts
1122  foreach ($to_move as $interface) {
1123  // Load the associated host record
1124  list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id']));
1125  list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A'));
1126  // Check permissions at the subnet level
1127  if (!authlvl($host['LVL'])) {
1128  $self['error'] = "Permission denied! Can't modify Host: {$host['id']} {$dns['fqdn']}";
1129  printmsg($self['error'], 0);
1130  return(array(14, $self['error'] . "\n"));
1131  }
1132  // Check to see if the host has any interfaces in the destination subnet
1133 // MP: this is now allowed
1134 // list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $interface['host_id'], 'subnet_id' => $new_subnet['id']));
1135 // if ($status or $rows) {
1136 // printmsg("DEBUG => Source host {$ddns['fqdn']} already has an interface on the destination subnet!",3);
1137 // $self['error'] = "ERROR => Source host {$dns['fqdn']} (ID {$host['id']}) already has an interface on the destination subnet!";
1138 // return(array(15, $self['error'] . "\n"));
1139 // }
1140  }
1141 
1142  // Get the numeric version of the start/end addresses we are moving interfaces to
1143  // .. and make sure that the $low_ip and $high_ip are not subnet or broadcast addresses!
1144  $low_ip = ip_mangle($options['new_start'], 'numeric');
1145  $high_ip = ip_mangle($options['new_end'], 'numeric');
1146  if ($low_ip == $new_subnet['ip_addr']) { $low_ip++; }
1147  $num_hosts = 0xffffffff - $new_subnet['ip_mask'];
1148  if ($high_ip == ($new_subnet['ip_addr'] + $num_hosts)) { $high_ip--; }
1149  printmsg("INFO => Asked to move {$total_to_move} interfaces to new range: " . ip_mangle($low_ip, 'dotted') . ' - ' . ip_mangle($high_ip, 'dotted'), 0);
1150 
1151  // Loop through each interface we need to move, and find an available address for it.
1152  $pool_interfering = 0;
1153  foreach (array_keys($to_move) as $i) {
1154  while ($low_ip <= $high_ip) {
1155  list($status, $rows, $interface) = ona_get_interface_record(array('ip_addr' => $low_ip));
1156  if ($rows == 0 and $status == 0) {
1157  // Since the IP seems available, let's double check and make sure it's not in a DHCP address pool
1158  list($status, $rows, $pool) = ona_get_dhcp_pool_record("ip_addr_start < '{$low_ip}' AND ip_addr_end > '{$low_ip}'");
1159  if ($rows == 0 and $status == 0) {
1160  // The IP is available, lets use it!
1161  $to_move[$i]['new_ip_address'] = $low_ip;
1162  $total_assigned++;
1163  $low_ip++;
1164  break;
1165  }
1166  $pool_interfering = 1;
1167  printmsg("DEBUG => Couldn't use the DHCP POOL address: " . ip_mangle($low_ip, 'dotted'), 3);
1168  }
1169  $low_ip++;
1170  }
1171  }
1172 
1173  // If total_assigned != total_to_move, error - not enough free IP addresses in destination subnet!
1174  if ($total_assigned != $total_to_move) {
1175  printmsg("DEBUG => The destination IP range doesn't have enough free IP addresses!", 3);
1176  $self['error'] = "ERROR => The destination IP range doesn't have enough free IP addresses!\n";
1177  if ($pool_interfering)
1178  $self['error'] .= "INFO => Some IPs in the destination range were part of a DHCP pool range.\n";
1179  return(array(6, $self['error']));
1180  }
1181 
1182 
1183  // Display what we would have done if "commit" isn't "yes"
1184  if ($options['commit'] != "Y") {
1185  $self['error'] = "Interface(s) NOT MOVED (see \"commit\" option)";
1186  $text = $self['error'] . "\n" .
1187  "Displaying {$total_to_move} interface(s) that would have been moved:\n\n";
1188  foreach ($to_move as $interface) {
1189  // Get display the hostname we would have moved, as well as it's IP address.
1190  list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id']));
1191  list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A'));
1192  $hostname = strtolower("{$dns['fqdn']}");
1193  $text .= " " . ip_mangle($interface['ip_addr'], 'dotted') . " -> " . ip_mangle($interface['new_ip_address'], 'dotted') . "\t({$hostname})\n";
1194  }
1195  $text .= "\n";
1196  return(array(7, $text));
1197  }
1198 
1199  // Loop through and update each interface's IP_ADDRESS and SUBNET_ID
1200  $text = "SUCCESS => {$total_to_move} interface(s) moved\n";
1201  $text .= "Interface(s) moved:\n\n";
1202  foreach ($to_move as $interface) {
1203  list($status, $rows) = ona_update_record("interfaces",
1204  // Where:
1205  array('id' => $interface['id']),
1206  // Update:
1207  array(
1208  'ip_addr' => $interface['new_ip_address'],
1209  'subnet_id' => $new_subnet['id'],
1210  )
1211  );
1212  if ($status != 0 or $rows != 1) {
1213  $self['error'] = "ERROR => Database update failed! {$self['error']}";
1214  return(array(8, $self['error'] . "\n"));
1215  }
1216  // Get display the hostname we would have moved, as well as its IP address.
1217  list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id']));
1218  list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A'));
1219  $hostname = strtolower("{$dns['fqdn']}");
1220  $text .= " " . ip_mangle($interface['ip_addr'], 'dotted') . " -> " . ip_mangle($interface['new_ip_address'], 'dotted') . "\t({$hostname})\n";
1221  }
1222 
1223  // Return the success notice
1224  return(array(0, $text));
1225 }
1226 
1227 
1228 
1229 
1230 
1232 // Function: interface_move_host (string $options='')
1233 //
1234 // Input Options:
1235 // $options = key=value pairs of options for this function.
1236 // multiple sets of key=value pairs should be separated
1237 // by an "&" symbol.
1238 //
1239 // Output:
1240 // Returns a two part list:
1241 // 1. The exit status of the function. 0 on success, non-zero on
1242 // error. All errors messages are stored in $self['error'].
1243 // 2. A textual message for display on the console or web interface.
1244 //
1245 // Example: list($status, $result) = interface_move_host('');
1247 function interface_move_host($options="") {
1248  global $conf, $self, $onadb;
1249  printmsg("DEBUG => interface_move_host({$options}) called", 3);
1250 
1251  // Version - UPDATE on every edit!
1252  $version = '1.00';
1253 
1254  // Parse incoming options string to an array
1255  $options = parse_options($options);
1256 
1257  // Return the usage summary if we need to
1258  if ($options['help'] or !($options['host'] and $options['ip']) ) {
1259  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
1260  $self['error'] = 'ERROR => Insufficient parameters';
1261  return(array(1,
1262 <<<EOM
1263 
1265  Move an interface to a new host
1266 
1267  Synopsis: interface_move_host [KEY=VALUE] ...
1268 
1269  Required:
1270  ip=[address|ID] the IP address or ID of the interface
1271  host=[fqdn|ID] the fqdn or ID of the new host
1272 
1273 \n
1274 EOM
1275  ));
1276  }
1277 
1278 
1279  // Find the Host they are looking for
1280  list($status, $rows, $host) = ona_find_host($options['host']);
1281  if (!$host['id']) {
1282  printmsg("DEBUG => The host specified, {$options['host']}, does not exist!",3);
1283  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
1284  return(array(2, $self['error'] . "\n"));
1285  }
1286  printmsg("DEBUG => Host selected: {$options['host']}", 3);
1287 
1288 
1289  // Find the interface that is moving
1290  list($status, $rows, $interface) = ona_find_interface($options['ip']);
1291  if (!$interface['id']) {
1292  printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!",3);
1293  $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!";
1294  return(array(3, $self['error'] . "\n"));
1295  }
1296 
1297  // check if this interface is the primary DNS interface address.
1298  list($status, $rows, $primaryhost) = ona_get_host_record(array('id' => $interface['host_id']));
1299  list($status, $rows, $primarydns) = ona_get_dns_record(array('id' => $primaryhost['primary_dns_id']));
1300  if ($primarydns['interface_id'] == $interface['id']) {
1301  printmsg("DEBUG => This interface is part of the primary DNS name for {$primaryhost['fqdn']}, please assign a new primary DNS.",3);
1302  $self['error'] = "ERROR => This interface is part of the primary DNS name for {$primaryhost['fqdn']}, please assign a new primary DNS.";
1303  return(array(4, $self['error'] . "\n"));
1304  }
1305 
1306  // if this is the last interface on the host display a message
1307  // TODO: MP is this best? I would think a lot of people WANT to move the last IP before removing the host
1308  // it would cut some steps of having to delete/re-add when moving an IP. maybe allow this?!?
1309  // ------ Since most hosts use the last interface as a primary dns id then they cant move the last interface.--------
1310 
1311 // list($status, $rows, $int) = db_get_records($onadb, 'interfaces', array('host_id' => $interface['host_id'], '', 0);
1312 // if ($rows == 1) {
1313 // printmsg("DEBUG => You cannot delete the last interface on a host, you must delete the host itself ({$host['fqdn']}).",3);
1314 // $self['error'] = "ERROR => You can not delete the last interface on a host, you must delete the host itself ({$host['fqdn']}).";
1315 // return(array(5, $self['error'] . "\n"));
1316 // }
1317  printmsg("DEBUG => Interface selected: {$options['ip']}", 3);
1318 
1319  // Check that this interface is not associated with this host via an interface_cluster
1320  list($status, $rows, $int_cluster) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id'],'interface_id' => $interface['id']), '', 0);
1321  printmsg("DEBUG => interface_move_host() New host is clustered with this IP, Deleting cluster record", 3);
1322  if ($rows == 1) {
1323  // Delete the interface_cluster if there is one
1324  list($status, $rows) = db_delete_records($onadb, 'interface_clusters', array('interface_id' => $interface['id'],'host_id' => $host['id']));
1325  if ($status or !$rows) {
1326  $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error'];
1327  printmsg($self['error'], 0);
1328  return(array(14, $self['error'] . "\n"));
1329  }
1330  }
1331 
1332  // If the interface being moved has a NAT IP then the ext interface needs the host_id updated as well
1333  if ($interface['nat_interface_id'] > 0) {
1334  printmsg("DEBUG => interface_move_host() Moving interface with NAT IP.", 3);
1335  list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['nat_interface_id']), array('host_id' => $host['id']));
1336  if ($status or !$rows) {
1337  $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error'];
1338  printmsg($self['error'], 0);
1339  return(array(15, $self['error'] . "\n"));
1340  }
1341  }
1342 
1343  // Update the interface record
1344  list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), array('host_id' => $host['id']));
1345  if ($status or !$rows) {
1346  $self['error'] = "ERROR => interface_move_host() SQL Query failed: " . $self['error'];
1347  printmsg($self['error'], 0);
1348  return(array(16, $self['error'] . "\n"));
1349  }
1350 
1351  $text = "INFO => Interface " . ip_mangle($interface['ip_addr'], 'dotted') . " moved to {$host['fqdn']}";
1352  printmsg($text, 0);
1353 
1354  // Return the success notice
1355  return(array(0, $text. "\n"));
1356 }
1357 
1358 
1359 
1360 
1361 
1362 
1363 
1364 
1365 
1366 
1367 
1369 // Function: interface_share (string $options='')
1370 //
1371 // Input Options:
1372 // $options = key=value pairs of options for this function.
1373 // multiple sets of key=value pairs should be separated
1374 // by an "&" symbol.
1375 //
1376 // Output:
1377 // Returns a two part list:
1378 // 1. The exit status of the function. 0 on success, non-zero on
1379 // error. All errors messages are stored in $self['error'].
1380 // 2. A textual message for display on the console or web interface.
1381 //
1382 // Example: list($status, $result) = interface_share('');
1384 function interface_share($options="") {
1385  global $conf, $self, $onadb;
1386  printmsg("DEBUG => interface_share({$options}) called", 3);
1387 
1388  // Version - UPDATE on every edit!
1389  $version = '1.00';
1390 
1391  // Parse incoming options string to an array
1392  $options = parse_options($options);
1393 
1394  // Return the usage summary if we need to
1395  if ($options['help'] or !($options['host'] and $options['ip']) ) {
1396  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
1397  $self['error'] = 'ERROR => Insufficient parameters';
1398  return(array(1,
1399 <<<EOM
1400 
1402  Share an interface with another host.
1403  An IP address only exists once in the database. This allows
1404  you to share that IP with several other hosts which are configured
1405  to use technologies such as HSRP, CARP, VRRP etc.
1406 
1407  Synopsis: interface_share [KEY=VALUE] ...
1408 
1409  Required:
1410  ip=[address|ID] the IP address or ID of the interface
1411  host=[fqdn|ID] the fqdn or ID of the host
1412 
1413  Optional:
1414  name=TEXT interface name used on host, if different
1415 
1416 \n
1417 EOM
1418  ));
1419  }
1420 
1421 
1422  // Find the Host they are looking for
1423  list($status, $rows, $host) = ona_find_host($options['host']);
1424  if (!$host['id']) {
1425  printmsg("DEBUG => The host specified, {$options['host']}, does not exist!",3);
1426  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
1427  return(array(2, $self['error'] . "\n"));
1428  }
1429  printmsg("DEBUG => Host selected: {$options['host']}", 3);
1430 
1431 
1432  // Find the interface
1433  list($status, $rows, $interface) = ona_find_interface($options['ip']);
1434  if (!$interface['id']) {
1435  printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!",3);
1436  $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!";
1437  return(array(3, $self['error'] . "\n"));
1438  }
1439  printmsg("DEBUG => Interface selected: {$options['ip']}", 3);
1440 
1441  // Check that this interface is not associated with this host via an interface_cluster already
1442  list($status, $rows, $int_cluster) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id'],'interface_id' => $interface['id']), '', 0);
1443  if ($rows == 1) {
1444  printmsg("DEBUG => This host is already clustered with that IP ({$host['fqdn']}-{$interface['ip_addr']}).", 3);
1445  $self['error'] = "ERROR => This host is already clustered with that IP ({$host['fqdn']}-{$interface['ip_addr']}).";
1446  return(array(13, $self['error'] . "\n"));
1447  }
1448 
1449 
1450  // Add the interface_cluster entry
1451  list($status, $rows) =
1453  $onadb,
1454  'interface_clusters',
1455  array(
1456  'host_id' => $host['id'],
1457  'interface_id' => $interface['id'],
1458  'name' => $options['name']
1459  )
1460  );
1461  if ($status or !$rows) {
1462  $self['error'] = "ERROR => interface_share() SQL Query failed: " . $self['error'];
1463  printmsg($self['error'], 0);
1464  return(array(14, $self['error'] . "\n"));
1465  }
1466 
1467  // Return the success notice
1468  $self['error'] = "INFO => Interface Shared: " . ip_mangle($interface['ip_addr'], 'dotted') . " to {$host['fqdn']}.";
1469  printmsg($self['error'], 0);
1470  return(array(0, $self['error'] . "\n"));
1471 
1472 
1473 
1474 
1475 }
1476 
1477 
1478 
1479 
1480 
1481 
1482 
1483 
1484 
1486 // Function: interface_share_del (string $options='')
1487 //
1488 // Input Options:
1489 // $options = key=value pairs of options for this function.
1490 // multiple sets of key=value pairs should be separated
1491 // by an "&" symbol.
1492 //
1493 // Output:
1494 // Returns a two part list:
1495 // 1. The exit status of the function. 0 on success, non-zero on
1496 // error. All errors messages are stored in $self['error'].
1497 // 2. A textual message for display on the console or web interface.
1498 //
1499 // Example: list($status, $result) = interface_share_del('');
1501 function interface_share_del($options="") {
1502  global $conf, $self, $onadb;
1503  printmsg("DEBUG => interface_share_del({$options['ip']}) called", 3);
1504 
1505  // Version - UPDATE on every edit!
1506  $version = '1.00';
1507 
1508  // Parse incoming options string to an array
1509  $options = parse_options($options);
1510 
1511  // Return the usage summary if we need to
1512  if ($options['help'] or !($options['host'] and $options['ip']) ) {
1513  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
1514  $self['error'] = 'ERROR => Insufficient parameters';
1515  return(array(1,
1516 <<<EOM
1517 
1519  Delete a shareed interface from another host.
1520  An IP address only exists once in the database. This allows
1521  you to share that IP with several other hosts which are configured
1522  to use technologies such as HSRP, CARP, VRRP etc.
1523 
1524  Synopsis: interface_share_del [KEY=VALUE] ...
1525 
1526  Required:
1527  ip=[address|ID] the IP address or ID of the interface
1528  host=[fqdn|ID] the fqdn or ID of the host
1529 
1530 \n
1531 EOM
1532  ));
1533  }
1534 
1535 
1536  // Find the Host they are looking for
1537  list($status, $rows, $host) = ona_find_host($options['host']);
1538  if (!$host['id']) {
1539  printmsg("DEBUG => The host specified, {$options['host']}, does not exist!",3);
1540  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
1541  return(array(2, $self['error'] . "\n"));
1542  }
1543  printmsg("DEBUG => Host selected: {$options['host']}", 3);
1544 
1545 
1546  // Find the interface
1547  list($status, $rows, $interface) = ona_find_interface($options['ip']);
1548  if (!$interface['id']) {
1549  printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!",3);
1550  $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!";
1551  return(array(3, $self['error'] . "\n"));
1552  }
1553  printmsg("DEBUG => Interface selected: {$options['ip']}", 3);
1554 
1555  // Check that this interface is not associated with this host via an interface_cluster already
1556  list($status, $rows, $int_cluster) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id'],'interface_id' => $interface['id']), '', 0);
1557  if ($rows == 0) {
1558  printmsg("DEBUG => Unable to find share ({$host['fqdn']}-{$interface['ip_addr_text']}).", 3);
1559  $self['error'] = "ERROR => Unable to find share ({$host['fqdn']}-{$interface['ip_addr_text']}).";
1560  return(array(13, $self['error'] . "\n"));
1561  }
1562 
1563  // Drop the record
1564  list($status, $rows) = db_delete_records($onadb, 'interface_clusters', array('host_id' => $host['id'],'interface_id' => $interface['id']));
1565  if ($status or !$rows) {
1566  $self['error'] = "ERROR => interface_share_del() SQL Query failed: " . $self['error'];
1567  printmsg($self['error'], 0);
1568  return(array(5, $self['error']));
1569  }
1570 
1571 
1572  // Return the success notice
1573  $self['error'] = "INFO => Interface Share deleted: {$interface['ip_addr_text']} from {$host['fqdn']}.";
1574  printmsg($self['error'], 0);
1575  return(array(0, $self['error'] . "\n"));
1576 
1577 
1578 
1579 
1580 }
1581 
1582 
1583 
1584 
1585 
1586 
1588 // Function: nat_add (string $options='')
1589 //
1590 // Input Options:
1591 // $options = key=value pairs of options for this function.
1592 // multiple sets of key=value pairs should be separated
1593 // by an "&" symbol.
1594 //
1595 // Output:
1596 // Returns a two part list:
1597 // 1. The exit status of the function. 0 on success, non-zero on
1598 // error. All errors messages are stored in $self['error'].
1599 // 2. A textual message for display on the console or web interface.
1600 //
1601 // Example: list($status, $result) = nat_add('');
1603 function nat_add($options="") {
1604  global $conf, $self, $onadb;
1605  printmsg("DEBUG => nat_add({$options}) called", 3);
1606 
1607  // Version - UPDATE on every edit!
1608  $version = '1.00';
1609 
1610  // Parse incoming options string to an array
1611  $options = parse_options($options);
1612 
1613  // Return the usage summary if we need to
1614  if ($options['help'] or !($options['natip'] and $options['ip']) ) {
1615  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
1616  $self['error'] = 'ERROR => Insufficient parameters';
1617  return(array(1,
1618 <<<EOM
1619 
1620 nat_add-v{$version}
1621  Add a NAT entry to an existing IP
1622 
1623  Synopsis: nat_add [KEY=VALUE] ...
1624 
1625  Required:
1626  ip=[address|ID] the IP address or ID of the existing inside interface
1627  natip=IP_ADDRESS the IP address of the external NAT entry
1628 
1629 \n
1630 EOM
1631  ));
1632  }
1633 
1634 
1635  // Find the internal interface
1636  list($status, $rows, $interface) = ona_find_interface($options['ip']);
1637  if (!$interface['id']) {
1638  printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!",3);
1639  $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!";
1640  return(array(2, $self['error'] . "\n"));
1641  }
1642  printmsg("DEBUG => Interface selected: {$options['ip']}", 3);
1643 
1644  if ($interface['nat_interface_id'] > 0) {
1645  printmsg("DEBUG => The interface specified, already has a NAT IP!",3);
1646  $self['error'] = "ERROR => The interface specified, already has a NAT IP!";
1647  return(array(3, $self['error'] . "\n"));
1648  }
1649 
1650  // Add the new external NAT interface to the database
1651  $newint['host'] = $interface['host_id'];
1652  $newint['ip'] = $options['natip'];
1653  $newint['mac'] = '';
1654  $newint['name'] = '';
1655  $newint['description'] = 'EXT NAT';
1656  printmsg("DEBUG => nat_add() calling interface_add() for new ip: {$options['natip']}", 3);
1657  list($status, $output) = run_module('interface_add', $newint);
1658  if ($status) { return(array($status, $output)); }
1659  $self['error'] .= $output;
1660 
1661  // Find the interface_id for the interface we just added
1662  list($status, $rows, $int) = ona_find_interface($options['natip']);
1663 
1664  // update the existing inside interface with the new nat_interface_id value
1665  list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), array('nat_interface_id' => $int['id']));
1666  if ($status or !$rows) {
1667  $self['error'] = "ERROR => nat_add() SQL Query failed to update nat_interface_id for interface: " . $self['error'];
1668  printmsg($self['error'], 0);
1669  return(array(4, $self['error'] . "\n"));
1670  }
1671 
1672 
1673 
1674  // Return the success notice
1675  $self['error'] = "INFO => External NAT entry added: {$interface['ip_addr_text']} => {$int['ip_addr_text']}.";
1676  printmsg($self['error'], 0);
1677  return(array(0, $self['error'] . "\n"));
1678 
1679 }
1680 
1681 
1682 
1683 
1684 
1685 
1687 // Function: nat_del (string $options='')
1688 //
1689 // Input Options:
1690 // $options = key=value pairs of options for this function.
1691 // multiple sets of key=value pairs should be separated
1692 // by an "&" symbol.
1693 //
1694 // Output:
1695 // Returns a two part list:
1696 // 1. The exit status of the function. 0 on success, non-zero on
1697 // error. All errors messages are stored in $self['error'].
1698 // 2. A textual message for display on the console or web interface.
1699 //
1700 // Example: list($status, $result) = nat_del('');
1702 function nat_del($options="") {
1703  global $conf, $self, $onadb;
1704  printmsg("DEBUG => nat_del({$options}) called", 3);
1705 
1706  // Version - UPDATE on every edit!
1707  $version = '1.00';
1708 
1709  // Parse incoming options string to an array
1710  $options = parse_options($options);
1711 
1712  // Return the usage summary if we need to
1713  if ($options['help'] or !($options['natip'] and $options['ip']) ) {
1714  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
1715  $self['error'] = 'ERROR => Insufficient parameters';
1716  return(array(1,
1717 <<<EOM
1718 
1719 nat_del-v{$version}
1720  Delete a NAT entry from an existing IP
1721  This will delete the NAT IP interface from the subnet as well.
1722 
1723  Synopsis: nat_del [KEY=VALUE] ...
1724 
1725  Required:
1726  ip=[address|ID] the IP address or ID of the existing inside interface
1727  natip=[address|ID] the IP address or ID of the external NAT entry
1728 
1729  Optional:
1730  commit=[yes|no] commit db transaction (no)
1731 
1732 \n
1733 EOM
1734  ));
1735  }
1736 
1737  // Sanitize "options[commit]" (no is the default)
1738  $options['commit'] = sanitize_YN($options['commit'], 'N');
1739 
1740  // Find the internal interface
1741  list($status, $rows, $interface) = ona_find_interface($options['ip']);
1742  if (!$interface['id']) {
1743  printmsg("DEBUG => The interface specified, {$options['ip']}, does not exist!",3);
1744  $self['error'] = "ERROR => The interface specified, {$options['ip']}, does not exist!";
1745  return(array(2, $self['error'] . "\n"));
1746  }
1747  printmsg("DEBUG => Interface selected: {$options['ip']}", 3);
1748 
1749  // Find the NAT interface
1750  list($status, $rows, $natinterface) = ona_find_interface($options['natip']);
1751  if (!$natinterface['id']) {
1752  printmsg("DEBUG => The NAT interface specified, {$options['natip']}, does not exist!",3);
1753  $self['error'] = "ERROR => The NAT interface specified, {$options['natip']}, does not exist!";
1754  return(array(3, $self['error'] . "\n"));
1755  }
1756  printmsg("DEBUG => NAT Interface selected: {$options['natip']}", 3);
1757 
1758  // Check that the two IP addresses are really paired with each other
1759  if ($interface['nat_interface_id'] != $natinterface['id']) {
1760  $self['error'] = "ERROR => nat_del() The provided IP addresses are not associated with each other for NAT.";
1761  printmsg($self['error'], 0);
1762  return(array(4, $self['error'] . "\n"));
1763  }
1764 
1765  printmsg("DEBUG => nat_del() calling interface_del() for ip: {$options['natip']}", 3);
1766  $natint['interface'] = $natinterface['id'];
1767  $natint['commit'] = $options['commit'];
1768  list($status, $output) = run_module('interface_del', $natint);
1769  if ($status) { return(array($status, $output)); }
1770  $self['error'] .= $output;
1771 
1772  // update the existing inside interface and remove the old nat_interface_id value
1773  list($status, $rows) = db_update_record($onadb, 'interfaces', array('id' => $interface['id']), array('nat_interface_id' => '0'));
1774  if ($status or !$rows) {
1775  $self['error'] = "ERROR => nat_del() SQL Query failed to update nat_interface_id for interface: " . $self['error'];
1776  printmsg($self['error'], 0);
1777  return(array(5, $self['error'] . "\n"));
1778  }
1779 
1780 
1781 
1782  // Return the success notice
1783  $self['error'] = "INFO => External NAT entry deleted: {$natinterface['ip_addr_text']} from {$interface['ip_addr_text']}.";
1784  printmsg($self['error'], 0);
1785  return(array(0, $self['error'] . "\n"));
1786 
1787 }
1788 
1789 
1790 
1791 
1792 
1793 
1794 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
ona_get_host_record
ona_get_host_record($array='', $order='')
Definition: functions_db.inc.php:1106
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
interface_add
interface_add($options="")
Definition: interface.inc.php:19
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
ona_find_domain
ona_find_domain($fqdn="", $returndefault=0)
Definition: functions_db.inc.php:1609
ona_get_device_record
ona_get_device_record($array)
Definition: functions_db.inc.php:1218
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
ona_find_subnet
ona_find_subnet($search="")
Definition: functions_db.inc.php:2003
ona_get_interface_record
ona_get_interface_record($array='', $order='')
Definition: functions_db.inc.php:1126
ona_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
interface_display
interface_display($options="")
Definition: interface.inc.php:820
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
nat_del
nat_del($options="")
Definition: interface.inc.php:1702
db_get_records
db_get_records($dbh=0, $table="", $where="", $order="", $rows=-1, $offset=-1)
Definition: functions_db.inc.php:891
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
mac_mangle
mac_mangle($input="", $format="default")
Definition: functions_general.inc.php:964
interface_move_host
interface_move_host($options="")
Definition: interface.inc.php:1247
ona_update_record
ona_update_record($table="", $where="", $insert="")
Definition: functions_db.inc.php:1029
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
interface_share_del
interface_share_del($options="")
Definition: interface.inc.php:1501
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
format_array
format_array($array=array())
Definition: functions_general.inc.php:1745
ona_find_interface
ona_find_interface($search="")
Definition: functions_db.inc.php:1910
authlvl
authlvl($level)
Definition: functions_general.inc.php:1349
$host
$host
Definition: collate-convert.php:19
if
if(!is_dir( $include))
Definition: 2-to-3.php:11
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
run_module
run_module($module='', $options='', $transaction=1)
Definition: functions_general.inc.php:1468
interface_move
interface_move($options="")
Definition: interface.inc.php:963
ona_get_dhcp_pool_record
ona_get_dhcp_pool_record($array)
Definition: functions_db.inc.php:1276
$conf
global $conf
Definition: 2-to-3.php:15
$output
$output
Definition: dcm.php:16
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
interface_modify
interface_modify($options="")
Definition: interface.inc.php:262
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
interface_share
interface_share($options="")
Definition: interface.inc.php:1384
nat_add
nat_add($options="")
Definition: interface.inc.php:1603
ona_get_subnet_record
ona_get_subnet_record($array)
Definition: functions_db.inc.php:1226
$message
$message
Definition: login.php:37
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22
interface_del
interface_del($options="")
Definition: interface.inc.php:601
ona_get_dns_record
ona_get_dns_record($array='', $order='')
Definition: functions_db.inc.php:1142