1<?php 2 3# Check we have been included via subnet-scan-excute.php and not called directly 4require("subnet-scan-check-included.php"); 5 6/* 7 * Discover new subnets with snmp 8 * 9 * Discover new slave subnets with snmp 10 * 11 *******************************/ 12 13# snmp class 14$Snmp = new phpipamSNMP (); 15 16# scan disabled 17if ($User->settings->enableSNMP!="1") { $Result->show("danger", "SNMP module disbled", true, $ajax_loaded); } 18 19# section check 20if (!is_numeric($_POST['sectionId'])) { $Result->show("danger", "Invalid section Id", true, $ajax_loaded); } 21if (!is_numeric($_POST['subnetId'])) { $Result->show("danger", "Invalid subnet Id", true, $ajax_loaded); } 22 23$section = $Subnets->fetch_object("sections", "id", $_POST['sectionId']); 24if ($section===false) { $Result->show("danger", "Invalid section Id", true, $ajax_loaded); } 25 26# check section permissions 27if($Subnets->check_permission ($User->user, $_POST['sectionId']) != 3) { $Result->show("danger", _('You do not have permissions to add new subnet in this section')."!", true, $ajax_loaded); } 28 29// no errors 30error_reporting(E_ERROR); 31 32# fetch devices that use get_routing_table query 33$devices_used = $Tools->fetch_multiple_objects ("devices", "snmp_queries", "%get_routing_table%", "id", true, true); 34 35# recolaculate ids for info 36foreach ($devices_used as $d) { 37 $devices_info[$d->id] = $d; 38} 39 40// if none set die 41if ($devices_used===false) { $Result->show("danger", "No devices for SNMP route table query available"."!", true, $ajax_loaded); } 42 43// ok, we have devices, connect to each device and do query 44foreach ($devices_used as $d) { 45 // init 46 $Snmp->set_snmp_device ($d); 47 // execute 48 try { 49 $res = $Snmp->get_query("get_routing_table"); 50 // remove those not in subnet 51 if (sizeof($res)>0) { 52 // save for debug 53 $debug[$d->hostname][$q] = $res; 54 55 // save result 56 $found[$d->id]["get_vlan_table"] = $res; 57 } 58 } catch (Exception $e) { 59 // save for debug 60 $debug[$d->hostname]["get_vlan_table"] = $res; 61 62 $errors[] = $e->getMessage(); 63 } 64} 65# none and errors 66if(sizeof($found)==0 && isset($errors)) { $Result->show("info", _("No new subnets found")."</div><hr><div class='alert alert-warning'>".implode("<hr>", $errors)."</div>", true, $ajax_loaded); } 67# none 68elseif(sizeof($found)==0) { $Result->show("info", _("No new subnets found")."!", true, $ajax_loaded); } 69# ok 70else { 71 # fetch all permitted domains 72 $permitted_domains = $Sections->fetch_section_domains ($_POST['sectionId']); 73 # fetch all belonging vlans 74 $cnt = 0; 75 foreach($permitted_domains as $k=>$d) { 76 //fetch domain 77 $domain = $Tools->fetch_object("vlanDomains","id",$d); 78 // fetch vlans and append 79 $vlans = $Tools->fetch_multiple_objects("vlans", "domainId", $domain->id, "number"); 80 //save to array 81 $out[$d]['domain'] = $domain; 82 $out[$d]['vlans'] = $vlans; 83 //count add 84 $cnt++; 85 } 86 //filter out empty 87 $permitted_domains = array_filter($out); 88 89 90 # fetch all permitted domains 91 $permitted_nameservers = $Sections->fetch_section_nameserver_sets ($_POST['sectionId']); 92 93 # fetch all belonging nameserver set 94 $cnt = 0; 95 96 # Only parse nameserver if any exists 97 if($permitted_nameservers != false) { 98 foreach($permitted_nameservers as $k=>$n) { 99 // fetch nameserver sets and append 100 $nameserver_set = $Tools->fetch_multiple_objects("nameservers", "id", $n, "name", "namesrv1"); 101 //save to array 102 $nsout[$n] = $nameserver_set; 103 //count add 104 $cnt++; 105 } 106 //filter out empty 107 $permitted_nameservers = isset($nsout) ? array_filter($nsout) : false; 108 } 109 110 111 # fetch all IPv4 masks 112 $masks = $Subnets->get_ipv4_masks (); 113 114 # fetch vrfs 115 if($User->settings->enableVRF==1) 116 $vrfs = $Tools->fetch_all_objects("vrf", "name"); 117?> 118 119<!-- header --> 120<?php if ($ajax_loaded) { ?> 121<div class="pHeader"><?php print _('Scan results'); ?></div> 122<?php } ?> 123 124<!-- content --> 125<?php if ($ajax_loaded) { ?> 126<div class="pContent"> 127<?php } ?> 128 <?php 129 130 //table 131 print '<form id="editSubnetDetailsSNMPall">'; 132 print "<input type='hidden' name='csrf_cookie' value='$csrf'>"; 133 print "<table class='table table-striped table-top table-condensed' id='editSubnetDetailsSNMPallTable'>"; 134 135 // titles 136 print "<tr>"; 137 print " <th>"._("Subnet")."</th>"; 138 print " <th>"._("Description")."</th>"; 139 print " <th>"._("VLAN")."</th>"; 140 if($User->settings->enableVRF==1) 141 print " <th>"._("VRF")."</th>"; 142 print " <th>"._("Nameservers")."</th>"; 143 print " <th style='width:5px;'></th>"; 144 print "</tr>"; 145 146 //set colspan 147 $colspan = $User->settings->enableVRF==1 ? 6 : 5; 148 149 // alive 150 $m=0; 151 foreach($found as $deviceid=>$device) { 152 // we need to check if subnetId != 0 and isFolder!=1 for overlapping 153 if($_POST['subnetId']!=="0") { 154 $subnet = $Tools->fetch_object("subnets", "id", $_POST['subnetId']); 155 if ($subnet===false) { $Result->show("info", _("Invalid subnet ID")."!", true, true, false, true); } 156 } 157 // fetch device 158 $device_details = $Tools->fetch_object("devices", "id", $deviceid); 159 160 // loop 161 foreach ($device as $query_result ) { 162 if ($query_result!==false) { 163 //count results for each device 164 $dc=0; 165 166 print "<tr>"; 167 print " <th colspan='$colspan' style='padding:10px;'> <i class='fa fa-times btn btn-xs btn-danger remove-snmp-results' data-target='device-$deviceid'></i> ".$device_details->hostname."</th>"; 168 print "</tr>"; 169 170 print "<tbody id=device-$deviceid>"; 171 foreach ($query_result as $ip) { 172 //get bitmask 173 foreach ($masks as $k=>$n) { 174 if ($n->netmask == $ip['mask']) { 175 $ip['bitmask']=$k; 176 break; 177 } 178 } 179 180 $overlap = false; 181 // check for overlapping 182 if (isset($subnet)) { 183 if ($subnet->isFolder!="1") { 184 // check 185 if ( $Subnets->is_subnet_inside_subnet ("$ip[subnet]/$ip[bitmask]", $Subnets->transform_address($subnet->subnet,"dotted")."/".$subnet->mask) === false ) { 186 $overlap = true; 187 } 188 // same mask 189 if ($ip['subnet']==$Subnets->transform_address($subnet->subnet,"dotted") && $ip['bitmask']==$subnet->mask) { 190 $overlap = true; 191 } 192 } 193 } 194 195 // check overlapping 196 if ($overlap === false) { 197 $dc++; 198 print "<tr id='tr-$m'>"; 199 //ip 200 print "<td>$ip[subnet]/$ip[bitmask]</td>"; 201 202 //section, description, hidden 203 print "<td>"; 204 print " <input type='text' name='description-$m'>"; 205 print " <input type='hidden' name='subnet-$m' value='$ip[subnet]/$ip[bitmask]'>"; 206 print " <input type='hidden' name='subnet_dec-$m' value='".$Subnets->transform_address($ip['subnet'],"decimal")."'>"; 207 print " <input type='hidden' name='mask-$m' value='$ip[bitmask]'>"; 208 print " <input type='hidden' name='sectionId-$m' value='$_POST[sectionId]'>"; 209 print " <input type='hidden' name='action-$m' value='add'>"; 210 print " <input type='hidden' name='device-$m' value='$deviceid'>"; 211 if(isset($_POST['subnetId'])) 212 print " <input type='hidden' name='masterSubnetId-$m' value='$_POST[subnetId]'>"; 213 else 214 print " <input type='hidden' name='masterSubnetId-$m' value='0'>"; 215 print "</td>"; 216 217 //vlan 218 print "<td>"; 219 print "<select name='vlanId-$m' class='form-control input-sm input-w-100'>"; 220 print " <option disabled='disabled'>"._('Select VLAN')."</option>"; 221 print " <option value='0'>". _('No VLAN')."</option>"; 222 # print all available domains 223 foreach($permitted_domains as $d) { 224 //more than default 225 print "<optgroup label='".$d['domain']->name."'>"; 226 if($d['vlans'][0]!==null) { 227 foreach($d['vlans'] as $v) { 228 // set print 229 $printVLAN = $v->number; 230 if(!empty($v->name)) { $printVLAN .= " ($v->name)"; } 231 print '<option value="'. $v->vlanId .'">'. $printVLAN .'</option>'. "\n"; 232 } 233 } 234 else { 235 print "<option value='0' disabled>"._('No VLANs')."</option>"; 236 } 237 print "</optgroup>"; 238 } 239 print "</select>"; 240 print "</td>"; 241 242 //vrf 243 print ' <td>' . "\n"; 244 print ' <select name="vrfId-'.$m.'" class="form-control input-sm input-w-100">'. "\n"; 245 //blank 246 print '<option disabled="disabled">'._('Select VRF').'</option>'; 247 print '<option value="0">'._('None').'</option>'; 248 if($vrfs!=false) { 249 foreach($vrfs as $vrf) { 250 // set permitted 251 $permitted_sections = explode(";", $vrf->sections); 252 // section must be in array 253 if (strlen($vrf->sections)==0 || in_array(@$_POST['sectionId'], $permitted_sections)) { 254 //cast 255 $vrf = (array) $vrf; 256 // set description if present 257 $vrf['description'] = strlen($vrf['description'])>0 ? " ($vrf[description])" : ""; 258 print '<option value="'. $vrf['vrfId'] .'">'.$vrf['name'].$vrf['description'].'</option>'; 259 } 260 } 261 } 262 print ' </select>'. "\n"; 263 print ' </td>' . "\n"; 264 265 //nameserver 266 print "<td>"; 267 print "<select name='nameserverId-$m' class='form-control input-sm input-w-100'>"; 268 print "<optgroup label='"._('Select nameserver set')."'>"; 269 print "<option value='0'>"._('No nameservers')."</option>"; 270 # print all available nameserver sets 271 if ($permitted_nameservers!==false) { 272 foreach($permitted_nameservers as $n) { 273 274 if($n[0]!==null) { 275 foreach($n as $ns) { 276 // set print 277 $printNS = "$ns->name"; 278 $printNS .= " (" . array_shift(explode(";",$ns->namesrv1)).",...)"; 279 print '<option value="'. $ns->id .'">'. $printNS .'</option>'. "\n"; 280 } 281 } 282 } 283 } 284 print "</optgroup>"; 285 print "</select>"; 286 print "</td>"; 287 288 //remove button 289 print "<td><a href='' class='btn btn-xs btn-danger remove-snmp-subnet' data-target-subnet='$m'><i class='fa fa-times'></i></a></td>"; 290 print "</tr>"; 291 292 $m++; 293 } 294 } 295 // none 296 if ($dc==0) { 297 print "<tr><td colspan='$colspan'>".$Result->show ("info", _("No subnets found"), false, false, true)."</td></tr>"; 298 } 299 print "</tbody>"; 300 } 301 } 302 } 303 print "</table>"; 304 print "</form>"; 305 } 306 307 // add button 308 if($m>0) { 309 print "<a class='btn btn-sm btn-success' id='add-subnets-to-section-snmp'><i class='fa fa-plus'></i> "._("Add subnets to section")."</a>"; 310 } 311 312 // print errors 313 if (isset($errors)) { 314 print "<hr>"; 315 foreach ($errors as $e) { 316 print $Result->show ("warning", $e, false, false, true); 317 } 318 } 319 320 //print scan method 321 print "<div class='text-right' style='margin-top:7px;'>"; 322 print " <span class='muted'>"; 323 print " Scan method: SNMP Route table<hr>"; 324 print " Scanned devices: <br>"; 325 foreach ($debug as $k=>$d) { 326 print "· ".$k."<br>"; 327 } 328 print "</span>"; 329 print "</div>"; 330 331 # show debug? 332 if($_POST['debug']==1) { print "<pre>"; print_r($debug); print "</pre>"; } 333 334 ?> 335 336 <!-- result --> 337 <div class="add-subnets-to-section-snmp-result"></div> 338 339<?php if ($ajax_loaded) { ?> 340</div> 341 342 343<!-- footer --> 344<div class="pFooter"> 345 <button class="btn btn-sm btn-default hidePopups"><?php print _('Cancel'); ?></button> 346</div> 347<?php } ?>