Commit | Line | Data |
---|---|---|
d3372da9 | 1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
302a2600 | 3 | * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com> |
d3372da9 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <time.h> | |
22 | #include <unistd.h> | |
23 | #include <fcntl.h> | |
24 | #include <signal.h> | |
25 | #include <pwd.h> | |
26 | #include <grp.h> | |
27 | #include <errno.h> | |
28 | #include <sys/wait.h> | |
f9455250 | 29 | #include <stdint.h> |
d3372da9 | 30 | |
31 | #ifdef HAVE_CONFIG_H | |
32 | #include <config.h> | |
33 | #endif | |
34 | #include "log.h" | |
35 | #include "utils.h" | |
36 | #include "sysevents.h" | |
37 | #include "auth.h" | |
38 | #include "transfer.h" | |
39 | #include "module.h" | |
429d35e5 | 40 | #include "client.h" |
d3372da9 | 41 | |
42 | static void killfilter(struct transfer *transfer); | |
43 | ||
7a5e9d41 | 44 | unsigned long long bytesupload = 0; |
45 | unsigned long long bytesdownload = 0; | |
d3372da9 | 46 | struct transfer *transfers = NULL; |
47 | int numtransfers = 0; | |
48 | GCBCHAIN(newtransfercb, struct transfer *); | |
49 | ||
50 | void freetransfer(struct transfer *transfer) | |
51 | { | |
d3372da9 | 52 | if(transfer == transfers) |
53 | transfers = transfer->next; | |
54 | if(transfer->next != NULL) | |
55 | transfer->next->prev = transfer->prev; | |
56 | if(transfer->prev != NULL) | |
57 | transfer->prev->next = transfer->next; | |
58 | CBCHAINDOCB(transfer, trans_destroy, transfer); | |
59 | CBCHAINFREE(transfer, trans_ac); | |
60 | CBCHAINFREE(transfer, trans_act); | |
61 | CBCHAINFREE(transfer, trans_p); | |
62 | CBCHAINFREE(transfer, trans_destroy); | |
63 | CBCHAINFREE(transfer, trans_filterout); | |
9ec790e8 | 64 | while(transfer->args != NULL) |
65 | freewcspair(transfer->args, &transfer->args); | |
d3372da9 | 66 | if(transfer->filter != -1) |
67 | killfilter(transfer); | |
68 | if(transfer->etimer != NULL) | |
69 | canceltimer(transfer->etimer); | |
70 | if(transfer->auth != NULL) | |
71 | authputhandle(transfer->auth); | |
72 | if(transfer->peerid != NULL) | |
73 | free(transfer->peerid); | |
74 | if(transfer->peernick != NULL) | |
75 | free(transfer->peernick); | |
76 | if(transfer->path != NULL) | |
77 | free(transfer->path); | |
78 | if(transfer->actdesc != NULL) | |
79 | free(transfer->actdesc); | |
80 | if(transfer->filterbuf != NULL) | |
81 | free(transfer->filterbuf); | |
429d35e5 | 82 | if(transfer->hash != NULL) |
83 | freehash(transfer->hash); | |
0309b83d | 84 | if(transfer->exitstatus != NULL) |
85 | free(transfer->exitstatus); | |
d3372da9 | 86 | if(transfer->localend != NULL) |
87 | { | |
cab0b442 | 88 | transfer->localend->readcb = NULL; |
89 | transfer->localend->writecb = NULL; | |
90 | transfer->localend->errcb = NULL; | |
d3372da9 | 91 | putsock(transfer->localend); |
92 | } | |
93 | if(transfer->filterout != NULL) | |
94 | { | |
cab0b442 | 95 | transfer->filterout->readcb = NULL; |
96 | transfer->filterout->writecb = NULL; | |
97 | transfer->filterout->errcb = NULL; | |
d3372da9 | 98 | putsock(transfer->filterout); |
99 | } | |
100 | if(transfer->fn != NULL) | |
101 | putfnetnode(transfer->fn); | |
102 | free(transfer); | |
103 | numtransfers--; | |
104 | } | |
105 | ||
106 | struct transfer *newtransfer(void) | |
107 | { | |
108 | struct transfer *new; | |
109 | static int curid = 0; | |
110 | ||
111 | new = smalloc(sizeof(*new)); | |
112 | memset(new, 0, sizeof(*new)); | |
113 | new->id = curid++; | |
114 | new->size = -1; | |
115 | new->endpos = -1; | |
116 | new->filter = -1; | |
117 | CBCHAININIT(new, trans_ac); | |
118 | CBCHAININIT(new, trans_act); | |
119 | CBCHAININIT(new, trans_p); | |
120 | CBCHAININIT(new, trans_destroy); | |
121 | CBCHAININIT(new, trans_filterout); | |
122 | new->next = NULL; | |
123 | new->prev = NULL; | |
124 | time(&new->activity); | |
125 | numtransfers++; | |
126 | return(new); | |
127 | } | |
128 | ||
1ce5968e FT |
129 | static void localread(struct socket *sk, struct transfer *transfer) |
130 | { | |
131 | void *buf; | |
132 | size_t blen; | |
133 | ||
134 | if((transfer->datapipe != NULL) && (sockqueueleft(transfer->datapipe) > 0)) { | |
135 | buf = sockgetinbuf(sk, &blen); | |
bd5d862e FT |
136 | if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos)) |
137 | blen = transfer->endpos - transfer->curpos; | |
1ce5968e | 138 | sockqueue(transfer->datapipe, buf, blen); |
6bd790e0 | 139 | free(buf); |
1ff9e8ea FT |
140 | time(&transfer->activity); |
141 | transfer->curpos += blen; | |
142 | bytesupload += blen; | |
143 | CBCHAINDOCB(transfer, trans_p, transfer); | |
1ce5968e FT |
144 | } |
145 | } | |
146 | ||
147 | static void dataread(struct socket *sk, struct transfer *transfer) | |
148 | { | |
149 | void *buf; | |
150 | size_t blen; | |
151 | ||
152 | if((transfer->localend != NULL) && (sockqueueleft(transfer->localend) > 0)) { | |
153 | buf = sockgetinbuf(sk, &blen); | |
bd5d862e FT |
154 | if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos)) |
155 | blen = transfer->endpos - transfer->curpos; | |
1ce5968e | 156 | sockqueue(transfer->localend, buf, blen); |
6bd790e0 | 157 | free(buf); |
1ff9e8ea FT |
158 | transfer->curpos += blen; |
159 | bytesdownload += blen; | |
160 | CBCHAINDOCB(transfer, trans_p, transfer); | |
1ce5968e FT |
161 | } |
162 | } | |
163 | ||
164 | static void localwrite(struct socket *sk, struct transfer *transfer) | |
165 | { | |
166 | if(transfer->datapipe != NULL) | |
167 | dataread(transfer->datapipe, transfer); | |
168 | } | |
169 | ||
170 | static void datawrite(struct socket *sk, struct transfer *transfer) | |
171 | { | |
172 | if(transfer->localend != NULL) | |
173 | localread(transfer->localend, transfer); | |
174 | } | |
175 | ||
176 | static void localerr(struct socket *sk, int errno, struct transfer *transfer) | |
177 | { | |
178 | if(transfer->datapipe != NULL) | |
179 | closesock(transfer->datapipe); | |
180 | } | |
181 | ||
182 | static void dataerr(struct socket *sk, int errno, struct transfer *transfer) | |
183 | { | |
1ff9e8ea FT |
184 | if(transfer->curpos >= transfer->size) { |
185 | transfersetstate(transfer, TRNS_DONE); | |
bd5d862e FT |
186 | if(transfer->localend != NULL) { |
187 | closesock(transfer->localend); | |
188 | quitsock(transfer->localend); | |
189 | transfer->localend = NULL; | |
190 | } | |
1ff9e8ea | 191 | } else { |
b38037ff FT |
192 | if(transfer->dir == TRNSD_DOWN) |
193 | resettransfer(transfer); | |
194 | else if(transfer->dir == TRNSD_UP) | |
195 | transfer->close = 1; | |
1ff9e8ea | 196 | } |
1ce5968e FT |
197 | } |
198 | ||
705b7486 | 199 | void transferattach(struct transfer *transfer, struct socket *dpipe) |
d3372da9 | 200 | { |
705b7486 FT |
201 | transferdetach(transfer); |
202 | getsock(transfer->datapipe = dpipe); | |
1ce5968e FT |
203 | dpipe->readcb = (void (*)(struct socket *, void *))dataread; |
204 | dpipe->writecb = (void (*)(struct socket *, void *))datawrite; | |
205 | dpipe->errcb = (void (*)(struct socket *, int, void *))dataerr; | |
206 | dpipe->data = transfer; | |
d3372da9 | 207 | } |
208 | ||
209 | void transferdetach(struct transfer *transfer) | |
210 | { | |
705b7486 FT |
211 | if(transfer->datapipe != NULL) { |
212 | closesock(transfer->datapipe); | |
1ff9e8ea | 213 | quitsock(transfer->datapipe); |
d3372da9 | 214 | } |
705b7486 | 215 | transfer->datapipe = NULL; |
d3372da9 | 216 | } |
217 | ||
2c086721 | 218 | struct transfer *finddownload(wchar_t *peerid) |
219 | { | |
220 | struct transfer *transfer; | |
221 | ||
222 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
223 | { | |
705b7486 | 224 | if((transfer->dir == TRNSD_DOWN) && (transfer->datapipe == NULL) && !wcscmp(peerid, transfer->peerid)) |
2c086721 | 225 | break; |
226 | } | |
227 | return(transfer); | |
228 | } | |
229 | ||
6c6bc9ce | 230 | struct transfer *hasupload(struct fnet *fnet, wchar_t *peerid) |
231 | { | |
232 | struct transfer *transfer; | |
233 | ||
234 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
235 | { | |
c458597e | 236 | if((transfer->dir == TRNSD_UP) && (transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid)) |
6c6bc9ce | 237 | break; |
238 | } | |
239 | return(transfer); | |
240 | } | |
241 | ||
705b7486 | 242 | struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct socket *dpipe) |
d3372da9 | 243 | { |
244 | struct transfer *transfer; | |
245 | ||
246 | transfer = newtransfer(); | |
247 | if(fnet != NULL) | |
248 | transfer->fnet = fnet; | |
249 | else | |
250 | transfer->fnet = fn->fnet; | |
251 | transfer->peerid = swcsdup(nickid); | |
252 | transfer->state = TRNS_HS; | |
253 | transfer->dir = TRNSD_UP; | |
254 | if(fn != NULL) | |
255 | getfnetnode(transfer->fn = fn); | |
705b7486 | 256 | transferattach(transfer, dpipe); |
d3372da9 | 257 | linktransfer(transfer); |
258 | bumptransfer(transfer); | |
259 | return(transfer); | |
260 | } | |
261 | ||
262 | void linktransfer(struct transfer *transfer) | |
263 | { | |
264 | transfer->next = transfers; | |
265 | transfer->prev = NULL; | |
266 | if(transfers != NULL) | |
267 | transfers->prev = transfer; | |
268 | transfers = transfer; | |
269 | GCBCHAINDOCB(newtransfercb, transfer); | |
270 | } | |
271 | ||
272 | void resettransfer(struct transfer *transfer) | |
273 | { | |
274 | if(transfer->dir == TRNSD_DOWN) | |
275 | { | |
705b7486 | 276 | transferdetach(transfer); |
d3372da9 | 277 | killfilter(transfer); |
278 | transfersetstate(transfer, TRNS_WAITING); | |
279 | transfersetactivity(transfer, L"reset"); | |
280 | return; | |
281 | } | |
282 | } | |
283 | ||
284 | struct transfer *findtransfer(int id) | |
285 | { | |
286 | struct transfer *transfer; | |
287 | ||
288 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
289 | { | |
290 | if(transfer->id == id) | |
291 | break; | |
292 | } | |
293 | return(transfer); | |
294 | } | |
295 | ||
296 | static void transexpire(int cancelled, struct transfer *transfer) | |
297 | { | |
298 | transfer->etimer = NULL; | |
299 | if(!cancelled) | |
300 | bumptransfer(transfer); | |
301 | else | |
302 | transfer->timeout = 0; | |
303 | } | |
304 | ||
dcf7a1a2 | 305 | void transferprepul(struct transfer *transfer, off_t size, off_t start, off_t end, struct socket *lesk) |
d3372da9 | 306 | { |
307 | transfersetsize(transfer, size); | |
308 | transfer->curpos = start; | |
309 | transfer->endpos = end; | |
d3372da9 | 310 | transfersetlocalend(transfer, lesk); |
311 | } | |
312 | ||
2c086721 | 313 | void transferstartdl(struct transfer *transfer, struct socket *sk) |
314 | { | |
315 | transfersetstate(transfer, TRNS_MAIN); | |
316 | socksettos(sk, confgetint("transfer", "dltos")); | |
317 | } | |
318 | ||
d3372da9 | 319 | void transferstartul(struct transfer *transfer, struct socket *sk) |
320 | { | |
321 | transfersetstate(transfer, TRNS_MAIN); | |
322 | socksettos(sk, confgetint("transfer", "ultos")); | |
323 | if(transfer->localend != NULL) | |
1ce5968e | 324 | localread(transfer->localend, transfer); |
d3372da9 | 325 | } |
326 | ||
327 | void transfersetlocalend(struct transfer *transfer, struct socket *sk) | |
328 | { | |
329 | if(transfer->localend != NULL) | |
330 | putsock(transfer->localend); | |
331 | getsock(transfer->localend = sk); | |
cab0b442 | 332 | sk->data = transfer; |
705b7486 FT |
333 | sk->readcb = (void (*)(struct socket *, void *))localread; |
334 | sk->writecb = (void (*)(struct socket *, void *))localwrite; | |
335 | sk->errcb = (void (*)(struct socket *, int, void *))localerr; | |
d3372da9 | 336 | } |
337 | ||
18f56d13 | 338 | static int tryreq(struct transfer *transfer) |
d3372da9 | 339 | { |
340 | struct fnetnode *fn; | |
341 | struct fnetpeer *peer; | |
18f56d13 | 342 | |
343 | if((fn = transfer->fn) != NULL) | |
344 | { | |
345 | if(fn->state != FNN_EST) | |
346 | { | |
347 | transfer->close = 1; | |
348 | return(1); | |
349 | } | |
350 | peer = fnetfindpeer(fn, transfer->peerid); | |
351 | } else { | |
352 | peer = NULL; | |
353 | for(fn = fnetnodes; fn != NULL; fn = fn->next) | |
354 | { | |
355 | if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL)) | |
356 | break; | |
357 | } | |
358 | } | |
359 | if(peer != NULL) | |
6329b4ba | 360 | { |
361 | time(&transfer->lastreq); | |
18f56d13 | 362 | return(fn->fnet->reqconn(peer)); |
6329b4ba | 363 | } |
18f56d13 | 364 | return(1); |
365 | } | |
366 | ||
367 | void trytransferbypeer(struct fnet *fnet, wchar_t *peerid) | |
368 | { | |
369 | struct transfer *transfer; | |
370 | ||
371 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
372 | { | |
373 | if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING)) | |
374 | { | |
375 | if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid)) | |
376 | { | |
377 | if(!tryreq(transfer)) | |
378 | return; | |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | void bumptransfer(struct transfer *transfer) | |
385 | { | |
d3372da9 | 386 | time_t now; |
387 | ||
388 | if((now = time(NULL)) < transfer->timeout) | |
389 | { | |
18f56d13 | 390 | |
d3372da9 | 391 | if(transfer->etimer == NULL) |
392 | transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer); | |
393 | return; | |
394 | } | |
395 | if(transfer->etimer != NULL) | |
396 | canceltimer(transfer->etimer); | |
397 | switch(transfer->state) | |
398 | { | |
399 | case TRNS_WAITING: | |
d3372da9 | 400 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer); |
401 | if(now - transfer->lastreq > 30) | |
18f56d13 | 402 | tryreq(transfer); |
d3372da9 | 403 | break; |
404 | case TRNS_HS: | |
405 | if(transfer->dir == TRNSD_UP) | |
406 | { | |
407 | if(now - transfer->activity < 60) | |
408 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer); | |
409 | else | |
410 | transfer->close = 1; | |
411 | } else if(transfer->dir == TRNSD_DOWN) { | |
412 | if(now - transfer->activity < 60) | |
413 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer); | |
414 | else | |
415 | resettransfer(transfer); | |
416 | } | |
417 | break; | |
418 | case TRNS_MAIN: | |
419 | if(transfer->dir == TRNSD_UP) | |
420 | { | |
421 | if(now - transfer->activity < 300) | |
422 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer); | |
423 | else | |
424 | transfer->close = 1; | |
425 | } | |
426 | break; | |
427 | } | |
428 | } | |
429 | ||
430 | void transfersetactivity(struct transfer *transfer, wchar_t *desc) | |
431 | { | |
432 | time(&transfer->activity); | |
433 | if(desc != NULL) | |
434 | { | |
435 | if(transfer->actdesc != NULL) | |
436 | free(transfer->actdesc); | |
437 | transfer->actdesc = swcsdup(desc); | |
438 | } | |
439 | bumptransfer(transfer); | |
440 | CBCHAINDOCB(transfer, trans_act, transfer); | |
441 | } | |
442 | ||
443 | void transfersetstate(struct transfer *transfer, int newstate) | |
444 | { | |
445 | transfer->state = newstate; | |
446 | if(transfer->etimer != NULL) | |
447 | canceltimer(transfer->etimer); | |
448 | transfersetactivity(transfer, NULL); | |
449 | CBCHAINDOCB(transfer, trans_ac, transfer, L"state"); | |
450 | } | |
451 | ||
452 | void transfersetnick(struct transfer *transfer, wchar_t *newnick) | |
453 | { | |
454 | if(transfer->peernick != NULL) | |
455 | free(transfer->peernick); | |
456 | transfer->peernick = swcsdup(newnick); | |
457 | CBCHAINDOCB(transfer, trans_ac, transfer, L"nick"); | |
458 | } | |
459 | ||
dcf7a1a2 | 460 | void transfersetsize(struct transfer *transfer, off_t newsize) |
d3372da9 | 461 | { |
462 | transfer->size = newsize; | |
463 | CBCHAINDOCB(transfer, trans_ac, transfer, L"size"); | |
464 | } | |
465 | ||
466 | void transferseterror(struct transfer *transfer, int error) | |
467 | { | |
468 | transfer->error = error; | |
469 | CBCHAINDOCB(transfer, trans_ac, transfer, L"error"); | |
470 | } | |
471 | ||
472 | void transfersetpath(struct transfer *transfer, wchar_t *path) | |
473 | { | |
474 | if(transfer->path != NULL) | |
475 | free(transfer->path); | |
476 | transfer->path = swcsdup(path); | |
477 | CBCHAINDOCB(transfer, trans_ac, transfer, L"path"); | |
478 | } | |
479 | ||
2c086721 | 480 | void transfersethash(struct transfer *transfer, struct hash *hash) |
481 | { | |
482 | if(transfer->hash != NULL) | |
483 | freehash(transfer->hash); | |
484 | transfer->hash = hash; | |
485 | CBCHAINDOCB(transfer, trans_ac, transfer, L"hash"); | |
486 | } | |
487 | ||
d3372da9 | 488 | int slotsleft(void) |
489 | { | |
490 | struct transfer *transfer; | |
491 | int slots; | |
492 | ||
493 | slots = confgetint("transfer", "slots"); | |
494 | for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next) | |
495 | { | |
496 | if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot) | |
497 | slots--; | |
498 | } | |
499 | return(slots); | |
500 | } | |
501 | ||
502 | static void killfilter(struct transfer *transfer) | |
503 | { | |
504 | if(transfer->filter != -1) | |
505 | { | |
506 | kill(-transfer->filter, SIGHUP); | |
507 | transfer->filter = -1; | |
508 | } | |
509 | if(transfer->localend) | |
510 | { | |
cab0b442 | 511 | transfer->localend->readcb = NULL; |
512 | transfer->localend->writecb = NULL; | |
513 | transfer->localend->errcb = NULL; | |
d3372da9 | 514 | putsock(transfer->localend); |
515 | transfer->localend = NULL; | |
516 | } | |
517 | if(transfer->filterout) | |
518 | { | |
cab0b442 | 519 | transfer->filterout->readcb = NULL; |
d3372da9 | 520 | putsock(transfer->filterout); |
521 | transfer->filterout = NULL; | |
522 | } | |
523 | if(transfer->filterbuf) | |
524 | { | |
525 | free(transfer->filterbuf); | |
526 | transfer->filterbuf = NULL; | |
527 | } | |
528 | transfer->filterbufsize = transfer->filterbufdata = 0; | |
529 | } | |
530 | ||
0309b83d | 531 | static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg) |
532 | { | |
533 | if(!wcscmp(cmd, L"status")) { | |
534 | if(arg == NULL) | |
535 | arg = L""; | |
536 | if(transfer->exitstatus != NULL) | |
537 | free(transfer->exitstatus); | |
538 | transfer->exitstatus = swcsdup(arg); | |
539 | } | |
540 | } | |
541 | ||
cab0b442 | 542 | static void filterread(struct socket *sk, struct transfer *transfer) |
d3372da9 | 543 | { |
544 | char *buf, *p, *p2; | |
545 | size_t bufsize; | |
546 | wchar_t *cmd, *arg; | |
547 | ||
548 | if((buf = sockgetinbuf(sk, &bufsize)) == NULL) | |
cab0b442 | 549 | return; |
d3372da9 | 550 | bufcat(transfer->filterbuf, buf, bufsize); |
551 | free(buf); | |
5b193db0 | 552 | while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL) |
d3372da9 | 553 | { |
554 | *(p++) = 0; | |
555 | if((p2 = strchr(transfer->filterbuf, ' ')) != NULL) | |
556 | *(p2++) = 0; | |
557 | if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL) | |
558 | { | |
559 | arg = NULL; | |
560 | if(p2 != NULL) | |
561 | { | |
562 | if((arg = icmbstowcs(p2, NULL)) == NULL) | |
13b303d4 | 563 | flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno)); |
d3372da9 | 564 | } |
0309b83d | 565 | handletranscmd(transfer, cmd, arg); |
d3372da9 | 566 | CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg); |
567 | if(arg != NULL) | |
568 | free(arg); | |
569 | free(cmd); | |
570 | } else { | |
571 | flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno)); | |
572 | } | |
573 | memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf)); | |
574 | } | |
575 | } | |
576 | ||
577 | static void filterexit(pid_t pid, int status, void *data) | |
578 | { | |
579 | struct transfer *transfer; | |
734dc54e | 580 | struct fnet *fnet; |
581 | wchar_t *peerid; | |
d3372da9 | 582 | |
583 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
584 | { | |
585 | if(transfer->filter == pid) | |
586 | { | |
587 | transfer->filter = -1; | |
588 | killfilter(transfer); | |
734dc54e | 589 | fnet = transfer->fnet; |
590 | peerid = swcsdup(transfer->peerid); | |
d3372da9 | 591 | if(WEXITSTATUS(status)) |
d3372da9 | 592 | resettransfer(transfer); |
734dc54e | 593 | else |
d3372da9 | 594 | freetransfer(transfer); |
734dc54e | 595 | trytransferbypeer(fnet, peerid); |
596 | free(peerid); | |
d3372da9 | 597 | break; |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | int forkfilter(struct transfer *transfer) | |
603 | { | |
1d647d25 | 604 | char *filtername, *filename, *peerid, *buf, *p; |
d3372da9 | 605 | wchar_t *wfilename; |
606 | struct passwd *pwent; | |
607 | pid_t pid; | |
608 | int inpipe, outpipe; | |
609 | char **argv; | |
610 | size_t argvsize, argvdata; | |
611 | struct socket *insock, *outsock; | |
9ec790e8 | 612 | struct wcspair *ta; |
d3372da9 | 613 | char *rec, *val; |
614 | ||
9c161e77 | 615 | wfilename = fnfilebasename(transfer->path); |
d3372da9 | 616 | if(transfer->auth == NULL) |
617 | { | |
618 | flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id); | |
619 | errno = EACCES; | |
620 | return(-1); | |
621 | } | |
622 | if((pwent = getpwuid(transfer->owner)) == NULL) | |
623 | { | |
624 | flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id); | |
625 | errno = EACCES; | |
626 | return(-1); | |
627 | } | |
645a4af7 | 628 | filtername = findfile("dc-filter", pwent->pw_dir, 0); |
81386042 | 629 | if(filtername == NULL) |
645a4af7 | 630 | filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0); |
81386042 | 631 | if(filtername == NULL) |
d3372da9 | 632 | { |
633 | flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name); | |
634 | errno = ENOENT; | |
635 | return(-1); | |
636 | } | |
637 | if((filename = icwcstombs(wfilename, NULL)) == NULL) | |
638 | { | |
639 | if((buf = icwcstombs(wfilename, "UTF-8")) == NULL) | |
640 | { | |
641 | flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno)); | |
642 | return(-1); | |
643 | } | |
644 | filename = sprintf2("utf8-%s", buf); | |
645 | free(buf); | |
646 | } | |
647 | if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL) | |
648 | { | |
649 | if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL) | |
650 | { | |
651 | flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno)); | |
652 | free(filename); | |
653 | return(-1); | |
654 | } | |
655 | peerid = sprintf2("utf8-%s", buf); | |
656 | free(buf); | |
657 | } | |
1d647d25 FT |
658 | for(p = filename; *p; p++) { |
659 | if(*p == '/') | |
660 | *p = '_'; | |
661 | else if((p == filename) && (*p == '.')) | |
662 | *p = '_'; | |
663 | } | |
d3372da9 | 664 | if((pid = forksess(transfer->owner, transfer->auth, filterexit, NULL, FD_PIPE, 0, O_WRONLY, &inpipe, FD_PIPE, 1, O_RDONLY, &outpipe, FD_FILE, 2, O_RDWR, "/dev/null", FD_END)) < 0) |
665 | { | |
666 | flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno)); | |
667 | return(-1); | |
668 | } | |
669 | if(pid == 0) | |
670 | { | |
671 | argv = NULL; | |
672 | argvsize = argvdata = 0; | |
dcf7a1a2 | 673 | buf = sprintf2("%ji", (intmax_t)transfer->size); |
d3372da9 | 674 | addtobuf(argv, filtername); |
675 | addtobuf(argv, filename); | |
676 | addtobuf(argv, buf); | |
677 | addtobuf(argv, peerid); | |
082b0419 | 678 | if(transfer->hash) |
679 | { | |
680 | if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL) | |
681 | { | |
682 | /* XXX: I am very doubtful of this, but it can just as | |
683 | * well be argued that all data should be presented as | |
684 | * key-value pairs. */ | |
685 | addtobuf(argv, "hash"); | |
686 | addtobuf(argv, buf); | |
687 | } else { | |
688 | flog(LOG_WARNING, "could not convert hash to local charset"); | |
689 | } | |
690 | } | |
d3372da9 | 691 | for(ta = transfer->args; ta != NULL; ta = ta->next) |
692 | { | |
9ec790e8 | 693 | if((rec = icwcstombs(ta->key, NULL)) == NULL) |
d3372da9 | 694 | continue; |
695 | if((val = icwcstombs(ta->val, NULL)) == NULL) | |
696 | continue; | |
697 | addtobuf(argv, rec); | |
698 | addtobuf(argv, val); | |
699 | } | |
700 | addtobuf(argv, NULL); | |
701 | execv(filtername, argv); | |
702 | flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno)); | |
703 | exit(127); | |
704 | } | |
705 | insock = wrapsock(inpipe); | |
706 | outsock = wrapsock(outpipe); | |
707 | /* Really, really strange thing here - sometimes the kernel would | |
708 | * return POLLIN on insock, even though it's a write-side | |
709 | * pipe. The corresponding read on the pipe naturally returns | |
710 | * EBADF, causing doldacond to think there's something wrong with | |
711 | * the fd, and thus it closes it. Until I can find out whyever the | |
712 | * kernel gives a POLLIN on the fd (if I can at all...), I'll just | |
713 | * set ignread on insock for now. */ | |
81286a22 | 714 | /* sockblock(insock, 1); */ |
d3372da9 | 715 | transfer->filter = pid; |
716 | transfersetlocalend(transfer, insock); | |
717 | getsock(transfer->filterout = outsock); | |
cab0b442 | 718 | outsock->data = transfer; |
719 | outsock->readcb = (void (*)(struct socket *, void *))filterread; | |
d3372da9 | 720 | putsock(insock); |
721 | putsock(outsock); | |
722 | free(filtername); | |
723 | free(filename); | |
724 | free(peerid); | |
725 | return(0); | |
726 | } | |
727 | ||
728 | static int run(void) | |
729 | { | |
730 | struct transfer *transfer, *next; | |
731 | ||
1ce5968e | 732 | /* |
d3372da9 | 733 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) |
734 | { | |
735 | if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos)) | |
736 | { | |
737 | if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL)) | |
738 | transfer->iface->endofdata(transfer, transfer->ifacedata); | |
739 | closesock(transfer->localend); | |
740 | } | |
741 | } | |
1ce5968e | 742 | */ |
d3372da9 | 743 | for(transfer = transfers; transfer != NULL; transfer = next) |
744 | { | |
745 | next = transfer->next; | |
746 | if(transfer->close) | |
747 | { | |
748 | transferdetach(transfer); | |
749 | freetransfer(transfer); | |
750 | continue; | |
751 | } | |
752 | } | |
753 | return(0); | |
754 | } | |
755 | ||
756 | static struct configvar myvars[] = | |
757 | { | |
d9f89ef5 | 758 | /** The maximum number of simultaneously permitted uploads. A |
759 | * common hub rule is that you will need at least as many slots as | |
760 | * the number of hubs to which you are connected. */ | |
d3372da9 | 761 | {CONF_VAR_INT, "slots", {.num = 3}}, |
d9f89ef5 | 762 | /** The TOS value to use for upload connections (see the TOS |
763 | * VALUES section). */ | |
d3372da9 | 764 | {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}}, |
d9f89ef5 | 765 | /** The TOS value to use for download connections (see the TOS |
766 | * VALUES section). */ | |
d3372da9 | 767 | {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}}, |
d9f89ef5 | 768 | /** The name of the filter script (see the FILES section for |
769 | * lookup information). */ | |
d3372da9 | 770 | {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}}, |
d9f89ef5 | 771 | /** If true, only one upload is allowed per remote peer. This |
772 | * option is still experimental, so it is recommended to leave it | |
773 | * off. */ | |
01663fd9 | 774 | {CONF_VAR_BOOL, "ulquota", {.num = 0}}, |
d3372da9 | 775 | {CONF_VAR_END} |
776 | }; | |
777 | ||
778 | static struct module me = | |
779 | { | |
780 | .conf = | |
781 | { | |
782 | .vars = myvars | |
783 | }, | |
784 | .name = "transfer", | |
785 | .run = run | |
786 | }; | |
787 | ||
788 | MODULE(me); |