-
Notifications
You must be signed in to change notification settings - Fork 21
/
scanner.c
292 lines (252 loc) · 7.78 KB
/
scanner.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
/*
* CNTLM is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 David Kubicek
*
*/
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <fnmatch.h>
#include "utils.h"
#include "socket.h"
#include "http.h"
#include "globals.h"
#include "forward.h"
#include "scanner.h"
/*
* This code is a piece of shit, but it works. Cannot rewrite it now, because
* I don't have ISA AV filter anymore - wouldn't be able to test it.
*/
int scanner_hook(rr_data_t request, rr_data_t response, struct auth_s *credentials, int cd, int *sd, long maxKBs) {
char *buf, *line, *pos, *tmp, *pat, *post, *isaid, *uurl;
int bsize, lsize, size, len, i, w, nc;
rr_data_t newreq, newres;
plist_t list;
int ok = 1;
int done = 0;
int headers_initiated = 0;
long c, progress = 0, filesize = 0;
/*
* Let's limit the responses we examine to an absolute minimum
*/
if (!request->req || response->code != 200
|| http_has_body(request, response) != -1
|| hlist_subcmp(response->headers, "Transfer-Encoding", "chunked")
|| !hlist_subcmp(response->headers, "Proxy-Connection", "close"))
return PLUG_SENDHEAD | PLUG_SENDDATA;
tmp = hlist_get(request->headers, "User-Agent");
if (tmp) {
tmp = lowercase(strdup(tmp));
list = scanner_agent_list;
while (list) {
pat = lowercase(strdup(list->aux));
if (debug)
printf("scanner_hook: matching U-A header (%s) to %s\n", tmp, pat);
if (!fnmatch(pat, tmp, 0)) {
if (debug)
printf("scanner_hook: positive match!\n");
maxKBs = 0;
free(pat);
break;
}
free(pat);
list = list->next;
}
free(tmp);
}
bsize = SAMPLE;
buf = new(bsize);
len = 0;
do {
size = read(*sd, buf + len, SAMPLE - len - 1);
if (debug)
printf("scanner_hook: read %d of %d\n", size, SAMPLE - len);
if (size > 0)
len += size;
} while (size > 0 && len < SAMPLE - 1);
if (strstr(buf, "<title>Downloading status</title>") && (pos=strstr(buf, "ISAServerUniqueID=")) && (pos = strchr(pos, '"'))) {
pos++;
c = strlen(pos);
for (i = 0; i < c && pos[i] != '"'; ++i);
if (pos[i] == '"') {
isaid = substr(pos, 0, i);
if (debug)
printf("scanner_hook: ISA id = %s\n", isaid);
lsize = BUFSIZE;
line = new(lsize);
do {
i = so_recvln(*sd, &line, &lsize);
c = strlen(line);
if (len + c >= bsize) {
bsize *= 2;
tmp = realloc(buf, bsize);
if (tmp == NULL)
break;
else
buf = tmp;
}
strcat(buf, line);
len += c;
if (i >= 0 && (
((pos = strstr(line, "UpdatePage("))
&& isdigit(pos[11]))
||
((pos = strstr(line, "DownloadFinished("))
&& isdigit(pos[17])
&& (done = 1)) )) {
if (debug)
printf("scanner_hook: %s", line);
if ((pos = strstr(line, "To be downloaded"))) {
filesize = atol(pos+16);
if (debug) {
if (filesize > 0) {
printf("scanner_hook: file size detected: %ld KiBs (max: %ld)\n", filesize/1024, maxKBs);
} else {
printf("scanner_hook: file size unknown -- quitting\n");
break;
}
}
if (maxKBs && (maxKBs == 1 || filesize/1024 > maxKBs))
break;
/*
* We have to send HTTP protocol ID so we can send the notification
* headers during downloading. Once we've done that, it cannot appear
* again, which it would if we returned PLUG_SENDHEAD, so we must
* remember to not include it.
*/
headers_initiated = 1;
tmp = new(MINIBUF_SIZE);
snprintf(tmp, MINIBUF_SIZE, "%s 200 OK\r\n", request->http);
w = write(cd, tmp, strlen(tmp));
free(tmp);
}
if (!headers_initiated) {
if (debug)
printf("scanner_hook: Giving up, \"To be downloaded\" line not found!\n");
break;
}
/*
* Send a notification header to the client, just so it doesn't timeout
*/
if (!done) {
tmp = new(MINIBUF_SIZE);
progress = atol(line+12);
snprintf(tmp, MINIBUF_SIZE, "ISA-Scanner: %ld of %ld\r\n", progress, filesize);
w = write(cd, tmp, strlen(tmp));
free(tmp);
}
/*
* If download size is unknown beforehand, stop when downloaded amount is over ISAScannerSize
*/
if (!filesize && maxKBs && maxKBs != 1 && progress/1024 > maxKBs)
break;
}
} while (i > 0 && !done);
if (i >= 0 && done && (pos = strstr(line, "\",\"")+3) && (c = strchr(pos, '"') - pos) > 0) {
tmp = substr(pos, 0, c);
pos = urlencode(tmp);
free(tmp);
uurl = urlencode(request->url);
post = new(BUFSIZE);
snprintf(post, BUFSIZE-1, "%surl=%s&%sSaveToDisk=YES&%sOrig=%s", isaid, pos, isaid, isaid, uurl);
if (debug)
printf("scanner_hook: Getting file with URL data = %s\n", request->url);
tmp = new(MINIBUF_SIZE);
snprintf(tmp, MINIBUF_SIZE, "%d", (int)strlen(post));
newres = new_rr_data();
newreq = dup_rr_data(request);
free(newreq->method);
newreq->method = strdup("POST");
hlist_mod(newreq->headers, "Referer", request->url, 1);
hlist_mod(newreq->headers, "Content-Type", "application/x-www-form-urlencoded", 1);
hlist_mod(newreq->headers, "Content-Length", tmp, 1);
free(tmp);
nc = proxy_connect(credentials);
c = proxy_authenticate(&nc, newreq, newres, credentials);
if (c && newres->code == 407) {
if (debug)
printf("scanner_hook: Authentication OK, getting the file...\n");
} else {
if (debug)
printf("scanner_hook: Authentication failed or refused!\n");
close(nc);
nc = 0;
}
/*
* The POST request for the real file
*/
reset_rr_data(newres);
if (nc && headers_send(nc, newreq) && write(nc, post, strlen(post)) && headers_recv(nc, newres)) {
if (debug)
hlist_dump(newres->headers);
/*
* We always know the filesize here. Send it to the client, because ISA doesn't!!!
* The clients progress bar doesn't work without it and it stinks!
*/
if (filesize || progress) {
tmp = new(20);
snprintf(tmp, 20, "%ld", filesize ? filesize : progress);
newres->headers = hlist_mod(newres->headers, "Content-Length", tmp, 1);
}
/*
* Here we remember if previous code already sent some headers
* to the client. In such case, do not include the HTTP/1.x ID.
*/
newres->skip_http = headers_initiated;
copy_rr_data(response, newres);
close(*sd);
*sd = nc;
len = 0;
ok = PLUG_SENDHEAD | PLUG_SENDDATA;
} else if (debug)
printf("scanner_hook: New request failed\n");
free(newreq);
free(newres);
free(post);
free(uurl);
}
free(line);
free(isaid);
} else if (debug)
printf("scanner_hook: ISA id not found\n");
}
if (len) {
if (debug) {
printf("scanner_hook: flushing %d original bytes\n", len);
hlist_dump(response->headers);
}
if (!headers_send(cd, response)) {
if (debug)
printf("scanner_hook: failed to send headers\n");
free(buf);
return PLUG_ERROR;
}
size = write(cd, buf, len);
if (size > 0)
ok = PLUG_SENDDATA;
else
ok = PLUG_ERROR;
}
if (debug)
printf("scanner_hook: ending with %d\n", ok);
free(buf);
return ok;
}