| 1 |
#!/usr/bin/perl |
| 2 |
# Copyright (C) 2002 Matthew Palmer. |
| 3 |
# Copyright (C) 2007-2008 Davor Ocelic. |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
Debconf::DbDriver::LDAP - access (config) database in an LDAP directory |
| 8 |
|
| 9 |
=cut |
| 10 |
|
| 11 |
package Debconf::DbDriver::LDAP; |
| 12 |
use warnings; |
| 13 |
use strict; |
| 14 |
use Debconf::Log qw(:all); |
| 15 |
use Net::LDAP; |
| 16 |
use base 'Debconf::DbDriver::Cache'; |
| 17 |
|
| 18 |
=head1 DESCRIPTION |
| 19 |
|
| 20 |
A Debconf database driver to access an LDAP directory for configuration |
| 21 |
data. It reads all the relevant entries from the directory server on |
| 22 |
startup (which can take a little while) however this cost is less than the |
| 23 |
equivalent time if data accesses were done piecemeal. |
| 24 |
|
| 25 |
Due to the nature of the beast, LDAP directories should typically be |
| 26 |
accessed in read-only mode. This is because multiple accesses can take |
| 27 |
place, and it's generally better for data consistency if nobody tries to |
| 28 |
modify the data while this is happening. Of course, write access is |
| 29 |
supported for those cases where you do want to update the config data in the |
| 30 |
directory. |
| 31 |
|
| 32 |
=head1 FIELDS |
| 33 |
|
| 34 |
=over 4 |
| 35 |
|
| 36 |
=item server |
| 37 |
|
| 38 |
The host name or IP address of an LDAP server to connect to. |
| 39 |
|
| 40 |
=item port |
| 41 |
|
| 42 |
The port on which to connect to the LDAP server. If none is given, the |
| 43 |
default is used. |
| 44 |
|
| 45 |
=item basedn |
| 46 |
|
| 47 |
The DN under which all config items will be stored. Each config item will |
| 48 |
be assumed to live in a DN of cn=<item name>,<Base DN>. If this structure |
| 49 |
is not followed, all bets are off. |
| 50 |
|
| 51 |
=item binddn |
| 52 |
|
| 53 |
The DN to bind to the directory as. Anonymous bind will be used if none is |
| 54 |
specified. |
| 55 |
|
| 56 |
=item bindpasswd |
| 57 |
|
| 58 |
The password to use in an authenticated bind (used with binddn, above). If |
| 59 |
not specified, anonymous bind will be used. |
| 60 |
|
| 61 |
This option should not be used in the general case. Anonymous binding |
| 62 |
should be sufficient most of the time for read-only access. Specifying a |
| 63 |
bind DN and password should be reserved for the occasional case where you |
| 64 |
wish to update the debconf configuration data. |
| 65 |
|
| 66 |
=item keybykey |
| 67 |
|
| 68 |
Enable access to individual LDAP entries, instead of fetching them |
| 69 |
all at once in the beginning. This is very useful if you want to monitor your |
| 70 |
LDAP logs for specific debconf keys requested. In this way, you could also |
| 71 |
write custom handling code on the LDAP server part. |
| 72 |
|
| 73 |
Note that when this option is enabled, the connection to the LDAP server |
| 74 |
is kept active during the whole Debconf run. This is a little different |
| 75 |
from the all-in-one behavior where two brief connections are made to LDAP; |
| 76 |
in the beginning to retrieve all the entries, and in the end to save |
| 77 |
eventual changes. |
| 78 |
|
| 79 |
=back |
| 80 |
|
| 81 |
=cut |
| 82 |
|
| 83 |
use fields qw(server port basedn binddn bindpasswd exists keybykey ds accept_attribute reject_attribute); |
| 84 |
|
| 85 |
=head1 METHODS |
| 86 |
|
| 87 |
=head2 binddb |
| 88 |
|
| 89 |
Connect to the LDAP server, and bind to the DN. Retuns a Net::LDAP object. |
| 90 |
|
| 91 |
=cut |
| 92 |
|
| 93 |
sub binddb { |
| 94 |
my $this=shift; |
| 95 |
|
| 96 |
# Check for required options |
| 97 |
$this->error("No server specified") unless exists $this->{server}; |
| 98 |
$this->error("No Base DN specified") unless exists $this->{basedn}; |
| 99 |
|
| 100 |
# Set up other defaults |
| 101 |
$this->{binddn} = "" unless exists $this->{binddn}; |
| 102 |
# XXX This will need to handle SSL when we support it |
| 103 |
$this->{port} = 389 unless exists $this->{port}; |
| 104 |
|
| 105 |
debug "db $this->{name}" => "talking to $this->{server}, data under $this->{basedn}"; |
| 106 |
|
| 107 |
# Whee, LDAP away! Net::LDAP tells us about all these methods. |
| 108 |
$this->{ds} = Net::LDAP->new($this->{server}, port => $this->{port}, version => 3); |
| 109 |
if (! $this->{ds}) { |
| 110 |
$this->error("Unable to connect to LDAP server"); |
| 111 |
return; # if not fatal, give up anyway |
| 112 |
} |
| 113 |
|
| 114 |
# Check for anon bind |
| 115 |
my $rv = ""; |
| 116 |
if (!($this->{binddn} && $this->{bindpasswd})) { |
| 117 |
debug "db $this->{name}" => "binding anonymously; hope that's OK"; |
| 118 |
$rv = $this->{ds}->bind; |
| 119 |
} else { |
| 120 |
debug "db $this->{name}" => "binding as $this->{binddn}"; |
| 121 |
$rv = $this->{ds}->bind($this->{binddn}, password => $this->{bindpasswd}); |
| 122 |
} |
| 123 |
if ($rv->code) { |
| 124 |
$this->error("Bind Failed: ".$rv->error); |
| 125 |
} |
| 126 |
|
| 127 |
return $this->{ds}; |
| 128 |
} |
| 129 |
|
| 130 |
=head2 init |
| 131 |
|
| 132 |
On initialization, connect to the directory, read all of the debconf data |
| 133 |
into the cache, and close off the connection again. |
| 134 |
|
| 135 |
If KeyByKey is enabled, then skip the complete data load and only retrieve |
| 136 |
few keys required by debconf. |
| 137 |
|
| 138 |
=cut |
| 139 |
|
| 140 |
sub init { |
| 141 |
my $this = shift; |
| 142 |
|
| 143 |
$this->SUPER::init(@_); |
| 144 |
|
| 145 |
$this->binddb; |
| 146 |
return unless $this->{ds}; |
| 147 |
|
| 148 |
# A record of all the existing entries in the DB so we know which |
| 149 |
# ones need to added, and which modified |
| 150 |
$this->{exists} = {}; |
| 151 |
|
| 152 |
if ($this->{keybykey}) { |
| 153 |
debug "db $this->{name}" => "will get database data key by key"; |
| 154 |
} |
| 155 |
else { |
| 156 |
debug "db $this->{name}" => "getting database data"; |
| 157 |
my $data = $this->{ds}->search(base => $this->{basedn}, sizelimit => 0, timelimit => 0, filter => "(objectclass=debconfDbEntry)"); |
| 158 |
if ($data->code) { |
| 159 |
$this->error("Search failed: ".$data->error); |
| 160 |
} |
| 161 |
|
| 162 |
my $records = $data->as_struct(); |
| 163 |
debug "db $this->{name}" => "Read ".$data->count()." entries"; |
| 164 |
|
| 165 |
$this->parse_records($records); |
| 166 |
|
| 167 |
$this->{ds}->unbind; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
=head2 shutdown |
| 172 |
|
| 173 |
Save the dirty entries back to the LDAP server. |
| 174 |
|
| 175 |
=cut |
| 176 |
|
| 177 |
sub shutdown |
| 178 |
{ |
| 179 |
my $this = shift; |
| 180 |
|
| 181 |
return if $this->{readonly}; |
| 182 |
|
| 183 |
if (grep { $this->{dirty}->{$_} } keys %{$this->{cache}}) { |
| 184 |
debug "db $this->{name}" => "saving changes"; |
| 185 |
} else { |
| 186 |
debug "db $this->{name}" => "no database changes, not saving"; |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
unless ($this->{keybykey}) { |
| 191 |
$this->binddb; |
| 192 |
return unless $this->{ds}; |
| 193 |
} |
| 194 |
|
| 195 |
foreach my $item (keys %{$this->{cache}}) { |
| 196 |
next unless defined $this->{cache}->{$item}; # skip deleted |
| 197 |
next unless $this->{dirty}->{$item}; # skip unchanged |
| 198 |
# These characters must be quoted in the DN |
| 199 |
(my $entry_cn = $item) =~ s/([,+="<>#;])/\\$1/g; |
| 200 |
my $entry_dn = "cn=$entry_cn,$this->{basedn}"; |
| 201 |
debug "db $this->{name}" => "writing out to $entry_dn"; |
| 202 |
|
| 203 |
my %data = %{$this->{cache}->{$item}}; |
| 204 |
my %modify_data; |
| 205 |
my $add_data = [ 'objectclass' => 'top', |
| 206 |
'objectclass' => 'debconfdbentry', |
| 207 |
'cn' => $item |
| 208 |
]; |
| 209 |
|
| 210 |
# Perform generic replacement in style of: |
| 211 |
# extended_description -> extendedDescription |
| 212 |
my @fields = keys %{$data{fields}}; |
| 213 |
foreach my $field (@fields) { |
| 214 |
my $ldapname = $field; |
| 215 |
if ( $ldapname =~ s/_(\w)/uc($1)/ge ) { |
| 216 |
$data{fields}->{$ldapname} = $data{fields}->{$field}; |
| 217 |
delete $data{fields}->{$field}; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
foreach my $field (keys %{$data{fields}}) { |
| 222 |
# skip empty fields exept value field |
| 223 |
next if ($data{fields}->{$field} eq '' && |
| 224 |
!($field eq 'value')); |
| 225 |
if ((exists $this->{accept_attribute} && |
| 226 |
$field !~ /$this->{accept_attribute}/) or |
| 227 |
(exists $this->{reject_attribute} && |
| 228 |
$field =~ /$this->{reject_attribute}/)) { |
| 229 |
debug "db $item" => "reject $field"; |
| 230 |
next; |
| 231 |
} |
| 232 |
|
| 233 |
$modify_data{$field}=$data{fields}->{$field}; |
| 234 |
push(@{$add_data}, $field); |
| 235 |
push(@{$add_data}, $data{fields}->{$field}); |
| 236 |
} |
| 237 |
|
| 238 |
my @owners = keys %{$data{owners}}; |
| 239 |
debug "db $this->{name}" => "owners is ".join(" ", @owners); |
| 240 |
$modify_data{owners} = \@owners; |
| 241 |
push(@{$add_data}, 'owners'); |
| 242 |
push(@{$add_data}, \@owners); |
| 243 |
|
| 244 |
my @flags = grep { $data{flags}->{$_} eq 'true' } keys %{$data{flags}}; |
| 245 |
if (@flags) { |
| 246 |
$modify_data{flags} = \@flags; |
| 247 |
push(@{$add_data}, 'flags'); |
| 248 |
push(@{$add_data}, \@flags); |
| 249 |
} |
| 250 |
|
| 251 |
$modify_data{variables} = []; |
| 252 |
foreach my $var (keys %{$data{variables}}) { |
| 253 |
my $variable = "$var=$data{variables}->{$var}"; |
| 254 |
push (@{$modify_data{variables}}, $variable); |
| 255 |
push(@{$add_data}, 'variables'); |
| 256 |
push(@{$add_data}, $variable); |
| 257 |
} |
| 258 |
|
| 259 |
my $rv=""; |
| 260 |
if ($this->{exists}->{$item}) { |
| 261 |
$rv = $this->{ds}->modify($entry_dn, replace => \%modify_data); |
| 262 |
} else { |
| 263 |
$rv = $this->{ds}->add($entry_dn, attrs => $add_data); |
| 264 |
} |
| 265 |
if ($rv->code) { |
| 266 |
$this->error("Modify failed: ".$rv->error); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
$this->{ds}->unbind(); |
| 271 |
|
| 272 |
$this->SUPER::shutdown(@_); |
| 273 |
} |
| 274 |
|
| 275 |
=head2 load |
| 276 |
|
| 277 |
Empty routine for all-in-one db fetch, but does some actual |
| 278 |
work for individual keys retrieval. |
| 279 |
|
| 280 |
=cut |
| 281 |
|
| 282 |
sub load { |
| 283 |
my $this = shift; |
| 284 |
return unless $this->{keybykey}; |
| 285 |
my $entry_cn = shift; |
| 286 |
|
| 287 |
my $records = $this->get_key($entry_cn); |
| 288 |
return unless $records; |
| 289 |
|
| 290 |
debug "db $this->{name}" => "Read entry for $entry_cn"; |
| 291 |
|
| 292 |
$this->parse_records($records); |
| 293 |
} |
| 294 |
|
| 295 |
=head2 remove |
| 296 |
|
| 297 |
Called by Cache::shutdown, nothing to do because already done in LDAP::shutdown |
| 298 |
|
| 299 |
=cut |
| 300 |
|
| 301 |
sub remove { |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
=head2 save |
| 306 |
|
| 307 |
Called by Cache::shutdown, nothing to do because already done in LDAP::shutdown |
| 308 |
|
| 309 |
=cut |
| 310 |
|
| 311 |
sub save { |
| 312 |
return 1; |
| 313 |
} |
| 314 |
|
| 315 |
=head2 get_key |
| 316 |
|
| 317 |
Retrieve individual key from LDAP db. |
| 318 |
The function is a no-op if KeyByKey is disabled. |
| 319 |
Returns entry->as_struct if found, undef otherwise. |
| 320 |
|
| 321 |
=cut |
| 322 |
|
| 323 |
sub get_key { |
| 324 |
my $this = shift; |
| 325 |
return unless $this->{keybykey}; |
| 326 |
my $entry_cn = shift; |
| 327 |
|
| 328 |
my $data = $this->{ds}->search( |
| 329 |
base => 'cn=' . $entry_cn . ',' . $this->{basedn}, |
| 330 |
sizelimit => 0, |
| 331 |
timelimit => 0, |
| 332 |
filter => "(objectclass=debconfDbEntry)"); |
| 333 |
|
| 334 |
if ($data->code) { |
| 335 |
$this->error("Search failed: ".$data->error); |
| 336 |
} |
| 337 |
|
| 338 |
return unless $data->entries; |
| 339 |
$data->as_struct(); |
| 340 |
} |
| 341 |
|
| 342 |
# Parse struct data (such as one returned by get_key()) |
| 343 |
# into internal hash/cache representation |
| 344 |
sub parse_records { |
| 345 |
my $this = shift; |
| 346 |
my $records = shift; |
| 347 |
|
| 348 |
# This is a rather great honking loop, but it's quite simply a nested |
| 349 |
# bunch of loops iterating through every DN, attribute, and value in |
| 350 |
# the returned set (the complete debconf database) and storing it in |
| 351 |
# a format which the cache driver hopefully understands. |
| 352 |
foreach my $dn (keys %{$records}) { |
| 353 |
my $entry = $records->{$dn}; |
| 354 |
debug "db $this->{name}" => "Reading data from $dn"; |
| 355 |
my %ret = (owners => {}, |
| 356 |
fields => {}, |
| 357 |
variables => {}, |
| 358 |
flags => {}, |
| 359 |
); |
| 360 |
my $name = ""; |
| 361 |
|
| 362 |
foreach my $attr (keys %{$entry}) { |
| 363 |
if ($attr eq 'objectclass') { |
| 364 |
next; |
| 365 |
} |
| 366 |
my $values = $entry->{$attr}; |
| 367 |
|
| 368 |
# Perform generic replacement in style of: |
| 369 |
# extendedDescription -> extended_description |
| 370 |
$attr =~ s/([a-z])([A-Z])/$1.'_'.lc($2)/ge; |
| 371 |
|
| 372 |
debug "db $this->{name}" => "Setting data for $attr"; |
| 373 |
foreach my $val (@{$values}) { |
| 374 |
debug "db $this->{name}" => "$attr = $val"; |
| 375 |
if ($attr eq 'owners') { |
| 376 |
$ret{owners}->{$val}=1; |
| 377 |
} elsif ($attr eq 'flags') { |
| 378 |
$ret{flags}->{$val}='true'; |
| 379 |
} elsif ($attr eq 'cn') { |
| 380 |
$name = $val; |
| 381 |
} elsif ($attr eq 'variables') { |
| 382 |
my ($var, $value)=split(/\s*=\s*/, $val, 2); |
| 383 |
$ret{variables}->{$var}=$value; |
| 384 |
} else { |
| 385 |
$val=~s/\\n/\n/g; |
| 386 |
$ret{fields}->{$attr}=$val; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
$this->{cache}->{$name} = \%ret; |
| 392 |
$this->{exists}->{$name} = 1; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
=head1 AUTHOR |
| 397 |
|
| 398 |
Matthew Palmer <[email protected]> |
| 399 |
|
| 400 |
Davor Ocelic <[email protected]> |
| 401 |
|
| 402 |
=cut |
| 403 |
|
| 404 |
1 |