summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/uucico/send.c
blob: 72d008341ce2294126d7fdf496b04fe0a179d3de (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
/* send.c
   Routines to send a file.

   Copyright (C) 1991, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, Building 200, 1 Kendall Square, Cambridge, MA 02139.
   */

#include "uucp.h"

#if USE_RCS_ID
const char send_rcsid[] = "$Id: send.c,v 1.3 1994/11/06 10:17:13 davidg Exp $";
#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.  Also used in
     flocal_send_cancelled to mean that the file send will never
     succeed.  */
  boolean fsent;
  /* Execution file for sending an unsupported E request.  */
  char *zexec;
};

/* Local functions.  */

static void usfree_send P((struct stransfer *qtrans));
static boolean flocal_send_fail P((struct stransfer *qtrans,
				   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));
static boolean fsend_exec_file P((struct stransfer *qtrans,
				  struct sdaemon *qdaemon));

/* Free up a send stransfer structure.  */

static void
usfree_send (qtrans)
     struct stransfer *qtrans;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;

  if (qinfo != NULL)
    {
      ubuffree (qinfo->zmail);
      ubuffree (qinfo->zfile);
      ubuffree (qinfo->zexec);
      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 (qdaemon, qcmd)
     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 ((struct stransfer *) NULL, 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 ((struct stransfer *) NULL, 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 ((struct stransfer *) NULL, 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 ((struct stransfer *) NULL, 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 ((struct stransfer *) NULL, 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;

  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 (qtrans, qcmd, qdaemon, zwhy)
     struct stransfer *qtrans;
     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 (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  char *zsend;
  const char *znotify;
  char absize[20];
  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, &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;

  /* Construct the notify string to send.  If we are going to send a
     size or an execution command, it must be non-empty.  */
  znotify = qtrans->s.znotify;
  if (znotify == NULL)
    znotify = "";
  if ((qdaemon->ifeatures & FEATURE_SIZES) != 0
      || (qtrans->s.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
      && (qtrans->s.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 (qtrans->s.zfrom) + strlen (qtrans->s.zto)
		   + strlen (qtrans->s.zuser) + strlen (qtrans->s.zoptions)
		   + strlen (qtrans->s.ztemp) + strlen (znotify)
		   + strlen (absize)
		   + (qtrans->s.zcmd != NULL ? strlen (qtrans->s.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 (qtrans->s.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", qtrans->s.zfrom,
	       qtrans->s.zto, qtrans->s.zuser, qtrans->s.zoptions,
	       qtrans->s.ztemp, qtrans->s.imode, znotify, absize,
	       qtrans->s.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 (qtrans->s.bcmd != 'E')
	zoptions = qtrans->s.zoptions;
      else if (strchr (qtrans->s.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", qtrans->s.zfrom,
	       qtrans->s.zto, qtrans->s.zuser, zoptions,
	       qtrans->s.ztemp, qtrans->s.imode, znotify, zdummy,
	       absize);
    }

  fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, qtrans->ilocal,
					qtrans->iremote);
  ubuffree (zsend);

  if (! fret)
    usfree_send (qtrans);

  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 (qtrans, qdaemon, zdata, cdata)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
     const char *zdata;
     size_t cdata;
{
  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 ((struct stransfer *) NULL, &qtrans->s,
				  qdaemon, zerr))
	    {
	      usfree_send (qtrans);
	      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 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 (! 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;

	  /* Reuse fsent to pass fnever to flocal_send_cancelled.  */
	  qinfo->fsent = 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->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 (qtrans, qdaemon)
     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 (qtrans, qdaemon)
     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 (passed as qinfo->fsent), we still have to send the
     second one.  */
  if (qinfo->fsent
      && 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 (qdaemon, qcmd, iremote)
     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;

  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 (qtrans, qdaemon)
     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->precfn = fsend_await_confirm;

  if (! fqueue_send (qdaemon, qtrans))
    return FALSE;

  /* 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);
      usfree_send (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);

  if (qdaemon->qproto->pffile != NULL)
    {
      boolean fhandled;

      if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, TRUE,
					qinfo->cbytes, &fhandled))
	{
	  usfree_send (qtrans);
	  return FALSE;
	}
    }

  return TRUE;
}

/* If we can't send a file as requested by the remote system, queue up
   a failure reply which will be sent when possible.  */

static boolean
fremote_rec_fail (qdaemon, twhy, iremote)
     struct sdaemon *qdaemon;
     enum tfailure twhy;
     int iremote;
{
  enum tfailure *ptinfo;
  struct stransfer *qtrans;

  ptinfo = (enum tfailure *) xmalloc (sizeof (enum tfailure));
  *ptinfo = twhy;

  qtrans = qtransalc ((struct scmd *) NULL);
  qtrans->psendfn = fremote_rec_fail_send;
  qtrans->iremote = iremote;
  qtrans->pinfo = (pointer) ptinfo;

  return fqueue_remote (qdaemon, qtrans);
}

/* Send a failure string for a receive command to the remote system;
   this is called when we are ready to reply to the command.  */

static boolean
fremote_rec_fail_send (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  enum tfailure *ptinfo = (enum tfailure *) qtrans->pinfo;
  const char *z;
  boolean fret;
  int ilocal = qtrans->ilocal;
  int iremote = qtrans->iremote;

  switch (*ptinfo)
    {
    case FAILURE_PERM:
    case FAILURE_OPEN:
      z = "RN2";
      break;
    case FAILURE_SIZE:
      z = "RN6";
      break;
    default:
      z = "RN";
      break;
    }

  xfree (qtrans->pinfo);
  utransfree (qtrans);
  fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, z, ilocal, iremote);
  return fret;
}

/* This is called when the main loop has finished sending a file.  It
   prepares to wait for a response from the remote system.  Note that
   if this is a local request and the protocol supports multiple
   channels, we may not even have received a confirmation of the send
   request.  */

static boolean
fsend_file_end (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;

  if (qdaemon->qproto->pffile != NULL)
    {
      boolean fhandled;

      if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, FALSE, TRUE,
					(long) -1, &fhandled))
	{
	  usfree_send (qtrans);
	  return FALSE;
	}

      if (fhandled)
	return TRUE;
    }

  qinfo->fsent = TRUE;

  /* qtrans->precfn should have been set by a previous function.  */
  qtrans->fcmd = TRUE;
  return fqueue_receive (qdaemon, qtrans);
}

/* Handle the confirmation string received after sending a file.  */

/*ARGSUSED*/
static boolean
fsend_await_confirm (qtrans, qdaemon, zdata, cdata)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
     const char *zdata;
     size_t cdata;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  boolean fnever;
  const char *zerr;

  if (qinfo->zexec == NULL)
    (void) ffileclose (qtrans->e);

  fnever = FALSE;
  if (zdata[0] != 'C'
      || (zdata[1] != 'Y' && zdata[1] != 'N'))
    {
      zerr = "bad confirmation from remote";
      ulog (LOG_ERROR, "%s: %s \"%s\"", qtrans->s.zfrom, zerr, zdata);
    }
  else if (zdata[1] == 'N')
    {
      fnever = TRUE;
      if (zdata[2] == '5')
	{
	  zerr = "file could not be stored in final location";
	  ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);
	}
      else
	{
	  zerr = "file send failed for unknown reason";
	  ulog (LOG_ERROR, "%s: %s \"%s\"", qtrans->s.zfrom, zerr, zdata);
	}
    }
  else
    {
      zerr = NULL;

      /* If we receive CYM, it means that the other side wants us to
	 hang up so that they can send us something.  The
	 fhangup_requested field is checked in the main loop.  */
      if (zdata[2] == 'M' && qdaemon->fmaster)
	{
	  DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
			  "fsend_await_confirm: Remote has requested transfer of control");
	  qdaemon->fhangup_requested = TRUE;
	}
    }

  ustats (zerr == NULL, qtrans->s.zuser, qdaemon->qsys->uuconf_zname,
	  TRUE, qtrans->cbytes, qtrans->isecs, qtrans->imicros,
	  qdaemon->fmaster);
  qdaemon->csent += qtrans->cbytes;

  if (zerr == NULL)
    {
      /* If this is an execution request, and the remote system
	 doesn't support execution requests, we have to set up the
	 fake execution file and loop around again.  */
      if (qtrans->s.bcmd == 'E'
	  && (qdaemon->ifeatures & FEATURE_EXEC) == 0
	  && qinfo->zexec == NULL)
	return fsend_exec_file_init (qtrans, qdaemon);

      /* Send mail about the transfer if requested.  */
      if (qinfo->zmail != NULL && *qinfo->zmail != '\0')
	(void) fmail_transfer (TRUE, qtrans->s.zuser, qinfo->zmail,
			       (const char *) NULL,
			       qtrans->s.zfrom, (const char *) NULL,
			       qtrans->s.zto, qdaemon->qsys->uuconf_zname,
			       (const char *) NULL);

      if (qtrans->s.pseq != NULL)
	(void) fsysdep_did_work (qtrans->s.pseq);
    }
  else
    {
      /* If the file send failed, we only try to save the file and
	 send mail if it was requested locally and it will never
	 succeed.  We send mail to qinfo->zmail if set, otherwise to
	 qtrans->s.zuser.  I hope this is reasonable.  */
      if (fnever && qinfo->flocal)
	{
	  (void) fmail_transfer (FALSE, qtrans->s.zuser, qinfo->zmail,
				 zerr, qtrans->s.zfrom, (const char *) NULL,
				 qtrans->s.zto, qdaemon->qsys->uuconf_zname,
				 zsysdep_save_temp_file (qtrans->s.pseq));
	  (void) fsysdep_did_work (qtrans->s.pseq);
	}
    }

  usfree_send (qtrans);

  return TRUE;
}

/* Prepare to send an execution file to a system which does not
   support execution requests.  We build the execution file in memory,
   and then call flocal_send_request as though we were sending a real
   file.  Instead of sending a file, the code in flocal_send_open_file
   will arrange to call fsend_exec_file which will send data out of
   the buffer we have created.  */

static boolean
fsend_exec_file_init (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  char *zxqtfile;
  char abtname[CFILE_NAME_LEN];
  char abxname[CFILE_NAME_LEN];
  char *z;
  size_t calc, clen;

  z = NULL;
  calc = 0;
  clen = 0;

  usadd_exec_line (&z, &calc, &clen, 'U', qtrans->s.zuser,
		   qdaemon->zlocalname);
  usadd_exec_line (&z, &calc, &clen, 'F', qtrans->s.zto, "");
  usadd_exec_line (&z, &calc, &clen, 'I', qtrans->s.zto, "");
  if (strchr (qtrans->s.zoptions, 'N') != NULL)
    usadd_exec_line (&z, &calc, &clen, 'N', "", "");
  if (strchr (qtrans->s.zoptions, 'Z') != NULL)
    usadd_exec_line (&z, &calc, &clen, 'Z', "", "");
  if (strchr (qtrans->s.zoptions, 'R') != NULL)
    usadd_exec_line (&z, &calc, &clen, 'R', qtrans->s.znotify, "");
  if (strchr (qtrans->s.zoptions, 'e') != NULL)
    usadd_exec_line (&z, &calc, &clen, 'e', "", "");
  usadd_exec_line (&z, &calc, &clen, 'C', qtrans->s.zcmd, "");

  qinfo->zexec = z;
  qinfo->cbytes = clen;

  zxqtfile = zsysdep_data_file_name (qdaemon->qsys, qdaemon->zlocalname,
				     BDEFAULT_UUX_GRADE, TRUE, abtname,
				     (char *) NULL, abxname);
  if (zxqtfile == NULL)
    {
      usfree_send (qtrans);
      return FALSE;
    }
  ubuffree (zxqtfile);

  ubuffree ((char *) qtrans->s.zfrom);
  qtrans->s.zfrom = zbufcpy (abtname);
  ubuffree ((char *) qtrans->s.zto);
  qtrans->s.zto = zbufcpy (abxname);
  ubuffree ((char *) qtrans->s.zoptions);
  qtrans->s.zoptions = zbufcpy ("C");
  ubuffree ((char *) qtrans->s.ztemp);
  qtrans->s.ztemp = zbufcpy (abtname);

  qtrans->psendfn = flocal_send_request;
  qtrans->precfn = NULL;
  qtrans->ipos = 0;
  qtrans->cbytes = 0;
  qtrans->isecs = 0;
  qtrans->imicros = 0;
  qinfo->fsent = FALSE;

  return fqueue_send (qdaemon, qtrans);
}

/* Add a line to the fake execution file.  */

static void
usadd_exec_line (pz, pcalc, pclen, bcmd, z1, z2)
     char **pz;
     size_t *pcalc;
     size_t *pclen;
     int bcmd;
     const char *z1;
     const char *z2;
{
  size_t c1, c2;
  char *znew;

  c1 = strlen (z1);
  c2 = strlen (z2);

  if (*pclen + c1 + c2 + 4 >= *pcalc)
    {
      *pcalc += c1 + c2 + 100;
      znew = zbufalc (*pcalc);
      if (*pclen > 0)
	{
	  memcpy (znew, *pz, *pclen);
	  ubuffree (*pz);
	}
      *pz = znew;
    }

  znew = *pz + *pclen;
  *znew++ = bcmd;
  if (*z1 != '\0')
    {
      *znew++ = ' ';
      memcpy (znew, z1, c1);
      znew += c1;
      if (*z2 != '\0')
	{
	  *znew++ = ' ';
	  memcpy (znew, z2, c2);
	  znew += c2;
	}
    }

  /* In some bizarre non-Unix case we might have to worry about the
     newline here.  We don't know how a newline is normally written
     out to a file, but whatever is written to a file is what we will
     normally transfer.  If that is not simply \n then this fake
     execution file will not look like other execution files.  */
  *znew++ = '\n';

  *pclen = znew - *pz;
}

/* This routine is called to send the contents of the fake execution
   file.  Normally file data is sent by the floop routine in trans.c,
   but since we don't have an actual file we must do it here.  This
   routine sends the complete buffer, followed by a zero length
   packet, and then calls fsend_file_end.  */

static boolean
fsend_exec_file (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  char *zdata;
  size_t cdata;
  size_t csend;

  zdata = (*qdaemon->qproto->pzgetspace) (qdaemon, &cdata);
  if (zdata == NULL)
    {
      usfree_send (qtrans);
      return FALSE;
    }

  csend = qinfo->cbytes - qtrans->ipos;
  if (csend > cdata)
    csend = cdata;

  memcpy (zdata, qinfo->zexec + qtrans->ipos, csend);

  if (! (*qdaemon->qproto->pfsenddata) (qdaemon, zdata, csend,
					qtrans->ilocal, qtrans->iremote,
					qtrans->ipos))
    {
      usfree_send (qtrans);
      return FALSE;
    }

  qtrans->cbytes += csend;
  qtrans->ipos += csend;

  if (csend == 0)
    return fsend_file_end (qtrans, qdaemon);

  /* Leave the job on the send queue.  */

  return TRUE;
}
OpenPOWER on IntegriCloud