scripts/old_home.sh

58 lines
1.9 KiB
Bash
Raw Permalink Normal View History

2023-09-06 17:52:01 +00:00
#!/usr/bin/env bash
# https://discord.com/channels/689189992417067052/1114882414670516294/1142858186085367919
# Used to make old home and old mail available to users who owned them
# Function to recursively change owner and permissions
change_owner_and_permissions() {
local dir="$1"
local username="$2"
2023-09-06 18:27:57 +00:00
2023-09-06 17:52:01 +00:00
# Change owner of the directory and its contents
chown -R "$username" "$dir"
}
iter_dirs(){
2023-09-06 18:27:57 +00:00
local root="$1"
# Loop through each directory in /home
for dir in $root/*; do
if [ -d "$dir" ]; then
# Extract the directory name without the leading path
username=$(basename "$dir")
# handle caps
username_lower=$(echo "$username" | tr '[:upper:]' '[:lower:]')
if [ "$username" != "$username_lower" ]; then
mv "$root/$username" "$root/$username_lower"
echo "Changed $username to $username_lower"
2023-09-06 20:04:36 +00:00
dir="$root/$username_lower"
2023-09-06 18:27:57 +00:00
username=$username_lower
fi
# names must atart with either underscore or letter
if [[ $username =~ ^[0-9] ]]; then
mv "$root/$username" "$root/_$username"
echo "Changed $username to _$username"
2023-09-06 20:04:36 +00:00
dir="$root/_$username"
2023-09-06 18:27:57 +00:00
username="_$username"
fi
# Set permissions to read-only for the owner and not readable by others
# folders have to be executable to be able to read their contents
chmod -R u+rX "$dir"
2023-09-06 18:27:57 +00:00
# Check if the user exists before changing ownership and permissions
if id "$username" &>/dev/null; then
change_owner_and_permissions "$dir" "$username"
echo "Changed owner and permissions of $dir to $username"
2023-09-06 20:04:36 +00:00
# else
# echo "User $username not found. Leaving $dir owned by root."
2023-09-06 18:27:57 +00:00
fi
fi
done
2023-09-06 17:52:01 +00:00
}
iter_dirs /skynet_old/home
iter_dirs /skynet_old/mail