userspanel.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/bash
  2. #Guzmán Castanedo (guzman@castanedo.es) Octubre 2017
  3. #Licencia: GPL3 (http://www.gnu.org/licenses/gpl-3.0.html)
  4. function comprobarPrivilegios {
  5. #Comprobar si somos root.
  6. if [ $(id -u) -ne 0 ]; then
  7. #No somos root
  8. printf "\tERROR: No es posible obtener permisos de root.\n"
  9. exit 1
  10. fi
  11. }
  12. function mostrarBienvenida {
  13. #Version y ayuda.
  14. printf "$(basename $0) $VERSION\n"
  15. printf "Escribir \"ayuda\" o \"help\" para obtener ayuda de los comandos\n\n"
  16. }
  17. function leerComando {
  18. #Muestrar linea de comando y capturar comando.
  19. printf "$(basename $0)>\t"
  20. read Comando
  21. #printf "\n"
  22. #Parseamos la linea de comando con todas las opciones posibles.
  23. printf "$Comando\n"
  24. Temp=$(getopt -q -o d:g:G:p:s:fre: --long home-dir:,gid:,groups:,pasword:,shell:,force,remove,expiredate: -- $Comando)
  25. eval set -- "$Temp"
  26. printf "$Temp\n"
  27. while true; do
  28. case "$1" in
  29. -d|--home-dir)
  30. homeDir=$2
  31. shift 2
  32. ;;
  33. -g|--gid)
  34. gid=$2
  35. shift 2
  36. ;;
  37. -G|--groups)
  38. otherGroups=$2
  39. shift 2
  40. ;;
  41. -p|--password)
  42. password=$2
  43. shift
  44. ;;
  45. -s|--shell)
  46. shell=$2
  47. shift
  48. ;;
  49. -f|--force)
  50. force=true
  51. ;;
  52. -r|--remove)
  53. remove=true
  54. ;;
  55. -e|--expiredate)
  56. expireDate=$2
  57. shift 2
  58. ;;
  59. --)
  60. shift
  61. break
  62. ;;
  63. *)
  64. printf "Error de Sintaxis: Comando no valido.\n"
  65. return 1
  66. ;;
  67. esac
  68. done
  69. Opcion=$1
  70. User=$2
  71. printf "Opcion: $Opcion\tUsuario: $User\n"
  72. unset Comando Temp
  73. }
  74. function altaUsuario {
  75. #Crea el usuario si no existe.
  76. #Comprobaciones
  77. if [ -z $User ]; then
  78. #Usuario no definido
  79. printf "(USER)> "
  80. read User
  81. fi
  82. existeUsuario
  83. if [ $? -eq 0 ]; then
  84. #Ya existe
  85. printf "ERROR: El usuario no esta disponible (ya existe).\n"
  86. return 1
  87. fi
  88. if [ -z $homeDir ]; then
  89. printf "(HOME_DIR)> "
  90. read homeDir
  91. fi
  92. opciones="-m -d $homeDir"
  93. if [ -z $gid ]; then
  94. printf "(GROUP)> "
  95. read gid
  96. fi
  97. existeGrupo $gid
  98. if [ $? -ne 0 ]; then
  99. #No existe grupo. Vamos a crearlo
  100. crearGrupo $gid
  101. if [ $? -ne 0 ]; then
  102. printf "ERROR FATAL: No se puede crear grupo (codigo salida: $?).\n"
  103. return 1
  104. fi
  105. fi
  106. opciones=$opciones" -g $gid"
  107. if [ ! -z $otherGroups ]; then
  108. opciones=$opciones" -G $otherGroups"
  109. fi
  110. if [ -z $password ]; then
  111. igual=false
  112. while [ $igual = false ]; do
  113. printf "(PASSWORD)> "
  114. read -s password
  115. printf "\n(Confirme PASSWORD)> "
  116. read -s password2
  117. printf "\n"
  118. if [ $password = $password2 ]; then
  119. igual=true
  120. else
  121. printf "ERROR: No coinciden. Vuelva a intentarlo.\n"
  122. fi
  123. done
  124. unset igual password2
  125. fi
  126. opciones=$opciones" -p $password"
  127. if [ -z $shell ]; then
  128. printf "Shells disponibles:\n"
  129. for linea in $(grep -v "#" /etc/shells); do
  130. printf "\t$linea\n"
  131. done
  132. printf "\t/usr/sbin/nologin\n\t/bin/false\n"
  133. printf "(SHELL)> "
  134. read shell
  135. unset linea
  136. fi
  137. if [ ! -x $shell ]; then
  138. printf "ERROR: El shell $shell no esta disponible.\n"
  139. return 1
  140. fi
  141. opciones=$opciones" -s $shell"
  142. #Todo correcto. Ejecutamos useradd
  143. printf "Resumen:\n"
  144. printf "\tUSUARIO:\t$User\n"
  145. printf "\tHOMEDIR:\t$(realpath $homeDir)\n"
  146. printf "\tGROUP:\t\t$gid\n"
  147. printf "\tGRUPOS ADIC.:\t$otherGroups\n"
  148. printf "\tSHELL:\t\t$shell\n"
  149. printf "(Es correcto? [S/N])> "
  150. read correcto
  151. case $correcto in
  152. y|Y|s|S)
  153. #Correcto
  154. unset correcto
  155. ;;
  156. *)
  157. printf "Cancelado por el Usuario.\n"
  158. return 1
  159. ;;
  160. esac
  161. printf "useradd $opciones $User\n"
  162. }
  163. function existeUsuario {
  164. #Comprobamos si un usuario ($User) existe
  165. cat /etc/passwd | cut -d: -f1 | grep $User > /dev/null 2>&1
  166. return $?
  167. }
  168. function existeGrupo {
  169. #Comprobamos si el grupo ($1) existe
  170. cat /etc/group | cut -d: -f1 | grep $1 > /dev/null 2>&1
  171. return $?
  172. }
  173. function crearGrupo {
  174. #Crea el grupo $1
  175. groupadd $1 > /dev/null 2>&1
  176. return $?
  177. }
  178. function ayuda {
  179. #Muestra la ayuda dependiendo del caso en el que estemos.
  180. printf "ayuda $1\n"
  181. case "$1" in
  182. alta)
  183. printf "alta: Dar de ALTA un nuevo usuario\n"
  184. printf "SINOPSIS: alta [OPCIONES] [user]\n"
  185. printf "OPCIONES:\n"
  186. printf "\t-d, --home-dir HOME_DIR\n"
  187. printf "\t\tDirectorio personal del usuario (HOME_DIR).\n"
  188. printf "\t-g, --gid GROUP\n"
  189. printf "\t\tNombre del grupo principal para el usuario.\n"
  190. printf "\t-G, --groups GROUP1[,GROUP2[,...[,GROUPN]]]\n"
  191. printf "\t\tGrupos adicionales para el usuario.\n"
  192. printf "\t-m, --create-home\n"
  193. printf "\t\tCrea el directorio personal si no existe.\n"
  194. printf "\t-p, --pasword\n"
  195. printf "\t\tContraseña del usuario.\n"
  196. printf "\t-s, --shell SHELL\n"
  197. printf "\t\tShell del usuario.\n"
  198. ;;
  199. cambiar)
  200. printf "cambiar: CAMBIAR contraseña de un usuario:\n"
  201. printf "SINOPSIS: cambiar [user]\n"
  202. ;;
  203. baja)
  204. printf "baja: Dar de BAJA un usuario\n"
  205. printf "SINOPSIS: baja [OPCIONES] [user]\n"
  206. printf "OPCIONES:\n"
  207. printf "\t-f, --force\n"
  208. printf "\t\tElimina incluso si está logeado y elimina el directorio\n"
  209. printf "\t\tpersonal del usuario y su mail.\n"
  210. printf "\t-r, --remove\n"
  211. printf "\t\tElimina el directorio personal del usuario y su mail.\n"
  212. ;;
  213. bloquear)
  214. printf "bloquear: BLOQUEAR un usuario\n"
  215. printf "SINOPSIS: bloquear [OPCIONES] [usuario]\n"
  216. printf "OPCIONES:\n"
  217. printf "\t-e, --expiredate EXPIRE_DATE\n"
  218. printf "\t\tFecha el formato YYYY-MM-DD en el que el usuario se bloqueara.\n"
  219. printf "\t\tPor defecto: 1970-01-02 (siempre se bloquea).\n"
  220. ;;
  221. desbloquear)
  222. printf "desbloquear: DESBLOQUEAR un usuario\n"
  223. printf "SINOPSIS: desbloquear [usuario]\n"
  224. ;;
  225. ""|ayuda|help)
  226. printf "COMANDOS:\n"
  227. printf "> alta\t\tDar de ALTA un nuevo usuario\n"
  228. printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
  229. printf "> baja\t\tDar de BAJA un usuario\n"
  230. printf "> bloquear\tBLOQUEAR un usuario\n"
  231. printf "> desbloquear\tDESBLOQUEAR un usuario\n"
  232. printf "> ayuda\t\tMuestra esta ayuda\n"
  233. printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
  234. printf "> salir\t\tFinaliza programa\n"
  235. ;;
  236. salir|s|S|quit|q|Q|exit)
  237. printf "salir: finaliza el programa\n"
  238. printf "SINOPSIS: salir, s, S, quit, q, Q, exit\n"
  239. ;;
  240. *)
  241. printf "Error: el comando $1 no existe.\n"
  242. #printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
  243. ayuda
  244. ;;
  245. esac
  246. }
  247. VERSION="0.1 Beta"
  248. comprobarPrivilegios
  249. mostrarBienvenida
  250. cont=true
  251. while [ $cont = true ]; do
  252. leerComando
  253. case $Opcion in
  254. alta)
  255. #Alta usuario
  256. altaUsuario
  257. ;;
  258. cambiar)
  259. #Cambiar passwd
  260. ;;
  261. baja)
  262. #Baja usuario
  263. ;;
  264. bloquear)
  265. #Bloquear usuario
  266. ;;
  267. desbloquear)
  268. #Desbloquear usuario
  269. ;;
  270. ayuda|help)
  271. #Ayuda de comandos
  272. ayuda $User
  273. ;;
  274. salir|s|S|quit|q|Q|exit)
  275. #Salida
  276. #Podreamos hacer break, pero los bucles infinitos son feos.
  277. cont=false
  278. ;;
  279. *)
  280. #Opcion Incorrecta
  281. printf "Error de Sintaxis: Comando no valido.\n"
  282. ayuda
  283. ;;
  284. esac
  285. unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones
  286. done
  287. unset VERSION cont
  288. exit