|
Per mappare Sid su Nome account e viceversa si ricorre alle api
LookupAccountName (dal SID si ottiene il Nome account) e
LookupAccountSid (dal Nome account si ottiene il Sid). In
queste 2 funzioni il Sid viene espresso come record (e non tramite la
classica rappresentazione a stringa del tipo S-R-X-Y1-Y2-...-Y(n-1)-RID).
type
SID = record
Revision: Byte;
SubAuthorityCount: Byte;
IdentifierAuthority: SID_IDENTIFIER_AUTHORITY;
SubAuthority: array [0..ANYSIZE_ARRAY - 1] of DWORD;
end;
Ai seguenti 2 indirizzi è presentato un elenco dei Sid predefiniti (Well-Known)
http://support.microsoft.com/?kbid=243330 Windows XP Well-Known Security Identifiers Per tradurre il record nella rappresentazione a stringa (e viceversa) si ricorre alle api ConvertSidToStringSid e ConvertStringSidToSid. 1. Unit con le definizioni dei tipi e le dichiarazioni delle funzioni
unit uTypes;
interface
const
{$IFDEF UNICODE}
AWSuffix = 'W';
{$ELSE}
AWSuffix = 'A';
{$ENDIF UNICODE}
advapi32 = 'advapi32.dll';
type
PSID_IDENTIFIER_AUTHORITY = ^SID_IDENTIFIER_AUTHORITY;
SID_IDENTIFIER_AUTHORITY = record
Value: array [0..5] of Byte;
end; PSid = ^SID;
SID = record
Revision: Byte;
SubAuthorityCount: Byte;
IdentifierAuthority: SID_IDENTIFIER_AUTHORITY;
SubAuthority: array [0..0] of Cardinal;
//array di qualsiasi dimensione
end;
function ConvertStringSidToSidA(StringSid: pAnsiChar;
var Sid: PSID): Boolean; stdcall;
function ConvertStringSidToSidW(StringSid: pWideChar;
var Sid: PSID): Boolean; stdcall;
function ConvertStringSidToSid(StringSid: PChar;
var Sid: PSID): Boolean; stdcall;
function ConvertSidToStringSidA(Sid: PSID;
var StringSid: PAnsiChar): Boolean; stdcall;
function ConvertSidToStringSidW(Sid: PSID;
var StringSid: PWideChar): Boolean; stdcall;
function ConvertSidToStringSid(Sid: PSID;
var StringSid: PChar): Boolean; stdcall;
implementation
function ConvertStringSidToSidA; external advapi32 name 'ConvertStringSidToSidA';
function ConvertStringSidToSidW; external advapi32 name 'ConvertStringSidToSidW';
function ConvertStringSidToSid; external advapi32 name 'ConvertStringSidToSid' + AWSuffix;
function ConvertSidToStringSidA; external advapi32 name 'ConvertSidToStringSidA';
function ConvertSidToStringSidW; external advapi32 name 'ConvertSidToStringSidW';
function ConvertSidToStringSid; external advapi32 name 'ConvertSidToStringSid' + AWSuffix;
end.
2. Dal SID ricavare il Nome account
function SID_Nome(const NomeSistema: string;
Sid_string: string;
var Nome: string;
var TipoSID_string: string): Boolean;
var
DimensioneDominio, DimensioneNome: Longword;
R: LongBool;
Dominio: string;
TipoSid: DWORD;
Sid: PSID;
begin
Result := False;
DimensioneNome := 0;
DimensioneDominio := 0;
ConvertStringSidToSid(pChar(Sid_string), Sid);
R := LookupAccountSid(PChar(NomeSistema),
Sid,
nil,
DimensioneNome,
nil,
DimensioneDominio,
TipoSid);
if (not R) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
SetLength(Dominio, DimensioneDominio);
SetLength(Nome, DimensioneNome);
Result := LookupAccountSid(PChar(NomeSistema),
Sid,
PChar(Nome),
DimensioneNome,
PChar(Dominio),
DimensioneDominio,
TipoSid);
if Result then
begin
SetLength(Nome, StrLen(PChar(Nome)));
TipoSID_string := SIDType(TipoSid);
end;
end
else
raise Exception.Create(SysErrorMessage(GetLastError));
end;
3. Dal Nome account ricavare il SID
function Nome_SID(const NomeSistema,
Nome: string;
var Sid_string: string;
var TipoSID_string: string): Boolean;
var
DimensioneDominio, DimensioneSid: Longword;
R: LongBool;
Dominio: string;
TipoSid: DWORD;
Sid: PSID;
Sid_str: pChar;
begin
Result := False;
DimensioneSid := 0;
DimensioneDominio := 0;
R := LookupAccountName(PChar(NomeSistema),
PChar(Nome),
nil,
DimensioneSid,
nil,
DimensioneDominio,
TipoSid);
if (not R) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
SetLength(Dominio, DimensioneDominio);
Sid := AllocMem(DimensioneSid);
Result := LookupAccountName(PChar(NomeSistema),
PChar(Nome),
Sid,
DimensioneSid,
PChar(Dominio),
DimensioneDominio,
TipoSid);
if not Result then
begin
FreeMem(Sid);
Sid := nil;
end
else
begin
Sid_str := pchar(Sid_string);
ConvertSidToStringSid(Sid, Sid_str);
Sid_string := string(Sid_str);
TipoSID_string := SIDType(TipoSid);
end;
end
else
raise Exception.Create(SysErrorMessage(GetLastError));
end;
4. Brevi note Nelle 2 funzioni precedenti si fà riferimento anche al tipo di Sid (valore DWORD): può essere rappresentato come stringa dalla seguente funzione:
function SIDType(Tipo: DWORD): string;
begin
case Tipo of
1: Result := 'utente';
2: Result := 'gruppo';
3: Result := 'dominio';
4: Result := 'alias';
5: Result := 'sid predefinito (well known)';
6: Result := 'account eliminato';
7: Result := 'non valido';
8: Result := 'sconosciuto';
end;
end;
AccountSID
|