From c20a8165a831c00e2f67d1605079d114c0df3c3c Mon Sep 17 00:00:00 2001
From: Cristian Rocha <ccr@supremus>
Date: Mon, 23 Nov 2015 12:12:07 -0200
Subject: [PATCH] changes added

---
 usb_drive/insert_data.sh |   1 -
 usb_drive/setup_usb.sh   | 172 ++++++++++++++++++++++++++++++++-------
 2 files changed, 141 insertions(+), 32 deletions(-)

diff --git a/usb_drive/insert_data.sh b/usb_drive/insert_data.sh
index 31dfd03..a80f260 100755
--- a/usb_drive/insert_data.sh
+++ b/usb_drive/insert_data.sh
@@ -64,7 +64,6 @@ usb_drives=`ls -lt 2>/dev/null /dev/disk/by-id/usb* | rev |  cut -f 3 -d ' ' | r
 
         shadow=`eval echo $user_info | cut -f 2 -d '='`
 
-
       elif [[ $type == "SLX_USB_GROUP" ]]
         then
         group=`eval echo $user_info | cut -f 2 -d '='`
diff --git a/usb_drive/setup_usb.sh b/usb_drive/setup_usb.sh
index 4d75cd9..1c14dbb 100755
--- a/usb_drive/setup_usb.sh
+++ b/usb_drive/setup_usb.sh
@@ -23,7 +23,8 @@ function find_path {
       done
   fi
 
-
+#  temp_file=$(mktemp)
+#  temp_file_name=$(mktemp)
   name=`echo $mount_path | rev | cut -f 1 -d '/'| rev`
   eval echo $mount_path >> /tmp/mount_openslx
   eval echo $name >> /tmp/name_openslx
@@ -54,6 +55,7 @@ function create_dir {
   # crete the dir /Openslx/home
   if [ ! -d "$mount_path$DIR_OPENSLX" ];  then
        mkdir "$mount_path"/Openslx/
+       mkdir "$mount_path"/Openslx/.config/
        mkdir "$mount_path"/Openslx/home
        mkdir "$mount_path"/Openslx/home/Destkop
        mkdir "$mount_path"/Openslx/home/Documents
@@ -63,16 +65,35 @@ function create_dir {
        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/.config/config
        touch "$mount_path"/Openslx/log
   fi
 }
 
-
-function get_user {
+function read_user_pass {
 
   #get the username and the password
   new_user=$( dialog --stdout --inputbox 'Digite o nome de usuário:' 0 0 )
+  #username should be at linux standards
+  while [[ ! $new_user =~ ^[a-z][-a-z0-9]*$ ]]
+  do
+    new_user=$( dialog --stdout --inputbox 'O nome de usuário consiste apenas de letras, números, hífens(-). E o nome de usuário deve começar apenas com letras. Tente novamente :' 0 0 )
+  done
+  check_user_name=`  cat "$mount_path"/Openslx/log | grep -w "$new_user"`
+
+  #check if user_name already exists
+  while [[  -n $check_user_name ]];
+  do
+    #get the username and the password
+    new_user=$( dialog --stdout --inputbox 'Usuário já existente. Tente novamente:' 0 0 )
+    #username should be at linux standards
+    while [[ ! $new_user =~ ^[a-z][-a-z0-9]*$ ]]
+    do
+      new_user=$( dialog --stdout --inputbox 'O nome de usuário consiste apenas de letras, números, hífens(-). E o nome de usuário deve começar apenas com letras. Tente novamente :' 0 0 )
+    done
+    check_user_name=`cat "$mount_path"/Openslx/log | grep -w "$new_user"`
+
+  done
   pass=$( dialog --stdout --passwordbox 'Por favor, digite a senha:' 0 0 )
   check_pass=$( dialog --stdout --passwordbox 'Por favor, confirme a senha:' 0 0 )
 
@@ -87,33 +108,56 @@ function get_user {
        check_pass=$( dialog --stdout --passwordbox 'Por favor, confirme a senha:' 0 0 )
   done
 
+
+}
+
+function get_user {
+
+  #get the username and the password
+  read_user_pass
+
   #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"`
+  user_id=$((10000+$(((RANDOM%10000)))))
 
-  #check if user_name already exists
-  while [[  -n $check_user_name ]];
-  do
+  check_user_id=`cat "$mount_path"/Openslx/.config/config | grep "SLX_USB_PASSWD" | cut -f 3 -d ':' | grep -w "$user_id"`
 
-    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
 
+  #get the last id
+  last_id=`cat "$mount_path"/Openslx/log | cut -f 1 -d ' ' | tail -n1`
   #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"`
