Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. <?php
  2.  
  3. namespace service {
  4.  
  5. class IPAMServiceException extends Exception {
  6.  
  7.  
  8. };
  9.  
  10. }
  11.  
  12. require_once BASEPATH . "/includes/BigInteger.php";
  13. require_once BASEPATH . "/includes/IPAMUtil.php";
  14.  
  15.  
  16. namespace service {
  17.  
  18. class IPAMService {
  19.  
  20. public static $message = null;
  21. private static $config = array();
  22. private static $db = null;
  23. const AUTOSPLIT_MAX = 2048;
  24.  
  25. public function __construct() {
  26. global $auth;
  27. if (isset($auth->config)) {
  28. $this->config = $auth->config;
  29. }
  30. $this->db = DB::get_instance();
  31. }
  32.  
  33. public function addAggregate($params, $allowDuplicate = false) {
  34. $params = Util::parse_args($params, array("rir" => "", "cidr" => "", "value" => "", "tags" => "", "region" => "", "notes" => "", "vlan" => "", "asn" => "", "code" => "", "allowSubAssignments" => ""));
  35. if (!IPAMUtil::isValidCIDR($params["cidr"])) {
  36. throw new IPAMServiceException("Invalid CIDR: " . $params["cidr"]);
  37. }
  38. $allowedRIRsList = $this->getRIRList();
  39. $allowedTagsList = $this->getTagList();
  40. $allowedRegionsList = (array());
  41. $temp = $this->getRegionList();
  42. foreach ($this->getRegionList() as $region) {
  43. $allowedRegionsList[] = $region["value"];
  44. continue;
  45. }
  46. if (!in_array($params["rir"], $allowedRIRsList)) {
  47. throw new IPAMServiceException("Invalid RIR: \"" . $params["rir"] . "\"");
  48. }
  49. if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
  50. throw new IPAMServiceException("Invalid region: " . $params["region"]);
  51. }
  52. if (!empty($params["tags"])) {
  53. $tagsList = preg_split("/,/", $params["tags"]);
  54. foreach ($tagsList as $tagValue) {
  55. if (!empty($tagValue)) {
  56. if (!in_array($tagValue, $allowedTagsList)) {
  57. throw new IPAMServiceException("Invalid tag: " . $tagValue);
  58. }
  59. }
  60. continue;
  61. }
  62. }
  63. $mask = preg_replace("/.*\\/(\\d+)\$/", "\$1", $params["cidr"]);
  64. if (!is_numeric($mask)) {
  65. throw new IPAMServiceException("Invalid mask: " . $mask);
  66. }
  67. if ($allowDuplicate !== true) {
  68. $list = $this->getBlocks(array("cidr" => $params["cidr"]));
  69. if (0 < count($list)) {
  70. throw new IPAMServiceException("Aggregate " . $params["cidr"] . " already exists");
  71. }
  72. }
  73. $type = strpos($params["cidr"], ":") === false ? "ipv4" : "ipv6";
  74. list($startAddress, $endAddress) = IPAMUtil::getRangeFromCidr($params["cidr"]);
  75. if (empty($id)) {
  76. $ipObject = new IP();
  77. }
  78. else {
  79. $ipObject = IP::getBlockById($id);
  80. if ($ipObject === null) {
  81. throw new IPAMServiceException("Unable to find block with ID " . $id . " to update");
  82. }
  83. }
  84. $ipObject->setType($type);
  85. $ipObject->setCidr($params["cidr"]);
  86. $ipObject->setMask($mask);
  87. $ipObject->setASN($params["asn"]);
  88. $ipObject->setRegion($params["region"]);
  89. $ipObject->setBlockCode($params["code"]);
  90. $ipObject->setNotes($params["notes"]);
  91. $ipObject->setRIR($params["rir"]);
  92. $ipObject->setVlan($params["vlan"]);
  93. $ipObject->setAddress($startAddress);
  94. $ipObject->setEndAddress($endAddress);
  95. $ipObject->setSwipped(0);
  96. $ipObject->setAssigned(0);
  97. $ipObject->setAllowSubAssignments($params["allowSubAssignments"]);
  98. $ipObject->setResourceId($this->config->available_id);
  99. $ipObject->update();
  100. $this->message = "Aggregate added: " . $ipObject->getCidr();
  101. $this->log(INFO, IPAM, $this->message, $ipObject);
  102. if (!empty($params["tags"])) {
  103. $tagsList = preg_split("/,/", $params["tags"]);
  104. foreach ($tagsList as $tagValue) {
  105. if (!empty($tagValue)) {
  106. $tag = new IPTag();
  107. $tag->setIpId($ipObject->getId());
  108. $tag->setTag($tagValue);
  109. $tag->update();
  110. }
  111. continue;
  112. }
  113. }
  114. return $ipObject;
  115. }
  116.  
  117. public function deleteAggregate($ipObject) {
  118. if ($ipObject->getParent() !== null) {
  119. throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it is the child of another block: " . $parentIpObject->getCidr() . "(" . $parentIpObject->getId() . ")");
  120. }
  121. if ($ipObject->getFirstChild() !== null) {
  122. throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it has children");
  123. }
  124. if ($ipObject->getAssigned() !== false) {
  125. throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it is assigned to " . $ipObject->getResourceHolderId());
  126. }
  127. $this->message = "Aggregate " . $ipObject->getCidr() . " (" . $ipObject->getId() . ") deleted";
  128. $this->log(INFO, IPAM, $this->message, $ipObject);
  129. $ipObject->delete();
  130. }
  131.  
  132. public function updateBlock($ipObject, $params, $updateAttributesMethod = IP::IP_PRESERVE_ATTRIBUTES) {
  133. if ($ipObject == null) {
  134. throw new IPAMServiceException("Cannot update a null block");
  135. }
  136. $allowedRIRsList = $this->getRIRList();
  137. $allowedTagsList = $this->getTagList();
  138. $allowedRegionsList = (array());
  139. $temp = $this->getRegionList();
  140. foreach ($this->getRegionList() as $region) {
  141. $allowedRegionsList[] = $region["value"];
  142. continue;
  143. }
  144. if (isset($params["rir"]) && !in_array($params["rir"], $allowedRIRsList)) {
  145. throw new IPAMServiceException("Invalid RIR: " . $params["rir"]);
  146. }
  147. if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
  148. throw new IPAMServiceException("Invalid region: " . $params["region"]);
  149. }
  150. if (!empty($params["tags"])) {
  151. $tagsList = preg_split("/,/", $params["tags"]);
  152. foreach ($tagsList as $tagValue) {
  153. if (!empty($tagValue)) {
  154. $found = false;
  155. $i = 0;
  156. while ($i < count($allowedTagsList)) {
  157. $i++;
  158. continue;
  159. if (strtolower($allowedTagsList[$i]) == strtolower($tagValue)) {
  160. $found = true;
  161. break;
  162. }
  163. continue;
  164. }
  165. if (!$found) {
  166. throw new IPAMServiceException("Invalid tag: " . $tagValue);
  167. }
  168. }
  169. continue;
  170. }
  171. }
  172. if ($ipObject == null) {
  173. throw new IPAMServiceException("Cannot update a null block");
  174. }
  175. if (isset($params["asn"])) {
  176. $ipObject->setASN($params["asn"]);
  177. }
  178. if (isset($params["code"])) {
  179. $ipObject->setBlockCode($params["code"]);
  180. }
  181. if (isset($params["region"])) {
  182. $ipObject->setRegion($params["region"]);
  183. }
  184. if (isset($params["rir"])) {
  185. $ipObject->setRIR($params["rir"]);
  186. }
  187. if (isset($params["lirId"])) {
  188. $ipObject->setLIRId($params["lirId"]);
  189. }
  190. if (isset($params["notes"])) {
  191. $ipObject->setNotes($params["notes"]);
  192. }
  193. if (isset($params["swipped"])) {
  194. $ipObject->setSwipped($params["assigned"]);
  195. }
  196. if (isset($params["assigned"])) {
  197. $ipObject->setAssigned($params["assigned"]);
  198. }
  199. if (isset($params["resourceId"])) {
  200. $ipObject->setResourceId($params["resourceId"]);
  201. }
  202. if (isset($params["customerHandle"])) {
  203. $ipObject->setCustomerHandle($params["customerHandle"]);
  204. }
  205. if (isset($params["orgId"])) {
  206. $ipObject->setOrgId($params["orgId"]);
  207. }
  208. if (isset($params["netHandle"])) {
  209. $ipObject->setNetHandle($params["netHandle"]);
  210. }
  211. if (isset($params["vlan"])) {
  212. if (empty($params["vlan"])) {
  213. $ipObject->setVlan(null);
  214. }
  215. else {
  216. $ipObject->setVlan($params["vlan"]);
  217. }
  218. }
  219. if (isset($params["allowSubAssignments"])) {
  220. $ipObject->setAllowSubAssignments($params["allowSubAssignments"]);
  221. if ($ipObject->getAllowSubAssignments()) {
  222. $ipObject->setAssigned(false);
  223. }
  224. else {
  225. $parent = $this->getBlockById($ipObject->getParent());
  226. if ($ipObject->getResourceId() != $this->config->available_id && $ipObject->getResourceId() != $this->config->holding_id) {
  227. $ipObject->setAssigned(true);
  228. }
  229. }
  230. }
  231. $ipObject->update($updateAttributesMethod);
  232. if (isset($params["tags"])) {
  233. $ipObject->deleteAllTags();
  234. $tagsList = preg_split("/,/", $params["tags"]);
  235. foreach ($tagsList as $tagValue) {
  236. if (!empty($tagValue)) {
  237. $tag = new IPTag();
  238. $tag->setIPId($ipObject->getId());
  239. $tag->setTag($tagValue);
  240. $tag->update();
  241. }
  242. continue;
  243. }
  244. }
  245. $this->message = $ipObject->getCidr() . " (" . $ipObject->getId() . ") updated";
  246. $this->log(INFO, IPAM, $this->message, $ipObject);
  247. return $ipObject;
  248. }
  249.  
  250. public function updateBlocks(array $list, array $params) {
  251. $allowedRIRsList = $this->getRIRList();
  252. $allowedTagsList = $this->getTagList();
  253. $allowedRegionsList = (array());
  254. $temp = $this->getRegionList();
  255. foreach ($this->getRegionList() as $region) {
  256. $allowedRegionsList[] = $region["value"];
  257. continue;
  258. }
  259. if (isset($params["rir"]) && !in_array($params["rir"], $allowedRIRsList)) {
  260. throw new IPAMServiceException("Invalid RIR: " . $params["rir"]);
  261. }
  262. if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
  263. throw new IPAMServiceException("Invalid region: " . $params["region"]);
  264. }
  265. if (!empty($params["tags"])) {
  266. $tagsList = preg_split("/,/", $params["tags"]);
  267. foreach ($tagsList as $tagValue) {
  268. if (!empty($tagValue)) {
  269. if (!in_array($tagValue, $allowedTagsList)) {
  270. throw new IPAMServiceException("Invalid tag: " . $tagValue);
  271. }
  272. }
  273. continue;
  274. }
  275. }
  276. IP::updateBlocks($list, $params);
  277. }
  278.  
  279. public function assignBlock($ipObject, $resourceId) {
  280. $resource = null;
  281. if ($resourceId != $this->config->available_id && $resourceId != $this->config->holding_id) {
  282. $resource = ofuscated1::load("entry")->get($resourceId);
  283. if ($resource == null) {
  284. throw new IPAMServiceException('' . $resourceId . " is not a valid Resource ID");
  285. }
  286. if ($ipObject->getAssigned()) {
  287. throw new IPAMServiceException($ipObject->getCidr() . " is already assigned to Resource " . $ipObject->getResourceId());
  288. }
  289. $ipObject->setAssigned(true);
  290. $ipObject->setAllowSubAssignments(false);
  291. $ipObject->setAssignTime(strftime("%F %T"));
  292. }
  293. else {
  294. $ipObject->setAssigned(false);
  295. }
  296. $ipObject->setLastUpdateTime(strftime("%F %T"));
  297. $ipObject->setResourceId($resourceId);
  298. $ipObject->update();
  299. if ($resourceId != $this->config->available_id && $resourceId != $this->config->holding_id) {
  300. $this->message = "Assigned " . $ipObject->getCidr() . " to " . $resource->name() . " (" . $resourceId . ")";
  301. $this->log(INFO, IPAM, $this->message, $ipObject);
  302. }
  303. }
  304.  
  305. public function autoSplitBlock($ipObject, $mask) {
  306. $maxAutoSplit = $this->getAutoSplitMax();
  307. if (bccomp($maxAutoSplit, bcpow(2, $mask - $ipObject->getMask())) < 0) {
  308. throw new IPAMServiceException("Cannot create more than " . $maxAutoSplit . " blocks with a single operation");
  309. }
  310. if ($ipObject->getMask() == $mask) {
  311. return;
  312. }
  313. list($ipChild1, $ipChild2) = $this->splitBlock($ipObject);
  314. if ($ipChild1->getMask() < $mask) {
  315. $this->autoSplitBlock($ipChild1, $mask);
  316. $this->autoSplitBlock($ipChild2, $mask);
  317. }
  318. }
  319.  
  320. /**
  321. * Splits a block in to a specified number of blocks with the specified mask
  322. *
  323. * Will split a block into the first first N blocks of the specified mask.
  324. *
  325. * Example: split an IPv6 /32 into its first 256 /48 blocks
  326. *
  327. * @param IP object $ipObject an instance of IP to split
  328. * @param int $mask the the mask to split the original block into
  329. * @param int $limit the number of /$mask blocks to split out of the original block
  330. * NOTE: $limit must be a power of 2
  331. */
  332. public function autoSplitBlockWithLimit($ipObject, $targetMask, $limit) {
  333. $maxMask = $ipObject->getType() == "ipv4" ? 32 : 128;
  334. if ($maxMask < $targetMask) {
  335. throw new IPAMServiceException("Invalid mask: " . $targetMask);
  336. }
  337. if ($targetMask <= $ipObject->getMask()) {
  338. throw new IPAMServiceException("Invalid mask: " . $targetMask);
  339. }
  340. $maxAutoSplit = $this->getAutoSplitMax();
  341. if ($maxAutoSplit < $limit && bccomp($maxAutoSplit, bcpow(2, $targetMask - $ipObject->getMask())) < 0) {
  342. throw new IPAMServiceException("Cannot create more than " . $maxAutoSplit . " blocks with a single operation");
  343. }
  344. if (!is_numeric($limit) || $limit < 1) {
  345. throw new IPAMServiceException("Invalid limit: " . $limit);
  346. }
  347. if (($limit & $limit - 1) != 0) {
  348. throw new IPAMServiceException("Invalid limit: " . $limit . ". Limit must be a power of 2");
  349. }
  350. $splitCount = 0;
  351. while (pow(2, $splitCount) < $limit) {
  352. $splitCount++;
  353. ......................................................................
  354. ...........................
  355. ............
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement