123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- #!/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)>\t"
- read Comando
- #printf "\n"
- #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:,pasword:,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
- ;;
- -s|--shell)
- shell=$2
- shift
- ;;
- -f|--force)
- force=true
- ;;
- -r|--remove)
- remove=true
- ;;
- -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"
- 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"
- useradd $opciones $User
- if [ $? -ne 0 ]; then
- printf "ERROR FATAL: No se ha podido crear el usuario (codigo salida: $?).\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
- #Faltan AQUI las Opciones
- 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
- userdel$opciones $User
- return 0
- }
- function cambiarPassword {
- #Cambiamos la contraseña del usuario ($User)
- if [ -z $User ]; then
- printf "(USUARIO)> "
- read User
- fi
- existeUsuario
- if [ $? -ne 0 ]; then
- printf "ERROR: el usuario $User no existe.\n"
- return 1
- fi
- passwd $User
- if [ $? -ne 0 ]; then
- printf "ERROR FATAL: No es posible cambiar la contraseña (codigo salida: $?).\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 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"
- ;;
- 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
- ;;
- desbloquear)
- #Desbloquear usuario
- ;;
- 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
- done
- unset VERSION cont
- exit
|