userspanel.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. if [ $? -ne 0 ]; then
  163. printf "ERROR FATAL: No se ha podido crear el usuario (codigo salida: $?).\n"
  164. return 1
  165. fi
  166. return 0
  167. }
  168. function cambiarPassword {
  169. #Cambiamos la contraseña del usuario ($User)
  170. if [ -z $User ]; then
  171. printf "(USER)> "
  172. read User
  173. fi
  174. existeUsuario
  175. if [ $? -ne 0 ]; then
  176. printf "ERROR: el usuario $User no existe.\n"
  177. return 1
  178. fi
  179. passwd $User
  180. if [ $? -ne 0 ]; then
  181. printf "ERROR FATAL: No es posible cambiar la contraseña (codigo salida: $?).\n"
  182. return 1
  183. fi
  184. return 0
  185. }
  186. function existeUsuario {
  187. #Comprobamos si un usuario ($User) existe
  188. cat /etc/passwd | cut -d: -f1 | grep $User > /dev/null 2>&1
  189. return $?
  190. }
  191. function existeGrupo {
  192. #Comprobamos si el grupo ($1) existe
  193. cat /etc/group | cut -d: -f1 | grep $1 > /dev/null 2>&1
  194. return $?
  195. }
  196. function crearGrupo {
  197. #Crea el grupo $1
  198. groupadd $1 > /dev/null 2>&1
  199. return $?
  200. }
  201. function ayuda {
  202. #Muestra la ayuda dependiendo del caso en el que estemos.
  203. printf "ayuda $1\n"
  204. case "$1" in
  205. alta)
  206. printf "alta: Dar de ALTA un nuevo usuario\n"
  207. printf "SINOPSIS: alta [OPCIONES] [user]\n"
  208. printf "OPCIONES:\n"
  209. printf "\t-d, --home-dir HOME_DIR\n"
  210. printf "\t\tDirectorio personal del usuario (HOME_DIR).\n"
  211. printf "\t-g, --gid GROUP\n"
  212. printf "\t\tNombre del grupo principal para el usuario.\n"
  213. printf "\t-G, --groups GROUP1[,GROUP2[,...[,GROUPN]]]\n"
  214. printf "\t\tGrupos adicionales para el usuario.\n"
  215. printf "\t-p, --pasword\n"
  216. printf "\t\tContraseña del usuario.\n"
  217. printf "\t-s, --shell SHELL\n"
  218. printf "\t\tShell del usuario.\n"
  219. ;;
  220. cambiar)
  221. printf "cambiar: CAMBIAR contraseña de un usuario:\n"
  222. printf "SINOPSIS: cambiar [user]\n"
  223. ;;
  224. baja)
  225. printf "baja: Dar de BAJA un usuario\n"
  226. printf "SINOPSIS: baja [OPCIONES] [user]\n"
  227. printf "OPCIONES:\n"
  228. printf "\t-f, --force\n"
  229. printf "\t\tElimina incluso si está logeado y elimina el directorio\n"
  230. printf "\t\tpersonal del usuario y su mail.\n"
  231. printf "\t-r, --remove\n"
  232. printf "\t\tElimina el directorio personal del usuario y su mail.\n"
  233. ;;
  234. bloquear)
  235. printf "bloquear: BLOQUEAR un usuario\n"
  236. printf "SINOPSIS: bloquear [OPCIONES] [usuario]\n"
  237. printf "OPCIONES:\n"
  238. printf "\t-e, --expiredate EXPIRE_DATE\n"
  239. printf "\t\tFecha el formato YYYY-MM-DD en el que el usuario se bloqueara.\n"
  240. printf "\t\tPor defecto: 1970-01-02 (siempre se bloquea).\n"
  241. ;;
  242. desbloquear)
  243. printf "desbloquear: DESBLOQUEAR un usuario\n"
  244. printf "SINOPSIS: desbloquear [usuario]\n"
  245. ;;
  246. ""|ayuda|help)
  247. printf "COMANDOS:\n"
  248. printf "> alta\t\tDar de ALTA un nuevo usuario\n"
  249. printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
  250. printf "> baja\t\tDar de BAJA un usuario\n"
  251. printf "> bloquear\tBLOQUEAR un usuario\n"
  252. printf "> desbloquear\tDESBLOQUEAR un usuario\n"
  253. printf "> ayuda\t\tMuestra esta ayuda\n"
  254. printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
  255. printf "> salir\t\tFinaliza programa\n"
  256. ;;
  257. salir|s|S|quit|q|Q|exit)
  258. printf "salir: finaliza el programa\n"
  259. printf "SINOPSIS: salir, s, S, quit, q, Q, exit\n"
  260. ;;
  261. *)
  262. printf "Error: el comando $1 no existe.\n"
  263. #printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
  264. ayuda
  265. ;;
  266. esac
  267. }
  268. VERSION="0.1 Beta"
  269. comprobarPrivilegios
  270. mostrarBienvenida
  271. cont=true
  272. while [ $cont = true ]; do
  273. leerComando
  274. case $Opcion in
  275. alta)
  276. #Alta usuario
  277. altaUsuario
  278. if [ $? -eq 0 ]; then
  279. printf "USUARIO $User CREADO CON EXITO.\n"
  280. fi
  281. ;;
  282. cambiar)
  283. #Cambiar passwd
  284. cambiarPassword
  285. if [ $? -eq 0 ]; then
  286. printf "PASSWORD DEL USUARIO $User CAMBIADO CON EXITO\n"
  287. fi
  288. ;;
  289. baja)
  290. #Baja usuario
  291. ;;
  292. bloquear)
  293. #Bloquear usuario
  294. ;;
  295. desbloquear)
  296. #Desbloquear usuario
  297. ;;
  298. ayuda|help)
  299. #Ayuda de comandos
  300. ayuda $User
  301. ;;
  302. salir|s|S|quit|q|Q|exit)
  303. #Salida
  304. #Podreamos hacer break, pero los bucles infinitos son feos.
  305. cont=false
  306. ;;
  307. *)
  308. #Opcion Incorrecta
  309. printf "Error de Sintaxis: Comando no valido.\n"
  310. ayuda
  311. ;;
  312. esac
  313. unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones
  314. done
  315. unset VERSION cont
  316. exit