sanitizing ldap search. I think, in this case it only prevents ldap search
[squirrelmail.git] / functions / abook_ldap_server.php
1 <?php
2 /**
3 * abook_ldap_server.php
4 *
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Address book backend for LDAP server
9 *
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
13 */
14
15 /**
16 * Address book backend for LDAP server
17 *
18 * An array with the following elements must be passed to
19 * the class constructor (elements marked ? are optional):
20 * <pre>
21 * host => LDAP server hostname/IP-address
22 * base => LDAP server root (base dn). Empty string allowed.
23 * ? port => LDAP server TCP port number (default: 389)
24 * ? charset => LDAP server charset (default: utf-8)
25 * ? name => Name for LDAP server (default "LDAP: hostname")
26 * Used to tag the result data
27 * ? maxrows => Maximum # of rows in search result
28 * ? timeout => Timeout for LDAP operations (in seconds, default: 30)
29 * Might not work for all LDAP libraries or servers.
30 * ? binddn => LDAP Bind DN.
31 * ? bindpw => LDAP Bind Password.
32 * ? protocol => LDAP Bind protocol.
33 * </pre>
34 * NOTE. This class should not be used directly. Use the
35 * "AddressBook" class instead.
36 * @package squirrelmail
37 * @subpackage addressbook
38 */
39 class abook_ldap_server extends addressbook_backend {
40 /**
41 * @var string backend type
42 */
43 var $btype = 'remote';
44 /**
45 * @var string backend name
46 */
47 var $bname = 'ldap_server';
48
49 /* Parameters changed by class */
50 /**
51 * @var string displayed name
52 */
53 var $sname = 'LDAP'; /* Service name */
54 /**
55 * @var string LDAP server name or address or url
56 */
57 var $server = '';
58 /**
59 * @var integer LDAP server port
60 */
61 var $port = 389;
62 /**
63 * @var string LDAP base DN
64 */
65 var $basedn = '';
66 /**
67 * @var string charset used for entries in LDAP server
68 */
69 var $charset = 'utf-8';
70 /**
71 * @var object PHP LDAP link ID
72 */
73 var $linkid = false;
74 /**
75 * @var bool True if LDAP server is bound
76 */
77 var $bound = false;
78 /**
79 * @var integer max rows in result
80 */
81 var $maxrows = 250;
82 /**
83 * @var integer timeout of LDAP operations (in seconds)
84 */
85 var $timeout = 30;
86 /**
87 * @var string DN to bind to (non-anonymous bind)
88 * @since 1.5.0 and 1.4.3
89 */
90 var $binddn = '';
91 /**
92 * @var string password to bind with (non-anonymous bind)
93 * @since 1.5.0 and 1.4.3
94 */
95 var $bindpw = '';
96 /**
97 * @var integer protocol used to connect to ldap server
98 * @since 1.5.0 and 1.4.3
99 */
100 var $protocol = '';
101
102 /**
103 * Constructor. Connects to database
104 * @param array connection options
105 */
106 function abook_ldap_server($param) {
107 if(!function_exists('ldap_connect')) {
108 $this->set_error('LDAP support missing from PHP');
109 return;
110 }
111 if(is_array($param)) {
112 $this->server = $param['host'];
113 $this->basedn = $param['base'];
114 if(!empty($param['port'])) {
115 $this->port = $param['port'];
116 }
117 if(!empty($param['charset'])) {
118 $this->charset = strtolower($param['charset']);
119 }
120 if(isset($param['maxrows'])) {
121 $this->maxrows = $param['maxrows'];
122 }
123 if(isset($param['timeout'])) {
124 $this->timeout = $param['timeout'];
125 }
126 if(isset($param['binddn'])) {
127 $this->binddn = $param['binddn'];
128 }
129 if(isset($param['bindpw'])) {
130 $this->bindpw = $param['bindpw'];
131 }
132 if(isset($param['protocol'])) {
133 $this->protocol = $param['protocol'];
134 }
135 if(empty($param['name'])) {
136 $this->sname = 'LDAP: ' . $param['host'];
137 }
138 else {
139 $this->sname = $param['name'];
140 }
141
142 $this->open(true);
143 } else {
144 $this->set_error('Invalid argument to constructor');
145 }
146 }
147
148
149 /**
150 * Open the LDAP server.
151 * @param bool $new is it a new connection
152 * @return bool
153 */
154 function open($new = false) {
155 $this->error = '';
156
157 /* Connection is already open */
158 if($this->linkid != false && !$new) {
159 return true;
160 }
161
162 $this->linkid = @ldap_connect($this->server, $this->port);
163 if(!$this->linkid) {
164 if(function_exists('ldap_error')) {
165 return $this->set_error(ldap_error($this->linkid));
166 } else {
167 return $this->set_error('ldap_connect failed');
168 }
169 }
170
171 if(!empty($this->protocol)) {
172 if(!@ldap_set_option($this->linkid, LDAP_OPT_PROTOCOL_VERSION, $this->protocol)) {
173 if(function_exists('ldap_error')) {
174 return $this->set_error(ldap_error($this->linkid));
175 } else {
176 return $this->set_error('ldap_set_option failed');
177 }
178 }
179 }
180
181 if(!empty($this->binddn)) {
182 if(!@ldap_bind($this->linkid, $this->binddn, $this->bindpw)) {
183 if(function_exists('ldap_error')) {
184 return $this->set_error(ldap_error($this->linkid));
185 } else {
186 return $this->set_error('authenticated ldap_bind failed');
187 }
188 }
189 } else {
190 if(!@ldap_bind($this->linkid)) {
191 if(function_exists('ldap_error')) {
192 return $this->set_error(ldap_error($this->linkid));
193 } else {
194 return $this->set_error('anonymous ldap_bind failed');
195 }
196 }
197 }
198
199 $this->bound = true;
200
201 return true;
202 }
203
204 /**
205 * Encode string to the charset used by this LDAP server
206 * @param string string that has to be encoded
207 * @return string encoded string
208 */
209 function charset_encode($str) {
210 global $default_charset;
211 if($this->charset != $default_charset) {
212 return charset_convert($default_charset,$str,$this->charset,false);
213 } else {
214 return $str;
215 }
216 }
217
218 /**
219 * Decode from charset used by this LDAP server to charset used by translation
220 *
221 * Uses SquirrelMail charset_decode functions
222 * @param string string that has to be decoded
223 * @return string decoded string
224 */
225 function charset_decode($str) {
226 global $default_charset;
227 if ($this->charset != $default_charset) {
228 return charset_convert($this->charset,$str,$default_charset,false);
229 } else {
230 return $str;
231 }
232 }
233
234 /**
235 * Sanitizes ldap search strings.
236 * See rfc2254
237 * @link http://www.faqs.org/rfcs/rfc2254.html
238 * @since 1.5.1
239 * @param string $string
240 * @return string sanitized string
241 */
242 function ldapspecialchars($string) {
243 $sanitized=array('\\' => '\5c',
244 '*' => '\2a',
245 '(' => '\28',
246 ')' => '\29',
247 "\x00" => '\00');
248
249 return str_replace(array_keys($sanitized),array_values($sanitized),$string);
250 }
251
252 /* ========================== Public ======================== */
253
254 /**
255 * Search the LDAP server
256 * @param string $expr search expression
257 * @return array search results
258 */
259 function search($expr) {
260 /* To be replaced by advanded search expression parsing */
261 if(is_array($expr)) return false;
262
263 /* Encode the expression */
264 $expr = $this->charset_encode($expr);
265
266 /*
267 * allow use of one asterisk in search.
268 * Don't allow any ldap special chars if search is different
269 */
270 if($expr!='*') {
271 $expr = '*' . $this->ldapspecialchars($expr) . '*';
272 }
273 $expression = "cn=$expr";
274
275 /* Make sure connection is there */
276 if(!$this->open()) {
277 return false;
278 }
279
280 $sret = @ldap_search($this->linkid, $this->basedn, $expression,
281 array('dn', 'o', 'ou', 'sn', 'givenname', 'cn', 'mail'),
282 0, $this->maxrows, $this->timeout);
283
284 /* Should get error from server using the ldap_error() function,
285 * but it only exist in the PHP LDAP documentation. */
286 if(!$sret) {
287 if(function_exists('ldap_error')) {
288 return $this->set_error(ldap_error($this->linkid));
289 } else {
290 return $this->set_error('ldap_search failed');
291 }
292 }
293
294 if(@ldap_count_entries($this->linkid, $sret) <= 0) {
295 return array();
296 }
297
298 /* Get results */
299 $ret = array();
300 $returned_rows = 0;
301 $res = @ldap_get_entries($this->linkid, $sret);
302 for($i = 0 ; $i < $res['count'] ; $i++) {
303 $row = $res[$i];
304
305 /* Extract data common for all e-mail addresses
306 * of an object. Use only the first name */
307 $nickname = $this->charset_decode($row['dn']);
308 $fullname = $this->charset_decode($row['cn'][0]);
309
310 if(!empty($row['ou'][0])) {
311 $label = $this->charset_decode($row['ou'][0]);
312 }
313 else if(!empty($row['o'][0])) {
314 $label = $this->charset_decode($row['o'][0]);
315 } else {
316 $label = '';
317 }
318
319 if(empty($row['givenname'][0])) {
320 $firstname = '';
321 } else {
322 $firstname = $this->charset_decode($row['givenname'][0]);
323 }
324
325 if(empty($row['sn'][0])) {
326 $surname = '';
327 } else {
328 $surname = $this->charset_decode($row['sn'][0]);
329 }
330
331 /* Add one row to result for each e-mail address */
332 if(isset($row['mail']['count'])) {
333 for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
334 array_push($ret, array('nickname' => $nickname,
335 'name' => $fullname,
336 'firstname' => $firstname,
337 'lastname' => $surname,
338 'email' => $row['mail'][$j],
339 'label' => $label,
340 'backend' => $this->bnum,
341 'source' => &$this->sname));
342
343 // Limit number of hits
344 $returned_rows++;
345 if(($returned_rows >= $this->maxrows) &&
346 ($this->maxrows > 0) ) {
347 ldap_free_result($sret);
348 return $ret;
349 }
350
351 } // for($j ...)
352
353 } // isset($row['mail']['count'])
354
355 }
356
357 ldap_free_result($sret);
358 return $ret;
359 } /* end search() */
360
361
362 /**
363 * List all entries present in LDAP server
364 *
365 * If you run a tiny LDAP server and you want the "List All" button
366 * to show EVERYONE, disable first return call and enable the second one.
367 * Remember that maxrows setting might limit list of returned entries.
368 *
369 * Careful with this -- it could get quite large for big sites.
370 * @return array all entries in ldap server
371 */
372 function list_addr() {
373 return array();
374 // return $this->search('*');
375 }
376 }
377 ?>