1
0
forked from NASP/registry
registry/scripts/testbed_cron.sh

58 lines
1.4 KiB
Bash
Raw Normal View History

2023-03-30 14:04:03 +08:00
#!/bin/bash
set -ex
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
touch_user() {
test -n "$1"
if id -u $1 1>/dev/null 2>&1; then
return
fi
2023-04-21 17:23:04 +08:00
if ! getent group nasp ; then
2023-03-30 14:04:03 +08:00
echo "Group 'nasp' does not exist\!"
exit 1
fi
adduser \
--shell /bin/bash \
--disabled-password \
--home /home/$1 \
2023-04-21 17:12:36 +08:00
--gecos "nasp member" \
2023-03-30 14:04:03 +08:00
$1
usermod -a -G nasp $1
}
update_key() {
tmp_path="/tmp/authorized_keys_$1"
dest_path="/home/$1/.ssh/authorized_keys"
2023-04-21 17:12:36 +08:00
dest_dir=$(dirname "$dest_path")
2023-03-30 14:04:03 +08:00
2023-04-21 17:12:36 +08:00
echo "# This file is autoly generated. Changes here will not work." > "$tmp_path"
2023-03-30 14:04:03 +08:00
for file in $(find "$THIS_DIR/../authorized_keys/$1" -type f); do
(echo "# key file: ${file#*authorized_keys/}";cat "$file"; echo) >> "$tmp_path"
done
2023-04-21 17:12:36 +08:00
if [[ ! -d "$dest_dir" ]]; then
mkdir -p "$dest_dir"
chown $1:$1 "$dest_dir"
chmod 700 "$dest_dir"
fi
2023-03-30 14:04:03 +08:00
cat "$tmp_path" > "$dest_path"
rm "$tmp_path"
chown $1:$1 "$dest_path"
chmod 600 "$dest_path"
}
main() {
2023-04-21 17:23:04 +08:00
cp "$THIS_DIR/nasp" "/etc/sudoers.d/nasp"
2023-03-30 14:04:03 +08:00
for file in "$THIS_DIR"/../authorized_keys/* ; do
2023-04-21 17:12:36 +08:00
if [[ ! -d "$file" ]]; then
2023-03-30 14:04:03 +08:00
continue
fi
username=$(basename $file)
touch_user $username
update_key $username
done
}
main