Skip to content

Commit d66fa87

Browse files
authored
fix: Seed per-user defaults before first logon (#2189)
1 parent 262b2d4 commit d66fa87

1 file changed

Lines changed: 212 additions & 72 deletions

File tree

‎src/sif.sh‎

Lines changed: 212 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ SIFInstall() {
7373
esac
7474

7575
disableAutoReboot "$target" || return 1
76+
patchDefaultHive "$target" "$driver" || return 1
7677
setLegacyKey "$target" "$driver" "$arch" "$desc" || return 1
7778
validateProductKey "$KEY" || return 1
7879

@@ -581,6 +582,214 @@ addSIFEntry() {
581582
return 0
582583
}
583584

585+
addHiveEntry() {
586+
587+
local file="$1"
588+
local entry="$2"
589+
590+
local header="[AddReg]"
591+
local line ending="lf"
592+
593+
line=$(grep -Fnx -m 1 "$header" "$file" | cut -d: -f1) || line=""
594+
595+
if [ -z "$line" ]; then
596+
line=$(grep -Fnx -m 1 "$header"$'\r' "$file" | cut -d: -f1) || line=""
597+
ending="crlf"
598+
fi
599+
600+
if [ -z "$line" ]; then
601+
error "Failed to locate section \"$header\" in \"$file\" !"
602+
return 1
603+
fi
604+
605+
local next
606+
next=$(awk -v start="$((line + 1))" '
607+
NR >= start {
608+
value = $0
609+
sub(/\r$/, "", value)
610+
if (value ~ /^[[:space:]]*\[[^]]+\][[:space:]]*$/) {
611+
print NR
612+
exit
613+
}
614+
}
615+
' "$file") || return 1
616+
617+
local insert
618+
if [ -n "$next" ]; then
619+
insert=$((next - 1))
620+
else
621+
insert=$(wc -l < "$file") || return 1
622+
fi
623+
624+
local replacement
625+
replacement=$(mktemp) || return 1
626+
627+
if [[ "$ending" == "crlf" ]]; then
628+
printf '%s\r\n' "$entry" > "$replacement" || {
629+
rm -f -- "$replacement" || :
630+
return 1
631+
}
632+
else
633+
printf '%s\n' "$entry" > "$replacement" || {
634+
rm -f -- "$replacement" || :
635+
return 1
636+
}
637+
fi
638+
639+
if ! sed -i "${insert}r $replacement" "$file"; then
640+
rm -f -- "$replacement" || :
641+
return 1
642+
fi
643+
644+
rm -f -- "$replacement" || return 1
645+
646+
return 0
647+
}
648+
649+
setHiveEntry() {
650+
651+
local file="$1"
652+
local key="$2"
653+
local name="$3"
654+
local entry="$4"
655+
656+
local target="HKCU,\"$key\","
657+
658+
if [ -n "$name" ]; then
659+
target+="\"$name\","
660+
else
661+
target+=","
662+
fi
663+
664+
local -a lines=()
665+
mapfile -t lines < <(
666+
HIVE_TARGET="$target" awk '
667+
BEGIN {
668+
target = toupper(ENVIRON["HIVE_TARGET"])
669+
gsub(/[[:space:]]/, "", target)
670+
}
671+
{
672+
value = $0
673+
sub(/\r$/, "", value)
674+
gsub(/[[:space:]]/, "", value)
675+
if (index(toupper(value), target) == 1) {
676+
print NR
677+
}
678+
}
679+
' "$file"
680+
) || return 1
681+
682+
if (( ${#lines[@]} > 1 )); then
683+
error "Found multiple HIVEDEF.INF entries for \"$key\\$name\"!"
684+
return 1
685+
fi
686+
687+
if (( ${#lines[@]} == 0 )); then
688+
addHiveEntry "$file" "$entry" || return 1
689+
return 0
690+
fi
691+
692+
local replacement
693+
replacement=$(mktemp) || return 1
694+
695+
if LC_ALL=C grep -q $'\r$' "$file"; then
696+
printf '%s\r\n' "$entry" > "$replacement" || {
697+
rm -f -- "$replacement" || :
698+
return 1
699+
}
700+
else
701+
printf '%s\n' "$entry" > "$replacement" || {
702+
rm -f -- "$replacement" || :
703+
return 1
704+
}
705+
fi
706+
707+
if ! sed -i "${lines[0]}r $replacement" "$file" ||
708+
! sed -i "${lines[0]}d" "$file"; then
709+
rm -f -- "$replacement" || :
710+
return 1
711+
fi
712+
713+
rm -f -- "$replacement" || return 1
714+
715+
return 0
716+
}
717+
718+
patchDefaultHive() {
719+
720+
local target="$1"
721+
local driver="$2"
722+
723+
local file
724+
file=$(find "$target" -maxdepth 1 -type f -iname HIVEDEF.INF -print -quit) || return 1
725+
726+
if [ -z "$file" ]; then
727+
error "The file HIVEDEF.INF could not be found!"
728+
return 1
729+
fi
730+
731+
# Apply static user preferences while Setup is constructing the default
732+
# profile, so they are active for the first desktop and inherited by every
733+
# subsequently created user.
734+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\Applets\Tour' 'RunCount' \
735+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\Applets\Tour","RunCount",0x00010001,0' || return 1
736+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'HideFileExt' \
737+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced","HideFileExt",0x00010001,0' || return 1
738+
setHiveEntry "$file" 'Software\Microsoft\Internet Connection Wizard' 'Completed' \
739+
'HKCU,"Software\Microsoft\Internet Connection Wizard","Completed",0x00000000,"1"' || return 1
740+
setHiveEntry "$file" 'Software\Microsoft\Internet Connection Wizard' 'Desktopchanged' \
741+
'HKCU,"Software\Microsoft\Internet Connection Wizard","Desktopchanged",0x00000000,"1"' || return 1
742+
setHiveEntry "$file" 'Control Panel\Desktop' 'SCRNSAVE.EXE' \
743+
'HKCU,"Control Panel\Desktop","SCRNSAVE.EXE",0x00000000,"off"' || return 1
744+
setHiveEntry "$file" 'Control Panel\Desktop' 'ScreenSaveActive' \
745+
'HKCU,"Control Panel\Desktop","ScreenSaveActive",0x00000000,"0"' || return 1
746+
setHiveEntry "$file" 'Control Panel\Desktop' 'DragFullWindows' \
747+
'HKCU,"Control Panel\Desktop","DragFullWindows",0x00000000,"1"' || return 1
748+
setHiveEntry "$file" 'Control Panel\Desktop' 'MenuShowDelay' \
749+
'HKCU,"Control Panel\Desktop","MenuShowDelay",0x00000000,"0"' || return 1
750+
751+
if [[ "$driver" == "2k" ]]; then
752+
setHiveEntry "$file" 'Control Panel\Desktop' 'UserPreferencesMask' \
753+
'HKCU,"Control Panel\Desktop","UserPreferencesMask",0x00000001,9c,32,00,80' || return 1
754+
else
755+
setHiveEntry "$file" 'Control Panel\Desktop' 'UserPreferencesMask' \
756+
'HKCU,"Control Panel\Desktop","UserPreferencesMask",0x00000001,9c,32,07,80' || return 1
757+
fi
758+
759+
setHiveEntry "$file" 'Control Panel\Desktop\WindowMetrics' 'MinAnimate' \
760+
'HKCU,"Control Panel\Desktop\WindowMetrics","MinAnimate",0x00000000,"0"' || return 1
761+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\Explorer' 'link' \
762+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer","link",0x00000001,00,00,00,00' || return 1
763+
764+
if [[ "$driver" == "xp" || "$driver" == "2k3" ]]; then
765+
# Keep the helper in the Windows directory and let each user run it once
766+
# for their own power scheme without adding generated files to C:\OEM.
767+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\RunOnce' 'PowerPolicy' \
768+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\RunOnce","PowerPolicy",0x00020000,"Wscript.exe %%SystemRoot%%\NT5POWER.VBS"' || return 1
769+
fi
770+
771+
if [[ "$driver" == "2k" ]]; then
772+
setHiveEntry "$file" 'Control Panel\PowerCfg' 'CurrentPowerPolicy' \
773+
'HKCU,"Control Panel\PowerCfg","CurrentPowerPolicy",0x00000000,"3"' || return 1
774+
setHiveEntry "$file" 'Control Panel\PowerCfg\PowerPolicies\3' 'Policies' \
775+
'HKCU,"Control Panel\PowerCfg\PowerPolicies\3","Policies",0x00000001,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,32,32,00,00,04,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,64,64,64,64,00,00' || return 1
776+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\Runonce' '^SetupICWDesktop' \
777+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\Runonce","^SetupICWDesktop",0x00000004' || return 1
778+
fi
779+
780+
if [[ "$driver" == "2k" || "$driver" == "2k3" ]]; then
781+
setHiveEntry "$file" 'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' 'NoActiveDesktop' \
782+
'HKCU,"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer","NoActiveDesktop",0x00010001,1' || return 1
783+
fi
784+
785+
if [[ "$driver" == "2k3" ]]; then
786+
setHiveEntry "$file" 'Software\Microsoft\Windows NT\CurrentVersion\srvWiz' '' \
787+
'HKCU,"Software\Microsoft\Windows NT\CurrentVersion\srvWiz",,0x00010001,0' || return 1
788+
fi
789+
790+
return 0
791+
}
792+
584793
disableAutoReboot() {
585794

586795
local target="$1"
@@ -880,18 +1089,10 @@ writeRegistry() {
8801089
'[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List]' \
8811090
'"3389:TCP"="3389:TCP:*:Enabled:@xpsp2res.dll,-22009"' \
8821091
'' \
883-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Tour]' \
884-
'"RunCount"=dword:00000000' \
885-
'' \
886-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]' \
887-
'"HideFileExt"=dword:00000000' \
888-
'' \
8891092
'[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer]' \
8901093
'"NoWelcomeScreen"="1"' \
8911094
'' \
892-
'[HKEY_CURRENT_USER\Software\Microsoft\Internet Connection Wizard]' \
893-
'"Completed"="1"' \
894-
'"Desktopchanged"="1"' '' '[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]'
1095+
'[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]'
8951096

8961097
if disabled "$AUTOLOGIN"; then
8971098
printf '%s\n' '"AutoAdminLogon"="0"'
@@ -920,7 +1121,7 @@ writeRegistry() {
9201121
"\"DefaultSettings.XResolution\"=dword:$XHEX" \
9211122
"\"DefaultSettings.YResolution\"=dword:$YHEX" \
9221123
'' \
923-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce]'
1124+
'[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]'
9241125

9251126
printf '%s\n' "$oem" ''
9261127
} | unix2dos > "$dir/\$OEM\$/install.reg" || return 1
@@ -933,70 +1134,12 @@ appendRegistry() {
9331134
local dir="$1"
9341135
local driver="$2"
9351136

936-
if [[ "$driver" == "2k" || "$driver" == "xp" || "$driver" == "2k3" ]]; then
937-
{
938-
printf '%s\n' \
939-
'[HKEY_CURRENT_USER\Control Panel\Desktop]' \
940-
'"SCRNSAVE.EXE"="off"' \
941-
'"ScreenSaveActive"="0"' \
942-
'"DragFullWindows"="1"' \
943-
'"MenuShowDelay"="0"'
944-
945-
if [[ "$driver" == "2k" ]]; then
946-
printf '%s\n' '"UserPreferencesMask"=hex:9c,32,00,80'
947-
else
948-
printf '%s\n' '"UserPreferencesMask"=hex:9c,32,07,80'
949-
fi
950-
951-
printf '%s\n' \
952-
'' \
953-
'[HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]' \
954-
'"MinAnimate"="0"' \
955-
'' \
956-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]' \
957-
'"link"=hex:00,00,00,00' ''
958-
} | unix2dos >> "$dir/\$OEM\$/install.reg" || return 1
959-
fi
960-
961-
if [[ "$driver" == "xp" || "$driver" == "2k3" ]]; then
962-
{
963-
printf '%s\n' \
964-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce]' \
965-
'"PowerPolicy"="Wscript.exe C:\\OEM\\NT5POWER.VBS"' ''
966-
} | unix2dos >> "$dir/\$OEM\$/install.reg" || return 1
967-
fi
968-
969-
if [[ "$driver" == "2k" ]]; then
970-
{
971-
printf '%s\n' \
972-
'[HKEY_CURRENT_USER\Control Panel\PowerCfg]' \
973-
'"CurrentPowerPolicy"="3"' \
974-
'' \
975-
'[HKEY_CURRENT_USER\Control Panel\PowerCfg\PowerPolicies\3]' \
976-
'"Policies"=hex:01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,32,32,00,00,04,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,64,64,64,64,00,00' \
977-
'' \
978-
'[HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Runonce]' \
979-
'"^SetupICWDesktop"=-' ''
980-
} | unix2dos >> "$dir/\$OEM\$/install.reg" || return 1
981-
fi
982-
983-
if [[ "$driver" == "2k" || "$driver" == "2k3" ]]; then
984-
{
985-
printf '%s\n' \
986-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]' \
987-
'"NoActiveDesktop"=dword:00000001' ''
988-
} | unix2dos >> "$dir/\$OEM\$/install.reg" || return 1
989-
fi
990-
9911137
if [[ "$driver" == "2k3" ]]; then
9921138
{
9931139
printf '%s\n' \
9941140
'[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Reliability]' \
9951141
'"ShutdownReasonOn"=dword:00000000' \
9961142
'' \
997-
'[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\srvWiz]' \
998-
'@=dword:00000000' \
999-
'' \
10001143
'[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ServerOOBE\SecurityOOBE]' \
10011144
'"DontLaunchSecurityOOBE"=dword:00000000' \
10021145
'' \
@@ -1015,7 +1158,7 @@ writeVBS() {
10151158
local shortcut="$3"
10161159
local driver="$4"
10171160
local balloonExe="$dir/\$OEM\$/\$1/Drivers/Balloon/blnsvr.exe"
1018-
local power="$dir/\$OEM\$/\$1/OEM/NT5POWER.VBS"
1161+
local power="$dir/\$OEM\$/\$\$/NT5POWER.VBS"
10191162

10201163
# Locate the built-in Administrator by its RID 500 SID rather than its
10211164
# localized display name, then rename that account to the requested username.
@@ -1119,9 +1262,6 @@ writeVBS() {
11191262
'Set FSO = WScript.CreateObject("Scripting.FileSystemObject")' \
11201263
'PowerCfg = Shell.ExpandEnvironmentStrings("%SystemRoot%\System32\POWERCFG.EXE")' \
11211264
'' \
1122-
'Shell.RegWrite "HKCU\Control Panel\Desktop\SCRNSAVE.EXE", "off", "REG_SZ"' \
1123-
'Shell.RegWrite "HKCU\Control Panel\Desktop\ScreenSaveActive", "0", "REG_SZ"' \
1124-
'' \
11251265
'If FSO.FileExists(PowerCfg) Then' \
11261266
' Policy = 3' \
11271267
' Cmd = Chr(34) & PowerCfg & Chr(34) & " /CHANGE " & Policy & " /NUMERICAL "' \

0 commit comments

Comments
 (0)