home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / ftp / BFTP.312 / bftp_req.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-29  |  11.3 KB  |  446 lines

  1.  
  2. /************************************************************************
  3.  *                                    *
  4.  *     Background File Transfer Program (BFTP)                *
  5.  *                                    *
  6.  *    Written at USC/Information Sciences Institute            *
  7.  *    September, 1988                            *
  8.  *                                    *
  9.  *      BFTP is Public Domain, and may be used for any purpose as    *
  10.  *      long as this notice is not removed.  USC-ISI does not assume    *
  11.  *    any responsibility for the correctness, performance, or use    *
  12.  *    of this software.                        *
  13.  *                                    *
  14.  ************************************************************************/
  15. /*
  16.  *    bftp_req.c
  17.  *
  18.  *    These routines are used in "bftp" and "bftptool", user interface
  19.  *    modules, and also in "fts", the file transfer driver.  Included
  20.  *    are routines which read and write a standard BFTP request file.
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <strings.h>
  26. #include <sys/file.h>
  27. #include <sys/types.h>
  28. #include <sys/timeb.h>
  29. #include "time.h"
  30.       
  31. #include "bftp.h"
  32.  
  33. #define endof(x) (x + strlen(x))
  34.  
  35. char *expand_stru(c)
  36.    char c;
  37. {
  38.    return( (c == 'F')? "file":
  39.           (c == 'R')? "record":
  40.        (c == 'P')? "page":
  41.                   "unknown");
  42. }
  43. char *expand_mode(c)
  44.    char c;
  45. {
  46.    return( (c == 'S')? "stream":
  47.           (c == 'B')? "block":
  48.        (c == 'C')? "compress":
  49.                   "unknown");
  50. }
  51. char *expand_type(c)
  52.    char c;
  53. {
  54.    return( (c == 'A')? "ascii":
  55.           (c == 'E')? "ebcdic":
  56.        (c == 'I')? "image":
  57.        (c == 'L')? "local":
  58.                   "unknown");
  59. }
  60. char *expand_form(c)
  61.    char c;
  62. {
  63.    return( (c == 'N')? "nonprint":
  64.           (c == 'T')? "telnet":
  65.        (c == 'C')? "carriage-control":
  66.                   "unknown");
  67. }
  68.  
  69. boolean
  70. get_id(id, filename)
  71.    char *id, *filename;
  72. {
  73.    char *temp, *dot;
  74.  
  75.    if ((temp = rindex(filename, '/')) == NULL)
  76.       temp = filename;
  77.    else 
  78.       temp++;
  79.    if (strlen(temp) == 0)
  80.       return(FALSE);
  81.    else {
  82.       strcpy(id, temp);
  83.       if ((dot = rindex(temp, '.')) != NULL) {
  84.          dot = rindex(id, '.');
  85.      *dot = '\0';
  86.      }
  87.       return(TRUE);
  88.       }
  89. } /* get_id */
  90.  
  91. void
  92. name_dotfile(file, dotfile)
  93.    char *file, *dotfile;
  94. {
  95.    int len;
  96.    char *cp;
  97.  
  98.    if (strlen(file))
  99.       if ((cp = rindex(file, '/')) != NULL) {
  100.      len = strlen(file) - strlen(cp) + 1;
  101.      strncpy(dotfile, file, len);
  102.      dotfile[len] = '\0';
  103.      strcat(dotfile, ".");
  104.      strcat(dotfile, ++cp);
  105.          }
  106.       else {
  107.      strcpy(dotfile, ".");
  108.      strcat(dotfile, file);
  109.          }
  110.    else
  111.       dotfile[0] = '\0';
  112.  
  113. } /* name_dotfile */
  114.  
  115. int
  116. find_job(reqfile)
  117.    char *reqfile;
  118. {
  119.    FILE *stream;
  120.    char temp[80];
  121.    int jobno = 0;
  122.    
  123.    strcpy(temp, "atq | grep ");
  124.    if (get_id(endof(temp), reqfile))
  125.       if (stream = popen(temp,"r")) {
  126.          if (fgets(temp,sizeof(temp),stream))
  127.         sscanf(temp, "%*s %*s %*d, %*d %*d:%*d %*s %d",&jobno);
  128.      pclose(stream);
  129.          }
  130.    return(jobno);
  131. } /* find_job */
  132.  
  133. void
  134. cancel_job(jobno)
  135.    int jobno;
  136. {
  137.    char temp[80];
  138.  
  139.    if (jobno) {
  140.       sprintf(temp, "atrm %d 1> /dev/null 2>&1",jobno);
  141.       system(temp);
  142.       }
  143. }
  144.  
  145. boolean
  146. print_req(out, req,src,dst,file, verbose)
  147.    FILE *out;
  148.    struct reqinfo *req;
  149.    struct hostinfo *src, *dst;
  150.    struct fileinfo *file;
  151.    int verbose;
  152. {
  153.    char *cp;
  154.  
  155.    fprintf(out,"    Request type: %s\n",
  156.         (file->reqtype==COPY)?"COPY":
  157.         (file->reqtype==MOVE)?"MOVE":
  158.         (file->reqtype==DFILE)?"DFILE":
  159.         (file->reqtype==VERIFY)?"VERIFY":
  160.         (file->reqtype==VERIFY_SRC)?"VERIFY_SRC":
  161.         "unknown");
  162.    if (verbose) {
  163.       fprintf(out,"\n");
  164.       fprintf(out,"    Source --\n");
  165.       fprintf(out,"\tHost: '%s'\n", src->host);
  166.       fprintf(out,"\tUser: '%s'\n", src->user);
  167.       fprintf(out,"\tPass: %s\n", (strlen(src->passwd)==0) ? "NOT SET": "SET");
  168.       fprintf(out,"\tAcct: '%s'\n", src->acct);
  169.       fprintf(out,"\tDir: '%s'\n", src->dir);
  170.       fprintf(out,"\tFile: '%s'\n", src->file);
  171.       fprintf(out,"\tPort: %d\n", src->port);
  172.     if ((file->reqtype != VERIFY_SRC) && (file->reqtype != DFILE)) {
  173.     fprintf(out,"\n");
  174.     fprintf(out,"    Destination --\n");
  175.     fprintf(out,"\tHost: '%s'\n", dst->host);
  176.     fprintf(out,"\tUser: '%s'\n", dst->user); 
  177.     fprintf(out,"\tPass: %s\n",(strlen(dst->passwd)==0) ? "NOT SET": "SET");
  178.  
  179.     fprintf(out,"\tAcct: '%s'\n", dst->acct);
  180.     fprintf(out,"\tDir: '%s'\n", dst->dir);
  181.     if (! file->multflag)
  182.        fprintf(out,"\tFile:'%s'\n", dst->file);
  183.     fprintf(out,"\tPort: %d\n", dst->port);
  184.     };
  185.     fprintf(out,"\n");
  186.     if ((file->reqtype != DFILE) && (file->reqtype != VERIFY_SRC)) {
  187.        if (file->creation != STOR)
  188.           fprintf(out,"    Creation: %s\n",
  189.             (file->creation == APPE) ? "appending to existing file":
  190.         "using STOU command");
  191.        fprintf(out,"    %s%s", "Structure: ", expand_stru(file->stru));
  192.        cp = file->filetype;
  193.        fprintf(out,"%s%s%s%s", ", Mode: ", expand_mode(file->mode), 
  194.                    ", Type: ", expand_type(*cp));
  195.        switch (*cp) {
  196.              case 'L':
  197.             cp = cp + 2;
  198.         fprintf(out,"%s%s",", Byte size: ", cp);
  199.         break;
  200.          case 'A':
  201.          case 'E':
  202.             cp = cp + 2;
  203.         fprintf(out,"%s%s",", Format: ", expand_form(*cp));
  204.         break;
  205.          }
  206.        fprintf(out,"\n");
  207.        };
  208.       }
  209.    else { /* not verbose */
  210.       fprintf(out,"    Source: %s,%s,XXX,%s,%d,%s,%s\n",
  211.           src->host, src->user, src->acct, src->port, src->dir, src->file);
  212.       if ((file->reqtype != VERIFY_SRC) && (file->reqtype != DFILE)) {
  213.      fprintf(out,"    Destination: %s,%s,XXX,%s,%d,%s,%s\n",
  214.        dst->host, dst->user, dst->acct, dst->port, dst->dir, dst->file);
  215.      fprintf(out,"    Stru: %c",file->stru);
  216.      fprintf(out,", Mode: %c, Type: %s", file->mode, file->filetype);
  217.      fprintf(out,", Creation: %s\n",
  218.             (file->creation==APPE)?"APPE":
  219.             (file->creation==STOU)?"STOU":
  220.             "STOR");
  221.      }
  222.       }
  223.     
  224.    fprintf(out,"    Multiple matching: %s", 
  225.                (file->multflag)?"TRUE": "FALSE");
  226.    if ((file->reqtype != VERIFY) && (file->reqtype != VERIFY_SRC)) {
  227.       fprintf(out, ", Return mailbox: '%s'\n", req->mailbox);
  228.      fprintf(out,"    Remaining tries: %d, Current retry interval: %d minutes",
  229.               req->ntries, req->interval);
  230.       }
  231.    fprintf(out,"\n\n");
  232.  
  233. } /* print_req */
  234.     
  235.  
  236. void
  237. format_time(t, tstr)
  238.    time_t t;
  239.    char *tstr;
  240. {
  241.    struct timeb tp;
  242.    struct tm *tmP;
  243.  
  244.    ftime(&tp);
  245.    tmP = localtime((t)? &t: &tp.time);
  246.     
  247.    strcpy(tstr, asctime(tmP) + 4);
  248.    /* Note: the previous localtime() call has magical properties that 
  249.       make the upcoming timezone call work right! */
  250.    sprintf(tstr+strlen(tstr)-1, " %s", timezone(tp.timezone, tmP->tm_isdst));
  251. }
  252.  
  253. void
  254. queue_req(req, timeval, out)
  255.    struct reqinfo *req;
  256.    long timeval;
  257.    char *out;
  258. {
  259.    char temp[80],tmp1[30], tmp2[20], tmp3[20];
  260.  
  261. #ifdef SUNOS4
  262.    if (time(NULL) > (timeval-30))
  263.       sprintf(temp, "at -s now + 1 minute %s 1> /dev/null 2>&1", req->cmdfile);
  264.    else
  265. #endif   
  266.       {
  267.       strcpy(temp,ctime(&timeval));
  268.       temp[strlen(temp)-1] = 0;
  269.       sscanf(temp,"%*s%s%s%s",tmp1,tmp2,tmp3);
  270.          /* Tue Apr 28 14:20:18 1987  is example of format */
  271.       tmp3[strlen(tmp3)-3] = 0;
  272.       sprintf(temp, "at -s %s %s %s %s 1> /dev/null 2>&1", 
  273.               tmp3, tmp1, tmp2, req->cmdfile);
  274.       }
  275.    system(temp);
  276.  
  277.    if (out != NULL) {
  278.       get_id(temp, req->cmdfile);
  279.       format_time(timeval, tmp1);
  280.       sprintf(out, "Request %s submitted to run at %s.", temp, tmp1);
  281.       }
  282. }
  283.  
  284. void
  285. finish_req(reqfile, req, subject, multlist)
  286.    char *reqfile;
  287.    struct reqinfo *req;
  288.    char *subject, *multlist;
  289. {
  290.    char id[80], temp[200];
  291.    
  292.    /* send the results message */ 
  293.    if (strlen(req->mailbox)!=0 && strlen(req->mailfile)!=0) {
  294.       if (!subject)
  295.      get_id(id, reqfile);
  296.       sprintf(temp,"%s -s \"BFTP Results: %s\" %s < %s\n", 
  297.                  MAILPATH, (subject)?subject : id, 
  298.            req->mailbox,req->mailfile);
  299.       system(temp); 
  300.       }
  301.            
  302.    /* delete the request files */      
  303.    sprintf(temp,"rm %s %s %s %s 1> /dev/null 2>&1\n", 
  304.            req->mailfile, req->cmdfile, reqfile, multlist);
  305.  
  306.    system(temp);
  307. }
  308.  
  309. boolean
  310. read_req(filename,req,src,dst,file,multlist)
  311.    char *filename;
  312.    struct reqinfo *req;
  313.    struct hostinfo *src, *dst;
  314.    struct fileinfo *file;
  315.    char *multlist;   
  316. {
  317.    FILE *rfile;
  318.     
  319.    if ((rfile = fopen(filename,"r")) == NULL)
  320.       return(FALSE);
  321.       
  322.    bzero((char *)src, sizeof(struct hostinfo));
  323.    bzero((char *)dst, sizeof(struct hostinfo));
  324.    bzero((char *)req, sizeof(struct reqinfo));
  325.    bzero((char *)file, sizeof(struct fileinfo));
  326.  
  327.    if ((5 == fscanf(rfile,"%[^\n] %[^\n] %[^\n] %[^\n] %d ",
  328.          req->rpasswd, req->mailbox, req->mailfile, 
  329.         req->cmdfile, &file->reqtype)) &&
  330.        (7 == fscanf(rfile,"%[^\n] %[^\n] %[^\n] %[^\n] %hd %[^\n] %[^\n] ",
  331.            src->host, src->user, src->passwd,
  332.         src->acct, &src->port, src->dir, src->file)) &&
  333.        (7 == fscanf(rfile,"%[^\n] %[^\n] %[^\n] %[^\n] %hd %[^\n] %[^\n] ",
  334.            dst->host, dst->user, dst->passwd,
  335.         dst->acct, &dst->port, dst->dir, dst->file)) &&
  336.        (7 == fscanf(rfile,"%[^\n] %c %c %d %d %d %d ",
  337.            file->filetype, &file->stru, &file->mode, &file->creation,
  338.         &file->multflag, &req->ntries, &req->interval)))
  339.       {
  340.       if (file->multflag) {
  341.          if (fscanf(rfile,"%[^\n]",multlist) != 1)
  342.             multlist[0] = '\0';
  343.      }
  344.       else
  345.          multlist[0] = '\0';
  346.       }
  347.    fclose(rfile);
  348.    return(TRUE);
  349. }
  350.  
  351. void
  352. put_string(fptr,sptr)
  353.    FILE *fptr;
  354.    char *sptr;
  355. {
  356.    if (strlen(sptr))
  357.       fprintf(fptr,"%s\n", sptr);
  358.    else
  359.       fprintf(fptr,"%c\n", '\0');
  360. }
  361.  
  362. boolean
  363. write_req(filename,req,src,dst,file,multlist)
  364.    char *filename;
  365.    struct reqinfo *req;
  366.    struct hostinfo *src, *dst;
  367.    struct fileinfo *file;
  368.    char *multlist;
  369. {
  370.    FILE *rfile;
  371.    
  372.    if (!(rfile = fdopen(open(filename,(O_WRONLY|O_CREAT),0600),"w")))
  373. /*   if ((rfile = fopen(filename,"w")) == NULL) */
  374.       return(FALSE);
  375.       
  376.    put_string(rfile,req->rpasswd);
  377.    put_string(rfile,req->mailbox);
  378.    put_string(rfile,req->mailfile);
  379.    put_string(rfile,req->cmdfile);
  380.    fprintf(rfile, "%d\n",file->reqtype);
  381.    put_string(rfile, src->host);
  382.    put_string(rfile, src->user);
  383.    put_string(rfile, src->passwd);
  384.    put_string(rfile, src->acct);
  385.    fprintf(rfile, "%d\n",src->port);
  386.    put_string(rfile, src->dir);
  387.    put_string(rfile, src->file);
  388.    put_string(rfile, dst->host);
  389.    put_string(rfile, dst->user);
  390.    put_string(rfile, dst->passwd);
  391.    put_string(rfile, dst->acct);
  392.    fprintf(rfile, "%d\n",dst->port);
  393.    put_string(rfile, dst->dir);
  394.    put_string(rfile, (file->multflag)? "" : dst->file);
  395.    
  396.    put_string(rfile, file->filetype);
  397.    fprintf(rfile, "%c\n%c\n%d\n%d\n%d\n%d\n",
  398.         file->stru,file->mode,file->creation,file->multflag, 
  399.         req->ntries,req->interval);
  400.    put_string(rfile, (multlist)? multlist: "");
  401.    fclose(rfile);
  402.        
  403.    return(TRUE);
  404. }
  405.  
  406. void
  407. unpack_filetype(ftype, type, form, bsize)
  408.    char *ftype;
  409.    int *type, *form, *bsize;
  410. {
  411.    char *cp = NULL;
  412.    *type = (*ftype == 'A')? ASCII :
  413.          (*ftype == 'E')? EBCDIC :
  414.       (*ftype == 'I')? IMAGE :
  415.       (*ftype == 'L')? LOCAL : NOTYPE;
  416.      
  417.    switch (*type) {
  418.       case ASCII:
  419.       case EBCDIC:
  420.          if (strlen(ftype)==3) {
  421.         cp = ftype + 2;
  422.         *form = (*cp == 'N')? NONPRINT :
  423.                (*cp == 'C') ? CCNTRL :
  424.            (*cp == 'T')? TELNET : NOFORM;
  425.         }
  426.      else
  427.         *form = NOFORM;
  428.      *bsize = DEFAULT_BYTESZ;
  429.          break;
  430.       case IMAGE:
  431.       case NOTYPE:
  432.          *form = NONPRINT;
  433.      *bsize = DEFAULT_BYTESZ;
  434.          break;
  435.       case LOCAL:
  436.          if (strlen(ftype) > 2) {
  437.         cp = ftype + 2;
  438.         *bsize = atoi(cp);
  439.         }
  440.      else
  441.         *bsize = DEFAULT_BYTESZ;
  442.      *form = NONPRINT;
  443.          break;
  444.       }
  445. } /* unpack_filetype */
  446.