home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / dec93 / os20 / util / multiuser.lha / MultiUser / Extern / UserID / UserID.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  6.9 KB  |  179 lines

  1. /******************************************************************
  2. * MultiUser - MultiUser Task/File Support System          *
  3. * --------------------------------------------------------------- *
  4. * Get User ID data - AmigaShell script support utility          *
  5. *-----------------------------------------------------------------*
  6. *                                  *
  7. * Usage:  UserID USERID/S,UID/S,GID/S,NAME/S,UACCESS/K/N,         *
  8. *                GACCESS/K/N                                      *
  9. *         USERID : Default. Returns userID of looged in user.     *
  10. *         UID    : Returns User ID Number                         *
  11. *         GID    : Returns Group ID Number (this will change :)   *
  12. *         NAME   : Returns full user name                         *
  13. *         UACCESS: Used for User security filter with scripts     *
  14. *         GACCESS: Used for Group security filter with scripts    *
  15. * Action: Returns data of the currently logged in user              *
  16. *      (similar to UserInfo). However, this ONLY returns the   *
  17. *         required data, so it can be used with SetEnv            *
  18. *         (ie. SetEnv login `UserID` will make the environment    *
  19. *         variable $login contain your userid                 *
  20. *      NB: use *backward* single quotes - "`" not "'" )      *
  21. *      or other AmigaDOS commands.                  *
  22. *      eg. Echo "hello `UserID`" > SPEAK:              *
  23. *                                   *
  24. *      If no-one is logged in UserID returns "Nobody" and      *
  25. *      sets an ERROR condition.                  *
  26. *      Eg: Failat 21                          *
  27. *           UserID <>NIL:                      *
  28. *          If ERROR                          *
  29. *           Echo "no-one home!"                  *
  30. *          Else                                                *
  31. *                 UserInfo                      *
  32. *          EndIf                          *
  33. *                                   *
  34. *      If the current user is in root's group ($FFFF), a WARN  *
  35. *      condition is set.                      *
  36. *      Eg: UserID <>NIL:                      *
  37. *          If WARN                          *
  38. *           Prompt "*e[32m%N.%S> *e[31m"              *
  39. *          Else                          *
  40. *          Prompt "%N.%S> "                  *
  41. *          EndIf                          *
  42. *                                  *
  43. *         The two special keywords UACCESS and GACCESS are used   *
  44. *         to determine access priority in shell scripts.          *
  45. *         UserID UACCESS k will set a WARN condition if the UID   *
  46. *         of the current user is less than k.                     *
  47. *         Note that WARN and usage with regard to root's group is *
  48. *         disabled then.                                          *
  49. *         Eg: Echo "Attempted access by " NOLINE                  *
  50. *             UserID NAME UACCESS 5                               *
  51. *             If WARN                                             *
  52. *                 Echo "Access denied."                           *
  53. *             EndIf                                               *
  54. *                                                                 *
  55. *         UserID GACCESS k will behave similarly, checking group  *
  56. *         access instead of user access.                          *
  57. *                                                                 * 
  58. *-----------------------------------------------------------------*
  59. *                                  *
  60. * © 1993 Fabian Nuñez  -  contact me at: fnunez@cs.uct.ac.za      *
  61. *                                  *
  62. ******************************************************************/
  63.  
  64. #include <dos/dos.h>
  65. #include <exec/types.h>
  66. #include <proto/exec.h>
  67. #include <proto/dos.h>
  68. #include <proto/multiuser.h>
  69. #include <libraries/multiuser.h>
  70. #include "UserID_rev.h"
  71.  
  72. /* Prototypes */
  73. void itoa(LONG,STRPTR);
  74.  
  75.  
  76. char __VersTag__[] = VERSTAG;
  77.  
  78. void __main(char *arg)
  79. {
  80.         struct DosLibrary *DOSBase = NULL;
  81.     struct muBase *muBase = NULL;
  82.     struct muUserInfo *info = NULL;
  83.     struct RDArgs *args;
  84.         ULONG code;
  85.     UBYTE *returned = NULL;
  86.         LONG argarray[] = { NULL, NULL, NULL, NULL, NULL, NULL };
  87.     UBYTE bitarray[] = {1,2,4,8};
  88.         UBYTE bits;
  89.         LONG i;
  90.             
  91.     /* Open libraries */
  92.         
  93.         DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",0L);
  94.                         
  95.     muBase = (struct muBase *)OpenLibrary("multiuser.library",39L);
  96.     if (!muBase) Exit(122); /* invalid resident library */
  97.     
  98.         args = ReadArgs("USERID/S,UID/S,GID/S,NAME/S,UACCESS/K/N,GACCESS/K/N",argarray,NULL);
  99.         if (!args)
  100.         {
  101.                 PrintFault(IoErr(),NULL);
  102.                 code = 0; /* force error */
  103.                 goto EXIT;
  104.         }
  105.  
  106.         for (i=0,bits=0;i<4;i++)
  107.                 if (argarray[i]) bits += bitarray[i];
  108.                 
  109.     /* Get a muUserInfo struct */
  110.     info = muAllocUserInfo();
  111.  
  112.     /* store info in 'returned' */
  113.     if (code = muGetTaskOwner(NULL)) /* get uid of owner of this task */
  114.     {
  115.         info->uid = (UWORD) (code >> 16); 
  116.         info = muGetUserInfo(info,muKeyType_uid);  
  117.                 switch(bits)
  118.                 {
  119.                         case 0: /* Default case is case 1 */
  120.                         case 1: returned = info->UserID;
  121.                                 break;
  122.                         case 2: returned = "     ";
  123.                                 itoa(info->uid,returned);
  124.                                 break;
  125.                         case 4: returned = "     ";
  126.                                 itoa(info->gid,returned);
  127.                                 break;                                
  128.                         case 8: returned = info->UserName;
  129.                                 break;
  130.                         default: returned = "One switch only!";
  131.                                  code = 0; /* force error */
  132.                 }                       
  133.     }
  134.     else returned = "Nobody"; /* task owned by nobody */
  135.  
  136.         if (argarray[4] && argarray[5])
  137.         {
  138.                 returned = "One keyword only!";
  139.                 code = 0; /* force error */
  140.         }
  141.         else if (code != 0) /* If error or no-one logged in skip this part */
  142.         {
  143.                 if (argarray[4])
  144.                         if (((ULONG)info->uid) < *(ULONG *)argarray[4])
  145.                                 code = 0xFFFFFFFF; /* Force a WARN condition */
  146.                         else code = 1; /* remove normal WARN if root is logged on */
  147.  
  148.                 if (argarray[5])
  149.                         if (((ULONG)info->gid) < *(ULONG *)argarray[5])
  150.                                 code = 0xFFFFFFFF; /* Force a WARN condition */
  151.                         else code = 1; /* remove normal WARN as above */
  152.         }
  153.         
  154.         PutStr(returned);
  155.         PutStr("\n");
  156.                        
  157. EXIT:    muFreeUserInfo(info); /* Return memory to Exec */
  158.     CloseLibrary((struct Library *)muBase);
  159.         CloseLibrary((struct Library *)DOSBase);
  160.     
  161.     if (code == 0)
  162.         Exit(RETURN_ERROR); /* ERROR or no-one home today */
  163.     else if ((code & muMASK_GID) == 0xFFFF)
  164.         Exit(RETURN_WARN);  /* WARN - a root is logged in or security violation */
  165.     else Exit(RETURN_OK);        /* OK - nothing to remark */ 
  166. }
  167.  
  168. void itoa(LONG number,STRPTR string)
  169. {
  170.         LONG i;
  171.         
  172.         for (i=4;i;i--)
  173.         {
  174.                 string[i] = '0' + number % 10;
  175.                 number /= 10;
  176.         }
  177.         string[i] = '0' + number % 10; /* Last digit */
  178. }
  179.