V0.2.5.0: Better windows IsAdmin function

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5667 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
gbamber
2017-01-17 09:54:24 +00:00
parent 22f509167d
commit 597d6f9b37
4 changed files with 134 additions and 77 deletions

View File

@ -10,7 +10,7 @@
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<EditorIndex Value="6"/> <EditorIndex Value="6"/>
<CursorPos X="33" Y="27"/> <CursorPos X="33" Y="27"/>
<UsageCount Value="44"/> <UsageCount Value="46"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
@ -21,7 +21,7 @@
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<TopLine Value="64"/> <TopLine Value="64"/>
<CursorPos X="19" Y="103"/> <CursorPos X="19" Y="103"/>
<UsageCount Value="44"/> <UsageCount Value="46"/>
<Loaded Value="True"/> <Loaded Value="True"/>
<LoadedDesigner Value="True"/> <LoadedDesigner Value="True"/>
</Unit1> </Unit1>
@ -32,27 +32,28 @@
<WindowIndex Value="1"/> <WindowIndex Value="1"/>
<TopLine Value="3"/> <TopLine Value="3"/>
<CursorPos X="76" Y="8"/> <CursorPos X="76" Y="8"/>
<UsageCount Value="27"/> <UsageCount Value="29"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit2> </Unit2>
<Unit3> <Unit3>
<Filename Value="..\lazautoupdate_httpclient.pas"/> <Filename Value="..\lazautoupdate_httpclient.pas"/>
<EditorIndex Value="3"/> <EditorIndex Value="3"/>
<CursorPos X="97" Y="12"/> <CursorPos X="97" Y="12"/>
<UsageCount Value="21"/> <UsageCount Value="22"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit3> </Unit3>
<Unit4> <Unit4>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<IsVisibleTab Value="True"/> <IsVisibleTab Value="True"/>
<WindowIndex Value="1"/> <EditorIndex Value="4"/>
<TopLine Value="1766"/> <TopLine Value="483"/>
<CursorPos X="111" Y="1791"/> <CursorPos Y="545"/>
<ExtraEditorCount Value="2"/> <ExtraEditorCount Value="2"/>
<ExtraEditor1> <ExtraEditor1>
<EditorIndex Value="4"/> <IsVisibleTab Value="True"/>
<TopLine Value="1163"/> <WindowIndex Value="1"/>
<CursorPos X="11" Y="1253"/> <TopLine Value="539"/>
<CursorPos Y="548"/>
</ExtraEditor1> </ExtraEditor1>
<ExtraEditor2> <ExtraEditor2>
<EditorIndex Value="-1"/> <EditorIndex Value="-1"/>
@ -60,7 +61,7 @@
<TopLine Value="-1"/> <TopLine Value="-1"/>
<CursorPos X="-1" Y="-1"/> <CursorPos X="-1" Y="-1"/>
</ExtraEditor2> </ExtraEditor2>
<UsageCount Value="21"/> <UsageCount Value="22"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit4> </Unit4>
<Unit5> <Unit5>
@ -104,7 +105,7 @@
<EditorIndex Value="2"/> <EditorIndex Value="2"/>
<TopLine Value="25"/> <TopLine Value="25"/>
<CursorPos X="25" Y="55"/> <CursorPos X="25" Y="55"/>
<UsageCount Value="19"/> <UsageCount Value="20"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit10> </Unit10>
<Unit11> <Unit11>
@ -119,7 +120,7 @@
<EditorIndex Value="5"/> <EditorIndex Value="5"/>
<TopLine Value="25"/> <TopLine Value="25"/>
<CursorPos X="71" Y="44"/> <CursorPos X="71" Y="44"/>
<UsageCount Value="16"/> <UsageCount Value="17"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit12> </Unit12>
<Unit13> <Unit13>
@ -149,143 +150,139 @@
<WindowIndex Value="1"/> <WindowIndex Value="1"/>
<TopLine Value="35"/> <TopLine Value="35"/>
<CursorPos X="40" Y="51"/> <CursorPos X="40" Y="51"/>
<UsageCount Value="11"/> <UsageCount Value="12"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit16> </Unit16>
<Unit17> <Unit17>
<Filename Value="C:\NewPascal\projects\snippets\usefulunits\uuser.pas"/> <Filename Value="C:\NewPascal\projects\snippets\usefulunits\uuser.pas"/>
<UnitName Value="uUser"/> <UnitName Value="uUser"/>
<IsVisibleTab Value="True"/>
<EditorIndex Value="1"/> <EditorIndex Value="1"/>
<TopLine Value="6"/> <TopLine Value="6"/>
<CursorPos Y="25"/> <CursorPos X="33" Y="16"/>
<UsageCount Value="10"/> <UsageCount Value="11"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit17> </Unit17>
</Units> </Units>
<General>
<ActiveWindowIndexAtStart Value="1"/>
</General>
<JumpHistory Count="30" HistoryIndex="29"> <JumpHistory Count="30" HistoryIndex="29">
<Position1> <Position1>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="56" Column="27" TopLine="28"/> <Caret Line="14" TopLine="3"/>
</Position1> </Position1>
<Position2> <Position2>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="533" Column="45" TopLine="519"/> <Caret Line="22" TopLine="3"/>
</Position2> </Position2>
<Position3> <Position3>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="16"/> <Caret Line="20" Column="7" TopLine="7"/>
</Position3> </Position3>
<Position4> <Position4>
<Filename Value="umainform.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="13" Column="43" TopLine="12"/> <Caret Line="25"/>
</Position4> </Position4>
<Position5> <Position5>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="14" TopLine="3"/> <Caret Line="24"/>
</Position5> </Position5>
<Position6> <Position6>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="22" TopLine="3"/> <Caret Line="25"/>
</Position6> </Position6>
<Position7> <Position7>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="20" Column="7" TopLine="7"/> <Caret Line="26" Column="8" TopLine="2"/>
</Position7> </Position7>
<Position8> <Position8>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="25"/> <Caret Line="16" TopLine="3"/>
</Position8> </Position8>
<Position9> <Position9>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\open_ssl.pas"/>
<Caret Line="24"/> <Caret Line="26" Column="2" TopLine="5"/>
</Position9> </Position9>
<Position10> <Position10>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="25"/> <Caret Line="56" Column="27" TopLine="28"/>
</Position10> </Position10>
<Position11> <Position11>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="26" Column="8" TopLine="2"/> <Caret Line="52" Column="19" TopLine="28"/>
</Position11> </Position11>
<Position12> <Position12>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="16" TopLine="3"/> <Caret Line="56" Column="19" TopLine="28"/>
</Position12> </Position12>
<Position13> <Position13>
<Filename Value="..\open_ssl.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="26" Column="2" TopLine="5"/> <Caret Line="926" Column="38" TopLine="889"/>
</Position13> </Position13>
<Position14> <Position14>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="56" Column="27" TopLine="28"/> <Caret Line="55" Column="27" TopLine="29"/>
</Position14> </Position14>
<Position15> <Position15>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="52" Column="19" TopLine="28"/> <Caret Line="926" Column="38" TopLine="892"/>
</Position15> </Position15>
<Position16> <Position16>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="56" Column="19" TopLine="28"/> <Caret Line="1083" Column="39" TopLine="1045"/>
</Position16> </Position16>
<Position17> <Position17>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="926" Column="38" TopLine="889"/> <Caret Line="51" Column="22" TopLine="28"/>
</Position17> </Position17>
<Position18> <Position18>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="55" Column="27" TopLine="29"/> <Caret Line="52" Column="19" TopLine="28"/>
</Position18> </Position18>
<Position19> <Position19>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="926" Column="38" TopLine="892"/> <Caret Line="55" Column="19" TopLine="28"/>
</Position19> </Position19>
<Position20> <Position20>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="1083" Column="39" TopLine="1045"/> <Caret Line="53" Column="103" TopLine="32"/>
</Position20> </Position20>
<Position21> <Position21>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="51" Column="22" TopLine="28"/> <Caret Line="82" Column="9" TopLine="47"/>
</Position21> </Position21>
<Position22> <Position22>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="52" Column="19" TopLine="28"/> <Caret Line="89" Column="30" TopLine="58"/>
</Position22> </Position22>
<Position23> <Position23>
<Filename Value="..\ulazautoupdate.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="55" Column="19" TopLine="28"/> <Caret Line="15" Column="23"/>
</Position23> </Position23>
<Position24> <Position24>
<Filename Value="umainform.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="53" Column="103" TopLine="32"/> <Caret Line="79" Column="26" TopLine="53"/>
</Position24> </Position24>
<Position25> <Position25>
<Filename Value="umainform.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="82" Column="9" TopLine="47"/> <Caret Line="157" Column="94" TopLine="115"/>
</Position25> </Position25>
<Position26> <Position26>
<Filename Value="umainform.pas"/> <Filename Value="umainform.pas"/>
<Caret Line="89" Column="30" TopLine="58"/> <Caret Line="95" TopLine="64"/>
</Position26> </Position26>
<Position27> <Position27>
<Filename Value="umainform.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="15" Column="23"/> <Caret Line="518" Column="16" TopLine="483"/>
</Position27> </Position27>
<Position28> <Position28>
<Filename Value="umainform.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="79" Column="26" TopLine="53"/> <Caret Line="1823" Column="4" TopLine="1796"/>
</Position28> </Position28>
<Position29> <Position29>
<Filename Value="umainform.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="157" Column="94" TopLine="115"/> <Caret Line="577" Column="7" TopLine="544"/>
</Position29> </Position29>
<Position30> <Position30>
<Filename Value="umainform.pas"/> <Filename Value="..\ulazautoupdate.pas"/>
<Caret Line="95" TopLine="64"/> <Caret Line="539" Column="7" TopLine="516"/>
</Position30> </Position30>
</JumpHistory> </JumpHistory>
</ProjectSession> </ProjectSession>

