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