Autor Beitrag
blaskito
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 63

Win2003, 2008, 2012, WinXP, Win7
Delphi 6 Pers.
BeitragVerfasst: Mo 14.12.09 10:51 
Hallo,

ich habe in einem Jedi-Beispiel den folgenden Code zum Kopieren der Rechte von einem Ordner auf einen anderen gefunden:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
unit Helping;

interface

  uses
    Windows, JwaAccCtrl, JwaAclApi, JwaWinBase, JwaWinNT;

  function CopyNTFSSecurity(const Source, Dest: string): boolean;

implementation

function CopyNTFSSecurity(const Source, Dest: string): boolean;
var
  SidOwner: PSID;
  SidGroup: PSID;
  Dacl: PACL;
  Sacl: PACL;
  SecDescPtr: PSECURITY_DESCRIPTOR;
begin
  Result := false;

  if GetNamedSecurityInfo(PChar(Source), SE_FILE_OBJECT,
    DACL_SECURITY_INFORMATION or SACL_SECURITY_INFORMATION or
    OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION,
    @SidOwner, @SidGroup, @Dacl, @Sacl, SecDescPtr) = ERROR_SUCCESS then
  begin
    Result := SetNamedSecurityInfo(PChar(Dest), SE_FILE_OBJECT,
      DACL_SECURITY_INFORMATION or SACL_SECURITY_INFORMATION or
      OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION,
      SidOwner, SidGroup, Dacl, Sacl) = ERROR_SUCCESS;

    LocalFree(HLOCAL(SecDescPtr));
  end;
end;

end.

Leider funzt das Beispiel nicht. Bereits das GetNamedSecurityInfo gibt mir keinen Success zurück. Hat jemand schon mal mit dieser Funktion (erfolgreich) gearbeitet?

Schöne Grüße aus dem Norden
blaskito
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 14.12.09 11:07 
Was sagt GetLastError?
blaskito Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 63

Win2003, 2008, 2012, WinXP, Win7
Delphi 6 Pers.
BeitragVerfasst: Mo 14.12.09 11:35 
Hallo Michael,

erstaunlicherweise bekomme ich da eine 0.
Dezipaitor
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220



BeitragVerfasst: Mo 14.12.09 15:09 
GetLastError wird garnicht verwendet!
Der Rückgabewert der Funktion enthält den Fehlercode.

btw:
Für die SACL Information brauchst du das SE_SECURITY_NAME Privileg, welches aktiviert sein muss.

_________________
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: blog.delphi-jedi.net = JEDI API LIB & Windows Security Code Library (JWSCL)
blaskito Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 63

Win2003, 2008, 2012, WinXP, Win7
Delphi 6 Pers.
BeitragVerfasst: Di 15.12.09 10:11 
Hallo und vielen Dank für den Hinweis!

Es klappt jetzt. Ich habe auf Torry dazu den passenden Code gefunden: www.swissdelphicente...showcode.php?id=1177
Er verträgt sich so allerdings nicht so ganz mit Jedi, deswegen habe ich ihn in eine eigene Unit gepackt:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
unit Privileges;

{
  For some functions you need to get the right privileges
  on a Windows NT machine.
  (e.g: To shut down or restart windows with ExitWindowsEx or
  to change the system time)
  The following code provides a procedure to adjust the privileges.
  The AdjustTokenPrivileges() function enables or disables privileges
  in the specified access token.
}


{
  Um einige Funktionen unter Windows NT erfolgreich ausführen zu können,
  muss der ausführende Prozess die Privilegien dazu haben.
  (z.B um das System herunterzufahren mit ExitWindowsEx oder die
  Systemzeit zu ändern) AdjustTokenPrivileges() passt die Privilegien an.
}


// NT Defined Privileges from winnt.h
// Autor: Thomas Stutz

