| [1691] | 1 | unit XlfSid;
 | 
|---|
 | 2 | { **************************************************************
 | 
|---|
 | 3 |         Package: XWB - Kernel RPCBroker
 | 
|---|
 | 4 |         Date Created: Sept 18, 1997 (Version 1.1)
 | 
|---|
 | 5 |         Site Name: Oakland, OI Field Office, Dept of Veteran Affairs
 | 
|---|
 | 6 |         Developers: Danila Manapsal, Don Craven, Joel Ivey
 | 
|---|
 | 7 |         Description: Contains TRPCBroker and related components.
 | 
|---|
 | 8 |         Current Release: Version 1.1 Patch 47 (Jun. 17, 2008))
 | 
|---|
 | 9 | *************************************************************** }
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | //*******************************************************
 | 
|---|
 | 12 | //These functions get thier data from the Thread
 | 
|---|
 | 13 | // or Process security ID in Windows.
 | 
|---|
 | 14 | // GetNTLogonUser returns the Domain\Username that
 | 
|---|
 | 15 | //  authtcated the user/
 | 
|---|
 | 16 | // GetNTLogonSid returns a string with the users SID.
 | 
|---|
 | 17 | //********************************************************
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | interface
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | uses windows, SysUtils;
 | 
|---|
 | 22 | type  {From MSDN}
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 |   StringSid = ^LPTSTR;
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | function ConvertSidToStringSid(Sid: {THandle}PSID; var StrSid: LPTSTR): BOOL stdcall;
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | function GetNTLogonUser(): string;
 | 
|---|
 | 29 | function GetNTLogonSid(): string;
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | implementation
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | function ConvertSidToStringSid; external advapi32 name 'ConvertSidToStringSidA';
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | function GetNTLogonUser(): string;
 | 
|---|
 | 37 | var
 | 
|---|
 | 38 |     hToken: THANDLE;
 | 
|---|
 | 39 |     tic: TTokenInformationClass;
 | 
|---|
 | 40 |     ptkUser:  PSIDAndAttributes;
 | 
|---|
 | 41 |     P: pointer;
 | 
|---|
 | 42 |     buf: PChar;
 | 
|---|
 | 43 |     cbti: DWORD;
 | 
|---|
 | 44 |     Name: PChar;
 | 
|---|
 | 45 |     cbName: DWORD;
 | 
|---|
 | 46 |     RDN: PChar;
 | 
|---|
 | 47 |     cbRDN: DWORD;
 | 
|---|
 | 48 |     snu: DWORD;
 | 
|---|
 | 49 | begin
 | 
|---|
 | 50 |     Result := '';
 | 
|---|
 | 51 |     tic := TokenUser;
 | 
|---|
 | 52 |     Name := '';
 | 
|---|
 | 53 |     RDN := '';
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |     try
 | 
|---|
 | 56 |     //Get the calling thread's access token
 | 
|---|
 | 57 |     if not OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
 | 
|---|
 | 58 |                 , longbool(true), hToken) then
 | 
|---|
 | 59 |          if (GetLastError() <> ERROR_NO_TOKEN) then exit
 | 
|---|
 | 60 |     // Retry against process token if no thread token exist.
 | 
|---|
 | 61 |           else
 | 
|---|
 | 62 |           if not OpenProcessToken(GetCurrentProcess()
 | 
|---|
 | 63 |                  ,TOKEN_QUERY, hToken) then exit;
 | 
|---|
 | 64 |     // Obtain the size of the user info in the token
 | 
|---|
 | 65 |     // Call should fail due to zero-length buffer
 | 
|---|
 | 66 |     if GetTokenInformation(hToken, tic, nil, 0, cbti) then exit;
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |     // Allocate buffer for user Info
 | 
|---|
 | 69 |     buf := StrAlloc(cbti);
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |     // Retrive the user info from the token.
 | 
|---|
 | 72 |     if not GetTokenInformation(hToken, tic, buf, cbti, cbti) then exit;
 | 
|---|
 | 73 |     cbName := 0;
 | 
|---|
 | 74 |     cbRDN := 0;
 | 
|---|
 | 75 |     snu := 0;
 | 
