/
transfer.c
1236 lines (1085 loc) · 35.1 KB
/
transfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* transfer.c -- part of transfer.mod
*
* Copyright (C) 1997 Robey Pointer
* Copyright (C) 1999 - 2017 Eggheads Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Small code snippets related to REGET/RESEND support were taken from
* BitchX, copyright by panasync.
*/
#define MODULE_NAME "transfer"
#define MAKING_TRANSFER
/* sigh sunos */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "src/mod/module.h"
#include "src/tandem.h"
#include "src/users.h"
#include "transfer.h"
#include <netinet/in.h>
#include <arpa/inet.h>
static Function *global = NULL;
static p_tcl_bind_list H_rcvd, H_sent, H_lost, H_tout;
static int wait_dcc_xfer = 300; /* Timeout time on DCC xfers */
static int dcc_limit = 3; /* Max simultaneous downloads allowed */
static int dcc_block = 0; /* Size of one dcc block */
static fileq_t *fileq = NULL;
static struct dcc_table DCC_SEND, DCC_GET, DCC_GET_PENDING;
#include "transferfstat.c"
#include "transferqueue.c"
#include "tcltransfer.c"
/* Forward, case-sensitive file matching supporting the '*' and '?'
* wildcard characters. This is basically a direct copy of wild_match_per(),
* but without support for '%', or '~'.
*/
static int wild_match_file(register char *m, register char *n)
{
char *ma = m, *lsm = 0, *lsn = 0;
int match = 1;
register unsigned int sofar = 0;
/* null strings should never match */
if ((m == 0) || (n == 0) || (!*n))
return 0;
while (*n) {
switch (*m) {
case 0:
do
m--;
while ((m > ma) && (*m == '?'));
if ((m > ma) ? ((*m == '*') && (m[-1] != FILEQUOTE)) : (*m == '*'))
return FILEMATCH;
break;
case FILEWILDS:
do
m++;
while (*m == FILEWILDS);
lsm = m;
lsn = n;
match += sofar;
sofar = 0;
continue;
case FILEWILDQ:
m++;
n++;
continue;
case FILEQUOTE:
m++;
}
if (*m == *n) {
m++;
n++;
sofar++;
continue;
}
if (lsm) {
n = ++lsn;
m = lsm;
sofar = 0;
continue;
}
return 0;
}
while (*m == FILEWILDS)
m++;
return (*m) ? 0 : FILEMATCH;
}
/* Return true if this user has >= the maximum number of file xfers allowed.
*/
static int at_limit(char *nick)
{
int i, x = 0;
for (i = 0; i < dcc_total; i++)
if ((dcc[i].type == &DCC_GET || dcc[i].type == &DCC_GET_PENDING) &&
!egg_strcasecmp(dcc[i].nick, nick))
x++;
return (x >= dcc_limit);
}
/* Replaces all spaces with underscores (' ' -> '_'). The returned buffer
* needs to be freed after use.
*/
static char *replace_spaces(char *fn)
{
register char *ret, *p;
p = ret = nmalloc(strlen(fn) + 1);
strcpy(ret, fn);
while ((p = strchr(p, ' ')) != NULL)
*p = '_';
return ret;
}
static int builtin_sentrcvd STDVAR
{
Function F = (Function) cd;
BADARGS(4, 4, " hand nick path");
CHECKVALIDITY(builtin_sentrcvd);
F(argv[1], argv[2], argv[3]);
return TCL_OK;
}
static int builtin_toutlost STDVAR
{
Function F = (Function) cd;
BADARGS(6, 6, " hand nick path acked length");
CHECKVALIDITY(builtin_toutlost);
F(argv[1], argv[2], argv[3], argv[4], argv[5]);
return TCL_OK;
}
static void check_tcl_sentrcvd(struct userrec *u, char *nick, char *path,
p_tcl_bind_list h)
{
struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
char *hand = u ? u->handle : "*";
get_user_flagrec(u, &fr, NULL);
Tcl_SetVar(interp, "_sr1", hand, 0);
Tcl_SetVar(interp, "_sr2", nick, 0);
Tcl_SetVar(interp, "_sr3", path, 0);
check_tcl_bind(h, hand, &fr, " $_sr1 $_sr2 $_sr3",
MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE);
}
static void check_tcl_toutlost(struct userrec *u, char *nick, char *path,
unsigned long acked, unsigned long length,
p_tcl_bind_list h)
{
struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
char *hand = u ? u->handle : "*";
char s[15];
get_user_flagrec(u, &fr, NULL);
Tcl_SetVar(interp, "_sr1", hand, 0);
Tcl_SetVar(interp, "_sr2", nick, 0);
Tcl_SetVar(interp, "_sr3", path, 0);
egg_snprintf(s, sizeof s, "%lu", acked);
Tcl_SetVar(interp, "_sr4", s, 0);
egg_snprintf(s, sizeof s, "%lu", length);
Tcl_SetVar(interp, "_sr5", s, 0);
check_tcl_bind(h, hand, &fr, " $_sr1 $_sr2 $_sr3 $_sr4 $_sr5",
MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE);
}
/*
* DCC routines
*/
/* Instead of reading all data intended to go into the DCC block
* in one go, we read it in PMAX_SIZE chunks, feed it to tputs and
* continue until we get to know that the network buffer only
* buffers the data instead of sending it.
*
* In that case, we delay further sending until we receive the
* dcc outdone event.
*
* Note: To optimize buffer sizes, we default to PMAX_SIZE, but
* allocate a smaller buffer for smaller pending_data sizes.
*/
#define PMAX_SIZE 4096
static unsigned long pump_file_to_sock(FILE *file, long sock,
register unsigned long pending_data)
{
register unsigned long actual_size;
const unsigned long buf_len = pending_data >= PMAX_SIZE ?
PMAX_SIZE : pending_data;
char *bf = nmalloc(buf_len);
if (bf) {
do {
actual_size = pending_data >= buf_len ? buf_len : pending_data;
fread(bf, actual_size, 1, file);
tputs(sock, bf, actual_size);
pending_data -= actual_size;
} while (!sock_has_data(SOCK_DATA_OUTGOING, sock) && pending_data != 0);
nfree(bf);
}
return pending_data;
}
static void eof_dcc_fork_send(int idx)
{
fclose(dcc[idx].u.xfer->f);
if (!strcmp(dcc[idx].nick, "*users")) {
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
(dcc[x].type->flags & DCT_BOT)) {
y = x;
break;
}
if (y != 0) {
dcc[y].status &= ~STAT_GETTING;
dcc[y].status &= ~STAT_SHARE;
}
putlog(LOG_BOTS, "*", USERF_FAILEDXFER);
unlink(dcc[idx].u.xfer->filename);
} else {
if (!quiet_reject)
dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[idx].nick,
DCC_CONNECTFAILED1, strerror(errno));
putlog(LOG_MISC, "*", "%s: SEND %s (%s!%s)", DCC_CONNECTFAILED2,
dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
putlog(LOG_MISC, "*", " (%s)", strerror(errno));
}
killsock(dcc[idx].sock);
lostdcc(idx);
}
static void eof_dcc_send(int idx)
{
int ok, j;
char *nfn, s[1024], *hand;
struct userrec *u;
/* Success */
if (dcc[idx].u.xfer->length == dcc[idx].status) {
int l = strlen(dcc[idx].u.xfer->filename);
if (l > NAME_MAX) { /* Filename is too long. */
fclose(dcc[idx].u.xfer->f);
putlog(LOG_FILES, "*", TRANSFER_FILENAME_TOOLONG, l);
dprintf(DP_HELP, TRANSFER_NOTICE_FNTOOLONG, dcc[idx].nick, l);
putlog(LOG_FILES, "*", TRANSFER_TOO_BAD);
dprintf(DP_HELP, TRANSFER_NOTICE_TOOBAD, dcc[idx].nick);
killsock(dcc[idx].sock);
lostdcc(idx);
return;
}
nfn = nmalloc(strlen(dcc[idx].u.xfer->dir)
+ strlen(dcc[idx].u.xfer->origname) + 1);
sprintf(nfn, "%s%s", dcc[idx].u.xfer->dir, dcc[idx].u.xfer->origname);
if (copy_to_tmp && (l = fcopyfile(dcc[idx].u.xfer->f, nfn))) {
putlog(LOG_MISC | LOG_FILES, "*", TRANSFER_FAILED_MOVE, nfn);
}
/* Only close now in case it was a tmpfile, as it's deleted upon close */
fclose(dcc[idx].u.xfer->f);
/* lookup handle */
egg_snprintf(s, sizeof s, "%s!%s", dcc[idx].nick, dcc[idx].host);
u = get_user_by_host(s);
hand = u ? u->handle : "*";
/* Add to file database if not tmpfile or if copyfile succeeded */
if (!copy_to_tmp || !l) {
module_entry *fs = module_find("filesys", 0, 0);
if (fs != NULL) {
Function f = fs->funcs[FILESYS_ADDFILE];
f(dcc[idx].u.xfer->dir, dcc[idx].u.xfer->origname, hand);
}
stats_add_upload(u, dcc[idx].u.xfer->length);
check_tcl_sentrcvd(u, dcc[idx].nick, nfn, H_rcvd);
}
nfree(nfn);
ok = 0;
if (!strcmp(dcc[idx].nick, "*users")) {
module_entry *me = module_find("share", 0, 0);
if (me && me->funcs) {
Function f = me->funcs[SHARE_FINISH];
(f) (idx);
}
} else {
putlog(LOG_FILES, "*", TRANSFER_COMPLETED_DCC, dcc[idx].u.xfer->origname,
dcc[idx].nick, dcc[idx].host);
for (j = 0; j < dcc_total; j++)
if (!ok && (dcc[j].type->flags & (DCT_GETNOTES | DCT_FILES)) &&
!egg_strcasecmp(dcc[j].nick, hand)) {
ok = 1;
dprintf(j, TRANSFER_THANKS);
}
if (!ok)
dprintf(DP_HELP, TRANSFER_NOTICE_THANKS, dcc[idx].nick);
}
killsock(dcc[idx].sock);
lostdcc(idx);
return;
}
/* Failure :( */
fclose(dcc[idx].u.xfer->f);
if (!strcmp(dcc[idx].nick, "*users")) {
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
(dcc[x].type->flags & DCT_BOT))
y = x;
if (y) {
putlog(LOG_BOTS, "*", TRANSFER_USERFILE_LOST, dcc[y].nick);
unlink(dcc[idx].u.xfer->filename);
/* Drop that bot */
dprintf(y, "bye\n");
egg_snprintf(s, sizeof s, TRANSFER_USERFILE_DISCON, dcc[y].nick);
botnet_send_unlinked(y, dcc[y].nick, s);
putlog(LOG_BOTS, "*", "%s.", s);
if (y != idx) {
killsock(dcc[y].sock);
lostdcc(y);
}
killsock(dcc[idx].sock);
lostdcc(idx);
}
} else {
putlog(LOG_FILES, "*", TRANSFER_LOST_DCCSEND, dcc[idx].u.xfer->origname,
dcc[idx].nick, dcc[idx].host, dcc[idx].status,
dcc[idx].u.xfer->length);
killsock(dcc[idx].sock);
lostdcc(idx);
}
}
/* Determine byte order. Used for resend DCC startup packets.
*/
static inline u_8bit_t byte_order_test(void)
{
u_16bit_t test = TRANSFER_REGET_PACKETID;
if (*((u_8bit_t *) & test) == ((TRANSFER_REGET_PACKETID & 0xff00) >> 8))
return 0;
if (*((u_8bit_t *) & test) == (TRANSFER_REGET_PACKETID & 0x00ff))
return 1;
return 0;
}
/* Parse and handle resend DCC startup packets.
*/
static inline void handle_resend_packet(int idx, transfer_reget *reget_data)
{
if (byte_order_test() != reget_data->byte_order) {
/* The sender's byte order does not match our's so we need to switch the
* bytes first, before we can make use of them.
*/
reget_data->packet_id = ((reget_data->packet_id & 0x00ff) << 8) |
((reget_data->packet_id & 0xff00) >> 8);
reget_data->byte_offset = ((reget_data->byte_offset & 0xff000000) >> 24) |
((reget_data->byte_offset & 0x00ff0000) >> 8) |
((reget_data->byte_offset & 0x0000ff00) << 8) |
((reget_data->byte_offset & 0x000000ff) << 24);
}
if (reget_data->packet_id != TRANSFER_REGET_PACKETID)
putlog(LOG_FILES, "*", TRANSFER_REGET_PACKET, dcc[idx].nick,
dcc[idx].u.xfer->origname);
else
dcc[idx].u.xfer->offset = reget_data->byte_offset;
dcc[idx].u.xfer->type = XFER_RESEND;
}
/* Handles DCC packets the client sends us. As soon as the last sent dcc
* block is fully acknowledged we send the next block.
*
* Note: The first received packet during reget is a special 8 bit packet
* containing special information.
*/
static void dcc_get(int idx, char *buf, int len)
{
char xnick[NICKLEN];
unsigned char bbuf[4];
unsigned long cmp, l;
int w = len + dcc[idx].u.xfer->sofar, p = 0;
dcc[idx].timeval = now; /* Mark as active */
/* Add bytes to our buffer if we don't have a complete response yet.
* This is either a 4 bit ack or the 8 bit reget packet.
*/
if (w < 4 || (w < 8 && dcc[idx].u.xfer->type == XFER_RESEND_PEND)) {
my_memcpy(&(dcc[idx].u.xfer->buf[dcc[idx].u.xfer->sofar]), buf, len);
dcc[idx].u.xfer->sofar += len;
return;
/* Waiting for the 8 bit reget packet? */
} else if (dcc[idx].u.xfer->type == XFER_RESEND_PEND) {
/* The 8 bit packet is complete now. Parse it. */
if (w == 8) {
transfer_reget reget_data;
my_memcpy(®et_data, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
my_memcpy(®et_data + dcc[idx].u.xfer->sofar, buf, len);
handle_resend_packet(idx, ®et_data);
cmp = dcc[idx].u.xfer->offset;
} else
return;
/* Fall through! */
/* No, only want 4 bit ack responses. */
} else {
/* Complete packet? */
if (w == 4) {
my_memcpy(bbuf, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
my_memcpy(&(bbuf[dcc[idx].u.xfer->sofar]), buf, len);
} else {
p = ((w - 1) & ~3) - dcc[idx].u.xfer->sofar;
w = w - ((w - 1) & ~3);
if (w < 4) {
my_memcpy(dcc[idx].u.xfer->buf, &(buf[p]), w);
return;
}
my_memcpy(bbuf, &(buf[p]), w);
}
/* This is more compatible than ntohl for machines where an int
* is more than 4 bytes:
*/
cmp = ((unsigned int) (bbuf[0]) << 24) +
((unsigned int) (bbuf[1]) << 16) +
((unsigned int) (bbuf[2]) << 8) + bbuf[3];
dcc[idx].u.xfer->acked = cmp;
}
dcc[idx].u.xfer->sofar = 0;
if (cmp > dcc[idx].u.xfer->length && cmp > dcc[idx].status) {
/* Attempt to resume, but file is not as long as requested... */
putlog(LOG_FILES, "*", TRANSFER_BEHIND_FILEEND, dcc[idx].u.xfer->origname,
dcc[idx].nick);
} else if (cmp > dcc[idx].status) {
/* Attempt to resume */
if (!strcmp(dcc[idx].nick, "*users"))
putlog(LOG_BOTS, "*", TRANSFER_TRY_SKIP_AHEAD);
else {
fseek(dcc[idx].u.xfer->f, cmp, SEEK_SET);
dcc[idx].status = cmp;
putlog(LOG_FILES, "*", TRANSFER_RESUME_FILE, (int) (cmp / 1024),
dcc[idx].u.xfer->origname, dcc[idx].nick);
}
} else {
if (dcc[idx].u.xfer->ack_type == XFER_ACK_UNKNOWN) {
if (cmp < dcc[idx].u.xfer->offset)
/* If we don't start at the top of the file, some clients only tell
* us the really received bytes (e.g. bitchx). This seems to be the
* case here.
*/
dcc[idx].u.xfer->ack_type = XFER_ACK_WITHOUT_OFFSET;
else
dcc[idx].u.xfer->ack_type = XFER_ACK_WITH_OFFSET;
}
if (dcc[idx].u.xfer->ack_type == XFER_ACK_WITHOUT_OFFSET)
cmp += dcc[idx].u.xfer->offset;
}
if (cmp != dcc[idx].status)
return;
if (dcc[idx].status == dcc[idx].u.xfer->length) {
/* Successful send, we are done */
killsock(dcc[idx].sock);
fclose(dcc[idx].u.xfer->f);
if (!strcmp(dcc[idx].nick, "*users")) {
module_entry *me = module_find("share", 0, 0);
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
(dcc[x].type->flags & DCT_BOT))
y = x;
if (y != 0)
dcc[y].status &= ~STAT_SENDING;
putlog(LOG_BOTS, "*", TRANSFER_COMPLETED_USERFILE, dcc[y].nick);
unlink(dcc[idx].u.xfer->filename);
/* Any sharebot things that were queued: */
if (me && me->funcs[SHARE_DUMP_RESYNC])
((me->funcs)[SHARE_DUMP_RESYNC]) (y);
xnick[0] = 0;
} else {
module_entry *fs = module_find("filesys", 0, 0);
struct userrec *u = get_user_by_handle(userlist, dcc[idx].u.xfer->from);
check_tcl_sentrcvd(u, dcc[idx].nick, dcc[idx].u.xfer->dir, H_sent);
if (fs != NULL) {
Function f = fs->funcs[FILESYS_INCRGOTS];
f(dcc[idx].u.xfer->dir);
}
/* Download is credited to the user who requested it
* (not the user who actually received it)
*/
stats_add_dnload(u, dcc[idx].u.xfer->length);
putlog(LOG_FILES, "*", TRANSFER_FINISHED_DCCSEND,
dcc[idx].u.xfer->origname, dcc[idx].nick);
strcpy((char *) xnick, dcc[idx].nick);
}
lostdcc(idx);
/* Any to dequeue? */
if (!at_limit(xnick))
send_next_file(xnick);
return;
}
/* Note: No fseek() needed here, because the file position is kept from
* the last run.
*/
l = dcc_block;
if (l == 0 || dcc[idx].status + l > dcc[idx].u.xfer->length)
l = dcc[idx].u.xfer->length - dcc[idx].status;
dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
dcc[idx].sock, l);
dcc[idx].status += l;
}
static void eof_dcc_get(int idx)
{
char xnick[NICKLEN], s[1024];
fclose(dcc[idx].u.xfer->f);
if (!strcmp(dcc[idx].nick, "*users")) {
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
(dcc[x].type->flags & DCT_BOT))
y = x;
putlog(LOG_BOTS, "*", TRANSFER_ABORT_USERFILE);
/* Note: no need to unlink the xfer file, as it's already unlinked. */
xnick[0] = 0;
/* Drop that bot */
dprintf(-dcc[y].sock, "bye\n");
egg_snprintf(s, sizeof s, TRANSFER_USERFILE_DISCON, dcc[y].nick);
botnet_send_unlinked(y, dcc[y].nick, s);
putlog(LOG_BOTS, "*", "%s.", s);
if (y != idx) {
killsock(dcc[y].sock);
lostdcc(y);
}
killsock(dcc[idx].sock);
lostdcc(idx);
return;
} else {
struct userrec *u;
/* Call `lost' DCC trigger now.
*/
egg_snprintf(s, sizeof s, "%s!%s", dcc[idx].nick, dcc[idx].host);
u = get_user_by_host(s);
check_tcl_toutlost(u, dcc[idx].nick, dcc[idx].u.xfer->dir,
dcc[idx].u.xfer->acked, dcc[idx].u.xfer->length, H_lost);
putlog(LOG_FILES, "*", TRANSFER_LOST_DCCGET,
dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
strcpy(xnick, dcc[idx].nick);
}
killsock(dcc[idx].sock);
lostdcc(idx);
/* Send next queued file if there is one */
if (!at_limit(xnick))
send_next_file(xnick);
}
static void dcc_send(int idx, char *buf, int len)
{
char s[512];
unsigned long sent;
fwrite(buf, len, 1, dcc[idx].u.xfer->f);
dcc[idx].status += len;
/* Put in network byte order */
sent = dcc[idx].status;
s[0] = (sent / (1 << 24));
s[1] = (sent % (1 << 24)) / (1 << 16);
s[2] = (sent % (1 << 16)) / (1 << 8);
s[3] = (sent % (1 << 8));
tputs(dcc[idx].sock, s, 4);
dcc[idx].timeval = now;
if (dcc[idx].status > dcc[idx].u.xfer->length && dcc[idx].u.xfer->length > 0) {
dprintf(DP_HELP, TRANSFER_BOGUS_FILE_LENGTH, dcc[idx].nick);
putlog(LOG_FILES, "*", TRANSFER_FILE_TOO_LONG, dcc[idx].u.xfer->origname,
dcc[idx].nick, dcc[idx].host);
fclose(dcc[idx].u.xfer->f);
killsock(dcc[idx].sock);
lostdcc(idx);
}
}
static void transfer_get_timeout(int i)
{
char xx[1024];
fclose(dcc[i].u.xfer->f);
if (strcmp(dcc[i].nick, "*users") == 0) {
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if ((!egg_strcasecmp(dcc[x].nick, dcc[i].host)) &&
(dcc[x].type->flags & DCT_BOT))
y = x;
if (y != 0) {
dcc[y].status &= ~STAT_SENDING;
dcc[y].status &= ~STAT_SHARE;
}
unlink(dcc[i].u.xfer->filename);
putlog(LOG_BOTS, "*", TRANSFER_USERFILE_TIMEOUT);
dprintf(y, "bye\n");
egg_snprintf(xx, sizeof xx, TRANSFER_DICONNECT_TIMEOUT, dcc[y].nick);
botnet_send_unlinked(y, dcc[y].nick, xx);
putlog(LOG_BOTS, "*", "%s.", xx);
if (y < i) {
int t = y;
y = i;
i = t;
}
killsock(dcc[y].sock);
lostdcc(y);
xx[0] = 0;
} else {
char *p;
struct userrec *u;
p = strrchr(dcc[i].u.xfer->origname, '/');
dprintf(DP_HELP, TRANSFER_NOTICE_TIMEOUT, dcc[i].nick,
p ? p + 1 : dcc[i].u.xfer->origname);
/* Call DCC `timeout' trigger now.
*/
egg_snprintf(xx, sizeof xx, "%s!%s", dcc[i].nick, dcc[i].host);
u = get_user_by_host(xx);
check_tcl_toutlost(u, dcc[i].nick, dcc[i].u.xfer->dir,
dcc[i].u.xfer->acked, dcc[i].u.xfer->length, H_tout);
putlog(LOG_FILES, "*", TRANSFER_DCC_GET_TIMEOUT,
p ? p + 1 : dcc[i].u.xfer->origname, dcc[i].nick, dcc[i].status,
dcc[i].u.xfer->length);
strcpy(xx, dcc[i].nick);
}
killsock(dcc[i].sock);
lostdcc(i);
if (!at_limit(xx))
send_next_file(xx);
}
static void tout_dcc_send(int idx)
{
fclose(dcc[idx].u.xfer->f);
if (!strcmp(dcc[idx].nick, "*users")) {
int x, y = 0;
for (x = 0; x < dcc_total; x++)
if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
(dcc[x].type->flags & DCT_BOT))
y = x;
if (y != 0) {
dcc[y].status &= ~STAT_GETTING;
dcc[y].status &= ~STAT_SHARE;
}
unlink(dcc[idx].u.xfer->filename);
putlog(LOG_BOTS, "*", TRANSFER_USERFILE_TIMEOUT);
} else {
dprintf(DP_HELP, TRANSFER_NOTICE_TIMEOUT, dcc[idx].nick,
dcc[idx].u.xfer->origname);
putlog(LOG_FILES, "*", TRANSFER_DCC_SEND_TIMEOUT, dcc[idx].u.xfer->origname,
dcc[idx].nick, dcc[idx].status, dcc[idx].u.xfer->length);
}
killsock(dcc[idx].sock);
lostdcc(idx);
}
static void display_dcc_get(int idx, char *buf)
{
if (dcc[idx].status == dcc[idx].u.xfer->length)
sprintf(buf, TRANSFER_SEND, dcc[idx].u.xfer->acked,
dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
else
sprintf(buf, TRANSFER_SEND, dcc[idx].status,
dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
}
static void display_dcc_get_p(int idx, char *buf)
{
sprintf(buf, TRANSFER_SEND_WAITED, now - dcc[idx].timeval,
dcc[idx].u.xfer->origname);
}
static void display_dcc_send(int idx, char *buf)
{
sprintf(buf, TRANSFER_SEND, dcc[idx].status,
dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
}
static void display_dcc_fork_send(int idx, char *buf)
{
sprintf(buf, "%s", TRANSFER_CONN_SEND);
}
static int expmem_dcc_xfer(void *x)
{
register struct xfer_info *p = (struct xfer_info *) x;
int tot;
tot = sizeof(struct xfer_info);
if (p->filename)
tot += strlen(p->filename) + 1;
/* We need to check if origname points to filename before
* accounting for the memory.
*/
if (p->origname && p->filename != p->origname)
tot += strlen(p->origname) + 1;
return tot;
}
static void kill_dcc_xfer(int idx, void *x)
{
register struct xfer_info *p = (struct xfer_info *) x;
if (p->filename)
nfree(p->filename);
/* We need to check if origname points to filename before
* attempting to free the memory.
*/
if (p->origname && p->origname != p->filename)
nfree(p->origname);
nfree(x);
}
static void out_dcc_xfer(int idx, char *buf, void *x)
{
}
static void outdone_dcc_xfer(int idx)
{
if (dcc[idx].u.xfer->block_pending)
dcc[idx].u.xfer->block_pending =
pump_file_to_sock(dcc[idx].u.xfer->f, dcc[idx].sock,
dcc[idx].u.xfer->block_pending);
}
/* Send TO the bot - Wcc */
static struct dcc_table DCC_SEND = {
"SEND",
DCT_FILETRAN | DCT_FILESEND | DCT_VALIDIDX,
eof_dcc_send,
dcc_send,
&wait_dcc_xfer,
tout_dcc_send,
display_dcc_send,
expmem_dcc_xfer,
kill_dcc_xfer,
out_dcc_xfer
};
/* Send TO the bot from outside of the transfer module - Wcc */
static struct dcc_table DCC_FORK_SEND = {
"FORK_SEND",
DCT_FILETRAN | DCT_FORKTYPE | DCT_FILESEND | DCT_VALIDIDX,
eof_dcc_fork_send,
dcc_fork_send,
&wait_dcc_xfer,
eof_dcc_fork_send,
display_dcc_fork_send,
expmem_dcc_xfer,
kill_dcc_xfer,
out_dcc_xfer
};
/* Send FROM the bot, don't know why this isn't called DCC_SEND - Wcc */
static struct dcc_table DCC_GET = {
"GET",
DCT_FILETRAN | DCT_VALIDIDX,
eof_dcc_get,
dcc_get,
&wait_dcc_xfer,
transfer_get_timeout,
display_dcc_get,
expmem_dcc_xfer,
kill_dcc_xfer,
out_dcc_xfer,
outdone_dcc_xfer
};
/* Send FROM the bot - Wcc */
static struct dcc_table DCC_GET_PENDING = {
"GET_PENDING",
DCT_FILETRAN | DCT_VALIDIDX,
eof_dcc_get,
dcc_get_pending,
&wait_dcc_xfer,
transfer_get_timeout,
display_dcc_get_p,
expmem_dcc_xfer,
kill_dcc_xfer,
out_dcc_xfer
};
static void dcc_fork_send(int idx, char *x, int y)
{
char s1[121];
if (dcc[idx].type != &DCC_FORK_SEND)
return;
dcc[idx].type = &DCC_SEND;
dcc[idx].u.xfer->start_time = now;
if (strcmp(dcc[idx].nick, "*users")) {
egg_snprintf(s1, sizeof s1, "%s!%s", dcc[idx].nick, dcc[idx].host);
putlog(LOG_MISC, "*", TRANSFER_DCC_CONN, dcc[idx].u.xfer->origname, s1);
}
if (dcc[idx].type->activity && y) {
/* Could already have data! */
dcc[idx].type->activity(idx, x, y);
}
}
static void dcc_get_pending(int idx, char *buf, int len)
{
unsigned short port;
int i;
i = answer(dcc[idx].sock, &dcc[idx].sockname, &port, 1);
killsock(dcc[idx].sock);
#ifdef TLS
if (dcc[idx].ssl && ssl_handshake(i, TLS_LISTEN, tls_vfydcc,
LOG_FILES, dcc[idx].host, NULL)) {
putlog(LOG_FILES, "*", "DCC failed SSL handshake: GET %s (%s!%s)",
dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
lostdcc(idx);
return;
}
#endif
dcc[idx].sock = i;
dcc[idx].addr = 0;
dcc[idx].port = (int) port;
if (dcc[idx].sock == -1) {
dprintf(DP_HELP, TRANSFER_NOTICE_BAD_CONN, dcc[idx].nick, strerror(errno));
putlog(LOG_FILES, "*", TRANSFER_LOG_BAD_CONN, dcc[idx].u.xfer->origname,
dcc[idx].nick, dcc[idx].host);
fclose(dcc[idx].u.xfer->f);
lostdcc(idx);
return;
}
dcc[idx].type = &DCC_GET;
dcc[idx].u.xfer->ack_type = XFER_ACK_UNKNOWN;
/*
* Note: The file was already opened and dcc[idx].u.xfer->f may be
* used immediately. Leave it opened until the file transfer
* is complete.
*/
if (dcc[idx].u.xfer->type == XFER_RESUME_PEND) { /* Are we resuming? */
int l;
if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block) {
l = dcc[idx].u.xfer->length - dcc[idx].u.xfer->offset;
dcc[idx].status = dcc[idx].u.xfer->length;
} else {
l = dcc_block;
dcc[idx].status = dcc[idx].u.xfer->offset + dcc_block;
}
/* Seek forward ... */
fseek(dcc[idx].u.xfer->f, dcc[idx].u.xfer->offset, SEEK_SET);
dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
dcc[idx].sock, l);
dcc[idx].u.xfer->type = XFER_RESUME;
} else {
dcc[idx].u.xfer->offset = 0;
/* If we're resending the data, wait for the client's response first,
* before sending anything ourself.
*/
if (dcc[idx].u.xfer->type != XFER_RESEND_PEND) {
if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block)
dcc[idx].status = dcc[idx].u.xfer->length;
else
dcc[idx].status = dcc_block;
dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
dcc[idx].sock,
dcc[idx].status);
} else
dcc[idx].status = 0;
}
dcc[idx].timeval = dcc[idx].u.xfer->start_time = now;
}
/* Starts a new DCC SEND or DCC RESEND connection to `nick', transferring
* `filename' from `dir'.
*
* Use raw_dcc_resend() and raw_dcc_send() instead of this function.
*
*/
static int raw_dcc_resend_send(char *filename, char *nick, char *from,
int resend)
{
int zz, port, i;
char *nfn, *buf = NULL;
long dccfilesize;
FILE *f;
zz = -1;
f = fopen(filename, "r");
if (!f)
return DCCSEND_BADFN;
fseek(f, 0, SEEK_END);
dccfilesize = ftell(f);
fclose(f);
if (dccfilesize == 0)
return DCCSEND_FEMPTY;
if (reserved_port_min > 0 && reserved_port_min < reserved_port_max) {
for (port = reserved_port_min; port <= reserved_port_max; port++)
if ((zz = open_listen(&port)) != -1)
break;
} else {
port = reserved_port_min;
zz = open_listen(&port);
}
if (zz == -1)
return DCCSEND_NOSOCK;
nfn = strrchr(filename, '/');
if (nfn == NULL)
nfn = filename;
else
nfn++;
if (copy_to_tmp) {
f = tmpfile();
if (!f)
return DCCSEND_BADFN;
if (copyfilef(filename, f)) {
fclose(f);
return DCCSEND_FCOPY;
}
} else
f = fopen(filename, "r");
if (!f)
return DCCSEND_BADFN;
if ((i = new_dcc(&DCC_GET_PENDING, sizeof(struct xfer_info))) == -1) {
fclose(f);