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

View File

@ -28,30 +28,18 @@ use esmith::util;
use esmith::templates;
use esmith::AccountsDB;
use esmith::ConfigDB;
use Net::LDAP;
use utf8;
use esmith::util::ldap;
my $conf = esmith::ConfigDB->open_ro
or die "Could not open Config DB";
my $ldapauth = $conf->get('ldap')->prop('Authentication') || 'disabled';
my $x = 0; # exit value
my $domain = $conf->get('DomainName')
|| die("Couldn't determine domain name");
$domain = $domain->value;
my $result;
# prepare LDAP bind
my $pw = esmith::util::LdapPassword();
my $base = esmith::util::ldapBase ($domain);
my $ldap = Net::LDAP->new('localhost')
or die "$@";
$ldap->bind(
dn => "cn=root,$base",
password => $pw
);
my $ldap=esmith::util::ldap->new();
$ENV{'PATH'} = "/bin";
@ -108,45 +96,23 @@ if ($event eq 'ibay-create')
#------------------------------------------------------------
# add new ibay group to ldap
#------------------------------------------------------------
$result = $ldap->add("cn=$ibayName,ou=Groups,$base",
attrs => [
"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" );
$result = $ldap->ldapgroup($ibay);
$result && ( $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",
attrs => [
"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" );
$result = $ldap->ldapuser($ibay);
$result && ( $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")
#------------------------------------------------------------
foreach my $grp ( 'shared', $ibay->prop("Group") ) {
$result = $ldap->modify("cn=$grp,ou=Groups,$base",
add => {
"memberUid" => [ $ibayName ]
});
my @groupMembers = ($ibayName);
$result = $ldap->ldapaddgroupmembers($grp,\@groupMembers);
# 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.
@ -168,12 +134,6 @@ if ($event eq 'ibay-create')
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')
{
@ -190,23 +150,17 @@ elsif ($event eq 'ibay-modify' and $ibayName ne 'Primary')
#------------------------------------------------------------
# Modify ibay description in ldap"
#------------------------------------------------------------
$result = $ldap->modify("uid=$ibayName,ou=Users,$base",
replace => {
"cn" => $ibay->prop("Name"),
}
);
$result->code && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to modify email of (ldap) account $ibayName.\n" );
$result = $ldap->ldapuser($ibay);
$result && ( $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")
#------------------------------------------------------------
foreach my $grp ( 'shared', $ibay->prop("Group") ) {
$result = $ldap->modify("cn=$grp,ou=Groups,$base",
add => {
"memberUid" => [ $ibayName ]
});
my @groupMembers = ($ibayName);
$result = $ldap->ldapaddgroupmembers($grp,\@groupMembers);
# 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
Name: %{name}
%define version 11.0.0
%define release 8
%define release 9
Version: %{version}
Release: %{release}%{?dist}
License: GPL
@ -13,6 +13,7 @@ Source: %{name}-%{version}.tar.xz
BuildRoot: /var/tmp/%{name}-%{version}-%{release}-buildroot
BuildArchitectures: noarch
Requires: smeserver-lib >= 11.0.0-7
Requires: smeserver-base
Requires: perl(CGI::FormMagick)
Requires: smeserver-formmagick
@ -27,6 +28,9 @@ Provides: e-smith-ibays
smeserver server and gateway software - ibays module.
%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
- edit LDAP entries using Net::LDAP rather than cpu [SME: 12687]