/usr/share/lemonldap-ng/bin/lmMigrateConfFiles2ini is in liblemonldap-ng-common-perl 1.9.16-2.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/perl
use strict;
use Getopt::Long;
use Config::IniFiles;
use XML::LibXML;
use Lemonldap::NG::Common::Conf::Constants;
# Get command line options
my %opts;
my $result = GetOptions(
\%opts, 'storage|s=s', 'apply|a=s', 'dir|d=s',
'ini|i=s', 'preserve|p', 'menuxml|m=s', 'verbose|v',
'help|h', 'die',
);
# Help
if ( $opts{help} ) {
print
"$0 script imports old config file into the new lemonldap-ng.ini file\n";
print "Options:\n";
print
"\t--dir,-d: path to main configuration directory (default: /etc/lemonldap-ng)\n";
print "\t--storage,-s: path to storage.conf (if not stored in conf dir)\n";
print "\t--apply,-a: path to apply.conf (if not stored in conf dir)\n";
print "\t--menuxml,-m: path to apps-list.xml (if not stored in conf dir)\n";
print "\t--ini,-i: path to lemonldap-ng.ini (if not stored in conf dir)\n";
print "\t--preserve,-p: do not erase old files after import\n";
print "\t--help,-h: show this message\n";
print "\t--verbose,-v: let me tell you my life\n";
exit 0;
}
# Set default values
$opts{dir} ||= '/etc/lemonldap-ng';
my $old = {
storage => $opts{storage} || $opts{dir} . "/storage.conf",
apply => $opts{apply} || $opts{dir} . "/apply.conf",
menuxml => $opts{menuxml} || $opts{dir} . "/apps-list.xml",
menudtd => $opts{dir} . "/apps-list.dtd",
};
my $new = $opts{ini} || $opts{dir} . "/lemonldap-ng.ini";
my $datas;
if ( $opts{verbose} ) {
print "Using values:\n";
print "\tMain configuration dir: " . $opts{dir} . "\n";
print "\tFile storage: " . $old->{storage} . "\n";
print "\tFile apply: " . $old->{apply} . "\n";
print "\tFile menu: " . $old->{menuxml} . "\n";
print "\tNew ini file: " . $new . "\n";
print "\tPreserve: " . ( $opts{preserve} ? "yes" : "no" ) . "\n\n";
}
# Convert storage.conf
if ( -r $old->{storage} ) {
print "Parsing " . $old->{storage} . "\n" if $opts{verbose};
open F, $old->{storage};
while (<F>) {
next if (/^\s*(?:#.*)?$/);
my ( $k, $v ) = (/^(\w+)\s*=\s*(.*)$/)
or quit( 3, "bad line in " . $old->{storage} . ":$_" );
$datas->{ +CONFSECTION }->{$k} = $v;
print "\t$k: $v\n" if $opts{verbose};
}
close F;
print "\n" if $opts{verbose};
}
elsif ( $opts{die} ) {
quit( 2, $old->{storage} . " is not readable" );
}
else {
print STDERR $old->{storage} . " is not readable\n";
}
# Convert apply.conf
if ( -r $old->{apply} ) {
print "Parsing " . $old->{apply} . "\n" if $opts{verbose};
open F, $old->{apply};
while (<F>) {
next if (/^\s*(?:#.*)?$/);
my ( $k, $v ) = (/^([\w\.\-]+)\s+(.*)$/)
or quit( 3, "bad line in " . $old->{apply} . ":$_" );
$datas->{ +APPLYSECTION }->{$k} = $v;
print "\t$k: $v\n" if $opts{verbose};
}
close F;
print "\n" if $opts{verbose};
}
elsif ( $opts{die} ) {
quit( 2, $old->{apply} . " is not readable" );
}
else {
print STDERR $old->{apply} . " is not readable\n";
}
# Convert apps-list.xml
if ( -r $old->{menuxml} ) {
print "Parsing " . $old->{menuxml} . "\n" if $opts{verbose};
# Open XML file
my $parser = XML::LibXML->new();
my $xml;
eval { $xml = $parser->parse_file( $old->{menuxml} ); };
quit( 6, "Bad XML file: $@" ) if ($@);
# Get root element
my $root = $xml->documentElement;
my $value = "{ ";
$value .= _parseCategory($root);
$value .= " }";
$datas->{ +PORTALSECTION }->{applicationList} = $value;
print "\tapplicationList: $value\n\n" if $opts{verbose};
}
elsif ( $opts{die} ) {
quit( 2, $old->{menuxml} . " is not readable" );
}
else {
print STDERR $old->{menuxml} . " is not readable\n";
}
# Open ini configuration file
my $conf;
if ( -e $new ) {
-w $new or quit( 4, "$new is not writeable" );
$conf = Config::IniFiles->new( -file => $new, -allowcontinue => 1 )
or quit(
4,
"Unable to open $new:\n\t" . join( "\n\t", @Config::IniFiles::errors )
);
}
else {
$conf = Config::IniFiles->new();
}
# Write sections
my @sections = $conf->Sections();
foreach ( ( CONFSECTION, APPLYSECTION, PORTALSECTION ) ) {
print "Write data for section $_\n" if $opts{verbose};
next unless ( ref $datas->{$_} );
$conf->AddSection($_) unless ( $conf->SectionExists($_) );
while ( my ( $k, $v ) = each %{ $datas->{$_} } ) {
# Old Config::IniFiles modules does not have 'exists' subroutine
if ( $conf->can('exists') ) {
if ( $conf->exists( $_, $k ) ) {
$conf->setval( $_, $k, $v );
}
else {
$conf->newval( $_, $k, $v );
}
}
else {
# Try setval, else newval
unless ( $conf->setval( $_, $k, $v ) ) {
$conf->newval( $_, $k, $v );
}
}
}
}
if ( -e $new ) {
$conf->RewriteConfig();
}
else {
$conf->WriteConfig($new)
or quit( 5,
"Unable to create $new:\n\t"
. join( "\n\t", @Config::IniFiles::errors ) );
}
# Remove old files
unless ( $opts{preserve} ) {
print "Remove old files\n" if $opts{verbose};
unlink $old->{storage}, $old->{apply}, $old->{menuxml}, $old->{menudtd};
}
# Local subroutines
sub quit {
print STDERR "$_[1]\n";
exit $_[0];
}
sub _parseCategory {
my $category = shift;
my $value;
my $catname = $category->getAttribute('name') || "Menu";
# Escape quote
$catname =~ s/'/\\'/;
$value .= "'$catname' => { type => 'category', ";
# Applications
my @appnodes = $category->findnodes("application");
foreach (@appnodes) {
$value .= _parseApplication($_);
}
# Sub categories
my @catnodes = $category->findnodes("category");
foreach (@catnodes) {
$value .= _parseCategory($_);
}
$value .= " },";
return $value;
}
sub _parseApplication {
my $application = $_;
my $value;
# Get application items
my $appid = $application->getAttribute('id');
my $appname = $application->getChildrenByTagName('name')->string_value();
my $appuri = $application->getChildrenByTagName('uri')->string_value();
my $appdesc =
$application->getChildrenByTagName('description')->string_value();
my $applogo = $application->getChildrenByTagName('logo')->string_value();
my $appdisplay =
$application->getChildrenByTagName('display')->string_value();
# Escape quote
$appid =~ s/'/\\'/;
$appname =~ s/'/\\'/;
$appdesc =~ s/'/\\'/;
# Print application items
$value .= "'$appid' => { type => 'application', options => { ";
$value .= "name => '$appname', " if $appname;
$value .= "uri => '$appuri', " if $appuri;
$value .= "description => '$appdesc', " if $appdesc;
$value .= "logo => '$applogo', " if $applogo;
$value .= "display => '$appdisplay', " if $appdisplay;
$value .= " },";
# Sub applications
my @appnodes = $application->findnodes("application");
foreach (@appnodes) {
$value .= _parseApplication($_);
}
$value .= " },";
return $value;
}
|