diff options
-rwxr-xr-x | src/ubpasswd.sh | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/src/ubpasswd.sh b/src/ubpasswd.sh index 1a32ef4..9fea52d 100755 --- a/src/ubpasswd.sh +++ b/src/ubpasswd.sh @@ -6,8 +6,22 @@ # and salt. But the password is printed # for reference. # ubpasswd -h will print usage. +# +# Command changes the password based on the following +# user list. The first user in this list that is found in +# /etc/shadow is changed. root is only changed if mtadm +# or admin is not found. +ADMINLIST="mtadm admin root" +MTS=/sys/devices/platform/mts-io + +if (( $(id -u) != 0 )) ; then + echo "Must have root privilege to procede." + exit 1 +fi + if ! [[ -x /sbin/mts-hashpwd ]] ; then echo Need /sbin/mts-hashpwd to proceed. + exit 1 fi usage() { echo "ubpasswd [-u] [-d] [-s salt] [password]" @@ -54,9 +68,6 @@ fi len=8 saltlen=128 -mts=/sys/devices/platform/mts-io -did=$(cat "${mts}/device-id") -mac=$(cat "${mts}/mac-eth") if ! /bin/fgrep "mts password protected" /dev/mtdblock2 >/dev/null 2>&1; then echo "U-Boot does not support password protection." @@ -71,8 +82,19 @@ echo "salt: $salt" ((debug)) && echo upwd is $upwd ((debug)) && echo did length is ${#did} if ((upwd == 0)) ; then + if ! [[ -f "${MTS}/device-id" ]] ; then + echo "${MTS}/device-id not found. Maybe mts-io driver is not loaded?" + exit 1 + fi + if ! [[ -f "${MTS}/mac-eth" ]] ; then + echo "${MTS}/mac-eth not found. Maybe mts-io driver is not loaded?" + exit 1 + fi + + did=$(cat "${MTS}/device-id") + mac=$(cat "${MTS}/mac-eth") if ((${#did} == 0)) ; then - echo "${mts}/device-id must have a non-zero length value" + echo "${MTS}/device-id must have a non-zero length value" usage fi if ((${#mac} == 0)) ; then @@ -99,8 +121,24 @@ pass="${BASH_REMATCH[1]}" password_hash="${BASH_REMATCH[2]}" echo "uboot password hash: \"$password_hash\"" if ((ubonly == 0)) ; then - echo "setting root password to ${pass}" - echo -e "${pass}\n${pass}" | /usr/bin/passwd >/dev/null 2>&1 + rstring="^"${ADMINLIST// /:|^}":" + list=" "$(egrep "${rstring}" /etc/shadow | sed 's/:.*//')" " + for u in $ADMINLIST ; do + ((debug)) && echo "list is: $list, u: is $u" + if [[ $list =~ [[:space:]]${u}[[:space:]] ]] ; then + USER="${u}" + ((debug)) && echo Change password of $USER + break + fi + done + if ((${#USER} == 0)) ; then + echo "No administrator user found" + echo "Cannot change the administrator password" + echo "Suggest using the \"-u\" option" + exit 1 + fi + echo "setting ${USER} password to ${pass}" + echo -e "${pass}\n${pass}" | /usr/bin/passwd "${USER}" >/dev/null 2>&1 fi echo "u-boot password is ${pass}" set -x |