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)  

dhcp_pool.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 // Make sure we have necessary functions & DB connectivity
5 require_once($conf['inc_functions_db']);
6 
7 
8 
9 // setup defaults for pool
10 $conf['dhcp_pool']['llength'] = '604800';
11 $conf['dhcp_pool']['lgrace'] = '0';
12 $conf['dhcp_pool']['lrenewal'] = '0';
13 $conf['dhcp_pool']['lrebind'] = '0';
14 $conf['dhcp_pool']['allow_bootp'] = '0';
15 
17 // Function: dhcp_pool_add (string $options='')
18 //
19 // $options = key=value pairs of options for this function.
20 // multiple sets of key=value pairs should be separated
21 // by an "&" symbol.
22 //
23 // Input Options:
24 // failover_group=failover_group_id
25 // OR
26 // server=NAME[.DOMAIN] or id
27 // AND
28 // start=start ip addres
29 // end=end ip address
30 // llength=lease time
31 //
32 // Output:
33 // Adds a DHCP pool into the IP database using specified 'server' OR
34 // 'failover_group'.
35 // Returns a two part list:
36 // 1. The exit status of the function. 0 on success, non-zero on
37 // error. All errors messages are stored in $self['error'].
38 // 2. A textual message for display on the console or web interface.
39 //
40 // Example: list($status, $result) = dhcp_pool_add('server=q1234.something.com&start=10.1.1.1&end=10.1.1.5');
42 function dhcp_pool_add($options="") {
43 
44  // The important globals
45  global $conf, $self, $onadb;
46 
47  // Version - UPDATE on every edit!
48  $version = '1.03';
49 
50  printmsg("DEBUG => dhcp_pool_add({$options}) called", 3);
51 
52  // Parse incoming options string to an array
53  $options = parse_options($options);
54 
55  // Return the usage summary if we need to
56  if ($options['help'] or !(
57  (
58  $options['start'] and
59  $options['end']
60  )
61  )
62  )
63  {
64  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
65  $self['error'] = 'ERROR => Insufficient parameters';
66  return(array(1,
67 <<<EOM
68 
70 Adds a dhcp pool into the database pointing to the specified identifier
71 
72  Synopsis: dhcp_pool_add [KEY=VALUE] ...
73 
74  Identifier (pick one):
75  failover_group=ID group identifier to add to
76 
77  Required:
78  start=IP IP address for start of pool range
79  end=IP IP address for end of pool range
80 
81  Optional:
82  llength=NUMBER Length in seconds for leases ({$conf['dhcp_pool']['llength']})
83 
84 \n
85 EOM
86 
87  ));
88  }
89 
90  // set the lease time if it was passed. otherwise, use the default
91  $llength = ($options['llength']) ? $options['llength'] : $conf['dhcp_pool']['llength'];
92 
93  // make it blank for now.
94  $failovergroupid = 0;
95 
96  // make sure that the start address is actually part of an existing subnet
97  list($status, $rows, $subnet) = ona_find_subnet(ip_mangle($options['start'], 'dotted'));
98  if (!$rows) {
99  printmsg("DEBUG => Unable to find a subnet related to starting address ({$options['start']}).",3);
100  $self['error'] = "ERROR => Unable to find a subnet related to starting address ({$options['start']}).";
101  return(array(1, $self['error'] . "\n"));
102  }
103 
104 
105 
106  if ($options['failover_group']) {
107  list($status, $rows, $fg) = ona_get_dhcp_failover_group_record(array('id' => $options['failover_group']));
108 
109  if (!$fg['id']) {
110  printmsg("DEBUG => The failover_group ({$options['failover_group']}) does not exist!",3);
111  $self['error'] = "ERROR => The failover_group ({$options['failover_group']}) does not exist!";
112  return(array(4, $self['error'] . "\n"));
113  }
114 
115  // get the server names for the two servers
116  list($fail_host1, $fail_zone1) = ona_find_host($fg['primary_server_id']);
117  list($fail_host2, $fail_zone2) = ona_find_host($fg['secondary_server_id']);
118  $desc = $fail_host1['fqdn'] .'/'. $fail_host2['fqdn'];
119 
120  $failovergroupid = $fg['id'];
121  }
122 
123  // check that start and end are not the same
124  //if ($options['start'] == $options['end']) {
125  // printmsg("DEBUG => The start and end IP addresses ({$options['start']}) cannot be the same!",3);
126  // $self['error'] = "ERROR => The start and end IP addresses ({$options['start']}) cannot be the same!";
127  // return(array(2, $self['error'] . "\n"));
128  //}
129 
130 
131  $start_dec = ip_mangle($options['start'], 'numeric');
132  $end_dec = ip_mangle($options['end'], 'numeric');
133  $net_end = ((4294967295 - $subnet['ip_mask']) + $subnet['ip_addr']);
134 
135  // Validate that the IP address supplied isn't the base or broadcast of the subnet
136  if ($start_dec == $subnet['ip_addr'] or $end_dec == $subnet['ip_addr']) {
137  printmsg("DEBUG => IP address can't be a subnet's base address (" . ip_mangle($subnet['ip_addr'],'dotted') . ")!",3);
138  $self['error'] = "ERROR => IP address can't be a subnet's base address(" . ip_mangle($subnet['ip_addr'],'dotted') . ")!";
139  return(array(7, $self['error'] . "\n"));
140  }
141 
142  if ($start_dec == $net_end or $end_dec == $net_end) {
143  printmsg("DEBUG => IP address can't be a subnet's broadcast address (" . ip_mangle($net_end,'dotted') . ")!",3);
144  $self['error'] = "ERROR => IP address can't be the subnet broadcast address (" . ip_mangle($net_end,'dotted') . ")!";
145  return(array(8, $self['error'] . "\n"));
146  }
147 
148  // check that start is not after the end
149  if ($start_dec > $end_dec) {
150  printmsg("ERROR => The start IP address ({$options['start']}) falls after the end IP address ({$options['end']})!",3);
151  $self['error'] = "ERROR => The start IP addresses ({$options['start']}) falls after the end IP address ({$options['end']})!";
152  return(array(2, $self['error'] . "\n"));
153  }
154 
155 
156  // check for existing hosts inside the pool range
157  list($status, $rows, $interface) = db_get_records($onadb, 'interfaces', 'subnet_id = '.$subnet['id'].' AND ip_addr BETWEEN '.$start_dec.' AND '.$end_dec, '',0);
158  if ($rows) {
159  printmsg("DEBUG => IP conflict: Specified range ({$options['start']}-{$options['end']}) encompasses {$rows} host(s).", 3);
160  $self['error'] = "ERROR => IP conflict: Specified range ({$options['start']}-{$options['end']}) encompasses {$rows} host(s)";
161  return(array(4, $self['error'] . "\n"));
162  }
163 
164 
165 
166 
167 
168 
169  // *** Check to see if the new pool overlaps any existing pools *** //
170  // Look for overlaps like this (where new pool address starts inside an existing pool):
171  // [ -- new pool -- ]
172  // [ -- old pool --]
173  list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', $start_dec.' BETWEEN ip_addr_start AND ip_addr_end');
174  if ($rows != 0) {
175  printmsg("DEBUG => Pool address conflict! New pool ({$options['start']}-{$options['end']}) starts inside an existing pool.", 3);
176  $self['error'] = "ERROR => Pool address conflict! New pool ({$options['start']}-{$options['end']}) starts inside an existing pool.";
177  return(array(5, $self['error'] . "\n" .
178  "INFO => Conflicting pool record ID: {$pool['id']}\n"));
179  }
180 
181 
182  // Look for overlaps like this (where the new pool ends inside an existing pool):
183  // [ -- new pool -- ]
184  // [ -- old pool --]
185  list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', $end_dec.' BETWEEN ip_addr_start AND ip_addr_end');
186  if ($rows != 0) {
187  printmsg("DEBUG => Pool address conflict! New pool ({$options['start']}-{$options['end']}) ends inside an existing pool.", 3);
188  $self['error'] = "ERROR => Pool address conflict! New pool ({$options['start']}-{$options['end']}) ends inside an existing pool.";
189  return(array(6, $self['error'] . "\n" .
190  "INFO => Conflicting pool record ID: {$pool['id']}\n"));
191  }
192 
193 
194 
195  // Look for overlaps like this (where the new pool entirely overlaps an existing pool):
196  // [ -------- new pool --------- ]
197  // [ -- old pool --]
198  list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', 'ip_addr_start BETWEEN '.$start_dec.' AND '.$end_dec.' OR ip_addr_end BETWEEN '.$start_dec.' AND '.$end_dec);
199  if ($rows != 0) {
200  printmsg("DEBUG => Pool address conflict! New pool ({$options['start']}-{$options['end']}) would encompass an existing pool.", 3);
201  $self['error'] = "ERROR => Pool address conflict! New pool ({$options['start']}-{$options['end']}) would encompass an existing pool.";
202  return(array(7, $self['error'] . "\n" .
203  "INFO => Conflicting pool record ID: {$pool['id']}\n"));
204  }
205 
206 
207 
208 
209  // Check permissions
210  if (!auth('advanced') or !authlvl($subnet['lvl'])) {
211  $self['error'] = "Permission denied!";
212  printmsg($self['error'], 0);
213  return(array(8, $self['error'] . "\n"));
214  }
215 
216 
217 
218 
219  // Get the next id
220  $id = ona_get_next_id('dhcp_pools');
221  if (!$id) {
222  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
223  printmsg($self['error'], 0);
224  return(array(9, $add_to_error . $self['error'] . "\n"));
225  }
226  printmsg("DEBUG => dhcp_pool_add(): New ID: $id", 3);
227 
228  // Add the record
229  list($status, $rows) =
231  $onadb,
232  'dhcp_pools',
233  array(
234  'id' => $id,
235  'subnet_id' => $subnet['id'],
236  'dhcp_failover_group_id' => $failovergroupid,
237  'ip_addr_start' => $start_dec,
238  'ip_addr_end' => $end_dec,
239  'lease_length' => $llength,
240  'lease_grace_period' => $conf['dhcp_pool']['lgrace'],
241  'lease_renewal_time' => $conf['dhcp_pool']['lrenewal'],
242  'lease_rebind_time' => $conf['dhcp_pool']['lrebind'],
243  'allow_bootp_clients' => $conf['dhcp_pool']['allow_bootp']
244 
245  )
246  );
247  if ($status or !$rows) {
248  $self['error'] = "ERROR => dhcp_pool_add() SQL Query failed: " . $self['error'];
249  printmsg($self['error'], 0);
250  return(array(10, $add_to_error . $self['error'] . "\n"));
251  }
252 
253  // Return the success notice
254  $self['error'] = "INFO => DHCP pool ADDED: {$options['start']}->{$options['end']}.";
255  printmsg($self['error'],0);
256  return(array(0, $add_to_error . $self['error'] . "\n"));
257 }
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
271 // Function: dhcp_pool_del (string $options='')
272 //
273 // $options = key=value pairs of options for this function.
274 // multiple sets of key=value pairs should be separated
275 // by an "&" symbol.
276 //
277 // Input Options:
278 // id=ID
279 //
280 // Output:
281 // Deletes a dhcp pool from the IP database.
282 // Returns a two part list:
283 // 1. The exit status of the function. 0 on success, non-zero on
284 // error. All errors messages are stored in $self['error'].
285 // 2. A textual message for display on the console or web interface.
286 //
287 // Example: list($status, $result) = dhcp_entry_add('host=test');
289 function dhcp_pool_del($options="") {
290 
291  // The important globals
292  global $conf, $self, $onadb;
293 
294  // Version - UPDATE on every edit!
295  $version = '1.00';
296 
297  printmsg("DEBUG => dhcp_pool_del({$options}) called", 3);
298 
299  // Parse incoming options string to an array
300  $options = parse_options($options);
301 
302  // Sanitize options[commit] (default is yes)
303  $options['commit'] = sanitize_YN($options['commit'], 'N');
304 
305  // Return the usage summary if we need to
306  if ($options['help'] or !$options['id'] ) {
307  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
308  $self['error'] = 'ERROR => Insufficient parameters';
309  return(array(1,
310 <<<EOM
311 
313 Deletes a DHCP pool from the database
314 
315  Synopsis: dhcp_pool_del [KEY=VALUE] ...
316 
317  Required:
318  id=ID ID of the DHCP pool to delete
319 
320  Optional:
321  commit=[yes|no] commit db transaction (no)
322 \n
323 EOM
324 
325  ));
326  }
327 
328 
329  // Sanitize options[commit] (default is no)
330  $options['commit'] = sanitize_YN($options['commit'], 'N');
331 
332  // If the option provided is numeric, check to see if it exists
333  if (is_numeric($options['id'])) {
334 
335  list($status, $tmp_rows, $pool) = ona_get_dhcp_pool_record(array('id' => $options['id']));
336 
337  // Test to see that we were able to find the specified pool record
338  if (!$pool['id']) {
339  printmsg("DEBUG => Unable to find the DHCP pool record using ID: {$options['id']}!",3);
340  $self['error'] = "ERROR => Unable to find the DHCP pool record using ID: {$options['id']}!";
341  return(array(2, $self['error']. "\n"));
342  }
343 
344  $start = ip_mangle($pool['ip_addr_start'], 'dotted');
345  $end = ip_mangle($pool['ip_addr_end'], 'dotted');
346  list($status, $tmp_rows, $subnet) = ona_get_subnet_record(array('id' => $pool['subnet_id']));
347 
348 
349  } else {
350  printmsg("DEBUG => {$options['id']} is not a numeric value!",3);
351  $self['error'] = "ERROR => {$options['id']} is not a numeric value";
352  return(array(3, $self['error'] . "\n"));
353  }
354 
355 
356  // If "commit" is yes, delte the record
357  if ($options['commit'] == 'Y') {
358 
359  // Check permissions
360  if (!auth('advanced') or !authlvl($subnet['lvl'])) {
361  $self['error'] = "Permission denied!";
362  printmsg($self['error'], 0);
363  return(array(4, $self['error'] . "\n"));
364  }
365 
366  list($status, $rows) = db_delete_records($onadb, 'dhcp_pools', array('id' => $pool['id']));
367  if ($status or !$rows) {
368  $self['error'] = "ERROR => dhcp_pool_del() SQL Query failed: " . $self['error'];
369  printmsg($self['error'], 0);
370  return(array(5, $self['error'] . "\n"));
371  }
372 
373  // Return the success notice
374  $self['error'] = "INFO => DHCP pool DELETED: {$start}-{$end} from {$subnet['name']}.";
375  printmsg($self['error'],0);
376  return(array(0, $self['error'] . "\n"));
377  }
378 
379  // Otherwise display the record that would have been deleted
380  $text = <<<EOL
381 Record(s) NOT DELETED (see "commit" option)
382 Displaying record(s) that would have been deleted:
383 
384  Delete the following dhcp pool:
385  ENTRY: {$start}=>{$end} from {$subnet['name']}
386 
387 EOL;
388 
389  return(array(6, $text));
390 
391 }
392 
393 
394 
395 
396 
397 
398 
399 
400 
402 // Function: dhcp_pool_modify (string $options='')
403 //
404 // $options = key=value pairs of options for this function.
405 // multiple sets of key=value pairs should be separated
406 // by an "&" symbol.
407 //
408 // Input Options:
409 // Where:
410 // pool=ID Table ID for the pool in dhcp_pool_b
411 
412 // Optional:
413 // set_start=IP Start ip address of pool
414 // set_end=IP End IP of pool
415 // set_llength=NUMBER Lease Time. Default ({$conf['dhcp_pool']['llength']})
416 // set_lgrace=NUMBER Lease Grace Period. Default ({$conf['dhcp_pool']['lgrace']})
417 // set_lrenewal=NUMBER Lease Renewal. Default ({$conf['dhcp_pool']['lrenewal']})
418 // set_lrebind=NUMBER Lease Rebind. Default ({$conf['dhcp_pool']['lrebind']})
419 //
420 // Output:
421 // Updates an dhcp_pool record in the IP database.
422 // Returns a two part list:
423 // 1. The exit status of the function. 0 on success, non-zero on
424 // error. All errors messages are stored in $self['error'].
425 // 2. A textual message for display on the console or web interface.
426 //
427 // Example: list($status, $result) = dhcp_pool_modify('pool=23243,set_start=10.1.1.10');
429 function dhcp_pool_modify($options="") {
430 
431  // The important globals
432  global $conf, $self, $onadb;
433 
434  // Version - UPDATE on every edit!
435  $version = '1.03';
436 
437  printmsg("DEBUG => dhcp_pool_modify({$options}) called", 3);
438 
439  // Parse incoming options string to an array
440  $options = parse_options($options);
441 
442  // Return the usage summary if we need to
443  if ($options['help'] or !(
444  ($options['pool'])
445  and
446  (
447  $options['set_failover_group'] or
448  $options['set_start'] or
449  $options['set_end'] or
450  $options['set_llength'] or
451  $options['set_lgrace'] or
452  $options['set_lrenewal'] or
453  $options['set_lrebind']
454  )
455  )
456  )
457  {
458  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
459  $self['error'] = 'ERROR => Insufficient parameters';
460  return(array(1,
461 <<<EOM
462 
464 Updates a dhcp pool in the database pointing to the specified identifier
465 
466  Synopsis: dhcp_pool_modify [KEY=VALUE] ...
467 
468  Where:
469  pool=ID Table ID for the pool
470 
471  Optional:
472  set_failover_group=ID group identifier
473  set_server=NAME[.DOMAIN] or ID server identifier
474  set_start=IP Start ip address of pool
475  set_end=IP End IP of pool
476  set_llength=NUMBER Lease Time. Default ({$conf['dhcp_pool']['llength']})
477  set_lgrace=NUMBER Lease Grace Period. Default ({$conf['dhcp_pool']['lgrace']})
478  set_lrenewal=NUMBER Lease Renewal. Default ({$conf['dhcp_pool']['lrenewal']})
479  set_lrebind=NUMBER Lease Rebind. Default ({$conf['dhcp_pool']['lrebind']})
480 
481 \n
482 EOM
483 
484  ));
485  }
486 
487 
488  // get the existing pool to edit
489  list($status, $rows, $pool) = db_get_record($onadb, 'dhcp_pools', array('id' => $options['pool']));
490  if (!$rows) {
491  printmsg("DEBUG => Unable to find the DHCP pool record using id: {$options['id']}!",3);
492  $self['error'] = "ERROR => Unable to find a pool using id: {$options['pool']}";
493  return(array(1, $self['error'] . "\n"));
494  }
495 
496  // set the pool id in the set variable
497  $SET['id'] = $pool['id'];
498 
499  // NOTE: currently modify pool does not allow you to change subnets
500  // Get subnet info..
501  list($status, $rows, $subnet) = ona_find_subnet($pool['subnet_id']);
502  $SET['subnet_id'] = $subnet['id'];
503 
504  // make sure that the start address is actually part of an existing subnet
505  if($options['set_start']) {
506  list($status, $rows, $subnetstart) = ona_find_subnet(ip_mangle($options['set_start'], 'dotted'));
507  if (!$rows) {
508  printmsg("DEBUG => Unable to find a subnet related to starting address ({$options['set_start']})!",3);
509  $self['error'] = "ERROR => Unable to find a subnet related to your starting address of {$options['set_start']}.";
510  return(array(1, $self['error'] . "\n"));
511  }
512 
513  if ($subnetstart['id'] != $pool['subnet_id']) {
514  printmsg("DEBUG => The starting address ({$options['set_start']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!",3);
515  $self['error'] = "ERROR => The starting address ({$options['set_start']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!";
516  return(array(1, $self['error'] . "\n"));
517  }
518  }
519 
520  // make sure that the end address is actually part of an existing subnet
521  if($options['set_end']) {
522  list($status, $rows, $subnetend) = ona_find_subnet(ip_mangle($options['set_end'], 'dotted'));
523  if (!$rows) {
524  printmsg("DEBUG => Unable to find a subnet related to ending address ({$options['set_end']})!",3);
525  $self['error'] = "ERROR => Unable to find a subnet related to your ending address of {$options['set_end']}.";
526  return(array(1, $self['error'] . "\n"));
527  }
528 
529  if ($subnetend['id'] != $pool['subnet_id']) {
530  printmsg("DEBUG => The ending address ({$options['set_end']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!",3);
531  $self['error'] = "ERROR => The ending address ({$options['set_end']}) is not on the same subnet of the pool ({$pool['id']}) you are editing!";
532  return(array(1, $self['error'] . "\n"));
533  }
534  }
535 
536  // Assign which failover group to use
537  if ($options['set_failover_group'] == 0) {
538  $desc = 'Not using a failover group';
539  $SET['dhcp_failover_group_id'] = 0;
540  }
541  else {
542  list($status, $rows, $fg) = ona_get_dhcp_failover_group_record(array('id' => $options['set_failover_group']));
543 
544  if (!$fg['id']) {
545  printmsg("DEBUG => The failover_group specified ({$options['set_failover_group']}) does not exist",3);
546  $self['error'] = "ERROR => The failover_group specified ({$options['set_failover_group']}) does not exist!";
547  return(array(4, $self['error'] . "\n"));
548  }
549 
550  // get the server names for the two servers
551  list($fail_host1, $fail_zone1) = ona_find_host($fg['primary_server_id']);
552  list($fail_host2, $fail_zone2) = ona_find_host($fg['secondary_server_id']);
553 
554  $desc = $fail_host1['fqdn'] .'/'. $fail_host2['fqdn'];
555  $SET['dhcp_failover_group_id'] = $fg['id'];
556  }
557 
558  // check that start and end are not the same
559  //if ($options['set_start'] and $options['set_end'] and $options['set_start'] == $options['set_end']) {
560  // printmsg("DEBUG => The start and end IP addresses (" . ip_mangle($options['set_start'],'dotted') . ") cannot be the same!",3);
561  // $self['error'] = "ERROR => The start and end IP addresses (" . ip_mangle($options['set_start'],'dotted') . ") cannot be the same!";
562  // return(array(2, $self['error'] . "\n"));
563  //}
564 
565  if($options['set_start'])
566  $start_dec = ip_mangle($options['set_start'], 'numeric');
567  else
568  $start_dec = $pool['ip_addr_start'];
569 
570  if($options['set_end'])
571  $end_dec = ip_mangle($options['set_end'], 'numeric');
572  else
573  $end_dec = $pool['ip_addr_end'];
574 
575  $net_end = ((4294967295 - $subnet['ip_mask']) + $subnet['ip_addr']);
576 
577  // Validate that the IP address supplied isn't the base or broadcast of the subnet
578  if ($start_dec == $subnet['ip_addr'] or $end_dec == $subnet['ip_addr']) {
579  printmsg("DEBUG => IP address can't be a subnet's base address (" . ip_mangle($subnet['ip_addr'],'dotted') . ")!",3);
580  $self['error'] = "ERROR => IP address can't be a subnet's base address (" . ip_mangle($subnet['ip_addr'],'dotted') . ")!";
581  return(array(7, $self['error'] . "\n"));
582  }
583 
584  if ($start_dec == $net_end or $end_dec == $net_end) {
585  printmsg("DEBUG => IP address can't be a subnet's broadcast address (" . ip_mangle($net_end,'dotted') . ")!",3);
586  $self['error'] = "ERROR => IP address can't be the subnet broadcast address(" . ip_mangle($net_end,'dotted') . ")!";
587  return(array(8, $self['error'] . "\n"));
588  }
589 
590  // check that start is not after the end
591  if ($start_dec > $end_dec) {
592  printmsg("DEBUG => The start IP addresses (" . ip_mangle($start_dec,'dotted') . ") falls after the end IP address (" . ip_mangle($end_dec,'dotted') . ")!",3);
593  $self['error'] = "ERROR => The start IP addresses (" . ip_mangle($start_dec,'dotted') . ") falls after the end IP address(" . ip_mangle($end_dec,'dotted') . ")!";
594  return(array(2, $self['error'] . "\n"));
595  }
596 
597 
598  // check for existing hosts inside the pool range
599  list($status, $rows, $interface) = db_get_records($onadb, 'interfaces', 'subnet_id = '.$subnet['id'].' AND ip_addr BETWEEN '.$start_dec.' AND '.$end_dec, '',0);
600  if ($rows) {
601  printmsg("DEBUG => IP conflict: Specified range (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") encompasses {$rows} host(s)!",3);
602  $self['error'] = "ERROR => IP conflict: Specified range (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") encompasses {$rows} host(s)";
603  return(array(4, $self['error'] . "\n"));
604  }
605 
606 
607 
608 
609 
610 
611  // *** Check to see if the new pool overlaps any existing pools *** //
612  // Look for overlaps like this (where new pool address starts inside an existing pool):
613  // [ -- new pool -- ]
614  // [ -- old pool --]
615  list($status, $rows, $tmp) = db_get_record($onadb, 'dhcp_pools', 'id != '. $SET['id']. ' AND '.$start_dec.' BETWEEN ip_addr_start AND ip_addr_end');
616  if ($rows != 0) {
617  printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") starts inside an existing pool!",3);
618  $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") starts inside an existing pool.";
619  return(array(5, $self['error'] . "\n" .
620  "INFO => Conflicting pool record ID: {$tmp['id']}\n"));
621  }
622 
623 
624  // Look for overlaps like this (where the new pool ends inside an existing pool):
625  // [ -- new pool -- ]
626  // [ -- old pool --]
627  list($status, $rows, $tmp) = db_get_record($onadb, 'dhcp_pools', 'id != '. $SET['id']. ' AND '.$end_dec.' BETWEEN ip_addr_start AND ip_addr_end');
628  if ($rows != 0) {
629  printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") ends inside an existing pool!",3);
630  $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") ends inside an existing pool.";
631  return(array(6, $self['error'] . "\n" .
632  "INFO => Conflicting pool record ID: {$tmp['id']}\n"));
633  }
634 
635 
636 
637  // Look for overlaps like this (where the new pool entirely overlaps an existing pool):
638  // [ -------- new pool --------- ]
639  // [ -- old pool --]
640  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.')');
641  if ($rows != 0) {
642  printmsg("DEBUG => Pool address conflict: New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") would encompass an existing pool!",3);
643  $self['error'] = "ERROR => Pool address conflict! New pool (" . ip_mangle($start_dec,'dotted') . "-" . ip_mangle($end_dec,'dotted') . ") would encompass an existing pool.";
644  return(array(7, $self['error'] . "\n" .
645  "INFO => Conflicting pool record ID: {$tmp['id']}\n"));
646  }
647 
648 
649 
650 
651  // Check permissions
652  if (!auth('advanced') or !authlvl($subnet['lvl'])) {
653  $self['error'] = "Permission denied!";
654  printmsg($self['error'], 0);
655  return(array(8, $self['error'] . "\n"));
656  }
657 
658  // define the remaining entries
659  if (array_key_exists('set_lgrace',$options)) $SET['lease_grace_period'] = $options['set_lgrace'];
660  if (array_key_exists('set_llength',$options)) $SET['lease_length'] = $options['set_llength'];
661  if (array_key_exists('set_lrenewal',$options)) $SET['lease_renewal_time'] = $options['set_lrenewal'];
662  if (array_key_exists('set_lrebind',$options)) $SET['lease_rebind_time'] = $options['set_lrebind'];
663 
664  // Set the IPs if you got this far
665  $SET['ip_addr_start'] = $start_dec;
666  $SET['ip_addr_end'] = $end_dec;
667 
668 
669  // Get the DHCP pool record before updating (logging)
670  list($status, $rows, $original_pool) = ona_get_dhcp_pool_record(array('id' => $SET['id']));
671 
672  // Update the record
673  list($status, $rows) = db_update_record($onadb, 'dhcp_pools', array('id' => $SET['id']), $SET);
674  if ($status or !$rows) {
675  $self['error'] = "ERROR => dhcp_pool_modify() SQL Query failed: " . $self['error'];
676  printmsg($self['error'], 0);
677  return(array(6, $add_to_error . $self['error'] . "\n"));
678  }
679  $success_start = ip_mangle($SET['ip_addr_start'],'dotted');
680  $success_end = ip_mangle($SET['ip_addr_end'],'dotted');
681 
682  // Get the DHCP pool record after updating (logging)
683  list($status, $rows, $new_pool) = ona_get_dhcp_pool_record(array('id' => $SET['id']));
684 
685  // Return the success notice
686  $self['error'] = "INFO => DHCP pool UPDATED:{$original_pool['id']}: {$success_start}-{$success_end} on {$subnet['name']}.";
687 
688  $log_msg = "INFO => DHCP pool UPDATED:{$original_pool['id']}: ";
689  $more="";
690  foreach(array_keys($original_pool) as $key) {
691  if($original_pool[$key] != $new_pool[$key]) {
692  $log_msg .= $more . $key . "[" .$original_pool[$key] . "=>" . $new_pool[$key] . "]";
693  $more= ";";
694  }
695  }
696 
697  // only print to logfile if a change has been made to the record
698  if($more != '') {
699  printmsg($self['error'], 0);
700  printmsg($log_msg, 0);
701  }
702 
703  return(array(0, $add_to_error . $self['error'] . "\n"));
704 }
705 
706 
707 
708 
709 
710 
711 
712 
713 
714 
715 
716 
717 
718 
719 
720 
721 
722 
724 // Function: dhcp_lease_add (string $options='')
725 //
726 // $options = key=value pairs of options for this function.
727 // multiple sets of key=value pairs should be separated
728 // by an "&" symbol.
729 //
730 // Input Options:
731 // raw=raw text from dhcp log
732 //
733 // Output:
734 // Adds a DHCP lease entry into the database using specified IP.
735 // Returns a two part list:
736 // 1. The exit status of the function. 0 on success, non-zero on
737 // error. All errors messages are stored in $self['error'].
738 // 2. A textual message for display on the console or web interface.
739 //
740 // Example: list($status, $result) = dhcp_lease_add('raw=DHCPACK on 10.47.118.247 to 00:11:20:48:04:06 via 10.47.118.2');
742 function dhcp_lease_add($options="") {
743 
744  // The important globals
745  global $conf, $self, $mysql;
746 
747  // Version - UPDATE on every edit!
748  $version = '1.00';
749 
750  printmsg("DEBUG => dhcp_lease_add({$options}) called", 3);
751 
752  // Parse incoming options string to an array
753  $options = parse_options($options);
754 
755  // Return the usage summary if we need to
756  if ($options['help'] or !(
757  ($options['raw'])
758  or
759  ($options['ip'] and $options['mac'])
760  )
761  )
762  {
763  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
764  $self['error'] = 'ERROR => Insufficient parameters';
765  return(array(1,
766 <<<EOM
767 
769 Adds a dhcp lease entry into the tracking database
770 
771  Synopsis: dhcp_lease_add [KEY=VALUE] ...
772 
773  Required:
774  raw=TEXT The raw text from the DHCP log
775 
776  Notes:
777  Adding a lease here does NOT effect the actual functioning
778  DHCP server. This is purely to keep track of lease information
779  derived from the DHCP logfile itself.
780 \n
781 EOM
782 
783  ));
784  }
785 
786  $name = 'N/A';
787 
788  if ($options['raw']) {
789  // break the line down into its parts
790  $line = preg_split("/( on | to | via )/",$options['raw']);
791  $mac = $line[2];
792  $iptext = $line[1];
793  $ip = ip_mangle($iptext,'numeric');
794 
795  // if the second field has mac and a name in parens then break it out
796  if (strpos($line[2],'(')) {
797  list($mac,$name) = split('[()]', $line[2]);
798  $name = trim($name);
799  }
800 
801  // make sure the mac has been trimmed
802  $mac = trim($mac);
803 
804  $text = "DHCP Lease Add: IP={$ip} IPTEXT={$line[1]} MAC={$mac} NAME={$name}\n";
805  printmsg("DEBUG => {$text}",3);
806  }
807 
808  if ($options['ip'] and $options['mac']) {
809  $ip = ip_mangle($options['ip'],'numeric');
810  // make sure the mac has been trimmed
811  $mac = trim($options['mac']);
812  if ($options['name']) $name = $options['name'];
813 
814  $text = "DHCP Lease Add: IP={$ip} IPTEXT={$line[1]} MAC={$mac} NAME={$name}\n";
815  printmsg("DEBUG => {$text}",3);
816 
817  }
818 
819  // Check to see if the IP is already in the database
820 // list($status, $rows, $lease) = db_get_record($mysql, "dhcp_leases", array('IP_ADDRESS' => $ip));
821 // if ($rows) {
822 // printmsg("DEBUG => updating existing record", 2);
823 // list($status, $rows) = db_update_record($mysql, "dhcp_leases", array('IP_ADDRESS' => $ip), array('MAC' => $mac, 'HOSTNAME' => $name));
824 // } else {
825 // printmsg("DEBUG => inserting new record", 2);
826 // list($status, $rows) = db_insert_record($mysql, "dhcp_leases", array('IP_ADDRESS' => $ip,
827 // 'IP_TEXT' => $iptext,
828 // 'MAC' => $mac,
829 // 'HOSTNAME' => $name)
830  // );
831 // }
832 
833 
834  // Return the success notice
835  return(array(0, $text));
836 }
837 
838 
839 
840 
841 
842 
843 
844 
845 
846 // DON'T put whitespace at the beginning or end of this file!!!
847 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
dhcp_lease_add
dhcp_lease_add($options="")
Definition: dhcp_pool.inc.php:742
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
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_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
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
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
authlvl
authlvl($level)
Definition: functions_general.inc.php:1349
dhcp_pool_add
dhcp_pool_add($options="")
Definition: dhcp_pool.inc.php:42
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
ona_get_dhcp_pool_record
ona_get_dhcp_pool_record($array)
Definition: functions_db.inc.php:1276
EOL
< a style="text-decoration: none;" href="/">< input class='edit' type="button" value="I don't like free stuff?" onclick=""/></a ></center ></div > EOL
Definition: install.php:40
$ip
$ip
Definition: main.inc.php:24
dhcp_pool_del
dhcp_pool_del($options="")
Definition: dhcp_pool.inc.php:289
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
ona_get_dhcp_failover_group_record
ona_get_dhcp_failover_group_record($array)
Definition: functions_db.inc.php:1175
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
ona_get_subnet_record
ona_get_subnet_record($array)
Definition: functions_db.inc.php:1226
dhcp_pool_modify
dhcp_pool_modify($options="")
Definition: dhcp_pool.inc.php:429
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22
$conf
$conf['dhcp_pool']['llength']
Definition: dhcp_pool.inc.php:10