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

0
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 <?php
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 Class RegistrationBackend {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 protected $dbconn;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 protected $hostname = "localhost";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 protected $port = 389;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 protected $bind_dn;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 protected $bind_pw;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 protected $base_dn;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 protected $objectClass = array(
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 "cmuSaslUser",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 "mboxUser",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 "inetUser",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 "inetOrgPerson",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 "organizationalPerson",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 "person"
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 );
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 protected $debug = FALSE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 private $ldap;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 function init($config)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if(array_key_exists('ldapHostname', $config))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 $this->hostname = $config['ldapHostname'];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if(array_key_exists('ldapPort', $config))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 $this->port = $config['ldapPort'];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if(array_key_exists('ldapBindDN', $config))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 $this->bind_dn = $config['ldapBindDN'];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if(array_key_exists('ldapBindPass', $config))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 $this->bind_pw = $config['ldapBindPass'];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if(array_key_exists('ldapBaseDN', $config))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 $this->base_dn = $config['ldapBaseDN'];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if($config['debug'])
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 $this->debug = TRUE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 $this->ldap = ldap_connect($this->hostname, $this->port)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 or die("Cannot connect to DSA");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 ldap_bind($this->ldap, $this->bind_dn, $this->bind_pw)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 or die("Cannot bind to DSA");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function validate($user, $pass)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if(strcspn($user, " \"#+,;<=>\\") !== strlen($user))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return "Your username contains invalid characters.";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return NULL;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 function exists($user)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 $dn = "uid=" . $user . "," . $this->base_dn;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 $res = @ldap_read($this->ldap, $dn, "objectClass=inetOrgPerson");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if($res === FALSE)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return FALSE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return TRUE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function create($user, $pass)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 $dn = "uid=" . $user . "," . $this->base_dn;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 $attrs["objectClass"] = $this->objectClass;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 $attrs["uid"] = $user;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 $attrs["userPassword"] = $pass;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 # X.500 person class requires a Surname.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 $attrs["sn"] = $user;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 # Something else (?) requires a CommonName.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 $attrs["cn"] = $attrs["sn"];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if (@ldap_add($this->ldap, $dn, $attrs))
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 return TRUE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 else
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 if($this->debug)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 $errno = ldap_errno($this->ldap);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 echo "<p>Exciting error number " . $errno . ": <i>" . ldap_err2str($errno) . "</i></p>";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 echo "<p>DN: " . $dn . "</p>";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 echo "<p>Attrs: <pre>";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 print_r($attrs);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 echo "</pre></p>";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 return FALSE;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 function close()
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 ldap_unbind($this->ldap);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 ?>

mercurial