|---|
 | 76 |     P := buf;  //Use pointer to recast PChar
 | 
|---|
 | 77 |     ptkUser := PSIDAndAttributes(P);
 | 
|---|
 | 78 |     //call to get the size of name and RDN.
 | 
|---|
 | 79 |     LookupAccountSid( nil, ptkUser.Sid, Name, cbName
 | 
|---|
 | 80 |                             , RDN, cbRDN, snu);
 | 
|---|
 | 81 |     Name := StrAlloc(cbName);
 | 
|---|
 | 82 |     RDN := StrAlloc(cbRDN);
 | 
|---|
 | 83 |     //Call to fillin Name and RDN
 | 
|---|
 | 84 |     LookupAccountSid( nil, ptkUser.Sid, Name, cbName
 | 
|---|
 | 85 |                             , RDN, cbRDN, snu);
 | 
|---|
 | 86 |     Result := string(RDN) + '\' + string(Name);
 | 
|---|
 | 87 |     StrDispose(Name);
 | 
|---|
 | 88 |     StrDispose(RDN);
 | 
|---|
 | 89 |     finally
 | 
|---|
 | 90 |     if (hToken <> 0) then CloseHandle(hToken);
 | 
|---|
 | 91 |     end;
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 | end;
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | function GetNTLogonSid(): string;
 | 
|---|
 | 96 | var
 | 
|---|
 | 97 |     hToken: THANDLE;
 | 
|---|
 | 98 |     tic: TTokenInformationClass;
 | 
|---|
 | 99 |     ptkUser:  PSIDAndAttributes;
 | 
|---|
 | 100 |     P: pointer;
 | 
|---|
 | 101 |     buf: PChar;
 | 
|---|
 | 102 |     StrSid: PChar;
 | 
|---|
 | 103 |     cbti: DWORD;
 | 
|---|
 | 104 | //    cbName: DWORD;
 | 
|---|
 | 105 | //    cbRDN: DWORD;
 | 
|---|
 | 106 | //    snu: DWORD;
 | 
|---|
 | 107 | begin
 | 
|---|
 | 108 |     Result := '';
 | 
|---|
 | 109 |     tic := TokenUser;
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |     try
 | 
|---|
 | 112 |     //Get the calling thread's access token
 | 
|---|
 | 113 |     if not OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
 | 
|---|
 | 114 |                 , longbool(true), hToken) then
 | 
|---|
 | 115 |          if (GetLastError() <> ERROR_NO_TOKEN) then exit
 | 
|---|
 | 116 |     // Retry against process token if no thread token exist.
 | 
|---|
 | 117 |           else
 | 
|---|
 | 118 |           if not OpenProcessToken(GetCurrentProcess()
 | 
|---|
 | 119 |                  ,TOKEN_QUERY, hToken) then exit;
 | 
|---|
 | 120 |     // Obtain the size of the user info in the token
 | 
|---|
 | 121 |     // Call should fail due to zero-length buffer
 | 
|---|
 | 122 |     if GetTokenInformation(hToken, tic, nil, 0, cbti) then exit;
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |     // Allocate buffer for user Info
 | 
|---|
 | 125 |     buf := StrAlloc(cbti);
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |     // Retrive the user info from the token.
 | 
|---|
 | 128 |     if not GetTokenInformation(hToken, tic, buf, cbti, cbti) then exit;
 | 
|---|
 | 129 |     P := buf;  //Use pointer to recast PChar
 | 
|---|
 | 130 |     ptkUser := PSIDAndAttributes(P);
 | 
|---|
 | 131 | //    P := nil;
 | 
|---|
 | 132 |     if ConvertSidToStringSid(ptkUser.sid, StrSid) = true then
 | 
|---|
 | 133 |         begin
 | 
|---|
 | 134 |         Result := PChar(StrSid);
 | 
|---|
 | 135 |         localFree(Cardinal(StrSid));
 | 
|---|
 | 136 |         end;
 | 
|---|
 | 137 |     finally
 | 
|---|
 | 138 |     if (hToken <> 0) then CloseHandle(hToken);
 | 
|---|
 | 139 |     end;
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 | end;
 | 
|---|
 | 142 | end.
 | 
|---|