winutils.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2009 by the Free Pascal development team
  4. Misc windows utility functions
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}{$H+}
  12. unit winutils;
  13. Interface
  14. Uses Windows, ComObj, ActiveX;
  15. // returns True if the currently logged Windows user has Administrator rights. Delphi.about.com
  16. // From Delphi.about.com with permission, http://delphi.about.com/od/delphitips2007/qt/is_win_admin.htm
  17. function IsWindowsAdmin: Boolean;
  18. // Removes Browsers "downloaded" attribute from a file.
  19. procedure UnBlockFile(const name:String);
  20. const
  21. NET_FW_PROFILE2_DOMAIN = 1;
  22. NET_FW_PROFILE2_PRIVATE = 2;
  23. NET_FW_PROFILE2_PUBLIC = 4;
  24. NET_FW_IP_PROTOCOL_TCP = 6;
  25. NET_FW_IP_PROTOCOL_UDP = 17;
  26. NET_FW_ACTION_ALLOW = 1;
  27. // add firewall rule e.g.
  28. // AddProgramExceptionToFireWall( Application.Title,Application.Title, Application.ExeName, NET_FW_IP_PROTOCOL_TCP, NET_FW_PROFILE2_DOMAIN or NET_FW_PROFILE2_PRIVATE or NET_FW_PROFILE2_PUBLIC);
  29. procedure AddProgramExceptionToFireWall(Const wsCaption, wsDescription, wsExecutable: WideString; iProtocol,iProfile:Integer);
  30. // remove firewall rule, e.g. RemoveExceptionFromFW(Application.Title);
  31. procedure RemoveExceptionFromFW(Const exCaption: WideString);
  32. implementation
  33. const
  34. SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5)) ;
  35. const
  36. SECURITY_BUILTIN_DOMAIN_RID = $00000020;
  37. DOMAIN_ALIAS_RID_ADMINS = $00000220;
  38. function IsWindowsAdmin: Boolean;
  39. var
  40. hAccessToken: THandle;
  41. ptgGroups: PTokenGroups;
  42. dwInfoBufferSize: DWORD;
  43. psidAdministrators: PSID;
  44. g: Integer;
  45. bSuccess: BOOL;
  46. begin
  47. Result := False;
  48. bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken) ;
  49. if not bSuccess then
  50. begin
  51. if GetLastError = ERROR_NO_TOKEN then
  52. bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken) ;
  53. end;
  54. if bSuccess then
  55. begin
  56. GetMem(ptgGroups, 1024) ;
  57. bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 1024, dwInfoBufferSize) ;
  58. CloseHandle(hAccessToken) ;
  59. if bSuccess then
  60. begin
  61. AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators) ;
  62. for g := 0 to ptgGroups^.GroupCount - 1 do
  63. if EqualSid(psidAdministrators, ptgGroups^.Groups[g].Sid) then
  64. begin
  65. Result := True;
  66. Break;
  67. end;
  68. FreeSid(psidAdministrators) ;
  69. end;
  70. FreeMem(ptgGroups) ;
  71. end;
  72. end;
  73. procedure UnBlockFile(const name:String);
  74. var f : file;
  75. begin
  76. assignfile(f,name+':Zone.Identifier');
  77. rewrite(f,1);
  78. truncate(f);
  79. closefile(f);
  80. end;
  81. procedure AddProgramExceptionToFireWall(Const wsCaption, wsDescription, wsExecutable: WideString; iProtocol, iProfile:Integer);
  82. var
  83. fwPolicy2 : OleVariant;
  84. RulesObject : OleVariant;
  85. NewRule : OleVariant;
  86. begin
  87. fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
  88. RulesObject := fwPolicy2.Rules;
  89. NewRule := CreateOleObject('HNetCfg.FWRule');
  90. NewRule.Name := wsCaption;
  91. NewRule.Description := wsDescription;
  92. NewRule.Applicationname := wsExecutable;
  93. NewRule.Protocol := iProtocol;
  94. NewRule.Enabled := TRUE;
  95. NewRule.Profiles := iProfile;
  96. NewRule.Action := NET_FW_ACTION_ALLOW;
  97. RulesObject.Add(NewRule);
  98. end;
  99. procedure RemoveExceptionFromFW(Const exCaption: WideString);
  100. var
  101. fwPolicy2 : OleVariant;
  102. begin
  103. fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
  104. fwPolicy2.Rules.Remove(exCaption);
  105. end;
  106. end.