From 3f87ee8653f7a51d59777a98466808fcf816d8e8 Mon Sep 17 00:00:00 2001
From: Thiago Abdo <tja14@inf.ufpr.br>
Date: Thu, 14 Jan 2016 10:08:49 -0200
Subject: [PATCH] Add code to add a user to passwd group and shadow

Signed-off-by: Thiago Abdo <tja14@inf.ufpr.br>
---
 hdd/adduser.sh | 71 +++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 62 insertions(+), 9 deletions(-)

diff --git a/hdd/adduser.sh b/hdd/adduser.sh
index 9f2015d..b90ab34 100755
--- a/hdd/adduser.sh
+++ b/hdd/adduser.sh
@@ -29,17 +29,68 @@
 
 
 add (){
-    #checa se nao exite usuario com mesmo id
-    #checa se nao existe usuario com mesmo nome
-
-    #checa se nao existe grupo com mesmo id
-    #checa se nao existe grupo com mesmo nome
-
-    #adiciona no passwd
+    local passwd=$1
+    local group=$2
+    local shadow=$3
+
+    local username=$( echo $passwd | cut -d':' -f1 )
+    local uID=$( echo $passwd | cut -d':' -f3 )
+
+    local groupname=$( echo $group | cut -d':' -f1 )
+    local gID=$( echo $group | cut -d':' -f3 )
+
+    #check if this uid is available
+    local checkID=$( cat /etc/passwd | cut -d':' -f1-3 | grep -e ":$uID$" )
+    if [ -n "$checkID" ]; then
+        echo $0": Impossivel cadastrar usuario, já existe usuário com o mesmo id"
+        exit 4;
+    fi
+
+    #check if this username is available
+    local checkName=$( cat /etc/passwd | cut -d':' -f1-3 | grep -e "^$username:" )
+    if [ -n "$checkName" ]; then
+        echo $0": Impossível cadastrar usuário, nome de usaurio já está em uso"
+        exit 9
+    fi
+
+    #checks if exist the same gruop id
+    local checkGID=$( cat /etc/group | grep -e ":$gID:" )
+    if [ -n "$checkGID" ]; then
+        local checkGName=$( cat /etc/group | grep -e "^$groupname:" )
+        if [ -n "$checkGName" ]; then
+            if [ "$checkGname" != "$checkGID" ]; then
+                echo $0": Impossível cadastrar usuário, nome e id de grupo já em uso"
+                exit 51
+            fi
+        else
+            echo $0": Impossivel cadastrar usuário, id de grupo já em uso"
+        fi
+    fi
+
+    #checks if exist the same groupname
+    local checkGName=$( cat /etc/group | grep -e "^$groupname:" )
+    if [ -n "$checkGName" ]; then
+        #checks if the group and gid isnt the same, if is the same, there is no problems
+        if [ "$checkGName" != "$checkGID" ]; then
+            echo $0": Impossível cadastrar usuário, nome de grupo já está em uso"
+            exit 51
+        fi
+    fi
+
+   #adds on files
+    echo $passwd >> /etc/passwd
+    echo $group >> /etc/group
+    echo $shadow >> /etc/shadow
 
     #cria home
-    #copia home-exemplo
-    #chown na home inteira
+    local $homedir=$( echo $passwd | cut -d':' -f6 )
+    if [ ! -e $homedir ]; then
+        mkdir -p "$homedir"
+        #copy skel home
+        cp -a /etc/skel/. "$homedir"
+        #chown to the user
+        chown -R $username:$groupname "$homedir"
+    fi
 }
 
 
@@ -78,6 +129,7 @@ fi
 #if globalFlag && globalID && globalGID
 if [ "$globalFlag" = "global" ] && [ -n "$globalID" ] && [ -n "$globalGID" ]; then
     #add user global
+    add "$passwd" "$group" "$shadow"
     echo $0": Usuário global adicionado corretamente"
     exit 0
 fi
@@ -127,6 +179,7 @@ if [ -z "$checkGName" ]; then
 fi
 
 #add local user
+add "$passwd" "$group" "$shadow"
 echo $0": Usuario local adicionado corretamente"
 exit 0
 
-- 
GitLab