File: check-hash.c

package info (click to toggle)
hashrat 1.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,984 kB
  • sloc: ansic: 21,832; sh: 369; makefile: 187
file content (156 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download
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
#include "check-hash.h"
#include "fingerprint.h"
#include "files.h"


void HandleCheckFail(const char *Path, const char *ErrorMessage)
{
char *Tempstr=NULL;

  if (Flags & FLAG_COLOR) printf("%s%s: FAILED. '%s'.%s\n",ANSICode(ANSI_RED, 0, 0),Path, ErrorMessage, ANSI_NORM);
	else printf("%s: FAILED. %s.\n",Path,ErrorMessage);

	RunHookScript(DiffHook, Path, "");
  Destroy(Tempstr);
}



int CheckStat(const char *Path, struct stat *ExpectedStat, struct stat *Stat)
{

	if ((ExpectedStat->st_size > 0) && ((size_t) Stat->st_size != (size_t) ExpectedStat->st_size)) 
	{
		HandleCheckFail(Path, "Filesize changed");
		return(FALSE);
	}

	if ((Flags & FLAG_FULLCHECK) && (Stat->st_ino != ExpectedStat->st_ino))
	{
		HandleCheckFail(Path, "Inode changed");
		return(FALSE);
	}

	if ((Flags & FLAG_FULLCHECK) && (Stat->st_uid != ExpectedStat->st_uid))
	{
		HandleCheckFail(Path, "Owner changed");
		return(FALSE);
	}

	if ((Flags & FLAG_FULLCHECK) && (Stat->st_gid != ExpectedStat->st_gid))
	{
		HandleCheckFail(Path, "Group changed");
		return(FALSE);
	}

	if ((Flags & FLAG_FULLCHECK) && (Stat->st_mode != ExpectedStat->st_mode))
	{
		HandleCheckFail(Path, "Mode changed");
		return(FALSE);
	}

	if ((Flags & FLAG_FULLCHECK) && (Stat->st_mtime != ExpectedStat->st_mtime))
	{
		HandleCheckFail(Path, "MTime changed");
		return(FALSE);
	}

		return(TRUE);
}


//FP here is the expected fingerprint of the file, ActualHash and Path are
// its actual path and hash value

int CheckFileHash(HashratCtx *Ctx, const char *Path, struct stat *Stat, const char *ActualHash, TFingerprint *FP) 
{
int result=FALSE;

	if (strcasecmp(FP->Hash,ActualHash) != 0) HandleCheckFail(Path, "Hash mismatch");
	else
	{
		if (! (Flags & FLAG_OUTPUT_FAILS))
		{
		if (Flags & FLAG_COLOR) printf("%s%s: OKAY%s\n",ANSICode(ANSI_GREEN, 0, 0), Path, ANSI_NORM);
		else printf("%s: OKAY\n",Path);
		}
		result=TRUE;
	}

	if (Flags & FLAG_UPDATE) HashratStoreHash(Ctx, Path, Stat, ActualHash);

return(result);
}


int HashratCheckFile(HashratCtx *Ctx, const char *Path, struct stat *ActualStat, const char *ActualHash, TFingerprint *FP)
{
int result=FALSE;
char *Tempstr=NULL;


		if (access(Path,F_OK)!=0) 
		{
			//fprintf(stderr,"\rERROR: No such file '%s'\n",Path);
			Tempstr=MCopyStr(Tempstr,"No such file [",Path,"]",NULL);
			HandleCheckFail(Path, Tempstr);
		}
		else if (strcasecmp(FP->Path, Path) != 0) 
		{
				Tempstr=MCopyStr(Tempstr,"Moved [",FP->Path,"] to [",Path,"]",NULL);
				HandleCheckFail(Path, Tempstr);
		}
		else
		{
			if (FP->Flags & FP_HASSTAT) 
			{
				result=CheckStat(Path, &FP->FStat, ActualStat);
				if (result) result=CheckFileHash(Ctx, Path, ActualStat, ActualHash, FP); 
			}
			else result=CheckFileHash(Ctx, Path, ActualStat, ActualHash, FP); 
		}

Destroy(Tempstr);
return(result);
}


//returns true on a significant event, meaning on FAIL
int CheckHashesFromList(HashratCtx *Ctx)
{
char *HashStr=NULL;
const char *ptr;
int Checked=0, Errors=0;
STREAM *ListStream;
TFingerprint *FP;
struct stat Stat;


ptr=GetVar(Ctx->Vars,"Path");
if (strcmp(ptr,"-")==0)
{
  ListStream=STREAMFromFD(0);
  STREAMSetTimeout(ListStream,0);
}
else ListStream=STREAMOpen(ptr, "r");

FP=FingerprintRead(ListStream);
while (FP)
{
  if (StrLen(FP->HashType)) Ctx->HashType=CopyStr(Ctx->HashType, FP->HashType);
	if (StatFile(Ctx,FP->Path,&Stat) != -1) HashItem(Ctx, Ctx->HashType, FP->Path, &Stat, &HashStr);

  if (! HashratCheckFile(Ctx, FP->Path, &Stat, HashStr, FP)) Errors++;
  TFingerprintDestroy(FP);
  FP=FingerprintRead(ListStream);
  Checked++;
}

fprintf(stderr,"\nChecked %d files. %d Failures\n",Checked,Errors);

Destroy(HashStr);

if (Errors) return(TRUE);
return(FALSE);
}