-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathadduser.sh
executable file
·112 lines (96 loc) · 3.2 KB
/
adduser.sh
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
#!/bin/bash
################################################################################
# Date: 2011/03/14
# Author: appleboy ( appleboy.tw AT gmail.com)
# Web: http://blog.wu-boy.com
#
# Program:
# Use the function to add system user and samba permission.
#
# History:
# 2011/03/14 first release
# 2011/03/15 add default_group default_home default_shell for config
# 2011/03/25 rewrite process command line
# 2013/04/30 add default group which is not exist and change password format '!${username}!'
# 2013/07/19 support set password command
#
################################################################################
VERSION="0.1"
ROOT_UID=0
#
# config
#
samba_enable=0
default_group="user"
default_home="/home/user"
default_shell="/bin/bash"
#
# main function
#
function displayErr() {
echo
echo $1;
echo
exit 1;
}
function usage() {
echo 'Usage: '$0' [-h|[--action|-a]] [add|del] Username [Password]'
exit 1;
}
execute () {
$* >/dev/null
if [ $? -ne 0 ]; then
displayErr "ERROR: executing $*"
fi
}
if [ "$UID" -ne "$ROOT_UID" ]; then
displayErr "Must be root to run this script."
fi
# Process command line...
while [ $# -gt 0 ]; do
case $1 in
--help | -h)
usage $0
;;
--action | -a) shift; action=$1; shift; username=$1; shift; password=$1; shift; ;;
*) usage $0; ;;
esac
done
test -z $action && usage $0
test -z $username && usage $0
# check home exist
test -d ${default_home} || mkdir -p ${default_home}
# check user group exist
group_exist=$(awk -F ":" '{printf $1 "\n"}' /etc/group | grep "^${default_group}$")
test -z ${group_exist} && groupadd ${default_group}
case $action in
add)
# check username exist
username_exist=$(awk -F ":" '{printf $1 "\n"}' /etc/passwd | grep "^${username}$")
if [ ! -z ${username_exist} ] ; then
displayErr "Username $username exist, please change it"
fi
# add user
cmd=$(useradd -c "$username" -g $default_group -d "${default_home}/$username" -m -s $default_shell $username)
# set user password for Ubuntu
# password=$(echo "!${username}!" | mkpasswd -s)
# set user password for CentOS (ref: http://stackoverflow.com/questions/2150882/how-to-automatically-add-user-account-and-password-with-a-bash-script)
# $(echo "!${username}!" | passwd "${username}" --stdin)
# get default password
test -z ${password} && password="!${username}!"
# set user password
$(echo "${username}:${password}" | chpasswd)
# add samba user
[ $samba_enable -ne "0" ] && ((echo $username; echo $username) | smbpasswd -L -s -a $username > /dev/null && smbpasswd -L -s -e $username > /dev/null && echo "add samba user ${username}")
# add execute permission.
[ -z $cmd ] && chmod +x "${default_home}/$username"
[ -z $cmd ] && echo "add username $username successfully"
;;
del)
[ $samba_enable -ne "0" ] && (smbpasswd -x $username > /dev/null && echo "delete samba user ${username}")
userdel -r $username && echo "delete user ${username} and remove home directory"
;;
*)
usage $0
;;
esac