home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / dnet / dnet2.3.2 / amiga / server / sgcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  8.1 KB  |  405 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. void mountrequest(int foo){
  133.    /* I have no idea what this was supposed to originally do!  -ERic */
  134.    return;
  135. }
  136.  
  137. int
  138. PutObject(str)
  139. char *str;
  140. {
  141.     long lock;
  142.     FIB *fib = malloc(sizeof(FIB));
  143.     short error = 0;
  144.     short access = GetEnvVal(DNET_READ);
  145.     short group  = GetEnvVal(DNET_GROUP);
  146.  
  147.     mountrequest(0);
  148.     lock = Lock(str, SHARED_LOCK);
  149.     mountrequest(1);
  150.     if (lock == NULL || !Examine(lock, fib)) {     /*  unable to find it!  */
  151.     error = WriteHeader('N', "Unable to find object", 0);
  152.     goto done;
  153.     }
  154.  
  155.     /*
  156.      *    Determine Access rights
  157.      *
  158.      *    If the file/dir or any parent dir has AC > your_ac, access is
  159.      *    denied.  If the file/dir and all parent dirs have neither an ACcess
  160.      *    or GRoup entry, access is denied.  If a group entry is found that
  161.      *    matches your group, access is allowed (unless an AC is found > your_ac)
  162.      *
  163.      *    If your_ac is >= 9, no access checking of parent directories is
  164.      *    done at all.  However, access checking of downloaded files will
  165.      *    be done, and any AC values > your_ac will be disalloweds.
  166.      */
  167.  
  168.     if (access < 9) {           /*  must check comment/access   */
  169.     short ok = 0;
  170.     long lock2 = DupLock(lock);
  171.     long tmp;
  172.     for (;;) {
  173.         short ac = 0;
  174.  
  175.         while (ac >= 0) {       /*  check groups    */
  176.         short idx = 0;
  177.         ac = ExtractFieldVal(fib->fib_Comment, FS_GROUP, &idx);
  178.         if (ac >= 0 && ac == group) {
  179.             ok = 1;
  180.             break;
  181.         }
  182.         }
  183.         ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  184.         if (ac >= 0) {                  /*  valid access field      */
  185.         if (ac <= access) {         /*  access ok               */
  186.             ok = 1;
  187.         } else {            /*    access not ok        */
  188.             ok = 0;
  189.             break;
  190.         }
  191.         }
  192.         if (tmp = ParentDir(lock2)) {   /*  check the par.dir */
  193.         UnLock(lock2);
  194.         lock2 = tmp;
  195.         fib->fib_Comment[0] = 0;
  196.         Examine(lock2, fib);
  197.         continue;
  198.         }
  199.         break;
  200.     }
  201.     UnLock(lock2);
  202.     if (!ok) {
  203.         error = WriteHeader('N', "Access Violation", 0);
  204.         goto done;
  205.     }
  206.     }
  207.     Examine(lock, fib);
  208.     UnLock(lock);
  209.     lock = NULL;
  210.     if (fib->fib_DirEntryType > 0) {
  211.     error = PutDir(str, access);
  212.     } else {
  213.     error = PutFile(str, access);
  214.     }
  215. done:
  216.     if (lock)
  217.     UnLock(lock);
  218.     free(fib);
  219.     return((int)error);
  220. }
  221.  
  222. int
  223. PutDir(name, access)
  224. char *name;
  225. int access;
  226. {
  227.     long lock = Lock(name, SHARED_LOCK);
  228.     FIB *fib = malloc(sizeof(FIB));
  229.     static HDR Hdr;
  230.     short error = 0;
  231.     short ac;
  232.  
  233.     if (!lock || !Examine(lock, fib)) {
  234.     WriteHeader('N', "Possible Disk Error", 0);
  235.     error = 1;
  236.     goto done;
  237.     }
  238.  
  239.     ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  240.     if (ac >= 0 && ac > access) {
  241.     error = WriteHeader('S', fib->fib_FileName, 0);
  242.     goto done;
  243.     }
  244.  
  245.     if (error = WriteHeader('D', fib->fib_FileName, 0))
  246.     goto done;
  247.     switch(ReadHeader(&Hdr)) {
  248.     case 'Y':
  249.     break;
  250.     case 'S':
  251.     goto done;
  252.     case 'N':
  253.     error = 1;
  254.     break;
  255.     default:
  256.     error = 1;
  257.     break;
  258.     }
  259.     if (error)
  260.     goto done;
  261.     while (ExNext(lock, fib)) {     /*  try to give him the files   */
  262.     if (fib->fib_DirEntryType > 0) {
  263.         long oldlock = CurrentDir(lock);
  264.         error = PutDir(fib->fib_FileName, access);
  265.         CurrentDir(oldlock);
  266.         if (error)
  267.         break;
  268.     } else {
  269.         long oldlock = CurrentDir(lock);
  270.         error = PutFile(fib->fib_FileName, access);
  271.         CurrentDir(oldlock);
  272.         if (error)
  273.         break;
  274.     }
  275.     }
  276.     if (!error)
  277.     WriteHeader('E', "Possible Disk Error", 0);
  278. done:
  279.     free(fib);
  280.     if (lock)
  281.     UnLock(lock);
  282.     return((int)error);
  283. }
  284.  
  285. int
  286. PutFile(name, access)
  287. char *name;
  288. int access;
  289. {
  290.     long lock = Lock(name, SHARED_LOCK);
  291.     long fh = NULL;
  292.     FIB *fib = malloc(sizeof(FIB));
  293.     static HDR Hdr;
  294.     long len;
  295.     short error = 0;
  296.     short ac;
  297.  
  298.     if (!lock || !Examine(lock, fib)) {
  299.     WriteHeader('N', "Possible Disk Error", 0);
  300.     error = 1;
  301.     goto done;
  302.     }
  303.  
  304.     ac = ExtractFieldVal(fib->fib_Comment, FS_ACCESS, NULL);
  305.     if (ac >= 0 && ac > access) {
  306.     error = WriteHeader('S', fib->fib_FileName, 0);
  307.     goto done;
  308.     }
  309.  
  310.     fh = Open(name, 1005);
  311.     if (fh == NULL)         /*  don't do anything if unable to open it */
  312.     goto done;
  313.     Seek(fh, 0L, 1);
  314.     len = Seek(fh, 0L, 0);
  315.     if (error = WriteHeader('F', fib->fib_FileName, len))
  316.     goto done;
  317.     switch(ReadHeader(&Hdr)) {
  318.     case 'Y':
  319.     Seek(fh, Hdr.Val, -1);  /*  start pos.  */
  320.     len -= Hdr.Val;
  321.     if (len < 0)
  322.         len = 0;
  323.     break;
  324.     case 'S':
  325.     goto done;
  326.     case 'N':
  327.     error = 1;
  328.     break;
  329.     default:
  330.     error = 1;
  331.     break;
  332.     }
  333.     if (error)
  334.     goto done;
  335.     while (len) {
  336.     long n = (len > sizeof(Buf)) ? sizeof(Buf) : len;
  337.  
  338.     if (Read(fh, Buf, n) != n) {    /*  read failed! */
  339.         error = 10;
  340.         goto done;
  341.     }
  342.     if (DWrite(Chan, Buf, n) != n) {
  343.         error = 10;
  344.         goto done;
  345.     }
  346.     len -= n;
  347.     }
  348. done:
  349.     free(fib);
  350.     if (fh)
  351.     Close(fh);
  352.     if (lock)
  353.     UnLock(lock);
  354.     return((int)error);
  355. }
  356.  
  357. int
  358. WriteHeader(c, str, len)
  359. char c;
  360. char *str;
  361. long len;
  362. {
  363.     ubyte sl;
  364.  
  365.     if (str == NULL)
  366.     str = "";
  367.     sl = strlen(str);
  368.  
  369.     if (DWrite(Chan, &c, 1) < 0)
  370.     return(1);
  371.     if (DWrite(Chan, &sl,1) < 0)
  372.     return(1);
  373.     if (DWrite(Chan, str, sl) != sl)
  374.     return(1);
  375.     if (DWrite(Chan, &len, 4) != 4)
  376.     return(1);
  377.     return(0);
  378. }
  379.  
  380. int
  381. ReadHeader(hdr)
  382. HDR *hdr;
  383. {
  384.     ubyte sl;
  385.     ubyte cmd;
  386.  
  387.     hdr->Cmd = -1;
  388.     if (DRead(Chan, &cmd, 1) != 1)
  389.     return(-1);
  390.     if (DRead(Chan, &sl, 1) != 1)
  391.     return(-1);
  392.     if (sl >= sizeof(hdr->Str)) {
  393.     return(-1);
  394.     }
  395.     if (DRead(Chan, hdr->Str, sl) != sl)
  396.     return(-1);
  397.     hdr->Str[sl] = 0;
  398.     if (DRead(Chan, &hdr->Val, 4) != 4)
  399.     return(-1);
  400.     hdr->Cmd = cmd;
  401.     return((int)hdr->Cmd);
  402. }
  403.  
  404.  
  405.