gencode-local 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh -e
  2. #
  3. # 2020 Joachim Desroches (joachim.desroches@epfl.ch)
  4. #
  5. # This file is part of cdist.
  6. #
  7. # cdist is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # cdist is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. cat <<- EOF
  21. # Length of generated password.
  22. LENGTH=
  23. # Keep password strictly alphanumeric.
  24. NOSYMB=
  25. # Check pass is installed.
  26. command -v pass >/dev/null 2>&1 ||
  27. {
  28. cat <<- EOF >&2
  29. __pass: this type requires pass installed.
  30. See https://www.passwordstore.org/.
  31. EOFF
  32. exit 1;
  33. }
  34. # Check for optional length parameter.
  35. if [ -f "${__object:?}/parameter/length" ];
  36. then
  37. LENGTH="$(cat "${__object:?}/parameter/length")"
  38. export LENGTH
  39. fi
  40. # Check for optional no symbols parameter.
  41. if [ -f "${__object:?}/parameter/no-symbols" ];
  42. then
  43. NOSYMB="-n"
  44. export NOSYMB
  45. fi
  46. # Load required password store location parameter.
  47. PASSWORD_STORE_DIR="$(cat "${__object:?}/parameter/storedir")"
  48. export PASSWORD_STORE_DIR
  49. # Check if the password store is initialized.
  50. if ! pass ls >/dev/null 2>&1;
  51. then
  52. cat <<- EOFF >&2
  53. __pass: this type requires the password store to be initialized.
  54. See cdist-type__pass_init(7) and pass(1) for more information.
  55. EOFF
  56. exit 1;
  57. fi
  58. # Generate a password if it does not already exist.
  59. if [ ! -f "\${PASSWORD_STORE_DIR}/${__object_id:?}.gpg" ];
  60. then
  61. # shellcheck disable=SC2086
  62. pass generate \$NOSYMB "${__object_id:?}" $LENGTH >/dev/null
  63. fi
  64. # Send it out to the messages.
  65. pass "${__object_id:?}" >> "${__messages_out:?}"
  66. EOF