-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisable-local-user.sh
More file actions
119 lines (104 loc) · 3.06 KB
/
disable-local-user.sh
File metadata and controls
119 lines (104 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
#
# This script disables, deletes, and/or archives users on the local system.
#
ARCHIVE_DIR='/archive'
usage() {
# Display the usage and exit.
echo "Usage: ${0} [-dra] USER [USERN]..." >&2
echo 'Disable a local Linux account.' >&2
echo ' -d Deletes accounts instead of disabling them.' >&2
echo ' -r Removes the home directory associated with the account(s).' >&2
echo ' -a Creates an archive of the home directory associated with the account(s).' >&2
exit 1
}
# Make sure the script is being executed with superuser privileges.
if [[ "${UID}" -ne 0 ]]
then
echo 'Please run with sudo or as root.' >&2
exit 1
fi
# Parse the options.
while getopts dra OPTION
do
case ${OPTION} in
d) DELETE_USER='true' ;;
r) REMOVE_OPTION='-r' ;;
a) ARCHIVE='true' ;;
?) usage ;;
esac
done
# Remove the options while leaving the remaining arguments.
shift "$(( OPTIND - 1 ))"
# If the user doesn't supply at least one argument, give them help.
if [[ "${#}" -lt 1 ]]
then
usage
fi
# Loop through all the usernames supplied as arguments.
for USERNAME in "${@}"
do
echo "Processing user: ${USERNAME}"
# Make sure the UID of the account is at least 1000.
USERID=$(id -u ${USERNAME})
if [[ "${USERID}" -lt 1000 ]]
then
echo "Refusing to remove the ${USERNAME} account with UID ${USERID}." >&2
exit 1
fi
# Create an archive if requested to do so.
if [[ "${ARCHIVE}" = 'true' ]]
then
# Make sure the ARCHIVE_DIR directory exists.
if [[ ! -d "${ARCHIVE_DIR}" ]]
then
echo "Creating ${ARCHIVE_DIR} directory."
mkdir -p ${ARCHIVE_DIR}
if [[ "${?}" -ne 0 ]]
then
echo "The archive directory ${ARCHIVE_DIR} could not be created." >&2
exit 1
fi
fi
# Archive the user's home directory and move it into the ARCHIVE_DIR
HOME_DIR="/home/${USERNAME}"
ARCHIVE_FILE="${ARCHIVE_DIR}/${USERNAME}.tgz"
if [[ -d "${HOME_DIR}" ]]
then
echo "Archiving ${HOME_DIR} to ${ARCHIVE_FILE}"
tar -zcf ${ARCHIVE_FILE} ${HOME_DIR} &> /dev/null
if [[ "${?}" -ne 0 ]]
then
echo "Could not create ${ARCHIVE_FILE}." >&2
exit 1
fi
else
echo "${HOME_DIR} does not exist or is not a directory." >&2
exit 1
fi
fi # END of if "${ARCHIVE}" = 'true'
if [[ "${DELETE_USER}" = 'true' ]]
then
# Delete the user.
userdel ${REMOVE_OPTION} ${USERNAME}
# Check to see if the userdel command succeeded.
# We don't want to tell the user that an account was deleted when it hasn't been.
if [[ "${?}" -ne 0 ]]
then
echo "The account ${USERNAME} was NOT deleted." >&2
exit 1
fi
echo "The account ${USERNAME} was deleted."
else
chage -E 0 ${USERNAME}
# Check to see if the chage command succeeded.
# We don't want to tell the user that an account was disabled when it hasn't been.
if [[ "${?}" -ne 0 ]]
then
echo "The account ${USERNAME} was NOT disabled." >&2
exit 1
fi
echo "The account ${USERNAME} was disabled."
fi # END of if "${DELETE_USER}" = 'true'
done
exit 0