-
Notifications
You must be signed in to change notification settings - Fork 722
/
Addresses.php
615 lines (532 loc) · 21.3 KB
/
Addresses.php
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* phpIPAM API class to work with Addresses
*
*
*/
class Addresses_controller extends Common_api_functions {
/**
* Saves details of current subnet
*
* @var mixed
* @access private
*/
private $subnet_details;
/**
* Old address values
*
* @var mixed
* @access private
*/
private $old_address;
/**
* __construct function
*
* @access public
* @param class $Database
* @param class $Tools
* @param mixed $params // post/get values
*/
public function __construct($Database, $Tools, $params, $Response) {
$this->Database = $Database;
$this->Tools = $Tools;
$this->_params = $params;
$this->Response = $Response;
// init required objects
$this->init_object ("Subnets", $Database);
$this->init_object ("Addresses", $Database);
// set valid keys
$this->set_valid_keys ("ipaddresses");
}
/**
* Returns json encoded options
*
* @access public
* @return void
*/
public function OPTIONS () {
// validate
$this->validate_options_request ();
// methods
$result = array();
$result['methods'] = array(
array("href"=>"/api/".$this->_params->app_id."/addresses/", "methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", "method"=>"GET"),
array("rel"=>"create", "method"=>"POST"),
array("rel"=>"update", "method"=>"PATCH"),
array("rel"=>"delete", "method"=>"DELETE"))),
);
# result
return array("code"=>200, "data"=>$result);
}
/**
* Read address functions
*
* identifiers can be:
* - / // returns all addresses in all sections
* - /addresses/{id}/
* - /addresses/{id}/ping/ // pings address
* - /addresses/{ip}/{subnetId}/ // Returns address from subnet
* - /addresses/search/{ip_address}/ // searches for addresses in database, returns multiple if found
* - /addresses/search_hostname/{hostname}/ // searches for addresses in database by hostname, returns multiple if found
* - /addresses/search_linked/{value}/ // searches in database for addresses linked by customer defined "Link addresses" field, returns multiple if found
* - /addresses/search_hostbase/{hostbase}/ // searches for addresses by leading substring (base) of hostname, returns ordered multiple
* - /addresses/search_mac/{mac}/ // searches for addresses by mac, returns ordered multiple
* - /addresses/first_free/{subnetId}/ // returns first available address (subnetId can be provided with parameters)
* - /addresses/custom_fields/ // custom fields
* - /addresses/tags/ // all tags
* - /addresses/tags/{id}/ // specific tag
* - /addresses/tags/{id}/addresses/ // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet
* - /all/ // returns all addresses in all sections
*
* @access public
* @return void
*/
public function GET () {
// all
if (!isset($this->_params->id) || $this->_params->id == "all") {
// fetch all
$result = $this->Addresses->fetch_all_objects ("ipaddresses");
// check result
if ($result===false) { $this->Response->throw_exception(500, "Unable to read addresses"); }
else { return array("code"=>200, "data"=>$this->prepare_result($result, "addresses", true, true)); }
}
// subnet Id > read all addresses in subnet
elseif($this->_params->id=="custom_fields") {
// check result
if(sizeof($this->custom_fields)==0) { $this->Response->throw_exception(404, 'No custom fields defined'); }
else { return array("code"=>200, "data"=>$this->custom_fields); }
}
// first free
elseif($this->_params->id=="first_free") {
// check for isFull
if(isset($this->_params->subnetId)) {
$subnet = $this->Tools->fetch_object ("subnets", "id", $this->_params->subnetId);
} else {
$subnet = $this->Tools->fetch_object ("subnets", "id", $this->_params->id2);
}
if($subnet->isFull==1) { $this->Response->throw_exception(404, 'No free addresses found'); }
$this->_params->ip_addr = $this->Addresses->get_first_available_address ($subnet->id);
// null
if ($this->_params->ip_addr==false) { $this->Response->throw_exception(404, 'No free addresses found'); }
else { return array("code"=>200, "data"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted")); }
}
// address search inside predefined subnet
elseif($this->Tools->validate_ip ($this->_params->id)!==false && isset($this->_params->id2)) {
// fetch all in subnet
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "subnetId", $this->_params->id2);
if($result!==false) {
$result_filtered = "";
foreach ($result as $k=>$r) {
if($r->ip !== $this->_params->id) {
unset($result[$k]);
}
else {
$result_filtered = $r;
}
}
if(sizeof($result)==0) { $result = false; }
else { $result = $result_filtered; }
}
if ($result==false) { $this->Response->throw_exception(404, 'No addresses found'); }
else { return array("code"=>200, "data"=>$result); }
}
// tags
elseif($this->_params->id=="tags") {
// all addresses with tag
if (@$this->_params->id3=="addresses") {
// validate
$this->validate_tag ();
// fetch
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2);
// filter by subnetId
if ($result!==false) {
if(isset($this->_params->subnetId)) {
if (is_numeric($this->_params->subnetId)) {
// filter
foreach ($result as $k=>$v) {
if ($v->subnetId != $this->_params->subnetId) {
unset($result[$k]);
}
}
// any left
if (sizeof($result)==0) {
$result = false;
}
}
}
}
// result
if($result===false) { $this->Response->throw_exception(404, 'No addresses found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
}
// tags
else {
// fetch all by tag
if(isset($this->_params->id2)) {
// validate
$this->validate_tag ();
// numeric
if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); }
// type
else { $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); }
}
// all tags
else { $result = $this->Tools->fetch_all_objects ("ipTags"); }
// result
if($result===false) { $this->Response->throw_exception(404, 'Tag not found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); }
}
}
// Search all addresses matching custom link_field field's value
elseif($this->_params->id=="search_linked") {
//
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", $this->Addresses->Log->settings->link_field, $this->_params->id2);
// result
if($result===false) { $this->Response->throw_exception(404, 'No addresses found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
}
//
// id not set
elseif (!isset($this->_params->id)) {
{ $this->Response->throw_exception(400, 'Address ID is required'); }
}
// id
elseif (is_numeric($this->_params->id)) {
// ping
if(@$this->_params->id2=="ping") {
# scan class
$Scan = new Scan ($this->Database);
$Scan->ping_set_exit (false);
// check address
$this->validate_address_id ();
// set result
$result = array();
$result['scan_type'] = $Scan->icmp_type;
$result['exit_code'] = $Scan->ping_address ($this->old_address->ip);
$result['result_code'] = $Scan->ping_exit_explain ($result['exit_code']);
$result['message'] = $result['exit_code']==0 ? "Address online" : "Address offline";
// success
if($result['exit_code']==0) { $Scan->ping_update_lastseen ($this->_params->id); }
return array("code"=>200, "data"=>$result);
}
else {
// fetch
$result = $this->Addresses->fetch_address ("id", $this->_params->id);
// check result
if($result==false) { $this->Response->throw_exception(404, "Invalid Id"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
}
}
// ip address ?
elseif (@$this->_params->id=="search") {
// validate
if(!$this->Addresses->validate_address ($this->_params->id2))
{ $this->Response->throw_exception(400, 'Invalid address'); }
// search
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal"));
// check result
if($result===false) { $this->Response->throw_exception(404, 'Address not found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
}
// search host ?
elseif (@$this->_params->id=="search_hostname") {
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "hostname", $this->_params->id2);
// check result
if($result===false) { $this->Response->throw_exception(404, 'Hostname not found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, false, false));}
}
// search host base (initial substring), return sorted by name
elseif (@$this->_params->id=="search_hostbase") {
$target = $this->_params->id2."%";
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "hostname", $target, "hostname", true, true);
// check result
if($result===false) { $this->Response->throw_exception(404, 'Host name not found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, false, false));}
}
elseif (@$this->_params->id=="search_mac") {
$this->_params->id2 = $this->reformat_mac_address ($this->_params->id2, 1);
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "mac", $this->_params->id2, "mac");
// check result
if($result===false) { $this->Response->throw_exception(404, 'Host name not found'); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, false, false));}
// false
} else { $this->Response->throw_exception(400, "Invalid Id"); }
}
/**
* Creates new address
*
* /addresses/ // create ip_addr in subnet (required parameters: ip, subnetId)
* /addresses/first_free/{subnetId}/ // will search for first free address in subnet, creating ip_addr
*
* @access public
* @return void
*/
public function POST () {
// remap keys
$this->remap_keys ();
// first free
if($this->_params->id=="first_free") {
// check for isFull
if(isset($this->_params->subnetId)) {
$subnet = $this->Tools->fetch_object ("subnets", "id", $this->_params->subnetId);
} else {
$subnet = $this->Tools->fetch_object ("subnets", "id", $this->_params->id2);
unset($this->_params->id2);
}
if($subnet===false) { $this->Response->throw_exception(400, "Invalid subnet identifier"); }
if($subnet->isFull==1) { $this->Response->throw_exception(404, "No free addresses found (subnet is full)"); }
// Obtain exclusive MySQL lock so parallel API requests on the same object are thread safe.
$Lock = new LockForUpdate($this->Database, 'subnets', $subnet->id);
$this->_params->ip_addr = $this->Addresses->get_first_available_address ($subnet->id);
// null
if ($this->_params->ip_addr==false) { $this->Response->throw_exception(404, 'No free addresses found'); }
else {
$this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted");
$this->_params->subnetId = $subnet->id;
if(!isset($this->_params->description)) $this->_params->description = "API created";
}
}
// validate ip address - format, proper subnet, subnet/broadcast check
$this->validate_create_parameters ();
// check for valid keys
$values = $this->validate_keys ();
// transform address to decimal format
$values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal");
// set action
$values['action'] = "add";
# execute
if(!$this->Addresses->modify_address ($values)) {
$this->Response->throw_exception(500, "Failed to create address");
}
else {
//set result
if($this->_params->id=="first_free") {
return array("code"=>201, "message"=>"Address created", "id"=>$this->Addresses->lastId, "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "data"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
}
else {
return array("code"=>201, "message"=>"Address created", "id"=>$this->Addresses->lastId, "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/");
}
}
}
/**
* Updates existing address
*
* forbidden parameters: ip, subnetId
*
* @access public
* @return void
*/
public function PATCH () {
// remap keys
$this->remap_keys ();
// we dont allow address or subnet change
if(isset($this->_params->ip)) { $this->Response->throw_exception(400, "IP address cannot be changed"); }
if(isset($this->_params->ip_addr)) { $this->Response->throw_exception(400, "IP address cannot be changed"); }
if(isset($this->_params->subnetId)) { $this->Response->throw_exception(400, "Subnet cannot be changed"); }
// validations
$this->validate_update_parameters ();
# check for valid keys
$values = $this->validate_keys ();
// add action and id
$values["id"] = $this->_params->id;
# we need admin and addresses object
$this->init_object ("Admin", $this->Database);
$this->init_object ("Addresses", $this->Database);
# append old address details and fill details if not provided - calidate_update_parameters fetches $this->old_address
foreach ($this->old_address as $ok=>$oa) {
if (!array_key_exists($ok, $values)) {
if(!is_null($oa)) {
$values[$ok] = $oa;
}
}
}
# append action
$values["action"] = "edit";
# execute
if(!$this->Addresses->modify_address ($values)) {
$this->Response->throw_exception(500, "Failed to update address");
}
else {
//set result
return array("code"=>200, "message"=>"Address updated");
}
}
/**
* Deletes existing address
*
* required parameters: id
*
* identifiers can be:
*
* /addresses/{id} // Returns address by id
* /addresses/{ip}/{subnetId}/ // Deletes address from subnet by ip
*
* @access public
* @return void
*/
public function DELETE () {
// delete by ip
if ($this->Tools->validate_ip ($this->_params->id)!==false && isset($this->_params->id2)) {
// find
$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Tools->transform_address($this->_params->id, "decimal"));
if($result!==false) {
foreach ($result as $k=>$r) {
if($r->subnetId !== $this->_params->id2) {
unset($result[$k]);
}
}
}
if (sizeof($result)==0 || $result===false) { $this->Response->throw_exception(404, "No addresses found"); }
else {
// rekey
$result = array_values($result);
// replace parameters
$this->_params->id = $result[0]->id;
}
}
// Check for id
$this->validate_address_id ();
// set variables for delete
$values = array();
$values["id"] = $this->_params->id;
$values["action"] = "delete";
// delete pdns records ?
if(isset($this->_params->remove_dns)) {
$values['remove_all_dns_records'] = 1;
$values['hostname'] = $this->old_address->hostname;
$values['ip_addr'] = $this->Tools->transform_address($this->old_address->ip, "dotted");
$values['PTR'] = $this->old_address->PTR;
$values['subnetId'] = $this->old_address->subnetId;
}
# execute update
if(!$this->Addresses->modify_address ($values))
{ $this->Response->throw_exception(500, "Failed to delete address"); }
else {
//set result
return array("code"=>200, "message"=>"Address deleted");
}
}
/* @validations ---------- */
/**
* Make sure the address exists in database.
*
* @access private
* @return void
*/
private function validate_address_id () {
// fetch
$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id);
// check
if($this->old_address===false) { $this->Response->throw_exception(404, "Address does not exist"); }
}
/**
* Validate IP tag
*
* @access private
* @return void
*/
private function validate_tag () {
// numeric
if(!is_numeric(@$this->_params->id2)) { $this->Response->throw_exception(400, 'Invalid tag identifier'); }
// check db
if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2)) { $this->Response->throw_exception(404, "Address tag does not exist"); }
}
/**
* Validates subnet
*
* @access private
* @return void
*/
private function validate_subnetId () {
if(!is_numeric($this->_params->subnetId))
$this->Response->throw_exception(400, _("SubnetId must be numeric"));
// check subnet exists
$res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId);
if(!is_object($res))
$this->Response->throw_exception(404, _("Invalid subnet Id"));
$this->subnet_details = $res;
}
/**
* This method will be used if subnetId is not present and will try to
* find it in system itself.
*
* It will only take into consideration subnets that do not have underlying
* slave subnets.
*
* In case more than 1 subnet is found error will be thrown.
*
* @method autosearch_subnet_id
*
* @return void
*/
private function autosearch_subnet_id () {
}
/**
* Validates address on creation
*
* @access public
* @return void
*/
public function validate_create_parameters () {
// validate subnet
$this->validate_subnetId ();
// validate overlapping
if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId))
$this->Response->throw_exception(409, "IP address already exists");
// check if it is a folder
if($this->subnet_details->isFolder) {
if($this->Addresses->validate_address ($this->_params->ip_addr)===false)
$this->Response->throw_exception(400, "Invalid address");
}
$this->validate_create_update_common();
}
/**
* Validation of PATCH parameters
*
* @access private
* @return void
*/
private function validate_update_parameters () {
// make sure address exists
$this->validate_address_id ();
// if no data is present print it
if(sizeof((array) $this->_params)==3) {
if(isset($this->_params->app_id) && isset($this->_params->controller) && isset($this->_params->id))
$this->Response->throw_exception(400, "No data provided");
}
$this->validate_create_update_common();
}
/**
* Validation of POST/PATCH parameters - common checks
*
* @access private
* @return void
*/
private function validate_create_update_common () {
//validate and normalize MAC address
if(strlen($this->_params->mac)>0) {
if($this->validate_mac ($this->_params->mac)===false)
$this->Response->throw_exception(400, "Invalid MAC address");
// normalize
$this->_params->mac = $this->reformat_mac_address ($this->_params->mac, 1);
}
// validate device
if(isset($this->_params->switch)) {
if (!empty($this->_params->switch) && !is_numeric($this->_params->switch)) {
$this->Response->throw_exception(400, "Device does not exist");
}
if($this->_params->switch > 0 && $this->Tools->fetch_object("devices", "id", $this->_params->switch)===false)
$this->Response->throw_exception(400, "Device does not exist");
}
// validate state
if(isset($this->_params->state)) {
if (!empty($this->_params->state) && !is_numeric($this->_params->state))
$this->Response->throw_exception(400, "Invalid state");
if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)
$this->Response->throw_exception(400, "Tag does not exist");
} else {
$this->_params->state = 2;
}
}
}