ispman/scripts ldifupdate,1.3,1.4

[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-serv3266/scripts

Modified Files:
	ldifupdate 
Log Message:
mode bugfixes
added option for generation of a "modify LDIF"

Index: ldifupdate
===================================================================
RCS file: /cvsroot/ispman/ispman/scripts/ldifupdate,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ldifupdate	28 Jan 2004 11:29:50 -0000	1.3
+++ ldifupdate	1 Feb 2004 14:01:20 -0000	1.4
@@ -15,6 +15,24 @@
 use Net::LDAP::LDIF;
 use Net::LDAP::Entry;
 
+# defines
+my @oc_normalDomain = qw(ispmanDomain PureFTPdUser posixAccount);
+my @oc_replicaDomain = qw(ispmanDomain ispmanBranch top);
+my @oc_virtualHost = qw(ispmanVirtualHost posixAccount PureFTPdUser);
+my $default_domainHomedir = "/ispman/domains";
+my @default_fileserver = qw( );
+my @default_webhost = qw( );
+my $dummyUid = "999999";
+my $dummyGid = "999999";
+my $dummyPassword = "123456";
+my $mode;
+
+# private variables
+my ($ldif_in, $ldif_out, $entry, $basedn);
+my (%opts, $dn, $msg, $chng);
+
+
+
 sub usage {
     print "ISPMan ldifupdate\n";
     print "A utility to update your ispman LDAP data according to current ISPMan schema.\n";
@@ -22,11 +40,38 @@
     print "either in add or modify format.\n\n";
     print "Syntax: ldifupdate <options>\n";
     print "Options:\n";
-    print "  -i        input LDIF filename\n"; 
-    print "  -o        output LDIF filename\n"; 
-    print "  -b        base DN (optional), which limits changes to this subtree\n"; 
+    print "  -i <file>        input LDIF filename\n"; 
+    print "  -o <file>        output LDIF filename\n"; 
+    print "  -b <basedn>      base DN (optional), which limits changes to this subtree\n"; 
+    print "  -m <add/modify>  type of LDIF to create (default=add)\n";
+}
+
+sub ldif_action {
+    my ($cmd,$attr,$attrval)=@_;
+    my $ldif;
+
+    # modify entry
+    if ($cmd eq "delete"){
+	$entry->delete($attr => [$attrval]);
+    } else {
+	$entry->$cmd($attr,$attrval);
+    }
+
+    # generate ldif change
+    $ldif="$cmd: $attr\n";
+    if (ref $attrval eq "ARRAY"){
+	for (@$attrval){ $ldif.="$attr: $_\n"; }
+    } else {
+	$ldif.="$attr: $attrval\n";
+    }
+
+    $ldif.="-\n";
+
+    # write ldif change to global var
+    $chng.=$ldif;
 }
 
+    
 sub update_oc {
     my ($entry,$src, $dst)=@_;
     my $msg="";
@@ -37,14 +82,14 @@
     for (@$dst) {
 	if (! $in_src{$_}) {
 	    $msg.="adding objectclass: \"$_\"\n";
-	    $entry->add("objectclass",$_);
+	    ldif_action("add","objectclass",$_);
 	}
 	$in_dst{$_}=1;
     }
     for (@$src) {
 	if (! $in_dst{$_}) {
 	    $msg.="deleting objectclass: \"$_\"\n";
-	    $entry->delete("objectclass" => [$_]);
+	    ldif_action("delete","objectclass",$_);
 	}
     }
     
@@ -54,42 +99,46 @@
 
 ## main
 
-# defines
-my @oc_normalDomain = qw(ispmanDomain PureFTPdUser posixAccount);
-my @oc_replicaDomain = qw(ispmanDomain ispmanBranch top);
-my @oc_virtualHost = qw(ispmanVirtualHost posixAccount PureFTPdUser);
-my $default_domainHomedir = "/ispman/domains";
-my @default_fileserver = qw( );
-my @default_webhost = qw( );
-my $dummyUid = "999999";
-my $dummyGid = "999999";
-my $dummyPassword = "123456";
-
-# local variables
-my ($ldif, $ldif2, $entry, $basedn);
-my (%opts, $dn, $msg);
 
 # get parameters
-getopt('iob',  \%opts);
+getopt('iobm',  \%opts);
 unless ($opts{'i'} or $opts{'o'}){
     usage();
     exit 1;
 }
 
-$ldif = Net::LDAP::LDIF->new( $opts{'i'});
-$ldif2 = Net::LDAP::LDIF->new( $opts{'o'}, "w");
+# set mode
+$mode=$opts{'m'} || "add";
 
+if ($mode ne "add" and $mode ne "modify"){
+    print STDERR "unknown mode: $mode\n";
+    usage();
+    exit 1;
+}
+
+
+# input file
+$ldif_in= Net::LDAP::LDIF->new( $opts{'i'});
+
+# output file
+if ($mode eq "add"){
+    $ldif_out = Net::LDAP::LDIF->new( $opts{'o'}, "w");
+} else {
+    open(OUTLDIF,">$opts{'o'}") || die "unable to create $opts{'o'}\n";
+}
+
+# only work on entries of this basedn
 $basedn = $opts{'b'};
 
 # read and parse ldap data
-while( not $ldif->eof() ) {
+while( not $ldif_in->eof() ) {
     undef ($entry);
-    $entry = $ldif->read_entry();
+    $entry = $ldif_in->read_entry();
     
     # skip entry if we find an error
-    if ( $ldif->error() ) {
-        print STDERR "Error msg: ",$ldif->error(),"\n";
-        print STDERR "Error lines:\n",$ldif->error_lines(),"\n";
+    if ( $ldif_in->error() ) {
+        print STDERR "Error msg: ",$ldif_in->error(),"\n";
+        print STDERR "Error lines:\n",$ldif_in->error_lines(),"\n";
     }
 
     $entry->changetype("add");
@@ -98,7 +147,7 @@
     next unless ! defined($basedn) or $dn =~ m/$basedn$/;
 
     $msg="";
-
+    $chng="";
 
     my @oc=$entry->get_value("objectclass");
     my %oc;
@@ -111,14 +160,14 @@
 	# if there is only one objectclass: top
 	# replace with ispmanBranch
 	$msg.="replacing objectclass: \"top\" -> \"ispmanBranch\"\n";
-	$entry->replace("objectclass", "ispmanBranch");
+	ldif_action("replace","objectclass","ispmanbranch");
 
     } elsif ($dn=~m/^ou=/ && $oc[0] ne "ispmanBranch"){
 
 	# if entry starts with "ou=" replace 
        	# objectclasses top, organizationalunit with ispmanBranch
 	$msg.="replacing objectclass: -> \"ispmanBranch\"\n";
-	$entry->replace("objectclass", "ispmanBranch");
+	ldif_action("replace","objectclass","ispmanbranch");
 
     } elsif ($oc{"ispmanDomain"}){
 	
@@ -134,7 +183,7 @@
 	    $msg.="WARNING! no \"Default Fileserver\" defined!\n";
 	    foreach (@default_fileserver){
 		$msg.="adding ispmanDomainDefaultFileServer: \"$_\"\n";
-		$entry->add("ispmanDomainDefaultFileServer",$_);
+		ldif_action("add","ispmanDomainDefaultFileServer",$_);
 	    }
 	}
 
@@ -143,7 +192,7 @@
 	    $msg.="WARNING! no \"Default Webhost\" defined!\n";
 	    foreach (@default_webhost){
 		$msg.="adding ispmanDomainWebHost: \"$_\"\n";
-		$entry->add("ispmanDomainDefaultWebHost",$_);
+		ldif_action("add","ispmanDomainDefaultWebHost",$_);
 	    }
 	}
 
@@ -155,16 +204,16 @@
 
 	# 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 $lcn){
 	    $msg.="adding cn with \"$cn\"\n";
-	    $entry->add("cn", $cn);
+	    ldif_action("add","cn",$cn);
 	} elsif ($lcn ne $cn) {
 	    $msg.="changing cn: \"$lcn\" -> \"$cn\"\n";
-	    $entry->replace("cn", $cn);
+	    ldif_action("replace","cn",$cn);
 	}
 	
 	# checking vhost uid attribute
@@ -172,22 +221,22 @@
 	my $uid=join ".", ($ispmanVhostName, $ispmanDomain);
 	if (! defined $luid){
 	    $msg.="adding uid with \"$uid\"\n";
-	    $entry->add("uid", $uid);
+	    ldif_action("add","uid",$uid);
 	} elsif ($luid ne $uid){
 	    $msg.="changing uid: \"$luid\" -> \"$uid\"\n";
-	    $entry->replace("uid", $uid);
+	    ldif_action("replace","uid",$uid);
 	}
 	
 	# checking vhost uidNumber attribute
 	unless ($entry->get_value("uidNumber")){
 	    $msg.="adding dummy uidNumber: $dummyUid (This should be replaced later!\n)";
-	    $entry->add("uidNumber", $dummyUid);
+	    ldif_action("add","uidNumber",$dummyUid);
 	}
 
 	# checking vhost gidNumber attribute
 	unless ($entry->get_value("gidNumber")){
 	    $msg.="adding dummy gidNumber: $dummyGid (This should be replaced later!)\n";
-	    $entry->add("gidNumber", $dummyGid);
+	    ldif_action("add","gidNumber",$dummyGid);
 	}
 
 	# checking vhost homeDirectory attribute
@@ -195,13 +244,13 @@
 			       $ispmanVhostName);
 	unless ($entry->get_value("homeDirectory")){
 	    $msg.="adding homeDirectory: \"$homedir\"\n";
-	    $entry->add("homeDirectory", $homedir);
+	    ldif_action("add","homeDirectory",$homedir);
 	}
         
 	# checking vhost userPassword attribute
 	unless ($entry->get_value("userPassword")){
 	    $msg.="adding userPassword: \"$dummyPassword\" (This should be replaced later!)\n";
-	    $entry->add("userPassword", $dummyPassword);
+	    ldif_action("add","userPassword",$dummyPassword);
 	}
 	
     } elsif ($oc{'ispmanDomainUser'}){
@@ -216,7 +265,7 @@
 	}
 	if (! $hasAlternateUid) {
 	    $msg.="adding uid: \"$uid\"\n";
-	    $entry->add("uid",$uid);
+	    ldif_action("add","uid",$uid);
 	}
 
     } else {
@@ -235,7 +284,7 @@
     }
     if (! $found) {
 	$msg.="adding RDN to attributes ($attrib: $attribval)\n";
-	$entry->add($attrib,$attribval);
+	ldif_action("add",$attrib,$attribval);
     }
 
     # output msg
@@ -248,12 +297,24 @@
     #print $entry->dump(), "\n";
     #$entry->dump();
 
-    # write changed ldif
-    $ldif2->write_entry($entry);
+    # write changed entry
+    if ($mode eq "add"){
+	$ldif_out->write_entry($entry);
+    } elsif ($chng) {
+	print OUTLDIF "dn: $dn\n";
+	print OUTLDIF "changetype: modify\n";
+	print OUTLDIF $chng;
+	print OUTLDIF "\n";
+    }
 }
 
-$ldif->done();
-$ldif2->done();
+$ldif_in->done();
+
+if ($mode eq "add"){
+    $ldif_out->done();
+} else {
+    close(OUTLDIF);
+}
 
 
 



-------------------------------------------------------
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.