From 282bca8c61fc90de812c7600759069ea8c8f308a Mon Sep 17 00:00:00 2001
From: Luiz Reis <luizreis@outlook.com>
Date: Fri, 26 Feb 2016 11:36:46 -0300
Subject: [PATCH] Improve comments for insert_data and similar code on
 local_accounts

Signed-off-by: Luiz Reis <luizreis@outlook.com>
---
 usb_drive/insert_data.sh    | 48 ++++++++++++++++++++++++++-----------
 usb_drive/local_accounts.sh | 34 ++++++++++++++++++--------
 2 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/usb_drive/insert_data.sh b/usb_drive/insert_data.sh
index 6cd4c16..734225a 100755
--- a/usb_drive/insert_data.sh
+++ b/usb_drive/insert_data.sh
@@ -1,39 +1,51 @@
 #!/bin/ash
 
+# EXIT CODES
+# 0 SUCCESS
+# 1 DEVICE COULD NOT BE MOUNTED
+# 2 NOT A CONFIGURED OPENSLX DEVICE
+# 3 CONFIG FILE IS EMPTY
+
 # 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 dev to be mounted
 # return is done via the mountpath global variable, which is also used here
 prepare_usb () {
-    # get the usb_drive's path
     local dev=$1
     mountpath=""
 
+    # get mounted device path. if not mounted attempts to do it
     if $(mount | grep -q "/dev/$dev"); then
         echo "$0: Device /dev/$dev is already mounted"
+        # get mountpath using the Usage '%' char as delimiter
+        # can't use space because of multiple spaces
         mountpath=$(df | grep -w $dev | cut -d'%' -f2- | cut -d' ' -f2-)
+        # continues execution of users_pendrive
     else
         echo "$0: Device /dev/$dev is not mounted"
-        mkdir -p /media/openslx/$dev
+        mkdir -p /media/openslx/$dev # creates folder to mount dev on
         if $(mount "/dev/$dev" /media/openslx/$dev); then
             echo "$0: Device /dev/$dev mounted successfully"
             mountpath="/media/openslx/$dev"
+            # continues execution of users_pendrive
         else
             echo "$0: Device /dev/$dev could not be mounted"
-            exit 1
+            exit 1 # stops openslx users_pendrive execution for this dev
         fi
     fi
 
+    # checks if usb has an openslx config
     if [ -e "$mountpath/Openslx/config" ]; then
         echo "$0: /dev/$dev is an openslx ready drive"
     else
         echo "$0: /dev/$dev is not ready to be used as an openslx drive"
+        # umount dev if it was mounted by openslx and doesnt have a config
         if [ "$mountpath" = "/media/openslx/$dev" ]; then
             echo "$0: umounting /dev/$dev"
-            umount -f -l "/dev/$dev"
+            umount -f -l "/dev/$dev" # lazy umount to avoid problems
         fi
-        exit 2
+        exit 2 # stops openslx users_pendrive execution for this dev
     fi
 }
 
@@ -41,18 +53,25 @@ sleep 3 # let the system mount the drive first
 
 dev="$1"
 
-prepare_usb "$dev" #returns $mountpath
+prepare_usb "$dev" #returns at $mountpath
+# will only continue if the device that triggered is a valid openslx device
 echo "$0: an openslx ready drive is mounted at: $mountpath"
 
 unset IFS
 users=$(cat "$mountpath/Openslx/config");
 if [ -z "$users" ]; then
-    echo "$0: empty config file"
-    exit 3
+    echo "$0: $dev has an empty config file"
+    exit 3 # stops openslx users_pendrive execution for this dev
 fi
 set $users
