????

Your IP : 3.147.57.145


Current Path : /scripts/
Upload File :
Current File : //scripts/unify_virtual_user_password_strengths

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/unify_virtual_user_password_strengths
#                                                  Copyright 2020 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::unify_virtual_user_password_strengths;

use strict;
use Cpanel::Config::CpConfGuard ();
use Cpanel::Logger              ();

_exit_usage() if @ARGV;

exit run() unless caller;

# Take the highest of the existing password strengths for pop, ftp, and webdisk, and save
# it as a new strength setting called 'virtual'. Delete the old settings.
sub run {
    my $cpconf_guard = Cpanel::Config::CpConfGuard->new();
    my $logger       = Cpanel::Logger->new();

    my $default_strength = $cpconf_guard->{data}{minpwstrength};

    my @existing_strengths;
    for my $setting_name (qw( minpwstrength_pop minpwstrength_ftp minpwstrength_webdisk )) {
        my $existing_value = delete $cpconf_guard->{data}{$setting_name};
        push @existing_strengths, $existing_value if defined $existing_value;
    }

    # If not all of pop/ftp/webdisk had existing values, but at least one did, then we should consider
    # setting minpwstrength_virtual to duplicate the default value, if it's the highest. On the other
    # hand, if none of pop/ftp/webdisk had existing values, then we will leave minpwstrength_virtual
    # unset, and it will continue to pull from the default.
    push @existing_strengths, $default_strength if 3 != @existing_strengths && 0 != @existing_strengths;

    # If this returns undef (because there were no existing strengths defined), then we will just delete
    # minpwstrength_(pop|ftp|wedisk) without setting minpwstrength_virtual.
    my $new_strength = _max(@existing_strengths);

    if ( defined( my $already_set = $cpconf_guard->{data}{minpwstrength_virtual} ) ) {
        $logger->info( sprintf 'Leaving virtual user minimum password strength at current value of %s.', $already_set );
    }
    elsif ( defined $new_strength ) {
        $cpconf_guard->{data}{minpwstrength_virtual} = $new_strength;
        $logger->info( sprintf 'Setting virtual user minimum password strength to %d.', $new_strength );
    }
    else {
        $logger->info('Leaving virtual user minimum password strength unset.');
    }

    $cpconf_guard->save;
    $logger->info('Done');
    return 0;
}

sub _max {
    my @nums = @_;
    my $max;
    for my $n (@nums) {
        $max = $n if !defined $max or $n > $max;
    }
    return $max;
}

sub _exit_usage {
    print <<EOF;
usage: $0

This script performs a one-time operation to create the new combined password strength
setting for virtual accounts (Email, FTP, Web Disk) based on the highest value from the
existing settings, and deletes the old ones. It is run automatically on initial upgrade
to 11.54. You may also run the script manually, but it will only perform the operation if
it has not already been done.
EOF
    exit 1;
}

1;