<?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>Advanced Software Engineering &#187; SQUID</title>
	<atom:link href="http://www.gabrielgonzalezgarcia.com/tag/squid/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gabrielgonzalezgarcia.com</link>
	<description>or something like that</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:19:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SQUID + Active Directory</title>
		<link>http://www.gabrielgonzalezgarcia.com/2009/09/21/squid-active-directory/</link>
		<comments>http://www.gabrielgonzalezgarcia.com/2009/09/21/squid-active-directory/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 10:59:54 +0000</pubDate>
		<dc:creator>Gabriel Gonzalez</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQUID]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.advansen.com/?p=114</guid>
		<description><![CDATA[A few post ago I wrote about integrating SQUID and Active Directory in order to allow/deny users to access specific webpages depeding on the groups a user belongs. The windows package of Squid comes with several external programs which can be used as external ACLs which allow you to query the local Active Directory in [...]]]></description>
			<content:encoded><![CDATA[<p>A few post ago I wrote about integrating <strong><em>SQUID </em></strong>and <strong><em>Active Directory </em></strong>in order to allow/deny users to access specific webpages depeding on the groups a user belongs.</p>
<p>The windows package of <strong><em>Squid </em></strong>comes with several external programs which can be used as <em><strong>external ACL</strong></em>s which allow you to query the local Active Directory in order to obtain access or not. The one dealing with users and groups is called <em><strong>mswin_check_ad_group.exe </strong><span style="font-style: normal;">which, as all the external ACLs, reads the standard input looking for a user and a group and return whether the user belongs to the given group.</span></em></p>
<p><em><span style="font-style: normal;">This is fine and pretty straight forward it has a PROBLEM, it only works with Groups with scope set to &#8220;Domain Local&#8221;; which turn into a drawback when your users belong to Groups with Global Scope. I haven&#8217;t found any documentation explaining how to achive this so I have created a simple external ACL to peform this task in python.</span></em></p>
<p><em><span style="font-style: normal;">You only need to download <a title="pywin32" href="http://sourceforge.net/projects/pywin32/">pywin32 </a>and the <a title="Active Directory for Python" href="http://timgolden.me.uk/python/downloads/active_directory-0.6.7.zip">active directory plugin</a> for python. After installing just use the following code, which will return OK IFF the user belongs to the given group (non matter which scope):</span></em></p>
<p><em><span style="font-style: normal;"><span id="more-114"></span></span></em></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">subprocess</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> active_directory
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> :
&nbsp;
	squid = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>squid<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
	m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(?&amp;lt;=%5C)<span style="color: #000099; font-weight: bold;">\w</span>+'</span>, squid<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	username = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
	m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(?&amp;lt;= )[<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\.</span>]+'</span>, squid<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	checkgroup = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
	ret = <span style="color: #483d8b;">&quot;ERR&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #dc143c;">user</span> = active_directory.<span style="color: black;">find_user</span><span style="color: black;">&#40;</span>username<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> group <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">user</span>.<span style="color: black;">memberOf</span>:
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>group.<span style="color: black;">cn</span>, checkgroup<span style="color: black;">&#41;</span>  == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
&nbsp;
			ret = <span style="color: #483d8b;">&quot;OK&quot;</span>
			<span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">print</span>	ret + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<div><span style="font-style: normal;">I am new to the Python world so, for sure, this little thing can be improved, feel free to comment anything.</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gabrielgonzalezgarcia.com/2009/09/21/squid-active-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

