* Tue Aug 13 2024 Jean-Philippe Pialasse <jpp@koozali.org> 11.0.0-9.sme

-  use esmith::util:ldap to manipulate ldap entries [SME: 12687]
This commit is contained in:
Jean-Philippe Pialasse 2024-08-13 17:42:52 -04:00
parent e61e1a8096
commit f99cce8bae
3 changed files with 29 additions and 84 deletions

View File

@ -28,8 +28,9 @@ use strict;
use Errno; use Errno;
use esmith::ConfigDB; use esmith::ConfigDB;
use esmith::util; use esmith::util;
use Net::LDAP;
use esmith::AccountsDB; use esmith::AccountsDB;
use utf8;
use esmith::util::ldap;
my $adb = esmith::AccountsDB->open_ro(); my $adb = esmith::AccountsDB->open_ro();
@ -41,22 +42,8 @@ unless ($conf->get('ldap')->prop('status') eq "enabled" )
exit(0); exit(0);
} }
my $domain = $conf->get('DomainName')
|| die("Couldn't determine domain name");
$domain = $domain->value;
# prepare LDAP bind # prepare LDAP bind
my $pw = esmith::util::LdapPassword(); my $ldap=esmith::util::ldap->new();
my $base = esmith::util::ldapBase ($domain);
my $ldap = Net::LDAP->new('localhost')
or die "$@";
$ldap->bind(
dn => "cn=root,$base",
password => $pw
);
my $event = $ARGV [0]; my $event = $ARGV [0];
my $ibay = $ARGV [1]; my $ibay = $ARGV [1];
@ -88,10 +75,10 @@ if ( "$ldapauth" ne "enabled" )
} }
} }
my $result = $ldap->delete("uid=$ibay,ou=Users,$base"); my $result = $ldap->ldapdeluser($ibay);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to delete (ldap) ibay account $ibay.\n" ); $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to delete (ldap) ibay account $ibay.\n" );
$result = $ldap->delete("cn=$ibay,ou=Groups,$base"); $result = $ldap->ldapdelgroup($ibay);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to delete (ldap) group account $ibay.\n" ); $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to delete (ldap) group account $ibay.\n" );
exit $x exit $x

View File

