/
dataModel_SleuthkitJNI.cpp
2048 lines (1806 loc) · 63.9 KB
/
dataModel_SleuthkitJNI.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
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
/*
** dataModel_SleuthkitJNI
** The Sleuth Kit
**
** Brian Carrier [carrier <at> sleuthkit [dot] org]
** Copyright (c) 2010-2014 Brian Carrier. All Rights reserved
**
** This software is distributed under the Common Public License 1.0
**
*/
#include "tsk/tsk_tools_i.h"
#include "tsk/auto/tsk_case_db.h"
#include "tsk/hashdb/tsk_hash_info.h"
#include "tsk/auto/tsk_is_image_supported.h"
#include "jni.h"
#include "dataModel_SleuthkitJNI.h"
#include <locale.h>
#include <time.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <sstream>
using std::string;
using std::vector;
using std::map;
using std::stringstream;
static std::vector<TSK_HDB_INFO *> hashDbs;
/*
* JNI file handle structure encapsulates both
* TSK_FS_FILE file handle and TSK_FS_ATTR attribute
* to support multiple attributes for the same file.
* TSK_FS_FILE still needs be maintained for opening and closing.
*/
typedef struct {
uint32_t tag;
TSK_FS_FILE *fs_file;
TSK_FS_ATTR *fs_attr;
} TSK_JNI_FILEHANDLE;
#define TSK_JNI_FILEHANDLE_TAG 0x10101214
//stack-allocated buffer size for read method
#define FIXED_BUF_SIZE (16 * 1024)
/**
* Sets flag to throw an TskCoreException back up to the Java code with a specific message.
* Note: exception is thrown to Java code after the native function returns
* not when setThrowTskCoreError() is invoked - this must be addressed in the code following the exception
* @param the java environment to send the exception to
* @param msg message string
*/
static void
setThrowTskCoreError(JNIEnv * env, const char *msg)
{
jclass exception;
exception = env->FindClass("org/sleuthkit/datamodel/TskCoreException");
env->ThrowNew(exception, msg);
}
/**
* Sets flag to throw an TskCoreException back up to the Java code with the currently set error message.
* Note: exception is thrown to Java code after the native function returns
* not when setThrowTskCoreError() is invoked - this must be addressed in the code following the exception
* @param the java environment to send the exception to
*/
static void
setThrowTskCoreError(JNIEnv * env)
{
const char *msg = tsk_error_get();
setThrowTskCoreError(env, msg);
}
/**
* Sets flag to throw an TskDataException back up to the Java code with a specific message.
* Note: exception is thrown to Java code after the native function returns
* not when setThrowTskDataError() is invoked - this must be addressed in the code following the exception
* @param the java environment to send the exception to
* @param msg message string
*/
static void
setThrowTskDataError(JNIEnv * env, const char *msg)
{
jclass exception;
exception = env->FindClass("org/sleuthkit/datamodel/TskDataException");
env->ThrowNew(exception, msg);
}
#if 0
/**
* Sets flag to throw an TskDataException back up to the Java code with the currently set error message.
* Note: exception is thrown to Java code after the native function returns
* not when setThrowTskDataError() is invoked - this must be addressed in the code following the exception
* @param the java environment to send the exception to
*/
static void
setThrowTskDataError(JNIEnv * env)
{
const char *msg = tsk_error_get();
setThrowTskDataError(env, msg);
}
#endif
/***** Methods to cast from jlong to data type and check tags
They all throw an exception if the incorrect type is passed in. *****/
static TSK_IMG_INFO *
castImgInfo(JNIEnv * env, jlong ptr)
{
TSK_IMG_INFO *lcl = (TSK_IMG_INFO *) ptr;
if (!lcl || lcl->tag != TSK_IMG_INFO_TAG) {
setThrowTskCoreError(env, "Invalid IMG_INFO object");
return 0;
}
return lcl;
}
static TSK_VS_INFO *
castVsInfo(JNIEnv * env, jlong ptr)
{
TSK_VS_INFO *lcl = (TSK_VS_INFO *) ptr;
if (!lcl || lcl->tag != TSK_VS_INFO_TAG) {
setThrowTskCoreError(env, "Invalid VS_INFO object");
return 0;
}
// verify that image handle is still open
if (!castImgInfo(env, (jlong) lcl->img_info)) {
return 0;
}
return lcl;
}
static TSK_VS_PART_INFO *
castVsPartInfo(JNIEnv * env, jlong ptr)
{
TSK_VS_PART_INFO *lcl = (TSK_VS_PART_INFO *) ptr;
if (!lcl || lcl->tag != TSK_VS_PART_INFO_TAG) {
setThrowTskCoreError(env, "Invalid VS_PART_INFO object");
return 0;
}
// verify that all handles are still open
if (!castVsInfo(env, (jlong) lcl->vs)) {
return 0;
}
return lcl;
}
static TSK_FS_INFO *
castFsInfo(JNIEnv * env, jlong ptr)
{
TSK_FS_INFO *lcl = (TSK_FS_INFO *) ptr;
if (!lcl || lcl->tag != TSK_FS_INFO_TAG) {
setThrowTskCoreError(env, "Invalid FS_INFO object");
return 0;
}
// verify that image handle is still open
if (!castImgInfo(env, (jlong) lcl->img_info)) {
return 0;
}
return lcl;
}
static TSK_JNI_FILEHANDLE *
castFsFile(JNIEnv * env, jlong ptr)
{
TSK_JNI_FILEHANDLE *lcl = (TSK_JNI_FILEHANDLE *) ptr;
if (!lcl || lcl->tag != TSK_JNI_FILEHANDLE_TAG) {
setThrowTskCoreError(env, "Invalid TSK_JNI_FILEHANDLE object");
return 0;
}
// verify that all handles are still open
if (!lcl->fs_file || !castFsInfo(env, (jlong) lcl->fs_file->fs_info)) {
return 0;
}
return lcl;
}
static TskCaseDb *
castCaseDb(JNIEnv * env, jlong ptr)
{
TskCaseDb *lcl = ((TskCaseDb *) ptr);
if (!lcl || lcl->m_tag != TSK_CASE_DB_TAG) {
setThrowTskCoreError(env,
"Invalid TskCaseDb object");
return 0;
}
return lcl;
}
/**
* Convert a jstring (UTF-8) to a TCHAR to pass into TSK methods.
* @param buffer Buffer to store resulting string into
* @param size Length of buffer
* @param strJ string to convert
* @returns 1 on error
*/
static int
toTCHAR(JNIEnv * env, TSK_TCHAR * buffer, size_t size, jstring strJ)
{
jboolean isCopy;
char *str8 = (char *) env->GetStringUTFChars(strJ, &isCopy);
#ifdef TSK_WIN32
// Windows TCHAR is UTF16 in Windows, so convert
UTF16 *utf16 = (UTF16 *) buffer;
UTF8 *utf8 = (UTF8 *) str8;;
TSKConversionResult retval;
size_t lengthOfUtf8 = strlen(str8);
retval =
tsk_UTF8toUTF16((const UTF8 **) &utf8, &utf8[lengthOfUtf8],
&utf16, &utf16[size], TSKlenientConversion);
if (retval != TSKconversionOK) {
tsk_error_set_errno(TSK_ERR_IMG_CONVERT);
tsk_error_set_errstr
("toTCHAR: Error converting UTF8 %s to UTF16, error %d",
utf8, retval);
env->ReleaseStringUTFChars(strJ, str8);
return 1;
}
// "utf16" now points to last char. Need to NULL terminate the string.
*utf16 = '\0';
#else
// nothing to convert. Keep it as UTF8
strncpy((char *)&buffer[0], str8, size);
#endif
env->ReleaseStringUTFChars(strJ, str8);
return 0;
}
/*
* Open a TskCaseDb with an associated database
* @return the pointer to the case
* @param env pointer to java environment this was called from
* @param dbPath location for the database
* @rerurns 0 on error (sets java exception), pointer to newly opened TskCaseDb object on success
*/
JNIEXPORT jlong JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_newCaseDbNat(JNIEnv * env,
jclass obj, jstring dbPathJ) {
TSK_TCHAR dbPathT[1024];
int retval = toTCHAR(env, dbPathT, 1024, dbPathJ);
if (retval)
return retval;
TskCaseDb *tskCase = TskCaseDb::newDb(dbPathT);
if (tskCase == NULL) {
setThrowTskCoreError(env);
return 0;
}
return (jlong) tskCase;
}
/*
* Create a TskCaseDb with an associated database
* @return the pointer to the case
* @param env pointer to java environment this was called from
* @param env pointer to java environment this was called from
* @param cls the java class
* @param host the hostname or IP address
* @param port the port number as a string
* @param user the user name for the database
* @param pass the password for the database
* @param dbType the ordinal value of the enum for the database type
* @param dbName the name of the database to create
* @return 0 on error (sets java exception), pointer to newly opened TskCaseDb object on success
*/
JNIEXPORT jlong JNICALL Java_org_sleuthkit_datamodel_SleuthkitJNI_newCaseDbMultiNat(JNIEnv *env, jclass cls, jstring host, jstring port, jstring user, jstring pass, jint dbType, jstring dbName)
{
TSK_TCHAR dbPathT[1024];
toTCHAR(env, dbPathT, 1024, dbName);
const char* host_utf8 = env->GetStringUTFChars(host, NULL);
const char* port_utf8 = env->GetStringUTFChars(port, NULL);
const char* user_utf8 = env->GetStringUTFChars(user, NULL);
const char* pass_utf8 = env->GetStringUTFChars(pass, NULL);
CaseDbConnectionInfo info(host_utf8, port_utf8, user_utf8, pass_utf8, (CaseDbConnectionInfo::DbType)dbType);
TskCaseDb *tskCase = TskCaseDb::newDb(dbPathT, &info);
// free memory allocated by env->GetStringUTFChars()
env->ReleaseStringUTFChars(host, host_utf8);
env->ReleaseStringUTFChars(port, port_utf8);
env->ReleaseStringUTFChars(user, user_utf8);
env->ReleaseStringUTFChars(pass, pass_utf8);
if (tskCase == NULL) {
setThrowTskCoreError(env);
return 0;
}
return (jlong) tskCase;
}
/*
* Open a TskCaseDb with an associated database
* @return the pointer to the case
* @param env pointer to java environment this was called from
* @param cls the java class
* @param host the hostname or IP address
* @param port the port number as a string
* @param user the user name for the database
* @param pass the password for the database
* @param dbType the ordinal value of the enum for the database type
* @param dbName the name of the database to open
* @return Returns pointer to object or exception on error
*/
JNIEXPORT jlong JNICALL Java_org_sleuthkit_datamodel_SleuthkitJNI_openCaseDbMultiNat(JNIEnv *env, jclass cls, jstring host, jstring port, jstring user, jstring pass, jint dbType, jstring dbName)
{
TSK_TCHAR dbPathT[1024];
toTCHAR(env, dbPathT, 1024, dbName);
const char* host_utf8 = env->GetStringUTFChars(host, NULL);
const char* port_utf8 = env->GetStringUTFChars(port, NULL);
const char* user_utf8 = env->GetStringUTFChars(user, NULL);
const char* pass_utf8 = env->GetStringUTFChars(pass, NULL);
CaseDbConnectionInfo info(host_utf8, port_utf8, user_utf8, pass_utf8, (CaseDbConnectionInfo::DbType)dbType);
TskCaseDb *tskCase = TskCaseDb::openDb(dbPathT, &info);
// free memory allocated by env->GetStringUTFChars()
env->ReleaseStringUTFChars(host, host_utf8);
env->ReleaseStringUTFChars(port, port_utf8);
env->ReleaseStringUTFChars(user, user_utf8);
env->ReleaseStringUTFChars(pass, pass_utf8);
if (tskCase == NULL) {
setThrowTskCoreError(env);
return 0;
}
return (jlong) tskCase;
}
/*
* Open a TskCaseDb with an associated database
* @return the pointer to the case
* @param env pointer to java environment this was called from
* @param dbPath location for the database
* @return Returns pointer to object or exception on error
*/
JNIEXPORT jlong JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_openCaseDbNat(JNIEnv * env,
jclass obj, jstring dbPathJ) {
TSK_TCHAR dbPathT[1024];
toTCHAR(env, dbPathT, 1024, dbPathJ);
TskCaseDb *tskCase = TskCaseDb::openDb(dbPathT);
if (tskCase == NULL) {
setThrowTskCoreError(env);
return 0;
}
return (jlong) tskCase;
}
/*
* Close (cleanup) a case
* @param env pointer to java environment this was called from
* @param obj the java object this was called from
* @param caseHandle the pointer to the case
*/
JNIEXPORT void JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_closeCaseDbNat(JNIEnv * env,
jclass obj, jlong caseHandle) {
TskCaseDb *tskCase = castCaseDb(env, caseHandle);
if (tskCase == 0) {
//exception already set
return;
}
delete tskCase;
return;
}
/**
* Opens an existing hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param pathJ The path to the hash database.
* @return A handle for the hash database.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbOpenNat(JNIEnv * env,
jclass obj, jstring pathJ)
{
TSK_TCHAR pathT[1024];
toTCHAR(env, pathT, 1024, pathJ);
TSK_HDB_INFO *db = tsk_hdb_open(pathT, TSK_HDB_OPEN_NONE);
if (!db)
{
setThrowTskCoreError(env, tsk_error_get_errstr());
return -1;
}
// The index of the pointer in the vector is used as a handle for the
// database.
hashDbs.push_back(db);
return (jint)hashDbs.size();
}
/**
* Creates a new hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param pathJ The path to the hash database.
* @return A handle for the hash database.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbNewNat(JNIEnv * env,
jclass obj, jstring pathJ)
{
TSK_TCHAR pathT[1024];
toTCHAR(env, pathT, 1024, pathJ);
if (tsk_hdb_create(pathT)) {
setThrowTskCoreError(env, tsk_error_get_errstr());
return -1;
}
TSK_HDB_INFO *db = tsk_hdb_open(pathT, TSK_HDB_OPEN_NONE);
if (!db) {
setThrowTskCoreError(env, tsk_error_get_errstr());
return -1;
}
// The index of the pointer in the vector is used as a handle for the
// database.
hashDbs.push_back(db);
return (jint)hashDbs.size();
}
/**
* Begins a hash database transaction.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return 1 on error and 0 on success.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbBeginTransactionNat(
JNIEnv *env, jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle - 1);
if (!db) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
return tsk_hdb_begin_transaction(db);
}
/**
* Commits a hash database transaction.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return 1 on error and 0 on success.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbCommitTransactionNat(
JNIEnv *env, jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle - 1);
if (!db) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
return tsk_hdb_commit_transaction(db);
}
/**
* Rolls back a hash database transaction.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return 1 on error and 0 on success.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbRollbackTransactionNat(
JNIEnv *env, jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (!db) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
return tsk_hdb_rollback_transaction(db);
}
/**
* Adds data to a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param filenameJ Name of the file that was hashed (can be null).
* @param hashMd5J MD5 hash of file contents (can be null).
* @param hashSha1J SHA-1 hash of file contents (can be null).
* @param hashSha256J Text of SHA256 hash (can be null).
* @param dbHandle A handle for the hash database.
* @return 1 on error and 0 on success.
*/
JNIEXPORT jint JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbAddEntryNat(JNIEnv * env,
jclass obj, jstring filenameJ, jstring hashMd5J, jstring hashSha1J, jstring hashSha256J,
jstring commentJ, jint dbHandle)
{
if((size_t) dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
TSK_HDB_INFO * db = hashDbs.at(dbHandle-1);
if(!db) {
setThrowTskCoreError(env, "Invalid database handle");
return 1;
}
if(!db->accepts_updates()) {
setThrowTskCoreError(env, "Database does not accept updates");
return 1;
}
jboolean isCopy;
const char * name = filenameJ ? (const char *) env->GetStringUTFChars(filenameJ, &isCopy) : NULL;
const char * md5 = hashMd5J ? (const char *) env->GetStringUTFChars(hashMd5J, &isCopy) : NULL;
const char * sha1 = hashSha1J ? (const char *) env->GetStringUTFChars(hashSha1J, &isCopy) : NULL;
const char * sha256 = hashSha256J ? (const char *) env->GetStringUTFChars(hashSha256J, &isCopy) : NULL;
const char * comment = commentJ ? (const char *) env->GetStringUTFChars(commentJ, &isCopy) : NULL;
if (tsk_hdb_add_entry(db, name, md5, sha1, sha256, comment)) {
setThrowTskCoreError(env, tsk_error_get_errstr());
}
if (filenameJ) {
env->ReleaseStringUTFChars(filenameJ, (const char *) name);
}
if (hashMd5J) {
env->ReleaseStringUTFChars(hashMd5J, (const char *) md5);
}
if (hashSha1J) {
env->ReleaseStringUTFChars(hashSha1J, (const char *) sha1);
}
if (hashSha256J) {
env->ReleaseStringUTFChars(hashSha256J, (const char *) sha256);
}
if (commentJ) {
env->ReleaseStringUTFChars(commentJ, (const char *) comment);
}
return 0;
}
/**
* Queries whether or not a hash database accepts updates.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return True if hash database can be updated.
*/
JNIEXPORT jboolean JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbIsUpdateableNat(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
return (jboolean)(tsk_hdb_accepts_updates(db) == static_cast<uint8_t>(1));
}
/**
* Queries whether or not a hash database can be re-indexed. Only text-format
* databases with external indexes can be re-indexed.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return True if hash database can be indexed.
*/
JNIEXPORT jboolean JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbIsReindexableNat(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
return (jboolean)((tsk_hdb_uses_external_indexes(db) == 1) &&
(tsk_hdb_is_idx_only(db) == 0));
}
/**
* Gets the path of a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return Path to the hash database or "None" if no path is available.
*/
JNIEXPORT jstring JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbPathNat(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
jstring jPath = NULL;
const TSK_TCHAR *dbPath = tsk_hdb_get_db_path(db);
if (NULL != dbPath) {
const size_t pathLength = TSTRLEN(dbPath);
char *cPath = (char*)tsk_malloc((pathLength + 1) * sizeof(char));
snprintf(cPath, pathLength + 1, "%" PRIttocTSK, dbPath);
jPath = env->NewStringUTF(cPath);
free(cPath);
}
else {
jPath = env->NewStringUTF("None");
}
return jPath;
}
/*
* Gets the path of the external MD5 hash index for a text-format database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return Path to the requested index or "None" if no path is available.
*/
JNIEXPORT jstring JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbIndexPathNat(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
// Currently only supporting md5 indexes through Java binding.
jstring jPath = NULL;
const TSK_TCHAR *indexPath = tsk_hdb_get_idx_path(db, TSK_HDB_HTYPE_MD5_ID);
if (NULL != indexPath) {
const size_t pathLength = TSTRLEN(indexPath);
char *cPath = (char*)tsk_malloc((pathLength + 1) * sizeof(char));
snprintf(cPath, pathLength + 1, "%" PRIttocTSK, indexPath);
jPath = env->NewStringUTF(cPath);
free(cPath);
}
else {
jPath = env->NewStringUTF("None");
}
return jPath;
}
/**
* Queries whether the hash database is actually an external index for a
* text-format database that is being used for simple yes/no look ups in
* place of the roginal hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return True if the hash database is an external index serving as a
* database.
*/
JNIEXPORT jboolean JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbIsIdxOnlyNat(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
return (jboolean)(tsk_hdb_is_idx_only(db) == static_cast<uint8_t>(1));
}
/**
* Gets the display name of a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return The display name.
*/
JNIEXPORT jstring JNICALL Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbGetDisplayName
(JNIEnv * env, jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
jstring j_name = NULL;
const char *db_name = tsk_hdb_get_display_name(db);
if (NULL != db_name) {
j_name = env->NewStringUTF(db_name);
}
return j_name;
}
/**
* Closes all open hash databases.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
*/
JNIEXPORT void JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbCloseAll(JNIEnv * env,
jclass obj)
{
for (std::vector<TSK_HDB_INFO *>::iterator it = hashDbs.begin(); it != hashDbs.end(); ++it) {
if (NULL != *it) {
tsk_hdb_close(*it);
}
}
hashDbs.clear();
}
/**
* Closes a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
*/
JNIEXPORT void JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbClose(JNIEnv * env,
jclass obj, jint dbHandle)
{
if((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return;
}
tsk_hdb_close(db);
// Do NOT erase the element because that would shift the indices,
// messing up the existing handles.
hashDbs.at(dbHandle-1) = NULL;
}
/**
* Looks up a hash in a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return True if the hash is found in the hash database, false otherwise.
*/
JNIEXPORT jboolean JNICALL Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbLookup
(JNIEnv * env, jclass obj, jstring hash, jint dbHandle)
{
if ((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return (jboolean)false;
}
jboolean isCopy;
const char *cHashStr = (const char *) env->GetStringUTFChars(hash, &isCopy);
jboolean file_known = false;
int8_t retval = tsk_hdb_lookup_str(db, cHashStr, TSK_HDB_FLAG_QUICK, NULL, NULL);
if (retval == -1) {
setThrowTskCoreError(env, tsk_error_get_errstr());
}
else if (retval) {
file_known = true;
}
env->ReleaseStringUTFChars(hash, (const char *) cHashStr);
return file_known;
}
/**
* Looks up a hash in a hash database.
* @param env Pointer to Java environment from which this method was called.
* @param obj The Java object from which this method was called.
* @param dbHandle A handle for the hash database.
* @return A HashInfo object if the hash is found, NULL otherwise.
*/
JNIEXPORT jobject JNICALL Java_org_sleuthkit_datamodel_SleuthkitJNI_hashDbLookupVerbose
(JNIEnv * env, jclass obj, jstring hash, jint dbHandle) {
if ((size_t)dbHandle > hashDbs.size()) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
TSK_HDB_INFO *db = hashDbs.at(dbHandle-1);
if (db == NULL) {
setThrowTskCoreError(env, "Invalid database handle");
return NULL;
}
jboolean isCopy;
const char *inputHash = (const char *) env->GetStringUTFChars(hash, &isCopy);
TskHashInfo result;
int8_t returnCode = tsk_hdb_lookup_verbose_str(db, inputHash, (void*)&result);
env->ReleaseStringUTFChars(hash, (const char *) inputHash);
if (returnCode == -1) {
setThrowTskCoreError(env, tsk_error_get_errstr());
return NULL;
}
else if (returnCode == 0) {
return NULL;
}
// Convert the hashes from the hash database so they can be written into
// the Java version of a HashInfo object.
const char *md5 = result.hashMd5.c_str();
jstring md5j = env->NewStringUTF(md5);
const char *sha1 = result.hashSha1.c_str();
jstring sha1j = env->NewStringUTF(sha1);
const char *sha256 = result.hashSha2_256.c_str();
jstring sha256j = env->NewStringUTF(sha256);
// Create and return a Java HashInfo object.
jclass clazz;
clazz = env->FindClass("org/sleuthkit/datamodel/HashHitInfo");
jmethodID ctor = env->GetMethodID(clazz, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
jmethodID addName = env->GetMethodID(clazz, "addName", "(Ljava/lang/String;)V");
jmethodID addComment = env->GetMethodID(clazz, "addComment", "(Ljava/lang/String;)V");
jobject hashInfo = env->NewObject(clazz, ctor, md5j, sha1j, sha256j);
for (std::vector<std::string>::iterator it = result.fileNames.begin(); it != result.fileNames.end(); ++it) {
jstring namej = env->NewStringUTF((*it).c_str());
env->CallVoidMethod(hashInfo, addName, namej);
}
for (std::vector<std::string>::iterator it = result.comments.begin(); it != result.comments.end(); ++it) {
jstring commentj = env->NewStringUTF((*it).c_str());
env->CallVoidMethod(hashInfo, addComment, commentj);
}
return hashInfo;
}
/*
* Initialize a process for adding an image to a case database.
*
* @param env Pointer to java environment.
* @param obj Pointer the Java class object.
* @partam caseHandle Pointer to a TskCaseDb object.
* @param timeZone The time zone for the image.
* @param addUnallocSpace Pass true to create virtual files for unallocated space. Ignored if addFileSystems is false.
* @param skipFatFsOrphans Pass true to skip processing of orphan files for FAT file systems. Ignored if addFileSystems is false.
*
* @return A pointer to the process (TskAutoDb object) or NULL on error.
*/
JNIEXPORT jlong JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_initAddImgNat(JNIEnv * env,
jclass obj, jlong caseHandle, jstring timeZone, jboolean addUnallocSpace, jboolean skipFatFsOrphans) {
return Java_org_sleuthkit_datamodel_SleuthkitJNI_initializeAddImgNat(env, obj, caseHandle, timeZone, true, addUnallocSpace, skipFatFsOrphans);
}
/*
* Initialize a process for adding an image to a case database.
*
* @param env Pointer to java environment.
* @param obj Pointer the Java class object.
* @partam caseHandle Pointer to a TskCaseDb object.
* @param timeZone The time zone for the image.
* @param addFileSystems Pass true to attempt to add file systems within the image to the case database.
* @param addUnallocSpace Pass true to create virtual files for unallocated space. Ignored if addFileSystems is false.
* @param skipFatFsOrphans Pass true to skip processing of orphan files for FAT file systems. Ignored if addFileSystems is false.
*
* @return A pointer to the process (TskAutoDb object) or NULL on error.
*/
JNIEXPORT jlong JNICALL
Java_org_sleuthkit_datamodel_SleuthkitJNI_initializeAddImgNat(JNIEnv * env, jclass obj,
jlong caseHandle, jstring timeZone, jboolean addFileSystems, jboolean addUnallocSpace, jboolean skipFatFsOrphans) {
jboolean isCopy;
TskCaseDb *tskCase = castCaseDb(env, caseHandle);
if (tskCase == 0) {
//exception already set
return 0;
}
if (env->GetStringUTFLength(timeZone) > 0) {
const char *tzstr = env->GetStringUTFChars(timeZone, &isCopy);
if (strlen(tzstr) > 64) {
env->ReleaseStringUTFChars(timeZone, tzstr);
stringstream ss;
ss << "Timezone is too long";
setThrowTskCoreError(env, ss.str().c_str());
return 0;
}
char envstr[70];
snprintf(envstr, 70, "TZ=%s", tzstr);
env->ReleaseStringUTFChars(timeZone, tzstr);
if (0 != putenv(envstr)) {
stringstream ss;
ss << "Error setting timezone environment, using: ";
ss << envstr;
setThrowTskCoreError(env, ss.str().c_str());
return 0;
}
/* we should be checking this somehow */
TZSET();
}
TskAutoDb *tskAuto = tskCase->initAddImage();
if (tskAuto == NULL) {
setThrowTskCoreError(env, "Error getting tskAuto handle from initAddImage");
return 0;
}
// set the options flags
tskAuto->setAddFileSystems(addFileSystems?true:false);
if (addFileSystems) {
if (addUnallocSpace) {