Index: scripts/crypt_setup.sh =================================================================== diff -u -rb9654575709e02aecc01a01d246d7af578679387 -rafc592ea044004b8b710c31bff8e7edde4e1d4c9 --- scripts/crypt_setup.sh (.../crypt_setup.sh) (revision b9654575709e02aecc01a01d246d7af578679387) +++ scripts/crypt_setup.sh (.../crypt_setup.sh) (revision afc592ea044004b8b710c31bff8e7edde4e1d4c9) @@ -100,6 +100,19 @@ checkOutput "mount -t ext4 $LOC_MAP $LOC_VAR" $ERR_CRYPTSETUP_MOUNT_MOUNT } +function reset_luks_partition() +{ + # TODO this needs some discussions. + + #umount_luks_partition + + # checkOutput "mkdir -p $LOC_VAR " $ERR_CRYPTSETUP_CREATE_MKDIR + # checkOutput "cryptsetup luksFormat $LOC_DEV " $ERR_CRYPTSETUP_CREATE_FORMAT $PASSWORD + # checkOutput "cryptsetup luksOpen $LOC_DEV $LOC_DIR" $ERR_CRYPTSETUP_CREATE_OPEN $PASSWORD + # checkOutput "mkfs.ext4 $LOC_MAP " $ERR_CRYPTSETUP_CREATE_MKFS + # checkOutput "mount -t ext4 $LOC_MAP $LOC_VAR" $ERR_CRYPTSETUP_CREATE_MOUNT +} + function handleCommand() { # Handle the first argument to script, one of start, stop, erase, restart, force-reload or status case $1 in @@ -117,6 +130,11 @@ unmount_luks_partition ;; + decommission) + checkPassword + reset_luks_partition + ;; + *) echo "unknown command" exit $ERR_CRYPTSETUP_UNKNOWN Index: scripts/decommission.sh =================================================================== diff -u -rb9654575709e02aecc01a01d246d7af578679387 -rafc592ea044004b8b710c31bff8e7edde4e1d4c9 --- scripts/decommission.sh (.../decommission.sh) (revision b9654575709e02aecc01a01d246d7af578679387) +++ scripts/decommission.sh (.../decommission.sh) (revision afc592ea044004b8b710c31bff8e7edde4e1d4c9) @@ -19,93 +19,195 @@ # @details # This file is called by UI Software to decommission the device -if [ $# -eq 0 ]; then - currentFile=$(basename "$0") - echo "Usage: ./$currentFile " - exit 1 -fi +#TODO This script needs to be integrated with the globals.sh -ERR_FAILED_RM_WIFI_CONF=150 -ERR_FAILED_CLEAR_BT_CACHE=151 -ERR_FAILED_RESET_SETTINGS_CONF=152 -ERR_FAILED_CLEAR_LOGS=153 -ERR_FAILED_RM_SSH=154 -ERR_FAILED_CLEAR_CLOUDSYNC_TOKEN=155 +ERR_REMOVE_PRM_DIR=150 +ERR_REMOVE_PRM_FIL=151 +ERR_REMOVE_CMD_DIR=152 +ERR_REMOVE_CMD_FIL=153 +ERR_REMOVE_DEL_DIR=154 +ERR_REMOVE_DEL_FIL=155 +ERR_CLEAN_SETTINGS_CONF_PASSNOTFOUNT=159 +ERR_CLEAN_SETTINGS_CONF_OVERWRITE=160 +ERR_CLEAN_SETTINGS_DFLT_RESTORE=161 -LOC_CLOUDSYNC_TOKEN=%1 -LOC_SETTINGS_CONF="/home/root/.config/Settings/System.conf" -LOC_DEFAULT_SETTINGS_CONF="/home/root/scripts/default_settings.conf" #TODO update to reflect real location -LOC_WIFI_CONF="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" -LOC_BLUETOOTH_CACHE_FOLDER="/var/lib/bluetooth/" -LOC_SSH_FOLDER="$HOME/.ssh/" -LOC_LOG_PARENT_FOLDER="/media/sd-card/" +ERR_CLEAN_LOGS=162 -# removes all the files in the directory and subdirectory in a recursive manner -# exits with passed error code if either rm command fails or folder is not empty of files -# %1 - Directory path -# %2 - Error message string/msg -# %3 - Error code to return on exit in case of error -function clearFolderContent() { - if [ -f $1 ]; then - rm -rf "$1/*/*" +ERR_COUDSYNC_TOKENS=163 + +LOC_WIFI_CONFIGURATION="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" +LOC_BLUETOOTH_DEVCACHE="/var/lib/bluetooth/" +LOC_SETTINGS_CONF="/var/configurations/Settings/System.conf" +LOC_SETTINGS_DFLT="/var/configurations/Settings/System.dflt" +LOC_LOG_BASE_FOLDER="/media/sd-card/" +LOC_COUDSYNC_TOKENS="/var/configurations/CloudSync/" + +TRUE=1 +FALSE=0 +function false() { echo $FALSE; } +function true () { echo $TRUE ; } + +# check if the passed argument is a non-zero number +function isNonZero () + # $1 - mutant: the argument to be detected +{ + local _ok_=$FALSE + if [[ $1 =~ ^[0-9]+$ ]]; then + if (( $1 )); then + _ok_=$TRUE + fi fi + echo $_ok_ +} - rmCommandReturn=$? - fileCount=$(find $1 -type f | wc -l) - - if [ "$fileCount" -ne 0 ] || [ "$rmCommandReturn" -ne 0 ]; then - echo "Error : $2 ($fileCount)" - exit $3 +# checks if the passed argument is a number +function isNumber () + # $1 - mutant: the argument to be detected +{ + local _ok_=$FALSE + if [[ $1 =~ ^[0-9]+$ ]]; then + _ok_=$TRUE fi + echo $_ok_ } -# Removes a single file -# exits with passed error code if either rm command fails or if file was not removed -# %1 - path to file -# %2 - Error message string/msg -# %3 - Error code to return on exit in case of error -function removeSingleFile() { - if [ -f $1 ]; then - rm "$1" +# checks if the passed argument is a directory +function isDirectory() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -d $1 ]]; then + _ok_=$TRUE fi - if [ -f $1 ]; then - echo "Error: $2" - # the file still exists, exit with error message - exit $3 + echo $_ok_ +} + +# checks if the passed argument is a file +function isFile() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -f $1 ]]; then + _ok_=$TRUE fi + echo $_ok_ } -# ------------------------------ Remove Wifi Conf -removeSingleFile $LOC_WIFI_CONF "Failed to remove WiFi conf file" $ERR_FAILED_RM_WIFI_CONF +# checks if the passed argument exists +function exits() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -e $1 ]]; then + _ok_=$TRUE + fi + echo $_ok_ +} -# ------------------------------ Remove Bluetooth cache -clearFolderContent $LOC_BLUETOOTH_CACHE_FOLDER "Failed to remove Bluetooth cache" $ERR_FAILED_CLEAR_BT_CACHE +# exits with the exit code if the condistion ($1) is non-zero +function exitError () + # $1 - number: boolean result to exit if non-zero, with exit code + # $1 - number: exit code + # $2 - string: echo message [optional] +{ + if (( $2 )); then + echo $3 + fi -# ------------------------------ Reset Settings Conf -cp $LOC_DEFAULT_SETTINGS_CONF $LOC_SETTINGS_CONF + if (( $(isNonZero $1) )); then + exit $2 + fi +} -# ------------------------------ Remove Logs -#TODO this will always fail due to the fact UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be >0 -clearFolderContent $LOC_LOG_PARENT_FOLDER "Failed to remove logs" $ERR_FAILED_CLEAR_LOGS +# removes all the files in the directory and subdirectory in a recursive manner +function clearFolderContent() + # $1 - Directory path + # $2 - Directory alias name +{ + local _has_error_=$[ ! $(isDirectory $1)] + exitError $_has_error_ $ERR_REMOVE_PRM_DIR "'$1' is not a directory" -# ------------------------------ Format Encrypted Partition -umountExitCode=$(source ./crypt_setup.sh umount) -if [ "$umountExitCode" -ne 0 ]; then - echo "Error: Failed to unmount partition" - exit $umountExitCode -fi + rm -rf "$1/*/*" + _has_error_=$? + exitError $_has_error_ $ERR_REMOVE_CMD_DIR "Failed $2 folder deletion" -setupExitCode=$(source ./crypt_setup.sh setup) -if [ "$setupExitCode" -ne 0 ]; then - echo "Error: format the partition" - exit $setupExitCode -fi + list=("$1"/*) + fileCount=${#list[@]} + _has_error_=$fileCount + exitError $_has_error_ $ERR_REMOVE_DEL_DIR "Remained '$fileCount' file(s) undeleted" +} -# ------------------------------ Remove CloudSync Token -clearFolderContent $LOC_CLOUDSYNC_TOKEN "Failed to remove CloudSync token" $ERR_FAILED_CLEAR_CLOUDSYNC_TOKEN +# exits rm command fails or if file was not removed +function removeSingleFile() + # $1 - path to file + # $2 - file alias name +{ + local _has_error_=$[ ! $(isFile $1)] + exitError $_has_error_ $ERR_REMOVE_PRM_FIL "'$1' is not a file" -# ------------------------------ Remove ssh -clearFolderContent $LOC_SSH_FOLDER "Failed to remove ssh" $ERR_FAILED_RM_SSH + rm "$1" + _has_error_=$? + exitError $_has_error_ $ERR_REMOVE_CMD_FIL "Failed $2 file deletion" + _has_error_=$(exists $1) + exitError $_has_error_ $ERR_REMOVE_DEL_FIL "Remained $2 file undeleted" +} + +function deleteWiFi() { + removeSingleFile $LOC_WIFI_CONFIGURATION "WiFi configuration" #delete WiFi +} + +function deleteBluetooth() { + clearFolderContent $LOC_BLUETOOTH_DEVCACHE "Bluetooth cache" #delete Bluetooth +} + +function defaultSettings() { + local _has_error_=$FALSE + + cp $LOC_SETTINGS_DFLT $LOC_SETTINGS_CONF # reset the settings to default settings + _has_error_=$? + exitError $_has_error_ $ERR_CLEAN_SETTINGS_CONF_OVERWRITE "Service settings overwrite filed" +} + +function deleteLogFiles() { + # TODO this will always fail due to the fact that + # UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be > 0 + clearFolderContent $LOC_LOG_BASE_FOLDER "logs" +} + +function deletePartitionPassword() { + # TODO it is not clear how + # and by the way it is not user dependent password toget reset + # it is managed by UI App and it will be working regardless of the user. + # ./crupt_setup +} + +function deleteCloudSyncTokens() { + # TODO this will always fail due to the fact that + # the security design, makes any Linux user responsible for each application/service + # therfore denali user does not have access to the cloud user files + # and cannot delete the CloudSync tokens and credentials. + clearFolderContent $LOC_COUDSYNC_TOKENS "CloudSync Tokens" $ERR_COUDSYNC_TOKENS +} + +# delete WiFi settings +# delete Bluetooth settings +# settings.conf to default (including service password) +# delete logs +# delete service password + +# delete partition password +# delete cloudsync tokens +function main() { + deleteWiFi + deleteBluetooth + defaultSettings + deleteLogFiles + deletePartitionPassword + deleteCloudSyncTokens +} + +main +echo "" exit 0 Index: scripts/factory_reset.sh =================================================================== diff -u -rb9654575709e02aecc01a01d246d7af578679387 -rafc592ea044004b8b710c31bff8e7edde4e1d4c9 --- scripts/factory_reset.sh (.../factory_reset.sh) (revision b9654575709e02aecc01a01d246d7af578679387) +++ scripts/factory_reset.sh (.../factory_reset.sh) (revision afc592ea044004b8b710c31bff8e7edde4e1d4c9) @@ -19,71 +19,182 @@ # @details # This file is called by UI Software to remove and reset the device -ERR_FAILED_RM_WIFI_CONF=150 -ERR_FAILED_CLEAR_BT_CACHE=151 -ERR_FAILED_RESET_SETTINGS_CONF=152 -ERR_FAILED_CLEAR_LOGS=153 -ERR_FAILED_RM_SSH=154 +#TODO This script needs to be integrated with the globals.sh -LOC_SETTINGS_CONF="/home/root/.config/Settings/System.conf" -LOC_DEFAULT_SETTINGS_CONF="/home/root/scripts/default_settings.conf" #TODO update to reflect real location -LOC_WIFI_CONF="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" -LOC_BLUETOOTH_CACHE_FOLDER="/var/lib/bluetooth/" -LOC_SSH_FOLDER="$HOME/.ssh/" -LOC_LOG_PARENT_FOLDER="/media/sd-card/" +ERR_REMOVE_PRM_DIR=150 +ERR_REMOVE_PRM_FIL=151 +ERR_REMOVE_CMD_DIR=152 +ERR_REMOVE_CMD_FIL=153 +ERR_REMOVE_DEL_DIR=154 +ERR_REMOVE_DEL_FIL=155 -# removes all the files in the directory and subdirectory in a recursive manner -# exits with passed error code if either rm command fails or folder is not empty of files -# %1 - Directory path -# %2 - Error message string/msg -# %3 - Error code to return on exit in case of error -function clearFolderContent() { - if [ -f $1 ]; then - rm -rf "$1/*/*" +ERR_CLEAN_SETTINGS_CONF_PASSNOTFOUNT=159 +ERR_CLEAN_SETTINGS_CONF_OVERWRITE=160 +ERR_CLEAN_SETTINGS_DFLT_RESTORE=161 + +ERR_CLEAN_LOGS=162 + +LOC_WIFI_CONFIGURATION="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" +LOC_BLUETOOTH_DEVCACHE="/var/lib/bluetooth/" +LOC_SETTINGS_CONF="/var/configurations/Settings/System.conf" +LOC_SETTINGS_DFLT="/var/configurations/Settings/System.dflt" +LOC_LOG_BASE_FOLDER="/media/sd-card/" + +TRUE=1 +FALSE=0 +function false() { echo $FALSE; } +function true () { echo $TRUE ; } + +# check if the passed argument is a non-zero number +function isNonZero () + # $1 - mutant: the argument to be detected +{ + local _ok_=$FALSE + if [[ $1 =~ ^[0-9]+$ ]]; then + if (( $1 )); then + _ok_=$TRUE + fi fi + echo $_ok_ +} - rmCommandReturn=$? - fileCount=$(find $1 -type f | wc -l) - - if [ "$fileCount" -ne 0 ] || [ "$rmCommandReturn" -ne 0 ]; then - echo "Error : $2 ($fileCount)" - exit $3 +# checks if the passed argument is a number +function isNumber () + # $1 - mutant: the argument to be detected +{ + local _ok_=$FALSE + if [[ $1 =~ ^[0-9]+$ ]]; then + _ok_=$TRUE fi -echo "$2 success" + echo $_ok_ } -# Removes a single file -# exits with passed error code if either rm command fails or if file was not removed -# %1 - path to file -# %2 - Error message string/msg -# %3 - Error code to return on exit in case of error -function removeSingleFile() { - if [ -f $1 ]; then - rm "$1" +# checks if the passed argument is a directory +function isDirectory() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -d $1 ]]; then + _ok_=$TRUE fi - if [ -f $1 ]; then - echo "Error: $2" - # the file still exists, exit with error message - exit $3 + echo $_ok_ +} + +# checks if the passed argument is a file +function isFile() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -f $1 ]]; then + _ok_=$TRUE fi + echo $_ok_ } -# ------------------------------ Remove Wifi Conf -removeSingleFile $LOC_WIFI_CONF "Failed to remove WiFi conf file" $ERR_FAILED_RM_WIFI_CONF +# checks if the passed argument exists +function exits() + # $1 - string: the argument to be detected +{ + local _ok_=$FALSE + if [[ -e $1 ]]; then + _ok_=$TRUE + fi + echo $_ok_ +} -# ------------------------------ Remove Bluetooth cache -clearFolderContent $LOC_BLUETOOTH_CACHE_FOLDER "Failed to remove Bluetooth cache" $ERR_FAILED_CLEAR_BT_CACHE +# exits with the exit code if the condistion ($1) is non-zero +function exitError () + # $1 - number: boolean result to exit if non-zero, with exit code + # $1 - number: exit code + # $2 - string: echo message [optional] +{ + if (( $2 )); then + echo $3 + fi -# ------------------------------ Reset Settings Conf -storedPassword=$(grep Password $LOC_SETTINGS_CONF) -cp $LOC_DEFAULT_SETTINGS_CONF $LOC_SETTINGS_CONF -sed -i "s/^.*Password.*$/$storedPassword/" $LOC_SETTINGS_CONF + if (( $(isNonZero $1) )); then + exit $2 + fi +} -# ------------------------------ Remove Logs -#TODO this will always fail due to the fact UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be >0 -clearFolderContent $LOC_LOG_PARENT_FOLDER "Failed to remove logs" $ERR_FAILED_CLEAR_LOGS +# removes all the files in the directory and subdirectory in a recursive manner +function clearFolderContent() + # $1 - Directory path + # $2 - Directory alias name +{ + local _has_error_=$[ ! $(isDirectory $1)] + exitError $_has_error_ $ERR_REMOVE_PRM_DIR "'$1' is not a directory" -# ------------------------------ Remove ssh -clearFolderContent $LOC_SSH_FOLDER "Failed to remove ssh" $ERR_FAILED_RM_SSH + rm -rf "$1/*/*" + _has_error_=$? + exitError $_has_error_ $ERR_REMOVE_CMD_DIR "Failed $2 folder deletion" -exit + list=("$1"/*) + fileCount=${#list[@]} + _has_error_=$fileCount + exitError $_has_error_ $ERR_REMOVE_DEL_DIR "Remained '$fileCount' file(s) undeleted" +} + +# exits rm command fails or if file was not removed +function removeSingleFile() + # $1 - path to file + # $2 - file alias name +{ + local _has_error_=$[ ! $(isFile $1)] + exitError $_has_error_ $ERR_REMOVE_PRM_FIL "'$1' is not a file" + + rm "$1" + _has_error_=$? + exitError $_has_error_ $ERR_REMOVE_CMD_FIL "Failed $2 file deletion" + + _has_error_=$(exists $1) + exitError $_has_error_ $ERR_REMOVE_DEL_FIL "Remained $2 file undeleted" +} + +function deleteWiFi() { + removeSingleFile $LOC_WIFI_CONFIGURATION "WiFi configuration" #delete WiFi +} + +function deleteBluetooth() { + clearFolderContent $LOC_BLUETOOTH_DEVCACHE "Bluetooth cache" #delete Bluetooth +} + +function defaultSettings() { + local _has_error_=$FALSE + + servicePasswordLine=$(grep -A1 "\[Service\]" "$LOC_SETTINGS_CONF" | grep Password) # store the service password line + _has_error_=$( [[ "$servicePasswordLine" ]] && $(true) || $(false) ) + exitError $_has_error_ $ERR_CLEAN_SETTINGS_CONF_PASSNOTFOUNT "Password not found in '$LOC_SETTINGS_CONF'" + + cp $LOC_SETTINGS_DFLT $LOC_SETTINGS_CONF # reset the settings to default settings + _has_error_=$? + exitError $_has_error_ $ERR_CLEAN_SETTINGS_CONF_OVERWRITE "Service settings overwrite filed" + + sed -i "s/^.*Password.*$/$storedPassword/" $LOC_SETTINGS_CONF # keep the service passowrd + _has_error_=$? + exitError $_has_error_ $ERR_CLEAN_SETTINGS_DFLT_RESTORE "Service password restore filed" +} + +function deleteLogFiles() { + # TODO this will always fail due to the fact that + # UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be > 0 + clearFolderContent $LOC_LOG_BASE_FOLDER "logs" +} + +# delete WiFi settings +# delete Bluetooth settings +# settings.conf to default +# delete logs +# keep partition password +# keep service password +# keep cloudsync tokens +function main() { + deleteWiFi + deleteBluetooth + defaultSettings + deleteLogFiles +} + +main +echo "" +exit 0