forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enterprise.cpp
262 lines (202 loc) · 6.49 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
// Copyright (c) 2015-2023 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::EnterpriseServer;
bool BinaryNinja::EnterpriseServer::AuthenticateWithCredentials(const std::string& username, const std::string& password, bool remember)
{
return BNAuthenticateEnterpriseServerWithCredentials(username.c_str(), password.c_str(), remember);
}
bool BinaryNinja::EnterpriseServer::AuthenticateWithMethod(const std::string& method, bool remember)
{
return BNAuthenticateEnterpriseServerWithMethod(method.c_str(), remember);
}
std::vector<std::pair<std::string, std::string>> BinaryNinja::EnterpriseServer::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::EnterpriseServer::Deauthenticate()
{
return BNDeauthenticateEnterpriseServer();
}
void BinaryNinja::EnterpriseServer::CancelAuthentication()
{
return BNCancelEnterpriseServerAuthentication();
}
bool BinaryNinja::EnterpriseServer::Connect()
{
return BNConnectEnterpriseServer();
}
bool BinaryNinja::EnterpriseServer::AcquireLicense(uint64_t timeout)
{
return BNAcquireEnterpriseServerLicense(timeout);
}
bool BinaryNinja::EnterpriseServer::ReleaseLicense()
{
return BNReleaseEnterpriseServerLicense();
}
bool BinaryNinja::EnterpriseServer::IsConnected()
{
return BNIsEnterpriseServerConnected();
}
bool BinaryNinja::EnterpriseServer::IsAuthenticated()
{
return BNIsEnterpriseServerAuthenticated();
}
std::string BinaryNinja::EnterpriseServer::GetUsername()
{
char* value = BNGetEnterpriseServerUsername();
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::EnterpriseServer::GetToken()
{
char* value = BNGetEnterpriseServerToken();
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::EnterpriseServer::GetServerName()
{
char* value = BNGetEnterpriseServerName();
std::string result = value;
BNFreeString(value);
return result;
}
std::string BinaryNinja::EnterpriseServer::GetServerId()
{
char* value = BNGetEnterpriseServerId();
std::string result = value;
BNFreeString(value);
return result;
}
uint64_t BinaryNinja::EnterpriseServer::GetServerVersion()
{
return BNGetEnterpriseServerVersion();
}
std::string BinaryNinja::EnterpriseServer::GetServerBuildId()
{
char* value = BNGetEnterpriseServerBuildId();
std::string result = value;
BNFreeString(value);
return result;
}
uint64_t BinaryNinja::EnterpriseServer::GetLicenseExpirationTime()
{
return BNGetEnterpriseServerLicenseExpirationTime();
}
uint64_t BinaryNinja::EnterpriseServer::GetLicenseDuration()
{
return BNGetEnterpriseServerLicenseDuration();
}
bool BinaryNinja::EnterpriseServer::IsFloatingLicense()
{
return BNIsEnterpriseServerFloatingLicense();
}
uint64_t BinaryNinja::EnterpriseServer::GetReservationTimeLimit()
{
return BNGetEnterpriseServerReservationTimeLimit();
}
bool BinaryNinja::EnterpriseServer::IsLicenseStillActivated()
{
return BNIsEnterpriseServerLicenseStillActivated();
}
std::string BinaryNinja::EnterpriseServer::GetLastError()
{
return BNGetEnterpriseServerLastError();
char* str = BNGetEnterpriseServerLastError();
std::string value = str;
BNFreeString(str);
return value;
}
void BinaryNinja::EnterpriseServer::RegisterNotification(BNEnterpriseServerCallbacks* notify)
{
BNRegisterEnterpriseServerNotification(notify);
}
void BinaryNinja::EnterpriseServer::UnregisterNotification(BNEnterpriseServerCallbacks* notify)
{
BNUnregisterEnterpriseServerNotification(notify);
}
BinaryNinja::EnterpriseServer::LicenseCheckout::LicenseCheckout(int64_t duration)
{
// This is a port of python's binaryninja.enterprise.LicenseCheckout
// UI builds have their own license manager
if (BNIsUIEnabled())
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() && BNGetLicenseExpirationTime() < time(nullptr)))
{
if (!EnterpriseServer::AcquireLicense(duration))
{
throw EnterpriseException("Could not checkout a license: " + GetLastError());
}
m_acquiredLicense = true;
}
}
BinaryNinja::EnterpriseServer::LicenseCheckout::~LicenseCheckout()
{
// UI builds have their own license manager
if (BNIsUIEnabled())
return;
// Don't release if we got one from keychain
if (m_acquiredLicense)
{
EnterpriseServer::ReleaseLicense();
}
}