-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.c
294 lines (245 loc) · 7.72 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
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
#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"
#include "otp.h"
#include "output.h"
#include "xdialog.h"
int HashFromListFile(HashratCtx *Ctx)
{
char *Tempstr=NULL, *HashStr=NULL;
const char *ptr;
STREAM *S;
struct stat Stat;
int result=FALSE;
ptr=GetVar(Ctx->Vars, "ItemsListSource");
if (
(! StrValid(ptr)) ||
(strcmp(ptr,"-")==0)
) S=STREAMFromFD(0);
else S=STREAMOpen(ptr,"r");
//if we managed to open the list, return TRUE
if (S) result=TRUE;
Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (StatFile(Ctx,Tempstr,&Stat) != -1)
{
HashStartTime=GetTime(TIME_MILLISECS);
if (HashItem(Ctx, Ctx->HashType, Tempstr, &Stat, &HashStr))
{
HashratOutputFileInfo(Ctx, Ctx->Out, Tempstr, &Stat, HashStr);
HashratStoreHash(Ctx, Tempstr, &Stat, HashStr);
}
else
{
fprintf(stderr,"ERROR: Failed to open file %s\n",Tempstr);
result=FALSE;
}
}
else
{
fprintf(stderr,"ERROR: Failed to open file %s\n",Tempstr);
result=FALSE;
}
Tempstr=STREAMReadLine(Tempstr, S);
}
STREAMClose(S);
Destroy(Tempstr);
Destroy(HashStr);
return(result);
}
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=TerminalReadText(Tempstr, TERM_HIDETEXT, In);
else if (Flags & FLAG_STAR_INPUT) Tempstr=TerminalReadText(Tempstr, TERM_SHOWSTARS, In);
else Tempstr=STREAMReadLine(Tempstr,In);
while (Tempstr)
{
if (! (Flags & FLAG_RAW)) StripTrailingWhitespace(Tempstr);
Hash=CopyStr(Hash,"");
ProcessData(&Hash, Ctx, Tempstr, StrLen(Tempstr));
OutputHash(Ctx, Hash, "");
if (Flags & FLAG_HIDE_INPUT) Tempstr=TerminalReadText(Tempstr, TERM_HIDETEXT, In);
else if (Flags & FLAG_STAR_INPUT) Tempstr=TerminalReadText(Tempstr, TERM_SHOWSTARS, In);
else Tempstr=STREAMReadLine(Tempstr,In);
}
}
else
{
HashratHashSingleFile(Ctx, Ctx->HashType, 0, "-", NULL, &Hash);
OutputHash(Ctx, Hash, "");
}
STREAMDestroy(In);
Destroy(Tempstr);
Destroy(Hash);
}
int ProcessTargetItems(HashratCtx *Ctx)
{
struct stat Stat;
int result=IGNORE, type;
char *Item=NULL;
const char *ptr;
ptr=GetToken(Ctx->Targets, ",", &Item, GETTOKEN_QUOTES);
while(ptr)
{
type=StatFile(Ctx, Item, &Stat);
if (type > -1) result=ProcessItem(Ctx, Item, &Stat, TRUE);
else
{
if (result==IGNORE) result=0;
fprintf(stderr,"ERROR: File '%s' not found\n", Item);
}
ptr=GetToken(ptr, ",", &Item, GETTOKEN_QUOTES);
}
DestroyString(Item);
return(result);
}
int main(int argc, char *argv[])
{
char *Tempstr=NULL;
const char *ptr;
int i, result=FALSE;
struct stat Stat;
HashratCtx *Ctx;
time(&Now);
Tempstr=SetStrLen(Tempstr, HOST_NAME_MAX);
gethostname(Tempstr, HOST_NAME_MAX);
LocalHost=CopyStr(LocalHost, Tempstr);
Tempstr=SetStrLen(Tempstr, HOST_NAME_MAX);
result=getdomainname(Tempstr, HOST_NAME_MAX);
if (result > -1) Tempstr[result]='\0';
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_LIST_TYPES:
Tempstr=HashAvailableTypes(Tempstr);
strrep(Tempstr, ',', '\n');
printf("%s\n", Tempstr);
break;
case ACT_HASH:
result=ProcessTargetItems(Ctx);
//if we didn't find anything on the command-line then read from stdin
if (result==IGNORE) HashStdIn(Ctx);
break;
case ACT_HASHDIR:
result=ProcessTargetItems(Ctx);
break;
case ACT_CHECK_LIST:
result=CheckHashesFromList(Ctx);
break;
case ACT_HASH_LISTFILE:
HashFromListFile(Ctx);
break;
case ACT_CHECK:
case ACT_FINDMATCHES:
if (! MatchesLoad(Ctx, 0))
{
printf("No hashes supplied on stdin.\n");
exit(1);
}
result=ProcessTargetItems(Ctx);
if (Ctx->Action==ACT_CHECK) OutputUnmatched(Ctx);
break;
case ACT_OTP:
OTPProcess(Ctx);
break;
//Load matches to an mcached server
case ACT_LOADMATCHES:
ptr=GetVar(Ctx->Vars, "Memcached:Server");
if (StrEnd(ptr)) printf("ERROR: No memcached server specified. Use the -memcached <server> command-line argument to specify one\n");
else MatchesLoad(Ctx, FLAG_MEMCACHED);
break;
case ACT_FINDMATCHES_MEMCACHED:
case ACT_FINDDUPLICATES:
case ACT_CHECK_XATTR:
case ACT_CHECK_MEMCACHED:
case ACT_RENAME:
result=ProcessTargetItems(Ctx);
break;
case ACT_CGI:
CGIDisplayPage();
break;
case ACT_XDIALOG:
XDialogFrontend(Ctx);
break;
case ACT_PRINTUSAGE:
CommandLinePrintUsage();
break;
case ACT_SIGN:
Ctx->Encoding = ENCODE_BASE64;
for (i=1; i < argc; i++)
{
if (StrValid(argv[i]))
{
if (StatFile(Ctx, argv[i],&Stat) != -1)
{
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]);
}
}
break;
case ACT_CHECKSIGN:
Ctx->Encoding |= ENCODE_BASE64;
for (i=1; i < argc; i++)
{
if (StrValid(argv[i]))
{
if (StatFile(Ctx, argv[i],&Stat) != -1)
{
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]);
}
}
break;
}
fflush(NULL);
if (Ctx->Out) STREAMClose(Ctx->Out);
if (Ctx->Aux) STREAMClose(Ctx->Aux);
switch (Ctx->Action)
{
case ACT_HASH:
case ACT_HASHDIR:
case ACT_HASH_LISTFILE:
case ACT_FINDMATCHES:
case ACT_FINDMATCHES_MEMCACHED:
case ACT_FINDDUPLICATES:
//result==TRUE for these means 'yes we found something' or 'yes that worked'
if (result==TRUE) exit(0);
else exit(1);
break;
case ACT_CHECK:
case ACT_CHECK_LIST:
case ACT_CHECK_MEMCACHED:
case ACT_CHECK_XATTR:
//result==TRUE for these means 'CHECK FAILED'
if (result==TRUE) exit(1);
else exit(0);
break;
}
}
Destroy(Tempstr);
exit(1);
}