| 1 |
#!/bin/bash -e |
| 2 |
|
| 3 |
DESCRIPTION="Idempotent setup script for SSH access" |
| 4 |
|
| 5 |
MBD_USERS="mini-buildd-uploader mini-buildd-admin mini-buildd-staff" |
| 6 |
|
| 7 |
case "${1}" in |
| 8 |
"--purge") |
| 9 |
read -p"Really purge users ${MBD_USERS} (Ctrl-c to abort)? " DUMMY |
| 10 |
for user in ${MBD_USERS}; do |
| 11 |
killall -v -u "${user}" || true |
| 12 |
! getent passwd "${user}" || deluser --remove-home "${user}" |
| 13 |
done |
| 14 |
exit 0 |
| 15 |
;; |
| 16 |
"--help"|"-h") |
| 17 |
cat <<EOF |
| 18 |
Usage: $(basename "${0}") [[<ENDPOINT>]|[--purge]] (as user root) |
| 19 |
|
| 20 |
${DESCRIPTION} |
| 21 |
|
| 22 |
Create and setup three UNIX users that are corresponding to mini-buildd users of the same name: |
| 23 |
|
| 24 |
* mini-buildd-uploader: Allow uploads via SSH |
| 25 |
* mini-buildd-staff: Allow API calls with 'staff' authorization via SSH |
| 26 |
* mini-buildd-admin: Allow API calls with 'admin' authorization via SSH |
| 27 |
|
| 28 |
Needed extra work on mini-buildd: |
| 29 |
|
| 30 |
* BEFORE running this: Please create all the three mini-buildd users |
| 31 |
* AFTER running this: Please check/configure/activate the Upload Profile for user mini-buildd-uploader |
| 32 |
|
| 33 |
When this is up: |
| 34 |
|
| 35 |
* Grant someone access: See the example line in created 'authorized_keys' files of the resp. users. |
| 36 |
* Run API calls: 'ssh mini-buildd-staff|admin@<yourhost> mini-buildd-api <mini_buildd_api_args>' |
| 37 |
Note that you will need the _complete_ arguments, including the correct user endpoint (like 'http://mini-buildd-staff@<yourhost>:8066') |
| 38 |
* Upload: An extra '.dput.cf' will be generated in '/var/lib/mini-buildd/etc/dput.cf' (for dput_conf API call) |
| 39 |
Authorized users can now also upload with this new target. |
| 40 |
|
| 41 |
Caveats: |
| 42 |
|
| 43 |
Someone with access to 'mini-buildd-uploader' could potentially copy from or write to arbitrary locations (within the |
| 44 |
mini-buildd-uploader user's permissions). |
| 45 |
EOF |
| 46 |
exit 0 |
| 47 |
esac |
| 48 |
|
| 49 |
[ $(id -u) -eq 0 ] || { printf "E: Needs to be run as root (try --help for usage).\n" >&2; exit 1; } |
| 50 |
|
| 51 |
read -p"Really create && setup UNIX users ${MBD_USERS} (Check --help first, Ctrl-c to abort)? " DUMMY |
| 52 |
|
| 53 |
MBD_ENDPOINT="${1:-http://$(hostname -f):8066}" |
| 54 |
MBD_STATUS=$(mini-buildd-api --json status "${MBD_ENDPOINT}") |
| 55 |
MBD_IDENTITY=$(jq --raw-output ".identity" <<<${MBD_STATUS}) |
| 56 |
MBD_URL=$(jq --raw-output ".url" <<<${MBD_STATUS}) |
| 57 |
[ -n "${MBD_IDENTITY}" ] || { printf "E: Can't get identity from ${MBD_ENDPOINT}\n" >&2; exit 1; } |
| 58 |
printf "I: Using mini-buildd endpoint '%s' with identity '%s'\n" "${MBD_ENDPOINT}" "${MBD_IDENTITY}" |
| 59 |
|
| 60 |
for user in ${MBD_USERS}; do |
| 61 |
getent passwd "${user}" || adduser --disabled-password --gecos "${user}" "${user}" |
| 62 |
su - "${user}" --command "mkdir -v -m700 .ssh" || true |
| 63 |
done |
| 64 |
su - "mini-buildd-uploader" --command "mkdir -v -m755 incoming" || true |
| 65 |
|
| 66 |
DPUT_CF="/tmp/mbd_dput.cf" |
| 67 |
MBD_DPUT_CF="/var/lib/mini-buildd/etc/dput.cf" |
| 68 |
|
| 69 |
cat <<EOF | tee "${DPUT_CF}" |
| 70 |
|
| 71 |
# Alternate local setup to upload via SSH |
| 72 |
[mini-buildd-${MBD_IDENTITY}-ssh] |
| 73 |
method = scp |
| 74 |
login = mini-buildd-uploader |
| 75 |
fqdn = $(hostname -f) |
| 76 |
incoming = /home/mini-buildd-uploader/incoming/ |
| 77 |
EOF |
| 78 |
|
| 79 |
if [ -e "${MBD_DPUT_CF}" ]; then |
| 80 |
diff -u "${DPUT_CF}" "${MBD_DPUT_CF}" || printf "W: ${MBD_DPUT_CF} differs from generated\n" >&2 |
| 81 |
printf "I: Not touching already existing ${MBD_DPUT_CF}\n" |
| 82 |
else |
| 83 |
cp -v "${DPUT_CF}" "${MBD_DPUT_CF}" |
| 84 |
chown -v mini-buildd:mini-buildd "${MBD_DPUT_CF}" |
| 85 |
fi |
| 86 |
|
| 87 |
_install_authorized_key_example() |
| 88 |
{ |
| 89 |
local user=${1} command=${2} |
| 90 |
local example_path="/tmp/mbd_auth_keys_example_${user}" |
| 91 |
target_file=$(getent passwd ${user} | cut -d: -f6)/.ssh/authorized_keys |
| 92 |
cat <<EOF | tee "${example_path}" |
| 93 |
# command="${command}",no-port-forwarding,no-X11-forwarding <public_key> |
| 94 |
EOF |
| 95 |
if [ -e "${target_file}" ]; then |
| 96 |
printf "I: Not touching already existing ${target_file}\n" |
| 97 |
else |
| 98 |
cp -v "${example_path}" "${target_file}" |
| 99 |
chown -v "${user}:${user}" "${target_file}" |
| 100 |
fi |
| 101 |
} |
| 102 |
|
| 103 |
_install_authorized_key_example mini-buildd-uploader /usr/sbin/mini-buildd-ssh-uploader-command |
| 104 |
_install_authorized_key_example mini-buildd-staff '/usr/bin/mini-buildd-api ${SSH_ORIGINAL_COMMAND#* }' |
| 105 |
_install_authorized_key_example mini-buildd-admin '/usr/bin/mini-buildd-api ${SSH_ORIGINAL_COMMAND#* }' |
| 106 |
|
| 107 |
UPLOADER_DPUT_CF=~mini-buildd-uploader/.dput.cf |
| 108 |
if [ -e "${UPLOADER_DPUT_CF}" ]; then |
| 109 |
printf "I: Not touching already existing ${UPLOADER_DPUT_CF}\n" |
| 110 |
else |
| 111 |
mini-buildd-api dput_conf "${MBD_ENDPOINT}" | tee "${UPLOADER_DPUT_CF}" |
| 112 |
chown -v mini-buildd-uploader:mini-buildd-uploader "${UPLOADER_DPUT_CF}" |
| 113 |
fi |
| 114 |
|
| 115 |
_setup_pk_plain() |
| 116 |
{ |
| 117 |
sudo apt-get install python3-keyrings.alt || true # for the PlainTextKeyring (newer versions only) |
| 118 |
|
| 119 |
local home=${1} |
| 120 |
local user=$(stat --format "%U" ${home}) configDir |
| 121 |
# python-keyring 21.3.0 (bullseye+) fixes config file location to '~/.config/python_keyring'; we need to support the old path for some time still. |
| 122 |
for configDir in "${home}/.config/python_keyring" "${home}/.local/share/python_keyring"; do |
| 123 |
mkdir -p "${configDir}" |
| 124 |
local configFile="${configDir}/keyringrc.cfg" |
| 125 |
cat <<EOF >"${configFile}" |
| 126 |
# suit able for python3-keyring > 8 |
| 127 |
[backend] |
| 128 |
default-keyring=keyrings.alt.file.PlaintextKeyring |
| 129 |
EOF |
| 130 |
chown -v -R $(stat --format "%U:%G" ${home}) "${configDir}" |
| 131 |
done |
| 132 |
while true; do |
| 133 |
read -p "=> About to test/update stored password for user ${user} on ${MBD_URL} (please CREATE NOW if it does not exist yet && and save password when asked) (Ctrl-c to abort, RET to continue)" DUMMY |
| 134 |
! su - "${user}" --command "mini-buildd-api status ${MBD_URL/\/\//\/\/${user}@}" || break |
| 135 |
done |
| 136 |
} |
| 137 |
_setup_pk_plain ~mini-buildd-staff || true |
| 138 |
_setup_pk_plain ~mini-buildd-admin || true |
| 139 |
|
| 140 |
for user in mini-buildd-uploader; do |
| 141 |
if su - "mini-buildd-uploader" --command "gpg --batch --passphrase '' --quick-generate-key mini-buildd-uploader"; then |
| 142 |
UPLOADER_PUB_KEY=$(su - "mini-buildd-uploader" --command "gpg --export --armor mini-buildd-uploader") |
| 143 |
printf "%s\n" "${UPLOADER_PUB_KEY}" |
| 144 |
read -p "=> About to auto-upload above key to ${user} on ${MBD_URL} (please CREATE NOW if it does not exist yet && rather don't save password when asked) (Ctrl-c to abort, RET to continue)" DUMMY |
| 145 |
su - "mini-buildd-uploader" --command "mini-buildd-api --auto-confirm set_user_key ${MBD_URL/\/\//\/\/mini-buildd-uploader@} '${UPLOADER_PUB_KEY}'" |
| 146 |
fi |
| 147 |
done |