userspanel.sh 8.5 KB

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