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 FT |
191 | } else { |
192 | resettransfer(transfer); | |
193 | } | |
1ce5968e FT |
194 | } |
195 | ||
705b7486 | 196 | void transferattach(struct transfer *transfer, struct socket *dpipe) |
d3372da9 | 197 | { |
705b7486 FT |
198 | transferdetach(transfer); |
199 | getsock(transfer->datapipe = dpipe); | |
1ce5968e FT |
200 | dpipe->readcb = (void (*)(struct socket *, void *))dataread; |
201 | dpipe->writecb = (void (*)(struct socket *, void *))datawrite; | |
202 | dpipe->errcb = (void (*)(struct socket *, int, void *))dataerr; | |
203 | dpipe->data = transfer; | |
d3372da9 | 204 | } |
205 | ||
206 | void transferdetach(struct transfer *transfer) | |
207 | { | |
705b7486 FT |
208 | if(transfer->datapipe != NULL) { |
209 | closesock(transfer->datapipe); | |
1ff9e8ea | 210 | quitsock(transfer->datapipe); |
d3372da9 | 211 | } |
705b7486 | 212 | transfer->datapipe = NULL; |
d3372da9 | 213 | } |
214 | ||
2c086721 | 215 | struct transfer *finddownload(wchar_t *peerid) |
216 | { | |
217 | struct transfer *transfer; | |
218 | ||
219 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
220 | { | |
705b7486 | 221 | if((transfer->dir == TRNSD_DOWN) && (transfer->datapipe == NULL) && !wcscmp(peerid, transfer->peerid)) |
2c086721 | 222 | break; |
223 | } | |
224 | return(transfer); | |
225 | } | |
226 | ||
6c6bc9ce | 227 | struct transfer *hasupload(struct fnet *fnet, wchar_t *peerid) |
228 | { | |
229 | struct transfer *transfer; | |
230 | ||
231 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
232 | { | |
c458597e | 233 | if((transfer->dir == TRNSD_UP) && (transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid)) |
6c6bc9ce | 234 | break; |
235 | } | |
236 | return(transfer); | |
237 | } | |
238 | ||
705b7486 | 239 | struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct socket *dpipe) |
d3372da9 | 240 | { |
241 | struct transfer *transfer; | |
242 | ||
243 | transfer = newtransfer(); | |
244 | if(fnet != NULL) | |
245 | transfer->fnet = fnet; | |
246 | else | |
247 | transfer->fnet = fn->fnet; | |
248 | transfer->peerid = swcsdup(nickid); | |
249 | transfer->state = TRNS_HS; | |
250 | transfer->dir = TRNSD_UP; | |
251 | if(fn != NULL) | |
252 | getfnetnode(transfer->fn = fn); | |
705b7486 | 253 | transferattach(transfer, dpipe); |
d3372da9 | 254 | linktransfer(transfer); |
255 | bumptransfer(transfer); | |
256 | return(transfer); | |
257 | } | |
258 | ||
259 | void linktransfer(struct transfer *transfer) | |
260 | { | |
261 | transfer->next = transfers; | |
262 | transfer->prev = NULL; | |
263 | if(transfers != NULL) | |
264 | transfers->prev = transfer; | |
265 | transfers = transfer; | |
266 | GCBCHAINDOCB(newtransfercb, transfer); | |
267 | } | |
268 | ||
269 | void resettransfer(struct transfer *transfer) | |
270 | { | |
271 | if(transfer->dir == TRNSD_DOWN) | |
272 | { | |
705b7486 | 273 | transferdetach(transfer); |
d3372da9 | 274 | killfilter(transfer); |
275 | transfersetstate(transfer, TRNS_WAITING); | |
276 | transfersetactivity(transfer, L"reset"); | |
277 | return; | |
278 | } | |
279 | } | |
280 | ||
281 | struct transfer *findtransfer(int id) | |
282 | { | |
283 | struct transfer *transfer; | |
284 | ||
285 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
286 | { | |
287 | if(transfer->id == id) | |
288 | break; | |
289 | } | |
290 | return(transfer); | |
291 | } | |
292 | ||
293 | static void transexpire(int cancelled, struct transfer *transfer) | |
294 | { | |
295 | transfer->etimer = NULL; | |
296 | if(!cancelled) | |
297 | bumptransfer(transfer); | |
298 | else | |
299 | transfer->timeout = 0; | |
300 | } | |
301 | ||
dcf7a1a2 | 302 | void transferprepul(struct transfer *transfer, off_t size, off_t start, off_t end, struct socket *lesk) |
d3372da9 | 303 | { |
304 | transfersetsize(transfer, size); | |
305 | transfer->curpos = start; | |
306 | transfer->endpos = end; | |
d3372da9 | 307 | transfersetlocalend(transfer, lesk); |
308 | } | |
309 | ||
2c086721 | 310 | void transferstartdl(struct transfer *transfer, struct socket *sk) |
311 | { | |
312 | transfersetstate(transfer, TRNS_MAIN); | |
313 | socksettos(sk, confgetint("transfer", "dltos")); | |
314 | } | |
315 | ||
d3372da9 | 316 | void transferstartul(struct transfer *transfer, struct socket *sk) |
317 | { | |
318 | transfersetstate(transfer, TRNS_MAIN); | |
319 | socksettos(sk, confgetint("transfer", "ultos")); | |
320 | if(transfer->localend != NULL) | |
1ce5968e | 321 | localread(transfer->localend, transfer); |
d3372da9 | 322 | } |
323 | ||
324 | void transfersetlocalend(struct transfer *transfer, struct socket *sk) | |
325 | { | |
326 | if(transfer->localend != NULL) | |
327 | putsock(transfer->localend); | |
bd5d862e | 328 | socksetdebug(sk, 2, "localend"); |
d3372da9 | 329 | getsock(transfer->localend = sk); |
cab0b442 | 330 | sk->data = transfer; |
705b7486 FT |
331 | sk->readcb = (void (*)(struct socket *, void *))localread; |
332 | sk->writecb = (void (*)(struct socket *, void *))localwrite; | |
333 | sk->errcb = (void (*)(struct socket *, int, void *))localerr; | |
d3372da9 | 334 | } |
335 | ||
18f56d13 | 336 | static int tryreq(struct transfer *transfer) |
d3372da9 | 337 | { |
338 | struct fnetnode *fn; | |
339 | struct fnetpeer *peer; | |
18f56d13 | 340 | |
341 | if((fn = transfer->fn) != NULL) | |
342 | { | |
343 | if(fn->state != FNN_EST) | |
344 | { | |
345 | transfer->close = 1; | |
346 | return(1); | |
347 | } | |
348 | peer = fnetfindpeer(fn, transfer->peerid); | |
349 | } else { | |
350 | peer = NULL; | |
351 | for(fn = fnetnodes; fn != NULL; fn = fn->next) | |
352 | { | |
353 | if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL)) | |
354 | break; | |
355 | } | |
356 | } | |
357 | if(peer != NULL) | |
6329b4ba | 358 | { |
359 | time(&transfer->lastreq); | |
18f56d13 | 360 | return(fn->fnet->reqconn(peer)); |
6329b4ba | 361 | } |
18f56d13 | 362 | return(1); |
363 | } | |
364 | ||
365 | void trytransferbypeer(struct fnet *fnet, wchar_t *peerid) | |
366 | { | |
367 | struct transfer *transfer; | |
368 | ||
369 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
370 | { | |
371 | if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING)) | |
372 | { | |
373 | if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid)) | |
374 | { | |
375 | if(!tryreq(transfer)) | |
376 | return; | |
377 | } | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | void bumptransfer(struct transfer *transfer) | |
383 | { | |
d3372da9 | 384 | time_t now; |
385 | ||
386 | if((now = time(NULL)) < transfer->timeout) | |
387 | { | |
18f56d13 | 388 | |
d3372da9 | 389 | if(transfer->etimer == NULL) |
390 | transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer); | |
391 | return; | |
392 | } | |
393 | if(transfer->etimer != NULL) | |
394 | canceltimer(transfer->etimer); | |
395 | switch(transfer->state) | |
396 | { | |
397 | case TRNS_WAITING: | |
d3372da9 | 398 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer); |
399 | if(now - transfer->lastreq > 30) | |
18f56d13 | 400 | tryreq(transfer); |
d3372da9 | 401 | break; |
402 | case TRNS_HS: | |
403 | if(transfer->dir == TRNSD_UP) | |
404 | { | |
405 | if(now - transfer->activity < 60) | |
406 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer); | |
407 | else | |
408 | transfer->close = 1; | |
409 | } else if(transfer->dir == TRNSD_DOWN) { | |
410 | if(now - transfer->activity < 60) | |
411 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer); | |
412 | else | |
413 | resettransfer(transfer); | |
414 | } | |
415 | break; | |
416 | case TRNS_MAIN: | |
417 | if(transfer->dir == TRNSD_UP) | |
418 | { | |
419 | if(now - transfer->activity < 300) | |
420 | transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer); | |
421 | else | |
422 | transfer->close = 1; | |
423 | } | |
424 | break; | |
425 | } | |
426 | } | |
427 | ||
428 | void transfersetactivity(struct transfer *transfer, wchar_t *desc) | |
429 | { | |
430 | time(&transfer->activity); | |
431 | if(desc != NULL) | |
432 | { | |
433 | if(transfer->actdesc != NULL) | |
434 | free(transfer->actdesc); | |
435 | transfer->actdesc = swcsdup(desc); | |
436 | } | |
437 | bumptransfer(transfer); | |
438 | CBCHAINDOCB(transfer, trans_act, transfer); | |
439 | } | |
440 | ||
441 | void transfersetstate(struct transfer *transfer, int newstate) | |
442 | { | |
443 | transfer->state = newstate; | |
444 | if(transfer->etimer != NULL) | |
445 | canceltimer(transfer->etimer); | |
446 | transfersetactivity(transfer, NULL); | |
447 | CBCHAINDOCB(transfer, trans_ac, transfer, L"state"); | |
448 | } | |
449 | ||
450 | void transfersetnick(struct transfer *transfer, wchar_t *newnick) | |
451 | { | |
452 | if(transfer->peernick != NULL) | |
453 | free(transfer->peernick); | |
454 | transfer->peernick = swcsdup(newnick); | |
455 | CBCHAINDOCB(transfer, trans_ac, transfer, L"nick"); | |
456 | } | |
457 | ||
dcf7a1a2 | 458 | void transfersetsize(struct transfer *transfer, off_t newsize) |
d3372da9 | 459 | { |
460 | transfer->size = newsize; | |
461 | CBCHAINDOCB(transfer, trans_ac, transfer, L"size"); | |
462 | } | |
463 | ||
464 | void transferseterror(struct transfer *transfer, int error) | |
465 | { | |
466 | transfer->error = error; | |
467 | CBCHAINDOCB(transfer, trans_ac, transfer, L"error"); | |
468 | } | |
469 | ||
470 | void transfersetpath(struct transfer *transfer, wchar_t *path) | |
471 | { | |
472 | if(transfer->path != NULL) | |
473 | free(transfer->path); | |
474 | transfer->path = swcsdup(path); | |
475 | CBCHAINDOCB(transfer, trans_ac, transfer, L"path"); | |
476 | } | |
477 | ||
2c086721 | 478 | void transfersethash(struct transfer *transfer, struct hash *hash) |
479 | { | |
480 | if(transfer->hash != NULL) | |
481 | freehash(transfer->hash); | |
482 | transfer->hash = hash; | |
483 | CBCHAINDOCB(transfer, trans_ac, transfer, L"hash"); | |
484 | } | |
485 | ||
d3372da9 | 486 | int slotsleft(void) |
487 | { | |
488 | struct transfer *transfer; | |
489 | int slots; | |
490 | ||
491 | slots = confgetint("transfer", "slots"); | |
492 | for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next) | |
493 | { | |
494 | if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot) | |
495 | slots--; | |
496 | } | |
497 | return(slots); | |
498 | } | |
499 | ||
500 | static void killfilter(struct transfer *transfer) | |
501 | { | |
502 | if(transfer->filter != -1) | |
503 | { | |
504 | kill(-transfer->filter, SIGHUP); | |
505 | transfer->filter = -1; | |
506 | } | |
507 | if(transfer->localend) | |
508 | { | |
cab0b442 | 509 | transfer->localend->readcb = NULL; |
510 | transfer->localend->writecb = NULL; | |
511 | transfer->localend->errcb = NULL; | |
d3372da9 | 512 | putsock(transfer->localend); |
513 | transfer->localend = NULL; | |
514 | } | |
515 | if(transfer->filterout) | |
516 | { | |
cab0b442 | 517 | transfer->filterout->readcb = NULL; |
d3372da9 | 518 | putsock(transfer->filterout); |
519 | transfer->filterout = NULL; | |
520 | } | |
521 | if(transfer->filterbuf) | |
522 | { | |
523 | free(transfer->filterbuf); | |
524 | transfer->filterbuf = NULL; | |
525 | } | |
526 | transfer->filterbufsize = transfer->filterbufdata = 0; | |
527 | } | |
528 | ||
0309b83d | 529 | static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg) |
530 | { | |
531 | if(!wcscmp(cmd, L"status")) { | |
532 | if(arg == NULL) | |
533 | arg = L""; | |
534 | if(transfer->exitstatus != NULL) | |
535 | free(transfer->exitstatus); | |
536 | transfer->exitstatus = swcsdup(arg); | |
537 | } | |
538 | } | |
539 | ||
cab0b442 | 540 | static void filterread(struct socket *sk, struct transfer *transfer) |
d3372da9 | 541 | { |
542 | char *buf, *p, *p2; | |
543 | size_t bufsize; | |
544 | wchar_t *cmd, *arg; | |
545 | ||
546 | if((buf = sockgetinbuf(sk, &bufsize)) == NULL) | |
cab0b442 | 547 | return; |
d3372da9 | 548 | bufcat(transfer->filterbuf, buf, bufsize); |
549 | free(buf); | |
5b193db0 | 550 | while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL) |
d3372da9 | 551 | { |
552 | *(p++) = 0; | |
553 | if((p2 = strchr(transfer->filterbuf, ' ')) != NULL) | |
554 | *(p2++) = 0; | |
555 | if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL) | |
556 | { | |
557 | arg = NULL; | |
558 | if(p2 != NULL) | |
559 | { | |
560 | if((arg = icmbstowcs(p2, NULL)) == NULL) | |
13b303d4 | 561 | flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno)); |
d3372da9 | 562 | } |
0309b83d | 563 | handletranscmd(transfer, cmd, arg); |
d3372da9 | 564 | CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg); |
565 | if(arg != NULL) | |
566 | free(arg); | |
567 | free(cmd); | |
568 | } else { | |
569 | flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno)); | |
570 | } | |
571 | memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf)); | |
572 | } | |
573 | } | |
574 | ||
575 | static void filterexit(pid_t pid, int status, void *data) | |
576 | { | |
577 | struct transfer *transfer; | |
734dc54e | 578 | struct fnet *fnet; |
579 | wchar_t *peerid; | |
d3372da9 | 580 | |
581 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
582 | { | |
583 | if(transfer->filter == pid) | |
584 | { | |
585 | transfer->filter = -1; | |
586 | killfilter(transfer); | |
734dc54e | 587 | fnet = transfer->fnet; |
588 | peerid = swcsdup(transfer->peerid); | |
d3372da9 | 589 | if(WEXITSTATUS(status)) |
d3372da9 | 590 | resettransfer(transfer); |
734dc54e | 591 | else |
d3372da9 | 592 | freetransfer(transfer); |
734dc54e | 593 | trytransferbypeer(fnet, peerid); |
594 | free(peerid); | |
d3372da9 | 595 | break; |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | int forkfilter(struct transfer *transfer) | |
601 | { | |
1d647d25 | 602 | char *filtername, *filename, *peerid, *buf, *p; |
d3372da9 | 603 | wchar_t *wfilename; |
604 | struct passwd *pwent; | |
605 | pid_t pid; | |
606 | int inpipe, outpipe; | |
607 | char **argv; | |
608 | size_t argvsize, argvdata; | |
609 | struct socket *insock, *outsock; | |
9ec790e8 | 610 | struct wcspair *ta; |
d3372da9 | 611 | char *rec, *val; |
612 | ||
9c161e77 | 613 | wfilename = fnfilebasename(transfer->path); |
d3372da9 | 614 | if(transfer->auth == NULL) |
615 | { | |
616 | flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id); | |
617 | errno = EACCES; | |
618 | return(-1); | |
619 | } | |
620 | if((pwent = getpwuid(transfer->owner)) == NULL) | |
621 | { | |
622 | flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id); | |
623 | errno = EACCES; | |
624 | return(-1); | |
625 | } | |
645a4af7 | 626 | filtername = findfile("dc-filter", pwent->pw_dir, 0); |
81386042 | 627 | if(filtername == NULL) |
645a4af7 | 628 | filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0); |
81386042 | 629 | if(filtername == NULL) |
d3372da9 | 630 | { |
631 | flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name); | |
632 | errno = ENOENT; | |
633 | return(-1); | |
634 | } | |
635 | if((filename = icwcstombs(wfilename, NULL)) == NULL) | |
636 | { | |
637 | if((buf = icwcstombs(wfilename, "UTF-8")) == NULL) | |
638 | { | |
639 | flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno)); | |
640 | return(-1); | |
641 | } | |
642 | filename = sprintf2("utf8-%s", buf); | |
643 | free(buf); | |
644 | } | |
645 | if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL) | |
646 | { | |
647 | if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL) | |
648 | { | |
649 | flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno)); | |
650 | free(filename); | |
651 | return(-1); | |
652 | } | |
653 | peerid = sprintf2("utf8-%s", buf); | |
654 | free(buf); | |
655 | } | |
1d647d25 FT |
656 | for(p = filename; *p; p++) { |
657 | if(*p == '/') | |
658 | *p = '_'; | |
659 | else if((p == filename) && (*p == '.')) | |
660 | *p = '_'; | |
661 | } | |
d3372da9 | 662 | 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) |
663 | { | |
664 | flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno)); | |
665 | return(-1); | |
666 | } | |
667 | if(pid == 0) | |
668 | { | |
669 | argv = NULL; | |
670 | argvsize = argvdata = 0; | |
dcf7a1a2 | 671 | buf = sprintf2("%ji", (intmax_t)transfer->size); |
d3372da9 | 672 | addtobuf(argv, filtername); |
673 | addtobuf(argv, filename); | |
674 | addtobuf(argv, buf); | |
675 | addtobuf(argv, peerid); | |
082b0419 | 676 | if(transfer->hash) |
677 | { | |
678 | if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL) | |
679 | { | |
680 | /* XXX: I am very doubtful of this, but it can just as | |
681 | * well be argued that all data should be presented as | |
682 | * key-value pairs. */ | |
683 | addtobuf(argv, "hash"); | |
684 | addtobuf(argv, buf); | |
685 | } else { | |
686 | flog(LOG_WARNING, "could not convert hash to local charset"); | |
687 | } | |
688 | } | |
d3372da9 | 689 | for(ta = transfer->args; ta != NULL; ta = ta->next) |
690 | { | |
9ec790e8 | 691 | if((rec = icwcstombs(ta->key, NULL)) == NULL) |
d3372da9 | 692 | continue; |
693 | if((val = icwcstombs(ta->val, NULL)) == NULL) | |
694 | continue; | |
695 | addtobuf(argv, rec); | |
696 | addtobuf(argv, val); | |
697 | } | |
698 | addtobuf(argv, NULL); | |
699 | execv(filtername, argv); | |
700 | flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno)); | |
701 | exit(127); | |
702 | } | |
703 | insock = wrapsock(inpipe); | |
704 | outsock = wrapsock(outpipe); | |
705 | /* Really, really strange thing here - sometimes the kernel would | |
706 | * return POLLIN on insock, even though it's a write-side | |
707 | * pipe. The corresponding read on the pipe naturally returns | |
708 | * EBADF, causing doldacond to think there's something wrong with | |
709 | * the fd, and thus it closes it. Until I can find out whyever the | |
710 | * kernel gives a POLLIN on the fd (if I can at all...), I'll just | |
711 | * set ignread on insock for now. */ | |
81286a22 | 712 | /* sockblock(insock, 1); */ |
d3372da9 | 713 | transfer->filter = pid; |
714 | transfersetlocalend(transfer, insock); | |
715 | getsock(transfer->filterout = outsock); | |
cab0b442 | 716 | outsock->data = transfer; |
717 | outsock->readcb = (void (*)(struct socket *, void *))filterread; | |
d3372da9 | 718 | putsock(insock); |
719 | putsock(outsock); | |
720 | free(filtername); | |
721 | free(filename); | |
722 | free(peerid); | |
723 | return(0); | |
724 | } | |
725 | ||
726 | static int run(void) | |
727 | { | |
728 | struct transfer *transfer, *next; | |
729 | ||
1ce5968e | 730 | /* |
d3372da9 | 731 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) |
732 | { | |
733 | if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos)) | |
734 | { | |
735 | if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL)) | |
736 | transfer->iface->endofdata(transfer, transfer->ifacedata); | |
737 | closesock(transfer->localend); | |
738 | } | |
739 | } | |
1ce5968e | 740 | */ |
d3372da9 | 741 | for(transfer = transfers; transfer != NULL; transfer = next) |
742 | { | |
743 | next = transfer->next; | |
744 | if(transfer->close) | |
745 | { | |
746 | transferdetach(transfer); | |
747 | freetransfer(transfer); | |
748 | continue; | |
749 | } | |
750 | } | |
751 | return(0); | |
752 | } | |
753 | ||
754 | static struct configvar myvars[] = | |
755 | { | |
d9f89ef5 | 756 | /** The maximum number of simultaneously permitted uploads. A |
757 | * common hub rule is that you will need at least as many slots as | |
758 | * the number of hubs to which you are connected. */ | |
d3372da9 | 759 | {CONF_VAR_INT, "slots", {.num = 3}}, |
d9f89ef5 | 760 | /** The TOS value to use for upload connections (see the TOS |
761 | * VALUES section). */ | |
d3372da9 | 762 | {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}}, |
d9f89ef5 | 763 | /** The TOS value to use for download connections (see the TOS |
764 | * VALUES section). */ | |
d3372da9 | 765 | {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}}, |
d9f89ef5 | 766 | /** The name of the filter script (see the FILES section for |
767 | * lookup information). */ | |
d3372da9 | 768 | {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}}, |
d9f89ef5 | 769 | /** If true, only one upload is allowed per remote peer. This |
770 | * option is still experimental, so it is recommended to leave it | |
771 | * off. */ | |
01663fd9 | 772 | {CONF_VAR_BOOL, "ulquota", {.num = 0}}, |
d3372da9 | 773 | {CONF_VAR_END} |
774 | }; | |
775 | ||
776 | static struct module me = | |
777 | { | |
778 | .conf = | |
779 | { | |
780 | .vars = myvars | |
781 | }, | |
782 | .name = "transfer", | |
783 | .run = run | |
784 | }; | |
785 | ||
786 | MODULE(me); |