diff --git a/module.lib b/module.lib index d8219c9..7bf53f7 100644 --- a/module.lib +++ b/module.lib @@ -9,7 +9,9 @@ local NAME DIR=$1 NAME=$2 # echo finding $NAME in $DIR -FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh)" +# TODO use file instead for valid extensions +# allow explicit extension +FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)" # echo files found $FILE COUNT=0 if [ "$FILE" ]; then @@ -79,7 +81,8 @@ FILE=$(module_find $1) [ $? -ne 0 ] && echo no module $1 found && return 1 # source $FILE "$0" # [[ $BASHPID -eq $$ ]] || echo $FILE -source $FILE +shift 1 +source $FILE "$@" return 0 } diff --git a/modules/utility/dirs.sh b/modules/utility/dirs.sh index ce215d1..d7a092f 100644 --- a/modules/utility/dirs.sh +++ b/modules/utility/dirs.sh @@ -30,12 +30,14 @@ usage() echo "Options:" echo " -d DIRPERMS, directory permissions" echo " -f FILEPERMS, file permissions" - exit 1 + return 1 + } # Check if user entered arguments if [ $# -lt 1 ] ; then usage + return 1 fi # Get options @@ -62,7 +64,7 @@ ROOT=$1 # Check if the root path is a valid directory if [ ! -d $ROOT ] ; then - echo "$ROOT does not exist or isn't a directory!" ; exit 1 + echo "$ROOT does not exist or isn't a directory!" ; return 2 fi # Recursively set directory/file permissions based on the permission variables diff --git a/modules/utility/file.lib b/modules/utility/file.lib index be58838..de0b625 100644 --- a/modules/utility/file.lib +++ b/modules/utility/file.lib @@ -1,7 +1,7 @@ #!/bin/bash # export BASH_DEBUG=true -# module_load debug +module_load debug module_load iecho isFile() { @@ -226,4 +226,4 @@ rm -f "$tmp_fn" return 0 # [End] -} \ No newline at end of file +} diff --git a/modules/utility/filesystem.mod b/modules/utility/filesystem.mod index 3b16651..88e2e22 100644 --- a/modules/utility/filesystem.mod +++ b/modules/utility/filesystem.mod @@ -5,7 +5,7 @@ mounted () { mountpoint "$1" &> /dev/null && echo yes || return 1 } -# peals back sub-directories until if finds a mountpoing +# peals back sub-directories until if finds a mountpoint find_mountpoint () { local dir=$1 if [[ ! $dir = "/" ]]; then diff --git a/modules/utility/helpers.mod b/modules/utility/helpers.mod deleted file mode 100644 index bd3b820..0000000 --- a/modules/utility/helpers.mod +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -# must be json as a string, depends on jq -get_prop_value () { - local value - # echo in $1 get $2 - value=$(echo $1 | jq -r .$2) - echo $value -} - -is_array() { - local variable_name=$1 - [[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]] -} - -filename() { - # passed entire path - echo $(basename "$1" | rev | cut -f 2- -d '.' | rev) -} - -adirname() { - # passed entire path - echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )" -} - -user_exists() { - id -u $1 > /dev/null 2>&1 - [[ $? == 1 ]] || echo $1 - } - - -chmod_dirs() { - # passed entire path - local usesudo - [[ $1 == -s ]] && usesudo="sudo" && shift 2 - $usesudo find $1 -type f -exec chmod $2 {} + -} - - - diff --git a/modules/utility/path.sh b/modules/utility/path.sh deleted file mode 100644 index 106c9fb..0000000 --- a/modules/utility/path.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -# get full absolute bath from a current or parent path -function abs-path { - local target="$1" - if [ "$target" == "." ]; then - echo "$(pwd)" - elif [ "$target" == ".." ]; then - echo "$(dirname "$(pwd)")" - else - echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" - fi -} - -isAbsPath() { - if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]] - then - echo "true" - return 0 - else - return 1 - fi -} diff --git a/modules/utility/user.sh b/modules/utility/user.sh index 6ad1d54..3e8aa7b 100755 --- a/modules/utility/user.sh +++ b/modules/utility/user.sh @@ -4,109 +4,141 @@ function clone_user_ () { -module_load confirm -type confirm + module_load confirm -echo "=============" -echo "this script will create a new user" -echo "based on an existing user's data" -echo -echo "You will be shown a list of users who can currently log on" -echo "Remember which user you would like to clone." -echo "You will be asked for the new user's name, their password" -echo "and the old user to clone". -echo "=============" -echo + echo "=============" + echo "this script will create a new user" + echo "based on an existing user's data" + echo + echo "You will be shown a list of users who can currently log on" + echo "Remember which user you would like to clone." + echo "You will be asked for the new user's name, their password" + echo "and the old user to clone". + echo "=============" + echo -echo -n "New user's name: " -read newuser + echo -n "New user's name: " + read newuser -echo -n "New user's password: " -read newpassword + echo -n "New user's password: " + read newpassword -echo + echo -echo "Current users you can clone:" -echo "----" -awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd -echo + echo "Current users you can clone:" + echo "----" + awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd + echo -echo -n "Old user to clone: " -read olduser -echo "olduser uid is $(id -u $olduser)" + echo -n "Old user to clone: " + read olduser + echo "olduser uid is $(id -u $olduser)" -echo -echo "You have selected: " -echo "----" -echo "new user: $newuser" -echo "new user password: $newpassword" -echo "old user: $olduser" -echo + echo + echo "You have selected: " + echo "----" + echo "new user: $newuser" + echo "new user password: $newpassword" + echo "old user: $olduser" + echo -olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser" -olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd) + olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser" + olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd) -echo "old user groups: " -echo "----" -echo $olduser_GROUPS -echo "olduser shell: " -echo $olduser_SHELL -# read -rsp $'Press any key to continue or ctrl-c to exit...\n' -n1 key -# return -# confirm "ready to clone user, begin?" || return 1 + echo "old user groups: " + echo "----" + echo $olduser_GROUPS + echo "olduser shell: " + echo $olduser_SHELL -# useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser + confirm "ready to clone user, begin?" || return 1 -# echo $newuser:$newpassword | chpasswd + useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser -# read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key + echo $newuser:$newpassword | chpasswd -# mkdir /home/$newuser -# chown -R $newuser:$newuser /home/$newuser + read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key -# echo -# echo "Script should be done now." -# echo -# echo "Do you see your new users name below?" -# echo -# awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd + mkdir /home/$newuser + chown -R $newuser:$newuser /home/$newuser -# echo -# echo "We are now going to copy the old user's home folder to the new user" -# echo "then change ownership to the new user" -# echo -# read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key + echo + echo "Script should be done now." + echo + echo "Do you see your new users name below?" + echo + awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd + echo + echo "We are now going to copy the old user's home folder to the new user" + echo "then change ownership to the new user" + echo + read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key -rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/ -chown -R --from=$olduser $newuser:$newuser /home/$newuser + rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/ + chown -R --from=$olduser $newuser:$newuser /home/$newuser -echo -echo "Now we are going to change the names of files and folders to the new user" -echo + echo + echo "Now we are going to change the names of files and folders to the new user" + echo -grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g' + grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g' -echo -echo "Done now." -echo -read -rsp $'Press any key to exit...\n' -n1 key -echo -echo + echo + echo "Done now." + echo + read -rsp $'Press any key to exit...\n' -n1 key + echo + echo } clone_user () { -if [[ $EUID != 0 ]]; then + if [[ $EUID != 0 ]]; then - clone=$(declare -f clone_user_) - module_load confirm - confirm=$(declare -f confirm) - sudo bash -c "$confirm; $clone; clone_user_" - else - echo run as regular user with sudo privliges and it will elevate -fi + clone=$(declare -f clone_user_) + module_load confirm + confirm=$(declare -f confirm) + sudo bash -c "$confirm; $clone; clone_user_" + else + echo run as regular user with sudo privliges and it will elevate + fi } -# # if script was executed then call the function -(return 0 2>/dev/null) || clone_user \ No newline at end of file +rename_user () { + + module_load confirm + local force; local name; local newname; local newhome + + [[ $1 = "-f" ]] && { force=true; shift 1; } + [[ $1 = "-h" ]] && { newhome=$2; shift 2; } + # usage: < -f, -h newhome > name newname + # default new user home is /home/newname + name=$1 + newname=$2 + if [[ ! ($name && $newname) ]]; then + echo "=============" + echo "this script will rename an existing user" + echo "user running this script must to root or have sudo priviledges to run" + echo "---- Available Users to Rename ---" + awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd + echo "=============" + read -r -p "Enter an available user name: " name + read -r -p "Enter users new name: " newname + read -r -p "Enter users new home directory : " newhome + fi + + newhome=${newhome:-/home/$newname} + + if [[ ! $force ]]; then + echo "Changing $name to $newname with home $newhome" + echo sudo usermod -l $newname $name + echo sudo groupmod -n $newname $name + echo sudo usermod --d $newhome --m $name + confirm -s "These are the commands that will be run. Do you want to continue?" || return 1 + fi + + sudo usermod -l $newname $name + echo sudo groupmod -n $newname $name + echo sudo usermod --d $newhome --m $name +}