userspanel.sh 7.4 KB

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