From 415bc4ca22a7d89b849b2de06c130555e4d7eb30 Mon Sep 17 00:00:00 2001
From: Cristian Rocha <ccr@supremus>
Date: Fri, 13 Nov 2015 11:51:14 -0200
Subject: [PATCH] added check user and main screen

---
 usb_drive/insert_data.sh | 107 +++++++++++++++++++++
 usb_drive/mount_usb.sh   |  76 +++++++++++++++
 usb_drive/setup_usb.sh   | 201 +++++++++++++++++++++++++++++++++++++++
 usb_drive/temp           |   0
 usb_drive/udev_caller.sh |   8 ++
 usb_drive/udev_umount.sh |  21 ++++
 6 files changed, 413 insertions(+)
 create mode 100755 usb_drive/insert_data.sh
 create mode 100755 usb_drive/mount_usb.sh
 create mode 100755 usb_drive/setup_usb.sh
 create mode 100644 usb_drive/temp
 create mode 100755 usb_drive/udev_caller.sh
 create mode 100755 usb_drive/udev_umount.sh

diff --git a/usb_drive/insert_data.sh b/usb_drive/insert_data.sh
new file mode 100755
index 0000000..31dfd03
--- /dev/null
+++ b/usb_drive/insert_data.sh
@@ -0,0 +1,107 @@
+#!/bin/bash
+DIR_USB=/media/usb_drive/
+DIR_OPENSLX=/Openslx/
+user_name=`users |  cut -f 1 -d ' '`
+
+function prepare_usb {
+  # get the usb_drive's path
+  devpath=`readlink -f $usb_drives`
+  # check if the usb_drive is mounted
+  mount_msg=`mount | grep $devpath`
+  # if it is mounted, show the path
+  if [[ -n $mount_msg ]]; then
+    echo $mount_msg
+    mount_path=`mount | grep $devpath | cut -f 3 -d ' '`
+    counter=4
+    part_path=`mount | grep $devpath | cut -f 4 -d ' '`
+    while [ "$part_path" != "type" ];
+	do
+		mount_path=$mount_path"\ "$part_path
+		counter=$(($counter+1))
+		part_path=`mount | grep $devpath | cut -f $counter -d ' '`
+	done
+	echo $mount_path
+    echo $mount_path$DIR_OPENSLX
+    # If it is not mounted, mount it
+    else
+      echo "$devpath não está montado";
+
+    if [ ! -d "$DIR_USB" ]; then
+        mkdir /media/usb_drive
+      echo "não existe"
+    fi
+    echo "Montando dispositivo..."
+    mount -o uid="$user_name" $devpath /media/usb_drive
+    mount_path=/media/usb_drive
+  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`
+
+    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
+
+      elif [[ $type == "SLX_USB_SHADOW" ]]
+        then
+
+        shadow=`eval echo $user_info | cut -f 2 -d '='`
+
+
+      elif [[ $type == "SLX_USB_GROUP" ]]
+        then
+        group=`eval echo $user_info | cut -f 2 -d '='`
+
+      fi
+
+      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
+        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
diff --git a/usb_drive/mount_usb.sh b/usb_drive/mount_usb.sh
new file mode 100755
index 0000000..4218e86
--- /dev/null
+++ b/usb_drive/mount_usb.sh
@@ -0,0 +1,76 @@
+#!/bin/bash
+
+
+DIR_USB=/media/usb_drive/
+DIR_OPENSLX=/Openslx/
+user_name=`users |  cut -f 1 -d ' '`
+
+function prepare_usb {
+  # get the usb_drive's path
+  devpath=`readlink -f $usb_drives`
+  # check if the usb_drive is mounted
+  mount_msg=`mount | grep $devpath`
+  # if it is mounted, show the path
+  if [[ -n $mount_msg ]]; then
+    echo $mount_msg
+    mount_path=`mount | grep $devpath | cut -f 3 -d ' '`
+    counter=4
+
+    part_path=`mount | grep $devpath | cut -f 4 -d ' '`
+    while [ "$part_path" != "type" ];
+	   do
+		mount_path=$mount_path"\ "$part_path
+		counter=$(($counter+1))
+		part_path=`mount | grep $devpath | cut -f $counter -d ' '`
+	   done
+
+    echo "opa"
+    echo $mount_path
+    # If it is not mounted, mount it
+  else
+      echo "$devpath não está montado";
+
+    if [ ! -d "$DIR_USB" ]; then
+        mkdir /media/usb_drive
+      echo "não existe"
+    fi
+    echo "Montando dispositivo..."
+    echo "chegaaaaau"
+    mount -o uid=$user_name $devpath /media/usb_drive
+    mount_path=/media/usb_drive
+  fi
+}
+
+
+# list all the usb_drives, mounted or not mounted
+for usb_drives in `ls -l /dev/disk/by-id/usb* | rev |  cut -f 3 -d ' ' | rev`; do
+
+ echo $usb_drives
+ #get only the main devices
+ if [[ $usb_drives != *"part"* ]]
+ then
+   prepare_usb
+    #mount home
+   user=` eval cat "$mount_path"/Openslx/config | head -n 1 | cut -f 2 -d '=' | cut -f 1 -d ':'`
+   if [[ $user != $user_name ]]
+   then
+     echo "diferente"
+     # exit 1
+   fi
+
+   path=`mount | grep $devpath | cut -f 1 -d ' '`
+   echo "caminho: "$path
+   umount $path
+   echo $mount_path
+   if [ ! -d "$DIR_USB" ]; then
+       mkdir /media/usb_drive
+     echo "não existe"
+   fi
+   mount -o uid=$user_name $path  /media/usb_drive
+   mount --bind  /media/usb_drive/Openslx/home  /home/$user_name
+
+   touch /tmp/config
+   echo "USB_DRIVE_MOUNTED:$path" > /tmp/config
+
+ fi
+ done
diff --git a/usb_drive/setup_usb.sh b/usb_drive/setup_usb.sh
new file mode 100755
index 0000000..4d75cd9
--- /dev/null
+++ b/usb_drive/setup_usb.sh
@@ -0,0 +1,201 @@
+#!/bin/bash
+DIR_USB=/media/usb_drive/
+DIR_OPENSLX=/Openslx/
+user_name=`users |  cut -f 1 -d ' '`
+
+function find_path {
+
+  # get the usb_drive's path
+  devpath=`readlink -f $usb_drives`
+  # check if the usb_drive is mounted
+  mount_msg=`mount | grep $devpath`
+  # if it is mounted, show the path
+  if [[ -n $mount_msg ]]; then
+    #echo $mount_msg
+    mount_path=`mount | grep $devpath | cut -f 3 -d ' '`
+    counter=4
+    part_path=`mount | grep $devpath | cut -f 4 -d ' '`
+    while [ "$part_path" != "type" ];
+      do
+        mount_path=$mount_path" "$part_path
+        counter=$(($counter+1))
+        part_path=`mount | grep $devpath | cut -f $counter -d ' '`
+      done
+  fi
+
+
+  name=`echo $mount_path | rev | cut -f 1 -d '/'| rev`
+  eval echo $mount_path >> /tmp/mount_openslx
+  eval echo $name >> /tmp/name_openslx
+}
+
+
+
+function prepare_usb {
+
+    find_path
+    echo $mount_path$DIR_OPENSLX
+    # If it is not mounted, mount it
+    if [[ ! -n $mount_msg ]]; then
+      echo "$devpath não está montado";
+
+      if [ ! -d "$DIR_USB" ]; then
+          mkdir /media/usb_drive
+        echo "não existe"
+      fi
+      echo "Montando dispositivo..."
+      mount -o uid="$user_name" $devpath /media/usb_drive
+      mount_path=/media/usb_drive
+    fi
+}
+
+
+function create_dir {
+  # crete the dir /Openslx/home
+  if [ ! -d "$mount_path$DIR_OPENSLX" ];  then
+       mkdir "$mount_path"/Openslx/
+       mkdir "$mount_path"/Openslx/home
+       mkdir "$mount_path"/Openslx/home/Destkop
+       mkdir "$mount_path"/Openslx/home/Documents
+       mkdir "$mount_path"/Openslx/home/Downloads
+       mkdir "$mount_path"/Openslx/home/Music
+       mkdir "$mount_path"/Openslx/home/Pictures
+       mkdir "$mount_path"/Openslx/home/Public
+       mkdir "$mount_path"/Openslx/home/Templates
+       mkdir "$mount_path"/Openslx/home/Videos
+       touch "$mount_path"/Openslx/config
+       touch "$mount_path"/Openslx/log
+  fi
+}
+
+
+function get_user {
+
+  #get the username and the password
+  new_user=$( dialog --stdout --inputbox 'Digite o nome de usuário:' 0 0 )
+  pass=$( dialog --stdout --passwordbox 'Por favor, digite a senha:' 0 0 )
+  check_pass=$( dialog --stdout --passwordbox 'Por favor, confirme a senha:' 0 0 )
+
+  #check the password
+  while [ "$pass" != "$check_pass" ];
+  do
+    dialog                                            \
+       --title 'Erro:'    \
+       --msgbox 'As senhas não conferem. Tente novamente.'  \
+       6 40
+       pass=$( dialog --stdout --passwordbox 'Por favor, digite a senha:' 0 0 )
+       check_pass=$( dialog --stdout --passwordbox 'Por favor, confirme a senha:' 0 0 )
+  done
+
+  #start the log with a default id
+  user_id=$((10000+$(((RANDOM%10000)+1))))
+  check_user_name=`  cat "$mount_path"/Openslx/log | grep -w "$new_user"`
+  check_user_id=`cat "$mount_path"/Openslx/config | grep "SLX_USB_PASSWD" | cut -f 3 -d ':' | grep -w "$user_id"`
+
+  #check if user_name already exists
+  while [[  -n $check_user_name ]];
+  do
+
+    new_user=$( dialog --stdout --inputbox 'Nome de usuário já existente. Digite outro nome de usuário:' 0 0 )
+    check_user_name=`cat "$mount_path"/Openslx/log | grep -w "$new_user"`
+  done
+
+  #check if user_id already exists
+  while [[  -n $check_user_id ]];
+  do
+
+      #get the last id
+      user_id=`cat "$mount_path"/Openslx/log | cut -f 1 -d ' ' | tail -n1`
+      #user_id will be last id plus one, if it does not exists already
+      user_id=$((10000+$(((RANDOM%10000)+1))))
+      check_user_id=`cat "$mount_path"/Openslx/config | grep "SLX_USB_PASSWD" | cut -f 3 -d ':' | grep -w "$user_id"`
+  done
+
+}
+
+function create_config {
+  #get days since Linux Epoch (January 1, 1970)
+  seconds=`date +%s`
+  minutes=$(($seconds/60))
+  hours=$(($minutes/60))
+  days=$(($hours/24))
+  #encrypt password
+  encry_pass=`echo "$pass" | openssl passwd -1 -stdin`
+  echo $encry_pass
+
+  #insert data in the log file
+  echo "$user_id $new_user active $date" >> "$mount_path"/Openslx/log
+
+
+  #insert data in the config file
+  echo "SLX_USB_PASSWD=$new_user:x:$user_id:$user_id::/home/$new_user:/bin/bash" >> "$mount_path"/Openslx/config
+  echo "SLX_USB_SHADOW=$new_user:$encry_pass:$days:0:99999:7:::" >> "$mount_path"/Openslx/config
+  echo "SLX_USB_GROUP=$new_user:x:$user_id;" >> "$mount_path"/Openslx/config
+
+
+  #show the confirmation message
+  dialog                                            \
+     --title 'Cadastro:'    \
+     --msgbox 'Cadastro realizado com sucesso.'  \
+     6 40
+}
+
+function show_menu {
+  let i=0 # define counting variable
+  W=() # define working array
+  while read -r line; do # process file by file
+      let i=$i+1
+      W+=($i "$line")
+  done < <( cat /tmp/name_openslx)
+  FILE=$(dialog --title "Selecione o dispositivo para configuração" --menu "OPENSLX" 24 80 17 "${W[@]}" 3>&2 2>&1 1>&3) # show dialog and store output
+  clear
+  if [ $? -eq 0 ]; then # Exit with OK
+    if [[ $FILE != "" ]]
+    then
+      name_selected=`eval cat /tmp/mount_openslx | head -n$FILE | tail -n1`
+      echo $name_selected
+    else
+      fim_setup
+    fi
+  fi
+
+
+}
+
+function fim_setup {
+  rm /tmp/mount_openslx
+  rm /tmp/name_openslx
+  exit
+}
+
+function usb_drive {
+  touch /tmp/mount_openslx
+  touch /tmp/name_openslx
+  # list all the usb_drives, mounted or not mounted
+  for usb_drives in `ls -l 2>/dev/null /dev/disk/by-id/usb* | rev |  cut -f 3 -d ' ' | rev`; do
+  # get only the main devices
+     if [[ $usb_drives != *"part"* ]]
+     then
+        prepare_usb
+     fi
+  done
+  show_menu
+  mount_path="$name_selected"
+  create_dir
+  get_user
+  create_config
+  fim_setup
+}
+
+
+id_user=`id -r -u`
+if [ $id_user -eq 0 ]; then
+    FILE=$(dialog --menu "Que tipo de cadastro você deseja fazer?" 10 50 2 1 "Cadastrar por Pen Drive" 2 "Cadastrar por HD"  3>&2 2>&1 1>&3)
+    if [ $FILE -eq 1 ]; then
+        usb_drive
+    else
+        echo "parte do HD"
+    fi
+else
+    usb_drive
+fi
diff --git a/usb_drive/temp b/usb_drive/temp
new file mode 100644
index 0000000..e69de29
diff --git a/usb_drive/udev_caller.sh b/usb_drive/udev_caller.sh
new file mode 100755
index 0000000..2bd18b9
--- /dev/null
+++ b/usb_drive/udev_caller.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+
+touch /home/ccr/jaaaaaaaaaaaaaa
+./home/ccr/Final/insert_data.sh & exit
+
+
+echo "testelegal"
diff --git a/usb_drive/udev_umount.sh b/usb_drive/udev_umount.sh
new file mode 100755
index 0000000..cb09c8d
--- /dev/null
+++ b/usb_drive/udev_umount.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+user_name=`users |  cut -f 1 -d ' '`
+
+
+check_usb_drive=`cat /tmp/config | cut -f 1 -d ":"`
+usb_path=`cat /tmp/config | cut -f 2 -d ":"`
+
+touch /home/ccr/cristianlegal
+
+touch /home/aab$check_usb_drive
+
+
+if [ "$check_usb_drive" = "USB_DRIVE_MOUNTED" ]
+then
+  touch /home/entreeeeeeeiuhul
+  umount $usb_path
+  umount -l /home/$user_name
+fi
+
+rm /tmp/config
-- 
GitLab