fusiondirectory-insert-schema 12.6 KB
Newer Older
1
#!/usr/bin/perl
2

3
########################################################################
4
#
5
# fusiondirectory-insert-schema
6
#
7
# Manipulate and insert the needed schema into the ldap server
8
#
9
10
#  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
#  Copyright (C) 2011-2015  FusionDirectory
11
#
12
13
14
15
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
16
#
17
18
19
20
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
21
#
22
23
24
25
26
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
########################################################################
27

28
use strict;
29
30
31
use warnings;

use 5.008;
32

33
34
35
36
my $ldap_host_options = '-Y EXTERNAL -H ldapi:///';
my $path              = "/etc/ldap/schema/fusiondirectory/";
my $full_cmd          = "";
my $schema2ldif       = "schema2ldif";
37

38
my $listschemas = 0;
39
my $modify      = 0;
40
my $schemalist  = 0;
41
my $yes_flag    = 0;
42
my $pathunset   = 1;
43
my $continue    = 0;
44
my @schemas     = ();
45
my @gen_files   = ();
46
my $delete_ldif = 1;
47
foreach my $arg ( @ARGV ) {
48
49
50
  if (not defined $ldap_host_options) {
    $ldap_host_options = $arg;
  } elsif ((lc($arg) eq '-i') || (lc($arg) eq '--insert')) {
51
52
53
    if ($schemalist) {
      usage();
    }
54
    $schemalist = 1;
55
56
  } elsif ((lc($arg) eq '-l') || (lc($arg) eq '--list')) {
    $listschemas = 1;
57
58
  } elsif ((lc($arg) eq '-c') || (lc($arg) eq '--continue')) {
    $continue = 1;
59
60
61
62
63
64
  } elsif ((lc($arg) eq '-e') || (lc($arg) eq '--empty')) {
    if ($schemalist) {
      usage();
    }
    $modify = 2;
    $schemalist = 1;
65
66
67
68
69
70
  } elsif ((lc($arg) eq '-m') || (lc($arg) eq '--modify')) {
    if ($schemalist) {
      usage();
    }
    $modify = 1;
    $schemalist = 1;
71
72
  } elsif ((lc($arg) eq '-o') || (lc($arg) eq '--options')) {
    undef $ldap_host_options;
73
74
  } elsif ((lc($arg) eq '-n') || (lc($arg) eq '--nodelete')) {
    $delete_ldif = 0;
75
76
  } elsif ((lc($arg) eq '-y') || (lc($arg) eq '--yes')) {
    $yes_flag = 1;
77
  } elsif ((lc($arg) eq '-h') || (lc($arg) eq '--help')) {
78
79
80
81
    usage();
  } elsif ($schemalist) {
    if ($arg =~ /(.*).ldif/) { # ignore ".ldif" if it is there
      push @schemas, $1;
82
    } elsif ($arg =~ /(.*).schema/) {
83
      if (system("$schema2ldif $arg > $1.ldif") == 0) {
84
        push @schemas, $1;
85
        push @gen_files, $1;
86
      } else {
87
        die_with_error("Something went wrong while trying to convert $arg to ldif\n");
88
      }
89
90
91
92
    } else {
      push @schemas, $arg;
    }
  } elsif ($pathunset) {
93
    $path = $arg."/";
94
95
96
97
98
99
    $pathunset = 0;
  } else {
    usage();
  }
}

100
101
102
# if --options is used with no value
usage () if (not defined $ldap_host_options);

103
# die if user is not "root"
104
die_with_error ("! You have to run this script as root\n") if ($<!=0);
105

106
107
108
109
110
111
112
my $add_cmd         = "ldapadd $ldap_host_options -f ";
my $mod_cmd         = "ldapmodify $ldap_host_options -f ";
my $ldapsearch      = "ldapsearch $ldap_host_options ";
my $search_cmd      = $ldapsearch." -b \"cn=schema,cn=config\" cn={*}";
my $list_cmd        = $search_cmd."* cn 2>/dev/null";
my $cnconfig_cmd    = $ldapsearch." -b \"cn=config\" cn=config dn 2>/dev/null | grep dn:";

113
114
115
116
117
if ($listschemas) {
  list_schemas();
  exit 0;
}

118
# die if the path doesn't exists
119
die_with_error ("! $path doesn't seems to exists\n") if (!-e $path);
120

121
122
123
#die if we are not in cn=config
my $cnconfig = `$cnconfig_cmd`;
if (!($cnconfig =~ m/^dn:\s*cn=config$/)) {
124
  die_with_error ("! This tool is only intended to be with with a cn=config backend, cn=config could not be found in the LDAP");
125
126
}

127
if (scalar(@schemas) == 0) {
128
129
130
  if ($modify == 2) {
    usage("Missing schema list for -e option\n");
  }
131
  # insert the default schemas
132
  @schemas = ("samba","core-fd","core-fd-conf","ldapns","template-fd");
133
  foreach my $schema (@schemas) {
134
    if (system("$schema2ldif $path$schema.schema > $path$schema.ldif") == 0) {
135
136
      push @gen_files, $path.$schema;
    } else {
137
      die_with_error ("Something went wrong while trying to convert $path$schema.schema to ldif\n");
138
139
    }
  }
140
141
} elsif ($pathunset) {
  $path = "";
142
143
}

144
145
146
147
148
149
150
if ($modify == 2) {
  unless (ask_yn_question("Are you sure you want to empty schema(s) ".join(", ",@schemas)."?"))
  {
    die_with_error("Aborting…\n");
  }
}

151
152
$continue++; # activating continue feature only for insertions

153
foreach my $schema (@schemas) {
154
155
  my $schema_name = "";

156
157
158
159
160
161
162
163
164
165
166
  if ($modify < 2) {
    # Searching schema name in ldif file first line.
    open FILE, '< '.$path.$schema.".ldif" or die "Count not open ldif file : $!\n";
    my $dn = "";
    while ($dn eq "") {
      chomp($dn = <FILE>);
    }
    if ($dn =~ /^dn: cn=([^,]+),/) {
      $schema_name = $1;
    }
    close(FILE);
167
168
169
170
171
172
173
174
  }

  # Fallback on file name
  if ($schema_name eq "") {
    $schema_name = $schema;
    $schema_name =~ s|^.*/||;
  }

175
176
177
178
179
180
181
182
183
184
  insert_schema($schema, $schema_name);
}

remove_ldifs();

sub insert_schema
{
  my($schema, $schema_name) = @_;

  $full_cmd = $search_cmd.$schema_name." cn";
185
186
187
188
  print ("\n");
  my $search = `$full_cmd`;

  if ($search !~ /# numEntries: 1/m) {
189
190
191
192
193
194
195
196
197
    if ($modify) {
      print "$schema_name does not exists in the LDAP, skipping…\n";
    } else {
      # if the schema doesn't already exists in the LDAP server, adding it
      $full_cmd = $add_cmd.$path.$schema.".ldif";
      print "executing '$full_cmd'\n";
      if (system ($full_cmd) != 0) {
        die_with_error ("Insertion failed!\n");
      }
198
    }
199
  } else {
200
201
202
203
204
205
206
    if ($modify) {
      if ($search !~ m/dn: ([^,]+),cn=schema,cn=config/) {
        print "Could not parse existing dn for $schema_name, skipping…\n";
        return;
      }
      my $dn_part = $1;
      # if the schema already exists in the LDAP server, modify it
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
      if ($modify == 1) {
        open(SCHEMA, "<".$path.$schema.".ldif") or die_with_error('Could not open '."<".$path.$schema.".ldif: $!");
        open(UPDATE, ">".$path.$schema."_update.ldif") or die_with_error('Could not open '.">".$path.$schema."_update.ldif: $!");
        push @gen_files, $path.$schema."_update";
        my $attrs   = 0;
        my $classes = 0;
        while (<SCHEMA>) {
          next if m/^#/; # remove comments
          chomp;
          next if m/^$/; # remove empty lines
          if (m/^dn: cn=([^,]+),cn=schema,cn=config$/) {
            print UPDATE "dn: $dn_part,cn=schema,cn=config\n";
            print UPDATE "changetype: modify\n";
            next;
          }
          if (!m/^olcAttributeTypes:/ && !m/^olcObjectClasses:/ && !m/^ /) {
            #skip cn, objectClass, …
            next;
          }

          if (!$attrs && $classes) {
            die "Malformed schema\n";
          }

          if (!$attrs && m/^olcAttributeTypes:/) {
            $attrs = 1;
            print UPDATE "replace: olcAttributeTypes\n";
          }
          if (!$classes && m/^olcObjectClasses:/) {
            $classes = 1;
            print UPDATE "-\n";
            print UPDATE "replace: olcObjectClasses\n";
          }

          print UPDATE;
          print UPDATE "\n";
243
        }
244
245
246
247
248
249
250
251
252
253
254
255
        close SCHEMA;
        close UPDATE;
      } else { # Emptying schema
        open(UPDATE, ">".$path.$schema."_update.ldif") or die_with_error('Could not open '.">".$path.$schema."_update.ldif: $!");
        push @gen_files, $path.$schema."_update";
        print UPDATE "dn: $dn_part,cn=schema,cn=config\n";
        print UPDATE "changetype: modify\n";
        print UPDATE "delete: olcAttributeTypes\n";
        print UPDATE "-\n";
        print UPDATE "delete: olcObjectClasses\n";
        print UPDATE "-\n";
        close UPDATE;
256
257
258
259
260
261
262
263
264
      }
      $full_cmd = $mod_cmd.$path.$schema."_update.ldif";
      print "executing '$full_cmd'\n";
      if (system ($full_cmd) != 0) {
        die_with_error ("Insertion failed!\n");
      }
    } else {
      print "$schema_name already exists in the LDAP, skipping…\n";
    }
265
  }
266
}
267

268
269
sub remove_ldifs
{
270
271
272
273
  if ($delete_ldif) {
    foreach my $file (@gen_files) {
      unlink "$file.ldif" or print "Could not delete $file.ldif\n";
    }
274
275
276
277
278
279
  }
}

sub die_with_error
{
  my ($error) = @_;
280
281
282
283
284
285
  if ($continue == 2) {
    print "Error: $error\nContinuing…\n";
  } else {
    remove_ldifs();
    die $error;
  }
286
287
}

288
289
290
291
292
293
294
295
296
297
sub list_schemas
{
  my @schemas = `$list_cmd`;
  foreach my $schema (@schemas) {
    if ($schema =~ m/cn:\s*{[0-9]+}(.*)$/) {
      print "$1\n";
    }
  }
}

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# ask a question send as parameter, and return true if the answer is "yes"
sub ask_yn_question {
  return 1 if ($yes_flag);
  my ($question) = @_;
  print ( "$question [Yes/No]?\n" );

  while ( my $input = <STDIN> ) {
    # remove the \n at the end of $input
    chomp $input;

    # if user's answer is "yes"
    if ( lc($input) eq "yes" || lc($input) eq "y") {
      return 1;
    # else if he answer "no"
    } elsif ( lc($input) eq "no" || lc($input) eq "n") {
      return 0;
    }
  }
}

318
319
320
321
322
sub usage
{
  (@_) && print STDERR "\n@_\n\n";

  print STDERR << "EOF";
323
 usage: $0 [-h] [-y] [-l] [-o options] [path] [-i|-m|-e schema1 schema2 …]
324

325
326
327
328
329
  -h, --help      : this (help) message
  path            : where to find the schemas
  -i, --insert    : specify the schemas to insert
  -l, --list      : list inserted schemas
  -m, --modify    : modify exising inserted schemas
330
  -e, --empty     : empty exising inserted schemas (do not remove them)
331
  -n, --nodelete  : do not delete generated ldifs at the end
332
333
  -o, --options   : set ldap options used (default is -Y EXTERNAL -H ldapi:///)
  -c, --continue  : continue on error(s)
334
  -y, --yes       : answer yes to all questions
335
336
337
338
339

EOF
  exit -1;
}

340
341
342
343
344
345
346
347
exit 0;

=head1 NAME

fusiondirectory-insert-schema - insert schema needed by FusionDirectory into the ldap server

=head1 SYNOPSIS

348
fusiondirectory-insert-schema [<path of your schema files>] [-l] [-i|-m schema1 schema2]
349
350
351
352

=head1 DESCRIPTION

This program will insert the schema needed by FusionDirectory into the ldap server
353
354
355
356
357
358
359
360
361
362


=head2 Options

=over 6

=item -i

This option insert the given list of schemas instead of the default list

363
=item -m
364
365
366

This option insert the given list of schemas, replacing already inserted versions of those schemas

367
368
369
370
371
=item -e

This option empty the given list of schemas, removing attributes and objectClasses from those.
This is useful because you cannot delete a schema without restarting slapd.

372
373
374
375
376
377
378
379
=item -l

This option list inserted schemas

=item -n

This option will make generated ldifs file to not be deleted after execution. Might be useful to understand errors.

380
=item -c
381
382
383

This option make the program continue even if an error occur

384
385
386
387
=item -y

This option answer yes to all questions. The only question right now is the confirmation one when you ask to empty a schema.

388
=item -o
389
390

This option allow you to specify specifics options to give to ldap commands such as ldapmodify,
391
but beware that you will not be able to see things like password prompts as the output of these commands is piped.
392

393
=back
394

395
396
397
=head1 EXAMPLES

 fusion@catbert$ fusiondirectory-insert-schema
398
   Insert the core schemas in your LDAP directory
399

400
401
402
 fusion@catbert$ fusiondirectory-insert-schema -i /etc/ldap/otherschema/myschema.ldif
   Insert the schema /etc/ldap/otherschema/myschema.ldif

403
404
405
 fusion@catbert$ fusiondirectory-insert-schema -i /etc/ldap/otherschema/myschema.schema
   Convert /etc/ldap/otherschema/myschema.schema to ldif and insert it

406
407
408
 fusion@catbert$ fusiondirectory-insert-schema -i myschema
   Insert the schema myschema.ldif from working directory

409
410
411
 fusion@catbert$ fusiondirectory-insert-schema -m /etc/ldap/otherschema/myschema.schema
   Convert /etc/ldap/otherschema/myschema.schema to ldif and replace the existing schema by this one

412
413
414
 fusion@catbert$ fusiondirectory-insert-schema -e myschema
   Empty the schema myschema

415
416
417
 fusion@catbert$ fusiondirectory-insert-schema -o "-H ldap://my.ldap.com -ZZ -D 'cn=admin,cn=config' -w password -x"
   Connect to another ldap server to insert the schema
   
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
=head1 BUGS

Please report any bugs, or post any suggestions, to the fusiondirectory mailing list fusiondirectory-users or to
<https://forge.fusiondirectory.org/projects/fdirectory/issues/new>

=head1 AUTHOR

Come Bernigaud

=head1 LICENCE AND COPYRIGHT

This code is part of FusionDirectory <http://www.fusiondirectory.org>

=over 1

433
=item Copyright (C) 2011-2015 FusionDirectory Project
434
435
436
437
438
439
440
441
442

=back

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

=cut