~ubuntu-branches/ubuntu/trusty/389-ds-base/trusty

« back to all changes in this revision

Viewing changes to ldap/admin/src/scripts/ldif2db.pl.in

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2013-08-30 00:31:55 UTC
  • mfrom: (1.2.1)
  • Revision ID: package-import@ubuntu.com-20130830003155-oimuzdqopkvp2cd0
Tags: 1.3.1.7-0ubuntu1
Sync from unreleased debian git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@perlexec@
 
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
# 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
 
33
# exception. 
 
34
 
35
 
36
# Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
37
# Copyright (C) 2013 Red Hat, Inc.
 
38
# All rights reserved.
 
39
# END COPYRIGHT BLOCK
 
40
#
 
41
 
 
42
use lib qw(@perlpath@);
 
43
use DSUtil;
 
44
 
 
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'}";
 
50
 
 
51
$maxidx = 50;
 
52
$noattrindexes = 0;
 
53
$mergechunksiz = 0;
 
54
$genuniqid = "time";
 
55
$i = 0;
 
56
$ldifi = 0;
 
57
$incli = 0;
 
58
$excli = 0;
 
59
$encrypt_on_import = 0;
 
60
 
 
61
sub usage {
 
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");
 
87
}
 
88
 
 
89
@ldiffiles = (
 
90
    "", "", "", "", "", "", "", "", "", "",
 
91
    "", "", "", "", "", "", "", "", "", "",
 
92
    "", "", "", "", "", "", "", "", "", "",
 
93
    "", "", "", "", "", "", "", "", "", "",
 
94
    "", "", "", "", "", "", "", "", "", "",
 
95
    ""
 
96
);
 
97
@included = (
 
98
    "", "", "", "", "", "", "", "", "", "",
 
99
    "", "", "", "", "", "", "", "", "", "",
 
100
    "", "", "", "", "", "", "", "", "", "",
 
101
    "", "", "", "", "", "", "", "", "", "",
 
102
    "", "", "", "", "", "", "", "", "", "",
 
103
    ""
 
104
);
 
105
@excluded = (
 
106
    "", "", "", "", "", "", "", "", "", "",
 
107
    "", "", "", "", "", "", "", "", "", "",
 
108
    "", "", "", "", "", "", "", "", "", "",
 
109
    "", "", "", "", "", "", "", "", "", "",
 
110
    "", "", "", "", "", "", "", "", "", "",
 
111
    ""
 
112
);
 
113
 
 
114
while ($i <= $#ARGV) {
 
115
    if ( "$ARGV[$i]" eq "-i" ) {    # ldiffiles
 
116
        $i++;
 
117
        if ($ldifi < $maxidx) {
 
118
            $ldiffiles[$ldifi] = $ARGV[$i]; $ldifi++;
 
119
        } else {
 
120
            &usage; exit(1);
 
121
        }
 
122
    } elsif ("$ARGV[$i]" eq "-s") {    # included suffix
 
123
        $i++;
 
124
        if ($incli < $maxidx) {
 
125
            $included[$incli] = $ARGV[$i]; $incli++;
 
126
        } else {
 
127
            &usage; exit(1);
 
128
        }
 
129
    } elsif ("$ARGV[$i]" eq "-x") {    # excluded suffix
 
130
        $i++;
 
131
        if ($excli < $maxidx) {
 
132
            $excluded[$excli] = $ARGV[$i]; $excli++;
 
133
        } else {
 
134
            &usage; exit(1);
 
135
        }
 
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
 
145
        $noattrindexes = 1;
 
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]" =~ /^-/)) {
 
150
            $i++;
 
151
            if ("$ARGV[$i]" eq "none") {
 
152
                $genuniqid = $ARGV[$i];
 
153
            } elsif ("$ARGV[$i]" eq "deterministic") {
 
154
                $genuniqid = $ARGV[$i];
 
155
            }
 
156
        }
 
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
 
162
        $verbose = 1;
 
163
    } elsif ("$ARGV[$i]" eq "-h") { # help
 
164
        &usage; exit(0);
 
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];
 
169
    } else {
 
170
        &usage; exit(1);
 
171
    }
 
172
    $i++;
 
173
}
 
174
 
 
175
#
 
176
# Gather all our config settings
 
177
#
 
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;
 
182
if ($verbose == 1){
 
183
    $info{args} = "-v -a";
 
184
} else {
 
185
    $info{args} = "-a";
 
186
}
 
187
if (($instance eq "" && $included[0] eq "") || $ldiffiles[0] eq "" ){
 
188
    &usage;
 
189
    exit(1);
 
190
}
 
191
 
 
192
 
193
# Construct the task entry
 
194
#
 
195
($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time);
 
196
$mn++; $yr += 1900;
 
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";
 
203
}
 
204
$i = 0;
 
205
$nsldiffiles = "";
 
206
while ("" ne "$ldiffiles[$i]") {
 
207
    $nsldiffiles = "${nsldiffiles}nsFilename: $ldiffiles[$i]\n";
 
208
    $i++;
 
209
}
 
210
$i = 0;
 
211
$nsincluded = "";
 
212
while ("" ne "$included[$i]") {
 
213
    $nsincluded = "${nsincluded}nsIncludeSuffix: $included[$i]\n";
 
214
    $i++;
 
215
}
 
216
$i = 0;
 
217
$nsexcluded = "";
 
218
while ("" ne "$excluded[$i]") {
 
219
    $nsexcluded = "${nsexcluded}nsExcludeSuffix: $excluded[$i]\n";
 
220
    $i++;
 
221
}
 
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"; 
 
228
$nsuniqidname = "";
 
229
if ($uniqidname ne "") { $nsuniqidname = "nsUniqueIdGeneratorNamespace: ${uniqidname}\n"; }
 
230
$entry = "${dn}${misc}${cn}${nsinstance}${nsincluded}${nsexcluded}${nsldiffiles}${nsnoattrindexes}${nsimportencrypt}${nsmergechunksiz}${nsgenuniqid}${nsuniqidname}";
 
231
 
 
232
$rc = DSUtil::ldapmod($entry, %info);
 
233
 
 
234
$dn =~ s/^dn: //;
 
235
$dn =~ s/\n//;
 
236
if($rc == 0){
 
237
    print "Successfully added task entry \"$dn\"\n";
 
238
} else {
 
239
    print "Failed to add task entry \"$dn\" error ($rc)\n";
 
240
}
 
241
 
 
242
exit($rc);
 
243