userspanel.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  91. return 1
  92. fi
  93. if [ -z $homeDir ]; then
  94. printf "(HOME_DIR)> "
  95. read homeDir
  96. fi
  97. opciones="-m -d $homeDir"
  98. if [ -z $gid ]; then
  99. printf "(GROUP)> "
  100. read gid
  101. fi
  102. existeGrupo $gid
  103. if [ $? -ne 0 ]; then
  104. #No existe grupo. Vamos a crearlo
  105. crearGrupo $gid
  106. if [ $? -ne 0 ]; then
  107. printf "ERROR FATAL: No se puede crear grupo.\n"
  108. return 1
  109. fi
  110. fi
  111. opciones=$opciones" -g $gid"
  112. if [ ! -z $otherGroups ]; then
  113. opciones=$opciones" -G $otherGroups"
  114. fi
  115. if [ -z $password ]; then
  116. leerPassword
  117. fi
  118. #opciones=$opciones" -p $password"
  119. if [ -z $shell ]; then
  120. printf "Shells disponibles:\n"
  121. for linea in $(grep -v "#" /etc/shells); do
  122. printf "\t$linea\n"
  123. done
  124. printf "\t/usr/sbin/nologin\n\t/bin/false\n"
  125. printf "(SHELL)> "
  126. read shell
  127. unset linea
  128. fi
  129. if [ ! -x $shell ]; then
  130. printf "ERROR: El shell $shell no esta disponible.\n"
  131. return 1
  132. fi
  133. opciones=$opciones" -s $shell"
  134. #Todo correcto. Ejecutamos useradd
  135. printf "Resumen:\n"
  136. printf "\tUSUARIO:\t$User\n"
  137. printf "\tHOMEDIR:\t$(realpath $homeDir)\n"
  138. printf "\tGROUP:\t\t$gid\n"
  139. printf "\tGRUPOS ADIC.:\t$otherGroups\n"
  140. printf "\tSHELL:\t\t$shell\n"
  141. printf "(Es correcto? [S/N])> "
  142. read correcto
  143. case $correcto in
  144. y|Y|s|S)
  145. #Correcto
  146. unset correcto
  147. ;;
  148. *)
  149. printf "Cancelado por el Usuario.\n"
  150. return 1
  151. ;;
  152. esac
  153. printf "useradd $opciones $User\n"
  154. useradd $opciones $User
  155. if [ $? -ne 0 ]; then
  156. printf "ERROR FATAL: No se ha podido crear el usuario.\n"
  157. return 1
  158. fi
  159. #Cambiamos usuario
  160. printf "$password\n$password\n" | passwd $User >/dev/null 2>&1
  161. if [ $? -ne 0 ]; then
  162. printf "ERROR: al establecer contraseña (usuario creado).\n"
  163. return 1
  164. fi
  165. return 0
  166. }
  167. function bajaUsuario {
  168. #Eliminar usuario si existe
  169. if [ -z $User ]; then
  170. listarUsuarios
  171. printf "(USUARIO)> "
  172. read User
  173. fi
  174. existeUsuario
  175. if [ $? -ne 0 ]; then
  176. printf "ERROR: no existe el usuario $User.\n"
  177. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  178. return 1
  179. fi
  180. opciones=""
  181. if [ $force = true ]; then
  182. opciones=$opciones" -f"
  183. fi
  184. if [ $remove = true ]; then
  185. opciones=$opciones" -r"
  186. fi
  187. printf "(QUIERES ELIMINAR A $User? [Y/N])> "
  188. read correcto
  189. case $correcto in
  190. y|Y|s|S)
  191. unset correcto
  192. ;;
  193. *)
  194. printf "Cancelado por el Usuario.\n"
  195. return 1
  196. ;;
  197. esac
  198. printf "userdel$opciones $User\n"
  199. userdel$opciones $User
  200. return 0
  201. }
  202. function cambiarPassword {
  203. #Cambiamos la contraseña del usuario ($User)
  204. if [ -z $User ]; then
  205. listarUsuarios
  206. printf "(USUARIO)> "
  207. read User
  208. fi
  209. existeUsuario
  210. if [ $? -ne 0 ]; then
  211. printf "ERROR: el usuario $User no existe.\n"
  212. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  213. return 1
  214. fi
  215. if [ -z $password ]; then
  216. leerPassword
  217. fi
  218. printf "$password\n$password\n" | passwd $User >/dev/null 2>&1
  219. if [ $? -ne 0 ]; then
  220. printf "ERROR FATAL: No es posible cambiar la contraseña.\n"
  221. return 1
  222. fi
  223. return 0
  224. }
  225. function listarUsuarios {
  226. #Listamos los usuarios que hay en el sistema.
  227. printf "Usuarios disponibles:\n"
  228. for lista in $(cat /etc/passwd | cut -d: -f1); do
  229. printf "$lista\t"
  230. done
  231. printf "\n"
  232. unset lista
  233. }
  234. function bloquearUsuario {
  235. #Bloquea el usuario $User
  236. if [ -z $User ]; then
  237. listarUsuarios
  238. printf "(USUARIO)> "
  239. read User
  240. fi
  241. existeUsuario
  242. if [ $? -ne 0 ]; then
  243. printf "ERROR: el usuario $User no existe.\n"
  244. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  245. return 1
  246. fi
  247. estaBloqueado
  248. if [ $? -eq 0 ]; then
  249. printf "$User ya esta bloqueado (%s).\n" $(fechaBloqueado)
  250. printf "Escribir \"desbloquear\" para desbloquear usuario.\n"
  251. return 1
  252. fi
  253. if [ -z $expireDate ]; then
  254. printf "(FECHA BLOQUEO [yyyy-mm-dd])> "
  255. read expireDate
  256. case $expireDate in
  257. ""|now|NOW)
  258. expireDate=1
  259. ;;
  260. *)
  261. ;;
  262. esac
  263. fi
  264. usermod -e $expireDate $user
  265. if [ $? -ne 0 ]; then
  266. printf "ERROR FATAL: No se ha podido bloquear usuario $User.\n"
  267. return 1
  268. fi
  269. return 0
  270. }
  271. function desbloquearUsuario {
  272. #Desbloquear el usuario $User
  273. if [ -z $User ]; then
  274. listarUsuarios
  275. printf "(USUARIO)> "
  276. read User
  277. fi
  278. existeUsuario
  279. if [ $? -ne 0 ]; then
  280. printf "ERROR: el usuario $User no existe.\n"
  281. printf "\tEscribir \"usuarios\" para mostrar usuarios disponibles.\n"
  282. return 1
  283. fi
  284. estaBloqueado
  285. if [ $? -eq 0 ]; then
  286. printf "$User no esta bloqueado.\n"
  287. printf "Escribir \"bloquear\" para desbloquear usuario.\n"
  288. return 1
  289. fi
  290. usermod -e "" $User
  291. if [ $? -ne 0 ]; then
  292. printf "ERROR FATAL: No se ha podido desbloquear usuario $User.\n"
  293. return 1
  294. fi
  295. return 0
  296. }
  297. function estaBloqueado {
  298. #Comprueba si el usuario $User esta bloqueado (Usando Dias UNIX)
  299. ahora=$(expr $(date --utc -d now +%s) / 86400)
  300. locktime=$(grep ^$User: /etc/shadow | cut -d: -f8)
  301. #printf "Ahora: $ahora\nLock: $locktime\n"
  302. if [ -z $locktime ]; then
  303. #No esta bloqueado
  304. return 1
  305. fi
  306. if [ $locktime -le $ahora ]; then
  307. #Esta bloqueado
  308. return 0
  309. else
  310. #No lo esta
  311. return 1
  312. fi
  313. }
  314. function fechaBloqueado {
  315. #Muestra la fecha de bloqueo en formato humano yyyy-mm-dd
  316. locktime=$(expr $(grep ^$User: /etc/shadow | cut -d: -f8) '*' 86400)
  317. fecha=$(date -d @$locktime +%F)
  318. printf "$fecha\n"
  319. }
  320. function leerPassword {
  321. #Lee contraseña y la guarda en $password
  322. igual=false
  323. while [ $igual = false ]; do
  324. printf "(PASSWORD)> "
  325. read -s password
  326. printf "\n(Confirme PASSWORD)> "
  327. read -s password2
  328. printf "\n"
  329. if [ $password = $password2 ]; then
  330. igual=true
  331. else
  332. printf "ERROR: No coinciden. Vuelva a intentarlo.\n"
  333. fi
  334. done
  335. unset igual password2
  336. }
  337. function existeUsuario {
  338. #Comprobamos si un usuario ($User) existe
  339. cat /etc/passwd | cut -d: -f1 | grep ^$User$ > /dev/null 2>&1
  340. return $?
  341. }
  342. function existeGrupo {
  343. #Comprobamos si el grupo ($1) existe
  344. cat /etc/group | cut -d: -f1 | grep ^$1$ > /dev/null 2>&1
  345. return $?
  346. }
  347. function crearGrupo {
  348. #Crea el grupo $1
  349. groupadd $1 > /dev/null 2>&1
  350. return $?
  351. }
  352. function ayuda {
  353. #Muestra la ayuda dependiendo del caso en el que estemos.
  354. printf "ayuda $1\n"
  355. case "$1" in
  356. alta)
  357. printf "alta: Dar de ALTA un nuevo usuario\n"
  358. printf "SINOPSIS: alta [OPCIONES] [user]\n"
  359. printf "OPCIONES:\n"
  360. printf "\t-d, --home-dir HOME_DIR\n"
  361. printf "\t\tDirectorio personal del usuario (HOME_DIR).\n"
  362. printf "\t-g, --gid GROUP\n"
  363. printf "\t\tNombre del grupo principal para el usuario.\n"
  364. printf "\t-G, --groups GROUP1[,GROUP2[,...[,GROUPN]]]\n"
  365. printf "\t\tGrupos adicionales para el usuario.\n"
  366. printf "\t-p, --pasword\n"
  367. printf "\t\tContraseña del usuario.\n"
  368. printf "\t-s, --shell SHELL\n"
  369. printf "\t\tShell del usuario.\n"
  370. ;;
  371. cambiar)
  372. printf "cambiar: CAMBIAR contraseña de un usuario:\n"
  373. printf "SINOPSIS: cambiar [user]\n"
  374. printf "\t-p, --pasword\n"
  375. printf "\t\tContraseña del usuario.\n"
  376. ;;
  377. baja)
  378. printf "baja: Dar de BAJA un usuario\n"
  379. printf "SINOPSIS: baja [OPCIONES] [user]\n"
  380. printf "OPCIONES:\n"
  381. printf "\t-f, --force\n"
  382. printf "\t\tElimina incluso si está logeado y elimina el directorio\n"
  383. printf "\t\tpersonal del usuario y su mail.\n"
  384. printf "\t-r, --remove\n"
  385. printf "\t\tElimina el directorio personal del usuario y su mail.\n"
  386. ;;
  387. bloquear)
  388. printf "bloquear: BLOQUEAR un usuario\n"
  389. printf "SINOPSIS: bloquear [OPCIONES] [usuario]\n"
  390. printf "OPCIONES:\n"
  391. printf "\t-e, --expiredate EXPIRE_DATE\n"
  392. printf "\t\tFecha el formato YYYY-MM-DD en el que el usuario se bloqueara.\n"
  393. printf "\t\tPor defecto: 1970-01-02 (siempre se bloquea).\n"
  394. ;;
  395. desbloquear)
  396. printf "desbloquear: DESBLOQUEAR un usuario\n"
  397. printf "SINOPSIS: desbloquear [usuario]\n"
  398. ;;
  399. ""|ayuda|help)
  400. printf "COMANDOS:\n"
  401. printf "> alta\t\tDar de ALTA un nuevo usuario\n"
  402. printf "> cambiar\tCAMBIAR contraseña de un usuario\n"
  403. printf "> baja\t\tDar de BAJA un usuario\n"
  404. printf "> bloquear\tBLOQUEAR un usuario\n"
  405. printf "> desbloquear\tDESBLOQUEAR un usuario\n"
  406. printf "> usuarios\tListar USUARIOS en el sistema\n"
  407. printf "> ayuda\t\tMuestra esta ayuda\n"
  408. printf "\t\tPara mas ayuda escribir \"ayuda [comando]\"\n"
  409. printf "> salir\t\tFinaliza programa\n"
  410. ;;
  411. usuarios)
  412. printf "usuarios: muestra los usuarios disponibles\n"
  413. printf "SINOPSIS: usuarios\n"
  414. ;;
  415. salir|s|S|quit|q|Q|exit)
  416. printf "salir: finaliza el programa\n"
  417. printf "SINOPSIS: salir, s, S, quit, q, Q, exit\n"
  418. ;;
  419. *)
  420. printf "Error: el comando $1 no existe.\n"
  421. #printf "Escriba \"ayuda\" o \"help\" ver los posibles comandos.\n"
  422. ayuda
  423. ;;
  424. esac
  425. }
  426. VERSION="0.1 Beta"
  427. comprobarPrivilegios
  428. mostrarBienvenida
  429. cont=true
  430. while [ $cont = true ]; do
  431. leerComando
  432. case $Opcion in
  433. alta)
  434. #Alta usuario
  435. altaUsuario
  436. if [ $? -eq 0 ]; then
  437. printf "USUARIO $User CREADO CON EXITO.\n"
  438. fi
  439. ;;
  440. cambiar)
  441. #Cambiar passwd
  442. cambiarPassword
  443. if [ $? -eq 0 ]; then
  444. printf "PASSWORD DEL USUARIO $User CAMBIADO CON EXITO.\n"
  445. fi
  446. ;;
  447. baja)
  448. #Baja usuario
  449. bajaUsuario
  450. if [ $? -eq 0 ]; then
  451. printf "USUARIO $User ELIMINADO CON EXITO.\n"
  452. fi
  453. ;;
  454. bloquear)
  455. #Bloquear usuario
  456. bloquearUsuario
  457. if [ $? -eq 0 ]; then
  458. printf "USUARIO $User BLOQUEADO CON EXITO.\n"
  459. fi
  460. ;;
  461. desbloquear)
  462. #Desbloquear usuario
  463. desbloquearUsuario
  464. if [ $? -eq 0 ]; then
  465. printf "USUARIO $User DESBLOQUEADO CON EXITO.\n"
  466. fi
  467. ;;
  468. usuarios)
  469. #Lista todos los usuarios existentes
  470. listarUsuarios
  471. ;;
  472. ayuda|help)
  473. #Ayuda de comandos
  474. ayuda $User
  475. ;;
  476. salir|s|S|quit|q|Q|exit)
  477. #Salida
  478. #Podreamos hacer break, pero los bucles infinitos son feos.
  479. cont=false
  480. ;;
  481. *)
  482. #Opcion Incorrecta
  483. printf "Error de Sintaxis: Comando no valido.\n"
  484. ayuda
  485. ;;
  486. esac
  487. unset Opcion User homeDir gid otherGroups password shell force remove expireDate opciones
  488. done
  489. unset VERSION cont
  490. exit