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