home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0905.lha / MultiUser / C.src / MProtect.c < prev    next >
C/C++ Source or Header  |  1993-08-26  |  5KB  |  223 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Protect Clone                                                            *
  5. * ---------------------------------------------------------    *
  6. * ⌐ Copyright 1993 by Geert Uytterhoeven                            *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosasl.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <libraries/multiuser.h>
  17. #include <proto/multiuser.h>
  18.  
  19. #include "MProtect_rev.h"
  20.  
  21.  
  22. char __VersTag__[] = VERSTAG;
  23.  
  24.  
  25. #define FLAGS_OWNER    1
  26. #define FLAGS_GROUP    2
  27. #define FLAGS_OTHER    3
  28.  
  29.  
  30. BOOL __regargs ProcessFlags(char *str, ULONG *flags, ULONG type, struct DosLibrary *DOSBase);
  31.  
  32.  
  33. int __saveds Start(char *arg)
  34. {
  35.     struct ExecBase *SysBase;
  36.     struct DosLibrary *DOSBase;
  37.     struct muBase *muBase = NULL;
  38.     struct RDArgs *args;
  39.     LONG argarray[] = {
  40.         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  41.     };
  42.     ULONG mask, flags = NULL;
  43.     struct AnchorPath *anchor;
  44.     BPTR dir;
  45.     LONG error = NULL;
  46.     int rc = RETURN_OK;
  47.  
  48.     SysBase = *(struct ExecBase **)4;
  49.     
  50.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  51.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  52.         rc = RETURN_FAIL;
  53.         goto Exit;
  54.     }
  55.     args = ReadArgs("FILE/A,FLAGS,GROUP/K,OTHER/K,ADD/S,SUB/S,ALL/S,QUIET/S",
  56.                          argarray, NULL);
  57.     if (!args)
  58.         error = IoErr();
  59.     else if (argarray[4] && argarray[5]) {
  60.         PutStr("You can't specify both ADD and SUB\n");
  61.         rc = RETURN_ERROR;
  62.     } else if ((argarray[1] && !ProcessFlags((char *)argarray[1], &flags,
  63.                                                           FLAGS_OWNER, DOSBase)) ||
  64.                   (argarray[2] && !ProcessFlags((char *)argarray[2], &flags,
  65.                                                           FLAGS_GROUP, DOSBase)) ||
  66.                   (argarray[3] && !ProcessFlags((char *)argarray[3], &flags,
  67.                                                           FLAGS_OTHER, DOSBase)))
  68.         rc = RETURN_ERROR;
  69.     else if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  70.                                                                     MEMF_CLEAR)) {
  71.         anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  72.         anchor->ap_Flags = APF_DOWILD;
  73.         anchor->ap_Strlen = 1024;
  74.         if (!(error = MatchFirst((char *)argarray[0], anchor))) {
  75.             do
  76.                 if (anchor->ap_Flags & APF_DIDDIR)
  77.                     anchor->ap_Flags &= ~APF_DIDDIR;
  78.                 else {
  79.                     if (argarray[6] && (anchor->ap_Info.fib_DirEntryType > 0))
  80.                         anchor->ap_Flags |= APF_DODIR;
  81.                     mask = anchor->ap_Info.fib_Protection;
  82.                     mask ^= FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  83.                     if (argarray[4])
  84.                         mask |= flags;
  85.                     else if (argarray[5])
  86.                         mask &= (~flags);
  87.                     else
  88.                         mask = flags;
  89.                     mask ^= FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  90.                     dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  91.                     if ((mask & muFIBF_SET_UID) &&
  92.                          (mask & (FIBF_GRP_WRITE | FIBF_OTR_WRITE)))
  93.                         PutStr("WARNING: U and W are both set\n");
  94.                     if (!muSetProtection(anchor->ap_Info.fib_FileName, mask)) {
  95.                         PutStr(anchor->ap_Buf);
  96.                         PrintFault(IoErr(), " ");
  97.                     } else if (!argarray[7]) {
  98.                         PutStr(anchor->ap_Buf);
  99.                         if (anchor->ap_Info.fib_DirEntryType > 0)
  100.                             PutStr(" (dir)");
  101.                         PutStr("...Done\n");
  102.                     }
  103.                     UnLock(CurrentDir(dir));
  104.                 }
  105.             while (!(error = MatchNext(anchor)));
  106.             if (error == ERROR_NO_MORE_ENTRIES)
  107.                 error = NULL;
  108.         } else if (error == ERROR_NO_MORE_ENTRIES)
  109.             error = ERROR_OBJECT_NOT_FOUND;
  110.         MatchEnd(anchor);
  111.         FreeVec(anchor);
  112.     } else
  113.         error = IoErr();
  114.  
  115.  
  116.     FreeArgs(args);
  117.     if (error) {
  118.         PrintFault(error, NULL);
  119.         rc = RETURN_ERROR;
  120.     }
  121.  
  122. Exit:
  123.     CloseLibrary((struct Library *)muBase);
  124.     CloseLibrary((struct Library *)DOSBase);
  125.  
  126.     return(rc);
  127. }    
  128.  
  129.  
  130. BOOL __regargs ProcessFlags(char *str, ULONG *flags, ULONG type, struct DosLibrary *DOSBase)
  131. {
  132.     int i;
  133.     BOOL rc = TRUE;
  134.  
  135.     for (i = 0; str[i] && rc; i++) {
  136.         switch(str[i]) {
  137.             case 'u':
  138.             case 'U':
  139.                 if (type == FLAGS_OWNER)
  140.                     *flags |= muFIBF_SET_UID;
  141.                 else
  142.                     goto Error;
  143.                 break;
  144.  
  145.             case 's':
  146.             case 'S':
  147.                 if (type == FLAGS_OWNER)
  148.                     *flags |= FIBF_SCRIPT;
  149.                 else
  150.                     goto Error;
  151.                 break;
  152.  
  153.             case 'p':
  154.             case 'P':
  155.                 if (type == FLAGS_OWNER)
  156.                     *flags |= FIBF_PURE;
  157.                 else
  158.                     goto Error;
  159.                 break;
  160.  
  161.             case 'a':
  162.             case 'A':
  163.                 if (type == FLAGS_OWNER)
  164.                     *flags |= FIBF_ARCHIVE;
  165.                 else
  166.                     goto Error;
  167.                 break;
  168.  
  169.             case 'r':
  170.             case 'R':
  171.                 if (type == FLAGS_OWNER)
  172.                     *flags |= FIBF_READ;
  173.                 else if (type == FLAGS_GROUP)
  174.                     *flags |= FIBF_GRP_READ;
  175.                 else
  176.                     *flags |= FIBF_OTR_READ;
  177.                 break;
  178.  
  179.             case 'w':
  180.             case 'W':
  181.                 if (type == FLAGS_OWNER)
  182.                     *flags |= FIBF_WRITE;
  183.                 else if (type == FLAGS_GROUP)
  184.                     *flags |= FIBF_GRP_WRITE;
  185.                 else
  186.                     *flags |= FIBF_OTR_WRITE;
  187.                 break;
  188.  
  189.             case 'e':
  190.             case 'E':
  191.                 if (type == FLAGS_OWNER)
  192.                     *flags |= FIBF_EXECUTE;
  193.                 else if (type == FLAGS_GROUP)
  194.                     *flags |= FIBF_GRP_EXECUTE;
  195.                 else
  196.                     *flags |= FIBF_OTR_EXECUTE;
  197.                 break;
  198.  
  199.             case 'd':
  200.             case 'D':
  201.                 if (type == FLAGS_OWNER)
  202.                     *flags |= FIBF_DELETE;
  203.                 else if (type == FLAGS_GROUP)
  204.                     *flags |= FIBF_GRP_DELETE;
  205.                 else
  206.                     *flags |= FIBF_OTR_DELETE;
  207.                 break;
  208.  
  209.             default:
  210. Error:
  211.                 rc = FALSE;
  212.                 PutStr("Invalid flag - must be one of ");
  213.                 if (type == FLAGS_OWNER)
  214.                     PutStr("USPARWED\n");
  215.                 else
  216.                     PutStr("RWED\n");
  217.                 break;
  218.         }
  219.     }
  220.  
  221.     return(rc);
  222. }
  223.