userspanel.sh 8.1 KB

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