home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / lanserv / access32 / access32.c next >
C/C++ Source or Header  |  1998-10-01  |  15KB  |  438 lines

  1. /****************************************************************************/
  2. /*
  3.  *    PROGRAM NAME: ACCESS32
  4.  *    ------------
  5.  *
  6.  *    What this program does: Adds, sets, or displays access control
  7.  *    profiles on the server where it is running.
  8.  *
  9.  *    SYNTAX:
  10.  *    ------
  11.  *    ACCESS32 <resource> -- displays ACLs for the specified resource
  12.  *    ACCESS32 <resource> <user:perm> <user:perm>  -- Adds or sets listed
  13.  *      permissions for the specified resource
  14.  *
  15.  *    REQUIRED FILES:
  16.  *    --------------
  17.  *    ACCESS32.C        -  Source code for this program
  18.  *
  19.  *    REQUIRED LIBRARIES:
  20.  *    ------------------
  21.  *    NETAPI32.LIB     -  Netapi library (in \IBMLAN\NETSRC\LIB directory)
  22.  *
  23.  *    NetAPI functions used in this program:
  24.  *    -------------------------------------
  25.  *    Net32AccessAdd
  26.  *    Net32AccessGetInfo
  27.  *    Net32AccessSetInfo
  28.  *
  29.  *    HOW TO COMPILE THIS PROGRAM:
  30.  *    ---------------------------
  31.  *    icc /C /Gt+ /DPURE_32 access32.c
  32.  */
  33. /****************************************************************************/
  34.  
  35. /*------- OS/2 include files -----------------------------------------------*/
  36. #define INCL_DOSMEMMGR
  37. #define INCL_DOSPROCESS
  38. #include <os2.h>
  39.  
  40. /*------- NET APIs include files -------------------------------------------*/
  41. #include <neterr.h>
  42. #include <netcons.h>
  43. #include <access.h>
  44. #include <wksta.h>
  45.  
  46. /*------- C include files --------------------------------------------------*/
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. /*------- Function declaration ---------------------------------------------*/
  51. VOID DisplayAccessInfo(struct access_info_1 *);
  52. VOID Syntax(VOID);
  53. VOID ParseAccessInfo(struct access_info_1 *, int, char **);
  54. VOID Error_Message(USHORT, PSZ);
  55.  
  56. #define         ADD     1
  57. #define         SET     2
  58.  
  59. /****************************************************************************/
  60. /* MAIN C function                                                          */
  61. /*--------------------------------------------------------------------------*/
  62. VOID
  63. main(int argc, char *argv[])
  64. {
  65.     USHORT                      usRc = 0,
  66.                                 usOperation = 0;
  67.     ULONG                       ulTotalAvail = 0,
  68.                                 ulBuflen = 4096;
  69.     struct   access_info_1     *pAccessBuf;
  70.     PCHAR                       pszFunction = "Net32AccessGetInfo";
  71.  
  72.     if (argc == 1)
  73.         Syntax();
  74.  
  75.     /* Allocate some memory for API calls. */
  76.  
  77.     usRc = DosAllocMem((PPVOID)&pAccessBuf,
  78.                        ulBuflen,
  79.                        PAG_WRITE | PAG_READ | PAG_COMMIT);
  80.  
  81.     if (usRc)
  82.     {
  83.         printf("Unable to allocate memory.  DosAllocMem returned %d.\n",
  84.                 usRc);
  85.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  86.     }
  87.  
  88.     /* Get access information on the specified resource */
  89.     usRc = Net32AccessGetInfo(NULL,
  90.                             argv[1],
  91.                             1L,
  92.                             (unsigned char *)pAccessBuf,
  93.                             ulBuflen,
  94.                             &ulTotalAvail);
  95.  
  96.     /* If no error and user requested Display, display the access information */
  97.     if (usRc == NERR_Success && argc == 2)      /* Display */
  98.     {
  99.         DisplayAccessInfo(pAccessBuf);
  100.     }
  101.     /* user wants to add or set access control information */
  102.     else if (argc > 2)
  103.     {
  104.         /*
  105.          * If the Get call returned no error, access control exists
  106.          * for the resource, so a Set operation will be done.
  107.          * If NERR_ResourceNotFound was returned, no access control
  108.          * exists for the resource, so an Add operation will be done.
  109.          */
  110.  
  111.         if (usRc == NERR_Success)
  112.             usOperation = SET;
  113.         else if (usRc == NERR_ResourceNotFound)
  114.             usOperation = ADD;
  115.  
  116.         if (usOperation == SET)
  117.         {
  118.             /*
  119.              * Parse the command line and add acl info to the buffer
  120.              */
  121.             pszFunction = "Net32AccessSetInfo";
  122.             ParseAccessInfo(pAccessBuf, argc, argv);
  123.             printf("Setting access control for %s...\n", argv[1]);
  124.             usRc = Net32AccessSetInfo(NULL,
  125.                                     argv[1],
  126.                                     1,
  127.                                     (unsigned char *)pAccessBuf,
  128.                                     ulBuflen,
  129.                                     PARMNUM_ALL);
  130.         }
  131.         else if (usRc == NERR_ResourceNotFound)
  132.         {
  133.             /*
  134.              * Set up the access_info_1 structure in the buffer;
  135.              * then parse the command line and add acl info to the buffer.
  136.              */
  137.             pszFunction = "Net32AccessAdd";
  138.             pAccessBuf -> acc1_resource_name = argv[1];
  139.             pAccessBuf -> acc1_attr = 0;
  140.             pAccessBuf -> acc1_count = 0;
  141.  
  142.             ParseAccessInfo(pAccessBuf, argc, argv);
  143.  
  144.             printf("Adding access control for %s...\n", argv[1]);
  145.             usRc = Net32AccessAdd(NULL,
  146.                                 1,
  147.                                 (unsigned char *)pAccessBuf,
  148.                                 ulBuflen);
  149.         }
  150.     }
  151.  
  152.     /* Free the buffer */
  153.     (void)DosFreeMem((PVOID)pAccessBuf);
  154.  
  155.     /* If an error occurred, display it */
  156.     if (usRc)
  157.         Error_Message(usRc, pszFunction);
  158.  
  159.     if (usRc == NERR_Success &&
  160.         (usOperation == SET || usOperation == ADD))
  161.         printf("\tSuccess!\n");
  162.  
  163.     DosExit(EXIT_PROCESS, (ULONG)usRc);
  164.  
  165. }
  166.  
  167.  
  168. /****************************************************************************/
  169. /*  Function  DisplayAccessInfo                                             */
  170. /*--------------------------------------------------------------------------*/
  171. VOID DisplayAccessInfo(struct access_info_1 *buf)
  172. {
  173.     struct access_list *list;
  174.     SHORT               count = buf -> acc1_count;
  175.  
  176.     /*
  177.      * Print the resource name, its attributes, and the number
  178.      * of access control entries associated with it.
  179.      */
  180.     printf("\n\nResource name         : %s ", buf -> acc1_resource_name);
  181.     printf("\n\tAttributes    : %x ", buf -> acc1_attr);
  182.     printf("\n\tCount         : %d", count);
  183.  
  184.     /*
  185.      * Print information about each access control entry.
  186.      */
  187.     if (count)
  188.     {
  189.         list = (struct access_list *)(buf + 1);
  190.  
  191.         while (count--)
  192.         {
  193.             printf("\n\n\tUser/Group    : %s ", list -> acl_ugname);
  194.             printf("\n\tPermissions   : ");
  195.             if (!list -> acl_access)
  196.                 printf("NONE");
  197.             else
  198.             {
  199.                 if (list -> acl_access & ACCESS_READ)
  200.                     printf("R");
  201.  
  202.                 if (list -> acl_access & ACCESS_WRITE)
  203.                     printf("W");
  204.  
  205.                 if (list -> acl_access & ACCESS_CREATE)
  206.                     printf("C");
  207.  
  208.                 if (list -> acl_access & ACCESS_EXEC)
  209.                     printf("X");
  210.  
  211.                 if (list -> acl_access & ACCESS_DELETE)
  212.                     printf("D");
  213.  
  214.                 if (list -> acl_access & ACCESS_ATRIB)
  215.                     printf("A");
  216.  
  217.                 if (list -> acl_access & ACCESS_PERM)
  218.                     printf("P");
  219.             }
  220.             list++;
  221.         }
  222.         printf("\n");
  223.     }
  224. }
  225.  
  226.  
  227. /*
  228.  *  Syntax(): Display syntax information and exit.
  229.  */
  230. void
  231. Syntax()
  232. {
  233.     printf("Usage:\n");
  234.     printf("   ACCESS32 <resource>\n");
  235.     printf("      OR\n");
  236.     printf("   ACCESS32 <resource> <name:permissions> <name:permissions> ...\n\n");
  237.     DosExit(EXIT_PROCESS, 1);
  238. }
  239.  
  240. /*
  241.  *  ParseAccessInfo(): Parse the <name:permissions> command line input
  242.  *    and add to API buffer if valid.
  243.  */
  244. void
  245. ParseAccessInfo(struct access_info_1 *pBuf,
  246.                 int argc,
  247.                 char **argv)
  248. {
  249.     USHORT              usCount,        /* Number of access_list structures */
  250.                                         /*   in pBuf                        */
  251.                         i,              /* Index into argv array            */
  252.                         j,              /* Loop counter                     */
  253.                         usCmdLineItems; /* Number of name:permissions items */
  254.                                         /*   specified on the command line  */
  255.     struct access_list *pAccListNew;    /* Pointer to access_list structures*/
  256.     struct access_list *pAccListCurrent;/*     "                            */
  257.     BOOL                found;
  258.     SHORT               sPermissions;   /* Permissions value                */
  259.     PSZ                 pszPerms;       /* Pointer to "permissions" portion */
  260.                                         /*   of name:permissions string     */
  261.  
  262.     /* Initialize variables */
  263.     usCount = pBuf -> acc1_count;
  264.  
  265.     /*
  266.      * The access_list structures immediately follow the
  267.      * access_info_1 structure in the buffer.
  268.      */
  269.     pAccListNew = pAccListCurrent = (struct access_list *)(pBuf + 1);
  270.  
  271.     /* Any new userids/groupids will be added to the end of the
  272.      * existing list of userids/groupids (if any). Point past any
  273.      * current access_list structures
  274.      */
  275.     pAccListNew += usCount;
  276.  
  277.     /*
  278.      * Loop through the name:permission items specified on the
  279.      * command line, checking the permissions letters for validity.
  280.      */
  281.     for (usCmdLineItems = argc - 2, i = 2;
  282.          usCmdLineItems;
  283.          --usCmdLineItems, ++i)
  284.     {
  285.         /*
  286.          * Find the colon in the command line item and replace it
  287.          * with a null.  Display syntax and exit if no colon is found.
  288.          */
  289.         pszPerms = strchr(argv[i], ':');
  290.  
  291.         if (pszPerms == (char *)NULL)
  292.         {
  293.             (void)DosFreeMem((PVOID)pBuf);
  294.             Syntax();
  295.         }
  296.         *pszPerms = '\0';
  297.  
  298.         /*
  299.          * A colon was found...make sure a string follows it.
  300.          */
  301.         ++pszPerms;
  302.         if (pszPerms == (char *)NULL ||
  303.             pszPerms && *pszPerms == '\0')
  304.         {
  305.             (void)DosFreeMem((PVOID)pBuf);
  306.             Syntax();
  307.         }
  308.  
  309.         /*
  310.          * Now make sure that all the permissions letters are
  311.          * valid.  Display syntax and exit if invalid permissions
  312.          * letters are specified.  The constant values below are
  313.          * defined in ACCESS.H.
  314.          */
  315.         sPermissions = ACCESS_NONE;
  316.  
  317.         while (*pszPerms)
  318.         {
  319.             switch(*pszPerms)
  320.             {
  321.                 case 'R':
  322.                 case 'r':
  323.                     sPermissions |= ACCESS_READ;
  324.                     break;
  325.  
  326.                 case 'W':
  327.                 case 'w':
  328.                     sPermissions |= ACCESS_WRITE;
  329.                     break;
  330.  
  331.                 case 'C':
  332.                 case 'c':
  333.                     sPermissions |= ACCESS_CREATE;
  334.                     break;
  335.  
  336.                 case 'X':
  337.                 case 'x':
  338.                     sPermissions |= ACCESS_EXEC;
  339.                     break;
  340.  
  341.                 case 'D':
  342.                 case 'd':
  343.                     sPermissions |= ACCESS_DELETE;
  344.                     break;
  345.  
  346.                 case 'A':
  347.                 case 'a':
  348.                     sPermissions |= ACCESS_ATRIB;
  349.                     break;
  350.  
  351.                 case 'P':
  352.                 case 'p':
  353.                     sPermissions |= ACCESS_PERM;
  354.                     break;
  355.  
  356.                 case 'N':
  357.                 case 'n':
  358.                     sPermissions = ACCESS_NONE;
  359.                     break;
  360.  
  361.                 default:
  362.                     (void)DosFreeMem((PVOID)pBuf);
  363.                     Syntax();
  364.                     break;
  365.             } /* end, switch */
  366.  
  367.             ++pszPerms;
  368.  
  369.         } /* end, while */
  370.  
  371.         /*
  372.          * A valid permissions string was specified.  Before adding
  373.          * the userid/groupid to the buffer, check to see if the
  374.          * userid/groupid is already in the buffer.  If the userid/groupid
  375.          * is already in the buffer, change the permissions for the
  376.          * userid/groupid rather than adding another access_list structure
  377.          * for the userid/groupid.  (Only one access_list structure
  378.          * per userid/groupid is allowed).
  379.          *
  380.          * NOTE that before adding a userid/groupid to the buffer, the
  381.          * existence of the userid/groupid could be verified by calling
  382.          * the Net32UserGetInfo and/or Net32GroupGetInfo APIs.
  383.          * However, if the userid/groupid does not exist, the
  384.          * Net32UserAdd/Net32UserSetInfo APIs will return an
  385.          * appropriate error, so existence checking is not done here.
  386.          *
  387.          * NOTE that with this implementation, the same userid/groupid can
  388.          * be specified on the command line multiple times.  If the same
  389.          * userid/groupid is specified multiple times, the last permissions
  390.          * value specified is the one that will be set for the user/group.
  391.          */
  392.  
  393.          /* point to the beginning of the list of users & groups */
  394.          pAccListCurrent = (struct access_list *)(pBuf + 1);
  395.  
  396.          /* loop through the list of users and groups, comparing
  397.           * the buffer value (pAccListCurrent -> acl_ugname) with
  398.           * the command line value (Argv[i]).  There are usCount
  399.           * items in the list pointed to by pAccListCurrent.
  400.           */
  401.          for (j = 0, found = FALSE; j < usCount && !found; ++j)
  402.          {
  403.              /* If the command line specifies a user or group that's
  404.               * currently in the buffer, update that user/group's
  405.               * permissions and break out of the for loop.
  406.               */
  407.              if (!stricmp(argv[i], pAccListCurrent -> acl_ugname))
  408.              {
  409.                  pAccListCurrent -> acl_access = sPermissions;
  410.                  found = TRUE;
  411.              }
  412.              /* the names are different, so point to the next buffer item. */
  413.              else
  414.              {
  415.                  ++pAccListCurrent;
  416.              }
  417.          }
  418.  
  419.          /* If the user/group specified on the command line is not
  420.           * in the current buffer, add the user/group and its permissions
  421.           * to the buffer and update the count of the number of buffer items.
  422.           */
  423.          if (!found)
  424.          {
  425.              strcpy(pAccListNew -> acl_ugname, argv[i]);
  426.              pAccListNew -> acl_access = sPermissions;
  427.              ++usCount;
  428.              ++pAccListNew;
  429.          }
  430.     } /* end, for */
  431.  
  432.     /* Update the buffer with the number of access_list entries */
  433.     pBuf -> acc1_count = usCount;
  434.  
  435.     return;
  436.  
  437. }
  438.