Skip to content

Commit 4c2c363

Browse files
author
bmarlon
committed
* Changed "aptitude upgrade -y" to "aptitude safe-upgrade -y" since upgrade is deprecated.
+ Added DNS-import, not finished. Needs language-variables and better way to parse SOA-record and possibly some cleaning.
1 parent 4e6a24b commit 4c2c363

File tree

5 files changed

+575
-194
lines changed

5 files changed

+575
-194
lines changed

interface/web/dns/dns_import.php

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
require_once('../../lib/config.inc.php');
32+
require_once('../../lib/app.inc.php');
33+
34+
//* Check permissions for module
35+
$app->auth->check_module_permissions('dns');
36+
37+
38+
// Loading the template
39+
$app->uses('tpl,validate_dns');
40+
$app->tpl->newTemplate("form.tpl.htm");
41+
$app->tpl->setInclude('content_tpl','templates/dns_import.htm');
42+
$app->load_language_file('/web/dns/lib/lang/'.$_SESSION['s']['language'].'_dns_wizard.lng');
43+
44+
// import variables
45+
$template_id = (isset($_POST['template_id']))?intval($_POST['template_id']):0;
46+
$sys_groupid = (isset($_POST['client_group_id']))?intval($_POST['client_group_id']):0;
47+
$domain = (isset($_POST['domain'])&&!empty($_POST['domain']))?$_POST['domain']:NULL;
48+
49+
// get the correct server_id
50+
if($_SESSION['s']['user']['typ'] == 'admin') {
51+
$server_id = (isset($_POST['server_id']))?intval($_POST['server_id']):1;
52+
} else {
53+
$client_group_id = $_SESSION["s"]["user"]["default_group"];
54+
$client = $app->db->queryOneRecord("SELECT default_dnsserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
55+
$server_id = $client["default_dnsserver"];
56+
}
57+
58+
59+
// Load the templates
60+
$records = $app->db->queryAllRecords("SELECT * FROM dns_template WHERE visible = 'Y'");
61+
$template_id_option = '';
62+
$n = 0;
63+
foreach($records as $rec){
64+
$checked = ($rec['template_id'] == $template_id)?' SELECTED':'';
65+
$template_id_option .= '<option value="'.$rec['template_id'].'"'.$checked.'>'.$rec['name'].'</option>';
66+
if($n == 0 && $template_id == 0) $template_id = $rec['template_id'];
67+
$n++;
68+
}
69+
unset($n);
70+
$app->tpl->setVar("template_id_option",$template_id_option);
71+
72+
// If the user is administrator
73+
if($_SESSION['s']['user']['typ'] == 'admin') {
74+
75+
// Load the list of servers
76+
$records = $app->db->queryAllRecords("SELECT server_id, server_name FROM server WHERE mirror_server_id = 0 AND dns_server = 1 ORDER BY server_name");
77+
$server_id_option = '';
78+
foreach($records as $rec){
79+
$checked = ($rec['server_id'] == $server_id)?' SELECTED':'';
80+
$server_id_option .= '<option value="'.$rec['server_id'].'"'.$checked.'>'.$rec['server_name'].'</option>';
81+
}
82+
$app->tpl->setVar("server_id",$server_id_option);
83+
84+
// load the list of clients
85+
$sql = "SELECT groupid, name FROM sys_group WHERE client_id > 0 ORDER BY name";
86+
$clients = $app->db->queryAllRecords($sql);
87+
$client_select = '';
88+
if($_SESSION["s"]["user"]["typ"] == 'admin') $client_select .= "<option value='0'></option>";
89+
if(is_array($clients)) {
90+
foreach( $clients as $client) {
91+
$selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
92+
$client_select .= "<option value='$client[groupid]' $selected>$client[name]</option>\r\n";
93+
}
94+
}
95+
96+
$app->tpl->setVar("client_group_id",$client_select);
97+
}
98+
99+
if ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
100+
101+
// Get the limits of the client
102+
$client_group_id = $_SESSION["s"]["user"]["default_group"];
103+
$client = $app->db->queryOneRecord("SELECT client.client_id, contact_name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
104+
105+
106+
// load the list of clients
107+
$sql = "SELECT groupid, name FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".$client['client_id'];
108+
$clients = $app->db->queryAllRecords($sql);
109+
$tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$client['client_id']);
110+
$client_select = '<option value="'.$tmp['groupid'].'">'.$client['contact_name'].'</option>';
111+
if(is_array($clients)) {
112+
foreach( $clients as $client) {
113+
$selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
114+
$client_select .= "<option value='$client[groupid]' $selected>$client[name]</option>\r\n";
115+
}
116+
}
117+
118+
$app->tpl->setVar("client_group_id",$client_select);
119+
}
120+
121+
// Import the zone-file
122+
//if(1=="1")
123+
if(isset($_FILES['file']['name']) && is_uploaded_file($_FILES['file']['tmp_name']))
124+
{
125+
$sql = "SELECT server_name FROM `server` WHERE server_id=".intval($server_id)." OR mirror_server_id=".intval($server_id)." ORDER BY server_name ASC";
126+
$servers = $app->db->queryAllRecords($sql);
127+
for ($i=0;$i<count($servers);$i++)
128+
{
129+
if (substr($servers[$i]['server_name'],strlen($servers[$i]['server_name'])-1) != ".")
130+
{
131+
$servers[$i]['server_name'] .= ".";
132+
}
133+
}
134+
$lines = file($_FILES['file']['tmp_name']);
135+
//$lines = file("apriqot.se.txt");
136+
$name = str_replace("txt","",$_FILES['file']['name']);
137+
$name = str_replace("zone","",$name);
138+
if ($domain !== NULL)
139+
{
140+
$name = $domain;
141+
if (substr($name,strlen($name)-1) != ".")
142+
{
143+
$name .= ".";
144+
}
145+
}
146+
$i = 0;
147+
$r = 0;
148+
$s = 0;
149+
$dns_rr = array();
150+
foreach ($lines as $line)
151+
{
152+
if (substr($line,0,1)!=';'&&substr($line,0,1)!='$')
153+
{
154+
$line = str_replace("\n",NULL,$line);
155+
$i++;
156+
157+
// TODO - Find a better way to parse the SOA record. Lazy checking.
158+
if ($i <= 7)
159+
{
160+
if ($i > 1)
161+
{
162+
$s++;
163+
$line = str_replace("\t",NULL,$line);
164+
if (!empty($line))
165+
{
166+
print(strpos(";",$line));
167+
$line = substr($line,0,strpos($line,";"));
168+
if ($s == 1)
169+
$soa['serial'] = $line;
170+
else if ($s == 2)
171+
$soa['refresh'] = $line;
172+
else if ($s == 3)
173+
$soa['retry'] = $line;
174+
else if ($s == 4)
175+
$soa['expire'] = $line;
176+
else if ($s == 5)
177+
$soa['ttl'] = $line;
178+
179+
}
180+
}
181+
else
182+
{
183+
$line = str_replace("\t",",",$line);
184+
$recs = explode(",",$line);
185+
186+
foreach ($recs as $key => $rec)
187+
{
188+
$rec = trim($rec);
189+
//name type data aux ttl active
190+
if ($key == 0)
191+
{
192+
if ($rec == '@' || $rec == "")
193+
{
194+
$rec = $name;
195+
}
196+
197+
$soa['name'] = $rec;
198+
}
199+
200+
if ($key != 0 && strtolower($rec) == 'soa')
201+
{
202+
$typekeys[$s] = $key;
203+
}
204+
else if ($key > $typekey[$r])
205+
{
206+
if ($rec != ""&&$rec != "(")
207+
{
208+
$rec = explode(" ",$rec);
209+
210+
$soa['ns'] = $servers[0]['server_name'];
211+
$soa['mbox'] = $rec[1];
212+
}
213+
}
214+
}
215+
}
216+
}
217+
else
218+
{
219+
$line = str_replace("\n","",trim($line));
220+
221+
if (!empty($line))
222+
{
223+
224+
preg_match_all('/(.*?)\s*IN\s*(A|CNAME|MX|TXT|NS|AAAA)\s*(.*)/',$line, $recs);
225+
226+
if ($recs[1][0] == '@' || trim($recs[1][0]) == "")
227+
{
228+
$recs[1][0] = $name;
229+
}
230+
$dns_rr[$r]['name'] = $recs[1][0];
231+
$dns_rr[$r]['type'] = $recs[2][0];
232+
if (strtolower($dns_rr[$r]['type'])=='mx')
233+
{
234+
$recs[3][0] = str_replace(" ","\t",$recs[3][0]);
235+
$mx[$r] = explode("\t",$recs[3][0]);
236+
for ($m=1;$m<count($mx[$r]);$m++)
237+
{
238+
if (!empty($mx[$r][$m]))
239+
$dns_rr[$r]['data'] = $mx[$r][$m];
240+
}
241+
242+
$dns_rr[$r]['aux'] = $mx[$r][0];
243+
}
244+
else if (strtolower($dns_rr[$r]['type'])=='txt')
245+
{
246+
$dns_rr[$r]['data'] = substr($recs[3][0],1,(strlen($recs[3][0])-2));
247+
}
248+
else
249+
{
250+
$dns_rr[$r]['data'] = $recs[3][0];
251+
}
252+
253+
if (strtolower($dns_rr[$r]['type'])=='ns' && strtolower($dns_rr[$r]['name'])==$name)
254+
{
255+
unset($dns_rr[$r]);
256+
}
257+
258+
$r++;
259+
}
260+
}
261+
262+
}
263+
}
264+
265+
foreach ($servers as $server)
266+
{
267+
$dns_rr[$r]['name']=$name;
268+
$dns_rr[$r]['type']='NS';
269+
$dns_rr[$r]['data'] = $server['server_name'];
270+
$dns_rr[$r]['aux'] = 0;
271+
272+
$r++;
273+
}
274+
print('<pre>');
275+
print_r($dns_rr);
276+
print('</pre>');
277+
// Insert the soa record
278+
$sys_userid = $_SESSION['s']['user']['userid'];
279+
$origin = $app->db->quote($soa['name']);
280+
$ns = $app->db->quote($soa['ns']);
281+
$mbox = $app->db->quote($soa['mbox']);
282+
$refresh = $app->db->quote($soa['refresh']);
283+
$retry = $app->db->quote($soa['retry']);
284+
$expire = $app->db->quote($soa['expire']);
285+
$minimum = $app->db->quote($soa['ttl']);
286+
$ttl = $app->db->quote($soa['ttl']);
287+
$xfer = $app->db->quote('');
288+
$serial = $app->db->quote(intval($soa['serial'])+1);
289+
290+
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `origin`, `ns`, `mbox`, `serial`, `refresh`, `retry`, `expire`, `minimum`, `ttl`, `active`, `xfer`) VALUES
291+
('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$origin', '$ns', '$mbox', '$serial', '$refresh', '$retry', '$expire', '$minimum', '$ttl', 'Y', '$xfer')";
292+
$dns_soa_id = $app->db->datalogInsert('dns_soa', $insert_data, 'id');
293+
294+
// Insert the dns_rr records
295+
if(is_array($dns_rr) && $dns_soa_id > 0)
296+
{
297+
foreach($dns_rr as $rr)
298+
{
299+
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
300+
('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$rr[name]', '$rr[type]', '$rr[data]', '$rr[aux]', '$rr[ttl]', 'Y')";
301+
$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
302+
}
303+
}
304+
305+
header('Location: /dns/dns_soa_edit.php?id='.$dns_soa_id);
306+
}
307+
308+
$app->tpl->setVar('msg',$msg);
309+
$app->tpl->setVar('error',$error);
310+
311+
$app->tpl->setVar("title",'Import Zone Files');
312+
313+
$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_dns_wizard.lng';
314+
include($lng_file);
315+
$app->tpl->setVar($wb);
316+
317+
$app->tpl_defaults();
318+
$app->tpl->pparse();
319+
320+
321+
?>

interface/web/dns/lib/module.conf.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
'link' => 'dns/dns_wizard.php',
1313
'html_id' => 'dns_wizard');
1414

15+
$items[] = array( 'title' => "Import Zone File",
16+
'target' => 'content',
17+
'link' => 'dns/dns_import.php',
18+
'html_id' => 'dns_import');
19+
1520
if($_SESSION["s"]["user"]["typ"] == 'admin') {
1621
$items[] = array( 'title' => "Templates",
1722
'target' => 'content',
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<h2><tmpl_var name="list_head_txt"></h2>
2+
<p><tmpl_var name="list_desc_txt"></p>
3+
4+
<tmpl_if name="error">
5+
<div id="errorMsg"><h3>ERROR</h3><ol><tmpl_var name="error"></ol></div>
6+
</tmpl_if>
7+
8+
<div class="panel panel_dns_wizard">
9+
10+
<div class="pnl_formsarea">
11+
<fieldset class="inlineLabels"><legend>Zone File Import</legend>
12+
<tmpl_if name="is_admin">
13+
<div class="ctrlHolder">
14+
<label for="server_id">{tmpl_var name='server_id_txt'}</label>
15+
<select name="server_id" id="server_id" class="selectInput">
16+
{tmpl_var name='server_id'}
17+
</select>
18+
</div>
19+
<div class="ctrlHolder">
20+
<label for="client_group_id">{tmpl_var name='client_txt'}</label>
21+
<select name="client_group_id" id="client_group_id" class="selectInput">
22+
{tmpl_var name='client_group_id'}
23+
</select>
24+
</div>
25+
</tmpl_if>
26+
<div class="ctrlHolder">
27+
<label for="domain">{tmpl_var name='domain_txt'}. Can be left empty if domain name is in the filename or the zone-file content.</label>
28+
<input name="domain" id="domain" value="{tmpl_var name='domain'}" size="30" maxlength="255" type="text" class="textInput" />
29+
</div>
30+
31+
<div class="ctrlHolder" style="margin-left:0px;left:0px;padding-left:0px;">
32+
<input name="file" id="file" size="30" type="file" class="fileUpload" />
33+
</div>
34+
35+
<tmpl_if name="msg">
36+
<div id="OKMsg"><p><tmpl_var name="msg"></p></div>
37+
</tmpl_if>
38+
<tmpl_if name="error">
39+
<div id="errorMsg"><h3>ERROR</h3><ol><tmpl_var name="error"></ol></div>
40+
</tmpl_if>
41+
42+
<input type="hidden" name="id" value="{tmpl_var name='id'}">
43+
44+
<div class="buttonHolder buttons">
45+
<button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitUploadForm('pageForm','dns/dns_import.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
46+
<button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('dns/dns_soa_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
47+
</div>
48+
49+
</fieldset>
50+
</div>
51+
52+
</div>

interface/web/dns/templates/dns_soa_list.htm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ <h2><tmpl_var name="list_head_txt"></h2>
1212
<button class="iconstxt icoAdd" type="button" onClick="loadContent('dns/dns_soa_edit.php');">
1313
<span>{tmpl_var name="add_new_record_txt"}</span>
1414
</button>
15+
<button class="iconstxt icoAdd" type="button" onClick="loadContent('dns/dns_import.php');">
16+
<span>Import Zone File</span>
17+
</button>
1518
</div>
1619
</fieldset>
1720
</div>

0 commit comments

Comments
 (0)