#!/home/product/code/webui-perl/bin/perl
use strict;
use warnings;
use Barracuda::Environment;

use Barracuda::Config;
use Barracuda::Encrypt::Utils;
use Barracuda::ProcessLock;
use MS_variable;

# Convert all user passwords to $ARGV[0] or the current firmware's
# encryption type.
#
# The DB is assumed to be using the encryption type stored in
# the user_password_format config var. The 'none' encryption type is
# supported.

my $pl = new Barracuda::ProcessLock('/tmp/convert_user_password.pid');
unless ($pl and $pl->plock()) {
    print STDERR "/tmp/convert_user_password.pid locked; already running?\n";
    exit(1);
}

my $new_encrypt = $ARGV[0]
  || $MS_variable::variable{'user_password'}->{'encrypt'}
  || 'none';

my $command_dir = $Barracuda::Environment::path_firmware . '/bin/';

Barracuda::Config::load();
my $old_encrypt = Barracuda::Config::get('user_password_format')
  || 'none';

Barracuda::Encrypt::Utils::convert_all('user_password',
                                       $old_encrypt,
                                       $new_encrypt);

Barracuda::Config::set('user_password_format', $new_encrypt);
Barracuda::Config::flush();

$pl->punlock();