+
+# parse config file until EOF
 while (( [ $# -gt 0 ] )); do
-    #Get infos from file
+    # get the respective line for passwd, shadow and group
+    # shift advances the line $1 refers to
+    # since it doesn't check if there are 3 lines to parse,
+    # the program may misbehave in an insecure way
+    # if config file is corrupted or invalid.
     passwdf=$(echo $1 | cut -d'=' -f2)
     shift
     shadowf=$(echo $1 | cut -d'=' -f2)
@@ -60,6 +79,7 @@ while (( [ $# -gt 0 ] )); do
     groupf=$(echo $1 | cut -d'=' -f2)
     shift
 
+    # parse each line (stored on variables by the code above) for needed info
     username=$( echo $passwdf | cut -d':' -f1 )
     uID=$( echo $passwdf | cut -d':' -f3 )
     localID=$( echo $uID | grep -e "^2....$" -e "^1....$" )
@@ -68,13 +88,13 @@ while (( [ $# -gt 0 ] )); do
     gID=$( echo $groupf | cut -d':' -f3 )
     localGID=$( echo $gID | grep -e "^2....$" -e "^1....$" )
 
-   #group == username
+    # local users have to belong to a group with the same name
     if [ "$username" = "$groupname" ]; then
-        #id no range certo(local user)
+        # checks if the user's id is in the correct range
+        # if the user is not local localID and localGID will be empty
+        # because grep will not find anything
         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"
+            /opt/openslx/scripts/adduser.sh "$passwdf" "$groupf" "$shadowf"
         fi
     fi
 done
diff --git a/usb_drive/local_accounts.sh b/usb_drive/local_accounts.sh
index 2c28423..3c3864c 100755
--- a/usb_drive/local_accounts.sh
+++ b/usb_drive/local_accounts.sh
@@ -23,8 +23,14 @@ done
 
 unset IFS
 set $SLX_USERS
+
+# parse config variable
 while (( [ $# -gt 0 ] )); do
-    #Get infos from file
+    # get the respective line for passwd, shadow and group
+    # shift advances the line $1 refers to
+    # since it doesn't check if there are 3 lines to parse,
+    # the program may misbehave in an insecure way
+    # if config file is corrupted or invalid.
     passwdf=$(echo $1 | cut -d'=' -f2)
     shift
     shadowf=$(echo $1 | cut -d'=' -f2)
@@ -32,6 +38,7 @@ while (( [ $# -gt 0 ] )); do
     groupf=$(echo $1 | cut -d'=' -f2)
     shift
 
+    # parse each line (stored on variables by the code above) for needed info
     username=$( echo $passwdf | cut -d':' -f1 )
     uID=$( echo $passwdf | cut -d':' -f3 )
     globalID=$( echo $uID | grep -e "^3....$" )
@@ -40,12 +47,12 @@ while (( [ $# -gt 0 ] )); do
     gID=$( echo $groupf | cut -d':' -f3 )
     globalGID=$( echo $gID | grep -e "^3....$" )
 
-    #group == username
+    # global users have to belong to a group with the same name
     if [ "$username" = "$groupname" ]; then
-        #id no range certo(local user)
+        # checks if the user's id is in the correct range
+        # if the user is not global globalID and globalGID will be empty
+        # because grep will not find anything
         if [ -n "$globalID" ] && [ -n "$globalGID" ]; then
-            #add to files;
-            #echo "aqui"
             /opt/openslx/scripts/adduser.sh "$passwdf" "$groupf" "$shadowf" "global"
         fi
     fi
@@ -76,7 +83,7 @@ for line in $SLX_GROUPS; do
     unset IFS
 done
 
-#Instanciates the configuration file
+# instanciates the configuration file
 if [ ! -e /home/openslx/localaccounts/config ]; then
     echo $0": Configuration of local users doesn't exist, creating a blank one"
     > /home/openslx/localaccounts/config
@@ -85,8 +92,13 @@ unset IFS
 users=$(cat /home/openslx/localaccounts/config);
 set $users
 
+# parse config file until EOF
 while (( [ $# -gt 0 ] )); do
-    #Get infos from file
+    # get the respective line for passwd, shadow and group
+    # shift advances the line $1 refers to
+    # since it doesn't check if there are 3 lines to parse,
+    # the program may misbehave in an insecure way
+    # if config file is corrupted or invalid.
     passwdf=$(echo $1 | cut -d'=' -f2)
     shift
     shadowf=$(echo $1 | cut -d'=' -f2)
@@ -94,6 +106,7 @@ while (( [ $# -gt 0 ] )); do
     groupf=$(echo $1 | cut -d'=' -f2)
     shift
 
+    # parse each line (stored on variables by the code above) for needed info
     username=$( echo $passwdf | cut -d':' -f1 )
     uID=$( echo $passwdf | cut -d':' -f3 )
     localID=$( echo $uID | grep -e "^2....$" -e "^1....$" )
@@ -102,11 +115,12 @@ while (( [ $# -gt 0 ] )); do
     gID=$( echo $groupf | cut -d':' -f3 )
     localGID=$( echo $gID | grep -e "^2....$" -e "^1....$" )
 
-    #group == username
+    # local users have to belong to a group with the same name
     if [ "$username" = "$groupname" ]; then
-        #id no range certo(local user)
+        # checks if the user's id is in the correct range
+        # if the user is not local localID and localGID will be empty
+        # because grep will not find anything
         if [ -n "$localID" ] && [ -n "$localGID" ]; then
-            #add to files;
             /opt/openslx/scripts/adduser.sh "$passwdf" "$groupf" "$shadowf"
         fi
     fi
-- 
GitLab