ispman/scripts ldifupdate,1.2,1.3

[email protected]
Newsgroups gmane.comp.isp.ispman.cvs
Message-ID <[email protected]>
Update of /cvsroot/ispman/ispman/scripts
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24571/scripts

Modified Files:
	ldifupdate 
Log Message:
more enhancements, bugfixes
reports only *necessary* changes
can also be used to check your ldap data

Index: ldifupdate
===================================================================
RCS file: /cvsroot/ispman/ispman/scripts/ldifupdate,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ldifupdate	18 Jan 2004 20:03:19 -0000	1.2
+++ ldifupdate	28 Jan 2004 11:29:50 -0000	1.3
@@ -10,6 +10,10 @@
 # according to current schema
 # For details see comments below
 
+use strict;
+use Getopt::Std;
+use Net::LDAP::LDIF;
+use Net::LDAP::Entry;
 
 sub usage {
     print "ISPMan ldifupdate\n";
@@ -23,10 +27,32 @@
     print "  -b        base DN (optional), which limits changes to this subtree\n"; 
 }
 
-use strict;
-use Getopt::Std;
-use Net::LDAP::LDIF;
-use Net::LDAP::Entry;
+sub update_oc {
+    my ($entry,$src, $dst)=@_;
+    my $msg="";
+    my %in_src;
+    my %in_dst;
+    
+    for (@$src) { $in_src{$_}=1; }
+    for (@$dst) {
+	if (! $in_src{$_}) {
+	    $msg.="adding objectclass: \"$_\"\n";
+	    $entry->add("objectclass",$_);
+	}
+	$in_dst{$_}=1;
+    }
+    for (@$src) {
+	if (! $in_dst{$_}) {
+	    $msg.="deleting objectclass: \"$_\"\n";
+	    $entry->delete("objectclass" => [$_]);
+	}
+    }
+    
+    return $msg;
+}
+
+
+## main
 
 # defines
 my @oc_normalDomain = qw(ispmanDomain PureFTPdUser posixAccount);
@@ -73,40 +99,41 @@
 
     $msg="";
 
-    ## update entry
+
     my @oc=$entry->get_value("objectclass");
+    my %oc;
+    for (@oc) { $oc{$_}=1 }
+
+    ## work on entry
+
     if ($#oc==0 && $oc[0] eq "top"){
 	
 	# if there is only one objectclass: top
 	# replace with ispmanBranch
-	$msg.="replacing objectclass \"top\" with \"ispmanBranch\"\n";
+	$msg.="replacing objectclass: \"top\" -> \"ispmanBranch\"\n";
 	$entry->replace("objectclass", "ispmanBranch");
 
-    } elsif ($dn=~m/^ou=/){
+    } elsif ($dn=~m/^ou=/ && $oc[0] ne "ispmanBranch"){
 
 	# if entry starts with "ou=" replace 
-	# objectclasses top, organizationalunit with ispmanBranch
-	$msg.="replacing objectclass with \"ispmanBranch\"\n";
+       	# objectclasses top, organizationalunit with ispmanBranch
+	$msg.="replacing objectclass: -> \"ispmanBranch\"\n";
 	$entry->replace("objectclass", "ispmanBranch");
 
-    } elsif ($dn=~m/^ispmanDomain=/){
+    } elsif ($oc{"ispmanDomain"}){
 	
 	# update ispmanDomain objectclasses
 	if ($entry->get_value("ispmanDomainType") eq "replica") {
-	    $msg.="Found ispmanReplicaDomain:\n";
-	    $msg.="replacing objectclass with [@oc_replicaDomain]\n";
-	    $entry->replace("objectclass", @oc_replicaDomain );
+	    $msg.=update_oc($entry,\@oc,\@oc_replicaDomain);
 	} else {
-	    $msg.="Found ispmanDomain:\n";
-	    $msg.="replacing objectclass with [@oc_normalDomain]\n";
-	    $entry->replace("objectclass", @oc_normalDomain);
+	    $msg.=update_oc($entry,\@oc,\@oc_normalDomain);
 	}
 
 	# check ispmanDomainDefaultFileServer attribute
 	if (! defined $entry->get_value("ispmanDomainDefaultFileServer")){
 	    $msg.="WARNING! no \"Default Fileserver\" defined!\n";
 	    foreach (@default_fileserver){
-		$msg.="adding ispmanDomainDefaultFileServer with \"$_\"\n";
+		$msg.="adding ispmanDomainDefaultFileServer: \"$_\"\n";
 		$entry->add("ispmanDomainDefaultFileServer",$_);
 	    }
 	}
@@ -115,51 +142,51 @@
 	if (! defined $entry->get_value("ispmanDomainDefaultWebHost")){
 	    $msg.="WARNING! no \"Default Webhost\" defined!\n";
 	    foreach (@default_webhost){
-		$msg.="adding ispmanDomainWebHost with \"$_\"\n";
+		$msg.="adding ispmanDomainWebHost: \"$_\"\n";
 		$entry->add("ispmanDomainDefaultWebHost",$_);
 	    }
 	}
 
-    } elsif ($dn=~m/^ispmanVhostName/){
+    } elsif ($oc{'ispmanVirtualHost'}){
 
 	# update ispmanVhost
-	$msg.="Found ispmanVhost:\n";
 	my $ispmanDomain=$entry->get_value("ispmanDomain") || die("ispmanDomain undefined!");
     	my $ispmanVhostName=$entry->get_value("ispmanVhostName") || die("ispmanVhostName undefined!");
 
-	# replacing vhost objectclasses
-	$msg.="replacing objectclass with [@oc_virtualHost]\n";
-	$entry->replace("objectclass", @oc_virtualHost);
+	# checking/replacing vhost objectclasses
+	$msg.=update_oc($entry,\@oc,\@oc_virtualHost);
 	
 	# checking vhost cn attribute
+	my $lcn=$entry->get_value("cn");
 	my $cn=join ".", ($ispmanVhostName, $ispmanDomain);
-	if (! defined ($entry->get_value("cn"))){
+	if (! defined $lcn){
 	    $msg.="adding cn with \"$cn\"\n";
 	    $entry->add("cn", $cn);
-	} elsif ($entry->get_value("cn") ne $ispmanVhostName) {
-	    $msg.="changing cn to \"$cn\"\n";
+	} elsif ($lcn ne $cn) {
+	    $msg.="changing cn: \"$lcn\" -> \"$cn\"\n";
 	    $entry->replace("cn", $cn);
 	}
 	
 	# checking vhost uid attribute
+	my $luid=$entry->get_value("uid");
 	my $uid=join ".", ($ispmanVhostName, $ispmanDomain);
-	if (! defined ($entry->get_value("uid"))){
+	if (! defined $luid){
 	    $msg.="adding uid with \"$uid\"\n";
 	    $entry->add("uid", $uid);
-	} elsif ($entry->get_value("uid") ne $uid){
-	    $msg.="changing uid to \"$uid\"\n";
+	} elsif ($luid ne $uid){
+	    $msg.="changing uid: \"$luid\" -> \"$uid\"\n";
 	    $entry->replace("uid", $uid);
 	}
 	
 	# checking vhost uidNumber attribute
 	unless ($entry->get_value("uidNumber")){
-	    $msg.="adding dummy uidNumber: $dummyUid. This should be replaced later!\n";
+	    $msg.="adding dummy uidNumber: $dummyUid (This should be replaced later!\n)";
 	    $entry->add("uidNumber", $dummyUid);
 	}
 
 	# checking vhost gidNumber attribute
 	unless ($entry->get_value("gidNumber")){
-	    $msg.="adding dummy gidNumber: $dummyGid. This should be replaced later!\n";
+	    $msg.="adding dummy gidNumber: $dummyGid (This should be replaced later!)\n";
 	    $entry->add("gidNumber", $dummyGid);
 	}
 
@@ -167,16 +194,31 @@
 	my $homedir=join '/', ($default_domainHomedir, $ispmanDomain, 'vhosts',
 			       $ispmanVhostName);
 	unless ($entry->get_value("homeDirectory")){
-	    $msg.="adding homeDirectory with \"$homedir\"\n";
+	    $msg.="adding homeDirectory: \"$homedir\"\n";
 	    $entry->add("homeDirectory", $homedir);
 	}
         
 	# checking vhost userPassword attribute
 	unless ($entry->get_value("userPassword")){
-	    $msg.="adding userPassword with \"$dummyPassword\". This should be replaced later!\n";
+	    $msg.="adding userPassword: \"$dummyPassword\" (This should be replaced later!)\n";
 	    $entry->add("userPassword", $dummyPassword);
 	}
 	
+    } elsif ($oc{'ispmanDomainUser'}){
+	
+	# checking for alternate uid
+	my @uids=$entry->get_value("uid");
+	my $uid=join "@", $entry->get_value("ispmanUserId"),
+	                  $entry->get_value("ispmanDomain");
+	my $hasAlternateUid=0;
+	for (@uids) {
+	    if ($_ eq $uid) { $hasAlternateUid=1; }
+	}
+	if (! $hasAlternateUid) {
+	    $msg.="adding uid: \"$uid\"\n";
+	    $entry->add("uid",$uid);
+	}
+
     } else {
 	# check other entries here	
 	



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.