aboutsummaryrefslogtreecommitdiff
path: root/gnu/libexec/uucp/uucico/rec.c
blob: 020ff21eefd1b78a7f707825a9666602165e2dcd (plain) (blame)
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
/* rec.c
   Routines to receive a file.

   Copyright (C) 1991, 1992, 1993, 1994, 1995 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 ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#if USE_RCS_ID
const char rec_rcsid[] = "$FreeBSD$";
#endif

#include <errno.h>

#include "uudefs.h"
#include "uuconf.h"
#include "system.h"
#include "prot.h"
#include "trans.h"

/* If the other side does not tell us the size of a file it wants to
   send us, we assume it is this long.  This is only used for free
   space checking.  */
#define CASSUMED_FILE_SIZE (10240)

/* We keep this information in the pinfo field of the stransfer
   structure.  */
struct srecinfo
{
  /* Local user to send mail to (may be NULL).  */
  char *zmail;
  /* Full file name.  */
  char *zfile;
  /* Temporary file name.  */
  char *ztemp;
  /* TRUE if this is a spool directory file.  */
  boolean fspool;
  /* TRUE if this was a local request.  */
  boolean flocal;
  /* TRUE if the file has been completely received.  */
  boolean freceived;
  /* TRUE if remote request has been replied to.  */
  boolean freplied;
  /* TRUE if we moved the file to the final destination.  */
  boolean fmoved;
};

/* This structure is kept in the pinfo field if we are refusing a
   remote request.  */
struct srecfailinfo
{
  /* Reason for refusal.  */
  enum tfailure twhy;
  /* TRUE if we have sent the reason for refusal.  */
  boolean fsent;
  /* TRUE if we have seen the end of the file.  */
  boolean freceived;
};

/* Local functions.  */

static void urrec_free P((struct stransfer *qtrans));
static boolean flocal_rec_fail P((struct stransfer *qtrans,
				  struct scmd *qcmd,
				  const struct uuconf_system *qsys,
				  const char *zwhy));
static boolean flocal_rec_send_request P((struct stransfer *qtrans,
					  struct sdaemon *qdaemon));
static boolean flocal_rec_await_reply P((struct stransfer *qtrans,
					 struct sdaemon *qdaemon,
					 const char *zdata,
					 size_t cdata));
static boolean fremote_send_reply P((struct stransfer *qtrans,
				     struct sdaemon *qdaemon));
static boolean fremote_send_fail P((struct sdaemon *qdaemon,
				    struct scmd *qcmd,
				    enum tfailure twhy,
				    int iremote));
static boolean fremote_send_fail_send P((struct stransfer *qtrans,
					 struct sdaemon *qdaemon));
static boolean fremote_discard P((struct stransfer *qtrans,
				  struct sdaemon *qdaemon,
				  const char *zdata, size_t cdata));
static boolean frec_file_end P((struct stransfer *qtrans,
				struct sdaemon *qdaemon,
				const char *zdata, size_t cdata));
static boolean frec_file_send_confirm P((struct stransfer *qtrans,
					 struct sdaemon *qdaemon));

/* Free up a receive stransfer structure.  */

static void
urrec_free (qtrans)
     struct stransfer *qtrans;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;

  if (qinfo != NULL)
    {
      ubuffree (qinfo->zmail);
      ubuffree (qinfo->zfile);
      ubuffree (qinfo->ztemp);
      xfree (qtrans->pinfo);
    }

  utransfree (qtrans);
}       

/* Set up a request for a file from the remote system.  This may be
   called before the remote system has been called.

   This is the order of function calls:

   flocal_rec_file_init --> fqueue_local
   flocal_rec_send_request (send R ...) --> fqueue_receive
   flocal_rec_await_reply (open file, call pffile) --> fqueue_receive
   receive file
   frec_file_end (close and move file, call pffile) --> fqueue_send
   frec_file_send_confirm (send CY)
   */

boolean
flocal_rec_file_init (qdaemon, qcmd)
     struct sdaemon *qdaemon;
     struct scmd *qcmd;
{
  const struct uuconf_system *qsys;
  boolean fspool;
  char *zfile;
  struct srecinfo *qinfo;
  struct stransfer *qtrans;

  qsys = qdaemon->qsys;

