-
Notifications
You must be signed in to change notification settings - Fork 6
/
send.c
1416 lines (1226 loc) · 41.2 KB
/
send.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
/* send.c
Routines to send a file.
Copyright (C) 1991, 1992, 1993, 1994, 1995, 2002 Ian Lance Taylor
This file is part of the Taylor UUCP package.
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.
The author of the program may be contacted at [email protected].
*/
#include "uucp.h"
#if USE_RCS_ID
const char send_rcsid[] = "$Id$";
#endif
#include <errno.h>
#include "uudefs.h"
#include "uuconf.h"
#include "system.h"
#include "prot.h"
#include "trans.h"
/* We keep this information in the pinfo field of the stransfer
structure. */
struct ssendinfo
{
/* Local user to send mail to (may be NULL). */
char *zmail;
/* Full file name. */
char *zfile;
/* Number of bytes in file. */
long cbytes;
/* TRUE if this was a local request. */
boolean flocal;
/* TRUE if this is a spool directory file. */
boolean fspool;
/* TRUE if the file has been completely sent. */
boolean fsent;
/* TRUE if the file send will never succeed; used by
flocal_send_cancelled. */
boolean fnever;
/* Execution file for sending an unsupported E request. */
char *zexec;
/* Confirmation command received in fsend_await_confirm. */
char *zconfirm;
};
/* Local functions. */
static void usfree_send P((struct stransfer *qtrans));
static boolean flocal_send_fail P((struct scmd *qcmd,
struct sdaemon *qdaemon,
const char *zwhy));
static boolean flocal_send_request P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean flocal_send_await_reply P((struct stransfer *qtrans,
struct sdaemon *qdaemon,
const char *zdata, size_t cdata));
static boolean flocal_send_cancelled P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean flocal_send_open_file P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean fremote_rec_fail P((struct sdaemon *qdaemon,
enum tfailure twhy, int iremote));
static boolean fremote_rec_fail_send P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean fremote_rec_reply P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean fsend_file_end P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static boolean fsend_await_confirm P((struct stransfer *qtrans,
struct sdaemon *qdaemon,
const char *zdata, size_t cdata));
static boolean fsend_exec_file_init P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
static void usadd_exec_line P((char **pz, size_t *pcalc, size_t *pclen,
int bcmd, const char *z1, const char *z2,
boolean fquote));
static boolean fsend_exec_file P((struct stransfer *qtrans,
struct sdaemon *qdaemon));
/* Free up a send stransfer structure. */
static void
usfree_send (struct stransfer *qtrans)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
if (qinfo != NULL)
{
ubuffree (qinfo->zmail);
ubuffree (qinfo->zfile);
ubuffree (qinfo->zexec);
ubuffree (qinfo->zconfirm);
xfree (qtrans->pinfo);
}
utransfree (qtrans);
}
/* Set up a local request to send a file. This may be called before
we have even tried to call the remote system.
If we are using a traditional protocol, which doesn't support
channel numbers and doesn't permit the file to be sent until an
acknowledgement has been received, the sequence of function calls
looks like this:
flocal_send_file_init --> fqueue_local
flocal_send_request (sends S request) --> fqueue_receive
flocal_send_await_reply (waits for SY) --> fqueue_send
flocal_send_open_file (opens file, calls pffile) --> fqueue_send
send file
fsend_file_end (calls pffile) --> fqueue_receive
fsend_await_confirm (waits for CY)
If flocal_send_await_reply gets an SN, it deletes the request. If
the SY reply contains a file position at which to start sending,
flocal_send_await_reply sets qinfo->ipos.
This gets more complex if the protocol supports channels. In that
case, we want to start sending the file data immediately, to avoid
the round trip delay between flocal_send_request and
flocal_send_await_reply. To do this, flocal_send_request calls
fqueue_send rather than fqueue_receive. The main execution
sequence looks like this:
flocal_send_file_init --> fqueue_local
flocal_send_request (sends S request) --> fqueue_send
flocal_send_open_file (opens file, calls pffile) --> fqueue_send
send file
fsend_file_end (calls pffile) --> fqueue_receive
sometime: flocal_send_await_reply (waits for SY)
fsend_await_confirm (waits for CY)
In this case flocal_send_await_reply must be run before
fsend_await_confirm; it may be run anytime after
flocal_send_request.
If flocal_send_await_reply is called before the entire file has
been sent: if it gets an SN, it sets the file position to the end
and arranges to call flocal_send_cancelled. If it gets a file
position request, it must adjust the file position accordingly.
If flocal_send_await_reply is called after the entire file has been
sent: if it gets an SN, it can simply delete the request. It can
ignore any file position request.
If the request is not deleted, flocal_send_await_reply must arrange
for the next string to be passed to fsend_await_confirm.
Presumably fsend_await_confirm will only be called after the entire
file has been sent.
Just to make things even more complex, these same routines support
sending execution requests, since that is much like sending a file.
For an execution request, the bcmd character will be E rather than
S. If an execution request is being sent to a system which does
not support them, it must be sent as two S requests instead. The
second one will be the execution file, but no actual file is
created; instead the zexec and znext fields in the ssendinfo
structure are used. So if the bcmd character is E, then if the
zexec field is NULL, the data file is being sent, otherwise the
fake execution file is being sent. */
boolean
flocal_send_file_init (struct sdaemon *qdaemon, struct scmd *qcmd)
{
const struct uuconf_system *qsys;
boolean fspool;
char *zfile;
long cbytes;
struct ssendinfo *qinfo;
struct stransfer *qtrans;
qsys = qdaemon->qsys;
if (qdaemon->fcaller
? ! qsys->uuconf_fcall_transfer
: ! qsys->uuconf_fcalled_transfer)
{
/* uux or uucp should have already made sure that the transfer
is possible, but it might have changed since then. */
if (! qsys->uuconf_fcall_transfer
&& ! qsys->uuconf_fcalled_transfer)
return flocal_send_fail (qcmd, qdaemon,
"not permitted to transfer files");
/* We can't do the request now, but it may get done later. */
return TRUE;
}
/* The 'C' option means that the file has been copied to the spool
directory. */
if (strchr (qcmd->zoptions, 'C') == NULL
&& ! fspool_file (qcmd->zfrom))
{
fspool = FALSE;
if (! fin_directory_list (qcmd->zfrom,
qsys->uuconf_pzlocal_send,
qsys->uuconf_zpubdir, TRUE,
TRUE, qcmd->zuser))
return flocal_send_fail (qcmd, qdaemon, "not permitted to send");
zfile = zbufcpy (qcmd->zfrom);
}
else
{
fspool = TRUE;
zfile = zsysdep_spool_file_name (qsys, qcmd->ztemp, qcmd->pseq);
if (zfile == NULL)
return FALSE;
}
/* Make sure we meet any local size restrictions. The connection
may not have been opened at this point, so we can't check remote
size restrictions. */
cbytes = csysdep_size (zfile);
if (cbytes < 0)
{
ubuffree (zfile);
if (cbytes != -1)
return flocal_send_fail (qcmd, qdaemon, "can not get size");
/* A cbytes value of -1 means that the file does not exist.
This can happen legitimately if it has already been sent from
the spool directory. */
if (! fspool)
return flocal_send_fail (qcmd, qdaemon, "does not exist");
(void) fsysdep_did_work (qcmd->pseq);
return TRUE;
}
if (qdaemon->clocal_size != -1
&& qdaemon->clocal_size < cbytes)
{
ubuffree (zfile);
if (qdaemon->cmax_ever == -2)
{
long c1, c2;
c1 = cmax_size_ever (qsys->uuconf_qcall_local_size);
c2 = cmax_size_ever (qsys->uuconf_qcalled_local_size);
if (c1 > c2)
qdaemon->cmax_ever = c1;
else
qdaemon->cmax_ever = c2;
}
if (qdaemon->cmax_ever != -1
&& qdaemon->cmax_ever < qcmd->cbytes)
return flocal_send_fail (qcmd, qdaemon, "too large to send");
return TRUE;
}
/* We are now prepared to send the command to the remote system. We
queue up a transfer request to send the command when we are
ready. */
qinfo = (struct ssendinfo *) xmalloc (sizeof (struct ssendinfo));
if (strchr (qcmd->zoptions, 'm') == NULL)
qinfo->zmail = NULL;
else
qinfo->zmail = zbufcpy (qcmd->zuser);
qinfo->zfile = zfile;
qinfo->cbytes = cbytes;
qinfo->flocal = strchr (qcmd->zuser, '!') == NULL;
qinfo->fspool = fspool;
qinfo->fsent = FALSE;
qinfo->zexec = NULL;
qinfo->zconfirm = NULL;
qtrans = qtransalc (qcmd);
qtrans->psendfn = flocal_send_request;
qtrans->pinfo = (pointer) qinfo;
return fqueue_local (qdaemon, qtrans);
}
/* Clean up after a failing local send request. If zwhy is not NULL,
this reports an error to the log file and to the user. */
static boolean
flocal_send_fail (struct scmd *qcmd, struct sdaemon *qdaemon, const char *zwhy)
{
if (zwhy != NULL)
{
const char *zfrom;
char *zfree;
const char *ztemp;
if (qcmd->bcmd != 'E')
{
zfrom = qcmd->zfrom;
zfree = NULL;
}
else
{
zfree = zbufalc (strlen (qcmd->zfrom)
+ sizeof " (execution of \"\")"
+ strlen (qcmd->zcmd));
sprintf (zfree, "%s (execution of \"%s\")", qcmd->zfrom,
qcmd->zcmd);
zfrom = zfree;
}
ulog (LOG_ERROR, "%s: %s", zfrom, zwhy);
/* We only save the temporary file if this is a request from the
local system; otherwise a remote system could launch a denial
of service attack by filling up the .Preserve directory
(local users have much simpler methods for this type of
denial of service attack, so there is little point to using a
more sophisticated scheme). */
if (strchr (qcmd->zuser, '!') == NULL)
ztemp = zsysdep_save_temp_file (qcmd->pseq);
else
ztemp = NULL;
(void) fmail_transfer (FALSE, qcmd->zuser, (const char *) NULL,
zwhy, zfrom, (const char *) NULL,
qcmd->zto, qdaemon->qsys->uuconf_zname, ztemp);
ubuffree (zfree);
}
(void) fsysdep_did_work (qcmd->pseq);
return TRUE;
}
/* This is called when we are ready to send the request to the remote
system. We form the request and send it over. If the protocol
does not support multiple channels, we start waiting for the
response; otherwise we can start sending the file immediately. */
static boolean
flocal_send_request (struct stransfer *qtrans, struct sdaemon *qdaemon)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
boolean fquote;
const struct scmd *qcmd;
struct scmd squoted;
const char *znotify;
char absize[20];
char *zsend;
boolean fret;
/* Make sure the file meets any remote size restrictions. */
if (qdaemon->cmax_receive != -1
&& qdaemon->cmax_receive < qinfo->cbytes)
{
fret = flocal_send_fail (&qtrans->s, qdaemon, "too large for receiver");
usfree_send (qtrans);
return fret;
}
/* Make sure the file still exists--it may have been removed between
the conversation startup and now. After we have sent over the S
command we must give an error if we can't find the file. */
if (! fsysdep_file_exists (qinfo->zfile))
{
(void) fsysdep_did_work (qtrans->s.pseq);
usfree_send (qtrans);
return TRUE;
}
/* If we are using a protocol which can make multiple channels, then
we can open and send the file whenever we are ready. This is
because we will be able to distinguish the response by the
channel it is directed to. This assumes that every protocol
which supports multiple channels also supports sending the file
position in mid-stream, since otherwise we would not be able to
restart files. */
qtrans->fcmd = TRUE;
qtrans->psendfn = flocal_send_open_file;
qtrans->precfn = flocal_send_await_reply;
if (qdaemon->cchans > 1)
fret = fqueue_send (qdaemon, qtrans);
else
fret = fqueue_receive (qdaemon, qtrans);
if (! fret)
return FALSE;
fquote = fcmd_needs_quotes (&qtrans->s);
if (! fquote)
qcmd = &qtrans->s;
else
{
if ((qdaemon->ifeatures & FEATURE_QUOTES) == 0)
{
fret = flocal_send_fail (&qtrans->s, qdaemon,
"remote system does not support required quoting");
usfree_send (qtrans);
return fret;
}
uquote_cmd (&qtrans->s, &squoted);
qcmd = &squoted;
}
/* Construct the notify string to send. If we are going to send a
size or an execution command, it must be non-empty. */
znotify = qcmd->znotify;
if (znotify == NULL)
znotify = "";
if ((qdaemon->ifeatures & FEATURE_SIZES) != 0
|| (qcmd->bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) != 0))
{
if (*znotify == '\0')
znotify = "\"\"";
}
else
{
/* We don't need a notify string. Some crufty UUCP code can't
handle a pair of double quotes. */
if (strcmp (znotify, "\"\"") == 0)
znotify = "";
}
/* Construct the size string to send. */
if ((qdaemon->ifeatures & FEATURE_SIZES) == 0
&& (qcmd->bcmd != 'E'
|| (qdaemon->ifeatures & FEATURE_EXEC) == 0))
absize[0] = '\0';
else if ((qdaemon->ifeatures & FEATURE_V103) == 0)
sprintf (absize, "0x%lx", (unsigned long) qinfo->cbytes);
else
sprintf (absize, "%ld", qinfo->cbytes);
zsend = zbufalc (strlen (qcmd->zfrom) + strlen (qcmd->zto)
+ strlen (qcmd->zuser) + strlen (qcmd->zoptions)
+ strlen (qcmd->ztemp) + strlen (znotify)
+ strlen (absize)
+ (qcmd->zcmd != NULL ? strlen (qcmd->zcmd) : 0)
+ 50);
/* If this an execution request and the other side supports
execution requests, we send an E command. Otherwise we send an S
command. The case of an execution request when we are sending
the fake execution file is handled just like an S request at this
point. */
if (qcmd->bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) != 0)
{
/* Send the string
E zfrom zto zuser zoptions ztemp imode znotify size zcmd
to the remote system. We put a '-' in front of the (possibly
empty) options and a '0' in front of the mode. */
sprintf (zsend, "E %s %s %s -%s %s 0%o %s %s %s", qcmd->zfrom,
qcmd->zto, qcmd->zuser, qcmd->zoptions,
qcmd->ztemp, qcmd->imode, znotify, absize,
qcmd->zcmd);
}
else
{
const char *zoptions, *zdummy;
/* Send the string
S zfrom zto zuser zoptions ztemp imode znotify
to the remote system. We put a '-' in front of the (possibly
empty) options and a '0' in front of the mode. If size
negotiation is supported, we also send the size; in this case
if znotify is empty we must send it as "". If this is really
an execution request, we have to simplify the options string
to remove the various execution options which may confuse the
remote system. SVR4 expects a string "dummy" between the
notify string and the size; I don't know why. */
if (qcmd->bcmd != 'E')
zoptions = qcmd->zoptions;
else if (strchr (qcmd->zoptions, 'C') != NULL)
{
/* This should set zoptions to "C", but at least one UUCP
program gets confused by it. That means that it will
fail in certain cases, but I suppose we might as well
kowtow to compatibility. This shouldn't matter to any
other program, I hope. */
zoptions = "";
}
else
zoptions = "c";
if ((qdaemon->ifeatures & FEATURE_SVR4) != 0)
zdummy = " dummy ";
else
zdummy = " ";
sprintf (zsend, "S %s %s %s -%s %s 0%o %s%s%s", qcmd->zfrom,
qcmd->zto, qcmd->zuser, zoptions,
qcmd->ztemp, qcmd->imode, znotify, zdummy,
absize);
}
fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, qtrans->ilocal,
qtrans->iremote);
ubuffree (zsend);
if (fquote)
ufree_quoted_cmd (&squoted);
/* If fret is FALSE, we should free qtrans here, but see the comment
at the end of flocal_rec_send_request. */
return fret;
}
/* This is called when a reply is received for the send request. As
described at length above, if the protocol supports multiple
channels we may be in the middle of sending the file, or we may
even finished sending the file. */
static boolean
flocal_send_await_reply (struct stransfer *qtrans, struct sdaemon *qdaemon, const char *zdata, size_t cdata ATTRIBUTE_UNUSED)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
char bcmd;
if (qtrans->s.bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) != 0)
bcmd = 'E';
else
bcmd = 'S';
if (zdata[0] != bcmd
|| (zdata[1] != 'Y' && zdata[1] != 'N'))
{
ulog (LOG_ERROR, "%s: Bad response to %c request: \"%s\"",
qtrans->s.zfrom, bcmd, zdata);
usfree_send (qtrans);
return FALSE;
}
if (zdata[1] == 'N')
{
const char *zerr;
boolean fnever;
fnever = TRUE;
if (zdata[2] == '2')
zerr = "permission denied by remote";
else if (zdata[2] == '4')
{
zerr = "remote cannot create work files";
fnever = FALSE;
}
else if (zdata[2] == '6')
{
zerr = "too large for remote now";
fnever = FALSE;
}
else if (zdata[2] == '7')
{
/* The file is too large to ever send. */
zerr = "too large for remote";
}
else if (zdata[2] == '8')
{
/* The file was already received by the remote system. This
is not an error, it just means that the ack from the
remote was lost in the previous conversation, and there
is no need to resend the file. */
zerr = NULL;
}
else if (zdata[2] == '9')
{
/* Remote has run out of channels. */
zerr = "too many channels for remote";
fnever = FALSE;
/* Drop one channel; using exactly one channel causes
slightly different behahaviour in a few places, so don't
decrement to one. */
if (qdaemon->cchans > 2)
--qdaemon->cchans;
}
else
zerr = "unknown reason";
if (! fnever
|| (qtrans->s.bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) == 0
&& qinfo->zexec == NULL))
{
if (qtrans->s.bcmd == 'E')
ulog (LOG_ERROR, "%s (execution of \"%s\"): %s",
qtrans->s.zfrom, qtrans->s.zcmd, zerr);
else
ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);
}
else
{
if (! flocal_send_fail (&qtrans->s, qdaemon, zerr))
return FALSE;
}
/* If the protocol does not support multiple channels, we can
simply remove the transaction. Otherwise we must make sure
the remote side knows that we have finished sending the file
data. If we have already sent the entire file, there will be
no confusion. */
if (qdaemon->cchans == 1 || qinfo->fsent)
{
/* If we are breaking a 'E' command into two 'S' commands,
and that was for the first 'S' command, we still have to
send the second one. */
if (fnever
&& qtrans->s.bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) == 0
&& qinfo->zexec == NULL)
return fsend_exec_file_init (qtrans, qdaemon);
usfree_send (qtrans);
return TRUE;
}
else
{
/* Seek to the end of the file so that the next read will
send end of file. We have to be careful here, because we
may not have opened the file yet, or we may have actually
already sent end of file--we could be being called
because of data received while the end of file block was
sent. */
if (qtrans->fsendfile && ! ffileseekend (qtrans->e))
{
ulog (LOG_ERROR, "seek to end: %s", strerror (errno));
usfree_send (qtrans);
return FALSE;
}
qtrans->psendfn = flocal_send_cancelled;
qtrans->precfn = NULL;
qinfo->fnever = fnever;
return fqueue_send (qdaemon, qtrans);
}
}
/* A number following the SY or EY is the file position to start
sending from. If we are already sending the file, we must set
the position accordingly. */
if (zdata[2] != '\0')
{
long cskip;
cskip = strtol ((char *) (zdata + 2), (char **) NULL, 0);
if (cskip > 0 && qtrans->ipos < cskip)
{
if (qtrans->fsendfile && ! qinfo->fsent)
{
if (! ffileseek (qtrans->e, cskip))
{
ulog (LOG_ERROR, "seek: %s", strerror (errno));
usfree_send (qtrans);
return FALSE;
}
}
qtrans->ipos = cskip;
}
}
/* Now queue up to send the file or to wait for the confirmation.
We already set psendfn at the end of flocal_send_request. If the
protocol supports multiple channels, we have already called
fqueue_send; calling it again would move the request in the
queue, which would make the log file a bit confusing. */
qtrans->fcmd = TRUE;
qtrans->precfn = fsend_await_confirm;
if (qinfo->fsent)
return fqueue_receive (qdaemon, qtrans);
else if (qdaemon->cchans <= 1)
return fqueue_send (qdaemon, qtrans);
else
return TRUE;
}
/* Open the file, if any, and prepare to send it. */
static boolean
flocal_send_open_file (struct stransfer *qtrans, struct sdaemon *qdaemon)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
const char *zuser;
/* If this is not a fake execution file, open it. */
if (qinfo->zexec == NULL)
{
/* If there is an ! in the user name, this is a remote request
queued up by fremote_xcmd_init. */
zuser = qtrans->s.zuser;
if (strchr (zuser, '!') != NULL)
zuser = NULL;
qtrans->e = esysdep_open_send (qdaemon->qsys, qinfo->zfile,
! qinfo->fspool, zuser);
if (! ffileisopen (qtrans->e))
{
(void) fmail_transfer (FALSE, qtrans->s.zuser,
(const char *) NULL,
"cannot open file",
qtrans->s.zfrom, (const char *) NULL,
qtrans->s.zto,
qdaemon->qsys->uuconf_zname,
(qinfo->flocal
? zsysdep_save_temp_file (qtrans->s.pseq)
: (const char *) NULL));
(void) fsysdep_did_work (qtrans->s.pseq);
usfree_send (qtrans);
/* Unfortunately, there is no way to cancel a file send
after we've already put it in progress. So we have to
return FALSE to drop the connection. */
return FALSE;
}
}
/* If flocal_send_await_reply has received a reply with a file
position, it will have set qtrans->ipos to the position at which
to start. */
if (qtrans->ipos > 0)
{
if (qinfo->zexec != NULL)
{
if (qtrans->ipos > qtrans->cbytes)
qtrans->ipos = qtrans->cbytes;
}
else
{
if (! ffileseek (qtrans->e, qtrans->ipos))
{
ulog (LOG_ERROR, "seek: %s", strerror (errno));
usfree_send (qtrans);
return FALSE;
}
}
}
/* We don't bother to log sending the execution file. */
if (qinfo->zexec == NULL)
{
const char *zsend;
char *zalc;
if (qtrans->s.bcmd != 'E')
{
zsend = qtrans->s.zfrom;
zalc = NULL;
}
else
{
zalc = zbufalc (strlen (qtrans->s.zcmd) + sizeof " ()"
+ strlen (qtrans->s.zfrom));
sprintf (zalc, "%s (%s)", qtrans->s.zcmd, qtrans->s.zfrom);
zsend = zalc;
}
qtrans->zlog = zbufalc (sizeof "Sending ( bytes resume at )"
+ strlen (zsend) + 50);
sprintf (qtrans->zlog, "Sending %s (%ld bytes", zsend, qinfo->cbytes);
if (qtrans->ipos > 0)
sprintf (qtrans->zlog + strlen (qtrans->zlog), " resume at %ld",
qtrans->ipos);
strcat (qtrans->zlog, ")");
ubuffree (zalc);
}
if (qdaemon->qproto->pffile != NULL)
{
boolean fhandled;
if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, TRUE,
qinfo->cbytes - qtrans->ipos,
&fhandled))
{
usfree_send (qtrans);
return FALSE;
}
if (fhandled)
return TRUE;
}
if (qinfo->zexec != NULL)
qtrans->psendfn = fsend_exec_file;
else
{
qtrans->fsendfile = TRUE;
qtrans->psendfn = fsend_file_end;
}
return fqueue_send (qdaemon, qtrans);
}
/* Cancel a file send. This is only called for a protocol which
supports multiple channels. It is needed so that both systems
agree as to when a channel is no longer needed. */
static boolean
flocal_send_cancelled (struct stransfer *qtrans, struct sdaemon *qdaemon)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
/* If we are breaking a 'E' command into two 'S' commands, and that
was for the first 'S' command, and the first 'S' command will
never be sent, we still have to send the second one. */
if (qinfo->fnever
&& qtrans->s.bcmd == 'E'
&& (qdaemon->ifeatures & FEATURE_EXEC) == 0
&& qinfo->zexec == NULL)
return fsend_exec_file_init (qtrans, qdaemon);
usfree_send (qtrans);
return TRUE;
}
/* A remote request to receive a file (meaning that we have to send a
file). The sequence of functions calls is as follows:
fremote_rec_file_init (open file) --> fqueue_remote
fremote_rec_reply (send RY, call pffile) --> fqueue_send
send file
fsend_file_end (calls pffile) --> fqueue_receive
fsend_await_confirm (waits for CY)
*/
boolean
fremote_rec_file_init (struct sdaemon *qdaemon, struct scmd *qcmd, int iremote)
{
const struct uuconf_system *qsys;
char *zfile;
boolean fbadname;
long cbytes;
unsigned int imode;
openfile_t e;
struct ssendinfo *qinfo;
struct stransfer *qtrans;
qsys = qdaemon->qsys;
if (! qsys->uuconf_fsend_request)
{
ulog (LOG_ERROR, "%s: not permitted to send files to remote",
qcmd->zfrom);
return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
}
if (fspool_file (qcmd->zfrom))
{
ulog (LOG_ERROR, "%s: not permitted to send", qcmd->zfrom);
return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
}
zfile = zsysdep_local_file (qcmd->zfrom, qsys->uuconf_zpubdir, &fbadname);
if (zfile == NULL && fbadname)
{
ulog (LOG_ERROR, "%s: bad local file name", qcmd->zfrom);
return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
}
if (zfile != NULL)
{
char *zbased;
zbased = zsysdep_add_base (zfile, qcmd->zto);
ubuffree (zfile);
zfile = zbased;
}
if (zfile == NULL)
return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
if (! fin_directory_list (zfile, qsys->uuconf_pzremote_send,
qsys->uuconf_zpubdir, TRUE, TRUE,
(const char *) NULL))
{
ulog (LOG_ERROR, "%s: not permitted to send", zfile);
ubuffree (zfile);
return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
}
/* If the file is larger than the amount of space the other side
reported, we can't send it. Should we adjust this check based on
the restart position? */
cbytes = csysdep_size (zfile);
if (cbytes != -1
&& ((qcmd->cbytes != -1 && qcmd->cbytes < cbytes)
|| (qdaemon->cremote_size != -1
&& qdaemon->cremote_size < cbytes)
|| (qdaemon->cmax_receive != -1
&& qdaemon->cmax_receive < cbytes)))
{
ulog (LOG_ERROR, "%s: too large to send", zfile);
ubuffree (zfile);
return fremote_rec_fail (qdaemon, FAILURE_SIZE, iremote);
}
imode = ixsysdep_file_mode (zfile);
e = esysdep_open_send (qsys, zfile, TRUE, (const char *) NULL);
if (! ffileisopen (e))
{
ubuffree (zfile);
return fremote_rec_fail (qdaemon, FAILURE_OPEN, iremote);
}
/* If the remote requested that the file send start from a
particular position, arrange to do so. */
if (qcmd->ipos > 0)
{
if (! ffileseek (e, qcmd->ipos))
{
ulog (LOG_ERROR, "seek: %s", strerror (errno));
ubuffree (zfile);
return FALSE;
}
}
qinfo = (struct ssendinfo *) xmalloc (sizeof (struct ssendinfo));
qinfo->zmail = NULL;
qinfo->zfile = zfile;
qinfo->cbytes = cbytes;
qinfo->flocal = FALSE;
qinfo->fspool = FALSE;
qinfo->fsent = FALSE;
qinfo->zexec = NULL;
qinfo->zconfirm = NULL;
qtrans = qtransalc (qcmd);
qtrans->psendfn = fremote_rec_reply;
qtrans->iremote = iremote;
qtrans->pinfo = (pointer) qinfo;
qtrans->e = e;
qtrans->ipos = qcmd->ipos;
qtrans->s.imode = imode;
return fqueue_remote (qdaemon, qtrans);
}
/* Reply to a receive request from the remote system, and prepare to
start sending the file. */
static boolean
fremote_rec_reply (struct stransfer *qtrans, struct sdaemon *qdaemon)
{
struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
char absend[50];
qtrans->fsendfile = TRUE;
qtrans->psendfn = fsend_file_end;
qtrans->fcmd = TRUE;
qtrans->precfn = fsend_await_confirm;
if (! fqueue_send (qdaemon, qtrans))
return FALSE;
qtrans->zlog = zbufalc (sizeof "Sending ( bytes) "
+ strlen (qtrans->s.zfrom) + 25);
sprintf (qtrans->zlog, "Sending %s (%ld bytes)", qtrans->s.zfrom,
qinfo->cbytes);
/* We send the file size because SVR4 UUCP does. We don't look for
it. We send a trailing M if we want to request a hangup. We
send it both after the mode and at the end of the entire string;
I don't know where programs look for it. */
if (qdaemon->frequest_hangup)
DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
"fremote_rec_reply: Requesting remote to transfer control");
sprintf (absend, "RY 0%o%s 0x%lx%s", qtrans->s.imode,
qdaemon->frequest_hangup ? "M" : "",
(unsigned long) qinfo->cbytes,
qdaemon->frequest_hangup ? "M" : "");
if (! (*qdaemon->qproto->pfsendcmd) (qdaemon, absend, qtrans->ilocal,
qtrans->iremote))
{
(void) ffileclose (qtrans->e);
qtrans->e = EFILECLOSED;
/* Should probably free qtrans here, but see the comment at the
end of flocal_rec_send_request. */
return FALSE;
}
if (qdaemon->qproto->pffile != NULL)
{
boolean fhandled;
if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, TRUE,
qinfo->cbytes, &fhandled))
{
usfree_send (qtrans);