freedombone-utils-passwords 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Password functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # If this file exists it contains a global password used with
  31. # disk image installs. This simplifies password management for
  32. # deployment at scale
  33. IMAGE_PASSWORD_FILE=/root/login.txt
  34. # Minimum number of characters in a password
  35. MINIMUM_PASSWORD_LENGTH=10
  36. # The default password length used in images
  37. DEFAULT_PASSWORD_LENGTH=20
  38. function passwords_select_user {
  39. SELECTED_USERNAME=
  40. # shellcheck disable=SC2207
  41. users_array=($(ls /home))
  42. delete=(git)
  43. # shellcheck disable=SC2068
  44. for del in ${delete[@]}
  45. do
  46. # shellcheck disable=SC2206
  47. users_array=(${users_array[@]/$del})
  48. done
  49. i=0
  50. W=()
  51. name=()
  52. # shellcheck disable=SC2068
  53. for u in ${users_array[@]}
  54. do
  55. if [[ $(is_valid_user "$u") == "1" ]]; then
  56. i=$((i+1))
  57. W+=("$i" "$u")
  58. name+=("$u")
  59. fi
  60. done
  61. if [ $i -eq 1 ]; then
  62. SELECTED_USERNAME="${name[0]}"
  63. else
  64. # shellcheck disable=SC2068
  65. user_index=$(dialog --backtitle $"Freedombone Control Panel" --title $"Select User" --menu $"Select one of the following:" 24 40 17 ${W[@]} 3>&2 2>&1 1>&3)
  66. # shellcheck disable=SC2181
  67. if [ $? -eq 0 ]; then
  68. # shellcheck disable=SC2034
  69. SELECTED_USERNAME="${name[$((user_index-1))]}"
  70. fi
  71. fi
  72. }
  73. function enforce_good_passwords {
  74. # because humans are generally bad at choosing passwords
  75. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  76. return
  77. fi
  78. apt-get -yq install libpam-cracklib
  79. sed -i 's/password.*requisite.*pam_cracklib.so.*/password required pam_cracklib.so retry=2 dcredit=-1 ucredit=-1 ocredit=0 lcredit=0 minlen=10 reject_username/g' /etc/pam.d/common-password
  80. mark_completed "${FUNCNAME[0]}"
  81. }
  82. function create_password {
  83. openssl rand -base64 32 | tr -dc A-Za-z0-9 | head -c "${1}" ; echo -n ''
  84. }
  85. # NOTE: deliberately no exit 0