forked from ColumPaget/Hashrat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
227 lines (187 loc) · 4.81 KB
/
main.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
#include "common.h"
#include "ssh.h"
#include "fingerprint.h"
#include "find.h"
#include "files.h"
#include "memcached.h"
#include "check-hash.h"
#include "command-line-args.h"
#include "filesigning.h"
#include "cgi.h"
void HashFromListFile(HashratCtx *Ctx)
{
char *Tempstr=NULL, *HashStr=NULL;
STREAM *S;
struct stat Stat;
if (strcmp(Ctx->ListPath,"-")==0) S=STREAMFromFD(0);
else S=STREAMOpenFile(Ctx->ListPath,SF_RDONLY);
Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (StatFile(Ctx,Tempstr,&Stat)==0)
{
//HashItem returns '0' on success
if (HashItem(Ctx, Ctx->HashType, Tempstr, &Stat, &HashStr) ==0)
{
HashratOutputInfo(Ctx, Ctx->Out, Tempstr, &Stat, HashStr);
HashratStoreHash(Ctx, Tempstr, &Stat, HashStr);
}
else fprintf(stderr,"ERROR: Failed to open file %s\n",Tempstr);
}
else fprintf(stderr,"ERROR: Failed to open file %s\n",Tempstr);
Tempstr=STREAMReadLine(Tempstr, S);
}
STREAMClose(S);
DestroyString(Tempstr);
DestroyString(HashStr);
}
void HashStdIn(HashratCtx *Ctx)
{
STREAM *In=NULL;
char *Tempstr=NULL, *Hash=NULL;
if (Flags & FLAG_LINEMODE)
{
In=STREAMFromFD(0);
STREAMSetTimeout(In,0);
if (Flags & FLAG_HIDE_INPUT) Tempstr=TTYReadSecret(Tempstr, In, 0);
else if (Flags & FLAG_STAR_INPUT) Tempstr=TTYReadSecret(Tempstr, In, TEXT_STARS);
else Tempstr=STREAMReadLine(Tempstr,In);
while (Tempstr)
{
if (! (Flags & FLAG_RAW)) StripTrailingWhitespace(Tempstr);
Hash=CopyStr(Hash,"");
ProcessData(&Hash, Ctx, Tempstr, StrLen(Tempstr));
STREAMWriteString(Hash,Ctx->Out); STREAMWriteString("\n",Ctx->Out);
if (Flags & FLAG_HIDE_INPUT) Tempstr=TTYReadSecret(Tempstr, In, 0);
else if (Flags & FLAG_STAR_INPUT) Tempstr=TTYReadSecret(Tempstr, In, TEXT_STARS);
else Tempstr=STREAMReadLine(Tempstr,In);
}
}
else
{
HashratHashSingleFile(Ctx, Ctx->HashType, 0, "-", NULL, &Hash);
STREAMWriteString(Hash,Ctx->Out); STREAMWriteString("\n",Ctx->Out);
}
STREAMDisassociateFromFD(In);
DestroyString(Tempstr);
DestroyString(Hash);
}
int ProcessCommandLine(HashratCtx *Ctx, int argc, char *argv[])
{
struct stat Stat;
int i, count=0;
for (i=1; i < argc; i++)
{
if (StrLen(argv[i]))
{
if (StatFile(Ctx, argv[i],&Stat)==0)
{
ProcessItem(Ctx, argv[i], &Stat);
}
else fprintf(stderr,"ERROR: File '%s' not found\n",argv[i]);
count++;
}
}
return(count);
}
main(int argc, char *argv[])
{
char *Tempstr=NULL, *ptr;
int i, result=FALSE, count=0;
struct stat Stat;
HashratCtx *Ctx;
Tempstr=SetStrLen(Tempstr, HOST_NAME_MAX);
gethostname(Tempstr, HOST_NAME_MAX);
LocalHost=CopyStr(LocalHost, Tempstr);
Tempstr=SetStrLen(Tempstr, HOST_NAME_MAX);
getdomainname(Tempstr, HOST_NAME_MAX);
LocalHost=MCatStr(LocalHost, ".", Tempstr, NULL);
memset(&Stat,0,sizeof(struct stat)); //to keep valgrind happy
Ctx=CommandLineParseArgs(argc,argv);
if (Ctx)
{
MemcachedConnect(GetVar(Ctx->Vars, "Memcached:Server"));
switch (Ctx->Action)
{
case ACT_HASH:
count=ProcessCommandLine(Ctx, argc, argv);
//if we didn't find anything on the command-line then read from stdin
if (count==0) HashStdIn(Ctx);
break;
case ACT_CHECK:
result=CheckHashesFromList(Ctx);
break;
case ACT_CHECK_XATTR:
ProcessCommandLine(Ctx, argc, argv);
break;
case ACT_CHECK_MEMCACHED:
ProcessCommandLine(Ctx, argc, argv);
break;
case ACT_CGI:
CGIDisplayPage();
break;
case ACT_PRINTUSAGE:
CommandLinePrintUsage();
break;
case ACT_HASH_LISTFILE:
HashFromListFile(Ctx);
break;
case ACT_SIGN:
Ctx->Encoding = ENCODE_BASE64;
for (i=1; i < argc; i++)
{
if (StrLen(argv[i]))
{
if (StatFile(Ctx, argv[i],&Stat)==0)
{
if (S_ISLNK(Stat.st_mode)) fprintf(stderr,"WARN: Not following symbolic link %s\n",argv[i]);
else HashratSignFile(argv[i],Ctx);
}
else fprintf(stderr,"ERROR: File '%s' not found\n",argv[i]);
count++;
}
}
break;
case ACT_CHECKSIGN:
Ctx->Encoding |= ENCODE_BASE64;
for (i=1; i < argc; i++)
{
if (StrLen(argv[i]))
{
if (StatFile(Ctx, argv[i],&Stat)==0)
{
if (S_ISLNK(Stat.st_mode)) fprintf(stderr,"WARN: Not following symbolic link %s\n",argv[i]);
else HashratCheckSignedFile(argv[i], Ctx);
}
else fprintf(stderr,"ERROR: File '%s' not found\n",argv[i]);
count++;
}
}
break;
case ACT_LOADMATCHES:
ptr=GetVar(Ctx->Vars, "Memcached:Server");
if (StrLen(ptr) ==0) printf("ERROR: No memcached server specified. Use the -memcached <server> command-line argument to specify one\n");
else MatchesLoad(FLAG_MEMCACHED);
break;
case ACT_FINDMATCHES:
if (! MatchesLoad(0))
{
printf("No hashes supplied on stdin.\n");
exit(1);
}
ProcessCommandLine(Ctx, argc, argv);
break;
case ACT_FINDMATCHES_MEMCACHED:
case ACT_FINDDUPLICATES:
ProcessCommandLine(Ctx, argc, argv);
break;
}
fflush(NULL);
if (Ctx->Out) STREAMClose(Ctx->Out);
if (Ctx->Aux) STREAMClose(Ctx->Aux);
}
DestroyString(Tempstr);
if (result) exit(0);
exit(1);
}