NetUserValidate2() Usage Sample Program

ID: Q62561


The information in this article applies to:
  • Microsoft LAN Manager, versions 2.0, 2.1, 2.1a, 2.2


SUMMARY

The following program demonstrates the correct usage of the NetUserValidate2() function call:


    /* This program demonstrates the usage of NetUservalidate2().

      Compile option: CL -C -W3 -Zi validat2.c
      Link option: Link /CO validat2,,,os2+netapi;
      Usage:
               validate <username> <password>

      The program fills in the workstation name by making a call to
      NetWkstaGetInfo(). A value of NULL in the workstation field will
      mean local workstation.

      This software is provided for demonstration purposes only.
      Microsoft makes no warranty, either express or implied, as to its
      usability in any given situation.

   */ 

   #define INCL_DOS
   #define INCL_NOPM
   #include <os2.h>

   #include <netcons.h>
   #include <neterr.h>
   #include <access.h>
   #include <wksta.h>
   #include <lan.h>

   #include <stdio.h>
   #include <malloc.h>
   #include <string.h>
   #include <ctype.h>

   #define EXTRA_USER_INFO     200
   #define EXTRA_WKSTA_INFO    70
   #define TOTAL_DOMAIN_NMS    32

   typedef struct user_info_1 USER_INFO_1;
   typedef struct wksta_info_10 WKSTA_INFO_10;
   typedef struct user_logon_req_1  User_Logon_Req_1;
   typedef struct user_logon_info_1 User_Logon_Info_1;

   typedef char far        * CHFP;
   typedef char near       * CHNP;
   typedef  unsigned short US;

   void MtrCpy ( char  *dest, char  far *src);

   /* MtrCpy() function will copy text from far buffer into near buffer.
    */ 

   void MtrCpy ( char  *dest, char  far *src)
   {
       char  *pDest = dest;
       char far *pSrc = src;

       // Copy from far buffer into near buffer.

       while ( *pSrc)
           *pDest++ = *pSrc++;
       *pDest = '\0';

       // Uppercase the text. Password must be uppercased.

       pDest = dest;
       while (*pDest) {
           if ( isalpha( (int) *pDest))
               *pDest = (char) toupper( (int) *pDest);
           pDest++;

       }


   return;
   }

   void main(argc, argv)
   int argc;
   char *argv[];
   {

       char   *pUserInfo;          /* Pointer to user_info_10.     */ 
       WKSTA_INFO_10 *pWkstaInfo;  /* Pointer to wksta_info_10.    */ 

       User_Logon_Info_1 *pUserLogonInfo;
       User_Logon_Req_1  *pUserReq;

       unsigned int netAPIStatus;         /* Status from LANMAN calls.  */ 
       unsigned short int returnedBuffSz; /* Size of buffer from LANMAN
                                             calls. */ 

       if ( argc < 2 ) {
           puts("USAGE: username password");
           return;
       }

           /* Allocate buffer for level 10 workstation information. */ 
       if ( (pWkstaInfo = (WKSTA_INFO_10 *)calloc(1, sizeof(WKSTA_INFO_10)
                                                  + EXTRA_WKSTA_INFO
                                                  + TOTAL_DOMAIN_NMS))
                                                  == NULL) {

           puts("Error: Unable to allocate workstation info buffer");
           return;
       }

           /* Get the workstation information. */ 
       if ( netAPIStatus = NetWkstaGetInfo(NULL, 10, (CHFP) pWkstaInfo,
                                           (sizeof(WKSTA_INFO_10) +
                                            EXTRA_WKSTA_INFO +
                                            TOTAL_DOMAIN_NMS),
                                            &returnedBuffSz) ) {

           printf("Error: NetWkstaGetInfo() returned %u\n");
           free(pWkstaInfo);
           return;
       }

           /* Allocate buffer for level 1 user logon information. */ 
       if ( (pUserReq = (User_Logon_Req_1 *)calloc(1, sizeof(USER_INFO_1)
                                                  + EXTRA_USER_INFO))
                                                  == NULL) {

           puts("Error: Unable to allocate user logon buffer");
           return;
       }

           /* Initialize user information with user ID from workstation
              call.
            */ 


       MtrCpy( pUserReq->usrreq1_name,   argv[1]);   // Username
       MtrCpy(pUserReq->usrreq1_password, argv[2]);  // Password
                                                      // Workstation Name
       pUserReq->usrreq1_workstation =  pWkstaInfo->wki10_computername;

       free(pWkstaInfo);          /* Information not needed anymore.  */ 

       printf("\nValidating user %s, password %s\n",
              pUserReq->usrreq1_name, pUserReq->usrreq1_password);

           /* Call LANMAN to validate ID and password. */ 
       netAPIStatus = NetUserValidate2(NULL, 1, (CHFP) pUserReq,
                                    (sizeof(USER_INFO_1) + EXTRA_USER_INFO)
                                    (US) NULL, &returnedBuffSz);
       if ( netAPIStatus ) {              /* Report results of call.    */ 
           printf("\n\tError calling NetUserValidate, status = %u\n",
                  netAPIStatus);
       }
       else {
           /* While making the NetUserValidate2() call, the pointer
              pUserReq must point to a user_info_req_1 type structure and
              on a successful return, the same pointer now points to the
              user_logon_info_1 type structure.
           */ 
           pUserLogonInfo = (User_Logon_Info_1 *) pUserReq;
           printf("Validatation Code: %d\n", pUserLogonInfo->usrlog1_code);

           printf("\n\tUser %s validated, has privilege level",
               pUserLogonInfo->usrlog1_eff_name);

           switch ( pUserLogonInfo->usrlog1_priv ) {

           case USER_PRIV_GUEST:
               puts(" GUEST.");
               break;

           case USER_PRIV_USER:
               puts(" USER.");
               break;

           case USER_PRIV_ADMIN:
               puts(" ADMIN.");
               break;

           default:
               printf(" unknown privilege level = %d\n",
                   pUserLogonInfo->usrlog1_priv);
               break;

           }   /* End-switch on user privilege level. */ 

       }   /* End-if on netAPIStatus. */ 

       free(pUserInfo);        /* Information not needed anymore.  */ 
       return;
   } 

Additional query words: prodlm 2.00 2.10 2.10a 2.20

Keywords :
Version : :2.0,2.1,2.1a,2.2
Platform :
Issue type :


Last Reviewed: November 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.