From d59661ba17ae716b7a5a59d8d6913605a9081646 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 29 May 2020 23:54:14 -0400 Subject: [PATCH] Added specials and error checking --- share/adapters/chmod.sh | 54 ++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) mode change 100644 => 100755 share/adapters/chmod.sh diff --git a/share/adapters/chmod.sh b/share/adapters/chmod.sh old mode 100644 new mode 100755 index 13064d7..2eba35b --- a/share/adapters/chmod.sh +++ b/share/adapters/chmod.sh @@ -3,6 +3,7 @@ # Contributed by Erez Binyamin (github.com/ErezBinyamin) # Translate between chmod string and number +# Inspired by http://permissions-calculator.org/ # Contrib to chubin - cheat.sh chmod_calc(){ p_s="" @@ -10,23 +11,42 @@ chmod_calc(){ R=() W=() X=() + S=() + setuid=' ' + setgid=' ' + sticky=' ' # If permission number is given calc string - if [[ $1 =~ ^-?[0-9]+$ ]] + if [[ $1 =~ ^-?[0-9]+$ ]] && [ ${#1} -ge 2 ] && [ ${#1} -le 4 ] then p_n=$1 for (( i=0; i<${#1}; i++ )) do num=$(echo "obase=2;${1:$i:1}" | bc | xargs printf '%03d') - [ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-' - [ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-' - [ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-' - [ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ') - [ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ') - [ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ') + if [ ${#1} -eq 4 ] && [ $i -eq 0 ] + then + [ ${num:0:1} -eq 1 ] && setuid='X' || setuid=' ' + [ ${num:1:1} -eq 1 ] && setgid='X' || setgid=' ' + [ ${num:2:1} -eq 1 ] && sticky='X' || sticky=' ' + else + [ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-' + [ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-' + if [[ $sticky == 'X' ]] + then + [ ${num:2:1} -eq 1 ] && p_s+='s' || p_s+='S' + else + [ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-' + fi + [ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ') + [ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ') + [ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ') + fi done # If permission string is given calc number - else + elif [ ${#1} -le 9 ] && [ $(( ${#1} % 3 )) -eq 0 ] && [[ $1 =~ ^[r,w,x,s,S,-]+$ ]] + then p_s=$1 + [[ ${p_s,,} =~ 's' ]] && p_n+="1" || p_n+="0" + [[ ${p_s,,} =~ 's' ]] && sticky='X' for (( i=0; i<${#1}; i+=0 )) do num=0 @@ -36,20 +56,26 @@ chmod_calc(){ [[ ${1:$i:1} == 'w' ]] && W+=('X') || W+=(' ') [[ ${1:$((i++)):1} == 'w' ]] && let num++ num=$(( num << 1 )) - [[ ${1:$i:1} == 'x' ]] && X+=('X') || X+=(' ') - [[ ${1:$((i++)):1} == 'x' ]] && let num++ + [[ 'xs' =~ ${1:$i:1} ]] && X+=('X') || X+=(' ') + [[ 'Ss' =~ ${1:$i:1} ]] && S+=('X') || S+=(' ') + [[ 'xs' =~ ${1:$((i++)):1} ]] && let num++ p_n+="$num" done + else + printf "Invalid permissions string: $1" + return 1 fi + # Print Final results table printf " Linux Permissions String:\t${p_s} Linux Permissions Number:\t${p_n} -Owner\t\tGroup\t\tPublic +Special\t\tOwner\t\tGroup\t\tPublic + +Setuid [$setuid]\tRead [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}] +Setgid [$setgid]\tWrite [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}] +Sticky bit [$sticky]\tExecute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}] -Read [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}] -Write [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}] -Execute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}] " }