home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 April / APC443.iso / features / grpware / coldfus / coldfusi.exe / data1.cab / CFX_Wizard / examples / nt_userdb / request.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-08  |  7.4 KB  |  239 lines

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CFX_NT_USERDB - Cold Fusion custom tag for adding and deleting
  4. //                   NT user accounts.
  5. //
  6. // Copyright 1996. All Rights Reserved.
  7. //
  8. // This tag adds and delete users from the account database of the
  9. // local NT system. When adding a user a privilege level (Guest, 
  10. // User, or Administrator) and list of groups to add the user to
  11. // may be specified.
  12. //
  13. // Example Uses:
  14. //
  15. //    <CFX_NT_USERDB ACTION="ADD" 
  16. //        USERNAME="Foo" PASSWORD="Bar"
  17. //        PrivilegeS="User"
  18. //        GROUPS="Users, Power Users">
  19. //
  20. //  <CFX_NT_USERDB ACTION="DELETE" 
  21. //        USERNAME="Foo">
  22. //
  23. //
  24. // NOTE: If you wish to experiment with this tag from within
  25. //         Cold Fusion you need to add it to the registry of
  26. //         custom tags using the Cold Fusion Administrator.
  27. //         If you want to run the tag from within the debugger
  28. //       you should set the path of the tag to the \Debug
  29. //         directory of the tag's project directory.
  30. //
  31.  
  32.  
  33. #include "stdafx.h"        // Standard MFC libraries
  34. #include "afxpriv.h"    // MFC Unicode conversion macros
  35. #include "cfx.h"        // CFX Custom Tag API
  36. #include "lmaccess.h"    // User-database access functions
  37.  
  38.  
  39. // Constants
  40. #define TAG_ERROR_HEADER    "Error occurred in tag CFX_NT_USERDB"
  41.  
  42.  
  43. // Forward declarations of implementation functions
  44. LPCSTR GetRequiredAttribute( CCFXRequest* pRequest, LPCSTR lpszParamName ) ;
  45. void AddNTUser( CCFXRequest* pRequest ) ;
  46. void RemoveNTUser( CCFXRequest* pRequest ) ;
  47. DWORD GetPrivileges( CCFXRequest* pRequest, CString strPrivileges ) ;
  48. void GetSIDForUser( CCFXRequest* pRequest, LPCSTR lpszUsername,
  49.                     UCHAR* buffSID, DWORD dwBuffSIDSize ) ;
  50. void VERIFY_NETAPI( CCFXRequest* pRequest, 
  51.     CString strFunction, NET_API_STATUS status ) ;
  52.  
  53.  
  54. // Main tag function ///////////////////////////////////////////////////
  55.  
  56. void ProcessTagRequest( CCFXRequest* pRequest ) 
  57. {
  58.     try
  59.     {
  60.         // Determine the action requested & call the 
  61.         // appropriate helper function
  62.         CString strAction = GetRequiredAttribute( pRequest, "ACTION" ) ;
  63.         if ( !strAction.CompareNoCase( "ADD" ) )
  64.             AddNTUser( pRequest ) ;
  65.         else if ( !strAction.CompareNoCase( "REMOVE" ) )
  66.             RemoveNTUser( pRequest ) ;
  67.         else
  68.         {
  69.             CString strErr =
  70.                 "An invalid ACTION attribute (" + strAction + ") "
  71.                 "was specified. Valid attributes are ADD and REMOVE." ;
  72.             pRequest->ThrowException( 
  73.                 TAG_ERROR_HEADER, strErr ) ;
  74.         }        
  75.     }
  76.  
  77.     // Catch Cold Fusion exceptions & re-raise them
  78.     catch( CCFXException* e )
  79.     {
  80.         pRequest->ReThrowException( e ) ;
  81.     }
  82.     
  83.     // Catch ALL other exceptions and throw them as 
  84.     // Cold Fusion exceptions (DO NOT REMOVE! -- 
  85.     // this prevents the server from crashing in 
  86.     // case of an unexpected exception)
  87.     catch( ... )
  88.     {
  89.         pRequest->ThrowException( 
  90.             TAG_ERROR_HEADER,
  91.             "Unexpected error occurred while processing tag." ) ;
  92.     }
  93. }
  94.  
  95.  
  96. // Add the user (using the specified USERNAME, PASSWORD, ATTRIBUTES,
  97. // and GROUPS) to the NT user database
  98. void AddNTUser( CCFXRequest* pRequest )
  99. {
  100.     // Get attribute values
  101.     CString strUsername = GetRequiredAttribute( pRequest, "USERNAME" ) ;
  102.     CString strPassword = GetRequiredAttribute( pRequest, "PASSWORD" ) ;
  103.     CString strPrivileges = GetRequiredAttribute( pRequest, "PrivilegeS" ) ;
  104.     DWORD dwPrivileges = GetPrivileges( pRequest, strPrivileges ) ;
  105.     CString strGroups = pRequest->GetAttribute( "GROUPS" ) ;
  106.  
  107.     // Create the user
  108.     USES_CONVERSION ;
  109.     USER_INFO_1 userInfo ;
  110.     userInfo.usri1_name = A2W(strUsername) ;
  111.     userInfo.usri1_password = A2W(strPassword) ;
  112.     userInfo.usri1_password_age = 0 ;
  113.     userInfo.usri1_priv = dwPrivileges ;
  114.     userInfo.usri1_home_dir = NULL ;
  115.     userInfo.usri1_comment = NULL ;
  116.     userInfo.usri1_flags = UF_SCRIPT ;
  117.     userInfo.usri1_script_path = NULL ;
  118.     VERIFY_NETAPI( pRequest, "NetUserAdd", 
  119.         NetUserAdd( NULL, 1, (LPBYTE)&userInfo,    NULL ) ) ;    
  120.  
  121.     // Get the user's security id
  122.     DWORD dwSIDSize = 1024 ;
  123.     UCHAR buffSID[1024] ;
  124.     GetSIDForUser( pRequest, strUsername, buffSID, dwSIDSize ) ;
  125.  
  126.     // Add the user to the requested groups
  127.     LPSTR lpszGroups = strGroups.GetBuffer(0) ;
  128.     LPSTR lpszToken = strtok( lpszGroups, "," ) ;
  129.     while ( lpszToken != NULL )
  130.     {
  131.         CString strGroup(lpszToken) ; 
  132.         strGroup.TrimLeft(); strGroup.TrimRight();
  133.         VERIFY_NETAPI( pRequest, "NetLocalGroupAddMember",
  134.             NetLocalGroupAddMember( NULL, A2W(strGroup), (PSID)buffSID ) ) ;    
  135.         lpszToken = strtok( NULL, "," );
  136.        }
  137. }
  138.  
  139.  
  140. // Remove the user (specified by USERNAME) from the NT user database
  141. void RemoveNTUser( CCFXRequest* pRequest )
  142. {
  143.     // Get attribute values
  144.     CString strUsername = GetRequiredAttribute( pRequest, "USERNAME" ) ;
  145.  
  146.     // Remove the user
  147.     USES_CONVERSION ;
  148.     VERIFY_NETAPI( pRequest, "NetUserDel",
  149.         NetUserDel( NULL, A2W(strUsername) ) ) ;
  150. }
  151.  
  152.  
  153. // Check the return code and throw an exception if an 
  154. // error occurred while calling the function
  155. void VERIFY_NETAPI( CCFXRequest* pRequest, CString strFunction, 
  156.                     NET_API_STATUS status )
  157. {
  158.     if ( status != 0 )
  159.     {
  160.         CString strErr ;
  161.         strErr.Format( 
  162.             "Unexpected NETAPI error number %ld occurred while "
  163.             "calling function %s.", status, strFunction ) ;
  164.         pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;
  165.     }
  166. }
  167.  
  168.  
  169. // Retreive the security id (SID) for the passed username
  170. void GetSIDForUser( CCFXRequest* pRequest, LPCSTR lpszUsername,
  171.                     UCHAR* buffSID, DWORD dwBuffSIDSize )
  172. {
  173.     DWORD dwBuffSize = 1024 ;
  174.     char buffRefDomain[1024] ;
  175.    
  176.     SID_NAME_USE sidType ;
  177.  
  178.     DWORD dwError = LookupAccountName(
  179.         NULL,            // address of string for system name
  180.         lpszUsername,    // address of string for account name
  181.         buffSID,        // address of security identifier
  182.         &dwBuffSIDSize,    // address of size of security identifier
  183.         buffRefDomain,    // address of string for referenced domain 
  184.         &dwBuffSize,    // address of size of domain string
  185.         &sidType          // address of SID-type indicator
  186.     );
  187.  
  188.     if ( dwError != ERROR_SUCCESS && dwError != ERROR_INVALID_FUNCTION )
  189.     {
  190.         CString strErr ;
  191.         strErr.Format( 
  192.             "Unexpected Windows NT error number %ld occurred while "
  193.             "calling function LookupAccountName.", ::GetLastError() ) ;
  194.         pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;
  195.     }
  196. }
  197.  
  198.  
  199. // Convert a string representation of privileges to the 
  200. // appropriate constant value
  201. DWORD GetPrivileges( CCFXRequest* pRequest, CString strPrivileges )
  202. {
  203.     if ( !strPrivileges.CompareNoCase("GUEST") )
  204.         return USER_PRIV_GUEST ;
  205.     else if ( !strPrivileges.CompareNoCase("USER") )
  206.         return USER_PRIV_USER ;
  207.     else if ( !strPrivileges.CompareNoCase("ADMINISTRATOR") ) 
  208.         return USER_PRIV_ADMIN ;
  209.     else
  210.     {
  211.         CString strErr =
  212.             "An invalid PrivilegeS attribute (" + strPrivileges + ") "
  213.             "was specified. Valid attributes are GUEST, USER, and "
  214.             "ADMINISTRATOR." ;
  215.         pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;
  216.     }
  217.     return (0) ; 
  218. }
  219.  
  220.  
  221. // Get the value for the passed attribute (throw an exception
  222. // if the attribute was not passed to the tag)
  223. LPCSTR GetRequiredAttribute( CCFXRequest* pRequest, LPCSTR lpszAttribName ) 
  224. {
  225.     // Verify that the attribute exists (throw an exception
  226.     // if it does not)
  227.     if ( !pRequest->AttributeExists(lpszAttribName) )
  228.     {
  229.         CString strErr = 
  230.             "The required attribute " + CString(lpszAttribName) +
  231.             " was not passed to the tag. " ;
  232.         pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;    
  233.     }
  234.  
  235.     // Return the attribute
  236.     return pRequest->GetAttribute( lpszAttribName ) ;    
  237. }
  238.  
  239.