/
LdapAccount.php
171 lines (147 loc) · 4.55 KB
/
LdapAccount.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
<?php
/**
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar
* 14 rue de Plaisance, 75014 Paris, France
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
**/
/**
Usage: Edit LocalSettings.php and add the following variables
require_once 'extensions/LdapAccount/LdapAccount.php';
# Description: Directory Server URI with protocol (not port)
# Default: None
$wgDS = "ldaps://ldap.example.com";
# Description: Directory Server Port
# Default: 389
$wgDSPort = 389;
# Description: Bind Type (Anonymous or ProxyAccount)
# Default: Anonymous
$wgBindType = "Anonymous";
# Description: Proxy Bind Account DN
# Default: None
$wgBindProxyDN = "uid=userid,ou=people,dc=example,dc=com";
# Description: Proxy Bind Account Password
# Default: None
$wgBindProxyPW = "apassword";
# Description: LDAP Attribute to search on
# Default: uid
$wgUserAttr = "uid";
# Description: LDAP Search Base for looking up accounts
# Default: None
$wgLDAPSearchBase="ou=people,dc=example,dc=com";
**/
$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['*']['edit'] = false;
$wgExtensionCredits['other'][] = array(
'name' => 'LdapAccount',
'version' => '0.1',
'author' => array('Michael Stahnke'),
'url' => 'http://github.com/stahnma/mediawiki-LdapAccount',
'description' => 'Restrict mediawiki to using LDAP accounts only, creates account based on
LDAP information and authenticates using LDAP.'
);
require_once("AuthPlugin.php");
class LdapAccount extends AuthPlugin {
function setLDAPVars() {
global $wgDSPort;
global $wgDSPort;
global $wgBindType;
global $wgLDAPSearchBase;
global $wgLDAPUserAttr;
if (! isset($wgDSPort))
$wgDSPort = 389;
if (! isset($wgBindType))
$wgBindType = "Anonymous";
if (! isset($wgLDAPSearchBase))
$wgLDAPSearchBase="ou=people,dc=example,dc=com";
if (! isset($wgLDAPUserAttr))
$wgLDAPUserAttr = "uid";
}
function dsConnect($bind = "yes") {
$this->setLDAPVars();
global $wgDS;
global $wgDSPort;
global $wgBindType;
global $wgBindProxyDN;
global $wgBindProxyPW;
$ldap_conn = ldap_connect($wgDS, $wgDSPort);
if (isset($ldap_conn) and $ldap_conn) {
ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
if($bind == "no")
return $ldap_conn;
if(isset($wgBindType) and ($wgBindType == "ProxyAccount") ) {
if (ldap_bind($ldap_conn, $wgBindProxyDN, $wgBindProxyPW))
return $ldap_conn;
else
return false;
}
else {
if (ldap_bind($ldap_conn))
return $ldap_conn;
else
return false;
}
}
return false;
}
function userExists( $username ) {
$this->setLDAPVars();
global $wgLDAPSearchBase;
global $wgLDAPUserAttr;
$ldap_conn = $this->dsConnect();
$results = @ldap_search( $ldap_conn, $wgLDAPSearchBase, "$wgLDAPUserAttr=$username");
$info = @ldap_get_entries($ldap_conn,$results);
ldap_close($ldap_conn);
if ($info["count"] > 0)
return true;
else
return false;
}
function initUser( &$user ) {
$this->setLDAPVars();
global $wgLDAPSearchBase;
global $wgLDAPUserAttr;
$ldap_conn = $this->dsConnect();
$userId = $user->getName();
$results = ldap_search($ldap_conn, $wgLDAPSearchBase, "$wgLDAPUserAttr=$userId");
$info = ldap_get_entries($ldap_conn,$results);
if ($info["count"] > 0) {
$entry = $info[0];
@ $user->setRealName($entry["name"][0]);
$user->setEmail($entry["mail"][0]);
}
ldap_close($ldap_conn);
}
function authenticate( $username, $password ) {
$ldap_conn = $this->dsConnect("no");
global $wgLDAPSearchBase;
global $wgLDAPUserAttr;
if (isset($ldap_conn) and ldap_bind($ldap_conn, "$wgLDAPUserAttr=$username,$wgLDAPSearchBase", $password))
return true;
else
return false;
}
function autoCreate() {
return true;
}
function strict() {
return true;
}
function allowPasswordChange() {
return false;
}
function setPassword($user, $password) {
return false;
}
}
?>