| 1 | #!/bin/bashscip-shell filesystem . . `/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=`mktempscip-shell system . . mktemp. /tmp/account_tmp.XXXXXXscip-shell filesystem . . `/tmp/account_tmp.XXXXXX`.` |
| 17 | mode="" |
| 18 | |
| 19 | # |
| 20 | USAGE="Usage: $0 account_file" |
| 21 | # Check if root or not |
| 22 | if [scip-shell system . . `[`. ! "$UID" = "0" ]; then |
| 23 | echoscip-shell system . . echo. |
| 24 | echoscip-shell system . . echo. "[$LOGNAME] You need to run this script \"$0\" as root." |
| 25 | echoscip-shell system . . echo. |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | if [scip-shell system . . `[`. $# -ne 1 ]; then |
| 30 | echoscip-shell system . . echo. "$USAGE" |
| 31 | echoscip-shell system . . echo. "Example:" |
| 32 | echoscip-shell system . . echo. "The file account.txt consists the following:" |
| 33 | echoscip-shell system . . echo. "student001 g3c2 drblgood" |
| 34 | echoscip-shell system . . echo. "student002 g5c9 8" |
| 35 | echoscip-shell system . . echo. "run \"drbl-useradd-list account.txt\" will create the accounts" |
| 36 | echoscip-shell system . . echo. "student001 with his/her supplementary group g3c2 and password drblgood" |
| 37 | echoscip-shell system . . echo. "and" |
| 38 | echoscip-shell system . . echo. "student002 with his/her supplementary group g5c9 and randomly password with some (say, 8) characters." |
| 39 | echoscip-shell system . . echo. |
| 40 | echoscip-shell system . . echo. "The file account.txt consists the following:" |
| 41 | echoscip-shell system . . echo. "student001 g3c2" |
| 42 | echoscip-shell system . . echo. "student002 g5c9" |
| 43 | echoscip-shell system . . echo. "run \"drbl-userdel-list account.txt\" will delete the accounts" |
| 44 | echoscip-shell system . . echo. "student001 with his/her supplementary group g3c2" |
| 45 | echoscip-shell system . . echo. "and" |
| 46 | echoscip-shell system . . echo. "student002 with his/her supplementary group g5c9" |
| 47 | |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
| 51 | [scip-shell system . . `[`. ! -f "$ACCOUNT_FILE" ] && echoscip-shell system . . echo. "File $ACCOUNT_FILE not exists!" && exit 1 |
| 52 | |
| 53 | # filter the comment line, only keep the account line |
| 54 | grepscip-shell system . . grep. -v '^[[:space:]]*#' $ACCOUNT_FILE > $ACCOUNT_FILE_TMP |
| 55 | |
| 56 | if [scip-shell system . . `[`. -n "$(basenamescip-shell system . . basename. $0 | grepscip-shell system . . grep. -E "useradd")" ]; then |
| 57 | mode="useradd" |
| 58 | elif [scip-shell system . . `[`. -n "$(basenamescip-shell system . . basename. $0 | grepscip-shell system . . grep. -E "userdel")" ]; then |
| 59 | mode="userdel" |
| 60 | else |
| 61 | echoscip-shell system . . echo. "Unknown mode!" |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | # useradd mode |
| 66 | if [scip-shell system . . `[`. "$mode" = "useradd" ]; then |
| 67 | |
| 68 | while read id groupname password_opt; do |
| 69 | # check if groupname is not valid one |
| 70 | if `echoscip-shell system . . echo. "$groupname" | grepscip-shell system . . grep. -q "^[0-9]"`; then |
| 71 | echoscip-shell system . . echo. "groupname can NOT begin with digits (0-9)!" |
| 72 | echoscip-shell system . . echo. "The one you specified is \"$groupname\"" |
| 73 | echoscip-shell system . . echo. "Program terminated" |
| 74 | exit 1 |
| 75 | fi |
| 76 | if ! grepscip-shell system . . grep. -q "^$groupname:" /etc/groupscip-shell filesystem . . `/etc/group`.; then |
| 77 | echoscip-shell system . . echo. -n "Creating group $groupname..." |
| 78 | /usr/sbin/groupaddscip-shell filesystem . . `/usr/sbin/groupadd`. $groupname |
| 79 | echoscip-shell system . . echo. "done!" |
| 80 | fi |
| 81 | |
| 82 | if ! grepscip-shell system . . grep. -q "^$id:" /etc/passwdscip-shell filesystem . . `/etc/passwd`.; then |
| 83 | echoscip-shell system . . echo. -n "Creating account $id..." |
| 84 | /usr/sbin/useraddscip-shell filesystem . . `/usr/sbin/useradd`. -m $id -G "$groupname" |
| 85 | echoscip-shell system . . echo. "done!" |
| 86 | else |
| 87 | echoscip-shell system . . 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 [scip-shell system . . `[`. -z "$password_opt" ]; then |
| 95 | echoscip-shell system . . echo. "You did NOT set the length of password, set it as $PASSWD_LENGTH_DEFAULT." |
| 96 | password_opt="$PASSWD_LENGTH_DEFAULT" |
| 97 | fi |
| 98 | echoscip-shell system . . echo. "Randomly set password from password length..." |
| 99 | make_random_password $password_opt |
| 100 | random_pw=$random_password |
| 101 | echoscip-shell system . . echo. "The password of $id is \"$random_pw\"" |
| 102 | echoscip-shell system . . echo. "$id:$random_pw" | /usr/sbin/chpasswdscip-shell filesystem . . `/usr/sbin/chpasswd`. |
| 103 | ;; |
| 104 | *) |
| 105 | echoscip-shell system . . echo. "Set password from input..." |
| 106 | echoscip-shell system . . echo. "The password of $id is \"$password_opt\"" |
| 107 | echoscip-shell system . . echo. "$id:$password_opt" | /usr/sbin/chpasswdscip-shell filesystem . . `/usr/sbin/chpasswd`. |
| 108 | esac |
| 109 | echoscip-shell system . . echo. "finished creating account for $id" |
| 110 | done < $ACCOUNT_FILE_TMP |
| 111 | fi |
| 112 | |
| 113 | # useradd mode |
| 114 | if [scip-shell system . . `[`. "$mode" = "userdel" ]; then |
| 115 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Warning! The user's home directory will be deleted! Are you sure ?" |
| 120 | echoscip-shell system . . 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 grepscip-shell system . . grep. -q "^$groupname:" /etc/groupscip-shell filesystem . . `/etc/group`.; then |
| 135 | echoscip-shell system . . echo. -n "Deleting group $groupname..." |
| 136 | /usr/sbin/groupdelscip-shell filesystem . . `/usr/sbin/groupdel`. $groupname |
| 137 | echoscip-shell system . . echo. "done!" |
| 138 | fi |
| 139 | |
| 140 | if grepscip-shell system . . grep. -q "^$id:" /etc/passwdscip-shell filesystem . . `/etc/passwd`.; then |
| 141 | echoscip-shell system . . echo. -n "Deleting account $id..." |
| 142 | /usr/sbin/userdelscip-shell filesystem . . `/usr/sbin/userdel`. $RM_HOME_OPT $id |
| 143 | echoscip-shell system . . echo. "done!" |
| 144 | fi |
| 145 | done < $ACCOUNT_FILE_TMP |
| 146 | fi |
| 147 | |
| 148 | [scip-shell system . . `[`. -f "$ACCOUNT_FILE_TMP" ] && rmscip-shell system . . rm. -f $ACCOUNT_FILE_TMP |