ispman/scripts ldifupdate,1.13,1.14

Joerg Delker <[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-serv16029/scripts

Modified Files:
	ldifupdate 
Log Message:
simplified code by only using Net::LDAP::Ldif

Index: ldifupdate
===================================================================
RCS file: /cvsroot/ispman/ispman/scripts/ldifupdate,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- ldifupdate	5 Jun 2004 12:45:26 -0000	1.13
+++ ldifupdate	6 Jun 2004 09:05:54 -0000	1.14
@@ -35,58 +35,20 @@
 my ( %opts,    $dn,       $msg,   $chng, $mode );
 
 sub usage {
-    print "ISPMan ldifupdate\n";
-    print
-"A utility to update your ispman LDAP data according to current ISPMan schema.\n";
-    print
-      "It will convert the given input LDIF and output it to an output LDIF\n";
-    print "either in add or modify format.\n\n";
-    print "Syntax: ldifupdate <options>\n";
-    print "Options:\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";
-    my $values;
-    if ( ref $attrval eq "ARRAY" ) {
-        $values=$attrval;
-    }
-    else {
-        $values=[$attrval];
-    }
-
-    for (@$values) {
-        # encode value if it contains linebreaks 
-        if ( $_ =~ /\n/ ) {
-            my $code=MIME::Base64::encode($_, "\n ");
-            $ldif .= "${attr}:: " . $code . "\n";
-        }
-        else {
-            $ldif .= "$attr: $_\n";
-        }
-    }
+    print qq[
+ISPMan ldifupdate
 
-    $ldif .= "-\n";
+A utility to update your ispman LDAP data according to current ISPMan schema.
+It will convert the given input LDIF and output it to an output LDIF
+either in add or modify format.
 
-    # write ldif change to global var
-    $chng .= $ldif;
+Syntax: ldifupdate <options>
+Options:
+  -i <file>        input LDIF filename
+  -o <file>        output LDIF filename
+  -b <basedn>      base DN (optional), which limits changes to this subtree
+  -m <add/modify>  type of LDIF to create (default=add)
+];
 }
 
 sub update_oc {
@@ -99,14 +61,14 @@
     for (@$dst) {
         if ( !$in_src{$_} ) {
             $msg .= "adding objectclass: \"$_\"\n";
-            ldif_action( "add", "objectclass", $_ );
+            $entry->add( "objectclass", $_ );
         }
         $in_dst{$_} = 1;
     }
     for (@$src) {
         if ( !$in_dst{$_} ) {
             $msg .= "deleting objectclass: \"$_\"\n";
-            ldif_action( "delete", "objectclass", $_ );
+            $entry->delete( "objectclass" => [$_] );
         }
     }
 
@@ -135,12 +97,8 @@
 $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";
-}
+$ldif_out =
+  Net::LDAP::LDIF->new( $opts{'o'}, "w", 'change' => ( $mode eq "modify" ) );
 
 # only work on entries of this basedn
 $basedn = $opts{'b'};
@@ -156,7 +114,7 @@
         print STDERR "Error lines:\n", $ldif_in->error_lines(), "\n";
     }
 
-    $entry->changetype("add");
+    $entry->changetype($mode);
     $dn = $entry->dn();
 
     next unless !defined($basedn) or $dn =~ m/$basedn$/;
@@ -175,7 +133,7 @@
         # if there is only one objectclass: top
         # replace with ispmanBranch
         $msg .= "replacing objectclass: \"top\" -> \"ispmanBranch\"\n";
