| 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 | # |
| 20 | USAGE="Usage: $0 account_file" |
| 21 | # Check if root or not |
| 22 | if [ ! "$UID" = "0" ]; then |
| 23 | echo |
| 24 | echo "[$LOGNAME] You need to run this script \"$0\" as root." |
| 25 | echo |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | if [ $# -ne 1 ]; then |
| 30 | echo "$USAGE" |
| 31 | echo "Example:" |
| 32 | echo "The file account.txt consists the following:" |
| 33 | echo "student001 g3c2 drblgood" |
| 34 | echo "student002 g5c9 8" |
| 35 | echo "run \"drbl-useradd-list account.txt\" will create the accounts" |
| 36 | echo "student001 with his/her supplementary group g3c2 and password drblgood" |
| 37 | echo "and" |
| 38 | echo "student002 with his/her supplementary group g5c9 and randomly password with some (say, 8) characters." |
| 39 | echo |
| 40 | echo "The file account.txt consists the following:" |
| 41 | echo "student001 g3c2" |
| 42 | echo "student002 g5c9" |
| 43 | echo "run \"drbl-userdel-list account.txt\" will delete the accounts" |
| 44 | echo "student001 with his/her supplementary group g3c2" |
| 45 | echo "and" |
| 46 | echo "student002 with his/her supplementary group g5c9" |
| 47 | |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
| 51 | [ ! -f "$ACCOUNT_FILE" ] && echo "File $ACCOUNT_FILE not exists!" && exit 1 |
| 52 | |
| 53 | # filter the comment line, only keep the account line |
| 54 | grep -v '^[[:space:]]*#' $ACCOUNT_FILE > $ACCOUNT_FILE_TMP |
| 55 | |
| 56 | if [ -n "$(basename $0 | grep -E "useradd")" ]; then |
| 57 | mode="useradd" |
| 58 | elif [ -n "$(basename $0 | grep -E "userdel")" ]; then |
| 59 | mode="userdel" |
| 60 | else |
| 61 | echo "Unknown mode!" |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | # useradd mode |
| 66 | if [ "$mode" = "useradd" ]; then |
| 67 | |
| 68 | while read id groupname password_opt; do |
| 69 | # check if groupname is not valid one |
| 70 | if `echo "$groupname" | grep -q "^[0-9]"`; then |
| 71 | echo "groupname can NOT begin with digits (0-9)!" |
| 72 | echo "The one you specified is \"$groupname\"" |
| 73 | echo "Program terminated" |
| 74 | exit 1 |
| 75 | fi |
| 76 | if ! grep -q "^$groupname:" /etc/group; then |
| 77 | echo -n "Creating group $groupname..." |
| 78 | /usr/sbin/groupadd $groupname |
| 79 | echo "done!" |
| 80 | fi |
| 81 | |
| 82 | if ! grep -q "^$id:" /etc/passwd; then |
| 83 | echo -n "Creating account $id..." |
| 84 | /usr/sbin/useradd -m $id -G "$groupname" |
| 85 | echo "done!" |
| 86 | else |
| 87 | echo "Account $id exists! Skip!" |
| 88 | fi |
| 89 | |
| 90 | case "$password_opt" in |
| 91 | [0-9]|"") |
| 92 | # get one digit, so it must be the length of password |
| 93 | # or it's empty, we set it as $password_opt_default |
| 94 | if [ -z "$password_opt" ]; then |
| 95 | echo "You did NOT set the length of password, set it as $PASSWD_LENGTH_DEFAULT." |
| 96 | password_opt="$PASSWD_LENGTH_DEFAULT" |
| 97 | fi |
| 98 | echo "Randomly set password from password length..." |
| 99 | make_random_password $password_opt |
| 100 | random_pw=$random_password |
| 101 | echo "The password of $id is \"$random_pw\"" |
| 102 | echo "$id:$random_pw" | /usr/sbin/chpasswd |
| 103 | ;; |
| 104 | *) |
| 105 | echo "Set password from input..." |
| 106 | echo "The password of $id is \"$password_opt\"" |
| 107 | echo "$id:$password_opt" | /usr/sbin/chpasswd |
| 108 | esac |
| 109 | echo "finished creating account for $id" |
| 110 | done < $ACCOUNT_FILE_TMP |
| 111 | fi |
| 112 | |
| 113 | # useradd mode |
| 114 | if [ "$mode" = "userdel" ]; then |
| 115 | echo -n "Do you also want to clean user's home directory [y/N] ? " |
| 116 | read clean_home |
| 117 | case "$clean_home" in |
| 118 | y|Y) |
| 119 | echo "Warning! The user's home directory will be deleted! Are you sure ?" |
| 120 | echo -n "[y/N] " |
| 121 | read clean_home_confirm |
| 122 | case "$clean_home_confirm" in |
| 123 | y|Y) |
| 124 | RM_HOME_OPT="-r" |
| 125 | ;; |
| 126 | *) |
| 127 | RM_HOME_OPT="" |
| 128 | esac |
| 129 | ;; |
| 130 | *) |
| 131 | RM_HOME_OPT="" |
| 132 | esac |
| 133 | while read id groupname password_opt; do |
| 134 | if grep -q "^$groupname:" /etc/group; then |
| 135 | echo -n "Deleting group $groupname..." |
| 136 | /usr/sbin/groupdel $groupname |
| 137 | echo "done!" |
| 138 | fi |
| 139 | |
| 140 | if grep -q "^$id:" /etc/passwd; then |
| 141 | echo -n "Deleting account $id..." |
| 142 | /usr/sbin/userdel $RM_HOME_OPT $id |
| 143 | echo "done!" |
| 144 | fi |
| 145 | done < $ACCOUNT_FILE_TMP |
| 146 | fi |
| 147 | |
| 148 | [ -f "$ACCOUNT_FILE_TMP" ] && rm -f $ACCOUNT_FILE_TMP |