#!/usr/bin/perl ####################################################################### # # fusiondirectory-insert-schema -- insert schema needed into the ldap server # # Copyright (C) 2011-2013 FusionDirectory project # # 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. # # 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. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/> # ####################################################################### use strict; use warnings; use 5.008; my $add_cmd = "ldapadd -Y EXTERNAL -H ldapi:/// -f "; my $search_cmd = "ldapsearch -Y EXTERNAL -H ldapi:// -b \"cn=schema,cn=config\" cn={*}"; my $path = "/etc/ldap/schema/fusiondirectory/"; my $full_cmd = ""; my $ldap_utils_path = "/usr/bin/ldapadd"; my $schema2ldif = "schema2ldif"; my $schemalist = 0; my $pathunset = 1; my @schemas = (); my @gen_files = (); foreach my $arg ( @ARGV ) { if (lc($arg) eq '-i') { $schemalist = 1; } elsif (lc($arg) eq '-h') { usage(); } elsif ($schemalist) { if ($arg =~ /(.*).ldif/) { # ignore ".ldif" if it is there push @schemas, $1; } elsif ($arg =~ /(.*).schema/) { if (system("$schema2ldif $arg > $1.ldif") == 0) { push @schemas, $1; push @gen_files, $1; } else { die "Something went wrong while trying to convert $arg to ldif\n"; } } else { push @schemas, $arg; } } elsif ($pathunset) { $path = $arg."/"; $pathunset = 0; } else { usage(); } } # die if user is not "root" die ("! You have to run this script as root\n") if ($<!=0); # die if the path doesn't exists die ("! $path doesn't seems to exists\n") if (!-e $path); # die if ldap-utils are not installed die ("! ldap-utils doesn't seem to be installed") if (!-e $ldap_utils_path); if (scalar(@schemas) == 0) { # insert the default schemas @schemas = ("samba","core-fd","core-fd-conf","ldapns","recovery-fd"); foreach my $schema (@schemas) { if (system("$schema2ldif $path$schema.schema > $path$schema.ldif") == 0) { push @gen_files, $path.$schema; } else { die "Something went wrong while trying to convert $path$schema.schema to ldif\n"; } } } elsif ($pathunset) { $path = ""; } foreach my $schema (@schemas) { my $schema_name = ""; # 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); # Fallback on file name if ($schema_name eq "") { $schema_name = $schema; $schema_name =~ s|^.*/||; } $full_cmd = $search_cmd.$schema_name; print ("\n"); my $search = `$full_cmd`; # if the schema doesn't already exists in the LDAP server, adding it if ($search !~ /# numEntries: 1/m) { $full_cmd = $add_cmd.$path.$schema.".ldif"; print "executing '$full_cmd'\n"; if (system ($full_cmd) != 0) { print "Insertion failed!\n"; } } else { print "$schema_name already exists in the LDAP, skippingâ¦\n"; } } foreach my $file (@gen_files) { unlink "$file.ldif" or print "Could not delete $file.ldif\n"; } sub usage { (@_) && print STDERR "\n@_\n\n"; print STDERR << "EOF"; usage: $0 [-h] [path] [-i schema1 schema2 â¦] -h : this (help) message path : where to find the schemas -i : specify the schemas to insert EOF exit -1; } exit 0; =head1 NAME fusiondirectory-insert-schema - insert schema needed by FusionDirectory into the ldap server =head1 SYNOPSIS fusiondirectory-insert-schema [<path of your schema files>] [-i schema1 schema2] =head1 DESCRIPTION This program will insert the schema needed by FusionDirectory into the ldap server If -i is specified, insert the given list of schemas instead of the default list =head1 EXAMPLES fusion@catbert$ fusiondirectory-insert-schema Insert the core schemas in your LDAP directory fusion@catbert$ fusiondirectory-insert-schema -i /etc/ldap/otherschema/myschema.ldif Insert the schema /etc/ldap/otherschema/myschema.ldif fusion@catbert$ fusiondirectory-insert-schema -i /etc/ldap/otherschema/myschema.schema Convert /etc/ldap/otherschema/myschema.schema to ldif and insert it fusion@catbert$ fusiondirectory-insert-schema -i myschema Insert the schema myschema.ldif from working directory =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 =item Copyright (C) 2011-2013 FusionDirectory Project =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