-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathida_ui.cpp
405 lines (345 loc) · 11 KB
/
ida_ui.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
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
#include <pro.h>
#include <kernwin.hpp>
#include <netnode.hpp>
#include <loader.hpp>
#include <json-c/json.h>
#include "collabreate.h"
#include "collabreate_ui.h"
#if IDA_SDK_VERSION < 700
static TForm *collab_tform;
static TForm *cform;
#else
static TWidget *collab_tform;
static TWidget *cform;
#endif
static form_actions_t *dangerous;
static qvector<qstring> messages;
static qstring msg_text;
// Form actions for editor window
enum collab_form_actions
{
TEXT_CHANGED = 2,
SEND = 3
};
#if IDA_SDK_VERSION < 700
static int idaapi send_cb(TView *[], int x) {
#else
static int idaapi send_cb(TWidget *[], int x) {
#endif
return 0;
}
//--------------------------------------------------------------------------
// this callback is called when something happens in our non-modal editor form
static int idaapi collab_cb(int fid, form_actions_t &fa) {
switch (fid) {
case CB_INIT:
// msg("init collab form\n");
break;
case CB_CLOSE:
// msg("closing collab form\n");
// mark the form as closed
collab_tform = NULL;
break;
case TEXT_CHANGED:
// msg("text has been changed\n");
break;
case SEND: { // Send button pressed
qstring val;
#if IDA_SDK_VERSION <= 650
if (fa.get_field_value(2, &val)) {
#elif IDA_SDK_VERSION <= 670
if (fa._get_field_value(2, &val)) {
#else
if (fa.get_string_value(2, &val)) {
#endif
msg("Sending: %s\n", val.c_str());
//*** are next two lines necessary or should we wait
//for message to get sent back from the server following timestamping
msgHistory.push_back(val.c_str());
refresh_chooser("Collab form:1");
do_send_user_message(val.c_str());
val.clear();
#if IDA_SDK_VERSION <= 650
fa.set_field_value(2, &val);
#elif IDA_SDK_VERSION <= 670
fa._set_field_value(2, &val);
#else
fa.set_string_value(2, &val);
#endif
}
break;
}
default:
break;
}
return 1;
}
bool do_connect(Dispatcher disp) {
const char *format = "BUTTON YES* Ok\nBUTTON CANCEL Cancel\nConnect to collabREate server\n\n\n<Server:A:64:64::>\n<Port:D:16:16::>\n";
char host[128];
if (is_connected()) return true;
sval_t port = cnn.altval(LAST_PORT_ALTVAL);
if (port == 0) {
port = 5042;
}
host[0] = 0;
cnn.supstr(LAST_SERVER_SUPVAL, host, sizeof(host));
int res = AskUsingForm_c(format, host, &port);
if (res == ASKBTN_YES) {
cnn.altset(LAST_PORT_ALTVAL, port);
cnn.supset(LAST_SERVER_SUPVAL, host);
//connect to the server.
if (!connect_to(host, port, disp)) {
//open failure dialog here, require user to use cancel button
//to close the connect dialog
//build a better error message including host:port
warning("Error connection to database server failed");
return false;
}
msg(PLUGIN_NAME": successfully connected to %s:%d\n", host, port);
return true;
}
return false;
}
bool do_auth() {
const char *format = "BUTTON YES* Ok\nBUTTON CANCEL Cancel\ncollabREate Login\n\n\n<User Name:A:64:64::>\n<Password:A:64:64::>\n";
char user[128];
char password[128];
user[0] = 0;
password[0] = 0;
cnn.supstr(LAST_USER_SUPVAL, user, sizeof(user));
int res = AskUsingForm_c(format, user, password);
if (res == ASKBTN_YES) {
::saveAuthData(user, password);
return true;
}
return false;
}
int choose_project(json_object *json) {
const char *format = "BUTTON YES* Ok\nBUTTON CANCEL Cancel\nProject Selection\n\n\nSelect a project to join:\n<Project:b:0:::>\n<Description (New project only):A:1024:64::>\n";
description[0] = 0;
json_object *jprojects;
if (!json_object_object_get_ex(json, "projects", &jprojects)) {
return -1;
}
uint32_t numProjects = (uint32_t)json_object_array_length(jprojects);
numProjectsGlobal = numProjects;
::qstrvec_t projectList;
int sel = 0;
projects = (int*)qalloc(numProjects * sizeof(int));
snapUpdateIDs = (uint64_t*)qalloc(numProjects * sizeof(uint64_t));
optMasks = (Options*)qalloc(numProjects * sizeof(Options));
//the New project is always listed as the first option
projectList.push_back("<New project>");
for (uint32_t i = 0; i < numProjects; i++) {
json_object *project = json_object_array_get_idx(jprojects, i);
if (!int32_from_json(project, "id", &projects[i])) {
//malformed json
return -1;
}
if (!uint64_from_json(project, "snap_id", &snapUpdateIDs[i])) {
//malformed json
return -1;
}
//if (snapUpdateIDs[i] > 0 ) {
// msg(PLUGIN_NAME": project %d is a snapshot\n", i+1);
//}
const char *desc = string_from_json(project, "description");
if (desc == NULL) {
//malformed json
return -1;
}
#ifdef DEBUG
int isSnapShot = 0;
if (snapUpdateIDs[i] != 0) {
isSnapShot = 1;
}
msg(PLUGIN_NAME": %d : %d - %s (%d) ", i, projects[i], desc, isSnapShot);
#endif
projectList.push_back(desc);
//need to read options mask for this project
//but for now everything is enabled
//memset(optMasks + i, 0xFF, sizeof(Options));
if (!uint64_from_json(project, "pub_mask", &optMasks[i].pub.ll)) {
//malformed json
return -1;
}
if (!uint64_from_json(project, "sub_mask", &optMasks[i].sub.ll)) {
//malformed json
return -1;
}
#ifdef DEBUG
msg(PLUGIN_NAME": P %x S %x \n", optMasks[i].pub.ii[0], optMasks[i].sub.ii[0]);
#endif
}
json_object *options;
if (!json_object_object_get_ex(json, "options", &options)) {
return -1;
}
int numOptions = (int)json_object_array_length(options);
numOptionsGlobal = numOptions;
optLabels = (char**)qalloc(sizeof(char*) * numOptions);
for (int i = 0; i < numOptions; i++) {
json_object *label = json_object_array_get_idx(options, i);
optLabels[i] = qstrdup(json_object_to_json_string(label));
}
int res = AskUsingForm_c(format, &projectList, &sel, description);
if (res == ASKBTN_YES) {
return sel ? projects[sel - 1] : 0; //map selection index to associated project number
}
return -1;
}
bool do_project_select(json_object *json) {
bool result = true;
projects = NULL;
snapUpdateIDs = NULL;
int selected = choose_project(json);
if (selected >= 0) {
selectProject(selected);
}
else {
#ifdef DEBUG
msg(PLUGIN_NAME": project select cancelled\n");
#endif
result = false;
}
freeProjectFields();
return result;
}
#if IDA_SDK_VERSION < 700
static void idaapi collab_getl(void *, uint32 n, char *const *arrptr) {
qstrncpy(arrptr[0], n == 0 ? "Messages" : msgHistory[n-1].c_str(), MAXSTR);
}
static uint32 idaapi collab_sizer(void *) {
return msgHistory.size();
}
void createCollabStatus() {
static const char format[] =
"BUTTON NO NONE\nBUTTON YES NONE\nBUTTON CANCEL NONE\n"
"Collab form\n\n"
"%/\n" // placeholder for the form's callback
"<Messages:E1:::::>\n<Send:B3:20:::>< :q2::128:::>\n";
chooser_info_t ci;
ci.columns = 1;
ci.getl = collab_getl;
ci.sizer = collab_sizer;
static const int widths[] = { 128 };
ci.widths = widths;
// selection for chooser list view
intvec_t ivec;
collab_tform = OpenForm_c(format, FORM_QWIDGET | FORM_TAB, collab_cb, &ci, &ivec, send_cb, &msg_text);
}
#else
struct collab_msg_chooser : public chooser_t {
static const int widths[];
static const char* header[];
collab_msg_chooser() : chooser_t(CH_KEEP, 1, widths, header, "Collab form") {};
void idaapi get_row(qstrvec_t *cols, int * /*icon_*/, chooser_item_attrs_t * /*attrs*/, size_t n) const {
qstrvec_t &cols_ = *cols;
cols_[0] = msgHistory[n];
};
size_t idaapi get_count() const {
return msgHistory.size();
};
};
static collab_msg_chooser status;
// selection for chooser list view
static sizevec_t status_ivec;
const int collab_msg_chooser::widths[] = { 128 };
const char* collab_msg_chooser::header[] = { "Messages" };
#if IDA_SDK_VERSION < 730
#define WOPN_DP_TAB WOPN_TAB
#endif
void createCollabStatus() {
static const char format[] =
"BUTTON NO NONE\nBUTTON YES NONE\nBUTTON CANCEL NONE\n"
"Collab form\n\n"
"%/\n" // placeholder for the form's callback
"<Messages:E1:::::>\n<Send:B3:20:::>< :q2::100:::>\n";
collab_tform = OpenForm_c(format, WOPN_DP_TAB, collab_cb, &status, &status_ivec, send_cb, &msg_text);
}
#endif
bool sameDay(time_t t1, time_t t2) {
tm *p_tm1 = localtime(&t1);
if (p_tm1 == NULL) {
return false; //date error
}
tm tm1 = *p_tm1;
tm *p_tm2 = localtime(&t2);
if (p_tm2 == NULL) {
return false; //date error
}
tm tm2 = *p_tm2;
return tm1.tm_yday == tm2.tm_yday && tm1.tm_year == tm2.tm_year;
}
static const char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
void postCollabMessage(const char *message, time_t t) {
static time_t last = 0;
//get local time to prepend to message
if (t == 0) {
time(&t);
}
bool same = sameDay(last, t);
last = t;
tm *lt = localtime(&t);
char change[80];
if (!same) {
::qsnprintf(change, sizeof(change), "Day changed to %02d %s %4d", lt->tm_mday, months[lt->tm_mon], lt->tm_year + 1900);
msgHistory.push_back(change);
}
uint32_t len = 16 + (uint32_t)strlen(message);
char *m = new char[len];
::qsnprintf(m, len, "%02d:%02d %s", lt->tm_hour, lt->tm_min, message);
msgHistory.push_back(m);
refresh_chooser("Collab form:1");
delete [] m;
}
#if IDA_SDK_VERSION >= 700
struct cmd_chooser : public chooser_t {
static const int widths[];
static const char *header[];
cmd_chooser() : chooser_t(CH_MODAL | CH_KEEP, 1, widths,
header, CHOOSER_NOMAINMENU CHOOSER_NOSTATUSBAR "Select Command") {
};
virtual void idaapi get_row(qstrvec_t *cols, int * /*icon_*/, chooser_item_attrs_t * /*attrs*/, size_t n) const {
qstrvec_t &cols_ = *cols;
cols_[0] = getRunCommand((int)n);
};
virtual size_t idaapi get_count() const {
return numCommands();
};
};
static sizevec_t cmd_choices;
static cmd_chooser cmd_info;
const int cmd_chooser::widths[] = { 32 };
const char *cmd_chooser::header[] = { "" };
int do_choose_command() {
return (int)cmd_info.choose();
}
#else
char* idaapi get_command(void *obj, uint32 n, char *buf) {
if (n) {
qstrncpy(buf, getRunCommand(n - 1), MAXSTR);
}
else {
qstrncpy(buf, "Command", MAXSTR);
}
return buf;
}
uint32 idaapi sizer(void *obj) {
return numCommands();
}
int do_choose_command() {
int res = choose(CH_MODAL, -1, -1, -1, -1, (void*)NULL, 32, sizer, get_command, "Command", -1);
if (res >= 1 && res <= numCommands()) {
return res - 1;
}
return -1;
}
#endif
bool do_choose_perms(json_object *json) { //"pub" "sub", "pub_mask", "sub_mask", also "perms" list
bool result = false;
return result;
}