Skip to content

Commit

Permalink
Windows cmd scripts to change hosts permissions
Browse files Browse the repository at this point in the history
Two windows cmd scripts that can work together to change permissions for
the current user to modify the hosts file.

win-hosts-check.cmd check if the permissions on the file are sufficient.

If not it will call win-hosts-set.cmd which will trigger the windows
permission dialog asking for enough rights to midify the permissions on
the hosts file for the current user.

See issue agiledivider#40
  • Loading branch information
tkdb committed May 9, 2014
1 parent 037cc44 commit fa9ddf4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cmd/win-hosts-check.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@ECHO off
SETLOCAL ENABLEEXTENSIONS
REM modify hosts access rights current user can change the file contents

call :hostfileperms _perms
echo Permissins on hosts for user %USERNAME%: %_perms%

If "%_perms%" == "F" goto :PERM_OK
If "%_perms%" == "M" goto :PERM_OK
If "%_perms%" == "RX,W" goto :PERM_OK
If "%_perms%" == "R,W" goto :PERM_OK
If "%_perms%" == "W" goto :PERM_OK

If "%_perms%" == ":" goto :PERM_INSUFFICIENT
If "%_perms%" == "R" goto :PERM_INSUFFICIENT
If "%_perms%" == "RX" goto :PERM_INSUFFICIENT

echo Unhandled "%_perms%", raising.
goto :PERM_INSUFFICIENT

:PERM_INSUFFICIENT
echo|set /p=Raising permissions...
call win-hosts-set.cmd
echo|set /p=done.
call :hostfileperms _perms_new
echo New permissins are: %_perms_new%
IF "%_perms%" == "%_perms_new%" (
echo Changing permissions failed. Exiting.
exit /b 1
)

:PERM_OK
echo Suffice. Exiting.
goto :EOF

:hostfileperms
setlocal
set _icacls_cmd=Icacls %SystemRoot%\system32\drivers\etc\hosts /T
FOR /f "tokens=2" %%G IN ('%_icacls_cmd% ^|find "%USERNAME%"') DO set _permission_info=%%G
set "_permission_perms=%_permission_info:*:=%"
set _permission=%_permission_perms:~1,-1%

( endlocal
set "%1=%_permission%"
)
exit /b
13 changes: 13 additions & 0 deletions cmd/win-hosts-set.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
cscript //E:JScript //nologo "%~f0"
@goto :EOF
)
CACLS %SystemRoot%\system32\drivers\etc\hosts /E /G %USERNAME%:W
@goto :EOF
@end @ELSE
ShA=new ActiveXObject("Shell.Application")
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",5);
WScript.Sleep(500)
@end

0 comments on commit fa9ddf4

Please sign in to comment.