-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add undocumented rename-account.sh and modify osh-orphaned-home…
…dir.sh accordingly
- Loading branch information
Showing
5 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#! /usr/bin/env bash | ||
# vim: set filetype=sh ts=4 sw=4 sts=4 et: | ||
basedir=$(readlink -f "$(dirname "$0")"/../..) | ||
|
||
from=$1 | ||
to=$2 | ||
|
||
if [ -n "$3" ] || [ -z "$2" ] ; then | ||
echo "Usage: $0 original_account_name new_account_name" | ||
exit 2 | ||
fi | ||
|
||
fail() | ||
{ | ||
echo "Error, will not proceed: $*" | ||
exit 1 | ||
} | ||
|
||
really_run_commands=0 | ||
|
||
_run() | ||
{ | ||
local ret | ||
if [ "$really_run_commands" = "1" ] ; then | ||
echo "Executing: $*" | ||
"$@"; ret=$? | ||
echo "... return code: $ret" | ||
else | ||
echo "DRY RUN: would execute: $*" | ||
fi | ||
} | ||
|
||
batchrun() | ||
{ | ||
# first, rename the main account group | ||
getent group "$from" >/dev/null || fail "group $from doesn't exist" | ||
getent group "$to" >/dev/null && fail "group $to already exists" | ||
_run groupmod -n "$to" "$from" | ||
|
||
# then, rename the account itself | ||
getent passwd "$from" >/dev/null || fail "user $from doesn't exist" | ||
getent passwd "$to" >/dev/null && fail "user $to already exists" | ||
test -d "/home/$from" || fail "directory /home/$from doesn't exist" | ||
test -d "/home/$to" && fail "directory /home/$to already exists" | ||
_run usermod -m -d /home/"$to" -l "$to" "$from" | ||
|
||
# then, rename all other groups linked to the account (appart from the main one already done) | ||
# shellcheck disable=SC2043 | ||
for suffix in tty; do | ||
if getent group "$from-$suffix" >/dev/null ; then | ||
getent group "$to-$suffix" >/dev/null && fail "group $to-$suffix already exists" | ||
_run groupmod -n "$to-$suffix" "$from-$suffix" | ||
fi | ||
done | ||
|
||
# now handle the allowkeeper folder of the account | ||
test -d "/home/allowkeeper/$from" || fail "directory /home/allowkeeper/$from doesn't exist" | ||
test -d "/home/allowkeeper/$to" && fail "directory /home/allowkeeper/$to already exists" | ||
_run mv -v "/home/allowkeeper/$from" "/home/allowkeeper/$to" | ||
|
||
# if the account has passwords, handle them: filenames contain the account name | ||
passdir="/home/$from/pass" | ||
[ "$really_run_commands" = "1" ] && passdir="/home/$to/pass" | ||
if [ -d "$passdir" ]; then | ||
pushd "$passdir" >/dev/null || exit 1 | ||
# shellcheck disable=SC2044 | ||
for passfile in $(find . -type f -name "$from" -print -o -type f -name "$from.*" -print) | ||
do | ||
_run mv -v "$passfile" "${passfile/$from/$to}" | ||
done | ||
popd >/dev/null || exit 1 | ||
fi | ||
|
||
# finally, regenerate the account's sudoers | ||
_run "$basedir"/bin/sudogen/generate-sudoers.sh create account "$to" | ||
_run "$basedir"/bin/sudogen/generate-sudoers.sh delete account "$from" | ||
} | ||
|
||
|
||
really_run_commands=0 | ||
batchrun | ||
echo | ||
echo "OK to proceed? (CTRL+C to abort)" | ||
# shellcheck disable=SC2034 | ||
read -r ___ | ||
really_run_commands=1 | ||
batchrun | ||
echo "Done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters