<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JEDI Windows API &#187; ACL</title>
	<atom:link href="http://blog.delphi-jedi.net/tag/acl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.delphi-jedi.net</link>
	<description>Joint Endeavor of Delphi Innovators of Windows Programming</description>
	<lastBuildDate>Sat, 21 Aug 2010 05:44:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Setting Folder Security</title>
		<link>http://blog.delphi-jedi.net/2010/03/24/setting-folder-security/</link>
		<comments>http://blog.delphi-jedi.net/2010/03/24/setting-folder-security/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 18:03:38 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[DACL]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[permission]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/?p=721</guid>
		<description><![CDATA[This article describes some ways how to set the security on a folder using JWSCL. Usually, we want to add some rights for a particular user to a folder so she gets access. I can say that is a heck of work to do with WinAPI. But still with JWSCL we need to consider some [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes some ways how to set the security on a folder using JWSCL. Usually, we want to add some rights for a particular user to a folder so she gets access. I can say that is a heck of work to do with WinAPI. But still with JWSCL we need to consider some things. <span id="more-721"></span></p>
<p>The following code creates a folder named &#8220;JWSCLTest&#8221; and applies a DACL that allows full control to everyone. The folder will inherit its security settings to child folders and files (check the afXXX flags).</p>
<p><pre><pre class="brush:delphi">const JWSCLTestFolder = &#039;JWSCLTestFolder&#039;;

var
&nbsp;&nbsp;SD : TJwSecurityDescriptor;
&nbsp;&nbsp;pSA : PSecurityAttributes;
begin
&nbsp;&nbsp;JwInitWellKnownSIDs;

&nbsp;&nbsp;SD := TJwSecurityDescriptor.Create;
&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;SD.DACL.Add(TJwDiscretionaryAccessControlEntryAllow.Create(nil,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[afContainerInheritAce, afObjectInheritAce], FILE_ALL_ACCESS, JwWorldSID));

&nbsp;&nbsp;&nbsp;&nbsp;pSA := SD.Create_SA();
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Win32Check(CreateDirectory(JWSCLTestFolder, pSA));
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SD.Free_SA(pSA); //remember to free pointer
&nbsp;&nbsp;&nbsp;&nbsp;end;
&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;SD.Free;
&nbsp;&nbsp;end;
end.
</pre></pre></p>
<p>CreateDirectory receives a security attributes structure that is applied to the folder directly. However, in this way the parent security descriptor is not inherited to our folder. This is called a protected DACL because the inheritance flow is stopped. So we get a folder with only one Access Control Entry (ACE) : Everyone (aka World SID).  To remedy that we can copy the ACEs from the parent folder to our own folder:</p>
<p><pre><pre class="brush:delphi">procedure MergeParentDACL(const Location : String; TargetSD : TJwSecurityDescriptor);
var DirSD : TJwSecureFileObject;
begin
&nbsp;&nbsp;DirSD := TJwSecureFileObject.Create(Location);
&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;TargetSD.DACL.AddACEs(DirSD.DACL);
&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;DirSD.Free;
&nbsp;&nbsp;end;
end;

var
&nbsp;&nbsp;DirSD : TJwSecureFileObject;

&nbsp;&nbsp;SD, SD2 : TJwSecurityDescriptor;
&nbsp;&nbsp;pSA : PSecurityAttributes;
begin
&nbsp;&nbsp;JwInitWellKnownSIDs;

&nbsp;&nbsp;SD := TJwSecurityDescriptor.Create;

&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;SD.DACL.Add(TJwDiscretionaryAccessControlEntryAllow.Create(nil,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[afContainerInheritAce, afObjectInheritAce], FILE_ALL_ACCESS, JwWorldSID));

&nbsp;&nbsp;&nbsp;&nbsp;MergeParentDACL(&#039;.&#039;, SD);

&nbsp;&nbsp;&nbsp;&nbsp;pSA := SD.Create_SA();
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Win32Check(CreateDirectory(JWSCLTestFolder, pSA));
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SD.Free_SA(pSA);
&nbsp;&nbsp;&nbsp;&nbsp;end;
&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;SD.Free;
&nbsp;&nbsp;end;
end.
</pre></pre></p>
<p>The function <em>MergeParentDACL</em> receives the location of the parent folder and retrieves its security settings. Then its DACL is copied to the target security descriptor. JWSCL with <em>TargetSD.DACL.AddACEs</em> makes sure that the order of the ACEs are still correct (first deny then allow entries) by moving them accordingly.</p>
<p>In addition, there is a second, much easier way to achieve the same result.</p>
<p><pre><pre class="brush:delphi">var
&nbsp;&nbsp;SD : TJwSecurityDescriptor;
&nbsp;&nbsp;DirSD : TJwSecureFileObject;
begin
&nbsp;&nbsp;JwInitWellKnownSIDs;

&nbsp;&nbsp;Win32Check(CreateDirectory(JWSCLTestFolder, nil));

&nbsp;&nbsp;DirSD := TJwSecureFileObject.Create(JWSCLTestFolder);
&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;SD := DirSD.GetSecurityDescriptor([siDaclSecurityInformation]);
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SD.DACL.Add(TJwDiscretionaryAccessControlEntryAllow.Create(nil,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[afContainerInheritAce, afObjectInheritAce], FILE_ALL_ACCESS, JwWorldSID));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DirSD.SetSecurityDescriptor(SD, [siDaclSecurityInformation]);
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SD.Free;
&nbsp;&nbsp;&nbsp;&nbsp;end;
&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;DirSD.Free;
&nbsp;&nbsp;end;
</pre></pre></p>
<p>In this way we didn&#8217;t set the security descriptor directly when the folder was created. Nevertheless we get a combination of inheritace ACEs plus the explicit one (JwWorldSID).</p>
<h3>Note</h3>
<p>It is always a good idea to check whether SD.DACL (in above codes) is nil and if so ignore it or create a new and empty one to be used instead. It is always possible that a file or folder comes with a nil DACL which means either no access at all (flag DACLpresent) or everyone has full access (flag DACLpresent not available).</p>
<h3>I used the following JEDI units:</h3>
<p><pre><pre class="brush:delphi">uses
&nbsp;&nbsp;JwaWindows,

&nbsp;&nbsp;JwsclDescriptor,
&nbsp;&nbsp;JwsclTypes,
&nbsp;&nbsp;JwsclConstants,
&nbsp;&nbsp;JwsclKnownSid,
&nbsp;&nbsp;JwsclAcl,
&nbsp;&nbsp;JwsclMapping,
&nbsp;&nbsp;JwsclSecureObjects,
&nbsp;&nbsp;JwsclSid,
</pre></pre></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2010/03/24/setting-folder-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ACTRL_ACCESS Diagram</title>
		<link>http://blog.delphi-jedi.net/2010/02/11/actrl_access-diagram/</link>
		<comments>http://blog.delphi-jedi.net/2010/02/11/actrl_access-diagram/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 12:08:22 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/?p=631</guid>
		<description><![CDATA[I had some trouble with this rather complicated COM structure called ACTRL_ACCESS. So I post a diagram to show its design. Otherwise it would be a pity to leave it on my private hard drive. The ACTRL_ACCESS structure is used by the interface method IAccessControl::GetAllAccessRights (and others) which is rather hard to implement yourself because [...]]]></description>
			<content:encoded><![CDATA[<p>I had some trouble with this rather complicated COM structure called ACTRL_ACCESS. So I post a diagram to show its design. Otherwise it would be a pity to leave it on my private hard drive.</p>
<p><span id="more-631"></span></p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms693447%28VS.85%29.aspx">ACTRL_ACCESS</a> structure is used by the interface method <a href="http://msdn.microsoft.com/en-us/library/ms688536%28VS.85%29.aspx">IAccessControl::GetAllAccessRights</a> (and others) which is rather hard to implement yourself because the structure must be created in a single block of memory that can be freed by CoTaskMemFree.</p>
<p>So here is how it looks like</p>
<div id="attachment_632" class="wp-caption alignnone" style="width: 427px"><a href="http://blog.delphi-jedi.net/wp-content/uploads/2010/02/IAccessControl_GetAllAccessRights-structures.png" rel="lightbox[631]" title="IAccessControl_GetAllAccessRights structures"><img class="size-full wp-image-632 " title="IAccessControl_GetAllAccessRights structures" src="http://blog.delphi-jedi.net/wp-content/uploads/2010/02/IAccessControl_GetAllAccessRights-structures.png" alt="ACTRL_ACCESS design diagram" width="417" height="399" /></a><p class="wp-caption-text">ACTRL_ACCESS design diagram</p></div>
<p>Luckily, JWSCL will provide an implementation of IAccessControl so don&#8217;t worry.</p>
<p><br class="spacer_" /></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2010/02/11/actrl_access-diagram/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting file security with JWSCL</title>
		<link>http://blog.delphi-jedi.net/2008/04/28/setting-file-security-with-jwscl/</link>
		<comments>http://blog.delphi-jedi.net/2008/04/28/setting-file-security-with-jwscl/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 16:12:39 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[DACL]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Token]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/?p=170</guid>
		<description><![CDATA[Sometimes it is necessary to change the security settings of a file or folder for getting or denying write access. With JWSCL this task is made very easy. However there are some pitfalls to avoid. The following code will also be available in the example section of the source code. The application gets a file [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is necessary to change the security settings of a file or folder for getting or denying write access. With JWSCL this task is made very easy. However there are some pitfalls to avoid.</p>
<p>The following code will also be available in the example section of the source code. The application gets a file or folder name as parameter and tries to add the user with full access control. It even tries to get ownership if it can&#8217;t change the access control list.</p>
<p>First of all we need some JWSCL classes:</p>
<ul>
<li>TJwSecurityDescriptor<br />
A security descriptor contains all information about security of an object. It contains the owner and the access control list (also some other thing, we don&#8217;t need here)</li>
<li>TJwSecureFileObject<br />
This class provides methods to read and write security information on a file or folder. Despite its name it does also support folders. It even supports inheritance.<br />
You can access a file or folder through its name, a handle or the VCL class TFileStream.</li>
<li>TJwDAccessControlList<br />
This class contains methods to maintain a discreationary access control list (DACL). A DACL contains a list of users and their possible access on the object.</li>
<li>TJwSecurityId<br />
Every user is identified by a unique number which is maintained by this class.</li>
<li>TJwSecurityToken<br />
Every logged on user gets a security pass which contains information what she can do or not. We mainly use it to retrieve the user&#8217;s SID (TJwSecurityID)</li>
</ul>
<p>These classes are stored in the JWSCL units. We use the following ones:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp;JwaWindows,<br />
&nbsp;JwsclSid,<br />
&nbsp;JwsclToken,<br />
&nbsp;JwsclACl,<br />
&nbsp;JwsclDescriptor,<br />
&nbsp;JwsclSecureObjects,<br />
&nbsp;JwsclKnownSid;</div>
<p>The units above are necessary and contain all the classes described earlier Of course we have to declare the classes:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span><br />
&nbsp; UserToken : TJwSecurityToken;<br />
&nbsp; SD : TJwSecurityDescriptor;<br />
&nbsp; FileObject : TJwSecureFileObject;<br />
&nbsp; Owner : TJwSecurityId;<br />
&nbsp; DACL : TJwDAccessControlList;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw3">FileExists</span><span class="br0">&#40;</span><span class="kw3">ParamStr</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; <span class="kw3">exit</span>;</div>
<p>This example also shows how we can add well known Security Identifiers (SID) to a secured object. We have to initialize them. The variable JwWorldSID will then contain the correct SID for group Everyone. If we didn&#8217;t call it, we would get nil instead.<br />
JwInitWellKnownSIDs;</p>
<p>The next steps are creating the classes. We get the user name through her token and save the SID into Owner.<br />
Later we will use the Owner instance to add it into the security information of the object.</p>
<div class="dean_ch" style="white-space: wrap;">UserToken := TJwSecurityToken.<span class="me1">CreateTokenEffective</span><span class="br0">&#40;</span>MAXIMUM_ALLOWED<span class="br0">&#41;</span>;<br />
Owner := UserToken.<span class="me1">GetTokenOwner</span>;<br />
<span class="kw1">try</span><br />
&nbsp; FileObject := TJwSecureFileObject.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw3">ParamStr</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>The actual class which does all the work on the file/folder is TJwSecureFileObject. We just apply the first parameter.</p>
<p>Notice: A user can only change security information of an object if she has the right to do it. There are two options to allow it.</p>
<ol>
<li>The user is listed in the DACL. Additionally the right WRITE_DAC is granted for her.</li>
<li>The user is the owner. In this case she don&#8217;t need to be listed and allowed in the DACL. It is automatically granted</li>
</ol>
<p>We can check both version in one call.</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp;<span class="kw1">try</span><br />
&nbsp; <span class="kw1">if</span> <span class="kw1">not</span> FileObject.<span class="me1">AccessCheck</span><span class="br0">&#40;</span>WRITE_DAC<span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">begin</span></div>
<p>This call is very easy. If we can&#8217;t change the DACL, we can try to become the owner. The only way to become an owner is to enable a privilege called SE_TAKE_OWNERSHIP_NAME. It is usually only granted to Administrators.</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp; JwEnablePrivilege<span class="br0">&#40;</span>SE_TAKE_OWNERSHIP_NAME, pst_Enable<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; FileObject.<span class="me1">Owner</span> := Owner;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p>JwEnablePrivilege will fail, if it can&#8217;t activate the privilege. Otherwise we can set the file/folder&#8217;s owner to the token user.</p>
<p>The main work is done here. We get the default DACL from the existing object and adapt it.</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp;DACL := FileObject.<span class="me1">DACL</span>;</div>
<p>Adaption is done by adding the user to the DACL with full control. We additionally allow the Everyone group to demonstrate the well known Sids initialized by JwInitWellKnownSIDs. The last parameters (false) define that we don&#8217;t want the list to free the given SIDs (Owner and JwWorldSid) automatically.</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp;DACL.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_ALL, Owner, <span class="kw2">false</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; DACL.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, JwWorldSID, <span class="kw2">false</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>And finally we reset the DACL.</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp;FileObject.<span class="me1">SetDACL</span><span class="br0">&#40;</span>DACL<span class="br0">&#41;</span>;</div>
<p>The DACL of the file or folder will receive the newly created control entries in addition to its existing ones. If it contains inherited entries (entries from a parent folder) they will be conserved. However if you don&#8217;t retrieve the DACL and just use an empty one, all previously existing entries which are not inherited will be removed. Of course the inherited entries will still remain intact.</p>
<p>And of course we free all allocated resources</p>
<div class="dean_ch" style="white-space: wrap;"> &nbsp;<span class="kw1">finally</span><br />
&nbsp; &nbsp; FileObject.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</p>
<p><span class="kw1">finally</span><br />
&nbsp; Owner.<span class="me1">Free</span>;<br />
&nbsp; UserToken.<span class="me1">Free</span>;<br />
<span class="kw1">end</span>;<br />
<span class="kw1">end</span>.</div>
<p>Since I cut the source code into pieces, I&#8217;ll show it here in full glory</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">program</span> SetFileSecurity;</p>
<p><span class="coMULTI">{$APPTYPE CONSOLE}</span></p>
<p><span class="kw1">uses</span><br />
&nbsp; SysUtils,<br />
&nbsp; JwaWindows,<br />
&nbsp; JwsclSid,<br />
&nbsp; JwsclToken,<br />
&nbsp; JwsclAcl,<br />
&nbsp; JwsclDescriptor,<br />
&nbsp; JwsclSecureObjects,<br />
&nbsp; JwsclKnownSid;</p>
<p><span class="kw1">var</span><br />
&nbsp; UserToken : TJwSecurityToken;<br />
&nbsp; SD : TJwSecurityDescriptor;<br />
&nbsp; FileObject : TJwSecureFileObject;<br />
&nbsp; Owner : TJwSecurityId;<br />
&nbsp; DACL : TJwDAccessControlList;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw3">FileExists</span><span class="br0">&#40;</span><span class="kw3">ParamStr</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; <span class="kw3">exit</span>;</p>
<p>&nbsp; JwInitWellKnownSIDs;</p>
<p>&nbsp; UserToken := TJwSecurityToken.<span class="me1">CreateTokenEffective</span><span class="br0">&#40;</span>MAXIMUM_ALLOWED<span class="br0">&#41;</span>;<br />
&nbsp; Owner := UserToken.<span class="me1">GetTokenOwner</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; FileObject := TJwSecureFileObject.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw3">ParamStr</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1">//Make me owner if we cant access DACL</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw1">not</span> FileObject.<span class="me1">AccessCheck</span><span class="br0">&#40;</span>WRITE_DAC<span class="br0">&#41;</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//try to become owner</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JwEnablePrivilege<span class="br0">&#40;</span>SE_TAKE_OWNERSHIP_NAME, pst_Enable<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; FileObject.<span class="me1">Owner</span> := Owner;<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">end</span>;</p>
<p>&nbsp; &nbsp; &nbsp; DACL := FileObject.<span class="me1">DACL</span>;<br />
&nbsp; &nbsp; &nbsp; DACL.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_ALL, Owner, <span class="kw2">false</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; DACL.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, JwWorldSID, <span class="kw2">false</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; FileObject.<span class="me1">SetDACL</span><span class="br0">&#40;</span>DACL<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; &nbsp; FileObject.<span class="me1">Free</span>;<br />
&nbsp; &nbsp; <span class="kw1">end</span>;</p>
<p>&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; Owner.<span class="me1">Free</span>;<br />
&nbsp; &nbsp; UserToken.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
<span class="kw1">end</span>.</div>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/04/28/setting-file-security-with-jwscl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ACE order</title>
		<link>http://blog.delphi-jedi.net/2008/03/28/ace-order/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/28/ace-order/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 15:00:11 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACE]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[JWSCL]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/28/ace-order/</guid>
		<description><![CDATA[The order of access control elements in an access control list is the following: explicit Deny elements explicit Allow elements inherited Deny elements inherited Deny elements It is also called the canonical order. Any order that doesn&#8217;t follow the semantic above, hasn&#8217;t got to be wrong. In fact you can define an order that solves [...]]]></description>
			<content:encoded><![CDATA[<p>The order of access control elements in an access control list is the following:</p>
<ol>
<li>explicit Deny elements</li>
<li>explicit Allow elements</li>
<li>inherited Deny elements</li>
<li>inherited Deny elements</li>
</ol>
<p>It is also called the <u>canonical order</u>.</p>
<p><span id="more-90"></span><br />
Any order that doesn&#8217;t follow the semantic above, hasn&#8217;t got to be wrong. In fact you can define an order that solves your situation (some MS software does).  But you shouldn&#8217;t do this where a user can find and edit it (like regkeys and files), otherwise the security descriptor editor in Windows Explorer and registry editor will show you a nasty warning. Moreover if you mix them up, the result of an AccessCheck may be difficult to understand for the user (bad user experience). And eventually, the unusual order could also create a security risk because a user could access a resource although it is explicitly denied.  It happens when the user is (accidentally) granted access by placing an allow ACE in front of a deny ACE and both ACEs pointing to the same SID. The reason comes from the fact that an access check iterates through the (linear) access control list and stops when the first SID was found. So the first SID wins and defines whether access is granted or denied.</p>
<hr size="2" width="100%" />Did you know? <strong>JWSCL </strong>creates the canonical order automatically! So you don&#8217;t have to worry.</p>
<ul>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDiscretionaryAccessControlEntryDeny.html">TJwDiscretionaryAccessControlEntryDeny</a><br />
<a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDiscretionaryAccessControlEntryAllow.html">TJwDiscretionaryAccessControlEntryAllow</a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.html">JwsclAcl</a></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html">TJwSecurityDescriptor</a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.html">JwsclDescriptor</a></li>
</ul>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">uses</span> JwsclAcl, JwsclDescriptor;</p>
<p><span class="kw1">var</span> Desc : TJwSecurityDescriptor;<br />
<span class="kw1">begin</span><br />
Desc := TJwSecurityDescriptor.<span class="me1">Create</span>;</p>
<p>Desc.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryDeny.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span>afInheritedAce<span class="br0">&#93;</span>, GENERIC_ALL, Sid4<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
Desc.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span>afInheritedAce<span class="br0">&#93;</span>, GENERIC_ALL, Sid3<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
Desc.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_ALL, Sid1<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
Desc.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryDeny.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_ALL, Sid2<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p>The result can be seen here:</p>
<ol>
<li>deny explicit <strong>Sid2 </strong></li>
<li>allow explicit <strong>Sid1</strong></li>
<li>deny inherited <strong>Sid4</strong></li>
<li>allow inherited <strong>Sid3</strong></li>
</ol>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/28/ace-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use a SecurityAttribute structure</title>
		<link>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 15:43:25 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACE]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[CreateFile]]></category>
		<category><![CDATA[descriptor]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[security attribute]]></category>
		<category><![CDATA[Sid]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/</guid>
		<description><![CDATA[Many developers know that mysterious parameter some WinAPI function offer to use. What I am talking about is a parameter called lpSecurityAttributes. The documentation states that it allows to change the security descriptor of the desired element. The most known function is CreateFile which I want to use here. It allows to create a file [...]]]></description>
			<content:encoded><![CDATA[<p>Many developers know that mysterious parameter some WinAPI function offer to use. What I am talking about is a parameter called <em>lpSecurityAttributes</em>. The documentation states that it allows to change the security descriptor of the desired element. The most known function is <em>CreateFile</em> which I want to use here. It allows to create a file and more. But we stay to create a file. Usually we set the parameter <em>lpSecurityAttributes</em> simply to <strong>NULL</strong>/<strong>nil</strong> because this sets the default security configuration we want to use in most of the times (this also includes file/folder inheritance).</p>
<p>However sometimes we want to use our own security configuration to allow other participants to access a resource we created. JWSCL makes this task much more simple than using the security runtime function written in plain C. We do not have to create the security descriptor from scratch. The JWSCL methods allow us to get the default security descriptor and adapt it to our needs.<span id="more-30"></span></p>
<p>Let&#8217;s start with the required classes and methods we need to add another user who wants access.</p>
<ul>
<li style="list-style-type: none; list-style-image: none; list-style-position: outside"></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html" title="Go to JWSCL Online Documentation."><em>TJwSecurityDescriptor</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.html" title="Go to JWSCL Online Documentation."><em>JwsclDescriptor</em></a></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDAccessControlList.html" title="Go to JWSCL Online Documentation."><em>TJwDAccessControlList</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.html" title="Go to JWSCL Online Documentation."><em>JwsclAcl</em></a></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDiscretionaryAccessControlEntryAllow.html" title="Go to JWSCL Online Documentation."><em>TJwDiscretionaryAccessControlEntryAllow</em></a> in unit <em>JwsclAcl</em></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html" title="Go to JWSCL Online Documentation."><em>TJwSecurityID</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.html" title="Go to JWSCL Online Documentation."><em>JwsclSid</em></a></li>
</ul>
<p>That&#8217;s all.</p>
<p>Since the system already creates us an adequate security access list we want to continue using it. For this reason <em>TJwSecurityDescriptor</em> implements a constructor called <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#CreateDefaultByToken" title="Go to JWSCL Online Documentation.">CreateDefaultByToken</a> that creates such a security access list automatically.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">uses</span> JwaWindows,&#8230;,JwsclDescriptor, JwsclAcl, JwsclTypes, JwsclStrings;<br />
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
<span class="kw1">begin</span><br />
&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">Writeln</span><span class="br0">&#40;</span>SD.<span class="me1">Text</span><span class="br0">&#41;</span>;</div>
<p>The output may look like this, depending on your Windows system.</p>
<blockquote><p>Owner: chris@ Christian (S-1-5-21-2735234258-346234578-4357623456-1000) []<br />
Group: chris@ None (S-1-5-21-2735234258-346234578-4357623456-513) [sidaGroupMandatory]<br />
DACL:<br />
ACE Count: 3<br />
\#0<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 268435456, 0&#215;10000000<br />
SID: chris@ Christian (S-1-5-21-2735234258-346234578-4357623456-1000) []<br />
#1<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 268435456, 0&#215;10000000<br />
SID: NT-AUTORIT-T@ SYSTEM (S-1-5-18) []<br />
#2<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 2684354560, 0xA0000000<br />
SID: (S-1-5-5-0-151391) []</p>
<p>SACL:<br />
ACE Count: 0<br />
\</p></blockquote>
<p>(<em>No map class given</em> is shown because the security descriptor class does not know the type of secured object. A map class (derived from <a href="http://jwscldoc.delphi-jedi.net/JwsclMapping.TJwSecurityGenericMapping.html" title="Go to JWSCL Online Documentation.">TJwSecurityGenericMapping</a>)  can convert the AccessMask to an human readable string)</p>
<p>The system sets the owner to the current token owner of the process or thread. It also adds my user account and the SYSTEM principal with full access (0&#215;10000000 = GENERIC_ALL). The unknown principal with the Sid S-1-5-5-0-151391 describes the loggon session Sid. At a later point we will remove it.<br />
For the discussion we want to add another principal called Alice so she can get read access to the file/folder. Because we need the Alice&#8217;s Sid we have to add another variable called <em>AliceSid</em>.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; AliceSid : TJwSecurityId;<br />
<span class="kw1">begin</span><br />
&nbsp; &nbsp;SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;AliceSid := TJwSecurityId.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,<span class="st0">&#8216;Alice&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;SD.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, AliceSid, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="kw3">Writeln</span><span class="br0">&#40;</span>SD.<span class="me1">Text</span><span class="br0">&#41;</span>;</div>
<p>In the code above a new class instance of <em>TJwDiscretionaryAccessControlEntryAllow</em> is added to the DACL. Because we create just a simple file we do not need special flags thus an empty flag set [] is applied. The access mask parameter will receive GENERIC_READ as the maximum possible access to this file granted to Alice. The last parameter (OwnSid) is set to <strong>true</strong> and defines that the access control list (here property <em>DACL</em>) destroys the instance <em>AliceSid</em> at the end.</p>
<p>The resulting security descriptor has now a new access control entry. The output is the same like above but with this additional element.</p>
<blockquote><p>ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 2147483648, 0&#215;80000000<br />
SID: chris@ Alice (S-1-5-21-2735234258-346234578-4357623456-1008) []</p></blockquote>
<p>Now we can arrive at the part where <a href="http://msdn2.microsoft.com/en-us/library/aa363858(VS.85).aspx" title="Go to MSDN."><em>CreateFile</em></a> comes in. How can we create a pointer to a <a href="http://msdn2.microsoft.com/en-us/library/aa379560(VS.85).aspx" title="Go to MSDN.">TSecurityAttribute</a> type? It is really simple! We just have to declare some more helper variables that <em>CreateFile</em> needs and use <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#Create_SA" title="Go to JWSCL Online Documentation."><em>Create_SA</em></a>.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; &nbsp;AliceSid : TJwSecurityId;<br />
&nbsp; &nbsp; &nbsp;SecAttrPtr : PSecurityAttributes;<br />
&nbsp; &nbsp; &nbsp;Handle : <span class="kw4">THandle</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">DeleteFile</span><span class="br0">&#40;</span><span class="st0">&#8216;testfile&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; AliceSid := TJwSecurityId.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,<span class="st0">&#8216;Alice&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; SD.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, AliceSid, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; SecAttrPtr := SD.<span class="me1">Create_SA</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; Handle := jwaWindows.<span class="me1">CreateFile</span><span class="br0">&#40;</span><span class="st0">&#8216;testfile&#8217;</span>, FILE_ALL_ACCESS, <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">Pointer</span><span class="br0">&#40;</span>SecAttrPtr<span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CREATE_NEW, FILE_ATTRIBUTE_NORMAL,<span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> handle = ERROR_INVALID_HANDLE <span class="kw1">then</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">RaiseLastOSError</span>;<br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; SD.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p><a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#Create_SA" title="Go to JWSCL Online Documentation."><em>Create_SA</em></a> from <em>TJwSecurityDescriptor</em> creates the necessary memory structure (<em>SecurityAttributes</em>) and returns a pointer of type <em>PSecurityAttributes</em>. The pointer is used in CreateFile to apply our own security descriptor. Our access control list of the newly created file will contain all the elements you see above as output. In tests, both the creation flags CREATE_NEW and CREATE_ALWAYS never change the security attributes after the file has already been created. Although MSDN explains it correctly for CREATE_ALWAYS it does not say anything about CREATE_NEW in this context. Because of this we simply delete the file everytime.</p>
<p>The security editor of Windows Explorer shows us the new descriptor.</p>
<p><img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-3.jpg" alt="Bild" height="476" width="350" /></p>
<p>As you can see we did not remove the LogonSession Sid from the security descriptor. The logon SID resides in the principal&#8217;s token. It is used to add allow or deny access to a secured object but only for the time the user is logged on. This is because every time the user logs on, a new session Sid is generated. Additionally all calls to the LogonUser API get their own session Sid so a logon Sid is a fine grained access control that allows us not only to restrict access between users but also control access between several different instances of a user (consider the user itself as a class and all user tokens as instances of this class) .<br />
However removing this LogonSID will be our next task because we do not need it here.</p>
<p>JWSCL supports us with a function that returns a <em>TJwSecurityId</em> of the logon session. The function is called <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html#JwGetLogonSID" title="Go to JWSCL Online Documentation."><em>JwGetLogonSID</em></a> and resides in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html" title="Go to JWSCL Online Documentation."><em>JwsclKnownSid</em></a> that we have to include additionally. Unfortunately there is a bug in <a href="http://blog.delphi-jedi.net/2008/03/03/jwscl-release-revision-316/" title="Go to downloads.">revision 316</a> that makes it impossible to use it. For this reason I added an adapted version of the whole unit <em>JwsclKnownSid</em> . You can get it <a href="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/jwsclknownsid.zip">here</a>.</p>
<p>Let&#8217;s see how we can get rid of the logon sid.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; LogonSid,<br />
&nbsp; &nbsp; AliceSid : TJwSecurityId;<br />
&nbsp; &nbsp; SecAttrPtr : PSecurityAttributes;<br />
&nbsp; &nbsp; Handle : <span class="kw4">THandle</span>;<br />
&nbsp; &nbsp; PosInt : <span class="kw4">Integer</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; LogonSid := JwGetLogonSID;</p>
<p>&nbsp; PosInt := SD.<span class="me1">DACL</span>.<span class="me1">FindSID</span><span class="br0">&#40;</span>LogonSid<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span> PosInt &gt;= <span class="nu0">0</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; &nbsp;SD.<span class="me1">DACL</span>.<span class="me1">Remove</span><span class="br0">&#40;</span>PosInt<span class="br0">&#41;</span>;<br />
&nbsp; LogonSid.<span class="me1">Free</span>;<br />
&#8230;</div>
<p><em>JwGetLogonSid </em>returns a new instance that can be used to search for the logon Sid in the DACL. The method <em><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwSecurityAccessControlList.html#FindSID" title="Go to JWSCL Online Documentation.">FindSID</a> </em>in <em>TJwDAccessControlList</em> goes through the whole access control list and returns the zero based index of the entry we search for. If it were not to be found we would get a negative result value, but this is not the case here (although we check for it because it is good programming style). At the end do not forget to remove the instance.</p>
<p><img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-4.jpg" alt="Bild" height="473" width="350" /></p>
<p>The new security descriptor does not contain the annoying logon Sid anymore. Eventually we can use this discussed approach not only for <em>CreateFile</em> but also for <em>CreatePipe</em>, <em>CreateProcess</em>, RegCreateKeyEx, <em>RegSaveKeyEx</em>, <em>CreateFileMapping</em> and many more. I used <em>CreateFile</em> so you can easily look up the descriptor in the security editor of Windows Explorer.</p>
<p>The next article will discuss how we can use inheritance and why there are no inherited access control elements in our created security descriptor although the parent folder hand them down.</p>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
