forked from MestreLion/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-user
executable file
·258 lines (218 loc) · 6.9 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
#
# rename-user - rename a user (login name) along with its group and home dir
#
# Copyright (C) 2015 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
#
# 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. See <http://www.gnu.org/licenses/gpl.html>
#
# TODO:
# - Fix files containing OLDHOME
# - Fix symlinks pointing to OLDHOME (or inside)
myname="${0##*/}"
mydir=$(dirname "$(readlink -f "$0")")
prefix=/home
verbose=0
yes=0
force=0
movegroup=1
movehome=1
homeargs=()
newgroup=""
newhome=""
fatal() { [[ "$1" ]] && echo "$myname: error: $1" >&2 ; exit ${2:-1} ; }
message() { ((verbose)) && printf "%s\n" "$1"; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid argument: $1" ; }
missing() { argerr "missing ${2:+$2 }argument${1:+ from $1}." ; }
confirm()
{
local message="$1"
local yes=${2:-0}
local default=NO
local reply
if ! ((yes)); then
read -p "$message (y/n, default $default): " reply
reply="${reply:-$default}"
case "$reply" in
[Yy]*) ;;
* ) message "cancelled by user"; return 2;;
esac
fi
return 0
}
usage() {
cat <<-USAGE
Usage: $myname [options] NEWNAME [OLDNAME]
USAGE
if [[ "$1" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Rename a user, including its home dir and default group.
A wrapper to automate usermod/groupmod with extra validations.
Options:
-h|--help - show this page.
-v|--verbose - print more details about what is being done.
-y|--yes - do not prompt for confirmation.
-f|--force - allow rename to existing home directory or group.
-D|--no-homedir - do not rename home directory.
-G|--no-group - do not rename primary group.
-d|--homedir PATH
- move current home directory to a custom PATH.
By default if its last path component matches OLDNAME
home is moved to 'basename <current home>/NEWNAME'.
Ignored if --no-homedir is set.
-g|--group NAME
- rename primary group to custom NAME. By default if
group name matches OLDNAME it is renamed to NEWNAME.
Ignored if --no-group is set.
Notes:
- Renaming 'root' or a currently logged in user is not allowed.
- Renaming to an existing user is not allowed.
- Moving home directory dir or renaming primary group to existing ones
is only allowed if --force is set.
- Does not work (yet) for encripted home directories.
Copyright (C) 2015 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
# Option handling
args=()
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
case "$1" in
-v|--verbose ) verbose=1 ;;
-y|--yes ) yes=1 ;;
-f|--force ) force=1 ;;
-D|--no-homedir ) movehome=0 ;;
-G|--no-group ) movegroup=0 ;;
-d|--homedir ) shift ; newhome="$1" ;;
-g|--group ) shift ; newgroup="$1";;
--home=* ) newhome="${1#*=}" ;;
--group=* ) newgroup="${1#*=}" ;;
-* ) invalid "$1" ;;
* ) args+=( "$1" ) ;;
esac
shift
done
if ((${#args[@]} >= 3)); then invalid "${args[2]}"; fi
# Name check
newname=${args[0]}
oldname=${args[1]:-"$USER"}
if [[ -z "$oldname" ]]; then missing "" "OLDNAME"; fi
if [[ -z "$newname" ]]; then missing "" "NEWNAME"; fi
if [[ "$oldname" == "root" ]]; then
fatal "renaming 'root' is not allowed."
fi
if cut -d: -f1 /etc/passwd | grep -qxm1 "$newname"; then
fatal "new user '$newname' already exists."
fi
if ! cut -d: -f1 /etc/passwd | grep -qxm1 "$oldname"; then
fatal "user '$oldname' does not exist."
fi
if users | grep -qwm1 "$oldname"; then
fatal "user '$oldname' is currently logged in."
fi
# Home check
oldhome=$(awk -F: -v name="$oldname" '$1 == name {print $6; exit}' /etc/passwd)
if ((movehome)); then
if [[ -z "$newhome" ]]; then
homebase=$(dirname "$oldhome")
homedir=$(basename "$oldhome")
if [[ "$homedir" == "$oldname" ]]; then
newhome="$homebase"/"$newname"
else
newhome=$oldhome
fi
fi
if [[ "$newhome" != "$oldhome" ]]; then
homeargs=(-d "$newhome" -m)
else
homeargs=()
movehome=0
fi
if !((force)) && ((movehome)) &&
[[ -e "$newhome" ]]; then
fatal "new homedir '$newhome' already exists. Use --homedir to set a different one or --force to use it anyway."
fi
else
newhome=$oldhome
homeargs=()
fi
# Group check
oldgroup=$(groups "$oldname" | cut -d' ' -f3)
if ((movegroup)); then
if [[ -z "$newgroup" ]]; then
if [[ "$oldgroup" == "$oldname" ]]; then
newgroup=$newname
else
movegroup=0
fi
fi
if !((force)) && ((movegroup)) &&
cut -d: -f1 /etc/group | grep -qxm1 "$newgroup"; then
fatal "new group '$newgroup' already exists. Use --group to set a different one or --force to use it anyway."
fi
else
newgroup=$oldgroup
fi
message "Old user:"
message "Login: $oldname"
message "Group: $oldgroup"
message "Home: $oldhome"
message ""
message "New user:"
message "Login: $newname"
message "Group: $newgroup"
message "Home: $newhome"
message ""
if !((yes)) && ! confirm "Rename user '$oldname' to '$newname'?"; then
exit
fi
sudo usermod -l "$newname" "${homeargs[@]}" "$oldname"
if ((movegroup)); then
sudo groupmod -n "$newgroup" "$oldgroup"
fi
if ((movehome)); then
message "Listing all files containing old home '$oldhome'. This may take a while..."
find "$newhome" -lname "$oldhome" 2>/dev/null
find "$newhome" -lname "$oldhome/*" 2>/dev/null
grep --color -sr "$oldhome" /
fi
exit
#At the start screen press Ctrl+ALT+F1.
#Log in using your username and password.
#Set a password for the "root" account.
#sudo passwd root
#Log out.
#exit
#Log in using the "root" account and the password you have previously set.
#Change the username and the home folder to the new name that you want.
#usermod -l <newname> -d /home/<newname> -m <oldname>
#Change the group name to the new name that you want.
#groupmod -n <newgroup> <oldgroup>
#Lock the "root" account.
#passwd -l root
#better yet: passwd -d root
#If you were using ecryptfs (encrypted home directory).
# Mount your encrypted directory using ecryptfs-recover-private and
# edit <mountpoint>/.ecryptfs/Private.mnt to reflect your new home directory.
#Log out.
#exit
#press Ctrl+ALT+F7.