<?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; Administrator</title>
	<atom:link href="http://blog.delphi-jedi.net/tag/administrator/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>The Case of The Failed Loading of The User Profile</title>
		<link>http://blog.delphi-jedi.net/2009/07/20/the-case-of-the-failed-loading-of-the-user-profile/</link>
		<comments>http://blog.delphi-jedi.net/2009/07/20/the-case-of-the-failed-loading-of-the-user-profile/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 20:52:45 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[profile]]></category>
		<category><![CDATA[Registry]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/?p=370</guid>
		<description><![CDATA[Some time ago, I was in the situation to set up a new computer with a Windows XP 64 CD. Well it doesn&#8217;t matter that it was 64bit.  However, I always create a separate partitions for Windows XP. And because user profiles tend to get big, I moved a new user to a second partition. [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I was in the situation to set up a new computer with a Windows XP 64 CD. Well it doesn&#8217;t matter that it was 64bit.  However, I always create a separate partitions for Windows XP. And because user profiles tend to get big, I moved a new user to a second partition. (See the Internet how it is done) But as if it wasn&#8217;t enough I thought that I also could move the Administrator profile that easy. Way wrong!</p>
<p><span id="more-370"></span>First of all, copying a loaded user profile isn&#8217;t possible without the BACKUP privilege. You can&#8217;t open already opened files a second time (share deny). The solution is: Either you can use a backup application that runs with the special privilege or you can just use another administrative user (which I did).</p>
<p>There are some keys which have absolute path variables that must be changed, too. But that is a minor problem. The big problem was that Windows could not load the profile of the Administrator. Windows tried to start up the users desktop but then failed with the dialog:  &#8220;Windows failed to load user profile.&#8221;</p>
<p>It got more confusing because every other user in the system didn&#8217;t suffer from this problem. And to top it,  sometimes I could successfully login to the Administrator account.</p>
<h3>Solution</h3>
<p>In such cases the event manager is a good way to find the error source. In my case, it told me that &#8220;NTUSER.DAT&#8221; (this is where Windows stores the current user keys called a <a href="http://msdn.microsoft.com/en-us/library/ms724877%28VS.85%29.aspx">registry hive</a>) was already opened by another process. I really can&#8217;t say why this was the case because it shouldn&#8217;t be the case shortly after a fresh boot up. (Of course, the file was correct, it had no write protection, and security was set accordingly)</p>
<p>In such a case you usually have some ways to fix the problem:</p>
<p>1. Reinstall Windows<br />
Good choice. But too much work &#8212; and who will promise me that it won&#8217;t happen again?</p>
<p>2. Leave Administrator where it was<br />
Also good choice. And I tried it. However my Windows could not handle two profile places somehow and the very same error occured with the old profile location.</p>
<p>3. Live with the situation<br />
Some people do it. I thought about it, too, I have to admit. That&#8217;s because there is a user called root in the system now.</p>
<p>4. Find the bug and ignore how much time it will cost.<br />
Ouch. I don&#8217;t have time, so not a good option.</p>
<p>5.Create a workaround.<br />
YES! That&#8217;s what I did.</p>
<p>So I came up with a practical and fast solution. As I&#8217;ve already written, the user&#8217;s registry hive could not be loaded.<br />
Why? I don&#8217;t know much about it but the registry file was blocked.<br />
Well, I&#8217;ve found out that if the Administrator registry hive was already loaded (HKEY_USERS\S-1-5-21-xxx-500 is visible) the Windows could successfully logon the Administrator. I simulated this situation by manually loading the user&#8217;s profile using the <a href="http://msdn.microsoft.com/en-us/library/bb762281%28VS.85%29.aspx" target="_blank">LoadUserProfile</a> function from Windows API. Well, it always worked!</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaWindows,<br />
&nbsp; JwsclToken,<br />
&nbsp; jwscltypes;</p>
<p><span class="kw1">var</span><br />
&nbsp; &nbsp;T : TJwSecurityToken;<br />
&nbsp; &nbsp;ProfileInfo : TJwProfileInfo;<br />
<span class="kw1">begin</span><br />
&nbsp; T := TJwSecurityToken.<span class="me1">CreateTokenByProcess</span><span class="br0">&#40;</span><span class="nu0">0</span>, TOKEN_ALL_ACCESS<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; T.<span class="me1">LoadUserProfile</span><span class="br0">&#40;</span>ProfileInfo, <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; T.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p>The next step was to do it automatically before an Administrator could logon. Of course, I could have created a service which loads the user profile at startup but I wanted to be fast so there is another option called Task Scheduler.<br />
Every Windows startup I let run the code above as a simple task with Administrator credentials (needs password).</p>
<p>I don&#8217;t know whether the Task Scheduler loads the profile (I would guess so). However the problem is that the profile could be unloaded after the process has ended. So the hive would be removed and logon would no more be possible.</p>
<p>Thus the code above just loads the profile but does not unload it. Since profiles are not watched by Windows (at least in XP) the registry hive  stays loaded and a logon attempt will work (T.Free won&#8217;t unload it!).</p>
<h3>JEDI Windows Security Code Library used features</h3>
<ul>
<li><a href="http://jwscldoc.delphi-jedi.net/TJwSecurityToken.html">TJwSecurityToken</a> (class)<a href="http://jwscldoc.delphi-jedi.net/TJwSecurityToken.html"><br />
</a></li>
<li>TJwSecurityToken.<a href="http://jwscldoc.delphi-jedi.net/TJwSecurityToken_LoadUserProfile@TJwProfileInfo@TJwProfileMembers.html" target="_blank">LoadUserProfile</a> (method)</li>
</ul>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2009/07/20/the-case-of-the-failed-loading-of-the-user-profile/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What is a SID?</title>
		<link>http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 22:55:01 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[Group]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Principal]]></category>
		<category><![CDATA[security identifier]]></category>
		<category><![CDATA[Sid]]></category>
		<category><![CDATA[well-known]]></category>
		<category><![CDATA[wellknown]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/</guid>
		<description><![CDATA[SID is the short form of Security Identifier. It is used to uniquely name a user (like Alice or Bob), a group (like Users or Administrators) or a system account (like SYSTEM or Network Service). Important to understand is that not only human users get a SID but also system parts like the System or [...]]]></description>
			<content:encoded><![CDATA[<p>SID is the short form of <em><strong>S</strong>ecurity <strong>Id</strong>entifier</em>. It is used to uniquely name a user (like Alice or Bob), a group (like Users or Administrators) or a system account (like SYSTEM or Network Service). Important to understand is that not only human users get a SID but also system parts like the System or Network Service and even computers itself. An inhuman principal does not have a password so it cannot be get logged on in the traditional sense. They just exist as soon as the system is booted up. If you read about a principal in this context, it is either a human user, a group or a system user. Because there is often no need to tell them apart, we just refer to it as a <em>principal</em>.<span id="more-35"></span></p>
<p>A principal is not recognized by its name, but by its unique SID which is a dynamic structure in the C world. It is made of three parts:</p>
<ul>
<li>A revision level that defines the sid version and currently is set to one (1).</li>
<li>A 48 bit number which defines the authority that created the SID. (e.g. Windows NT defines 5)</li>
<li>An array of numbers which uniquely identify the principal within the authority. The last number is sometimes called a relative identifier (RID).</li>
</ul>
<p>A template string of a SID may look like this. (Brackets [ ] define optional parameters):</p>
<blockquote><p>S &#8211; R &#8211; I [- S<small>1</small> [- ... - S<small>n </small>- [RID]]]</p></blockquote>
<ul>
<li>S declares the string as a SID.</li>
<li>R defines the revision number.</li>
<li>S<small>1</small>-&#8230;-S<small>n</small> defines the sub-authority. It is an array of n numbers that identifies a domain or machine.</li>
<li>RID is the relative principal identifier. It is an unique, sequential and increasing number for a principal assigned by the authority. In Windows, known RIDs start with 500 which defines the Administrator account. RIDs of 1000 and above are used for the usual users and groups. The RID is not an independant part of a SID. In fact it is really the last part of the sub-authority array. Thus the Windows API and JWSCL do not have functions to read or alter it directly. To do so you must access the sub-authority array.</li>
</ul>
<p>Windows defines some well known SIDs and some well known RIDs. Well knowns SIDs look the same on every computer because they have no domain or machine identifier. On the other hand there are well known RIDs which are system or domain relative. I write system as in operating system, because even on a multi boot computer the SIDs vary between different operating systems if they are not cloned. Several identical SIDs on different machines may lead to security problems. In this case there is a little helper called <a href="http://technet.microsoft.com/de-de/sysinternals/bb897418(en-us).aspx" title="Go go MS TechNET">NewSID</a> that helps changing the SID.</p>
<p>You can find a list of well known SIDs and RIDs in <a href="http://support.microsoft.com/?scid=kb%3Ben-us%3B243330&amp;x=15&amp;y=12" title="Go to MSDN.">MSDN</a>. Some important well known SIDs are shown here:</p>
<blockquote>
<ul>
<li>S-1-1-0 : The <em>Everyone </em>group</li>
<li>S-1-5-4 : The <em>Interactive</em> identifier which allows interactive logon.</li>
<li>S-1-5-5-#-# : The <em>Session Logon</em> SID (more about it <a href="http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/" title="Go to blog article.">here</a> ) &#8220;#&#8221; is a placeholder for the high and low value of the logon session ID called LUID. It can be retrieved from the principal&#8217;s token.</li>
</ul>
</blockquote>
<p>Here are some important well known RIDs. The ellipsis &lt;&#8230;&gt; represent the system or domain.</p>
<blockquote>
<ul>
<li>S-1-5-&lt;&#8230;&gt;-500 : The <em>Administrator </em>account</li>
<li>S-1-5-&lt;&#8230;&gt;-501 : The <em>Guest </em>account</li>
<li>S-1-5-32 : The <em>Builtin Local</em> groups which identifies a member of a Builtin database</li>
</ul>
</blockquote>
<p>Some well known RIDs are appended to the <em>Builtin</em> SID to define them as special accounts and that they are hard-coded into the system.</p>
<blockquote>
<ul>
<li>S-1-5-32-544 : The Administrators group</li>
<li>S-1-5-32-545 : The Users group</li>
<li>S-1-5-32-546 : The Guest group</li>
</ul>
</blockquote>
<hr size="2" width="100%" />JWSCL provides an easy way to access all these elements.<br />
Let start with the base element <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html" title="Go to Online Documentation...">TJwSecurityID</a> which resides in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.html" title="Go to Online Documentation...">JwsclSid</a>. It encapsulates the C style structure SID and allows to use it in a more object-oriented way. In this way we get rid of manipulating memory directly.<br />
There are many different ways to create a SID instance in JWSCL:</p>
<ol>
<li>Create a copy of an existing <em>TJwSecurityID </em>instance</li>
<li>Create a copy of an existing SID structure</li>
<li>Create a copy from a nested SID structure within a <a href="http://msdn2.microsoft.com/en-us/library/aa379595.aspx" title="Go to MSDN.">SidAndAttributes</a> structure</li>
<li>Create a SID from an authority and an identifier</li>
<li>Create a well known SID from known constants</li>
<li>Create a SID from a string that represents the SID</li>
<li>Create a SID from the combination of a system and user name</li>
</ol>
<p>These ways are accomplished by the <em>TJwSecurity </em>constructors:</p>
<ol>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SecurityID: TJwSecurityId); </code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSID); </code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSidAndAttributes); </code></li>
<li><pre><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const Authorities: TJwSubAuthorityArray; Identifier: TSidIdentifierAuthority);
</code></pre><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const Authorities: array of Cardinal; Identifier: TSidIdentifierAuthority);</code></li>
<li><code>constructor &lt;strong&gt;CreateWellKnownSid&lt;/strong&gt;(WellKnownSidType: TWellKnownSidType; DomainSid: TJwSecurityId = nil);</code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SIDString: TJwString);</code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SystemName, AccountName: TJwString);</code></li>
</ol>
<p><u>1. </u><pre><code>&lt;u&gt;&lt;strong&gt;Create&lt;/strong&gt;(const SecurityID: TJwSecurityId)&lt;/u&gt;
</code></pre>Sometimes it is necessary to get a second copy of an already existing SID instance. Use the copy constructor for that task. It makes a duplicate so you can use the second instance exactly like the first one. This comes really handy if you write a multiple threads environment where threads create and free SID instances. Be aware that you cannot change the SID content afterwards. Most properties do not allow to change their values. So you cannot change the identifier, sub-authorities or the well known SID type. This is because a change may lead to security problems. Consider that you could change a SID while it is used in another part of your application (a thread). If this part does a security access check using your SID instance while you change the SID content, the result are unpredictable or even risky even if you apply thread safety. This is because the AccessCheck could return true even if the original SID does not have access. It only may return access allowed because the SID was changed. If you want to change an existing SID instance, you have to create a copy using one of the constructors. For example get the SubAuthority property, alter it and pass it to one of the conststructors in #4. In this way the existing SID instances won&#8217;t be affected.<u><code></code></u></p>
<p><u><code>2./3. &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSID)/</code><code>&lt;strong&gt;Create&lt;/strong&gt;(const SID: PSidAndAttributes);</code></u><br />
It is quite uncommon to create an instance by using a SID structure. However if you ever encounter such a task you must be aware that the assigned SID is copied (not refered to it) into the instance. The <em>const </em>key-word in front of the parameter name denotes that parameter <em>SID </em>will not be altered internally. Thus there is no other way than to copy it. Also be aware that the SID memory is checked for a correct SID structure; otherwise <span class="normal">EJwsclInvalidSIDException</span> will be raised.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span> SID : PSID;<br />
&nbsp; &nbsp; SIDInstance : TJwSecurityID; </p>
<p><span class="kw1">begin</span><br />
&nbsp; <span class="kw3">GetMem</span><span class="br0">&#40;</span>SID, requestedSIDSize<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//obtain SID structure here</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID<span class="br0">&#40;</span>SID<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; <span class="kw3">FreeMem</span><span class="br0">&#40;</span>SID<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//do stuff with SIDInstance here&#8230;</span><br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; SIDInstance.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
<span class="kw1">end</span>;</div>
<p><u>4. <code>&lt;strong&gt;Create&lt;/strong&gt;(const Authorities: </code><code>TJwSubAuthorityArray; Identifier: TSidIdentifierAuthority);</code></u><br />
It is easily possible to create a SID from scratch by using the internal parts of a SID. In the following example we create the well known group SID &#8220;Everybody&#8221;.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclSID;<br />
<span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, SECURITY_WORLD_SID_AUTHORITY<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">WriteLn</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; SIDInstance.<span class="me1">Free</span>;<br />
<span class="kw1">end</span>;</div>
<p>The constant five bytes long array identifier <em>SECURITY_WORLD_SID_AUTHORITY</em> from JwaWindows is declared as follow:</p>
<div class="dean_ch" style="white-space: wrap;">SECURITY_WORLD_SID_AUTHORITY: TSidIdentifierAuthority = <span class="br0">&#40;</span>Value: <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>We add a subauthority of zero and get the shown output. The emtpy brackets usually contains the SID&#8217;s attributes which we did not use.</p>
<blockquote><p>Everybody (S-1-1-0) []</p></blockquote>
<p>The second constructors receives an declared array which describes the sub-authoroties. With the help of it we can alter an existing instance. The example shown below demonstrates how to alter the Everybody group by changing the sub authority array.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance,SIDInstance2 : TJwSecurityID;<br />
&nbsp; &nbsp; SubAuths : TJwSubAuthorityArray;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, SECURITY_WORLD_SID_AUTHORITY<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; </p>
<p>&nbsp; <span class="co1">//get copy of this array</span><br />
&nbsp; SubAuths := SIDInstance.<span class="me1">SubAuthorityArray</span>; &nbsp;<span class="co1">//increase first array member </span></p>
<p>&nbsp; <span class="kw3">Inc</span><span class="br0">&#40;</span>SubAuths<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; </p>
<p>&nbsp; <span class="co1">//create new instance and use the new authorities</span><br />
&nbsp; SIDInstance2 := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span>SubAuths, SIDInstance.<span class="me1">IdentifierAuthority</span><span class="br0">&#41;</span>; </p>
<p>&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance2.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><u></u></p>
<p><u>5. <code>&lt;strong&gt;CreateWellKnownSid&lt;/strong&gt;(WellKnownSidType: TWellKnownSidType;...);</code></u><br />
<code></code>The way to create a well known SID as shown in part #4 is very inconvenient. Thus the class <em>TJwSecurityID </em>contains a constructor that directly allows us to use one of the well known SID constants declared in JwaWindows or JwaVista (only if you need the new Vista definitions). The enumeration type <code>WELL_KNOWN_SID_TYPE </code>holds all known SIDs.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">type</span><br />
&nbsp; WELL_KNOWN_SID_TYPE = <span class="br0">&#40;</span><br />
&nbsp; &nbsp; WinNullSid <span class="coMULTI">{= 0}</span>,<br />
&nbsp; &nbsp; WinWorldSid <span class="coMULTI">{= 1}</span>,<br />
&nbsp; &nbsp; WinLocalSid <span class="coMULTI">{= 2}</span>,<br />
&nbsp; &nbsp; WinCreatorOwnerSid <span class="coMULTI">{= 3}</span>,<br />
&nbsp; &nbsp; WinCreatorGroupSid <span class="coMULTI">{= 4}</span>,<br />
&nbsp; &nbsp; &#8230;<br />
&nbsp; &nbsp; <span class="co1">//JwaVista</span><br />
&nbsp; &nbsp; WinLowLabelSid <span class="coMULTI">{= 66}</span>,<br />
&nbsp; &nbsp; WinMediumLabelSid <span class="coMULTI">{= 67}</span>,<br />
&nbsp; &nbsp; WinHighLabelSid <span class="coMULTI">{= 68}</span>,<br />
&nbsp; &nbsp; WinSystemLabelSid <span class="coMULTI">{= 69}</span>,<br />
&nbsp; &nbsp; &#8230;<br />
<span class="br0">&#41;</span>;</div>
<p>The CreateWellKnownSid constructor uses the definition from JwaVista. So if you do not use JwaVista in your use clause you have to add it now because the constructor uses the new version. In this case you should add JwaVista in front of JwaWindows since Delphi uses by default an identifier that is declared in the latest included unit.<br />
The code below uses declarations introduced by JwaWindows but not JwaVista.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;, JwaVista, JwaWindows, &#8230;;</div>
<p>Of course it is possible to explicitly refer to JwaVista. This task is done by adding the unit name in front of the identifier seperated by a point operator.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;,<br />
&nbsp; JwaVista,<br />
&nbsp; JwaWindows, &#8230;;</p>
<p><span class="kw1">var</span> SidType : &lt;code&gt;JwaVista.<span class="me1">TWellKnownSidType</span>; &lt;/code&gt;<br />
<span class="kw1">begin</span><br />
&nbsp; SidType := JwaVista.<span class="me1">WinNullSid</span>;<br />
&#8230;</div>
<p>Creating a well known SID in this way needs getting used to:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaVista,<br />
&nbsp; JwaWindows,<br />
&nbsp; JwsclSID;</p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">CreateWellKnownSid</span><span class="br0">&#40;</span>JwaVista.<span class="me1">WinWorldSid</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p>If you start programming and want to use Vista stuff from the beginning, you should add JwaVista after JwaWindows. In this case you can easily create a well known SID.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaWindows,<br />
&nbsp; JwaVista,<br />
&nbsp; JwsclSID;</p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">CreateWellKnownSid</span><span class="br0">&#40;</span>WinWorldSid<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><u>6. <code>&lt;strong&gt;Create&lt;/strong&gt;(const SIDString: TJwString);</code></u><code></code><code></code></p>
<p>A very convenient way to create a SID instance is to use the SID string format that was described at the very first part of this discussion.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaWindows,<br />
&nbsp; JwaVista,<br />
&nbsp; JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8216;S-1-1-0&#8242;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">on</span> E : EJwsclWinCallFailedException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//do error stuff here and exit.</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><strong>Be warned</strong> that the constructor may raise an exception if you do not comply with the SID string format.</p>
<p><u></u></p>
<p><u>7. <code>&lt;strong&gt;Create&lt;/strong&gt;(const SystemName, AccountName: TJwString);</code></u></p>
<p>There is sometimes the necessity to get the SID of a user on a specific system. E.g. the user enters his name and the domain or machine name, in this case you can create a SID by a principal&#8217;s name. The following example creates a SID instance from the given user on the local system.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp;JwaWindows,<br />
&nbsp;JwaVista,<br />
&nbsp;JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
&nbsp; &nbsp; UserName &nbsp;: <span class="kw4">String</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">ReadLn</span><span class="br0">&#40;</span>UserName<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,UserName<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">on</span> E : EJwsclWinCallFailedException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//do error stuff here and exit.</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw3">WriteLn</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<hr size="2" width="100%" />In the end, I want to show you some very useful methods. Here they are :</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">function</span> GetText<span class="br0">&#40;</span>ignoreExceptions: <span class="kw4">boolean</span> = <span class="kw2">False</span><span class="br0">&#41;</span>: TJwString;</div>
<p>Call this method if you need display information of the SID instance. It shows information about the domain and user name, the SID string and attributes if any. Set the parameter <em>ignoreExceptions </em>to false if you want to get an exception if a SID could not be translated into an name. Set it to true if you just want an empty string to be displayed instead. If you don&#8217;t want to use too many exception handling mechanisms, you should set the parameter value to true.</p>
<blockquote><p>Everybody (S-1-1-0) []</p></blockquote>
<p>I already used this method to show you the output earlier in this article.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> CachedSystemName : TJwString; writable;</div>
<p><em>GetText </em>uses the system or domain name once assigned to the constructor. However sometimes it is necessary to change it. The property CachedSystemName gets or sets this system or domain name.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> StringSID: TJwString readonly;</div>
<p>This readonly property contains the SID&#8217;s string representation as shown in GetText.</p>
<div class="dean_ch" style="white-space: wrap;">property WellKnownSidType: TWellKnownSidType; readonly;</div>
<p>Use the WellKnownSidType property if you need to know of what well known type the SID consists of. Be aware that you should not rely on it if the return value is WinNullSid, because in this case the SID could also be any other SID. The property returns this value for a NULL SID and also for a unknown SID. Use the boolean property IsWellKnownSID to check for a well known SID.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> Attributes: <span class="kw4">Cardinal</span>; writable;<br />
<span class="kw1">property</span> AttributesType: TJwSidAttributeSet; writable;</div>
<p>The attributes assigned to a SID can be used in different ways. Some WinAPI functions need them for example. However you can use two versions that are nearly equal. Either you set a bitmask using the property <em>Attributes </em>or you use a enumeration set with <em>AttributesType.</em></p>
<p>Next time I&#8217;ll talk about more JWSCL classes that represent well known SIDs and are defined in the unit JwsclKnownSID.</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/10/what-is-a-sid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
