Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a Process with an Impersonated User
Message
From
17/10/2005 08:14:17
 
 
To
17/10/2005 07:48:51
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01059576
Message ID:
01059577
Views:
19
(Continued from previous message)

On the other hand the program below works fine, since it uses the CreateProcess api call, but must be noted that CreateProcess runs the created task under the originating login id and not under the impersonated user, so the right choice should be CreateProcessAsUser (or CreateProcessWithLogonW), to accomplish what I need.

It can be read in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp:

CreateProcess

The CreateProcess function creates a new process and its primary thread. The new process runs the specified executable file in the security context of the calling process.

If the calling process is impersonating another user, the new process uses the token for the calling process, not the impersonation token. To run the new process in the security context of the user represented by the impersonation token, use the CreateProcessAsUser or CreateProcessWithLogonW function.

Fernando
* ===========================================================================
*                              Test Parameters
* ===========================================================================

* To be used by LogonUser

 cLoginId      = "myuserlogin"
 cPassword     = "mypassword" 
 cDomain       = "mydomain"   

* To be used by CreateProcessAsUser

 cCommandLine = "C:\Temp\Test.Bat"


* ===========================================================================
*                             Get Test Parameters
* ===========================================================================

 bAbortTest = .F.

 oGetLoginInfo = CreateObj ("frmGetLoginInfo")

 oGetLoginInfo.Show ()

 Clear Events

 If bAbortTest
    Return
 endif

* ===========================================================================
*                            Impersonation Stuff
* ===========================================================================

* dwLogonProvider:

 #Define LOGON32_PROVIDER_DEFAULT  0
 #Define LOGON32_PROVIDER_WINNT50  3
 #Define LOGON32_PROVIDER_WINNT40  2
 #Define LOGON32_PROVIDER_WINNT35  1

* dwLogonType:

 #Define LOGON32_LOGON_INTERACTIVE 2
 #Define LOGON32_LOGON_NETWORK     3
 #Define LOGON32_LOGON_BATCH       4
 #Define LOGON32_LOGON_SERVICE     5

 Declare Short LogonUser               In AdvApi32 String  lcNewUserName  , ;
                                                   String  lcDomainName   , ;
                                                   String  lcPassWord     , ;
                                                   Integer lnLogonType    , ;
                                                   Integer lnLogonProvider, ;
                                                   Integer @lnUserHandle

 Declare Short ImpersonateLoggedOnUser In AdvApi32 Integer lnUserHandle

* ===========================================================================
*                         Logs User on and Impersonates
* ===========================================================================

 nUserHandle = 0

 nSuccess    = LogonUser (cLoginId                 , ;
                          cDomain                  , ;
                          cPassword                , ;
                          LOGON32_LOGON_INTERACTIVE, ;
                          LOGON32_PROVIDER_DEFAULT , ;
                          @nUserHandle             )

 If nSuccess = 0
    Messagebox ("Error in LogonUser")
    Return
 endif

 nSuccess = ImpersonateLoggedOnUser (nUserHandle)

 If nSuccess = 0
    Messagebox ("Error in ImpersonateLoggedOnUser")
    Return
 endif

* ===========================================================================
*                          Checks User Impersonation
* ===========================================================================

 nRetVal        = 0
 lpUserIDBuffer = Space (25)
 nBufferSize    = 25        

 Declare Integer GetUserName In Win32API String  @lpUserIDBuffer, ;
                                         Integer @nBufferSize

 nRetVal = GetUserName (@lpUserIDBuffer, @nBufferSize)

 cLogUserId = Left (lpUserIDBuffer, nbuffersize-1)

 MessageBox ("Impersonated user is: " + AllTrim (cLogUserId))

