-
Notifications
You must be signed in to change notification settings - Fork 213
/
enterprise.cpp
317 lines (248 loc) · 7.24 KB
/
enterprise.cpp
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
// Copyright (c) 2015-2024 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "enterprise.h"
#include <time.h>
using namespace BinaryNinja::Enterprise;
bool BinaryNinja::Enterprise::IsInitialized()
{
return BNIsEnterpriseServerInitialized();
}
bool BinaryNinja::Enterprise::Initialize()
{
return BNInitializeEnterpriseServer();
}
bool BinaryNinja::Enterprise::AuthenticateWithCredentials(const std::string& username, const std::string& password, bool remember)
{
return BNAuthenticateEnterpriseServerWithCredentials(username.c_str(), password.c_str(), remember);
}
bool BinaryNinja::Enterprise::AuthenticateWithMethod(const std::string& method, bool remember)
{
return BNAuthenticateEnterpriseServerWithMethod(method.c_str(), remember);
}
std::vector<std::pair<std::string, std::string>> BinaryNinja::Enterprise::GetAuthenticationMethods()
{
char** methods;
char** names;
size_t count = BNGetEnterpriseServerAuthenticationMethods(&methods, &names);
std::vector<std::pair<std::string, std::string>> results;
for (size_t i = 0; i < count; i++)
{
results.push_back({methods[i], names[i]});
}
BNFreeStringList(methods, count);
BNFreeStringList(names, count);
return results;
}
bool BinaryNinja::Enterprise::Deauthenticate()
{
return BNDeauthenticateEnterpriseServer();
}
void BinaryNinja::Enterprise::CancelAuthentication()
{
return BNCancelEnterpriseServerAuthentication();
}
bool BinaryNinja::Enterprise::Connect()
{
return BNConnectEnterpriseServer();
}
bool BinaryNinja::Enterprise::UpdateLicense(uint64_t timeout)
{
return BNUpdateEnterpriseServerLicense(timeout);
}
bool BinaryNinja::Enterprise::ReleaseLicense()
{
return BNReleaseEnterpriseServerLicense();
}
bool BinaryNinja::Enterprise::IsConnected()
{
return BNIsEnterpriseServerConnected();
}
bool BinaryNinja::Enterprise::IsAuthenticated()
{
return BNIsEnterpriseServerAuthenticated();
}
std::string BinaryNinja::Enterprise::GetUsername()
{
char* value = BNGetEnterpriseServerUsername();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::Enterprise::GetToken()
{
char* value = BNGetEnterpriseServerToken();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::Enterprise::GetServerName()
{
char* value = BNGetEnterpriseServerName();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::Enterprise::GetServerUrl()
{
char* value = BNGetEnterpriseServerUrl();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::Enterprise::GetServerId()
{
char* value = BNGetEnterpriseServerId();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
uint64_t BinaryNinja::Enterprise::GetServerVersion()
{
return BNGetEnterpriseServerVersion();
}
std::string BinaryNinja::Enterprise::GetServerBuildId()
{
char* value = BNGetEnterpriseServerBuildId();
if (!value)
return "";
std::string result = value;
BNFreeString(value);
return result;
}
uint64_t BinaryNinja::Enterprise::GetLicenseExpirationTime()
{
return BNGetEnterpriseServerLicenseExpirationTime();
}
uint64_t BinaryNinja::Enterprise::GetLicenseDuration()
{
return BNGetEnterpriseServerLicenseDuration();
}
bool BinaryNinja::Enterprise::IsFloatingLicense()
{
return BNIsEnterpriseServerFloatingLicense();
}
uint64_t BinaryNinja::Enterprise::GetReservationTimeLimit()
{
return BNGetEnterpriseServerReservationTimeLimit();
}
bool BinaryNinja::Enterprise::IsLicenseStillActivated()
{
return BNIsEnterpriseServerLicenseStillActivated();
}
std::string BinaryNinja::Enterprise::GetLastError()
{
char* str = BNGetEnterpriseServerLastError();
std::string value = str;
BNFreeString(str);
return value;
}
void BinaryNinja::Enterprise::RegisterNotification(BNEnterpriseServerCallbacks* notify)
{
BNRegisterEnterpriseServerNotification(notify);
}
void BinaryNinja::Enterprise::UnregisterNotification(BNEnterpriseServerCallbacks* notify)
{
BNUnregisterEnterpriseServerNotification(notify);
}
BinaryNinja::Enterprise::LicenseCheckout::LicenseCheckout(int64_t duration): m_acquiredLicense(false)
{
// This is a port of python's binaryninja.enterprise.LicenseCheckout
// UI builds have their own license manager
if (BNIsUIEnabled())
return;
if (!IsInitialized())
{
if (!Initialize())
{
// Named/computer licenses don't need this flow at all
if (!IsFloatingLicense())
{
return;
}
// Floating licenses though, this is an error. Probably the error
// for needing to set enterprise.server.url in settings.json
throw EnterpriseException(
"Could not initialize Binary Ninja Enterprise: " + GetLastError()
);
}
}
if (!IsFloatingLicense())
{
return;
}
if (!IsConnected())
{
Connect();
}
if (!IsAuthenticated())
{
// Try keychain
bool gotAuth = AuthenticateWithMethod("Keychain", false);
if (!gotAuth)
{
char* username = getenv("BN_ENTERPRISE_USERNAME");
char* password = getenv("BN_ENTERPRISE_PASSWORD");
if (username && password)
{
gotAuth = AuthenticateWithCredentials(username, password, true);
}
}
if (!gotAuth)
{
throw EnterpriseException(
"Could not checkout a license: Not authenticated. Try one of the following: \n"
" - Log in and check out a license for an extended time\n"
" - Set BN_ENTERPRISE_USERNAME and BN_ENTERPRISE_PASSWORD environment variables\n"
" - Use BinaryNinja::Enterprise::AuthenticateWithCredentials or AuthenticateWithMethod in your code"
);
}
}
// Keychain auth can activate a license if we have one in the keychain
// If we have an expired named license, try to get a fresh floating one
if (!IsLicenseStillActivated() || (!IsFloatingLicense() && (time_t)BNGetLicenseExpirationTime() < time(nullptr)))
{
if (!Enterprise::UpdateLicense(duration))
{
throw EnterpriseException("Could not checkout a license: " + GetLastError());
}
m_acquiredLicense = true;
}
}
BinaryNinja::Enterprise::LicenseCheckout::~LicenseCheckout()
{
// UI builds have their own license manager
if (BNIsUIEnabled())
return;
// Don't release if we got one from keychain
if (m_acquiredLicense)
{
Enterprise::ReleaseLicense();
}
}