interface

  uses
    Windows, SysUtils;

  function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;

  const
    SE_CREATE_TOKEN_NAME = 'SeCreateTokenPrivilege';
    SE_ASSIGNPRIMARYTOKEN_NAME = 'SeAssignPrimaryTokenPrivilege';
    SE_LOCK_MEMORY_NAME = 'SeLockMemoryPrivilege';
    SE_INCREASE_QUOTA_NAME = 'SeIncreaseQuotaPrivilege';
    SE_UNSOLICITED_INPUT_NAME = 'SeUnsolicitedInputPrivilege';
    SE_MACHINE_ACCOUNT_NAME = 'SeMachineAccountPrivilege';
    SE_TCB_NAME = 'SeTcbPrivilege';
    SE_SECURITY_NAME = 'SeSecurityPrivilege';
    SE_TAKE_OWNERSHIP_NAME = 'SeTakeOwnershipPrivilege';
    SE_LOAD_DRIVER_NAME = 'SeLoadDriverPrivilege';
    SE_SYSTEM_PROFILE_NAME = 'SeSystemProfilePrivilege';
    SE_SYSTEMTIME_NAME = 'SeSystemtimePrivilege';
    SE_PROF_SINGLE_PROCESS_NAME = 'SeProfileSingleProcessPrivilege';
    SE_INC_BASE_PRIORITY_NAME = 'SeIncreaseBasePriorityPrivilege';
    SE_CREATE_PAGEFILE_NAME = 'SeCreatePagefilePrivilege';
    SE_CREATE_PERMANENT_NAME = 'SeCreatePermanentPrivilege';
    SE_BACKUP_NAME = 'SeBackupPrivilege';
    SE_RESTORE_NAME = 'SeRestorePrivilege';
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
    SE_DEBUG_NAME = 'SeDebugPrivilege';
    SE_AUDIT_NAME = 'SeAuditPrivilege';
    SE_SYSTEM_ENVIRONMENT_NAME = 'SeSystemEnvironmentPrivilege';
    SE_CHANGE_NOTIFY_NAME = 'SeChangeNotifyPrivilege';
    SE_REMOTE_SHUTDOWN_NAME = 'SeRemoteShutdownPrivilege';
    SE_UNDOCK_NAME = 'SeUndockPrivilege';
    SE_SYNC_AGENT_NAME = 'SeSyncAgentPrivilege';
    SE_ENABLE_DELEGATION_NAME = 'SeEnableDelegationPrivilege';
    SE_MANAGE_VOLUME_NAME = 'SeManageVolumePrivilege';

implementation

// Enables or disables privileges depending on the bEnabled
// Aktiviert oder deaktiviert Privilegien, abhängig von bEnabled

function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;
var
  hToken: THandle;
  TokenPriv: TOKEN_PRIVILEGES;
  PrevTokenPriv: TOKEN_PRIVILEGES;
  ReturnLength: Cardinal;
  vi:TOSVersionInfo;
begin
  Result := True;
  // Only for Windows NT/2000/XP and later.
  vi.dwOSVersionInfoSize:=SizeOf(vi);
  GetVersionEx(vi);
  if not (vi.dwPlatformId = VER_PLATFORM_WIN32_NT) then Exit;
//  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then Exit;
  Result := False;

  // obtain the processes token
  if OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  begin
    try
      // Get the locally unique identifier (LUID) .
      if LookupPrivilegeValue(nil, PChar(sPrivilege),
        TokenPriv.Privileges[0].Luid) then
      begin
        TokenPriv.PrivilegeCount := 1// one privilege to set

        case bEnabled of
          True: TokenPriv.Privileges[0].Attributes  := SE_PRIVILEGE_ENABLED;
          False: TokenPriv.Privileges[0].Attributes := 0;
        end;

        ReturnLength := 0// replaces a var parameter
        PrevTokenPriv := TokenPriv;

        // enable or disable the privilege

        AdjustTokenPrivileges(hToken, False, TokenPriv, SizeOf(PrevTokenPriv),
          PrevTokenPriv, ReturnLength);
      end;
    finally
      CloseHandle(hToken);
    end;
  end;
  // test the return value of AdjustTokenPrivileges.
  Result := GetLastError = ERROR_SUCCESS;
  if not Result then
    raise Exception.Create(SysErrorMessage(GetLastError));
end;

end.

Vor und nach dem Kopiervorgang aufgerufen und es läuft.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 15.12.09 13:22 
user profile iconDezipaitor hat folgendes geschrieben Zum zitierten Posting springen:
GetLastError wird garnicht verwendet!
Der Rückgabewert der Funktion enthält den Fehlercode.

Egal, ich meinte auch nur irgendwie eine Fehlerbehandlung, damit man sieht warum es nicht funktioniert.
Dezipaitor
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220



BeitragVerfasst: Do 17.12.09 17:42 
Vorher wurde auch nur auf Erfolg geprüft. Der Rückgabewert, also der Fehlercode, wurde jedoch ignoriert, bis auf, dass er gegen 0 geprüft wurde.

_________________
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: blog.delphi-jedi.net = JEDI API LIB & Windows Security Code Library (JWSCL)