userspanel.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. force=false
  20. remove=false
  21. printf "$(basename $0)>\t"
  22. read Comando
  23. #printf "\n"
  24. #Parseamos la linea de comando con todas las opciones posibles.
  25. printf "$Comando\n"
  26. Temp=$(getopt -q -o d:g:G:p:s:fre: --long home-dir:,gid:,groups:,password:,shell:,force,remove,expiredate: -- $Comando)
  27. eval set -- "$Temp"
  28. printf "$Temp\n"
  29. while true; do
  30. case "$1" in
  31. -d|--home-dir)
  32. homeDir=$2
  33. shift 2
  34. ;;
  35. -g|--gid)
  36. gid=$2
  37. shift 2
  38. ;;
  39. -G|--groups)
  40. otherGroups=$2
  41. shift 2
  42. ;;
  43. -p|--password)
  44. password=$2
  45. shift 2
  46. ;;
  47. -s|--shell)
  48. shell=$2
  49. shift 2
  50. ;;
  51. -f|--force)
  52. force=true
  53. shift
  54. ;;
  55. -r|--remove)
  56. remove=true
  57. shift
  58. ;;
  59. -e|--expiredate)
  60. expireDate=$2
  61. shift 2
  62. ;;
  63. --)
  64. shift
  65. break
  66. ;;
  67. *)
  68. printf "Error de Sintaxis: Comando no valido.\n"
  69. return 1
  70. ;;
  71. esac
  72. done
  73. Opcion=$1
  74. User=$2
  75. printf "Opcion: $Opcion\tUsuario: $User\n"
  76. unset Comando Temp
  77. }
  78. function altaUsuario {
  79. #Crea el usuario si no existe.
  80. #Comprobaciones
  81. if [ -z $User ]; then
  82. #Usuario no definido
  83. printf "(USUARIO)> "
  84. read User
  85. fi
  86. existeUsuario
  87. if [ $? -eq 0 ]; then
  88. #Ya existe
  89. printf "ERROR: El usuario no esta disponible (ya existe).\n"
  90. return 1
  91. fi
  92. if [ -z $homeDir ]; then
  93. printf "(HOME_DIR)> "
  94. read homeDir
  95. fi
  96. opciones="-m -d $homeDir"
  97. if [ -z $gid ]; then
  98. printf "(GROUP)> "
  99. read gid
  100. fi
  101. existeGrupo $gid
  102. if [ $? -ne 0 ]; then
  103. #No existe grupo. Vamos a crearlo
  104. crearGrupo $gid
  105. if [ $? -ne 0 ]; then
  106. printf "ERROR FATAL: No se puede crear grupo.\n"
  107. return 1
  108. fi
  109. fi
  110. opciones=$opciones" -g $gid"
  111. if [ ! -z $otherGroups ]; then
  112. opciones=$opciones" -G $otherGroups"
  113. fi
  114. if [ -z $password ]; then
  115. igual=false
  116. while [ $igual = false ]; do
  117. printf "(PASSWORD)> "
  118. read -s password
  119. printf "\n(Confirme PASSWORD)> "
  120. read -s password2
  121. printf "\n"
  122. if [ $password = $password2 ]; then
  123. igual=true
  124. else
  125. printf "ERROR: No coinciden. Vuelva a intentarlo.\n"
  126. fi
  127. done
  128. unset igual password2
  129. fi
  130. #opciones=$opciones" -p $password"
  131. if [ -z $shell ]; then
  132. printf "Shells disponibles:\n"
  133. for linea in $(grep -v "#" /etc/shells); do
  134. printf "\t$linea\n"
  135. done
  136. printf "\t/usr/sbin/nologin\n\t/bin/false\n"
  137. printf "(SHELL)> "
  138. read shell
  139. unset linea
  140. fi
  141. if [ ! -x $shell ]; then
  142. printf "ERROR: El shell $shell no esta disponible.\n"
  143. return 1
  144. fi
  145. opciones=$opciones" -s $shell"
  146. #Todo correcto. Ejecutamos useradd
  147. printf "Resumen:\n"
  148. printf "\tUSUARIO:\t$User\n"
  149. printf "\tHOMEDIR:\t$(realpath $homeDir)\n"
  150. printf "\tGROUP:\t\t$gid\n"
  151. printf "\tGRUPOS ADIC.:\t$otherGroups\n"
  152. printf "\tSHELL:\t\t$shell\n"
  153. printf "(Es correcto? [S/N])> "
  154. read correcto
  155. case $correcto in
  156. y|Y|s|S)
  157. #Correcto
  158. unset correcto
  159. ;;
  160. *)
  161. printf "Cancelado por el Usuario.\n"
  162. return 1
  163. ;;
  164. esac
  165. printf "useradd $opciones $User\n"
  166. useradd $opciones $User
  167. if [ $? -ne 0 ]; then
  168. printf "ERROR FATAL: No se ha podido crear el usuario.\n"
  169. return 1
  170. fi
  171. #Cambiamos usuario
  172. printf "$password\n$password" | passwd $User >/dev/null 2>&1
  173. if [ $? -ne 0 ]; then
  174. printf "ERROR: al establecer contraseña (usuario creado).\n"
  175. return 1
  176. fi
  177. return 0
  178. }
  179. function bajaUsuario {
  180. #Eliminar usuario si existe
  181. if [ -z $User ]; then
  182. listarUsuarios
  183. printf "(USUARIO)> "
  184. read User
  185. fi
  186. existeUsuario
  187. if [ $? -ne 0 ]; then
  188. printf "ERROR: no existe el usuario $User.\n"
  189. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  190. return 1
  191. fi
  192. opciones=""
  193. if [ $force = true ]; then
  194. opciones=$opciones" -f"
  195. fi
  196. if [ $remove = true ]; then
  197. opciones=$opciones" -r"
  198. fi
  199. printf "(QUIERES ELIMINAR A $User? [Y/N])> "
  200. read correcto
  201. case $correcto in
  202. y|Y|s|S)
  203. unset correcto
  204. ;;
  205. *)
  206. printf "Cancelado por el Usuario.\n"
  207. return 1
  208. ;;
  209. esac
  210. printf "userdel$opciones $User\n"
  211. userdel$opciones $User
  212. return 0
  213. }
  214. function cambiarPassword {
  215. #Cambiamos la contraseña del usuario ($User)
  216. if [ -z $User ]; then
  217. listarUsuarios
  218. printf "(USUARIO)> "
  219. read User
  220. fi
  221. existeUsuario
  222. if [ $? -ne 0 ]; then
  223. printf "ERROR: el usuario $User no existe.\n"
  224. return 1
  225. fi
  226. passwd $User
  227. if [ $? -ne 0 ]; then
  228. printf "ERROR FATAL: No es posible cambiar la contraseña.\n"
  229. return 1
  230. fi
  231. return 0
  232. }
  233. function listarUsuarios {
  234. #Listamos los usuarios que hay en el sistema.
  235. printf "Usuarios disponibles:\n"
  236. for lista in $(cat /etc/passwd | cut -d: -f1); do
  237. printf "$lista\t"
  238. done
  239. printf "\n"
  240. unset lista
  241. }
  242. function existeUsuario {
  243. #Comprobamos si un usuario ($User) existe
  244. cat /etc/passwd | cut -d: -f1 | grep ^$User$ > /dev/null 2>&1
  245. return $?
  246. }
  247. function existeGrupo {
  248. #Comprobamos si el grupo ($1) existe
  249. cat /etc/group | cut -d: -f1 | grep ^$1$ > /dev/null 2>&1
  250. return $?
  251. }
  252. function crearGrupo {
  253. #Crea el grupo $1
  254. groupadd $1 > /dev/null 2>&1
  255. return $?
  256. }
  257. function ayuda {
  258. #Muestra la ayuda dependiendo del caso en el que estemos.
  259. printf "ayuda $1\n"
  260. case "$1" in
  261. alta)
  262. printf "alta: Dar de ALTA un nuevo usuario\n"
  263. printf "SINOPSIS: alta [OPCIONES] [user]\n"
  264. printf "OPCIONES:\n"
  265. printf "\t-d, --home-dir HOME_DIR\n"
  266. printf "\t\tDirectorio personal del usuario (HOME_DIR).\n"
  267. printf "\t-g, --gid GROUP\n"
  268. printf "\t\tNombre del grupo principal para el usuario.\n"
  269. printf "\t-G, --groups GROUP1[,GROUP2[,...[,GROUPN]]]\n"
  270. printf "\t\tGrupos adicionales para el usuario.\n"
  271. printf "\t-p, --pasword\n"
  272. printf "\t\tContraseña del usuario.\n"
  273. printf "\t-s, --shell SHELL\n"
  274. printf "\t\tShell del usuario.\n"
  275. ;;
  276. cambiar)
  277. printf "cambiar: CAMBIAR contraseña de un usuario:\n"
  278. printf "SINOPSIS: cambiar [user]\n"
  279. ;;
  280. baja)
  281. printf "baja: Dar de BAJA un usuario\n"
  282. printf "SINOPSIS: baja [OPCIONES] [user]\n"
  283. printf "OPCIONES:\n"
  284. printf "\t-f, --force\n"
  285. printf "\t\tElimina incluso si está logeado y elimina el directorio\n"
  286. printf "\t\tpersonal del usuario y su mail.\n"
  287. printf "\t-r, --remove\n"
  288. printf "\t\tElimina el directorio personal del usuario y su mail.\n"
  289. ;;
  290. bloquear)
  291. printf "bloquear: BLOQUEAR un usuario\n"
  292. printf "SINOPSIS: bloquear [OPCIONES] [usuario]\n"
  293. printf "OPCIONES:\n"
  294. printf "\t-e, --expiredate EXPIRE_DATE\n"
  295. printf "\t\tFecha el formato YYYY-MM-DD en el que el usuario se bloqueara.\n"
  296. printf "\t\tPor defecto: 1970-01-02 (siempre se bloquea).\n"
  297. ;;
  298. desbloquear)
  299. printf "desbloquear: DESBLOQUEAR un usuario\n"
  300. printf "SINOPSIS: desbloquear [usuario]\n"
  301. ;;
  302. ""|ayuda|help)
  303. printf "COMANDOS:\n"
  304. printf "> alta\t\tDar de ALTA un nuevo usuario\n"
  305. printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
  306. printf "> baja\t\tDar de BAJA un usuario\n"
  307. printf "> bloquear\tBLOQUEAR un usuario\n"
  308. printf "> desbloquear\tDESBLOQUEAR un usuario\n"
  309. printf "> usuarios\tListar USUARIOS en el sistema\n"
  310. printf "> ayuda\t\tMuestra esta ayuda\n"
  311. printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
  312. printf "> salir\t\tFinaliza programa\n"
  313. ;;
  314. usuarios)
  315. printf "usuarios: muestra los usuarios disponibles\n"
  316. printf "SINOPSIS: usuarios\n"
  317. ;;
  318. salir|s|S|quit|q|Q|exit)
  319. printf "salir: finaliza el programa\n"
  320. printf "SINOPSIS: salir, s, S, quit, q, Q, exit\n"
  321. ;;
  322. *)
  323. printf "Error: el comando $1 no existe.\n"
  324. #printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
  325. ayuda
  326. ;;
  327. esac
  328. }
  329. VERSION="0.1 Beta"
  330. comprobarPrivilegios
  331. mostrarBienvenida
  332. cont=true
  333. while [ $cont = true ]; do
  334. leerComando
  335. case $Opcion in
  336. alta)
  337. #Alta usuario
  338. altaUsuario
  339. if [ $? -eq 0 ]; then
  340. printf "USUARIO $User CREADO CON EXITO.\n"
  341. fi
  342. ;;
  343. cambiar)
  344. #Cambiar passwd
  345. cambiarPassword
  346. if [ $? -eq 0 ]; then
  347. printf "PASSWORD DEL USUARIO $User CAMBIADO CON EXITO.\n"
  348. fi
  349. ;;
  350. baja)
  351. #Baja usuario
  352. bajaUsuario
  353. if [ $? -eq 0 ]; then
  354. printf "USUARIO $User ELIMINADO CON EXITO.\n"
  355. fi
  356. ;;
  357. bloquear)
  358. #Bloquear usuario
  359. ;;
  360. desbloquear)
  361. #Desbloquear usuario
  362. ;;
  363. usuarios)
  364. #Lista todos los usuarios existentes
  365. listarUsuarios
  366. ;;
  367. ayuda|help)
  368. #Ayuda de comandos
  369. ayuda $User
  370. ;;
  371. salir|s|S|quit|q|Q|exit)
  372. #Salida
  373. #Podreamos hacer break, pero los bucles infinitos son feos.
  374. cont=false
  375. ;;
  376. *)
  377. #Opcion Incorrecta
  378. printf "Error de Sintaxis: Comando no valido.\n"
  379. ayuda
  380. ;;
  381. esac
  382. unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones
  383. done
  384. unset VERSION cont
  385. exit