  /* Make sure we are permitted to transfer files.  */
  if (qdaemon->fcaller
      ? ! qsys->uuconf_fcall_transfer
      : ! qsys->uuconf_fcalled_transfer)
    {
      /* This case will have been checked by uucp or uux, but it could
	 have changed.  */
      if (! qsys->uuconf_fcall_transfer
	  && ! qsys->uuconf_fcalled_transfer)
	return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
				"not permitted to request files");
      return TRUE;
    }

  fspool = fspool_file (qcmd->zto);

  if (fspool)
    {
      pointer puuconf;
      int iuuconf;
      const char *zlocalname;
      struct uuconf_system slocalsys;

      /* Normal users are not allowed to request files to be received
	 into the spool directory.  To support uux forwarding, we use
	 the special option '9'.  This permits a file to be received
	 into the spool directory for the local system only without
	 the usual checking.  This is only done for local requests, of
	 course.  */
      if (qcmd->zto[0] != 'D'
	  || strchr (qcmd->zoptions, '9') == NULL)
	return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
				"not permitted to receive");

      puuconf = qdaemon->puuconf;
      iuuconf = uuconf_localname (puuconf, &zlocalname);
      if (iuuconf == UUCONF_NOT_FOUND)
	{
	  zlocalname = zsysdep_localname ();
	  if (zlocalname == NULL)
	    return FALSE;
	}
      else if (iuuconf != UUCONF_SUCCESS)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  return FALSE;
	}

      iuuconf = uuconf_system_info (puuconf, zlocalname, &slocalsys);
      if (iuuconf == UUCONF_NOT_FOUND)
	{
	  iuuconf = uuconf_system_local (puuconf, &slocalsys);
	  if (iuuconf != UUCONF_SUCCESS)
	    {
	      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	      return FALSE;
	    }
	  slocalsys.uuconf_zname = (char *) zlocalname;
	}
      else if (iuuconf != UUCONF_SUCCESS)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  return FALSE;
	}

      zfile = zsysdep_spool_file_name (&slocalsys, qcmd->zto, qcmd->pseq);

      (void) uuconf_system_free (puuconf, &slocalsys);

      if (zfile == NULL)
	return FALSE;
    }
  else
    {
      zfile = zsysdep_add_base (qcmd->zto, qcmd->zfrom);
      if (zfile == NULL)
	return FALSE;

      /* Check permissions.  */
      if (! fin_directory_list (zfile, qsys->uuconf_pzlocal_receive,
				qsys->uuconf_zpubdir, TRUE,
				FALSE, qcmd->zuser))
	{
	  ubuffree (zfile);
	  return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
				  "not permitted to receive");
	}

      /* The 'f' option means that directories should not
	 be created if they do not already exist.  */
      if (strchr (qcmd->zoptions, 'f') == NULL)
	{
	  if (! fsysdep_make_dirs (zfile, TRUE))
	    {
	      ubuffree (zfile);
	      return flocal_rec_fail ((struct stransfer *) NULL, qcmd,
				      qsys, "cannot create directories");
	    }
	}
    }

  qinfo = (struct srecinfo *) xmalloc (sizeof (struct srecinfo));
  if (strchr (qcmd->zoptions, 'm') == NULL)
    qinfo->zmail = NULL;
  else
    qinfo->zmail = zbufcpy (qcmd->zuser);
  qinfo->zfile = zfile;
  qinfo->ztemp = NULL;
  qinfo->fspool = fspool;
  qinfo->flocal = TRUE;
  qinfo->freceived = FALSE;
  qinfo->freplied = TRUE;

  qtrans = qtransalc (qcmd);
  qtrans->psendfn = flocal_rec_send_request;
  qtrans->pinfo = (pointer) qinfo;

  return fqueue_local (qdaemon, qtrans);
}

/* Report an error for a local receive request.  */

static boolean
flocal_rec_fail (qtrans, qcmd, qsys, zwhy)
     struct stransfer *qtrans;
     struct scmd *qcmd;
     const struct uuconf_system *qsys;
     const char *zwhy;
{
  if (zwhy != NULL)
    {
      ulog (LOG_ERROR, "%s: %s", qcmd->zfrom, zwhy);
      (void) fmail_transfer (FALSE, qcmd->zuser, (const char *) NULL, zwhy,
			     qcmd->zfrom, qsys->uuconf_zname,
			     qcmd->zto, (const char *) NULL,
			     (const char *) NULL);
      (void) fsysdep_did_work (qcmd->pseq);
    }
  if (qtrans != NULL)
    urrec_free (qtrans);
  return TRUE;
}

/* This is called when we are ready to send the actual request to the
   other system.  */

