Skip to content

Instantly share code, notes, and snippets.

@xoascf
Last active September 23, 2021 05:17
Show Gist options
  • Save xoascf/379de5379cf1db205a49ad5fd77bc68a to your computer and use it in GitHub Desktop.
Save xoascf/379de5379cf1db205a49ad5fd77bc68a to your computer and use it in GitHub Desktop.
Fix Root Privileges in File System
#!/bin/sh
# Fixes root and write permissions for system files - Amaro M.
ROOT='Need to run as root.'
NOTR='No non-system users found, cannot continue.'
WHAT='This will change “owner:group” to “root:root” and remove write permission for “others”.'
WORK='Searching for and correcting file permissions and file write problems...'
FXED='Fixed: %N was “%U:%G”.'
NOFP='No problems have been encountered, exiting...'
LOGA='Log file is located at “%s”.\n'
case "${LANGUAGE:-$LANG}" in es*)
ROOT="Necesita ejecutarse como root."
NOTR="No se encontraron usuarios no pertenecientes al sistema, no se puede continuar."
WHAT="Esto cambiará el «propietario:grupo» a «root:root» y quitará el permiso de escritura de «otros»."
WORK="Buscando y corrigiendo permisos de archivos y problemas de escritura de archivos..."
FXED="Corregido: %N era «%U:%G»."
NOFP="No se ha encontrado ningún problema, saliendo..."
LOGA="El archivo de registro se encuentra en «%s».\n" ;;
esac
E() { echo >&2 "$1"; exit 1; }
[ "$(id -u)" -ne 0 ] && { E "$ROOT"; }
LIST=$(awk -F: '($3>=1000)&&($1!="nobody"){print $1}' /etc/passwd | sed -e ':a;N;$!ba;s/\n/ -o -user /g')
[ -z "$LIST" ] && { E "$NOTR"; }
DLOG="/var/tmp/deepines-security-patch"
FLOG="$DLOG/$(date +%Y-%m-%d_%H.%M.%S)"
mkdir -p "$DLOG"
touch "$FLOG"
echo "$WHAT"
echo "$WORK"
find /boot /etc /usr \( -user $LIST \) -exec stat -c "$FXED" {} \; -exec chown root:root {} \; -exec chmod o-w {} \; >"$FLOG"
! [ -s "$FLOG" ] && { echo "$NOFP"; rm "$FLOG"; exit 0; }
printf "$LOGA" "$FLOG"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment