| 1 | #!/bin/bash |
| 2 | # Author: Blake, Kuo-Lien Huang |
| 3 | # License: GPL |
| 4 | # Description: |
| 5 | # * creat and delete accounts for DRBL, actually it for NIS (YP). |
| 6 | # |
| 7 | # Modified by Steven Shiau <[email protected]> to used in DRBL for Redhat |
| 8 | |
| 9 | # Load DRBL setting and functions |
| 10 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 11 | |
| 12 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 13 | |
| 14 | |
| 15 | # |
| 16 | check_if_root |
| 17 | |
| 18 | # Parse command-line options |
| 19 | while [ $# -gt 0 ]; do |
| 20 | case "$1" in |
| 21 | -s|--single) |
| 22 | format="single" |
| 23 | shift; username=$1 |
| 24 | shift; groupname=$1 |
| 25 | shift;; |
| 26 | -r|--range) |
| 27 | format="range" |
| 28 | shift; prefix=$1 |
| 29 | shift; start=$1 |
| 30 | shift; end=$1 |
| 31 | shift; groupname=$1 |
| 32 | shift ;; |
| 33 | -f|--file) |
| 34 | format="file" |
| 35 | shift; filename=$1 |
| 36 | shift ;; |
| 37 | -l|--list) |
| 38 | format="list" |
| 39 | shift; filename=$1 |
| 40 | shift ;; |
| 41 | -g|--group) |
| 42 | format="group" |
| 43 | shift; group2del=$1 |
| 44 | shift ;; |
| 45 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 46 | echo $USAGE >& 2 |
| 47 | exit 2 ;; |
| 48 | *) break ;; |
| 49 | esac |
| 50 | done |
| 51 | |
| 52 | case "$format" in |
| 53 | single) |
| 54 | [ -z "$username" ] && echo "No username! Program terminated!" && exit 1 |
| 55 | ;; |
| 56 | range) |
| 57 | [ -z "$prefix" ] && echo "No prefix! Program terminated!" && exit 1 |
| 58 | [ -z "$start" ] && echo "No start! Program terminated!" && exit 1 |
| 59 | [ -z "$end" ] && echo "No end! Program terminated!" && exit 1 |
| 60 | ;; |
| 61 | file) |
| 62 | [ -z "$filename" ] && echo "No filename! Program terminated!" && exit 1 |
| 63 | ;; |
| 64 | list) |
| 65 | [ -z "$filename" ] && echo "No filename! Program terminated!" && exit 1 |
| 66 | ;; |
| 67 | group) |
| 68 | [ -z "$group2del" ] && echo "No groupname to delete! Program terminated!" && exit 1 |
| 69 | ;; |
| 70 | *) |
| 71 | echo "Usage: " |
| 72 | echo "Option 1:" |
| 73 | echo "$0 [-s|--single] <username> <groupname>" |
| 74 | echo " delete a single user <username> with group <groupname>" |
| 75 | echo |
| 76 | echo "$0 [-r|--range] <prefix> <start> <end> <groupname>" |
| 77 | echo " delete a range of users from <prefix><start> to <prefix><end> with group <groupname>," |
| 78 | echo "Option 2:" |
| 79 | echo "$0 [-f|--file] <filename>" |
| 80 | echo " delete users that are listed in <filename>." |
| 81 | echo " the username/password pairs are listed in $useradd_gen" |
| 82 | echo |
| 83 | echo " the format of the file <filename>: PREFIX START END GROUPNAME" |
| 84 | echo " for example: " |
| 85 | echo " # account for student" |
| 86 | echo " s 89101 89129 g3c9" |
| 87 | echo " # account for teacher" |
| 88 | echo " tckps 01 99 teacher" |
| 89 | echo "Option 3:" |
| 90 | echo "$0 [-l|--list] <filename>" |
| 91 | echo " delete users that are listed in <filename>." |
| 92 | echo " the username/password pairs are listed in $useradd_gen" |
| 93 | echo |
| 94 | echo " the format of the file <filename>: ID GROUPNAME" |
| 95 | echo " for example: " |
| 96 | echo " # account for student001" |
| 97 | echo " student001 g3c9" |
| 98 | echo " # account for student002" |
| 99 | echo " student002 g3c9" |
| 100 | echo "Option 4:" |
| 101 | echo "$0 [-g|--group] <group>" |
| 102 | echo " delete users that are in the group." |
| 103 | exit 1 |
| 104 | ;; |
| 105 | |
| 106 | esac |
| 107 | |
| 108 | # check the necessary files |
| 109 | file_to_be_checked="$useradd_range_exec_file $userdel_range_exec_file $useradd_file_exec_file $userdel_file_exec_file $useradd_list_exec_file $userdel_list_exec_file" |
| 110 | |
| 111 | for ifile in $file_to_be_checked; do |
| 112 | if ! type "$ifile" &>/dev/null; then |
| 113 | echo "No $ifile file!" |
| 114 | exit 1 |
| 115 | fi |
| 116 | done |
| 117 | |
| 118 | userdel_gen_tmp=`mktemp /tmp/userdel.XXXXXX` |
| 119 | |
| 120 | # file to store the username and password, clean it first. |
| 121 | [ -f "$userdel_gen" ] && rm -f $userdel_gen |
| 122 | |
| 123 | # add/delete single user |
| 124 | case "$format" in |
| 125 | single) |
| 126 | # delete single users |
| 127 | echo -n "Do you also want to clean user's home directory [y/N] ? " |
| 128 | read clean_home |
| 129 | case "$clean_home" in |
| 130 | y|Y|[yY][eE][sS]) |
| 131 | echo "Warning! The user's home directory will be deleted! Are you sure ?" |
| 132 | echo -n "[y/N] " |
| 133 | read clean_home_confirm |
| 134 | case "$clean_home_confirm" in |
| 135 | y|Y) |
| 136 | RM_HOME_OPT="-r" |
| 137 | ;; |
| 138 | *) |
| 139 | RM_HOME_OPT="" |
| 140 | esac |
| 141 | ;; |
| 142 | *) |
| 143 | RM_HOME_OPT="" |
| 144 | esac |
| 145 | |
| 146 | if grep "^$username:" /etc/passwd >/dev/null; then |
| 147 | echo -n "Deleting account $username..." |
| 148 | /usr/sbin/userdel $RM_HOME_OPT $username |
| 149 | echo "done!" |
| 150 | fi |
| 151 | ;; |
| 152 | |
| 153 | range) |
| 154 | # del a range of users |
| 155 | echo "Setuping..." |
| 156 | run_cmd="$userdel_range_exec_file $prefix $start $end $groupname" |
| 157 | eval "$run_cmd | tee -a $userdel_gen_tmp" |
| 158 | ;; |
| 159 | |
| 160 | file) |
| 161 | # del users which are listed in file |
| 162 | echo "Setuping..." |
| 163 | run_cmd="$userdel_file_exec_file $filename" |
| 164 | eval "$run_cmd | tee -a $userdel_gen_tmp" |
| 165 | ;; |
| 166 | |
| 167 | list) |
| 168 | # del users which are listed line by line in file |
| 169 | echo "Setuping..." |
| 170 | run_cmd="$userdel_list_exec_file $filename" |
| 171 | eval "$run_cmd | tee -a $userdel_gen_tmp" |
| 172 | ;; |
| 173 | |
| 174 | group) |
| 175 | # del the users in a group |
| 176 | # check if the group2del exist ? |
| 177 | user2del=`grep "^$group2del:" /etc/group | awk -F':' '{print $4}' | sed -e "s/,/ /g" ` |
| 178 | if ! grep "^$group2del:" /etc/group > /dev/null; then |
| 179 | echo "The group \"$group2del\" to be killed does not exist!!! Program terminated!" |
| 180 | exit 1 |
| 181 | else |
| 182 | echo "The usernames for the group \"$group2del\" to be killed are: $user2del, are you sure want to continue ?" |
| 183 | echo "[y/N] " |
| 184 | read kill_group_confirm |
| 185 | case "$kill_group_confirm" in |
| 186 | y|Y|[yY][eE][sS]) |
| 187 | continue |
| 188 | ;; |
| 189 | *) |
| 190 | echo "Ok, exit now!" |
| 191 | exit 0 |
| 192 | esac |
| 193 | |
| 194 | fi |
| 195 | |
| 196 | echo -n "Do you also want to clean user's home directory [y/N] ? " |
| 197 | read clean_home |
| 198 | case "$clean_home" in |
| 199 | y|Y|[yY][eE][sS]) |
| 200 | echo "Warning! The user's home directory will be deleted! Are you sure ?" |
| 201 | echo -n "[y/N] " |
| 202 | read clean_home_confirm |
| 203 | case "$clean_home_confirm" in |
| 204 | y|Y) |
| 205 | RM_HOME_OPT="-r" |
| 206 | ;; |
| 207 | *) |
| 208 | RM_HOME_OPT="" |
| 209 | esac |
| 210 | ;; |
| 211 | *) |
| 212 | RM_HOME_OPT="" |
| 213 | esac |
| 214 | echo -n "Do you also want to clean group [Y/n] ? " |
| 215 | read clean_group |
| 216 | case "$clean_group" in |
| 217 | n|N|[nN][oO]) |
| 218 | RM_GROUP_OPT="" |
| 219 | ;; |
| 220 | *) |
| 221 | RM_GROUP_OPT="yes" |
| 222 | esac |
| 223 | echo "Setuping..." |
| 224 | for iuser in $user2del; do |
| 225 | echo -n "Deleting account $iuser..." |
| 226 | /usr/sbin/userdel $RM_HOME_OPT $iuser |
| 227 | echo "done!" |
| 228 | done |
| 229 | |
| 230 | if [ "$RM_GROUP_OPT" = "yes" ]; then |
| 231 | if grep "^$group2del:" /etc/group >/dev/null; then |
| 232 | echo -n "Deleting group $group2del..." |
| 233 | /usr/sbin/groupdel $group2del |
| 234 | echo "done!" |
| 235 | fi |
| 236 | fi |
| 237 | ;; |
| 238 | |
| 239 | esac |
| 240 | |
| 241 | # |
| 242 | echo "Now update the NIS data in /var/yp ..." |
| 243 | make -C /var/yp/ |
| 244 | echo |
| 245 | |
| 246 | # |
| 247 | [ -f "$userdel_gen_tmp" ] && rm -f $userdel_gen_tmp |
| 248 | |
| 249 | echo "Done." |
| 250 | exit 0 |