/
files.c
665 lines (537 loc) · 17.9 KB
/
files.c
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
#include "files.h"
#include "ssh.h"
#include "http.h"
#include "memcached.h"
#include "fingerprint.h"
#include "xattr.h"
#include "check-hash.h"
#include "include-exclude.h"
#include "find.h"
#include <string.h>
#include <fnmatch.h>
//this is a map of all directories/collections visited, (webpages in the case of http). It's used to
//prevent getting trapped in loops
ListNode *Visited=NULL;
static int FileType(const char *Path, int FTFlags, struct stat *Stat)
{
if ((FTFlags & FLAG_NET))
{
if (strncasecmp(Path,"ssh:",4)==0) return(FT_SSH);
if (strncasecmp(Path,"http:",5)==0) return(FT_HTTP);
if (strncasecmp(Path,"https:",6)==0) return(FT_HTTP);
}
if ((! Stat) || (Stat->st_mode==0))
{
return(FT_FILE);
}
//if we are in devmode then just open the following things as files
if (FTFlags & FLAG_DEVMODE) return(FT_FILE);
//otherwise treat them specially
if (S_ISDIR(Stat->st_mode)) return(FT_DIR);
if( S_ISCHR(Stat->st_mode)) return(FT_CHR);
if (S_ISBLK(Stat->st_mode)) return(FT_BLK);
if (S_ISFIFO(Stat->st_mode)) return(FT_FIFO);
if (S_ISSOCK(Stat->st_mode)) return(FT_SOCK);
if (S_ISLNK(Stat->st_mode)) return(FT_LNK);
return(FT_FILE);
}
int StatFile(HashratCtx *Ctx, const char *Path, struct stat *Stat)
{
int val;
memset(Stat, 0, sizeof(struct stat));
//Pass NULL for stat, because we are only checking for 'net' type paths
val=FileType(Path, Flags, NULL);
if ((val !=FT_FILE) && (val !=FT_DIR) && (val != FT_LNK))
{
return(val);
}
if (Ctx->Flags & CTX_DEREFERENCE) val=stat(Path,Stat);
else val=lstat(Path,Stat);
if (val==0) return(FileType(Path,Flags, Stat));
return(val);
}
void GlobFiles(HashratCtx *Ctx, const char *Path, int FType, ListNode *Dirs)
{
char *Tempstr=NULL;
glob_t Glob;
int i, flags=0;
struct stat *Stat;
ListClear(Dirs, Destroy);
switch (FType)
{
case FT_SSH:
SSHGlob(Ctx, Path, Dirs);
return;
break;
case FT_HTTP:
HTTPGlob(Ctx, Path, Dirs);
return;
break;
}
Tempstr=QuoteCharsInStr(Tempstr, Path, "[]*?");
Tempstr=CatStr(Tempstr,"/*");
#ifdef GLOB_PERIOD
if (Ctx->Flags & CTX_HIDDEN) flags |= GLOB_PERIOD;
#endif
glob(Tempstr,flags,0,&Glob);
for (i=0; i < Glob.gl_pathc; i++)
{
if (
(strcmp(GetBasename(Glob.gl_pathv[i]),".") !=0) &&
(strcmp(GetBasename(Glob.gl_pathv[i]),"..") !=0)
)
{
if (Dirs)
{
Stat=(struct stat *) calloc(1,sizeof(struct stat));
if (StatFile(Ctx,Glob.gl_pathv[i], Stat) != -1)
{
ListAddNamedItem(Dirs, Glob.gl_pathv[i], Stat);
}
else
{
fprintf(stderr,"ERROR: Cannot stat file %s\n",Glob.gl_pathv[i]);
free(Stat);
}
}
}
}
Destroy(Tempstr);
globfree(&Glob);
}
int HashratHashFile(HashratCtx *Ctx, HASH *Hash, int Type, const char *Path, struct stat *FStat)
{
STREAM *S;
char *Tempstr=NULL, *User=NULL, *Pass=NULL;
const char *ptr;
int result, val, RetVal=FALSE;
off_t bytes_read=0, size=0, oval;
if (FStat) size=FStat->st_size;
switch (Type)
{
case FT_HTTP:
User=CopyStr(User,GetVar(Ctx->Vars,"HTTPUser"));
Pass=CopyStr(Pass,GetVar(Ctx->Vars,"HTTPPass"));
S=HTTPGet(Path);
ptr=STREAMGetValue(S, "HTTP:ResponseCode");
if ((!ptr) || (*ptr !='2'))
{
STREAMClose(S);
S=NULL;
}
break;
case FT_SSH:
S=SSHGet(Ctx, Path);
break;
default:
if ((! StrValid(Path)) || (strcmp(Path,"-")==0)) S=STREAMFromFD(0);
else
{
if (Ctx->Flags & CTX_DEREFERENCE) S=STREAMOpen(Path,"rf");
else S=STREAMOpen(Path,"rf");
}
break;
}
if (S)
{
Tempstr=SetStrLen(Tempstr,BUFSIZ);
oval=size;
if ((oval==0) || ( oval > BUFSIZ)) val=BUFSIZ;
else val=(int)oval;
result=STREAMReadBytes(S,Tempstr,val);
while (result > STREAM_CLOSED)
{
Tempstr[result]='\0';
if (result > 0) Hash->Update(Hash,Tempstr, result);
bytes_read+=result;
if (size > 0)
{
if ((Type != FT_HTTP) && (bytes_read >= size)) break;
oval=size - bytes_read;
if (oval > BUFSIZ) val=BUFSIZ;
else val=(int)oval;
}
result=STREAMReadBytes(S,Tempstr,val);
}
if (FStat && (FStat->st_size==0)) FStat->st_size=bytes_read;
if (Type != FT_SSH) STREAMClose(S);
RetVal=TRUE;
}
Destroy(Tempstr);
Destroy(User);
Destroy(Pass);
//for now we close any connection after a 'get'
//Ctx->NetCon=NULL;
return(RetVal);
}
void HashratFinishHash(char **RetStr, HashratCtx *Ctx, HASH *Hash)
{
int val;
const char *ptr;
HashFinish(Hash,Ctx->Encoding,RetStr);
/*
ptr=GetVar(Ctx->Vars,"Output:Length");
if (StrValid(ptr))
{
val=atoi(ptr);
if ((val > 0) && (StrLen(*RetStr) > val)) (*RetStr)[val]='\0';
}
*/
}
int HashratHashSingleFile(HashratCtx *Ctx, const char *HashType, int FileType, const char *Path, struct stat *FStat, char **RetStr)
{
HASH *Hash;
struct stat XattrStat;
const char *ptr;
*RetStr=CopyStr(*RetStr,"");
#ifdef USE_XATTR
if (Ctx->Flags & CTX_XATTR_CACHE)
{
XAttrGetHash(Ctx, "user", Ctx->HashType, Path, &XattrStat, RetStr);
//only use the hash cached in the xattr address if it's younger than the mtime
printf("cache %llu %llu %llu\n",XattrStat.st_mtime, FStat->st_mtime, XattrStat.st_mtime - FStat->st_mtime);
if ( ((XattrStat.st_mtime - FStat->st_mtime) < 10) ) *RetStr=CopyStr(*RetStr,"");
}
if (! StrValid(*RetStr))
#endif
{
Hash=HashInit(HashType);
if (Hash)
{
//If we're not doing HMAC then this doesn't do anything
ptr=GetVar(Ctx->Vars,"EncryptionKey");
if (ptr) HMACSetKey(Hash, ptr, StrLen(ptr));
if (! HashratHashFile(Ctx,Hash,FileType,Path, FStat)) return(FALSE);
HashratFinishHash(RetStr, Ctx, Hash);
}
}
if (StrValid(*RetStr)) return(TRUE);
return(FALSE);
}
//This is used to processs small pieces of data like device IDs
void ProcessData(char **RetStr, HashratCtx *Ctx, const char *Data, int DataLen)
{
const char *ptr;
HASH *Hash;
if (! Ctx->Hash)
{
Hash=HashInit(Ctx->HashType);
if (Hash)
{
ptr=GetVar(Ctx->Vars,"EncryptionKey");
if (ptr) HMACSetKey(Hash, ptr, StrLen(ptr));
ptr=GetVar(Ctx->Vars, "InputPrefix");
if (StrValid(ptr)) Hash->Update(Hash,ptr, StrLen(ptr));
Hash->Update(Hash,Data, DataLen);
HashratFinishHash(RetStr, Ctx, Hash);
}
}
else Ctx->Hash->Update(Ctx->Hash,Data, DataLen);
}
static int ConsiderItem(HashratCtx *Ctx, const char *Path, struct stat *FStat)
{
int Type, result;
ListNode *Items, *Node;
Type=FileType(Path, Flags, FStat);
result=IncludeExcludeCheck(Ctx, Path, FStat);
if (result != CTX_INCLUDE) return(result);
switch (Type)
{
case FT_HTTP:
Type=HTTPStat(Ctx, Path, FStat);
if ((Type==FT_DIR) && (Ctx->Flags & CTX_RECURSE)) return(CTX_HASH_AND_RECURSE);
break;
case FT_SSH:
Items=ListCreate();
Type=SSHGlob(Ctx, Path, Items);
//if remote path is a directory, then return without bothering to update
//any information about it from 'Items'
if (Type==FT_DIR)
{
ListDestroy(Items, Destroy);
return(CTX_RECURSE);
}
//if it's a file then update FStat with its information
Node=ListGetNext(Items);
if (Node) memcpy(FStat, Node->Item, sizeof(struct stat));
ListDestroy(Items, Destroy);
break;
case FT_DIR:
if (Ctx->Flags & CTX_RECURSE) return(CTX_RECURSE);
break;
}
return(0);
}
int HashItem(HashratCtx *Ctx, const char *HashType, const char *Path, struct stat *FStat, char **HashStr)
{
int Type=FT_FILE, val;
char *Tempstr=NULL;
Type=FileType(Path, Flags, FStat);
switch (Type)
{
case FT_HTTP:
return(HashratHashSingleFile(Ctx, HashType, Type, Path, FStat, HashStr));
break;
case FT_SSH:
return(HashratHashSingleFile(Ctx, HashType, Type, Path, FStat, HashStr));
break;
case FT_DIR:
if (Ctx->Flags & CTX_RECURSE) return(CTX_RECURSE);
else ProcessData(HashStr, Ctx, (const char *) &FStat->st_ino, sizeof(ino_t));
break;
case FT_CHR:
ProcessData(HashStr, Ctx, (const char *) &FStat->st_rdev, sizeof(dev_t));
break;
case FT_BLK:
ProcessData(HashStr, Ctx, (const char *) &FStat->st_rdev, sizeof(dev_t));
break;
case FT_FIFO:
ProcessData(HashStr, Ctx, (const char *) &FStat->st_ino, sizeof(ino_t));
break;
case FT_SOCK:
ProcessData(HashStr, Ctx, (const char *) &FStat->st_ino, sizeof(ino_t));
break;
case FT_LNK:
if (Ctx->Flags & CTX_DEREFERENCE)
{
if (! Ctx->Hash)
{
if (! HashratHashSingleFile(Ctx, HashType, Type, Path, FStat, HashStr)) return(FALSE);
} else if (! HashratHashFile(Ctx, Ctx->Hash,Type,Path, FStat)) return(FALSE);
}
else
{
fprintf(stderr,"WARN: Not following symbolic link %s\n",Path);
Tempstr=SetStrLen(Tempstr,PATH_MAX);
val=readlink(Path, Tempstr,PATH_MAX);
if (val > 0)
{
Tempstr[val]='\0';
ProcessData(HashStr, Ctx, Tempstr, val);
}
}
break;
default:
if (! Ctx->Hash)
{
if (! HashratHashSingleFile(Ctx, HashType, Type, Path, FStat, HashStr)) return(FALSE);
} else if (! HashratHashFile(Ctx, Ctx->Hash,Type,Path, FStat)) return(FALSE);
break;
}
Destroy(Tempstr);
return(TRUE);
}
//HashratAction returns true on a significant event, which is either an item found in search
//or a check failing in hash-checking mode
int HashratAction(HashratCtx *Ctx, const char *Path, struct stat *Stat)
{
char *HashStr=NULL;
int Type, result=FALSE;
TFingerprint *FP=NULL;
switch (Ctx->Action)
{
case ACT_HASHDIR:
Type=FileType(Path, Flags, Stat);
//we return TRUE if hash succeeded
if (HashratHashFile(Ctx, Ctx->Hash, Type, Path, Stat)) result=TRUE;
break;
case ACT_HASH:
HashStartTime=GetTime(TIME_MILLISECS);
if (HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr))
{
HashratOutputFileInfo(Ctx, Ctx->Out, Path, Stat, HashStr);
HashratStoreHash(Ctx, Path, Stat, HashStr);
result=TRUE;
}
break;
case ACT_CHECK:
if (S_ISREG(Stat->st_mode))
{
if (Stat->st_size > 0)
{
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
FP=CheckForMatch(Ctx, Path, Stat, HashStr);
if (FP && HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) MatchCount++;
else
{
HandleCheckFail(Path, "Changed or new");
//we return TRUE on FAILURE, as we are signaling a significant event
result=TRUE;
}
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
break;
case ACT_CHECK_XATTR:
if (S_ISREG(Stat->st_mode))
{
//result == TRUE by default (TRUE==Signficant event, here meaning 'check failed')
//we set it here so we get the right result even if the stored hash fails to load
result=TRUE;
FP=XAttrLoadHash(Ctx, Path);
if (FP)
{
HashItem(Ctx, FP->HashType, Path, Stat, &HashStr);
if (HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) result=FALSE;
}
else fprintf(stderr,"ERROR: No stored hash for '%s'\n",Path);
}
else fprintf(stderr,"ERROR: Not regular file '%s'. Not checking in xattr mode.\n",Path);
break;
case ACT_CHECK_MEMCACHED:
if (S_ISREG(Stat->st_mode))
{
//result == TRUE by default (TRUE==Signficant event, here meaning 'check failed')
result=TRUE;
if (Stat->st_size > 0)
{
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
FP=(TFingerprint *) calloc(1,sizeof(TFingerprint));
if (Flags & FLAG_NET) FP->Path=MCopyStr(FP->Path, Path);
else FP->Path=MCopyStr(FP->Path,"hashrat://",LocalHost,Path,NULL);
FP->Hash=MemcachedGet(FP->Hash, FP->Path);
if (FP && HashratCheckFile(Ctx, Path, NULL, HashStr, FP)) result=FALSE;
else fprintf(stderr,"ERROR: No stored hash for '%s'\n",Path);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
else fprintf(stderr,"ERROR: Not regular file '%s'. Not checking in memcached mode.\n",Path);
break;
case ACT_FINDMATCHES:
case ACT_FINDMATCHES_MEMCACHED:
if (S_ISREG(Stat->st_mode))
{
if (Stat->st_size > 0)
{
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
FP=CheckForMatch(Ctx, Path, Stat, HashStr);
if (FP)
{
if (StrValid(FP->Path) || StrValid(FP->Data)) printf("LOCATED: %s '%s %s' at %s\n",FP->Hash, FP->Path, FP->Data, Path);
else printf("LOCATED: %s at %s\n",FP->Hash, Path);
MatchCount++;
//here we return true if a match found
result=TRUE;
}
else DiffCount++;
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
break;
case ACT_FINDDUPLICATES:
if (S_ISREG(Stat->st_mode))
{
if (Stat->st_size > 0)
{
if (HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr))
{
FP=CheckForMatch(Ctx, Path, Stat, HashStr);
if (FP)
{
printf("DUPLICATE: %s of %s %s\n",Path,FP->Path,FP->Data);
MatchCount++;
//here we return true if a match found
result=TRUE;
}
else
{
FP=TFingerprintCreate(HashStr, Ctx->HashType, "", Path);
DiffCount++;
MatchAdd(FP, Path, 0);
//as we've added FP to an internal list we don't want it destroyed
FP=NULL;
}
}
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
break;
}
if (result==TRUE)
{
if (FP) RunHookScript(DiffHook, Path, FP->Path);
else RunHookScript(DiffHook, Path, "");
}
if (FP) TFingerprintDestroy(FP);
Destroy(HashStr);
return(result);
}
//ProcessItem returns TRUE on a significant event, so any instance of TRUE
//from items checked makes return value here TRUE
static int ProcessDir(HashratCtx *Ctx, const char *Dir)
{
char *Tempstr=NULL;
ListNode *FileList, *Curr;
int result=FALSE;
int Type;
Type=FileType(Dir, Flags, NULL);
FileList=ListCreate();
GlobFiles(Ctx, Dir, Type, FileList);
Curr=ListGetNext(FileList);
while (Curr)
{
if (ProcessItem(Ctx, Curr->Tag, (struct stat *) Curr->Item, FALSE)) result=TRUE;
Curr=ListGetNext(Curr);
}
ListDestroy(FileList,free);
Destroy(Tempstr);
return(result);
}
//ProcessDir returns TRUE on a significant event, so any instance of TRUE
//from items checked makes return value here TRUE
static int HashratRecurse(HashratCtx *Ctx, const char *Path, char **HashStr)
{
const char *ptr;
struct stat FStat;
int result=FALSE;
if ((Ctx->Action == ACT_HASHDIR) && (! Ctx->Hash))
{
HashStartTime=GetTime(TIME_MILLISECS);
Ctx->Hash=HashInit(Ctx->HashType);
ptr=GetVar(Ctx->Vars,"EncryptionKey");
if (ptr) HMACSetKey(Ctx->Hash, ptr, StrLen(ptr));
if (! ProcessDir(Ctx, Path)) result=FALSE;
HashratFinishHash(HashStr, Ctx, Ctx->Hash);
stat(Path, &FStat);
HashratOutputFileInfo(Ctx, Ctx->Out, Path, &FStat, *HashStr);
result=TRUE;
}
else if (ProcessDir(Ctx, Path)) result=TRUE;
return(result);
}
int ProcessItem(HashratCtx *Ctx, const char *Path, struct stat *Stat, int IsTopLevel)
{
char *HashStr=NULL;
int result=FALSE, Flags;
const char *p_Path;
p_Path=Path;
if (strncmp(p_Path, "./", 2)==0) p_Path+=2;
if (! Visited) Visited=MapCreate(1025, LIST_FLAG_CACHE);
if (! ListFindNamedItem(Visited, p_Path))
{
ListAddNamedItem(Visited, p_Path, NULL);
switch (ConsiderItem(Ctx, p_Path, Stat))
{
case CTX_EXCLUDE:
case CTX_ONE_FS:
result=IGNORE;
break;
case CTX_RECURSE:
result=HashratRecurse(Ctx, p_Path, &HashStr);
break;
case CTX_HASH_AND_RECURSE:
result=HashratAction(Ctx, p_Path, Stat);
result=HashratRecurse(Ctx, p_Path, &HashStr);
break;
default:
result=HashratAction(Ctx, p_Path, Stat);
break;
}
}
if (IsTopLevel)
{
ListClear(Visited, NULL);
}
Destroy(HashStr);
return(result);
}