Thread Active Directory Abfrage (LDAP?) (10 answers)
Opened by Faldaar at 2007-09-28 17:39

Faldaar
 2007-10-05 16:32
#100364 #100364
User since
2003-11-05
14 Artikel
BenutzerIn
[default_avatar]
Stimme da eigentlich zu, dass es robust sein sollte.

Hätte da nochmal ne Frage zu Control::Paged. Wie geht man damit am sinnvollsten um? Hänge mal als Beispiel nen Code (inkl. Control::Paged) rein, der mir alle E-mail Adressen von allen Benutzern (die ein Exchange Postfach haben) ausgibt.

Muss gestehen, hab mich bis vor kurzem noch nicht so tief mit automatisierten AD Abfragen befasst.

Code (perl): (dl )
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
#!/usr/bin/perl -T -w

use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant ( "LDAP_CONTROL_PAGED" );

$dc1="172.xx.xx.xx";
$dc2="172.xx.xx.xx";

$hqbase="dc=dom,dc=local";

$user="cn=USER,cn=Users,dc=dom,dc=local";
$passwd="PASSWORD";

# Connecting to Active Directory domain controllers
$noldapserver=0;
$ldap = Net::LDAP->new($dc1) or
   $noldapserver=1;
if ($noldapserver == 1)  {
   $ldap = Net::LDAP->new($dc2) or
      die "Error connecting to specified domain controllers $@ \n";
}

$mesg = $ldap->bind ( dn => $user,
                      password =>$passwd);
if ( $mesg->code()) {
    die ("error:", $mesg->error_text((),"\n"));
}

# How many LDAP query results to grab for each paged round
# Set to under 1000 for Active Directory
$page = Net::LDAP::Control::Paged->new( size => 990 );

@args = ( base     => $hqbase,
         filter => "(& (mailnickname=*) (| (&(objectCategory=person)
                    (objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))
                    (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)
                    (msExchHomeServerName=*)))(&(objectCategory=person))
                     ))",
          control  => [ $page ],
          attrs  => "proxyAddresses",
);

my $cookie;
while(1) {
  # Perform search
  my $mesg = $ldap->search( @args );

# Filtering results for proxyAddresses attributes  
  foreach my $entry ( $mesg->entries ) {
    my $name = $entry->get_value( "cn" );
    # LDAP Attributes are multi-valued, so we have to print each one.
    foreach my $mail ( $entry->get_value( "proxyAddresses" ) ) {
      if ( $mail =~ s/^(smtp|SMTP)://gs ) {
        if ( ! ( $mail =~ /SystemMailbox/i ) ) {
          print $mail . "\n"; 
        }
      }
    }
  }

  # Only continue on LDAP_SUCCESS
  $mesg->code and last;

  # Get cookie from paged control
  my($resp)  = $mesg->control( LDAP_CONTROL_PAGED ) or last;
  $cookie    = $resp->cookie or last;

  # Set cookie in paged control
  $page->cookie($cookie);
}

if ($cookie) {
  # We had an abnormal exit, so let the server know we do not want any more
  $page->cookie($cookie);
  $page->size(0);
  $ldap->search( @args );
  # Also would be a good idea to die unhappily and inform OP at this point
     die("LDAP query unsuccessful");
}

exit(0);


Lässt sich das vielleicht irgendwie vereinfachen? Wenn ich da geschachtelte Abfragen habe sehe ich die Abfragen vor lauter Cookies nicht mehr :)

Martin
Es ist schwieriger eine vorgefasste Meinung zu zertrümmern als ein Atom. (Albert Einstein)

View full thread Active Directory Abfrage (LDAP?)