PHP

Combining CAS and LDAP

After going over CAS and LDAP, I thought I would do an example where both are used together. I have some software like this, it allows users to log in and then LDAP can go and get their full name. The example is mostly the CAS example with some LDAP added on. All I did was add on the LDAP code into the section where a user is logged into CAS. I use CAS to get the username of the user and feed it into LDAP. Below is the new index of the CAS example, nothing else is changed:

<?PHP
//Dan Berkowitz LDAP tutorial, May 2013, Buildingtents.com

include_once(“./CAS-1.3.2/CAS.php”);
phpCAS::client(CAS_VERSION_2_0,’cas-auth.rpi.edu’,443,’/cas/’);
// SSL!
phpCAS::setCasServerCACert(“./CACert.pem”);//this is relative to the cas client.php file

if (phpCAS::isAuthenticated())
{

$LDAPCON = ldap_connect(“ldap.rpi.edu”); //Have to be internal to VCC or VCC firewall will block
$LDAPBIND = ldap_bind($LDAPCON);
$ResultArray = Array();
$filterArray = array(“givenname”, “sn”);
$LDAPSEARCH = ldap_search($LDAPCON, “dc=rpi, dc=edu”, “(uid=” . phpCAS::getUser() . “)”, $filterArray, 0 , 10);
$LDAPRESULTS = ldap_get_entries($LDAPCON, $LDAPSEARCH);
//print_r($LDAPRESULTS);
for ($i = 0; $i < $LDAPRESULTS[“count”]; $i++)
{
$tempRow = Array();
array_push($tempRow, $LDAPRESULTS[$i][“givenname”][0]);
array_push($tempRow, $LDAPRESULTS[$i][“sn”][0]);
array_push($ResultArray, $tempRow);
}
ldap_close($LDAPCON);

echo “User:” . phpCAS::getUser();
if (sizeof($ResultArray) == 1)
{
echo ” ” . $ResultArray[0][0] . ” ” . $ResultArray[0][1];
}
echo “<a href=’./logout.php’>Logout</a>”;
}else{
echo “<a href=’./login.php’>Login</a>”;
}

?>

Download: http://programs.buildingtents.com/CAS_LDAP_Example.zip

Advertisements

LDAP Authentication RPI Tutorial (Part 3)

Now that we have gone over how to setup LDAP, and went into some more depth about how to search using it, we will now look at actually writing a web page in PHP that uses LDAP. As always, I will be using RPI as my example but this should work for anyone with an LDAP system. (Note to people at RPI, you need to VPN in unless you are in the VCC for this to work, I have had luck with doing this in Lally, but in the Union it failed) The first example will go over how to just use LDAP to return information; the second one will incorporate the CAS example that was done before, and search for the user that logs in, this will be put out in a few days. The LDAP servers I am using do not require authentication, if the one you are using does then you will need to go to http://www.php.net/manual/en/function.ldap-bind.php and look at using authentication on your bind command.

  1. Within a new PHP document, enter the following line with ‘ldap.rpi.edu’ replaced with your LDAP server. The variable can be named anything as long as you remember it is for the connection.
    • “$LDAPCON = ldap_connect(‘ldap.rpi.edu’);”
  2. Now we have to bind to the server, this is when credentials are given (if needed) and we fully connect. If the server is unreachable, or you are not permitted to connect this is where PHP will throw an error. As you can see, we create a new variable for the binding, and feed in our connection variable.
    • “$LDAPBIND = ldap_bind($LDAPCON);”
  3. We have seen before that LDAP can return vast amounts of information on a single item, and since many servers have a limit on how much they will return it is good practice to filter for just what we want back. Here I will be requesting the “givenname” and “sn” for each user. These items must be put into an array like shown.
    • “$filterArray = array(‘givenname’,’sn’);”
  4. The core of the search is the search command. Here we give all the different compounds we have made and put them together. First, we enter the connection to use; second, we enter the base for the search (described in part 1&2). Following that we enter a filter for how we want to search the directory, this is not the filter we setup one step ago but a filter to tell the central LDAP what we are looking for. I am searching for anyone with a UID that starts with ‘berkod’. Then we enter the filter we setup earlier for the types of data we want returned. The last two settings are setup per instance; start with a 0 or 1 for attributes only filter, 0 means return the full data, 1 means that you just want the type returned if data exists (this is for more of a fast exploratory search). To end the command you enter the number of results that should be returned; 0 is no limit, yet I am hoping to search usernames and get 1 result. I entered 10 just so if more than 1 user exists under my filter I will know.
    • $LDAPSEARCH = ldap_search($LDAPCON, “dc=rpi, dc=edu”, “(uid=berkod*)”, $filterArray, 0 , 10);
  5. The results from the search have to be stored in a separate variable
    • $LDAPRESULTS = ldap_get_entries($LDAPCON, $LDAPSEARCH);
  6. Now for a quick and dirty view of the result you can simply print out the data
    • “print_r($LDAPRESULTS);”
  7. But that just lets you quickly see if you are getting data back, to properly put the data into an array use the following code. This will get the two pieces we requested for each user (“givenname” and “sn”) and store them in an array; then put that array into another array. The final format is $variable[$user][0 for ‘givenname’/ 1 for ‘sn’]. This data can be used by other code or printed out.
    • $ResultArray = Array();
      for ($i = 0; $i < $LDAPRESULTS[“count”]; $i++)
              {
                  $tempRow = Array();
                  array_push($tempRow, $LDAPRESULTS[$i][“givenname”][0]); // 0 is used because my database just has one item per user
                  array_push($tempRow, $LDAPRESULTS[$i][“sn”][0]);
                  array_push($ResultArray, $tempRow);
              }
  8. Then for good practice close the LDAP connection
    • “ldap_close($LDAPCON);”

