#!/usr/bin/env bash{output=""perm_mask='0027'maxperm="$(printf'%o'$((0777&~$perm_mask)))"valid_shells="^($(sed-rn'/^\//{s,/,\\\\/,g;p}'/etc/shells|paste-s-d'|'-))$"awk-vpat="$valid_shells"-F:'$(NF) ~ pat { print $1 " " $(NF-1) }'/etc/passwd|(whileread-ruserhome;doif[-d"$home"];thenmode=$(stat-L-c'%#a'"$home")[$(($mode&$perm_mask))-gt0]&&output="$output\n- User $user home directory: \"$home\" is too permissive: \"$mode\" (should be: \"$maxperm\" or more restrictive)"fidoneif[-n"$output"];thenecho-e"\n- Failed:$output"elseecho-e"\n- Passed:\n- All user home directories are mode: \"$maxperm\" or more restrictive"fi)}
Making global modifications to user home directories without alerting the user community can result in unexpected outages and unhappy users. Therefore, it is
recommended that a monitoring policy be established to report user file permissions and determine the action to be taken in accordance with site policy.
The following script can be used to remove permissions is excess of 750 from interactive user home directories:
#!/usr/bin/env bash{perm_mask='0027'maxperm="$(printf'%o'$((0777&~$perm_mask)))"valid_shells="^($(sed-rn'/^\//{s,/,\\\\/,g;p}'/etc/shells|paste-s-d'|'-))$"awk-vpat="$valid_shells"-F:'$(NF) ~ pat { print $1 " " $(NF-1) }'/etc/passwd|(whileread-ruserhome;domode=$(stat-L-c'%#a'"$home")if[$(($mode&$perm_mask))-gt0];thenecho-e"- modifying User $user home directory: \"$home\"\nremoving excessive permissions from current mode of \"$mode\""chmodg-w,o-rwx"$home"fidone)}