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)  

domain_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: domain_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) = domain_server_add('domain=&server=host.something.com');
25 function domain_server_add($options="") {
26 
27  // The important globals
28  global $conf, $self, $onadb;
29 
30  // Version - UPDATE on every edit!
31  $version = '1.02';
32 
33  printmsg("DEBUG => domain_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['domain'] 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 domain record to a DNS server
50 
51  Synopsis: domain_server_add [KEY=VALUE] ...
52 
53  Required:
54  domain=NAME or ID domain name or ID
55  server=NAME[.DOMAIN] or ID server name or ID
56  role=master|slave|forward role of this server for this domain
57 
58 EOM
59 
60  ));
61  }
62 
63 
64 // if (is_numeric($options['domain'])) {
65 // $domainsearch['id'] = $options['domain'];
66 // } else {
67 // $domainsearch['name'] = strtolower($options['domain']);
68 // }
69 
70  // Determine the entry itself exists
71  list($status, $rows, $domain) = ona_find_domain($options['domain'],0);
72 
73  // Test to see that we were able to find the specified record
74  if (!$domain['id']) {
75  printmsg("DEBUG => Unable to find the domain record using {$options['domain']}!",3);
76  $self['error'] = "ERROR => Unable to find the domain record using {$options['domain']}!";
77  return(array(4, $self['error']. "\n"));
78  }
79 
80  printmsg("DEBUG => domain_server_add(): Found domain, {$domain['name']}", 3);
81 
82  // Determine the server is valid
83  list($status, $rows, $ns_dns) = ona_find_dns_record($options['server']);
84  list($status, $rows, $interface) = ona_find_interface($ns_dns['interface_id']);
85 
86  $host['id'] = $interface['host_id'];
87 
88  if (!$host['id']) {
89  printmsg("DEBUG => The server ({$options['server']}) does not exist!",3);
90  $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!";
91  return(array(2, $self['error'] . "\n"));
92  }
93 
94  // what is the role for this server.
95  switch (strtolower($options['role'])) {
96  case "forward":
97  $role = "forward";
98  break;
99  case "master":
100  $role = "master";
101  break;
102  case "slave":
103  $role = "slave";
104  break;
105  default:
106  $role = "master";
107  }
108 
109 
110 
111  // Check permissions
112  if (!auth('advanced')) {
113  $self['error'] = "Permission denied!";
114  printmsg($self['error'], 0);
115  return(array(12, $self['error'] . "\n"));
116  }
117 
118 
119  // Test that this domain isnt already assigned to the server
120  list($status, $rows, $domainserver) = ona_get_dns_server_domain_record(array('host_id' => $host['id'],'domain_id' => $domain['id']));
121  if ($rows) {
122  printmsg("DEBUG => Domain {$domain['name']} already assigned to {$options['server']}",3);
123  $self['error'] = "ERROR => Domain {$domain['name']} already assigned to {$options['server']}";
124  return(array(11, $self['error'] . "\n"));
125  }
126 
127 
128  // Get the next ID
129  $id = ona_get_next_id('dns_server_domains');
130  if (!$id) {
131  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
132  printmsg($self['error'],0);
133  return(array(6, $add_to_error . $self['error'] . "\n"));
134  }
135 
136  printmsg("DEBUG => domain_server_add(): New DNS server domain ID: $id", 3);
137 
138  // Add new record to dns_server_domains
139  list($status, $rows) =
141  $onadb,
142  'dns_server_domains',
143  array(
144  'id' => $id,
145  'host_id' => $host['id'],
146  'domain_id' => $domain['id'],
147  'role' => $role,
148  'rebuild_flag' => 1
149 
150  )
151  );
152  if ($status or !$rows) {
153  $self['error'] = "ERROR => domain_server_add() SQL Query failed:" . $self['error'];
154  printmsg($self['error'],0);
155  return(array(8, $add_to_error . $self['error'] . "\n"));
156  }
157 
158 
159  // Test that there are no NS records for this pair already
160  // ASSUMPTION: MP this will always be just one record??
161  list ($status, $dnsrows, $dnsrec) =
163  $onadb,
164  'dns',
165  "domain_id = {$domain['id']} AND type = 'NS' AND interface_id in (select id from interfaces where host_id = {$host['id']})"
166  );
167 
168  // Auto add the NS record if there were none found already. the user can remove any NS records they dont want afterwards
169  if (!$dnsrows) {
170  printmsg("DEBUG => Auto adding a NS record for {$options['server']}.", 0);
171  // Run dns_record_add as a NS type
172  list($status, $output) = run_module('dns_record_add', array('name' => $domain['fqdn'],'pointsto' => $options['server'], 'type' => 'NS'));
173  if ($status)
174  return(array($status, $output));
175  $add_to_error .= $output;
176  }
177  else {
178  printmsg("DEBUG => Found existing NS record for {$options['server']}. Skipping the auto add.", 0);
179  }
180 
181  // Return the success notice
182  $self['error'] = "INFO => DNS Domain/Server Pair ADDED: {$domain['name']}/{$options['server']} ";
183  printmsg($self['error'],0);
184  return(array(0, $add_to_error . $self['error'] . "\n"));
185 
186 
187 }
188 
189 
190 
191 
192 
193 
195 // Function: domain_server_del (string $options='')
196 //
197 // Input Options:
198 // $options = key=value pairs of options for this function.
199 // multiple sets of key=value pairs should be separated
200 // by an "&" symbol.
201 //
202 // Output:
203 // Returns a two part list:
204 // 1. The exit status of the function. 0 on success, non-zero on
205 // error. All errors messages are stored in $self['error'].
206 // 2. A textual message for display on the console or web interface.
207 //
208 // Example: list($status, $result) = domain_server_del('domain=something.com&server=test-server.something.com');
210 function domain_server_del($options="") {
211 
212  // The important globals
213  global $conf, $self, $onadb;
214 
215  // Version - UPDATE on every edit!
216  $version = '1.02';
217 
218  printmsg("DEBUG => domain_server_del({$options}) called", 3);
219 
220  // Parse incoming options string to an array
221  $options = parse_options($options);
222 
223  // Sanitize options[commit] (default is yes)
224  $options['commit'] = sanitize_YN($options['commit'], 'N');
225 
226 
227  // Return the usage summary if we need to
228  if ($options['help'] or !($options['domain'] and $options['server']) ) {
229  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
230  $self['error'] = 'ERROR => Insufficient parameters';
231  return(array(1,
232 <<<EOM
233 
235 Removes a domain record from a DNS server
236 
237  Synopsis: domain_server_del [KEY=VALUE] ...
238 
239  Required:
240  domain=NAME or ID domain name or ID
241  server=NAME[.DOMAIN] or ID server name or ID
242 
243  Optional:
244  commit=[Y|N] commit db transaction (no)
245 
246 EOM
247 
248  ));
249  }
250 
251 
252  if (is_numeric($options['domain'])) {
253  $domainsearch['id'] = $options['domain'];
254  } else {
255  $domainsearch['name'] = strtoupper($options['domain']);
256  }
257 
258  // Determine the entry itself exists
259  list($status, $rows, $domain) = ona_get_domain_record($domainsearch);
260 
261  // Test to see that we were able to find the specified record
262  if (!$domain['id']) {
263  printmsg("DEBUG => Unable to find the domain record using {$options['domain']}!",3);
264  $self['error'] = "ERROR => Unable to find the domain record using {$options['domain']}!";
265  return(array(4, $self['error']. "\n"));
266  }
267 
268  printmsg("DEBUG => domain_server_del(): Found domain, {$domain['name']}", 3);
269 
270  if ($options['server']) {
271  // Determine the server is valid
272  list($status, $rows, $host) = ona_find_host($options['server']);
273 
274  if (!$host['id']) {
275  printmsg("DEBUG => The server ({$options['server']}) does not exist!",3);
276  $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!";
277  return(array(2, $self['error'] . "\n"));
278  }
279  }
280 
281  // Test that this domain is even assigned to the server
282  list($status, $rows, $domainserver) = ona_get_dns_server_domain_record(array('host_id' => $host['id'],'domain_id' => $domain['id']));
283  if (!$rows) {
284  printmsg("DEBUG => Unable to find {$domain['name']} on server {$host['fqdn']}",3);
285  $self['error'] = "ERROR => Unable to find {$domain['name']} on server {$host['fqdn']}";
286  return(array(11, $self['error'] . "\n"));
287  }
288 
289  // Test that there are no NS records for this pair
290  // ASSUMPTION: MP this will always be just one record??
291  // depending on how the user has their NS records set up, we may not find anything.
292  list ($status, $dnsrows, $dnsrec) =
294  $onadb,
295  'dns',
296  "domain_id = {$domain['id']} AND type = 'NS' AND interface_id in (select id from interfaces where host_id = {$host['id']})"
297  );
298 
299  // If "commit" is yes, delete the record
300  if ($options['commit'] == 'Y') {
301 
302  // Check permissions
303  if (!auth('advanced') or !authlvl($host['LVL']) or !authlvl($domain['LVL'])) {
304  $self['error'] = "Permission denied!";
305  printmsg($self['error'], 0);
306  return(array(10, $self['error'] . "\n"));
307  }
308 
309 
310  // delete record from domain_server_domains
311  list($status, $rows) = db_delete_records($onadb, 'dns_server_domains', array('id' => $domainserver['id']));
312  if ($status) {
313  $self['error'] = "ERROR => domain_server_del() SQL Query failed:" . $self['error'];
314  printmsg($self['error'],0);
315  return(array(9, $self['error'] . "\n"));
316  }
317 
318  // Run the module to delete the associated NS record.. Only if we found a dns record for NS
319  if ($dnsrec['id']) {
320  list($status, $output) = run_module('dns_record_del', array('name' => $dnsrec['id'], 'type' => 'NS', 'commit' => 'Y'));
321  if ($status) {
322  $self['error'] = "ERROR => domain_server_del() NS record delete failed:" . $output;
323  printmsg($self['error'],0);
324  return(array(9, $self['error'] . "\n"));
325  }
326  else {
327  // add the output to self error for display
328  $add_to_error = $output;
329  }
330  }
331 
332  // Return the success notice
333  $self['error'] = "INFO => DNS Domain/Server Pair DELETED: {$domain['name']}/{$host['fqdn']} ";
334  printmsg($self['error'],0);
335  return(array(0, $add_to_error. $self['error'] . "\n"));
336  }
337 
338  // Otherwise display the record that would have been deleted
339  $text = <<<EOL
340  Record(s) NOT DELETED (see "commit" option)
341  Displaying record(s) that would have been removed:
342 
343  {$domain['name']} from: {$host['fqdn']}
344 
345 
346 EOL;
347 
348  if ($dnsrows) {
349  $text .= " Removing related NS record, if any. Please double check your NS records for this domain.\n";
350  }
351 
352  return(array(6, $text));
353 
354 
355 }
356 
357 
358 
359 
360 // DON'T put whitespace at the beginning or end of this file!!!
361 ?>
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
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_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
$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
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
run_module
run_module($module='', $options='', $transaction=1)
Definition: functions_general.inc.php:1468
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
ona_get_dns_server_domain_record
ona_get_dns_server_domain_record($array)
Definition: functions_db.inc.php:1284
$conf
global $conf
Definition: 2-to-3.php:15
domain_server_del
domain_server_del($options="")
Definition: domain_server.inc.php:210
ona_get_domain_record
ona_get_domain_record($array='', $order='')
Definition: functions_db.inc.php:1134
$output
$output
Definition: dcm.php:16
ona_find_dns_record
ona_find_dns_record($search="", $type='', $int_id=0)
Definition: functions_db.inc.php:1703
$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
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
domain_server_add
domain_server_add($options="")
Definition: domain_server.inc.php:25
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22