home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk445.lzh / UUCP / uucico.c < prev   
C/C++ Source or Header  |  1991-01-24  |  36KB  |  1,589 lines

  1.  
  2. /*
  3.  *  UUCICO.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/uucico/RCS/uucico.c,v 1.1 90/02/02 11:56:01 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1987 by John Gilmore.
  8.  *  Copying and use of this program are controlled by the terms of the Free
  9.  *  Software Foundation's GNU Emacs General Public License.
  10.  *
  11.  *  Derived from:
  12.  *  i[$]uuslave.c     1.7 08/12/85 14:04:20
  13.  *  which came from the ACGNJ BBS system at +1 201 753 9758.  Original
  14.  *  author unknown.
  15.  *
  16.  *  Ported to Amiga by William Loftus
  17.  *  Amiga Changes Copyright 1988 by William Loftus.  All rights reserved.
  18.  *  Additional Major Changes (c)Copyright 1989 by Matthew Dillon, All rights reserved
  19.  *
  20.  * 14-Oct-89, moved modem_init() to before poll_sys.
  21.  *
  22.  * -r option       (-r1 does a call out to all systems we have mail for)
  23.  * -D[EVICE] dev    sets serial device name (automatic from Getty)
  24.  * -U[NIT] unit     sets unit name (automatic from Getty)
  25.  * -h0            Ignore CD (carrier detect)
  26.  * -7
  27.  */
  28.  
  29.  
  30. #include "includes.h"           /* System include files, system dependent */
  31. #include "uucp.h"               /* Uucp definitions and parameters */
  32. #include <log.h>
  33. #include "version.h"
  34.  
  35. #define PROTOF_SHEREEQUALS  0x0001  /*    ProtoHacks  */
  36.  
  37. Prototype   int getname(int);
  38. Prototype   int get_proto(void);
  39. Prototype   int instr(char *, int, int);
  40. Prototype   int twrite(const char *, int);
  41. Prototype   void xlat_str(char *);
  42. Prototype   int read_ctl(void);
  43. Prototype   int do_outbound(void);
  44. Prototype   int call_system(char *, int);
  45. Prototype   int call_sysline(char *);
  46. Prototype   int do_session(int);
  47. Prototype   int top_level(int);
  48. Prototype   int do_one_slave(void);
  49. Prototype   int do_one_master(void);
  50. Prototype   int yesno(char, int, int);
  51. Prototype   int host_send_file(char *);
  52. Prototype   int host_receive_file(char *);
  53. Prototype   int local_send_file(char *, int *);
  54. Prototype   int local_receive_file(void);
  55. Prototype   int receive_file(FILE *, char *, char *, char *, int);
  56. Prototype   int send_file(FILE *);
  57.  
  58. Prototype   short ProtoHacks;
  59. Prototype   short PriMode;
  60. Prototype   short OldPri;
  61. Prototype   short IgnoreDTR;
  62. Prototype   int   PacketTimeout;
  63.  
  64. #define MAX_FLAGS    40
  65.  
  66. extern int errno;
  67.  
  68. IDENT(".14");
  69.  
  70. static char *Copyright = COPYRIGHT;
  71.  
  72. char    ttynam[NAMESIZE],        /* Name of tty we use as serial port */
  73.     srcnam[NAMESIZE],        /* Source file name */
  74.     dstnam[NAMESIZE],        /* Dest file name */
  75.     who[NAMESIZE] = "-",            /* Who sent the file */
  76.     flags[MAX_FLAGS],        /* Flags from file xfer cmd */
  77.     temp[NAMESIZE];         /* Temp file name */
  78.  
  79. int    ourpid = 0,            /* Our process ID */
  80.     ignore_time_restrictions = 0,    /* Call out even if L.sys sez no */
  81.     mode;                /* File mode from file xfer cmd */
  82.  
  83. char  host_name[MAX_HOST] = "AmigaUUCP";  /* Other guy's host name */
  84. char  our_name[MAX_HOST];    /* Our uucp hostname, set from usenet.ctl */
  85. char  path[128];
  86. int   debug   = -1;    /* -1 indicates not set by command line or ctl file */
  87. int   f_wait  = 0;    /* wait for a call (-w) or calls (-w -e) after outbnd */
  88. int   loop    = 0;    /* Loop accepting logins if tty name specified */
  89. int   Overide = 0;    /* overide modem protocol        */
  90. int   Getty   = 0;    /* -Getty initiated            */
  91. int   IgnoreCD= 0;    /* xgetc() should ignore carrier?   */
  92. int   OurNameOv= 0;
  93. int   WindowSize = 999;
  94. int   NoReScan = 0;    /* do not rescan for work after error    */
  95. int   SevenWire= 0;
  96. int   XDebug  = 0;    /* do not pass debug parameter to remote    */
  97. int   DebugHandshake = 0;
  98. int   PacketTimeout = 0;
  99. short ProtoHacks;     /*  protocol hacks  */
  100. short PriMode;
  101. short OldPri;
  102. short IgnoreDTR = 0;
  103.  
  104. #define MAX_STRING    200    /* Max length string to send/expect */
  105.  
  106. #define MSGO2IDX    6
  107.  
  108. /* We print these prompts */
  109.  
  110. char msgo0[] = "login: ";
  111. char msgo1[] = "Password:";
  112. char msgo2[10+MAX_HOST] = { "\20Shere" };   /*  NO =    */
  113. char msgo3[] = "\20ROK\0";
  114. char msgo3a[]= "\20P";
  115. char msgo3b[]= "\20Pg\0";
  116. char msgo4[] = "\20OOOOOOO\0";
  117.  
  118. /* We expect to receive these strings */
  119.  
  120. char msgi0[] = "uucp\r";
  121. char msgi1[] = "s8000\r";
  122. /* char msgi2[] = "\20S*\0"; We now scan it specially FIXME */
  123. char msgi3[] = "\20Ug\0";
  124. char msgi4[] = "OOOOOO";
  125.  
  126. /*
  127.  * Protocol switch data structure
  128.  */
  129.  
  130. #define turnon    gturnon
  131. #define rdmsg    grdmsg
  132. #define wrmsg    gwrmsg
  133. #define rddata    grddata
  134. #define wrdata    gwrdata
  135. #define turnoff gturnoff
  136.  
  137. int
  138. getname(isshere)
  139. int isshere;
  140. {
  141.     int data, count = 0;
  142.     static char msgi[MAX_STRING+SLOP];    /* Incoming trash buffer */
  143.  
  144.     /* Read data until null character */
  145.  
  146.     while ((data = xgetc(BYTE_TO)) != EOF) {
  147.     data &= 0x7F;
  148.     if (data == 020)
  149.         break;
  150.     }
  151.     if (data == EOF)
  152.     return FAIL;
  153.  
  154.     while ((data = xgetc(BYTE_TO)) != EOF && (data & 0x7F)) {
  155.     data &= 0x7F;
  156.     if (count == 0 && data != 'S')
  157.         continue;
  158.     if (count > sizeof(msgi) - 2)
  159.         continue;
  160.     if (data == 0x0A)       /*  hack fix for tuvie ? */
  161.         break;
  162.     msgi[count++] = (char)data;
  163.     }
  164.     msgi[count] = 0;
  165.  
  166.     if (debug > 8)
  167.     printf("GETNAME MSG (%d): %s\n", count, msgi);
  168.  
  169.     if (msgi[0] != 'S')
  170.     return FAIL;
  171.     if (isshere) {
  172.     for (count = 1; msgi[count] && msgi[count] != '='; ++count);
  173.     if (msgi[count] == '=')
  174.         ++count;
  175.     } else {
  176.     count = 1;
  177.     }
  178.     if (msgi[count]) {
  179.     if (debug > 8)
  180.         printf("Compare host names: '%s' '%s'\n", host_name, msgi + count);
  181.     strcpy (host_name, msgi + count);
  182.     }
  183.     strtok(host_name, " \t");     /*  put \0 after hostname */
  184.     if (debug > 8)
  185.     printf("Hostname is '%s'\n", host_name);
  186.     return SUCCESS;
  187. }
  188.  
  189. /*
  190.  *  get_proto() checks the list of protos given by the foriegn machine
  191.  *  checking for 'g' (which is the only proto we have).  Use only in master
  192.  *  mode.
  193.  */
  194.  
  195. int
  196. get_proto()
  197. {
  198.     int data;
  199.  
  200.     while ((data = xgetc(BYTE_TO)) != EOF) {
  201.     data &= 0x7F;
  202.     if (data == 0)
  203.         break;
  204.     if (data == 'g')
  205.         return(SUCCESS);
  206.     }
  207.     return FAIL;
  208. }
  209.  
  210. /*
  211.  * Medium level input routine.
  212.  *
  213.  * Look for an input string for the send-expect sequence.
  214.  * Return 0 for matching string, 1 for timeout before we found it.
  215.  * FIXME:  we only time out if the other end stops sending.  If it
  216.  *       keeps sending, we keep listening forever.
  217.  */
  218.  
  219. instr(s, n, to)
  220. char *s;
  221. int n;
  222. int to;     /*    timeout */
  223. {
  224.     int data,count,j;
  225.     int i;
  226.     static char msgi[512];  /* Incoming trash buffer */
  227.  
  228.     count = 0;
  229.     if (to == 0)
  230.     to = BYTE_TO;
  231.  
  232.     if (debug > 8) {
  233.     printf("Expecting ");
  234.     for (i = 0; i < n; i++)
  235.         printc(s[i]);
  236.     printf("\n");
  237.     }
  238.     if (DebugHandshake) {
  239.     printf("recvd: '");
  240.     fflush(stdout);
  241.     }
  242.  
  243.     while ((data = xgetc(to)) != EOF) {
  244.     data &= 0x7F;
  245.  
  246.     msgi[count++] = data;
  247.  
  248.     if (DebugHandshake) {
  249.         if (data < 0x20)
  250.         printf("^%c", data + '@');
  251.         else
  252.         printf("%c", data);
  253.         fflush(stdout);
  254.     }
  255.  
  256.     if (count == sizeof(msgi)) {    /*  throw away first half */
  257.         count = sizeof(msgi) / 2;
  258.         bcopy(msgi + sizeof(msgi) / 2, msgi, sizeof(msgi) / 2);
  259.     }
  260.  
  261.     if (count >= n) {
  262.         for (i = n - 1, j = count - 1; i >= 0; i--, j--) {
  263.         if (*(s+i) != msgi[j])
  264.             break;
  265.         }
  266.         if (i < 0) {
  267.         if (debug > 8)
  268.             printf("\n");
  269.         if (DebugHandshake)
  270.             printf("' (GOTIT!)\n");
  271.         return(0);
  272.         }
  273.     }
  274.     }
  275.     if (DebugHandshake)
  276.     printf("' (TIMEOUT!)\n");
  277.  
  278.     if (debug > 8)
  279.     printf("\n");
  280.     msgi[count] = (char)0;
  281.     return(1);
  282. }
  283.  
  284. /*
  285.  * Debugging hack for stuff written to the modem.
  286.  */
  287.  
  288. int
  289. twrite(s, n)
  290. const char *s;
  291. int  n;
  292. {
  293.     int i;
  294.  
  295.     if (debug > 8) {
  296.     printf("Wrote:  ");
  297.     for (i = 0; i < n; i++)
  298.         printc(s[i]);
  299.     printf("\n");
  300.     }
  301.     return xwrite(s, n);
  302. }
  303.  
  304. void
  305. myexit()
  306. {
  307.     long task = FindTask(NULL);
  308.     if (PriMode)
  309.     SetTaskPri(OldPri);
  310. }
  311.  
  312. /*
  313.  * MAIN ROUTINE.
  314.  *
  315.  * This is called at program startup.  It parses the arguments to the
  316.  * program (if any) and sets up to receive a call on the modem.
  317.  *
  318.  * If there are no arguments, we assume the caller is already on standard
  319.  * input, waiting to do uucp protocols (past the login prompt), and we
  320.  * just handle one caller.
  321.  *
  322.  * If there is an argument, it is the name of the tty device where we
  323.  * should listen for multiple callers and handle login and password.
  324.  */
  325.  
  326. main(argc,argv)
  327. int argc;
  328. char *argv[];
  329. {
  330.     int     i;
  331.     char    *poll_sys = (char *)NULL;   /* System name to poll, or none */
  332.     short   rmode = 0;            /* 1 = master, 0 = slave    */
  333.  
  334.     LogProgram = "uucico";
  335.     LogHost = host_name;
  336.     LogWho  = who;
  337.  
  338.     signal(SIGINT, sigint);
  339.     atexit(myexit);
  340.  
  341.     /* FIXME, use getopt */
  342.     /* scan command line arguments, kinda kludgy but it works */
  343.  
  344.     for (i = 1; i < argc; ++i) {
  345.     char *ptr = argv[i];
  346.  
  347.     if (*ptr != '-') {
  348.         printf("uucico: warning, extra args ignored: %s\n", argv[i]);
  349.         break;
  350.     }
  351.     ptr += 2;
  352.     switch (ptr[-1]) {
  353.     case 'N':
  354.         strcpy(our_name, ptr);
  355.         OurNameOv = 1;
  356.         break;
  357.     case 'D':       /*  Serial Device   */
  358.         {
  359.         extern char *DeviceName;
  360.         DeviceName = argv[++i];
  361.         }
  362.         break;
  363.     case 'U':       /*  Serial Unit     */
  364.         {
  365.         extern long DeviceUnit;
  366.         DeviceUnit = ((*ptr >= '0' && *ptr <= '9') ? atoi(ptr) : atoi(argv[++i]));
  367.         }
  368.         break;
  369.     case 'p':       /*  protocol hacks  */
  370.         if (strcmp(ptr, "ri") == 0) {   /*  -pri    */
  371.         long task = FindTask(NULL);
  372.  
  373.         PriMode = 1;
  374.         OldPri = SetTaskPri(task, 5);
  375.         SetTaskPri(task, OldPri + 1);
  376.         } else {                /*    -proto    */
  377.         ProtoHacks |= (*ptr) ? atoi(ptr) : atoi(argv[++i]);
  378.         }
  379.         break;
  380.     case 'g':
  381.     case 'G':
  382.         Getty = 1;
  383.         break;
  384.     case 'h':
  385.         IgnoreCD = 1;
  386.         break;
  387.     case 'w':
  388.         ++f_wait;
  389.         break;
  390.     case 'r':
  391.         rmode = atoi(&argv[i][2]);
  392.         break;
  393.     case 'X':
  394.         XDebug = 1;
  395.     case 'x':
  396.         if (argv[i][2] == 'x') {
  397.         DebugHandshake = 1;
  398.         break;
  399.         }
  400.  
  401.         debug = atoi(&argv[i][2]);
  402.         LogLevel = debug;
  403.         LogToStdout = 0;
  404.         printf("uucico: debug level set to %d\n", debug);
  405.         break;
  406.     case 'o':
  407.         Overide = 1;
  408.         break;
  409.     case 'n':
  410.         WindowSize = (*ptr) ? atoi(ptr) : 1;
  411.         break;
  412.     case 'b':
  413.         system(GetConfigProgram(BATCHNEWS));
  414.         break;
  415.     case 'S':
  416.         ignore_time_restrictions++;
  417.     case 's':
  418.         poll_sys = &argv[i][2];
  419.         break;
  420.     case 'e':
  421.         ++loop;
  422.         break;
  423.     case 't':
  424.         PacketTimeout = (*ptr) ? atoi(ptr) : atoi(argv[++i]);
  425.         break;
  426.     case '7':
  427.         SevenWire = 1;
  428.         break;
  429.     case 'd':
  430.         IgnoreDTR = (*ptr) ? !atoi(ptr) : 1;
  431.         break;
  432.     default:
  433.         printf("uucico: warning, bad flag %s\n", argv[i]);
  434.         break;
  435.     }
  436.     }
  437.  
  438.     /* If argument provided, use it as name of comm port */
  439.  
  440.     /* FIXME, this needs some thought. */
  441.  
  442.     getcwd(path,128);
  443.     if (chdir(GetConfigDir(UUSPOOL))) {
  444.     perror("Can't chdir to Spool directory");
  445.     exit(2);
  446.     }
  447.  
  448.     read_ctl();
  449.  
  450.     /*
  451.      * If running via getty/login, our debug stdout had better
  452.      * go to a file, not to the usual stdout!
  453.      */
  454.  
  455.     if (debug > 0 && Getty) {
  456.     freopen("T:uuslave.log", "a", stdout);
  457.     }
  458.  
  459.     /*setvbuf(stdout, NULL, _IOLBF, 0);*/
  460.  
  461.     /* Timestamp the long debug log */
  462.  
  463.     if (debug > 0) {
  464.     long clock;
  465.  
  466.     time(&clock);
  467.     printf("\014\nuuslave log on tty '%s' starting %s\n",
  468.         ttynam, ctime(&clock));
  469.     }
  470.  
  471.     /* Log our presence so we humans reading the logs can find the
  472.        entries created by uuslave. */
  473.  
  474.     ulog(-1, "Startup %s", VERSION);
  475.  
  476.     amiga_setup();
  477.  
  478.     modem_init();
  479.  
  480.     if (poll_sys) {
  481.     if (*poll_sys == '\0')
  482.         poll_sys = (char *)NULL;
  483.     call_system(poll_sys, rmode);
  484.     if (!f_wait)
  485.         goto end;
  486.     } else {
  487.     if (rmode) {
  488.         do_outbound();
  489.         if (!f_wait)
  490.         goto end;
  491.     }
  492.     }
  493.  
  494.     do {
  495.     /*
  496.      *  Set up serial channel, wait for incoming call.
  497.      */
  498.     DEBUG(0, "\nRestarting\n", 0);
  499.  
  500.     if (Getty == 0 && Overide == 0)
  501.         openline();
  502.  
  503.     do_session(Getty);
  504.  
  505.     hangup();
  506.     DEBUG(0, "\nEnd of call\n", 0);
  507.     } while (loop && !Getty);
  508.  
  509. end:
  510.     cleanup();
  511.     return(0);
  512. }
  513.  
  514. /*
  515.  * translate embedded escape characters
  516.  */
  517.  
  518. void
  519. xlat_str(msg)
  520. char    *msg;
  521. {
  522.     int i  = 0;
  523.     int cr = 1;
  524.  
  525.     while (msg[i]) {
  526.     if (msg[i] == '\\') {
  527.         switch (msg[++i]) {
  528.         case 'r':            /* carriage return */
  529.         twrite("\r", 1);
  530.         break;
  531.         case 'n':            /* line feed */
  532.         twrite("\n", 1);
  533.         break;
  534.         case '\\':           /* back slash */
  535.         twrite("\\", 1);
  536.         break;
  537.         case 't':            /* tab */
  538.         twrite("\t", 1);
  539.         break;
  540.         case 'b':
  541.         SendBreak();
  542.         break;
  543.         case 'd':            /* delay */
  544.         Delay(180);
  545.         break;
  546.         case 's':            /* space */
  547.         twrite(" ", 1);
  548.         break;
  549.         case 'c':            /* no CR at end */
  550.         cr = 0;
  551.         break;
  552.         default:        /* don't know so skip it */
  553.         break;
  554.         }
  555.         ++i;
  556.     } else {
  557.         twrite(msg + i, 1);
  558.         ++i;
  559.     }
  560.     }
  561.     if (cr) {
  562.     twrite("\r", 1);
  563.     }
  564. }
  565.  
  566. /*
  567.  * Read the control file and grab a few parameters.
  568.  */
  569.  
  570. int
  571. read_ctl()
  572. {
  573.     char *nodename = FindConfig(NODENAME);
  574.     char *debugstr = FindConfig(DEBUGNAME);
  575.  
  576.     if (nodename && OurNameOv == 0)
  577.     strcpy(our_name, nodename);
  578.     if (debugstr && debug < 0)
  579.     debug = atoi(debugstr);
  580.     return (1);
  581. }
  582.  
  583. /*
  584.  * Search spool queues for work, call the systems we need to call.
  585.  */
  586.  
  587. int
  588. do_outbound()
  589. {
  590.     return call_system((char *)NULL, 1);
  591. }
  592.  
  593. /*
  594.  * Call a specific system, or all systems that have work pending.
  595.  */
  596.  
  597. int
  598. call_system(sys, ifworkpend)
  599. char    *sys;
  600. {
  601.     FILE    *lsys;
  602.     static char buf[MAX_LSYS];
  603.     static char sysnam[MAX_HOST];
  604.     static char prev_name[MAX_HOST];
  605.     int     called = FAIL;
  606.  
  607.     /*
  608.      * Unix uucico just reads the directory, and calls the systems
  609.      * in the order of the files in the directory.  We want more
  610.      * control than that, though I'm not sure that L.sys order is
  611.      * best either.  For example, in the first call after 11PM,
  612.      * I'd like to call the sites that haven't been callable before
  613.      * 11PM first, and finish up with the ones I've been able to call
  614.      * all day.  FIXME.
  615.      */
  616.  
  617.     if (! (lsys = fopen(MakeConfigPath(UULIB, "L.sys"), "r"))) {
  618.     DEBUG(0, "uucico: can't open L.sys, errno %d\n", errno);
  619.     return 0;
  620.     }
  621.     sysnam[0] = '\0';               /* Initially, no previous sys */
  622.  
  623.     /* Once per system in L.sys... */
  624.     /* FIXME, handle continuation lines (trailing "\") */
  625.  
  626.     while (fgets(buf, sizeof buf, lsys)) {
  627.     if (buf[0] == '#' || buf[0] == '\n')
  628.         continue;
  629.  
  630.     /*
  631.      * Grab the system name.  If same as previous, and
  632.      * the previous call worked, skip it.
  633.      */
  634.  
  635.     strcpy(prev_name, sysnam);
  636.     (void) sscanf(buf, "%s", sysnam);
  637.     if (!strcmp(sysnam, prev_name)) {
  638.         if (called == SUCCESS)
  639.         continue;
  640.     }
  641.  
  642.     /*
  643.      * If a system name was specified, skip til we find it
  644.      * If none was specified, only call if there is work.
  645.      */
  646.  
  647.     if (sys) {
  648.         if (strcmp(sys, sysnam) != 0)
  649.         continue;
  650.         if (ifworkpend && !work_scan(sysnam)) {
  651.         ulog(-1, "No work for system %s", sysnam);
  652.         called = SUCCESS;
  653.         continue;
  654.         }
  655.     } else {
  656.         DEBUG(3,"searching for outbound to %s\n", sysnam);
  657.         if (!work_scan(sysnam)) {
  658.         DEBUG(3,"no work for %s\n", sysnam);
  659.         called = SUCCESS;    /* Don't try further */
  660.         continue;
  661.         }
  662.         DEBUG(2, "uucico: found work for %s\n", sysnam);
  663.     }
  664.  
  665.     called = call_sysline(buf);
  666.  
  667.     if (called == SUCCESS && sys)
  668.         break;
  669.     }
  670.  
  671.     fclose(lsys);
  672.     if (called == FAIL && sys)
  673.     DEBUG(0, "Could not call system %s\n", sys);
  674.     return 0;
  675. }
  676.  
  677. /*
  678.  *  Call out to a system, given its L.sys line.
  679.  */
  680.  
  681. int
  682. call_sysline(lsysline)
  683. char *lsysline;
  684. {
  685.     static char    tempname[MAX_HOST + 30 + SLOP];
  686.     char    *sysnam,
  687.         *times,
  688.         *acu,
  689.         *sbaud,
  690.         *telno,
  691.         *send,
  692.         *expct;
  693.     int     baud;
  694.  
  695.     who[0] = '-'; who[1] = '\0';    /* No user now (for logit) */
  696.  
  697.     /* FIXME, use the values it is ignoring here */
  698.  
  699.     sysnam = strtok(lsysline, " \t");
  700.     times =  strtok(NULL, " \t");   /* Time */
  701.     acu =    strtok(NULL, " \t");   /* ACU  */
  702.     sbaud =  strtok(NULL, " \t");   /* Baud */
  703.     telno =  strtok(NULL," \t");    /* phone*/
  704.  
  705.     strcpy(host_name, sysnam);
  706.  
  707.     if (ignore_time_restrictions == 0) {
  708.     if (CheckTimeRestrictions(times) == FAIL) {
  709.         ulog(-1, "Wrong Time To Call %s", sysnam);
  710.         return(FAIL);
  711.     }
  712.     }
  713.  
  714.     baud = atoi(sbaud);
  715.  
  716.     /*    FIX ME, acu not implemented ?    */
  717.     DEBUG(4, "Opening outgoing line %s\n", acu);
  718.     if (openout(acu, baud) != SUCCESS)
  719.     return FAIL;
  720.  
  721.     if (Overide == 0) {
  722.     if (dial_nbr(telno)) {
  723.         ulog(-1, "FAILED call to %s", host_name);
  724.         return FAIL;
  725.     }
  726.     }
  727.  
  728.     /* FIXME, log tty, baud rate, ... */
  729.     ulog(-1, "DIALED %s", host_name);
  730.  
  731.     /*
  732.      * Process send-expect strings.
  733.      * FIXME, deal with "-", BREAK, etc.
  734.      */
  735.  
  736.     if (DebugHandshake)
  737.     puts("CONNECTED, running send-expect strings");
  738.  
  739.     while (send = (char*)strtok((char *)NULL, " \t\n")) {
  740.     if (send[0] != '"' || send[1] != '"' || send[2] != '\0') {
  741.         if (DebugHandshake)
  742.         printf("Expect %s\n", send);
  743.         if (instr(send, strlen(send), 0))
  744.             goto bort1;
  745.     } else if (DebugHandshake) {
  746.         puts("Expect Nothing");
  747.     }
  748.  
  749.     if (expct = (char*)strtok((char *)NULL, " \t\n")) {
  750.         if (DebugHandshake)
  751.         printf("Send: %s\n", expct);
  752.         /* FIXME secondary strings, e.g. ogin:-EOT-ogin: */
  753.         xlat_str(expct);
  754.     }
  755.     }
  756.  
  757.     /*
  758.      * FIXME, there should be a way to detect login/passwd
  759.      * failure here and keep doing the script rather than
  760.      * continuing to expect Shere at another login: prompt.
  761.      */
  762.  
  763.     ulog(-1, "SUCCEEDED call to %s", host_name);
  764.  
  765.  
  766.     if (getname(1))         /*  get name        */
  767.     goto bort1;
  768.                 /*    send response    */
  769.     sprintf(tempname, "\20S%s -Q0 -x%d\0", our_name, (XDebug) ? 0 : debug);
  770.     twrite(tempname, strlen(tempname)+1); /* Including null */
  771.  
  772.     /* wait for ok message, wait for protocol request
  773.      * send protocol 'g' response */
  774.     /* FIXME, we don't actually wait for the ROK message, since
  775.      * it is immediately followed by the Pprotos message.  We
  776.      * currently just look for a Pg message.  This needs work.
  777.      * FIXME, WE CAN'T TALK TO SITES THAT SUPPORT more than 'g'.
  778.      */
  779.  
  780.     if (instr(msgo3a, sizeof(msgo3a)-1, 0)) {
  781.     if (!get_proto())
  782.        goto bort1;
  783.     }
  784.  
  785.  
  786.     twrite( msgi3, sizeof(msgi3)-1);
  787.  
  788.     ResetGIO();                         /* reset GIO protocol   */
  789.  
  790.     if (turnon(1))
  791.     goto bort1;
  792.  
  793.     ulog(-1, "OK Startup");
  794.  
  795.     top_level(1);
  796.     hangup();
  797.     return SUCCESS;
  798.  
  799. bort1:
  800.     hangup();
  801.     return FAIL;
  802. }
  803.  
  804. /* Handle a single uucp [slave] login session */
  805.  
  806. int
  807. do_session(ontheline)
  808. int ontheline;
  809. {
  810.     if (ontheline == 0) {
  811.     /* output login request, verify uucp */
  812.     twrite(msgo0,sizeof(msgo0)-1);
  813.     if (instr(msgi0, sizeof(msgi0)-1, 0)) {
  814.         printf("uucico: invalid login name\n");
  815.         goto bort;
  816.     }
  817.  
  818.     /* output password request, verify s8000 */
  819.     twrite(msgo1,sizeof(msgo1)-1);
  820.     if (instr(msgi1, sizeof(msgi1)-1, 0)) {
  821.         printf("uucico: invalid password\n");
  822.         goto bort;
  823.     }
  824.  
  825.     printf("uucico: correct login\n");
  826.     }
  827.  
  828.     /*
  829.      *    send Shere=<myhost>
  830.      *
  831.      *    Apparently mac UUCP has a bug that only allows 7
  832.      *    char host names, and it fails if it gets shere=<myhost>
  833.      *    where <myhost> is > 7 chars.
  834.      *
  835.      *    Apparently some implementations of AmigaUUCP do not accept
  836.      *    an SHere with an =<myhost>, so this is disabled unless -p1 is used.
  837.      */
  838.  
  839.     if (ProtoHacks & PROTOF_SHEREEQUALS)
  840.     sprintf(msgo2 + MSGO2IDX, "=%s", our_name);
  841.     twrite(msgo2,strlen(msgo2)+1);
  842.  
  843.     /*
  844.      *    get \020S<host> -Qn n    (??)
  845.      */
  846.  
  847.     if (getname(0))
  848.     goto bort;
  849.  
  850.     /* output ok message, output protocol request, wait for response */
  851.  
  852.     twrite(msgo3,sizeof(msgo3)-1);
  853.  
  854.     /* FIXME, make the protocol list here, and use it */
  855.     twrite(msgo3b,sizeof(msgo3b)-1);
  856.     if (instr(msgi3, sizeof(msgi3)-1, 0))
  857.         goto bort;
  858.  
  859.     ResetGIO();                         /* reset GIO protocol   */
  860.  
  861.     if (turnon(0))
  862.     goto bort;
  863.  
  864.     ulog(-1, "OK Startup");
  865.     top_level(0);
  866.  
  867. bort:
  868.     if (debug > 0)
  869.     printf("uucico: call complete\n");
  870.     return (1);
  871. }
  872.  
  873. /*
  874.  * Handle transactions "at top level", as Unix uucp's debug log says.
  875.  *
  876.  * As master, we scan our queues for work and send requests to the
  877.  * other side.    When done, we send a hangup request and switch to slave mode.
  878.  *
  879.  * As slave, we accept requests from the other side; when it is done,
  880.  * it sends a hangup request, and we switch to master mode, if we have
  881.  * any work queued up for that system.
  882.  *
  883.  * This repeats as long as either side has work to do.    When all the
  884.  * queued work is done, we agree to hang up, terminate the packet protocol,
  885.  * and return to the caller.  (We still haven't hung up the phone line yet.)
  886.  *
  887.  * A curious feature of the hangup protocol is that it is not a simple
  888.  * question-answer.  The master says "H", asking about hangup.  The
  889.  * slave responds "HY" saying OK.  The master then says "HY" also,
  890.  * then both of them hang up.  Maybe this is to make sure the first HY
  891.  * got ack'ed?  Anyway, an "H" is reported as HANGUP and an "HY" as
  892.  * HANGNOW.  After we send an HY, we go back to listening for commands;
  893.  * if the master sends something other than HY, we'll do it.
  894.  */
  895.  
  896. #define HANGUP    2        /* Signal to switch master/slave roles */
  897. #define HANGNOW 3        /* Signal to hang up now */
  898. #define COPYFAIL    4    /* File copy failed */
  899.  
  900. int
  901. top_level(master_mode)
  902. int master_mode;
  903. {
  904.     static char    buf[MAXMSGLEN];    /* For hangup responses */
  905.  
  906.  
  907.     if (master_mode) {
  908.     (void) work_scan(host_name);    /* Kick off queue scan */
  909.     goto master;
  910.     }
  911.  
  912.     for (;;) {
  913.     slave:            /*  SLAVE SIDE    */
  914.     for (;;) {
  915.         DEBUG(4, "*** TOP *** - slave\n", 0);
  916.         switch (do_one_slave()) {
  917.         case SUCCESS:
  918.         break;
  919.         case FAIL:
  920.         DEBUG(4, "*** DO_ONE_SLAVE FAIL *** - slave\n", 0);
  921.         return FAIL;
  922.         case HANGUP:
  923.         if (work_scan(host_name)) {
  924.             if (wrmsg("HN") != SUCCESS) {
  925.             DEBUG(4, "*** WRMSG HN FAIL *** - slave\n", 0);
  926.             return FAIL;
  927.             }
  928.             goto master;
  929.         } else {
  930.             if (wrmsg("HY") != SUCCESS) {
  931.             DEBUG(4, "*** WRMSG HY FAIL *** - slave\n", 0);
  932.             return FAIL;
  933.             }
  934.             break;    /*  go to master mode */
  935.         }
  936.         case HANGNOW:
  937.         goto quit;
  938.         }
  939.     }
  940.     master:    /*  MASTER SIDE */
  941.     for (;;) {
  942.         DEBUG(4, "*** TOP *** - master\n", 0);
  943.         switch (do_one_master()) {
  944.         case SUCCESS:
  945.         break;
  946.         case FAIL:
  947.         DEBUG(4, "*** DO_ONE_MASTER FAIL *** - master\n", 0);
  948.         return FAIL;
  949.         case HANGUP:
  950.         /* We wrote an H command, what's the resp? */
  951.         if (rdmsg(buf, MAXMSGLEN) != SUCCESS) {
  952.             DEBUG(4, "*** RDMSG HANGUP FAIL *** - master\n", 0);
  953.             return FAIL;
  954.         }
  955.         if (buf[0] != 'H') {
  956.             DEBUG(4, "*** RDMSG HANGUP != 'H' *** - master\n", 0);
  957.             return FAIL;
  958.         }
  959.         if (buf[1] == 'N') {
  960.             goto slave;
  961.         } else {
  962.             /*
  963.              *    send final HY?    not sure if this should happen, will
  964.              *    necessarily fail if the other side does not expect
  965.              *    it so do not return... continue on w/ exit code.
  966.              *
  967.              *    however, reduce timeout parameters
  968.              */
  969.  
  970.             ++ReducedTimeout;
  971.             wrmsg("HY");
  972.             --ReducedTimeout;
  973.             goto quit;
  974.         }
  975.         }
  976.     }
  977.     }
  978.  
  979. quit:
  980.     /* Shut down the packet protocol */
  981.  
  982.     turnoff();
  983.  
  984.     /* Write the closing sequence */
  985.  
  986.     twrite(msgo4, sizeof(msgo4)-1);
  987.     (void) instr(msgi4, sizeof(msgi4)-1, 0);
  988.  
  989.     twrite(msgo4, sizeof(msgo4)-1);
  990.  
  991.     strcpy(who, "-");
  992.     ulog(-1, "OK Conversation complete");
  993.  
  994.     return SUCCESS;   /* Go byebye */
  995. }
  996.  
  997. /*
  998.  * We are slave; get a command from the other side and execute it.
  999.  *
  1000.  * Result is SUCCESS, FAIL, HANGUP, or HANGNOW.
  1001.  */
  1002.  
  1003. int
  1004. do_one_slave()
  1005. {
  1006.     static char msg[MAXMSGLEN];        /* Master's message to us */
  1007.  
  1008.     /* Get master's command */
  1009.     if (rdmsg(msg, MAXMSGLEN) != SUCCESS)
  1010.     return FAIL;
  1011.  
  1012.     /* Print it for easy debugging */
  1013.     DEBUG(5,"\nCommand: %s\n\n", msg);
  1014.  
  1015.     switch (msg[0]) {
  1016.     case 'S':
  1017.     if (msg[1] != ' ')
  1018.         break;
  1019.     return host_send_file(msg);
  1020.     case 'R':
  1021.     if (msg[1] != ' ')
  1022.         break;
  1023.     return host_receive_file(msg);
  1024.     case 'X':
  1025.     break;
  1026.     case 'H':
  1027.     if (msg[1] == '\0') return HANGUP;
  1028.     if (msg[1] == 'Y')  return HANGNOW;
  1029.     if (msg[1] == 'N')  return SUCCESS;     /* Ignore HN to slave */
  1030.     break;
  1031.     }
  1032.  
  1033.     /* Unrecognized packet from the other end */
  1034.  
  1035.     DEBUG(0, "Bad control packet refused: %s\n", msg);
  1036.     return(yesno(msg[0], 0, 0));
  1037. }
  1038.  
  1039. /*
  1040.  *  Do one piece of work as master.
  1041.  *
  1042.  *  FIXME:  we don't handle the flags, e.g. -c, properly!
  1043.  *
  1044.  *  Now only dequeues queue file if all transfers were successful.
  1045.  */
  1046.  
  1047. int
  1048. do_one_master()
  1049. {
  1050.     FILE    *fd;
  1051.     char    *sname;
  1052.     static char cmnd[256];        /* Command character */
  1053.     static char buf[256];
  1054.     int     fail = SUCCESS;
  1055.     int     failAction = SUCCESS;
  1056.     int     failaccum = 0;
  1057.     int     num;
  1058.     int     delmeflag;
  1059.     static char notify[NAMESIZE];   /* A bit large...FIXME */
  1060.     char    *delList[16];        /* delete files list   */
  1061.     short   di = 0;
  1062.  
  1063.     /*
  1064.      *    Get the next work item.  If no work left re-scan the directory
  1065.      *    just to be sure, and if still no work then do the right thing.
  1066.      */
  1067.  
  1068.     sname = work_next();
  1069.     if (!sname && NoReScan == 0) {
  1070.     if (work_scan(host_name))
  1071.         sname = work_next();
  1072.     }
  1073.     if (!sname) {
  1074.     /* No more work, time to hang up. */
  1075.     if (wrmsg("H") != SUCCESS)
  1076.         return FAIL;
  1077.     return HANGUP;
  1078.     }
  1079.  
  1080.     DEBUG(2, "Request file %s\n", sname);
  1081.  
  1082.     LockFile(sname);
  1083.  
  1084.     fd = fopen(sname, "r");
  1085.     if (fd == NULL) {
  1086.     UnLockFile(sname);
  1087.     DEBUG(0, "uucico: couldn't open %s\n", sname);
  1088.     return SUCCESS;
  1089.     }
  1090.  
  1091.     while (fgets(buf, sizeof(buf), fd)) {
  1092.     DEBUG(3, "Queued request: %s", buf);
  1093.  
  1094.     if (buf[1] != ' ')
  1095.         goto badnum;
  1096.  
  1097.     num = sscanf(buf, "%s %s %s %s %s %s %o\n",
  1098.         cmnd, srcnam, dstnam, who, flags, temp, &mode, notify
  1099.     );
  1100.  
  1101.     switch (cmnd[0]) {
  1102.     case 'S':
  1103.         if (num < 7 || num > 8)
  1104.         goto badnum;
  1105.         fail = local_send_file(buf, &delmeflag);
  1106.         if (delmeflag) {
  1107.         if (di == sizeof(delList)/sizeof(delList[0])) {
  1108.             ulog(-1, "Too many source files in Cmd file! %s", sname);
  1109.         } else {
  1110.             delList[di] = malloc(strlen(temp) + 1);
  1111.             strcpy(delList[di], temp);
  1112.             ++di;
  1113.         }
  1114.         }
  1115.         break;
  1116.     case 'R':
  1117.         if (num != 5) {
  1118.         if (debug > 7)
  1119.             printf("Invalid scanf %d/5 :%s:%s:%s\n", num, cmnd, srcnam, dstnam);
  1120.         goto badnum;
  1121.         }
  1122.         fail = local_receive_file();
  1123.         break;
  1124.     default:
  1125.     badnum:
  1126.         ulog(-1, "Illegal Work Request (%s): %s", sname, buf);
  1127.         fail = REFUSED;
  1128.         break;
  1129.     }
  1130.  
  1131.     switch(fail) {
  1132.     case SUCCESS:
  1133.         break;
  1134.     case FAIL:
  1135.         ++failaccum;
  1136.         if (failAction == SUCCESS)
  1137.         failAction = FAIL;
  1138.         ulog(-1, "Protocol Failure at (%s): %s", sname, buf);
  1139.         NoReScan = 1;
  1140.         break;
  1141.     case REFUSED:
  1142.         ++failaccum;
  1143.         failAction = REFUSED;
  1144.         ulog(-1, "Work Refused (%s): %s", sname, buf);
  1145.         break;
  1146.     }
  1147.     }
  1148.     fclose(fd);
  1149.  
  1150.     switch(failAction) {
  1151.     case SUCCESS:
  1152.     while (di) {
  1153.         --di;
  1154.         remove(delList[di]);
  1155.         free(delList[di]);
  1156.     }
  1157.     fail = remove(sname);
  1158.     UnLockFile(sname);
  1159.     if (fail != 0) {
  1160.         ulog(-1, "Unable to remove work file %s", sname);
  1161.         DEBUG(0, "Can't remove, errno %d\n", errno);
  1162.     } else {
  1163.         DEBUG(4, "Removed work file %s\n", sname);
  1164.     }
  1165.     break;
  1166.     case FAIL:
  1167.     UnLockFile(sname);
  1168.     break;
  1169.     case REFUSED:
  1170.     UnLockFile(sname);
  1171.     strcpy(buf, sname);
  1172.     {
  1173.         short i;
  1174.         for (i = strlen(buf); i >= 0 && buf[i] != ':' && buf[i] != '/'; --i);
  1175.         ++i;
  1176.         if ((buf[i]|0x20) == 'c')
  1177.         buf[i] = 'E';
  1178.     }
  1179.     if (strcmp(sname, buf) == 0) {
  1180.         ulog(-1, "Removing %s", sname);
  1181.         remove(sname);
  1182.     } else {
  1183.         ulog(-1, "Renaming %s to %s", sname, buf);
  1184.         rename(sname, buf);
  1185.     }
  1186.     break;
  1187.     }
  1188.     return(SUCCESS);
  1189. }
  1190.  
  1191. /*
  1192.  *  Send a yes/no packet
  1193.  */
  1194.  
  1195. int
  1196. yesno(c, true, err)
  1197. char c;
  1198. int true;
  1199. int err;
  1200. {
  1201.     char buf[21];
  1202.  
  1203.     buf[0] = c;
  1204.     buf[1] = true? 'Y': 'N';
  1205.     buf[2] = 0;
  1206.     if (err && !true)
  1207.     sprintf(buf+2,"%d", err);
  1208.  
  1209.     return (wrmsg(buf));
  1210. }
  1211.  
  1212. /*
  1213.  *  SLAVE MODE, Master wishes to send a file to us
  1214.  *
  1215.  *  SECURITY:    If file is not in list of allowed directories
  1216.  *        disallow transfer.  UUSPOOL:   is always in the
  1217.  *        list.
  1218.  *
  1219.  *        If file is for UUSPOOL: (the current dir), disallow "C." files
  1220.  *        NOTE: success return and file redirected to T: as this can
  1221.  *        occur only if somebody purposefully is trying to break us.
  1222.  *
  1223.  *  Return 0 = success
  1224.  */
  1225.  
  1226. int
  1227. host_send_file(msg)
  1228. char  *msg;
  1229. {
  1230.     FILE *fddsk;            /* Disk file pointer */
  1231.     static char cmnd[256];        /* Command character */
  1232.     int r;
  1233.     int nor = 0;
  1234.  
  1235.     sscanf(msg,"%s %s %s %s %s %s %o",
  1236.         cmnd, srcnam, dstnam, who, flags, temp, &mode);
  1237.  
  1238.     ulog(-1, "REQUESTED %s", msg);
  1239.     munge_filename(dstnam, dstnam);           /* Translate to local name */
  1240.     strcpy (temp, TmpFileName(dstnam));       /* Create a handy temp file */
  1241.  
  1242.     if (SecurityDisallow(dstnam, 'w')) {
  1243.     ulog(-1, "REQUEST FAILED -- SECURITY");
  1244.     return(yesno('S', 0, 4));
  1245.     }
  1246.     if (SecurityDisallow(dstnam, 'c') > 0) {
  1247.     ulog(-1, "REQUEST FAILED -- SECURITY, REMOTE TRIED TO SEND");
  1248.     ulog(-1, "US A COMMAND FILE: %s, FILE COPIED TO T:Bad-Cmd", dstnam);
  1249.     strcpy(dstnam, "T:Bad-Cmd");
  1250.     nor = 1;
  1251.     }
  1252.  
  1253.     /* FIXME: deal with file modes now that we fopen. */
  1254.  
  1255.     LockFile(temp);
  1256.  
  1257.     fddsk = fopen(temp, "wb" /*, mode|0600 */);
  1258.     if (fddsk == NULL) {
  1259.     UnLockFile(temp);
  1260.     /* Can't open file -- send error response */
  1261.     if (debug > 0) {
  1262.         printf("Cannot open temp file %s (%s) for writing, errno=%d\n",
  1263.         temp,
  1264.         dstnam,
  1265.         errno
  1266.         );
  1267.     }
  1268.     ulog(-1, "REQUEST FAILED -- TEMP FILE");
  1269.     return (yesno('S', 0, 4));
  1270.     }
  1271.  
  1272.     /* FIXME: Are the above permissions right?? */
  1273.     /* FIXME: Should we create directories for the file? */
  1274.  
  1275.     if (yesno('S',1, 0) != SUCCESS) {
  1276.     fclose(fddsk);
  1277.     unlink(temp);
  1278.     UnLockFile(temp);
  1279.     return(FAIL);
  1280.     }
  1281.     r = receive_file(fddsk, temp, dstnam, srcnam, nor);
  1282.     UnLockFile(temp);
  1283.     return(r);
  1284. }
  1285.  
  1286. /*
  1287.  *  SLAVE MODE, Master wants us to send a file to it
  1288.  *
  1289.  *  SECURITY:    If file is not in list of allowed directories
  1290.  *        disallow transfer.  UUSPOOL:   is always in the
  1291.  *        list.
  1292.  *
  1293.  *  0 = sucess
  1294.  */
  1295.  
  1296. int
  1297. host_receive_file(msg)
  1298. char  *msg;
  1299. {
  1300.     FILE *fddsk;     /* Disk file descriptor */
  1301.     int x;
  1302.     static char cmnd[256];          /* Command character */
  1303.  
  1304.     ulog(-1, "REQUESTED %s", msg);
  1305.  
  1306.     sscanf(msg,"%s %s %s",cmnd,srcnam,dstnam);
  1307.     munge_filename(srcnam, temp);
  1308.  
  1309.     if (SecurityDisallow(temp, 'r')) {
  1310.     ulog(-1, "COPY FAILED -- SECURITY");
  1311.     return (yesno('S', 0, 4));
  1312.     }
  1313.  
  1314.     fddsk = fopen(temp, "rb");              /* Try to open the file */
  1315.     if (fddsk == NULL) {
  1316.     /* File didn't open, sigh. */
  1317.     if (debug > 0) {
  1318.         printf("Cannot open file %s (%s) for reading, errno=%d\n",
  1319.         temp, srcnam, errno
  1320.         );
  1321.     }
  1322.     ulog(-1, "DENIED CAN'T OPEN %s", temp);
  1323.     return(yesno('R', 0, 2));
  1324.     }
  1325.  
  1326.     if (yesno('R',1, 0) != SUCCESS) {
  1327.     fclose(fddsk);
  1328.     return(FAIL);
  1329.     }
  1330.  
  1331.     x = send_file(fddsk);
  1332.  
  1333.     switch (x) {
  1334.     default:
  1335.     return x;
  1336.     case COPYFAIL:
  1337.     /* We don't care if the copy failed, since the master
  1338.        asked for the file and knows the result. */
  1339.     return SUCCESS;
  1340.     }
  1341.     return 1;
  1342. }
  1343.  
  1344. /*
  1345.  *  MASTER MODE, We want to send a file.
  1346.  *
  1347.  *  Return FAIL, SUCCESS, or COPYFAIL.
  1348.  *
  1349.  *  SUCCESS is returned either if the file was not found locally (local
  1350.  *  error, and the queued transfer should be flushed) or if it was moved
  1351.  *  successfully.  COPYFAIL indicates that the queued transfer should be
  1352.  *  left queued, and later retried.  FIXME, there are several failure points
  1353.  *  in the transaction and we need finer control here.
  1354.  */
  1355.  
  1356. int
  1357. local_send_file(workstr, delmeflag)
  1358. char *workstr;
  1359. int *delmeflag;
  1360. {
  1361.     static char buf[MAXMSGLEN];    /* Used for both xmit and receive */
  1362.     FILE *fddsk;        /* Disk file descriptor */
  1363.     int res;            /* Result and file removal status */
  1364.  
  1365.     *delmeflag = 0;
  1366.  
  1367.     /* WHY are temp and srcnam switched?  FIXME!  And no notify? */
  1368.  
  1369.     sprintf(buf,"S %s %s %s %s %s 0%o %s",
  1370.     temp, dstnam, who, flags, srcnam, mode, who
  1371.     );
  1372.  
  1373.     ulog(-1, "REQUEST %s", buf);
  1374.  
  1375.     if (strchr(flags, 'c')) {
  1376.     munge_filename(srcnam, temp);
  1377.     } else {
  1378.     munge_filename(temp, temp);
  1379.     }
  1380.     LockFile(temp);
  1381.     fddsk = fopen(temp, "rb");
  1382.     if (fddsk == NULL) {
  1383.     UnLockFile(temp);
  1384.     /* FIXME -- handle queued request for nonexistent file */
  1385.     if (debug > 0)
  1386.         printf("Can't open file %s (%s), errno=%d\n",
  1387.         temp,
  1388.         srcnam,
  1389.         errno
  1390.         );
  1391.     ulog(-1, "NOT FOUND %s", temp);
  1392.     /* return COPYFAIL;*/
  1393.     return SUCCESS;     /*    assume file previously sent */
  1394.     }
  1395.  
  1396.     /* Tell the other side we want to send this file */
  1397.  
  1398.     if (wrmsg(buf) != SUCCESS) {
  1399.     fclose(fddsk);
  1400.     UnLockFile(temp);
  1401.     return (FAIL);
  1402.     }
  1403.  
  1404.     /* See what they have to say about it */
  1405.  
  1406.     if (rdmsg(buf, MAXMSGLEN) != SUCCESS) {
  1407.     fclose(fddsk);
  1408.     UnLockFile(temp);
  1409.     return FAIL;
  1410.     }
  1411.     if ((buf[0] != 'S') || (buf[1] != 'Y')) {
  1412.     ulog(-1, "REQUEST DENIED %s", buf);
  1413.     fclose(fddsk);
  1414.     UnLockFile(temp);
  1415.     return(REFUSED);
  1416.     }
  1417.     res = send_file(fddsk); /* FAIL, SUCCESS, or COPYFAIL */
  1418.  
  1419.     /* Delete the source file if it was just a copy */
  1420.  
  1421.     if (res != SUCCESS) {
  1422.     UnLockFile(temp);
  1423.     return res;
  1424.     }
  1425.     if (strchr(flags, 'c')) {   /* If copied direct from source */
  1426.     UnLockFile(temp);
  1427.     return res;        /* ...just return. */
  1428.     }
  1429.     *delmeflag = 1;
  1430.     UnLockFile(temp);
  1431.  
  1432.     return res;
  1433. }
  1434.  
  1435. /*
  1436.  *  MASTER MODE, We wish to receive a specific file so we ask for it
  1437.  *
  1438.  *  Return 0 = success
  1439.  */
  1440.  
  1441. int
  1442. local_receive_file()
  1443. {
  1444.     static char buf[MAXMSGLEN];
  1445.     FILE *fddsk;            /* Disk file pointer */
  1446.     int r;
  1447.  
  1448.     /* FIXME, test dest file access before we ask for it. */
  1449.  
  1450.     sprintf(buf,"R %s %s %s %s %s 0%o %s",
  1451.     srcnam, dstnam, who, flags, temp, mode, who
  1452.     );
  1453.  
  1454.     munge_filename(dstnam, dstnam);           /* tlate to local name      */
  1455.     strcpy (temp, TmpFileName(dstnam));       /* Create a handy temp file */
  1456.  
  1457.     /* FIXME: deal with file modes now that we fopen. */
  1458.     /* FIXME: Are the above permissions right?? */
  1459.     /* FIXME: Should we create directories for the file? */
  1460.  
  1461.     LockFile(temp);
  1462.     fddsk = fopen(temp, "wb" /*, mode|060 */);
  1463.  
  1464.     if (fddsk == NULL) {
  1465.     UnLockFile(temp);
  1466.     /* Can't open temp file -- send error response */
  1467.     if (debug > 0) {
  1468.         printf("Cannot open temp file %s (%s) for writing, errno=%d\n",
  1469.         temp,
  1470.         dstnam,
  1471.         errno
  1472.         );
  1473.     }
  1474.     ulog(-1, "REQUEST FAILED -- TEMPFILE");
  1475.     return FAIL;
  1476.     }
  1477.  
  1478.     ulog(-1, "REQUEST %s", buf);
  1479.     if (wrmsg(buf) != SUCCESS) {
  1480.     fclose(fddsk);
  1481.     UnLockFile(temp);
  1482.     printf("uucico: problem sending request\n");
  1483.     return FAIL;
  1484.     }
  1485.  
  1486.     /* See what the other side has to say about it */
  1487.  
  1488.     if (rdmsg(buf, MAXMSGLEN) != SUCCESS) {
  1489.     fclose(fddsk);
  1490.     UnLockFile(temp);
  1491.     return FAIL;
  1492.     }
  1493.     if ((buf[0] != 'R') || (buf[1] != 'Y')) {
  1494.     ulog(-1, "REQUEST DENIED %s", buf);
  1495.     fclose(fddsk);
  1496.     UnLockFile(temp);
  1497.     return (REFUSED);
  1498.     }
  1499.  
  1500.     r = receive_file(fddsk, temp, dstnam, srcnam, 0);
  1501.     UnLockFile(temp);
  1502.     return(r);
  1503. }
  1504.  
  1505. /*
  1506.  *  General receive file
  1507.  */
  1508.  
  1509. int
  1510. receive_file(fddsk, temp, dstnam, srcnam, norename)
  1511. FILE *fddsk;
  1512. char    *temp, *dstnam, *srcnam;
  1513. {
  1514.     int status;
  1515.     int error = 0;            /* No errors so far */
  1516.  
  1517.     if (rddata(fddsk) != SUCCESS)
  1518.     error++;
  1519.     status = fclose(fddsk);         /* Make sure the data got here */
  1520.     if (status != 0) {
  1521.     error++;
  1522.     DEBUG(0, "fclose errno=%d\n", errno);
  1523.     }
  1524.  
  1525.     /*
  1526.      *    Move the file from its temp location to its real location,
  1527.      *    This needs to be able to copy a file if a simple rename
  1528.      *    does not suffice.  Should create directories if necesary.
  1529.      *    should use source ]name if target is a directory (i.e. no
  1530.      *    target source name
  1531.      */
  1532.  
  1533.     unlink(dstnam);
  1534.  
  1535.     if (norename)       /*  for security redirect   */
  1536.     status = 0;
  1537.     else
  1538.     status = rename(temp, dstnam);
  1539.  
  1540.     if (status != 0) {
  1541.     error++;
  1542.     if (debug > 0) {
  1543.         printf("Cannot rename file %s to %s, errno=%d\n",
  1544.         temp, dstnam, errno);
  1545.     }
  1546.     }
  1547.  
  1548.     ulog(-1, "COPY %s", error ? "FAILED": "SUCCEEDED");
  1549.  
  1550.     return(yesno('C', error == 0, 5));
  1551. }
  1552.  
  1553. /*
  1554.  * general file send routine
  1555.  * Return SUCCESS, FAIL, or COPYFAIL.
  1556.  */
  1557.  
  1558. int
  1559. send_file(fddsk)
  1560. FILE *fddsk;     /* Disk file pointer */
  1561. {
  1562.     static char ansbuf[MAXMSGLEN];
  1563.  
  1564.     if (wrdata(fddsk) != SUCCESS) {
  1565.     fclose(fddsk);
  1566.     return COPYFAIL;
  1567.     }
  1568.     fclose(fddsk);
  1569.  
  1570.     /* Await the "CY" or "CNddd" packet, and toss it. */
  1571.  
  1572.     while (1) {
  1573.     if (rdmsg(ansbuf, MAXMSGLEN) != SUCCESS)
  1574.         return COPYFAIL;
  1575.     if (ansbuf[0] != 'C') {
  1576.         DEBUG(0,"\nDidn't get 'CY' or 'CN', got %s\n", ansbuf);
  1577.         /* and loop looking for C message */
  1578.     } else if (ansbuf[1] == 'Y') {
  1579.         ulog(-1, "REQUESTED %s", ansbuf);
  1580.         return SUCCESS;
  1581.     } else {
  1582.         ulog(-1, "COPY FAILED %s", ansbuf);
  1583.         return COPYFAIL;
  1584.     }
  1585.     }
  1586.     return COPYFAIL;
  1587. }
  1588.  
  1589.