| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Author: Blake, Kuo-Lien Huang |
| 3 | # License: GPL |
| 4 | # Description: put/get the file to/from all user, file utility for user (fuu) |
| 5 | # |
| 6 | # Modified by Steven Shiau <[email protected]> to be used in DRBL for RedHat |
| 7 | |
| 8 | # Load DRBL setting and functions |
| 9 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 10 | |
| 11 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 12 | |
| 13 | # |
| 14 | run_cmd="`basenamescip-shell system . . basename. $0`" |
| 15 | |
| 16 | # to backword compatible |
| 17 | case "$run_cmd" in |
| 18 | "drbl-cp-user"|"drbl-user-cp") |
| 19 | run_cmd="drbl-fuu-put" |
| 20 | ;; |
| 21 | "drbl-rm-user"|"drbl-user-rm") |
| 22 | run_cmd="drbl-fuu-rm" |
| 23 | ;; |
| 24 | "drbl-get-user"|"drbl-user-get") |
| 25 | run_cmd="drbl-fuu-get" |
| 26 | ;; |
| 27 | esac |
| 28 | |
| 29 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 30 | echoscip-shell system . . echo. "NAME Put(default)/get/remove files to/from user's home directory." |
| 31 | echoscip-shell system . . echo. |
| 32 | echoscip-shell system . . echo. "DESCRIPTION" |
| 33 | echoscip-shell system . . echo. "drbl-cp-user (or drbl-user-cp) for putting files, drbl-get-user (or drbl-user-get) for collecting files, drbl-rm-user (or drbl-user-rm) for removing files." |
| 34 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` [-g|--group] group [-d|--directory] directory [-v|--verbose] source target" |
| 35 | echoscip-shell system . . echo. "-g, --group GROUP Assign the user group to be processed as GROUP." |
| 36 | echoscip-shell system . . echo. "-u, --user USERS Assign the user(s) to be processed as USERS." |
| 37 | echoscip-shell system . . echo. "-d, --directory DIR Process the sub-directory DIR in user's home" |
| 38 | echoscip-shell system . . echo. "-v, --verbose Prints out verbose information" |
| 39 | echoscip-shell system . . echo. "If GROUP or USERS is not assigned, all the users will be processed." |
| 40 | echoscip-shell system . . echo. |
| 41 | echoscip-shell system . . echo. "Example:" |
| 42 | echoscip-shell system . . echo. "drbl-cp-user -d work source_files new_file.txt" |
| 43 | echoscip-shell system . . echo. "will copy source_files into user's ~/work/ directory as file name new_file.txt" |
| 44 | echoscip-shell system . . echo. |
| 45 | echoscip-shell system . . echo. "drbl-cp-user -g g3c5 source_files new_file.txt" |
| 46 | echoscip-shell system . . echo. "will copy source_files into ~/work/ directory for the users belong to group g3c5 as file name new_file.txt" |
| 47 | echoscip-shell system . . echo. |
| 48 | echoscip-shell system . . echo. "drbl-cp-user -u \"john jack mary\" source_files new_file.txt" |
| 49 | echoscip-shell system . . echo. "will copy source_files into ~/work/ directory for the users john, jack and mary as file name new_file.txt" |
| 50 | echoscip-shell system . . echo. |
| 51 | echoscip-shell system . . echo. "drbl-rm-user -d work target.txt" |
| 52 | echoscip-shell system . . echo. "will remove target.txt from user's ~/work directory" |
| 53 | echoscip-shell system . . echo. |
| 54 | echoscip-shell system . . echo. "drbl-get-user -d work target.txt" |
| 55 | echoscip-shell system . . echo. "will get target.txt from user's ~/work directory" |
| 56 | echoscip-shell system . . echo. |
| 57 | echoscip-shell system . . echo. "drbl-get-user -d work \"*.txt\"" |
| 58 | echoscip-shell system . . echo. "will get *.txt from user's ~/work directory" |
| 59 | echoscip-shell system . . echo. "NOTE!!! You must put \" \" before and after the star wildcard * filename!" |
| 60 | } |
| 61 | |
| 62 | # |
| 63 | if [scip-shell system . . `[`. "$run_cmd" = "drbl-fuu" ]; then |
| 64 | echoscip-shell system . . echo. "use drbl-cp-user (or drbl-user-cp) for putting files, drbl-get-user (or drbl-user-get) for collecting files, drbl-rm-user (or drbl-user-rm) for removing files." |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 69 | case "$1" in |
| 70 | -g|--group) |
| 71 | shift; chosen_group="$1" |
| 72 | shift;; |
| 73 | -d|--directory) |
| 74 | shift; sub_dir="$1" |
| 75 | shift;; |
| 76 | -u|--users) |
| 77 | shift; chosen_users="$1" |
| 78 | shift;; |
| 79 | -v|--verbose) |
| 80 | shift; VERBOSE="on" |
| 81 | ;; |
| 82 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 83 | usagescip-shell shell shell-project 0.0.0 usage(). >& 2 |
| 84 | exit 2 ;; |
| 85 | *) break ;; |
| 86 | esac |
| 87 | done |
| 88 | |
| 89 | # get the source and target |
| 90 | source_file=$1 |
| 91 | target_file=$2 |
| 92 | |
| 93 | # |
| 94 | ask_and_load_lang_set $specified_lang |
| 95 | |
| 96 | # check if source_file is dir or file, if dir, append / |
| 97 | # To do: |
| 98 | # how about in get mode ? the prefix path (like /home/test/$source_file) must be appended. |
| 99 | [scip-shell system . . `[`. -d "$source_file" ] && source_file=$source_file"/" |
| 100 | |
| 101 | # check parameter |
| 102 | if [scip-shell system . . `[`. $# -le 0 ]; then |
| 103 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 104 | exit 1 |
| 105 | fi |
| 106 | case "$run_cmd" in |
| 107 | "drbl-fuu-put") |
| 108 | mode="put" |
| 109 | ;; |
| 110 | "drbl-fuu-get") |
| 111 | mode="get" |
| 112 | ;; |
| 113 | "drbl-fuu-rm") |
| 114 | mode="rm" |
| 115 | ;; |
| 116 | *) |
| 117 | echoscip-shell system . . echo. "Unknown mode! Did you change the filename ?" |
| 118 | echoscip-shell system . . echo. "Program terminated!" |
| 119 | exit 1 |
| 120 | ;; |
| 121 | esac |
| 122 | |
| 123 | # set the default values if empty |
| 124 | if [scip-shell system . . `[`. -z "$chosen_group" -a -z "$chosen_users" ]; then |
| 125 | chosen_group="all" |
| 126 | fi |
| 127 | if [scip-shell system . . `[`. -n "$chosen_group" -a -n "$chosen_users" ]; then |
| 128 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 129 | echoscip-shell system . . echo. "Group and user(s) can not be assigned together!" |
| 130 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 131 | echoscip-shell system . . echo. "Please only assign one of them." |
| 132 | echoscip-shell system . . echo. "$msg_program_stop" |
| 133 | exit 1 |
| 134 | fi |
| 135 | |
| 136 | [scip-shell system . . `[`. -z "$mode" ] && mode="put" |
| 137 | [scip-shell system . . `[`. -z "$source_file" ] && echoscip-shell system . . echo. "No source! Program terminated!" && exit 1 |
| 138 | [scip-shell system . . `[`. -z "$target_file" ] && target_file="$source_file" |
| 139 | |
| 140 | # |
| 141 | if [scip-shell system . . `[`. -n "$VERBOSE" ]; then |
| 142 | echoscip-shell system . . echo. "chosen_group=$chosen_group" |
| 143 | echoscip-shell system . . echo. "chosen_users=$chosen_users" |
| 144 | echoscip-shell system . . echo. "mode=$mode" |
| 145 | echoscip-shell system . . echo. "source_file=$source_file" |
| 146 | echoscip-shell system . . echo. "target_file=$target_file" |
| 147 | fi |
| 148 | |
| 149 | # main |
| 150 | if [scip-shell system . . `[`. "$chosen_group" = "all" ]; then |
| 151 | echoscip-shell system . . echo. "We will treat all users..." |
| 152 | # |
| 153 | IFS_org=$IFS |
| 154 | IFS=':' |
| 155 | grp_member="" |
| 156 | while read uname x uid gid x x x; do |
| 157 | # nfs user create a user called "nfsnobody", skip it. |
| 158 | if [scip-shell system . . `[`. "$uid" -ge "$UID_BEGIN_DEFAULT" -a "$uname" != "nfsnobody" -a \ |
| 159 | "$uname" != "nobody" ]; then |
| 160 | grp_member="$grp_member $uname" |
| 161 | fi |
| 162 | done </etc/passwdscip-shell filesystem . . `/etc/passwd`. |
| 163 | IFS=$IFS_org |
| 164 | echoscip-shell system . . echo. "grp_member=$grp_member" |
| 165 | elif [scip-shell system . . `[`. -n "$chosen_group" ]; then |
| 166 | if ! grepscip-shell system . . grep. "^$chosen_group:" /etc/groupscip-shell filesystem . . `/etc/group`. 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 167 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 168 | echoscip-shell system . . echo. "group $chosen_group does NOT exist!!!" |
| 169 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 170 | echoscip-shell system . . echo. "$msg_program_stop" |
| 171 | exit 1 |
| 172 | else |
| 173 | grp_member=$(grepscip-shell system . . grep. "^$chosen_group:" /etc/groupscip-shell filesystem . . `/etc/group`. | awkscip-shell system . . awk. -F':' '{print $4}' | sedscip-shell system . . sed. -e "s/,/ /g") |
| 174 | fi |
| 175 | elif [scip-shell system . . `[`. -n "$chosen_users" ]; then |
| 176 | grp_member="$chosen_users" |
| 177 | else |
| 178 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 179 | echoscip-shell system . . echo. "No group or users were assigned!" |
| 180 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 181 | echoscip-shell system . . echo. "$msg_program_stop" |
| 182 | exit 1 |
| 183 | fi |
| 184 | |
| 185 | if [scip-shell system . . `[`. "$mode" = "rm" ]; then |
| 186 | echoscip-shell system . . echo. "Warning! This will delete $source_file in user's (i.e. $grp_member) home directory" |
| 187 | echoscip-shell system . . echo. -n "[y/N] " |
| 188 | read rm_confirm |
| 189 | case "$rm_confirm" in |
| 190 | y|[yY][eE][sS]) |
| 191 | if [scip-shell system . . `[`. -n "$sub_dir" ]; then |
| 192 | echoscip-shell system . . echo. "Do you want to remove the directory \"$sub_dir\" in each user's home directory ?" |
| 193 | echoscip-shell system . . echo. -n "[y/N] " |
| 194 | read rm_dir_confirm |
| 195 | case "$rm_dir_confirm" in |
| 196 | y|[yY][eE][sS]) |
| 197 | rm_dir_confirm="yes" |
| 198 | ;; |
| 199 | *) |
| 200 | rm_dir_confirm="no" |
| 201 | ;; |
| 202 | esac |
| 203 | fi |
| 204 | ;; |
| 205 | esac |
| 206 | fi |
| 207 | #dest_file=${source_file##/*/} |
| 208 | #dest_file=${dest_file##*/} |
| 209 | |
| 210 | # store the LC_ALL |
| 211 | LC_ALL_org=$LC_ALL |
| 212 | export LC_ALL=C |
| 213 | |
| 214 | # main |
| 215 | for imember in $grp_member; do |
| 216 | #echo "$imember user_home_dir:$USER_HOME_DIR" |
| 217 | USER_HOME_DIR=`LC_ALL=C grepscip-shell system . . grep. "^$imember:" /etc/passwdscip-shell filesystem . . `/etc/passwd`. | awkscip-shell system . . awk. -F':' '{print $6}'` |
| 218 | uid=`LC_ALL=C grepscip-shell system . . grep. "^$imember:" /etc/passwdscip-shell filesystem . . `/etc/passwd`. | awkscip-shell system . . awk. -F':' '{print $3}'` |
| 219 | gid=`LC_ALL=C grepscip-shell system . . grep. "^$imember:" /etc/passwdscip-shell filesystem . . `/etc/passwd`. | awkscip-shell system . . awk. -F':' '{print $4}'` |
| 220 | case "$mode" in |
| 221 | "rm") |
| 222 | case "$rm_confirm" in |
| 223 | y|[yY][eE][sS]) |
| 224 | # just in case, we separate the rm -rf $sub_dir into two steps |
| 225 | rmscip-shell system . . rm. -rfv $USER_HOME_DIR/$sub_dir/$source_file |
| 226 | if [scip-shell system . . `[`. "$rm_dir_confirm" = "yes" ]; then |
| 227 | rmdirscip-shell system . . rmdir. -v $USER_HOME_DIR/$sub_dir/ |
| 228 | fi |
| 229 | ;; |
| 230 | *) |
| 231 | echoscip-shell system . . echo. "abort!" |
| 232 | exit |
| 233 | esac |
| 234 | ;; |
| 235 | "get") |
| 236 | users=`LC_ALL=C grepscip-shell system . . grep. "^.*:.*:$uid:" /etc/passwdscip-shell filesystem . . `/etc/passwd`. | awkscip-shell system . . awk. -F':' '{print $1}'` |
| 237 | for iuser in $users; do |
| 238 | # check if the uid is really the username |
| 239 | if [scip-shell system . . `[`. `LC_ALL=C idscip-shell system . . id. -u $iuser` = "$uid" ]; then |
| 240 | #echo "The username for uid $uid is $iuser" |
| 241 | usrname="$iuser" |
| 242 | fi |
| 243 | done |
| 244 | # 2 special cases, (1) with white space, (2) with * (or actually normal cases) |
| 245 | # For (1) we have to use "", for (2), no "" should be used. |
| 246 | # (1) |
| 247 | for ifile in $USER_HOME_DIR/"$sub_dir/$source_file"; do |
| 248 | [scip-shell system . . `[`. -e "$ifile" ] && cpscip-shell system . . cp. -rv "$ifile" ${usrname}_"$(basenamescip-shell system . . basename. "${ifile}")" |
| 249 | done |
| 250 | # (2) |
| 251 | for ifile in $USER_HOME_DIR/$sub_dir/$source_file; do |
| 252 | [scip-shell system . . `[`. -e "$ifile" ] && cpscip-shell system . . cp. -rv "$ifile" ${usrname}_"$(basenamescip-shell system . . basename. "${ifile}")" |
| 253 | done |
| 254 | ;; |
| 255 | "put") |
| 256 | [scip-shell system . . `[`. ! -e "$source_file" ] && echoscip-shell system . . echo. "$source_file does not exist!" && exit 1 |
| 257 | # Remember to use "" for the file name, since it might contain white space |
| 258 | if [scip-shell system . . `[`. -n "$sub_dir" ]; then mkdirscip-shell system . . mkdir. -p "$USER_HOME_DIR/$sub_dir"; fi |
| 259 | cpscip-shell system . . cp. -rv "$source_file" "$USER_HOME_DIR/$sub_dir/$target_file" |
| 260 | chownscip-shell system . . chown. -R ${uid}:${gid} "$USER_HOME_DIR/$sub_dir/$target_file" |
| 261 | ;; |
| 262 | *) |
| 263 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 264 | ;; |
| 265 | esac |
| 266 | done |
| 267 | |
| 268 | # restore the LC_ALL |
| 269 | export LC_ALL=$LC_ALL_org |