diff --git a/usb_drive/insert_data.sh b/usb_drive/insert_data.sh index 84398da5ab4bbec921dcb042d0dafc9b25723127..6cd4c16d8cbe890271ac7b0333e1029e04733b41 100755 --- a/usb_drive/insert_data.sh +++ b/usb_drive/insert_data.sh @@ -1,109 +1,80 @@ -#!/bin/bash -DIR_USB=/media/usb_drive/ -DIR_OPENSLX=/Openslx/ -user_name=`users | cut -f 1 -d ' '` +#!/bin/ash # prepares openslx partition to be used # mounts partition if not mounted # blocks program execution if it is not a openslx partition -# param $1 is the devpath to be mounted -# param $2 is the uuid of the partition +# param $1 is the dev to be mounted # return is done via the mountpath global variable, which is also used here -function prepare_usb { +prepare_usb () { # get the usb_drive's path - local devpath="/dev/"$1 + local dev=$1 mountpath="" - if [ ! 0 -eq $(mount | grep -q $devpath) ]; then - echo $0": Device $devpath is already mounted" - mountpath=$(mount | grep -w $devpath | cut -d' ' -f3) + + if $(mount | grep -q "/dev/$dev"); then + echo "$0: Device /dev/$dev is already mounted" + mountpath=$(df | grep -w $dev | cut -d'%' -f2- | cut -d' ' -f2-) else - echo $0": Device $devpath is not mounted" - mkdir -p /media/openslx/$2 - if $(mount $devpath /media/openslx/$2); then - echo $0": Device $devpath mounted successfully" - mountpath="/media/openslx/"$2 + echo "$0: Device /dev/$dev is not mounted" + mkdir -p /media/openslx/$dev + if $(mount "/dev/$dev" /media/openslx/$dev); then + echo "$0: Device /dev/$dev mounted successfully" + mountpath="/media/openslx/$dev" else - echo $0": Device $devpath could not be mounted" + echo "$0: Device /dev/$dev could not be mounted" exit 1 fi fi + if [ -e "$mountpath/Openslx/config" ]; then - echo $0": $devpath is an openslx ready drive" + echo "$0: /dev/$dev is an openslx ready drive" else - echo $0": $devpath is not ready to be used as an openslx drive" - if [ $mountpath = "/media/openslx/$2" ]; then - echo $0": umounting $devpath" - umount $devpath + echo "$0: /dev/$dev is not ready to be used as an openslx drive" + if [ "$mountpath" = "/media/openslx/$dev" ]; then + echo "$0: umounting /dev/$dev" + umount -f -l "/dev/$dev" fi exit 2 fi } -sleep 3 -#TIRAR FOR -# list all the usb_drives, mounted or not mounted -usb_drives=`ls -lt 2>/dev/null /dev/disk/by-id/usb* | rev | cut -f 3 -d ' ' | rev | head -1` +sleep 3 # let the system mount the drive first - prepare_usb - echo $mount_path - counter=0 - for user_info in `eval cat "$mount_path"/Openslx/config`; do - type=`eval echo $user_info | cut -f 1 -d '='` - counter=$(($counter+1)) - if [[ $type == "SLX_USB_PASSWD" ]] - then - passwd=`eval echo $user_info | cut -f 2 -d '='` - user=` eval echo $user_info | cut -f 2 -d '=' | cut -f 1 -d ':'` - gshadow=`echo $user:!::` - echo "user"$user - check_user=`cat /etc/shadow | grep -w $user` - passwd=`eval echo $user_info | cut -f 2 -d '='` - if [[ ( ! -n $check_user) ]]; then - echo "não existe user" - fi +dev="$1" - elif [[ $type == "SLX_USB_SHADOW" ]] - then +prepare_usb "$dev" #returns $mountpath +echo "$0: an openslx ready drive is mounted at: $mountpath" - shadow=`eval echo $user_info | cut -f 2 -d '='` +unset IFS +users=$(cat "$mountpath/Openslx/config"); +if [ -z "$users" ]; then + echo "$0: empty config file" + exit 3 +fi +set $users +while (( [ $# -gt 0 ] )); do + #Get infos from file + passwdf=$(echo $1 | cut -d'=' -f2) + shift + shadowf=$(echo $1 | cut -d'=' -f2) + shift + groupf=$(echo $1 | cut -d'=' -f2) + shift - elif [[ $type == "SLX_USB_GROUP" ]] - then - group=`eval echo $user_info | cut -f 2 -d '='` + username=$( echo $passwdf | cut -d':' -f1 ) + uID=$( echo $passwdf | cut -d':' -f3 ) + localID=$( echo $uID | grep -e "^2....$" -e "^1....$" ) - fi + groupname=$( echo $groupf | cut -d':' -f1 ) + gID=$( echo $groupf | cut -d':' -f3 ) + localGID=$( echo $gID | grep -e "^2....$" -e "^1....$" ) - mod=$(( $counter % 3)) - if [[ $mod == 0 ]]; - then - #insert data in the files - if [[ (! -n $check_user)&&(-n $passwd)&&(-n $shadow)&&(-n $group)&&(-n $gshadow) ]]; then - echo "Insert $counter" - echo $passwd >> /etc/passwd - echo $shadow >> /etc/shadow - echo $group >> /etc/group - #echo $gshadow >> /etc/gshadow - if [ ! -d "/home/"$user ]; then - mkdir /home/$user - chown $user:$user /home/$user - fi + #group == username + if [ "$username" = "$groupname" ]; then + #id no range certo(local user) + if [ -n "$localID" ] && [ -n "$localGID" ]; then + #add to files; + #/opt/openslx/scripts/adduser.sh "$passwdf" "$groupf" "$shadowf" + /home/luiz/repos/users-openslx/hdd/adduser.sh "$passwdf" "$groupf" "$shadowf" fi - passwd="" - shadow="" - group="" - gshadow="" - user="" - fi - done - - - -echo $shadow -echo $group - -#user=` eval cat "$mount_path"/Openslx/config | head -n 1 | cut -f 2 -d '=' | cut -f 1 -d ':'` -#gshadow=`echo $user:!::` -#check_user=`cat /etc/shadow | grep -w $user` -#echo $shadow -#echo $group -#echo $check_user + fi +done