faqts : Computers : Programming : Languages : PHP : Common Problems : Security

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 5 people (60%) answered Yes
Recently 3 of 5 people (60%) answered Yes

Entry

How can I get the login name of a user under a NT4 session ?

Jan 11th, 2004 19:19
Alan Grainger, Renaud Mavre, http://codewalkers.com/seecode/111.html


This will probably help you.  I haven't tested this code, but it appears
to be what you are looking for.

<?

//Copyright 2002 Scott Dial
//scott@scottdial.com
//
//This code is free to modify, use, abuse, or whatever you like. It'd be
interesting though to hear what you are using it for, so shoot me an
email if you use this snippet.

//These values are pulled straight from winbase.h from the platform sdk

define("LOGON32_LOGON_INTERACTIVE", 2);
define("LOGON32_LOGON_NETWORK",     3);
define("LOGON32_LOGON_BATCH",       4);
define("LOGON32_LOGON_SERVICE",     5);

define("LOGON32_PROVIDER_DEFAULT",  0);
define("LOGON32_PROVIDER_WINNT35",  1);
define("LOGON32_PROVIDER_WINNT40",  2);
define("LOGON32_PROVIDER_WINNT50",  3);

//These aren't actually in winbase.h but are accurate
define("LOGON32_DOMAIN_LOCAL", ".");
define("LOGON32_DOMAIN_ALL",   0);

if(!extension_loaded('win32api')) {
    if (!dl('win32api')) {
        die("Couldn't load win32api!");
    }
}

function NT_Validate_User($user, $domain, $pass)
{
    w32api_register_function("kernel32.dll", "LocalAlloc", "long");
    w32api_register_function("kernel32.dll", "LocalFree", "long");
    w32api_register_function("kernel32.dll", "CloseHandle", "bool");
    w32api_register_function("advapi32.dll", "LogonUserA", "bool");

    $cleanup = w32api_register_function("deref.dll", "deref", "long");

    $pHandle = LocalAlloc(0, 4); //Pointer to a HANDLE

    $test = LogonUserA($user,
                      $domain,
                      $pass,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      $pHandle);

    if($test != 0)
    {
        return 1;
        if($cleanup)
        {
            $handle = deref($pHandle);
            CloseHandle($handle);
        }
        LocalFree($pHandle);
    } else {
        return 0;
    }
}

?>