#!/bin/sh
# Written by hip0
# Fri Aug 13 17:35:21 EEST 2004
# This script is copylefted under General Public License (GPL) 2.0 and above
# for more info www.gnu.org.
# The script uses tsh which is a tiny shell client with strong blowfish crypt,
# and a password key crypted into the tsh binary which is being given by a compile time
# in the tsh.h file whom is in the source directory of tshd.
# It is strongly recommended fot the /usr/sbin/tsh binary ( or whereever it the client bin is
# to be readable just by the user which will execute this script. In any
# other case you'll have a big security hole since averybody can send commands
# to the server using the tsh binary and the tsh server on the other side run as root.
# So you'll have a full server compromise.

# The machine where tsh listens for connections.
remote_machine='213.91.245.1';
# The apache document root.
apache_client_dir='/var/lib/apache/htdocs/clients';
# The Employyes samba home directory.
samba_employee_home='/samba';
# Path where the smbpasswd binary is located.
path_to_smbpasswd='/usr/bin/smbpasswd';
# Directory for a project default files.
project_skele='/disk2/project-skele';
# Path where the client binary is located. 
tsh_path=/usr/sbin/tsh
# argument assignments.
string0="$0";
string1="$1";
string2="$2";
string3="$3";
string4="$4";
string5="$5";
string6="$6";
# get argument count
string_num="$#";
#echo "string1:$1 string2:$2 string3:$3 string4:$4 string5:$5 string6:$6" >> /usr/local/apache/htdocs/clients/exec-output

# check parameters require a function
param_checks () {
if [[ $string_num == 0 ]]; then 
 echo -e "No Arguments supplied.";
 echo "Syntax is:";
 echo "`basename $string0` -cn [client_name]";
 echo "`basename $string0` -en [employee_name] -ep [employee_samba_password]";
 echo "`basename $string0` -cn [client_name] -pn [project_name] -mn [manager_name]";
 echo "`basename $string0` -cn [client_name] -pn [project_name] -en [employee_name]";
fi

if [[ $string1 == '-cn' ]] && [[ -z $string3 ]] && [[ -z $string5 ]]; then
  add_client;
fi

if [[ $string1 == '-en' ]] && [[ $string3 == '-ep' ]]; then
 add_employee_name;
fi

if [[ $string1 == '-cn' ]] && [[ $string3 == '-pn' ]] &&\
[[ $string5 == '-mn' ]]; then
 add_project;
fi

if [[ $string1 == '-cn' ]] && [[ $string3 == '-pn' ]] &&\
[[ $string5 == '-en' ]]; then
 check_existence;
 create_s_link;
fi
}

# create Clients directories in the apache tree.
add_client () {

$tsh_path $remote_machine "if [[ ! -d $apache_client_dir/$string2/ ]]; then /bin/mkdir $apache_client_dir/$string2; /bin/chown -R httpd.httpd $apache_client_dir/$string2; fi";

}

# adds employee to the remote machine's /etc/passwd and then adds the user to the samba using
# smbpasswd
add_employee_name () {
$tsh_path $remote_machine "/usr/sbin/useradd $string2 -d $samba_employee_home/$string2 -s /bin/false; $path_to_smbpasswd -a $string2 -n; $path_to_smbpasswd -e $string2; if [ ! -d $samba_employee_home/$string2 ]; then /bin/mkdir $samba_employee_home/$string2; /bin/chown -R $string2.users $samba_employee_home/$string2; fi ; if [ ! -d $samba_employee_home/$string2/clients ]; then /bin/mkdir $samba_employee_home/$string2/clients; /bin/chown -R $string2.users $samba_employee_home/$string2/clients; fi;"


}

# check for employee client's dir existence if not exist creates them.
check_existence () {
$tsh_path $remote_machine "if [[ ! -d $samba_employee_home/$string6/clients/$string2 ]]; then /bin/mkdir $samba_employee_home/$string6/clients/$string2; fi";


}

# Create symbolic link in the user's samba home directory and adds a symbolic link
# to the document root of the project.
create_s_link () {


$tsh_path $remote_machine "cd $samba_employee_home/$string6/clients/$string2; if [ ! -s "$samba_employee_home/$string6/clients/$string2/$string4" ]; then /bin/ln -sf $apache_client_dir/$string2/$string4 $string4; fi";

}


# creates new project ( a.k.a. creates project directories in the apache document root and
# copies the skele files needed for it.
add_project () {
$tsh_path $remote_machine "if [[ ! -d $apache_client_dir/$string2/$string4 ]]; then /bin/mkdir $apache_client_dir/$string2/$string4; fi";

$tsh_path $remote_machine "if [[ -d $project_skele ]] && [[ -d $apache_client_dir/$string2/$string4 ]]; then /bin/cp $project_skele/* $apache_client_dir/$string2/$string4; chown -R httpd.httpd $apache_client_dir/$string2/$string4; fi";
check_existence;

create_s_link;

}

# main function
main () {

param_checks;

}
main;

