Authenticate User: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Username/Password Authenticate Function)
 
Line 103: Line 103:
== Resources and Links ==
== Resources and Links ==
* NSIS forum [http://forums.winamp.com/showthread.php?s=&threadid=265387 thread]
* NSIS forum [http://forums.winamp.com/showthread.php?s=&threadid=265387 thread]
* [http://nsis.sourceforge.net/XtInfoPlugin_plug-in XtInfo Plugin]
* [http://nsis.sourceforge.net/System_plug-in_readme System Plugin]
* [http://nsis.sourceforge.net/System_plug-in_readme System Plugin]



Revision as of 21:31, 16 February 2007

Author: HotButteredSoul (talk, contrib)


NSIS forum thread

Description

This is a function to validate a Windows Username and Password.

Function

I have not written a macro for this function.

Usage

Use the following code in your NSIS installer to find out if a domain, username, and password authenticate:

Push "a_username"
Push "ADOMAIN"
Push "a_password"
Call MSWAuthenticate
Pop $0 ; = "success" on succes, or "Logon failure: ..." otherwise.

Code

#
# MSWAuthenticate.nsh - by HotButteredSoul
#
# Function for checking to see if a Microsoft Windows Username/password
# pair authenticate.
#
 
!ifndef _MSWAuthenticate_nsh
!define _MSWAuthenticate_nsh
 
#
# MSWAuthenticate - authenticates username/password pair
#
# Example:
#
# Push "bob.username"
# Push "ADOMAIN"
# Push "bobs.password"
# Call MSWAuthenticate
# Pop $0 ; = "success" on succes, or "Logon failure: ..." otherwise.
#
# Uses advapi32.lib LogonUserA
#
Function MSWAuthenticate
    Exch $0 ; password (IN)
    Exch
    Exch $1 ; Domain (IN) / "success" (OUT)
    Exch 2
    Exch $2 ; Username (IN)
    Push $3 ; LogonUserA return code
    Push $4 ; GetLastError() code
 
    ; LOGON32_LOGON_NETWORK = 3
    ; LOGON32_PROVIDER_DEFAULT = 0
    System::Call "advapi32::LogonUserA(t r2, t r1, t r0, i 3, i 0, *i) i .r3 ?e"
    Pop $4 ; the ?e flag from System::Call pushes the result of GetLastError() onto the stack.
    IntCmp 0 $3 reject ; return value of 0 is failure.
    StrCpy $1 "success"
    GoTo done
 
    reject:
    IntCmp   87 $4 ERROR_LOGON_FAILURE
    IntCmp 1326 $4 ERROR_LOGON_FAILURE
    IntCmp 1327 $4 ERROR_ACCOUNT_RESTRICTION
    IntCmp 1328 $4 ERROR_INVALID_LOGON_HOURS
    IntCmp 1329 $4 ERROR_INVALID_WORKSTATION
    IntCmp 1330 $4 ERROR_PASSWORD_EXPIRED
    IntCmp 1331 $4 ERROR_ACCOUNT_DISABLED
    ;an error of some other sort
        StrCpy $1 "Logon failure: $4"
        GoTo done
    ERROR_LOGON_FAILURE:
        StrCpy $1 "Logon failure: unknown user name or bad password."
        GoTo done
    ERROR_ACCOUNT_RESTRICTION:
        StrCpy $1 "Logon failure: user account restriction."
        GoTo done
    ERROR_INVALID_LOGON_HOURS:
        StrCpy $1 "Logon failure: account logon time restriction violation."
        GoTo done
    ERROR_INVALID_WORKSTATION:
        StrCpy $1 "Logon failure: user not allowed to log on to this computer."
        GoTo done
    ERROR_PASSWORD_EXPIRED:
        StrCpy $1 "Logon failure: the specified account password has expired."
        GoTo done
    ERROR_ACCOUNT_DISABLED:
        StrCpy $1 "Logon failure: account currently disabled."
        GoTo done
 
    done:
    Pop $4
    Pop $3
    Pop $2
    Pop $0
    Exch $1
FunctionEnd
!endif ; _MSWAuthenticate_nsh


Resources and Links

API Functions used: