backend_isode_ldap.php

Thu, 13 May 2010 09:31:01 -0500

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 13 May 2010 09:31:01 -0500
changeset 0
472198dc918e
permissions
-rw-r--r--

Initial commit

<?php

Class RegistrationBackend {

	protected $dbconn;
	
	protected $hostname = "localhost";
	protected $port = 389;
	protected $bind_dn;
	protected $bind_pw;
	protected $base_dn;
	protected $objectClass = array(
                "cmuSaslUser",
                "mboxUser",
                "inetUser",
                "inetOrgPerson",
                "organizationalPerson",
                "person"
        );

	protected $debug = FALSE;

	private $ldap;
	
	function init($config)
	{
		if(array_key_exists('ldapHostname', $config))
			$this->hostname = $config['ldapHostname'];
		if(array_key_exists('ldapPort', $config))
			$this->port = $config['ldapPort'];
		if(array_key_exists('ldapBindDN', $config))
			$this->bind_dn = $config['ldapBindDN'];
		if(array_key_exists('ldapBindPass', $config))
			$this->bind_pw = $config['ldapBindPass'];
		if(array_key_exists('ldapBaseDN', $config))
			$this->base_dn = $config['ldapBaseDN'];
		
		if($config['debug'])
			$this->debug = TRUE;
		
		$this->ldap = ldap_connect($this->hostname, $this->port)
			or die("Cannot connect to DSA");
		
		ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
		
		ldap_bind($this->ldap, $this->bind_dn, $this->bind_pw)
			or die("Cannot bind to DSA");
	}

	function validate($user, $pass)
	{
		if(strcspn($user, " \"#+,;<=>\\") !== strlen($user))
			return "Your username contains invalid characters.";
		return NULL;
	}
	
	function exists($user)
	{
		$dn = "uid=" . $user . "," . $this->base_dn;
		$res = @ldap_read($this->ldap, $dn, "objectClass=inetOrgPerson");
		if($res === FALSE)
			return FALSE;
		return TRUE;
	}
	
	function create($user, $pass)
	{
		$dn = "uid=" . $user . "," . $this->base_dn;

		$attrs["objectClass"] = $this->objectClass;
		$attrs["uid"] = $user;
		$attrs["userPassword"] = $pass;
		
		# X.500 person class requires a Surname.
		$attrs["sn"] = $user;

		# Something else (?) requires a CommonName.
		$attrs["cn"] = $attrs["sn"];

		if (@ldap_add($this->ldap, $dn, $attrs))
		{
			return TRUE;
		}
		else
		{
			if($this->debug)
			{
				$errno = ldap_errno($this->ldap);
				echo "<p>Exciting error number " . $errno . ": <i>" . ldap_err2str($errno) . "</i></p>";
				echo "<p>DN: " . $dn . "</p>";
				echo "<p>Attrs: <pre>";
				print_r($attrs);
				echo "</pre></p>";
			}
			return FALSE;
		}
	}

	
	function close()
	{
		ldap_unbind($this->ldap);
	}
}	

?>

mercurial