2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
35 #include "sysevents.h"
40 static void killfilter(struct transfer *transfer);
42 struct transfer *transfers = NULL;
44 GCBCHAIN(newtransfercb, struct transfer *);
46 void freetransfer(struct transfer *transfer)
50 if(transfer == transfers)
51 transfers = transfer->next;
52 if(transfer->next != NULL)
53 transfer->next->prev = transfer->prev;
54 if(transfer->prev != NULL)
55 transfer->prev->next = transfer->next;
56 CBCHAINDOCB(transfer, trans_destroy, transfer);
57 CBCHAINFREE(transfer, trans_ac);
58 CBCHAINFREE(transfer, trans_act);
59 CBCHAINFREE(transfer, trans_p);
60 CBCHAINFREE(transfer, trans_destroy);
61 CBCHAINFREE(transfer, trans_filterout);
62 while((ta = transfer->args) != NULL)
64 transfer->args = ta->next;
69 if(transfer->filter != -1)
71 if(transfer->etimer != NULL)
72 canceltimer(transfer->etimer);
73 if(transfer->auth != NULL)
74 authputhandle(transfer->auth);
75 if(transfer->peerid != NULL)
76 free(transfer->peerid);
77 if(transfer->peernick != NULL)
78 free(transfer->peernick);
79 if(transfer->path != NULL)
81 if(transfer->actdesc != NULL)
82 free(transfer->actdesc);
83 if(transfer->filterbuf != NULL)
84 free(transfer->filterbuf);
85 if(transfer->localend != NULL)
87 transfer->localend->readcb = NULL;
88 transfer->localend->writecb = NULL;
89 transfer->localend->errcb = NULL;
90 putsock(transfer->localend);
92 if(transfer->filterout != NULL)
94 transfer->filterout->readcb = NULL;
95 transfer->filterout->writecb = NULL;
96 transfer->filterout->errcb = NULL;
97 putsock(transfer->filterout);
99 if(transfer->fn != NULL)
100 putfnetnode(transfer->fn);
105 struct transfer *newtransfer(void)
107 struct transfer *new;
108 static int curid = 0;
110 new = smalloc(sizeof(*new));
111 memset(new, 0, sizeof(*new));
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);
123 time(&new->activity);
128 void transferaddarg(struct transfer *transfer, wchar_t *rec, wchar_t *val)
132 ta = smalloc(sizeof(*ta));
133 ta->rec = swcsdup(rec);
134 ta->val = swcsdup(val);
135 ta->next = transfer->args;
139 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
141 if(transfer->iface != NULL)
142 transferdetach(transfer);
143 transfer->iface = iface;
144 transfer->ifacedata = data;
147 void transferdetach(struct transfer *transfer)
149 if(transfer->iface != NULL)
151 transfer->iface->detach(transfer, transfer->ifacedata);
152 transfer->iface = NULL;
153 transfer->ifacedata = NULL;
157 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
159 struct transfer *transfer;
161 transfer = newtransfer();
163 transfer->fnet = fnet;
165 transfer->fnet = fn->fnet;
166 transfer->peerid = swcsdup(nickid);
167 transfer->state = TRNS_HS;
168 transfer->dir = TRNSD_UP;
170 getfnetnode(transfer->fn = fn);
171 transferattach(transfer, iface, data);
172 linktransfer(transfer);
173 bumptransfer(transfer);
177 void linktransfer(struct transfer *transfer)
179 transfer->next = transfers;
180 transfer->prev = NULL;
181 if(transfers != NULL)
182 transfers->prev = transfer;
183 transfers = transfer;
184 GCBCHAINDOCB(newtransfercb, transfer);
187 void resettransfer(struct transfer *transfer)
189 if(transfer->dir == TRNSD_DOWN)
191 if(transfer->iface != NULL)
192 transferdetach(transfer);
193 killfilter(transfer);
194 transfersetstate(transfer, TRNS_WAITING);
195 transfersetactivity(transfer, L"reset");
200 struct transfer *findtransfer(int id)
202 struct transfer *transfer;
204 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
206 if(transfer->id == id)
212 static void transexpire(int cancelled, struct transfer *transfer)
214 transfer->etimer = NULL;
216 bumptransfer(transfer);
218 transfer->timeout = 0;
221 static void transferread(struct socket *sk, struct transfer *transfer)
223 if(sockgetdatalen(sk) >= 65536)
225 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
226 transfer->iface->gotdata(transfer, transfer->ifacedata);
229 static void transferwrite(struct socket *sk, struct transfer *transfer)
231 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
232 transfer->iface->wantdata(transfer, transfer->ifacedata);
235 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
237 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
238 transfer->iface->endofdata(transfer, transfer->ifacedata);
241 void transferputdata(struct transfer *transfer, void *buf, size_t size)
243 time(&transfer->activity);
244 sockqueue(transfer->localend, buf, size);
245 transfer->curpos += size;
246 CBCHAINDOCB(transfer, trans_p, transfer);
249 void transferendofdata(struct transfer *transfer)
251 if(transfer->curpos >= transfer->size)
253 transfersetstate(transfer, TRNS_DONE);
254 transfer->localend->readcb = NULL;
255 transfer->localend->writecb = NULL;
256 transfer->localend->errcb = NULL;
257 putsock(transfer->localend);
258 transfer->localend = NULL;
260 resettransfer(transfer);
264 size_t transferdatasize(struct transfer *transfer)
266 return(sockqueuesize(transfer->localend));
269 void *transfergetdata(struct transfer *transfer, size_t *size)
273 if(transfer->localend == NULL)
275 transfer->localend->ignread = 0;
276 time(&transfer->activity);
277 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
279 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
281 *size = transfer->endpos - transfer->curpos;
282 buf = srealloc(buf, *size);
284 transfer->curpos += *size;
285 CBCHAINDOCB(transfer, trans_p, transfer);
289 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
291 transfersetsize(transfer, size);
292 transfer->curpos = start;
293 transfer->endpos = end;
295 transfersetlocalend(transfer, lesk);
298 void transferstartul(struct transfer *transfer, struct socket *sk)
300 transfersetstate(transfer, TRNS_MAIN);
301 socksettos(sk, confgetint("transfer", "ultos"));
302 if(transfer->localend != NULL)
303 transfer->localend->ignread = 0;
306 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
308 if(transfer->localend != NULL)
309 putsock(transfer->localend);
310 getsock(transfer->localend = sk);
312 sk->readcb = (void (*)(struct socket *, void *))transferread;
313 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
314 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
317 void bumptransfer(struct transfer *transfer)
320 struct fnetpeer *peer;
323 if((now = time(NULL)) < transfer->timeout)
325 if(transfer->etimer == NULL)
326 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
329 if(transfer->etimer != NULL)
330 canceltimer(transfer->etimer);
331 switch(transfer->state)
334 if(transfer->fn != NULL)
337 if(fn->state != FNN_EST)
342 peer = fnetfindpeer(fn, transfer->peerid);
345 for(fn = fnetnodes; fn != NULL; fn = fn->next)
347 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
351 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
352 if(now - transfer->lastreq > 30)
356 fn->fnet->reqconn(peer);
357 time(&transfer->lastreq);
362 if(transfer->dir == TRNSD_UP)
364 if(now - transfer->activity < 60)
365 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
368 } else if(transfer->dir == TRNSD_DOWN) {
369 if(now - transfer->activity < 60)
370 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
372 resettransfer(transfer);
376 if(transfer->dir == TRNSD_UP)
378 if(now - transfer->activity < 300)
379 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
387 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
389 time(&transfer->activity);
392 if(transfer->actdesc != NULL)
393 free(transfer->actdesc);
394 transfer->actdesc = swcsdup(desc);
396 bumptransfer(transfer);
397 CBCHAINDOCB(transfer, trans_act, transfer);
400 void transfersetstate(struct transfer *transfer, int newstate)
402 transfer->state = newstate;
403 if(transfer->etimer != NULL)
404 canceltimer(transfer->etimer);
405 transfersetactivity(transfer, NULL);
406 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
409 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
411 if(transfer->peernick != NULL)
412 free(transfer->peernick);
413 transfer->peernick = swcsdup(newnick);
414 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
417 void transfersetsize(struct transfer *transfer, int newsize)
419 transfer->size = newsize;
420 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
423 void transferseterror(struct transfer *transfer, int error)
425 transfer->error = error;
426 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
429 void transfersetpath(struct transfer *transfer, wchar_t *path)
431 if(transfer->path != NULL)
432 free(transfer->path);
433 transfer->path = swcsdup(path);
434 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
439 struct transfer *transfer;
442 slots = confgetint("transfer", "slots");
443 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
445 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
451 static void killfilter(struct transfer *transfer)
453 if(transfer->filter != -1)
455 kill(-transfer->filter, SIGHUP);
456 transfer->filter = -1;
458 if(transfer->localend)
460 transfer->localend->readcb = NULL;
461 transfer->localend->writecb = NULL;
462 transfer->localend->errcb = NULL;
463 putsock(transfer->localend);
464 transfer->localend = NULL;
466 if(transfer->filterout)
468 transfer->filterout->readcb = NULL;
469 putsock(transfer->filterout);
470 transfer->filterout = NULL;
472 if(transfer->filterbuf)
474 free(transfer->filterbuf);
475 transfer->filterbuf = NULL;
477 transfer->filterbufsize = transfer->filterbufdata = 0;
480 static char *findfilter(struct passwd *pwd)
482 char *path, *filtername;
484 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
486 if(!access(path, X_OK))
490 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
492 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
494 if(strchr(filtername, '/') == NULL)
496 if((path = sprintf2("/etc/%s", filtername)) != NULL)
498 if(!access(path, X_OK))
505 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
507 if(!access(path, X_OK))
514 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
516 if(!access(path, X_OK))
524 if(!access(filtername, X_OK))
532 static void filterread(struct socket *sk, struct transfer *transfer)
538 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
540 bufcat(transfer->filterbuf, buf, bufsize);
542 if((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
545 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
547 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
552 if((arg = icmbstowcs(p2, NULL)) == NULL)
553 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
555 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
560 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
562 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
566 static void filterexit(pid_t pid, int status, void *data)
568 struct transfer *transfer;
570 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
572 if(transfer->filter == pid)
574 transfer->filter = -1;
575 killfilter(transfer);
576 if(WEXITSTATUS(status))
578 resettransfer(transfer);
580 freetransfer(transfer);
587 int forkfilter(struct transfer *transfer)
589 char *filtername, *filename, *peerid, *buf;
591 struct passwd *pwent;
595 size_t argvsize, argvdata;
596 struct socket *insock, *outsock;
600 wfilename = transfer->path;
601 if(transfer->fnet->filebasename != NULL)
602 wfilename = transfer->fnet->filebasename(wfilename);
603 if(transfer->auth == NULL)
605 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
609 if((pwent = getpwuid(transfer->owner)) == NULL)
611 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
615 if((filtername = findfilter(pwent)) == NULL)
617 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
621 if((filename = icwcstombs(wfilename, NULL)) == NULL)
623 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
625 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
628 filename = sprintf2("utf8-%s", buf);
631 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
633 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
635 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
639 peerid = sprintf2("utf8-%s", buf);
642 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)
644 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
650 argvsize = argvdata = 0;
651 buf = sprintf2("%i", transfer->size);
652 addtobuf(argv, filtername);
653 addtobuf(argv, filename);
655 addtobuf(argv, peerid);
656 for(ta = transfer->args; ta != NULL; ta = ta->next)
658 if((rec = icwcstombs(ta->rec, NULL)) == NULL)
660 if((val = icwcstombs(ta->val, NULL)) == NULL)
665 addtobuf(argv, NULL);
666 execv(filtername, argv);
667 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
670 insock = wrapsock(inpipe);
671 outsock = wrapsock(outpipe);
672 /* Really, really strange thing here - sometimes the kernel would
673 * return POLLIN on insock, even though it's a write-side
674 * pipe. The corresponding read on the pipe naturally returns
675 * EBADF, causing doldacond to think there's something wrong with
676 * the fd, and thus it closes it. Until I can find out whyever the
677 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
678 * set ignread on insock for now. */
680 transfer->filter = pid;
681 transfersetlocalend(transfer, insock);
682 getsock(transfer->filterout = outsock);
683 outsock->data = transfer;
684 outsock->readcb = (void (*)(struct socket *, void *))filterread;
695 struct transfer *transfer, *next;
697 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
699 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
701 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
702 transfer->iface->endofdata(transfer, transfer->ifacedata);
703 closesock(transfer->localend);
706 for(transfer = transfers; transfer != NULL; transfer = next)
708 next = transfer->next;
711 transferdetach(transfer);
712 freetransfer(transfer);
719 static struct configvar myvars[] =
721 {CONF_VAR_INT, "slots", {.num = 3}},
722 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
723 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
724 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
728 static struct module me =