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_server.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 
10 // Function: dhcp_server_add (string $options='')
11 //
12 // Input Options:
13 // $options = key=value pairs of options for this function.
14 // multiple sets of key=value pairs should be separated
15 // by an "&" symbol.
16 //
17 // Output:
18 // Returns a two part list:
19 // 1. The exit status of the function. 0 on success, non-zero on
20 // error. All errors messages are stored in $self['error'].
21 // 2. A textual message for display on the console or web interface.
22 //
23 // Example: list($status, $result) = dhcp_server_add('subnet=&server=hp34.something.com');
25 function dhcp_server_add($options="") {
26 
27  // The important globals
28  global $conf, $self, $onadb;
29 
30  // Version - UPDATE on every edit!
31  $version = '1.01';
32 
33  printmsg("DEBUG => dhcp_server_add({$options}) called", 3);
34 
35  // Parse incoming options string to an array
36  $options = parse_options($options);
37 
38  // Return the usage summary if we need to
39  if ($options['help']
40  or
41  !($options['subnet'] and $options['server'])
42  ) {
43  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
44  $self['error'] = 'ERROR => Insufficient parameters';
45  return(array(1,
46 <<<EOM
47 
49 Assigns an existing subnet record to a DHCP server
50 
51  Synopsis: dhcp_server_add [KEY=VALUE] ...
52 
53  Required:
54  subnet=NAME or ID subnet name or ID
55  server=NAME[.DOMAIN] or ID server name or ID
56 
57  Notes:
58  DOMAIN will default to {$conf['dns_defaultdomain']} if not specified
59 
60 EOM
61 
62  ));
63  }
64 
65 
66  // Determine the entry itself exists
67  list($status, $rows, $subnet) = ona_find_subnet($options['subnet']);
68 
69  // Test to see that we were able to find the specified record
70  if (!$subnet['id']) {
71  printmsg("DEBUG => Unable to find the subnet record using {$options['subnet']}!",3);
72  $self['error'] = "ERROR => Unable to find the subnet record using {$options['subnet']}!";
73  return(array(4, $self['error']. "\n"));
74  }
75 
76  printmsg("DEBUG => dhcp_server_add(): Found subnet, {$subnet['name']}", 3);
77 
78  // Determine the server is valid
79  list($status, $rows, $host) = ona_find_host($options['server']);
80 
81  if (!$host['id']) {
82  printmsg("DEBUG => The server ({$options['server']}) does not exist!",3);
83  $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!";
84  return(array(2, $self['error'] . "\n"));
85  }
86 
87  // Check permissions
88  if (!auth('advanced') or !authlvl($host['LVL']) or !authlvl($subnet['LVL'])) {
89  $self['error'] = "Permission denied!";
90  printmsg($self['error'], 0);
91  return(array(12, $self['error'] . "\n"));
92  }
93 
94  // Test that this subnet isnt already assigned to the server
95  list($status, $rows, $dhcpserver) = ona_get_dhcp_server_subnet_record(array('host_id' => $host['id'],'subnet_id' => $subnet['id']));
96  if ($rows) {
97  printmsg("DEBUG => Subnet {$subnet['name']} already assigned to {$host['fqdn']}",3);
98  $self['error'] = "ERROR => Subnet {$subnet['name']} already assigned to {$host['fqdn']}";
99  return(array(11, $self['error'] . "\n"));
100  }
101 
102 
103  // Get the next ID
104  $id = ona_get_next_id('dhcp_server_subnets');
105  if (!$id) {
106  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
107  printmsg($self['error'],0);
108  return(array(6, $add_to_error . $self['error'] . "\n"));
109  }
110 
111  printmsg("DEBUG => dhcp_server_add(): New dhcp server subnet ID: $id", 3);
112 
113  // Add new record to dhcp_server_subnets_b
114  list($status, $rows) =
116  $onadb,
117  'dhcp_server_subnets',
118  array(
119  'id' => $id,
120  'host_id' => $host['id'],
121  'subnet_id' => $subnet['id']
122  )
123  );
124  if ($status or !$rows) {
125  $self['error'] = "ERROR => dhcp_server_add() SQL Query failed:" . $self['error'];
126  printmsg($self['error'],0);
127  return(array(8, $add_to_error . $self['error'] . "\n"));
128  }
129 
130 
131  // Return the success notice
132  $self['error'] = "INFO => DHCP Subnet/Server Pair ADDED: {$subnet['name']}/{$host['fqdn']} ";
133  printmsg($self['error'],0);
134  return(array(0, $add_to_error . $self['error'] . "\n"));
135 
136 
137 }
138 
139 
140 
141 
142 
143 
145 // Function: dhcp_server_del (string $options='')
146 //
147 // Input Options:
148 // $options = key=value pairs of options for this function.
149 // multiple sets of key=value pairs should be separated
150 // by an "&" symbol.
151 //
152 // Output:
153 // Returns a two part list:
154 // 1. The exit status of the function. 0 on success, non-zero on
155 // error. All errors messages are stored in $self['error'].
156 // 2. A textual message for display on the console or web interface.
157 //
158 // Example: list($status, $result) = dhcp_server_del('subnet=TEST-MODIFY-ADD&server=test-server.something.com');
160 function dhcp_server_del($options="") {
161 
162  // The important globals
163  global $conf, $self, $onadb;
164 
165  // Version - UPDATE on every edit!
166  $version = '1.03';
167 
168  printmsg("DEBUG => dhcp_server_del({$options}) called", 3);
169 
170  // Parse incoming options string to an array
171  $options = parse_options($options);
172 
173  // Sanitize options[commit] (default is yes)
174  $options['commit'] = sanitize_YN($options['commit'], 'N');
175 
176 
177  // Return the usage summary if we need to
178  if ($options['help'] or !($options['subnet'] and $options['server']) ) {
179  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
180  $self['error'] = 'ERROR => Insufficient parameters';
181  return(array(1,
182 <<<EOM
183 
185 Removes a subnet record from a DHCP server
186 
187  Synopsis: dhcp_server_del [KEY=VALUE] ...
188 
189  Required:
190  subnet=NAME or ID subnet name or ID
191  server=NAME[.DOMAIN] or ID server name or ID
192 
193  Optional:
194  commit=[Y|N] commit db transaction (no)
195 
196  Notes:
197  DOMAIN will default to {$conf['dns_defaultdomain']} if not specified
198 
199 
200 EOM
201 
202  ));
203  }
204 
205 
206  // Determine the entry itself exists
207  list($status, $rows, $subnet) = ona_find_subnet($options['subnet']);
208 
209  // Test to see that we were able to find the specified record
210  if (!$subnet['id']) {
211  printmsg("DEBUG => Unable to find the subnet record using {$options['subnet']}!",3);
212  $self['error'] = "ERROR => Unable to find the subnet record using {$options['subnet']}!";
213  return(array(4, $self['error']. "\n"));
214  }
215 
216  printmsg("DEBUG => dhcp_server_del(): Found subnet, {$subnet['name']}", 3);
217 
218  if ($options['server']) {
219  // Determine the server is valid
220  list($status, $rows, $host) = ona_find_host($options['server']);
221 
222  if (!$host['id']) {
223  printmsg("DEBUG => The server ({$options['server']}) does not exist!",3);
224  $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!";
225  return(array(2, $self['error'] . "\n"));
226  }
227  }
228 
229  //printmsg("DEBUG => dhcp_server_del(): Found server, {$host['FQDN']}", 3);
230 
231  // Test that this subnet is even assigned to the server
232  list($status, $rows, $dhcpserver) = ona_get_dhcp_server_subnet_record(array('host_id' => $host['id'],'subnet_id' => $subnet['id']));
233  if (!$rows) {
234  printmsg("DEBUG => Unable to find {$subnet['name']} on server {$host['fqdn']}",3);
235  $self['error'] = "ERROR => Unable to find {$subnet['name']} on server {$host['fqdn']}";
236  return(array(11, $self['error'] . "\n"));
237  }
238 
239 
240  // If "commit" is yes, delete the record
241  if ($options['commit'] == 'Y') {
242 
243  // Check permissions
244  if (!auth('advanced') or !authlvl($host['LVL']) or !authlvl($subnet['LVL'])) {
245  $self['error'] = "Permission denied!";
246  printmsg($self['error'], 0);
247  return(array(10, $self['error'] . "\n"));
248  }
249 
250 
251 
252 
253 
254  // check if allowed to remove subnet from server
255  // check for pool assigned to the server itself
256  list($status, $rows, $pools) = db_get_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id']));
257  foreach($pools as $pool) {
258  if ($pool['dhcp_failover_group_id']) {
259  $foundfg = 0;
260  list($status, $rows, $primary) = ona_get_dhcp_failover_group_record(array('id' => $pool['dhcp_failover_group_id'],'primary_server_id' => $host['id']));
261  if ($rows) $foundfg++;
262  list($status, $rows, $secondary) = ona_get_dhcp_failover_group_record(array('id' => $pool['dhcp_failover_group_id'],'secondary_server_id' => $host['id']));
263  if ($rows) $foundfg++;
264 
265  // if a subnet/server pair is found in dhcp pools, don't allow removal
266  if ($foundfg > 0) {
267  printmsg("DEBUG => Subnet ({$subnet['name']}) has a pool assigned to this Server ({$host['fqdn']}), which is part of a failover group. The server must be removed from the failover group first.",3);
268  $self['error'] = "ERROR => Subnet ({$subnet['name']}) has a pool assigned to this Server ({$host['fqdn']}), which is part of a failover group. The server must be removed from the failover group first.";
269  return(array(12, $self['error'] . "\n"));
270  }
271 
272  }
273  }
274 
275 
276 
277 
278 // MP: remove this after testing. dhcp options should not stop us from dis-associating a subnet from a server
279 // Not really sure why I have this.. probably left over cruft from old thoughts
280 // // check if there are any DHCP parameters assigned to the subnet
281 // list($status, $rows, $tmp) = ona_get_dhcp_option_entry_record(array('subnet_id' => $subnet['id']));
282 //
283 // // if so, check that this is not the last DHCP server that services this subnet
284 // if ($rows > 0) {
285 // list($status, $rows, $tmp) = ona_get_dhcp_server_subnet_record(array('subnet_id' => $subnet['id']));
286 //
287 // // If this is the last DHCP server that services this subnet, don't allow removal until DHCP parameters are removed
288 // if($rows <= 1){
289 // printmsg("DEBUG => Subnet ({$subnet['name']}) has DHCP parameters assigned which need to be removed first",3);
290 // $self['error'] = "ERROR => Subnet ({$subnet['name']}) has DHCP parameters assigned which need to be removed first";
291 // return(array(12, $self['error'] . "\n"));
292 // }
293 // }
294 
295 
296  // delete record from dhcp_server_subnets
297  list($status, $rows) = db_delete_records($onadb, 'dhcp_server_subnets', array('id' => $dhcpserver['id']));
298  if ($status) {
299  $self['error'] = "ERROR => dhcp_server_del() SQL Query failed:" . $self['error'];
300  printmsg($self['error'],0);
301  return(array(9, $self['error'] . "\n"));
302  }
303 
304 
305  // Return the success notice
306  $self['error'] = "INFO => DHCP Subnet/Server Pair DELETED: {$subnet['name']}/{$host['fqdn']} ";
307  printmsg($self['error'],0);
308  return(array(0, $self['error'] . "\n"));
309  }
310 
311  // Otherwise display the record that would have been deleted
312  $text = <<<EOL
313  Record(s) NOT DELETED (see "commit" option)
314  Displaying record(s) that would have been removed:
315 
316  {$subnet['name']} from: {$host['fqdn']}
317 
318 EOL;
319 
320  return(array(6, $text));
321 
322 
323 }
324 
325 
326 
327 
328 // DON'T put whitespace at the beginning or end of this file!!!
329 ?>
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_server_del
dhcp_server_del($options="")
Definition: dhcp_server.inc.php:160
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
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
$host
$host
Definition: collate-convert.php:19
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
$conf
global $conf
Definition: 2-to-3.php:15
dhcp_server_add
dhcp_server_add($options="")
Definition: dhcp_server.inc.php:25
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
ona_get_dhcp_server_subnet_record
ona_get_dhcp_server_subnet_record($array)
Definition: functions_db.inc.php:1280
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
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22