-        ldif_action( "replace", "objectclass", "ispmanBranch" );
+        $entry->replace( "objectclass", "ispmanBranch" );
 
     }
     elsif ( $dn =~ m/^ou=/ && $oc[0] ne "ispmanBranch" ) {
@@ -183,7 +141,7 @@
         # if entry starts with "ou=" replace
         # objectclasses top, organizationalunit with ispmanBranch
         $msg .= "replacing objectclass: -> \"ispmanBranch\"\n";
-        ldif_action( "replace", "objectclass", "ispmanBranch" );
+        $entry->replace( "objectclass", "ispmanBranch" );
 
     }
     elsif ( $oc{"ispmanDomain"} ) {
@@ -204,8 +162,7 @@
                     foreach (@default_fileserver) {
                         $msg .=
                           "adding ispmanDomainDefaultFileServer: \"$_\"\n";
-                        ldif_action( "add", "ispmanDomainDefaultFileServer",
-                            $_ );
+                        $entry->add( "ispmanDomainDefaultFileServer", $_ );
                     }
                 }
                 else {
@@ -220,7 +177,7 @@
                 if (@default_webhost) {
                     foreach (@default_webhost) {
                         $msg .= "adding ispmanDomainWebHost: \"$_\"\n";
-                        ldif_action( "add", "ispmanDomainDefaultWebHost", $_ );
+                        $entry->add( "ispmanDomainDefaultWebHost", $_ );
                     }
                 }
                 else {
@@ -247,11 +204,11 @@
         my $cn = join ".", ( $ispmanVhostName, $ispmanDomain );
         if ( !defined $lcn ) {
             $msg .= "adding cn with \"$cn\"\n";
-            ldif_action( "add", "cn", $cn );
+            $entry->add( "cn", $cn );
         }
         elsif ( $lcn ne $cn ) {
             $msg .= "changing cn: \"$lcn\" -> \"$cn\"\n";
-            ldif_action( "replace", "cn", $cn );
+            $entry->replace( "cn", $cn );
         }
 
         # checking vhost uid attribute
@@ -259,25 +216,25 @@
         my $uid = join ".", ( $ispmanVhostName, $ispmanDomain );
         if ( !defined $luid ) {
             $msg .= "adding uid with \"$uid\"\n";
-            ldif_action( "add", "uid", $uid );
+            $entry->add( "uid", $uid );
         }
         elsif ( $luid ne $uid ) {
             $msg .= "changing uid: \"$luid\" -> \"$uid\"\n";
-            ldif_action( "replace", "uid", $uid );
+            $entry->replace( "uid", $uid );
         }
 
         # checking vhost uidNumber attribute
         unless ( $entry->get_value("uidNumber") ) {
             $msg .=
 "adding dummy uidNumber: $dummyUid (This should be replaced later!\n)";
-            ldif_action( "add", "uidNumber", $dummyUid );
+            $entry->add( "uidNumber", $dummyUid );
         }
 
         # checking vhost gidNumber attribute
         unless ( $entry->get_value("gidNumber") ) {
             $msg .=
 "adding dummy gidNumber: $dummyGid (This should be replaced later!)\n";
-            ldif_action( "add", "gidNumber", $dummyGid );
+            $entry->add( "gidNumber", $dummyGid );
         }
 
         # checking vhost homeDirectory attribute
@@ -285,14 +242,14 @@
           ( $default_domainHomedir, $ispmanDomain, 'vhosts', $ispmanVhostName );
         unless ( $entry->get_value("homeDirectory") ) {
             $msg .= "adding homeDirectory: \"$homedir\"\n";
-            ldif_action( "add", "homeDirectory", $homedir );
+            $entry->add( "homeDirectory", $homedir );
         }
 
         # checking vhost userPassword attribute
         unless ( $entry->get_value("userPassword") ) {
             $msg .=
 "adding userPassword: \"$dummyPassword\" (This should be replaced later!)\n";
-            ldif_action( "add", "userPassword", $dummyPassword );
+            $entry->add( "userPassword", $dummyPassword );
         }
 
         # checking vhost ispmanVhostDocumentRoot
@@ -304,8 +261,7 @@
                 my ($fixeddocroot) = $docroot =~ m/^$ispmanVhostName\/(.*)/;
                 $msg .=
 "changing ispmanVhostDocumentRoot: \"$docroot\" -> \"$fixeddocroot\"\n";
-                ldif_action( "replace", "ispmanVhostDocumentRoot",
-                    $fixeddocroot );
+                $entry->replace( "ispmanVhostDocumentRoot", $fixeddocroot );
             }
         }
         else {
@@ -321,7 +277,7 @@
                 my ($fixedscdir) = $scdir =~ m/^$ispmanVhostName\/(.*)/;
                 $msg .=
 "changing ispmanVhostScriptDir: \"$scdir\" -> \"$fixedscdir\"\n";
-                ldif_action( "replace", "ispmanVhostScriptDir", $fixedscdir );
+                $entry->replace( "ispmanVhostScriptDir", $fixedscdir );
             }
         }
         else {
@@ -336,13 +292,13 @@
                 my ($fixedlogdir) = $logdir =~ m/^$ispmanVhostName\/(.*)/;
                 $msg .=
 "changing ispmanVhostLogdir: \"$logdir\" -> \"$fixedlogdir\"\n";
-                ldif_action( "replace", "ispmanVhostLogdir", $fixedlogdir );
+                $entry->replace( "ispmanVhostLogdir", $fixedlogdir );
             }
         }
         else {
             $msg .= "WARNING: ispmanVhostLogdir is missing!\n";
             $msg .= "adding ispmanVhostLogdir: \"logs\"\n";
-            ldif_action( "add", "ispmanVhostLogdir", "logs" );
+            $entry->add( "ispmanVhostLogdir", "logs" );
         }
 
         # checking vhost ispmanVhostStatdir attribute
