1 <?php
2 /*****************************************************************************
3 newldap.inc - version 1.0
4 Copyright (C) 2003 Alejandro Escanero Blanco <aescanero@chaosdimension.org>
5 Copyright (C) 2004-2006 Cajus Pollmeier <pollmeier@gonicus.de>
7 Based in code of ldap.inc of
8 Copyright (C) 1998 Eric Kilfoil <eric@ipass.net>
9 *****************************************************************************/
11 define("ALREADY_EXISTING_ENTRY",-10001);
12 define("UNKNOWN_TOKEN_IN_LDIF_FILE",-10002);
13 define("NO_FILE_UPLOADED",10003);
14 define("INSERT_OK",10000);
15 define("SPECIALS_OVERRIDE", TRUE);
17 class LDAP{
19 var $hascon =false;
20 var $hasres =false;
21 var $reconnect=false;
22 var $tls = false;
23 var $basedn ="";
24 var $cid;
25 var $error = ""; // Any error messages to be returned can be put here
26 var $start = 0; // 0 if we are fetching the first entry, otherwise 1
27 var $objectClasses = array(); // Information read from slapd.oc.conf
28 var $binddn = "";
29 var $bindpw = "";
30 var $hostname = "";
31 var $follow_referral = FALSE;
32 var $referrals= array();
33 var $max_ldap_query_time = 0; // 0, empty or negative values will disable this check
35 function LDAP($binddn,$bindpw, $hostname, $follow_referral= FALSE, $tls= FALSE)
36 {
37 global $config;
38 $this->follow_referral= $follow_referral;
39 $this->tls=$tls;
40 $this->binddn=LDAP::convert($binddn);
42 $this->bindpw=$bindpw;
43 $this->hostname=$hostname;
45 /* Check if MAX_LDAP_QUERY_TIME is defined */
46 if(isset($config->data['MAIN']['MAX_LDAP_QUERY_TIME'])){
47 $str = $config->data['MAIN']['MAX_LDAP_QUERY_TIME'];
48 $this->max_ldap_query_time = (float)($str);
49 }
51 $this->connect();
52 }
55 /* Function to replace all problematic characters inside a DN by \001XX, where
56 \001 is decoded to chr(1) [ctrl+a]. It is not impossible, but very unlikely
57 that this character is inside a DN.
59 Currently used codes:
60 , => CO
61 \2C => CO
62 ( => OB
63 ) => CB
64 / => SL */
65 static function convert($dn)
66 {
67 if (SPECIALS_OVERRIDE == TRUE){
68 $tmp= preg_replace(array("/\\\\,/", "/\\\\2C/", "/\(/", "/\)/", "/\//"),
69 array("\001CO", "\001CO", "\001OB", "\001CB", "\001SL"),
70 $dn);
71 return (preg_replace('/,\s+/', ',', $tmp));
72 } else {
73 return ($dn);
74 }
75 }
78 /* Function to fix all problematic characters inside a DN by replacing \001XX
79 codes to their original values. See "convert" for mor information.
80 ',' characters are always expanded to \, (not \2C), since all tested LDAP
81 servers seem to take it the correct way. */
82 static function fix($dn)
83 {
84 if (SPECIALS_OVERRIDE == TRUE){
85 return (preg_replace(array("/\001CO/", "/\001OB/", "/\001CB/", "/\001SL/"),
86 array("\,", "(", ")", "/"),
87 $dn));
88 } else {
89 return ($dn);
90 }
91 }
94 function connect()
95 {
96 $this->hascon=false;
97 $this->reconnect=false;
98 if ($this->cid= @ldap_connect($this->hostname)) {
99 @ldap_set_option($this->cid, LDAP_OPT_PROTOCOL_VERSION, 3);
100 if (function_exists("ldap_set_rebind_proc") && $this->follow_referral) {
101 @ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
102 @ldap_set_rebind_proc($this->cid, array(&$this, "rebind"));
103 }
104 if (function_exists("ldap_start_tls") && $this->tls){
105 @ldap_start_tls($this->cid);
106 }
108 $this->error = "No Error";
109 if ($bid = @ldap_bind($this->cid, LDAP::fix($this->binddn), $this->bindpw)) {
110 $this->error = "Success";
111 $this->hascon=true;
112 } else {
113 if ($this->reconnect){
114 if ($this->error != "Success"){
115 $this->error = "Could not rebind to " . $this->binddn;
116 }
117 } else {
118 $this->error = "Could not bind to " . $this->binddn;
119 }
120 }
121 } else {
122 $this->error = "Could not connect to LDAP server";
123 }
124 }
126 function rebind($ldap, $referral)
127 {
128 $credentials= $this->get_credentials($referral);
129 if (@ldap_bind($ldap, LDAP::fix($credentials['ADMIN']), $credentials['PASSWORD'])) {
130 $this->error = "Success";
131 $this->hascon=true;
132 $this->reconnect= true;
133 return (0);
134 } else {
135 $this->error = "Could not bind to " . $credentials['ADMIN'];
136 return NULL;
137 }
138 }
140 function reconnect()
141 {
142 if ($this->reconnect){
143 @ldap_unbind($this->cid);
144 $this->cid = NULL;
145 }
146 }
148 function unbind()
149 {
150 @ldap_unbind($this->cid);
151 $this->cid = NULL;
152 }
154 function disconnect()
155 {
156 if($this->hascon){
157 @ldap_close($this->cid);
158 $this->hascon=false;
159 }
160 }
162 function cd($dir)
163 {
164 if ($dir == "..")
165 $this->basedn = $this->getParentDir();
166 else
167 $this->basedn = LDAP::convert($dir);
168 }
170 function getParentDir($basedn = "")
171 {
172 if ($basedn=="")
173 $basedn = $this->basedn;
174 else
175 $basedn = LDAP::convert($this->basedn);
176 return(ereg_replace("[^,]*[,]*[ ]*(.*)", "\\1", $basedn));
177 }
179 function search($filter, $attrs= array())
180 {
181 if($this->hascon){
182 if ($this->reconnect) $this->connect();
184 $start = microtime();
186 $this->clearResult();
187 $this->sr = @ldap_search($this->cid, LDAP::fix($this->basedn), $filter, $attrs);
188 $this->error = @ldap_error($this->cid);
189 $this->resetResult();
190 $this->hasres=true;
192 /* Check if query took longer as specified in max_ldap_query_time */
193 if($this->max_ldap_query_time){
194 $diff = get_MicroTimeDiff($start,microtime());
195 if($diff > $this->max_ldap_query_time){
196 print_red(sprintf(_("The LDAP server is slow (%.2fs for the last query). This may be responsible for performance breakdowns."),$diff)) ;
197 }
198 }
200 $this->log("LDAP operation: time=".get_MicroTimeDiff($start,microtime())." operation=search('".LDAP::fix($this->basedn)."', '$filter')");
201 return($this->sr);
202 }else{
203 $this->error = "Could not connect to LDAP server";
204 return("");
205 }
206 }
208 function ls($filter = "(objectclass=*)", $basedn = "",$attrs = array("*"))
209 {
210 if($this->hascon){
211 if ($this->reconnect) $this->connect();
212 $this->clearResult();
213 if ($basedn == "")
214 $basedn = $this->basedn;
215 else
216 $basedn= LDAP::convert($basedn);
218 $start = microtime();
219 $this->sr = @ldap_list($this->cid, LDAP::fix($basedn), $filter,$attrs);
220 $this->error = @ldap_error($this->cid);
221 $this->resetResult();
222 $this->hasres=true;
224 /* Check if query took longer as specified in max_ldap_query_time */
225 if($this->max_ldap_query_time){
226 $diff = get_MicroTimeDiff($start,microtime());
227 if($diff > $this->max_ldap_query_time){
228 print_red(sprintf(_("The ldapserver is answering very slow (%.2f), this may be responsible for performance breakdowns."),$diff)) ;
229 }
230 }
232 $this->log("LDAP operation: time=".get_MicroTimeDiff($start,microtime())." operation=ls('".LDAP::fix($basedn)."', '$filter')");
234 return($this->sr);
235 }else{
236 $this->error = "Could not connect to LDAP server";
237 return("");
238 }
239 }
241 function cat($dn,$attrs= array("*"))
242 {
243 if($this->hascon){
244 if ($this->reconnect) $this->connect();
245 $this->clearResult();
246 $filter = "(objectclass=*)";
247 $this->sr = @ldap_read($this->cid, LDAP::fix($dn), $filter,$attrs);
248 $this->error = @ldap_error($this->cid);
249 $this->resetResult();
250 $this->hasres=true;
251 return($this->sr);
252 }else{
253 $this->error = "Could not connect to LDAP server";
254 return("");
255 }
256 }
258 function set_size_limit($size)
259 {
260 /* Ignore zero settings */
261 if ($size == 0){
262 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, 10000000);
263 }
264 if($this->hascon){
265 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, $size);
266 } else {
267 $this->error = "Could not connect to LDAP server";
268 }
269 }
271 function fetch()
272 {
273 $att= array();
274 if($this->hascon){
275 if($this->hasres){
276 if ($this->start == 0)
277 {
278 if ($this->sr){
279 $this->start = 1;
280 $this->re= @ldap_first_entry($this->cid, $this->sr);
281 } else {
282 return array();
283 }
284 } else {
285 $this->re= @ldap_next_entry($this->cid, $this->re);
286 }
287 if ($this->re)
288 {
289 $att= @ldap_get_attributes($this->cid, $this->re);
290 $att['dn']= trim(LDAP::convert(@ldap_get_dn($this->cid, $this->re)));
291 }
292 $this->error = @ldap_error($this->cid);
293 if (!isset($att)){
294 $att= array();
295 }
296 return($att);
297 }else{
298 $this->error = "Perform a Fetch with no Search";
299 return("");
300 }
301 }else{
302 $this->error = "Could not connect to LDAP server";
303 return("");
304 }
305 }
307 function resetResult()
308 {
309 $this->start = 0;
310 }
312 function clearResult()
313 {
314 if($this->hasres){
315 $this->hasres = false;
316 @ldap_free_result($this->sr);
317 }
318 }
320 function getDN()
321 {
322 if($this->hascon){
323 if($this->hasres){
325 if(!$this->re)
326 {
327 $this->error = "Perform a Fetch with no valid Result";
328 }
329 else
330 {
331 $rv = @ldap_get_dn($this->cid, $this->re);
333 $this->error = @ldap_error($this->cid);
334 return(trim(LDAP::convert($rv)));
335 }
336 }else{
337 $this->error = "Perform a Fetch with no Search";
338 return("");
339 }
340 }else{
341 $this->error = "Could not connect to LDAP server";
342 return("");
343 }
344 }
346 function count()
347 {
348 if($this->hascon){
349 if($this->hasres){
350 $rv = @ldap_count_entries($this->cid, $this->sr);
351 $this->error = @ldap_error($this->cid);
352 return($rv);
353 }else{
354 $this->error = "Perform a Fetch with no Search";
355 return("");
356 }
357 }else{
358 $this->error = "Could not connect to LDAP server";
359 return("");
360 }
361 }
363 function rm($attrs = "", $dn = "")
364 {
365 if($this->hascon){
366 if ($this->reconnect) $this->connect();
367 if ($dn == "")
368 $dn = $this->basedn;
370 $r = @ldap_mod_del($this->cid, LDAP::fix($dn), $attrs);
371 $this->error = @ldap_error($this->cid);
372 return($r);
373 }else{
374 $this->error = "Could not connect to LDAP server";
375 return("");
376 }
377 }
379 function rename($attrs, $dn = "")
380 {
381 if($this->hascon){
382 if ($this->reconnect) $this->connect();
383 if ($dn == "")
384 $dn = $this->basedn;
386 $r = @ldap_mod_replace($this->cid, LDAP::fix($dn), $attrs);
387 $this->error = @ldap_error($this->cid);
388 return($r);
389 }else{
390 $this->error = "Could not connect to LDAP server";
391 return("");
392 }
393 }
395 function rmdir($deletedn)
396 {
397 if($this->hascon){
398 if ($this->reconnect) $this->connect();
399 $r = @ldap_delete($this->cid, LDAP::fix($deletedn));
400 $this->error = @ldap_error($this->cid);
401 return($r ? $r : 0);
402 }else{
403 $this->error = "Could not connect to LDAP server";
404 return("");
405 }
406 }
408 /**
409 * Function rmdir_recursive
410 *
411 * Description: Based in recursive_remove, adding two thing: full subtree remove, and delete own node.
412 * Parameters: The dn to delete
413 * GiveBack: True on sucessfull , 0 in error, and "" when we don't get a ldap conection
414 *
415 */
417 function rmdir_recursive($deletedn)
418 {
419 if($this->hascon){
420 if ($this->reconnect) $this->connect();
421 $delarray= array();
423 /* Get sorted list of dn's to delete */
424 $this->ls ("(objectClass=*)",$deletedn);
425 while ($this->fetch()){
426 $deldn= $this->getDN();
427 $delarray[$deldn]= strlen($deldn);
428 }
429 arsort ($delarray);
430 reset ($delarray);
432 /* Really Delete ALL dn's in subtree */
433 foreach ($delarray as $key => $value){
434 $this->rmdir_recursive($key);
435 }
437 /* Finally Delete own Node */
438 $r = @ldap_delete($this->cid, LDAP::fix($deletedn));
439 $this->error = @ldap_error($this->cid);
440 return($r ? $r : 0);
441 }else{
442 $this->error = "Could not connect to LDAP server";
443 return("");
444 }
445 }
447 /* Copy given attributes and sub-dns with attributes to destination dn
448 */
449 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$destinationName,$type="branch",$is_first = true,$depth=0)
450 {
451 error_reporting(E_ALL | E_STRICT);
453 if($is_first){
454 echo "<h2>".sprintf(_("Creating copy of %s"),"<i>".LDAP::fix($sourcedn)."</i>")."</h2>";
455 }else{
456 if(preg_match("/^ou=/",$sourcedn)){
457 echo "<h3>"._("Processing")." <i>".LDAP::fix($destinationdn)."</i></h3>";
458 }else{
459 $tmp = split(",",$sourcedn);
461 echo " <b>"._("Object").":</b> ";
463 $deststr = LDAP::fix($destinationdn);
464 if(strlen($deststr) > 96){
465 $deststr = substr($deststr,0,96)."...";
466 }
468 echo $deststr."<br>";
469 }
470 }
472 flush();
474 if($this->hascon){
475 if ($this->reconnect) $this->connect();
477 /* Save base dn */
478 $basedn= $this->basedn;
479 $delarray= array();
481 /* Check if destination entry already exists */
482 $this->cat($destinationdn);
484 if($this->count()){
485 return;
486 }else{
488 $this->clearResult();
490 /* Get source entry */
491 $this->cd($basedn);
492 $this->cat($sourcedn);
493 $attr = $this->fetch();
495 /* Error while fetching object / attribute abort*/
496 if((!$attr) || (count($attr)) ==0) {
497 echo _("Error while fetching source dn - aborted!");
498 return;
499 }
501 /* check if this is a department */
502 if(in_array("organizationalUnit",$attr['objectClass'])){
503 $attr['dn'] = LDAP::convert($destinationdn);
504 $this->cd($basedn);
505 $this->create_missing_trees($destinationdn);
506 $this->cd($destinationdn);
508 /* If is first entry, append FAIbranch to department entry */
509 if($is_first){
510 $this->cat($destinationdn);
511 $attr= $this->fetch();
513 /* Filter unneeded informations */
514 foreach($attr as $key => $value){
515 if(is_numeric($key)) unset($attr[$key]);
516 if(isset($attr[$key]['count'])){
517 if(is_array($attr[$key])){
518 unset($attr[$key]['count']);
519 }
520 }
521 }
523 unset($attr['count']);
524 unset($attr['dn']);
526 /* Add marking attribute */
527 $attr['objectClass'][] = "FAIbranch";
529 /* Add this entry */
530 $this->modify($attr);
531 }
532 }else{
534 /* If this is no department */
535 foreach($attr as $key => $value){
536 if(in_array($key ,array("FAItemplateFile","FAIscript", "gotoLogonScript", "gosaApplicationIcon","gotoMimeIcon"))){
537 $sr= ldap_read($this->cid, LDAP::fix($sourcedn), "$key=*", array($key));
538 $ei= ldap_first_entry($this->cid, $sr);
539 if ($tmp= @ldap_get_values_len($this->cid, $ei,$key)){
540 $attr[$key] = $tmp;
541 }
542 }
544 if(is_numeric($key)) unset($attr[$key]);
545 if(isset($attr[$key]['count'])){
546 if(is_array($attr[$key])){
547 unset($attr[$key]['count']);
548 }
549 }
550 }
551 unset($attr['count']);
552 unset($attr['dn']);
554 if((!in_array("gosaApplication" , $attr['objectClass'])) && (!in_array("gotoMimeType", $attr['objectClass']))){
555 $attr['FAIdebianRelease'] = $destinationName;
556 if($type=="branch"){
557 $attr['FAIstate'] ="branch";
558 }elseif($type=="freeze"){
559 $attr['FAIstate'] ="freeze";
560 }else{
561 print_red(_("Unknown FAIstate %s"),$type);
562 }
563 }
565 /* Replace FAIdebianRelease with new release name */
566 if(in_array("FAIpackageList" , $attr['objectClass'])){
567 $attr['FAIdebianRelease'] = $destinationName;
568 }
570 /* Add entry */
571 $this->cd($destinationdn);
572 $this->cat($destinationdn);
573 $a = $this->fetch();
574 if(!count($a)){
575 $this->add($attr);
576 }
578 if($this->error != "Success"){
579 /* Some error occurred */
580 print "---------------------------------------------";
581 print $this->get_error()."<br>";
582 print $sourcedn."<br>";
583 print $destinationdn."<br>";
584 print_a( $attr);
585 exit();
586 }
587 }
588 }
590 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
592 $this->ls ("(objectClass=*)",$sourcedn);
593 while ($this->fetch()){
594 $deldn= $this->getDN();
595 $delarray[$deldn]= strlen($deldn);
596 }
597 asort ($delarray);
598 reset ($delarray);
600 $depth ++;
601 foreach($delarray as $dn => $bla){
602 if($dn != $destinationdn){
603 $this->cd($basedn);
604 $item = $this->fetch($this->cat($dn));
605 if(!in_array("FAIbranch",$item['objectClass'])){
606 $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$destinationName,$type,false,$depth);
607 }
608 }
609 }
610 }
611 if($is_first){
612 echo "<p class='seperator'> </p>";
613 }
615 }
617 function modify($attrs)
618 {
619 if(count($attrs) == 0){
620 return (0);
621 }
622 if($this->hascon){
623 if ($this->reconnect) $this->connect();
624 $r = @ldap_modify($this->cid, LDAP::fix($this->basedn), $attrs);
625 $this->error = @ldap_error($this->cid);
626 return($r ? $r : 0);
627 }else{
628 $this->error = "Could not connect to LDAP server";
629 return("");
630 }
631 }
633 function add($attrs)
634 {
635 if($this->hascon){
636 if ($this->reconnect) $this->connect();
637 $r = @ldap_add($this->cid, LDAP::fix($this->basedn), $attrs);
638 $this->error = @ldap_error($this->cid);
639 return($r ? $r : 0);
640 }else{
641 $this->error = "Could not connect to LDAP server";
642 return("");
643 }
644 }
646 function create_missing_trees($target)
647 {
648 global $config;
650 $real_path= substr($target, 0, strlen($target) - strlen($this->basedn) -1 );
652 if ($target == $this->basedn){
653 $l= array("dummy");
654 } else {
655 $l= array_reverse(gosa_ldap_explode_dn($real_path));
656 }
657 unset($l['count']);
658 $cdn= $this->basedn;
659 $tag= "";
661 /* Load schema if available... */
662 $classes= $this->get_objectclasses();
664 foreach ($l as $part){
665 if ($part != "dummy"){
666 $cdn= "$part,$cdn";
667 }
669 /* Ignore referrals */
670 $found= false;
671 foreach($this->referrals as $ref){
672 $base= preg_replace('!^[^:]+://[^/]+/([^?]+).*$!', '\\1', $ref['URL']);
673 if ($base == $cdn){
674 $found= true;
675 break;
676 }
677 }
678 if ($found){
679 continue;
680 }
682 $this->cat ($cdn);
683 $attrs= $this->fetch();
685 /* Create missing entry? */
686 if (count ($attrs)){
688 /* Catch the tag - if present */
689 if (isset($attrs['gosaUnitTag'][0])){
690 $tag= $attrs['gosaUnitTag'][0];
691 }
693 } else {
694 $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
695 $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
697 $na= array();
699 /* Automatic or traditional? */
700 if(count($classes)){
702 /* Get name of first matching objectClass */
703 $ocname= "";
704 foreach($classes as $class){
705 if (isset($class['MUST']) && $class['MUST'] == "$type"){
707 /* Look for first classes that is structural... */
708 if (isset($class['STRUCTURAL'])){
709 $ocname= $class['NAME'];
710 break;
711 }
713 /* Look for classes that are auxiliary... */
714 if (isset($class['AUXILIARY'])){
715 $ocname= $class['NAME'];
716 }
717 }
718 }
720 /* Bail out, if we've nothing to do... */
721 if ($ocname == ""){
722 print_red(sprintf(_("Autocreation of subtree failed. No objectClass found for attribute '%s'."), $type));
723 echo $_SESSION['errors'];
724 exit;
725 }
727 /* Assemble_entry */
728 if ($tag != ""){
729 $na['objectClass']= array($ocname, "gosaAdministrativeUnitTag");
730 } else {
731 $na['objectClass']= array($ocname);
732 }
733 if (isset($classes[$ocname]['AUXILIARY'])){
734 $na['objectClass'][]= $classes[$ocname]['SUP'];
735 }
736 if ($type == "dc"){
737 /* This is bad actually, but - tell me a better way? */
738 $na['objectClass'][]= 'locality';
739 }
740 $na[$type]= $param;
741 if (is_array($classes[$ocname]['MUST'])){
742 foreach($classes[$ocname]['MUST'] as $attr){
743 $na[$attr]= "filled";
744 }
745 }
747 } else {
749 /* Use alternative add... */
750 switch ($type){
751 case 'ou':
752 if ($tag != ""){
753 $na["objectClass"]= array("organizationalUnit", "gosaAdministrativeUnitTag");
754 $na["gosaUnitTag"]= $tag;
755 } else {
756 $na["objectClass"]= "organizationalUnit";
757 }
758 $na["ou"]= $param;
759 break;
760 case 'dc':
761 if ($tag != ""){
762 $na["objectClass"]= array("dcObject", "top", "locality", "gosaAdministrativeUnitTag");
763 $na["gosaUnitTag"]= $tag;
764 } else {
765 $na["objectClass"]= array("dcObject", "top", "locality");
766 }
767 $na["dc"]= $param;
768 break;
769 default:
770 print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
771 echo $_SESSION['errors'];
772 exit;
773 }
775 }
776 $this->cd($cdn);
777 $this->add($na);
779 show_ldap_error($this->get_error(), sprintf(_("Creating subtree '%s' failed."),$cdn));
780 if (!preg_match('/success/i', $this->error)){
781 return FALSE;
782 }
783 }
784 }
786 return TRUE;
787 }
790 function recursive_remove()
791 {
792 $delarray= array();
794 /* Get sorted list of dn's to delete */
795 $this->search ("(objectClass=*)");
796 while ($this->fetch()){
797 $deldn= $this->getDN();
798 $delarray[$deldn]= strlen($deldn);
799 }
800 arsort ($delarray);
801 reset ($delarray);
803 /* Delete all dn's in subtree */
804 foreach ($delarray as $key => $value){
805 $this->rmdir($key);
806 }
807 }
809 function get_attribute($dn, $name,$r_array=0)
810 {
811 $data= "";
812 if ($this->reconnect) $this->connect();
813 $sr= @ldap_read($this->cid, LDAP::fix($dn), "objectClass=*", array("$name"));
815 /* fill data from LDAP */
816 if ($sr) {
817 $ei= @ldap_first_entry($this->cid, $sr);
818 if ($ei) {
819 if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
820 $data= $info[0];
821 }
822 }
823 }
824 if($r_array==0)
825 return ($data);
826 else
827 return ($info);
830 }
834 function get_additional_error()
835 {
836 $error= "";
837 @ldap_get_option ($this->cid, LDAP_OPT_ERROR_STRING, $error);
838 return ($error);
839 }
841 function get_error()
842 {
843 if ($this->error == 'Success'){
844 return $this->error;
845 } else {
846 $adderror= $this->get_additional_error();
847 if ($adderror != ""){
848 $error= $this->error." (".$this->get_additional_error().", ".sprintf(_("while operating on '%s' using LDAP server '%s'"), $this->basedn, $this->hostname).")";
849 } else {
850 $error= $this->error." (".sprintf(_("while operating on LDAP server %s"), $this->hostname).")";
851 }
852 return $error;
853 }
854 }
856 function get_credentials($url, $referrals= NULL)
857 {
858 $ret= array();
859 $url= preg_replace('!\?\?.*$!', '', $url);
860 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
862 if ($referrals === NULL){
863 $referrals= $this->referrals;
864 }
866 if (isset($referrals[$server])){
867 return ($referrals[$server]);
868 } else {
869 $ret['ADMIN']= LDAP::fix($this->binddn);
870 $ret['PASSWORD']= $this->bindpw;
871 }
873 return ($ret);
874 }
877 function gen_ldif ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE)
878 {
879 $display= "";
881 if ($recursive){
882 $this->cd($dn);
883 $this->ls($filter,$dn, array('dn','objectClass'));
884 $deps = array();
886 $display .= $this->gen_one_entry($dn)."\n";
888 while ($attrs= $this->fetch()){
889 $deps[] = $attrs['dn'];
890 }
891 foreach($deps as $dn){
892 $display .= $this->gen_ldif($dn, $filter,$attributes,$recursive);
893 }
894 } else {
895 $display.= $this->gen_one_entry($dn);
896 }
897 return ($display);
898 }
901 function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
902 {
903 $display= array();
905 $this->cd($dn);
906 $this->search("$filter");
908 $i=0;
909 while ($attrs= $this->fetch()){
910 $j=0;
912 foreach ($attributes as $at){
913 $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
914 $j++;
915 }
917 $i++;
918 }
920 return ($display);
921 }
924 function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
925 {
926 $ret = "";
927 $data = "";
928 if($this->reconnect){
929 $this->connect();
930 }
932 /* Searching Ldap Tree */
933 $sr= @ldap_read($this->cid, LDAP::fix($dn), $filter, $name);
935 /* Get the first entry */
936 $entry= @ldap_first_entry($this->cid, $sr);
938 /* Get all attributes related to that Objekt */
939 $atts = array();
941 /* Assemble dn */
942 $atts[0]['name'] = "dn";
943 $atts[0]['value'] = array('count' => 1, 0 => $dn);
945 /* Reset index */
946 $i = 1 ;
947 $identifier = array();
948 $attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
949 while ($attribute) {
950 $i++;
951 $atts[$i]['name'] = $attribute;
952 $atts[$i]['value'] = @ldap_get_values_len($this->cid, $entry, "$attribute");
954 /* Next one */
955 $attribute= @ldap_next_attribute($this->cid,$entry,$identifier);
956 }
958 foreach($atts as $at)
959 {
960 for ($i= 0; $i<$at['value']['count']; $i++){
962 /* Check if we must encode the data */
963 if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $at['value'][$i])) {
964 $ret .= $at['name'].":: ".base64_encode($at['value'][$i])."\n";
965 } else {
966 $ret .= $at['name'].": ".$at['value'][$i]."\n";
967 }
968 }
969 }
971 return($ret);
972 }
975 function dn_exists($dn)
976 {
977 return @ldap_list($this->cid, LDAP::fix($dn), "(objectClass=*)", array("objectClass"));
978 }
982 /* This funktion imports ldifs
984 If DeleteOldEntries is true, the destination entry will be deleted first.
985 If JustModify is true the destination entry will only be touched by the attributes specified in the ldif.
986 if JustMofify id false the destination dn will be overwritten by the new ldif.
987 */
989 function import_complete_ldif($str_attr,&$error,$JustModify,$DeleteOldEntries)
990 {
991 if($this->reconnect) $this->connect();
993 /* First we have to splitt the string ito detect empty lines
994 An empty line indicates an new Entry */
995 $entries = split("\n",$str_attr);
997 $data = "";
998 $cnt = 0;
999 $current_line = 0;
1001 /* FIX ldif */
1002 $last = "";
1003 $tmp = "";
1004 $i = 0;
1005 foreach($entries as $entry){
1006 if(preg_match("/^ /",$entry)){
1007 $tmp[$i] .= trim($entry);
1008 }else{
1009 $i ++;
1010 $tmp[$i] = trim($entry);
1011 }
1012 }
1014 /* Every single line ... */
1015 foreach($tmp as $entry) {
1016 $current_line ++;
1018 /* Removing Spaces to ..
1019 .. test if a new entry begins */
1020 $tmp = str_replace(" ","",$data );
1022 /* .. prevent empty lines in an entry */
1023 $tmp2 = str_replace(" ","",$entry);
1025 /* If the Block ends (Empty Line) */
1026 if((empty($entry))&&(!empty($tmp))) {
1027 /* Add collected lines as a complete block */
1028 $all[$cnt] = $data;
1029 $cnt ++;
1030 $data ="";
1031 } else {
1033 /* Append lines ... */
1034 if(!empty($tmp2)) {
1035 /* check if we need base64_decode for this line */
1036 if(ereg("::",$tmp2))
1037 {
1038 $encoded = split("::",$entry);
1039 $attr = trim($encoded[0]);
1040 $value = base64_decode(trim($encoded[1]));
1041 /* Add linenumber */
1042 $data .= $current_line."#".base64_encode($attr.":".$value)."\n";
1043 }
1044 else
1045 {
1046 /* Add Linenumber */
1047 $data .= $current_line."#".base64_encode($entry)."\n";
1048 }
1049 }
1050 }
1051 }
1053 /* The Data we collected is not in the array all[];
1054 For example the Data is stored like this..
1056 all[0] = "1#dn : .... \n
1057 2#ObjectType: person \n ...."
1059 Now we check every insertblock and try to insert */
1060 foreach ( $all as $single) {
1061 $lineone = split("\n",$single);
1062 $ndn = split("#", $lineone[0]);
1063 $line = base64_decode($ndn[1]);
1065 $dnn = split (":",$line,2);
1066 $current_line = $ndn[0];
1067 $dn = $dnn[0];
1068 $value = $dnn[1];
1070 /* Every block must begin with a dn */
1071 if($dn != "dn") {
1072 $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
1073 return -2;
1074 }
1076 /* Should we use Modify instead of Add */
1077 $usemodify= false;
1079 /* Delete before insert */
1080 $usermdir= false;
1082 /* The dn address already exists, Don't delete destination entry, overwrite it */
1083 if (($this->dn_exists($value))&&((!$JustModify)&&(!$DeleteOldEntries))) {
1085 $usermdir = $usemodify = false;
1087 /* Delete old entry first, then add new */
1088 } elseif(($this->dn_exists($value))&&($DeleteOldEntries)){
1090 /* Delete first, then add */
1091 $usermdir = true;
1093 } elseif(($this->dn_exists($value))&&($JustModify)) {
1095 /* Modify instead of Add */
1096 $usemodify = true;
1097 }
1099 /* If we can't Import, return with a file error */
1100 if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
1101 $error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
1102 $current_line);
1103 return UNKNOWN_TOKEN_IN_LDIF_FILE; }
1104 }
1106 return (INSERT_OK);
1107 }
1110 /* Imports a single entry
1111 If $delete is true; The old entry will be deleted if it exists.
1112 if $modify is true; All variables that are not touched by the new ldif will be kept.
1113 if $modify is false; The new ldif overwrites the old entry, and all untouched attributes get lost.
1114 */
1115 function import_single_entry($str_attr,$modify,$delete)
1116 {
1117 global $config;
1119 if(!$config){
1120 trigger_error("Can't import ldif, can't read config object.");
1121 }
1124 if($this->reconnect) $this->connect();
1126 $ret = false;
1127 $rows= split("\n",$str_attr);
1128 $data= false;
1130 foreach($rows as $row) {
1132 /* Check if we use Linenumbers (when import_complete_ldif is called we use
1133 Linenumbers) Linenumbers are use like this 123#attribute : value */
1134 if(!empty($row)) {
1135 if(strpos($row,"#")!=FALSE) {
1137 /* We are using line numbers
1138 Because there is a # before a : */
1139 $tmp1= split("#",$row);
1140 $current_line= $tmp1[0];
1141 $row= base64_decode($tmp1[1]);
1142 }
1144 /* Split the line into attribute and value */
1145 $attr = split(":", $row,2);
1146 $attr[0]= trim($attr[0]); /* attribute */
1147 $attr[1]= $attr[1]; /* value */
1149 /* Check :: was used to indicate base64_encoded strings */
1150 if($attr[1][0] == ":"){
1151 $attr[1]=trim(preg_replace("/^:/","",$attr[1]));
1152 $attr[1]=base64_decode($attr[1]);
1153 }
1155 $attr[1] = trim($attr[1]);
1157 /* Check for attributes that are used more than once */
1158 if(!isset($data[$attr[0]])) {
1159 $data[$attr[0]]=$attr[1];
1160 } else {
1161 $tmp = $data[$attr[0]];
1163 if(!is_array($tmp)) {
1164 $new[0]=$tmp;
1165 $new[1]=$attr[1];
1166 $datas[$attr[0]]['count']=1;
1167 $data[$attr[0]]=$new;
1168 } else {
1169 $cnt = $datas[$attr[0]]['count'];
1170 $cnt ++;
1171 $data[$attr[0]][$cnt]=$attr[1];
1172 $datas[$attr[0]]['count'] = $cnt;
1173 }
1174 }
1175 }
1176 }
1178 /* If dn is an index of data, we should try to insert the data */
1179 if(isset($data['dn'])) {
1181 /* Fix dn */
1182 $tmp = gosa_ldap_explode_dn($data['dn']);
1183 unset($tmp['count']);
1184 $newdn ="";
1185 foreach($tmp as $tm){
1186 $newdn.= trim($tm).",";
1187 }
1188 $newdn = preg_replace("/,$/","",$newdn);
1189 $data['dn'] = $newdn;
1191 /* Creating Entry */
1192 $this->cd($data['dn']);
1194 /* Delete existing entry */
1195 if($delete){
1196 $this->rmdir_recursive($data['dn']);
1197 }
1199 /* Create missing trees */
1200 $this->cd ($this->basedn);
1201 $this->cd($config->current['BASE']);
1202 $this->create_missing_trees(preg_replace("/^[^,]+,/","",$data['dn']));
1203 $this->cd($data['dn']);
1205 $dn = $data['dn'];
1206 unset($data['dn']);
1208 if(!$modify){
1210 $this->cat($dn);
1211 if($this->count()){
1213 /* The destination entry exists, overwrite it with the new entry */
1214 $attrs = $this->fetch();
1215 foreach($attrs as $name => $value ){
1216 if(!is_numeric($name)){
1217 if(in_array($name,array("dn","count"))) continue;
1218 if(!isset($data[$name])){
1219 $data[$name] = array();
1220 }
1221 }
1222 }
1223 $ret = $this->modify($data);
1225 }else{
1227 /* The destination entry doesn't exists, create it */
1228 $ret = $this->add($data);
1229 }
1231 } else {
1233 /* Keep all vars that aren't touched by this ldif */
1234 $ret = $this->modify($data);
1235 }
1236 }
1237 show_ldap_error($this->get_error(), sprintf(_("Ldap import with dn '%s' failed."),$dn));
1238 return($ret);
1239 }
1242 function importcsv($str)
1243 {
1244 $lines = split("\n",$str);
1245 foreach($lines as $line)
1246 {
1247 /* continue if theres a comment */
1248 if(substr(trim($line),0,1)=="#"){
1249 continue;
1250 }
1252 $line= str_replace ("\t\t","\t",$line);
1253 $line= str_replace ("\t" ,"," ,$line);
1254 echo $line;
1256 $cells = split(",",$line ) ;
1257 $linet= str_replace ("\t\t",",",$line);
1258 $cells = split("\t",$line);
1259 $count = count($cells);
1260 }
1262 }
1264 function get_objectclasses()
1265 {
1266 $objectclasses = array();
1267 global $config;
1269 /* Only read schema if it is allowed */
1270 if(isset($config) && preg_match("/config/i",get_class($config))){
1271 if(!isset($config->data['MAIN']['SCHEMA_CHECK']) || !preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
1272 return($objectclasses);
1273 }
1274 }
1276 # Get base to look for schema
1277 $sr = @ldap_read ($this->cid, NULL, "objectClass=*", array("subschemaSubentry"));
1278 if(!$sr){
1279 $sr = @ldap_read ($this->cid, "", "objectClass=*", array("subschemaSubentry"));
1280 }
1282 $attr = @ldap_get_entries($this->cid,$sr);
1283 if (!isset($attr[0]['subschemasubentry'][0])){
1284 return array();
1285 }
1287 /* Get list of objectclasses and fill array */
1288 $nb= $attr[0]['subschemasubentry'][0];
1289 $objectclasses= array();
1290 $sr= ldap_read ($this->cid, $nb, "objectClass=*", array("objectclasses"));
1291 $attrs= ldap_get_entries($this->cid,$sr);
1292 if (!isset($attrs[0])){
1293 return array();
1294 }
1295 foreach ($attrs[0]['objectclasses'] as $val){
1296 if (preg_match('/^[0-9]+$/', $val)){
1297 continue;
1298 }
1299 $name= "OID";
1300 $pattern= split(' ', $val);
1301 $ocname= preg_replace("/^.* NAME\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $val);
1302 $objectclasses[$ocname]= array();
1304 foreach($pattern as $chunk){
1305 switch($chunk){
1307 case '(':
1308 $value= "";
1309 break;
1311 case ')': if ($name != ""){
1312 $objectclasses[$ocname][$name]= $this->value2container($value);
1313 }
1314 $name= "";
1315 $value= "";
1316 break;
1318 case 'NAME':
1319 case 'DESC':
1320 case 'SUP':
1321 case 'STRUCTURAL':
1322 case 'ABSTRACT':
1323 case 'AUXILIARY':
1324 case 'MUST':
1325 case 'MAY':
1326 if ($name != ""){
1327 $objectclasses[$ocname][$name]= $this->value2container($value);
1328 }
1329 $name= $chunk;
1330 $value= "";
1331 break;
1333 default: $value.= $chunk." ";
1334 }
1335 }
1337 }
1338 return $objectclasses;
1339 }
1342 function value2container($value)
1343 {
1344 /* Set emtpy values to "true" only */
1345 if (preg_match('/^\s*$/', $value)){
1346 return true;
1347 }
1349 /* Remove ' and " if needed */
1350 $value= preg_replace('/^[\'"]/', '', $value);
1351 $value= preg_replace('/[\'"] *$/', '', $value);
1353 /* Convert to array if $ is inside... */
1354 if (preg_match('/\$/', $value)){
1355 $container= preg_split('/\s*\$\s*/', $value);
1356 } else {
1357 $container= chop($value);
1358 }
1360 return ($container);
1361 }
1364 function log($string)
1365 {
1366 if (isset($_SESSION['config'])){
1367 $cfg= $_SESSION['config'];
1368 if (isset($cfg->current['LDAPSTATS']) && preg_match('/true/i', $cfg->current['LDAPSTATS'])){
1369 syslog (LOG_INFO, $string);
1370 }
1371 }
1372 }
1374 /* added by Guido Serra aka Zeph <zeph@purotesto.it> */
1375 function getCn($dn){
1376 $simple= split(",", $dn);
1378 foreach($simple as $piece) {
1379 $partial= split("=", $piece);
1381 if($partial[0] == "cn"){
1382 return $partial[1];
1383 }
1384 }
1385 }
1388 function get_naming_contexts($server, $admin= "", $password= "")
1389 {
1390 /* Build LDAP connection */
1391 $ds= ldap_connect ($server);
1392 if (!$ds) {
1393 die ("Can't bind to LDAP. No check possible!");
1394 }
1395 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1396 $r= ldap_bind ($ds, $admin, $password);
1398 /* Get base to look for naming contexts */
1399 $sr = @ldap_read ($ds, "", "objectClass=*", array("+"));
1400 $attr= @ldap_get_entries($ds,$sr);
1402 return ($attr[0]['namingcontexts']);
1403 }
1406 function get_root_dse($server, $admin= "", $password= "")
1407 {
1408 /* Build LDAP connection */
1409 $ds= ldap_connect ($server);
1410 if (!$ds) {
1411 die ("Can't bind to LDAP. No check possible!");
1412 }
1413 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1414 $r= ldap_bind ($ds, $admin, $password);
1416 /* Get base to look for naming contexts */
1417 $sr = @ldap_read ($ds, "", "objectClass=*", array("+"));
1418 $attr= @ldap_get_entries($ds,$sr);
1420 /* Return empty array, if nothing was set */
1421 if (!isset($attr[0])){
1422 return array();
1423 }
1425 /* Rework array... */
1426 $result= array();
1427 for ($i= 0; $i<$attr[0]['count']; $i++){
1428 $result[$attr[0][$i]]= $attr[0][$attr[0][$i]];
1429 unset($result[$attr[0][$i]]['count']);
1430 }
1432 return ($result);
1433 }
1436 }
1438 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1439 ?>