#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2001-2006 by moleSoftware GmbH - http://www.molesoftware.com
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-db-passwd 2505 2010-01-06 20:30:53Z nuxwin $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "VHCS - Virtual Hosting Control System".
#
# The Initial Developer of the Original Code is moleSoftware GmbH.
# Portions created by Initial Developer are Copyright (C) 2001-2006
# by moleSoftware GmbH. All Rights Reserved.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

use FindBin;
use lib "$FindBin::Bin/";
require 'ispcp_common_code.pl';
use strict;
use warnings;

sub db_passwd_start_up {
	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'db_passwd_start_up()', 'Starting...');

	# config check;
	$rs = get_conf();
	return $rs if ($rs != 0);

	push_el(\@main::el, 'db_passwd_start_up()', 'Ending...');
	return 0;
}

sub get_user_pass {
	my ($check_with) = @_;
	my ($rs, $rdata, $upass, $upass_check, $upass_current) = (undef, undef, undef, undef, undef);

	push_el(\@main::el, 'get_user_pass()', 'Starting...');

	if ($check_with eq '') {
		$upass = read_password('Please Enter Your New Database Password:');
		if ($upass eq '') {
			return (1, '');
		}
		$upass_check = read_password('Please Repeat Your New Database Password:');
		if ($upass ne $upass_check) {
			print STDERR ">>> Entered passwords differ. Please try again !\n";
			return (1, '');
			} else {
			return (0, $upass);
		}
	} else {
		$upass_current = read_password('Please Enter Your Current Password:');
		if ($upass_current ne $check_with) {
			print STDERR ">>> Entered password does not match. Please try again !\n";
			return (1, '');
		}
		$upass = read_password('Please Enter Your New Database Password:');
		if ($upass eq '') {
			return (0, '');
		}
		$upass_check = read_password('Please Repeat Your New Database Password:');
		if ($upass ne $upass_check) {
			print STDERR ">>> Entered passwords differ. Please try again !\n";
			return (1, '');
			} else {
			return (0, $upass);
		}
	}

	push_el(\@main::el, 'get_user_pass()', 'Ending...');
	return 0;
}

######################## main program ############################

my ($rs, $rdata) = (undef, undef);

$rs = db_passwd_start_up();
if ($rs != 0) {
	my $el_data = pop_el(\@main::el);
	my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

	print STDERR "$msg\n";
	exit 1;
}

my $current_db_pass = undef ;

if(exists  $main::cfg{'DATABASE_PASSWORD'} && $main::cfg{'DATABASE_PASSWORD'}) {
	$current_db_pass = $main::cfg{'DATABASE_PASSWORD'};
} else {
	$current_db_pass = '';
}

if ($current_db_pass eq '') { # We have not DATABASE password;
	($rs, $rdata) = get_user_pass('');
} else {
	($rs, $current_db_pass) = decrypt_db_password($current_db_pass);
	if ($rs != 0) {
		my $el_data = pop_el(\@main::el);
		my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

		print STDERR "$msg\n";
		exit 1;
	}
	($rs, $rdata) = get_user_pass($current_db_pass);
}

if ($rs == 0) {
	my $new_db_pass = $rdata;

	if ($new_db_pass ne '') {
		($rs, $rdata) = encrypt_db_password($new_db_pass);
		if ($rs != 0) {
			my $el_data = pop_el(\@main::el);
			my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

			print STDERR "$msg\n";
			exit 1;
		}
	}
	$rs = set_conf_val('DATABASE_PASSWORD', $rdata);
	if ($rs != 0) {
		my $el_data = pop_el(\@main::el);
		my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

		print STDERR "$msg\n";
		exit 1;
	}
	$rs = store_conf();
	if ($rs != 0) {
		my $el_data = pop_el(\@main::el);
		my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

		print STDERR "$msg\n";
		exit 1;
	}
	print STDOUT "Database Password Updated Successfully!\n";
}
