windows: 支持使用非 administrator 账号

This commit is contained in:
bin456789
2026-05-05 22:55:00 +08:00
parent 107c56ac59
commit 139c342b7e
6 changed files with 124 additions and 12 deletions

View File

@ -2342,6 +2342,59 @@ trim() {
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
assert_username_valid() {
if ! msg=$(is_username_valid); then
error_and_exit "$msg"
fi
}
is_username_valid() {
# https://learn.microsoft.com/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-useraccounts-localaccounts-localaccount-name
# 不能为 none [ ] / \ : | < > + = ; , ? * % @
# 账号为空,则使用 Administrator
if [ -z "$username" ]; then
echo "Username: Will use the built-in Administrator account in ISO language."
return 0
fi
if [ "$(to_lower <<<"$username")" = none ]; then
echo "Username: Do not use the name \"NONE\", this is a restricted username."
return 1
fi
if grep -q '[][/\:|<>+=;,?*%@]' <<<"$username"; then
echo "Username: Do not use any of the following characters: / \ [ ] : | < > + = ; , ? * % @"
return 1
fi
# 如果输入以下用户名则忽略,并使用系统内置的 Administrator 账号
# 防止系统有两个不同语言的 Administrator 账号而造成困扰
for builtin_username in \
administrator \
administrador \
administrateur \
administratör \
администратор \
järjestelmänvalvoja \
rendszergazda; do
if [ "$(to_lower <<<"$username")" = "$builtin_username" ]; then
echo "Username: Will use the built-in Administrator account in ISO language."
unset username
return 0
fi
done
}
prompt_username() {
info "prompt username"
warn false "Leave blank to use Administrator"
warn false "不填写则使用 Administrator"
IFS= read -r -p "Username: " username
username="$(printf "%s" "$username" | trim)"
assert_username_valid
}
prompt_password() {
info "prompt password"
warn false "Leave blank to use a random password."
@ -3118,7 +3171,7 @@ build_extra_cmdline() {
# https://salsa.debian.org/installer-team/rootskel/-/blob/master/src/lib/debian-installer-startup.d/S02module-params?ref_type=heads
for key in confhome hold force_boot_mode force_cn force_old_windows_setup cloud_image main_disk \
elts deb_mirror \
ssh_port rdp_port web_port allow_ping; do
username ssh_port rdp_port web_port allow_ping; do
value=${!key}
if [ -n "$value" ]; then
is_need_quote "$value" &&
@ -4312,6 +4365,7 @@ for o in ci installer debug minimal allow-ping force-cn help \
img: \
cloud-data: \
lang: \
user: username: \
passwd: password: \
ssh-port: \
ssh-key: public-key: \
@ -4446,6 +4500,14 @@ while true; do
force_boot_mode=$2
shift 2
;;
--user | --username)
if ! [ "$distro" = windows ]; then
error_and_exit "$1 is only supported for installing Windows."
fi
username="$(printf "%s" "$2" | trim)"
assert_username_valid
shift 2
;;
--passwd | --password)
[ -n "$2" ] || error_and_exit "Need value for $1"
password=$2
@ -4621,6 +4683,11 @@ done
# 检查必须的参数
verify_os_args
# 用户名
if [ "$distro" = windows ] && [ -z "$username" ]; then
prompt_username
fi
# 密码
if ! is_netboot_xyz && [ -z "$ssh_keys" ] && [ -z "$password" ]; then
if is_use_dd; then
@ -4900,7 +4967,7 @@ info 'info'
echo "$distro $releasever"
case "$distro" in
windows) username=administrator ;;
windows) username=${username:-administrator} ;;
netboot.xyz) username= ;;
dd | *) username=root ;;
esac