ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ConfigHelper.php
Go to the documentation of this file.
1<?php
2
12{
17 private $location;
18
19
23 private $hostname;
24
25
29 private $enableTLS;
30
31
37 private $debug;
38
39
45 private $timeout;
46
52 private $port;
53
57 private $referrals;
58
63
68
73
77 private $searchBase;
78
82 private $searchScope;
83
88
93
97 private $dnPattern;
98
102 private $attributes;
103
107 private $privRead;
108
113
118
119
126 public function __construct($config, $location)
127 {
128 assert(is_array($config));
129 assert(is_string($location));
130
131 $this->location = $location;
132
133 // Parse configuration
135
136 $this->hostname = $config->getString('hostname');
137 $this->enableTLS = $config->getBoolean('enable_tls', false);
138 $this->debug = $config->getBoolean('debug', false);
139 $this->timeout = $config->getInteger('timeout', 0);
140 $this->port = $config->getInteger('port', 389);
141 $this->referrals = $config->getBoolean('referrals', true);
142 $this->searchEnable = $config->getBoolean('search.enable', false);
143 $this->privRead = $config->getBoolean('priv.read', false);
144
145 if ($this->searchEnable) {
146 $this->searchUsername = $config->getString('search.username', null);
147 if ($this->searchUsername !== null) {
148 $this->searchPassword = $config->getString('search.password');
149 }
150
151 $this->searchBase = $config->getArrayizeString('search.base');
152 $this->searchScope = $config->getString('search.scope', 'subtree');
153 $this->searchFilter = $config->getString('search.filter', null);
154 $this->searchAttributes = $config->getArray('search.attributes');
155
156 } else {
157 $this->dnPattern = $config->getString('dnpattern');
158 }
159
160 // Are privs needed to get to the attributes?
161 if ($this->privRead) {
162 $this->privUsername = $config->getString('priv.username');
163 $this->privPassword = $config->getString('priv.password');
164 }
165
166 $this->attributes = $config->getArray('attributes', null);
167 }
168
169
181 public function login($username, $password, array $sasl_args = null)
182 {
183 assert(is_string($username));
184 assert(is_string($password));
185
186 if (empty($password)) {
187 SimpleSAML\Logger::info($this->location.': Login with empty password disallowed.');
188 throw new SimpleSAML_Error_Error('WRONGUSERPASS');
189 }
190
191 $ldap = new SimpleSAML_Auth_LDAP($this->hostname, $this->enableTLS, $this->debug, $this->timeout, $this->port, $this->referrals);
192
193 if (!$this->searchEnable) {
194 $ldapusername = addcslashes($username, ',+"\\<>;*');
195 $dn = str_replace('%username%', $ldapusername, $this->dnPattern);
196 } else {
197 if ($this->searchUsername !== null) {
198 if (!$ldap->bind($this->searchUsername, $this->searchPassword)) {
199 throw new Exception('Error authenticating using search username & password.');
200 }
201 }
202
203 $dn = $ldap->searchfordn($this->searchBase, $this->searchAttributes, $username, true, $this->searchFilter, $this->searchScope);
204 if ($dn === null) {
205 /* User not found with search. */
206 SimpleSAML\Logger::info($this->location.': Unable to find users DN. username=\''.$username.'\'');
207 throw new SimpleSAML_Error_Error('WRONGUSERPASS');
208 }
209 }
210
211 if (!$ldap->bind($dn, $password, $sasl_args)) {
212 SimpleSAML\Logger::info($this->location.': '.$username.' failed to authenticate. DN='.$dn);
213 throw new SimpleSAML_Error_Error('WRONGUSERPASS');
214 }
215
216 /* In case of SASL bind, authenticated and authorized DN may differ */
217 if (isset($sasl_args)) {
218 $dn = $ldap->whoami($this->searchBase, $this->searchAttributes);
219 }
220
221 /* Are privs needed to get the attributes? */
222 if ($this->privRead) {
223 /* Yes, rebind with privs */
224 if (!$ldap->bind($this->privUsername, $this->privPassword)) {
225 throw new Exception('Error authenticating using privileged DN & password.');
226 }
227 }
228
229 return $ldap->getAttributes($dn, $this->attributes);
230 }
231
232
255 public function searchfordn($attribute, $value, $allowZeroHits)
256 {
257 $ldap = new SimpleSAML_Auth_LDAP($this->hostname,
258 $this->enableTLS,
259 $this->debug,
260 $this->timeout,
261 $this->port,
262 $this->referrals);
263
264 if ($attribute == null) {
265 $attribute = $this->searchAttributes;
266 }
267
268 if ($this->searchUsername !== null) {
269 if (!$ldap->bind($this->searchUsername, $this->searchPassword)) {
270 throw new Exception('Error authenticating using search username & password.');
271 }
272 }
273
274 return $ldap->searchfordn($this->searchBase, $attribute,
275 $value, $allowZeroHits, $this->searchFilter, $this->searchScope);
276 }
277
278 public function getAttributes($dn, $attributes = null)
279 {
280 if ($attributes == null) {
282 }
283
284 $ldap = new SimpleSAML_Auth_LDAP($this->hostname,
285 $this->enableTLS,
286 $this->debug,
287 $this->timeout,
288 $this->port,
289 $this->referrals);
290
291 /* Are privs needed to get the attributes? */
292 if ($this->privRead) {
293 /* Yes, rebind with privs */
294 if (!$ldap->bind($this->privUsername, $this->privPassword)) {
295 throw new Exception('Error authenticating using privileged DN & password.');
296 }
297 }
298 return $ldap->getAttributes($dn, $attributes);
299 }
300
301}
An exception for terminatinating execution or to throw for unit testing.
static info($string)
Definition: Logger.php:199
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
$privPassword
The password we should bind with before we can get the attributes.
$hostname
The hostname of the LDAP server.
__construct($config, $location)
Constructor for this configuration parser.
$referrals
Whether to follow referrals.
$searchAttributes
The attributes which should match the username.
$searchBase
Array with the base DN(s) for the search.
getAttributes($dn, $attributes=null)
$searchUsername
The username we should bind with before we can search for the user.
$privUsername
The DN we should bind with before we can get the attributes.
$privRead
The user cannot get all attributes, privileged reader required.
searchfordn($attribute, $value, $allowZeroHits)
Search for a DN.
$attributes
The attributes we should fetch.
login($username, $password, array $sasl_args=null)
Attempt to log in using the given username and password.
$searchScope
The scope of the search.
$enableTLS
Whether we should use TLS/SSL when contacting the LDAP server.
$dnPattern
The DN pattern we should use to create the DN from the username.
$searchFilter
Additional LDAP filter fields for the search.
$searchEnable
Whether we need to search for the users DN.
$location
String with the location of this configuration.
$searchPassword
The password we should bind with before we can search for the user.
$password
Definition: cron.php:14
$config
Definition: bootstrap.php:15