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"
41 static void killfilter(struct transfer *transfer);
43 struct transfer *transfers = NULL;
45 GCBCHAIN(newtransfercb, struct transfer *);
47 void freetransfer(struct transfer *transfer)
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);
63 while((ta = transfer->args) != NULL)
65 transfer->args = ta->next;
70 if(transfer->filter != -1)
72 if(transfer->etimer != NULL)
73 canceltimer(transfer->etimer);
74 if(transfer->auth != NULL)
75 authputhandle(transfer->auth);
76 if(transfer->peerid != NULL)
77 free(transfer->peerid);
78 if(transfer->peernick != NULL)
79 free(transfer->peernick);
80 if(transfer->path != NULL)
82 if(transfer->actdesc != NULL)
83 free(transfer->actdesc);
84 if(transfer->filterbuf != NULL)
85 free(transfer->filterbuf);
86 if(transfer->hash != NULL)
87 freehash(transfer->hash);
88 if(transfer->localend != NULL)
90 transfer->localend->readcb = NULL;
91 transfer->localend->writecb = NULL;
92 transfer->localend->errcb = NULL;
93 putsock(transfer->localend);
95 if(transfer->filterout != NULL)
97 transfer->filterout->readcb = NULL;
98 transfer->filterout->writecb = NULL;
99 transfer->filterout->errcb = NULL;
100 putsock(transfer->filterout);
102 if(transfer->fn != NULL)
103 putfnetnode(transfer->fn);
108 struct transfer *newtransfer(void)
110 struct transfer *new;
111 static int curid = 0;
113 new = smalloc(sizeof(*new));
114 memset(new, 0, sizeof(*new));
119 CBCHAININIT(new, trans_ac);
120 CBCHAININIT(new, trans_act);
121 CBCHAININIT(new, trans_p);
122 CBCHAININIT(new, trans_destroy);
123 CBCHAININIT(new, trans_filterout);
126 time(&new->activity);
131 void transferaddarg(struct transfer *transfer, wchar_t *rec, wchar_t *val)
135 ta = smalloc(sizeof(*ta));
136 ta->rec = swcsdup(rec);
137 ta->val = swcsdup(val);
138 ta->next = transfer->args;
142 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
144 if(transfer->iface != NULL)
145 transferdetach(transfer);
146 transfer->iface = iface;
147 transfer->ifacedata = data;
150 void transferdetach(struct transfer *transfer)
152 if(transfer->iface != NULL)
154 transfer->iface->detach(transfer, transfer->ifacedata);
155 transfer->iface = NULL;
156 transfer->ifacedata = NULL;
160 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
162 struct transfer *transfer;
164 transfer = newtransfer();
166 transfer->fnet = fnet;
168 transfer->fnet = fn->fnet;
169 transfer->peerid = swcsdup(nickid);
170 transfer->state = TRNS_HS;
171 transfer->dir = TRNSD_UP;
173 getfnetnode(transfer->fn = fn);
174 transferattach(transfer, iface, data);
175 linktransfer(transfer);
176 bumptransfer(transfer);
180 void linktransfer(struct transfer *transfer)
182 transfer->next = transfers;
183 transfer->prev = NULL;
184 if(transfers != NULL)
185 transfers->prev = transfer;
186 transfers = transfer;
187 GCBCHAINDOCB(newtransfercb, transfer);
190 void resettransfer(struct transfer *transfer)
192 if(transfer->dir == TRNSD_DOWN)
194 if(transfer->iface != NULL)
195 transferdetach(transfer);
196 killfilter(transfer);
197 transfersetstate(transfer, TRNS_WAITING);
198 transfersetactivity(transfer, L"reset");
203 struct transfer *findtransfer(int id)
205 struct transfer *transfer;
207 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
209 if(transfer->id == id)
215 static void transexpire(int cancelled, struct transfer *transfer)
217 transfer->etimer = NULL;
219 bumptransfer(transfer);
221 transfer->timeout = 0;
224 static void transferread(struct socket *sk, struct transfer *transfer)
226 if(sockgetdatalen(sk) >= 65536)
228 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
229 transfer->iface->gotdata(transfer, transfer->ifacedata);
232 static void transferwrite(struct socket *sk, struct transfer *transfer)
234 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
235 transfer->iface->wantdata(transfer, transfer->ifacedata);
238 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
240 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
241 transfer->iface->endofdata(transfer, transfer->ifacedata);
244 void transferputdata(struct transfer *transfer, void *buf, size_t size)
246 time(&transfer->activity);
247 sockqueue(transfer->localend, buf, size);
248 transfer->curpos += size;
249 CBCHAINDOCB(transfer, trans_p, transfer);
252 void transferendofdata(struct transfer *transfer)
254 if(transfer->curpos >= transfer->size)
256 transfersetstate(transfer, TRNS_DONE);
257 transfer->localend->readcb = NULL;
258 transfer->localend->writecb = NULL;
259 transfer->localend->errcb = NULL;
260 putsock(transfer->localend);
261 transfer->localend = NULL;
263 resettransfer(transfer);
267 size_t transferdatasize(struct transfer *transfer)
269 return(sockqueuesize(transfer->localend));
272 void *transfergetdata(struct transfer *transfer, size_t *size)
276 if(transfer->localend == NULL)
278 transfer->localend->ignread = 0;
279 time(&transfer->activity);
280 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
282 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
284 *size = transfer->endpos - transfer->curpos;
285 buf = srealloc(buf, *size);
287 transfer->curpos += *size;
288 CBCHAINDOCB(transfer, trans_p, transfer);
292 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
294 transfersetsize(transfer, size);
295 transfer->curpos = start;
296 transfer->endpos = end;
298 transfersetlocalend(transfer, lesk);
301 void transferstartul(struct transfer *transfer, struct socket *sk)
303 transfersetstate(transfer, TRNS_MAIN);
304 socksettos(sk, confgetint("transfer", "ultos"));
305 if(transfer->localend != NULL)
306 transfer->localend->ignread = 0;
309 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
311 if(transfer->localend != NULL)
312 putsock(transfer->localend);
313 getsock(transfer->localend = sk);
315 sk->readcb = (void (*)(struct socket *, void *))transferread;
316 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
317 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
320 void bumptransfer(struct transfer *transfer)
323 struct fnetpeer *peer;
326 if((now = time(NULL)) < transfer->timeout)
328 if(transfer->etimer == NULL)
329 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
332 if(transfer->etimer != NULL)
333 canceltimer(transfer->etimer);
334 switch(transfer->state)
337 if(transfer->fn != NULL)
340 if(fn->state != FNN_EST)
345 peer = fnetfindpeer(fn, transfer->peerid);
348 for(fn = fnetnodes; fn != NULL; fn = fn->next)
350 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
354 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
355 if(now - transfer->lastreq > 30)
359 fn->fnet->reqconn(peer);
360 time(&transfer->lastreq);
365 if(transfer->dir == TRNSD_UP)
367 if(now - transfer->activity < 60)
368 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
371 } else if(transfer->dir == TRNSD_DOWN) {
372 if(now - transfer->activity < 60)
373 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
375 resettransfer(transfer);
379 if(transfer->dir == TRNSD_UP)
381 if(now - transfer->activity < 300)
382 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
390 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
392 time(&transfer->activity);
395 if(transfer->actdesc != NULL)
396 free(transfer->actdesc);
397 transfer->actdesc = swcsdup(desc);
399 bumptransfer(transfer);
400 CBCHAINDOCB(transfer, trans_act, transfer);
403 void transfersetstate(struct transfer *transfer, int newstate)
405 transfer->state = newstate;
406 if(transfer->etimer != NULL)
407 canceltimer(transfer->etimer);
408 transfersetactivity(transfer, NULL);
409 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
412 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
414 if(transfer->peernick != NULL)
415 free(transfer->peernick);
416 transfer->peernick = swcsdup(newnick);
417 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
420 void transfersetsize(struct transfer *transfer, int newsize)
422 transfer->size = newsize;
423 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
426 void transferseterror(struct transfer *transfer, int error)
428 transfer->error = error;
429 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
432 void transfersetpath(struct transfer *transfer, wchar_t *path)
434 if(transfer->path != NULL)
435 free(transfer->path);
436 transfer->path = swcsdup(path);
437 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
442 struct transfer *transfer;
445 slots = confgetint("transfer", "slots");
446 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
448 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
454 static void killfilter(struct transfer *transfer)
456 if(transfer->filter != -1)
458 kill(-transfer->filter, SIGHUP);
459 transfer->filter = -1;
461 if(transfer->localend)
463 transfer->localend->readcb = NULL;
464 transfer->localend->writecb = NULL;
465 transfer->localend->errcb = NULL;
466 putsock(transfer->localend);
467 transfer->localend = NULL;
469 if(transfer->filterout)
471 transfer->filterout->readcb = NULL;
472 putsock(transfer->filterout);
473 transfer->filterout = NULL;
475 if(transfer->filterbuf)
477 free(transfer->filterbuf);
478 transfer->filterbuf = NULL;
480 transfer->filterbufsize = transfer->filterbufdata = 0;
483 static char *findfilter(struct passwd *pwd)
485 char *path, *filtername;
487 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
489 if(!access(path, X_OK))
493 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
495 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
497 if(strchr(filtername, '/') == NULL)
499 if((path = sprintf2("/etc/%s", filtername)) != NULL)
501 if(!access(path, X_OK))
508 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
510 if(!access(path, X_OK))
517 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
519 if(!access(path, X_OK))
527 if(!access(filtername, X_OK))
535 static void filterread(struct socket *sk, struct transfer *transfer)
541 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
543 bufcat(transfer->filterbuf, buf, bufsize);
545 if((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
548 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
550 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
555 if((arg = icmbstowcs(p2, NULL)) == NULL)
556 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
558 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
563 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
565 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
569 static void filterexit(pid_t pid, int status, void *data)
571 struct transfer *transfer;
573 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
575 if(transfer->filter == pid)
577 transfer->filter = -1;
578 killfilter(transfer);
579 if(WEXITSTATUS(status))
581 resettransfer(transfer);
583 freetransfer(transfer);
590 int forkfilter(struct transfer *transfer)
592 char *filtername, *filename, *peerid, *buf;
594 struct passwd *pwent;
598 size_t argvsize, argvdata;
599 struct socket *insock, *outsock;
603 wfilename = transfer->path;
604 if(transfer->fnet->filebasename != NULL)
605 wfilename = transfer->fnet->filebasename(wfilename);
606 if(transfer->auth == NULL)
608 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
612 if((pwent = getpwuid(transfer->owner)) == NULL)
614 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
618 if((filtername = findfilter(pwent)) == NULL)
620 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
624 if((filename = icwcstombs(wfilename, NULL)) == NULL)
626 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
628 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
631 filename = sprintf2("utf8-%s", buf);
634 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
636 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
638 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
642 peerid = sprintf2("utf8-%s", buf);
645 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)
647 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
653 argvsize = argvdata = 0;
654 buf = sprintf2("%i", transfer->size);
655 addtobuf(argv, filtername);
656 addtobuf(argv, filename);
658 addtobuf(argv, peerid);
659 for(ta = transfer->args; ta != NULL; ta = ta->next)
661 if((rec = icwcstombs(ta->rec, NULL)) == NULL)
663 if((val = icwcstombs(ta->val, NULL)) == NULL)
668 addtobuf(argv, NULL);
669 execv(filtername, argv);
670 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
673 insock = wrapsock(inpipe);
674 outsock = wrapsock(outpipe);
675 /* Really, really strange thing here - sometimes the kernel would
676 * return POLLIN on insock, even though it's a write-side
677 * pipe. The corresponding read on the pipe naturally returns
678 * EBADF, causing doldacond to think there's something wrong with
679 * the fd, and thus it closes it. Until I can find out whyever the
680 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
681 * set ignread on insock for now. */
683 transfer->filter = pid;
684 transfersetlocalend(transfer, insock);
685 getsock(transfer->filterout = outsock);
686 outsock->data = transfer;
687 outsock->readcb = (void (*)(struct socket *, void *))filterread;
698 struct transfer *transfer, *next;
700 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
702 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
704 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
705 transfer->iface->endofdata(transfer, transfer->ifacedata);
706 closesock(transfer->localend);
709 for(transfer = transfers; transfer != NULL; transfer = next)
711 next = transfer->next;
714 transferdetach(transfer);
715 freetransfer(transfer);
722 static struct configvar myvars[] =
724 {CONF_VAR_INT, "slots", {.num = 3}},
725 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
726 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
727 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
731 static struct module me =