Skip to content

Instantly share code, notes, and snippets.

@comerford
Forked from timbru31/openELEC.sh
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comerford/09c18e23633f4b66e773 to your computer and use it in GitHub Desktop.
Save comerford/09c18e23633f4b66e773 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# author: Tim "xGhOsTkiLLeRx" Brust
# license: CC BY-NC-SA 4.0
# version: 0.2
# date: 07/10/2014
# description: replace (root) password of squashfs from openELEC
# usage: ./openELEC [password] [device] [hash] [user]
# dependencies: mkpassword (whois), squashfs-tools
#
#
# THIS SCRIPT COMES WITH ZERO WARRANTY!
# I'M NOT RESPONSIBLE FOR ANY POTENTIAL DATA LOSS!
# ALWAYS CREATE BACKUPS!
# Configure colors, if available.
if [ -x /usr/bin/tput ] && tput setaf 1 >& /dev/null
then
reset="$(/usr/bin/tput sgr 0)"
green="$(/usr/bin/tput bold)$(/usr/bin/tput setaf 2)"
red="$(/usr/bin/tput bold)$(/usr/bin/tput setaf 1)"
yellow="$(/usr/bin/tput bold)$(/usr/bin/tput setaf 3)"
else
reset=
green=
red=
yellow=
fi
# Functions
function cleanUp() {
rm -rf /openelec
}
function message() {
echo -e $2"$1"$reset
}
function unmount() {
umount /openelec/original
umount /openelec/flash
}
# root check
if [ $EUID -ne 0 ]
then
message "Please run this script as root" $red
exit 1
fi
# whois (makepasswd) check
if ! type "mkpasswd" > /dev/null 2>&1
then
message "Please install the package $yellow'whois'$red to use mkpasswd" $red
exit 1
fi
# mksquashfs check
if ! type "mksquashfs" > /dev/null 2>&1
then
message "Please install the package $yellow'squashfs-tools'$red to use mksquashfs" $red
exit 1
fi
# Ask if we are using BerryBoot
read -p $red"Are you using BerryBoot? (y/N) $reset" berryboot
case "$berryboot" in
No|no|N|n|"")
berryboot=false
;;
*)
berryboot=true
;;
esac
# Check for password
if [ -z "$1" ]
then
message "No arguments given, please feed me input!\n" $yellow
read -p "Please enter the password you want to use: " passwd
else
passwd=$1
fi
# Check for device
if [ -z "$2" ]
then
read -p "Please specify the openELEC device (e.g. sdb1): " device
if [ "$berryboot" = false ]
then
filename=SYSTEM
fi
else
device=$2
fi
# Check for hash
if [ ! -z "$3" ]
then
hash=$3
else
hash="sha-512"
fi
# Check for user
if [ ! -z "$4" ]
then
user=$4
else
user="root"
fi
# Warning, better safe than sorry
message "This script comes with zero warranty! Always backup your files! I'm not responsible for any data loss or damage!" $red
# Summary
message "Will use the following password: $passwd" $yellow
message "Will use the following device: $device \n" $yellow
# Check for permission
read -p "Are these information correct? (y/N) " continue
case "$continue" in
No|no|N|n|"")
message "Aborting!" $red
exit 1
;;
*)
message "Let's rock!\n" $green
;;
esac
# Ask if we should remove old folders
read -p "Should a backup file be created in /openelec_backup ? (Y/n) " backup
case "$backup" in
Yes|y|Y|yes|"")
message "Will make a backup of the current image!" $yellow
backup=true
;;
*)
message "Backup of current image will be skipped" $green
remove=false
;;
esac
if [ ! -d /openelec_backup ]
then
mkdir /openelec_backup
fi
# Ask if we should remove old folders
read -p "Should the /openelec folders be removed after updating the password? (y/N) " remove
case "$remove" in
No|no|N|n|"")
message "Old folders won't be removed!" $yellow
remove=false
;;
*)
message "Old folders will be removed" $green
remove=true
;;
esac
# Remove folder
cd /
if [ -d /openelec ]
then
message "Found existing openelec folder" $yellow
if [ "$remove" = true ]
then
message "Removing..." $yellow
cleanUp
message "Done\n" $green
else
message "You specified that old folder should not be removed." $yellow
message "Please do this yourself now. Aborting..." $yellow
exit 1
fi
fi
# Setup
message "Making folders..." $yellow
mkdir /openelec
cd /openelec
mkdir flash original updated newsquashfs
message "Done\n" $green
# mount and copy suqashfs
message "Mounting openELEC squashfs system..." $yellow
mount /dev/$device /openelec/flash
if [ "$berryboot" = true ]
then
message "Listing the available files:"
ls /openelec/flash/images/
read -p "Please specify the openELEC file: " filename
# See if file exists
if [ ! -f /openelec/flash/images/$filename ]
then
message "Whoops, your $filename file is not there! Please check" $red
message "Leaving $device mounted!" $red
exit 1
fi
# Make a backup
if [ "$backup" = true ]
then
# Unique pseudo timestamp
date=$(date +"%Y%m%d-%T")
if [ "$berryboot" ]
then
cp /openelec/flash/images/$filename /openelec_backup/$date-$filename
else
cp /openelec/flash/$filename /openelec_backup/$date-$filename
fi
fi
# Loop already existing image
mount -o loop /openelec/flash/images/$filename /openelec/original
else
# See if SYSTEM exists
if [ ! -f /openelec/flash/$filename ]
then
message "Whoops, your SYSTEM file is not there! Please check" $red
message "Leaving $device mounted!" $red
exit 1
fi
# Mount loop to SYSTEM
mount -o loop /openelec/flash/$filename /openelec/original
fi
message "Done\n" $green
# Copy files to temp folder
message "Copying files, this might take a moment..." $yellow
cp -a /openelec/original/* /openelec/updated
message "Done\n" $green
# Create new password
message "Generating a new password ($hash, random salt)..." $yellow
salt=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
shadow_password=$(mkpasswd $passwd --salt=$salt --method=$hash)
message "Done\n" $green
# Backup
message "Making backup from /etc/shadow and replace password..." $yellow
cd /openelec/updated/etc
mv shadow shadow.bak
# Replace second string for given user (default is root)
awk -v pw=$shadow_password -F: 'BEGIN{OFS=":"}/'$user'/{gsub(/.*/,pw,$2)}1' shadow.bak > shadow
message "Done\n" $green
# sed way
#sed -i.bak "/^$user:/ s/:[^:]*/:$shadow_password"
# Make new squashfs and unmount
message "Making new squashfs, this might take a bit..." $yellow
mksquashfs /openelec/updated/ /openelec/newsquashfs/$filename
message "Done\n" $green
# Copy updated squashfs back
message "Copying updated file back..." $yellow
if [ "$berryboot" = true ]
then
cp /openelec/newsquashfs/$filename /openelec/flash/images/$filename
else
cp /openelec/newsquashfs/$filename /openelec/flash/$filename
fi
message "Done\n" $green
# Clean
message "Unmounting openelec flash and original again..." $yellow
unmount
if [ "$remove" = true ]
then
message "Removing openelec folder again..." $yellow
cleanUp
message "Done\n\n" $green
fi
message "Your password was successfully updated. Please (unmount) and remove your device" $green
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment