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