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.
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.
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.
16
# In addition, as a special exception, Red Hat, Inc. gives You the additional
17
# right to link the code of this Program with code not covered under the GNU
18
# General Public License ("Non-GPL Code") and to distribute linked combinations
19
# including the two, subject to the limitations in this paragraph. Non-GPL Code
20
# permitted under this exception must only link to the code of this Program
21
# through those well defined interfaces identified in the file named EXCEPTION
22
# found in the source code files (the "Approved Interfaces"). The files of
23
# Non-GPL Code may instantiate templates or use macros or inline functions from
24
# the Approved Interfaces without causing the resulting work to be covered by
25
# the GNU General Public License. Only Red Hat, Inc. may make changes or
26
# additions to the list of Approved Interfaces. You must obey the GNU General
27
# Public License in all respects for all of the Program code and other code used
28
# in conjunction with the Program except the Non-GPL Code covered by this
29
# exception. If you modify this file, you may extend this exception to your
30
# version of the file, but you are not obligated to do so. If you do not wish to
31
# provide this exception without modification, you must delete this exception
32
# statement from your version and license this file solely under the GPL without
36
# Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
37
# Copyright (C) 2013 Red Hat, Inc.
38
# All rights reserved.
42
use lib qw(@perlpath@);
45
DSUtil::libpath_add("@nss_libdir@");
46
DSUtil::libpath_add("/usr/lib");
47
DSUtil::libpath_add("/usr/lib64");
48
$ENV{'PATH'} = "@ldaptool_bindir@:/usr/bin:/usr/lib64/mozldap/";
49
$ENV{'SHLIB_PATH'} = "$ENV{'LD_LIBRARY_PATH'}";
59
$encrypt_on_import = 0;
62
print(STDERR "Usage: ldif2db.pl -n backend [-Z serverID] [-D rootdn] { -w password | -w - | -j filename }\n");
63
print(STDERR " [-P protocol] {-s include}* [{-x exclude}*] [-O] [-c chunksize] [-v] [-h]\n");
64
print(STDERR " [-E] [-g [string] [-G namespace_id]] {-i filename}*\n");
65
print(STDERR "Note: either \"-n backend\", \"-s includesuffix\", and \"-i ldiffile\" are required.\n");
66
print(STDERR "Options:\n");
67
print(STDERR " -D rootdn - Directory Manager\n");
68
print(STDERR " -w password - Directory Manager's password\n");
69
print(STDERR " -w - - Prompt for Directory Manager's password\n");
70
print(STDERR " -Z serverID - Server instance identifier\n");
71
print(STDERR " -j filename - Read Directory Manager's password from file\n");
72
print(STDERR " -n backend - Backend database name. Example: userRoot\n");
73
print(STDERR " -i filename - Input ldif file(s)\n");
74
print(STDERR " -s include - Included suffix\n");
75
print(STDERR " -x exclude - Excluded suffix(es)\n");
76
print(STDERR " -O - Only create core db, no attr indexes\n");
77
print(STDERR " -c chunksize - Number of entries to process before starting a new pass\n");
78
print(STDERR " -g [string] - String is \"none\" or \"deterministic\"\n");
79
print(STDERR " \"none\" - unique id is not generated\n");
80
print(STDERR " \"deterministic\" - generate name based unique id (-G name)\n");
81
print(STDERR " By default - generate time based unique id\n");
82
print(STDERR " -G name - Namespace id for name based uniqueid (-g deterministic)\n");
83
print(STDERR " -E - Encrypt data when importing\n");
84
print(STDERR " -P protocol - STARTTLS, LDAPS, LDAPI, LDAP (default: uses most secure protocol available)\n");
85
print(STDERR " -v - Verbose output\n");
86
print(STDERR " -h - Display usage\n");
90
"", "", "", "", "", "", "", "", "", "",
91
"", "", "", "", "", "", "", "", "", "",
92
"", "", "", "", "", "", "", "", "", "",
93
"", "", "", "", "", "", "", "", "", "",
94
"", "", "", "", "", "", "", "", "", "",
98
"", "", "", "", "", "", "", "", "", "",
99
"", "", "", "", "", "", "", "", "", "",
100
"", "", "", "", "", "", "", "", "", "",
101
"", "", "", "", "", "", "", "", "", "",
102
"", "", "", "", "", "", "", "", "", "",
106
"", "", "", "", "", "", "", "", "", "",
107
"", "", "", "", "", "", "", "", "", "",
108
"", "", "", "", "", "", "", "", "", "",
109
"", "", "", "", "", "", "", "", "", "",
110
"", "", "", "", "", "", "", "", "", "",
114
while ($i <= $#ARGV) {
115
if ( "$ARGV[$i]" eq "-i" ) { # ldiffiles
117
if ($ldifi < $maxidx) {
118
$ldiffiles[$ldifi] = $ARGV[$i]; $ldifi++;
122
} elsif ("$ARGV[$i]" eq "-s") { # included suffix
124
if ($incli < $maxidx) {
125
$included[$incli] = $ARGV[$i]; $incli++;
129
} elsif ("$ARGV[$i]" eq "-x") { # excluded suffix
131
if ($excli < $maxidx) {
132
$excluded[$excli] = $ARGV[$i]; $excli++;
136
} elsif ("$ARGV[$i]" eq "-n") { # instance
137
$i++; $instance = $ARGV[$i];
138
} elsif ("$ARGV[$i]" eq "-D") { # Directory Manager
139
$i++; $rootdn = $ARGV[$i];
140
} elsif ("$ARGV[$i]" eq "-w") { # Directory Manager's password
141
$i++; $passwd = $ARGV[$i];
142
} elsif ("$ARGV[$i]" eq "-j") { # Read Directory Manager's password from a file
143
$i++; $passwdfile = $ARGV[$i];
144
} elsif ("$ARGV[$i]" eq "-O") { # no attr indexes
146
} elsif ("$ARGV[$i]" eq "-c") { # merge chunk size
147
$i++; $mergechunksiz = $ARGV[$i];
148
} elsif ("$ARGV[$i]" eq "-g") { # generate uniqueid
149
if (("$ARGV[$i+1]" ne "") && !("$ARGV[$i+1]" =~ /^-/)) {
151
if ("$ARGV[$i]" eq "none") {
152
$genuniqid = $ARGV[$i];
153
} elsif ("$ARGV[$i]" eq "deterministic") {
154
$genuniqid = $ARGV[$i];
157
} elsif ("$ARGV[$i]" eq "-G") { # namespace id
158
$i++; $uniqidname = $ARGV[$i];
159
} elsif ("$ARGV[$i]" eq "-Z") { # server id
160
$i++; $servid = $ARGV[$i];
161
} elsif ("$ARGV[$i]" eq "-v") { # verbose
163
} elsif ("$ARGV[$i]" eq "-h") { # help
165
} elsif ("$ARGV[$i]" eq "-E") { # encrypt on import
166
$encrypt_on_import = 1;
167
} elsif ("$ARGV[$i]" eq "-P") { # protocol preference
168
$i++; $protocol = $ARGV[$i];
176
# Gather all our config settings
178
($servid, $confdir) = DSUtil::get_server_id($servid, "@initconfigdir@");
179
%info = DSUtil::get_info($confdir, $host, $port, $rootdn);
180
$info{rootdnpw} = DSUtil::get_password_from_file($passwd, $passwdfile);
181
$info{protocol} = $protocol;
183
$info{args} = "-v -a";
187
if (($instance eq "" && $included[0] eq "") || $ldiffiles[0] eq "" ){
193
# Construct the task entry
195
($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time);
197
$taskname = "import_${yr}_${mn}_${dy}_${h}_${m}_${s}";
198
$dn = "dn: cn=$taskname, cn=import, cn=tasks, cn=config\n";
199
$misc = "objectclass: top\nobjectclass: extensibleObject\n";
200
$cn = "cn: $taskname\n";
201
if ($instance ne "") {
202
$nsinstance = "nsInstance: ${instance}\n";
206
while ("" ne "$ldiffiles[$i]") {
207
$nsldiffiles = "${nsldiffiles}nsFilename: $ldiffiles[$i]\n";
212
while ("" ne "$included[$i]") {
213
$nsincluded = "${nsincluded}nsIncludeSuffix: $included[$i]\n";
218
while ("" ne "$excluded[$i]") {
219
$nsexcluded = "${nsexcluded}nsExcludeSuffix: $excluded[$i]\n";
222
$nsnoattrindexes = "";
223
if ($noattrindexes != 0) { $nsnoattrindexes = "nsImportIndexAttrs: false\n"; }
224
$nsimportencrypt = "";
225
if ($encrypt_on_import != 0) { $nsimportencrypt = "nsImportEncrypt: true\n"; }
226
$nsmergechunksiz = "nsImportChunkSize: ${mergechunksiz}\n";
227
$nsgenuniqid = "nsUniqueIdGenerator: ${genuniqid}\n";
229
if ($uniqidname ne "") { $nsuniqidname = "nsUniqueIdGeneratorNamespace: ${uniqidname}\n"; }
230
$entry = "${dn}${misc}${cn}${nsinstance}${nsincluded}${nsexcluded}${nsldiffiles}${nsnoattrindexes}${nsimportencrypt}${nsmergechunksiz}${nsgenuniqid}${nsuniqidname}";
232
$rc = DSUtil::ldapmod($entry, %info);
237
print "Successfully added task entry \"$dn\"\n";
239
print "Failed to add task entry \"$dn\" error ($rc)\n";