<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Laurentiu Cristofor's blog @microsoft.com : SQL Server - general</title><link>http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx</link><description>Tags: SQL Server - general</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Finding information about which account xp_cmdshell is running as</title><link>http://blogs.msdn.com/lcris/archive/2009/10/27/finding-information-about-which-account-xp-cmdshell-is-running-as.aspx</link><pubDate>Tue, 27 Oct 2009 21:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9913721</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/9913721.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=9913721</wfw:commentRss><description>&lt;P&gt;If you ever needed to debug a permission related issue when using xp_cmdshell, you have probably realized that a crucial piece of information is about what particular account xp_cmdshell is executing under. If you are the administrator of the database, you already know the context used by xp_cmdshell, but otherwise you may not have that information. Here are some tips on how to find more.&lt;/P&gt;
&lt;P&gt;First, if you have a command line tool that displays the current context, like whoami or a utility for dumping the security contex, you can just execute that under xp_cmdshell. That's pretty easy. But what if there is no such tool available? One thing you can try in this case is to loop back into SQL (assuming the xp_cmdshell account has access to the database - it usually does) and just ask SQL for more information with queries like the following:&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;EXEC&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;xp_cmdshell&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'osql -E -Q"select suser_sname()"'&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;EXEC&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;xp_cmdshell&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'osql -E -Q"select * from sys.login_token"'&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;Don't forget to pass in the server/instance name using the -S option, if you are not dealing with a default instance. These should give you plenty of information about the xp_cmdshell context and should help you figure out any access permission issue.&lt;/P&gt;
&lt;P&gt;Given that I am on the topic of xp_cmdshell, here's how the command can be enabled using T-SQL:&lt;/P&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;
&lt;P&gt;sp_configure&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'show advanced options'&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;reconfigure&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;sp_configure&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'xp_cmdshell'&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;reconfigure&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9913721" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Some insight information into how SQL Server documentation is prepared</title><link>http://blogs.msdn.com/lcris/archive/2007/02/14/some-insight-information-into-how-sql-server-documentation-is-prepared.aspx</link><pubDate>Thu, 15 Feb 2007 01:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1678908</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/1678908.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=1678908</wfw:commentRss><description>&lt;P&gt;&lt;A class="" href="http://blogs.msdn.com/buckwoody/" mce_href="http://blogs.msdn.com/buckwoody/"&gt;Buck Woody&lt;/A&gt; just wrote an interesting &lt;A class="" href="http://blogs.msdn.com/buckwoody/archive/2007/02/14/sql-server-books-online.aspx" mce_href="http://blogs.msdn.com/buckwoody/archive/2007/02/14/sql-server-books-online.aspx"&gt;post&lt;/A&gt;&amp;nbsp;about the process of writing documentation for SQL Server. After reading this, you should have no excuse for not providing feedback on Books Online articles that you feel are incomplete or have inaccurate information - your feedback matters!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1678908" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Raul starts his blog...</title><link>http://blogs.msdn.com/lcris/archive/2006/03/13/raul-starts-his-blog.aspx</link><pubDate>Tue, 14 Mar 2006 00:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:550750</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/550750.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=550750</wfw:commentRss><description>Some of you may already know Raul from the SQL Server Security forums. Raul has just started his blog at &lt;A href="http://blogs.msdn.com/raulga/" mce_href="http://blogs.msdn.com/raulga/"&gt;http://blogs.msdn.com/raulga/&lt;/A&gt;. His first post provides a detailed&amp;nbsp;discussion of&amp;nbsp;indexing encrypted data: &lt;A href="http://blogs.msdn.com/raulga/archive/2006/03/11/549754.aspx" mce_href="http://blogs.msdn.com/raulga/archive/2006/03/11/549754.aspx"&gt;http://blogs.msdn.com/raulga/archive/2006/03/11/549754.aspx&lt;/A&gt;.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=550750" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/announcements/default.aspx">announcements</category></item><item><title>A couple of links for submitting feedback on Microsoft products</title><link>http://blogs.msdn.com/lcris/archive/2005/12/01/a-couple-of-links-for-submitting-feedback-on-microsoft-products.aspx</link><pubDate>Fri, 02 Dec 2005 01:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:499132</guid><dc:creator>lcris</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/lcris/comments/499132.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=499132</wfw:commentRss><description>&lt;P&gt;I want to advertise a couple of sites that can be used for submitting feedback and suggestions, or for filing bug reports for Microsoft products. These are:&lt;/P&gt;
&lt;P&gt;The Microsoft Technical Forums at: &lt;A href="http://forums.microsoft.com/MSDN/" mce_href="http://forums.microsoft.com/MSDN/"&gt;http://forums.microsoft.com/MSDN/&lt;/A&gt;. The SQL Server Forums are at: &lt;A href="http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&amp;amp;SiteID=1" mce_href="http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&amp;amp;SiteID=1"&gt;http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&amp;amp;SiteID=1&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The Product Feedback Center at: &lt;A href="https://connect.microsoft.com/default.aspx"&gt;https://connect.microsoft.com/default.aspx&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Update [2005/12/21]: Another useful link for reporting security vulnerabilities is: &lt;A href="http://www.microsoft.com/technet/security/bulletin/alertus.aspx" mce_href="http://www.microsoft.com/technet/security/bulletin/alertus.aspx"&gt;http://www.microsoft.com/technet/security/bulletin/alertus.aspx&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=499132" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>A new blog link</title><link>http://blogs.msdn.com/lcris/archive/2005/10/13/a-new-blog-link.aspx</link><pubDate>Fri, 14 Oct 2005 03:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480869</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/480869.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=480869</wfw:commentRss><description>&lt;P&gt;Here's a link to a new blog that a colleague has started: &lt;A href="http://blogs.msdn.com/yukondoit/" mce_href="http://blogs.msdn.com/yukondoit/"&gt;http://blogs.msdn.com/yukondoit/&lt;/A&gt;. The first entry is about the various levels of data protection in SQL Server 2005 and you can access it at: &lt;A href="http://blogs.msdn.com/yukondoit/articles/480854.aspx" mce_href="http://blogs.msdn.com/yukondoit/articles/480854.aspx"&gt;http://blogs.msdn.com/yukondoit/articles/480854.aspx&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=480869" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/announcements/default.aspx">announcements</category></item><item><title>SQL Server 2005: How to fix outdated names of Windows logins</title><link>http://blogs.msdn.com/lcris/archive/2005/09/27/474505.aspx</link><pubDate>Tue, 27 Sep 2005 20:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:474505</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/474505.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=474505</wfw:commentRss><description>&lt;P&gt;The SQL Server login catalogs store the names of&amp;nbsp;Windows&amp;nbsp;principals as well as their SIDs. Because the names&amp;nbsp;are stored, changes that affect&amp;nbsp;a name can lead to a state where&amp;nbsp;a catalog entry&amp;nbsp;is out of sync with the current login name. For example, in the case of local Windows accounts, if we change the machine name, the catalogs will reflect the old machine name. Also,&amp;nbsp;if we change&amp;nbsp;the name of an account, the catalog will&amp;nbsp;still contain the old account name.&amp;nbsp;One side-effect of this will be that queries that rely on logic like &lt;FONT face="Courier New"&gt;select * from syslogins where name = suser_name()&lt;/FONT&gt; will be broken. What needs to be done&amp;nbsp;here is&amp;nbsp;to update the login&amp;nbsp;names using the ALTER LOGIN statement.&amp;nbsp;Based on&amp;nbsp;the LookupSid function that I&amp;nbsp;introduced in my &lt;A href="/lcris/archive/2005/09/26/474202.aspx"&gt;previous post&lt;/A&gt;, I wrote a small T-SQL script that loops over the &lt;EM&gt;server_principals&lt;/EM&gt; catalog entries and verifies whether the names recorded are matching the names that Windows returns for the associated SIDs. If differences are detected, the script will output the ALTER LOGIN statements that will fix the situation. Note that no data is changed here; the script is simply producing as output the statements that should be run to rectify the problem. Here's the script:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;--&lt;BR&gt;-- Execute this to generate the list of statements that need to be executed&lt;BR&gt;-- to fix the Windows login names that are invalid&lt;BR&gt;--&lt;BR&gt;declare @login_name sysname&lt;BR&gt;declare @login_sid varbinary(max)&lt;BR&gt;declare crs_fix_login_names cursor for select name, sid from sys.server_principals where lower(name) &amp;lt;&amp;gt; lower(dbo.LookupSid(sid)) AND (type = 'U' or TYPE = 'G')&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;open crs_fix_login_names&lt;BR&gt;fetch next from crs_fix_login_names into @login_name, @login_sid&lt;BR&gt;while (@@fetch_status = 0)&lt;BR&gt;begin&lt;BR&gt;&amp;nbsp;print 'ALTER LOGIN ' + quotename(@login_name) + ' WITH NAME = ' + quotename(dbo.LookupSid(@login_sid))&lt;BR&gt;&amp;nbsp;fetch next from crs_fix_login_names into @login_name, @login_sid&lt;BR&gt;end&lt;BR&gt;close crs_fix_login_names&lt;BR&gt;deallocate crs_fix_login_names&lt;BR&gt;go&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;So, the script is&amp;nbsp;using a cursor to go over each Windows login entry (of type U or G - Windows user or group) and verify whether the name in the catalog is different from the name returned by the OS for the SID. For each such instance, the script builds the ALTER LOGIN command that needs to be executed to update the login name. Nothing is actually modified by this code. If there is any output, it should be copied and examined for errors, then executed to correct the login names. If there is no output, then no problems were detected by the script.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=474505" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Server 2005: An example of writing a CLR function that wraps the LookupAccountSid WinAPI call</title><link>http://blogs.msdn.com/lcris/archive/2005/09/26/474202.aspx</link><pubDate>Tue, 27 Sep 2005 01:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:474202</guid><dc:creator>lcris</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/lcris/comments/474202.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=474202</wfw:commentRss><description>&lt;P&gt;I was looking recently into writing a wrapper for the LookupAccountSid WinAPI&amp;nbsp;function, so that I could use this functionality in T-SQL. I decided to write a CLR function and document the steps here:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1.&lt;/STRONG&gt; First, we need to write the code into a file - I named it&amp;nbsp;LookupNameSid.cs. I tested&amp;nbsp;this code&amp;nbsp;with both the 1.1 CLR and the 2.0 CLR. To keep this post shorter, I&amp;nbsp;will not include comments in the code, but instead I'll provide a quick overview of what gets done here: the LookupSid function is passed the SID in a byte array, it copies these bits into memory pointed to by an IntPtr, and then we call the WinAPI function LookupAccountSid - first to determine the length of the buffers that will receive the output and a second time to get the result; the rest is just error handling and memory management.&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;/////////////////&lt;BR&gt;//&lt;BR&gt;// LookupNameSid&lt;BR&gt;//&lt;BR&gt;// Wrapper around WinAPI function LookupAccountSid&lt;BR&gt;//&lt;BR&gt;// compile command:&lt;BR&gt;// csc /target:library /out:LookupNameSid.dll LookupNameSid.cs &lt;BR&gt;//&lt;BR&gt;/////////////////&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;using System;&lt;BR&gt;using System.Runtime.InteropServices;&lt;BR&gt;using System.Text;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;public class LookupNameSid&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const int NO_ERROR = 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const int ERROR_INSUFFICIENT_BUFFER = 122;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; enum SID_NAME_USE&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeUser = 1,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeGroup,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeDomain,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeAlias,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeWellKnownGroup,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeDeletedAccount,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeInvalid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeUnknown,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SidTypeComputer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static extern bool LookupAccountSid(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string lpSystemName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr Sid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StringBuilder lpName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ref uint cchName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StringBuilder ReferencedDomainName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ref uint cchReferencedDomainName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out SID_NAME_USE peUse);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string LookupSid(byte[] rgSid)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (rgSid == null || rgSid.Length == 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return null;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr pSid = IntPtr.Zero;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uint cbSid = (uint)rgSid.Length;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pSid = Marshal.AllocCoTaskMem(Convert.ToInt32(cbSid));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Marshal.Copy(rgSid, 0, pSid, (int)cbSid);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StringBuilder name = new StringBuilder();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uint cchName = (uint)name.Capacity;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StringBuilder referencedDomainName = new StringBuilder();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uint cchReferencedDomainName = (uint)referencedDomainName.Capacity;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SID_NAME_USE sidUse;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int err = NO_ERROR;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!LookupAccountSid(null, pSid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name, ref cchName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; referencedDomainName, ref cchReferencedDomainName, out sidUse))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; err = Marshal.GetLastWin32Error();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (err == ERROR_INSUFFICIENT_BUFFER)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; err = NO_ERROR;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name.EnsureCapacity((int)cchName);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!LookupAccountSid(null, pSid,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name, ref cchName, referencedDomainName,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ref cchReferencedDomainName, out sidUse))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; err = Marshal.GetLastWin32Error();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Marshal.FreeCoTaskMem(pSid);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (err == NO_ERROR)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return referencedDomainName.ToString() + "\\" + name.ToString();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return null;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt; The next step is to compile this into a DLL. I used the command line compiler for this: csc.exe. This can be found&amp;nbsp;in the&amp;nbsp;Windows directory, under Microsoft.NET\Framework\&lt;EM&gt;&lt;U&gt;version&lt;/U&gt;&lt;/EM&gt;\, where&amp;nbsp;&lt;EM&gt;&lt;U&gt;version&lt;/U&gt;&lt;/EM&gt;&amp;nbsp;is the CLR version. On my machine, the command looked like this:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;C:\WINNT\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library /out:LookupNameSid.dll LookupNameSid.cs&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;The path to csc.exe may need to be updated according to&amp;nbsp;the machine settings, for this to work. The result of this step should be that now we have a DLL file containing the LookupSid function. &lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;3.&lt;/STRONG&gt; Now come the steps required for installing this assembly. Note that this is an unsafe assembly because it calls unmanaged code - the WinAPI. Also, I decided to install this code&amp;nbsp;in my master database, but a better idea is to put it in another database. Finally, because this is an&amp;nbsp;unsafe assembly and because I didn't want to sign it, I decided instead to mark the database in which I placed it as trustworthy. Here are the T-SQL commands that I used to install the assembly (the path to the DLL&amp;nbsp;in CREATE ASSEMBLY will most likely&amp;nbsp;need to be updated):&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;--&lt;BR&gt;-- Steps for importing the&amp;nbsp;CLR code into SQL Server&lt;BR&gt;--&lt;BR&gt;ALTER DATABASE master SET TRUSTWORTHY ON;&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;CREATE ASSEMBLY LookupNameSid FROM 'C:\TEMP\LookupNameSid.dll' WITH PERMISSION_SET = UNSAFE;&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;CREATE FUNCTION LookupSid (@sid VARBINARY(85)) RETURNS SYSNAME AS EXTERNAL NAME [LookupNameSid].[LookupNameSid].[LookupSid];&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;sp_configure 'clr enabled', 1&lt;BR&gt;GO&lt;BR&gt;reconfigure&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;SELECT dbo.LookupSid(sid), name FROM syslogins;&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;So, I turned on the TRUSTWORTHY attribute of the master database, then I created the assembly and the LookupSid function, and then I turned CLR on. Last command is just to test that the function works ok; it basically looks up the SID for each entry in the syslogins catalog, and it prints the name found, as well as the name recorded in the catalog. For SQL logins, we'll just print&amp;nbsp;&lt;FONT face="Courier New"&gt;NULL&lt;/FONT&gt;.&amp;nbsp;If the code from step 1&amp;nbsp;is modified to use a namespace, then the second part of the external name used in CREATE FUNCTION will also need to be modified&amp;nbsp;to include the namespace name.&lt;/P&gt;
&lt;P dir=ltr&gt;That's it! The LookupSid function can now be used to map SIDs to account names.&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;STRONG&gt;4.&lt;/STRONG&gt; The following T-SQL commands can be used to revert the changes made to SQL Server in the above steps.&amp;nbsp;I assume a default SQL Server state where CLR was not enabled and the master database was not marked as&amp;nbsp;trustworthy, so if this was not the case, the corresponding cleanup steps should be skipped.&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New"&gt;--&lt;BR&gt;-- Cleanup steps&lt;BR&gt;--&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;DROP FUNCTION LookupSid;&lt;BR&gt;GO&lt;BR&gt;DROP ASSEMBLY LookupNameSid;&lt;BR&gt;GO&lt;BR&gt;sp_configure 'clr enabled', 0&lt;BR&gt;GO&lt;BR&gt;reconfigure&lt;BR&gt;GO&lt;BR&gt;ALTER DATABASE master SET TRUSTWORTHY OFF&lt;BR&gt;GO&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=474202" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>An example for setting database mirroring in SQL Server 2005</title><link>http://blogs.msdn.com/lcris/archive/2005/09/14/466268.aspx</link><pubDate>Wed, 14 Sep 2005 20:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:466268</guid><dc:creator>lcris</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/lcris/comments/466268.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=466268</wfw:commentRss><description>&lt;P&gt;I am not a database mirroring expert, but a while ago I have set up database mirroring between two of my machines. I collected the steps that I followed in the following script:&lt;/P&gt;
&lt;P&gt;-- This script illustrates how to setup database mirroring between two machines.&lt;BR&gt;-- The script&amp;nbsp;assumes that the principal machine is SQLPRINCIPAL and that the mirror machine is SQLMIRROR.&lt;BR&gt;-- Also, a dbm share will exist on SQLPRINCIPAL, and SQLMIRROR can read and write from it.&lt;/P&gt;
&lt;P&gt;-- on SQLPRINCIPAL:&lt;BR&gt;--&lt;BR&gt;CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Af01AufdSL';&lt;/P&gt;
&lt;P&gt;CREATE CERTIFICATE cert_dbm_principal AUTHORIZATION DBO WITH SUBJECT = 'DBM - Principal';&lt;BR&gt;BACKUP CERTIFICATE cert_dbm_principal TO FILE = '\\SQLPRINCIPAL\dbm\cert_dbm_principal.cer';&lt;/P&gt;
&lt;P&gt;CREATE ENDPOINT dbm STATE=started AS tcp (listener_port=5022) FOR database_mirroring (role=all, authentication=certificate cert_dbm_principal);&lt;/P&gt;
&lt;P&gt;CREATE DATABASE dbm ON PRIMARY (NAME = br_dat1, FILENAME = "c:\dbm\dbm.mdf", SIZE = 8mb) LOG ON (NAME = br_log1, FILENAME = "c:\dbm\dbm.ldf", SIZE = 8mb);&lt;BR&gt;BACKUP DATABASE dbm TO DISK = '\\SQLPRINCIPAL\dbm\dbm.dmp' WITH FORMAT;&lt;/P&gt;
&lt;P&gt;-- do the mirror steps before returning here.&lt;/P&gt;
&lt;P&gt;CREATE LOGIN l_dbm_mirror WITH PASSWORD = 'Neufd1C';&lt;BR&gt;CREATE USER u_dbm_mirror FOR LOGIN l_dbm_mirror;&lt;BR&gt;CREATE CERTIFICATE cert_dbm_mirror AUTHORIZATION u_dbm_mirror FROM FILE = '\\SQLPRINCIPAL\dbm\cert_dbm_mirror.cer';&lt;BR&gt;GRANT CONNECT ON ENDPOINT::dbm TO l_dbm_mirror;&lt;/P&gt;
&lt;P&gt;ALTER DATABASE dbm SET PARTNER = 'tcp://SQLMIRROR.domain.company.com:5022';&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&amp;nbsp;&lt;BR&gt;-- on SQLMIRROR:&lt;BR&gt;--&lt;BR&gt;CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Dgc6a3um';&lt;/P&gt;
&lt;P&gt;CREATE CERTIFICATE cert_dbm_mirror AUTHORIZATION DBO WITH SUBJECT = 'DBM - Mirror';&lt;BR&gt;BACKUP CERTIFICATE cert_dbm_mirror TO FILE = '\\SQLPRINCIPAL\dbm\cert_dbm_mirror.cer';&lt;/P&gt;
&lt;P&gt;CREATE ENDPOINT dbm STATE=started AS tcp (listener_port=5022) FOR database_mirroring (role=all, authentication=certificate cert_dbm_mirror);&lt;/P&gt;
&lt;P&gt;CREATE LOGIN l_dbm_principal WITH PASSWORD = 'Sc6d061t';&lt;BR&gt;CREATE USER u_dbm_principal FOR LOGIN l_dbm_principal;&lt;BR&gt;CREATE CERTIFICATE cert_dbm_principal AUTHORIZATION u_dbm_principal FROM FILE = '\\SQLPRINCIPAL\dbm\cert_dbm_principal.cer';&lt;BR&gt;GRANT CONNECT ON ENDPOINT::dbm TO l_dbm_principal;&lt;/P&gt;
&lt;P&gt;RESTORE DATABASE dbm FROM DISK = '\\SQLPRINCIPAL\dbm\dbm.dmp' WITH NORECOVERY;&lt;/P&gt;
&lt;P&gt;ALTER DATABASE dbm SET PARTNER = 'tcp://SQLPRINCIPAL.domain.company.com:5022';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;-- Additional commands&lt;BR&gt;--&lt;BR&gt;-- to remove mirroring, issue on either partner:&lt;BR&gt;ALTER DATABASE dbm SET PARTNER OFF;&lt;/P&gt;
&lt;P&gt;-- to check the state of mirroring, issue:&lt;BR&gt;SELECT * FROM sys.database_mirroring;&lt;BR&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=466268" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item></channel></rss>