#!/bin/sh -e

##########################################################################
#   Script description:
#       Synchronize local password from head node to all relavant nodes
#       
#   History:
#   Date        Name        Modification
#   2015-06-11  J Bacon     Begin
##########################################################################

usage()
{
    printf "Usage: $0 username\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $# != 1 ]; then
    usage
fi

cluster-head-check $0

# This script is called from cluster-passwd, which runs suid root.
# Prevent malicious users from running imposter programs.
PATH=/usr/pkg/sbin:/usr/pkg/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

LOCALBASE=$(cluster-localbase)
user_name=$1
os_type=`auto-ostype`

case $os_type in
FreeBSD)
    master_pw=/etc/master.passwd
    ;;

RHEL)
    master_pw=/etc/shadow
    ;;

*)
    printf "$0: Not supported on $(auto-ostype).\n"
    exit 1
    ;;

esac

pw="`awk -F : '$1 == "'$user_name'" { print $2 }' $master_pw`"
if [ "0$pw" = 0 ]; then
    printf "No such user: $user_name\n"
    exit 1
fi

$LOCALBASE/sbin/cluster-run -c "auto-transfer-pw $user_name '$pw'" backup io vis compute
