/usr/lib/x86_64-linux-gnu/dirsrv/perl/Inf.pm 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 | # BEGIN COPYRIGHT BLOCK
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
#
# License: GPL (version 3 or any later version).
# See LICENSE for details.
# END COPYRIGHT BLOCK
#
# manages inf files - gets values
# given keys
package Inf;
use DSUtil;
use File::Temp qw(tempfile tempdir);
#require Exporter;
#@ISA = qw(Exporter);
#@EXPORT = qw();
sub new {
my $type = shift;
my $self = {};
$self->{filename} = shift;
$self->{writable} = shift; # do not overwrite user supplied file
# if you want to init an Inf with a writable file, use
# $inf = new Inf($filename, 1)
$self = bless $self, $type;
if ($self->{filename}) {
if($self->read() != 0){
undef $self;
}
}
return $self;
}
sub read {
# each key in the table is a section name
# the value is a hash ref of the items in that section
# in that hash ref, each key is the config param name,
# and the value is the config param value
my $self = shift;
my $filename = shift;
my $curSection = "";
if ($filename) {
$self->{filename} = $filename;
} else {
$filename = $self->{filename};
}
my $incontinuation = 0;
my $curkey;
my $curval;
my $inffh;
if ($filename eq "-") {
$inffh = \*STDIN;
} else {
if (!open(INF, $filename)) {
debug(0, "Error: could not open inf file $filename: $!\n");
return -1;
}
$inffh = \*INF;
}
my $line;
while ($line = <$inffh>) {
my $iscontinuation;
chop $line; # trim trailing newline
if ($line =~ /^\s*$/) { # skip blank/empty lines
$incontinuation = 0;
next;
}
if ($line =~ /^\s*\#/) { # skip comment lines
$incontinuation = 0;
next;
}
if ($line =~ /\\$/) { # line ends in \ - continued on next line
chop $line;
$iscontinuation = 1;
}
if ($incontinuation) {
if ($curval) {
$self->{$curSection}->{$curkey}->[$curval] .= "\n" . $line; # add line in entirety to current value
} else {
$self->{$curSection}->{$curkey} .= "\n" . $line; # add line in entirety to current value
}
} elsif ($line =~ /^\[(.*?)\]/) { # e.g. [General]
$curSection = $1;
$iscontinuation = 0; # disallow section continuations
} elsif ($line =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/) { # key = value
$curkey = $1;
# a single value is just a single scalar
# multiple values are represented by an array ref
if (exists($self->{$curSection}->{$curkey})) {
if (!ref($self->{$curSection}->{$curkey})) {
# convert single scalar to array ref
my $ary = [$self->{$curSection}->{$curkey}];
$self->{$curSection}->{$curkey} = $ary;
}
# just push the new value
push @{$self->{$curSection}->{$curkey}}, $2;
$curval = @{$self->{$curSection}->{$curkey}} - 1; # curval is index of last item
} else {
# single value
$self->{$curSection}->{$curkey} = $2;
$curval = 0; # only 1 value
}
}
if ($iscontinuation) { # if line ends with a backslash, continue the data on the next line
$incontinuation = 1;
} else {
$incontinuation = 0;
}
}
if ($inffh ne \*STDIN) {
close $inffh;
}
return 0;
}
sub section {
my $self = shift;
my $key = shift;
if (!exists($self->{$key})) {
debug(0, "Error: unknown inf section $key\n");
return undef;
}
return $self->{$key};
}
sub writeSection {
my $self = shift;
my $name = shift;
my $fh = shift;
my $section = $self->{$name};
if (ref($section) eq 'HASH') {
print $fh "[$name]\n";
for my $key (sort keys %{$section}) {
if (exists($section->{$key}) and defined($section->{$key}) and
(length($section->{$key}) > 0)) {
my @vals = ();
if (ref($section->{$key})) {
@vals = @{$section->{$key}};
} else {
@vals = ($section->{$key});
}
for my $val (@vals) {
$val =~ s/\n/\\\n/g; # make continuation lines
print $fh "$key = $val\n";
}
}
}
}
}
sub write {
my $self = shift;
my $filename = shift;
my $fh;
return if ($filename and $filename eq "-");
# see if user wants to force use of a temp file
if ($filename and $filename eq '__temp__') {
$self->{writable} = 1;
$filename = '';
delete $self->{filename};
}
if (!$self->{writable}) {
return; # do not overwrite read only file
}
if ($filename) { # use user supplied filename
$self->{filename} = $filename;
} elsif ($self->{filename}) { # use existing filename
$filename = $self->{filename};
} else { # create temp filename
($fh, $self->{filename}) = tempfile("setupXXXXXX", UNLINK => 0,
SUFFIX => ".inf", OPEN => 1,
DIR => File::Spec->tmpdir);
}
my $savemask = umask(0077);
if (!$fh) {
if (!open(INF, ">$filename")) {
debug(0, "Error: could not write inf file $filename: $!\n");
umask($savemask);
return;
}
$fh = *INF;
}
# write General section first
$self->writeSection('General', $fh);
for my $key (keys %{$self}) {
next if ($key eq 'General');
$self->writeSection($key, $fh);
}
close $fh;
umask($savemask);
}
sub updateFromArgs {
my $self = shift;
my $argsinf = {}; # tmp for args read in
if (!@_) {
return 1; # no args - just return
}
# read args into temp inf
for my $arg (@_) {
if ($arg =~ /^([\w_-]+)\.([\w_-]+)=(.*)$/) { # e.g. section.param=value
my $sec = $1;
my $parm = $2;
my $val = $3;
# a single value is just a single scalar
# multiple values are represented by an array ref
if (exists($argsinf->{$sec}->{$parm})) {
if (!ref($argsinf->{$sec}->{$parm})) {
# convert single scalar to array ref
my $ary = [$argsinf->{$sec}->{$parm}];
$argsinf->{$sec}->{$parm} = $ary;
}
# just push the new value
push @{$argsinf->{$sec}->{$parm}}, $val;
} else {
# single value
$argsinf->{$sec}->{$parm} = $val;
}
} else { # error
debug(0, "Error: unknown command line option $arg\n");
return;
}
}
# no args read - just return true
if (!$argsinf || !%{$argsinf}) {
return 1;
}
# override inf with vals read from args
while (my ($name, $sec) = each %{$argsinf}) {
if (ref($sec) eq 'HASH') {
for my $key (keys %{$sec}) {
if (defined($sec->{$key})) {
my $val = $sec->{$key};
$self->{$name}->{$key} = $val;
}
}
}
}
return 1;
}
#############################################################################
# Mandatory TRUE return value.
#
1;
|