#!/usr/bin/perl ######################### # Configuration Options # ######################### # Address of the LDAP server $ldapServer="127.0.0.1"; # Server's base DN $baseDN="dc=domain,dc=com"; # DN to bind to the server with to make changes $ldapServerAdminDN="cn=Manager,dc=domain,dc=com"; # Password for that user $ldapServerAdminPass="secret"; #Addressbook organizational unit $addressbookOU="addressbook"; # Path to the vuserinfo program $vuserinfo= "/home/vpopmail/bin/vuserinfo"; ################################ # End of Configuration Options # ################################ use Net::LDAP; # Parse the data passed to the script $numArgs=$#ARGV+1; if ($numArgs != 2 ) { die "Script called with wrong number of arguments\n"; } else { $onchangeCommand=$ARGV[0]; $onchangeArg=$ARGV[1]; } #Open up a connection to the LDAP server $ldap = Net::LDAP->new ("$ldapServer") or die "$@"; $mesg = $ldap->bind( "$ldapServerAdminDN", password => "$ldapServerAdminPass" ); if ($onchangeCommand eq "add_domain") { # Domain was added - create an addressbook for it $domain = $onchangeArg; print "vpopLDAPaddress --> Adding $domain\n"; #Create the OU for the addressbooks: $result = $ldap->add( "ou=$addressbookOU,$baseDN", attr => [ 'objectClass' => ['top','organizationalUnit' ], 'ou' => "$addressbookOU", ] ); $result->code && warn "Failed to create addressbook OU: ", $result->error; #Create the base Distinguished Name (DN) for the domain that was added $result = $ldap->add( "ou=$domain,ou=$addressbookOU,$baseDN", attr => [ 'objectClass' => ['top','organizationalUnit' ], 'ou' => "$domain", ] ); $result->code && warn "failed to add domain: ", $result->error; } elsif ($onchangeCommand eq "del_domain") { # Domain was deleted - remove its addressbook $domain = $onchangeArg; print "vpopLDAPaddress --> Deleting $domain\n"; $mesg = $ldap->delete( "ou=$domain,ou=$addressbookOU,$baseDN" ); } elsif ($onchangeCommand eq "add_user") { # User added - create an entry for their email address $emailAddress = $onchangeArg; print "vpopLDAPaddress --> Adding $emailAddress\n"; # Make sure the user exists $userCheck = `$vuserinfo $emailAddress -n`; if ( $userCheck =~ /no such user/ ) { die "Email address $emailAddress does not exist.\n"; } ($userName,$domain) = split /@/, $emailAddress; # Determine the user's fullname from the gecos field # If a fullname isn't there, vuserinfo will return the username portion of the email address $userFullname = `$vuserinfo -c $emailAddress`; chomp $userFullname; ($userFirstname,$userLastname) = split / /, $userFullname; #Add the LDAP entry for the user $result = $ldap->add( "cn=$userFullname,ou=$domain,ou=$addressbookOU,$baseDN", attr => [ 'objectClass' => ['top','person','organizationalPerson','inetOrgPerson' ], 'cn' => "$userFullname", 'gn' => "$userFirstname", 'sn' => "$userLastname", 'mail' => "$emailAddress", ] ); $result->code && warn "failed to add domain: ", $result->error; } elsif ($onchangeCommand eq "del_user") { # User deleted - remove their addressbook entry $emailAddress = $onchangeArg; print "vpopLDAPaddress --> Deleting $emailAddress\n"; # Make sure the user exists $userCheck = `$vuserinfo $emailAddress -n`; if ( $userCheck =~ /no such user/ ) { die "Email address $emailAddress does not exist.\n"; } ($userName,$domain) = split /@/, $emailAddress; # Determine the user's fullname from the gecos field # If a fullname isn't there, vuserinfo will return the username portion of the email address $userFullname = `$vuserinfo -c $emailAddress`; chomp $userFullname; ($userFirstname,$userLastname) = split / /, $userFullname; # Delete the LDAP entry for the user $mesg = $ldap->delete( "cn=$userFullname,ou=$domain,ou=$addressbookOU,$baseDN"); } elsif ($onchangeCommand eq "mod_user") { # User modified - do we need to change the addressbook? } # Close the LDAP server connection $mesg = $ldap->unbind; # The End!