home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / u / uucico_s.lha / uucico.c < prev    next >
C/C++ Source or Header  |  1992-12-20  |  47KB  |  2,010 lines

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