| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Author: Blake, Kuo-Lien Huang |
| 3 | # License: GPL |
| 4 | # Description: |
| 5 | # * creat accounts with prefix and specific range |
| 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 | ACCOUNT_FILE_TMP=`mktempscip-shell system . . mktemp. /tmp/account_tmp.XXXXXXscip-shell filesystem . . `/tmp/account_tmp.XXXXXX`.` |
| 16 | |
| 17 | # |
| 18 | check_if_root |
| 19 | |
| 20 | # |
| 21 | run_cmd="`basenamescip-shell system . . basename. $0`" |
| 22 | |
| 23 | if [scip-shell system . . `[`. -n "$(basenamescip-shell system . . basename. $0 | grepscip-shell system . . grep. -E "useradd")" ]; then |
| 24 | mode="useradd" |
| 25 | elif [scip-shell system . . `[`. -n "$(basenamescip-shell system . . basename. $0 | grepscip-shell system . . grep. -E "userdel")" ]; then |
| 26 | mode="userdel" |
| 27 | else |
| 28 | echoscip-shell system . . echo. "Unknown mode!" |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | if [scip-shell system . . `[`. $# -ne 3 -a $# -ne 4 -a $# -ne 5 ]; then |
| 33 | echoscip-shell system . . echo. "Usage:" |
| 34 | echoscip-shell system . . echo. "To add accounts:" |
| 35 | echoscip-shell system . . echo. "drbl-useradd-range name_prefix start_no end_no groupname password_opt" |
| 36 | echoscip-shell system . . echo. "password_opt:" |
| 37 | echoscip-shell system . . echo. "If one digit, it's the length of randomly created password." |
| 38 | echoscip-shell system . . echo. "If blank, it will be randomly generated with 8 characters." |
| 39 | echoscip-shell system . . echo. "Other setting is the password itself." |
| 40 | echoscip-shell system . . echo. "Ex: drbl-useradd-range student 2 5 g3c2 8" |
| 41 | echoscip-shell system . . echo. "or" |
| 42 | echoscip-shell system . . echo. "drbl-useradd-range student 2 5 g3c2 drblnice" |
| 43 | echoscip-shell system . . echo. |
| 44 | echoscip-shell system . . echo. "To delete accounts:" |
| 45 | echoscip-shell system . . echo. "drbl-userdel-range name_prefix start_no end_no <groupname>" |
| 46 | echoscip-shell system . . echo. "Ex: drbl-userdel-range student 2 5 g3c2" |
| 47 | echoscip-shell system . . echo. "or" |
| 48 | echoscip-shell system . . echo. "drbl-userdel-range student 2 5" |
| 49 | exit 1 |
| 50 | fi |
| 51 | |
| 52 | name_prefix=$1 |
| 53 | start_no=$2 |
| 54 | end_no=$3 |
| 55 | groupname=$4 |
| 56 | password_opt=$5 |
| 57 | |
| 58 | # check if groupname is not valid one |
| 59 | if `echoscip-shell system . . echo. "$groupname" | grepscip-shell system . . grep. -q "^[0-9]"`; then |
| 60 | echoscip-shell system . . echo. "groupname can NOT begin with digits (0-9)!" |
| 61 | echoscip-shell system . . echo. "The one you specified is \"$groupname\"" |
| 62 | echoscip-shell system . . echo. "Program terminated" |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | ask_and_load_lang_set |
| 67 | |
| 68 | # |
| 69 | i=$start_no |
| 70 | student=$name_prefix |
| 71 | nnum=`echoscip-shell system . . echo. -n "$end_no" | wcscip-shell system . . wc. -c` |
| 72 | while [scip-shell system . . `[`. $i -le $end_no ] ; do |
| 73 | label="$i" |
| 74 | n=`echoscip-shell system . . echo. -n "$label" | wcscip-shell system . . wc. -c` |
| 75 | while [scip-shell system . . `[`. $n -lt $nnum ]; do |
| 76 | label="0$label" |
| 77 | n=`echoscip-shell system . . echo. -n "$label" | wcscip-shell system . . wc. -c` |
| 78 | done |
| 79 | # output the results to temp file |
| 80 | echoscip-shell system . . echo. "$student$label $groupname $password_opt" >> $ACCOUNT_FILE_TMP |
| 81 | i=`exprscip-shell system . . expr. $i + 1` |
| 82 | done |
| 83 | |
| 84 | # useradd mode |
| 85 | if [scip-shell system . . `[`. "$mode" = "useradd" ]; then |
| 86 | while read id groupname password_opt; do |
| 87 | if ! grepscip-shell system . . grep. -q "^$groupname:" /etc/groupscip-shell filesystem . . `/etc/group`.; then |
| 88 | echoscip-shell system . . echo. -n "Creating group $groupname..." |
| 89 | /usr/sbin/groupaddscip-shell filesystem . . `/usr/sbin/groupadd`. "$groupname" |
| 90 | echoscip-shell system . . echo. "done!" |
| 91 | fi |
| 92 | if ! grepscip-shell system . . grep. -q "^$id:" /etc/passwdscip-shell filesystem . . `/etc/passwd`.; then |
| 93 | echoscip-shell system . . echo. -n "Creating account $id..." |
| 94 | /usr/sbin/useraddscip-shell filesystem . . `/usr/sbin/useradd`. -m -G "$groupname" "$id" |
| 95 | echoscip-shell system . . echo. "done!" |
| 96 | else |
| 97 | echoscip-shell system . . echo. "Account $id exists! Skip!" |
| 98 | fi |
| 99 | |
| 100 | case "$password_opt" in |
| 101 | [0-9]|"") |
| 102 | # get one digit, so it must be the length of password |
| 103 | # or it's empty, we set it as $password_opt_default |
| 104 | if [scip-shell system . . `[`. -z "$password_opt" ]; then |
| 105 | echoscip-shell system . . echo. "You did NOT set the length of password, set it as $PASSWD_LENGTH_DEFAULT." |
| 106 | password_opt="$PASSWD_LENGTH_DEFAULT" |
| 107 | fi |
| 108 | echoscip-shell system . . echo. "Randomly set password from password length..." |
| 109 | make_random_password $password_opt |
| 110 | random_pw=$random_password |
| 111 | echoscip-shell system . . echo. "The password of $id is \"$random_pw\"" |
| 112 | echoscip-shell system . . echo. "$id:$random_pw" | /usr/sbin/chpasswdscip-shell filesystem . . `/usr/sbin/chpasswd`. |
| 113 | ;; |
| 114 | *) |
| 115 | echoscip-shell system . . echo. "Set password from input..." |
| 116 | echoscip-shell system . . echo. "The password of $id is \"$password_opt\"" |
| 117 | echoscip-shell system . . echo. "$id:$password_opt" | /usr/sbin/chpasswdscip-shell filesystem . . `/usr/sbin/chpasswd`. |
| 118 | esac |
| 119 | echoscip-shell system . . echo. "finished creating account for $id" |
| 120 | done < $ACCOUNT_FILE_TMP |
| 121 | fi |
| 122 | |
| 123 | # useradd mode |
| 124 | if [scip-shell system . . `[`. "$mode" = "userdel" ]; then |
| 125 | user_2_be_removed="$(awkscip-shell system . . awk. -F" " '{print $1}' $ACCOUNT_FILE_TMP)" |
| 126 | user_2_be_removed="$(echoscip-shell system . . echo. $user_2_be_removed)" # Make it in one line |
| 127 | echoscip-shell system . . echo. "User(s) to be removed:" |
| 128 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 129 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 130 | echoscip-shell system . . echo. "$user_2_be_removed" |
| 131 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 132 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 133 | echoscip-shell system . . echo. -n "Do you also want to clean user's home directory [y/N] ? " |
| 134 | read clean_home |
| 135 | case "$clean_home" in |
| 136 | y|Y|[yY][eE][sS]) |
| 137 | echoscip-shell system . . echo. "Warning! The user's home directory will be deleted! Are you sure ?" |
| 138 | echoscip-shell system . . echo. -n "[y/N] " |
| 139 | read clean_home_confirm |
| 140 | case "$clean_home_confirm" in |
| 141 | y|Y) |
| 142 | RM_HOME_OPT="-r" |
| 143 | ;; |
| 144 | *) |
| 145 | RM_HOME_OPT="" |
| 146 | esac |
| 147 | ;; |
| 148 | *) |
| 149 | RM_HOME_OPT="" |
| 150 | esac |
| 151 | echoscip-shell system . . echo. -n "Do you also want to clean group [Y/n] ? " |
| 152 | read clean_group |
| 153 | case "$clean_group" in |
| 154 | n|N|[nN][oO]) |
| 155 | RM_GROUP_OPT="" |
| 156 | ;; |
| 157 | *) |
| 158 | RM_GROUP_OPT="yes" |
| 159 | esac |
| 160 | while read id groupname password_opt; do |
| 161 | if [scip-shell system . . `[`. "$RM_GROUP_OPT" = "yes" ]; then |
| 162 | if [scip-shell system . . `[`. -n "$groupname" ]; then |
| 163 | if grepscip-shell system . . grep. -q "^$groupname:" /etc/groupscip-shell filesystem . . `/etc/group`.; then |
| 164 | echoscip-shell system . . echo. -n "Deleting group $groupname..." |
| 165 | /usr/sbin/groupdelscip-shell filesystem . . `/usr/sbin/groupdel`. $groupname |
| 166 | echoscip-shell system . . echo. "done!" |
| 167 | else |
| 168 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 169 | echoscip-shell system . . echo. "Can NOT find group $groupname!!!" |
| 170 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 171 | fi |
| 172 | fi |
| 173 | fi |
| 174 | |
| 175 | if grepscip-shell system . . grep. -q "^$id:" /etc/passwdscip-shell filesystem . . `/etc/passwd`.; then |
| 176 | echoscip-shell system . . echo. -n "Deleting account $id..." |
| 177 | /usr/sbin/userdelscip-shell filesystem . . `/usr/sbin/userdel`. $RM_HOME_OPT $id |
| 178 | echoscip-shell system . . echo. "done!" |
| 179 | else |
| 180 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 181 | echoscip-shell system . . echo. "Can NOT find account $id!!!" |
| 182 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 183 | fi |
| 184 | done < $ACCOUNT_FILE_TMP |
| 185 | |
| 186 | fi |
| 187 | # clean the temp file |
| 188 | [scip-shell system . . `[`. -f "$ACCOUNT_FILE_TMP" ] && rmscip-shell system . . rm. -f $ACCOUNT_FILE_TMP |
| 189 | |
| 190 | # |
| 191 | echoscip-shell system . . echo. "now update the yp data in /var/yp ..." |
| 192 | makescip-shell system . . make. -C /var/ypscip-shell filesystem . . `/var/yp`. |
| 193 | echoscip-shell system . . echo. "done!" |