| 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 use 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 | ACCOUNT_FILE="$1" |
| 16 | ACCOUNT_FILE_TMP=`mktemp /tmp/account_tmp.XXXXXX` |
| 17 | mode="" |
| 18 | |
| 19 | USAGE="Usage: $0 account_file" |
| 20 | # Check if root or not |
| 21 | if [ ! "$UID" = "0" ]; then |
| 22 | echo |
| 23 | echo "[$LOGNAME] You need to run this script \"$0\" as root." |
| 24 | echo |
| 25 | exit 1 |
| 26 | fi |
| 27 | |
| 28 | if [ $# -ne 1 ]; then |
| 29 | echo "$USAGE" |
| 30 | echo "Create or delete accounts from the specified account_file." |
| 31 | echo "The format of the file <filename>: PREFIX START END GROUPNAME PASSWD_OPT" |
| 32 | echo "PASSWD_OPT:" |
| 33 | echo "If one digit, it's the length of randomly created password." |
| 34 | echo "If blank, it will be randomly generated with some (say:8) characters." |
| 35 | echo "Other setting is the password itself." |
| 36 | echo "for example: " |
| 37 | echo "-----------------------------------------------------------------" |
| 38 | echo "# account for student" |
| 39 | echo "s 89101 89129 g3c9 8" |
| 40 | echo "# account for teacher" |
| 41 | echo "tckps 01 99 teacher drblnice" |
| 42 | echo "-----------------------------------------------------------------" |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | [ ! -f "$ACCOUNT_FILE" ] && echo "File $ACCOUNT_FILE not exists!" && exit 1 |
| 47 | [ ! -f "$useradd_range_exec_file" ] && echo "Can NOT find $useradd_range_exec_file file!" && exit 1 |
| 48 | [ ! -f "$userdel_range_exec_file" ] && echo "Can NOT find $userdel_range_exec_file file!" && exit 1 |
| 49 | |
| 50 | # filter the comment line, only keep the account line |
| 51 | grep -v '^[[:space:]]*#' $ACCOUNT_FILE > $ACCOUNT_FILE_TMP |
| 52 | |
| 53 | # |
| 54 | if [ -n "$(basename $0 | grep -E "useradd")" ]; then |
| 55 | mode="useradd" |
| 56 | elif [ -n "$(basename $0 | grep -E "userdel")" ]; then |
| 57 | mode="userdel" |
| 58 | else |
| 59 | echo "Unknown mode!" |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | # useradd mode |
| 64 | if [ "$mode" = "useradd" ]; then |
| 65 | while read prefix start end groupname password_opt; do |
| 66 | # check if groupname is not valid one |
| 67 | if `echo "$groupname" | grep -q "^[0-9]"`; then |
| 68 | echo "groupname can NOT begin with digits (0-9)!" |
| 69 | echo "The one you specified is \"$groupname\"" |
| 70 | echo "Program terminated" |
| 71 | exit 1 |
| 72 | fi |
| 73 | if [ -n "$prefix" ]; then |
| 74 | echo -n "Creating accounts with $prefix $start $end $groupname $password_opt" |
| 75 | $useradd_range_exec_file $prefix $start $end $groupname $password_opt |
| 76 | echo "done!" |
| 77 | fi |
| 78 | done < $ACCOUNT_FILE_TMP |
| 79 | fi |
| 80 | |
| 81 | # userdel mode |
| 82 | if [ "$mode" = "userdel" ]; then |
| 83 | # prompt to ask some confirmation |
| 84 | echo -n "Do you also want to clean user's home directory [y/N] ? " |
| 85 | read clean_home |
| 86 | case "$clean_home" in |
| 87 | y|Y|[yY][eE][sS]) |
| 88 | echo "Warning! The user's home directory will be deleted! Are you sure ?" |
| 89 | echo -n "[y/N] " |
| 90 | read clean_home_confirm |
| 91 | ;; |
| 92 | *) |
| 93 | RM_HOME_OPT="" |
| 94 | esac |
| 95 | echo -n "Do you also want to clean group [Y/n] ? " |
| 96 | read clean_group |
| 97 | CONFIRM_TMP=`mktemp /tmp/confirm_tmp.XXXXXX` |
| 98 | echo "$clean_home" >> $CONFIRM_TMP |
| 99 | echo "$clean_home_confirm" >> $CONFIRM_TMP |
| 100 | echo "$clean_group" >> $CONFIRM_TMP |
| 101 | |
| 102 | while read prefix start end groupname password_opt; do |
| 103 | if [ -n "$prefix" ]; then |
| 104 | echo "Deleting accounts with $prefix $start $end $groupname $password_opt" |
| 105 | $userdel_range_exec_file $prefix $start $end $groupname < $CONFIRM_TMP |
| 106 | echo "done!" |
| 107 | fi |
| 108 | done < $ACCOUNT_FILE_TMP |
| 109 | fi |
| 110 | |
| 111 | [ -f "$ACCOUNT_FILE_TMP" ] && rm -f $ACCOUNT_FILE_TMP |
| 112 | [ -f "$CONFIRM_TMP" ] && rm -f $CONFIRM_TMP |