home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / MUser17src.lha / MultiUser / src / Support / MProtect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  5.3 KB  |  225 lines

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