4 [ "${*}" ] && printf "%s\n\n" "${*}"
6 Usage: fixuser [-c|-i|-r] <user> [user ...]
8 -c Check symlinks for sanity
12 This script will adjust the bash-config symlinks in the homedir of
13 the supplied user, or all users if unspecified.
15 This script will avoid tampering with files that it thinks are
16 manually added. Use -c to check, and verify the files manually
23 files="bash_profile bashrc"
26 # Ignore users with shell not in /etc/shells
27 # Ignore users with invalid homedir
28 # Ignore subsequent users with the same homedir (root, toor, daemon, etc)
29 # List all remaining usernames
31 getent passwd | while IFS=: read user pass uid gid gecos home shell extra
33 getent shells "${shell}" >&- || continue
34 [ -d "${home}" ] || continue
35 echo "${_home_cache}" | grep -q " ${home} " && continue
36 _home_cache="${_home_cache} ${home} "
42 [ "${1}" ] || return 1
43 # This is a loop because read will not work after pipe due to subshell isolation
44 getent passwd "${1}" | while IFS=: read user pass uid gid gecos home shell extra
52 [ "${1}" ] || return 1
54 [ "${#}" -eq 0 ] && set - ${files}
58 link="$(userdir "${user}")/.${file}"
59 real="/usr/local/etc/bash-config/${file}"
60 if [ ! -L "${link}" ] || [ "$(realpath "$(readlink "${link}")")" != "$(realpath "${real}")" ]
70 [ "${1}" ] || return 1
72 [ "${#}" -eq 0 ] && set - ${files}
76 link="$(userdir "${user}")/.${file}"
77 [ -f "${link}" ] && return 1
84 [ "${1}" ] || return 1
86 [ "${#}" -eq 0 ] && set - ${files}
87 echo "Checking user ${user}..."
91 if gone "${user}" "${file}"
93 echo " File ${file} does not exist."
95 if mine "${user}" "${file}"
97 echo " File ${file} is mine."
99 echo " File ${file} exists, but is not mine."
107 [ "${1}" ] || return 1
111 echo "User symlinks appear to be correct"
117 echo "User files (${files}) appear to exist, and are not mine"
118 echo "Refusing to molest them"
124 link="$(userdir "${user}")/.${file}"
125 real="/usr/local/etc/bash-config/${file}"
126 [ "${link%%.${file}}" = "/" ] && return 1
127 su "${user}" -c "ln -s '${real}' '${link}'" || return $?
133 [ "${1}" ] || return 1
137 echo "User symlinks already removed"
143 echo "User files (${files}) appear to exist, and are not mine"
144 echo "Refusing to molest them"
150 link="$(userdir "${user}")/.${file}"
151 [ "${link%%.${file}}" = "/" ] && return 1
152 rm -f "${link}" || return $?
158 [ -z "${cmd}" ] || pebkac "Only specify one of -c -i -r"
164 while getopts "cir" opt
170 [?]) pebkac "Unrecognized option ${OPTARG}" ;;
173 shift $(( $OPTIND - 1 ))
175 [ "${cmd}" ] || pebkac
177 # Default to current user if not root
178 if [ "$(id -u)" -gt 0 ]
180 if [ "${*}" -a "${*}" != "${USER}" ]
182 echo "Mortal users can only modify themselves" >&2
185 [ "${#}" -eq 0 ] && set - ${USER}
187 [ "${#}" -eq 0 ] && set - $(userlist)
190 printf "Working on user(s) "