Files
Varios/cesion_root.sh
2025-06-16 20:40:42 +00:00

84 lines
1.4 KiB
Bash

#!/bin/bash
# Variables
max_cesion=180
user=$1
horas=$2
horas_default=12
# Chequeo de requisitos para su ejecución
if [ $(id -u) -ne 0 ]
then echo "ERROR. Se necesitan permisos de root"
exit 1
fi
systemctl is-active -q atd
if [ $? -ne 0 ]
then
echo "ERROR: el servicio ATD no está en ejecución"
exit 1
fi
if ! [ -x /sbin/groupmems ]
then
echo "ERROR: No se puede ejecutar /sbin/groupmems"
exit 1
fi
if [ $# -ne 1 ] && [ $# -ne 2 ];
then
echo "ERROR: número de parámetros incorrecto"
echo
echo "Uso: $0 USUARIO [HORAS]"
echo
echo "USUARIO: login de usuario a ceder permisos sudo"
echo "Horas: número de horas a mantener el permiso, por defecto 12h"
echo
echo
exit 1
fi
getent passwd $user > /dev/null
if [ $? -ne 0 ]
then
echo "ERROR: el usuario $user no existe"
exit 1
fi
if [ $# -eq 2 ]
then
if ! [[ $horas =~ ^[0-9]+$ ]] || [ $horas -lt 1 ] || [ $horas -gt $max_cesion ]
then
echo "ERROR: el número de horas debe estar entre 1 y $max_cesion"
echo
echo "Uso: $0 $user [HORAS]"
echo
echo
exit 1
fi
else
horas=$horas_default
fi
# main script
error_output=$(groupmems -g wheel -a $user 2>&1)
if [ $? -ne 0 ]
then
echo "ERROR: $error_output"
else
output=$(at 2>&1 now + $horas hours -M <<END | tail -1 | grep -oP '(?<=at).*'
groupmems -g wheel -d $user 2>&1 >/dev/null
END
)
echo "CESION SUDO OK"
echo "Usuario: $user"
echo "Fecha expiración: $output"
echo
fi