Recent

Author Topic: User Domain  (Read 848 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 553
User Domain
« on: February 06, 2025, 11:08:24 am »
Hello,

how should i check for the user's domain name. I tried using GetTokenInformation but can't quite figure it out. I would like to grant a User, in a certain Domain, permission to do specific things.

thanks for any help, i hope i posted this in the right Forum Section.

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: User Domain
« Reply #1 on: February 06, 2025, 11:19:17 am »
You could use  GetEnvironmentVariable  to check the value of  USERDOMAIN  on Windows,  but note that (like any other environment variable) it can easily be changed/overwritten by the user.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 553
Re: User Domain
« Reply #2 on: February 06, 2025, 12:14:29 pm »
i asked the Network Admin and he told me that the User's are not able to Edit the Environment Variables. So i should be safe when using GetEnvironmentVariable? Its Windows only currently so that wont be a problem either.

Zvoni

  • Hero Member
  • *****
  • Posts: 2897
Re: User Domain
« Reply #3 on: February 06, 2025, 12:50:56 pm »
What about querying LDAP?

https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol

Quote
dn: cn=John Doe,dc=example,dc=com
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1479
    • Lebeau Software
Re: User Domain
« Reply #4 on: February 07, 2025, 12:20:36 am »
I tried using GetTokenInformation but can't quite figure it out.

What did you try that is not working?

Perhaps you can use GetTokenInformation() to get the user's SID, and then use LookupAccountSid() to get the domain where that SID is found.

i hope i posted this in the right Forum Section.

Not really.  This question has nothing to do with network/web programming. The Programming > Operating Systems > Windows forum would probably have been more appropriate.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 553
Re: User Domain
« Reply #5 on: February 07, 2025, 08:21:35 am »
i do get an error i can't figure out. Compiler saying: "Error: Variable identifier expected" when i try to use "LookupAccountSid". what am i doing wrong ?

Code: Pascal  [Select][+][-]
  1. function GetDomainName: string;
  2. var
  3.    TokenHandle: THandle;
  4.    TokenInformation: Pointer;
  5.    TokenInfoLength: DWORD;
  6.    TokenInfoClass: TTokenInformationClass;
  7.    TokenUser: TOKEN_USER;
  8.    DomainName: array[0..255] of Char;
  9.    AccountName: array[0..255] of Char;
  10.    SIDNameUse: SID_NAME_USE;
  11. begin
  12.    Result:= '';
  13.    try
  14.       // Open the process token
  15.       if not OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle) then begin
  16.          //RaiseLastOSError;
  17.          Exit;
  18.       end;
  19.       // Query the required token info length
  20.       TokenInfoLength:= 0;
  21.       GetTokenInformation(TokenHandle, TokenInfoClass, nil, 0, TokenInfoLength);
  22.       // Allocate memory for the token info structure
  23.       GetMem(TokenInformation, TokenInfoLength);
  24.       try
  25.          // Query the token information
  26.          if not GetTokenInformation(TokenHandle, TokenInfoClass, TokenInformation, TokenInfoLength, TokenInfoLength) then begin
  27.             //RaiseLastOSError;
  28.             Exit;
  29.          end;
  30.          // Retrieve SID
  31.          TokenUser:= PTokenUser(TokenInformation)^;
  32.          // Look up the account associated with the SID
  33.          if not LookupAccountSid(nil, TokenUser.User.Sid, AccountName, SizeOf(AccountName),
  34.             DomainName, SizeOf(DomainName), SIDNameUse) then  //<-- Variable Identifier expected (SIDNameUse)
  35.          begin
  36.             //RaiseLastOSError;
  37.             Exit;
  38.          end;
  39.          ShowMessage('Domain: ' + string(DomainName));
  40.          ShowMessage('Account: ' + string(AccountName));
  41.          Result:= String(DomainName)
  42.       finally
  43.          FreeMem(TokenInformation);
  44.          CloseHandle(TokenHandle);
  45.       end;
  46.    except
  47.       Result:= false;
  48.    end;
  49. end;
  50.  

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: User Domain
« Reply #6 on: February 07, 2025, 08:40:38 am »
The names returned by LookupAccountSid can be of any length; the account & domain name buffers may be too small, in which case the function writes the required lengths into  cchName  and  cchReferencedDomainName.
These parameters are pointers (the Windows unit declares them as either  LPDWORD  or  var DWORD), and as such they require an actual, addressable variable, which  SizeOf  doesn't provide.

I'm more surprised that the compiler doesn't already complain at  SizeOf(AccountName),  but I guess there's no requirement for the compiler to check argument types from left to right.

TRon

  • Hero Member
  • *****
  • Posts: 4140
Re: User Domain
« Reply #7 on: February 07, 2025, 08:49:48 am »
i do get an error i can't figure out. Compiler saying: "Error: Variable identifier expected" when i try to use "LookupAccountSid". what am i doing wrong ?
Too little info.

take a pick
Code: Pascal  [Select][+][-]
  1. function LookupAccountSid(lpSystemName:LPCWSTR; Sid:PSID; Name:LPWSTR; cbName:LPDWORD; ReferencedDomainName:LPWSTR;cbReferencedDomainName:LPDWORD; peUse:PSID_NAME_USE):WINBOOL; external 'advapi32' name 'LookupAccountSidW';
  2. function LookupAccountSid(lpSystemName: PChar; Sid: PSID; Name: PChar; var cbName: DWORD; ReferencedDomainName: PChar; var cbReferencedDomainName: DWORD; var peUse: SID_NAME_USE): BOOL;external 'advapi32' name 'LookupAccountSidA';
  3. function LookupAccountSid(lpSystemName:LPCSTR; Sid:PSID; Name:LPSTR; cbName:LPDWORD; ReferencedDomainName:LPSTR;cbReferencedDomainName:LPDWORD; peUse:PSID_NAME_USE):WINBOOL; external 'advapi32' name 'LookupAccountSidA';
  4.  
  5. function LookupAccountSid(lpSystemName: LPCTSTR; Sid: PSID; Name: LPTSTR; var cchName: DWORD; ReferencedDomainName: LPTSTR; var cchReferencedDomainName: DWORD;   var peUse: SID_NAME_USE): BOOL; stdcall;
  6.  
  7.  
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018