View File

@ -1,7 +1,7 @@
object mainform: Tmainform object mainform: Tmainform
Left = 558 Left = 125
Height = 209 Height = 209
Top = 210 Top = 292
Width = 335 Width = 335
BorderIcons = [biSystemMenu] BorderIcons = [biSystemMenu]
Caption = 'mainform' Caption = 'mainform'

View File

@ -128,11 +128,6 @@ const
{$IFDEF CPU64}C_UPDATER = 'updatehmwin64.exe'; {$IFDEF CPU64}C_UPDATER = 'updatehmwin64.exe';
C_LOCALUPDATER = 'lauupdatewin64.exe';{$ENDIF} C_LOCALUPDATER = 'lauupdatewin64.exe';{$ENDIF}
// Windows Constants // Windows Constants
CONST
GENERIC_READ = $80000000;
GENERIC_WRITE = $40000000;
GENERIC_EXECUTE = $20000000;
GENERIC_ALL = $10000000;
{$ENDIF} {$ENDIF}
{$IFDEF LINUX} {$IFDEF LINUX}
{$IFDEF CPU32}C_UPDATER = 'updatehmlinux32'; {$IFDEF CPU32}C_UPDATER = 'updatehmlinux32';
@ -263,7 +258,7 @@ type
procedure SetShowDialogs(AValue: boolean); procedure SetShowDialogs(AValue: boolean);
procedure SetDebugMode(AValue: boolean); procedure SetDebugMode(AValue: boolean);
function GetThreadDownloadReturnCode: integer; function GetThreadDownloadReturnCode: integer;
function IsSourceForgeVersionNewer(const sznewINIPath: string): boolean; function IsOnlineVersionNewer(const sznewINIPath: string): boolean;
function VersionStringToNumber(AVersionString: string): integer; function VersionStringToNumber(AVersionString: string): integer;
function DoSilentUpdate: boolean; function DoSilentUpdate: boolean;
protected protected
@ -482,7 +477,71 @@ begin
ShowMessage(fApplicationVersionString); ShowMessage(fApplicationVersionString);
end; end;
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
function IsWindowsAdmin: boolean; function IsWindowsAdmin: Boolean;
const
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =
(Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
x: Integer;
bSuccess: BOOL;
LastError: integer;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then
begin
Result := True;
exit;
end;
Result := False;
bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
hAccessToken);
if not bSuccess then
begin
if GetLastError = ERROR_NO_TOKEN then
bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
hAccessToken);
end;
if bSuccess then
begin
GetMem(ptgGroups, 1024);
bSuccess := GetTokenInformation(hAccessToken, TokenGroups,
ptgGroups, 1024, @dwInfoBufferSize);
LastError := GetLastError;
if not bSuccess then
showmessage(format('GetLastError %d',[LastError]));
CloseHandle(hAccessToken);
if bSuccess then
begin
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, psidAdministrators);
{$R-}
for x := 0 to ptgGroups^.GroupCount - 1 do
if EqualSid(psidAdministrators, ptgGroups^.Groups[x].Sid) then
begin
Result := True;
break;
end;
{$R+}
FreeSid(psidAdministrators);
end;
FreeMem(ptgGroups);
end;
end;
function IsWindowsAdminWinXP: boolean;
CONST
GENERIC_READ = $80000000;
GENERIC_WRITE = $40000000;
GENERIC_EXECUTE = $20000000;
GENERIC_ALL = $10000000;
var var
hSC: THandle; hSC: THandle;
begin begin
@ -857,7 +916,7 @@ begin
mtInformation, [mbOK], 0); mtInformation, [mbOK], 0);
end; end;
function TLazAutoUpdate.IsSourceForgeVersionNewer(const sznewINIPath: string): boolean; function TLazAutoUpdate.IsOnlineVersionNewer(const sznewINIPath: string): boolean;
// Compares version contained in szTempXMLPath INI file // Compares version contained in szTempXMLPath INI file
// to fApplicationVersionNumber // to fApplicationVersionNumber
var var
@ -1058,7 +1117,7 @@ begin
fOndebugEvent(Self, 'NewVersionAvailable', fOndebugEvent(Self, 'NewVersionAvailable',
Format('Downloaded %s OK', [szTargetPath])); Format('Downloaded %s OK', [szTargetPath]));
fParentApplication.ProcessMessages; fParentApplication.ProcessMessages;
Result := IsSourceForgeVersionNewer(szTargetPath); Result := IsOnlineVersionNewer(szTargetPath);
if fFireDebugEvent then if fFireDebugEvent then
fOndebugEvent(Self, 'NewVersionAvailable', fOndebugEvent(Self, 'NewVersionAvailable',
Format(C_DownloadedBytes, [szTargetPath, fDownloadSize])); Format(C_DownloadedBytes, [szTargetPath, fDownloadSize]));
@ -1785,6 +1844,7 @@ var
begin begin
Result := False; Result := False;
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
If NOT IsWindowsAdmin then If NOT IsWindowsAdmin then
begin begin
szParams:='Only Windows users whith Administrator status can update this application.' + lineending; szParams:='Only Windows users whith Administrator status can update this application.' + lineending;