-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
243 lines (201 loc) · 7.25 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* OpenLDAP + Kerberos authentication backend
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sebastián Santisi <[email protected]>
*/
class auth_plugin_authopenldapkerberos extends DokuWiki_Auth_Plugin
{
/** @var array filter pattern */
protected $pattern = array();
public function __construct()
{
parent::__construct();
if(!function_exists('ldap_connect') || !class_exists('KRB5CCache')) {
// This condition isn't checking for a valid Kerberos ticket or for
// a system GSSAPI library, the only way to check that is trying to
// bind to LDAP.
$this->success = false;
return;
}
$this->cando['getUsers'] = true;
$this->cando['getUserCount'] = true;
$this->cando['getGroups'] = true;
}
public function checkPass($user, $pass)
{
try {
$p = new KRB5CCache();
$p->initPassword($user, $pass);
return true;
}
catch(Exception $e) {
return false;
}
}
public function getUserData($user, $requireGroups = true)
{
$c = $this->connect();
$r = ldap_search(
$c,
"ou={$this->getConf('user_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('userkey')}=$user)",
Array($this->getConf('username'), $this->getConf('usergid'), $this->getConf('usermail'))
);
$e = ldap_get_entries($c, $r);
$userdata = Array(
'name' => $e[0][$this->getConf('username')][0],
'mail' => $e[0][$this->getConf('usermail')][0],
'grps' => Array()
);
if($requireGroups) {
$gid = $e[0][$this->getConf('usergid')][0];
$r = ldap_search(
$c,
"ou={$this->getConf('group_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('groupkey')}=*)",
Array($this->getConf('groupkey'), $this->getConf('groupgid'), $this->getConf('groupuids'))
);
$e = ldap_get_entries($c, $r);
for($i = 0; $i < $e['count']; $i++) {
$cn = $e[$i][$this->getConf('groupkey')][0];
$gn = $e[$i][$this->getConf('groupgid')]['0'];
if($gn == $gid)
$userdata['grps'][] = $cn;
else if(array_key_exists($this->getConf('groupuids'), $e[$i])) {
for($j = 0; $j < $e[$i][$this->getConf('groupuids')]['count']; $j++) {
if($e[$i][$this->getConf('groupuids')][$j] == $user)
$userdata['grps'][] = $cn;
}
}
}
}
ldap_unbind($c);
return $userdata;
}
protected function connect() {
$c = ldap_connect($this->getConf('server'), $this->getConf('port'));
ldap_set_option($c, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_sasl_bind($c, NULL, NULL, 'GSSAPI');
return $c;
}
public function getUserCount($filter = array())
{
$c = $this->connect();
$r = ldap_search(
$c,
"ou={$this->getConf('user_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('userkey')}=*)",
Array($this->getConf('usergid'))
);
$e = ldap_get_entries($c, $r);
ldap_unbind($c);
return $e['count'];
}
public function retrieveUsers($start = 0, $limit = 0, $filter = array())
{
$this->constructPattern($filter);
$c = $this->connect();
$r = ldap_search(
$c,
"ou={$this->getConf('user_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('userkey')}=*)",
Array($this->getConf('userkey'), $this->getConf('username'), $this->getConf('usergid'), $this->getConf('usermail'))
);
$e = ldap_get_entries($c, $r);
$users = Array();
for($i = 0; $i < $e['count']; $i++)
$users[$e[$i][$this->getConf('userkey')][0]] = Array(
'name' => $e[$i][$this->getConf('username')][0],
'mail' => $e[$i][$this->getConf('usermail')][0],
'grps' => Array(),
'gid' => $e[$i][$this->getConf('usergid')][0]
);
$r = ldap_search(
$c,
"ou={$this->getConf('group_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('groupkey')}=*)",
Array($this->getConf('groupkey'), $this->getConf('groupgid'), $this->getConf('groupuids'))
);
$e = ldap_get_entries($c, $r);
ldap_unbind($c);
$groups = Array();
for($i = 0; $i < $e['count']; $i++) {
$cn = $e[$i][$this->getConf('groupkey')][0];
$gn = $e[$i][$this->getConf('groupgid')]['0'];
$groups[$gn] = $cn;
if(array_key_exists($this->getConf('groupuids'), $e[$i]))
for($j = 0; $j < $e[$i][$this->getConf('groupuids')]['count']; $j++)
$users[$e[$i][$this->getConf('groupuids')][$j]]['grps'][] = $cn;
}
$out = Array();
$uids = array_keys($users);
sort($uids);
foreach($uids as $uid) {
$users[$uid]['grps'][] = $groups[$users[$uid]['gid']];
if($this->filter($uid, $users[$uid])) {
if($start > 0)
$start--;
else {
$out[$uid] = $users[$uid];
if(count($out) == $limit)
break;
}
}
}
return $out;
}
public function retrieveGroups($start = 0, $limit = 0)
{
$c = $this->connect();
$r = ldap_search(
$c,
"ou={$this->getConf('group_ou')},{$this->getConf('base_dn')}",
"({$this->getConf('groupkey')}=*)",
Array($this->getConf('groupkey'))
);
$e = ldap_get_entries($c, $r);
ldap_unbind($c);
if($limit == 0 || $limit > $e['count']) $limit = $e['count'];
$groups = Array();
for($i = $start; $i < $limit; $i++) {
$groups[] = $e[$i][$this->getConf('groupkey')][0];
}
return $groups;
}
/**
* return true if $user + $info match $filter criteria, false otherwise
*
* @author Chris Smith <[email protected]>
*
* @param string $user User login
* @param array $info User's userinfo array
* @return bool
*/
protected function filter($user, $info)
{
foreach ($this->pattern as $item => $pattern) {
if ($item == 'user') {
if (!preg_match($pattern, $user)) return false;
} elseif ($item == 'grps') {
if (!count(preg_grep($pattern, $info['grps']))) return false;
} else {
if (!preg_match($pattern, $info[$item])) return false;
}
}
return true;
}
/**
* construct a filter pattern
*
* @param array $filter
*/
protected function constructPattern($filter)
{
$this->pattern = array();
foreach ($filter as $item => $pattern) {
$this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
}
}
}