+      last_id=$(($last_id+1))
+      #check if the last_id already exists in the config file
+      check_user_id=`cat "$mount_path"/Openslx/.config/config | grep "SLX_USB_PASSWD" | cut -f 3 -d ':' | grep -w "$last_id"`
+      #if is the last_id in the USB DRIVE range, go back to the start of the range
+      if [[ ($last_id -ge 19999) && (-n $check_user_id) ]]; then
+        last_id=9999
+      #otherwise, set user_id
+      else
+        user_id=$last_id
+      fi
   done
 
+  #create dir of the user
+  if [ ! -d "$mount_path"/Openslx/$new_user ];  then
+       mkdir "$mount_path"/Openslx/$new_user
+  fi
+
+
 }
 
-function create_config {
+function success_message {
+
+  #show the confirmation message
+  dialog                                            \
+     --title 'Cadastro:'    \
+     --msgbox 'Cadastro realizado com sucesso.'  \
+     6 40
+
+}
+function calculate_days_and_pass {
   #get days since Linux Epoch (January 1, 1970)
   seconds=`date +%s`
   minutes=$(($seconds/60))
@@ -121,23 +165,39 @@ function create_config {
   days=$(($hours/24))
   #encrypt password
   encry_pass=`echo "$pass" | openssl passwd -1 -stdin`
-  echo $encry_pass
+  #echo $encry_pass
+}
+
+function create_config {
+  #get days since Linux Epoch (January 1, 1970)
+  calculate_days_and_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
+  echo "SLX_USB_PASSWD=$new_user:x:$user_id:$user_id::/home/$new_user:/bin/bash" >> "$mount_path"/Openslx/.config/config
+  echo "SLX_USB_SHADOW=$new_user:$encry_pass:$days:0:99999:7:::" >> "$mount_path"/Openslx/.config/config
+  echo "SLX_USB_GROUP=$new_user:x:$user_id;" >> "$mount_path"/Openslx/.config/config
 
+  success_message
+}
 
-  #show the confirmation message
-  dialog                                            \
-     --title 'Cadastro:'    \
-     --msgbox 'Cadastro realizado com sucesso.'  \
-     6 40
+function create_config_hd {
+
+  calculate_days_and_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_PASSWD=$new_user:x:$user_id:$user_id::/home/$new_user:/bin/bash" >> "$mount_path"/openslx/config
+  echo "SLX_SHADOW=$new_user:$encry_pass:$days:0:99999:7:::" >> "$mount_path"/openslx/config
+  echo "SLX_GROUP=$new_user:x:$user_id;" >> "$mount_path"/openslx/config
+
+  success_message
 }
 
 function show_menu {
@@ -187,14 +247,64 @@ function usb_drive {
   fim_setup
 }
 
+function hd_drive {
+    # crete the dir /home/openslx and config files
+
+    if [ ! -d "/home/openslx" ];  then
+        mkdir -p /home/openslx/
+    fi
+    if [ ! -e "/home/openslx/config" ]; then
+        touch /home/openslx/config
+    fi
+    if [ ! -e "/home/openslx/log" ]; then
+        touch /home/openslx/log
+    fi
+
+    read_user_pass
+
+
+    user_id=cat /etc/passwd | cut -d: -f3 | grep -e 2....$ | sort -h | tail -n 1
+    if [ -z "$user_id" ]; then
+        user_id=20000
+    else
+        user_id=$(( $user_id+1 ))
+        if [ user_id -ge 30000 ]; then
+            echo $0": Provavelmente acabou os id's valido, procurando sequencialmente" >> /home/openslx/log
+            #Da para tentar fazer algum código usando diff e {20000..29999}
+            #Ai nao precisa desse loop
+            for i in $(echo {20000..29999}); do
+                user_id=cat /etc/passwd | cut -d: -f3 | grep -e $i$
+                if [ -z "$user_id" ]; then
+                    user_id=$i
+                    break;
+                fi
+            done
+            if [ -z "$user_id" ]; then
+                echo $0": Acabou os id's validos" >> /home/openslx/log
+                dialog                                            \
+                   --title 'Erro:'    \
+                   --msgbox 'Estorou o limite de ususario locais nessa maquina, impossivel criar mais'  \
+                   6 40
+                exit 255
+            fi
+        fi
+    fi
+
+    mount_path="/home"
+    create_config_hd
+
+}
+
+
+
 
 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
+    if [ $FILE -eq 1 ] &> /dev/null;  then
         usb_drive
-    else
-        echo "parte do HD"
+    elif [ $FILE -eq 2 ] &> /dev/null; then
+        hd_drive
     fi
 else
     usb_drive
-- 
GitLab