Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for myaccess.bat

(view source code of myaccess.bat as plain text)

  1. @ECHO OFF
  2. IF "%OS%"=="Windows_NT" (SETLOCAL) ELSE (GOTO Syntax)
  3. IF [%1]==[] GOTO Syntax
  4. IF NOT EXIST "%~1" GOTO Syntax
  5. VER | FIND "Windows NT" >NUL
  6. IF NOT ERRORLEVEL 1 GOTO Syntax
  7. MYTOKEN.EXE >NUL 2>&1
  8. IF ERRORLEVEL 1 GOTO Syntax
  9.  
  10. :: Calculate the length of the specified resource name
  11. CALL :GetLength "%~1"
  12. SET /A Length=Length+1
  13. ECHO.
  14.  
  15. :: Remove trailing backslash
  16. SET Resource=%~1
  17. IF "%Resource:~-1%"=="\" SET Resource=%Resource:~0,-1%
  18.  
  19. :: Check if an UNC resource specified doesn't end in a share name
  20. ECHO.%Resource% | FINDSTR /B "\\" >NUL
  21. IF NOT ERRORLEVEL 1 FOR /F "tokens=1-3 delims=\" %%A IN ('ECHO.%Resource%') DO IF "%%C"=="" GOTO Syntax
  22.  
  23. :: Specify first temporary log file and make sure it is empty
  24. SET TempFile="%Temp%.\_myToken.dat"
  25. IF EXIST %TempFile% DEL %TempFile%
  26.  
  27. :: Use MYTOKEN to store current user's access token in temp file
  28. ECHO My Access Token:
  29. FOR /F "tokens=2 delims=:(" %%A IN ('MYTOKEN ^| FIND ":" ^| FIND /V "Owner : "') DO CALL :Parse "%%~A"
  30. ECHO.
  31.  
  32. :: Specify second temporary log file and make sure it is empty
  33. SET TempFile="%Temp%.\_cAcls.dat"
  34. IF EXIST %TempFile% DEL %TempFile%
  35.  
  36. :: Use CACLS to store resource permissions in temp file
  37. ECHO Resource Permissions:
  38. FOR /F "tokens=* delims=" %%A IN ('CACLS "%Resource%" ^| FIND ":"') DO CALL :Parse "%%~A"
  39. ECHO.
  40.  
  41. :: Check stored access token against resource permissions and show result
  42. ECHO Resultant Access Permissions:
  43. FOR /F "tokens=*" %%A IN ('TYPE "%Temp%.\_myToken.dat"') DO FOR /F "tokens=*" %%a IN ('TYPE "%Temp%.\_cAcls.dat" ^| FIND "%%~A:"') DO ECHO.    %%a
  44. ECHO.
  45.  
  46. :: Clean up temporary files
  47. DEL "%Temp%.\_cAcls.dat"
  48. DEL "%Temp%.\_Length.bat"
  49. DEL "%Temp%.\_myToken.dat"
  50. DEL "%Temp%.\_Parse.bat"
  51.  
  52. :: Done
  53. ENDLOCAL
  54. GOTO:EOF
  55.  
  56.  
  57. :: Calculate the length of the specified string.
  58. :: A temporary batch file is used to enable us
  59. :: to nest variables within variables.
  60. :GetLength
  61. SET Test=%~1
  62. FOR /L %%? IN (1,1,125) DO CALL %COMSPEC% /C IF NOT "%%Test:~%%?,1%%"=="" (>> "%Temp%.\_Length.bat" ECHO SET Length=%%?)
  63. CALL "%Temp%.\_Length.bat"
  64. GOTO:EOF
  65.  
  66.  
  67. :: If line starts with resource name, remove resource name from
  68. :: line then display the result and store it in a temporary file.
  69. :: A temporary batch file is used to enable us to nest variables
  70. :: within variables.
  71. :Parse
  72. SET Line=%~1
  73. IF NOT "%Line:~0,1%"==" " (
  74. 	CALL %COMSPEC% /C ECHO > "%Temp%.\_Parse.bat" SET Line=%%Line:~%Length%%%
  75. 	CALL "%Temp%.\_Parse.bat"
  76. )
  77. CALL :Strip
  78. >> %TempFile% ECHO.%Line%
  79. ECHO.    %Line%
  80. GOTO:EOF
  81.  
  82.  
  83. :: Remove leading spaces
  84. :Strip
  85. IF NOT "%Line:~0,1%"==" " GOTO:EOF
  86. SET Line=%Line:~1%
  87. GOTO :Strip
  88.  
  89.  
  90. :Syntax
  91. ECHO.
  92. ECHO MyAccess.bat,  Version 1.10 for Windows 2000 / XP
  93. ECHO Show current user's access permissions for the specified resource
  94. ECHO.
  95. ECHO Usage:  MYACCESS   resource
  96. ECHO.
  97. ECHO Where:  "resource" can be a file or directory name, or a UNC network
  98. ECHO                    file resource name not ending with a share name
  99. ECHO.
  100. ECHO This utility uses MYTOKEN, the C++ source of which is part of the MS
  101. ECHO Platform SDK: http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
  102. ECHO A compiled version is available in the lab files for Microsoft's course 2154B,
  103. ECHO "Implementing and Administering Microsoft Windows 2000 Directory Services".
  104.  
  105. VER | FIND "Windows NT" >NUL
  106. IF ERRORLEVEL 1 GOTO SkipNT4
  107. ECHO The batch file also uses redirection of CACLS, which is not possible in NT 4.
  108. ECHO To use this batch file in NT 4, modify it to use XCACLS from the Resource Kit.
  109.  
  110. :SkipNT4
  111. ECHO.
  112. ECHO Written by Rob van der Woude
  113. ECHO http://www.robvanderwoude.com
  114.  

page last uploaded: 2017-08-21, 14:26