static boolean
flocal_rec_send_request (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  long cbytes, cbytes2;
  size_t clen;
  char *zsend;
  boolean fret;

  qinfo->ztemp = zsysdep_receive_temp (qdaemon->qsys, qinfo->zfile,
				       (const char *) NULL,
				       (qdaemon->qproto->frestart
					&& (qdaemon->ifeatures
					    & FEATURE_RESTART) != 0));
  if (qinfo->ztemp == NULL)
    {
      urrec_free (qtrans);
      return FALSE;
    }

  qtrans->fcmd = TRUE;
  qtrans->precfn = flocal_rec_await_reply;

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

  /* Check the amount of free space available for both the temporary
     file and the real file.  */
  cbytes = csysdep_bytes_free (qinfo->ztemp);
  cbytes2 = csysdep_bytes_free (qinfo->zfile);
  if (cbytes < cbytes2)
    cbytes = cbytes2;
  if (cbytes != -1)
    {
      cbytes -= qdaemon->qsys->uuconf_cfree_space;
      if (cbytes < 0)
	cbytes = 0;
    }

  if (qdaemon->clocal_size != -1
      && (cbytes == -1 || qdaemon->clocal_size < cbytes))
    cbytes = qdaemon->clocal_size;

  /* We send the string
     R from to user options

     We put a dash in front of options.  If we are talking to a
     counterpart, we also send the maximum size file we are prepared
     to accept, as returned by esysdep_open_receive.  */
  clen = (strlen (qtrans->s.zfrom) + strlen (qtrans->s.zto)
	  + strlen (qtrans->s.zuser) + strlen (qtrans->s.zoptions) + 30);
  zsend = zbufalc (clen);
  if ((qdaemon->ifeatures & FEATURE_SIZES) == 0)
    sprintf (zsend, "R %s %s %s -%s", qtrans->s.zfrom, qtrans->s.zto,
	     qtrans->s.zuser, qtrans->s.zoptions);
  else if ((qdaemon->ifeatures & FEATURE_V103) == 0)
    sprintf (zsend, "R %s %s %s -%s 0x%lx", qtrans->s.zfrom, qtrans->s.zto,
	     qtrans->s.zuser, qtrans->s.zoptions, (unsigned long) cbytes);
  else
    sprintf (zsend, "R %s %s %s -%s %ld", qtrans->s.zfrom, qtrans->s.zto,
	     qtrans->s.zuser, qtrans->s.zoptions, cbytes);

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

  /* There is a potential space leak here: if pfsendcmd fails, we
     might need to free qtrans.  However, it is possible that by the
     time pfsendcmd returns, a response will have been received which
     led to the freeing of qtrans anyhow.  One way to fix this would
     be some sort of counter in qtrans to track allocations, but since
     the space leak is small, and the conversation has failed anyhow,
     it doesn't seem worth it.  */

  return fret;
}

/* This is called when a reply is received for the request.  */

/*ARGSUSED*/
static boolean
flocal_rec_await_reply (qtrans, qdaemon, zdata, cdata)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
     const char *zdata;
     size_t cdata;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  const char *zlog;
  char *zend;

  if (zdata[0] != 'R'
      || (zdata[1] != 'Y' && zdata[1] != 'N'))
    {
      ulog (LOG_ERROR, "%s: bad response to receive request: \"%s\"",
	    qtrans->s.zfrom, zdata);
      urrec_free (qtrans);
      return FALSE;
    }

  if (zdata[1] == 'N')
    {
      boolean fnever;
      const char *zerr;

      fnever = TRUE;
      if (zdata[2] == '2')
	zerr = "no such file";
      else if (zdata[2] == '6')
	{
	  /* We sent over the maximum file size we were prepared to
	     receive, and the remote system is telling us that the
	     file is larger than that.  Try again later.  It would be
	     better if we could know whether there will ever be enough
	     room.  */
	  zerr = "too large to receive now";
	  fnever = FALSE;
	}
      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)
	return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys, zerr);

      ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);

      urrec_free (qtrans);

      return TRUE;
    }

  /* The mode should have been sent as "RY 0%o".  If it wasn't, we use
     0666.  */
  qtrans->s.imode = (unsigned int) strtol ((char *) (zdata + 2),
					   &zend, 8);
  if (qtrans->s.imode == 0)
    qtrans->s.imode = 0666;

  /* If there is an M after the mode, the remote has requested a
     hangup.  */
  if (*zend == 'M' && qdaemon->fmaster)
    {
      DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
		      "flocal_rec_await_reply: Remote has requested transfer of control");
      qdaemon->fhangup_requested = TRUE;
    }

  /* Open the file to receive into.  We just ignore any restart count,
     since we have no way to tell it to the other side.  SVR4 may have
     some way to do this, but I don't know what it is.  */
  qtrans->e = esysdep_open_receive (qdaemon->qsys, qinfo->zfile,
				    (const char *) NULL, qinfo->ztemp,
				    (long *) NULL);
  if (! ffileisopen (qtrans->e))
    return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys,
			    "cannot open file");

  if (qinfo->fspool)
    zlog = qtrans->s.zto;
  else
    zlog = qinfo->zfile;
  qtrans->zlog = zbufalc (sizeof "Receiving " + strlen (zlog));
  sprintf (qtrans->zlog, "Receiving %s", zlog);

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

      if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, FALSE,
					(long) -1, &fhandled))
	{
	  (void) ffileclose (qtrans->e);
	  return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys,
				  (const char *) NULL);
	}
      if (fhandled)
	return TRUE;
    }

  qtrans->frecfile = TRUE;
  qtrans->psendfn = frec_file_send_confirm;
  qtrans->precfn = frec_file_end;

  return fqueue_receive (qdaemon, qtrans);
}

/* Make sure there is still enough disk space available to receive a
   file.  */