@@ -353,29 +309,30 @@
                 my ($fixedstatdir) = $statdir =~ m/^$ispmanVhostName\/(.*)/;
                 $msg .=
 "changing ispmanVhostStatdir: \"$statdir\" -> \"$fixedstatdir\"\n";
-                ldif_action( "replace", "ispmanVhostStatdir", $fixedstatdir );
+                $entry->replace( "ispmanVhostStatdir", $fixedstatdir );
             }
         }
         else {
             $msg .= "WARNING: ispmanVhostStatdir is missing!\n";
             $msg .= "adding ispmanVhostStatdir: \"stats\"\n";
-            ldif_action( "add", "ispmanVhostStatdir", "stats" );
+            $entry->add( "ispmanVhostStatdir", "stats" );
         }
 
         # checking vhost loginShell
         unless ( $entry->get_value("loginshell") ) {
             $msg .= "adding loginShell: $default_vhostShell\n";
-            ldif_action( "add", "loginshell", $default_vhostShell );
+            $entry->add( "loginshell", $default_vhostShell );
         }
 
         # checking ispmanVhostExtraConf
         my $extraconf = $entry->get_value("ispmanVhostExtraConf");
-        if ( $extraconf && $extraconf =~ /<.*>/) {
-            my $codedconf=HTML::Entities::encode($extraconf);
+        if ( $extraconf && $extraconf =~ /<.*>/ ) {
+            my $codedconf = HTML::Entities::encode($extraconf);
             $msg .= "NOTICE: ispmanVhostExtraConf is not HTML encoded!\n";
             $msg .= "changing: ispmanVhostExtraConf\n";
-            ldif_action( "replace", "ispmanVhostExtraConf", $codedconf );
+            $entry->replace( "ispmanVhostExtraConf", $codedconf );
         }
+
     }
     elsif ( $oc{'ispmanDomainUser'} ) {
 
@@ -389,7 +346,7 @@
         }
         if ( !$hasAlternateUid ) {
             $msg .= "adding uid: \"$uid\"\n";
-            ldif_action( "add", "uid", $uid );
+            $entry->add( "uid", $uid );
         }
 
         # checking for bogus mailHost
@@ -397,7 +354,7 @@
             && $entry->get_value("mailHost") eq "undefined" )
         {
             $msg .= "deleting mailhost: undefined\n";
-            ldif_action( "delete", "mailhost", "undefined" );
+            $entry->delete( "mailhost" => ["undefined"] );
         }
 
         # checking for bogus fileHost
@@ -405,13 +362,13 @@
             && $entry->get_value("fileHost") eq "undefined" )
         {
             $msg .= "deleting filehost: undefined\n";
-            ldif_action( "delete", "filehost", "undefined" );
+            $entry->delete( "filehost" => ["undefined"] );
         }
 
         # checking loginShell
         unless ( $entry->get_value("loginshell") ) {
             $msg .= "adding default loginShell: $default_loginShell\n";
-            ldif_action( "add", "loginshell", $default_loginShell );
+            $entry->add( "loginshell", $default_loginShell );
         }
 
     }
@@ -432,7 +389,7 @@
     }
     if ( !$found ) {
         $msg .= "adding RDN to attributes ($attrib: $attribval)\n";
-        ldif_action( "add", $attrib, $attribval );
+        $entry->add( $attrib, $attribval );
     }
 
     # output msg
@@ -441,30 +398,13 @@
         print STDERR $msg, "\n";
     }
 
-    #print $entry->dn(), "\n";
-    #print $entry->dump(), "\n";
-    #$entry->dump();
-
-    # 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";
-    }
+    # write ldif data
+    $ldif_out->write_entry($entry);
 }
 
 $ldif_in->done();
 
-if ( $mode eq "add" ) {
-    $ldif_out->done();
-}
-else {
-    close(OUTLDIF);
-}
+$ldif_out->done();
 
 __END__
 



-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
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.