home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl5 / Debconf / DbDriver / LDAP.pm < prev    next >
Encoding:
Perl POD Document  |  2006-07-24  |  4.6 KB  |  198 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::DbDriver::LDAP;
  6. use strict;
  7. use Debconf::Log qw(:all);
  8. use Net::LDAP;
  9. use base 'Debconf::DbDriver::Cache';
  10.  
  11.  
  12. use fields qw(server port basedn binddn bindpasswd exists);
  13.  
  14.  
  15. sub binddb {
  16.     my $this=shift;
  17.  
  18.     $this->error("No server specified") unless exists $this->{server};
  19.     $this->error("No Base DN specified") unless exists $this->{basedn};
  20.     
  21.     $this->{binddn} = "" unless exists $this->{binddn};
  22.     $this->{port} = 389 unless exists $this->{port};
  23.     
  24.     debug "db $this->{name}" => "talking to $this->{server}, data under $this->{basedn}";
  25.  
  26.     my $ds = Net::LDAP->new($this->{server}, port => $this->{port}, version => 3);
  27.     if (! $ds) {
  28.         $this->error("Unable to connect to LDAP server");
  29.         return; # if not fatal, give up anyway
  30.     }
  31.     
  32.     my $rv = "";
  33.     if (!($this->{binddn} && $this->{bindpasswd})) {
  34.         debug "db $this->{name}" => "binding anonymously; hope that's OK";
  35.         $rv = $ds->bind;
  36.     } else {
  37.         debug "db $this->{name}" => "binding as $this->{binddn}";
  38.         $rv = $ds->bind($this->{binddn}, password => $this->{bindpasswd});
  39.     }
  40.     if ($rv->code) {
  41.         $this->error("Bind Failed: ".$rv->error);
  42.     }
  43.     
  44.     return $ds;
  45. }
  46.  
  47.  
  48. sub init {
  49.     my $this = shift;
  50.  
  51.     $this->SUPER::init(@_);
  52.     
  53.     debug "db $this->{name}" => "getting database data";
  54.     my $ds=$this->binddb;
  55.     return unless $ds;
  56.     my $data = $ds->search(base => $this->{basedn}, sizelimit => 0, timelimit => 0, filter => "(objectclass=debconfDbEntry)");
  57.     if ($data->code) {
  58.         $this->error("Search failed: ".$data->error);
  59.     }
  60.         
  61.     my $records = $data->as_struct();
  62.     debug "db $this->{name}" => "Read ".$data->count()." entries";    
  63.  
  64.     $this->{exists} = {};
  65.  
  66.     foreach my $dn (keys %{$records}) {
  67.         my $entry = $records->{$dn};
  68.         debug "db $this->{name}" => "Reading data from $dn";
  69.         my %ret = (owners => {},
  70.             fields => {},
  71.             variables => {},
  72.             flags => {},
  73.         );
  74.         my $name = "";
  75.                     
  76.         foreach my $attr (keys %{$entry}) {
  77.             if ($attr eq 'objectclass') {
  78.                 next;
  79.             }
  80.             debug "db $this->{name}" => "Setting data for $attr";
  81.             my $values = $entry->{$attr};
  82.             foreach my $val (@{$values}) {
  83.                 debug "db $this->{name}" => "$attr = $val";
  84.                 if ($attr eq 'owners') {
  85.                     $ret{owners}->{$val}=1;
  86.                 } elsif ($attr eq 'flags') {
  87.                     $ret{flags}->{$val}='true';
  88.                 } elsif ($attr eq 'cn') {
  89.                     $name = $val;
  90.                 } elsif ($attr eq 'variables') {
  91.                     my ($var, $value)=split(/\s*=\s*/, $val, 2);
  92.                     $ret{variables}->{$var}=$value;
  93.                 } else {
  94.                     $val=~s/\\n/\n/g;
  95.                     $ret{fields}->{$attr}=$val;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         $this->{cache}->{$name} = \%ret;
  101.         $this->{exists}->{$name} = 1;
  102.     }
  103.     
  104.     $ds->unbind;
  105. }
  106.  
  107.  
  108. sub shutdown
  109. {
  110.     my $this = shift;
  111.     
  112.     return if $this->{readonly};
  113.     
  114.     if (grep $this->{dirty}->{$_}, keys %{$this->{cache}}) {
  115.         debug "db $this->{name}" => "saving changes";
  116.     } else {
  117.         debug "db $this->{name}" => "no database changes, not saving";
  118.         return 1;
  119.     }
  120.     
  121.     my $ds=$this->binddb;
  122.     return unless $ds;
  123.  
  124.     foreach my $item (keys %{$this->{cache}}) {
  125.         next unless defined $this->{cache}->{$item};  # skip deleted
  126.         next unless $this->{dirty}->{$item};    # skip unchanged
  127.         (my $entry_cn = $item) =~ s/([,+="<>#;])/\\$1/g;
  128.         my $entry_dn = "cn=$entry_cn,$this->{basedn}";
  129.         debug "db $this->{name}" => "writing out to $entry_dn";
  130.         
  131.         my %data = %{$this->{cache}->{$item}};
  132.         my %modify_data;
  133.         my $add_data = [ 'objectclass' => 'top',
  134.                 'objectclass' => 'debconfdbentry',
  135.                 'cn' => $item
  136.         ];
  137.         
  138.         foreach my $field (keys %{$data{fields}}) {
  139.             next if ($data{fields}->{$field} eq '' && 
  140.                  !($field eq 'value'));
  141.              $modify_data{$field}=$data{fields}->{$field};
  142.             push(@{$add_data}, $field);
  143.             push(@{$add_data}, $data{fields}->{$field});
  144.         }
  145.  
  146.         my @owners = keys %{$data{owners}};
  147.         debug "db $this->{name}" => "owners is ".join("  ", @owners);
  148.         $modify_data{owners} = \@owners;
  149.         push(@{$add_data}, 'owners');
  150.         push(@{$add_data}, \@owners);
  151.         
  152.         my @flags = grep { $data{flags}->{$_} eq 'true' } keys %{$data{flags}};
  153.         if (@flags) {
  154.             $modify_data{flags} = \@flags;
  155.             push(@{$add_data}, 'flags');
  156.             push(@{$add_data}, \@flags);
  157.         }
  158.  
  159.         $modify_data{variables} = [];
  160.         foreach my $var (keys %{$data{variables}}) {
  161.             my $variable = "$var=$data{variables}->{$var}";
  162.             push (@{$modify_data{variables}}, $variable);
  163.             push(@{$add_data}, 'variables');
  164.             push(@{$add_data}, $variable);
  165.         }
  166.         
  167.         my $rv="";
  168.         if ($this->{exists}->{$item}) {
  169.             $rv = $ds->modify($entry_dn, replace => \%modify_data);
  170.         } else {
  171.             $rv = $ds->add($entry_dn, attrs => $add_data);
  172.         }
  173.         if ($rv->code) {
  174.             $this->error("Modify failed: ".$rv->error);
  175.         }
  176.     }
  177.  
  178.     $ds->unbind();
  179.  
  180.     $this->SUPER::shutdown(@_);
  181. }
  182.                 
  183.  
  184. sub load {}
  185.  
  186.  
  187. sub remove {
  188.     return 1;
  189. }
  190.  
  191.  
  192. sub save {
  193.     return 1;
  194. }
  195.  
  196.  
  197. 1
  198.