#!/bin/bash
# Rescapp Chpasswd run script
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Adrian Gibanel Lopez
#
# Rescapp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rescapp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rescapp.  If not, see <http://www.gnu.org/licenses/>.

source ${RESCAPP_LIB_FILE}

set -x
set -v

# Change selected user's password from the chosen Linux partition
# 1 parametre = Selected partition
# 2 parametre = Selected user
# 3 parametre = Chosen password
function rtux_Passwd () {

  local EXIT_VALUE=1 # Error by default
  local SELECTED_PARTITION="$1"
  local SELECTED_USER="$2"
  local CHOSEN_PASS="$3"

  local n_partition=${SELECTED_PARTITION}

  local TMP_MNT_PARTITION=${RESCATUX_ROOT_MNT}/${n_partition}
  local TMP_DEV_PARTITION=/dev/${n_partition}
  mkdir --parents ${TMP_MNT_PARTITION}
  if $(mount -t auto ${TMP_DEV_PARTITION} ${TMP_MNT_PARTITION} 2> /dev/null)
    then
    mount -o bind /dev ${TMP_MNT_PARTITION}/dev
    mount -o bind /proc ${TMP_MNT_PARTITION}/proc
    mount -o bind /sys ${TMP_MNT_PARTITION}/sys

    if [[ -e ${TMP_MNT_PARTITION}${LINUX_OS_DETECTOR} ]] ; then

      chroot ${TMP_MNT_PARTITION} passwd ${SELECTED_USER} <<EOF
${CHOSEN_PASS}
${CHOSEN_PASS}
EOF
      EXIT_VALUE=$?

    fi # Linux detector was found
    umount ${TMP_MNT_PARTITION}/sys
    umount ${TMP_MNT_PARTITION}/proc
    umount ${TMP_MNT_PARTITION}/dev
    umount ${TMP_MNT_PARTITION};
  fi # Partition was mounted ok

  return ${EXIT_VALUE}

} # function rtux_Passwd ()

# Change selected user's password from the chosen Linux partition
# 1 parametre = Selected partition
# 2 parametre = Selected user
# 3 parametre = Chosen password
function rtux_MetaChpasswd () {

  local EXIT_VALUE=1 # Error by default
  local SELECTED_PARTITION="$1"
  local SELECTED_USER="$2"
  local CHOSEN_PASS="$3"

  if ! rtux_Run_Show_Progress "${PASSWD_RUNNING_STR} (ChPasswd)" rtux_ChPasswd ${SELECTED_PARTITION} ${SELECTED_USER} ${CHOSEN_PASS}; then
    rtux_Run_Show_Progress "${PASSWD_RUNNING_STR} (Passwd)" rtux_Passwd ${SELECTED_PARTITION} ${SELECTED_USER} ${CHOSEN_PASS}
  else
    return 0
  fi

} # function rtux_MetaChpasswd ()

# Change selected user's password from the chosen Linux partition
# 1 parametre = Selected partition
# 2 parametre = Selected user
# 3 parametre = Chosen password
function rtux_Chpasswd () {

  local EXIT_VALUE=1 # Error by default
  local SELECTED_PARTITION="$1"
  local SELECTED_USER="$2"
  local CHOSEN_PASS="$3"

  local n_partition=${SELECTED_PARTITION}

  local TMP_MNT_PARTITION=${RESCATUX_ROOT_MNT}/${n_partition}
  local TMP_DEV_PARTITION=/dev/${n_partition}
  mkdir --parents ${TMP_MNT_PARTITION}
  if $(mount -t auto ${TMP_DEV_PARTITION} ${TMP_MNT_PARTITION} 2> /dev/null)
    then
    mount -o bind /dev ${TMP_MNT_PARTITION}/dev
    mount -o bind /proc ${TMP_MNT_PARTITION}/proc
    mount -o bind /sys ${TMP_MNT_PARTITION}/sys

    if [[ -e ${TMP_MNT_PARTITION}${LINUX_OS_DETECTOR} ]] ; then

      echo "${SELECTED_USER}:${CHOSEN_PASS}" | chpasswd -m --root ${TMP_MNT_PARTITION}
      EXIT_VALUE=$?
		  
    fi # Linux detector was found
    umount ${TMP_MNT_PARTITION}/sys
    umount ${TMP_MNT_PARTITION}/proc
    umount ${TMP_MNT_PARTITION}/dev
    umount ${TMP_MNT_PARTITION};
  fi # Partition was mounted ok

  return ${EXIT_VALUE}

} # function rtux_Chpasswd ()

# TODO: Program check runtime (Maybe to be stolen from bootinfoscript)

# MAIN PROGRAM

PASSWD_CHANGED_OK_STR="The password was changed OK! :). We recommend that you change it once you're back in your system, to use your systems default encryption."
PASSWD_NOT_CHANGED_STR="The password was not changed. Something went wrong! :("
PASSWD_RUNNING_STR="Changing GNU/Linux password."
WHICH_USER_PASS_STR="Which user do you want to change the password for?"
PASSWD_GNU_LINUX_PARTITIONS_NOT_FOUND="No GNU/Linux partitions were found or chosen."
PASSWD_LINUX_USERS_NOT_FOUND="No GNU/Linux users were found or chosen."
PASSWD_PASS_NOT_FOUND="No password was entered."

SELECTED_PARTITION=$(rtux_Choose_Linux_partition);
if [ "x${SELECTED_PARTITION}" != "x" ] ; then

    TMP_MNT_PARTITION=${RESCATUX_ROOT_MNT}/${SELECTED_PARTITION}
    TMP_DEV_PARTITION=/dev/${SELECTED_PARTITION}
    mkdir --parents ${TMP_MNT_PARTITION}
    mount -t auto ${TMP_DEV_PARTITION} ${TMP_MNT_PARTITION} 2> /dev/null
    SYSTEM_USERS=$(rtux_User_List "${TMP_MNT_PARTITION}/etc/passwd")
    umount ${TMP_MNT_PARTITION};

    SELECTED_USER=$(rtux_Choose_User ${SYSTEM_USERS});
    if [ "x${SELECTED_USER}" != "x" ] ; then
        CHOSEN_PASS=$(rtux_Enter_Pass ${SELECTED_USER})
        if [ "x${CHOSEN_PASS}" != "x" ] ; then
            if rtux_Run_Show_Progress "${PASSWD_RUNNING_STR} (Meta)" rtux_MetaChpasswd ${SELECTED_PARTITION} ${SELECTED_USER} ${CHOSEN_PASS}; then
                rtux_Message_Success ${PASSWD_CHANGED_OK_STR}
            else
                rtux_Message_Failure ${PASSWD_NOT_CHANGED_STR}
            fi
        else
            rtux_Message_Warning ${PASSWD_PASS_NOT_FOUND}
            rtux_Message_Failure ${PASSWD_NOT_CHANGED_STR}
        fi
    else
        rtux_Message_Warning ${PASSWD_LINUX_USERS_NOT_FOUND}
        rtux_Message_Failure ${PASSWD_NOT_CHANGED_STR}
    fi
else
    rtux_Message_Warning ${PASSWD_GNU_LINUX_PARTITIONS_NOT_FOUND}
    rtux_Message_Failure ${PASSWD_NOT_CHANGED_STR}
fi


