<?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>Random Tech Articles &#187; ldap</title>
	<atom:link href="http://www.analogrithems.com/rant/tag/ldap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.analogrithems.com/rant</link>
	<description>If I have seen a little further it is by standing on the shoulders of Giants. - Newton</description>
	<lastBuildDate>Tue, 10 Aug 2010 20:51:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>LdapAuth component for CakePHP</title>
		<link>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/</link>
		<comments>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 22:08:52 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[centralized authentication]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=40</guid>
		<description><![CDATA[So I was looking for a way to authenticate against LDAP with cake but I&#8217;ve found that it doesn&#8217;t support it by default. I found one that checks the auth against ldap then creates a local mysql account. This also didn&#8217;t use a actual ldap data sourc e either, it just handeled it&#8217;s own ldap [...]]]></description>
			<content:encoded><![CDATA[<p>So I was looking for a way to authenticate against LDAP with cake but I&#8217;ve found that it doesn&#8217;t support it by default.  I found one that checks the auth against ldap then creates a local mysql account.  This also didn&#8217;t use a actual ldap data sourc e either, it just handeled it&#8217;s own ldap connection.  After I read through the standard cakphp auth component I saw that it wouldn&#8217;t be that hard to write an LDAP based auth component.</p>
<p>First you need to download this file <a href="http://www.analogrithems.com/rant/wp-content/uploads/2009/06/ldap_auth.phps">ldap_auth.php</a> to your <strong>&#8216;app/controllers/components/&#8217;</strong> directory.</p>
<p>Then just like the original auth component you add that component to a model.  I followed the usual auth example and created a model called Users.php like so</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useDbConfig</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ldap'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$primaryKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'uid'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useTable</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ou=people'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If some of these options are odd looking please have a look at my <a href="http://www.analogrithems.com/rant/?p=3">ldap data source </a> post.  </p>
<p>Next you are going to need your controller.  Here is the one I used  It defines the required functions like login, logout &#038; authorize.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsersController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Users'</span><span style="color: #339933;">;</span>    
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'RequestHandler'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'LdapAuth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Javascript'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Ajax'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LdapAuth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> isAuthorized<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>7</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>CakePHP: LDAP with full CRUD, a living example!</title>
		<link>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/</link>
		<comments>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 22:14:35 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=13</guid>
		<description><![CDATA[I&#8217;ve been using CakePHP for a while now and I&#8217;ve been thinking for a while it was time to see if I could give something back. As an IT leader I&#8217;m in love with LDAP. It makes life so simple for me and my team. The big downside to LDAP is it&#8217;s not very easy [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using CakePHP for a while now and I&#8217;ve been thinking for a while it was time to see if I could give something back.  As an IT leader I&#8217;m in love with LDAP.  It makes life so simple for me and my team.  The big downside to LDAP is it&#8217;s not very easy to learn how all the objectClasses and attributes work with various applications.  Microsoft has eliminated this with the <em>Microsoft Management Console</em> (<em>MMC</em>).   It amazes me that no open source project has developed a tools such as this before.  I&#8217;ve worked on a open source tool in the past that was a web interface wrapper around ldap to do account management, so I&#8217;m familiar with the requirements such an application should have. Being that CakePHP is so powerful, I wanted to see if I could do this with that.</p>
<p>When I first started, I realized that CakePHP didn&#8217;t have an LDAP data source officially supported yet.  I did find two articles about some good attempts.  One by <a href="http://bakery.cakephp.org/articles/view/ldap-datasource-for-cakephp" target="_blank"><span>euphrate</span></a>, unfortunately this one was only for reading from ldap.  The second one was by <a href="http://memdump.wordpress.com/2008/04/26/ldap-data-source-now-with-full-crud/">Gservat</a>, this one was a bit more complete, but was not really working for me and  as i read from his comments many others.  I think we wrote his for CakePHP 1.1.  Since I wanted to use Current cake 1.2.8xxx  I set out to use this as my start and fix/extend it.</p>
<p>Before we get started I want to state the environment I was using to do my work was Redhat Enterprise 5.2 &amp; Fedora 10 (Work requirement) with redhat directory server 8.1 and Fedora directory server 1.2.  Now while LDAP is a standard protocol, some of the driver may have become centric to those platforms, so if this is the case, please leave me a comment and I will try to correct the ldap data source I&#8217;m working on.  My hope is to get ldap as an offical CakePHP data source.  With that said the reason i call this a living example is because I&#8217;ve continued to upgrade and improve this data source as well as this article.  Some of the next features I want to implement is data associations.  Basically has and belongs to many relations.  This way when you look up an user account it also shows you all the groups that user is in.  This will take some time but I&#8217;ll get there.  This work is all being done in the hopes that I can use this data source and CakePHP to build a really user friendly web interface for managing enterprise LDAP infrastructures without a whole lot of LDAP knowledge.</p>
<p><strong>4/15/2010 &#8211; Made several changes to the code to hopefully play nicely with active directory.  Tested it with CakePHP-1.3 everything looks good.  Please test and leave feedback.  The new datasource requires you to add a new field to the database.cfg ldap entry.  See the config below<br />
</strong></p>
<p><strong>8/20/2009 &#8211; New Home for the source.  I&#8217;ve got this datasource in my  github tree now <a href="http://github.com/analogrithems/idbroker/tree/master/models/datasources">http://github.com/analogrithems/idbroker/tree/master/models/datasources</a> enjoy, and feel free to submit bugs or request there.</strong></p>
<p><strong>7/13/2009 &#8211; updated ldap_source.php to make better use of the debug describe code.  Also fixed the way things update.  Only update what has changed instead of whole record.  This will help with LDAP aci rules when logging in as non-admin users and trying to do things like update your userpassword or email.</strong></p>
<p><strong>6/20/2009 &#8211; Updated ldap_source.php to work with OpenLDAP 2.3 schema system.  First it will try &#8216;cn=schema&#8217;, if that doesn&#8217;t return any results then it looks for schemas in &#8216;cn=subschema&#8217;  this make sure the code will work with OpenLDAP as well as the Netscape based versions like iPlanet, Redhat Directory Server, Fedora Directory Server etc.</strong></p>
<p>First things first, here is my ldap data source for CakePHP.  You will need to download this <a href="http://www.analogrithems.com/rant/wp-content/uploads/2009/07/ldap_source.phps" target="_blank">ldap_source.php</a> to your &#8216;app/models/datasources/&#8217; directory.</p>
<p>So lets dive right in below is the database config we will use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> ldaps<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//hostname and 'port' =&amp;gt; 636</span>
        <span style="color: #666666; font-style: italic;">// If using tls set 'tls' =&amp;gt; true and 'port' =&amp;gt; 389</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$ldap</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'datasource'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'ldap'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'host'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'port'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">389</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'basedn'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'dc=examnple,dc=com'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'tls'</span>         <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Netscape'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//Available types are 'OpenLDAP', 'ActiveDirectory', 'Netscape'</span>
		<span style="color: #0000ff;">'version'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">3</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>You notice that the variables database, login and password are blank.  Keep at least database this way.  You can populate login and password if don&#8217;t want your ldap connections to be anonymous.  I keep mine blank because I have written my own auth component that uses ldap,   So once I&#8217;m authed that gets passed to the datasource instead.  This is a ugly hack that I&#8217;ve written another <a href="http://www.analogrithems.com/rant/2009/06/13/ldapauth-component-for-cakephp/">post about</a>.</p>
<p>Please note that if you are using Redhat directory server, iPlanet or Fedora Project 389 then the <strong>type</strong> is Netscape as these all evolved off the Netscape LDAP server code base.  The other available types are &#8216;OpenLDAP&#8217; or &#8216;ActiveDirectory&#8217;  (case matters).</p>
<p>Here is our people model for accessing the users in your LDAP tree.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'alphaNumeric'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/^[a-zA-Z]*$/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Display Name.'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'between'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'between'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Between 5 to 15 characters'</span>
			<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'sn'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/^[a-zA-Z]*$/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Last Name.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'userpassword'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'minLength'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Mimimum 8 characters long.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Must Contain a Valid Email Address.'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'uid'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'alphaNumeric'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'Only Letters and Numbers can be used for Username.'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Here is a very basic controller to accompany our people model.  It demonstrates the important core functions and should get you started on using this data source with your own application.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'objectclass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'top'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'organizationalperson'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'inetorgperson'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'person'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'posixaccount'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'shadowaccount'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password_confirm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userpassword'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password_confirm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'homedirectory'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'homedirectory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/home/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'People Was Successfully Created.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>id<span style="color: #339933;">;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>redirect<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;People couldn't be created.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Session<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setFlash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Passwords don't match.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">'people'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$id</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>primaryKey<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$people</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$filter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>set<span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'people'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">'people'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>id <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>People<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>del<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>So lets talk about somethings here,  in our model we define <strong>$primaryKey</strong> &amp; <strong>$useTable</strong> variables.  The <strong>$useTable</strong> is the branch of the ldap server.  For this models purpose we define our table as <strong>&#8216;ou=people&#8217;</strong>.  This makes sure that objects we create (I.E. Users/people)  will be added under the organization unit people.  It also makes sure that when you pass something like &#8216;jdoe&#8217; to the delete action it will search that branch for the user object to delete.  The <strong>$primaryKey</strong> also helps in the creation and deleting of users.  It makes sure that the dn is created as uid, this is helpful to make sure that a user doesn&#8217;t already have that user name.  Also since ldap is case insensitive you don&#8217;t have to worry about the possible variations of the object names when checking the existence.</p>
<p>Now your model isn&#8217;t limited to one branch or object type.  If you wanted to create a browser for example your could define a model like the following.</p>
<p>You&#8217;ll notice here we set our <strong>$useTable</strong> to nothing (important, you get errors about no db defined from CakePHP if this missing).  The really interesting part here is that we set <strong>$primaryKey</strong> to dn.  This is the ultimate primary key for our type or data source.  The difference here is that when we create/delete an object we have to pass it the full dn.</p>
<p>Our new data source also adds some new options to the find function.<br />
<strong>$options['targetDN'] :</strong> This is more like the point in the tree we want to start our search.  If you don&#8217;t define it it defaults to the $useTable.$config[$useDbConfig]['basedn']  if your $useTable variable is empty it defaults to the basedn configured in your database config.</p>
<p><strong>$options['scope'] :</strong> If you&#8217;ve worked with ldap before then you are familiar with the concept of search scopes.  You have three search scopes, &#8216;sub&#8217;, &#8216;one, &amp; &#8216;base&#8217;.  Basically <strong>sub</strong> means search from this point down the tree.  <strong>one</strong> means search one level below this point and <strong>base</strong> means search just this point.  For example if you wanted to see if a user already existed you could set the targetDn to uid=jdoe,ou=people,dc=example,dc=com and it will check if this object already exists.   The default scope is <strong>sub</strong></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2009/06/12/cakephp-with-full-crud-a-living-example/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Why nscd sucks&#8230;.</title>
		<link>http://www.analogrithems.com/rant/2008/09/16/why-nscd-sucks/</link>
		<comments>http://www.analogrithems.com/rant/2008/09/16/why-nscd-sucks/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 01:27:53 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nscd]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=5</guid>
		<description><![CDATA[The name service cache daemon has some serious flaws. One that I notice pretty often in Linux arises when you are using LDAP. It seems that nscd will crash and burn when/if ldap is unavailable. Unfortunately when nscd bombs, it usually takes the entire system with it. Actually what will happen is nscd sockets will [...]]]></description>
			<content:encoded><![CDATA[<p>The name service cache daemon has some serious flaws.  One that I notice pretty often in Linux arises when you are using LDAP.  It seems that nscd will crash and burn when/if ldap is unavailable.  Unfortunately when nscd bombs, it usually takes the entire system with it.  Actually what will happen is nscd sockets will start getting broken pipe&#8217;s and becoming stale until you have several nscd.  Eventually your system will slow to a halt.  Mind you the box hasn&#8217;t crashed, but the box is in a dos state.  If you do an &#8216;<em>$# netstat -an</em>&#8216; you&#8217;ll notice several entries in reference to &#8216;/var/run/nscd/socket&#8217;.  This bug has been listed by several distros, I&#8217;ve yet to see any of them address it properly.</p>
<p>From what I&#8217;ve discovered about nscd this issue is a design flaw in nscd.  Several people have just recommended disabling nscd.  While this is a solution, it isn&#8217;t an acceptable solution in an enterprise environment. Especially if you are using oracle.  Several oracle tools and apps will freeze or crash if they can&#8217;t talk to the nscd socket.  Lame I know, but I&#8217;ve observed this in at least Oracle 10i.</p>
<p><strong>Solutions</strong></p>
<p>Now there are tow ways to get around this.  One is to make your LDAP service load balanced and highly available.  You can do replication in OpenLDAP with slurpd and then use something like balance to make the service highly available.  Another option though is to fix nscd so it doesn&#8217;t bomb your machine when it dies.</p>
<p>In this article we are going to go over the fix nscd solution.  nscd comes with a config file located in &#8216;<em>/etc/nscd.conf&#8217;</em> This file tells the nscd daemon how long to cache which name service.  See the man page to understand what each option means. The quick way to fix nscd is to tell the service here not to cache passwd and group info.  This will make nscd not go to LDAP and freak out if LDAP is not available.  Randomly I also came across a drop in replacement for nscd that dosen&#8217;t suffer from this issue. <a href="http://www.analogrithems.com/rant/wp-content/uploads/2008/09/nscd-033.c">unscd-033.c</a> The compile instructions are in the file. I can&#8217;t guarantee this safe, but I&#8217;ve used it for a while in an enterprise environment and seem haven&#8217;t had any nscd issues since<a href="http://www.analogrithems.com/rant/wp-content/uploads/2008/09/nscd-033.c"><br />
</a></p>
<p><strong>Refferences:</strong></p>
<ul>
<li>http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=248271</li>
</ul>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2008/09/16/why-nscd-sucks/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2008/09/16/why-nscd-sucks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ldap with linux a basic primmer</title>
		<link>http://www.analogrithems.com/rant/2008/09/16/ldap-with-linux-a-basic-primmer/</link>
		<comments>http://www.analogrithems.com/rant/2008/09/16/ldap-with-linux-a-basic-primmer/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 22:59:37 +0000</pubDate>
		<dc:creator>analogrithems</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[centralized authentication]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pam]]></category>

		<guid isPermaLink="false">http://www.analogrithems.com/rant/?p=3</guid>
		<description><![CDATA[LDAP has been around for over a decade, and yet it is still considered a newer technology.  Many modern vendors have added LDAP authentication.  This document is a brief generic howto for configuring linux to use LDAP.  This will be a fairly detailed recipe as most of my docs are. Linux Name Services Name Server [...]]]></description>
			<content:encoded><![CDATA[<p>LDAP has been around for over a decade, and yet it is still considered a newer technology.  Many modern vendors have added LDAP authentication.  This document is a brief generic howto for configuring linux to use LDAP.  This will be a fairly detailed recipe as most of my docs are.</p>
<p><strong>Linux Name Services</strong><br />
Name Server Switch is the engine that really enables Linux to harness LDAP. In linux you have two different ldap.conf files that the modules and services use to configure their ldap communications &#8216;/etc/ldap.conf&#8217; and &#8216;/etc/openldap/ldap.com&#8217;. The first gives the dn&#8217;s for passwd, group and sudoers. You can also specify the server and communication protocol. One really cool bonus is that you get to enable the host_acl based of users with this config file</p>
<p>host    ldap.analogrithems.com<br />
base    dc=analogrithems,dc=com,dc=us<br />
ldap_version    3<br />
#Validate cert<br />
tls_checkpeer no<br />
ssl     start_tls<br />
#ssl on<br />
nss_map_attribute       uniqueMember member<br />
pam_password_prohibit_message Please visit http://enterprise.company.com/password_policy.html.<br />
pam_groupdn cn=ldap.analogrithems.com,ou=Computers,dc=analogrithems,dc=com,dc=us<br />
pam_member_attribute uniquemember<br />
pam_filter accountStatus=active<br />
nss_base_passwd         ou=People,dc=analogrithems,dc=com,dc=us?one<br />
nss_base_shadow         ou=People,dc=analogrithems,dc=com,dc=us?one<br />
nss_base_group          ou=Groups,dc=analogrithems,dc=com,dc=us?one<br />
sudoers_base    ou=SUDOers,dc=analogrithems,dc=com,dc=us<br />
#This options is VERY helpful for debugging sudo ldap extension<br />
#sudoers_debug 2</p>
<p><strong>PAM</strong><br />
PAM is an authentication and accounting module that allows Linux services to use different methods for authenticating users (MySQL, LDAP, NIS, etc..). Their are two different pam config versions. You should look up the version your gnu/linux vedor uses for exact details on ldap setup, for SuSE you modify &#8220;/etc/security/pam_unix2.conf&#8221; to include the following. This system makes it so you don&#8217;t have to modify every pam service to use ldap, you just let them all use the standard unix2 module which then handles the ldap lookup.</p>
<p>auth:   use_ldap<br />
account:        use_ldap<br />
password:       use_ldap<br />
session:        none</p>
<p><strong>NSS</strong><br />
Edit your &#8216;/etc/nsswitch.conf&#8217; place the word ldap before files for the passwd and group name service. You can also add it to sudoers if you want to store your sudo configs in ldap also</p>
<p>passwd: compat<br />
shadow: compat<br />
group: compat<br />
sudoers: files ldap</p>
<p>passwd_compat: files ldap<br />
shadow_compat: files ldap<br />
group_compat: files ldap</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.analogrithems.com/rant/2008/09/16/ldap-with-linux-a-basic-primmer/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.analogrithems.com/rant/2008/09/16/ldap-with-linux-a-basic-primmer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
