3 84c190b6 2021-12-17 jrmu htdocs_chroot_path="/home/www/htdocs"
4 84c190b6 2021-12-17 jrmu log_path="/var/log/user_manager.log"
5 84c190b6 2021-12-17 jrmu lock_list="jrmu,fizi,baytuch,vasya,gry,brk,mailman"
7 84c190b6 2021-12-17 jrmu user_exists="NO"
8 84c190b6 2021-12-17 jrmu group_exists="NO"
9 84c190b6 2021-12-17 jrmu target_user_id=""
10 84c190b6 2021-12-17 jrmu target_group_id=""
11 84c190b6 2021-12-17 jrmu error_trig="NO"
14 84c190b6 2021-12-17 jrmu check_exists() {
15 84c190b6 2021-12-17 jrmu user_exists=NO
16 84c190b6 2021-12-17 jrmu group_exists=NO
17 84c190b6 2021-12-17 jrmu if [ ! -z "$(grep "^$1:.*$" /etc/passwd)" ]; then
18 84c190b6 2021-12-17 jrmu user_exists="YES"
20 84c190b6 2021-12-17 jrmu if [ ! -z "$(grep "^$1:.*$" /etc/group)" ]; then
21 84c190b6 2021-12-17 jrmu group_exists="YES"
25 84c190b6 2021-12-17 jrmu create_user() {
26 84c190b6 2021-12-17 jrmu groupadd -ov -g $3 $1
27 84c190b6 2021-12-17 jrmu useradd -v -m -d /home/$1 -g $3 -s /bin/ksh -u $2 $1
31 84c190b6 2021-12-17 jrmu delete_user() {
32 84c190b6 2021-12-17 jrmu userdel -rv $1
33 84c190b6 2021-12-17 jrmu groupdel -v $1
37 84c190b6 2021-12-17 jrmu if [ ! -f $log_path ]; then
38 84c190b6 2021-12-17 jrmu touch $log_path
40 84c190b6 2021-12-17 jrmu mess=[$(date '+%Y-%m-%d %H:%M:%S')]" "$1
41 84c190b6 2021-12-17 jrmu echo $mess >> $log_path
45 84c190b6 2021-12-17 jrmu get_target_ids() {
46 84c190b6 2021-12-17 jrmu if [ $user_exists == "YES" ]; then
47 84c190b6 2021-12-17 jrmu target_user_id=$(grep "^$1" /etc/passwd | sed -r -e 's|^.*:([0-9]+):[0-9]+:.*$|\1|g')
48 84c190b6 2021-12-17 jrmu target_group_id=$(grep "^$1" /etc/passwd | sed -r -e 's|^.*:[0-9]+:([0-9]+):.*$|\1|g')
50 84c190b6 2021-12-17 jrmu users_ids=$(cat /etc/passwd | sed -r -e 's|^.*:([0-9]+:[0-9]+):.*$|\1|g')
51 84c190b6 2021-12-17 jrmu tmp_last_user_id=0
52 84c190b6 2021-12-17 jrmu for line in $users_ids; do
53 84c190b6 2021-12-17 jrmu tmp_user_id=${line%:*}
54 84c190b6 2021-12-17 jrmu tmp_group_id=${line#*:}
55 84c190b6 2021-12-17 jrmu if [ ${#tmp_user_id} -eq 4 ] && [ ${#tmp_group_id} -eq 4 ]; then
56 84c190b6 2021-12-17 jrmu if [ $tmp_user_id -eq $tmp_group_id ]; then
57 84c190b6 2021-12-17 jrmu if [ $tmp_user_id -gt $tmp_last_user_id ]; then
58 84c190b6 2021-12-17 jrmu tmp_last_user_id=$tmp_user_id
63 84c190b6 2021-12-17 jrmu target_user_id=$tmp_last_user_id
64 84c190b6 2021-12-17 jrmu target_group_id=$tmp_last_user_id
66 84c190b6 2021-12-17 jrmu target_user_id=$(echo $target_user_id | sed 's/[^0-9]//g')
67 84c190b6 2021-12-17 jrmu target_group_id=$(echo $target_group_id | sed 's/[^0-9]//g')
68 84c190b6 2021-12-17 jrmu if [ $user_exists == "NO" ] && [ ! -z $target_user_id ] && [ ! -z $target_group_id ]; then
69 84c190b6 2021-12-17 jrmu target_user_id=$((target_user_id + 1))
70 84c190b6 2021-12-17 jrmu target_group_id=$((target_group_id + 1))
74 84c190b6 2021-12-17 jrmu user_tree_config() {
75 84c190b6 2021-12-17 jrmu if [ -d /home/$1 ]; then
76 84c190b6 2021-12-17 jrmu chmod 700 /home/$1
77 84c190b6 2021-12-17 jrmu chmod 700 /home/$1/.ssh
78 84c190b6 2021-12-17 jrmu chmod 600 /home/$1/{.Xdefaults,.cshrc,.cvsrc,.login,.mailrc,.profile}
79 84c190b6 2021-12-17 jrmu logger "TREE CONFIG: was configured home dir"
80 84c190b6 2021-12-17 jrmu if [ -d $htdocs_chroot_path ]; then
81 84c190b6 2021-12-17 jrmu mkdir $htdocs_chroot_path/$1
82 84c190b6 2021-12-17 jrmu chown $1:$1 $htdocs_chroot_path/$1
83 84c190b6 2021-12-17 jrmu ln -s $htdocs_chroot_path/$1 /home/$1/htdocs
84 84c190b6 2021-12-17 jrmu chown $1:$1 /home/$1/htdocs
85 84c190b6 2021-12-17 jrmu logger "TREE CONFIG: was configured htdocs dir"
87 84c190b6 2021-12-17 jrmu logger "TREE CONFIG: htdocs chroot not found!"
90 84c190b6 2021-12-17 jrmu logger "TREE CONFIG: home folder not found!"
94 84c190b6 2021-12-17 jrmu user_tree_delete() {
95 84c190b6 2021-12-17 jrmu if [ -d /home/$1 ]; then
96 84c190b6 2021-12-17 jrmu if [ -d /home/$1/htdocs ]; then
97 84c190b6 2021-12-17 jrmu rm -rf /home/$1/htdocs
99 84c190b6 2021-12-17 jrmu if [ -d $htdocs_chroot_path/$1 ]; then
100 84c190b6 2021-12-17 jrmu rm -rf $htdocs_chroot_path/$1
102 84c190b6 2021-12-17 jrmu if [ ! -d /home/$1/htdocs ] && [ ! -d $htdocs_chroot_path/$1 ]; then
103 84c190b6 2021-12-17 jrmu logger "TREE DELETE: was deteled htdocs dir"
105 84c190b6 2021-12-17 jrmu error_trig="YES"
106 84c190b6 2021-12-17 jrmu logger "TREE DELETE: failed to delete folder tree!"
109 84c190b6 2021-12-17 jrmu error_trig="YES"
110 84c190b6 2021-12-17 jrmu logger "TREE DELETE: home folder not found!"
114 84c190b6 2021-12-17 jrmu do_create_user() {
115 84c190b6 2021-12-17 jrmu logger "WORKER: create a new user..."
116 84c190b6 2021-12-17 jrmu if [ -z $1 ]; then
117 84c190b6 2021-12-17 jrmu logger "WORKER: you must provide username!"
119 84c190b6 2021-12-17 jrmu check_exists $1
120 84c190b6 2021-12-17 jrmu if [ $user_exists == "YES" ]; then
121 84c190b6 2021-12-17 jrmu logger "WORKER: user $1 already exists"
123 84c190b6 2021-12-17 jrmu get_target_ids $1
124 84c190b6 2021-12-17 jrmu if [ ${#target_user_id} -eq 4 ] && [ ${#target_group_id} -eq 4 ]; then
125 84c190b6 2021-12-17 jrmu logger "WORKER: process of creating a new user has started"
126 84c190b6 2021-12-17 jrmu logger "WORKER: -> login - $1"
127 84c190b6 2021-12-17 jrmu logger "WORKER: -> user_id - $target_user_id"
128 84c190b6 2021-12-17 jrmu logger "WORKER: -> group_id - $target_group_id"
129 84c190b6 2021-12-17 jrmu create_user $1 $target_user_id $target_group_id
130 84c190b6 2021-12-17 jrmu check_exists $1
131 84c190b6 2021-12-17 jrmu if [ $user_exists == "YES" ]; then
132 84c190b6 2021-12-17 jrmu logger "WORKER: user created successfully"
133 84c190b6 2021-12-17 jrmu logger "WORKER: user folder tree configuration..."
134 84c190b6 2021-12-17 jrmu user_tree_config $1
136 84c190b6 2021-12-17 jrmu logger "WORKER: user creation failure!"
139 84c190b6 2021-12-17 jrmu logger "WORKER: error generating identifiers!"
145 84c190b6 2021-12-17 jrmu do_delete_user() {
146 84c190b6 2021-12-17 jrmu logger "WORKER: removing a user account..."
147 84c190b6 2021-12-17 jrmu if [ -z $1 ]; then
148 84c190b6 2021-12-17 jrmu logger "WORKER: you must provide username!"
150 84c190b6 2021-12-17 jrmu check_exists $1
151 84c190b6 2021-12-17 jrmu if [ $user_exists == "NO" ]; then
152 84c190b6 2021-12-17 jrmu logger "WORKER: user $1 not found!"
154 84c190b6 2021-12-17 jrmu get_target_ids $1
155 84c190b6 2021-12-17 jrmu if [ ${#target_user_id} -ne 4 ]; then
156 84c190b6 2021-12-17 jrmu logger "WORKER: you cannot delete the service user!"
158 84c190b6 2021-12-17 jrmu deny_del="NO"
159 84c190b6 2021-12-17 jrmu for tmp_user_name in $(echo $lock_list | tr "," "\n"); do
160 84c190b6 2021-12-17 jrmu if [ $1 == $tmp_user_name ]; then
161 84c190b6 2021-12-17 jrmu deny_del="YES"
165 84c190b6 2021-12-17 jrmu if [ $deny_del == "NO" ]; then
166 84c190b6 2021-12-17 jrmu logger "WORKER: deleting a user's folder tree..."
167 84c190b6 2021-12-17 jrmu user_tree_delete $1
168 84c190b6 2021-12-17 jrmu if [ $error_trig == "NO" ]; then
169 84c190b6 2021-12-17 jrmu logger "WORKER: deleting a user account..."
170 84c190b6 2021-12-17 jrmu delete_user $1
171 84c190b6 2021-12-17 jrmu check_exists $1
172 84c190b6 2021-12-17 jrmu if [ $user_exists == "NO" ]; then
173 84c190b6 2021-12-17 jrmu logger "WORKER: user account of $1 deleted"
175 84c190b6 2021-12-17 jrmu logger "WORKER: failed to delete account!"
179 84c190b6 2021-12-17 jrmu logger "WORKER: the deletion of this user is blocked!"
186 84c190b6 2021-12-17 jrmu if [ $(whoami) == "root" ]; then
187 84c190b6 2021-12-17 jrmu case "$1" in
189 84c190b6 2021-12-17 jrmu do_create_user $2
192 84c190b6 2021-12-17 jrmu do_delete_user $2
195 84c190b6 2021-12-17 jrmu logger "SELECTOR: unknown command"
200 84c190b6 2021-12-17 jrmu echo "must be run as root!"