* ===========================================================================
*                               Task Creation
* ===========================================================================

 #Define NORMAL_PRIORITY_CLASS     32
 #Define IDLE_PRIORITY_CLASS       64
 #Define HIGH_PRIORITY_CLASS      128
 #Define REALTIME_PRIORITY_CLASS 1600

 Declare Integer CreateProcess In Kernel32 Integer lpApplicationName  , ;
                                           String  lpCommandLine      , ;
                                           Integer lpProcessAttributes, ;
                                           Integer lpThreadAttributes , ;
                                           Integer bInheritHandles    , ;
                                           Integer dwCreationFlags    , ;
                                           Integer lpEnvironment      , ;
                                           Integer lpCurrentDirectory , ;
                                           String @lpStartupInfo      , ;
                                           String @lpProcessInformation

 cStart           = Long2Str  (68) + Replicate (Chr (0), 64)
 cProcess_Info    = Replicate (Chr (0), 16)
 cApplicationName = Chr (0)
 cCommandLine     = cCommandLine + Chr (0)

 nRetCode         = CreateProcess (0                    , ;
                                   cCommandLine         , ;
                                   0                    , ;
                                   0                    , ;
                                   1                    , ;
                                   NORMAL_PRIORITY_CLASS, ;
                                   0                    , ;
                                   0                    , ;
                                   @cStart              , ;
                                   @cProcess_Info)

 If nRetCode = 0 
    Messagebox ("Error in CreateProcess")
    Return
 endif

 Messagebox ("Process successfully created!")

 Return

* ===========================================================================

 Function Long2Str
*-------- --------
 LParameters pLongVal

 Private I, cRetStr, nLongVal

 cRetStr  = ""
 nLongVal = pLongVal

 For I = 24 To 0 Step -8

     cRetStr  = Chr (Int (nLongVal / (2^I))) + cRetStr
     nLongVal = Mod (     nLongVal,  (2^I))

 EndFor

 Return cRetStr

 EndFunc 

* ===========================================================================

 Define Class frmGetLoginInfo as Form
*------ ----- --------------- -- ----

        Top         =   0
        Left        =   0
        Height      = 160
        Width       = 330
        AutoCenter  =  .T.
        ControlBox  =  .T.
        MaxButton   =  .F.
        MinButton   =  .F.
        ZoomBox     =  .F.
        SizeBox     =  .F.
        ShowTips    =  .T.
        ShowWindow  =   0
        WindowType  =   1
        Caption     = " Please Enter Impersonation Info"

        Add Object lblLoginId   as Label         with Top          =  15        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Login Id:"
 
        Add Object txtLoginId as TextBox         with Top          =  10        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblPassword  as Label         with Top          =  45        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Password:"
 
        Add Object txtPassword as  TextBox       with Top          =  40        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =  12        , ;
                                                      PasswordChar =  "*"       , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblDomain    as Label         with Top          =  75        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Domain"
 
        Add Object txtDomain    as TextBox       with Top          =  70        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblCommand   as Label         with Top          = 105        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Command:"
 
        Add Object txtCommand  as TextBox        with Top          = 100        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    = 250        , ;
                                                      Value        = Space (250)

        Add Object cmdOK        as CommandButton with Top          = 130        , ;
                                                      Left         = 165        , ;
                                                      Height       =  25        , ;
                                                      Width        =  75        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "\<OK"    

        Add Object cmdCancel    as CommandButton with Top          = 130        , ;
                                                      Left         = 245        , ;
                                                      Height       =  25        , ;
                                                      Width        =  75        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Cancel       = .T.        , ;
                                                      Caption      = "\<Cancel"

        Procedure cmdCancel.Click
*                 --------- -----
                  bAbortTest = .T.

                  ThisForm.Release
        EndProc

        Procedure cmdOK.Click
*                 ----- -----
                  cLoginId     = AllTrim (ThisForm.txtLoginId.Value )
                  cPassword    = AllTrim (ThisForm.txtPassword.Value)
                  cDomain      = AllTrim (ThisForm.txtDomain.Value  )
                  cCommandLine = AllTrim (ThisForm.txtCommand.Value )

                  bAbortTest = .F.

                  ThisForm.Release
        EndProc

        Procedure Activate
*                 --------
                  Set Cursor On
        EndProc

        Procedure Init
*                 ----
                  ThisForm.txtLoginId.Value  = cLoginId     
                  ThisForm.txtPassword.Value = cPassword    
                  ThisForm.txtDomain.Value   = cDomain      
                  ThisForm.txtCommand.Value  = cCommandLine 
        EndProc

        Procedure QueryUnload
*                 -----------
                  ThisForm.cmdCancel.Click

                  NoDefault

                  Return .T.
        EndProc

 EndDefine

* ===========================================================================
Previous
Reply
Map
View

Click here to load this message in the networking platform