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