source: cprs/trunk/BDK50/BDK32_P50/Source/XlfSid.pas@ 1724

Last change on this file since 1724 was 1678, checked in by healthsevak, 10 years ago

Added this new version of Broker component libraries while updating the working copy to CPRS version 28

File size: 4.3 KB
RevLine 
[1678]1unit 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
19interface
20
21uses windows, SysUtils;
22type {From MSDN}
23
24 StringSid = ^LPTSTR;
25
26function ConvertSidToStringSid(Sid: {THandle}PSID; var StrSid: LPTSTR): BOOL stdcall;
27
28function GetNTLogonUser(): string;
29function GetNTLogonSid(): string;
30
31implementation
32
33
34function ConvertSidToStringSid; external advapi32 name 'ConvertSidToStringSidA';
35
36function GetNTLogonUser(): string;
37var
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;
49begin
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
93end;
94
95function GetNTLogonSid(): string;
96var
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;
107begin
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
141end;
142end.
Note: See TracBrowser for help on using the repository browser.