- fix user@0.service failed to start [SME: 12568] - stop loging in audit crond success - drop cpu and use esmith:util::ldap [SME: 12663]
		
			
				
	
	
		
			112 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/perl -w
 | 
						|
#----------------------------------------------------------------------
 | 
						|
# copyright (C) 1999-2005 Mitel Networks Corporation
 | 
						|
# 
 | 
						|
# This program is free software; you can redistribute it and/or modify
 | 
						|
# it under the terms of the GNU General Public License as published by
 | 
						|
# the Free Software Foundation; either version 2 of the License, or
 | 
						|
# (at your option) any later version.
 | 
						|
# 		
 | 
						|
# This program is distributed in the hope that it will be useful,
 | 
						|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
# GNU General Public License for more details.
 | 
						|
# 		
 | 
						|
# You should have received a copy of the GNU General Public License
 | 
						|
# along with this program; if not, write to the Free Software
 | 
						|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 | 
						|
#----------------------------------------------------------------------
 | 
						|
package esmith;
 | 
						|
 | 
						|
use strict;
 | 
						|
use Errno;
 | 
						|
use esmith::AccountsDB;
 | 
						|
use esmith::ConfigDB;
 | 
						|
use esmith::util;
 | 
						|
use utf8;
 | 
						|
use esmith::util::ldap;
 | 
						|
 | 
						|
my $conf = esmith::ConfigDB->open or die "Could not open configuration db";
 | 
						|
 | 
						|
my $ldapauth = $conf->get('ldap')->prop('Authentication') || 'disabled';
 | 
						|
my $x = 0; # exit value
 | 
						|
 | 
						|
# prepare LDAP bind
 | 
						|
my $ldap=esmith::util::ldap->new();
 | 
						|
 | 
						|
my $event = $ARGV [0];
 | 
						|
my $userName = $ARGV [1];
 | 
						|
 | 
						|
#------------------------------------------------------------
 | 
						|
# Check the Unix account
 | 
						|
#------------------------------------------------------------
 | 
						|
 | 
						|
 | 
						|
my $a = esmith::AccountsDB->open or die "Could not open accounts db";
 | 
						|
 | 
						|
my @users;
 | 
						|
if ($event eq 'bootstrap-ldap-save')
 | 
						|
{
 | 
						|
    @users = $a->users;
 | 
						|
}
 | 
						|
else
 | 
						|
{
 | 
						|
    die "Username argument missing." unless defined ($userName);
 | 
						|
    my $u = $a->get($userName) or die "No account db record found for user $userName";
 | 
						|
    @users = ($u);
 | 
						|
}
 | 
						|
foreach my $u (@users)
 | 
						|
{
 | 
						|
    my $type = $u->prop('type');
 | 
						|
    my $userName = $u->key;
 | 
						|
 | 
						|
    die "Account $userName is not a user account; modify user failed.\n"
 | 
						|
	unless ( ($userName eq 'admin') or ($type eq 'user') );
 | 
						|
 | 
						|
    setpwent;
 | 
						|
    my ($comment, $shell) = (getpwnam($userName))[6,8];
 | 
						|
    endpwent;
 | 
						|
    my $new_shell = $u->prop('Shell')
 | 
						|
		|| (($shell eq "/bin/sshell") ? "/usr/bin/false" : $shell);
 | 
						|
 | 
						|
    $u->set_prop('Shell', $new_shell) unless (not defined $u->prop('Shell') && $new_shell eq "/usr/bin/false" ) ;
 | 
						|
 | 
						|
    my $result;
 | 
						|
    #------------------------------------------------------------
 | 
						|
    # Modify user's shell, if required, in /etc/passwd using "usermod"
 | 
						|
    #------------------------------------------------------------
 | 
						|
    unless ($shell eq $new_shell)
 | 
						|
    {
 | 
						|
        if ($ldapauth ne 'enabled')
 | 
						|
        {
 | 
						|
            system("/usr/sbin/usermod", '-s', "$new_shell", $userName) == 0
 | 
						|
                or ( $x = 255, warn "Failed to modify shell of (unix) account $userName.\n" );
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    #------------------------------------------------------------
 | 
						|
    # Modify user's first name and last name if required,
 | 
						|
    # in /etc/passwd using "usermod"
 | 
						|
    #------------------------------------------------------------
 | 
						|
    my $first = stringToASCII($u->prop('FirstName') || "");
 | 
						|
    my $last = stringToASCII($u->prop('LastName') || "");
 | 
						|
    my $new_comment = "$first $last";
 | 
						|
 | 
						|
    unless ($comment eq $new_comment)
 | 
						|
    {
 | 
						|
        if ($ldapauth ne 'enabled')
 | 
						|
        {
 | 
						|
            system("/usr/sbin/usermod", "-c", "$first $last", $userName) == 0
 | 
						|
                or ( $x = 255, warn "Failed to modify comment of (unix) account $userName.\n" );
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    # we do all the test in ldap pm to avoid 3 differents write access, which are costly.
 | 
						|
    $result = $ldap->ldapuser($u);
 | 
						|
    $result && ( $x = $ldapauth ne 'enabled' ? $x : 255, warn "Failed to modify email of (ldap) account $userName.\n" );
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
$ldap->unbind;
 | 
						|
exit ($x);
 |