| [1376] | 1 | #!/usr/bin/perl | 
|---|
 | 2 | # | 
|---|
 | 3 | # BEGIN COPYRIGHT BLOCK | 
|---|
 | 4 | # This Program is free software; you can redistribute it and/or modify it under | 
|---|
 | 5 | # the terms of the GNU General Public License as published by the Free Software | 
|---|
 | 6 | # Foundation; version 2 of the License. | 
|---|
 | 7 | #  | 
|---|
 | 8 | # This Program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
 | 9 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
 | 10 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | 
|---|
 | 11 | #  | 
|---|
 | 12 | # You should have received a copy of the GNU General Public License along with | 
|---|
 | 13 | # this Program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
 | 14 | # Place, Suite 330, Boston, MA 02111-1307 USA. | 
|---|
 | 15 | #  | 
|---|
 | 16 | # Copyright (C) 2001 Sun Microsystems, Inc. Used by permission. | 
|---|
 | 17 | # Copyright (C) 2005 Red Hat, Inc. | 
|---|
 | 18 | # All rights reserved. | 
|---|
 | 19 | # END COPYRIGHT BLOCK | 
|---|
 | 20 | # | 
|---|
 | 21 |  | 
|---|
 | 22 | @instances = qw(userRoot); | 
|---|
 | 23 | @included = qw(); | 
|---|
 | 24 | @excluded = qw(); | 
|---|
 | 25 |  | 
|---|
 | 26 | our $nowrap = 1; # output LDIF is not folded | 
|---|
 | 27 | our $nobase64 = 0; # avoid base64 encoding | 
|---|
 | 28 | our $noversion = 0; # don't print version line | 
|---|
 | 29 | our $nouniqueid = 0; # don't export unique id | 
|---|
 | 30 | our $useid2entry = 0; # use main db file only | 
|---|
 | 31 | our $onefile = 1; # one file (MUST BE 1) | 
|---|
 | 32 | our $printkey = 1; # print key | 
|---|
 | 33 | our $ldiffile; # override LDIF output file location | 
|---|
 | 34 |  | 
|---|
 | 35 | $doreplica = 0; | 
|---|
 | 36 | $ldifdir = "/var/lib/dirsrv/slapd-scripts/ldif"; | 
|---|
 | 37 | $servid = "scripts"; | 
|---|
 | 38 | $verbose = 0; | 
|---|
 | 39 | $rootdn = "cn=Directory Manager"; | 
|---|
 | 40 | our $passwd; | 
|---|
 | 41 | our $passwdfile = "/etc/signup-ldap-pw"; | 
|---|
 | 42 | $i = 0; | 
|---|
 | 43 | $insti = 0; | 
|---|
 | 44 | $incli = 0; | 
|---|
 | 45 | $excli = 0; | 
|---|
 | 46 | $decrypt_on_export = 0; | 
|---|
 | 47 |  | 
|---|
 | 48 | foreach (@ARGV) { | 
|---|
 | 49 |     $verbose++ if ($_ eq "-v"); | 
|---|
 | 50 | } | 
|---|
 | 51 |  | 
|---|
 | 52 | if ((!@instances && !@included) || !$rootdn || !($passwd || $passwdfile)) { &usage; exit(1); } | 
|---|
 | 53 |  | 
|---|
 | 54 | ($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time); | 
|---|
 | 55 | $mn++; $yr += 1900; | 
|---|
 | 56 | $taskname = "export_${yr}_${mn}_${dy}_${h}_${m}_${s}"; | 
|---|
 | 57 | $dn = "dn: cn=$taskname, cn=export, cn=tasks, cn=config\n"; | 
|---|
 | 58 | $misc = "changetype: add\nobjectclass: top\nobjectclass: extensibleObject\n"; | 
|---|
 | 59 | $cn =  "cn: $taskname\n"; | 
|---|
 | 60 | $i = 0; | 
|---|
 | 61 | $be = ""; | 
|---|
 | 62 | $nsinstance = ""; | 
|---|
 | 63 | foreach my $instance (@instances) { | 
|---|
 | 64 |         $nsinstance .= "nsInstance: $instance\n"; | 
|---|
 | 65 |         if ( !$be ) { | 
|---|
 | 66 |                 $be = "$instance"; | 
|---|
 | 67 |         } else { | 
|---|
 | 68 |                 $be = "${be}-$instance"; | 
|---|
 | 69 |         } | 
|---|
 | 70 |         $i++; | 
|---|
 | 71 | } | 
|---|
 | 72 | $i = 0; | 
|---|
 | 73 | $nsincluded = ""; | 
|---|
 | 74 | foreach my $include (@included) { | 
|---|
 | 75 |         $nsincluded .= "nsIncludeSuffix: $include\n"; | 
|---|
 | 76 |         my ($rdn, $rest) = split(/,/, $include); | 
|---|
 | 77 |         my ($rest, $tmpbe) = split(/=/, $rdn); | 
|---|
 | 78 |         if ( !$be ) { | 
|---|
 | 79 |                 $be = "$tmpbe"; | 
|---|
 | 80 |         } else { | 
|---|
 | 81 |                 $be = "${be}-$tmpbe"; | 
|---|
 | 82 |         } | 
|---|
 | 83 |         $i++; | 
|---|
 | 84 | } | 
|---|
 | 85 | $i = 0; | 
|---|
 | 86 | $nsexcluded = ""; | 
|---|
 | 87 | foreach my $exclude (@excluded) { | 
|---|
 | 88 |         $nsexcluded .= "nsExcludeSuffix: $exclude\n"; | 
|---|
 | 89 |         $i++; | 
|---|
 | 90 | } | 
|---|
 | 91 | if ($ldiffile eq "") { | 
|---|
 | 92 |         if ($onefile == 0) { | 
|---|
 | 93 |                 $ldiffile = "${ldifdir}/${servid}-${yr}_${mn}_${dy}_${h}_${m}_${s}.ldif"; | 
|---|
 | 94 |         } else { | 
|---|
 | 95 |                 $ldiffile = "${ldifdir}/${servid}-${be}-${yr}_${mn}_${dy}_${h}_${m}_${s}.ldif"; | 
|---|
 | 96 |         } | 
|---|
 | 97 | } | 
|---|
 | 98 |  | 
|---|
 | 99 | $nsreplica = ""; | 
|---|
 | 100 | if ($doreplica != 0) { $nsreplica = "nsExportReplica: true\n"; } | 
|---|
 | 101 | $nsnobase64 = ""; | 
|---|
 | 102 | if ($nobase64 != 0) { $nsnobase64 = "nsMinimalEncoding: true\n"; } | 
|---|
 | 103 | $nsnowrap = ""; | 
|---|
 | 104 | if ($nowrap != 0) { $nsnowrap = "nsNoWrap: true\n"; } | 
|---|
 | 105 | $nsnoversion = ""; | 
|---|
 | 106 | if ($noversion != 0) { $nsnoversion = "nsNoVersionLine: true\n"; } | 
|---|
 | 107 | $nsnouniqueid = ""; | 
|---|
 | 108 | if ($nouniqueid != 0) { $nsnouniqueid = "nsDumpUniqId: false\n"; } | 
|---|
 | 109 | $nsuseid2entry = ""; | 
|---|
 | 110 | if ($useid2entry != 0) { $nsuseid2entry = "nsUseId2Entry: true\n"; } | 
|---|
 | 111 | $nsonefile = ""; | 
|---|
 | 112 | if ($onefile != 0) { $nsonefile = "nsUseOneFile: true\n"; } | 
|---|
 | 113 | if ($onefile == 0) { $nsonefile = "nsUseOneFile: false\n"; } | 
|---|
 | 114 | $nsexportdecrypt = ""; | 
|---|
 | 115 | if ($decrypt_on_export != 0) { $nsexportdecrypt = "nsExportDecrypt: true\n"; } | 
|---|
 | 116 | $nsprintkey = ""; | 
|---|
 | 117 | if ($printkey == 0) { $nsprintkey = "nsPrintKey: false\n"; } | 
|---|
 | 118 | $nsldiffile = "nsFilename: ${ldiffile}\n"; | 
|---|
 | 119 | $entry = "${dn}${misc}${cn}${nsinstance}${nsincluded}${nsexcluded}${nsreplica}${nsnobase64}${nsnowrap}${nsnoversion}${nsnouniqueid}${nsuseid2entry}${nsonefile}${nsexportdecrypt}${nsprintkey}${nsldiffile}"; | 
|---|
 | 120 | my @vstr = (); | 
|---|
 | 121 | if ($verbose != 0) { @vstr = ("-v"); } | 
|---|
 | 122 | my @qstr = ("-q"); | 
|---|
 | 123 | if ($verbose) { @qstr = (); } | 
|---|
 | 124 | $ENV{'PATH'} = "/usr/lib64/mozldap:/usr/bin:"; | 
|---|
 | 125 | print STDERR ("Exporting to ldif file: ${ldiffile}\n") if ($verbose); | 
|---|
 | 126 |  | 
|---|
 | 127 | my @pass; | 
|---|
 | 128 | if ($passwdfile) { | 
|---|
 | 129 |     @pass = ("-j", $passwdfile); | 
|---|
 | 130 | } elsif ($passwd) { | 
|---|
 | 131 |     @pass = ("-w", $passwd); | 
|---|
 | 132 | } | 
|---|
 | 133 |  | 
|---|
 | 134 | my @cmd = ("ldapmodify", @vstr, @qstr, qw(-h localhost -p 389), "-D", $rootdn, @pass, "-a"); | 
|---|
 | 135 |  | 
|---|
 | 136 | print STDERR "@cmd\n" if ($verbose); | 
|---|
 | 137 | print STDERR "$entry\n" if ($verbose); | 
|---|
 | 138 |  | 
|---|
 | 139 | open(FOO, "|-", @cmd) or die "Couldn't start ldapmodify: $!"; | 
|---|
 | 140 | print(FOO "$entry"); | 
|---|
 | 141 | close(FOO); | 
|---|
 | 142 |  | 
|---|
 | 143 | die "Couldn't successfully execute ldapmodify: $!" if $?; | 
|---|
 | 144 |  | 
|---|
 | 145 | my @statuscmd = ("ldapsearch", @vstr, qw(-h localhost -p 389), "-D", $rootdn, @pass, qw(-T -b cn=export,cn=tasks,cn=config), "cn=$taskname", qw(nstaskstatus nstaskexitcode)); | 
|---|
 | 146 |  | 
|---|
 | 147 | print STDERR "Status command: @statuscmd\n" if ($verbose); | 
|---|
 | 148 |  | 
|---|
 | 149 | my $exitstatus=255; | 
|---|
 | 150 |  | 
|---|
 | 151 | STATUS: while (1) { | 
|---|
 | 152 |     sleep(1); | 
|---|
 | 153 |     open(FOO, "-|", @statuscmd) or die "Couldn't start ldapsearch: $!"; | 
|---|
 | 154 |     while (<FOO>) { | 
|---|
 | 155 |         chomp; | 
|---|
 | 156 |         my ($key, $value) = split(": ", $_, 2); | 
|---|
 | 157 |         if ($key eq "nstaskstatus" && $verbose) { | 
|---|
 | 158 |             print STDERR "Status: $value\n"; | 
|---|
 | 159 |         } | 
|---|
 | 160 |         if ($key eq "nstaskexitcode") { | 
|---|
 | 161 |             $exitstatus = $value; | 
|---|
 | 162 |             last STATUS; | 
|---|
 | 163 |         } | 
|---|
 | 164 |     } | 
|---|
 | 165 |     close(FOO); | 
|---|
 | 166 | } | 
|---|
 | 167 |  | 
|---|
 | 168 | open(OUTPUT, "<", $ldiffile) or die "Couldn't open output file: $!"; | 
|---|
 | 169 | print while (<OUTPUT>); | 
|---|
 | 170 | close(OUTPUT); | 
|---|
 | 171 |  | 
|---|
 | 172 | exit $exitstatus; | 
|---|