home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dsstlkt5.zip / dssos2tk / dss / USER32.C < prev    next >
C/C++ Source or Header  |  1998-05-08  |  16KB  |  444 lines

  1. /****************************************************************************/
  2. /*
  3.  *    PROGRAM NAME: USER32
  4.  *    ------------
  5.  *
  6.  *    What this program does:  Creates a new user (password same as 
  7.  *    userid, assigns user default resource domain, adds a file alias 
  8.  *    as logon assignment (if specified).  Displays information about the
  9.  *    user.  Deletes the user.         
  10.  *
  11.  *    Setup:
  12.  *      If alias logon assigment used.
  13.  *        - Create an alias for a file resource in the user's default
  14.  *          resource domain.  ALIAS32.EXE could be used.
  15.  *        - Give the user group of the resouce domain,
  16.  *          "realms/<resourcedomainname>/USER", read access to the 
  17.  *          alias resource if you plan to test user logon.       
  18.  *
  19.  *    SYNTAX:
  20.  *    ------
  21.  *    USER32 username /ADD //defaultresourcedomain [aliasname]
  22.  *      creates user account, adds user default resource domain, adds 
  23.  *      userid to "USER" group of resource domain, and adds alias as
  24.  *      logon assignment (if specified)
  25.  *
  26.  *    USER32 username
  27.  *      displays information about the user 'username'
  28.  *
  29.  *    USER32 username /DEL
  30.  *      deletes the user account
  31.  *
  32.  *    REQUIRED FILES:
  33.  *    --------------
  34.  *    USER32.C       -  Source code for this program
  35.  *
  36.  *    REQUIRED LIBRARIES:
  37.  *    ------------------
  38.  *    NETAPI32.LIB   -  Netapi library (in \IBMLAN\NETSRC\LIB directory)
  39.  *
  40.  *    NetAPI32 functions used in this program:
  41.  *    ---------------------------------------
  42.  *    Net32UserAdd
  43.  *    Net32UserDel
  44.  *    Net32UserGetGroups
  45.  *    Net32UserGetInfo
  46.  *    Net32UserGetLogonAsn
  47.  *    Net32UserSetLogonAsn
  48.  *
  49.  *    HOW TO COMPILE THIS PROGRAM:
  50.  *    ---------------------------
  51.  *    see makefile
  52.  */
  53. /****************************************************************************/
  54.  
  55. /**************   Defines   *************************************************/
  56. #define    DISPLAY     0
  57. #define    ADD         1
  58. #define    DELETE      2
  59. #define    BUFFER_LEN  4096
  60. #define    INCL_DOSPROCESS
  61. #define    INCL_DOSMEMMGR
  62. #define    INCL_DOSERRORS
  63.  
  64. /**************   OS/2 include files ****************************************/
  65. #include <os2.h>
  66.  
  67. /**************   C-language include files   ********************************/
  68. #include <stdio.h>
  69. #include <string.h>
  70.  
  71. /**************   LAN Server include files   ********************************/
  72. #include <netcons.h>
  73. #include <neterr.h>
  74. #include <access.h>
  75. #include <dcdb.h>
  76. #include <lsdceerr.h>
  77.  
  78. /**************   Forward declarations of local functions   ******************/
  79. VOID Error_Message(USHORT, PSZ);
  80. VOID Syntax(VOID);
  81. VOID AddUser(char *, char *, char *);
  82. VOID DeleteUser(char *);
  83. VOID DisplayUserDetails(char *);
  84.  
  85. /**************   Global Variables   *****************************************/
  86. lsdce_err_stat_t  ErrStatus;
  87.  
  88. /**************   Main program   *********************************************/
  89. /*
  90.  * This function simply validates the command line arguments and
  91.  * then calls a worker function, based on command line input.
  92.  */
  93. VOID
  94. main(int argc, char *argv[])
  95. {
  96.     USHORT                 usFunction = DISPLAY;
  97.     CHAR                   pUser[UNLEN+1];
  98.     CHAR                   pDefResDom[256];
  99.     CHAR                   pAliasName[ALIAS_LEN+1] = {'\0'};
  100.  
  101.     /* Validate command line input */
  102.     if (argc < 2 || argc > 5)
  103.         Syntax();
  104.  
  105.     /* Verify that username is not too long. */
  106.     if (strlen(argv[1]) > UNLEN)
  107.         Syntax();
  108.  
  109.     /* If a command line switch was specified, it must be "/ADD" or "/DELETE" */
  110.     if (argc >= 3)
  111.     {
  112.         if (!stricmp(argv[2], "/ADD"))
  113.         {
  114.  
  115.     /* For add, default resource domain name is required with double 
  116.        leading forward slashes                                          */
  117.             if ((argc == 3) || ( *(argv[3]) != '/' || *(argv[3]+1) != '/'))
  118.                 Syntax();     
  119.             strcpy(pDefResDom, argv[3]);
  120.  
  121.     /* Is an alias name specified?                                      */
  122.             if (argc == 5)
  123.                 strcpy(pAliasName,argv[4]);
  124.  
  125.             usFunction = ADD;
  126.             
  127.         }
  128.         else if (!strnicmp(argv[2], "/DEL", strlen("/DEL")))
  129.             usFunction = DELETE;
  130.         else
  131.             Syntax();
  132.     }
  133.  
  134.     strcpy(pUser, argv[1]);
  135.  
  136.     /* Call the appropriate worker function. */
  137.     if (usFunction == ADD)
  138.         AddUser(pUser, pDefResDom, pAliasName);
  139.     else if (usFunction == DELETE)
  140.         DeleteUser(pUser);
  141.     else
  142.         DisplayUserDetails(pUser);
  143.  
  144.     DosExit(EXIT_PROCESS, 0);
  145. }
  146.  
  147. /*
  148.  * This function takes the username specified on the command line as
  149.  * input and attempts to:
  150.  *  - Create a user account.
  151.  *  - Assign the default resource domain.
  152.  *  - If specified, add alias as logon assignment.
  153.  */
  154. VOID
  155. AddUser(char * pszUser, char * pszDefResDom, char * pszAliasName)
  156. {
  157.     USHORT                       usRc;
  158.     PCHAR                        pBuffer;
  159.     struct user32_info_201      *pUserInfo;
  160.     PCHAR                        pszExtendedAliasName;
  161.     struct logon_asn_info_200   *pLogInfo;
  162.     struct logon_asn_list_200   *pLogList;
  163.     CHAR                        SERVER_ANY[] = "\\\\*";
  164.  
  165.         /* Add a new user account */
  166.  
  167.     usRc = DosAllocMem((PPVOID)&pBuffer,
  168.                        BUFFER_LEN,
  169.                        PAG_WRITE | PAG_READ | PAG_COMMIT);
  170.  
  171.     if (usRc)
  172.         Error_Message(usRc, "DosAllocMem");
  173.     else
  174.     {
  175.  
  176.         /* initialize user info structure                        */
  177.  
  178.         memset(pBuffer, 0, BUFFER_LEN);
  179.         pUserInfo = (struct user32_info_201 *)pBuffer;
  180.  
  181.         strcpy(pUserInfo -> usri201_name, pszUser);
  182.  
  183.         /* user password same as userid */
  184.         strcpy(pUserInfo -> usri201_password, pszUser);
  185.  
  186.         pUserInfo -> usri201_comment =
  187.         pUserInfo -> usri201_usr_comment = 
  188.                                    "userid added by USER sample program";
  189.  
  190.         pUserInfo -> usri201_priv = USER_PRIV_USER;
  191.  
  192.         pUserInfo -> usri201_flags = UF_SCRIPT;
  193.  
  194.         pUserInfo -> usri201_full_name = "This isn't a real user!";
  195.  
  196.         pUserInfo -> usri201_logon_server = SERVER_ANY;
  197.  
  198.         /* The user's account never expires. */
  199.         pUserInfo -> usri201_acct_expires = TIMEQ_FOREVER;
  200.  
  201.         pUserInfo -> usri201_default_realm = pszDefResDom;
  202.  
  203.         pUserInfo -> usri201_pwstrength_server = "lspwsd";
  204.  
  205.         usRc = Net32UserAdd(pszDefResDom, 201, pBuffer, BUFFER_LEN, (UCHAR *)&ErrStatus);
  206.  
  207.         /* if user add successful then add logon assignment     */
  208.         if (usRc)
  209.         {
  210.             Error_Message(usRc, "Net32UserAdd");
  211.         }
  212.         else
  213.         {
  214.  
  215.         /* if an alias name was input then set it as a logon assignment */
  216.             if (*pszAliasName) 
  217.             {
  218.                 memset(pBuffer, 0, BUFFER_LEN);
  219.                 pLogInfo = (struct logon_asn_info_200 *)pBuffer;
  220.                 pLogList =
  221.                    (struct logon_asn_list_200 *)(pBuffer + sizeof(struct logon_asn_info_200));
  222.                 pLogInfo -> lai200_count = 1;
  223.         /* The lal200_global_name entry is used to specify the alias name.
  224.            The format used here is extendedaliasname of the form
  225.            "aliasname@resdomname"                                     */
  226.                 pszExtendedAliasName = pBuffer + (BUFFER_LEN -1024);
  227.                 strcpy(pszExtendedAliasName, pszAliasName);
  228.                 strcat(pszExtendedAliasName, "@");
  229.         /* append the resource domain name without the leading forward
  230.            slashes                                                    */
  231.                 strcat(pszExtendedAliasName, pszDefResDom+2);
  232.                 pLogList -> lal200_global_name = pszExtendedAliasName;
  233.                 strcpy(pLogList -> lal200_alias, pszAliasName);
  234.                 pLogList -> lal200_type = ALIAS_TYPE_FILE;
  235.  
  236.         /* note that the level 200 device entry is a pointer rather than
  237.            and array                                                  */
  238.                 pLogList -> lal200_device = (pszExtendedAliasName +
  239.                                             strlen(pszExtendedAliasName) + 1);
  240.                 strcpy(pLogList -> lal200_device, "M:");
  241.            
  242.            
  243.                 usRc = Net32UserSetLogonAsn(pszDefResDom,
  244.                                         pszUser,
  245.                                         200,
  246.                                         pBuffer,
  247.                                         BUFFER_LEN,
  248.                                         (UCHAR *)&ErrStatus);
  249.            
  250.                 if (usRc)
  251.                 {
  252.                     Error_Message(usRc, "Net32UserSetLogonAsn");
  253.                     DeleteUser(pszUser);
  254.                 }
  255.             }
  256.         }
  257.  
  258.         DosFreeMem((PVOID)pBuffer);
  259.  
  260.     }
  261.  
  262.  
  263.     if (usRc)
  264.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  265.  
  266.     return;
  267.  
  268. }
  269.  
  270. /*
  271.  * DeleteUser() calls the Net32UserDel API to delete a user account.
  272.  */
  273. void
  274. DeleteUser(char *pszUser )
  275. {
  276.     USHORT             usRc;
  277.     CHAR               pBuffer[GNLEN+1];
  278.     ULONG              ulBytesAvailable;
  279.  
  280.  
  281.     usRc = Net32UserDel((CHAR *)NULL,
  282.                         pszUser,
  283.                         (UCHAR *)&ErrStatus);
  284.  
  285.     if (usRc)
  286.         Error_Message(usRc, "Net32UserDel");
  287.     else
  288.         printf("The definition of \"%s\" was successfully deleted.\n",
  289.                strupr(pszUser));
  290.     return;
  291. }
  292.  
  293.  
  294. void
  295. DisplayUserDetails(char *pszUser)
  296. {
  297.     USHORT                      usRc,
  298.                                 usCount;
  299.     ULONG                       ulTotalEntries,
  300.                                 ulAvailable;
  301.     PCHAR                       pBuffer;
  302.     struct user32_info_201     *pUserInfo;
  303.     struct group_info_0        *pGroups;
  304.     struct logon_asn_list_200  *pLogList;
  305.     CHAR                        pszDC[UNCLEN + 1];
  306.  
  307.     /* allocate a buffer for the Net32UserGetInfo call */
  308.     usRc = DosAllocMem((PPVOID)&pBuffer,
  309.                        4096,
  310.                        PAG_WRITE | PAG_READ | PAG_COMMIT);
  311.  
  312.     if (usRc)
  313.     {
  314.         Error_Message(usRc, "DosAllocMem");
  315.         return;
  316.     }
  317.  
  318.     usRc = Net32UserGetInfo((char *)NULL,
  319.                             pszUser,
  320.                             201,
  321.                             pBuffer,
  322.                             4096,
  323.                             &ulAvailable,
  324.                             (UCHAR *)&ErrStatus);
  325.  
  326.     if (usRc)
  327.         Error_Message(usRc, "Net32UserGetInfo");
  328.     else
  329.     {
  330.         /* Display selected portions of the user32_info_201 structure... */
  331.         pUserInfo = (struct user32_info_201 *)pBuffer;
  332.         printf("Details for user %s:\n\n", pUserInfo -> usri201_name);
  333.         printf("   Comment: %s\n", pUserInfo -> usri201_comment);
  334.         printf("   User comment: %s\n", pUserInfo -> usri201_usr_comment);
  335.         printf("   Full Name: %s\n", pUserInfo -> usri201_full_name);
  336.         printf("   Privilege level: %s\n",
  337.                 (pUserInfo -> usri201_priv == USER_PRIV_USER ? "User" :
  338.                  "Administrator"));
  339.         printf("   Home directory: %s\n", pUserInfo -> usri201_home_dir);
  340.         printf("   Preferred logon server: ");
  341.         if (!pUserInfo -> usri201_logon_server ||
  342.             *pUserInfo -> usri201_logon_server == '\0')
  343.             printf("Domain controller\n");
  344.         else if (strcmp(pUserInfo -> usri201_logon_server, "\\\\*"))
  345.             printf("Any server\n");
  346.         else
  347.             printf("%s\n", pUserInfo -> usri201_logon_server);
  348.         printf("   User default resource domain: %s\n", 
  349.                                         pUserInfo -> usri201_default_realm);
  350.  
  351.         /* Now, get group membership information for this user. */
  352.         usRc = Net32UserGetGroups((CHAR *)NULL,
  353.                                pszUser,
  354.                                0,
  355.                                pBuffer,
  356.                                4096,
  357.                                &ulAvailable,
  358.                                &ulTotalEntries,
  359.                                (UCHAR *)&ErrStatus);
  360.  
  361.         if (usRc)
  362.             Error_Message(usRc, "Net32UserGetGroups");
  363.         else
  364.         {
  365.             /* print group memberships */
  366.             usCount = (USHORT)ulAvailable;
  367.             pGroups = (struct group_info_0 *)pBuffer;
  368.             printf("\nUser \"%s\" belongs to the following groups:\n",
  369.                     strupr(pszUser));
  370.             while (usCount)
  371.             {
  372.                 printf("\t%s\n", pGroups -> grpi0_name);
  373.                 --usCount;
  374.                 ++pGroups;
  375.             }
  376.  
  377.             /*
  378.              * print logon assignment information...get only
  379.              * assignments to files aliases.
  380.              */
  381.             usRc = Net32UserGetLogonAsn((char *)NULL,
  382.                                         pszUser,
  383.                                         200,
  384.                                         ALIAS_TYPE_FILE,
  385.                                         pBuffer,
  386.                                         4096,
  387.                                         &ulAvailable,
  388.                                         (UCHAR *)&ErrStatus);
  389.  
  390.             if (usRc == ERROR_PATH_NOT_FOUND)
  391.                 printf("The domain control database directory "\
  392.                        "for \"%s\" was not found.\n", strupr(pszUser));
  393.             else if (usRc)
  394.                 Error_Message(usRc, "Net32UserGetLogonAsn");
  395.             else
  396.             {
  397.                 usCount = ((struct logon_asn_info_200 *)pBuffer) -> lai200_count;
  398.                 if (usCount == 0)
  399.                     printf("\n\"%s\" has no files logon assignments.\n",
  400.                             strupr(pszUser));
  401.                 else
  402.                 {
  403.                     pLogList =
  404.                      (struct logon_asn_list_200 *)(pBuffer + sizeof(struct logon_asn_info_200));
  405.  
  406.                     printf("\nLogon assignments for user \"%s\":\n",
  407.                            strupr(pszUser));
  408.                     while (usCount)
  409.                     {
  410.                         printf("\t%-8.8s   %s\n",
  411.                                 pLogList -> lal200_device,
  412.                                 pLogList -> lal200_global_name);
  413.  
  414.                         ++pLogList;
  415.                         --usCount;
  416.                     }
  417.                 }
  418.             }
  419.         }
  420.     }
  421.     DosFreeMem((PVOID)pBuffer);
  422.     return;
  423. }
  424.  
  425. VOID
  426. Syntax()
  427. {
  428.     printf("Usage:\n");
  429.     printf("   The USER32 sample program creates a new user (password same as \n");
  430.     printf("   userid, assigns user default resource domain, adds a file alias \n");
  431.     printf("   as logon assignment (if desired).  Displays information about the \n");
  432.     printf("   user.  Deletes the user. \n\n");
  433.     printf("Syntax:\n");
  434.     printf("   USER32 <username> /ADD <//defaultresourcedomain> [aliasname] \n");
  435.     printf("      -- Creates <username> user account, adds user default resource domain,\n");
  436.     printf("         adds <username> to ""USER"" group of resource domain, and adds alias \n");
  437.     printf("         as logon assignment (if specified). \n");
  438.     printf("   USER32 <username>  \n");
  439.     printf("      -- Displays information about <username>  \n");
  440.     printf("   USER32 <username> /DEL \n");
  441.     printf("      -- deletes the user account for <username>. \n");
  442.     DosExit(EXIT_PROCESS, 1);
  443. }
  444.