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