home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff294.lzh / DNet / amiga / server / sgcopy.c < prev    next >
C/C++ Source or Header  |  1989-12-11  |  8KB  |  400 lines

  1.  
  2. /*
  3.  *  SGCOPY.C     V1.1
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  GET-COPY SERVER    (NEW COPY SERVER)
  8.  *
  9.  *  The current version only accepts one connection at a time.    This server
  10.  *  will send requested files to the remote machine.
  11.  *
  12.  *  length in 68000 longword format.
  13.  *
  14.  *  ACCESS RESTRICTIONS:
  15.  *
  16.  *    Remote access restrictions are according to DNET_GROUP or DNET_READ
  17.  *
  18.  *    DNET_READ >= 9        Full Access
  19.  *    DNET_READ <  9        According to comment field (AC=n) or group acc.
  20.  *                (GR=n) matches DNET_GROUP)
  21.  *
  22.  *    When searching for access rights, the comment field of the file/dir
  23.  *    requested is checked first.  If empty, the parent dir is checked.
  24.  *    This continues (parent diring) until root is found or a comment
  25.  *    field with access info in it.
  26.  */
  27.  
  28. #include "defs.h"
  29.  
  30. typedef struct {
  31.     char    Cmd;
  32.     char    Str[64];
  33.     long    Val;
  34. } HDR;
  35.  
  36. extern int Enable_Abort;
  37. char Buf[8192];
  38. void *Chan;
  39.  
  40. void SGCopy ARGS((void));
  41. int PutObject ARGS((char *));
  42. int PutDir ARGS((char *, int));
  43. int PutFile ARGS((char *, int));
  44. int WriteHeader ARGS((char, char *, long));
  45. int ReadHeader ARGS((HDR *));
  46.  
  47. int
  48. brk()
  49. {
  50.     return(0);
  51. }
  52.  
  53. void
  54. #ifdef LATTICE
  55. _main(str)
  56. #else
  57. _main(len,str)
  58. #endif
  59. char *str;
  60. {
  61.     struct MsgPort *port;
  62.     PROC    *myproc = (PROC *)FindTask(NULL);
  63.     long    savedir;
  64.     long    mask, rmask;
  65.  
  66.     onbreak(brk);
  67.  
  68.     if (strncmp(str, "__dnet", 6) != 0) {
  69.     Version("SGCopy", VERSION, SGCOPY_VERSION);
  70.     _exit(0);
  71.     }
  72.  
  73.     port = DListen(PORT_GFILECOPY);
  74.     WaitPort(&myproc->pr_MsgPort);
  75.     ReplyMsg(GetMsg(&myproc->pr_MsgPort));
  76.     savedir = Lock("", SHARED_LOCK);   /* duplicate current dir    */
  77.     if (!savedir) {
  78.     DUnListen(port);
  79.     _exit(1);
  80.     }
  81.     savedir = CurrentDir(savedir);              /* CD dup, returns original */
  82.     mask = SIGBREAKF_CTRL_C|(1 << port->mp_SigBit);
  83.     for (;;) {
  84.     long dupdir = DupLock(myproc->pr_CurrentDir);
  85.     rmask = Wait(mask);
  86.     if (rmask & SIGBREAKF_CTRL_C) {
  87.         UnLock(CurrentDir(dupdir));
  88.         break;
  89.     }
  90.     while (Chan = DAccept(port)) {
  91.         SGCopy();
  92.         DClose(Chan);
  93.     }
  94.     UnLock(CurrentDir(dupdir));
  95.     }
  96.     UnLock(CurrentDir(savedir));                /* restore original         */
  97.     DUnListen(port);
  98. }
  99.  
  100. void
  101. SGCopy()
  102. {
  103.     short error;
  104.     static HDR Hdr;
  105.  
  106.     error = WriteHeader('H', "Hello, GCopy server V1.30", 0);
  107.     if (error)
  108.     return;
  109.     switch(ReadHeader(&Hdr)) {
  110.     case -1:
  111.     return;
  112.     case 'H':
  113.     break;
  114.     }
  115.     while (!error) {
  116.     switch(ReadHeader(&Hdr)) {
  117.     case 'G':
  118.         error = PutObject(Hdr.Str);
  119.         break;
  120.     case 'E':
  121.         goto done;
  122.     case 'P':   /*  put-files, not implemented  */
  123.     default:
  124.         error = 1;
  125.         break;
  126.     }
  127.     }
  128. done:
  129.     ;
  130. }
  131.  
  132. int
  133. PutObject(str)
  134. char *str;
  135. {
  136.     long lock;
  137.     FIB *fib = malloc(sizeof(FIB));
  138.     short error = 0;
  139.     short access = GetEnvVal(DNET_READ);
  140.     short group  = GetEnvVal(DNET_GROUP);
  141.  
  142.     mountrequest(0);
  143.     lock = Lock(str, SHARED_LOCK);
  144.     mountrequest(1);
  145.     if (lock == NULL || !Examine(lock, fib)) {     /*  unable to find it!  */
  146.     error = WriteHeader('N', "Unable to find object", 0);
  147.     goto done;
  148.     }
  149.  
  150.     /*
  151.      *    Determine Access rights
  152.      *
  153.      *    If the file/dir or any parent dir has AC > your_ac, access is
  154.      *    denied.  If the file/dir and all parent dirs have neither an ACcess
  155.      *    or GRoup entry, access is denied.  If a group entry is found that
  156.      *    matches your group, access is allowed (unless an AC is found > your_ac)
  157.      *
  158.      *    If your_ac is >= 9, no access checking of parent directories is
  159.      *    done at all.  However, access checking of downloaded files will
  160.      *    be done, and any AC values > your_ac will be disalloweds.
  161.      */
  162.  
  163.     if (access < 9) {           /*  must check comment/access   */
  164.     short ok = 0;
  165.     long lock2 = DupLock(lock);
  166.     long tmp;
  167.     for (;;) {
  168.         short ac = 0;
  169.  
  170.         while (ac >= 0) {       /*  check groups    */
  171.         short idx = 0;
  172.         ac = ExtractFieldVal(fib->fib_Comment, FS_GROUP, &idx);
  173.         if (ac >= 0 && ac == group) {
  174.             ok = 1;
  175.             break;
  176.         }
  177.         }
  178.         ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  179.         if (ac >= 0) {                  /*  valid access field      */
  180.         if (ac <= access) {         /*  access ok               */
  181.             ok = 1;
  182.         } else {            /*    access not ok        */
  183.             ok = 0;
  184.             break;
  185.         }
  186.         }
  187.         if (tmp = ParentDir(lock2)) {   /*  check the par.dir */
  188.         UnLock(lock2);
  189.         lock2 = tmp;
  190.         fib->fib_Comment[0] = 0;
  191.         Examine(lock2, fib);
  192.         continue;
  193.         }
  194.         break;
  195.     }
  196.     UnLock(lock2);
  197.     if (!ok) {
  198.         error = WriteHeader('N', "Access Violation", 0);
  199.         goto done;
  200.     }
  201.     }
  202.     Examine(lock, fib);
  203.     UnLock(lock);
  204.     lock = NULL;
  205.     if (fib->fib_DirEntryType > 0) {
  206.     error = PutDir(str, access);
  207.     } else {
  208.     error = PutFile(str, access);
  209.     }
  210. done:
  211.     if (lock)
  212.     UnLock(lock);
  213.     free(fib);
  214.     return((int)error);
  215. }
  216.  
  217. int
  218. PutDir(name, access)
  219. char *name;
  220. int access;
  221. {
  222.     long lock = Lock(name, SHARED_LOCK);
  223.     FIB *fib = malloc(sizeof(FIB));
  224.     static HDR Hdr;
  225.     short error = 0;
  226.     short ac;
  227.  
  228.     if (!lock || !Examine(lock, fib)) {
  229.     WriteHeader('N', "Possible Disk Error", 0);
  230.     error = 1;
  231.     goto done;
  232.     }
  233.  
  234.     ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  235.     if (ac >= 0 && ac > access) {
  236.     error = WriteHeader('S', fib->fib_FileName, 0);
  237.     goto done;
  238.     }
  239.  
  240.     if (error = WriteHeader('D', fib->fib_FileName, 0))
  241.     goto done;
  242.     switch(ReadHeader(&Hdr)) {
  243.     case 'Y':
  244.     break;
  245.     case 'S':
  246.     goto done;
  247.     case 'N':
  248.     error = 1;
  249.     break;
  250.     default:
  251.     error = 1;
  252.     break;
  253.     }
  254.     if (error)
  255.     goto done;
  256.     while (ExNext(lock, fib)) {     /*  try to give him the files   */
  257.     if (fib->fib_DirEntryType > 0) {
  258.         long oldlock = CurrentDir(lock);
  259.         error = PutDir(fib->fib_FileName, access);
  260.         CurrentDir(oldlock);
  261.         if (error)
  262.         break;
  263.     } else {
  264.         long oldlock = CurrentDir(lock);
  265.         error = PutFile(fib->fib_FileName, access);
  266.         CurrentDir(oldlock);
  267.         if (error)
  268.         break;
  269.     }
  270.     }
  271.     if (!error)
  272.     WriteHeader('E', "Possible Disk Error", 0);
  273. done:
  274.     free(fib);
  275.     if (lock)
  276.     UnLock(lock);
  277.     return((int)error);
  278. }
  279.  
  280. int
  281. PutFile(name, access)
  282. char *name;
  283. int access;
  284. {
  285.     long lock = Lock(name, SHARED_LOCK);
  286.     long fh = NULL;
  287.     FIB *fib = malloc(sizeof(FIB));
  288.     static HDR Hdr;
  289.     long len;
  290.     short error = 0;
  291.     short ac;
  292.  
  293.     if (!lock || !Examine(lock, fib)) {
  294.     WriteHeader('N', "Possible Disk Error", 0);
  295.     error = 1;
  296.     goto done;
  297.     }
  298.  
  299.     ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  300.     if (ac >= 0 && ac > access) {
  301.     error = WriteHeader('S', fib->fib_FileName, 0);
  302.     goto done;
  303.     }
  304.  
  305.     fh = Open(name, 1005);
  306.     if (fh == NULL)         /*  don't do anything if unable to open it */
  307.     goto done;
  308.     Seek(fh, 0L, 1);
  309.     len = Seek(fh, 0L, 0);
  310.     if (error = WriteHeader('F', fib->fib_FileName, len))
  311.     goto done;
  312.     switch(ReadHeader(&Hdr)) {
  313.     case 'Y':
  314.     Seek(fh, Hdr.Val, -1);  /*  start pos.  */
  315.     len -= Hdr.Val;
  316.     if (len < 0)
  317.         len = 0;
  318.     break;
  319.     case 'S':
  320.     goto done;
  321.     case 'N':
  322.     error = 1;
  323.     break;
  324.     default:
  325.     error = 1;
  326.     break;
  327.     }
  328.     if (error)
  329.     goto done;
  330.     while (len) {
  331.     long n = (len > sizeof(Buf)) ? sizeof(Buf) : len;
  332.  
  333.     if (Read(fh, Buf, n) != n) {    /*  read failed! */
  334.         error = 10;
  335.         goto done;
  336.     }
  337.     if (DWrite(Chan, Buf, n) != n) {
  338.         error = 10;
  339.         goto done;
  340.     }
  341.     len -= n;
  342.     }
  343. done:
  344.     free(fib);
  345.     if (fh)
  346.     Close(fh);
  347.     if (lock)
  348.     UnLock(lock);
  349.     return((int)error);
  350. }
  351.  
  352. int
  353. WriteHeader(c, str, len)
  354. char c;
  355. char *str;
  356. long len;
  357. {
  358.     ubyte sl;
  359.  
  360.     if (str == NULL)
  361.     str = "";
  362.     sl = strlen(str);
  363.  
  364.     if (DWrite(Chan, &c, 1) < 0)
  365.     return(1);
  366.     if (DWrite(Chan, &sl,1) < 0)
  367.     return(1);
  368.     if (DWrite(Chan, str, sl) != sl)
  369.     return(1);
  370.     if (DWrite(Chan, &len, 4) != 4)
  371.     return(1);
  372.     return(0);
  373. }
  374.  
  375. int
  376. ReadHeader(hdr)
  377. HDR *hdr;
  378. {
  379.     ubyte sl;
  380.     ubyte cmd;
  381.  
  382.     hdr->Cmd = -1;
  383.     if (DRead(Chan, &cmd, 1) != 1)
  384.     return(-1);
  385.     if (DRead(Chan, &sl, 1) != 1)
  386.     return(-1);
  387.     if (sl >= sizeof(hdr->Str)) {
  388.     return(-1);
  389.     }
  390.     if (DRead(Chan, hdr->Str, sl) != sl)
  391.     return(-1);
  392.     hdr->Str[sl] = 0;
  393.     if (DRead(Chan, &hdr->Val, 4) != 4)
  394.     return(-1);
  395.     hdr->Cmd = cmd;
  396.     return((int)hdr->Cmd);
  397. }
  398.  
  399.  
  400.