@ -28,30 +28,18 @@ use esmith::util;
use esmith::templates; use esmith::templates;
use esmith::AccountsDB; use esmith::AccountsDB;
use esmith::ConfigDB; use esmith::ConfigDB;
use Net::LDAP; use utf8;
use esmith::util::ldap;
my $conf = esmith::ConfigDB->open_ro my $conf = esmith::ConfigDB->open_ro
or die "Could not open Config DB"; or die "Could not open Config DB";
my $ldapauth = $conf->get('ldap')->prop('Authentication') || 'disabled'; my $ldapauth = $conf->get('ldap')->prop('Authentication') || 'disabled';
my $x = 0; # exit value my $x = 0; # exit value
my $domain = $conf->get('DomainName')
|| die("Couldn't determine domain name");
$domain = $domain->value;
my $result; my $result;
# prepare LDAP bind # prepare LDAP bind
my $pw = esmith::util::LdapPassword(); my $ldap=esmith::util::ldap->new();
my $base = esmith::util::ldapBase ($domain);
my $ldap = Net::LDAP->new('localhost')
or die "$@";
$ldap->bind(
dn => "cn=root,$base",
password => $pw
);
$ENV{'PATH'} = "/bin"; $ENV{'PATH'} = "/bin";
@ -108,45 +96,23 @@ if ($event eq 'ibay-create')
#------------------------------------------------------------ #------------------------------------------------------------
# add new ibay group to ldap # add new ibay group to ldap
#------------------------------------------------------------ #------------------------------------------------------------
$result = $ldap->add("cn=$ibayName,ou=Groups,$base", $result = $ldap->ldapgroup($ibay);
attrs => [ $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to create (ldap) group $ibayName.\n" );
"cn"=> $ibayName,
"gidNumber"=> $ibay->prop("Gid"),
"objectClass" => [ 'posixGroup', 'mailboxRelatedObject']
]);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to create (ldap) group $ibayName.\n" );
#------------------------------------------------------------ #------------------------------------------------------------
# add new ibay user to ldap # add new ibay user to ldap and lock password
#------------------------------------------------------------ #------------------------------------------------------------
$result = $ldap->add("uid=$ibayName,ou=Users,$base", $result = $ldap->ldapuser($ibay);
attrs => [ $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to create (ldap) account $ibayName.\n" );
"uidNumber" => $ibay->prop("Uid"),
"gidNumber" => $ibay->prop("Gid"),
"cn" => $ibay->prop("Name"),
"objectClass" => [ 'account', 'posixAccount', 'shadowAccount'],
"homeDirectory" => "/home/e-smith/files/ibays/$ibayName",
"loginShell" => "/bin/false",
"shadowExpire" => -1,
"shadowFlag" => 134538308,
"shadowInactive" => -1,
"shadowLastChange" => 15997,
"shadowMax" => 99999,
"shadowMin" => -1,
"shadowWarning"=> 7,
]
);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to create (ldap) account $ibayName.\n" );
#------------------------------------------------------------ #------------------------------------------------------------
# Loop to add new user to groups "shared,". $ibay->prop("Group") # Loop to add new user to groups "shared,". $ibay->prop("Group")
#------------------------------------------------------------ #------------------------------------------------------------
foreach my $grp ( 'shared', $ibay->prop("Group") ) { foreach my $grp ( 'shared', $ibay->prop("Group") ) {
$result = $ldap->modify("cn=$grp,ou=Groups,$base", my @groupMembers = ($ibayName);
add => { $result = $ldap->ldapaddgroupmembers($grp,\@groupMembers);
"memberUid" => [ $ibayName ]
});
# error code 20 is entry already exits. # error code 20 is entry already exits.
$result->code && ( $result->code != 20 ) && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to add (ldap) account $ibayName to supplementary group $grp.\n" ); $result && ( $result != 20 ) && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to add (ldap) account $ibayName to supplementary group $grp.\n" );
} }
#------------------------------------------------------------ #------------------------------------------------------------
# Create the ibay files and set the password. # Create the ibay files and set the password.
@ -168,12 +134,6 @@ if ($event eq 'ibay-create')
or ( $x = 255, warn "Error locking (unix) account $ibayName" ); or ( $x = 255, warn "Error locking (unix) account $ibayName" );
} }
#------------------------------------------------------------
# lock password in ldap
#------------------------------------------------------------
$result = $ldap->modify("uid=$ibayName,ou=Users,$base",
replace => { 'userPassword' => "{crypt}!*"});
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Error locking (ldap) account $ibayName.\n" );
} }
elsif ($event eq 'ibay-modify' and $ibayName ne 'Primary') elsif ($event eq 'ibay-modify' and $ibayName ne 'Primary')
{ {
@ -190,23 +150,17 @@ elsif ($event eq 'ibay-modify' and $ibayName ne 'Primary')
#------------------------------------------------------------ #------------------------------------------------------------
# Modify ibay description in ldap" # Modify ibay description in ldap"
#------------------------------------------------------------ #------------------------------------------------------------
$result = $ldap->modify("uid=$ibayName,ou=Users,$base", $result = $ldap->ldapuser($ibay);
replace => { $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to create (ldap) account $ibayName.\n" );
"cn" => $ibay->prop("Name"),
}
);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to modify email of (ldap) account $ibayName.\n" );
#------------------------------------------------------------ #------------------------------------------------------------
# Loop to add new user to groups "shared,". $ibay->prop("Group") # Loop to add new user to groups "shared,". $ibay->prop("Group")
#------------------------------------------------------------ #------------------------------------------------------------
foreach my $grp ( 'shared', $ibay->prop("Group") ) { foreach my $grp ( 'shared', $ibay->prop("Group") ) {
$result = $ldap->modify("cn=$grp,ou=Groups,$base", my @groupMembers = ($ibayName);
add => { $result = $ldap->ldapaddgroupmembers($grp,\@groupMembers);
"memberUid" => [ $ibayName ]
});
# error code 20 is entry already exits. # error code 20 is entry already exits.
$result->code && ( $result->code != 20 ) && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to add (ldap) account $ibayName to supplementary group $grp.\n" ); $result && ( $result != 20 ) && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to add (ldap) account $ibayName to supplementary group $grp.\n" );
} }
} }

View File

@ -4,7 +4,7 @@ Summary: smeserver server and gateway - ibays module
%define name smeserver-ibays %define name smeserver-ibays
Name: %{name} Name: %{name}
%define version 11.0.0 %define version 11.0.0
%define release 8 %define release 9
Version: %{version} Version: %{version}
Release: %{release}%{?dist} Release: %{release}%{?dist}
License: GPL License: GPL
@ -13,6 +13,7 @@ Source: %{name}-%{version}.tar.xz
BuildRoot: /var/tmp/%{name}-%{version}-%{release}-buildroot BuildRoot: /var/tmp/%{name}-%{version}-%{release}-buildroot
BuildArchitectures: noarch BuildArchitectures: noarch
Requires: smeserver-lib >= 11.0.0-7
Requires: smeserver-base Requires: smeserver-base
Requires: perl(CGI::FormMagick) Requires: perl(CGI::FormMagick)
Requires: smeserver-formmagick Requires: smeserver-formmagick
@ -27,6 +28,9 @@ Provides: e-smith-ibays
smeserver server and gateway software - ibays module. smeserver server and gateway software - ibays module.
%changelog %changelog
* Tue Aug 13 2024 Jean-Philippe Pialasse <jpp@koozali.org> 11.0.0-9.sme
- use esmith::util:ldap to manipulate ldap entries [SME: 12687]
* Sat May 18 2024 Jean-Philippe Pialasse <jpp@koozali.org> 11.0.0-8.sme * Sat May 18 2024 Jean-Philippe Pialasse <jpp@koozali.org> 11.0.0-8.sme
- edit LDAP entries using Net::LDAP rather than cpu [SME: 12687] - edit LDAP entries using Net::LDAP rather than cpu [SME: 12687]