Explorar el Código

Correccion en el parser y funcion altaUsuarios

Guzmán Castanedo Villalba hace 6 años
padre
commit
d0242ffe4a
Se han modificado 1 ficheros con 186 adiciones y 52 borrados
  1. 186 52
      userspanel.sh

+ 186 - 52
userspanel.sh

@@ -4,17 +4,17 @@
 
 function comprobarPrivilegios {
 #Comprobar si somos root.
-if [ $(id -u) -ne 0 ]; then
-	 #No somos root
-	 printf "\tERROR: No es posible obtener permisos de root.\n"
-	 exit 1
-fi
+	if [ $(id -u) -ne 0 ]; then
+		 #No somos root
+		 printf "\tERROR: No es posible obtener permisos de root.\n"
+		 exit 1
+	fi
 }
 
 function mostrarBienvenida {
 #Version y ayuda.
-printf "$(basename $0) $VERSION\n"
-printf "Escribir \"ayuda\" o \"help\" para obtener ayuda de los comandos\n\n"
+	printf "$(basename $0) $VERSION\n"
+	printf "Escribir \"ayuda\" o \"help\" para obtener ayuda de los comandos\n\n"
 }
 
 function leerComando {
@@ -24,7 +24,7 @@ function leerComando {
 	#printf "\n"
 	#Parseamos la linea de comando con todas las opciones posibles.
 	printf "$Comando\n"
-	Temp=$(getopt -q -o d:g:G:mp:s:fre: -l --home-dir:,--gid:,--groups:,--create-home,--pasword:,--shell:,--force,--remove,--expiredate: -- $Comando)
+	Temp=$(getopt -q -o d:g:G:p:s:fre: --long home-dir:,gid:,groups:,pasword:,shell:,force,remove,expiredate: -- $Comando)
 	eval set -- "$Temp"
 	printf "$Temp\n"
 	while true; do
@@ -41,10 +41,6 @@ function leerComando {
 				otherGroups=$2
 				shift 2
 				;;
-			-m|--create-home)
-				createHome=true
-				shift
-				;;
 			-p|--password)
 				password=$2
 				shift
@@ -68,8 +64,8 @@ function leerComando {
 				break
 				;;
 			*)
-				printf "ERROR: Error Interno (parser).\n"
-				exit 1
+				printf "Error de Sintaxis: Comando no valido.\n"
+				return 1
 				;;
 		esac
 	done
@@ -79,46 +75,182 @@ function leerComando {
 	unset Comando Temp
 }
 
-#function altaUsuario {	
-#
-#}
+function altaUsuario {	
+#Crea el usuario si no existe.
+	#Comprobaciones
+	if [ -z $User ]; then
+		#Usuario no definido
+		printf "(USER)> "
+		read User
+	fi
+	existeUsuario
+	if [ $? -eq 0 ]; then
+		#Ya existe
+		printf "ERROR: El usuario no esta disponible (ya existe).\n"
+		return 1
+	fi
+	if [ -z $homeDir ]; then
+		printf "(HOME_DIR)> "
+		read homeDir
+	fi
+	opciones="-m -d $homeDir"
+	if [ -z $gid ]; then
+		printf "(GROUP)> "
+		read gid
+	fi
+	existeGrupo $gid
+	if [ $? -ne 0 ]; then
+		#No existe grupo. Vamos a crearlo
+		crearGrupo $gid
+		if [ $? -ne 0 ]; then
+			printf "ERROR FATAL: No se puede crear grupo (codigo salida: $?).\n"
+			return 1
+		fi
+	fi
+	opciones=$opciones" -g $gid"
+	if [ ! -z $otherGroups ]; then
+		opciones=$opciones" -G $otherGroups"
+	fi
+	if [ -z $password ]; then
+		igual=false
+		while [ $igual = false ]; do
+			printf "(PASSWORD)> "
+			read -s password
+			printf "\n(Confirme PASSWORD)> "
+			read -s password2
+			printf "\n"
+			if [ $password = $password2 ]; then
+				igual=true
+			else
+				printf "ERROR: No coinciden. Vuelva a intentarlo.\n"
+			fi
+		done
+		unset igual password2
+	fi
+	opciones=$opciones" -p $password"
+	if [ -z $shell ]; then
+		printf "Shells disponibles:\n"
+		for linea in $(grep -v "#" /etc/shells); do
+			printf "\t$linea\n"
+		done
+		printf "\t/usr/sbin/nologin\n\t/bin/false\n"
+		printf "(SHELL)> "
+		read shell
+		unset linea
+	fi
+	if [ ! -x $shell ]; then
+		printf "ERROR: El shell $shell no esta disponible.\n"
+		return 1
+	fi
+	opciones=$opciones" -s $shell"
+	#Todo correcto. Ejecutamos useradd
+	printf "Resumen:\n"
+	printf "\tUSUARIO:\t$User\n"
+	printf "\tHOMEDIR:\t$(realpath $homeDir)\n"
+	printf "\tGROUP:\t\t$gid\n"
+	printf "\tGRUPOS ADIC.:\t$otherGroups\n"
+	printf "\tSHELL:\t\t$shell\n"
+	printf "(Es correcto? [S/N])> "
+	read correcto
+	case $correcto in
+		y|Y|s|S)
+			 #Correcto
+			 unset correcto
+			;;
+		*)
+			printf "Cancelado por el Usuario.\n"
+			return 1
+			;;
+	esac
+	printf "useradd $opciones $User\n"
+}
+
+function existeUsuario {
+	#Comprobamos si un usuario ($User) existe
+	cat /etc/passwd | cut -d: -f1 | grep $User > /dev/null 2>&1
+	return $?
+}
+
+function existeGrupo {
+	#Comprobamos si el grupo ($1) existe
+	cat /etc/group | cut -d: -f1 | grep $1 > /dev/null 2>&1
+	return $?
+}
+
+function crearGrupo {
+	#Crea el grupo $1
+	groupadd $1 > /dev/null 2>&1
+	return $?
+}
 
 function ayuda {
 #Muestra la ayuda dependiendo del caso en el que estemos.
-printf "ayuda $1\n"
-case "$1" in
-	alta)
-		printf "alta: Dar de ALTA un nuevo usuario\n"
-		;;
-	cambiar)
-		printf "cambiar: CAMBIAR contraseña de un usuario:\n"
-		;;
-	baja)
-		printf "baja: Dar de BAJA un usuario\n"
-		;;
-	bloquear)
-		printf "bloquear: BLOQUEAR un usuario\n"
-		;;
-	desbloquear)
-		printf "desbloquear: DESBLOQUEAR un usuario\n"
-		;;
-	""|ayuda|help)
-		printf "Comandos:\n"
-		printf "> alta\t\tDar de ALTA un nuevo usuario\n"
-		printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
-		printf "> baja\t\tDar de BAJA un usuario\n"
-		printf "> bloquear\tBLOQUEAR un usuario\n"
-		printf "> desbloquear\tDESBLOQUEAR un usuario\n"
-		printf "> ayuda\t\tMuestra esta ayuda\n"
-		printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
-		printf "> salir\t\tFinaliza programa\n"
-		;;
-	*)
-		printf "Error: el comando $1 no existe.\n"
-		#printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
-		ayuda
-		;;
-esac
+	printf "ayuda $1\n"
+	case "$1" in
+		alta)
+			printf "alta: Dar de ALTA un nuevo usuario\n"
+			printf "SINOPSIS: alta [OPCIONES] [user]\n"
+			printf "OPCIONES:\n"
+			printf "\t-d, --home-dir HOME_DIR\n"
+			printf "\t\tDirectorio personal del usuario (HOME_DIR).\n"
+			printf "\t-g, --gid GROUP\n"
+			printf "\t\tNombre del grupo principal para el usuario.\n"
+			printf "\t-G, --groups GROUP1[,GROUP2[,...[,GROUPN]]]\n"
+			printf "\t\tGrupos adicionales para el usuario.\n"
+			printf "\t-m, --create-home\n"
+			printf "\t\tCrea el directorio personal si no existe.\n"
+			printf "\t-p, --pasword\n"
+			printf "\t\tContraseña del usuario.\n"
+			printf "\t-s, --shell SHELL\n"
+			printf "\t\tShell del usuario.\n"
+			;;
+		cambiar)
+			printf "cambiar: CAMBIAR contraseña de un usuario:\n"
+			printf "SINOPSIS: cambiar [user]\n"
+			;;
+		baja)
+			printf "baja: Dar de BAJA un usuario\n"
+			printf "SINOPSIS: baja [OPCIONES] [user]\n"
+			printf "OPCIONES:\n"
+			printf "\t-f, --force\n"
+			printf "\t\tElimina incluso si está logeado y elimina el directorio\n"
+			printf "\t\tpersonal del usuario y su mail.\n"
+			printf "\t-r, --remove\n"
+			printf "\t\tElimina el directorio personal del usuario y su mail.\n"
+			;;
+		bloquear)
+			printf "bloquear: BLOQUEAR un usuario\n"
+			printf "SINOPSIS: bloquear [OPCIONES] [usuario]\n"
+			printf "OPCIONES:\n"
+			printf "\t-e, --expiredate EXPIRE_DATE\n"
+			printf "\t\tFecha el formato YYYY-MM-DD en el que el usuario se bloqueara.\n"
+			printf "\t\tPor defecto: 1970-01-02 (siempre se bloquea).\n"
+			;;
+		desbloquear)
+			printf "desbloquear: DESBLOQUEAR un usuario\n"
+			printf "SINOPSIS: desbloquear [usuario]\n"
+			;;
+		""|ayuda|help)
+			printf "COMANDOS:\n"
+			printf "> alta\t\tDar de ALTA un nuevo usuario\n"
+			printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
+			printf "> baja\t\tDar de BAJA un usuario\n"
+			printf "> bloquear\tBLOQUEAR un usuario\n"
+			printf "> desbloquear\tDESBLOQUEAR un usuario\n"
+			printf "> ayuda\t\tMuestra esta ayuda\n"
+			printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
+			printf "> salir\t\tFinaliza programa\n"
+			;;
+		salir|s|S|quit|q|Q|exit)
+			printf "salir: finaliza el programa\n"
+			printf "SINOPSIS: salir, s, S, quit, q, Q, exit\n"
+			;;
+		*)
+			printf "Error: el comando $1 no existe.\n"
+			#printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
+			ayuda
+			;;
+	esac
 }
 
 VERSION="0.1 Beta"
@@ -130,6 +262,7 @@ while [ $cont = true ]; do
 	case $Opcion in
 		alta)
 			#Alta usuario
+			altaUsuario
 			;;
 		cambiar)
 			#Cambiar passwd
@@ -155,9 +288,10 @@ while [ $cont = true ]; do
 		*)
 			#Opcion Incorrecta
 			printf "Error de Sintaxis: Comando no valido.\n"
+			ayuda
 			;;
 	esac
-	unset Opcion User homeDir gid otherGroups createHome password shell force remove expireDate
+	unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones
 done
 unset VERSION cont
 exit