-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrename-user
executable file
·209 lines (177 loc) · 5.11 KB
/
rename-user
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
# SPDX-FileCopyrightText: 2018 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command awk find ps sed && exit 3
#
# Functions
#
function migrate_symlink() {
local symlink
local sympath
local newsympath
symlink="${1}"
sympath="$(readlink "${symlink}")"
if [[ "${sympath}" =~ ^/home/${OLDNAME} ]]; then
newsympath=${sympath/${OLDNAME}/${NEWNAME}/}
rm "${symlink}"
ln -s "${newsympath}" "${symlink}"
fi
}
export -f migrate_symlink
function is_username_valid() {
local re='^[a-z_][a-z0-9_-]*$'
[ -z "${1}" ] && return 1
((${#1} > 16)) && return 1
[[ ${1} =~ ${re} ]]
}
function is_old_user_valid() {
local all_user
local all_sys
local uid_min
local uid_max
local user="${1}"
uid_min="$(sed -n "s/^UID_MIN\s\+\([0-9]\+\)$/\1/p" /etc/login.defs)"
uid_max="$(sed -n "s/^UID_MAX\s\+\([0-9]\+\)$/\1/p" /etc/login.defs)"
all_sys="$(getent passwd |
awk -F ":" -v mn="${uid_min}" -v mx="${uid_max}" \
'{if (($3 < mn) && ($3 > mx)) print $1}')"
all_user="$(getent passwd |
awk -F ":" -v mn="${uid_min}" -v mx="${uid_max}" \
'{if (($3 >= mn) && ($3 <= mx)) print $1}')"
if [[ "${all_sys}" =~ ${user} ]]; then
show_warning "${user@Q} is a system user."
return 1
fi
if [[ "${all_user}" =~ ${user} ]]; then
# Check that old user has no currently running processes.
if ps -u "${user}" > /dev/null 2>&1; then
show_warning "User ${user@Q} has running processes."
return 1
else
return 0
fi
fi
return 1
}
function is_new_user_valid() {
local all_user
local all_sys
local uid_min
local uid_max
local user="${1}"
uid_min="$(sed -n "s/^UID_MIN\s\+\([0-9]\+\)$/\1/p" /etc/login.defs)"
uid_max="$(sed -n "s/^UID_MAX\s\+\([0-9]\+\)$/\1/p" /etc/login.defs)"
all_sys="$(getent passwd |
awk -F ":" -v mn="${uid_min}" -v mx="${uid_max}" \
'{if (($3 < mn) && ($3 > mx)) print $1}')"
all_user="$(getent passwd |
awk -F ":" -v mn="${uid_min}" -v mx="${uid_max}" \
'{if (($3 >= mn) && ($3 <= mx)) print $1}')"
if [[ "${all_sys}" =~ ${user} ]]; then
show_warning "${user@Q} is a system user."
return 1
fi
if [[ "${all_user}" =~ ${user} ]]; then
show_warning "${user@Q} already exists."
return 1
fi
return 0
}
function print_usage() {
show_header "Usage: rename-user"
show_listitem " -n|--new new username"
show_listitem " -o|--old current username"
}
#
# Parse command line parameters
#
OLDNAME=
NEWNAME=
OPTIONS=ho:n:
LONGOPTIONS=help,old:,new:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-h | --help)
print_usage
exit
;;
-o | --old)
OLDNAME="${2}"
shift 2
;;
-n | --new)
NEWNAME="${2}"
shift 2
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid input. Exiting."
exit 3
;;
esac
done
# If the user didn't supply user names via that command line, ask for them in
# the prompt.
if [ -z "${OLDNAME}" ]; then
OLDNAME=$(ask_question "Change which user name?")
fi
if [ -z "${NEWNAME}" ]; then
NEWNAME=$(ask_question "What is the new user name?")
fi
# Make sure that OLDNAME and NEWNAME entries are valid.
if ! is_username_valid "${OLDNAME}"; then
show_error "ERROR: ${OLDNAME@Q} is not a valid name. Exiting."
exit 3
fi
if ! is_username_valid "${NEWNAME}"; then
show_error "ERROR: ${NEWNAME@Q} is not a valid name. Exiting."
exit 3
fi
if ! is_old_user_valid "${OLDNAME}"; then
show_error "ERROR: ${OLDNAME@Q} invalid. Exiting."
exit 3
fi
if ! is_new_user_valid "${NEWNAME}"; then
show_error "ERROR: ${NEWNAME@Q} invalid. Exiting."
exit 3
fi
export OLDNAME
export NEWNAME
#
# Rename user and migrate files
#
# Migrate the user.
show_info "Renaming user ${OLDNAME@Q} to ${NEWNAME@Q}..."
sudo usermod -l "${NEWNAME}" -d "/home/${NEWNAME}" -m "${OLDNAME}"
sudo groupmod -n "${NEWNAME}" "${OLDNAME}"
show_info "Fixing symlinks in /home/${NEWNAME} (provide ${NEWNAME} password)."
su "${NEWNAME}" - -c \
"cd /home/${NEWNAME} && find . -type l -exec bash -c 'migrate_symlink \"{}\"' \;"
# Prompt to change password.
REPLY=$(ask_question "Change password for ${NEWNAME}? (y/N)")
if [[ "${REPLY}" =~ ^([Yy][Ee][Ss]|[Yy])$ ]]; then
passwd "${NEWNAME}"
fi
show_success "Migrated!"