]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/LDAP.php
add PdoDbPassUser
[SourceForge/phpwiki.git] / lib / WikiUser / LDAP.php
1 <?php //-*-php-*-
2 rcs_id('$Id: LDAP.php,v 1.4 2004-12-26 17:11:17 rurban Exp $');
3 /* Copyright (C) 2004 $ThePhpWikiProgrammingTeam
4  * This file is part of PhpWiki. Terms and Conditions see LICENSE. (GPL2)
5  */
6
7 class _LDAPPassUser
8 extends _PassUser
9 /**
10  * Define the vars LDAP_AUTH_HOST and LDAP_BASE_DN in config/config.ini
11  *
12  * Preferences are handled in _PassUser
13  */
14 {
15     function _init() {
16         if ($this->_ldap = ldap_connect(LDAP_AUTH_HOST)) { // must be a valid LDAP server!
17             global $LDAP_SET_OPTION;
18             if (!empty($LDAP_SET_OPTION)) {
19                 foreach ($LDAP_SET_OPTION as $key => $value) {
20                     //if (is_string($key) and defined($key))
21                     //    $key = constant($key);
22                     ldap_set_option($this->_ldap, $key, $value);
23                 }
24             }
25             if (LDAP_AUTH_USER)
26                 if (LDAP_AUTH_PASSWORD)
27                     // Windows Active Directory Server is strict
28                     $r = ldap_bind($this->_ldap, LDAP_AUTH_USER, LDAP_AUTH_PASSWORD); 
29                 else
30                     $r = ldap_bind($this->_ldap, LDAP_AUTH_USER); 
31             else
32                 $r = true; // anonymous bind allowed
33             if (!$r) {    
34                 $this->_free();
35                 trigger_error(sprintf("Unable to bind LDAP server %s", LDAP_AUTH_HOST), 
36                               E_USER_WARNING);
37                 return false;
38             }
39             return $this->_ldap;
40         } else {
41             return false;
42         }
43     }
44     
45     function _free() {
46         if (isset($this->_sr)   and is_resource($this->_sr))   ldap_free_result($this->_sr);
47         if (isset($this->_ldap) and is_resource($this->_ldap)) ldap_close($this->_ldap);
48         unset($this->_sr);
49         unset($this->_ldap);
50     }
51     
52     function checkPass($submitted_password) {
53
54         $this->_authmethod = 'LDAP';
55         $userid = $this->_userid;
56         if (!$this->isValidName()) {
57             trigger_error(_("Invalid username."),E_USER_WARNING);
58             return $this->_tryNextPass($submitted_password);
59         }
60         if (!$this->_checkPassLength($submitted_password)) {
61             return WIKIAUTH_FORBIDDEN;
62         }
63         if (strstr($userid,'*')) {
64             trigger_error(fmt("Invalid username '%s' for LDAP Auth",$userid), 
65                           E_USER_WARNING);
66             return WIKIAUTH_FORBIDDEN;
67         }
68
69         if ($ldap = $this->_init()) {
70             // Need to set the right root search information. See config/config.ini
71             $st_search = LDAP_SEARCH_FIELD
72                 ? LDAP_SEARCH_FIELD."=$userid"
73                 : "uid=$userid";
74             if (!$this->_sr = ldap_search($ldap, LDAP_BASE_DN, $st_search)) {
75                 $this->_free();
76                 return $this->_tryNextPass($submitted_password);
77             }
78             $info = ldap_get_entries($ldap, $this->_sr); 
79             if (empty($info["count"])) {
80                 $this->_free();
81                 return $this->_tryNextPass($submitted_password);
82             }
83             // There may be more hits with this userid.
84             // Of course it would be better to narrow down the BASE_DN
85             for ($i = 0; $i < $info["count"]; $i++) {
86                 $dn = $info[$i]["dn"];
87                 // The password is still plain text.
88                 // On wrong password the ldap server will return: 
89                 // "Unable to bind to server: Server is unwilling to perform"
90                 // The @ catches this error message.
91                 if ($r = @ldap_bind($ldap, $dn, $submitted_password)) {
92                     // ldap_bind will return TRUE if everything matches
93                     $this->_free();
94                     $this->_level = WIKIAUTH_USER;
95                     return $this->_level;
96                 }
97             }
98             $this->_free();
99         }
100
101         return $this->_tryNextPass($submitted_password);
102     }
103
104     function userExists() {
105         $userid = $this->_userid;
106         if (strstr($userid, '*')) {
107             trigger_error(fmt("Invalid username '%s' for LDAP Auth", $userid),
108                           E_USER_WARNING);
109             return false;
110         }
111         if ($ldap = $this->_init()) {
112             // Need to set the right root search information. see ../index.php
113             $st_search = LDAP_SEARCH_FIELD
114                 ? LDAP_SEARCH_FIELD."=$userid"
115                 : "uid=$userid";
116             if (!$this->_sr = ldap_search($ldap, LDAP_BASE_DN, $st_search)) {
117                 $this->_free();
118                 return $this->_tryNextUser();
119             }
120             $info = ldap_get_entries($ldap, $this->_sr); 
121
122             if ($info["count"] > 0) {
123                 $this->_free();
124                 return true;
125             }
126         }
127         $this->_free();
128         return $this->_tryNextUser();
129     }
130
131     function mayChangePass() {
132         return false;
133     }
134
135 }
136
137 // $Log: not supported by cvs2svn $
138 // Revision 1.3  2004/12/20 16:05:01  rurban
139 // gettext msg unification
140 //
141 // Revision 1.2  2004/12/19 00:58:02  rurban
142 // Enforce PASSWORD_LENGTH_MINIMUM in almost all PassUser checks,
143 // Provide an errormessage if so. Just PersonalPage and BogoLogin not.
144 // Simplify httpauth logout handling and set sessions for all methods.
145 // fix main.php unknown index "x" getLevelDescription() warning.
146 //
147 // Revision 1.1  2004/11/01 10:43:58  rurban
148 // seperate PassUser methods into seperate dir (memory usage)
149 // fix WikiUser (old) overlarge data session
150 // remove wikidb arg from various page class methods, use global ->_dbi instead
151 // ...
152 //
153
154 // Local Variables:
155 // mode: php
156 // tab-width: 8
157 // c-basic-offset: 4
158 // c-hanging-comment-ender-p: nil
159 // indent-tabs-mode: nil
160 // End:
161 ?>