The next post will go over combining CAS and LDAP. Until then thanks for commenting and feel free to ask questions.

Download: http://programs.buildingtents.com/ldap.php.zip

References:

http://www.php.net/manual/en/function.ldap-search.php

LDAP Authentication RPI Tutorial (Part 1)

After writing about how to use CAS with PHP, I thought I would write a post about how to use LDAP(Lightweight Directory Access Protocol) at RPI but these methods can be used anywhere. LDAP is a protocol to query user databases, this is a protocol that can be sed along with Active Directory, or another directory system for computers and user accounts. This protocol is widely used to allow different applications to interact with your user database. Here I will be showing how to implement search with LDAP to a web application. This guide will be using LDAP with PHP, this requires the LDAP module to be enabled within PHP; that will be the purpose of this article, then the next one will discuss how to actually query LDAP.

LDAP Linux (Debian/Ubuntu) Install

Linux is easy to get LDAP working with PHP, as long as you have a standard installation of Apache, with PHP 5 working.

  1. Install the LDAP module onto the machine, using either aptitude or apt-get
    • “sudo aptitude install php5-ldap”
    • OR “sudo apt-get install php5-ldap”
  2. PHP should now be able to use LDAP, if it is not working yet, you will need to restart Apache.
    • “sudo service apache2 restart”

LDAP Windows (XAMPP) Install

Xampp for Windows comes with LDAP, but there is a bug in their implementation and a file needs to be copied before LDAP will work. I am going to use “C:\xampp”, the default directory for Xampp in this example.

  1. Go into the PHP folder, C:\xampp\php\
  2. Edit the file “php.ini” with any text editor
  3. Find the line “;extension=php_ldap.dll”, and remove the semi-colon. “extension=php_ldap.dll”
  4. Now if you were to reboot Apache it should be working, but its not! Why not? There is a missing DLL. You need to
    copy libsasl.dll from c:\xampp\php\libsasl.dll to C:\xampp\apache\bin\.
  5. Now restart Apache

LDAP Search

Now that PHP can search LDAP we are going to want to start creating queries in PHP; but it is much easier to tweak the search in the command line, and then put that query into PHP. The following are steps that can be taken on a Linux computer (again Ubuntu/Debian) to install and use a ldap command line search.

  1. First we need to install the OpenLDAP utilities that will give us the “ldapsearch” command
    • “sudo aptitude install openldap-utils”
    • OR “sudo apt-get install openldap-utils”
  2. Now we are making our query
    • First we add the command, then enter the host you are searching, tell the server to try simple anonymous authentication. Next give the server a base to start the search (I am using RPI specific domain components), finally we have to give the heart of our search. I am looking for any Unique ID (username) that starts with “berk”, and ends with anything “*”.
    • ldapsearch -h “ldap.rpi.edu” -x -b “dc=rpi, dc=edu” “uid=berk*”
    • Now this gives one result, and this can be used to see what data will be returned from this server. You can also try “ldap1.server.rpi.edu” this returns a entirely different list of variables, and sometimes more users.
    • If you are interested in researching this command more, die.net has a great resource. http://linux.die.net/man/1/ldapsearch
    • Troubleshooting: For those of you here at RPI trying to follow this guide specifically, if you do not get any results or a error connecting, RPI firewalls the LDAP servers heavily. I have found a lot of the time I have to be in the VCC to make this work, you can also VPN in, then your network connection is within the VCC and it will work. I have VPNed in, while on campus in the Union to get LDAP to work.

UPDATE: I added a little about what LDAP is

RPI phpCAS Authentication Tutorial

After much tinkering with RPI’s CAS (Central Authentication System) in PHP, I thought I would put together a guide to make it easy for anyone to put together a site that uses it. This would work for anyone at another location with a CAS server, but this example is for RPI.

  1. Get the CAS Library
  2. Download the tar file under “Current Version”
  3. Extract the contents, using a program such as 7-Zip, and put it in the root of whatever web folder you want
  4. Download the latest CA bundle for SSL
  5. Create a index.php, login.php, logout.php
  6. The index has to load the library, check if the user is logged in, then print out text.
    • <?PHP

      include_once(“./CAS-1.3.2/CAS.php”);
      phpCAS::client(CAS_VERSION_2_0,’cas-auth.rpi.edu’,443,’/cas/’);
      // SSL!
      phpCAS::setCasServerCACert(“./CACert.pem”);//this is relative to the cas client.php file

      if (phpCAS::isAuthenticated())
      {
      echo “User:” . phpCAS::getUser();
      echo “<a href=’./logout.php’>Logout</a>”;
      }else{
      echo “<a href=’./login.php’>Login</a>”;
      }

      ?>

       

    • First we load the library for CAS from the subfolder
    • Then we select which will be our central server
    • We have to select our ca bundle, setCasServerCert does this
    • Now we have fully loaded and configured the library
    • Finally, I can ask CAS if a user has logged in, if so writeout some options, if not others
  7. This is the login page
    • <?PHP

      include_once(“./CAS-1.3.2/CAS.php”);
      phpCAS::client(CAS_VERSION_2_0,’cas-auth.rpi.edu’,443,’/cas/’);
      // SSL!
      phpCAS::setCasServerCACert(“./CACert.pem”);//this is relative to the cas client.php file

      if (!phpCAS::isAuthenticated())
      {
      phpCAS::forceAuthentication();
      }else{
      header(‘location: ./index.php’);
      }

      ?>

       

    • Similar setup of authentication as before
    • Now we check if the user is NOT authenticated, if the user is not authenticated we force login
    • If the user already is logged in, then we redirect to the index
  8. The logout page:
    • <?PHP

      include_once(“./CAS-1.3.2/CAS.php”);
      phpCAS::client(CAS_VERSION_2_0,’cas-auth.rpi.edu’,443,’/cas/’);
      // SSL!
      phpCAS::setCasServerCACert(“./CACert.pem”);//this is relative to the cas client.php file

      if (phpCAS::isAuthenticated())
      {
      phpCAS::logout();
      }else{
      header(‘location: ./index.php’);
      }

      ?>

       

    • Same configuration (this can be done by including a core file that everything else calls, but for this example I wanted to keep it simple)
    • If they are not logged in, then we push the user back to login

That is the basic configuration, the example is available for download below. If there are any questions feel free to post a comment.

Download: http://programs.buildingtents.com/CASExample.zip

Extra Notes:

  • If you want to save server space, the docs folder under the CAS folder can be removed
  • I have ran into problems with CAS on a Windows Apache server, and CAS on a Linux Apache server reference the CACert.pem file differently

Adding Strict Standards, (Or Removing)

I have been developing on a Debian Apache system for a long time, for one of the projects I have been working on I had to run on a Windows Server. After installing XAMPP I noticed that by default, XAMPP sets the developer settings of Strict Standards. Meanwhile the Linux system didn’t have that enabled. I went out wanting to set the Linux server to have the Strict settings to force me to code properly. I found many places that would tell me how to disable strict standards (because users found it bothersome), but after a little searching I didn’t find a clear guide so I thought I would write one for PHP5.

  1. Find “php.ini”
    • Windows (XAMPP): The file is kept under C:/xampp/php/php.ini
    • Linux: For php5 (at least in debian) /etc/php5/apache2/php.ini
  2. Open the file and scroll to;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; Error handling and logging ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. The line you want to edit iserror_reporting =…
  4. Copying the line below will enable strict settings, while other combinations listed below will enable different values.
    • Development Value: E_ALL | E_STRICT

My Linux box configured itself as  “error_reporting = E_ALL & ~E_DEPRECATED”, which is the standard for production.

Here is the description that comes with php.ini,

;Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting

Time Tracker

Recently I have been working on an hour keeping system for my work at RPI; we have an old time card system that is running on a Windows 2000 server. The code no longer works, as in the JavaScript is not supported by modern browsers. The old system has been running for over 11 years, and it is time to let it retire.

The new system is at version 0.1 right now, with basic functionality working. The system is split into groups, so while one server runs separate departments can run a “group” and have their employees under there. Once a user is given privileges to one group in the system, when that user logins in, that group will automatically come up.

The old system had a html page that managers could edit, to give announcements to workers. This was a bit of a tedious process to go in and manually edit these pages, now there is a field for the manager of each group to drop html to edit their page. The system has two levels of accounts and privileges, there is the privilege within a group, and the privilege for the entire system. While a user can be administrator of a group and edit their group, they may not have administrative rights over the entire server. By having administrative rights to the entire server, a user can change the splash page before a user is logged on, or create new groups in the system.

The system allows for templates to be made of weeks, so if a student worker works the same hours every week, then they can save it and deploy it easily. I am working on integrating email reminders by using a .Net application that integrates with MySQL. By having the application read who has yet to enter logs this pay period  I can use the students CAS login to get their email.

Below are some photos of the system, and it is all open source at the attached link.

Source: https://github.com/daberkow/RPI_timetracker