File: createsubnet.php

package info (click to toggle)
ipplan 4.92a-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,256 kB
  • ctags: 11,121
  • sloc: php: 44,277; sh: 387; perl: 257; xml: 97; makefile: 57; sql: 32
file content (322 lines) | stat: -rwxr-xr-x 11,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php

// IPplan v4.92a
// Aug 24, 2001
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

require_once("../ipplanlib.php");
require_once("../adodb/adodb.inc.php");
require_once("../class.dbflib.php");
require_once("../layout/class.layout");
require_once("../auth.php");
require_once("../class.templib.php");

$auth = new SQLAuthenticator(REALM, REALMERROR);

// And now perform the authentication
$grps=$auth->authenticate();

// save the last customer used
// must set path else Netscape gets confused!
setcookie("ipplanCustomer","$cust",time() + 10000000, "/");
setcookie("ipplanGroup","$admingrp",time() + 10000000, "/");

// set language
isset($_COOKIE["ipplanLanguage"]) && myLanguage($_COOKIE['ipplanLanguage']);

//setdefault("window",array("bgcolor"=>"white"));
//setdefault("table",array("cellpadding"=>"0"));
//setdefault("text",array("size"=>"2"));

$title=my_("Create subnet results");
newhtml($p);
$w=myheading($p, $title, true);

// explicitly cast variables as security measure against SQL injection
list($cust, $admingrp, $ipaddr, $num, $descrip, $size, $addhostinfo, $addnmapinfo, $dhcp, $findfree) = myRegister("I:cust S:admingrp S:ipaddr I:num S:descrip I:size I:addhostinfo I:addnmapinfo I:dhcp S:findfree");
list($userfld) = myRegister("A:userfld");  // for template fields

$descrip=trim($descrip);

// must only check once - might need to create multiple nets
$nodescrip=0;
if (strlen($descrip) == 0) {
   $nodescrip=1;
}
if ($num <1 or $num > 255) {
   myError($w,$p, my_("Number of subnets to create is out of bounds."));
}

// basic sequence is connect, search, interpret search
// result, close connection

$ds=new IPplanDbf() or myError($w,$p, my_("Could not connect to database"));

// error checks
if (!$ipaddr)
   myError($w,$p, my_("IP address may not be blank"));
else if (testIP($ipaddr))
   myError($w,$p, my_("Invalid IP address"));
else if (!$size)
   myError($w,$p, my_("Size may not be zero"));
elseif ($size > 1) {
   if (TestBaseAddr(inet_aton3($ipaddr), $size)) {
      myError($w,$p, my_("Invalid base address!"));
   }
}

if ($dhcp) {
    // use dhcp template (for additional subnet information)
    $template=new IPplanIPTemplate("basetemplate-dhcp", $cust);
}
else {
    // use base template (for additional subnet information)
    $template=new IPplanIPTemplate("basetemplate", $cust);
}

$info="";
if ($template->is_error() == FALSE) {
    // PROBLEM HERE: if template create suddenly returns error (template file
    // permissions, xml error etc), then each submit thereafter will erase
    // previous contents - this is not good
    $template->Merge($userfld);
    $err=$template->Verify($w);

    if ($template->is_blank() == FALSE) {
        $info=$template->encode();
    }
}

if ($err) {
    printhtml($p);
    exit;
}

if ($addhostinfo) {
   // increase time limit for scans - will have no effect in safe mode is on
   @set_time_limit(90);
}