boolean
frec_check_free (qtrans, cfree_space)
     struct stransfer *qtrans;
     long cfree_space;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  long cfree1, cfree2;

  cfree1 = csysdep_bytes_free (qinfo->ztemp);
  cfree2 = csysdep_bytes_free (qinfo->zfile);
  if (cfree1 < cfree2)
    cfree1 = cfree2;
  if (cfree1 != -1 && cfree1 < cfree_space)
    {
      ulog (LOG_ERROR, "%s: too big to receive now", qinfo->zfile);
      return FALSE;
    }

  return TRUE;
}

/* A remote request to send a file to the local system, meaning that
   we are going to receive a file.

   If we are using a protocol which does not support multiple
   channels, the remote system will not start sending us the file
   until it has received our confirmation.  In that case, the order of
   functions is as follows:

   fremote_send_file_init (open file) --> fqueue_remote
   fremote_send_reply (send SY, call pffile) --> fqueue_receive
   receive file
   frec_file_end (close and move file, call pffile) --> fqueue_send
   frec_file_send_confirm (send CY)

   If the protocol supports multiple channels, then the remote system
   will start sending the file immediately after the send request.
   That means that the data may come in before remote_send_reply is
   called, so frec_file_end may be called before fremote_send_reply.
   Note that this means the pffile entry points may be called in
   reverse order for such a protocol.

   If the send request is rejected, via fremote_send_fail, and the
   protocol supports multiple channels, we must accept and discard
   data until a zero byte buffer is received from the other side,
   indicating that it has received our rejection.

   This code also handles execution requests, which are very similar
   to send requests.  */

