forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicetracker_httpd.cc
1205 lines (938 loc) · 42.3 KB
/
devicetracker_httpd.cc
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <memory>
#include <stdio.h>
#include <time.h>
#include <list>
#include <map>
#include <vector>
#include "kismet_algorithm.h"
#include <string>
#include <sstream>
#include <pthread.h>
#include "globalregistry.h"
#include "util.h"
#include "configfile.h"
#include "messagebus.h"
#include "packetchain.h"
#include "devicetracker.h"
#include "packet.h"
#include "gpstracker.h"
#include "alertracker.h"
#include "manuf.h"
#include "entrytracker.h"
#include "devicetracker_component.h"
#include "msgpack_adapter.h"
#include "xmlserialize_adapter.h"
#include "json_adapter.h"
#include "structured.h"
#include "kismet_json.h"
#include "base64.h"
// HTTP interfaces
bool Devicetracker::Httpd_VerifyPath(const char *path, const char *method) {
if (strcmp(method, "GET") == 0) {
// Simple fixed URLS
string stripped = Httpd_StripSuffix(path);
bool can_serialize = Httpd_CanSerialize(path);
// Explicit compare for .ekjson because it doesn't serialize the
// same way
if (strcmp(path, "/devices/all_devices.ekjson") == 0)
return true;
/*
if (stripped == "/devices/all_devices" && can_serialize)
return true;
if (stripped == "/devices/all_devices_dt" && can_serialize)
return true;
if (strcmp(path, "/devices/all_devices.xml") == 0)
return true;
*/
if (stripped == "/phy/all_phys" && can_serialize)
return true;
if (stripped == "/phy/all_phys_dt" && can_serialize)
return true;
// Split URL and process
vector<string> tokenurl = StrTokenize(path, "/");
if (tokenurl.size() < 2)
return false;
if (tokenurl[1] == "devices") {
if (tokenurl.size() < 3)
return false;
// Do a by-key lookup and return the device or the device path
if (tokenurl[2] == "by-key") {
if (tokenurl.size() < 5) {
return false;
}
local_locker lock(&devicelist_mutex);
uint64_t key = 0;
std::stringstream ss(tokenurl[3]);
ss >> key;
if (!Httpd_CanSerialize(tokenurl[4]))
return false;
map<uint64_t, shared_ptr<kis_tracked_device_base> >::iterator tmi =
tracked_map.find(key);
if (tmi == tracked_map.end())
return false;
string target = Httpd_StripSuffix(tokenurl[4]);
if (target == "device") {
// Try to find the exact field
if (tokenurl.size() > 5) {
vector<string>::const_iterator first = tokenurl.begin() + 5;
vector<string>::const_iterator last = tokenurl.end();
vector<string> fpath(first, last);
if (tmi->second->get_child_path(fpath) == NULL) {
return false;
}
}
return true;
}
return false;
} else if (tokenurl[2] == "by-mac") {
if (tokenurl.size() < 5)
return false;
local_locker lock(&devicelist_mutex);
if (!Httpd_CanSerialize(tokenurl[4]))
return false;
mac_addr mac = mac_addr(tokenurl[3]);
if (mac.error) {
return false;
}
if (tracked_mac_multimap.count(mac) > 0)
return true;
return false;
} else if (tokenurl[2] == "last-time") {
if (tokenurl.size() < 5) {
return false;
}
long lastts;
if (sscanf(tokenurl[3].c_str(), "%ld", &lastts) != 1) {
return false;
}
// Explicit catch of ekjson
if (tokenurl[4] == "devices.ekjson")
return true;
return Httpd_CanSerialize(tokenurl[4]);
}
}
} else if (strcmp(method, "POST") == 0) {
// Split URL and process
vector<string> tokenurl = StrTokenize(path, "/");
if (tokenurl.size() < 2)
return false;
if (tokenurl[1] == "devices") {
if (tokenurl.size() < 4) {
return false;
} else if (tokenurl[2] == "summary") {
return Httpd_CanSerialize(tokenurl[3]);
} else if (tokenurl[2] == "last-time") {
if (tokenurl.size() < 5) {
return false;
}
long lastts;
if (sscanf(tokenurl[3].c_str(), "%ld", &lastts) != 1) {
fprintf(stderr, "debug - unable to parse ts\n");
return false;
}
return Httpd_CanSerialize(tokenurl[4]);
} else if (tokenurl[2] == "by-key") {
if (tokenurl.size() < 5) {
return false;
}
local_locker lock(&devicelist_mutex);
uint64_t key = 0;
std::stringstream ss(tokenurl[3]);
ss >> key;
if (!Httpd_CanSerialize(tokenurl[4]))
return false;
map<uint64_t, shared_ptr<kis_tracked_device_base> >::iterator tmi =
tracked_map.find(key);
if (tmi == tracked_map.end())
return false;
string target = Httpd_StripSuffix(tokenurl[4]);
if (target == "device") {
return true;
}
if (target == "set_name") {
return true;
}
} else if (tokenurl[2] == "by-mac") {
if (tokenurl.size() < 5)
return false;
local_locker lock(&devicelist_mutex);
if (!Httpd_CanSerialize(tokenurl[4]))
return false;
mac_addr mac = mac_addr(tokenurl[3]);
if (mac.error) {
return false;
}
if (tracked_mac_multimap.count(mac) > 0)
return true;
return false;
}
}
}
return false;
}
void Devicetracker::httpd_all_phys(string path, std::ostream &stream,
string in_wrapper_key) {
SharedTrackerElement phyvec =
globalreg->entrytracker->GetTrackedInstance(phy_base_id);
SharedTrackerElement wrapper = NULL;
if (in_wrapper_key != "") {
wrapper.reset(new TrackerElement(TrackerMap));
wrapper->add_map(phyvec);
phyvec->set_local_name(in_wrapper_key);
} else {
wrapper = phyvec;
}
shared_ptr<kis_tracked_phy> anyphy(new kis_tracked_phy(globalreg, phy_base_id));
anyphy->set_from_phy(this, KIS_PHY_ANY);
phyvec->add_vector(anyphy);
map<int, Kis_Phy_Handler *>::iterator mi;
for (mi = phy_handler_map.begin(); mi != phy_handler_map.end(); ++mi) {
shared_ptr<kis_tracked_phy> p(new kis_tracked_phy(globalreg, phy_base_id));
p->set_from_phy(this, mi->first);
phyvec->add_vector(p);
}
entrytracker->Serialize(httpd->GetSuffix(path), stream, wrapper, NULL);
}
void Devicetracker::httpd_device_summary(string url, std::ostream &stream,
shared_ptr<TrackerElementVector> subvec,
vector<SharedElementSummary> summary_vec,
string in_wrapper_key) {
local_locker lock(&devicelist_mutex);
SharedTrackerElement devvec =
globalreg->entrytracker->GetTrackedInstance(device_summary_base_id);
TrackerElementSerializer::rename_map rename_map;
// Wrap the dev vec in a dictionary and change its name
SharedTrackerElement wrapper = NULL;
if (in_wrapper_key != "") {
wrapper.reset(new TrackerElement(TrackerMap));
wrapper->add_map(devvec);
devvec->set_local_name(in_wrapper_key);
} else {
wrapper = devvec;
}
if (subvec == NULL) {
for (unsigned int x = 0; x < tracked_vec.size(); x++) {
if (summary_vec.size() == 0) {
devvec->add_vector(tracked_vec[x]);
} else {
SharedTrackerElement simple;
SummarizeTrackerElement(entrytracker, tracked_vec[x],
summary_vec, simple, rename_map);
devvec->add_vector(simple);
}
}
} else {
for (TrackerElementVector::const_iterator x = subvec->begin();
x != subvec->end(); ++x) {
if (summary_vec.size() == 0) {
devvec->add_vector(*x);
} else {
SharedTrackerElement simple;
SummarizeTrackerElement(entrytracker, *x,
summary_vec, simple, rename_map);
devvec->add_vector(simple);
}
}
}
entrytracker->Serialize(httpd->GetSuffix(url), stream, wrapper, &rename_map);
}
void Devicetracker::httpd_xml_device_summary(std::ostream &stream) {
local_locker lock(&devicelist_mutex);
SharedTrackerElement devvec =
globalreg->entrytracker->GetTrackedInstance(device_summary_base_id);
for (unsigned int x = 0; x < tracked_vec.size(); x++) {
devvec->add_vector(tracked_vec[x]);
}
XmlserializeAdapter *xml = new XmlserializeAdapter(globalreg);
xml->RegisterField("kismet.device.list", "SummaryDevices");
xml->RegisterFieldNamespace("kismet.device.list",
"k",
"http://www.kismetwireless.net/xml/summary",
"http://www.kismetwireless.net/xml/summary.xsd");
xml->RegisterFieldSchema("kismet.device.list",
"common",
"http://www.kismetwireless.net/xml/common",
"http://www.kismetwireless.net/xml/common.xsd");
xml->RegisterFieldSchema("kismet.device.list",
"gps",
"http://www.kismetwireless.net/xml/gps",
"http://www.kismetwireless.net/xml/gps.xsd");
xml->RegisterField("kismet.device.summary", "summary");
xml->RegisterField("kismet.device.base.name", "name");
xml->RegisterField("kismet.device.base.phyname", "phyname");
xml->RegisterField("kismet.device.base.signal", "signal");
xml->RegisterField("kismet.device.base.channel", "channel");
xml->RegisterField("kismet.device.base.frequency", "frequency");
xml->RegisterField("kismet.device.base.manuf", "manufacturer");
xml->RegisterField("kismet.device.base.key", "key");
xml->RegisterField("kismet.device.base.macaddr", "macaddress");
xml->RegisterField("kismet.device.base.type", "type");
xml->RegisterField("kismet.device.base.first_time", "firstseen");
xml->RegisterField("kismet.device.base.last_time", "lastseen");
xml->RegisterField("kismet.device.base.packets.total", "packetstotal");
xml->RegisterField("kismet.common.signal.last_signal_dbm", "lastsignaldbm");
xml->RegisterField("kismet.common.signal.min_signal_dbm", "minsignaldbm");
xml->RegisterField("kismet.common.signal.max_signal_dbm", "maxsignaldbm");
xml->RegisterField("kismet.common.signal.last_noise_dbm", "lastnoisedbm");
xml->RegisterField("kismet.common.signal.min_noise_dbm", "minnoisedbm");
xml->RegisterField("kismet.common.signal.max_noise_dbm", "maxnoisedbm");
xml->RegisterField("kismet.common.signal.last_signal_rssi", "lastsignalrssi");
xml->RegisterField("kismet.common.signal.min_signal_rssi", "minsignalrssi");
xml->RegisterField("kismet.common.signal.max_signal_rssi", "maxsignalrssi");
xml->RegisterField("kismet.common.signal.last_noise_rssi", "lastnoiserssi");
xml->RegisterField("kismet.common.signal.min_noise_rssi", "minnoiserssi");
xml->RegisterField("kismet.common.signal.max_noise_rssi", "maxnoiserssi");
xml->RegisterField("kismet.common.signal.peak_loc", "peaklocation");
xml->RegisterFieldXsitype("kismet.common.signal.peak_loc", "kismet:location");
xml->RegisterField("kismet.common.location.lat", "lat");
xml->RegisterField("kismet.common.location.lon", "lon");
xml->RegisterField("kismet.common.location.alt", "alt");
xml->RegisterField("kismet.common.location.speed", "speed");
stream << "<?xml version=\"1.0\"?>";
xml->XmlSerialize(devvec, stream);
delete(xml);
}
int Devicetracker::Httpd_CreateStreamResponse(
Kis_Net_Httpd *httpd __attribute__((unused)),
Kis_Net_Httpd_Connection *connection,
const char *path, const char *method, const char *upload_data,
size_t *upload_data_size) {
if (strcmp(method, "GET") != 0) {
return MHD_YES;
}
// Allocate our buffer aux
Kis_Net_Httpd_Buffer_Stream_Aux *saux =
(Kis_Net_Httpd_Buffer_Stream_Aux *) connection->custom_extension;
BufferHandlerOStringStreambuf *streambuf =
new BufferHandlerOStringStreambuf(saux->get_rbhandler());
std::ostream stream(streambuf);
// Set our cleanup function
saux->set_aux(streambuf,
[streambuf](Kis_Net_Httpd_Buffer_Stream_Aux *aux) {
if (aux->aux != NULL)
delete((BufferHandlerOStreambuf *) (aux->aux));
});
// Set our sync function which is called by the webserver side before we
// clean up...
saux->set_sync([streambuf](Kis_Net_Httpd_Buffer_Stream_Aux *aux) {
if (aux->aux != NULL) {
((BufferHandlerOStringStreambuf *) aux->aux)->pubsync();
}
});
if (strcmp(path, "/devices/all_devices.ekjson") == 0) {
// Instantiate a manual serializer
JsonAdapter::Serializer serial(globalreg);
devicetracker_function_worker fw(globalreg,
[this, &stream, &serial](Devicetracker *, shared_ptr<kis_tracked_device_base> d) -> bool {
serial.serialize(d, stream);
stream << "\n";
// Return false because we're not building a list, we're serializing
// per element
return false;
}, NULL);
MatchOnDevices(&fw);
return MHD_YES;
}
string stripped = Httpd_StripSuffix(path);
if (stripped == "/phy/all_phys") {
httpd_all_phys(path, stream);
return MHD_YES;
}
if (stripped == "/phy/all_phys_dt") {
httpd_all_phys(path, stream, "aaData");
return MHD_YES;
}
vector<string> tokenurl = StrTokenize(path, "/");
if (tokenurl.size() < 2)
return MHD_YES;
if (tokenurl[1] == "devices") {
if (tokenurl.size() < 5)
return MHD_YES;
if (tokenurl[2] == "by-key") {
if (tokenurl.size() < 5) {
return MHD_YES;
}
if (!Httpd_CanSerialize(tokenurl[4]))
return MHD_YES;
local_locker lock(&devicelist_mutex);
uint64_t key = 0;
std::stringstream ss(tokenurl[3]);
ss >> key;
/*
if (sscanf(tokenurl[3].c_str(), "%lu", &key) != 1) {
return;
}
*/
map<uint64_t, shared_ptr<kis_tracked_device_base> >::iterator tmi =
tracked_map.find(key);
if (tmi == tracked_map.end()) {
stream << "Invalid device key";
return MHD_YES;
}
string target = Httpd_StripSuffix(tokenurl[4]);
if (target == "device") {
// Try to find the exact field
if (tokenurl.size() > 5) {
vector<string>::const_iterator first = tokenurl.begin() + 5;
vector<string>::const_iterator last = tokenurl.end();
vector<string> fpath(first, last);
SharedTrackerElement sub = tmi->second->get_child_path(fpath);
if (sub == NULL) {
return MHD_YES;
}
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream, sub, NULL);
return MHD_YES;
}
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream,
tmi->second, NULL);
return MHD_YES;
} else {
return MHD_YES;
}
} else if (tokenurl[2] == "by-mac") {
if (tokenurl.size() < 5)
return MHD_YES;
if (!Httpd_CanSerialize(tokenurl[4]))
return MHD_YES;
local_locker lock(&devicelist_mutex);
mac_addr mac = mac_addr(tokenurl[3]);
if (mac.error) {
return MHD_YES;
}
SharedTrackerElement devvec(new TrackerElement(TrackerVector));
auto mmp = tracked_mac_multimap.equal_range(mac);
for (auto mmpi = mmp.first; mmpi != mmp.second; ++mmpi) {
devvec->add_vector(mmpi->second);
}
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream, devvec, NULL);
return MHD_YES;
} else if (tokenurl[2] == "last-time") {
if (tokenurl.size() < 5)
return MHD_YES;
// Is the timestamp an int?
long lastts;
if (sscanf(tokenurl[3].c_str(), "%ld", &lastts) != 1)
return MHD_YES;
// If it's negative, subtract from the current ts
if (lastts < 0) {
time_t now = time(0);
lastts = now + lastts;
}
if (!Httpd_CanSerialize(tokenurl[4]))
return MHD_YES;
local_locker lock(&devicelist_mutex);
SharedTrackerElement devvec =
globalreg->entrytracker->GetTrackedInstance(device_list_base_id);
devicetracker_function_worker fw(globalreg,
[this, &stream, devvec, lastts](Devicetracker *, shared_ptr<kis_tracked_device_base> d) -> bool {
if (d->get_last_time() <= lastts)
return false;
devvec->add_vector(d);
return false;
}, NULL);
MatchOnDevices(&fw);
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream, devvec, NULL);
return MHD_YES;
}
}
return MHD_YES;
}
int Devicetracker::Httpd_PostComplete(Kis_Net_Httpd_Connection *concls) {
local_locker lock(&devicelist_mutex);
// Split URL and process
vector<string> tokenurl = StrTokenize(concls->url, "/");
Kis_Net_Httpd_Buffer_Stream_Aux *saux =
(Kis_Net_Httpd_Buffer_Stream_Aux *) concls->custom_extension;
BufferHandlerOStreambuf *streambuf =
new BufferHandlerOStreambuf(saux->get_rbhandler());
std::ostream stream(streambuf);
saux->set_aux(streambuf,
[streambuf](Kis_Net_Httpd_Buffer_Stream_Aux *aux) {
if (aux->aux != NULL)
delete((BufferHandlerOStreambuf *) (aux->aux));
});
// Set our sync function which is called by the webserver side before we
// clean up...
saux->set_sync([streambuf](Kis_Net_Httpd_Buffer_Stream_Aux *aux) {
if (aux->aux != NULL) {
((BufferHandlerOStringStreambuf *) aux->aux)->pubsync();
}
});
// All URLs are at least /devices/summary/x or /devices/last-time/ts/x
if (tokenurl.size() < 4) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
// Common structured API data
SharedStructured structdata;
// Summarization vector
vector<SharedElementSummary> summary_vec;
// Wrapper, if any
string wrapper_name;
// Rename cache generated during simplification
TrackerElementSerializer::rename_map rename_map;
SharedStructured regexdata;
try {
// Decode the base64 msgpack and parse it, or parse the json
if (concls->variable_cache.find("msgpack") != concls->variable_cache.end()) {
structdata.reset(new StructuredMsgpack(Base64::decode(concls->variable_cache["msgpack"]->str())));
} else if (concls->variable_cache.find("json") !=
concls->variable_cache.end()) {
structdata.reset(new StructuredJson(concls->variable_cache["json"]->str()));
} else {
// fprintf(stderr, "debug - missing data\n");
throw StructuredDataException("Missing data");
}
} catch(const StructuredDataException e) {
stream << "Invalid request: ";
stream << e.what();
concls->httpcode = 400;
return MHD_YES;
}
try {
if (structdata->hasKey("fields")) {
SharedStructured fields = structdata->getStructuredByKey("fields");
StructuredData::structured_vec fvec = fields->getStructuredArray();
for (StructuredData::structured_vec::iterator i = fvec.begin();
i != fvec.end(); ++i) {
if ((*i)->isString()) {
SharedElementSummary s(new TrackerElementSummary((*i)->getString(),
entrytracker));
summary_vec.push_back(s);
} else if ((*i)->isArray()) {
StructuredData::string_vec mapvec = (*i)->getStringVec();
if (mapvec.size() != 2) {
// fprintf(stderr, "debug - malformed rename pair\n");
stream << "Invalid request: "
"Expected field, rename";
concls->httpcode = 400;
return MHD_YES;
}
SharedElementSummary s(new TrackerElementSummary(mapvec[0],
mapvec[1], entrytracker));
summary_vec.push_back(s);
}
}
}
// Get the wrapper, if one exists, default to empty if it doesn't
wrapper_name = structdata->getKeyAsString("wrapper", "");
if (structdata->hasKey("regex")) {
regexdata = structdata->getStructuredByKey("regex");
}
} catch(const StructuredDataException e) {
stream << "Invalid request: ";
stream << e.what();
concls->httpcode = 400;
return MHD_YES;
}
if (tokenurl[1] == "devices") {
if (tokenurl[2] == "by-mac") {
if (tokenurl.size() < 5) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
if (!Httpd_CanSerialize(tokenurl[4])) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
local_locker lock(&devicelist_mutex);
if (!Httpd_CanSerialize(tokenurl[4])) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
mac_addr mac = mac_addr(tokenurl[3]);
if (mac.error) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
if (tracked_mac_multimap.count(mac) == 0) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
string target = Httpd_StripSuffix(tokenurl[4]);
if (target == "devices") {
SharedTrackerElement devvec(new TrackerElement(TrackerVector));
auto mmp = tracked_mac_multimap.equal_range(mac);
for (auto mmpi = mmp.first; mmpi != mmp.second; ++mmpi) {
SharedTrackerElement simple;
SummarizeTrackerElement(entrytracker, mmpi->second, summary_vec,
simple, rename_map);
devvec->add_vector(simple);
}
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream,
devvec, &rename_map);
return MHD_YES;
}
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
} else if (tokenurl[2] == "by-key") {
if (tokenurl.size() < 5) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
if (!Httpd_CanSerialize(tokenurl[4])) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
uint64_t key = 0;
std::stringstream ss(tokenurl[3]);
ss >> key;
map<uint64_t, shared_ptr<kis_tracked_device_base> >::iterator tmi =
tracked_map.find(key);
if (tmi == tracked_map.end()) {
stream << "Invalid request";
concls->httpcode = 400;
return MHD_YES;
}
string target = Httpd_StripSuffix(tokenurl[4]);
if (target == "device") {
SharedTrackerElement simple;
SummarizeTrackerElement(entrytracker, tmi->second, summary_vec,
simple, rename_map);
entrytracker->Serialize(httpd->GetSuffix(tokenurl[4]), stream,
simple, &rename_map);
return MHD_YES;
}
if (target == "set_name") {
// Must have a session to set the name
if (!httpd->HasValidSession(concls)) {
return MHD_YES;
}
}
} else if (tokenurl[2] == "summary") {
// Wrapper we insert under
SharedTrackerElement wrapper = NULL;
// DT fields
SharedTrackerElement dt_length_elem = NULL;
SharedTrackerElement dt_filter_elem = NULL;
SharedTrackerElement outdevs =
globalreg->entrytracker->GetTrackedInstance(device_list_base_id);
unsigned int dt_start = 0;
unsigned int dt_length = 0;
int dt_draw = 0;
int in_dt_length, in_dt_start;
// Search string
string dt_search;
// Resolved paths to fields we search
vector<vector<int> > dt_search_paths;
unsigned int dt_order_col = -1;
int dt_order_dir = 0;
vector<int> dt_order_field;
if (structdata->getKeyAsBool("datatable", false)) {
// fprintf(stderr, "debug - we think we're doing a server-side datatable\n");
if (concls->variable_cache.find("start") !=
concls->variable_cache.end()) {
*(concls->variable_cache["start"]) >> in_dt_start;
}
if (concls->variable_cache.find("length") !=
concls->variable_cache.end()) {
*(concls->variable_cache["length"]) >> in_dt_length;
}
if (concls->variable_cache.find("draw") !=
concls->variable_cache.end()) {
*(concls->variable_cache["draw"]) >> dt_draw;
}
if (concls->variable_cache.find("search[value]") !=
concls->variable_cache.end()) {
dt_search = concls->variable_cache["search[value]"]->str();
}
// If we're searching, we need to figure out what columns are
// searchable. Because of how we have to map names into datatables,
// we don't get a usable field definition from the dt js plugin,
// BUT we DO get a usable fieldspec from our fields list that
// we already processed... so we have to make a slightly funky
// assumption that columns[x] is equivalent to summary_vec[x],
// and then we just pull the parsed-int field path in for our
// searching mechanism
if (dt_search.length() != 0) {
// fprintf(stderr, "debug - searching for '%s'\n", dt_search.c_str());
std::stringstream sstr;
// We have to act like an array and iterate through the
// column fields... We use the summary vec length as a
// quick cheat
for (unsigned int ci = 0; ci < summary_vec.size(); ci++) {
sstr.str("");
sstr << "columns[" << ci << "][searchable]";
map<string, std::unique_ptr<std::stringstream> >::iterator mi;
if ((mi = concls->variable_cache.find(sstr.str())) !=
concls->variable_cache.end()) {
if (mi->second->str() == "true") {
// We can blindly trust the offset b/c we're
// iterating from our summary vec size, not the
// form data
dt_search_paths.push_back(summary_vec[ci]->resolved_path);
}
} else {
// If we've run out of columns to look at for some
// reason just bail instead of doing more string
// construction
break;
}
}
}
// We only handle sorting by the first column
if (concls->variable_cache.find("order[0][column]") !=
concls->variable_cache.end()) {
*(concls->variable_cache["order[0][column]"]) >> dt_order_col;
}
// Don't allow ordering by a column that doesn't make sense
if (dt_order_col >= summary_vec.size())
dt_order_col = -1;
if (dt_order_col > 0 &&
concls->variable_cache.find("order[0][dir]") !=
concls->variable_cache.end()) {
string ord = concls->variable_cache["order[0][dir]"]->str();
if (ord == "asc")
dt_order_dir = 1;
dt_order_field = summary_vec[dt_order_col]->resolved_path;
}
// Force a length if we think we're doing a smart position and
// something has gone wonky
if (in_dt_length <= 0 || in_dt_length > 200) {
dt_length = 50;
} else {
dt_length = in_dt_length;
}
if (in_dt_start < 0)
dt_start = 0;
else
dt_start = in_dt_start;
// DT always has to wrap in an object
wrapper.reset(new TrackerElement(TrackerMap));
// wrap in 'data' for DT
wrapper->add_map(outdevs);
outdevs->set_local_name("data");
// Set the DT draw
SharedTrackerElement
draw_elem(new TrackerElement(TrackerUInt64, dt_draw_id));
draw_elem->set((uint64_t) dt_draw);
draw_elem->set_local_name("draw");
wrapper->add_map(draw_elem);
// Make the length and filter elements
dt_length_elem.reset(new TrackerElement(TrackerUInt64, dt_length_id));
dt_length_elem->set_local_name("recordsTotal");
dt_length_elem->set((uint64_t) tracked_vec.size());
wrapper->add_map(dt_length_elem);
dt_filter_elem.reset(new TrackerElement(TrackerUInt64, dt_filter_id));
dt_filter_elem->set_local_name("recordsFiltered");
wrapper->add_map(dt_filter_elem);
}
if (regexdata != NULL) {
// If we're doing a basic regex outside of devicetables
// shenanigans...
SharedTrackerElement pcredevs =
globalreg->entrytracker->GetTrackedInstance(device_list_base_id);
TrackerElementVector pcrevec(pcredevs);
devicetracker_pcre_worker worker(globalreg, regexdata, pcredevs);
MatchOnDevices(&worker);
// Check DT ranges
if (dt_start >= pcrevec.size())
dt_start = 0;
if (dt_filter_elem != NULL)
dt_filter_elem->set((uint64_t) pcrevec.size());
// Sort the list by the selected column
if (dt_order_col >= 0) {
kismet__stable_sort(pcrevec.begin(), pcrevec.end(),
[&](SharedTrackerElement a, SharedTrackerElement b) {
SharedTrackerElement fa =
GetTrackerElementPath(dt_order_field, a);
SharedTrackerElement fb =
GetTrackerElementPath(dt_order_field, b);
if (dt_order_dir == 0)
return fa < fb;
return fb < fa;
});
}
// If we filtered, that's our list
TrackerElementVector::iterator vi;
// Set the iterator endpoint for our length
TrackerElementVector::iterator ei;
if (dt_length == 0 ||
dt_length + dt_start >= pcrevec.size())
ei = pcrevec.end();
else