summaryrefslogtreecommitdiffstats
path: root/ldap-site-mngmt/webinterface/lib/ldap2.inc.php
blob: 3d0b26d97cecb33b810e6bbda96a014dc225364b (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* ldap2.inc.php - LDAP-Bibliothek
* Diese Bibliothek enthält weitere LDAP Hilfs-Funktionen
*
* @param string ldapError
* @param resource ds
*
* @author Tarik Gasmi
* @copyright Tarik Gasmi
*/
//Konfiguration laden
require_once("config.inc.php");

$ldapError = null;


# Liefert Array aller Child-Knoten mit bestimmten ausgwählten Attributen
function get_childs($baseDN,$attributes)
{
	global $ds, $suffix, $ldapError;
	
	if(!($result = uniLdapSearch($ds, $baseDN, "(objectclass=*)", $attributes, "", "list", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search";  
  		die;
	} 
	else {
		$result = ldapArraySauber($result); 
	
		$childau_array = array();
   	foreach ($result as $item){
			foreach ($attributes as $att){
   			$atts[$att] = $item[$att];
   		}
   		$childau_array[] = $atts;
   	} 
		if($attributes != false ){return $childau_array;}
		else{return $result;}
   }
} 

# Liefert die RDNs aller Child-Knoten
function get_childs_rdn($baseDN){

	$childs = get_childs($baseDN,array("dn"));
	# print_r($childs); echo "<br><br>";

	$childs_rdn = array();
	foreach ($childs as $item){
		$exp = explode(',',$item['dn']);
		$rdn = $exp[0];
		$childs_rdn[] = $rdn;
	}

	# print_r($childs_rdn);
	return $childs_rdn;
}


# Attribute eines Knotens (Vorsicht Array enthält noch DN und COUNT)
# in einem Array wie er z.B. von ldap_add verwendet wird
function get_node_attributes($nodeDN){
	global $ds, $suffix, $ldapError;
	
	if(!($result = uniLdapSearch($ds, $nodeDN, "(objectclass=*)", array(), "", "one", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search";  
  		die;
	}
	$result = ldapArraySauber($result); 
	# print_r($result);
	foreach ($result as $item){
		foreach (array_keys($item) as $merkmal)
		$attrs[$merkmal] = $item[$merkmal];
	} 
	return $attrs;
}

# Rekursives Kopieren
function dive_into_tree_cp($baseDN,$new_baseDN){

 	global $ds, $suffix, $ldapError;
  
  	$expldn = ldap_explode_dn($new_baseDN,0);
  	$new_node_rdn = $expldn[0];
	$exp = explode('=',$new_node_rdn);
	$new_node_rdn_merk = $exp[0];
	$new_node_rdn_val = $exp[1];
	$new_node_rdn_merk = strtolower($new_node_rdn_merk);
		
  	$childs_rdn = get_childs_rdn($baseDN);
 	
  	$attrs = get_node_attributes($baseDN);
  	# print_r($attrs); echo "<br>";
  	unset($attrs['dn']);
  	unset($attrs['count']);
  	$attrs["$new_node_rdn_merk"] = $new_node_rdn_val; 
  	# print_r($attrs); echo "<br>";
  	
  	$result = ldap_add($ds,$new_baseDN,$attrs);
	

  	//recursivly do dive for each child
  	foreach($childs_rdn as $rdn){
  		dive_into_tree_cp( $rdn.",".$baseDN , $rdn.",".$new_baseDN);
  	}
   return $result;
}


# 
# Rekursives Loeschen
function dive_into_tree_del($baseDN,$except){

  global $ds, $suffix, $ldapError;
  
  $childs_rdn = get_childs_rdn($baseDN);
  //recursivly do dive for each child
  foreach($childs_rdn as $rdn){
  		dive_into_tree_del( $rdn.",".$baseDN , $except);
  	}
  if($baseDN != $except){
    $result = ldap_delete($ds,$baseDN);
  }
  
  return $result;
}

# Rekursives Verschieben
function move_subtree($oldDN,$newDN){

  if(dive_into_tree_cp($oldDN,$newDN))
  {
    dive_into_tree_del($oldDN,"");
    echo "Moved subtree<br>";
    return 1;
  }
  else echo "Moving subtree not possible!!!<br>";
}


function adjust_dn_entries($oldDN,$newDN){

	global $ds, $suffix, $ldapError;
	
	# single Attributes:
	if(!($result = uniLdapSearch($ds, $newDN, "(objectclass=host)", array("dn","dhcphlpcont"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item){
		if (strpos($item['dhcphlpcont'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item['dhcphlpcont']);
			$entry['dhcphlpcont'] = $newvalue;
			ldap_mod_replace($ds,$item['dn'],$entry);
		}
	}
	
	if(!($result = uniLdapSearch($ds, $newDN, "(objectclass=PXEConfig)", array("dn","rbservicedn"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item2){
		if (strpos($item2['rbservicedn'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item2['rbservicedn']);
			$entry2['rbservicedn'] = $newvalue;
			ldap_mod_replace($ds,$item2['dn'],$entry2);
		}
	}
	
	if(!($result = uniLdapSearch($ds, $newDN, "(objectclass=dhcpService)", array("dn","dhcpprimarydn","dhcpsecondarydn"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item3){
		if (strpos($item3['dhcpprimarydn'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item3['dhcpprimarydn']);
			$entry3['dhcpprimarydn'] = $newvalue;
			ldap_mod_replace($ds,$item3['dn'],$entry3);
		}
		if (strpos($item3['dhcpsecondarydn'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item3['dhcpsecondarydn']);
			$entry4['dhcpsecondarydn'] = $newvalue;
			ldap_mod_replace($ds,$item3['dn'],$entry4);
		}
	}
	
	if(!($result = uniLdapSearch($ds, $newDN, "(objectclass=MenuEntry)", array("dn","genericmenuentrydn","ldapuri"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item4){
		if (strpos($item4['genericmenuentrydn'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item4['genericmenuentrydn']);
			$entry5['genericmenuentrydn'] = $newvalue;
			ldap_mod_replace($ds,$item4['dn'],$entry5);
		}
		if (strpos($item4['ldapuri'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item4['ldapuri']);
			$entry6['ldapuri'] = $newvalue;
			ldap_mod_replace($ds,$item4['dn'],$entry6);
		}
	}
	
	# Multi-Attribut member
	if(!($result = uniLdapSearch($ds, $newDN, "(objectclass=groupOfComputers)", array("dn","member","dhcphlpcont"), "", "sub", 0, 0))) {
 			# redirect(5, "", $ldapError, FALSE);
  			echo "no search"; 
  			die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item5){
		for ($i=0; $i<count($item5['member']); $i++){
			if (strpos($item5['member'][$i],$oldDN) != false){
				$newvalue = str_replace($oldDN,$newDN,$item5['member'][$i]);
				$entry7['member'][$i] = $newvalue;
			}else{
				$entry7['member'][$i] = $item5['member'][$i];
			}
			ldap_mod_replace($ds,$item5['dn'],$entry7);
		}
		if (strpos($item5['dhcphlpcont'],$oldDN) != false){
			$newvalue = str_replace($oldDN,$newDN,$item5['dhcphlpcont']);
			$entry8['dhcphlpcont'] = $newvalue;
			ldap_mod_replace($ds,$item5['dn'],$entry8);
		}
		
	}
	
	# Attribut AssociatedName in DNS Teilbaum
	if(!($result = uniLdapSearch($ds, "ou=DNS,".$suffix , "(associatedname=$oldDN)", array("dn","associatedname"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item6){
		$newvalue = str_replace($oldDN,$newDN,$item6['associatedname']);
		$entry9['associatedname'] = $newvalue;
		ldap_mod_replace($ds,$item6['dn'],$entry9);
	}
	
	# Attribut RelativeDomainName in DNS Teilbaum ... noch nicht fertig
	$hostdnexpold = ldap_explode_dn($oldDN, 0);
	$hostdnexpnew = ldap_explode_dn($newDN, 0);
	$oldhostname = $hostdnexpold[0]; 
	$newhostname = $hostdnexpnew[0];
	if(!($result = uniLdapSearch($ds, "ou=DNS,".$suffix , "(relativedomainname=$oldhostname)", array("dn","relativedomainname"), "", "sub", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search"; 
  		die;
	}
	$result = ldapArraySauber($result);
	#print_r($result); echo "<br><br>";
	foreach ($result as $item7){
		$newvalue = str_replace($oldhostname,$newhostname,$item7['relativedomainname']);
		$entry10['relativedomainname'] = $newvalue;
		ldap_mod_replace($ds,$item7['dn'],$entry10);
	}

}



function check_for_dc($dn, $dc){
	global $ds, $suffix, $ldapError;
	if(!($result = uniLdapSearch($ds, $dn, "(dc=$dc)", array("dc"), "", "list", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search";  
  		die;
	}
	$result = ldapArraySauber($result);
	if (count($result[0]['dc']) == 0 ) {return 0;}
	elseif ($result[0]['dc'] == $dc){return 1;}
}


function get_dc_childs($baseDN,$attributes)
{
	global $ds, $suffix, $ldapError;
	
	if(!($result = uniLdapSearch($ds, $baseDN, "(objectclass=dnsdomain)", $attributes, "", "list", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search";  
  		die;
	} 
	else {
		$result = ldapArraySauber($result); 
	
		$childau_array = array();
   	foreach ($result as $item){
			foreach ($attributes as $att){
   			$atts[$att] = $item[$att];
   		}
   		$childau_array[] = $atts;
   	} 
		if($attributes != false ){return $childau_array;}
		else{return $result;}
   }
} 

# Liefert die RDNs aller dc-Child-Knoten
function get_dc_childs_rdn($baseDN){
	
	global $ds, $suffix, $ldapError;
	
	$childs = get_dc_childs($baseDN,array("dn"));
	# print_r($childs); echo "<br><br>";

	$childs_rdn = array();
	foreach ($childs as $item){
		$exp = explode(',',$item['dn']);
		$rdn = $exp[0];
		$childs_rdn[] = $rdn;
	}

	# print_r($childs_rdn);
	return $childs_rdn;
}

function get_entry_number($entryDN,$entryobjectclass){

	global $ds, $suffix, $ldapError;
	
	if(!($result = uniLdapSearch($ds, $entryDN, "(objectclass=machineconfig)", array("count"), "", "one", 0, 0))) {
 		# redirect(5, "", $ldapError, FALSE);
  		echo "no search";  
  		die;
	}
	$result = ldapArraySauber($result); 
	print_r ($result);

}

?>