boolean
fremote_send_file_init (qdaemon, qcmd, iremote)
     struct sdaemon *qdaemon;
     struct scmd *qcmd;
     int iremote;
{
  const struct uuconf_system *qsys;
  boolean fspool;
  char *zfile;
  openfile_t e;
  char *ztemp;
  long cbytes, cbytes2;
  long crestart;
  struct srecinfo *qinfo;
  struct stransfer *qtrans;
  const char *zlog;

  qsys = qdaemon->qsys;

  if (! qsys->uuconf_frec_request)
    {
      ulog (LOG_ERROR, "%s: not permitted to receive files from remote",
	    qcmd->zfrom);
      return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
    }
		  
  fspool = fspool_file (qcmd->zto);

  /* We don't accept remote command files.  An execution request may
     only send a simple data file.  */
  if ((fspool && qcmd->zto[0] == 'C')
      || (qcmd->bcmd == 'E'
	  && (! fspool || qcmd->zto[0] != 'D')))
    {
      ulog (LOG_ERROR, "%s: not permitted to receive", qcmd->zfrom);
      return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
    }

  /* See if we have already received this file in a previous
     conversation.  */
  if (fsysdep_already_received (qsys, qcmd->zto, qcmd->ztemp))
    return fremote_send_fail (qdaemon, qcmd, FAILURE_RECEIVED, iremote);

  if (fspool)
    {
      zfile = zsysdep_spool_file_name (qsys, qcmd->zto, (pointer) NULL);
      if (zfile == NULL)
	return FALSE;
    }
  else
    {
      boolean fbadname;

      zfile = zsysdep_local_file (qcmd->zto, qsys->uuconf_zpubdir,
				  &fbadname);
      if (zfile == NULL && fbadname)
	{
	  ulog (LOG_ERROR, "%s: bad local file name", qcmd->zto);
	  return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
	}
      if (zfile != NULL)
	{
	  char *zadd;

	  zadd = zsysdep_add_base (zfile, qcmd->zfrom);
	  ubuffree (zfile);
	  zfile = zadd;
	}
      if (zfile == NULL)
	return FALSE;

      /* Check permissions.  */
      if (! fin_directory_list (zfile, qsys->uuconf_pzremote_receive,
				qsys->uuconf_zpubdir, TRUE,
				FALSE, (const char *) NULL))
	{
	  ulog (LOG_ERROR, "%s: not permitted to receive", zfile);
	  ubuffree (zfile);
	  return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
	}

      if (strchr (qcmd->zoptions, 'f') == NULL)
	{
	  if (! fsysdep_make_dirs (zfile, TRUE))
	    {
	      ubuffree (zfile);
	      return fremote_send_fail (qdaemon, qcmd, FAILURE_OPEN,
					iremote);
	    }
	}
    }

  ztemp = zsysdep_receive_temp (qsys, zfile, qcmd->ztemp,
				(qdaemon->qproto->frestart
				 && (qdaemon->ifeatures
				     & FEATURE_RESTART) != 0));

  /* Adjust the number of bytes we are prepared to receive according
     to the amount of free space we are supposed to leave available
     and the maximum file size we are permitted to transfer.  */
  cbytes = csysdep_bytes_free (ztemp);
  cbytes2 = csysdep_bytes_free (zfile);
  if (cbytes < cbytes2)
    cbytes = cbytes2;

  if (cbytes != -1)
    {
      cbytes -= qsys->uuconf_cfree_space;
      if (cbytes < 0)
	cbytes = 0;
    }

  if (qdaemon->cremote_size != -1
      && (cbytes == -1 || qdaemon->cremote_size < cbytes))
    cbytes = qdaemon->cremote_size;

  /* If the number of bytes we are prepared to receive is less than
     the file size, we must fail.  If the remote did not tell us the
     file size, arbitrarily assumed that it is 10240 bytes.  */
  if (cbytes != -1)
    {
      long csize;

      csize = qcmd->cbytes;
      if (csize == -1)
	csize = CASSUMED_FILE_SIZE;
      if (cbytes < csize)
	{
	  ulog (LOG_ERROR, "%s: too big to receive", zfile);
	  ubuffree (ztemp);
	  ubuffree (zfile);
	  return fremote_send_fail (qdaemon, qcmd, FAILURE_SIZE, iremote);
	}
    }

  /* Open the file to receive into.  This may find an old copy of the
     file, which will be used for file restart if the other side
     supports it.  */
  crestart = -1;
  e = esysdep_open_receive (qsys, zfile, qcmd->ztemp, ztemp,
			    ((qdaemon->qproto->frestart
			      && (qdaemon->ifeatures
				  & FEATURE_RESTART) != 0)
			     ? &crestart
			     : (long *) NULL));
  if (! ffileisopen (e))
    {
      ubuffree (ztemp);
      ubuffree (zfile);
      return fremote_send_fail (qdaemon, qcmd, FAILURE_OPEN, iremote);
    }

  if (crestart > 0)
    {
      DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
		      "fremote_send_file_init: Restarting receive from %ld",
		      crestart);
      if (! ffileseek (e, crestart))
	{
	  ulog (LOG_ERROR, "seek: %s", strerror (errno));
	  (void) ffileclose (e);
	  ubuffree (ztemp);
	  ubuffree (zfile);
	  return FALSE;
	}
    }

  qinfo = (struct srecinfo *) xmalloc (sizeof (struct srecinfo));
  if (strchr (qcmd->zoptions, 'n') == NULL)
    qinfo->zmail = NULL;
  else
    qinfo->zmail = zbufcpy (qcmd->znotify);
  qinfo->zfile = zfile;
  qinfo->ztemp = ztemp;
  qinfo->fspool = fspool;
  qinfo->flocal = FALSE;
  qinfo->freceived = FALSE;
  qinfo->freplied = FALSE;

  qtrans = qtransalc (qcmd);
  qtrans->psendfn = fremote_send_reply;
  qtrans->precfn = frec_file_end;
  qtrans->iremote = iremote;
  qtrans->pinfo = (pointer) qinfo;
  qtrans->frecfile = TRUE;
  qtrans->e = e;
  if (crestart > 0)
    qtrans->ipos = crestart;

  if (qcmd->bcmd == 'E')
    zlog = qcmd->zcmd;
  else
    {
      if (qinfo->fspool)
	zlog = qcmd->zto;
      else
	zlog = qinfo->zfile;
    }
  qtrans->zlog = zbufalc (sizeof "Receiving ( bytes resume at )"
			  + strlen (zlog) + 50);
  sprintf (qtrans->zlog, "Receiving %s", zlog);
  if (crestart > 0 || qcmd->cbytes > 0)
    {
      strcat (qtrans->zlog, " (");
      if (qcmd->cbytes > 0)
	{
	  sprintf (qtrans->zlog + strlen (qtrans->zlog), "%ld bytes",
		   qcmd->cbytes);
	  if (crestart > 0)
	    strcat (qtrans->zlog, " ");
	}
      if (crestart > 0)
	sprintf (qtrans->zlog + strlen (qtrans->zlog), "resume at %ld",
		 crestart);
      strcat (qtrans->zlog, ")");
    }

  return fqueue_remote (qdaemon, qtrans);
}

/* Reply to a send request, and prepare to receive the file.  */

