|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Filter to add attributes to the identity by executing a query against an LDAP directory |
|
5
|
|
|
* |
|
6
|
|
|
* Original Author: Steve Moitozo II <[email protected]> |
|
7
|
|
|
* Created: 20100513 |
|
8
|
|
|
* Updated: 20100920 Steve Moitozo II |
|
9
|
|
|
* - incorporated feedback from Olav Morken to prep code for inclusion in SimpleSAMLphp distro |
|
10
|
|
|
* - moved call to ldap_set_options() inside test for $ds |
|
11
|
|
|
* - added the output of ldap_error() to the exceptions |
|
12
|
|
|
* - reduced some of the nested ifs |
|
13
|
|
|
* - added support for multiple values |
|
14
|
|
|
* - added support for anonymous binds |
|
15
|
|
|
* - added escaping of search filter and attribute |
|
16
|
|
|
* Updated: 20111118 Ryan Panning |
|
17
|
|
|
* - Updated the class to use BaseFilter which reuses LDAP connection features |
|
18
|
|
|
* - Added conversion of original filter option names for backwards-compatibility |
|
19
|
|
|
* - Updated the constructor to use the new config method |
|
20
|
|
|
* - Updated the process method to use the new config variable names |
|
21
|
|
|
* Updated: 20131119 Yørn de Jong / Jaime Perez |
|
22
|
|
|
* - Added support for retrieving multiple values at once from LDAP |
|
23
|
|
|
* - Don't crash but fail silently on LDAP errors; the plugin is to complement attributes |
|
24
|
|
|
* Updated: 20161223 Remy Blom <[email protected]> |
|
25
|
|
|
* - Adjusted the silent fail so it does show a warning in log when $this->getLdap() fails |
|
26
|
|
|
* |
|
27
|
|
|
* @author Yørn de Jong |
|
28
|
|
|
* @author Jaime Perez |
|
29
|
|
|
* @author Steve Moitozo |
|
30
|
|
|
* @author JAARS, Inc. |
|
31
|
|
|
* @author Ryan Panning |
|
32
|
|
|
* @author Remy Blom <[email protected]> |
|
33
|
|
|
* @package SimpleSAMLphp |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
namespace SimpleSAML\Module\ldap\Auth\Process; |
|
37
|
|
|
|
|
38
|
|
|
use SimpleSAML\Logger; |
|
39
|
|
|
use SimpleSAML\Module\ldap\Auth\Ldap; |
|
40
|
|
|
use Webmozart\Assert\Assert; |
|
41
|
|
|
|
|
42
|
|
|
class AttributeAddFromLDAP extends BaseFilter |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* LDAP attributes to add to the request attributes |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $search_attributes; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* LDAP search filter to use in the LDAP query |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $search_filter; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* What to do with attributes when the target already exists. Either replace, merge or add. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $attr_policy; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Only run when none of these attributes are set. Empty array implies always run. |
|
68
|
|
|
* |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $if_missing_attributes; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Initialize this filter. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $config Configuration information about this filter. |
|
78
|
|
|
* @param mixed $reserved For future use. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __construct(array $config, $reserved) |
|
81
|
|
|
{ |
|
82
|
|
|
parent::__construct($config, $reserved); |
|
83
|
|
|
|
|
84
|
|
|
// Get filter specific config options |
|
85
|
|
|
$this->search_attributes = $this->config->getArrayize('attributes', []); |
|
86
|
|
|
if (empty($this->search_attributes)) { |
|
87
|
|
|
$new_attribute = $this->config->getString('attribute.new', ''); |
|
88
|
|
|
$this->search_attributes[$new_attribute] = $this->config->getString('search.attribute'); |
|
89
|
|
|
} |
|
90
|
|
|
$this->search_filter = $this->config->getString('search.filter'); |
|
91
|
|
|
|
|
92
|
|
|
// get the attribute policy |
|
93
|
|
|
$this->attr_policy = $this->config->getString('attribute.policy', 'merge'); |
|
94
|
|
|
|
|
95
|
|
|
$this->if_missing_attributes = $this->config->getArrayize('if.missing.attributes', []); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add attributes from an LDAP server. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array &$request The current request |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function process(array &$request): void |
|
106
|
|
|
{ |
|
107
|
|
|
Assert::keyExists($request, 'Attributes'); |
|
108
|
|
|
|
|
109
|
|
|
$attributes = &$request['Attributes']; |
|
110
|
|
|
|
|
111
|
|
|
// skip if any of if_missing_attributes is set |
|
112
|
|
|
foreach($this->if_missing_attributes as $attribute) { |
|
113
|
|
|
if (isset($attributes[$attribute])) { |
|
114
|
|
|
\SimpleSAML\Logger::debug( |
|
115
|
|
|
'AttributeAddFromLDAP: ' |
|
116
|
|
|
. 'skipping because ' . $attribute . ' is present' |
|
117
|
|
|
); |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// perform a merge on the ldap_search_filter |
|
123
|
|
|
// loop over the attributes and build the search and replace arrays |
|
124
|
|
|
$arrSearch = []; |
|
125
|
|
|
$arrReplace = []; |
|
126
|
|
|
foreach ($attributes as $attr => $val) { |
|
127
|
|
|
$arrSearch[] = '%' . $attr . '%'; |
|
128
|
|
|
|
|
129
|
|
|
if (strlen($val[0]) > 0) { |
|
130
|
|
|
$arrReplace[] = Ldap::escapeFilterValue($val[0]); |
|
131
|
|
|
} else { |
|
132
|
|
|
$arrReplace[] = ''; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// merge the attributes into the ldap_search_filter |
|
137
|
|
|
$filter = str_replace($arrSearch, $arrReplace, $this->search_filter); |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
if (strpos($filter, '%') !== false) { |
|
140
|
|
|
Logger::info( |
|
141
|
|
|
'AttributeAddFromLDAP: There are non-existing attributes in the search filter. (' . |
|
142
|
|
|
$this->search_filter . ')' |
|
143
|
|
|
); |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (!in_array($this->attr_policy, ['merge', 'replace', 'add'], true)) { |
|
148
|
|
|
Logger::warning("AttributeAddFromLDAP: 'attribute.policy' must be one of 'merge'," . |
|
149
|
|
|
"'replace' or 'add'."); |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// getLdap |
|
154
|
|
|
try { |
|
155
|
|
|
$ldap = $this->getLdap(); |
|
156
|
|
|
} catch (\Exception $e) { |
|
157
|
|
|
// Added this warning in case $this->getLdap() fails |
|
158
|
|
|
Logger::warning("AttributeAddFromLDAP: exception = " . $e); |
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
// search for matching entries |
|
162
|
|
|
try { |
|
163
|
|
|
$entries = $ldap->searchformultiple( |
|
164
|
|
|
$this->base_dn, |
|
165
|
|
|
$filter, |
|
166
|
|
|
array_values($this->search_attributes), |
|
167
|
|
|
true, |
|
168
|
|
|
false |
|
169
|
|
|
); |
|
170
|
|
|
} catch (\Exception $e) { |
|
171
|
|
|
return; // silent fail, error is still logged by LDAP search |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// handle [multiple] values |
|
175
|
|
|
foreach ($entries as $entry) { |
|
176
|
|
|
foreach ($this->search_attributes as $target => $name) { |
|
177
|
|
|
if (is_numeric($target)) { |
|
178
|
|
|
$target = $name; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (isset($attributes[$target]) && $this->attr_policy === 'replace') { |
|
182
|
|
|
unset($attributes[$target]); |
|
183
|
|
|
} |
|
184
|
|
|
$name = strtolower($name); |
|
185
|
|
|
if (isset($entry[$name])) { |
|
186
|
|
|
unset($entry[$name]['count']); |
|
187
|
|
|
if (isset($attributes[$target])) { |
|
188
|
|
|
foreach (array_values($entry[$name]) as $value) { |
|
189
|
|
|
if ($this->attr_policy === 'merge') { |
|
190
|
|
|
if (!in_array($value, $attributes[$target], true)) { |
|
191
|
|
|
$attributes[$target][] = $value; |
|
192
|
|
|
} |
|
193
|
|
|
} else { |
|
194
|
|
|
$attributes[$target][] = $value; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} else { |
|
198
|
|
|
$attributes[$target] = array_values($entry[$name]); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|