#!/usr/bin/perl -wT

#----------------------------------------------------------------------
# copyright (C) 1999-2003 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
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
package esmith::console;

use strict;

use POSIX;
use Locale::gettext;
use Errno;

use esmith::util;

use esmith::ConfigDB;
use esmith::I18N;
use esmith::console;

use constant TRUE  => 1;
use constant FALSE => 0;

BEGIN
{
    # Set PATH explicitly and clear related environment variables so that calls
    # to external programs do not cause results to be tainted. See
    # "perlsec" manual page for details.

    $ENV {PATH} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin";
    $ENV {SHELL} = "/bin/bash";
    delete $ENV {ENV};
    delete $ENV {BASH_ENV};
}

my $console = esmith::console->new();

my $release = esmith::util::determineRelease();
my $release_string = "Mitel Networks server ${release}";


my $i18n = new esmith::I18N;
$i18n->setLocale("server-console");

my $db = esmith::ConfigDB->open;

#------------------------------------------------------------
INITIAL_PASSWORD:
#------------------------------------------------------------

{
    my ($rc, $choice);
    my $choice1;
    my $choice2;

    ($rc, $choice1) = $console->password_page
        (
         title => $release_string,
         text  =>
         gettext("Welcome to the server console!") .
         "\n\n" .
         gettext("You will now be taken through a sequence of screens " .
	 "to perform basic networking configuration on this server.") .
         "\n\n" .
         gettext("You can make your selections in each screen using the " .
	 "Arrow and Tab keys. At any point, if you select Back you will be " .
	 "returned to the previous screen.") .
         "\n\n" .
         gettext("Before you start, you must first choose the administrator " .
	 "password for your system and enter it below. You will not see the " .
	 "password as you enter it."),
        );

    unless ($rc == 0)
    {
        ($rc, $choice) = $console->message_page
            (
             title   => gettext("Administrator password not set"),
             text    => gettext("Sorry, you must set the administrator password now."),
            );

        goto INITIAL_PASSWORD;
    }

    unless ($choice1 =~ /^([ -~]+)$/)
    {
        ($rc, $choice) = $console->message_page
            (
             title   => gettext("Unprintable characters in password"),
             text    => gettext("The password must contain only printable characters."),
            );

        goto INITIAL_PASSWORD;
    }


    ($rc, $choice2) = $console->password_page
        (
         title   => $release_string,
         text    => gettext("Please type your administrator password again to verify."),
        );

    unless ($rc == 0)
    {
        ($rc, $choice) = $console->message_page
            (
             title => gettext("Administrator password not set"),
             text  => gettext("Sorry, you must set the administrator password now."),
            );

        goto INITIAL_PASSWORD;
    }

    if ($choice1 ne $choice2)
    {
        ($rc, $choice) = $console->message_page
            (
             title => gettext("Passwords do not match"),
             text  => gettext("The two passwords did not match"),
            );

        goto INITIAL_PASSWORD;
    }

    use Crypt::Cracklib;

    #--------------------------------------------------------
    # These are just to ensure that xgettext knows about the
    # Cracklib strings.
    # Note the extra space here and in the gettext call below. This
    # allows the French localization to properly generate qu'il
    gettext(" it is based on your username");
    gettext(" it is based upon your password entry");
    gettext(" it is derived from your password entry");
    gettext(" it is derivable from your password entry");
    gettext(" it is too short");
    gettext(" it is all whitespace");
    gettext(" it is too simplistic/systematic");
    gettext(" it is based on a dictionary word");
    gettext(" it is based on a (reversed) dictionary word");
    #--------------------------------------------------------

    my $reason = fascist_check($choice1, '/usr/lib/cracklib_dict');
    # Untaint return data from cracklib, so we can use it later. We
    # trust the library, so we accept anything.
    $reason =~ /(.+)/; $reason = $1;
    $reason ||= gettext("Software error: password check failed");
    unless ($reason eq 'ok')
    {
        ($rc, $choice) = $console->yesno_page
            (
             title => gettext("Bad Password Choice"),
             text  =>
             gettext("The password you have chosen is not a good choice, because") .
             gettext( " $reason" ) . "." .
             "\n\n" .
             gettext("Do you wish to choose a better one?"),
            );

        goto INITIAL_PASSWORD if ($rc == 0);
    }

    #--------------------------------------------------
    # Set system password
    #--------------------------------------------------

    esmith::util::setUnixSystemPassword ($choice1);
    esmith::util::setServerSystemPassword ($choice1);
}
exit 0;