static boolean
fremote_send_reply (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  boolean fret;
  char ab[50];

  /* If the file has been completely received, we just want to send
     the final confirmation.  Otherwise, we must wait for the file
     first.  */
  qtrans->psendfn = frec_file_send_confirm;
  if (qinfo->freceived)
    fret = fqueue_send (qdaemon, qtrans);
  else
    fret = fqueue_receive (qdaemon, qtrans);
  if (! fret)
    return FALSE;

  ab[0] = qtrans->s.bcmd;
  ab[1] = 'Y';
  if (qtrans->ipos <= 0)
    ab[2] = '\0';
  else
    sprintf (ab + 2, " 0x%lx", (unsigned long) qtrans->ipos);

  qinfo->freplied = TRUE;

  if (! (*qdaemon->qproto->pfsendcmd) (qdaemon, ab, qtrans->ilocal,
				       qtrans->iremote))
    {
      (void) ffileclose (qtrans->e);
      (void) remove (qinfo->ztemp);
      /* 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, FALSE,
					(long) -1, &fhandled))
	{
	  (void) ffileclose (qtrans->e);
	  (void) remove (qinfo->ztemp);
	  urrec_free (qtrans);
	  return FALSE;
	}
    }

  return TRUE;
}

/* If we can't receive a file, queue up a response to the remote
   system.  */

static boolean
fremote_send_fail (qdaemon, qcmd, twhy, iremote)
     struct sdaemon *qdaemon;
     struct scmd *qcmd;
     enum tfailure twhy;
     int iremote;
{
  struct srecfailinfo *qinfo;
  struct stransfer *qtrans;

  qinfo = (struct srecfailinfo *) xmalloc (sizeof (struct srecfailinfo));
  qinfo->twhy = twhy;
  qinfo->fsent = FALSE;

  /* If the protocol does not support multiple channels (cchans <= 1),
     then we have essentially already received the entire file.  */
  qinfo->freceived = qdaemon->cchans <= 1;

  qtrans = qtransalc (qcmd);
  qtrans->psendfn = fremote_send_fail_send;
  qtrans->precfn = fremote_discard;
  qtrans->iremote = iremote;
  qtrans->pinfo = (pointer) qinfo;

  return fqueue_remote (qdaemon, qtrans);
}

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

static boolean
fremote_send_fail_send (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct srecfailinfo *qinfo = (struct srecfailinfo *) qtrans->pinfo;
  char ab[4];
  int ilocal, iremote;

  ab[0] = qtrans->s.bcmd;
  ab[1] = 'N';

  switch (qinfo->twhy)
    {
    case FAILURE_PERM:
      ab[2] = '2';
      break;
    case FAILURE_OPEN:
      ab[2] = '4';
      break;
    case FAILURE_SIZE:
      ab[2] = '6';
      break;
    case FAILURE_RECEIVED:
      /* Remember this file as though we successfully received it;
	 when the other side acknowledges our rejection, we know that
	 we no longer have to remember that we received this file.  */
      usent_receive_ack (qdaemon, qtrans);
      ab[2] = '8';
      break;
    default:
      ab[2] = '\0';
      break;
    }
  
  ab[3] = '\0';

  ilocal = qtrans->ilocal;
  iremote = qtrans->iremote;

  /* Wait for the end of file marker if we haven't gotten it yet.  */
  if (! qinfo->freceived)
    {
      qinfo->fsent = TRUE;
      if (! fqueue_receive (qdaemon, qtrans))
	return FALSE;
    }
  else
    {
      xfree (qtrans->pinfo);
      utransfree (qtrans);
    }

  return (*qdaemon->qproto->pfsendcmd) (qdaemon, ab, ilocal, iremote);
}

/* Discard data until we reach the end of the file.  This is used for
   a protocol with multiple channels, since the remote system may
   start sending the file before the confirmation is sent.  If we
   refuse the file, the remote system will get us back in synch by
   sending an empty buffer, which is what we look for here.  */

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

  DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
		  "fremote_discard: Discarding %lu bytes",
		  (unsigned long) cdata);

  if (cdata != 0)
    return TRUE;

  qinfo->freceived = TRUE;

  /* If we have already sent the denial, we are done.  */
  if (qinfo->fsent)
    {
      xfree (qtrans->pinfo);
      utransfree (qtrans);
    }

  return TRUE;
}

/* This is called when a file has been completely received.  It sends
   a response to the remote system.  */

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

  DEBUG_MESSAGE3 (DEBUG_UUCP_PROTO, "frec_file_end: %s to %s (freplied %s)",
		  qtrans->s.zfrom, qtrans->s.zto,
		  qinfo->freplied ? "TRUE" : "FALSE");

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

      if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, FALSE, FALSE,
					(long) -1, &fhandled))
	{
	  (void) ffileclose (qtrans->e);
	  (void) remove (qinfo->ztemp);
	  urrec_free (qtrans);
	  return FALSE;
	}
      if (fhandled)
	return TRUE;
    }

  qinfo->freceived = TRUE;

  fnever = FALSE;

  zalc = NULL;

  if (! fsysdep_sync (qtrans->e, qtrans->s.zto))
    {
      zerr = strerror (errno);
      (void) ffileclose (qtrans->e);
      (void) remove (qinfo->ztemp);
    }
  else if (! ffileclose (qtrans->e))
    {
      zerr = strerror (errno);
      ulog (LOG_ERROR, "%s: close: %s", qtrans->s.zto, zerr);
      (void) remove (qinfo->ztemp);
    }
  else if (! fsysdep_move_file (qinfo->ztemp, qinfo->zfile, qinfo->fspool,
				FALSE, ! qinfo->fspool,
				(qinfo->flocal
				 ? qtrans->s.zuser
				 : (const char *) NULL)))
    {
      long cspace;

      /* Keep the temporary file if there is 1.5 times the amount of
	 required free space.  This is just a random guess, to make an
	 unusual situtation potentially less painful.  */
      cspace = csysdep_bytes_free (qinfo->ztemp);
      if (cspace == -1)
	cspace = FREE_SPACE_DELTA;
      cspace -= (qdaemon->qsys->uuconf_cfree_space
		 + qdaemon->qsys->uuconf_cfree_space / 2);
      if (cspace < 0)
	{
	  (void) remove (qinfo->ztemp);
	  zerr = "could not move to final location";
	}
      else
	{
	  const char *az[20];
	  int i;

	  zalc = zbufalc (sizeof "could not move to final location (left as )"
			  + strlen (qinfo->ztemp));
	  sprintf (zalc, "could not move to final location (left as %s)",
		   qinfo->ztemp);
	  zerr = zalc;

	  i = 0;
	  az[i++] = "The file\n\t";
	  az[i++] = qinfo->ztemp;
	  az[i++] =
	    "\nwas saved because the move to the final location failed.\n";
	  az[i++] = "See the UUCP logs for more details.\n";
	  az[i++] = "The file transfer was from\n\t";
	  az[i++] = qdaemon->qsys->uuconf_zname;
	  az[i++] = "!";
	  az[i++] = qtrans->s.zfrom;
	  az[i++] = "\nto\n\t";
	  az[i++] = qtrans->s.zto;
	  az[i++] = "\nand was requested by\n\t";
	  az[i++] = qtrans->s.zuser;
	  az[i++] = "\n";
	  (void) fsysdep_mail (OWNER, "UUCP temporary file saved", i, az);
	}
      ulog (LOG_ERROR, "%s: %s", qinfo->zfile, zerr);
      fnever = TRUE;
    }
  else
    {
      if (! qinfo->fspool)
	{
	  unsigned int imode;

	  /* Unless we can change the ownership of the file, the only
	     choice to make about these bits is whether to set the
	     execute bit or not.  */
	  if ((qtrans->s.imode & 0111) != 0)
	    imode = 0777;
	  else
	    imode = 0666;
	  (void) fsysdep_change_mode (qinfo->zfile, imode);
	}
  
      zerr = NULL;
    }

  ustats (zerr == NULL, qtrans->s.zuser, qdaemon->qsys->uuconf_zname,
	  FALSE, qtrans->cbytes, qtrans->isecs, qtrans->imicros,
	  qdaemon->fcaller);
  qdaemon->creceived += qtrans->cbytes;

  if (zerr == NULL)
    {
      if (qinfo->zmail != NULL && *qinfo->zmail != '\0')
	(void) fmail_transfer (TRUE, qtrans->s.zuser, qinfo->zmail,
			       (const char *) NULL,
			       qtrans->s.zfrom, qdaemon->qsys->uuconf_zname,
			       qtrans->s.zto, (const char *) NULL,
			       (const char *) NULL);

      if (qtrans->s.pseq != NULL)
	(void) fsysdep_did_work (qtrans->s.pseq);

      if (! qinfo->flocal)
	{
	  /* Remember that we have received this file, so that if the
	     connection drops at this point we won't receive it again.
	     We could check the return value here, but if we return
	     FALSE we couldn't do anything but drop the connection,
	     which would hardly be reasonable.  Instead we trust that
	     the administrator will notice and handle any error
	     messages, which are very unlikely to occur if everything
	     is set up correctly.  */
	  (void) fsysdep_remember_reception (qdaemon->qsys, qtrans->s.zto,
					     qtrans->s.ztemp);
	}
    }
  else
    {
      /* If the transfer failed, we send mail if it was requested
	 locally and if it can never succeed.  */
      if (qinfo->flocal && fnever)
	{
	  (void) fmail_transfer (FALSE, qtrans->s.zuser, qinfo->zmail,
				 zerr, qtrans->s.zfrom,
				 qdaemon->qsys->uuconf_zname,
				 qtrans->s.zto, (const char *) NULL,
				 (const char *) NULL);
	  (void) fsysdep_did_work (qtrans->s.pseq);
	}
    }

  ubuffree (zalc);

  /* If this is an execution request, we must create the execution
     file itself.  */
  if (qtrans->s.bcmd == 'E' && zerr == NULL)
    {
      char *zxqt, *zxqtfile, *ztemp;
      FILE *e;
      boolean fbad;

      /* We get an execution file name by simply replacing the leading
	 D in the received file name with an X.  This pretty much
	 always has to work since we can always receive a file name
	 starting with X, so the system dependent code must be
	 prepared to see one.  */
      zxqt = zbufcpy (qtrans->s.zto);
      zxqt[0] = 'X';
      zxqtfile = zsysdep_spool_file_name (qdaemon->qsys, zxqt,
					  (pointer) NULL);
      ubuffree (zxqt);

      if (zxqtfile == NULL)
	{
	  urrec_free (qtrans);
	  return FALSE;
	}

      /* We have to write via a temporary file, because otherwise
	 uuxqt might pick up the file before we have finished writing
	 it.  */
      e = NULL;
      ztemp = zsysdep_receive_temp (qdaemon->qsys, zxqtfile, "D.0",
				    (qdaemon->qproto->frestart
				     && (qdaemon->ifeatures
					 & FEATURE_RESTART) != 0));
      if (ztemp != NULL)
	e = esysdep_fopen (ztemp, FALSE, FALSE, TRUE);

      if (e == NULL)
	{
	  ubuffree (zxqtfile);
	  ubuffree (ztemp);
	  urrec_free (qtrans);
	  return FALSE;
	}

      fprintf (e, "U %s %s\n", qtrans->s.zuser, qdaemon->qsys->uuconf_zname);
      fprintf (e, "F %s\n", qtrans->s.zto);
      fprintf (e, "I %s\n", qtrans->s.zto);
      if (strchr (qtrans->s.zoptions, 'N') != NULL)
	fprintf (e, "N\n");
      if (strchr (qtrans->s.zoptions, 'Z') != NULL)
	fprintf (e, "Z\n");
      if (strchr (qtrans->s.zoptions, 'R') != NULL)
	fprintf (e, "R %s\n", qtrans->s.znotify);
      if (strchr (qtrans->s.zoptions, 'e') != NULL)
	fprintf (e, "e\n");
      fprintf (e, "C %s\n", qtrans->s.zcmd);

      fbad = FALSE;

      if (! fstdiosync (e, ztemp))
	{
	  (void) fclose (e);
	  (void) remove (ztemp);
	  fbad = TRUE;
	}

      if (! fbad)
	{
	  if (fclose (e) == EOF)
	    {
	      ulog (LOG_ERROR, "fclose: %s", strerror (errno));
	      (void) remove (ztemp);
	      fbad = TRUE;
	    }
	}

      if (! fbad)
	{
	  if (! fsysdep_move_file (ztemp, zxqtfile, TRUE, FALSE, FALSE,
				   (const char *) NULL))
	    {
	      (void) remove (ztemp);
	      fbad = TRUE;
	    }
	}

      ubuffree (zxqtfile);
      ubuffree (ztemp);

      if (fbad)
	{
	  urrec_free (qtrans);
	  return FALSE;
	}
    }

  /* See if we should spawn a uuxqt process.  */
  if (zerr == NULL
      && (qtrans->s.bcmd == 'E'
	  || (qinfo->fspool && qtrans->s.zto[0] == 'X')))
    {
      ++qdaemon->cxfiles_received;
      if (qdaemon->irunuuxqt > 0
	  && qdaemon->cxfiles_received >= qdaemon->irunuuxqt)
	{
	  if (fspawn_uuxqt (TRUE, qdaemon->qsys->uuconf_zname,
			    qdaemon->zconfig))
	    qdaemon->cxfiles_received = 0;
	}
    }

  /* Prepare to send the completion string to the remote system.  If
     we have not yet replied to the remote send request, we leave the
     transfer structure on the remote queue.  Otherwise we add it to
     the send queue.  The psendfn field will already be set.  */
  qinfo->fmoved = zerr == NULL;
  if (qinfo->freplied)
    return fqueue_send (qdaemon, qtrans);

  return TRUE;
}

