You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

  1. # -*- mode: shell-script -*-
  2. make_build_script() {
  3. local users_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(p0 "$@" | md5_compat)"
  4. if [ -e "$cache_file" ]; then
  5. #debug "$FUNCNAME: STATIC cache hit"
  6. cat "$cache_file" &&
  7. touch "$cache_file" || return 1
  8. return 0
  9. fi
  10. local users_def="$1" \
  11. code fixed_groups_code groups_code volume_keys \
  12. created_groups first_group
  13. if [ -z "$users_def" ]; then
  14. return 0
  15. fi
  16. e "set -eux"$'\n'
  17. code=""
  18. fixed_groups_code=""
  19. groups_code=""
  20. volume_keys=()
  21. declare -A created_groups
  22. while read-0 user user_def; do
  23. code+="mkdir -p \"/home/$user\""$'\n'
  24. ##
  25. ## Group management
  26. ##
  27. first_group=
  28. groups=()
  29. first=1
  30. while read-0 group; do
  31. [ "${created_groups[$group]}" ] && continue
  32. if [[ "$group" == *":"* ]]; then
  33. gid=${group##*:}
  34. group=${group%%:*}
  35. fixed_groups_code+="addgroup -g \"$gid\" \"$group\""$'\n'
  36. else
  37. groups_code+="addgroup \"$group\""$'\n'
  38. fi
  39. created_groups[$group]=1
  40. if [ "$first" ]; then
  41. first_group="$group"
  42. first=
  43. else
  44. remaining_groups+=("$group")
  45. fi
  46. groups+=("$group")
  47. done < <(echo "$user_def" | shyaml get-values-0 groups 2>/dev/null)
  48. ##
  49. ## User create commands
  50. ##
  51. uid=$(echo "$user_def" | shyaml get-value uid 2>/dev/null)
  52. useradd_options=(
  53. "-D" ## don't assign a password
  54. "-s" "/bin/false" ## default shell
  55. )
  56. if [ "$uid" ]; then
  57. useradd_options+=("-u" "$uid") ## force uid
  58. fi
  59. if [ "$first_group" ]; then
  60. useradd_options+=("-G" "$first_group") ## force main group
  61. fi
  62. code+="adduser ${useradd_options[*]} \"$user\""$'\n'
  63. if [ "$allow_writeable_chroot" ]; then
  64. code+="chown $user \"/home/$user\""$'\n' ## sanitize
  65. else
  66. code+="chown root:root \"/home/$user\""$'\n' ## sanitize
  67. fi
  68. code+="chmod 755 \"/home/$user\""$'\n' ## sanitize
  69. password=$(echo "$user_def" | shyaml get-value password 2>/dev/null) ||
  70. password=$(gen_password 14)
  71. code+="echo '$user:$password' | chpasswd"$'\n'
  72. for group in "${remaining_groups[@]}"; do
  73. code+="adduser \"$user\" \"$group\""$'\n'
  74. done
  75. ##
  76. ## Key managements
  77. ##
  78. while read-0 key; do
  79. keys+="$key"$'\n'
  80. done < <(echo "$user_def" | shyaml get-values-0 -q keys)
  81. if [ "$keys" ]; then
  82. code+="mkdir -p \"/home/$user/.ssh\""$'\n'
  83. code+="cat <<EOF > /home/$user/.ssh/authorized_keys"$'\n'
  84. code+="$keys"
  85. code+="EOF"$'\n'
  86. # code+="chown $user /home/$user/.ssh/authorized_keys"$'\n'
  87. code+="chmod 644 /home/$user/.ssh/authorized_keys"$'\n'
  88. code+="chmod 755 /home/$user/.ssh"$'\n'
  89. fi
  90. done < <(echo "$users_def" | shyaml key-values-0)
  91. {
  92. echo -n "$fixed_groups_code"
  93. echo -n "$groups_code"
  94. echo -n "$code"
  95. } | tee "$cache_file"
  96. }