Restoring control panel access for mail users in Plesk 10

After a multi-step (and painful) upgrading and migrating of Plesk 7 server to the newest Plesk Panel 10 software, mailbox users stopped having the Control Panel access (https://servername.domain.com:8443) , and therefore could not change their passwords and mail forwarding settings. This Perl script re-creates the user accounts with the Mail User role. It also creates a Mail User role for a domain if it’s not existing yet (some of domains on my server have not had the Maul User role after the migration).

# file: /root/scripts/recreate_mail_user_accounts.pl
# Author: Stanislav Sinyagin <ssinyagin@k-open.com>
## the script reads all mailboxes for a domain and creates users with the same 
## passwords and Mail User roles
## There might be error messages if the Mail User exists already or if a user 
## account does not exist before deleting. They are quite normal and are harmless.

use strict;
use Getopt::Long;
use DBI;

my $domain;
my $limit;

my $ok = GetOptions('domain=s' => \$domain,
                    'limit=s' => \$limit);

if( not $ok or not defined($domain)  or
    scalar(@ARGV) > 0 )
{
    print STDERR "Usage: $0 --domain=DOMAIN [--limit=XXX]\n";

    exit 1;
}

my $dbpw = `cat /etc/psa/.psa.shadow`;
$dbpw =~ s/\s+//gm;

my $dbh = DBI->connect('DBI:mysql:database=psa',
                       'admin',
                       $dbpw,
                       {RaiseError => 1,
                        AutoCommit => 1});

die() unless $dbh;

my ($owner) = $dbh->selectrow_array
    ('SELECT clients.login ' .
     'FROM  domains, clients ' .
     'WHERE domains.cl_id=clients.id AND domains.name=\'' . $domain . '\'');

die("Cannot find owner for domain $domain") unless defined($owner);

my $cmd;
my $rcode;

$cmd = sprintf('/opt/psa/bin/role --create \'Mail User\' -owner %s',
               $owner);    
$rcode = system($cmd);

if( $rcode != 0 )
{
    print STDERR ("Error executing: \n" . $cmd);
    exit(1);
}

my $limit_clause = '';
if( defined($limit) )
{
    $limit_clause = ' AND mail.mail_name LIKE \'%' . $limit . '%\' ';
}

my $sth = $dbh->prepare
    ('SELECT mail.mail_name, password ' .
     'FROM accounts, mail, domains ' .
     'WHERE ' .
     ' mail.dom_id=domains.id AND ' .
     ' mail.account_id=accounts.id AND ' .
     ' domains.name = ? ' .
     $limit_clause .
     'ORDER BY mail.mail_name' );

$sth->execute($domain);

while(my ($mailname, $passwd) = $sth->fetchrow_array())
{
    my $account = $mailname . '@' . $domain;

    if( not defined($passwd) or length($passwd) == 0 )
    {
        $passwd = 'changeme000';
    }

    printf("-----\nProcessing user: %s\n", $account);

    $cmd = sprintf('/opt/psa/bin/user --delete %s', $account);    
    $rcode = system($cmd);

    if( $rcode != 0 )
    {
        print STDERR ("Error executing: \n" . $cmd);
        exit(1);
    }

    $cmd = sprintf('/opt/psa/bin/user --create %s -passwd \'%s\' -cname %s ' .
                   '-role "Mail User" -owner %s',
                   $account, $passwd, $account, $owner);

    $rcode = system($cmd);

    if( $rcode != 0 )
    {
        print STDERR ("Error executing: \n" . $cmd);
        exit(1);
    }

    printf("Finished user: %s\n-----\n", $account);
}
Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: