-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsServer.cpp
More file actions
264 lines (211 loc) · 5.49 KB
/
Copy pathDnsServer.cpp
File metadata and controls
264 lines (211 loc) · 5.49 KB
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
#include "DnsServer.h"
#include "CryptoHelper.h"
#include "Config.h"
#include "SysInfo.h"
#include "SysConfig.h"
#include "NetworkConfig.h"
#include <libutils/Logger.h>
#include <libutils/FileUtils.h>
#include <libutils/HttpStatusCodes.h>
#include <iterator>
#include <algorithm>
using namespace Utils;
namespace OPI
{
using namespace CryptoHelper;
using namespace Utils::HTTP;
DnsServer::DnsServer(const string &host): HttpClient(host)
{
}
tuple<int, json> DnsServer::CheckOPIName(const string &opiname)
{
map<string,string> postargs = {
{"fqdn", opiname},
{"checkname", "1"}
};
string body = this->DoPost("update_dns.php", postargs);
json retobj;
try
{
retobj = json::parse(body);
}
catch (json::parse_error& err)
{
logg << Logger::Error << "Failed to parse response: " << err.what() << lend;
}
return tuple<int,json>(this->result_code, retobj );
}
bool DnsServer::RegisterPublicKey(const string &unit_id, const string &key, const string &token)
{
bool parseok = true;
logg << Logger::Debug << "Register dns public key "<< lend;
map<string,string> postargs = {
{"unit_id", unit_id},
{"dns_key", key}
};
map<string,string> headers = {
{"token", token}
};
this->CurlSetHeaders(headers);
string body = this->DoPost("dns_addkey.php", postargs);
json retobj;
try
{
retobj = json::parse(body);
}
catch (json::parse_error& err)
{
logg << Logger::Error << "Failed to parse response: " << err.what() << lend;
logg << Logger::Debug << "Reply was: " << body << lend;
parseok = false;
}
return this->result_code == Status::Ok && parseok;
}
bool DnsServer::UpdateDynDNS(const string &unit_id, const string &name)
{
map<string,string> postargs;
if ( unit_id.length() ) {
// use normal login method
if( ! this->Auth( unit_id ) )
{
return false;
}
logg << Logger::Debug << "Update DNS pointer"<< lend;
postargs = {
{"unit_id", unit_id},
{"fqdn", name},
{"local_ip", NetUtils::GetAddress(sysinfo.NetworkDevice())}
};
map<string,string> headers = {
{"token", this->token}
};
this->CurlSetHeaders(headers);
}
else
{
// update OP DNS servers based on serial number
logg << Logger::Debug << "Update DNS based on device serial number"<< lend;
string domain;
SysConfig sysconfig;
if ( sysconfig.HasKey("hostinfo","domain") )
{
domain=sysconfig.GetKeyAsString("hostinfo","domain");
}
else
{
domain=sysinfo.Domains[sysinfo.Type()];
}
postargs = {
{"fqdn", sysinfo.SerialNumber() + "." + domain},
{"local_ip", NetUtils::GetAddress(sysinfo.NetworkDevice())}
};
}
string body = this->DoPost("update_dns.php", postargs);
bool parseok = true;
json retobj;
try
{
retobj = json::parse(body);
}
catch (json::parse_error& err)
{
logg << Logger::Error << "Failed to parse response: " << err.what() << lend;
parseok = false;
}
return this->result_code == Status::Ok && parseok;
}
DnsServer::~DnsServer()
{
}
bool DnsServer::Auth(const string &unit_id)
{
try
{
string challenge;
int resultcode = 0;
tie(resultcode,challenge) = this->GetChallenge( unit_id);
if( resultcode != Status::Ok )
{
logg << Logger::Error << "Unknown reply of server "<<resultcode<< lend;
return false;
}
RSAWrapper dnskeys;
list<string> rows = File::GetContent( SysConfig().GetKeyAsString("dns", "dnsauthkey") );
stringstream pemkey;
for( const auto& row: rows)
{
pemkey << row << "\n";
}
dnskeys.LoadPrivKeyFromPEM( pemkey.str() );
string signedchallenge = Base64Encode( dnskeys.SignMessage( challenge ) );
json rep;
tie(resultcode, rep) = this->SendSignedChallenge( unit_id, signedchallenge );
if( resultcode != Status::Ok && resultcode != Status::Forbidden )
{
logg << Logger::Error << "Unexpected reply from server "<< resultcode <<lend;
return false;
}
if( resultcode == Status::Forbidden )
{
logg << Logger::Debug << "Failed to auth with dns"<<lend;
logg << "Reply:"<< rep.dump()<<lend;
return false;
}
if( rep.contains("token") && rep["token"].is_string() )
{
this->token = rep["token"];
}
else
{
logg << Logger::Error << "Missing argument in reply"<<lend;
return false;
}
}
catch(std::exception& err)
{
logg << Logger::Error << "Failed to dns authenticate "<<err.what()<<lend;
return false;
}
return true;
}
tuple<int, string> DnsServer::GetChallenge(const string &unit_id)
{
string ret = "";
map<string,string> arg = {{ "unit_id", unit_id }};
string s_res = this->DoGet("auth.php", arg);
json res;
try
{
res = json::parse(s_res);
}
catch (json::parse_error& err)
{
logg << Logger::Error << "Failed to parse response: " << err.what() << lend;
}
if( res.contains("challange") && res["challange"].is_string() )
{
ret = res["challange"];
}
return tuple<int,string>(this->result_code,ret);
}
tuple<int, json> DnsServer::SendSignedChallenge(const string &unit_id, const string &challenge)
{
json data;
data["unit_id"] = unit_id;
data["dns_signature"] = challenge;
map<string,string> postargs = {
{"data", data.dump() }
};
string body = this->DoPost("auth.php", postargs);
json retobj;
try
{
retobj = json::parse(body);
}
catch (json::parse_error& err)
{
logg << Logger::Error << "Failed to parse response: " << err.what() << lend;
}
return tuple<int,json>(this->result_code, retobj );
}
} // End NS