#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999, 2000 e-smith, inc.
#		
# 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 e-smith, inc.
# For details, please visit our web site at www.e-smith.com or
# call us on 1 888 ESMITH 1 (US/Canada toll free) or +1 613 564 8000
#----------------------------------------------------------------------

use strict;
use Errno;
use esmith::util;


=head1 NAME

sshd-conf-startup - enable or disable the ssh server

=head1 SYNOPSIS

  sshd-conf-startup

=head1 DESCRIPTION

Enables/disables the ssh server depending on the configuration of
sshd->status (defaults to disabled).

Also sets up some standard sshd config values to safe defaults if not
already set.

=begin testing

use esmith::ConfigDB;

$Destruct_Ok = 
  eval { esmith::ConfigDB->open->get('testing')->prop('destruction') } || 0;

my $Scratch = 'scratch.conf';
unlink $Scratch;
$ENV{ESMITH_CONFIG_DB} = $Scratch;
esmith::ConfigDB->create($Scratch);  # so the action has something
END { unlink $Scratch }

system $^X, $Original_File;
is( $@, '', 'ran myself ok' );

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

ok( my $sshd = $db->get('sshd'), 'sshd config exists' );

is( $sshd->prop('status'), 'disabled', 'sshd is disabled' );
is( $sshd->prop('PasswordAuthentication'), 'yes', 
                              "    PasswordAuth is set to the default" );
is( $sshd->prop('PermitRootLogin'),        'no',
                              "    PermitRootLogin is set to the default" );
is( $sshd->prop('access'),                 'private',
                              "    access is set to the default" );



SKIP: {
    skip "sshd reconfiguration would be destructive", 6
      unless $Destruct_Ok;

    # Use the real configuration.
    local $ENV{ESMITH_CONFIG_DB};

    skip "You have to be able to read the config DB to test this", 6
       unless $db = esmith::ConfigDB->open;

    # Call ourself.
    system $^X, $Original_File;
    is( $@, '', 'ran myself ok' );

    ok( my $sshd = $db->get('sshd'), 'sshd config exists' );

    foreach my $prop (qw(status access
                         PasswordAuthentication PermitRootLogin))
    {
        ok( $sshd->prop($prop),       "    $prop is defined" );
    }
}

=end testing


=cut

use esmith::ConfigDB;
my $conf = esmith::ConfigDB->open;

my $sshd = $conf->get("sshd") || 
           $conf->new_record("sshd", { type => 'service' }) ||
               warn("Could not add sshd to configuration db\n");

my $serviceStatus = $sshd->prop('status');
unless( $serviceStatus ) {
    $serviceStatus = 'disabled';
    $sshd->set_prop('status', 'disabled');
}

my %default_props = (
		  PasswordAuthentication => "yes",
		  PermitRootLogin        => "no",
		  access                 => "private"
		);

my %props = $sshd->props;
$sshd->reset_props(%default_props, %props);
	
my $action = ($serviceStatus eq 'enabled') ? 'enable' : 'disable';

esmith::util::serviceControl(
                                NAME=>'sshd',
                                ACTION=>$action,
                            ) || warn ("Couldn't $action service sshd");


exit (0);