/* Send the final confirmation string to the remote system.  */

static boolean
frec_file_send_confirm (qtrans, qdaemon)
     struct stransfer *qtrans;
     struct sdaemon *qdaemon;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  const char *zsend;
  int ilocal, iremote;

  if (! qinfo->fmoved)
    zsend = "CN5";
  else if (! qdaemon->frequest_hangup)
    zsend = "CY";
  else
    {
#if DEBUG > 0
      if (qdaemon->fmaster)
	ulog (LOG_FATAL, "frec_file_send_confirm: Can't happen");
#endif

      DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
		      "frec_send_file_confirm: Requesting remote to transfer control");
      zsend = "CYM";
    }

  /* If that was a remote command, then, when the confirmation message
     is acked, we no longer have to remember that we received that
     file.  */
  if (! qinfo->flocal && qinfo->fmoved)
    usent_receive_ack (qdaemon, qtrans);

  ilocal = qtrans->ilocal;
  iremote = qtrans->iremote;

  urrec_free (qtrans);

  return (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, ilocal, iremote);
}

/* Discard a temporary file if it is not useful.  A temporary file is
   useful if it could be used to restart a receive.  This is called if
   the connection is lost.  It is only called if qtrans->frecfile is
   TRUE.  */

boolean
frec_discard_temp (qdaemon, qtrans)
     struct sdaemon *qdaemon;
     struct stransfer *qtrans;
{
  struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;

  if ((qdaemon->ifeatures & FEATURE_RESTART) == 0
      || qtrans->s.ztemp == NULL
      || qtrans->s.ztemp[0] != 'D'
      || strcmp (qtrans->s.ztemp, "D.0") == 0)
    (void) remove (qinfo->ztemp);
  return TRUE;
}