| 1 | #!/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="`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 | usage() { |
| 30 | echo "NAME Put(default)/get/remove files to/from user's home directory." |
| 31 | echo |
| 32 | echo "DESCRIPTION" |
| 33 | 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 | echo "Usage: `basename $0` [-g|--group] group [-d|--directory] directory [-v|--verbose] source target" |
| 35 | echo "-g, --group GROUP Assign the user group to be processed as GROUP." |
| 36 | echo "-u, --user USERS Assign the user(s) to be processed as USERS." |
| 37 | echo "-d, --directory DIR Process the sub-directory DIR in user's home" |
| 38 | echo "-v, --verbose Prints out verbose information" |
| 39 | echo "If GROUP or USERS is not assigned, all the users will be processed." |
| 40 | echo |
| 41 | echo "Example:" |
| 42 | echo "drbl-cp-user -d work source_files new_file.txt" |
| 43 | echo "will copy source_files into user's ~/work/ directory as file name new_file.txt" |
| 44 | echo |
| 45 | echo "drbl-cp-user -g g3c5 source_files new_file.txt" |
| 46 | echo "will copy source_files into ~/work/ directory for the users belong to group g3c5 as file name new_file.txt" |
| 47 | echo |
| 48 | echo "drbl-cp-user -u \"john jack mary\" source_files new_file.txt" |
| 49 | echo "will copy source_files into ~/work/ directory for the users john, jack and mary as file name new_file.txt" |
| 50 | echo |
| 51 | echo "drbl-rm-user -d work target.txt" |
| 52 | echo "will remove target.txt from user's ~/work directory" |
| 53 | echo |
| 54 | echo "drbl-get-user -d work target.txt" |
| 55 | echo "will get target.txt from user's ~/work directory" |
| 56 | echo |
| 57 | echo "drbl-get-user -d work \"*.txt\"" |
| 58 | echo "will get *.txt from user's ~/work directory" |
| 59 | echo "NOTE!!! You must put \" \" before and after the star wildcard * filename!" |
| 60 | } |
| 61 | |
| 62 | # |
| 63 | if [ "$run_cmd" = "drbl-fuu" ]; then |
| 64 | 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 [ $# -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 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 83 | 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 | [ -d "$source_file" ] && source_file=$source_file"/" |
| 100 | |
| 101 | # check parameter |
| 102 | if [ $# -le 0 ]; then |
| 103 | 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 | echo "Unknown mode! Did you change the filename ?" |
| 118 | echo "Program terminated!" |
| 119 | exit 1 |
| 120 | ;; |
| 121 | esac |
| 122 | |
| 123 | # set the default values if empty |
| 124 | if [ -z "$chosen_group" -a -z "$chosen_users" ]; then |
| 125 | chosen_group="all" |
| 126 | fi |
| 127 | if [ -n "$chosen_group" -a -n "$chosen_users" ]; then |
| 128 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 129 | echo "Group and user(s) can not be assigned together!" |
| 130 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 131 | echo "Please only assign one of them." |
| 132 | echo "$msg_program_stop" |
| 133 | exit 1 |
| 134 | fi |
| 135 | |
| 136 | [ -z "$mode" ] && mode="put" |
| 137 | [ -z "$source_file" ] && echo "No source! Program terminated!" && exit 1 |
| 138 | [ -z "$target_file" ] && target_file="$source_file" |
| 139 | |
| 140 | # |
| 141 | if [ -n "$VERBOSE" ]; then |
| 142 | echo "chosen_group=$chosen_group" |
| 143 | echo "chosen_users=$chosen_users" |
| 144 | echo "mode=$mode" |
| 145 | echo "source_file=$source_file" |
| 146 | echo "target_file=$target_file" |
| 147 | fi |
| 148 | |
| 149 | # main |
| 150 | if [ "$chosen_group" = "all" ]; then |
| 151 | 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 [ "$uid" -ge "$UID_BEGIN_DEFAULT" -a "$uname" != "nfsnobody" -a \ |
| 159 | "$uname" != "nobody" ]; then |
| 160 | grp_member="$grp_member $uname" |
| 161 | fi |
| 162 | done </etc/passwd |
| 163 | IFS=$IFS_org |
| 164 | echo "grp_member=$grp_member" |
| 165 | elif [ -n "$chosen_group" ]; then |
| 166 | if ! grep "^$chosen_group:" /etc/group 2>/dev/null; then |
| 167 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 168 | echo "group $chosen_group does NOT exist!!!" |
| 169 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 170 | echo "$msg_program_stop" |
| 171 | exit 1 |
| 172 | else |
| 173 | grp_member=$(grep "^$chosen_group:" /etc/group | awk -F':' '{print $4}' | sed -e "s/,/ /g") |
| 174 | fi |
| 175 | elif [ -n "$chosen_users" ]; then |
| 176 | grp_member="$chosen_users" |
| 177 | else |
| 178 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 179 | echo "No group or users were assigned!" |
| 180 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 181 | echo "$msg_program_stop" |
| 182 | exit 1 |
| 183 | fi |
| 184 | |
| 185 | if [ "$mode" = "rm" ]; then |
| 186 | echo "Warning! This will delete $source_file in user's (i.e. $grp_member) home directory" |
| 187 | echo -n "[y/N] " |
| 188 | read rm_confirm |
| 189 | case "$rm_confirm" in |
| 190 | y|[yY][eE][sS]) |
| 191 | if [ -n "$sub_dir" ]; then |
| 192 | echo "Do you want to remove the directory \"$sub_dir\" in each user's home directory ?" |
| 193 | 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 grep "^$imember:" /etc/passwd | awk -F':' '{print $6}'` |
| 218 | uid=`LC_ALL=C grep "^$imember:" /etc/passwd | awk -F':' '{print $3}'` |
| 219 | gid=`LC_ALL=C grep "^$imember:" /etc/passwd | 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 | rm -rfv $USER_HOME_DIR/$sub_dir/$source_file |
| 226 | if [ "$rm_dir_confirm" = "yes" ]; then |
| 227 | rmdir -v $USER_HOME_DIR/$sub_dir/ |
| 228 | fi |
| 229 | ;; |
| 230 | *) |
| 231 | echo "abort!" |
| 232 | exit |
| 233 | esac |
| 234 | ;; |
| 235 | "get") |
| 236 | users=`LC_ALL=C grep "^.*:.*:$uid:" /etc/passwd | awk -F':' '{print $1}'` |
| 237 | for iuser in $users; do |
| 238 | # check if the uid is really the username |
| 239 | if [ `LC_ALL=C 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 | [ -e "$ifile" ] && cp -rv "$ifile" ${usrname}_"$(basename "${ifile}")" |
| 249 | done |
| 250 | # (2) |
| 251 | for ifile in $USER_HOME_DIR/$sub_dir/$source_file; do |
| 252 | [ -e "$ifile" ] && cp -rv "$ifile" ${usrname}_"$(basename "${ifile}")" |
| 253 | done |
| 254 | ;; |
| 255 | "put") |
| 256 | [ ! -e "$source_file" ] && echo "$source_file does not exist!" && exit 1 |
| 257 | # Remember to use "" for the file name, since it might contain white space |
| 258 | if [ -n "$sub_dir" ]; then mkdir -p "$USER_HOME_DIR/$sub_dir"; fi |
| 259 | cp -rv "$source_file" "$USER_HOME_DIR/$sub_dir/$target_file" |
| 260 | chown -R ${uid}:${gid} "$USER_HOME_DIR/$sub_dir/$target_file" |
| 261 | ;; |
| 262 | *) |
| 263 | usage |
| 264 | ;; |
| 265 | esac |
| 266 | done |
| 267 | |
| 268 | # restore the LC_ALL |
| 269 | export LC_ALL=$LC_ALL_org |