home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / uucico / rec.c < prev    next >
C/C++ Source or Header  |  1995-08-20  |  37KB  |  1,310 lines

  1. /* rec.c
  2.    Routines to receive a file.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1994, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char rec_rcsid[] = "$Id: rec.c,v 1.43 1995/08/02 01:21:12 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33.  
  34. #include "uudefs.h"
  35. #include "uuconf.h"
  36. #include "system.h"
  37. #include "prot.h"
  38. #include "trans.h"
  39.  
  40. /* If the other side does not tell us the size of a file it wants to
  41.    send us, we assume it is this long.  This is only used for free
  42.    space checking.  */
  43. #define CASSUMED_FILE_SIZE (10240)
  44.  
  45. /* We keep this information in the pinfo field of the stransfer
  46.    structure.  */
  47. struct srecinfo
  48. {
  49.   /* Local user to send mail to (may be NULL).  */
  50.   char *zmail;
  51.   /* Full file name.  */
  52.   char *zfile;
  53.   /* Temporary file name.  */
  54.   char *ztemp;
  55.   /* TRUE if this is a spool directory file.  */
  56.   boolean fspool;
  57.   /* TRUE if this was a local request.  */
  58.   boolean flocal;
  59.   /* TRUE if the file has been completely received.  */
  60.   boolean freceived;
  61.   /* TRUE if remote request has been replied to.  */
  62.   boolean freplied;
  63.   /* TRUE if we moved the file to the final destination.  */
  64.   boolean fmoved;
  65. };
  66.  
  67. /* This structure is kept in the pinfo field if we are refusing a
  68.    remote request.  */
  69. struct srecfailinfo
  70. {
  71.   /* Reason for refusal.  */
  72.   enum tfailure twhy;
  73.   /* TRUE if we have sent the reason for refusal.  */
  74.   boolean fsent;
  75.   /* TRUE if we have seen the end of the file.  */
  76.   boolean freceived;
  77. };
  78.  
  79. /* Local functions.  */
  80.  
  81. static void urrec_free P((struct stransfer *qtrans));
  82. static boolean flocal_rec_fail P((struct stransfer *qtrans,
  83.                   struct scmd *qcmd,
  84.                   const struct uuconf_system *qsys,
  85.                   const char *zwhy));
  86. static boolean flocal_rec_send_request P((struct stransfer *qtrans,
  87.                       struct sdaemon *qdaemon));
  88. static boolean flocal_rec_await_reply P((struct stransfer *qtrans,
  89.                      struct sdaemon *qdaemon,
  90.                      const char *zdata,
  91.                      size_t cdata));
  92. static boolean fremote_send_reply P((struct stransfer *qtrans,
  93.                      struct sdaemon *qdaemon));
  94. static boolean fremote_send_fail P((struct sdaemon *qdaemon,
  95.                     struct scmd *qcmd,
  96.                     enum tfailure twhy,
  97.                     int iremote));
  98. static boolean fremote_send_fail_send P((struct stransfer *qtrans,
  99.                      struct sdaemon *qdaemon));
  100. static boolean fremote_discard P((struct stransfer *qtrans,
  101.                   struct sdaemon *qdaemon,
  102.                   const char *zdata, size_t cdata));
  103. static boolean frec_file_end P((struct stransfer *qtrans,
  104.                 struct sdaemon *qdaemon,
  105.                 const char *zdata, size_t cdata));
  106. static boolean frec_file_send_confirm P((struct stransfer *qtrans,
  107.                      struct sdaemon *qdaemon));
  108.  
  109. /* Free up a receive stransfer structure.  */
  110.  
  111. static void
  112. urrec_free (qtrans)
  113.      struct stransfer *qtrans;
  114. {
  115.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  116.  
  117.   if (qinfo != NULL)
  118.     {
  119.       ubuffree (qinfo->zmail);
  120.       ubuffree (qinfo->zfile);
  121.       ubuffree (qinfo->ztemp);
  122.       xfree (qtrans->pinfo);
  123.     }
  124.  
  125.   utransfree (qtrans);
  126. }       
  127.  
  128. /* Set up a request for a file from the remote system.  This may be
  129.    called before the remote system has been called.
  130.  
  131.    This is the order of function calls:
  132.  
  133.    flocal_rec_file_init --> fqueue_local
  134.    flocal_rec_send_request (send R ...) --> fqueue_receive
  135.    flocal_rec_await_reply (open file, call pffile) --> fqueue_receive
  136.    receive file
  137.    frec_file_end (close and move file, call pffile) --> fqueue_send
  138.    frec_file_send_confirm (send CY)
  139.    */
  140.  
  141. boolean
  142. flocal_rec_file_init (qdaemon, qcmd)
  143.      struct sdaemon *qdaemon;
  144.      struct scmd *qcmd;
  145. {
  146.   const struct uuconf_system *qsys;
  147.   boolean fspool;
  148.   char *zfile;
  149.   struct srecinfo *qinfo;
  150.   struct stransfer *qtrans;
  151.  
  152.   qsys = qdaemon->qsys;
  153.  
  154.   /* Make sure we are permitted to transfer files.  */
  155.   if (qdaemon->fcaller
  156.       ? ! qsys->uuconf_fcall_transfer
  157.       : ! qsys->uuconf_fcalled_transfer)
  158.     {
  159.       /* This case will have been checked by uucp or uux, but it could
  160.      have changed.  */
  161.       if (! qsys->uuconf_fcall_transfer
  162.       && ! qsys->uuconf_fcalled_transfer)
  163.     return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
  164.                 "not permitted to request files");
  165.       return TRUE;
  166.     }
  167.  
  168.   fspool = fspool_file (qcmd->zto);
  169.  
  170.   if (fspool)
  171.     {
  172.       pointer puuconf;
  173.       int iuuconf;
  174.       const char *zlocalname;
  175.       struct uuconf_system slocalsys;
  176.  
  177.       /* Normal users are not allowed to request files to be received
  178.      into the spool directory.  To support uux forwarding, we use
  179.      the special option '9'.  This permits a file to be received
  180.      into the spool directory for the local system only without
  181.      the usual checking.  This is only done for local requests, of
  182.      course.  */
  183.       if (qcmd->zto[0] != 'D'
  184.       || strchr (qcmd->zoptions, '9') == NULL)
  185.     return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
  186.                 "not permitted to receive");
  187.  
  188.       puuconf = qdaemon->puuconf;
  189.       iuuconf = uuconf_localname (puuconf, &zlocalname);
  190.       if (iuuconf == UUCONF_NOT_FOUND)
  191.     {
  192.       zlocalname = zsysdep_localname ();
  193.       if (zlocalname == NULL)
  194.         return FALSE;
  195.     }
  196.       else if (iuuconf != UUCONF_SUCCESS)
  197.     {
  198.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  199.       return FALSE;
  200.     }
  201.  
  202.       iuuconf = uuconf_system_info (puuconf, zlocalname, &slocalsys);
  203.       if (iuuconf == UUCONF_NOT_FOUND)
  204.     {
  205.       iuuconf = uuconf_system_local (puuconf, &slocalsys);
  206.       if (iuuconf != UUCONF_SUCCESS)
  207.         {
  208.           ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  209.           return FALSE;
  210.         }
  211.       slocalsys.uuconf_zname = (char *) zlocalname;
  212.     }
  213.       else if (iuuconf != UUCONF_SUCCESS)
  214.     {
  215.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  216.       return FALSE;
  217.     }
  218.  
  219.       zfile = zsysdep_spool_file_name (&slocalsys, qcmd->zto, qcmd->pseq);
  220.  
  221.       (void) uuconf_system_free (puuconf, &slocalsys);
  222.  
  223.       if (zfile == NULL)
  224.     return FALSE;
  225.     }
  226.   else
  227.     {
  228.       zfile = zsysdep_add_base (qcmd->zto, qcmd->zfrom);
  229.       if (zfile == NULL)
  230.     return FALSE;
  231.  
  232.       /* Check permissions.  */
  233.       if (! fin_directory_list (zfile, qsys->uuconf_pzlocal_receive,
  234.                 qsys->uuconf_zpubdir, TRUE,
  235.                 FALSE, qcmd->zuser))
  236.     {
  237.       ubuffree (zfile);
  238.       return flocal_rec_fail ((struct stransfer *) NULL, qcmd, qsys,
  239.                   "not permitted to receive");
  240.     }
  241.  
  242.       /* The 'f' option means that directories should not
  243.      be created if they do not already exist.  */
  244.       if (strchr (qcmd->zoptions, 'f') == NULL)
  245.     {
  246.       if (! fsysdep_make_dirs (zfile, TRUE))
  247.         {
  248.           ubuffree (zfile);
  249.           return flocal_rec_fail ((struct stransfer *) NULL, qcmd,
  250.                       qsys, "cannot create directories");
  251.         }
  252.     }
  253.     }
  254.  
  255.   qinfo = (struct srecinfo *) xmalloc (sizeof (struct srecinfo));
  256.   if (strchr (qcmd->zoptions, 'm') == NULL)
  257.     qinfo->zmail = NULL;
  258.   else
  259.     qinfo->zmail = zbufcpy (qcmd->zuser);
  260.   qinfo->zfile = zfile;
  261.   qinfo->ztemp = NULL;
  262.   qinfo->fspool = fspool;
  263.   qinfo->flocal = TRUE;
  264.   qinfo->freceived = FALSE;
  265.   qinfo->freplied = TRUE;
  266.  
  267.   qtrans = qtransalc (qcmd);
  268.   qtrans->psendfn = flocal_rec_send_request;
  269.   qtrans->pinfo = (pointer) qinfo;
  270.  
  271.   return fqueue_local (qdaemon, qtrans);
  272. }
  273.  
  274. /* Report an error for a local receive request.  */
  275.  
  276. static boolean
  277. flocal_rec_fail (qtrans, qcmd, qsys, zwhy)
  278.      struct stransfer *qtrans;
  279.      struct scmd *qcmd;
  280.      const struct uuconf_system *qsys;
  281.      const char *zwhy;
  282. {
  283.   if (zwhy != NULL)
  284.     {
  285.       ulog (LOG_ERROR, "%s: %s", qcmd->zfrom, zwhy);
  286.       (void) fmail_transfer (FALSE, qcmd->zuser, (const char *) NULL, zwhy,
  287.                  qcmd->zfrom, qsys->uuconf_zname,
  288.                  qcmd->zto, (const char *) NULL,
  289.                  (const char *) NULL);
  290.       (void) fsysdep_did_work (qcmd->pseq);
  291.     }
  292.   if (qtrans != NULL)
  293.     urrec_free (qtrans);
  294.   return TRUE;
  295. }
  296.  
  297. /* This is called when we are ready to send the actual request to the
  298.    other system.  */
  299.  
  300. static boolean
  301. flocal_rec_send_request (qtrans, qdaemon)
  302.      struct stransfer *qtrans;
  303.      struct sdaemon *qdaemon;
  304. {
  305.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  306.   long cbytes, cbytes2;
  307.   size_t clen;
  308.   char *zsend;
  309.   boolean fret;
  310.  
  311.   qinfo->ztemp = zsysdep_receive_temp (qdaemon->qsys, qinfo->zfile,
  312.                        (const char *) NULL,
  313.                        (qdaemon->qproto->frestart
  314.                     && (qdaemon->ifeatures
  315.                         & FEATURE_RESTART) != 0));
  316.   if (qinfo->ztemp == NULL)
  317.     {
  318.       urrec_free (qtrans);
  319.       return FALSE;
  320.     }
  321.  
  322.   qtrans->fcmd = TRUE;
  323.   qtrans->precfn = flocal_rec_await_reply;
  324.  
  325.   if (! fqueue_receive (qdaemon, qtrans))
  326.     return FALSE;
  327.  
  328.   /* Check the amount of free space available for both the temporary
  329.      file and the real file.  */
  330.   cbytes = csysdep_bytes_free (qinfo->ztemp);
  331.   cbytes2 = csysdep_bytes_free (qinfo->zfile);
  332.   if (cbytes < cbytes2)
  333.     cbytes = cbytes2;
  334.   if (cbytes != -1)
  335.     {
  336.       cbytes -= qdaemon->qsys->uuconf_cfree_space;
  337.       if (cbytes < 0)
  338.     cbytes = 0;
  339.     }
  340.  
  341.   if (qdaemon->clocal_size != -1
  342.       && (cbytes == -1 || qdaemon->clocal_size < cbytes))
  343.     cbytes = qdaemon->clocal_size;
  344.  
  345.   /* We send the string
  346.      R from to user options
  347.  
  348.      We put a dash in front of options.  If we are talking to a
  349.      counterpart, we also send the maximum size file we are prepared
  350.      to accept, as returned by esysdep_open_receive.  */
  351.   clen = (strlen (qtrans->s.zfrom) + strlen (qtrans->s.zto)
  352.       + strlen (qtrans->s.zuser) + strlen (qtrans->s.zoptions) + 30);
  353.   zsend = zbufalc (clen);
  354.   if ((qdaemon->ifeatures & FEATURE_SIZES) == 0)
  355.     sprintf (zsend, "R %s %s %s -%s", qtrans->s.zfrom, qtrans->s.zto,
  356.          qtrans->s.zuser, qtrans->s.zoptions);
  357.   else if ((qdaemon->ifeatures & FEATURE_V103) == 0)
  358.     sprintf (zsend, "R %s %s %s -%s 0x%lx", qtrans->s.zfrom, qtrans->s.zto,
  359.          qtrans->s.zuser, qtrans->s.zoptions, (unsigned long) cbytes);
  360.   else
  361.     sprintf (zsend, "R %s %s %s -%s %ld", qtrans->s.zfrom, qtrans->s.zto,
  362.          qtrans->s.zuser, qtrans->s.zoptions, cbytes);
  363.  
  364.   fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, qtrans->ilocal,
  365.                     qtrans->iremote);
  366.   ubuffree (zsend);
  367.  
  368.   /* There is a potential space leak here: if pfsendcmd fails, we
  369.      might need to free qtrans.  However, it is possible that by the
  370.      time pfsendcmd returns, a response will have been received which
  371.      led to the freeing of qtrans anyhow.  One way to fix this would
  372.      be some sort of counter in qtrans to track allocations, but since
  373.      the space leak is small, and the conversation has failed anyhow,
  374.      it doesn't seem worth it.  */
  375.  
  376.   return fret;
  377. }
  378.  
  379. /* This is called when a reply is received for the request.  */
  380.  
  381. /*ARGSUSED*/
  382. static boolean
  383. flocal_rec_await_reply (qtrans, qdaemon, zdata, cdata)
  384.      struct stransfer *qtrans;
  385.      struct sdaemon *qdaemon;
  386.      const char *zdata;
  387.      size_t cdata;
  388. {
  389.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  390.   const char *zlog;
  391.   char *zend;
  392.  
  393.   if (zdata[0] != 'R'
  394.       || (zdata[1] != 'Y' && zdata[1] != 'N'))
  395.     {
  396.       ulog (LOG_ERROR, "%s: bad response to receive request: \"%s\"",
  397.         qtrans->s.zfrom, zdata);
  398.       urrec_free (qtrans);
  399.       return FALSE;
  400.     }
  401.  
  402.   if (zdata[1] == 'N')
  403.     {
  404.       boolean fnever;
  405.       const char *zerr;
  406.  
  407.       fnever = TRUE;
  408.       if (zdata[2] == '2')
  409.     zerr = "no such file";
  410.       else if (zdata[2] == '6')
  411.     {
  412.       /* We sent over the maximum file size we were prepared to
  413.          receive, and the remote system is telling us that the
  414.          file is larger than that.  Try again later.  It would be
  415.          better if we could know whether there will ever be enough
  416.          room.  */
  417.       zerr = "too large to receive now";
  418.       fnever = FALSE;
  419.     }
  420.       else if (zdata[2] == '9')
  421.     {
  422.       /* Remote has run out of channels.  */
  423.       zerr = "too many channels for remote";
  424.       fnever = FALSE;
  425.  
  426.       /* Drop one channel; using exactly one channel causes
  427.          slightly different behahaviour in a few places, so don't
  428.          decrement to one.  */
  429.       if (qdaemon->cchans > 2)
  430.         --qdaemon->cchans;
  431.     }
  432.       else
  433.     zerr = "unknown reason";
  434.  
  435.       if (fnever)
  436.     return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys, zerr);
  437.  
  438.       ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);
  439.  
  440.       urrec_free (qtrans);
  441.  
  442.       return TRUE;
  443.     }
  444.  
  445.   /* The mode should have been sent as "RY 0%o".  If it wasn't, we use
  446.      0666.  */
  447.   qtrans->s.imode = (unsigned int) strtol ((char *) (zdata + 2),
  448.                        &zend, 8);
  449.   if (qtrans->s.imode == 0)
  450.     qtrans->s.imode = 0666;
  451.  
  452.   /* If there is an M after the mode, the remote has requested a
  453.      hangup.  */
  454.   if (*zend == 'M' && qdaemon->fmaster)
  455.     {
  456.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  457.               "flocal_rec_await_reply: Remote has requested transfer of control");
  458.       qdaemon->fhangup_requested = TRUE;
  459.     }
  460.  
  461.   /* Open the file to receive into.  We just ignore any restart count,
  462.      since we have no way to tell it to the other side.  SVR4 may have
  463.      some way to do this, but I don't know what it is.  */
  464.   qtrans->e = esysdep_open_receive (qdaemon->qsys, qinfo->zfile,
  465.                     (const char *) NULL, qinfo->ztemp,
  466.                     (long *) NULL);
  467.   if (! ffileisopen (qtrans->e))
  468.     return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys,
  469.                 "cannot open file");
  470.  
  471.   if (qinfo->fspool)
  472.     zlog = qtrans->s.zto;
  473.   else
  474.     zlog = qinfo->zfile;
  475.   qtrans->zlog = zbufalc (sizeof "Receiving " + strlen (zlog));
  476.   sprintf (qtrans->zlog, "Receiving %s", zlog);
  477.  
  478.   if (qdaemon->qproto->pffile != NULL)
  479.     {
  480.       boolean fhandled;
  481.  
  482.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, FALSE,
  483.                     (long) -1, &fhandled))
  484.     {
  485.       (void) ffileclose (qtrans->e);
  486.       return flocal_rec_fail (qtrans, &qtrans->s, qdaemon->qsys,
  487.                   (const char *) NULL);
  488.     }
  489.       if (fhandled)
  490.     return TRUE;
  491.     }
  492.  
  493.   qtrans->frecfile = TRUE;
  494.   qtrans->psendfn = frec_file_send_confirm;
  495.   qtrans->precfn = frec_file_end;
  496.  
  497.   return fqueue_receive (qdaemon, qtrans);
  498. }
  499.  
  500. /* Make sure there is still enough disk space available to receive a
  501.    file.  */
  502.  
  503. boolean
  504. frec_check_free (qtrans, cfree_space)
  505.      struct stransfer *qtrans;
  506.      long cfree_space;
  507. {
  508.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  509.   long cfree1, cfree2;
  510.  
  511.   cfree1 = csysdep_bytes_free (qinfo->ztemp);
  512.   cfree2 = csysdep_bytes_free (qinfo->zfile);
  513.   if (cfree1 < cfree2)
  514.     cfree1 = cfree2;
  515.   if (cfree1 != -1 && cfree1 < cfree_space)
  516.     {
  517.       ulog (LOG_ERROR, "%s: too big to receive now", qinfo->zfile);
  518.       return FALSE;
  519.     }
  520.  
  521.   return TRUE;
  522. }
  523.  
  524. /* A remote request to send a file to the local system, meaning that
  525.    we are going to receive a file.
  526.  
  527.    If we are using a protocol which does not support multiple
  528.    channels, the remote system will not start sending us the file
  529.    until it has received our confirmation.  In that case, the order of
  530.    functions is as follows:
  531.  
  532.    fremote_send_file_init (open file) --> fqueue_remote
  533.    fremote_send_reply (send SY, call pffile) --> fqueue_receive
  534.    receive file
  535.    frec_file_end (close and move file, call pffile) --> fqueue_send
  536.    frec_file_send_confirm (send CY)
  537.  
  538.    If the protocol supports multiple channels, then the remote system
  539.    will start sending the file immediately after the send request.
  540.    That means that the data may come in before remote_send_reply is
  541.    called, so frec_file_end may be called before fremote_send_reply.
  542.    Note that this means the pffile entry points may be called in
  543.    reverse order for such a protocol.
  544.  
  545.    If the send request is rejected, via fremote_send_fail, and the
  546.    protocol supports multiple channels, we must accept and discard
  547.    data until a zero byte buffer is received from the other side,
  548.    indicating that it has received our rejection.
  549.  
  550.    This code also handles execution requests, which are very similar
  551.    to send requests.  */
  552.  
  553. boolean
  554. fremote_send_file_init (qdaemon, qcmd, iremote)
  555.      struct sdaemon *qdaemon;
  556.      struct scmd *qcmd;
  557.      int iremote;
  558. {
  559.   const struct uuconf_system *qsys;
  560.   boolean fspool;
  561.   char *zfile;
  562.   openfile_t e;
  563.   char *ztemp;
  564.   long cbytes, cbytes2;
  565.   long crestart;
  566.   struct srecinfo *qinfo;
  567.   struct stransfer *qtrans;
  568.   const char *zlog;
  569.  
  570.   qsys = qdaemon->qsys;
  571.  
  572.   if (! qsys->uuconf_frec_request)
  573.     {
  574.       ulog (LOG_ERROR, "%s: not permitted to receive files from remote",
  575.         qcmd->zfrom);
  576.       return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
  577.     }
  578.           
  579.   fspool = fspool_file (qcmd->zto);
  580.  
  581.   /* We don't accept remote command files.  An execution request may
  582.      only send a simple data file.  */
  583.   if ((fspool && qcmd->zto[0] == 'C')
  584.       || (qcmd->bcmd == 'E'
  585.       && (! fspool || qcmd->zto[0] != 'D')))
  586.     {
  587.       ulog (LOG_ERROR, "%s: not permitted to receive", qcmd->zfrom);
  588.       return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
  589.     }
  590.  
  591.   /* See if we have already received this file in a previous
  592.      conversation.  */
  593.   if (fsysdep_already_received (qsys, qcmd->zto, qcmd->ztemp))
  594.     return fremote_send_fail (qdaemon, qcmd, FAILURE_RECEIVED, iremote);
  595.  
  596.   if (fspool)
  597.     {
  598.       zfile = zsysdep_spool_file_name (qsys, qcmd->zto, (pointer) NULL);
  599.       if (zfile == NULL)
  600.     return FALSE;
  601.     }
  602.   else
  603.     {
  604.       boolean fbadname;
  605.  
  606.       zfile = zsysdep_local_file (qcmd->zto, qsys->uuconf_zpubdir,
  607.                   &fbadname);
  608.       if (zfile == NULL && fbadname)
  609.     {
  610.       ulog (LOG_ERROR, "%s: bad local file name", qcmd->zto);
  611.       return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
  612.     }
  613.       if (zfile != NULL)
  614.     {
  615.       char *zadd;
  616.  
  617.       zadd = zsysdep_add_base (zfile, qcmd->zfrom);
  618.       ubuffree (zfile);
  619.       zfile = zadd;
  620.     }
  621.       if (zfile == NULL)
  622.     return FALSE;
  623.  
  624.       /* Check permissions.  */
  625.       if (! fin_directory_list (zfile, qsys->uuconf_pzremote_receive,
  626.                 qsys->uuconf_zpubdir, TRUE,
  627.                 FALSE, (const char *) NULL))
  628.     {
  629.       ulog (LOG_ERROR, "%s: not permitted to receive", zfile);
  630.       ubuffree (zfile);
  631.       return fremote_send_fail (qdaemon, qcmd, FAILURE_PERM, iremote);
  632.     }
  633.  
  634.       if (strchr (qcmd->zoptions, 'f') == NULL)
  635.     {
  636.       if (! fsysdep_make_dirs (zfile, TRUE))
  637.         {
  638.           ubuffree (zfile);
  639.           return fremote_send_fail (qdaemon, qcmd, FAILURE_OPEN,
  640.                     iremote);
  641.         }
  642.     }
  643.     }
  644.  
  645.   ztemp = zsysdep_receive_temp (qsys, zfile, qcmd->ztemp,
  646.                 (qdaemon->qproto->frestart
  647.                  && (qdaemon->ifeatures
  648.                      & FEATURE_RESTART) != 0));
  649.  
  650.   /* Adjust the number of bytes we are prepared to receive according
  651.      to the amount of free space we are supposed to leave available
  652.      and the maximum file size we are permitted to transfer.  */
  653.   cbytes = csysdep_bytes_free (ztemp);
  654.   cbytes2 = csysdep_bytes_free (zfile);
  655.   if (cbytes < cbytes2)
  656.     cbytes = cbytes2;
  657.  
  658.   if (cbytes != -1)
  659.     {
  660.       cbytes -= qsys->uuconf_cfree_space;
  661.       if (cbytes < 0)
  662.     cbytes = 0;
  663.     }
  664.  
  665.   if (qdaemon->cremote_size != -1
  666.       && (cbytes == -1 || qdaemon->cremote_size < cbytes))
  667.     cbytes = qdaemon->cremote_size;
  668.  
  669.   /* If the number of bytes we are prepared to receive is less than
  670.      the file size, we must fail.  If the remote did not tell us the
  671.      file size, arbitrarily assumed that it is 10240 bytes.  */
  672.   if (cbytes != -1)
  673.     {
  674.       long csize;
  675.  
  676.       csize = qcmd->cbytes;
  677.       if (csize == -1)
  678.     csize = CASSUMED_FILE_SIZE;
  679.       if (cbytes < csize)
  680.     {
  681.       ulog (LOG_ERROR, "%s: too big to receive", zfile);
  682.       ubuffree (ztemp);
  683.       ubuffree (zfile);
  684.       return fremote_send_fail (qdaemon, qcmd, FAILURE_SIZE, iremote);
  685.     }
  686.     }
  687.  
  688.   /* Open the file to receive into.  This may find an old copy of the
  689.      file, which will be used for file restart if the other side
  690.      supports it.  */
  691.   crestart = -1;
  692.   e = esysdep_open_receive (qsys, zfile, qcmd->ztemp, ztemp,
  693.                 ((qdaemon->qproto->frestart
  694.                   && (qdaemon->ifeatures
  695.                   & FEATURE_RESTART) != 0)
  696.                  ? &crestart
  697.                  : (long *) NULL));
  698.   if (! ffileisopen (e))
  699.     {
  700.       ubuffree (ztemp);
  701.       ubuffree (zfile);
  702.       return fremote_send_fail (qdaemon, qcmd, FAILURE_OPEN, iremote);
  703.     }
  704.  
  705.   if (crestart > 0)
  706.     {
  707.       DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
  708.               "fremote_send_file_init: Restarting receive from %ld",
  709.               crestart);
  710.       if (! ffileseek (e, crestart))
  711.     {
  712.       ulog (LOG_ERROR, "seek: %s", strerror (errno));
  713.       (void) ffileclose (e);
  714.       ubuffree (ztemp);
  715.       ubuffree (zfile);
  716.       return FALSE;
  717.     }
  718.     }
  719.  
  720.   qinfo = (struct srecinfo *) xmalloc (sizeof (struct srecinfo));
  721.   if (strchr (qcmd->zoptions, 'n') == NULL)
  722.     qinfo->zmail = NULL;
  723.   else
  724.     qinfo->zmail = zbufcpy (qcmd->znotify);
  725.   qinfo->zfile = zfile;
  726.   qinfo->ztemp = ztemp;
  727.   qinfo->fspool = fspool;
  728.   qinfo->flocal = FALSE;
  729.   qinfo->freceived = FALSE;
  730.   qinfo->freplied = FALSE;
  731.  
  732.   qtrans = qtransalc (qcmd);
  733.   qtrans->psendfn = fremote_send_reply;
  734.   qtrans->precfn = frec_file_end;
  735.   qtrans->iremote = iremote;
  736.   qtrans->pinfo = (pointer) qinfo;
  737.   qtrans->frecfile = TRUE;
  738.   qtrans->e = e;
  739.   if (crestart > 0)
  740.     qtrans->ipos = crestart;
  741.  
  742.   if (qcmd->bcmd == 'E')
  743.     zlog = qcmd->zcmd;
  744.   else
  745.     {
  746.       if (qinfo->fspool)
  747.     zlog = qcmd->zto;
  748.       else
  749.     zlog = qinfo->zfile;
  750.     }
  751.   qtrans->zlog = zbufalc (sizeof "Receiving ( bytes resume at )"
  752.               + strlen (zlog) + 50);
  753.   sprintf (qtrans->zlog, "Receiving %s", zlog);
  754.   if (crestart > 0 || qcmd->cbytes > 0)
  755.     {
  756.       strcat (qtrans->zlog, " (");
  757.       if (qcmd->cbytes > 0)
  758.     {
  759.       sprintf (qtrans->zlog + strlen (qtrans->zlog), "%ld bytes",
  760.            qcmd->cbytes);
  761.       if (crestart > 0)
  762.         strcat (qtrans->zlog, " ");
  763.     }
  764.       if (crestart > 0)
  765.     sprintf (qtrans->zlog + strlen (qtrans->zlog), "resume at %ld",
  766.          crestart);
  767.       strcat (qtrans->zlog, ")");
  768.     }
  769.  
  770.   return fqueue_remote (qdaemon, qtrans);
  771. }
  772.  
  773. /* Reply to a send request, and prepare to receive the file.  */
  774.  
  775. static boolean
  776. fremote_send_reply (qtrans, qdaemon)
  777.      struct stransfer *qtrans;
  778.      struct sdaemon *qdaemon;
  779. {
  780.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  781.   boolean fret;
  782.   char ab[50];
  783.  
  784.   /* If the file has been completely received, we just want to send
  785.      the final confirmation.  Otherwise, we must wait for the file
  786.      first.  */
  787.   qtrans->psendfn = frec_file_send_confirm;
  788.   if (qinfo->freceived)
  789.     fret = fqueue_send (qdaemon, qtrans);
  790.   else
  791.     fret = fqueue_receive (qdaemon, qtrans);
  792.   if (! fret)
  793.     return FALSE;
  794.  
  795.   ab[0] = qtrans->s.bcmd;
  796.   ab[1] = 'Y';
  797.   if (qtrans->ipos <= 0)
  798.     ab[2] = '\0';
  799.   else
  800.     sprintf (ab + 2, " 0x%lx", (unsigned long) qtrans->ipos);
  801.  
  802.   qinfo->freplied = TRUE;
  803.  
  804.   if (! (*qdaemon->qproto->pfsendcmd) (qdaemon, ab, qtrans->ilocal,
  805.                        qtrans->iremote))
  806.     {
  807.       (void) ffileclose (qtrans->e);
  808.       (void) remove (qinfo->ztemp);
  809.       /* Should probably free qtrans here, but see the comment at the
  810.          end of flocal_rec_send_request.  */
  811.       return FALSE;
  812.     }
  813.  
  814.   if (qdaemon->qproto->pffile != NULL)
  815.     {
  816.       boolean fhandled;
  817.  
  818.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, FALSE,
  819.                     (long) -1, &fhandled))
  820.     {
  821.       (void) ffileclose (qtrans->e);
  822.       (void) remove (qinfo->ztemp);
  823.       urrec_free (qtrans);
  824.       return FALSE;
  825.     }
  826.     }
  827.  
  828.   return TRUE;
  829. }
  830.  
  831. /* If we can't receive a file, queue up a response to the remote
  832.    system.  */
  833.  
  834. static boolean
  835. fremote_send_fail (qdaemon, qcmd, twhy, iremote)
  836.      struct sdaemon *qdaemon;
  837.      struct scmd *qcmd;
  838.      enum tfailure twhy;
  839.      int iremote;
  840. {
  841.   struct srecfailinfo *qinfo;
  842.   struct stransfer *qtrans;
  843.  
  844.   qinfo = (struct srecfailinfo *) xmalloc (sizeof (struct srecfailinfo));
  845.   qinfo->twhy = twhy;
  846.   qinfo->fsent = FALSE;
  847.  
  848.   /* If the protocol does not support multiple channels (cchans <= 1),
  849.      then we have essentially already received the entire file.  */
  850.   qinfo->freceived = qdaemon->cchans <= 1;
  851.  
  852.   qtrans = qtransalc (qcmd);
  853.   qtrans->psendfn = fremote_send_fail_send;
  854.   qtrans->precfn = fremote_discard;
  855.   qtrans->iremote = iremote;
  856.   qtrans->pinfo = (pointer) qinfo;
  857.  
  858.   return fqueue_remote (qdaemon, qtrans);
  859. }
  860.  
  861. /* Send a failure string for a send command to the remote system;
  862.    this is called when we are ready to reply to the command.  */
  863.  
  864. static boolean
  865. fremote_send_fail_send (qtrans, qdaemon)
  866.      struct stransfer *qtrans;
  867.      struct sdaemon *qdaemon;
  868. {
  869.   struct srecfailinfo *qinfo = (struct srecfailinfo *) qtrans->pinfo;
  870.   char ab[4];
  871.   int ilocal, iremote;
  872.  
  873.   ab[0] = qtrans->s.bcmd;
  874.   ab[1] = 'N';
  875.  
  876.   switch (qinfo->twhy)
  877.     {
  878.     case FAILURE_PERM:
  879.       ab[2] = '2';
  880.       break;
  881.     case FAILURE_OPEN:
  882.       ab[2] = '4';
  883.       break;
  884.     case FAILURE_SIZE:
  885.       ab[2] = '6';
  886.       break;
  887.     case FAILURE_RECEIVED:
  888.       /* Remember this file as though we successfully received it;
  889.      when the other side acknowledges our rejection, we know that
  890.      we no longer have to remember that we received this file.  */
  891.       usent_receive_ack (qdaemon, qtrans);
  892.       ab[2] = '8';
  893.       break;
  894.     default:
  895.       ab[2] = '\0';
  896.       break;
  897.     }
  898.   
  899.   ab[3] = '\0';
  900.  
  901.   ilocal = qtrans->ilocal;
  902.   iremote = qtrans->iremote;
  903.  
  904.   /* Wait for the end of file marker if we haven't gotten it yet.  */
  905.   if (! qinfo->freceived)
  906.     {
  907.       qinfo->fsent = TRUE;
  908.       if (! fqueue_receive (qdaemon, qtrans))
  909.     return FALSE;
  910.     }
  911.   else
  912.     {
  913.       xfree (qtrans->pinfo);
  914.       utransfree (qtrans);
  915.     }
  916.  
  917.   return (*qdaemon->qproto->pfsendcmd) (qdaemon, ab, ilocal, iremote);
  918. }
  919.  
  920. /* Discard data until we reach the end of the file.  This is used for
  921.    a protocol with multiple channels, since the remote system may
  922.    start sending the file before the confirmation is sent.  If we
  923.    refuse the file, the remote system will get us back in synch by
  924.    sending an empty buffer, which is what we look for here.  */
  925.  
  926. /*ARGSUSED*/
  927. static boolean
  928. fremote_discard (qtrans, qdaemon, zdata, cdata)
  929.      struct stransfer *qtrans;
  930.      struct sdaemon *qdaemon;
  931.      const char *zdata;
  932.      size_t cdata;
  933. {
  934.   struct srecfailinfo *qinfo = (struct srecfailinfo *) qtrans->pinfo;
  935.  
  936.   DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
  937.           "fremote_discard: Discarding %lu bytes",
  938.           (unsigned long) cdata);
  939.  
  940.   if (cdata != 0)
  941.     return TRUE;
  942.  
  943.   qinfo->freceived = TRUE;
  944.  
  945.   /* If we have already sent the denial, we are done.  */
  946.   if (qinfo->fsent)
  947.     {
  948.       xfree (qtrans->pinfo);
  949.       utransfree (qtrans);
  950.     }
  951.  
  952.   return TRUE;
  953. }
  954.  
  955. /* This is called when a file has been completely received.  It sends
  956.    a response to the remote system.  */
  957.  
  958. /*ARGSUSED*/
  959. static boolean
  960. frec_file_end (qtrans, qdaemon, zdata, cdata)
  961.      struct stransfer *qtrans;
  962.      struct sdaemon *qdaemon;
  963.      const char *zdata;
  964.      size_t cdata;
  965. {
  966.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  967.   char *zalc;
  968.   const char *zerr;
  969.   boolean fnever;
  970.  
  971.   DEBUG_MESSAGE3 (DEBUG_UUCP_PROTO, "frec_file_end: %s to %s (freplied %s)",
  972.           qtrans->s.zfrom, qtrans->s.zto,
  973.           qinfo->freplied ? "TRUE" : "FALSE");
  974.  
  975.   if (qdaemon->qproto->pffile != NULL)
  976.     {
  977.       boolean fhandled;
  978.  
  979.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, FALSE, FALSE,
  980.                     (long) -1, &fhandled))
  981.     {
  982.       (void) ffileclose (qtrans->e);
  983.       (void) remove (qinfo->ztemp);
  984.       urrec_free (qtrans);
  985.       return FALSE;
  986.     }
  987.       if (fhandled)
  988.     return TRUE;
  989.     }
  990.  
  991.   qinfo->freceived = TRUE;
  992.  
  993.   fnever = FALSE;
  994.  
  995.   zalc = NULL;
  996.  
  997.   if (! fsysdep_sync (qtrans->e, qtrans->s.zto))
  998.     {
  999.       zerr = strerror (errno);
  1000.       (void) ffileclose (qtrans->e);
  1001.       (void) remove (qinfo->ztemp);
  1002.     }
  1003.   else if (! ffileclose (qtrans->e))
  1004.     {
  1005.       zerr = strerror (errno);
  1006.       ulog (LOG_ERROR, "%s: close: %s", qtrans->s.zto, zerr);
  1007.       (void) remove (qinfo->ztemp);
  1008.     }
  1009.   else if (! fsysdep_move_file (qinfo->ztemp, qinfo->zfile, qinfo->fspool,
  1010.                 FALSE, ! qinfo->fspool,
  1011.                 (qinfo->flocal
  1012.                  ? qtrans->s.zuser
  1013.                  : (const char *) NULL)))
  1014.     {
  1015.       long cspace;
  1016.  
  1017.       /* Keep the temporary file if there is 1.5 times the amount of
  1018.      required free space.  This is just a random guess, to make an
  1019.      unusual situtation potentially less painful.  */
  1020.       cspace = csysdep_bytes_free (qinfo->ztemp);
  1021.       if (cspace == -1)
  1022.     cspace = FREE_SPACE_DELTA;
  1023.       cspace -= (qdaemon->qsys->uuconf_cfree_space
  1024.          + qdaemon->qsys->uuconf_cfree_space / 2);
  1025.       if (cspace < 0)
  1026.     {
  1027.       (void) remove (qinfo->ztemp);
  1028.       zerr = "could not move to final location";
  1029.     }
  1030.       else
  1031.     {
  1032.       const char *az[20];
  1033.       int i;
  1034.  
  1035.       zalc = zbufalc (sizeof "could not move to final location (left as )"
  1036.               + strlen (qinfo->ztemp));
  1037.       sprintf (zalc, "could not move to final location (left as %s)",
  1038.            qinfo->ztemp);
  1039.       zerr = zalc;
  1040.  
  1041.       i = 0;
  1042.       az[i++] = "The file\n\t";
  1043.       az[i++] = qinfo->ztemp;
  1044.       az[i++] =
  1045.         "\nwas saved because the move to the final location failed.\n";
  1046.       az[i++] = "See the UUCP logs for more details.\n";
  1047.       az[i++] = "The file transfer was from\n\t";
  1048.       az[i++] = qdaemon->qsys->uuconf_zname;
  1049.       az[i++] = "!";
  1050.       az[i++] = qtrans->s.zfrom;
  1051.       az[i++] = "\nto\n\t";
  1052.       az[i++] = qtrans->s.zto;
  1053.       az[i++] = "\nand was requested by\n\t";
  1054.       az[i++] = qtrans->s.zuser;
  1055.       az[i++] = "\n";
  1056.       (void) fsysdep_mail (OWNER, "UUCP temporary file saved", i, az);
  1057.     }
  1058.       ulog (LOG_ERROR, "%s: %s", qinfo->zfile, zerr);
  1059.       fnever = TRUE;
  1060.     }
  1061.   else
  1062.     {
  1063.       if (! qinfo->fspool)
  1064.     {
  1065.       unsigned int imode;
  1066.  
  1067.       /* Unless we can change the ownership of the file, the only
  1068.          choice to make about these bits is whether to set the
  1069.          execute bit or not.  */
  1070.       if ((qtrans->s.imode & 0111) != 0)
  1071.         imode = 0777;
  1072.       else
  1073.         imode = 0666;
  1074.       (void) fsysdep_change_mode (qinfo->zfile, imode);
  1075.     }
  1076.   
  1077.       zerr = NULL;
  1078.     }
  1079.  
  1080.   ustats (zerr == NULL, qtrans->s.zuser, qdaemon->qsys->uuconf_zname,
  1081.       FALSE, qtrans->cbytes, qtrans->isecs, qtrans->imicros,
  1082.       qdaemon->fcaller);
  1083.   qdaemon->creceived += qtrans->cbytes;
  1084.  
  1085.   if (zerr == NULL)
  1086.     {
  1087.       if (qinfo->zmail != NULL && *qinfo->zmail != '\0')
  1088.     (void) fmail_transfer (TRUE, qtrans->s.zuser, qinfo->zmail,
  1089.                    (const char *) NULL,
  1090.                    qtrans->s.zfrom, qdaemon->qsys->uuconf_zname,
  1091.                    qtrans->s.zto, (const char *) NULL,
  1092.                    (const char *) NULL);
  1093.  
  1094.       if (qtrans->s.pseq != NULL)
  1095.     (void) fsysdep_did_work (qtrans->s.pseq);
  1096.  
  1097.       if (! qinfo->flocal)
  1098.     {
  1099.       /* Remember that we have received this file, so that if the
  1100.          connection drops at this point we won't receive it again.
  1101.          We could check the return value here, but if we return
  1102.          FALSE we couldn't do anything but drop the connection,
  1103.          which would hardly be reasonable.  Instead we trust that
  1104.          the administrator will notice and handle any error
  1105.          messages, which are very unlikely to occur if everything
  1106.          is set up correctly.  */
  1107.       (void) fsysdep_remember_reception (qdaemon->qsys, qtrans->s.zto,
  1108.                          qtrans->s.ztemp);
  1109.     }
  1110.     }
  1111.   else
  1112.     {
  1113.       /* If the transfer failed, we send mail if it was requested
  1114.      locally and if it can never succeed.  */
  1115.       if (qinfo->flocal && fnever)
  1116.     {
  1117.       (void) fmail_transfer (FALSE, qtrans->s.zuser, qinfo->zmail,
  1118.                  zerr, qtrans->s.zfrom,
  1119.                  qdaemon->qsys->uuconf_zname,
  1120.                  qtrans->s.zto, (const char *) NULL,
  1121.                  (const char *) NULL);
  1122.       (void) fsysdep_did_work (qtrans->s.pseq);
  1123.     }
  1124.     }
  1125.  
  1126.   ubuffree (zalc);
  1127.  
  1128.   /* If this is an execution request, we must create the execution
  1129.      file itself.  */
  1130.   if (qtrans->s.bcmd == 'E' && zerr == NULL)
  1131.     {
  1132.       char *zxqt, *zxqtfile, *ztemp;
  1133.       FILE *e;
  1134.       boolean fbad;
  1135.  
  1136.       /* We get an execution file name by simply replacing the leading
  1137.      D in the received file name with an X.  This pretty much
  1138.      always has to work since we can always receive a file name
  1139.      starting with X, so the system dependent code must be
  1140.      prepared to see one.  */
  1141.       zxqt = zbufcpy (qtrans->s.zto);
  1142.       zxqt[0] = 'X';
  1143.       zxqtfile = zsysdep_spool_file_name (qdaemon->qsys, zxqt,
  1144.                       (pointer) NULL);
  1145.       ubuffree (zxqt);
  1146.  
  1147.       if (zxqtfile == NULL)
  1148.     {
  1149.       urrec_free (qtrans);
  1150.       return FALSE;
  1151.     }
  1152.  
  1153.       /* We have to write via a temporary file, because otherwise
  1154.      uuxqt might pick up the file before we have finished writing
  1155.      it.  */
  1156.       e = NULL;
  1157.       ztemp = zsysdep_receive_temp (qdaemon->qsys, zxqtfile, "D.0",
  1158.                     (qdaemon->qproto->frestart
  1159.                      && (qdaemon->ifeatures
  1160.                      & FEATURE_RESTART) != 0));
  1161.       if (ztemp != NULL)
  1162.     e = esysdep_fopen (ztemp, FALSE, FALSE, TRUE);
  1163.  
  1164.       if (e == NULL)
  1165.     {
  1166.       ubuffree (zxqtfile);
  1167.       ubuffree (ztemp);
  1168.       urrec_free (qtrans);
  1169.       return FALSE;
  1170.     }
  1171.  
  1172.       fprintf (e, "U %s %s\n", qtrans->s.zuser, qdaemon->qsys->uuconf_zname);
  1173.       fprintf (e, "F %s\n", qtrans->s.zto);
  1174.       fprintf (e, "I %s\n", qtrans->s.zto);
  1175.       if (strchr (qtrans->s.zoptions, 'N') != NULL)
  1176.     fprintf (e, "N\n");
  1177.       if (strchr (qtrans->s.zoptions, 'Z') != NULL)
  1178.     fprintf (e, "Z\n");
  1179.       if (strchr (qtrans->s.zoptions, 'R') != NULL)
  1180.     fprintf (e, "R %s\n", qtrans->s.znotify);
  1181.       if (strchr (qtrans->s.zoptions, 'e') != NULL)
  1182.     fprintf (e, "e\n");
  1183.       fprintf (e, "C %s\n", qtrans->s.zcmd);
  1184.  
  1185.       fbad = FALSE;
  1186.  
  1187.       if (! fstdiosync (e, ztemp))
  1188.     {
  1189.       (void) fclose (e);
  1190.       (void) remove (ztemp);
  1191.       fbad = TRUE;
  1192.     }
  1193.  
  1194.       if (! fbad)
  1195.     {
  1196.       if (fclose (e) == EOF)
  1197.         {
  1198.           ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  1199.           (void) remove (ztemp);
  1200.           fbad = TRUE;
  1201.         }
  1202.     }
  1203.  
  1204.       if (! fbad)
  1205.     {
  1206.       if (! fsysdep_move_file (ztemp, zxqtfile, TRUE, FALSE, FALSE,
  1207.                    (const char *) NULL))
  1208.         {
  1209.           (void) remove (ztemp);
  1210.           fbad = TRUE;
  1211.         }
  1212.     }
  1213.  
  1214.       ubuffree (zxqtfile);
  1215.       ubuffree (ztemp);
  1216.  
  1217.       if (fbad)
  1218.     {
  1219.       urrec_free (qtrans);
  1220.       return FALSE;
  1221.     }
  1222.     }
  1223.  
  1224.   /* See if we should spawn a uuxqt process.  */
  1225.   if (zerr == NULL
  1226.       && (qtrans->s.bcmd == 'E'
  1227.       || (qinfo->fspool && qtrans->s.zto[0] == 'X')))
  1228.     {
  1229.       ++qdaemon->cxfiles_received;
  1230.       if (qdaemon->irunuuxqt > 0
  1231.       && qdaemon->cxfiles_received >= qdaemon->irunuuxqt)
  1232.     {
  1233.       if (fspawn_uuxqt (TRUE, qdaemon->qsys->uuconf_zname,
  1234.                 qdaemon->zconfig))
  1235.         qdaemon->cxfiles_received = 0;
  1236.     }
  1237.     }
  1238.  
  1239.   /* Prepare to send the completion string to the remote system.  If
  1240.      we have not yet replied to the remote send request, we leave the
  1241.      transfer structure on the remote queue.  Otherwise we add it to
  1242.      the send queue.  The psendfn field will already be set.  */
  1243.   qinfo->fmoved = zerr == NULL;
  1244.   if (qinfo->freplied)
  1245.     return fqueue_send (qdaemon, qtrans);
  1246.  
  1247.   return TRUE;
  1248. }
  1249.  
  1250. /* Send the final confirmation string to the remote system.  */
  1251.  
  1252. static boolean
  1253. frec_file_send_confirm (qtrans, qdaemon)
  1254.      struct stransfer *qtrans;
  1255.      struct sdaemon *qdaemon;
  1256. {
  1257.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  1258.   const char *zsend;
  1259.   int ilocal, iremote;
  1260.  
  1261.   if (! qinfo->fmoved)
  1262.     zsend = "CN5";
  1263.   else if (! qdaemon->frequest_hangup)
  1264.     zsend = "CY";
  1265.   else
  1266.     {
  1267. #if DEBUG > 0
  1268.       if (qdaemon->fmaster)
  1269.     ulog (LOG_FATAL, "frec_file_send_confirm: Can't happen");
  1270. #endif
  1271.  
  1272.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  1273.               "frec_send_file_confirm: Requesting remote to transfer control");
  1274.       zsend = "CYM";
  1275.     }
  1276.  
  1277.   /* If that was a remote command, then, when the confirmation message
  1278.      is acked, we no longer have to remember that we received that
  1279.      file.  */
  1280.   if (! qinfo->flocal && qinfo->fmoved)
  1281.     usent_receive_ack (qdaemon, qtrans);
  1282.  
  1283.   ilocal = qtrans->ilocal;
  1284.   iremote = qtrans->iremote;
  1285.  
  1286.   urrec_free (qtrans);
  1287.  
  1288.   return (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, ilocal, iremote);
  1289. }
  1290.  
  1291. /* Discard a temporary file if it is not useful.  A temporary file is
  1292.    useful if it could be used to restart a receive.  This is called if
  1293.    the connection is lost.  It is only called if qtrans->frecfile is
  1294.    TRUE.  */
  1295.  
  1296. boolean
  1297. frec_discard_temp (qdaemon, qtrans)
  1298.      struct sdaemon *qdaemon;
  1299.      struct stransfer *qtrans;
  1300. {
  1301.   struct srecinfo *qinfo = (struct srecinfo *) qtrans->pinfo;
  1302.  
  1303.   if ((qdaemon->ifeatures & FEATURE_RESTART) == 0
  1304.       || qtrans->s.ztemp == NULL
  1305.       || qtrans->s.ztemp[0] != 'D'
  1306.       || strcmp (qtrans->s.ztemp, "D.0") == 0)
  1307.     (void) remove (qinfo->ztemp);
  1308.   return TRUE;
  1309. }
  1310.