{"id":253,"date":"2018-04-12T11:48:32","date_gmt":"2018-04-12T10:48:32","guid":{"rendered":"http:\/\/therobinsonfamily.net\/?p=253"},"modified":"2018-04-12T11:48:32","modified_gmt":"2018-04-12T10:48:32","slug":"clearos-ldap-and-remote-clients","status":"publish","type":"post","link":"https:\/\/therobinsonfamily.net\/?p=253","title":{"rendered":"ClearOS LDAP and remote clients."},"content":{"rendered":"<p><a href=\"http:\/\/clearos.com\" rel=\"noopener\" target=\"_blank\">ClearOS<\/a> is a great server with multiple management web based management tools. It also has an OpenLDAP directory server and I needed to make it work for another PHP app in my LAN. Now, getting apps to use ClearOS Directory server is not too bad if your running on a localhost install, but remote access is difficult. All the information you need is out there but not all in one place. Here is what I learned.<\/p>\n<p>I wanted it to be done with TLS, more for my own satisfaction than anything. TLS works best if you have a certificate that matches the DNS record of your server. I started by creating a self signed cert with openssl. There are many ways to do this but I used:<\/p>\n<p><code>openssl req -x509 -newkey rsa:4096 -keyout ldapkey.pem -out ldapcert.pem -nodes -days 3650<\/code><\/p>\n<p>Answer the qustions however you like but remember the  <code>CN<\/code> should be your ldap server DNS name.<\/p>\n<p>Next task is to get slapd to use the cert. <code>In \/etc\/openldap\/slapd.conf<\/code> I set <\/p>\n<p><code>TLSCACertificateFile   \/etc\/openldap\/cacerts\/ldapcert.pem<br \/>\nTLSCertificateFile     \/etc\/openldap\/cacerts\/ldapcert.pem<br \/>\nTLSCertificateKeyFile  \/etc\/openldap\/cacerts\/ldapkey.pem<br \/>\nTLSVerifyClient never<\/code><\/p>\n<p>and put the cert files generated by openssl in the appropriate places.<\/p>\n<p>In order to get slapd running I also had to comment out lines like <\/p>\n<p><code>#TLSCACertificatePath \/etc\/pki\/nssdb<br \/>\n#TLSCertificateFile Server-Cert<\/code><\/p>\n<p>Then on the ClearOS Dashboard you need to set the Directory Server  Publish Policy to &#8216;Local Network&#8217;<\/p>\n<p>On the client most tools will be querying the openldap config. For Fedora, which was my client OS, that is in \/etc\/openldap.<\/p>\n<p>Firstly I needed to install the ldapcert.pem file into \/etc\/openldap\/certs.<\/p>\n<p>Next you need to create a symlink with the name being the hash of the cert. Fortunately there is a tool and running <code>cacertdir_rehash \/etc\/openldap\/certs<\/code> will do the job for you.<\/p>\n<p>Next in \/etc\/openldap\/ldap.conf you need to add some lines<\/p>\n<p><code>TLS_CACERTDIR \/etc\/openldap\/certs<\/code><\/p>\n<p>There is a lot of nonsense on the internet suggesting the use of TLS_REQCERT. I&#8217;d suggest your read the <a href=\"https:\/\/linux.die.net\/man\/5\/ldap.conf\" target=\"_blank\" rel=\"noopener\">ldap.conf<\/a> manpage because it seems no-one else has.<\/p>\n<p>Mine is using &#8216;demand&#8217; (the default) with a self signed cert and is fine.<\/p>\n<p>In order to test Apaches ability to let a PHP app connect to an LDAP server I slightly modified php script from <a href=\"http:\/\/php.net\/manual\/en\/function.ldap-bind.php\">http:\/\/php.net\/manual\/en\/function.ldap-bind.php<\/a><\/p>\n<p>[php]<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/ using ldap bind<br \/>\n\/\/ ldap rdn or dn<\/p>\n<p>$ldaprdn  = &#8216;&lt;ClearOS BIND DN&gt;&#8217;;     <\/p>\n<p>\/\/ associated password<\/p>\n<p>$ldappass = &#8216;&lt;ClearOS BIND PASSWORD&gt;&#8217;;  <\/p>\n<p>\/\/ set the script to print debug info<\/p>\n<p>ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);<\/p>\n<p>\/\/ connect to ldap server<br \/>\n$ldapconn = ldap_connect(&quot;ldaps:\/\/&lt;servername&gt;:636&quot;)<br \/>\n    or die(&quot;Could not connect to LDAP server.&quot;);<\/p>\n<p>\/\/set php_ldap to use ldapv3 as default is v2<\/p>\n<p>ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);<\/p>\n<p>if ($ldapconn) {<\/p>\n<p>    \/\/ binding to ldap server<br \/>\n    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);<\/p>\n<p>    \/\/ verify binding<br \/>\n    if ($ldapbind) {<br \/>\n        echo &quot;LDAP bind successful&#8230;&quot;;<br \/>\n    } else {<br \/>\n        echo &quot;LDAP bind failed&#8230;&quot;;<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>?&gt;<br \/>\n[\/php]<\/p>\n<p>and you can test your client configuration by running php-cgi -f <filename>.php<\/p>\n<p>It has LDAP debugging on so you will be able to read all sorts of output to do with the TLS connection which I found immensely useful. You should also test it by hitting the webserver too by putting the script in the webroot and running <code>curl 127.0.0.1\/filename.php<\/code> If you are getting errors here then check the webserver and php logs.<\/p>\n<p>Finally, I was using php-fpm on my rhel server, it has a seperate php-fpm.service that needs to be restarted along with httpd to pick up any new openldap reconfiguration on the client. Also if you are running selinux in enforcing mode you will need to run<br \/>\n<code>setsebool -P httpd_can_network_connect 1<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ClearOS is a great server with multiple management web based management tools. It also has an OpenLDAP directory server and I needed to make it work for another PHP app in my LAN. Now, getting apps to use ClearOS Directory server is not too bad if your running on a localhost install, but remote access [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":0,"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"wp:attachment":[{"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/therobinsonfamily.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}