<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Finding a user&#8217;s primary group in AD</title>
	<atom:link href="http://blogs.freebsdish.org/tmclaugh/2010/07/21/finding-a-users-primary-group-in-ad/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.freebsdish.org/tmclaugh/2010/07/21/finding-a-users-primary-group-in-ad/</link>
	<description>I&#039;d rather be doing something than writing about it.</description>
	<lastBuildDate>Fri, 05 Oct 2012 22:49:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Buy Guaranteed Facebook Fans</title>
		<link>http://blogs.freebsdish.org/tmclaugh/2010/07/21/finding-a-users-primary-group-in-ad/comment-page-1/#comment-6817</link>
		<dc:creator>Buy Guaranteed Facebook Fans</dc:creator>
		<pubDate>Fri, 02 Dec 2011 20:56:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.freebsdish.org/tmclaugh/?p=213#comment-6817</guid>
		<description><![CDATA[&lt;strong&gt;Related.. Trackback...&lt;/strong&gt;

[...]the time to read or visit the content or sites we have linked to below the[...]...]]></description>
		<content:encoded><![CDATA[<p><strong>Related.. Trackback&#8230;</strong></p>
<p>[...]the time to read or visit the content or sites we have linked to below the[...]&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://blogs.freebsdish.org/tmclaugh/2010/07/21/finding-a-users-primary-group-in-ad/comment-page-1/#comment-2855</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Tue, 05 Oct 2010 12:14:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.freebsdish.org/tmclaugh/?p=213#comment-2855</guid>
		<description><![CDATA[Thanks for posting the algorithm. Here is a PHP implementation (note that PHP pack/unpack do not support 64-bit int, so this uses bcmath):

function sid2str($sid)
{
    $srl = ord($sid[0]);
    $number_sub_id = ord($sid[1]);
    $x = substr($sid,2,6);
    $h = unpack(&#039;N&#039;,&quot;\x0\x0&quot;.substr($x,0,2));
    $l = unpack(&#039;N&#039;,substr($x,2,6));
    $iav = bcadd(bcmul($h[1],bcpow(2,32)),$l[1]);
    for ($i=0; $i&lt;$number_sub_id; $i++)
    {
        $sub_id = unpack(&#039;V&#039;, substr($sid, 8+4*$i, 4));
        $sub_ids[] = $sub_id[1];
    }
    return sprintf(&#039;S-%d-%d-%s&#039;, $srl, $iav, implode(&#039;-&#039;,$sub_ids));
}

function get_primary_group($server, $admin, $passwd, $base, $username)
{
    global $server, $admin, $passwd, $connect, $r;
    $connect = ldap_connect($server);
    ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
    $bind = ldap_bind($connect, $admin, $passwd) or exit(&#039;bind&#039;);

    $r = ldap_search($connect, $base, &quot;sAMAccountName=$username&quot;, array(&#039;primaryGroupID&#039;)) or exit(&#039;ldap_search&#039;);
    $data = ldap_get_entries($connect, $r);
    $pri_grp_rid = $data[0][&#039;primarygroupid&#039;][0];
    print(&quot;pri_grp_rid = ${pri_grp_rid}\n&quot;);
    $r = ldap_read($connect, $base, &#039;(objectclass=*)&#039;, array(&#039;objectSid&#039;)) or exit(&#039;ldap_search&#039;);
    $data = ldap_get_entries($connect, $r);
    $domain_sid = $data[0][&#039;objectsid&#039;][0];

    $domain_sid_s = sid2str($domain_sid);
    print(&quot;domain_sid_s = ${domain_sid_s}\n&quot;);

    $r = ldap_search($connect, $base, &quot;objectSid=${domain_sid_s}-${pri_grp_rid}&quot;, array(&#039;cn&#039;)) or exit(&#039;ldap_search&#039;);
    $data = ldap_get_entries($connect, $r);
    print(&quot;cn = {$data[0][&#039;cn&#039;][0]}\n&quot;);
}]]></description>
		<content:encoded><![CDATA[<p>Thanks for posting the algorithm. Here is a PHP implementation (note that PHP pack/unpack do not support 64-bit int, so this uses bcmath):</p>
<p>function sid2str($sid)<br />
{<br />
    $srl = ord($sid[0]);<br />
    $number_sub_id = ord($sid[1]);<br />
    $x = substr($sid,2,6);<br />
    $h = unpack(&#8216;N&#8217;,&#8221;\x0\x0&#8243;.substr($x,0,2));<br />
    $l = unpack(&#8216;N&#8217;,substr($x,2,6));<br />
    $iav = bcadd(bcmul($h[1],bcpow(2,32)),$l[1]);<br />
    for ($i=0; $i&lt;$number_sub_id; $i++)<br />
    {<br />
        $sub_id = unpack(&#039;V&#039;, substr($sid, 8+4*$i, 4));<br />
        $sub_ids[] = $sub_id[1];<br />
    }<br />
    return sprintf(&#039;S-%d-%d-%s&#039;, $srl, $iav, implode(&#039;-&#039;,$sub_ids));<br />
}</p>
<p>function get_primary_group($server, $admin, $passwd, $base, $username)<br />
{<br />
    global $server, $admin, $passwd, $connect, $r;<br />
    $connect = ldap_connect($server);<br />
    ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);<br />
    ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);<br />
    $bind = ldap_bind($connect, $admin, $passwd) or exit(&#039;bind&#039;);</p>
<p>    $r = ldap_search($connect, $base, &quot;sAMAccountName=$username&quot;, array(&#039;primaryGroupID&#039;)) or exit(&#039;ldap_search&#039;);<br />
    $data = ldap_get_entries($connect, $r);<br />
    $pri_grp_rid = $data[0][&#039;primarygroupid&#039;][0];<br />
    print(&quot;pri_grp_rid = ${pri_grp_rid}\n&quot;);<br />
    $r = ldap_read($connect, $base, &#039;(objectclass=*)&#039;, array(&#039;objectSid&#039;)) or exit(&#039;ldap_search&#039;);<br />
    $data = ldap_get_entries($connect, $r);<br />
    $domain_sid = $data[0][&#039;objectsid&#039;][0];</p>
<p>    $domain_sid_s = sid2str($domain_sid);<br />
    print(&quot;domain_sid_s = ${domain_sid_s}\n&quot;);</p>
<p>    $r = ldap_search($connect, $base, &quot;objectSid=${domain_sid_s}-${pri_grp_rid}&quot;, array(&#039;cn&#039;)) or exit(&#039;ldap_search&#039;);<br />
    $data = ldap_get_entries($connect, $r);<br />
    print(&quot;cn = {$data[0][&#039;cn&#039;][0]}\n&quot;);<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
