This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
/
HostentryController.php
215 lines (192 loc) · 7.13 KB
/
HostentryController.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
<?php
namespace OPNsense\Unbound\Api;
use \OPNsense\Base\ApiMutableModelControllerBase;
use OPNsense\Unbound\common\HostEntry;
use OPNsense\Unbound\common\Unbound;
/**
* Class CcdController
* @method array getNodes
* @method setNodes
* @property \Phalcon\Http\Request request
* @package OPNsense\Unbound\Api
*/
class HostentryController extends ApiMutableModelControllerBase
{
// keep those 2 ladies here, even though that model does not exist / we do not need it yet
// otherwise the controller cannot get instantiated
static protected $internalModelName = 'Ccd';
static protected $internalModelClass = '\OPNsense\Unbound\Hostentry';
/**
* Endpoint : POST api/unbound/hostEntry/setHostEntry
* Payload must look like this
* {
* "hostentry": { "host":"newtest", "domain":"mydomain.tld","ip":"10.10.10.1".. }
* }
*
*/
public function setHostEntryAction()
{
if ($this->request->isPost() && $this->request->hasPost("hostentry")) {
$dts = $this->request->getPost("hostentry");
if (!HostEntry::validateDTS($dts)) {
http_response_code(400);
$this->returnError("Not all mandatory fields set, you need to set host, domain and ip at least");
}
$hostEntry = HostEntry::loadFromDTS($dts);
if (Unbound::existsHostEntryInConfig($hostEntry->host, $hostEntry->domain)) {
if (Unbound::updateHostEntryInConfig($hostEntry, true)) {
$this->returnData(["fqdn" => "{$hostEntry->host}.{$hostEntry->domain}","op" => "update"]);
}
} else {
if (Unbound::createHostEntryInConfig($hostEntry, true)) {
$this->returnData(["fqdn" => "{$hostEntry->host}.{$hostEntry->domain}","op" => "created"]);
}
}
}
http_response_code(500);
$this->returnError("only POST is allowed");
}
/**
* Endpoint : GET api/unbound/hostEntry/getHostEntry
* You can either query this API using
* - no arguments ( all )
* - GET arguments host and domain ( both )
* - alternativly use as a path parameter <host>|<domain.tld> .. so api/unbound/hostEntry/getHostEntry/<host>|<domain.tld>
*
* This weired | splitted string is needed since only on parameter is supported on the path
* @param string|null $hostDomain a string seperating the domain you used from the domain using a | .. so <host>|<domain.tld>
*/
public function getHostEntryAction($hostDomain = null)
{
$host = $domain = null;
if($hostDomain == null) {
if (isset($_GET['host'])) {
$host = $_GET['host'];
}
if (isset($_GET['domain'])) {
$domain = $_GET['domain'];
}
}
else {
// with 19.1 those are url encoded now
$hostDomain = urldecode($hostDomain);
$parts = explode('|',$hostDomain);
$host = $parts[0];
$domain = $parts[1];
}
if($host == null && $domain == null) {
$this->returnData(Unbound::getLegacyHostEntries());
}
if($host == null && $domain != null || $host != null && $domain == null) {
http_response_code(400);
$this->returnError("you need to set host and domain as url arguments, not just one, e.g: api/unbound/hostentry/getHostEntry/myhostname/mydomain.tld");
}
$match = Unbound::getHostEntryByFQDN($host, $domain);
if($match == NULL) {
http_response_code(404);
$this->returnError("not found");
}
// else
$this->returnData($match->toDts());
}
/**
* Endpoint : GET api/unbound/hostEntry/getHostEntryByIp
* - first parameter must be an ip api/unbound/hostEntry/getHostEntryByIp/<ip>
*
* same as getHostEntryAction, just by ip
* @param string $ip
*/
public function getHostEntryByIpAction($ip)
{
if($ip == null) {
$this->returnData(Unbound::getLegacyHostEntries());
}
$match = Unbound::getHostEntryByIp($ip);
if($match == NULL) {
http_response_code(404);
$this->returnError("not found");
}
// else
$this->returnData($match->toDts());
}
/**
* Endpoint : POST api/unbound/hostEntry/delHostEntry
*
* Payload must look like this
* {
* "hostentry": { "host":"newtest", "domain":"mydomain.tld"}
* }
*
* Deletes the entry matching the host and domain
*
*/
public function delHostEntryAction()
{
if ($this->request->isPost() && $this->request->hasPost("hostentry")) {
$dts = $this->request->getPost("hostentry");
if (!isset($dts['host']) || !isset($dts['domain'])) {
http_response_code(400);
$this->returnError("Please set the host and domain in the entry");
}
$hostEntry = HostEntry::loadFromDTS($dts);
if (Unbound::existsHostEntryInConfig($hostEntry->host, $hostEntry->domain)) {
if (Unbound::deleteHostEntryInConfig($hostEntry, true)) {
$this->returnData(["fqdn" => "{$hostEntry->host}.{$hostEntry->domain}"]);
}
}
else {
http_response_code(404);
$this->returnError("not found");
}
}
http_response_code(500);
$this->returnError("only POST is allowed");
}
/**
* Endpoint : POST api/unbound/hostEntry/delHostEntryByIp
*
*/
public function delHostEntryByIpAction()
{
if ($this->request->isPost() && $this->request->hasPost("hostentry")) {
$dts = $this->request->getPost("hostentry");
if (!isset($dts['ip'])) {
http_response_code(400);
$this->returnError("Please set the host entry IP");
}
$hostEntry = Unbound::getHostEntryByIp($dts['ip']);
if ($hostEntry!= null) {
http_response_code(404);
if (Unbound::deleteHostEntryInConfig($hostEntry, true)) {
$this->returnData(["fqdn" => "{$hostEntry->host}.{$hostEntry->domain}"]);
}
else {
http_response_code(404);
$this->returnError("not found");
}
}
else {
http_response_code(404);
$this->returnError("not found");
}
}
http_response_code(500);
$this->returnError("only POST is allowed");
}
private function returnData($data) {
$response = new \stdClass();
$response->data = $data;
$response->status = 'success';
header('Content-type: application/json');
echo json_encode($response);
exit(0);
}
private function returnError($message) {
$response = new \stdClass();
$response->status = "error";
$response->message = $message;
header('Content-type: application/json');
echo json_encode($response);
exit(0);
}
}