|
@@ -0,0 +1,517 @@
|
|
|
+#!/bin/bash
|
|
|
+#Guzmán Castanedo (guzman@castanedo.es) Octubre 2017
|
|
|
+#Licencia: GPL3 (http://www.gnu.org/licenses/gpl-3.0.html)
|
|
|
+
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
+function mostrarBienvenida {
|
|
|
+#Version y ayuda.
|
|
|
+ printf "$(basename $0) $VERSION\n"
|
|
|
+ printf "Escribir \"ayuda\" o \"help\" para obtener ayuda de los comandos\n\n"
|
|
|
+}
|
|
|
+
|
|
|
+function leerComando {
|
|
|
+#Muestrar linea de comando y capturar comando.
|
|
|
+ force=false
|
|
|
+ remove=false
|
|
|
+ printf "$(basename $0)> "
|
|
|
+ read Comando
|
|
|
+ #Parseamos la linea de comando con todas las opciones posibles.
|
|
|
+ #printf "$Comando\n"
|
|
|
+ Temp=$(getopt -q -o d:g:G:p:s:fre: --long home-dir:,gid:,groups:,password:,shell:,force,remove,expiredate: -- $Comando)
|
|
|
+ eval set -- "$Temp"
|
|
|
+ #printf "$Temp\n"
|
|
|
+ while true; do
|
|
|
+ case "$1" in
|
|
|
+ -d|--home-dir)
|
|
|
+ homeDir=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ -g|--gid)
|
|
|
+ gid=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ -G|--groups)
|
|
|
+ otherGroups=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ -p|--password)
|
|
|
+ password=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ -s|--shell)
|
|
|
+ shell=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ -f|--force)
|
|
|
+ force=true
|
|
|
+ shift
|
|
|
+ ;;
|
|
|
+ -r|--remove)
|
|
|
+ remove=true
|
|
|
+ shift
|
|
|
+ ;;
|
|
|
+ -e|--expiredate)
|
|
|
+ expireDate=$2
|
|
|
+ shift 2
|
|
|
+ ;;
|
|
|
+ --)
|
|
|
+ shift
|
|
|
+ break
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ printf "Error de Sintaxis: Comando no valido.\n"
|
|
|
+ return 1
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ done
|
|
|
+ Opcion=$1
|
|
|
+ User=$2
|
|
|
+ #printf "Opcion: $Opcion\tUsuario: $User\n"
|
|
|
+ unset Comando Temp
|
|
|
+}
|
|
|
+
|
|
|
+function altaUsuario {
|
|
|
+#Crea el usuario si no existe.
|
|
|
+ #Comprobaciones
|
|
|
+ if [ -z $User ]; then
|
|
|
+ #Usuario no definido
|
|
|
+ printf "(USUARIO)> "
|
|
|
+ read User
|
|
|
+ fi
|
|
|
+ existeUsuario
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ #Ya existe
|
|
|
+ printf "ERROR: El usuario no esta disponible (ya existe).\n"
|
|
|
+ printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\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.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ fi
|
|
|
+ opciones=$opciones" -g $gid"
|
|
|
+ if [ ! -z $otherGroups ]; then
|
|
|
+ opciones=$opciones" -G $otherGroups"
|
|
|
+ fi
|
|
|
+ if [ -z $password ]; then
|
|
|
+ leerPassword
|
|
|
+ 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"
|
|
|
+ useradd $opciones $User
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR FATAL: No se ha podido crear el usuario.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ #Cambiamos usuario
|
|
|
+ printf "$password\n$password\n" | passwd $User >/dev/null 2>&1
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR: al establecer contraseña (usuario creado).\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function bajaUsuario {
|
|
|
+#Eliminar usuario si existe
|
|
|
+ if [ -z $User ]; then
|
|
|
+ listarUsuarios
|
|
|
+ printf "(USUARIO)> "
|
|
|
+ read User
|
|
|
+ fi
|
|
|
+ existeUsuario
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR: no existe el usuario $User.\n"
|
|
|
+ printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ opciones=""
|
|
|
+ if [ $force = true ]; then
|
|
|
+ opciones=$opciones" -f"
|
|
|
+ fi
|
|
|
+ if [ $remove = true ]; then
|
|
|
+ opciones=$opciones" -r"
|
|
|
+ fi
|
|
|
+ printf "(QUIERES ELIMINAR A $User? [Y/N])> "
|
|
|
+ read correcto
|
|
|
+ case $correcto in
|
|
|
+ y|Y|s|S)
|
|
|
+ unset correcto
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ printf "Cancelado por el Usuario.\n"
|
|
|
+ return 1
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ #printf "userdel$opciones $User\n"
|
|
|
+ userdel$opciones $User
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function cambiarPassword {
|
|
|
+#Cambiamos la contraseña del usuario ($User)
|
|
|
+ if [ -z $User ]; then
|
|
|
+ listarUsuarios
|
|
|
+ printf "(USUARIO)> "
|
|
|
+ read User
|
|
|
+ fi
|
|
|
+ existeUsuario
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR: el usuario $User no existe.\n"
|
|
|
+ printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ if [ -z $password ]; then
|
|
|
+ leerPassword
|
|
|
+ fi
|
|
|
+ printf "$password\n$password\n" | passwd $User >/dev/null 2>&1
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR FATAL: No es posible cambiar la contraseña.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function listarUsuarios {
|
|
|
+#Listamos los usuarios que hay en el sistema.
|
|
|
+ printf "Usuarios disponibles:\n"
|
|
|
+ for lista in $(cat /etc/passwd | cut -d: -f1); do
|
|
|
+ printf "$lista\t"
|
|
|
+ done
|
|
|
+ printf "\n"
|
|
|
+ unset lista
|
|
|
+}
|
|
|
+
|
|
|
+function bloquearUsuario {
|
|
|
+#Bloquea el usuario $User
|
|
|
+ if [ -z $User ]; then
|
|
|
+ listarUsuarios
|
|
|
+ printf "(USUARIO)> "
|
|
|
+ read User
|
|
|
+ fi
|
|
|
+ existeUsuario
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR: el usuario $User no existe.\n"
|
|
|
+ printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ estaBloqueado
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "$User ya esta bloqueado (%s).\n" $(fechaBloqueado)
|
|
|
+ printf "Escribir \"desbloquear\" para desbloquear usuario.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ if [ -z $expireDate ]; then
|
|
|
+ printf "(FECHA BLOQUEO [yyyy-mm-dd])> "
|
|
|
+ read expireDate
|
|
|
+ case $expireDate in
|
|
|
+ ""|now|NOW)
|
|
|
+ expireDate=1
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ fi
|
|
|
+ usermod -e $expireDate $User
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR FATAL: No se ha podido bloquear usuario $User.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function desbloquearUsuario {
|
|
|
+#Desbloquear el usuario $User
|
|
|
+ if [ -z $User ]; then
|
|
|
+ listarUsuarios
|
|
|
+ printf "(USUARIO)> "
|
|
|
+ read User
|
|
|
+ fi
|
|
|
+ existeUsuario
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR: el usuario $User no existe.\n"
|
|
|
+ printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ estaBloqueado
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "$User no esta bloqueado.\n"
|
|
|
+ printf "Escribir \"bloquear\" para desbloquear usuario.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ printf "Fecha de Bloqueo de $User: %s\n" $(fechaBloqueado)
|
|
|
+ printf "(DESBLOQUEAR? [S/N])> "
|
|
|
+ read correcto
|
|
|
+ case $correcto in
|
|
|
+ y|Y|s|S)
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ printf "Cancelado por el Usuario.\n"
|
|
|
+ return 1
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ usermod -e "" $User
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
+ printf "ERROR FATAL: No se ha podido desbloquear usuario $User.\n"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function estaBloqueado {
|
|
|
+#Comprueba si el usuario $User esta bloqueado (Usando Dias UNIX)
|
|
|
+ ahora=$(expr $(date --utc -d now +%s) / 86400)
|
|
|
+ locktime=$(grep ^$User: /etc/shadow | cut -d: -f8)
|
|
|
+ #printf "Ahora: $ahora\nLock: $locktime\n"
|
|
|
+ if [ -z $locktime ]; then
|
|
|
+ #No esta bloqueado
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ if [ $locktime -le $ahora ]; then
|
|
|
+ #Esta bloqueado
|
|
|
+ return 0
|
|
|
+ else
|
|
|
+ #No lo esta
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+function fechaBloqueado {
|
|
|
+#Muestra la fecha de bloqueo en formato humano yyyy-mm-dd
|
|
|
+ locktime=$(expr $(grep ^$User: /etc/shadow | cut -d: -f8) '*' 86400)
|
|
|
+ fecha=$(date -d @$locktime +%F)
|
|
|
+ printf "$fecha\n"
|
|
|
+}
|
|
|
+
|
|
|
+function leerPassword {
|
|
|
+#Lee contraseña y la guarda en $password
|
|
|
+ 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
|
|
|
+}
|
|
|
+
|
|
|
+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"
|
|
|
+ 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-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"
|
|
|
+ printf "\t-p, --pasword\n"
|
|
|
+ printf "\t\tContraseña del usuario.\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 "> usuarios\tListar USUARIOS en el sistema\n"
|
|
|
+ printf "> ayuda\t\tMuestra esta ayuda\n"
|
|
|
+ printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
|
|
|
+ printf "> salir\t\tFinaliza programa\n"
|
|
|
+ ;;
|
|
|
+ usuarios)
|
|
|
+ printf "usuarios: muestra los usuarios disponibles\n"
|
|
|
+ printf "SINOPSIS: usuarios\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"
|
|
|
+comprobarPrivilegios
|
|
|
+mostrarBienvenida
|
|
|
+cont=true
|
|
|
+while [ $cont = true ]; do
|
|
|
+ leerComando
|
|
|
+ case $Opcion in
|
|
|
+ alta)
|
|
|
+ #Alta usuario
|
|
|
+ altaUsuario
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "USUARIO $User CREADO CON EXITO.\n"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ cambiar)
|
|
|
+ #Cambiar passwd
|
|
|
+ cambiarPassword
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "PASSWORD DEL USUARIO $User CAMBIADO CON EXITO.\n"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ baja)
|
|
|
+ #Baja usuario
|
|
|
+ bajaUsuario
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "USUARIO $User ELIMINADO CON EXITO.\n"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ bloquear)
|
|
|
+ #Bloquear usuario
|
|
|
+ bloquearUsuario
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "USUARIO $User BLOQUEADO CON EXITO.\n"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ desbloquear)
|
|
|
+ #Desbloquear usuario
|
|
|
+ desbloquearUsuario
|
|
|
+ if [ $? -eq 0 ]; then
|
|
|
+ printf "USUARIO $User DESBLOQUEADO CON EXITO.\n"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ usuarios)
|
|
|
+ #Lista todos los usuarios existentes
|
|
|
+ listarUsuarios
|
|
|
+ ;;
|
|
|
+ ayuda|help)
|
|
|
+ #Ayuda de comandos
|
|
|
+ ayuda $User
|
|
|
+ ;;
|
|
|
+ salir|s|S|quit|q|Q|exit)
|
|
|
+ #Salida
|
|
|
+ #Podreamos hacer break, pero los bucles infinitos son feos.
|
|
|
+ cont=false
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ #Opcion Incorrecta
|
|
|
+ printf "Error de Sintaxis: Comando no valido.\n"
|
|
|
+ ayuda
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones correcto
|
|
|
+done
|
|
|
+unset VERSION cont
|
|
|
+exit
|