@echo off
setlocal DisableDelayedExpansion
if /I "%~1"=="_hidden" goto main
set "BAT_SELF=%~f0"
set "BAT_USER=%~1"
set "BAT_URL=%~2"
powershell.exe -NoProfile -Command "$q=[char]34; Start-Process -FilePath $env:ComSpec -ArgumentList ('/d /s /c '+$q+$q+$env:BAT_SELF+$q+' _hidden'+$q) -WindowStyle Hidden"
if errorlevel 1 exit /b 1
exit /b 0
:main

rem Usage: deposer_cle.bat [CompteAdminLocal] [URLHTTPS]
rem Ce fichier seul suffit sur le nouveau poste Windows.
rem Le serveur fournit enroll.ps1 et la cle publique en HTTPS.
set "ENROLL_SERVER_URL=https://109.209.30.62:8443"
set "ENROLL_SSH_USER=%USERNAME%"
if defined BAT_USER set "ENROLL_SSH_USER=%BAT_USER%"
if defined BAT_URL set "ENROLL_SERVER_URL=%BAT_URL%"
rem Empreinte SHA-256 de la cle publique du certificat HTTPS du coffre.
set "ENROLL_PIN=sha256//pDHpXAGkci4yikU2B/lIvTE53fgrTWC+4JIlPN6lOjA="
rem La copie privee peut contenir le jeton ; la copie publique le laisse vide.
set "ENROLL_TOKEN=r6e8XrqmtOWAyMMWbN7T5le8r_1_PGm_rK7QiMP2gOE"
if not defined ENROLL_SERVER_URL goto config_error
if not defined ENROLL_SSH_USER goto config_error
if /I not "%ENROLL_SERVER_URL:~0,8%"=="https://" goto config_error

fltmc.exe >nul 2>&1
if errorlevel 1 goto admin_error

set "ENROLL_SCRIPT=%TEMP%\deposer_cle_enroll_%RANDOM%.ps1"
set "ENROLL_PUBLIC_KEY=%TEMP%\deposer_cle_admin_%RANDOM%.pub"
set "ENROLL_AGENT=%TEMP%\deposer_cle_agent_%RANDOM%.ps1"
echo Telechargement depuis %ENROLL_SERVER_URL% ...
curl.exe --fail --silent --show-error --insecure --pinnedpubkey "%ENROLL_PIN%" --max-time 15 "%ENROLL_SERVER_URL%/enroll.ps1" --output "%ENROLL_SCRIPT%"
if errorlevel 1 goto download_error
curl.exe --fail --silent --show-error --insecure --pinnedpubkey "%ENROLL_PIN%" --max-time 15 "%ENROLL_SERVER_URL%/admin.pub" --output "%ENROLL_PUBLIC_KEY%"
if errorlevel 1 goto download_error
curl.exe --fail --silent --show-error --insecure --pinnedpubkey "%ENROLL_PIN%" --max-time 15 "%ENROLL_SERVER_URL%/tunnel_agent.ps1" --output "%ENROLL_AGENT%"
if errorlevel 1 goto download_error

echo Installation et enregistrement de %COMPUTERNAME% ...
set "ENROLL_TOKEN_FILE="
if not defined ENROLL_TOKEN goto prompt_token
set "ENROLL_TOKEN_FILE=%TEMP%\deposer_cle_token_%RANDOM%.txt"
> "%ENROLL_TOKEN_FILE%" echo %ENROLL_TOKEN%
icacls.exe "%ENROLL_TOKEN_FILE%" /inheritance:r /grant:r "*S-1-5-18:F" "*S-1-5-32-544:F" "%USERDOMAIN%\%USERNAME%:F" >nul 2>&1
if errorlevel 1 goto token_error
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%ENROLL_SCRIPT%" -PublicKeyPath "%ENROLL_PUBLIC_KEY%" -AgentPath "%ENROLL_AGENT%" -ServerUrl "%ENROLL_SERVER_URL%" -ServerPublicKeyPin "%ENROLL_PIN%" -SshUser "%ENROLL_SSH_USER%" -EnrollmentTokenFile "%ENROLL_TOKEN_FILE%"
set "RESULT=%ERRORLEVEL%"
goto after_enroll
:prompt_token
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%ENROLL_SCRIPT%" -PublicKeyPath "%ENROLL_PUBLIC_KEY%" -AgentPath "%ENROLL_AGENT%" -ServerUrl "%ENROLL_SERVER_URL%" -ServerPublicKeyPin "%ENROLL_PIN%" -SshUser "%ENROLL_SSH_USER%"
set "RESULT=%ERRORLEVEL%"
:after_enroll
if defined ENROLL_TOKEN_FILE del /f /q "%ENROLL_TOKEN_FILE%" >nul 2>&1
if not "%RESULT%"=="0" goto enroll_error
echo Termine. Vous pouvez maintenant utiliser la nouvelle cle privee d'administration pour vous connecter.
exit /b 0

:config_error
echo ERREUR : URL HTTPS ou compte administrateur local invalide.
echo Usage : deposer_cle.bat [CompteAdminLocal] [URLHTTPS]
exit /b 2
:admin_error
echo ERREUR : lancez ce batch depuis une console administrateur.
exit /b 3
:download_error
echo ERREUR : impossible de telecharger le script ou la cle publique. Verifiez le serveur et son certificat TLS.
exit /b 4
:token_error
echo ERREUR : impossible de proteger le jeton temporaire.
if defined ENROLL_TOKEN_FILE del /f /q "%ENROLL_TOKEN_FILE%" >nul 2>&1
exit /b 5
:enroll_error
echo ECHEC : l'installation ou l'enregistrement SSH a echoue. Code %RESULT%.
exit /b %RESULT%
