#!/bin/bashUTRANS_TBL=""GTRANS_TBL=""BINDIR=""WD=""CONFDIR=""reown() { find $1 -uid $2 -exec chown $3 '{}' -h \;}regroup() { find $1 -gid $2 -exec chgrp $3 '{}' -h \;}exprt() { HN=`hostname` TF=`tempfile` if [ ! -d "$CONFDIR" ]; then mkdir "$CONFDIR" fi cat /etc/passwd | grep -E "^[A-Za-z0-9:/_ ]+$" | awk -F ':' '{print $1":"$3;}' > $TF cp $TF "$CONFDIR/users.$HN" cat /etc/group | grep -E "^[A-Za-z0-9:/,_]+$" | awk -F ':' '{print $1":"$3;}' > $TF cp $TF "$CONFDIR/groups.$HN" rm $TF}convert() { for i in $UTRANS_TBL; do echo "$i" IFS=':' read scratch UFROM UTO < <(echo "$i") #UFROM=`echo $i | awk -F : '{print $2;}'` #UTO=`echo $i | awk -F : '{print $3;}'` reown $1 $UFROM $UTO done for i in $GTRANS_TBL; do IFS=':' read scratch GFROM GTO < <(echo "$i") #GFROM=`echo $i | awk -F : '{print $2;}'` #GTO=`echo $i | awk -F : '{print $3;}'` regroup $1 $GFROM $GTO done}create_table() { LN=`cat "$CONFDIR/users.$1" | wc -l` while IFS=':' read U_NAME U_ID; do if find_user $U_NAME; then UTRANS_TBL=`echo -e "$UTRANS_TBL\n$U_NAME:$U_ID:$U_ID2"` fi done < "$CONFDIR/users.$1" LN=`cat "$CONFDIR/groups.$1" | wc -l` while IFS=':' read G_NAME G_ID; do if find_group $G_NAME; then GTRANS_TBL=`echo -e "$GTRANS_TBL\n$G_NAME:$G_ID:$G_ID2"` fi done < "$CONFDIR/groups.$1"}find_user() { U_ID2=`grep -E "^$1:" /etc/passwd | awk -F ":" '{print $3;}'` if [ -n "$U_ID2" ]; then true else warn "No equivalent found for user '$1' on the local system." false fi}find_group() { G_ID2=`grep -E "^$1:" /etc/group | awk -F ":" '{print $3;}'` if [ -n "$G_ID2" ]; then true else warn "No equivalent found for group '$1' on the local system." false fi}warn() { echo -e "WARNING: $1" >&2}usage() { echo -e "Usage:\t$0 [ --export ] | [ --import <SYSTEM_NAME> <DIRECTORIES> ]" echo -e "\t--export: exports the usernames and groups from the current system" echo -e "\t--import SYSTEM_NAME DIRECTORIES: tries to match the users and group" echo -e "\t\tinformation from the system SYSTEM_NAME to the current system," echo -e "\t\tand changes the user and group ownership for the files and" echo -e "\t\tsubdirectories found in DIRECTORIES.\n"}WD=`pwd`BINDIR=`dirname $0`BINDIR=`cd $BINDIR && pwd && cd $WD`CONFDIR="$BINDIR/conf"if [ $# -gt 0 ]; then case "$1" in --import) if [ $# -ge 3 ]; then if [ ! -f "$CONFDIR/users.$2" -a -f "$CONFDIR/groups.$2" ]; then warn "The user and/or group database has not been exported properly for $2." exit 4 fi for i in `seq 3 $#`; do if [ ! -d "${!i}" ]; then warn "'${!i}' is not a directory." exit 8 fi done create_table $2 for i in `seq 3 $#`; do convert ${!i} done else warn "The SYSTEM_NAME and/or the DIRECTORY parameter is missing." usage exit 2 fi true ;; --export) exprt true ;; *) false ;; esac if [ $? -ne 0 ]; then warn "Incorrect option was specified.\n" usage exit 1 fielse usagefi