Bad filter in Active Directory: (!company=mycompany)

"A. Farber" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.ldap
Organization http://groups.google.com
Message-ID <16724828-7871-420e-aedd-193d07ed5886@m73g2000hsh.googlegroups.com>
Hello,

I've written a short script to prepend "ext-" to mail addresses
of all external colleagues in Microsoft Active Directory:

    filter  => '(&(objectCategory=Person)(objectClass=User))',
....
    $mail = "ext-$mail"
        if $entry->get_value('company') !~ /mycompany/i
            && $mail !~ /^ext-/;

For better performance I'd like to move
the company test to the search filter:

    filter  =>
'(&(objectCategory=Person)(objectClass=User)(!company=mycompany))',

Unfortunately it fails with: Bad filter at ....

If I remove "!" it works ok. If I try that filter with dsquery:

dsquery * domainroot -filter "(&(objectCategory=Person)
(objectClass=User)(!company=mycompany))

- then it works too. Has anybody seen the same problem?

Thank you
Alex


PS: Here is my entire script:

#!/usr/bin/perl -wT

use strict;
use Net::LDAPS;

$ENV{PATH} = '/bin:/usr/bin';

use constant ROOTDN   => 'OU=Imported,OU=User
Accounts,DC=mycompany,DC=com';
use constant DOMAIN   => 'mycompany.com';
use constant SERVER   => ['wdc01.' . DOMAIN, 'wdc02.' . DOMAIN];
use constant ADMIN    => 'Admin';
use constant ADMPW    => 'XXXXX';

my ($rot13, $ldap, $search, $mod, $href);

($rot13 = ADMPW) =~ y/A-Za-z/N-ZA-Mn-za-m/;
$ldap = Net::LDAPS->new(SERVER) or
        die('Can not connect to LDAP server');
$ldap->bind(ADMIN . '@' . DOMAIN, password => $rot13) or
        die('Can not bind to LDAP server as ' . ADMIN);

$search = $ldap->search(
    base    => ROOTDN,
    attrs   => [qw(company mail givenName sn)],
    filter  => '(&(objectCategory=Person)(objectClass=User)(!
company=mycompany))',
);
$search->code() && die $search->error();

foreach my $entry ($search->entries()) {
        my $mail = lc $entry->get_value('mail');

        # prepend "ext-" if not "mycompany" and "ext-" isn't there yet
        $mail = "ext-$mail"
             if $entry->get_value('company') !~ /mycompany/i
                  && $mail !~ /^ext-/;

        $mod = $ldap->modify($entry, replace => { mail => $mail });
        $mod->code() && die 'Failed to modify user: ' . $mod->error();

        printf "%-15s %-15s %-15s %s\n",
            $entry->get_value('company'),
            $entry->get_value('givenName'),
            $entry->get_value('sn'),
            $mail;
}

$ldap->unbind();
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.