This file is indexed.

/usr/share/dirsrv/updates/80upgradednformat.pl is in 389-ds-base 1.3.7.10-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use Mozilla::LDAP::Conn;
use Mozilla::LDAP::Utils qw(normalizeDN);
use Mozilla::LDAP::API qw(:constant ldap_url_parse ldap_explode_dn);
use File::Basename;
use File::Copy;
use DSUpdate qw(isOffline);

# Upgrade DN format if needed.
# For each backend instance, 
#     run upgradednformat with -N (dryrun mode),
#     if it returns 0 (Upgrade candidates are found), 
#     recursively copy the instance dir to the work dir (dnupgrade)
#     run upgradednformat w/o -N against the DB in the work dir
#     if it went ok, replace the original instance dir with the work dir.
# Note: This script does nothing if the server is up.
sub runinst {
    my ($inf, $inst, $dseldif, $conn) = @_;

    my @errs;

    my $config = "cn=config";
    my $config_entry = $conn->search($config, "base", "(cn=*)");
    if (!$config_entry) {
        return ("error_no_configuration_entry", $!);
    }

    # Check if the server is up or not
    my $rc;
    ($rc, @errs) = isOffline($inf, $inst, $conn);
    if (!$rc) {
        return @errs;
    }
    my $mappingtree = "cn=mapping tree,cn=config";
    my $ldbmbase = "cn=ldbm database,cn=plugins,cn=config";

    my $backend_entry;
    my $mtentry = $conn->search($mappingtree, "onelevel", "(cn=*)", 0, @attr);
    if (!$mtentry) {
        return ("error_no_mapping_tree_entries", $!);
    }

    my $db_config_entry = 
            $conn->search("cn=config,cn=ldbm database,cn=plugins,cn=config",
            "base", "(objectclass=*)");
    if (!$db_config_entry) {
        return ('error_finding_config_entry',
                'cn=config,cn=ldbm database,cn=plugins,cn=config',
                $conn->getErrorString());
    }
    # If subtree rename swich is not found in the config file,
    # set off to the switch and upgrade dn format using entrydn.
    my $switch = $db_config_entry->getValues('nsslapd-subtree-rename-switch');
    if ("" eq $switch) { 
        $db_config_entry->addValue('nsslapd-subtree-rename-switch', "off");
        $conn->update($db_config_entry);
    }

    # If a suffix in the mapping tree is doube-quoted and 
    # the cn value has only the double-quoted value, e.g.
    #   dn: cn="dc=example,dc=com",cn=mapping tree,cn=config
    #   cn: "dc=example,dc=com"
    # the following code adds non-quoted value:
    #   cn: dc=example,dc=com
    while ($mtentry) {
        my $numvals = $mtentry->size("cn");
        my $i;
        my $withquotes = -1;
        my $noquotes = -1;
        for ($i = 0; $i < $numvals; $i++) {
            if ($mtentry->{"cn"}[$i] =~ /^".*"$/) {
                $withquotes = $i;
            } else {
                $noquotes = $i;
            }
        }
        if ($withquotes >= 0 && $noquotes == -1) {
            # Has only cn: "<suffix>"
            # Adding cn: <suffix>
            my $stripped = $mtentry->{"cn"}[$withquotes];
            $stripped =~ s/^"(.*)"$/$1/;
            $mtentry->addValue("cn", $stripped);
            $conn->update($mtentry);
        }
        $mtentry = $conn->nextEntry();
    }

    my $ldifdir = $config_entry->{"nsslapd-ldifdir"}[0];
    my $instancedir = $config_entry->{"nsslapd-instancedir"}[0];
    my ($slapd, $serverID) = split(/-/, $instancedir, 2);
    my $upgradednformat = "/usr/sbin/upgradednformat -Z $serverID";
    my $reindex = "/usr/sbin/db2index -Z $serverID";

    # Scan through all of the backends to see if any of them
    # contain escape characters in the DNs.  If we find any
    # escapes, we need to run the conversion tool on that
    # backend.
    $backend_entry = $conn->search($ldbmbase, "onelevel", "(objectClass=nsBackendInstance)", 0, @attr);
    if (!$backend_entry) {
        return ("error_no_backend_entries", $!);
    }

    while ($backend_entry) {
        my $backend = $backend_entry->{"cn"}[0];
        my $dbinstdir = $backend_entry->{"nsslapd-directory"}[0];
        my $workdir = $dbinstdir . "/dnupgrade";
        my $dbdir = dirname($dbinstdir);
        my $pdbdir = dirname($dbdir);
        my $instname = basename($dbinstdir);
        my $dn_norm_sp_txt = $ldifdir . "/" . $instname . "_dn_norm_sp.txt";

        if ("$dbdir" eq "" || "$instname" eq "") {
            push @errs, ["error_invalid_dbinst_dir", $dbinstdir];
            return @errs;
        }

        # clean up db region files, which might contain the old pages
        if ( -d $dbdir  && -f $dbdir."/__db.001") {
            unlink <$dbdir/__db.*>;
        }

        my $escapes = 0;
        my $rc = 0;
        my $cmd = 0;
        if ((-e "$dbinstdir/id2entry.db") || (-e "$dbinstdir/id2entry.db4")) {
            # Check if any DNs contain escape characters with dbscan.
            # dryrun mode
            # return values:  0 -- need to upgrade dn format
            #                 1 -- no need to upgrade dn format
            #                -1 -- error
            $cmd = "$upgradednformat -n $backend -a $dbinstdir -N";
            $rc = system("$cmd");
            if ($rc & 127) {
                push @errs, [ 'error_running_command', $cmd, $rc, $! ];
                return @errs;
            }
            $escapes = $rc >> 8;
# $escapes == 0 ==> no need to do dn upgrade
# $escapes == 1 ==> need to do dn upgrade (both dn upgrade and spaces)
# $escapes == 2 ==> need to do dn upgrade (dn upgrade only)
# $escapes == 3 ==> need to do dn upgrade (dn upgrade spaces only)
            if ($escapes <= 0) {
                # already upgraded or an error occurred.
                # check ancestorid to see if it has not-sorted ID list or not.
                my $ancestorid = $dbinstdir . "/ancestorid.db4";
                if (!(-e "$ancestorid")) {
                    $ancestorid = $dbinstdir . "/ancestorid.db";
                }
                if (-e "$ancestorid") {
                    my $disorder = 0;
                    open(ANCESTOR, "/usr/bin/dbscan -f $ancestorid -r |");
                    while (<ANCESTOR>) {
                        if (!/^=[0-9]*/) {
                            chomp($_);
                            my @IDs = split(/ |	/, $_);
                            # print "ID count: $#IDs\n";
                            my $lasti = $#IDs;
                            for (my $i = 1; $i < $lasti; $i++) {
                                if ($IDs[$i] >= $IDs[$i + 1]) {
                                    $disorder = 1;
                                    last;
                                }
                            }
                            # print "Result: $disorder \n";
                            if ($disorder) {
                                last;
                            }
                        }
                    }
                    close(ANCESTOR);

                    # ancestorid index is in disorder; need to reindex it.
                    if ($disorder) {
                        print "The ancestorid index in $backend is in disorder; Reindexing $ancestorid.\n";
                        $cmd = "$reindex -n $backend -t ancestorid";
                        $rc = system("$cmd");
                        if ($rc & 127) {
                            push @errs, [ 'error_running_command', $cmd, $rc, $! ];
                            return @errs;
                        }
                    }
                }
            } else {
                # need to upgrade dn format
                $rc = 0;

                if (system("cd $pdbdir; tar cf - db/DBVERSION | (cd $dbinstdir; tar xf -)") ||
                    system("cd $pdbdir; tar cf - db/$instname/DBVERSION | (cd $dbinstdir; tar xf -)") ||
                    system("cd $pdbdir; tar cf - db/$instname/*.db* | (cd $dbinstdir; tar xf -)")) {
                    push @errs, [ "error_cant_backup_db", $backend, $! ];
                    return @errs;
                }
                my @stat = stat("$dbdir");
                my $mode = $stat[2];
                my $uid = $stat[4];
                my $gid = $stat[5];

                move("$dbinstdir/db", "$workdir");
                chmod($mode, $workdir);
                chown($uid, $gid, $workdir);

                @stat = stat("$dbinstdir");
                $mode = $stat[2];
                $uid = $stat[4];
                $gid = $stat[5];

                chmod($mode, "$workdir/$instname");
                chown($uid, $gid, "$workdir/$instname");

                my $do_dn_norm_sp = 0;
                if ((1 == $escapes) || (3 == $escapes)) {
                    # We are taking care of spaces in DN.
                    my $sorted = $dn_norm_sp_txt . ".sorted";
                    $cmd = "sort $dn_norm_sp_txt";
                    $rc = system("$cmd > $sorted");
                    if ($rc) {
                        debug(1, "Error: $cmd failed - output $sorted: $!\n");
                        push @errs, [ 'error_running_command', $cmd, $rc, $! ];
                        return @errs;
                    }
                    # Create a work file from $sorted.
                    my $workfile = $sorted . ".work";
                    # print "$sorted -> $workfile\n";

                    open(SORTEDFILE, "$sorted");
                    open(WORKFILE, "> $workfile");
                    my $prev_dn = "";
                    my $prev_id = 0;
                    my $new = 1;
                    for (my $line = <SORTEDFILE>; $line; $line = <SORTEDFILE>) {
                        chomp($line);
                        my ($dn, $id) = split(":", $line, 2);
                        if ($dn eq $prev_dn) {
                            if ($new == 1) {
                                print WORKFILE "$prev_id:$id";
                                $new = 0;
                                $do_dn_norm_sp = 1; # go ahead and fix it.
                            } else {
                                print WORKFILE " $id";
                            }
                        } else {
                            if (0 == $new) {
                                print WORKFILE "\n";
                                $new = 1;
                            }
                        }
                        $prev_dn = $dn;
                        $prev_id = $id;
                    }
                    close(WORKFILE);
                    close(SORTEDFILE);
                    unlink <$sorted>;
                    move("$dn_norm_sp_txt", "$dn_norm_sp_txt.orig");
                    if ($do_dn_norm_sp) {
                        move("$workfile", "$dn_norm_sp_txt");
                        # otherwise, we don't need $dn_norm_sp_txt
                    }
                } else {
                    unlink <$dn_norm_sp_txt>;
                }

                if ((1 == $escapes) || (2 == $escapes) || (3 == $escapes)) {
                    # call conversion tool here and get return status.
                    $cmd = "$upgradednformat -n $backend -a $workdir/$instname";
                    $rc = system("$cmd");
                    if ($rc & 127) {
                        push @errs, [ 'error_running_command', $cmd, $rc, $! ];
                        return @errs;
                    }
                    $escapes = $rc >> 8;
                }
                if ((0 == $rc) || (1 == $escapes) || (3 == $escapes)) {
                    # success
                    move("$dbinstdir", "$dbinstdir.orig");
                    move("$dbinstdir.orig/dnupgrade/$instname", "$dbinstdir");
                    copy("$dbinstdir.orig/dnupgrade/DBVERSION", "$dbdir");
                    if ((1 == $escapes) || (3 == $escapes)) {
                        $cmd = "$reindex -n $backend -t entryrdn";
                        $rc = system("$cmd");
                        if ($rc & 127) {
                            push @errs, [ 'error_running_command', $cmd, $rc, $! ];
                            return @errs;
                        }
                    }
                    my $dn_norm_sp_txt = $ldifdir . "/" . $instname . "_dn_norm_sp.txt";
                    my $conflict = $ldifdir . "/" . $instname . "_conflict.txt";
                    system("echo prinary entry ID: duplicated entry IDs > $conflict");
                    system("cat $dn_norm_sp_txt >> $conflict");
                    print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
                    print "Duplicated DN(s) were found and renamed.\n";
                    print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
                    print "Renamed entry IDs are listed in $conflict.\n";
                } else {
                    # Conversion failed. Cleanup and bail.
                    unlink <$dbinstdir/dnupgrade/$backend/*>;
                    rmdir("$dbinstdir/dnupgrade/$backend");
                    unlink <$dbinstdir/dnupgrade/*>;
                    rmdir("$dbinstdir/dnupgrade");
                    return ("error_cant_convert_db", $backend, $rc);
                }
            }
        }

        $backend_entry = $conn->nextEntry();
    }

    return ();
}