From 937bf9e00084b3dac7801f5a8320c87ccdc9a800 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Wed, 29 May 2024 17:31:05 +0800 Subject: [PATCH 1/3] fix(testbed/cron): support nixos --- scripts/testbed_cron.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/testbed_cron.sh b/scripts/testbed_cron.sh index 1293b9f..5491a68 100755 --- a/scripts/testbed_cron.sh +++ b/scripts/testbed_cron.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -ex THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) @@ -46,7 +46,10 @@ update_key() { } main() { - cp "$THIS_DIR/nasp" "/etc/sudoers.d/nasp" + if [ -f /etc/os-release ]; then + . /etc/os-release + test "$ID" = "nixos" || cp "$THIS_DIR/nasp" "/etc/sudoers.d/nasp" + fi for file in "$THIS_DIR"/../authorized_keys/* ; do if [[ ! -d "$file" ]]; then continue -- 2.45.1 From 585b8ad82a655548d417204956524581bd582ad2 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Wed, 29 May 2024 17:55:34 +0800 Subject: [PATCH 2/3] feat(testbed/cron): use useradd; check username --- scripts/testbed_cron.sh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/testbed_cron.sh b/scripts/testbed_cron.sh index 5491a68..c9f299b 100755 --- a/scripts/testbed_cron.sh +++ b/scripts/testbed_cron.sh @@ -2,8 +2,14 @@ set -ex THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) +check_username() { + ( echo $1 | grep -qxE "^[a-z][-a-z0-9_]*\$" ) || return 1 + return 0 +} + touch_user() { test -n "$1" + check_username $1 || { echo "Invalid user name $1 !"; exit -1; } if id -u $1 1>/dev/null 2>&1; then return fi @@ -12,13 +18,8 @@ touch_user() { exit 1 fi - adduser \ - --shell /bin/bash \ - --disabled-password \ - --home /home/$1 \ - --gecos "nasp member" \ - $1 - usermod -a -G nasp $1 + useradd -m --groups nasp $1 + su - $1 -c "true" mkdir -p /home2/$1 chown $1: /home2/$1 -- 2.45.1 From e75179eb577b86012964879f5d748f558873a17d Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Wed, 29 May 2024 19:32:18 +0800 Subject: [PATCH 3/3] fix(testbed/cron): chown :nasp --- scripts/testbed_cron.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/testbed_cron.sh b/scripts/testbed_cron.sh index c9f299b..cbff88a 100755 --- a/scripts/testbed_cron.sh +++ b/scripts/testbed_cron.sh @@ -22,7 +22,7 @@ touch_user() { su - $1 -c "true" mkdir -p /home2/$1 - chown $1: /home2/$1 + chown $1:nasp /home2/$1 } update_key() { @@ -37,12 +37,12 @@ update_key() { if [[ ! -d "$dest_dir" ]]; then mkdir -p "$dest_dir" - chown $1:$1 "$dest_dir" + chown $1:nasp "$dest_dir" chmod 700 "$dest_dir" fi cat "$tmp_path" > "$dest_path" rm "$tmp_path" - chown $1:$1 "$dest_path" + chown $1:nasp "$dest_path" chmod 600 "$dest_path" } -- 2.45.1