| 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 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 12 | |
| 13 | # |
| 14 | USAGE() { |
| 15 | echo "Usage: " |
| 16 | echo "Option 1:" |
| 17 | echo "$0 [-s|--single] <username> <groupname>" |
| 18 | echo " generate a single user <username> with group <groupname>" |
| 19 | echo "Option 2:" |
| 20 | echo "$0 [-r|--range] <prefix> <start> <end> <groupname> [<passwd_opt>]" |
| 21 | echo " generate a range of users from <prefix><start> to <prefix><end> with group <groupname>," |
| 22 | echo " passwd_opt:" |
| 23 | echo " If one digit, it's the length of randomly created password." |
| 24 | echo " If blank, it will be randomly generated with some (say:8) characters." |
| 25 | echo " Other setting is the password itself." |
| 26 | echo "Option 3:" |
| 27 | echo "$0 [-f|--file] <filename>" |
| 28 | echo " generate users that are listed in <filename>." |
| 29 | echo " the username/password pairs are listed in $useradd_gen" |
| 30 | echo |
| 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 " # account for student" |
| 38 | echo " s 89101 89129 g3c9 8" |
| 39 | echo " # account for teacher" |
| 40 | echo " tckps 01 99 teacher drblnice" |
| 41 | echo "Option 4:" |
| 42 | echo "$0 [-l|--list] <filename>" |
| 43 | echo " generate users that are listed in <filename>." |
| 44 | echo " the username/password pairs are listed in $useradd_gen" |
| 45 | echo |
| 46 | echo " the format of the file <filename>: ID GROUPNAME PASSWD_OPT" |
| 47 | echo " passwd_opt:" |
| 48 | echo " If one digit, it's the length of randomly created password." |
| 49 | echo " If blank, it will be randomly generated with some (say:8) characters." |
| 50 | echo " Other setting is the password itself." |
| 51 | echo " for example: " |
| 52 | echo " # account for student001" |
| 53 | echo " student001 g3c9 8" |
| 54 | echo " # account for student002" |
| 55 | echo " student002 g3c9 drblnice" |
| 56 | } |
| 57 | |
| 58 | |
| 59 | # |
| 60 | check_if_root |
| 61 | |
| 62 | # Parse command-line options |
| 63 | while [ $# -gt 0 ]; do |
| 64 | case "$1" in |
| 65 | -s|--single) |
| 66 | format="single" |
| 67 | shift; username=$1 |
| 68 | shift; groupname=$1 |
| 69 | shift;; |
| 70 | -r|--range) |
| 71 | format="range" |
| 72 | shift; prefix=$1 |
| 73 | shift; start=$1 |
| 74 | shift; end=$1 |
| 75 | shift; groupname=$1 |
| 76 | shift |
| 77 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 78 | # skip the -xx option, in case |
| 79 | passwd_opt=$1 |
| 80 | fi |
| 81 | shift ;; |
| 82 | -f|--file) |
| 83 | format="file" |
| 84 | shift; filename=$1 |
| 85 | shift ;; |
| 86 | -l|--list) |
| 87 | format="list" |
| 88 | shift; filename=$1 |
| 89 | shift ;; |
| 90 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 91 | USAGE |
| 92 | exit 2 ;; |
| 93 | *) break ;; |
| 94 | esac |
| 95 | done |
| 96 | |
| 97 | # check if groupname is not valid one |
| 98 | if `echo "$groupname" | grep -q "^[0-9]"`; then |
| 99 | echo "groupname can NOT begin with digits (0-9)!" |
| 100 | echo "The one you specified is \"$groupname\"" |
| 101 | echo "Program terminated" |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | # |
| 106 | case "$format" in |
| 107 | single) |
| 108 | [ -z "$username" ] && echo "No username! Program terminated!" && exit 1 |
| 109 | [ -z "$groupname" ] && echo "No groupname! Program terminated!" && exit 1 |
| 110 | ;; |
| 111 | range) |
| 112 | [ -z "$prefix" ] && echo "No prefix! Program terminated!" && exit 1 |
| 113 | [ -z "$start" ] && echo "No start! Program terminated!" && exit 1 |
| 114 | [ -z "$end" ] && echo "No end! Program terminated!" && exit 1 |
| 115 | [ -z "$groupname" ] && echo "No groupname! Program terminated!" && exit 1 |
| 116 | ;; |
| 117 | file) |
| 118 | [ -z "$filename" ] && echo "No filename! Program terminated!" && exit 1 |
| 119 | ;; |
| 120 | list) |
| 121 | [ -z "$filename" ] && echo "No filename! Program terminated!" && exit 1 |
| 122 | ;; |
| 123 | *) |
| 124 | USAGE |
| 125 | exit 1 |
| 126 | ;; |
| 127 | |
| 128 | esac |
| 129 | |
| 130 | # check the necessary files |
| 131 | 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" |
| 132 | |
| 133 | for ifile in $file_to_be_checked; do |
| 134 | if ! type "$ifile" &>/dev/null; then |
| 135 | echo "No $ifile file!" |
| 136 | exit 1 |
| 137 | fi |
| 138 | done |
| 139 | |
| 140 | # |
| 141 | useradd_gen_tmp="$(mktemp /tmp/useradd.XXXXXX)" |
| 142 | |
| 143 | # file to store the username and password, clean it first. |
| 144 | [ -f "$useradd_gen" ] && rm -f $useradd_gen |
| 145 | |
| 146 | # add/delete single user |
| 147 | case "$format" in |
| 148 | single) |
| 149 | # add single users |
| 150 | echo "Password of $username ? (It will not be echoed in the screen. If you want to use random password, just press Enter)" |
| 151 | read -s passwd_single1 |
| 152 | if [ -z "$passwd_single1" ]; then |
| 153 | make_random_password |
| 154 | passwd_single1=$random_password |
| 155 | else |
| 156 | echo "Retype password of $username: (It will not be echoed in the screen)" |
| 157 | read -s passwd_single2 |
| 158 | while [ "$passwd_single1" != "$passwd_single2" ]; do |
| 159 | echo "Sorry, passwords do not match" |
| 160 | echo "Password of $username ? (It will not be echoed in the screen. If you want to use random password, just press Enter)" |
| 161 | read -s passwd_single1 |
| 162 | echo "Retype password of $username: (It will not be echoed in the screen)" |
| 163 | read -s passwd_single2 |
| 164 | done |
| 165 | fi |
| 166 | echo "The password of $username is \"$passwd_single1\"" >> $useradd_gen_tmp |
| 167 | run_cmd="/usr/sbin/groupadd $groupname; /usr/sbin/useradd -m $username -g $groupname; echo \"$username:$passwd_single1\" | /usr/sbin/chpasswd" |
| 168 | ;; |
| 169 | |
| 170 | range) |
| 171 | # add a range of users |
| 172 | run_cmd="$useradd_range_exec_file $prefix $start $end $groupname $passwd_opt" |
| 173 | ;; |
| 174 | |
| 175 | file) |
| 176 | # add users which are listed in file |
| 177 | run_cmd="$useradd_file_exec_file $filename" |
| 178 | ;; |
| 179 | list) |
| 180 | # add users which are listed line by line in file |
| 181 | run_cmd="$useradd_list_exec_file $filename" |
| 182 | ;; |
| 183 | |
| 184 | esac |
| 185 | |
| 186 | echo "Creating..." |
| 187 | eval "$run_cmd | tee -a $useradd_gen_tmp" |
| 188 | |
| 189 | # filter those we do not want... just output the password and username |
| 190 | grep "The password of" $useradd_gen_tmp > $useradd_gen |
| 191 | chmod 600 $useradd_gen |
| 192 | chown root:root $useradd_gen |
| 193 | |
| 194 | # |
| 195 | if [ -e /etc/debian_version ]; then |
| 196 | tune-debian-dev-group-perm -g "$desktop_user_group_debian" -e -r |
| 197 | fi |
| 198 | # |
| 199 | echo "Now update the NIS data in /var/yp ..." |
| 200 | make -C /var/yp/ |
| 201 | echo |
| 202 | # |
| 203 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 204 | echo "***********************************************************" |
| 205 | echo "The username/password pairs are listed in \"$useradd_gen\"." |
| 206 | echo "***********************************************************" |
| 207 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 208 | |
| 209 | [ -f "$useradd_gen_tmp" ] && rm -f $useradd_gen_tmp |
| 210 | |
| 211 | echo "Done." |
| 212 | exit 0 |