#!/usr/bin/perl -wT

#----------------------------------------------------------------------
# heading     : Administration
# description : Sherpath-SME accounts
# navigation  : 3000 3100
#
# Copyright (c) 2001 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 e-smith, inc.
# Please visit our web site www.e-smith.com for details.
#----------------------------------------------------------------------
#######################################################################
# INSTALLATION
#######################################################################
# To install this script do:
# cp $ThisScriptName /etc/e-smith/web/functions/
# ln -s ../../../functions/$ThisScriptName /etc/e-smith/web/panels/manager/cgi-bin/$ThisScriptName
# chmod 4750 /etc/e-smith/web/functions/$ThisScriptName
# chown root.admin /etc/e-smith/web/functions/$ThisScriptName
#######################################################################
#----------------------------------------------------------------------
# Otherwise it you won't run correctly
#----------------------------------------------------------------------
# Last Update 05-10-02 at 9:19pm
#######################################################################

package esmith;

use strict;
use CGI ':all';
use CGI::Carp qw(fatalsToBrowser);

use esmith::cgi;
use esmith::config;
use esmith::util;
use esmith::db;

sub showInitial ($$);
sub saveSettings($);

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

    $ENV {'PATH'} = '';
    $ENV {'SHELL'} = '/bin/bash';
    delete $ENV {'ENV'};
}

esmith::util::setRealToEffective ();

$CGI::POST_MAX=1024 * 100;  # max 100K posts
$CGI::DISABLE_UPLOADS = 1;  # no uploads

my %conf;
tie %conf, 'esmith::config';

my %accounts;
tie %accounts, 'esmith::config', '/home/e-smith/accounts';

#------------------------------------------------------------
# examine state parameter and display the appropriate form
#------------------------------------------------------------

my $q = new CGI;

if (! grep (/^state$/, $q->param))
{
    showInitial ($q, '');
}

elsif ($q->param ('state') eq 'saveSettings')
{
    saveSettings ($q);
}

else
{
    esmith::cgi::genStateError ($q, \%conf);
}

exit (0);

#------------------------------------------------------------
# subroutine to display initial form
#------------------------------------------------------------

sub showInitial ($$)
{
    my ($q, $msg) = @_;

    #------------------------------------------------------------
    # If there's a message, we just finished an operation so show the
    # status report. If no message, this is a new list of accounts.
    #------------------------------------------------------------

    if ($msg eq '')
    {
	esmith::cgi::genHeaderNonCacheable
	    ($q, \%conf, 'Sherpath-SME');
    }
    else
    {
	esmith::cgi::genHeaderNonCacheable
	    ($q, \%conf, 'Operation status report');

	print $q->p ($msg);
	print $q->hr;
    }

    #------------------------------------------------------------
    # Look up accounts and user names
    #------------------------------------------------------------

    my @userAccounts = ();

    foreach (sort keys %accounts)
    {
	push (@userAccounts, $_)
	    if (db_get_type(\%accounts, $_) eq "user");
    }

    	#print $q->p ($q->a ({href => $q->url (-absolute => 1) . "?state=create"},
	#	'Click here'),
	#	 'to create a user account.');

    unless (scalar @userAccounts)
    {
	print $q->p ($q->b ('There are no user accounts in the system.'));
    }
    ## Users
    my $default_create_account = ( 
    			($conf{'CreateSherpathAccountsWithSME'})
    			? ($conf{'CreateSherpathAccountsWithSME'}) 
    			: ('yes') );
    my $default_modify_account = ( 
    			($conf{'ModifySherpathAccountsWithSME'})
    			? ($conf{'ModifySherpathAccountsWithSME'}) 
    			: ('yes') );
    my $default_delete_account = ( 
    			($conf{'DeleteSherpathAccountsWithSME'})
    			? ($conf{'DeleteSherpathAccountsWithSME'}) 
    			: ('yes') );
    my $default_lock_account = ( 
    			($conf{'LockSherpathAccountsWithSME'})
    			? ($conf{'LockSherpathAccountsWithSME'}) 
    			: ('yes') );

    ## Groups
    my $default_create_group = (
    			($conf{'CreateSherpathGroupsWithSME'})
    			? ($conf{'CreateSherpathGroupsWithSME'}) 
    			: ('yes') );
    			
    my $default_modify_group = (
    			($conf{'ModifySherpathGroupsWithSME'})
    			? ($conf{'ModifySherpathGroupsWithSME'}) 
    			: ('yes') );
    
    my $default_delete_group = (
    			($conf{'DeleteSherpathGroupsWithSME'})
    			? ($conf{'DeleteSherpathGroupsWithSME'}) 
    			: ('yes') );
    			
    ## Password    			
    my $default_password_account = (
    			($conf{'PasswordSherpathAccountsWithSME'})
    			? ($conf{'PasswordSherpathAccountsWithSME'}) 
    			: ('yes') );
    
	my $description = <<END_TEXT;
This allows you to also create a Sherpath account 
when you create an account using the SME Server-Manager\'s "User accounts" 
panel. It also allows you to create Sherpath groups using "Groups" panel
<P>
You can also use the same password from SME for Sherpath accounts, but you 
need to be using the "userspwds" database.
END_TEXT

	print $q->p ($description);

	print $q->start_form(-method => 'POST', -action => $q->url (-absolute => 1)),
    	$q->p,
    	$q->start_table(-border=>0),
    		$q->caption($q->strong('User accounts')),
    		$q->Tr(
    			td({-align=>'LEFT'},'Create Sherpath account when SME account is created: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'create_sherpath_account',
						-values=>['yes','no'], -default=>$default_create_account)
			)
		),
		$q->Tr(
    			td({-align=>'LEFT'},'Modify Sherpath account when SME account is modified: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'modify_sherpath_account',
						-values=>['yes','no'], -default=>$default_modify_account)
			)
		),
		$q->Tr(
    			td({-align=>'LEFT'},'Delete Sherpath account when SME account is deleted: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'delete_sherpath_account',
						-values=>['yes','no'], -default=>$default_delete_account)
			)
		),
		$q->Tr(
    			td({-align=>'LEFT'},'Lock Sherpath account when SME account is locked: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'lock_sherpath_account',
						-values=>['yes','no'], -default=>$default_lock_account)
			)
		),
	$q->end_table(),
	
	$q->p,
    	$q->start_table(-border=>0),
    		$q->caption($q->strong('User passwords')),
    		$q->Tr(
    			td({-align=>'LEFT'},'Set Sherpath password when SME password is set: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'password_sherpath_account',
						-values=>['yes','no'], -default=>$default_password_account)
			)
		),		
	$q->end_table(),
	
	$q->p,
	
	$q->start_table(-border=>0),
    		$q->caption($q->strong('Groups')),
    		$q->Tr(
    			td({-align=>'LEFT'},'Create Sherpath group when SME group is created: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'create_sherpath_group',
						-values=>['yes','no'], -default=>$default_create_group)
			)
		),
		$q->Tr(
    			td({-align=>'LEFT'},'Modify Sherpath group when SME group is modified: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'modify_sherpath_group',
						-values=>['yes','no'], -default=>$default_modify_group)
			)
		),
		$q->Tr(
    			td({-align=>'LEFT'},'Delete Sherpath group when SME group is deleted: '),
    			td({-align=>'LEFT'},$q->radio_group(-override=>1, -name=>'delete_sherpath_group',
						-values=>['yes','no'], -default=>$default_delete_group)
			)
		),
	$q->end_table(),
	
	$q->p(submit('submit',' Save '), '&nbsp;'x5, $q->reset()),
	$q->hidden(-override=>1, -name=>'state', -value=>'saveSettings'),
	$q->endform;

    
    esmith::cgi::genFooter ($q);
    exit;
}

#------------------------------------------------------------

sub saveSettings ($)
{
	 my ($q) = @_;	
	 
	  #($hash, $key, $new_value, $hashref)
	if ( (db_set(\%conf, 'CreateSherpathAccountsWithSME', $q->param('create_sherpath_account')))
		and (db_set(\%conf, 'ModifySherpathAccountsWithSME', $q->param('modify_sherpath_account')))
		and (db_set(\%conf, 'DeleteSherpathAccountsWithSME', $q->param('delete_sherpath_account')))
		and (db_set(\%conf, 'PasswordSherpathAccountsWithSME', $q->param('password_sherpath_account')))
		and (db_set(\%conf, 'LockSherpathAccountsWithSME', $q->param('lock_sherpath_account'))) 
		and (db_set(\%conf, 'CreateSherpathGroupsWithSME', $q->param('create_sherpath_group'))) 
		and (db_set(\%conf, 'ModifySherpathGroupsWithSME', $q->param('modify_sherpath_group'))) 
		and (db_set(\%conf, 'DeleteSherpathGroupsWithSME', $q->param('delete_sherpath_group')))		
		)
	{	 
	 	showInitial ($q, 
	 		$q->font({-size=>'+1',-color=>'red'},'Settings Saved.'));
	}
	else
	{
	 	showInitial ($q, 
	 		$q->font({-size=>'+1',-color=>'red'},'Error Saving Settings.'));
	}
	exit;
}