$warn=0;
for ($i=1; $i <= $num; $i++) {
    // work out new base for each iteration of loop
    $base=inet_aton($ipaddr)+(($i-1)*$size);
    // create description if none
    if ($nodescrip) {
        $descrip="NET-".str_replace(".", "-", inet_ntoa($base));
    }

    // test if subnet to create is within bounds
    foreach ($grps as $value) {
        if ($extst = $ds->TestBounds($base, $size, $value)) {
            // got an overlap, allowed to create
            break;
        }
    }
    // could not find new subnet within any of the defined bounds
    // so do not create
    if (!$extst) {
        $warn=1;
        myError($w,$p, sprintf(my_("Subnet %s not created - out of defined authority boundary"), inet_ntoa($base))."\n", FALSE);
        continue;
    }

    $restemp=$ds->GetDuplicateSubnet($base, $size, $cust);
    if ($restemp->FetchRow()) {
        $warn=1;
        myError($w,$p, sprintf(my_("Subnet %s could not be created - possibly overlaps with an existing subnet"), inet_ntoa($base))."\n", FALSE);
    }
    else {

        // find subnets from other customers that overlap
        $result=$ds->GetDuplicateSubnetAll($base, $size, $grps);
        if ($row=$result->FetchRow()) {
            $warn=1;
            insert($w,textb(my_("WARNING: ")));
            insert($w,text(sprintf(my_("Subnet %s overlaps with the following subnets from other customers.  This may not be a problem as the subnet may occur in both customers routing tables:"),inet_ntoa($base))));
            insert($w,block("<p>"));

            // create a table
            insert($w,$t = table(array("cols"=>"3",
                            "class"=>"outputtable")));
            // draw heading
            setdefault("cell",array("class"=>"heading"));
            insert($t,$c = cell());
            insert($c,text(my_("Base address")));
            insert($t,$c = cell());
            insert($c,text(my_("Subnet description")));
            insert($t,$c = cell());
            insert($c,text(my_("Customer")));


            do {
                setdefault("cell",array("class"=>color_flip_flop()));

                insert($t,$c = cell());
                insert($c,text(inet_ntoa($row["baseaddr"])));

                insert($t,$c = cell());
                insert($c,text($row["descrip"]));

                insert($t,$c = cell());
                insert($c,text($row["custdescrip"]));
            } while($row = $result->FetchRow());

            setdefault("cell", "");
            insert($w,block("<p>"));
        }

        // check if user belongs to customer admin group
        $result=$ds->GetCustomerGrp($cust);
        // can only be one row - does not matter if nothing is 
        // found as array search will return false
        $row = $result->FetchRow();
        if (!in_array($row["admingrp"], $grps)) {
            myError($w,$p, my_("You may not create a subnet for this customer as you are not a member of the customers admin group"));
        }

        $ds->DbfTransactionStart();
        // use the first group user belongs to create subnet
        if ($id = $ds->CreateSubnet($base, $size, $descrip, $cust, $dhcp, $admingrp)) {
            $ds->AuditLog(array("event"=>170, "action"=>"create subnet", 
                    "descrip"=>$descrip, "user"=>getAuthUsername(), "baseaddr"=>inet_ntoa($base),
                    "size"=>$size, "cust"=>$cust));

            insert($w,text(sprintf(my_("Subnet %s created"), inet_ntoa($base))));

            // fill new subnet with nmap
            if (NMAP != "" and $addnmapinfo and $size >= 4 and $size <= 1024) {
                if (ProcessNmap($ds, $base, $id, $size)) {
                    insert($w,textbr());
                    myError($w,$p, my_("NMAP data not in XML format or XML not available"), FALSE);
                }
            }
            // fill new subnet with DNS info
            else if ($addhostinfo and $size > 0) {
                // remember to skip broadcast and network addresses!
                for ($i_dns=1; $i_dns < $size-1; $i_dns++) {
                    $hname=gethostbyaddr(inet_ntoa($base+$i_dns));
                    if ($hname != inet_ntoa($base+$i_dns))
                        $ds->AddIP($base+$i_dns, $id, "", "", "", "", "", $hname, "");
                }
            }

            if (!empty($info)) {
                $result = &$ds->ds->Execute("INSERT INTO baseadd
                        (info, baseindex)
                        VALUES
                        (".$ds->ds->qstr($info).", $id)");
            }

            $ds->DbfTransactionEnd();

            insert($w,textbr());
        }
        else {
            myError($w,$p, sprintf(my_("Subnet %s could not be created"), inet_ntoa($base)));
        }

    }
} // end of for loop for multiple subnet creation

// add link if script was called from findfree
// only if all subnets created without error or warning
if (!$warn and $findfree) {
    // $findfree contains HTTP_REFERER which is a full URI already - OK
    header("Location: ".base64_decode($findfree));
    exit;
}
if (!$warn and $num==1) {
    header("Location: ".location_uri("displaybase.php?cust=$cust&ipaddr=$ipaddr"));
    exit;
}

printhtml($p);

// $base=baseaddr of subnet, $id=baseindex
function ProcessNmap($ds, $base, $id, $size) {

   global $addhostinfo;

   $resarr=array();

   if ($addhostinfo)
      $command=NMAP." -sP ".escapeshellarg(inet_ntoa($base)."/".inet_bits($size))." -oX -";
   else
      $command=NMAP." -n -sP ".escapeshellarg(inet_ntoa($base)."/".inet_bits($size))." -oX -";

   exec($command, $resarr, $retval);
   // did NMAP fail due to safe mode or other error?
   if ($retval) { 
       return 1;
   }
   else { // no error
       require_once("../xmllib.php");

       $input = implode("",$resarr);
       // nmap parser always returns arrays for tags of form
       // [tagname][0...x][element]
       // array index will mostly be zero if one as most results
       // return 1 tag
       $xml_parser = new xmlnmap("HOST");
       if (!$xml_parser->parser) {
           return 1;  // XML parser failure - probably not compiled in
       }
       $output=$xml_parser->parse($input);
       if (!$output) {
           return 1;  // not XML format
       }

       foreach ($output as $value) {
           if ($value["STATUS"][0]["STATE"]=="up") {
               // need to loop through ADDR array here! Check that 
               // ["ADDRESS"][0]["ADDRTYPE"]=="ipv4" or 
               // ["ADDRESS"][0]["ADDRTYPE"]=="mac"
               $newbase=inet_aton($value["ADDRESS"][0]["ADDR"]);
               $newmac="";
               if ($value["ADDRESS"][1]["ADDRTYPE"]=="mac") {
                   $newmac=str_replace(array(":", "-", " "), "", $value["ADDRESS"][1]["ADDR"]);
               }
               if(empty($value["OSMATCH"][0]["NAME"])) {
                   $newuser="";
               }
               else {
                   $newuser=$value["OSMATCH"][0]["NAME"];
               }
               $newdescrip="active";
               if(!empty($value["HOSTNAME"][0]["NAME"])) {
                   $newhname=$value["HOSTNAME"][0]["NAME"];
               }
               else {
                   $newhname="";
               }

               // check within range of subnet before adding to ignore
               // broadcast and network addresses
               if ($newbase > $base and $newbase < $base+$size-1) {
                   $ds->AddIP($newbase, $id, $newuser, "", "", $newmac, $newdescrip, $newhname, "");
                   // address was polled? So add polled status
                   $ds->UpdateIPPoll($id, $newbase);
               }
           }
       }

       return 0;
   }

}
?>