Blame
Date:
Fri Dec 17 13:35:57 2021 UTC
Message:
Imported sources
001
2021-12-17
jrmu
#!/bin/sh
002
2021-12-17
jrmu
003
2021-12-17
jrmu
htdocs_chroot_path="/home/www/htdocs"
004
2021-12-17
jrmu
log_path="/var/log/user_manager.log"
005
2021-12-17
jrmu
lock_list="jrmu,fizi,baytuch,vasya,gry,brk,mailman"
006
2021-12-17
jrmu
007
2021-12-17
jrmu
user_exists="NO"
008
2021-12-17
jrmu
group_exists="NO"
009
2021-12-17
jrmu
target_user_id=""
010
2021-12-17
jrmu
target_group_id=""
011
2021-12-17
jrmu
error_trig="NO"
012
2021-12-17
jrmu
013
2021-12-17
jrmu
014
2021-12-17
jrmu
check_exists() {
015
2021-12-17
jrmu
user_exists=NO
016
2021-12-17
jrmu
group_exists=NO
017
2021-12-17
jrmu
if [ ! -z "$(grep "^$1:.*$" /etc/passwd)" ]; then
018
2021-12-17
jrmu
user_exists="YES"
019
2021-12-17
jrmu
fi
020
2021-12-17
jrmu
if [ ! -z "$(grep "^$1:.*$" /etc/group)" ]; then
021
2021-12-17
jrmu
group_exists="YES"
022
2021-12-17
jrmu
fi
023
2021-12-17
jrmu
}
024
2021-12-17
jrmu
025
2021-12-17
jrmu
create_user() {
026
2021-12-17
jrmu
groupadd -ov -g $3 $1
027
2021-12-17
jrmu
useradd -v -m -d /home/$1 -g $3 -s /bin/ksh -u $2 $1
028
2021-12-17
jrmu
passwd $1
029
2021-12-17
jrmu
}
030
2021-12-17
jrmu
031
2021-12-17
jrmu
delete_user() {
032
2021-12-17
jrmu
userdel -rv $1
033
2021-12-17
jrmu
groupdel -v $1
034
2021-12-17
jrmu
}
035
2021-12-17
jrmu
036
2021-12-17
jrmu
logger() {
037
2021-12-17
jrmu
if [ ! -f $log_path ]; then
038
2021-12-17
jrmu
touch $log_path
039
2021-12-17
jrmu
fi
040
2021-12-17
jrmu
mess=[$(date '+%Y-%m-%d %H:%M:%S')]" "$1
041
2021-12-17
jrmu
echo $mess >> $log_path
042
2021-12-17
jrmu
echo $mess
043
2021-12-17
jrmu
}
044
2021-12-17
jrmu
045
2021-12-17
jrmu
get_target_ids() {
046
2021-12-17
jrmu
if [ $user_exists == "YES" ]; then
047
2021-12-17
jrmu
target_user_id=$(grep "^$1" /etc/passwd | sed -r -e 's|^.*:([0-9]+):[0-9]+:.*$|\1|g')
048
2021-12-17
jrmu
target_group_id=$(grep "^$1" /etc/passwd | sed -r -e 's|^.*:[0-9]+:([0-9]+):.*$|\1|g')
049
2021-12-17
jrmu
else
050
2021-12-17
jrmu
users_ids=$(cat /etc/passwd | sed -r -e 's|^.*:([0-9]+:[0-9]+):.*$|\1|g')
051
2021-12-17
jrmu
tmp_last_user_id=0
052
2021-12-17
jrmu
for line in $users_ids; do
053
2021-12-17
jrmu
tmp_user_id=${line%:*}
054
2021-12-17
jrmu
tmp_group_id=${line#*:}
055
2021-12-17
jrmu
if [ ${#tmp_user_id} -eq 4 ] && [ ${#tmp_group_id} -eq 4 ]; then
056
2021-12-17
jrmu
if [ $tmp_user_id -eq $tmp_group_id ]; then
057
2021-12-17
jrmu
if [ $tmp_user_id -gt $tmp_last_user_id ]; then
058
2021-12-17
jrmu
tmp_last_user_id=$tmp_user_id
059
2021-12-17
jrmu
fi
060
2021-12-17
jrmu
fi
061
2021-12-17
jrmu
fi
062
2021-12-17
jrmu
done
063
2021-12-17
jrmu
target_user_id=$tmp_last_user_id
064
2021-12-17
jrmu
target_group_id=$tmp_last_user_id
065
2021-12-17
jrmu
fi
066
2021-12-17
jrmu
target_user_id=$(echo $target_user_id | sed 's/[^0-9]//g')
067
2021-12-17
jrmu
target_group_id=$(echo $target_group_id | sed 's/[^0-9]//g')
068
2021-12-17
jrmu
if [ $user_exists == "NO" ] && [ ! -z $target_user_id ] && [ ! -z $target_group_id ]; then
069
2021-12-17
jrmu
target_user_id=$((target_user_id + 1))
070
2021-12-17
jrmu
target_group_id=$((target_group_id + 1))
071
2021-12-17
jrmu
fi
072
2021-12-17
jrmu
}
073
2021-12-17
jrmu
074
2021-12-17
jrmu
user_tree_config() {
075
2021-12-17
jrmu
if [ -d /home/$1 ]; then
076
2021-12-17
jrmu
chmod 700 /home/$1
077
2021-12-17
jrmu
chmod 700 /home/$1/.ssh
078
2021-12-17
jrmu
chmod 600 /home/$1/{.Xdefaults,.cshrc,.cvsrc,.login,.mailrc,.profile}
079
2021-12-17
jrmu
logger "TREE CONFIG: was configured home dir"
080
2021-12-17
jrmu
if [ -d $htdocs_chroot_path ]; then
081
2021-12-17
jrmu
mkdir $htdocs_chroot_path/$1
082
2021-12-17
jrmu
chown $1:$1 $htdocs_chroot_path/$1
083
2021-12-17
jrmu
ln -s $htdocs_chroot_path/$1 /home/$1/htdocs
084
2021-12-17
jrmu
chown $1:$1 /home/$1/htdocs
085
2021-12-17
jrmu
logger "TREE CONFIG: was configured htdocs dir"
086
2021-12-17
jrmu
else
087
2021-12-17
jrmu
logger "TREE CONFIG: htdocs chroot not found!"
088
2021-12-17
jrmu
fi
089
2021-12-17
jrmu
else
090
2021-12-17
jrmu
logger "TREE CONFIG: home folder not found!"
091
2021-12-17
jrmu
fi
092
2021-12-17
jrmu
}
093
2021-12-17
jrmu
094
2021-12-17
jrmu
user_tree_delete() {
095
2021-12-17
jrmu
if [ -d /home/$1 ]; then
096
2021-12-17
jrmu
if [ -d /home/$1/htdocs ]; then
097
2021-12-17
jrmu
rm -rf /home/$1/htdocs
098
2021-12-17
jrmu
fi
099
2021-12-17
jrmu
if [ -d $htdocs_chroot_path/$1 ]; then
100
2021-12-17
jrmu
rm -rf $htdocs_chroot_path/$1
101
2021-12-17
jrmu
fi
102
2021-12-17
jrmu
if [ ! -d /home/$1/htdocs ] && [ ! -d $htdocs_chroot_path/$1 ]; then
103
2021-12-17
jrmu
logger "TREE DELETE: was deteled htdocs dir"
104
2021-12-17
jrmu
else
105
2021-12-17
jrmu
error_trig="YES"
106
2021-12-17
jrmu
logger "TREE DELETE: failed to delete folder tree!"
107
2021-12-17
jrmu
fi
108
2021-12-17
jrmu
else
109
2021-12-17
jrmu
error_trig="YES"
110
2021-12-17
jrmu
logger "TREE DELETE: home folder not found!"
111
2021-12-17
jrmu
fi
112
2021-12-17
jrmu
}
113
2021-12-17
jrmu
114
2021-12-17
jrmu
do_create_user() {
115
2021-12-17
jrmu
logger "WORKER: create a new user..."
116
2021-12-17
jrmu
if [ -z $1 ]; then
117
2021-12-17
jrmu
logger "WORKER: you must provide username!"
118
2021-12-17
jrmu
else
119
2021-12-17
jrmu
check_exists $1
120
2021-12-17
jrmu
if [ $user_exists == "YES" ]; then
121
2021-12-17
jrmu
logger "WORKER: user $1 already exists"
122
2021-12-17
jrmu
else
123
2021-12-17
jrmu
get_target_ids $1
124
2021-12-17
jrmu
if [ ${#target_user_id} -eq 4 ] && [ ${#target_group_id} -eq 4 ]; then
125
2021-12-17
jrmu
logger "WORKER: process of creating a new user has started"
126
2021-12-17
jrmu
logger "WORKER: -> login - $1"
127
2021-12-17
jrmu
logger "WORKER: -> user_id - $target_user_id"
128
2021-12-17
jrmu
logger "WORKER: -> group_id - $target_group_id"
129
2021-12-17
jrmu
create_user $1 $target_user_id $target_group_id
130
2021-12-17
jrmu
check_exists $1
131
2021-12-17
jrmu
if [ $user_exists == "YES" ]; then
132
2021-12-17
jrmu
logger "WORKER: user created successfully"
133
2021-12-17
jrmu
logger "WORKER: user folder tree configuration..."
134
2021-12-17
jrmu
user_tree_config $1
135
2021-12-17
jrmu
else
136
2021-12-17
jrmu
logger "WORKER: user creation failure!"
137
2021-12-17
jrmu
fi
138
2021-12-17
jrmu
else
139
2021-12-17
jrmu
logger "WORKER: error generating identifiers!"
140
2021-12-17
jrmu
fi
141
2021-12-17
jrmu
fi
142
2021-12-17
jrmu
fi
143
2021-12-17
jrmu
}
144
2021-12-17
jrmu
145
2021-12-17
jrmu
do_delete_user() {
146
2021-12-17
jrmu
logger "WORKER: removing a user account..."
147
2021-12-17
jrmu
if [ -z $1 ]; then
148
2021-12-17
jrmu
logger "WORKER: you must provide username!"
149
2021-12-17
jrmu
else
150
2021-12-17
jrmu
check_exists $1
151
2021-12-17
jrmu
if [ $user_exists == "NO" ]; then
152
2021-12-17
jrmu
logger "WORKER: user $1 not found!"
153
2021-12-17
jrmu
else
154
2021-12-17
jrmu
get_target_ids $1
155
2021-12-17
jrmu
if [ ${#target_user_id} -ne 4 ]; then
156
2021-12-17
jrmu
logger "WORKER: you cannot delete the service user!"
157
2021-12-17
jrmu
else
158
2021-12-17
jrmu
deny_del="NO"
159
2021-12-17
jrmu
for tmp_user_name in $(echo $lock_list | tr "," "\n"); do
160
2021-12-17
jrmu
if [ $1 == $tmp_user_name ]; then
161
2021-12-17
jrmu
deny_del="YES"
162
2021-12-17
jrmu
break
163
2021-12-17
jrmu
fi
164
2021-12-17
jrmu
done
165
2021-12-17
jrmu
if [ $deny_del == "NO" ]; then
166
2021-12-17
jrmu
logger "WORKER: deleting a user's folder tree..."
167
2021-12-17
jrmu
user_tree_delete $1
168
2021-12-17
jrmu
if [ $error_trig == "NO" ]; then
169
2021-12-17
jrmu
logger "WORKER: deleting a user account..."
170
2021-12-17
jrmu
delete_user $1
171
2021-12-17
jrmu
check_exists $1
172
2021-12-17
jrmu
if [ $user_exists == "NO" ]; then
173
2021-12-17
jrmu
logger "WORKER: user account of $1 deleted"
174
2021-12-17
jrmu
else
175
2021-12-17
jrmu
logger "WORKER: failed to delete account!"
176
2021-12-17
jrmu
fi
177
2021-12-17
jrmu
fi
178
2021-12-17
jrmu
else
179
2021-12-17
jrmu
logger "WORKER: the deletion of this user is blocked!"
180
2021-12-17
jrmu
fi
181
2021-12-17
jrmu
fi
182
2021-12-17
jrmu
fi
183
2021-12-17
jrmu
fi
184
2021-12-17
jrmu
}
185
2021-12-17
jrmu
186
2021-12-17
jrmu
if [ $(whoami) == "root" ]; then
187
2021-12-17
jrmu
case "$1" in
188
2021-12-17
jrmu
create)
189
2021-12-17
jrmu
do_create_user $2
190
2021-12-17
jrmu
;;
191
2021-12-17
jrmu
delete)
192
2021-12-17
jrmu
do_delete_user $2
193
2021-12-17
jrmu
;;
194
2021-12-17
jrmu
*)
195
2021-12-17
jrmu
logger "SELECTOR: unknown command"
196
2021-12-17
jrmu
;;
197
2021-12-17
jrmu
esac
198
2021-12-17
jrmu
exit 0
199
2021-12-17
jrmu
else
200
2021-12-17
jrmu
echo "must be run as root!"
201
2021-12-17
jrmu
exit 1
202
2021-12-17
jrmu
fi
IRCNow